Skip to content

Commit

Permalink
Port speed set utility (sonic-net#174)
Browse files Browse the repository at this point in the history
* Add portconfig utility

Signed-off-by: Andriy Moroz <c_andriym@mellanox.com>

* Fix units conversion in scripts/intfutil

Signed-off-by: Andriy Moroz <c_andriym@mellanox.com>

* Remove old script

Signed-off-by: Andriy Moroz <c_andriym@mellanox.com>
  • Loading branch information
andriymoroz-mlnx authored and lguohan committed Jan 16, 2018
1 parent 495584b commit 17e0bf9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
13 changes: 13 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='<interface_name>', required=True)
@click.argument('interface_speed', metavar='<interface_speed>', 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
Expand Down
2 changes: 1 addition & 1 deletion scripts/intfutil
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
78 changes: 78 additions & 0 deletions scripts/portconfig
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def get_test_suite():
'scripts/intfutil',
'scripts/lldpshow',
'scripts/port2alias',
'scripts/portconfig',
'scripts/portstat',
'scripts/teamshow'
],
Expand Down

0 comments on commit 17e0bf9

Please sign in to comment.