Skip to content

Commit c237619

Browse files
committed
Fix all review comments and support python 3
Review comments: - Add testcases for show and config command - Address review comments in db_migrator - Use "size" to represent the size of the PG and "headroom" for xoff - Fix typo Signed-off-by: Stephen Sun <[email protected]>
1 parent 0685448 commit c237619

9 files changed

Lines changed: 231 additions & 45 deletions

File tree

config/main.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ def reload(ctx, no_dynamic_buffer):
17041704
), fg="yellow")
17051705

17061706
if buffer_model_updated:
1707-
print "Buffer calculation model updated, restarting swss is required to take effect"
1707+
print("Buffer calculation model updated, restarting swss is required to take effect")
17081708

17091709
def is_dynamic_buffer_enabled(config_db):
17101710
"""Return whether the current system supports dynamic buffer calculation"""
@@ -2584,7 +2584,7 @@ def pgmaps_check_legality(ctx, interface_name, input_pg, is_new_pg):
25842584
ctx.fail("PG {} doesn't exist".format(input_pg))
25852585
return
25862586

2587-
for k, v in existing_pgs.iteritems():
2587+
for k, v in existing_pgs.items():
25882588
port, existing_pg = k
25892589
if port == interface_name:
25902590
existing_lower = int(existing_pg[0])
@@ -2633,7 +2633,7 @@ def remove_pg_on_port(ctx, interface_name, pg_map):
26332633
# Remvoe all dynamic lossless PGs on the port
26342634
existing_pgs = config_db.get_table("BUFFER_PG")
26352635
removed = False
2636-
for k, v in existing_pgs.iteritems():
2636+
for k, v in existing_pgs.items():
26372637
port, existing_pg = k
26382638
if port == interface_name and (not pg_map or pg_map == existing_pg):
26392639
need_to_remove = False
@@ -3372,7 +3372,7 @@ def priority(ctx, interface_name, priority, status):
33723372
clicommon.run_command("pfc config priority {0} {1} {2}".format(status, interface_name, priority))
33733373

33743374
#
3375-
# 'buffer_profile' group ('config buffer_profile ...')
3375+
# 'buffer' group ('config buffer ...')
33763376
#
33773377

33783378
@config.group(cls=clicommon.AbbreviationGroup)
@@ -3400,11 +3400,11 @@ def profile(ctx):
34003400
@click.option('--size', metavar='<size>', type=int, help="Set reserved size size")
34013401
@click.option('--dynamic_th', metavar='<dynamic_th>', type=str, help="Set dynamic threshold")
34023402
@click.option('--pool', metavar='<pool>', type=str, help="Buffer pool")
3403-
@click.pass_context
3404-
def add_profile(ctx, profile, xon, xoff, size, dynamic_th, pool):
3403+
@clicommon.pass_db
3404+
def add_profile(db, profile, xon, xoff, size, dynamic_th, pool):
34053405
"""Add or modify a buffer profile"""
3406-
config_db = ConfigDBConnector()
3407-
config_db.connect()
3406+
config_db = db.cfgdb
3407+
ctx = click.get_current_context()
34083408

34093409
profile_entry = config_db.get_entry('BUFFER_PROFILE', profile)
34103410
if profile_entry:
@@ -3420,11 +3420,11 @@ def add_profile(ctx, profile, xon, xoff, size, dynamic_th, pool):
34203420
@click.option('--size', metavar='<size>', type=int, help="Set reserved size size")
34213421
@click.option('--dynamic_th', metavar='<dynamic_th>', type=str, help="Set dynamic threshold")
34223422
@click.option('--pool', metavar='<pool>', type=str, help="Buffer pool")
3423-
@click.pass_context
3424-
def set_profile(ctx, profile, xon, xoff, size, dynamic_th, pool):
3423+
@clicommon.pass_db
3424+
def set_profile(db, profile, xon, xoff, size, dynamic_th, pool):
34253425
"""Add or modify a buffer profile"""
3426-
config_db = ConfigDBConnector()
3427-
config_db.connect()
3426+
config_db = db.cfgdb
3427+
ctx = click.get_current_context()
34283428

