Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix portconfig portchannel 201911 #1384

Open
wants to merge 2 commits into
base: 201911
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions scripts/portconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import argparse
import swsssdk

PORT_TABLE_NAME = "PORT"
PORTCHANNEL_TABLE_NAME = "PORTCHANNEL"
PORT_PREFIX_ETHERNET = "Ethernet"
PORT_PREFIX_PORTCHANNEL = "PortChannel"
PORT_SPEED_CONFIG_FIELD_NAME = "speed"
PORT_FEC_CONFIG_FIELD_NAME = "fec"
PORT_MTU_CONFIG_FIELD_NAME = "mtu"


class portconfig(object):
"""
Process aclstat
Process portconfig
"""
def __init__(self, verbose, port, namespace):

def __init__(self, verbose, table_name, port, namespace):
self.verbose = verbose

# Set up db connections
Expand All @@ -44,59 +49,67 @@ class portconfig(object):
self.db.connect()

# check whether table for this port exists
port_tables = self.db.get_table(PORT_TABLE_NAME)
port_tables = self.db.get_table(table_name)
if not port_tables.has_key(port):
raise Exception("Invalid port specified")

def list_params(self, port):
def list_params(self, table_name, port):
# chack whether table for this port exists
port_tables = self.db.get_table(PORT_TABLE_NAME)
port_tables = self.db.get_table(table_name)
if port_tables.has_key(port):
print(port_tables[port])

def set_speed(self, port, speed):
def set_speed(self, table_name, port, speed):
if self.verbose:
print("Setting speed %s on port %s" % (speed, port))
self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_SPEED_CONFIG_FIELD_NAME: speed})
self.db.mod_entry(table_name, port, {PORT_SPEED_CONFIG_FIELD_NAME: speed})

def set_fec(self, port, fec):
def set_fec(self, table_name, port, fec):
if self.verbose:
print("Setting fec %s on port %s" % (fec, port))
self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_FEC_CONFIG_FIELD_NAME: fec})
self.db.mod_entry(table_name, port, {PORT_FEC_CONFIG_FIELD_NAME: fec})

def set_mtu(self, port, mtu):
def set_mtu(self, table_name, port, mtu):
if self.verbose:
print("Setting mtu %s on port %s" % (mtu, port))
self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_MTU_CONFIG_FIELD_NAME: mtu})
self.db.mod_entry(table_name, port, {PORT_MTU_CONFIG_FIELD_NAME: mtu})


def main():
parser = argparse.ArgumentParser(description='Set SONiC port parameters',
version='1.0.0',
formatter_class=argparse.RawTextHelpFormatter)
version='1.0.0',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None)
parser.add_argument('-l', '--list', action='store_true', help='list port parametars', default=False)
parser.add_argument('-l', '--list', action='store_true', help='list port parameters', default=False)
parser.add_argument('-s', '--speed', type=int, help='port speed value in Mbit', default=None)
parser.add_argument('-f', '--fec', type=str, help='port fec mode value in (none, rs, fc)', default=None)
parser.add_argument('-m', '--mtu', type=int, help='port mtu value in bytes', default=None)
parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False)
parser.add_argument('-n', '--namespace', metavar='namespace details', type = str, required = False,
help = 'The asic namespace whose DB instance we need to connect', default=None)
parser.add_argument('-n', '--namespace', metavar='namespace details', type=str, required=False,
help='The asic namespace whose DB instance we need to connect', default=None)
args = parser.parse_args()

if args.namespace is not None:
swsssdk.SonicDBConfig.load_sonic_global_db_config(namespace=args.namespace)

try:
port = portconfig(args.verbose, args.port, args.namespace)
if args.port.startswith(PORT_PREFIX_ETHERNET):
port_table_name = PORT_TABLE_NAME
elif args.port.startswith(PORT_PREFIX_PORTCHANNEL):
port_table_name = PORTCHANNEL_TABLE_NAME
else:
raise Exception("Invalid port type specified")

port = portconfig(args.verbose, port_table_name, args.port, args.namespace)
if args.list:
port.list_params(args.port)
port.list_params(port_table_name, args.port)
elif args.speed or args.fec or args.mtu:
if args.speed:
port.set_speed(args.port, args.speed)
port.set_speed(port_table_name, args.port, args.speed)
if args.fec:
port.set_fec(args.port, args.fec)
port.set_fec(port_table_name, args.port, args.fec)
if args.mtu:
port.set_mtu(args.port, args.mtu)
port.set_mtu(port_table_name, args.port, args.mtu)
else:
parser.print_help()
sys.exit(1)
Expand All @@ -105,5 +118,6 @@ def main():
print(e.message, file=sys.stderr)
sys.exit(1)


if __name__ == "__main__":
main()