From 17e0bf9be5f16ba301f30d3354e7554b49fd61e5 Mon Sep 17 00:00:00 2001 From: Andriy Moroz Date: Tue, 16 Jan 2018 10:45:35 +0200 Subject: [PATCH] Port speed set utility (#174) * Add portconfig utility Signed-off-by: Andriy Moroz * Fix units conversion in scripts/intfutil Signed-off-by: Andriy Moroz * Remove old script Signed-off-by: Andriy Moroz --- config/main.py | 13 ++++++++ scripts/intfutil | 2 +- scripts/portconfig | 78 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100755 scripts/portconfig diff --git a/config/main.py b/config/main.py index c101a445a1b9..ebe64169a4b2 100755 --- a/config/main.py +++ b/config/main.py @@ -371,6 +371,19 @@ def startup(interface_name, verbose): command = "ip link set {} up".format(interface_name) run_command(command, display_cmd=verbose) +# +# 'speed' subcommand +# + +@interface.command() +@click.argument('interface_name', metavar='', required=True) +@click.argument('interface_speed', metavar='', required=True) +@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output") +def speed(interface_name, interface_speed, verbose): + """Set interface speed""" + command = "portconfig -p {} -s {}".format(interface_name, interface_speed) + if verbose: command += " -vv" + run_command(command, display_cmd=verbose) # # 'acl' group diff --git a/scripts/intfutil b/scripts/intfutil index 6c44a74fdfce..07fa734b9c99 100755 --- a/scripts/intfutil +++ b/scripts/intfutil @@ -54,7 +54,7 @@ def db_port_status_get(db, intf_name, status_type): return "N/A" if status_type == PORT_SPEED and status != "N/A": - status = '{}G'.format(status[:2] if int(status) < 100000 else status[:3]) + status = '{}G'.format(status[:-3]) return status diff --git a/scripts/portconfig b/scripts/portconfig new file mode 100755 index 000000000000..2505ae971c06 --- /dev/null +++ b/scripts/portconfig @@ -0,0 +1,78 @@ +#!/usr/bin/python +""" +portconfig is the utility to show and change ECN configuration + +usage: portconfig [-h] [-v] [-s] [-p PROFILE] [-gmin GREEN_MIN] + [-gmax GREEN_MAX] [-ymin YELLOW_MIN] [-ymax YELLOW_MAX] + [-rmin RED_MIN] [-rmax RED_MAX] [-vv] + +optional arguments: + -h --help show this help message and exit + -v --version show program's version number and exit + -vv --verbose verbose output + -p' --port port name + -s --speed port speed in Mbits +""" +from __future__ import print_function + +import os +import sys +import json +import argparse +import swsssdk + +PORT_TABLE_NAME = "PORT" +PORT_SPEED_CONFIG_FIELD_NAME = "speed" + +class portconfig(object): + """ + Process aclstat + """ + def __init__(self, verbose, port): + self.verbose = verbose + + # Set up db connections + self.db = swsssdk.ConfigDBConnector() + self.db.connect() + # check whether table for this port exists + port_tables = self.db.get_table(PORT_TABLE_NAME) + if not port_tables.has_key(port): + raise Exception("Invalid port specified") + + def list_params(self, port): + # chack whether table for this port exists + port_tables = self.db.get_table(PORT_TABLE_NAME) + if port_tables.has_key(port): + print(port_tables[port]) + + def set_speed(self, 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}) + +def main(): + parser = argparse.ArgumentParser(description='Set SONiC port parameters', + 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('-s', '--speed', type=int, help='port speed value in Mbit', default=None) + parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False) + args = parser.parse_args() + + try: + port = portconfig(args.verbose, args.port) + if args.list: + port.list_params(args.port) + elif args.speed: + port.set_speed(args.port, args.speed) + else: + parser.print_help() + sys.exit(1) + + except Exception as e: + print(e.message, file=sys.stderr) + sys.exit(1) + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index bc23ad9d2961..bec645946b02 100644 --- a/setup.py +++ b/setup.py @@ -47,6 +47,7 @@ def get_test_suite(): 'scripts/intfutil', 'scripts/lldpshow', 'scripts/port2alias', + 'scripts/portconfig', 'scripts/portstat', 'scripts/teamshow' ],