34293429
profile_entry = config_db.get_entry('BUFFER_PROFILE', profile)
34303430
if not profile_entry:
@@ -3497,15 +3497,15 @@ def update_profile(ctx, config_db, profile_name, xon, xoff, size, dynamic_th, po
34973497

34983498
@profile.command('remove')
34993499
@click.argument('profile', metavar='<profile>', required=True)
3500-
@click.pass_context
3501-
def remove_profile(ctx, profile):
3500+
@clicommon.pass_db
3501+
def remove_profile(db, profile):
35023502
"""Delete a buffer profile"""
3503-
config_db = ConfigDBConnector()
3504-
config_db.connect()
3503+
config_db = db.cfgdb
3504+
ctx = click.get_current_context()
35053505

35063506
full_profile_name = '[BUFFER_PROFILE|{}]'.format(profile)
35073507
existing_pgs = config_db.get_table("BUFFER_PG")
3508-
for k, v in existing_pgs.iteritems():
3508+
for k, v in existing_pgs.items():
35093509
port, pg = k
35103510
referenced_profile = v.get('profile')
35113511
if referenced_profile and referenced_profile == full_profile_name:

doc/Command-Reference.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,18 +2365,18 @@ This command is used to configure a lossless buffer profile.
23652365
- Usage:
23662366
23672367
```
2368-
config buffer_profile add <profile_name> -xon <xon_threshold> -xoff <xoff_threshold> [-headroom <headroom_size>] [-dynamic_th <dynamic_th_value>] [-pool <ingress_lossless_pool_name>]
2369-
config buffer_profile set <profile_name> -xon <xon_threshold> -xoff <xoff_threshold> [-headroom <headroom_size>] [-dynamic_th <dynamic_th_value>] [-pool <ingress_lossless_pool_name>]
2368+
config buffer_profile add <profile_name> -xon <xon_threshold> -xoff <xoff_threshold> [-size <size>] [-dynamic_th <dynamic_th_value>] [-pool <ingress_lossless_pool_name>]
2369+
config buffer_profile set <profile_name> -xon <xon_threshold> -xoff <xoff_threshold> [-size <size>] [-dynamic_th <dynamic_th_value>] [-pool <ingress_lossless_pool_name>]
23702370
config buffer_profile remove <profile_name>
23712371
```
23722372
23732373
All the parameters are devided to two groups, one for headroom and one for dynamic_th. For any command at lease one group of parameters should be provided.
23742374
For headroom parameters:
23752375
2376-
- At lease one of `xoff` and `headroom` should be provided and the other will be optional and conducted via the formula `xon + xoff = headroom`.
2376+
- At lease one of `xoff` and `size` should be provided and the other will be optional and conducted via the formula `xon + xoff = size`.
23772377
All other parameters are optional.
23782378
- `xon` is madantory.
2379-
- `xon` + `xoff` <= `headroom`; For Mellanox platform xon + xoff == headroom
2379+
- `xon` + `xoff` <= `size`; For Mellanox platform xon + xoff == size
23802380
23812381
If only headroom parameters are provided, the `dynamic_th` will be taken from `CONFIG_DB.DEFAULT_LOSSLESS_BUFFER_PARAMETER.default_dynamic_th`.
23822382
@@ -2578,20 +2578,20 @@ This command is used to display the status of buffer pools and profiles currentl
25782578
---------- --------------------------------
25792579
```
25802580
2581-
**show buffer configure**
2581+
**show buffer configuration**
25822582
25832583
This command is used to display the status of buffer pools and profiles currently configured.
25842584
25852585
- Usage:
25862586
25872587
```
2588-
show buffer configure
2588+
show buffer configuration
25892589
```
25902590
25912591
- Example:
25922592
25932593
```
2594-
admin@sonic:~$ show buffer configure
2594+
admin@sonic:~$ show buffer configuration
25952595
Pool: ingress_lossless_pool
25962596
---- --------
25972597
type ingress

scripts/db_migrator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def migrate_config_db_buffer_tables_for_dynamic_calculation(self, speed_list, ca
188188
'''
189189
# Migrate BUFFER_PROFILEs, removing dynamically generated profiles
190190
dynamic_profile = self.configDB.get_table('BUFFER_PROFILE')
191-
profile_pattern = 'pg_lossless_([0-9]*000)_([0-9]*m)_profile'
192-
for name, info in dynamic_profile.iteritems():
191+
profile_pattern = 'pg_lossless_([1-9][0-9]*000)_([1-9][0-9]*m)_profile'
192+
for name, info in dynamic_profile.items():
193193
m = re.search(profile_pattern, name)
194194
if not m:
195195
continue
@@ -212,12 +212,12 @@ def migrate_config_db_buffer_tables_for_dynamic_calculation(self, speed_list, ca
212212
ports = self.configDB.get_table('PORT')
213213
all_cable_lengths = self.configDB.get_table('CABLE_LENGTH')
214214
if not buffer_pgs or not ports or not all_cable_lengths:
215-
log.log_notice("At lease one of tables BUFFER_PG, PORT and CABLE_LENGTH hasn't been defined, skip following mitration")
215+
log.log_notice("At lease one of tables BUFFER_PG, PORT and CABLE_LENGTH hasn't been defined, skip following migration")
216216
abandon_method()
217217
return True
218218

219-
cable_lengths = all_cable_lengths[all_cable_lengths.keys()[0]]
220-
for name, profile in buffer_pgs.iteritems():
219+
cable_lengths = all_cable_lengths[list(all_cable_lengths.keys())[0]]
220+
for name, profile in buffer_pgs.items():
221221
# do the db migration
222222
port, pg = name
223223
if pg != '3-4':
@@ -278,7 +278,7 @@ def prepare_dynamic_buffer_for_warm_reboot(self, buffer_pools = None, buffer_pro
278278
warmreboot_state = self.stateDB.get(self.stateDB.STATE_DB, 'WARM_RESTART_ENABLE_TABLE|system', 'enable')
279279
mmu_size = self.stateDB.get(self.stateDB.STATE_DB, 'BUFFER_MAX_PARAM_TABLE|global', 'mmu_size')
280280
if warmreboot_state == 'true' and not mmu_size:
281-
log.log_notice("This is the very first run of buffermgrd (dynamc), prepare info requred from warm reboot")
281+
log.log_notice("This is the very first run of buffermgrd (dynamic), prepare info required from warm reboot")
282282
else:
283283
return True
284284

@@ -298,7 +298,7 @@ def prepare_dynamic_buffer_for_warm_reboot(self, buffer_pools = None, buffer_pro
298298
app_table_name = table_name + "_TABLE"
299299
if not entries:
300300
entries = self.configDB.get_table(table_name)
301-
for key, items in entries.iteritems():
301+
for key, items in entries.items():
302302
# copy items to appl db
303303
if reference_field_name:
304304
confdb_ref = items.get(reference_field_name)
@@ -328,7 +328,7 @@ def prepare_dynamic_buffer_for_warm_reboot(self, buffer_pools = None, buffer_pro
328328
appl_db_key = app_table_name + ':' + ':'.join(key)
329329
else:
330330
appl_db_key = app_table_name + ':' + key
331-
for field, data in items.iteritems():
331+
for field, data in items.items():
332332
self.appDB.set(self.appDB.APPL_DB, appl_db_key, field, data)
333333

334334
if keys_copied:

scripts/mmuconfig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ BUFFER_PROFILE_FIELDS = {
2929
"alpha": DYNAMIC_THRESHOLD
3030
}
3131

32+
# mock the redis for unit test purposes #
33+
try:
34+
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
35+
modules_path = os.path.join(os.path.dirname(__file__), "..")
36+
tests_path = os.path.join(modules_path, "tests")
37+
sys.path.insert(0, modules_path)
38+
sys.path.insert(0, tests_path)
39+
import mock_tables.dbconnector
40+
41+
except KeyError:
42+
pass
3243

3344
class MmuConfig(object):
3445
def __init__(self, verbose, config):
@@ -111,7 +122,6 @@ class MmuConfig(object):
111122
def main(config):
112123
if config:
113124
parser = argparse.ArgumentParser(description='Show and change: mmu configuration',
114-
version='1.0.0',
115125
formatter_class=argparse.RawTextHelpFormatter)
116126

117127
parser.add_argument('-l', '--list', action='store_true', help='show mmu configuration')
@@ -120,7 +130,6 @@ def main(config):
120130
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0')
121131
else:
122132
parser = argparse.ArgumentParser(description='Show buffer state',
123-
version='1.0.0',
124133
formatter_class=argparse.RawTextHelpFormatter)
125134

126135
parser.add_argument('-l', '--list', action='store_true', help='show buffer state')

show/main.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,8 +1437,7 @@ def boot():
14371437
def mmu():
14381438
"""Show mmu configuration"""
14391439
cmd = "mmuconfig -l"
1440-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, text=True)
1441-
click.echo(proc.stdout.read())
1440+
run_command(cmd)
14421441

14431442
#
14441443
# 'buffer' command ("show buffer")
@@ -1455,8 +1454,7 @@ def buffer():
14551454
def configuration():
14561455
"""show buffer configuration"""
14571456
cmd = "mmuconfig -l"
1458-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
1459-
click.echo(proc.stdout.read())
1457+
run_command(cmd)
14601458

14611459
#
14621460
# 'information' command ("show buffer state")
@@ -1465,8 +1463,7 @@ def configuration():
14651463
def information():
14661464
"""show buffer information"""
14671465
cmd = "buffershow -l"
1468-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
1469-
click.echo(proc.stdout.read())
1466+
run_command(cmd)
14701467

14711468

14721469
#
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
show_buffer_configuration="""\
2+
Pool: egress_lossless_pool
3+
---- --------
4+
mode dynamic
5+
size 13945824
6+
type egress
7+
---- --------
8+
9+
Pool: egress_lossy_pool
10+
---- -------
11+
mode dynamic
12+
type egress
13+
---- -------
14+
15+
Pool: ingress_lossless_pool
16+
---- -------
17+
mode dynamic
18+
type ingress
19+
---- -------
20+
21+
Pool: ingress_lossy_pool
22+
---- -------
23+
mode dynamic
24+
type ingress
25+
---- -------
26+
27+
Profile: ingress_lossy_profile
28+
---------- --------------------------------
29+
dynamic_th 3
30+
pool [BUFFER_POOL|ingress_lossy_pool]
31+
size 0
32+
---------- --------------------------------
33+
34+
Profile: headroom_profile
35+
---------- -----------------------------------
36+
dynamic_th 0
37+
pool [BUFFER_POOL|ingress_lossless_pool]
38+
xon 18432
39+
xoff 32768
40+
size 51200
41+
---------- -----------------------------------
42+
43+
Profile: alpha_profile
44+
------------- -----------------------------------
45+
dynamic_th 0
46+
pool [BUFFER_POOL|ingress_lossless_pool]
47+
headroom_type dynamic
48+
------------- -----------------------------------
49+
50+
"""
51+
52+
show_buffer_information_output="""\
53+
Pool: egress_lossless_pool
54+
---- --------
55+
mode dynamic
56+
size 13945824
57+
type egress
58+
---- --------
59+
60+
Pool: egress_lossy_pool
61+
---- -------
62+
mode dynamic
63+
size 4580864
64+
type egress
65+
---- -------
66+
67+
Pool: ingress_lossless_pool
68+
---- -------
69+
mode dynamic
70+
size 4580864
71+
type ingress
72+
---- -------
73+
74+
Pool: ingress_lossy_pool
75+
---- -------
76+
mode dynamic
77+
size 4580864
78+
type ingress
79+
---- -------
80+
81+
Profile: ingress_lossy_profile
82+
---------- --------------------------------------
83+
dynamic_th 3
84+
pool [BUFFER_POOL_TABLE|ingress_lossy_pool]
85+
size 0
86+
---------- --------------------------------------
87+
88+
Profile: headroom_profile
89+
---------- -----------------------------------------
90+
dynamic_th 0
91+
pool [BUFFER_POOL_TABLE|ingress_lossless_pool]
92+
xon 18432
93+
xoff 32768
94+
size 51200
95+
---------- -----------------------------------------
96+
97+
"""
98+
99+
testData = {
100+
'show_buffer_configuration' : [ {'cmd' : ['buffer', 'configuration'],
101+
'rc_output': show_buffer_configuration
102+
}
103+
],
104+
'show_buffer_information' : [ {'cmd' : ['buffer', 'information'],
105+
'rc_output': show_buffer_information_output
106+
}
107+
]
108+
}

0 commit comments

Comments
 (0)