This repository has been archived by the owner on Jan 9, 2021. It is now read-only.
/
datastructures.py
441 lines (349 loc) · 12.9 KB
/
datastructures.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
#!/usr/bin/env python3
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import ctypes
import enum
import errno
import fcntl
import mmap
import multiprocessing
import os
import resource
import select
import py2bpf._bpf._syscall as _syscall
import py2bpf._bpf._perf_event as pe
from py2bpf._translation._datastructures import (
FileDescriptorDatastructure, RuntimeDatastructure
)
class BpfMapType(enum.IntEnum):
HASH = 1
ARRAY = 2
PROG_ARRAY = 3
PERF_EVENT_ARRAY = 4
PERCPU_HASH = 5
PERCPU_ARRAY = 6
STACK_TRACE = 7
class _BpfAttrMapCreate(ctypes.Structure):
_fields_ = [
('map_type', ctypes.c_uint),
('key_size', ctypes.c_uint),
('value_size', ctypes.c_uint),
('max_entries', ctypes.c_uint),
('map_flags', ctypes.c_uint),
]
class _BpfAttrMapElem(ctypes.Structure):
_fields_ = [
('map_fd', ctypes.c_uint),
('key', ctypes.c_char_p),
('value_or_next_key', ctypes.c_char_p),
('flags', ctypes.c_char_p),
]
class _MapCmd(enum.IntEnum):
CREATE = 0
LOOKUP_ELEM = 1
UPDATE_ELEM = 2
DELETE_ELEM = 3
GET_NEXT_KEY = 4
def _map_create(map_type, key_size, value_size, max_entries):
attr = _BpfAttrMapCreate(
map_type=map_type,
key_size=key_size,
value_size=value_size,
max_entries=max_entries,
map_flags=0
)
fd = _syscall.bpf(_MapCmd.CREATE, ctypes.pointer(attr), ctypes.sizeof(attr))
if fd < 0:
eno = _syscall._get_errno()
raise OSError(eno, 'Failed to create bpf map: {}'.format(
os.strerror(eno)))
return fd
def _update_elem(fd, key, value):
key_p = ctypes.cast(ctypes.pointer(key), ctypes.c_char_p)
value_p = ctypes.cast(ctypes.pointer(value), ctypes.c_char_p)
attr = _BpfAttrMapElem(
map_fd=fd,
key=key_p,
value_or_next_key=value_p,
flags=0,
)
attr_p = ctypes.pointer(attr)
if _syscall.bpf(_MapCmd.UPDATE_ELEM, attr_p, ctypes.sizeof(attr)) != 0:
eno = _syscall._get_errno()
raise OSError(eno, 'Failed to update bpf map: {}'.format(
os.strerror(eno)))
class BpfMap(FileDescriptorDatastructure):
def __init__(self, max_entries):
self.fd = -1
key_size = ctypes.sizeof(self.KEY_TYPE)
value_size = ctypes.sizeof(self.VALUE_TYPE)
self.fd = _map_create(BpfMapType.HASH, key_size, value_size, max_entries)
def close(self):
if self.fd >= 0:
os.close(self.fd)
def __getitem__(self, key):
if not isinstance(key, self.KEY_TYPE):
key = self.KEY_TYPE(key)
return self.lookup(key)
def __setitem__(self, key, value):
if not isinstance(key, self.KEY_TYPE):
key = self.KEY_TYPE(key)
if not isinstance(value, self.VALUE_TYPE):
value = self.VALUE_TYPE(value)
self.update(key, value)
def __delitem__(self, key):
if not isinstance(key, self.KEY_TYPE):
key = self.KEY_TYPE(key)
self.delete(key)
def update(self, key, value):
if not isinstance(key, self.KEY_TYPE):
raise TypeError('key {} is not instance of key_type {}'.format(
repr(key), repr(self.KEY_TYPE)))
if not isinstance(value, self.VALUE_TYPE):
raise TypeError('value {} is not instance of value_type {}'.format(
repr(value), repr(self.VALUE_TYPE)))
_update_elem(self.fd, key, value)
def lookup(self, key):
if not isinstance(key, self.KEY_TYPE):
raise TypeError('key {} is not instance of key_type {}'.format(
repr(key), repr(self.KEY_TYPE)))
key_p = ctypes.cast(ctypes.pointer(key), ctypes.c_char_p)
value = self.VALUE_TYPE()
value_p = ctypes.cast(ctypes.pointer(value), ctypes.c_char_p)
attr = _BpfAttrMapElem(
map_fd=self.fd,
key=key_p,
value_or_next_key=value_p,
flags=0,
)
attr_p = ctypes.pointer(attr)
ret = _syscall.bpf(_MapCmd.LOOKUP_ELEM, attr_p, ctypes.sizeof(attr))
if ret == 0:
return value
eno = _syscall._get_errno()
if eno == errno.ENOENT:
raise KeyError(key)
raise OSError(eno, 'Failed to lookup bpf map: {}'.format(
os.strerror(eno)))
def delete(self, key):
if not isinstance(key, self.KEY_TYPE):
raise TypeError('key {} is not instance of key_type {}'.format(
repr(key), repr(self.KEY_TYPE)))
key_p = ctypes.cast(ctypes.pointer(key), ctypes.c_char_p)
attr = _BpfAttrMapElem(map_fd=self.fd, key=key_p, flags=0)
attr_p = ctypes.pointer(attr)
ret = _syscall.bpf(_MapCmd.DELETE_ELEM, attr_p, ctypes.sizeof(attr))
if ret == 0:
return
eno = _syscall._get_errno()
if eno == errno.ENOENT:
raise KeyError(key)
raise OSError(eno, 'Failed to lookup bpf map: {}'.format(
os.strerror(eno)))
def get_next_key(self, last_key):
if not isinstance(last_key, self.KEY_TYPE):
raise TypeError('key {} is not instance of key_type {}'.format(
repr(last_key), repr(self.KEY_TYPE)))
key_p = ctypes.cast(ctypes.pointer(last_key), ctypes.c_char_p)
next_key = self.KEY_TYPE()
next_key_p = ctypes.cast(ctypes.pointer(next_key), ctypes.c_char_p)
attr = _BpfAttrMapElem(
map_fd=self.fd,
key=key_p,
value_or_next_key=next_key_p,
flags=0,
)
attr_p = ctypes.pointer(attr)
ret = _syscall.bpf(_MapCmd.GET_NEXT_KEY, attr_p, ctypes.sizeof(attr))
if ret == 0:
return next_key
eno = _syscall._get_errno()
if eno == errno.ENOENT:
return None
raise OSError(eno, 'Failed to get next key: {}'.format(
os.strerror(eno)))
def keys(self):
keys = []
last_key = self.KEY_TYPE()
while True:
next_key = self.get_next_key(last_key)
if next_key is None:
return keys
keys.append(next_key)
last_key = next_key
def items(self):
return [(k, self.lookup(k)) for k in self.keys()]
def create_map(key_type, value_type, max_entries, default=None):
class MapClass(BpfMap):
KEY_TYPE = key_type
VALUE_TYPE = value_type
DEFAULT_VALUE = default if default is not None else value_type()
return MapClass(max_entries)
PERF_MAX_STACK_DEPTH = 127
class BpfStackTraceMap(BpfMap):
KEY_TYPE = ctypes.c_uint32
VALUE_TYPE = ctypes.c_uint64 * PERF_MAX_STACK_DEPTH
def __init__(self, max_entries):
self.fd = -1
key_size = ctypes.sizeof(self.KEY_TYPE)
value_size = ctypes.sizeof(self.VALUE_TYPE)
self.fd = _map_create(
BpfMapType.STACK_TRACE, key_size, value_size, max_entries)
def __del__(self):
if self.fd >= 0:
os.close(self.fd)
def __getitem__(self, key):
if not isinstance(key, self.KEY_TYPE):
key = self.KEY_TYPE(key)
return self.lookup(key)
def __delitem__(self, key):
if not isinstance(key, self.KEY_TYPE):
key = self.KEY_TYPE(key)
self.delete(key)
def lookup(self, key):
if not isinstance(key, self.KEY_TYPE):
raise TypeError('key {} is not instance of key_type {}'.format(
repr(key), repr(self.KEY_TYPE)))
key_p = ctypes.cast(ctypes.pointer(key), ctypes.c_char_p)
value = self.VALUE_TYPE()
value_p = ctypes.cast(ctypes.pointer(value), ctypes.c_char_p)
attr = _BpfAttrMapElem(
map_fd=self.fd,
key=key_p,
value_or_next_key=value_p,
flags=0,
)
attr_p = ctypes.pointer(attr)
ret = _syscall.bpf(_MapCmd.LOOKUP_ELEM, attr_p, ctypes.sizeof(attr))
if ret == 0:
return value
eno = _syscall._get_errno()
if eno == errno.ENOENT:
raise KeyError(key)
raise OSError(eno, 'Failed to lookup bpf map: {}'.format(
os.strerror(eno)))
def delete(self, key):
if not isinstance(key, self.KEY_TYPE):
raise TypeError('key {} is not instance of key_type {}'.format(
repr(key), repr(self.KEY_TYPE)))
key_p = ctypes.cast(ctypes.pointer(key), ctypes.c_char_p)
attr = _BpfAttrMapElem(map_fd=self.fd, key=key_p, flags=0)
attr_p = ctypes.pointer(attr)
ret = _syscall.bpf(_MapCmd.DELETE_ELEM, attr_p, ctypes.sizeof(attr))
if ret == 0:
return
eno = _syscall._get_errno()
if eno == errno.ENOENT:
raise KeyError(key)
raise OSError(eno, 'Failed to lookup bpf map: {}'.format(
os.strerror(eno)))
class PerfQueue:
def __init__(self, data_type, cpu, num_pages=9):
self.mm_fd = -1
self.data_type = data_type
self.num_pages = num_pages
try:
attr = pe.PerfEventAttr()
attr.type = pe.PERF_TYPE_SOFTWARE
attr.config = pe.PERF_COUNT_SW_BPF_OUTPUT
attr.sample_type = pe.PERF_SAMPLE_RAW
self.mm_fd = pe.perf_event_open(attr, cpu=cpu)
if self.mm_fd < 0:
eno = _syscall._get_errno()
raise OSError(eno, 'Failed to create perf event: {}'.format(
os.strerror(eno)))
fcntl.ioctl(self.mm_fd, pe.PERF_EVENT_IOC_ENABLE, 0)
self.pagesz = resource.getpagesize()
self.mm = mmap.mmap(self.mm_fd, self.num_pages * self.pagesz)
except Exception:
self.close()
raise
def close(self):
if self.mm_fd > 0:
os.close(self.mm_fd)
self.mm_fd = -1
def __iter__(self):
self.items = []
return self
def __next__(self):
while len(self.items) == 0:
self.items.extend(self.get_items())
return self.items.pop()
def get_items(self, timeout_ms=1000):
if timeout_ms != 0:
p = select.poll()
p.register(self.mm_fd, select.POLLIN)
p.poll(timeout_ms)
p.unregister(self.mm_fd)
class Sample(ctypes.Structure):
_pack_ = 1
_fields_ = [
('header', pe.PerfEventHeader),
('size', ctypes.c_uint32),
('data', self.data_type)
]
items = []
page = pe.PerfEventMmapPage.from_buffer(self.mm)
data_size = (self.num_pages - 1) * self.pagesz
begin, end = page.data_tail % data_size, page.data_head % data_size
while begin != end:
data_left = data_size - begin
header = pe.PerfEventHeader.from_buffer_copy(
self.mm, begin + self.pagesz)
if header.type == pe.PERF_RECORD_SAMPLE:
if data_left < header.size:
self.mm.seek(begin + self.pagesz)
data = self.mm.read(data_left)
self.mm.seek(self.pagesz)
data += self.mm.read(header.size - data_left)
self.mm.seek(0)
s = Sample.from_buffer_copy(data)
else:
s = Sample.from_buffer_copy(self.mm, begin + self.pagesz)
items.append(s.data)
elif header.type == pe.PERF_RECORD_LOST:
print('lost')
else:
assert False
begin = (begin + header.size) % data_size
page.data_tail = page.data_head
return items
class BpfQueue(FileDescriptorDatastructure):
def __init__(self, data_type, num_pages=9):
self.queues = {}
self.fd = _map_create(
BpfMapType.PERF_EVENT_ARRAY, 4, 4, multiprocessing.cpu_count())
for cpu in range(multiprocessing.cpu_count()):
q = PerfQueue(data_type, cpu, num_pages)
_update_elem(self.fd, ctypes.c_int(cpu), ctypes.c_int(q.mm_fd))
self.queues[cpu] = q
def close(self):
for cpu, q in self.queues.items():
q.close()
self.queues = {}
if self.fd > 0:
os.close(self.fd)
self.fd = -1
def __iter__(self):
self.items = []
return self
def __next__(self):
while len(self.items) == 0:
self.items.extend(self.get_items())
return self.items.pop()
def get_items(self, timeout_ms=1000):
p = select.poll()
for cpu, q in self.queues.items():
p.register(q.mm_fd, select.POLLIN)
p.poll(timeout_ms)
for cpu, q in self.queues.items():
p.unregister(q.mm_fd)
items = []
for cpu, q in self.queues.items():
items.extend(q.get_items(timeout_ms=0))
return items
def get_cpu_queue(self, cpu):
return self.queues[cpu]