forked from sonic-net/sonic-platform-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcmshell.py
501 lines (413 loc) · 17.5 KB
/
bcmshell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#-------------------------------------------------------------------------------
#
# Copyright 2012 Cumulus Networks, inc all rights reserved
#
#-------------------------------------------------------------------------------
#
from __future__ import print_function
try:
import sys
import os
import time
import socket
import re
except ImportError as e:
raise ImportError (str(e) + "- required module not found")
#-------------------------------------------------------------------------------
#
class bcmshell (object):
"""Simple expect like class that connects to a BCM diag shell, exported as a
socket fd by switchd, allowing users to run commands and extract delimited
output. bcmdiag opens the socket file, flushes the socket's read side and
issues commands via bcmdiag.run(). The command output, upto the diag shell
prompt, is read from the socket and returned to the caller."""
version = "1.0"
#---------------
#
def __init__(self, keepopen=False, timeout=10, opennow=False, logfileobj=None,
socketname="/var/run/docker-syncd/sswsyncd.socket", prompt='^drivshell>\s*$'):
"""Constructor:
keepopen - indicates that switchd socket should be kept open between
uses because the invoking application has a bunch of consecutive
activity. In this use case, the socket must be explicitly closed by
bcmshell.close() to allow multiplexing of multiple applications.
timeout - the time, in seconds, that bcmshell.run() will sit on the
domain socket wait for read input. Note that as long as the socket is
handing us data, we'll keep reading for up <timeout> seconds.
logfileobj - a file object that should log all issued commands. None
disables command logging; sys.stdout logs every command to stdout.
logfileobj is flushed after every write.
socketname - the name of the switchd socket file.
prompt - the diag shell prompt that delimits the end of output"""
if type(prompt) is not str:
raise SyntaxError("bcmshell constructor prompt expects an re string")
else:
self.re_prompt = re.compile(prompt, re.MULTILINE)
self.re_connectprompt = re.compile("bcmshell\r\n\s*" + prompt, re.MULTILINE)
if timeout <= 0:
raise ValueError("bcmshell.timeout must be > 0")
else:
self.timeout = timeout
if timeout > 180:
sys.stderr.write("warning: bcmshell.timeout, %s, is > 180s\n" %
str(self.timeout))
if not os.access(socketname, os.F_OK):
raise ValueError("socket %s does not exist" % socketname)
elif not os.access(socketname, os.R_OK | os.W_OK):
raise ValueError("missing read/write permissions for %s" % socketname)
else:
self.socketname = socketname
if logfileobj is None:
self.logfileobj = None
elif type(logfileobj) is not file:
raise TypeError("bcmshell.logfileobj must be a file object not %s" %
type(logfileobj))
elif 'w' not in logfileobj.mode:
raise TypeError("bcmshell.logfileobj is not a writeable file object")
else:
self.logfileobj = logfileobj
self.keepopen = keepopen
self.socketobj = None
self.buffer = ''
# text editing tools
#
self.re_oneline = re.compile('\n\s+')
self.re_reg_parse_raw = re.compile('^[\w()]+\.(.+)\[.*=(.*)$')
self.re_reg_parse_fields = re.compile('^[\w()]+\.(.+)\[.*\<(.*)\>')
self.re_get_field = re.compile('^(.+)=(.+)')
self.re_table_header = re.compile('^.*:[ <]+', re.MULTILINE)
self.re_table_trailer = re.compile('[ >]+$', re.MULTILINE)
self.re_conv = re.compile(r'(\d+|\D+)')
# interface name conversion
#
self.modname = dict()
for I in range(0, 63):
self.modname['xe' + str(I)] = 'swp' + str(I)
# open socket if required
#
if opennow and keepopen:
self.__open__()
#---------------
#
def __del__(self):
"""Destructor: flush and close all files that we opened"""
try:
self.close()
except Exception:
pass
#---------------
#
def __str__(self):
"""Return object state in human-readable form"""
s = []
s.append(repr(self))
s.append('version: ' + self.version)
s.append('keepopen: ' + str(self.keepopen))
s.append('timeout: ' + str(self.timeout) + ' seconds')
s.append('logfileobj: ' + str(self.logfileobj))
s.append('socketname: ' + str(self.socketname))
s.append('socketobj: ' + str(self.socketobj))
s.append('prompt: \"' + str(self.re_prompt.pattern) + '\"')
s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:])
return '\n'.join(s)
#---------------
#
def getreg(self, reg, fields=False):
"""Get device register(s) by name.
The general format in which registers are returned from the shell is
reg_name(N).M=V for raw values and reg_name(N).M=<F1=V1,F2=V2> where N
is an array index, M is a module or port name, V is a value, and F is a
field name. Register/field values are all converted to unsigned
integers. There is a built in name converstion fucntion to change
Broadcom "xe*" port names to "swp*" names that match the kernel.
bcmshell.getreg('reg_name') returns dict of lists of values. As an
optimization, if there is only one entry in the dict, only the list of
values is returned and if there is only one entry in each list, only the
values are returned
Examples:
bcmshell.getreg('cmic_config') returns a value...
1057759299.
bcmshell.getreg('protocol_pkt_control') returns a list of values...
[0, 0, 0, ..., 0]
bcmshell.getreg('egr_mtu') returns a dict of values....
{'cpu0': 0x3fff,
'xe0': 0x3fff,
...
'lb0': 0x3fff}
bcmshell.getreg('pg_min_cell') returns a dict of lists of values....
{'cpu0:'[0, 0, 0, 0, 0, 0, 0, 0],
'xe0': [0, 0, 0, 0, 0, 0, 0, 0],
...
'lb0': [0, 0, 0, 0, 0, 0, 0, 0]}
bcmshell.getreg('reg_name', True) returns dict of lists of dicts of
field/values. The same optimizations used for raw values apply.
Examples:
bcmshell.getreg('cmic_config', True) returns a dict of field/values...
{'IGNORE_MMU_BKP_TXDMA_PKT': 0,
'EN_SER_INTERLEAVE_PARITY': 1,
'MIIM_ADDR_MAP_ENABLE': 1,
'IGNORE_MMU_BKP_REMOTE_PKT': 0,
...
'DMA_GARBAGE_COLLECT_EN': 0}
bcmshell.getreg('protocol_pkt_control', True) returns
[{'SRP_PKT_TO_CPU':0, 'SRP_FWD_ACTION':0,... 'ARP_REPLY_DROP':0},
{'SRP_PKT_TO_CPU':0, 'SRP_FWD_ACTION':0,... 'ARP_REPLY_DROP':0},
...
{'SRP_PKT_TO_CPU':0, 'SRP_FWD_ACTION':0,... 'ARP_REPLY_DROP':0}]
bcmshell.getreg('egr_mtu', fields=True) returns a dict of dicts...
{'cpu0': {'MTU_SIZE',0x3fff, 'MTU_ENABLE',0},
'xe0': {'MTU_SIZE',0x3fff, 'MTU_ENABLE',0},
...
'lb0': {'MTU_SIZE',0x3fff, 'MTU_ENABLE',0}}
bcmshell.getreg('pg_min_cell') returns a dict of lists of values....
{'cpu0:'[{'PG_MIN':0}, {'PG_MIN':0},... {'PG_MIN':0}],
'xe0:'[{'PG_MIN':0}, {'PG_MIN':0},... {'PG_MIN':0}],
...
'lb0:'[{'PG_MIN':0}, {'PG_MIN':0},... {'PG_MIN':0}]}
"""
# make sure everything is sane and read the register
#
if type(reg) is not str:
raise TypeError("expecting string argument to bmcdiag.getreg(reg)")
elif reg.find('\n') >= 0:
raise ValueError("unexpected newline in bmcdiag.getreg(%s)" % reg )
elif reg.find('\s') >= 0:
raise ValueError("unexpected whitespace in bmcdiag.getreg(%s)" % reg)
if fields:
t = self.run('getreg ' + reg)
else:
t = self.run('getreg raw ' + reg)
if 'Syntax error parsing' in t:
raise RuntimeError('\"%s\" is not a register' % reg)
# get the results into a list
#
t = self.re_oneline.sub('', t)
t = t.split('\n')
if t[-1] == '':
t.pop()
# get the results into a dict (module) of lists (array) of values/fields
#
def __parse_reg__(text, fields=False):
if fields:
m = self.re_reg_parse_fields.search(text)
s = m.group(2).split(',')
t = dict([self.__get_field__(S) for S in s])
else:
m = self.re_reg_parse_raw.search(text)
t = int(m.group(2), 16)
return(self.modname.get(m.group(1), m.group(1)), t)
t = [__parse_reg__(T, fields) for T in t]
d = dict()
for I in t:
if I[0] in d:
d[I[0]].append(I[1])
else:
d[I[0]] = [I[1]]
# now optimize the return
#
for I in iter(d):
if len(d[I]) == 1:
d[I] = d[I][0]
if len(d) == 1:
return d.values()[0]
else:
return d
#---------------
#
def gettable(self, table, fields=False, start=None, entries=None):
"""Get device memory based table(s) by name. Tables are returned as a
list of value/field-dict. Table entry/field values are converted into
unsigned integers. If "fields" is set to True, we return a list of
dictionaries of field/value.
Examples:
bcmshell.gettable('egr_ing_port') returns
[0, 0, 0, ... 0]
bcmshell.gettable('egr_ing_port', True) returns
[{'HIGIG2': 0, 'PORT_TYPE': 0},
{'HIGIG2': 0, 'PORT_TYPE': 0},
{'HIGIG2': 0, 'PORT_TYPE': 0},
...
{'HIGIG2': 0, 'PORT_TYPE': 0}]
"""
if type(table) is not str:
raise TypeError("bcmshell.gettable(table) expects string not %s" %
type(table))
elif table.find('\n') >= 0:
raise ValueError("unexpected newline in bmcshell.gettable(%s)" %
table )
elif table.find('\s') >= 0:
raise ValueError("unexpected whitespace in bmcshell.gettable(%s)" %
table)
cmd = 'dump all'
if not fields:
cmd += ' raw'
cmd += " %s" % table
if start != None or entries != None:
cmd += " %d" % (start or 0)
cmd += " %d" % (entries or 1)
t = self.run(cmd)
if 'Unknown option or memory' in t:
raise RuntimeError('\"%s\" is not a table' % table)
if 'out of range' in t:
err = table
if start != None or entries != None:
err += " %d" % (start or 0)
err += " %d" % (entries or 1)
raise IndexError('\"%s\" table index is out of range' % err)
# get all of the return into a list
#
t = self.re_oneline.sub('', t)
t = self.re_table_header.sub('', t)
t = self.re_table_trailer.sub('', t)
t = t.split('\n')
if t[-1] == '':
t.pop()
# parse the contents
#
def __parse_table__(text, fields=False):
if fields:
t = text.split(',')
v = [self.__get_field__(T) for T in t]
return dict(v)
else:
t = text.split()
v = 0
for I in range(len(t)):
v += (int(t[I], 16) << (32 * I))
return v
t = [__parse_table__(T, fields) for T in t]
return t
#---------------
#
def cmd(self, cmd):
"""Run a command and print the results"""
s = self.run(cmd)
if 'Unknown command:' in s:
raise ValueError(s)
print(s)
#---------------
#
def prettyprint(self, d, level=0):
"""Print the structured output generated by getreg and gettable in a
human readable format"""
s = level * 8 * " "
if type(d) is dict:
for I in sorted(d, key=self.__name_conv__):
if type(d[I]) is int:
print("%s %30s: " % (s, I), end=" ")
else:
print("%s %s:" % (s, I))
self.prettyprint(d[I], (level + 1))
elif type(d) is list:
for I in range(len(d)):
i = "[" + str(I) + "]"
if type(d[I]) is int or type(d[I]) is long:
print("%s %10s: " % (s, i), end=" ")
else:
print("%s %s:" % (s, i))
self.prettyprint(d[I], (level + 1))
else:
print("%s" % (hex(d)))
#---------------
#
def close(self):
"""Close the socket object"""
if self.socketobj is not None:
self.socketobj.shutdown(socket.SHUT_RDWR)
self.socketobj.close()
self.socketobj = None
#---------------
#
def run(self, cmd):
"""Issue the command to the diag shell and collect the return data until
we detect the prompt. cmd must be a string and must not include a
newline, i.e. we expect a single command to be run per call."""
if type(cmd) is not str:
raise TypeError("expecting string argument to bmcdiag.run(cmd)")
elif cmd.find('\n') >= 0:
raise ValueError("unexpected newline in bmcdiag.run(cmd)")
self.__open__()
try:
self.socketobj.sendall(cmd + '\n')
except socket.error as err:
(errno, errstr) = err.args
raise IOError("unable to send command \"%s\", %s" % (cmd, errstr))
self.buffer = ''
self.socketobj.settimeout(self.timeout)
quitting_time = time.time() + self.timeout
while True:
try:
self.buffer += self.socketobj.recv(4096)
except socket.timeout:
raise RuntimeError("recv stalled for %d seconds" % self.timeout)
found = self.re_prompt.search(self.buffer)
if found:
break
if time.time() > quitting_time:
raise RuntimeError("accepting input for %d seconds" % self.timeout)
if found.end(0) != len(self.buffer):
raise RuntimeError("prompt detected in the middle of input")
if not self.keepopen:
self.close()
return self.buffer[:found.start(0)]
#---------------
#
def __name_conv__(self, s):
l = self.re_conv.findall(s)
for I in range(len(l)):
if l[I].isdigit():
l[I] = int(l[I])
return l
#---------------
#
def __get_field__(self, text):
m = self.re_get_field.search(text)
return (m.group(1), int(m.group(2), 16))
#---------------
#
def __open__(self):
"""Open the bcm diag shell socket exported by switchd. Complete any
dangling input by issuing a newline and flush the read side in case the
last user left something lying around. No-op if the socket is already
open. NOTE: socket.connect is non-blocking, so we need to exchange
a command with the bcm diag shell to know that we've actually obtained
socket ownership."""
if self.socketobj is None:
timeout = self.timeout
while True:
try:
self.socketobj = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socketobj.settimeout(self.timeout)
self.socketobj.connect(self.socketname)
except socket.error as err:
(errno, errstr) = err.args
if timeout == 0:
raise IOError("unable to open %s, %s" % (self.socketname, errstr))
time.sleep(1)
timeout -= 1
else:
break;
# flush out the socket in case it was left dirty
try:
self.socketobj.sendall('echo bcmshell\n')
quitting_time = time.time() + self.timeout
buf = ''
while True:
try:
buf += self.socketobj.recv(1024)
except socket.timeout:
raise IOError("unable to receive data from %s for %d seconds" %
(self.socketname, self.timeout))
found = self.re_connectprompt.search(buf)
if found:
break
if time.time() > quitting_time:
raise IOError("unable to flush %s for %d seconds" %
(self.socketname, self.timeout))
except socket.error as err:
(errno, errstr) = err.args
raise IOError("Socket error: unable to flush %s on open: %s" % (self.socketname, errstr))
except IOError as e:
raise IOError("unable to flush %s on open: %s" % (self.socketname, e.message))
except Exception:
raise IOError("unable to flush %s on open" % self.socketname)