-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_maps.py
executable file
·229 lines (182 loc) · 7.95 KB
/
process_maps.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
import json
import os
import subprocess
import ipaddress
import cal
import hex_types as ht
BASE_PATH = '/sys/fs/bpf/maps'
EXCLUDE_ADDRS = "fe80::", "ff02::"
def out_ip6_sd(src_ip6, dst_ip6):
return f"{src_ip6}".ljust(25)+"- "+f"{dst_ip6}".ljust(25)
def out_ns(value):
intero = int(value/1000000000)
vstring=str(value)
return f"{intero}.{vstring[len(str(intero)):]}"
def get_ip6_sd_from_key(key):
if 'saddr' in key:
src_ip6 = ipaddress.IPv6Address(ht.ipv6_int128_from_int8(key['saddr']['in6_u']['u6_addr8']))
else:
src_ip6 = '*'
if 'daddr' in key:
dst_ip6 = ipaddress.IPv6Address(ht.ipv6_int128_from_int8(key['daddr']['in6_u']['u6_addr8']))
else:
src_ip6 = '*'
return (src_ip6,dst_ip6)
def process_pcpu_values_u64(val_array):
num_val_array = []
str_details = ""
for val in val_array:
num_val_array.append(val['value'])
str_details=str_details+str(val['value']).rjust(7)+':'
return (num_val_array, str_details)
def process_pcpu_values_struct_count(val_array):
num_val_array = []
str_details = ""
for val in val_array:
num_val_array.append(val['value']['count'])
str_details=str_details+str(val['value']['count']).rjust(7)+':'
return (num_val_array, str_details)
class ProcessMap:
def __init__(self, map_name, package='system', program=None):
self.package = package
self.program = program
self.map_name = map_name
if package == 'system' :
self.map_path = f"{BASE_PATH}/system/{self.map_name}"
else:
self.map_path = f"{BASE_PATH}/{self.package}/{self.program}/{self.map_name}"
def read(self):
self.map_as_array = []
if os.path.exists(self.map_path):
try :
self.map_as_array = json.loads(cal.bpftool_map_dump(self.map_path))
except Exception as e:
print (e)
print (self.map_path)
return -1
else:
return -1
return 0
if __name__ == "__main__":
pm = ProcessMap('pcpu_sd_tbmon','meter','ip6_sd_tbmon')
result = pm.read()
if result == 0 :
#print (json.dumps(pm.map_as_array))
output_rows = []
for my_obj in pm.map_as_array :
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
for v in my_obj['values']:
if v['value']['last_time'] != 0:
output_rows.append (out_ip6_sd(src_ip6, dst_ip6) +
f" cpu: {v['cpu']} time: {out_ns(v['value']['last_time'])} "+
f"tokens: "+f"{v['value']['last_tokens']}".rjust(8))
output_rows.sort()
output_rows = filter(lambda line : not any([el in line for el in EXCLUDE_ADDRS]), output_rows)
for element in output_rows:
print (element)
#experiment to add an entry to the map
#ipa = ipaddress.IPv6Address('::74')
#ipb = ipaddress.IPv6Address('cafe::')
#v = [ht.u64(100),ht.u64(100),ht.u64(1000),ht.u64(1000),ht.u64(100),ht.u64(100)]
#cal.cal_map_update(pm.map_path, [ipa,ipb], v)
pm = ProcessMap('map_pcpu_lse','hike_default','lse')
result = pm.read()
if result == 0 :
#print (pm.map_as_array)
for my_obj in pm.map_as_array :
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
for v in my_obj['values']:
print (out_ip6_sd(src_ip6, dst_ip6) +
f" cts: {out_ns(v['value']['cts_ns'])} timeout: {out_ns(v['value']['timeout_ns'])}")
pm = ProcessMap('ipv6_hset_sd_map','hike_default','ip6_hset_srcdst')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
#print (my_obj)
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
print (out_ip6_sd(src_ip6, dst_ip6) +
f" cts: {out_ns(my_obj['value']['cts_ns'])} timeout: {out_ns(my_obj['value']['timeout_ns'])}")
pm = ProcessMap('map_pcpu_mon','hike_default','monitor')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
(num_val_array,str_details) = process_pcpu_values_u64(my_obj['values'])
print (f"{my_obj['key']}".rjust(3)+" : "+f"{sum(num_val_array)}".rjust(8)+" "+str_details)
#experiment to update a key value pair
#print (pm.map_path)
#cal.bpftool_map_update(pm.map_path, ["01","00","00", "00"], ["01", "00", "00", "00","00", "00","00", "00"], map_reference_type="pinned", value_type="hex")
#cal.cal_map_update(pm.map_path, ht.u32(256), 256)
pm = ProcessMap('pcpu_meter','meter','ip6_sd_meter')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
(num_val_array,str_details) = process_pcpu_values_struct_count(my_obj['values'])
print (out_ip6_sd(src_ip6, dst_ip6)+f"{sum(num_val_array)}".rjust(8)+str_details)
pm = ProcessMap('pcpu_dst_meter','meter','ip6_dst_meter')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
(num_val_array,str_details) = process_pcpu_values_struct_count(my_obj['values'])
print (out_ip6_sd(src_ip6, dst_ip6)+f"{sum(num_val_array)}".rjust(8)+str_details)
pm = ProcessMap('pcpu_tb_dst','meter','ip6_dst_tbmon')
result = pm.read()
if result == 0 :
output_rows = []
for my_obj in pm.map_as_array :
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
for v in my_obj['values']:
if v['value']['last_time'] != 0:
output_rows.append (out_ip6_sd(src_ip6, dst_ip6) +
f" cpu: {v['cpu']} time: {out_ns(v['value']['last_time'])} "+
f"tokens: "+f"{v['value']['last_tokens']}".rjust(8))
output_rows.sort()
output_rows = filter(lambda line : not any([el in line for el in EXCLUDE_ADDRS]), output_rows)
for element in output_rows:
print (element)
pm = ProcessMap('pcpu_sd_dec2zero','sampler','ip6_sd_dec2zero')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
(src_ip6,dst_ip6) = get_ip6_sd_from_key(my_obj['key'])
(num_val_array,str_details) = process_pcpu_values_struct_count(my_obj['values'])
print (out_ip6_sd(src_ip6, dst_ip6)+f"{sum(num_val_array)}".rjust(8)+str_details)
pm = ProcessMap('sid_list_1','devel_encap','show_pkt_info')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
print (my_obj)
pm = ProcessMap('sid_list_2','devel_encap','show_pkt_info')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
print (my_obj)
pm = ProcessMap('sid_list_3','devel_encap','show_pkt_info')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
print (my_obj)
pm = ProcessMap('sid_list_4','devel_encap','show_pkt_info')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
print (my_obj)
###################
pm = ProcessMap('chaddr','routingpaolo','paolo_chaddr')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
print (my_obj)
#experiment to add an entry to the map
ip6 = ipaddress.IPv6Address('2001:db8:555:666:777:888::')
cal.cal_map_update(pm.map_path, ht.u16(2), ip6)
pm = ProcessMap('usid_params','routingpaolo','paolo_adv_usid')
result = pm.read()
if result == 0 :
for my_obj in pm.map_as_array :
print (my_obj)
#experiment to update a key value pair
#print (pm.map_path)
cal.bpftool_map_update(pm.map_path, ["01", "00", "00", "00"], ["04", "00", "00", "00"], map_reference_type="pinned", value_type="hex")
cal.cal_map_update(pm.map_path, ht.u32(2), ht.u32(2))