forked from p4lang/behavioral-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanomsg_client.py
executable file
·423 lines (348 loc) · 13.6 KB
/
nanomsg_client.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
#!/usr/bin/python
# Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Antonin Bas ([email protected])
#
#
import nnpy
import struct
import sys
import json
import argparse
parser = argparse.ArgumentParser(description='BM nanomsg event logger client')
parser.add_argument('--socket', help='IPC socket to which to subscribe',
action="store", default="ipc:///tmp/bm-0-log.ipc")
parser.add_argument('--json', help='JSON description of P4 program',
action="store")
args = parser.parse_args()
class NameMap:
def __init__(self):
self.names = {}
def load_names(self, json_src):
with open(json_src, 'r') as f:
json_ = json.load(f)
for type_ in {"header_type", "header", "parser",
"deparser", "action", "pipeline", "checksum"}:
json_list = json_[type_ + "s"]
for obj in json_list:
self.names[(type_, obj["id"])] = obj["name"]
for pipeline in json_["pipelines"]:
tables = pipeline["tables"]
for obj in tables:
self.names[("table", obj["id"])] = obj["name"]
conds = pipeline["conditionals"]
for obj in conds:
self.names[("condition", obj["id"])] = obj["name"]
def get_name(self, type_, id_):
return self.names.get( (type_, id_), None )
name_map = NameMap()
def name_lookup(type_, id_):
return name_map.get_name(type_, id_)
class MSG_TYPES:
(PACKET_IN, PACKET_OUT,
PARSER_START, PARSER_DONE, PARSER_EXTRACT,
DEPARSER_START, DEPARSER_DONE, DEPARSER_EMIT,
CHECKSUM_UPDATE,
PIPELINE_START, PIPELINE_DONE,
CONDITION_EVAL, TABLE_HIT, TABLE_MISS,
ACTION_EXECUTE) = range(15)
@staticmethod
def get_msg_class(type_):
classes = {
MSG_TYPES.PACKET_IN: PacketIn,
MSG_TYPES.PACKET_OUT: PacketOut,
MSG_TYPES.PARSER_START: ParserStart,
MSG_TYPES.PARSER_DONE: ParserDone,
MSG_TYPES.PARSER_EXTRACT: ParserExtract,
MSG_TYPES.DEPARSER_START: DeparserStart,
MSG_TYPES.DEPARSER_DONE: DeparserDone,
MSG_TYPES.DEPARSER_EMIT: DeparserEmit,
MSG_TYPES.CHECKSUM_UPDATE: ChecksumUpdate,
MSG_TYPES.PIPELINE_START: PipelineStart,
MSG_TYPES.PIPELINE_DONE: PipelineDone,
MSG_TYPES.CONDITION_EVAL: ConditionEval,
MSG_TYPES.TABLE_HIT: TableHit,
MSG_TYPES.TABLE_MISS: TableMiss,
MSG_TYPES.ACTION_EXECUTE: ActionExecute,
}
return classes[type_]
@staticmethod
def get_str(type_):
strs = {
MSG_TYPES.PACKET_IN: "PACKET_IN",
MSG_TYPES.PACKET_OUT: "PACKET_OUT",
MSG_TYPES.PARSER_START: "PARSER_START",
MSG_TYPES.PARSER_DONE: "PARSER_DONE",
MSG_TYPES.PARSER_EXTRACT: "PARSER_EXTRACT",
MSG_TYPES.DEPARSER_START: "DEPARSER_START",
MSG_TYPES.DEPARSER_DONE: "DEPARSER_DONE",
MSG_TYPES.DEPARSER_EMIT: "DEPARSER_EMIT",
MSG_TYPES.CHECKSUM_UPDATE: "CHECKSUM_UPDATE",
MSG_TYPES.PIPELINE_START: "PIPELINE_START",
MSG_TYPES.PIPELINE_DONE: "PIPELINE_DONE",
MSG_TYPES.CONDITION_EVAL: "CONDITION_EVAL",
MSG_TYPES.TABLE_HIT: "TABLE_HIT",
MSG_TYPES.TABLE_MISS: "TABLE_MISS",
MSG_TYPES.ACTION_EXECUTE: "ACTION_EXECUTE",
}
return strs[type_]
class Msg(object):
def __init__(self, msg):
self.msg = msg
def extract_hdr(self):
struct_ = struct.Struct("iiQQQ")
(_, self.switch_id,
self.sig, self.id_, self.copy_id) = struct_.unpack_from(self.msg)
return struct_.size
def extract(self):
bytes_extracted = self.extract_hdr()
msg_remainder = self.msg[bytes_extracted:]
return self.struct_.unpack(msg_remainder)
def __str__(self):
return "type: %s, switch_id: %d, sig: %d, id: %d, copy_id: %d" %\
(self.type_str, self.switch_id,
self.sig, self.id_, self.copy_id)
class PacketIn(Msg):
def __init__(self, msg):
super(PacketIn, self).__init__(msg)
self.type_ = MSG_TYPES.PACKET_IN
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.port_in, = super(PacketIn, self).extract()
def __str__(self):
return super(PacketIn, self).__str__() +\
", port_in: %d" % self.port_in
class PacketOut(Msg):
def __init__(self, msg):
super(PacketOut, self).__init__(msg)
self.type_ = MSG_TYPES.PACKET_OUT
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.port_out, = super(PacketOut, self).extract()
def __str__(self):
return super(PacketOut, self).__str__() +\
", port_out: %d" % self.port_out
class ParserStart(Msg):
def __init__(self, msg):
super(ParserStart, self).__init__(msg)
self.type_ = MSG_TYPES.PARSER_START
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.parser_id, = super(ParserStart, self).extract()
def __str__(self):
s = super(ParserStart, self).__str__()
s += ", parser_id: " + str(self.parser_id)
name = name_lookup("parser", self.parser_id)
if name: s += " (" + name + ")"
return s
class ParserDone(Msg):
def __init__(self, msg):
super(ParserDone, self).__init__(msg)
self.type_ = MSG_TYPES.PARSER_DONE
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.parser_id, = super(ParserDone, self).extract()
def __str__(self):
s = super(ParserDone, self).__str__()
s += ", parser_id: " + str(self.parser_id)
name = name_lookup("parser", self.parser_id)
if name: s += " (" + name + ")"
return s
class ParserExtract(Msg):
def __init__(self, msg):
super(ParserExtract, self).__init__(msg)
self.type_ = MSG_TYPES.PARSER_EXTRACT
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.header_id, = super(ParserExtract, self).extract()
def __str__(self):
s = super(ParserExtract, self).__str__()
s += ", header_id: " + str(self.header_id)
name = name_lookup("header", self.header_id)
if name: s += " (" + name + ")"
return s
class DeparserStart(Msg):
def __init__(self, msg):
super(DeparserStart, self).__init__(msg)
self.type_ = MSG_TYPES.DEPARSER_START
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.deparser_id, = super(DeparserStart, self).extract()
def __str__(self):
s = super(DeparserStart, self).__str__()
s += ", deparser_id: " + str(self.deparser_id)
name = name_lookup("deparser", self.deparser_id)
if name: s += " (" + name + ")"
return s
class DeparserDone(Msg):
def __init__(self, msg):
super(DeparserDone, self).__init__(msg)
self.type_ = MSG_TYPES.DEPARSER_DONE
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.deparser_id, = super(DeparserDone, self).extract()
def __str__(self):
s = super(DeparserDone, self).__str__()
s += ", deparser_id: " + str(self.deparser_id)
name = name_lookup("deparser", self.deparser_id)
if name: s += " (" + name + ")"
return s
class DeparserEmit(Msg):
def __init__(self, msg):
super(DeparserEmit, self).__init__(msg)
self.type_ = MSG_TYPES.DEPARSER_EMIT
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.header_id, = super(DeparserEmit, self).extract()
def __str__(self):
s = super(DeparserEmit, self).__str__()
s += ", header_id: " + str(self.header_id)
name = name_lookup("header", self.header_id)
if name: s += " (" + name + ")"
return s
class ChecksumUpdate(Msg):
def __init__(self, msg):
super(ChecksumUpdate, self).__init__(msg)
self.type_ = MSG_TYPES.CHECKSUM_UPDATE
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.cksum_id, = super(ChecksumUpdate, self).extract()
def __str__(self):
s = super(ChecksumUpdate, self).__str__()
s += ", cksum_id: " + str(self.cksum_id)
name = name_lookup("checksum", self.cksum_id)
if name: s += " (" + name + ")"
return s
class PipelineStart(Msg):
def __init__(self, msg):
super(PipelineStart, self).__init__(msg)
self.type_ = MSG_TYPES.PIPELINE_START
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.pipeline_id, = super(PipelineStart, self).extract()
def __str__(self):
s = super(PipelineStart, self).__str__()
s += ", pipeline_id: " + str(self.pipeline_id)
name = name_lookup("pipeline", self.pipeline_id)
if name: s += " (" + name + ")"
return s
class PipelineDone(Msg):
def __init__(self, msg):
super(PipelineDone, self).__init__(msg)
self.type_ = MSG_TYPES.PIPELINE_DONE
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.pipeline_id, = super(PipelineDone, self).extract()
def __str__(self):
s = super(PipelineDone, self).__str__()
s += ", pipeline_id: " + str(self.pipeline_id)
name = name_lookup("pipeline", self.pipeline_id)
if name: s += " (" + name + ")"
return s
class ConditionEval(Msg):
def __init__(self, msg):
super(ConditionEval, self).__init__(msg)
self.type_ = MSG_TYPES.CONDITION_EVAL
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("ii")
def extract(self):
self.condition_id, self.result = super(ConditionEval, self).extract()
self.result = True if self.result != 0 else False
def __str__(self):
s = super(ConditionEval, self).__str__()
s += ", condition_id: " + str(self.condition_id)
name = name_lookup("condition", self.condition_id)
if name: s += " (" + name + ")"
s += ", result: " + str(self.result)
return s
class TableHit(Msg):
def __init__(self, msg):
super(TableHit, self).__init__(msg)
self.type_ = MSG_TYPES.TABLE_HIT
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("ii")
def extract(self):
self.table_id, self.entry_hdl = super(TableHit, self).extract()
def __str__(self):
s = super(TableHit, self).__str__()
s += ", table_id: " + str(self.table_id)
name = name_lookup("table", self.table_id)
if name: s += " (" + name + ")"
s += ", entry_hdl: " + str(self.entry_hdl)
return s
class TableMiss(Msg):
def __init__(self, msg):
super(TableMiss, self).__init__(msg)
self.type_ = MSG_TYPES.TABLE_MISS
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.table_id, = super(TableMiss, self).extract()
def __str__(self):
s = super(TableMiss, self).__str__()
s += ", table_id: " + str(self.table_id)
name = name_lookup("table", self.table_id)
if name: s += " (" + name + ")"
return s
class ActionExecute(Msg):
def __init__(self, msg):
super(ActionExecute, self).__init__(msg)
self.type_ = MSG_TYPES.ACTION_EXECUTE
self.type_str = MSG_TYPES.get_str(self.type_)
self.struct_ = struct.Struct("i")
def extract(self):
self.action_id, = super(ActionExecute, self).extract()
def __str__(self):
s = super(ActionExecute, self).__str__()
s += ", action_id: " + str(self.action_id)
name = name_lookup("action", self.action_id)
if name: s += " (" + name + ")"
return s
def recv_msgs():
def get_msg_type(msg):
type_, = struct.unpack('i', msg[:4])
return type_
sub = nnpy.Socket(nnpy.AF_SP, nnpy.SUB)
sub.connect(args.socket)
sub.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, '')
while True:
msg = sub.recv()
msg_type = get_msg_type(msg)
try:
p = MSG_TYPES.get_msg_class(msg_type)(msg)
except:
print "Unknown msg type", msg_type
continue
p.extract()
print p
def main():
json_src = args.json
if json_src:
name_map.load_names(json_src)
recv_msgs()
if __name__ == "__main__":
main()