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
474cd9c3
Commit
474cd9c3
authored
4 years ago
by
Martin Teichmann
Browse files
Options
Downloads
Patches
Plain Diff
use readable opcodes in tests, kindof
parent
783a3b72
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ebpf.py
+6
-15
6 additions, 15 deletions
ebpf.py
ebpf_test.py
+23
-1
23 additions, 1 deletion
ebpf_test.py
with
29 additions
and
16 deletions
ebpf.py
+
6
−
15
View file @
474cd9c3
...
@@ -41,8 +41,8 @@ class Opcode(Enum):
...
@@ -41,8 +41,8 @@ class Opcode(Enum):
REG
=
8
REG
=
8
LONG
=
3
LONG
=
3
H
=
8
W
=
0
W
=
0
H
=
8
B
=
0x10
B
=
0x10
DW
=
0x18
DW
=
0x18
...
@@ -60,13 +60,7 @@ class Opcode(Enum):
...
@@ -60,13 +60,7 @@ class Opcode(Enum):
return
OpcodeFlags
({
self
})
+
value
return
OpcodeFlags
({
self
})
+
value
def
__repr__
(
self
):
def
__repr__
(
self
):
return
self
.
name
return
'
O.
'
+
self
.
name
def
__eq__
(
self
,
value
):
return
self
is
value
or
self
.
value
==
value
def
__hash__
(
self
):
return
super
().
__hash__
()
class
OpcodeFlags
:
class
OpcodeFlags
:
def
__init__
(
self
,
opcodes
):
def
__init__
(
self
,
opcodes
):
...
@@ -83,13 +77,10 @@ class OpcodeFlags:
...
@@ -83,13 +77,10 @@ class OpcodeFlags:
return
OpcodeFlags
(
self
.
opcodes
|
value
.
opcodes
)
return
OpcodeFlags
(
self
.
opcodes
|
value
.
opcodes
)
def
__repr__
(
self
):
def
__repr__
(
self
):
return
"
|
"
.
join
(
op
.
name
for
op
in
self
.
opcodes
)
return
"
+
"
.
join
(
repr
(
op
)
for
op
in
self
.
opcodes
)
def
__eq__
(
self
,
value
):
def
__eq__
(
self
,
value
):
if
isinstance
(
value
,
int
):
return
self
.
value
==
value
.
value
return
self
.
value
==
value
else
:
self
.
opcodes
==
value
.
opcodes
class
AssembleError
(
Exception
):
class
AssembleError
(
Exception
):
...
@@ -468,7 +459,7 @@ class PseudoFd(Expression):
...
@@ -468,7 +459,7 @@ class PseudoFd(Expression):
else
:
else
:
free
=
False
free
=
False
self
.
ebpf
.
append
(
Opcode
.
DW
,
dst
,
1
,
0
,
self
.
fd
)
self
.
ebpf
.
append
(
Opcode
.
DW
,
dst
,
1
,
0
,
self
.
fd
)
self
.
ebpf
.
append
(
0
,
0
,
0
,
0
,
0
)
self
.
ebpf
.
append
(
Opcode
.
W
,
0
,
0
,
0
,
0
)
return
dst
,
long
,
signed
,
free
return
dst
,
long
,
signed
,
free
...
@@ -492,7 +483,7 @@ class RegisterDesc:
...
@@ -492,7 +483,7 @@ class RegisterDesc:
self
.
no
,
0
,
0
,
value
)
self
.
no
,
0
,
0
,
value
)
else
:
else
:
instance
.
append
(
Opcode
.
DW
,
self
.
no
,
0
,
0
,
value
&
0xffffffff
)
instance
.
append
(
Opcode
.
DW
,
self
.
no
,
0
,
0
,
value
&
0xffffffff
)
instance
.
append
(
0
,
0
,
0
,
0
,
value
>>
32
)
instance
.
append
(
Opcode
.
W
,
0
,
0
,
0
,
value
>>
32
)
elif
isinstance
(
value
,
Expression
):
elif
isinstance
(
value
,
Expression
):
value
.
calculate
(
self
.
no
,
self
.
long
,
self
.
signed
,
True
)
value
.
calculate
(
self
.
no
,
self
.
long
,
self
.
signed
,
True
)
elif
isinstance
(
value
,
Instruction
):
elif
isinstance
(
value
,
Instruction
):
...
...
This diff is collapsed.
Click to expand it.
ebpf_test.py
+
23
−
1
View file @
474cd9c3
from
unittest
import
TestCase
,
main
from
unittest
import
TestCase
,
main
from
.ebpf
import
AssembleError
,
EBPF
,
Instruction
from
.
import
ebpf
from
.ebpf
import
AssembleError
,
EBPF
,
Opcode
,
OpcodeFlags
,
Opcode
as
O
from
.bpf
import
ProgType
from
.bpf
import
ProgType
opcodes
=
list
((
v
.
value
,
v
)
for
v
in
Opcode
)
opcodes
.
sort
(
reverse
=
True
)
def
Instruction
(
opcode
,
dst
,
src
,
off
,
imm
):
if
isinstance
(
opcode
,
OpcodeFlags
):
return
ebpf
.
Instruction
(
opcode
,
dst
,
src
,
off
,
imm
)
bigger
=
[(
k
,
v
)
for
k
,
v
in
opcodes
if
opcode
>=
k
]
for
bk
,
bv
in
bigger
:
parts
=
{
bv
}
lo
=
opcode
-
bk
for
k
,
v
in
opcodes
[:
-
1
]:
if
lo
>=
k
:
lo
-=
k
parts
.
add
(
v
)
if
lo
==
0
:
break
else
:
raise
RuntimeError
return
ebpf
.
Instruction
(
OpcodeFlags
(
parts
),
dst
,
src
,
off
,
imm
)
class
Tests
(
TestCase
):
class
Tests
(
TestCase
):
def
test_assemble
(
self
):
def
test_assemble
(
self
):
e
=
EBPF
()
e
=
EBPF
()
...
...
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