Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ebpfCAT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
karaboDevices
ebpfCAT
Commits
404a01f3
Commit
404a01f3
authored
4 years ago
by
Martin Teichmann
Browse files
Options
Downloads
Patches
Plain Diff
make MapType an Enum
parent
dd64debc
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
arraymap.py
+4
-4
4 additions, 4 deletions
arraymap.py
bpf.py
+26
-2
26 additions, 2 deletions
bpf.py
ebpfcat.py
+1
-1
1 addition, 1 deletion
ebpfcat.py
hashmap.py
+5
-5
5 additions, 5 deletions
hashmap.py
with
36 additions
and
12 deletions
arraymap.py
+
4
−
4
View file @
404a01f3
from
struct
import
pack
,
unpack
from
struct
import
pack
,
unpack
from
.ebpf
import
Map
,
Memory
,
Opcode
from
.ebpf
import
Map
,
Memory
,
Opcode
from
.
import
bpf
from
.
bpf
import
create_map
,
lookup_elem
,
MapType
,
update_elem
class
ArrayGlobalVarDesc
:
class
ArrayGlobalVarDesc
:
...
@@ -42,10 +42,10 @@ class ArrayMapAccess:
...
@@ -42,10 +42,10 @@ class ArrayMapAccess:
self
.
size
=
size
self
.
size
=
size
def
read
(
self
):
def
read
(
self
):
self
.
data
=
bpf
.
lookup_elem
(
self
.
fd
,
b
"
\0\0\0\0
"
,
self
.
size
)
self
.
data
=
lookup_elem
(
self
.
fd
,
b
"
\0\0\0\0
"
,
self
.
size
)
def
write
(
self
):
def
write
(
self
):
bpf
.
update_elem
(
self
.
fd
,
b
"
\0\0\0\0
"
,
self
.
data
,
0
)
update_elem
(
self
.
fd
,
b
"
\0\0\0\0
"
,
self
.
data
,
0
)
class
ArrayMap
(
Map
):
class
ArrayMap
(
Map
):
...
@@ -64,7 +64,7 @@ class ArrayMap(Map):
...
@@ -64,7 +64,7 @@ class ArrayMap(Map):
self
.
name
=
name
self
.
name
=
name
def
init
(
self
,
ebpf
):
def
init
(
self
,
ebpf
):
fd
=
bpf
.
create_map
(
2
,
4
,
self
.
position
,
1
)
fd
=
create_map
(
MapType
.
ARRAY
,
4
,
self
.
position
,
1
)
setattr
(
ebpf
,
self
.
name
,
ArrayMapAccess
(
fd
,
self
.
position
))
setattr
(
ebpf
,
self
.
name
,
ArrayMapAccess
(
fd
,
self
.
position
))
with
ebpf
.
save_registers
(
list
(
range
(
6
))),
ebpf
.
get_stack
(
4
)
as
stack
:
with
ebpf
.
save_registers
(
list
(
range
(
6
))),
ebpf
.
get_stack
(
4
)
as
stack
:
ebpf
.
append
(
Opcode
.
ST
,
10
,
0
,
stack
,
0
)
ebpf
.
append
(
Opcode
.
ST
,
10
,
0
,
stack
,
0
)
...
...
This diff is collapsed.
Click to expand it.
bpf.py
+
26
−
2
View file @
404a01f3
...
@@ -7,6 +7,29 @@ from os import strerror
...
@@ -7,6 +7,29 @@ from os import strerror
class
BPFError
(
OSError
):
class
BPFError
(
OSError
):
pass
pass
class
MapType
(
Enum
):
UNSPEC
=
0
HASH
=
1
ARRAY
=
2
PROG_ARRAY
=
3
PERF_EVENT_ARRAY
=
4
PERCPU_HASH
=
5
PERCPU_ARRAY
=
6
STACK_TRACE
=
7
CGROUP_ARRAY
=
8
LRU_HASH
=
9
LRU_PERCPU_HASH
=
10
LPM_TRIE
=
11
ARRAY_OF_MAPS
=
12
HASH_OF_MAPS
=
13
DEVMAP
=
14
SOCKMAP
=
15
CPUMAP
=
16
XSKMAP
=
17
SOCKHASH
=
18
class
ProgType
(
Enum
):
class
ProgType
(
Enum
):
UNSPEC
=
0
UNSPEC
=
0
SOCKET_FILTER
=
1
SOCKET_FILTER
=
1
...
@@ -44,7 +67,8 @@ def bpf(cmd, fmt, *args):
...
@@ -44,7 +67,8 @@ def bpf(cmd, fmt, *args):
return
ret
,
unpack
(
fmt
,
attr
.
raw
)
return
ret
,
unpack
(
fmt
,
attr
.
raw
)
def
create_map
(
map_type
,
key_size
,
value_size
,
max_entries
):
def
create_map
(
map_type
,
key_size
,
value_size
,
max_entries
):
return
bpf
(
0
,
"
IIII
"
,
map_type
,
key_size
,
value_size
,
max_entries
)[
0
]
assert
isinstance
(
map_type
,
MapType
)
return
bpf
(
0
,
"
IIII
"
,
map_type
.
value
,
key_size
,
value_size
,
max_entries
)[
0
]
def
lookup_elem
(
fd
,
key
,
size
):
def
lookup_elem
(
fd
,
key
,
size
):
value
=
create_string_buffer
(
size
)
value
=
create_string_buffer
(
size
)
...
@@ -100,7 +124,7 @@ def prog_test_run(fd, data_in, data_out, ctx_in, ctx_out,
...
@@ -100,7 +124,7 @@ def prog_test_run(fd, data_in, data_out, ctx_in, ctx_out,
return
ret
,
retval
,
duration
,
data_out
.
value
,
ctx_out
.
value
return
ret
,
retval
,
duration
,
data_out
.
value
,
ctx_out
.
value
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
fd
=
create_map
(
1
,
4
,
4
,
10
)
fd
=
create_map
(
MapType
.
HASH
,
4
,
4
,
10
)
update_elem
(
fd
,
b
"
asdf
"
,
b
"
ckde
"
,
0
)
update_elem
(
fd
,
b
"
asdf
"
,
b
"
ckde
"
,
0
)
ret
=
lookup_elem
(
fd
,
b
"
asdf
"
,
4
)
ret
=
lookup_elem
(
fd
,
b
"
asdf
"
,
4
)
ret
[
2
:
4
]
=
b
"
kk
"
ret
[
2
:
4
]
=
b
"
kk
"
...
...
This diff is collapsed.
Click to expand it.
ebpfcat.py
+
1
−
1
View file @
404a01f3
...
@@ -6,7 +6,7 @@ from .ebpf import EBPF
...
@@ -6,7 +6,7 @@ from .ebpf import EBPF
from
.bpf
import
ProgType
,
create_map
,
update_elem
,
prog_test_run
,
lookup_elem
from
.bpf
import
ProgType
,
create_map
,
update_elem
,
prog_test_run
,
lookup_elem
def
script
():
def
script
():
fd
=
create_map
(
1
,
4
,
4
,
7
)
fd
=
create_map
(
MapType
.
HASH
,
4
,
4
,
7
)
update_elem
(
fd
,
b
"
AAAA
"
,
b
"
BBBB
"
,
0
)
update_elem
(
fd
,
b
"
AAAA
"
,
b
"
BBBB
"
,
0
)
e
=
EBPF
(
ProgType
.
XDP
,
"
GPL
"
)
e
=
EBPF
(
ProgType
.
XDP
,
"
GPL
"
)
...
...
This diff is collapsed.
Click to expand it.
hashmap.py
+
5
−
5
View file @
404a01f3
...
@@ -2,7 +2,7 @@ from contextlib import contextmanager
...
@@ -2,7 +2,7 @@ from contextlib import contextmanager
from
struct
import
pack
,
unpack
,
unpack
from
struct
import
pack
,
unpack
,
unpack
from
.ebpf
import
AssembleError
,
Expression
,
Opcode
,
Map
from
.ebpf
import
AssembleError
,
Expression
,
Opcode
,
Map
from
.
import
bpf
from
.
bpf
import
create_map
,
lookup_elem
,
MapType
,
update_elem
class
HashGlobalVar
(
Expression
):
class
HashGlobalVar
(
Expression
):
...
@@ -43,7 +43,7 @@ class HashGlobalVarDesc:
...
@@ -43,7 +43,7 @@ class HashGlobalVarDesc:
return
self
return
self
if
instance
.
loaded
:
if
instance
.
loaded
:
fd
=
instance
.
__dict__
[
self
.
name
].
fd
fd
=
instance
.
__dict__
[
self
.
name
].
fd
ret
=
bpf
.
lookup_elem
(
fd
,
pack
(
"
B
"
,
self
.
count
),
4
)
ret
=
lookup_elem
(
fd
,
pack
(
"
B
"
,
self
.
count
),
4
)
return
unpack
(
"
i
"
if
self
.
signed
else
"
I
"
,
ret
)[
0
]
return
unpack
(
"
i
"
if
self
.
signed
else
"
I
"
,
ret
)[
0
]
ret
=
instance
.
__dict__
.
get
(
self
.
name
,
None
)
ret
=
instance
.
__dict__
.
get
(
self
.
name
,
None
)
if
ret
is
None
:
if
ret
is
None
:
...
@@ -57,8 +57,8 @@ class HashGlobalVarDesc:
...
@@ -57,8 +57,8 @@ class HashGlobalVarDesc:
def
__set__
(
self
,
ebpf
,
value
):
def
__set__
(
self
,
ebpf
,
value
):
if
ebpf
.
loaded
:
if
ebpf
.
loaded
:
fd
=
ebpf
.
__dict__
[
self
.
name
].
fd
fd
=
ebpf
.
__dict__
[
self
.
name
].
fd
bpf
.
update_elem
(
fd
,
pack
(
"
B
"
,
self
.
count
),
update_elem
(
fd
,
pack
(
"
B
"
,
self
.
count
),
pack
(
"
i
"
if
self
.
signed
else
"
I
"
,
value
),
0
)
pack
(
"
i
"
if
self
.
signed
else
"
I
"
,
value
),
0
)
return
return
with
ebpf
.
save_registers
([
3
]):
with
ebpf
.
save_registers
([
3
]):
with
value
.
get_address
(
3
,
False
,
self
.
signed
,
True
):
with
value
.
get_address
(
3
,
False
,
self
.
signed
,
True
):
...
@@ -84,7 +84,7 @@ class HashMap(Map):
...
@@ -84,7 +84,7 @@ class HashMap(Map):
return
ret
return
ret
def
init
(
self
,
ebpf
):
def
init
(
self
,
ebpf
):
fd
=
bpf
.
create_map
(
1
,
1
,
4
,
self
.
count
)
fd
=
create_map
(
MapType
.
HASH
,
1
,
4
,
self
.
count
)
for
v
in
self
.
vars
:
for
v
in
self
.
vars
:
getattr
(
ebpf
,
v
.
name
).
fd
=
fd
getattr
(
ebpf
,
v
.
name
).
fd
=
fd
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment