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