@@ -18,17 +18,23 @@ optional arguments:
18
18
19
19
import os
20
20
import sys
21
- import argparse
21
+ import click
22
22
import tabulate
23
23
import traceback
24
24
import json
25
+ from utilities_common .general import load_db_config
26
+ from sonic_py_common import multi_asic
27
+ from utilities_common import multi_asic as multi_asic_util
25
28
26
29
BUFFER_POOL_TABLE_NAME = "BUFFER_POOL"
27
30
BUFFER_PROFILE_TABLE_NAME = "BUFFER_PROFILE"
28
31
DEFAULT_LOSSLESS_BUFFER_PARAMETER_NAME = "DEFAULT_LOSSLESS_BUFFER_PARAMETER"
29
32
30
33
DYNAMIC_THRESHOLD = "dynamic_th"
34
+ DYNAMIC_THRESHOLD_MIN = - 8
35
+ DYNAMIC_THRESHOLD_MAX = 8
31
36
STATIC_THRESHOLD = "static_th"
37
+ STATIC_THRESHOLD_MIN = 0
32
38
BUFFER_PROFILE_FIELDS = {
33
39
"alpha" : DYNAMIC_THRESHOLD ,
34
40
"staticth" : STATIC_THRESHOLD
42
48
sys .path .insert (0 , modules_path )
43
49
sys .path .insert (0 , tests_path )
44
50
import mock_tables .dbconnector
51
+ if os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] == "multi_asic" :
52
+ import mock_tables .mock_multi_asic
53
+ mock_tables .dbconnector .load_namespace_config ()
54
+ else :
55
+ mock_tables .dbconnector .load_database_config ()
45
56
46
57
except KeyError :
47
58
pass
48
59
49
60
from swsscommon .swsscommon import SonicV2Connector , ConfigDBConnector
50
61
51
62
class MmuConfig (object ):
52
- def __init__ (self , verbose , config , filename ):
63
+ def __init__ (self , verbose , config , filename , namespace ):
53
64
self .verbose = verbose
54
65
self .config = config
55
66
self .filename = filename
67
+ self .namespace = namespace
68
+ self .multi_asic = multi_asic_util .MultiAsic (namespace_option = namespace )
69
+ self .config_db = None
70
+ self .db = None
56
71
57
- # Set up db connections
58
- if self .config :
59
- self .db = ConfigDBConnector ()
60
- self .db .connect ()
61
- else :
62
- self .db = SonicV2Connector (use_unix_socket_path = False )
63
- self .db .connect (self .db .STATE_DB , False )
72
+ # For unit testing
73
+ self .updated_profile_table = {}
64
74
65
75
def get_table (self , tablename ):
66
76
if self .config :
67
- return self .db .get_table (tablename )
77
+ return self .config_db .get_table (tablename )
68
78
69
79
entries = {}
70
80
keys = self .db .keys (self .db .STATE_DB , tablename + '*' )
@@ -77,13 +87,15 @@ class MmuConfig(object):
77
87
78
88
return entries
79
89
90
+ @multi_asic_util .run_on_multi_asic
80
91
def list (self ):
92
+ namespace_str = f" for namespace { self .multi_asic .current_namespace } " if multi_asic .is_multi_asic () else ''
81
93
lossless_traffic_pattern = self .get_table (DEFAULT_LOSSLESS_BUFFER_PARAMETER_NAME )
82
94
if lossless_traffic_pattern :
83
95
for _ , pattern in lossless_traffic_pattern .items ():
84
96
config = []
85
97
86
- print ("Lossless traffic pattern:" )
98
+ print (f "Lossless traffic pattern{ namespace_str } :" )
87
99
for field , value in pattern .items ():
88
100
config .append ([field , value ])
89
101
print (tabulate .tabulate (config ) + "\n " )
@@ -93,97 +105,88 @@ class MmuConfig(object):
93
105
for pool_name , pool_data in buf_pools .items ():
94
106
config = []
95
107
96
- print ("Pool: " + pool_name )
108
+ print (f "Pool{ namespace_str } : " + pool_name )
97
109
for field , value in pool_data .items ():
98
110
config .append ([field , value ])
99
111
print (tabulate .tabulate (config ) + "\n " )
100
112
if self .verbose :
101
113
print ("Total pools: %d\n \n " % len (buf_pools ))
102
114
else :
103
- print ("No buffer pool information available" )
115
+ print (f "No buffer pool information available{ namespace_str } " )
104
116
105
117
buf_profs = self .get_table (BUFFER_PROFILE_TABLE_NAME )
106
118
if buf_profs :
107
119
for prof_name , prof_data in buf_profs .items ():
108
120
config = []
109
121
110
- print ("Profile: " + prof_name )
122
+ print (f "Profile{ namespace_str } : " + prof_name )
111
123
for field , value in prof_data .items ():
112
124
config .append ([field , value ])
113
125
print (tabulate .tabulate (config ) + "\n " )
114
126
if self .verbose :
115
127
print ("Total profiles: %d" % len (buf_profs ))
116
128
else :
117
- print ("No buffer profile information available" )
129
+ print (f "No buffer profile information available{ namespace_str } " )
118
130
131
+ @multi_asic_util .run_on_multi_asic
119
132
def set (self , profile , field_alias , value ):
133
+ namespace_str = f" for namespace { self .multi_asic .current_namespace } " if multi_asic .is_multi_asic () else ''
120
134
if os .geteuid () != 0 and os .environ .get ("UTILITIES_UNIT_TESTING" , "0" ) != "2" :
121
135
sys .exit ("Root privileges required for this operation" )
122
136
123
137
field = BUFFER_PROFILE_FIELDS [field_alias ]
124
- buf_profs = self .db .get_table (BUFFER_PROFILE_TABLE_NAME )
125
- v = int (value )
138
+ buf_profs = self .config_db .get_table (BUFFER_PROFILE_TABLE_NAME )
126
139
if field == DYNAMIC_THRESHOLD :
127
- if v < - 8 or v > 8 :
128
- sys .exit ("Invalid alpha value: 2^(%s)" % (value ))
129
-
130
140
if profile in buf_profs and DYNAMIC_THRESHOLD not in buf_profs [profile ]:
131
141
sys .exit ("%s not using dynamic thresholding" % (profile ))
132
142
elif field == STATIC_THRESHOLD :
133
- if v < 0 :
134
- sys .exit ("Invalid static threshold value: (%s)" % (value ))
135
-
136
143
if profile in buf_profs and STATIC_THRESHOLD not in buf_profs [profile ]:
137
144
sys .exit ("%s not using static threshold" % (profile ))
138
145
else :
139
146
sys .exit ("Set field %s not supported" % (field ))
140
147
141
148
if self .verbose :
142
- print ("Setting %s %s value to %s" % (profile , field , value ))
143
- self .db .mod_entry (BUFFER_PROFILE_TABLE_NAME , profile , {field : value })
149
+ print ("Setting %s %s value to %s%s " % (profile , field , value , namespace_str ))
150
+ self .config_db .mod_entry (BUFFER_PROFILE_TABLE_NAME , profile , {field : value })
144
151
if self .filename is not None :
145
- prof_table = self .db .get_table (BUFFER_PROFILE_TABLE_NAME )
152
+ self . updated_profile_table [ self . multi_asic . current_namespace ] = self .config_db .get_table (BUFFER_PROFILE_TABLE_NAME )
146
153
with open (self .filename , "w" ) as fd :
147
- json .dump (prof_table , fd )
148
-
149
-
150
- def main (config ):
151
- if config :
152
- parser = argparse .ArgumentParser (description = 'Show and change: mmu configuration' ,
153
- formatter_class = argparse .RawTextHelpFormatter )
154
-
155
- parser .add_argument ('-l' , '--list' , action = 'store_true' , help = 'show mmu configuration' )
156
- parser .add_argument ('-p' , '--profile' , type = str , help = 'specify buffer profile name' , default = None )
157
- parser .add_argument ('-a' , '--alpha' , type = str , help = 'set n for dyanmic threshold alpha 2^(n)' , default = None )
158
- parser .add_argument ('-s' , '--staticth' , type = str , help = 'set static threshold' , default = None )
159
- parser .add_argument ('-v' , '--version' , action = 'version' , version = '%(prog)s 1.0' )
160
- else :
161
- parser = argparse .ArgumentParser (description = 'Show buffer state' ,
162
- formatter_class = argparse .RawTextHelpFormatter )
163
-
164
- parser .add_argument ('-l' , '--list' , action = 'store_true' , help = 'show buffer state' )
165
- parser .add_argument ('-v' , '--version' , action = 'version' , version = '%(prog)s 1.0' )
166
-
167
- parser .add_argument ('-vv' , '--verbose' , action = 'store_true' , help = 'verbose output' , default = False )
168
- parser .add_argument ('-f' , '--filename' , help = 'file used by mock tests' , type = str , default = None )
169
-
154
+ json .dump (self .updated_profile_table , fd )
155
+
156
+ @click .command (help = 'Show and change: mmu configuration' )
157
+ @click .option ('-l' , '--list' , 'show_config' , is_flag = True , help = 'show mmu configuration' )
158
+ @click .option ('-p' , '--profile' , type = str , help = 'specify buffer profile name' , default = None )
159
+ @click .option ('-a' , '--alpha' , type = click .IntRange (DYNAMIC_THRESHOLD_MIN , DYNAMIC_THRESHOLD_MAX ), help = 'set n for dyanmic threshold alpha 2^(n)' , default = None )
160
+ @click .option ('-s' , '--staticth' , type = click .IntRange (min = STATIC_THRESHOLD_MIN ), help = 'set static threshold' , default = None )
161
+ @click .option ('-n' , '--namespace' , type = click .Choice (multi_asic .get_namespace_list ()), help = 'Namespace name or skip for all' , default = None )
162
+ @click .option ('-vv' , '--verbose' , is_flag = True , help = 'verbose output' , default = False )
163
+ @click .version_option (version = '1.0' )
164
+ def main (show_config , profile , alpha , staticth , namespace , verbose ):
165
+ # A test file created for unit test purposes
166
+ filename = None
170
167
if os .environ .get ("UTILITIES_UNIT_TESTING" , "0" ) == "2" :
171
- sys . argv . extend ([ '-f' , '/tmp/mmuconfig' ])
168
+ filename = '/tmp/mmuconfig'
172
169
173
-
174
- args = parser .parse_args ()
170
+ # Buffershow and mmuconfig cmds share this script
171
+ # Buffershow cmd cannot modify configs hence config is set to False
172
+ config = True if sys .argv [0 ].split ('/' )[- 1 ] == "mmuconfig" else False
175
173
176
174
try :
177
- mmu_cfg = MmuConfig (args .verbose , config , args .filename )
178
- if args .list :
175
+ load_db_config ()
176
+ mmu_cfg = MmuConfig (verbose , config , filename , namespace )
177
+
178
+ # Both mmuconfig and buffershow have access to show_config option
179
+ if show_config :
179
180
mmu_cfg .list ()
180
- elif config and args .profile :
181
- if args .alpha :
182
- mmu_cfg .set (args .profile , "alpha" , args .alpha )
183
- elif args .staticth :
184
- mmu_cfg .set (args .profile , "staticth" , args .staticth )
181
+ # Buffershow cannot modify profiles
182
+ elif config and profile :
183
+ if alpha :
184
+ mmu_cfg .set (profile , "alpha" , alpha )
185
+ elif staticth :
186
+ mmu_cfg .set (profile , "staticth" , staticth )
185
187
else :
186
- parser .print_help ()
188
+ ctx = click .get_current_context ()
189
+ click .echo (ctx .get_help ())
187
190
sys .exit (1 )
188
191
189
192
except Exception as e :
@@ -192,7 +195,4 @@ def main(config):
192
195
sys .exit (1 )
193
196
194
197
if __name__ == "__main__" :
195
- if sys .argv [0 ].split ('/' )[- 1 ] == "mmuconfig" :
196
- main (True )
197
- else :
198
- main (False )
198
+ main ()
0 commit comments