forked from hakiri/p4-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_digest.py
46 lines (35 loc) · 1.55 KB
/
get_digest.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
import nnpy
import struct
import ipaddress
from p4utils.utils.helper import load_topo
from p4utils.utils.sswitch_thrift_API import SimpleSwitchThriftAPI
class DigestController():
def __init__(self, sw_name):
self.topo = load_topo('topology.json')
self.sw_name = sw_name
self.thrift_port = self.topo.get_thrift_port(sw_name)
self.controller = SimpleSwitchThriftAPI(self.thrift_port)
def recv_msg_digest(self, msg):
topic, device_id, ctx_id, list_id, buffer_id, num = struct.unpack("<iQiiQi",
msg[:32])
#print num, len(msg)
offset = 9
msg = msg[32:]
for sub_message in range(num):
random_num, src, dst = struct.unpack("!BII", msg[0:offset])
print("random number:", random_num, "src ip:", str(ipaddress.IPv4Address(src)), "dst ip:", str(ipaddress.IPv4Address(dst)))
msg = msg[offset:]
self.controller.client.bm_learning_ack_buffer(ctx_id, list_id, buffer_id)
def run_digest_loop(self):
sub = nnpy.Socket(nnpy.AF_SP, nnpy.SUB)
notifications_socket = self.controller.client.bm_mgmt_get_info().notifications_socket
print("connecting to notification sub %s" % notifications_socket)
sub.connect(notifications_socket)
sub.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, '')
while True:
msg = sub.recv()
self.recv_msg_digest(msg)
def main():
DigestController("s1").run_digest_loop()
if __name__ == "__main__":
main()