Skip to content

Commit 17e0bf9

Browse files
andriymoroz-mlnxlguohan
authored andcommitted
Port speed set utility (sonic-net#174)
* Add portconfig utility Signed-off-by: Andriy Moroz <[email protected]> * Fix units conversion in scripts/intfutil Signed-off-by: Andriy Moroz <[email protected]> * Remove old script Signed-off-by: Andriy Moroz <[email protected]>
1 parent 495584b commit 17e0bf9

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

config/main.py

+13
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,19 @@ def startup(interface_name, verbose):
371371
command = "ip link set {} up".format(interface_name)
372372
run_command(command, display_cmd=verbose)
373373

374+
#
375+
# 'speed' subcommand
376+
#
377+
378+
@interface.command()
379+
@click.argument('interface_name', metavar='<interface_name>', required=True)
380+
@click.argument('interface_speed', metavar='<interface_speed>', required=True)
381+
@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output")
382+
def speed(interface_name, interface_speed, verbose):
383+
"""Set interface speed"""
384+
command = "portconfig -p {} -s {}".format(interface_name, interface_speed)
385+
if verbose: command += " -vv"
386+
run_command(command, display_cmd=verbose)
374387

375388
#
376389
# 'acl' group

scripts/intfutil

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def db_port_status_get(db, intf_name, status_type):
5454
return "N/A"
5555

5656
if status_type == PORT_SPEED and status != "N/A":
57-
status = '{}G'.format(status[:2] if int(status) < 100000 else status[:3])
57+
status = '{}G'.format(status[:-3])
5858

5959
return status
6060

scripts/portconfig

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python
2+
"""
3+
portconfig is the utility to show and change ECN configuration
4+
5+
usage: portconfig [-h] [-v] [-s] [-p PROFILE] [-gmin GREEN_MIN]
6+
[-gmax GREEN_MAX] [-ymin YELLOW_MIN] [-ymax YELLOW_MAX]
7+
[-rmin RED_MIN] [-rmax RED_MAX] [-vv]
8+
9+
optional arguments:
10+
-h --help show this help message and exit
11+
-v --version show program's version number and exit
12+
-vv --verbose verbose output
13+
-p' --port port name
14+
-s --speed port speed in Mbits
15+
"""
16+
from __future__ import print_function
17+
18+
import os
19+
import sys
20+
import json
21+
import argparse
22+
import swsssdk
23+
24+
PORT_TABLE_NAME = "PORT"
25+
PORT_SPEED_CONFIG_FIELD_NAME = "speed"
26+
27+
class portconfig(object):
28+
"""
29+
Process aclstat
30+
"""
31+
def __init__(self, verbose, port):
32+
self.verbose = verbose
33+
34+
# Set up db connections
35+
self.db = swsssdk.ConfigDBConnector()
36+
self.db.connect()
37+
# check whether table for this port exists
38+
port_tables = self.db.get_table(PORT_TABLE_NAME)
39+
if not port_tables.has_key(port):
40+
raise Exception("Invalid port specified")
41+
42+
def list_params(self, port):
43+
# chack whether table for this port exists
44+
port_tables = self.db.get_table(PORT_TABLE_NAME)
45+
if port_tables.has_key(port):
46+
print(port_tables[port])
47+
48+
def set_speed(self, port, speed):
49+
if self.verbose:
50+
print("Setting speed %s on port %s" % (speed, port))
51+
self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_SPEED_CONFIG_FIELD_NAME: speed})
52+
53+
def main():
54+
parser = argparse.ArgumentParser(description='Set SONiC port parameters',
55+
version='1.0.0',
56+
formatter_class=argparse.RawTextHelpFormatter)
57+
parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None)
58+
parser.add_argument('-l', '--list', action='store_true', help='list port parametars', default=False)
59+
parser.add_argument('-s', '--speed', type=int, help='port speed value in Mbit', default=None)
60+
parser.add_argument('-vv', '--verbose', action='store_true', help='Verbose output', default=False)
61+
args = parser.parse_args()
62+
63+
try:
64+
port = portconfig(args.verbose, args.port)
65+
if args.list:
66+
port.list_params(args.port)
67+
elif args.speed:
68+
port.set_speed(args.port, args.speed)
69+
else:
70+
parser.print_help()
71+
sys.exit(1)
72+
73+
except Exception as e:
74+
print(e.message, file=sys.stderr)
75+
sys.exit(1)
76+
77+
if __name__ == "__main__":
78+
main()

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def get_test_suite():
4747
'scripts/intfutil',
4848
'scripts/lldpshow',
4949
'scripts/port2alias',
50+
'scripts/portconfig',
5051
'scripts/portstat',
5152
'scripts/teamshow'
5253
],

0 commit comments

Comments
 (0)