-
Notifications
You must be signed in to change notification settings - Fork 3
/
receive_report.py
executable file
·151 lines (128 loc) · 4.88 KB
/
receive_report.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
#!/usr/bin/env python3
import sys
import io
from scapy.all import sniff
from scapy.all import Packet
from scapy.all import ShortField, BitField
from scapy.layers.inet import Ether,IP, TCP, UDP, bind_layers
class INTREP(Packet):
name = "INT Report Header v2.0"
fields_desc = [
BitField("version", 0, 4),
BitField("hw_id", 0, 6),
BitField("seq_number", 0, 22),
BitField("node_id", 0, 32)]
class INTIndiviREP(Packet):
name = "INT Report Individual Header v2.0"
fields_desc = [
BitField("rep_type", 0, 4),
BitField("in_type", 0, 4),
BitField("rep_len", 0, 8),
BitField("md_len", 0, 8),
BitField("flag", 0, 4),
BitField("rsvd", 0, 4),
ShortField("RepMdBits", 0),
ShortField("DomainID", 0),
ShortField("DSMdBits", 0),
ShortField("DSMdstatus", 0)]
class INTShim(Packet):
name = "INT Shim header v2.1"
fields_desc = [
BitField("type", 0, 4),
BitField("next_protocol", 0, 2),
BitField("rsvd", 0, 2),
BitField("int_length", 0, 8),
ShortField("NPT Dependent Field", 0)]
class INTMD(Packet):
name = "INT-MD Header v2.1"
fields_desc = [
BitField("version", 0, 4),
BitField("flags", 0, 3),
BitField("reserved", 0, 12),
BitField("HopMetaLength", 0, 5),
BitField("RemainingHopCount", 0, 8),
BitField("instruction_mask_0003", 0, 4),
BitField("instruction_mask_0407", 0, 4),
BitField("instruction_mask_0811", 0, 4),
BitField("instruction_mask_1215", 0, 4),
ShortField("DomainID", 0),
ShortField("DomainInstructions", 0),
ShortField("DomainFlags", 0)]
bind_layers(UDP,INTREP,dport=1234)
bind_layers(INTREP,INTIndiviREP)
bind_layers(INTIndiviREP,Ether,in_type=3)
bind_layers(INTShim,INTMD,type = 1)
SWITCH_ID_BIT = 0b10000000
L1_PORT_IDS_BIT = 0b01000000
HOP_LATENCY_BIT = 0b00100000
QUEUE_BIT = 0b00010000
INGRESS_TSTAMP_BIT = 0b00001000
EGRESS_TSTAMP_BIT = 0b00000100
L2_PORT_IDS_BIT = 0b00000010
EGRESS_PORT_TX_UTIL_BIT = 0b00000001
class HopMetadata():
def __init__(self):
self.switch_id = None
self.l1_ingress_port_id = None
self.l1_egress_port_id = None
self.hop_latency = None
self.q_id = None
self.q_occupancy = None
self.ingress_tstamp = None
self.egress_tstamp = None
self.l2_ingress_port_id = None
self.l2_egress_port_id = None
self.egress_port_tx_util = None
@staticmethod
def from_bytes(data, ins_map):
hop = HopMetadata()
d = io.BytesIO(data)
if ins_map & SWITCH_ID_BIT:
hop.switch_id = int.from_bytes(d.read(4), byteorder='big')
if ins_map & L1_PORT_IDS_BIT:
hop.l1_ingress_port_id = int.from_bytes(d.read(2), byteorder='big')
hop.l1_egress_port_id = int.from_bytes(d.read(2), byteorder='big')
if ins_map & HOP_LATENCY_BIT:
hop.hop_latency = int.from_bytes(d.read(4), byteorder='big')
if ins_map & QUEUE_BIT:
hop.q_id = int.from_bytes(d.read(1), byteorder='big')
hop.q_occupancy = int.from_bytes(d.read(3), byteorder='big')
if ins_map & INGRESS_TSTAMP_BIT:
hop.ingress_tstamp = int.from_bytes(d.read(8), byteorder='big')
if ins_map & EGRESS_TSTAMP_BIT:
hop.egress_tstamp = int.from_bytes(d.read(8), byteorder='big')
if ins_map & L2_PORT_IDS_BIT:
hop.l2_ingress_port_id = int.from_bytes(d.read(4), byteorder='big')
hop.l2_egress_port_id = int.from_bytes(d.read(4), byteorder='big')
if ins_map & EGRESS_PORT_TX_UTIL_BIT:
hop.egress_port_tx_util = int.from_bytes(d.read(4), byteorder='big')
return hop
def __str__(self):
return str(vars(self))
def parse_metadata(int_pkt):
int_pkt.show()
instructions = (int_pkt[INTMD].instruction_mask_0003 << 4) + int_pkt[INTMD].instruction_mask_0407
int_len = int_pkt.int_length-3
hop_meta_len = int_pkt[INTMD].HopMetaLength
int_metadata = int_pkt.load[:int_len<<2]
hop_count = int(int_len /hop_meta_len)
hop_metadata = []
for i in range(hop_count):
metadata_source = int_metadata[i*hop_meta_len<<2:(i+1)*hop_meta_len<<2]
meta = HopMetadata.from_bytes(metadata_source, instructions)
print(meta)
hop_metadata.append(meta)
return hop_metadata
def handle_pkt(pkt):
if IP in pkt :
print("\n\n********* Receiving Telemtry Report ********")
pkt[INTREP].show()
parse_metadata(INTShim(pkt.load))
def main():
iface = 's3-cpu-eth1'
print("sniffing on %s" % iface)
sys.stdout.flush()
sniff(iface = iface,filter='inbound and tcp or udp',
prn = lambda x: handle_pkt(x))
if __name__ == '__main__':
main()