Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bcc
Commits
8cea2e95
Commit
8cea2e95
authored
Aug 12, 2015
by
4ast
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #127 from iovisor/bblanco_dev
Changes to support map entry read/write in fuse
parents
f04003e1
2582ecfc
Changes
7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
182 additions
and
131 deletions
+182
-131
src/cc/bpf_common.cc
src/cc/bpf_common.cc
+18
-13
src/cc/bpf_common.h
src/cc/bpf_common.h
+3
-4
src/cc/bpf_module.cc
src/cc/bpf_module.cc
+96
-96
src/cc/bpf_module.h
src/cc/bpf_module.h
+3
-2
src/cc/table_desc.h
src/cc/table_desc.h
+4
-4
src/python/bpf/__init__.py
src/python/bpf/__init__.py
+54
-10
tests/cc/test_clang.py
tests/cc/test_clang.py
+4
-2
No files found.
src/cc/bpf_common.cc
View file @
8cea2e95
...
...
@@ -104,6 +104,12 @@ size_t bpf_num_tables(void *program) {
return
mod
->
num_tables
();
}
size_t
bpf_table_id
(
void
*
program
,
const
char
*
table_name
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
~
0ull
;
return
mod
->
table_id
(
table_name
);
}
int
bpf_table_fd
(
void
*
program
,
const
char
*
table_name
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
-
1
;
...
...
@@ -170,27 +176,26 @@ size_t bpf_table_leaf_size_id(void *program, size_t id) {
return
mod
->
table_leaf_size
(
id
);
}
int
bpf_table_
update
(
void
*
program
,
const
char
*
table_name
,
const
char
*
key
,
const
char
*
leaf
)
{
int
bpf_table_
key_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
0
;
return
mod
->
table_
update
(
table_name
,
key
,
leaf
);
if
(
!
mod
)
return
-
1
;
return
mod
->
table_
key_printf
(
id
,
buf
,
buflen
,
key
);
}
int
bpf_table_update_id
(
void
*
program
,
size_t
id
,
const
char
*
key
,
const
char
*
leaf
)
{
int
bpf_table_leaf_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
0
;
return
mod
->
table_
update
(
id
,
key
,
leaf
);
if
(
!
mod
)
return
-
1
;
return
mod
->
table_
leaf_printf
(
id
,
buf
,
buflen
,
leaf
);
}
int
bpf_table_key_s
nprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
)
{
int
bpf_table_key_s
scanf
(
void
*
program
,
size_t
id
,
const
char
*
buf
,
void
*
key
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
0
;
return
mod
->
table_key_
printf
(
id
,
buf
,
buflen
,
key
);
if
(
!
mod
)
return
-
1
;
return
mod
->
table_key_
scanf
(
id
,
buf
,
key
);
}
int
bpf_table_leaf_s
nprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
)
{
int
bpf_table_leaf_s
scanf
(
void
*
program
,
size_t
id
,
const
char
*
buf
,
void
*
leaf
)
{
auto
mod
=
static_cast
<
ebpf
::
BPFModule
*>
(
program
);
if
(
!
mod
)
return
0
;
return
mod
->
table_
key_printf
(
id
,
buf
,
buflen
,
leaf
);
if
(
!
mod
)
return
-
1
;
return
mod
->
table_
leaf_scanf
(
id
,
buf
,
leaf
);
}
}
src/cc/bpf_common.h
View file @
8cea2e95
...
...
@@ -37,6 +37,7 @@ void * bpf_function_start(void *program, const char *name);
size_t
bpf_function_size_id
(
void
*
program
,
size_t
id
);
size_t
bpf_function_size
(
void
*
program
,
const
char
*
name
);
size_t
bpf_num_tables
(
void
*
program
);
size_t
bpf_table_id
(
void
*
program
,
const
char
*
table_name
);
int
bpf_table_fd
(
void
*
program
,
const
char
*
table_name
);
int
bpf_table_fd_id
(
void
*
program
,
size_t
id
);
const
char
*
bpf_table_name
(
void
*
program
,
size_t
id
);
...
...
@@ -50,10 +51,8 @@ size_t bpf_table_leaf_size(void *program, const char *table_name);
size_t
bpf_table_leaf_size_id
(
void
*
program
,
size_t
id
);
int
bpf_table_key_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
);
int
bpf_table_leaf_snprintf
(
void
*
program
,
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
);
//int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key);
//int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf);
int
bpf_table_update
(
void
*
program
,
const
char
*
table_name
,
const
char
*
key
,
const
char
*
leaf
);
int
bpf_table_update_id
(
void
*
program
,
size_t
id
,
const
char
*
key
,
const
char
*
leaf
);
int
bpf_table_key_sscanf
(
void
*
program
,
size_t
id
,
const
char
*
buf
,
void
*
key
);
int
bpf_table_leaf_sscanf
(
void
*
program
,
size_t
id
,
const
char
*
buf
,
void
*
leaf
);
#ifdef __cplusplus
}
...
...
src/cc/bpf_module.cc
View file @
8cea2e95
This diff is collapsed.
Click to expand it.
src/cc/bpf_module.h
View file @
8cea2e95
...
...
@@ -64,6 +64,7 @@ class BPFModule {
size_t
function_size
(
size_t
id
)
const
;
size_t
function_size
(
const
std
::
string
&
name
)
const
;
size_t
num_tables
()
const
;
size_t
table_id
(
const
std
::
string
&
name
)
const
;
int
table_fd
(
size_t
id
)
const
;
int
table_fd
(
const
std
::
string
&
name
)
const
;
const
char
*
table_name
(
size_t
id
)
const
;
...
...
@@ -72,13 +73,13 @@ class BPFModule {
size_t
table_key_size
(
size_t
id
)
const
;
size_t
table_key_size
(
const
std
::
string
&
name
)
const
;
int
table_key_printf
(
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
key
);
int
table_key_scanf
(
size_t
id
,
const
char
*
buf
,
void
*
key
);
const
char
*
table_leaf_desc
(
size_t
id
)
const
;
const
char
*
table_leaf_desc
(
const
std
::
string
&
name
)
const
;
size_t
table_leaf_size
(
size_t
id
)
const
;
size_t
table_leaf_size
(
const
std
::
string
&
name
)
const
;
int
table_leaf_printf
(
size_t
id
,
char
*
buf
,
size_t
buflen
,
const
void
*
leaf
);
int
table_update
(
size_t
id
,
const
char
*
key
,
const
char
*
leaf
);
int
table_update
(
const
std
::
string
&
name
,
const
char
*
key
,
const
char
*
leaf
);
int
table_leaf_scanf
(
size_t
id
,
const
char
*
buf
,
void
*
leaf
);
char
*
license
()
const
;
unsigned
kern_version
()
const
;
private:
...
...
src/cc/table_desc.h
View file @
8cea2e95
...
...
@@ -31,10 +31,10 @@ struct TableDesc {
size_t
max_entries
;
std
::
string
key_desc
;
std
::
string
leaf_desc
;
llvm
::
Function
*
key_
reader
;
llvm
::
Function
*
leaf_
reader
;
llvm
::
Function
*
key_
writer
;
llvm
::
Function
*
leaf_
writer
;
llvm
::
Function
*
key_
sscanf
;
llvm
::
Function
*
leaf_
sscanf
;
llvm
::
Function
*
key_
snprintf
;
llvm
::
Function
*
leaf_
snprintf
;
};
}
// namespace ebpf
src/python/bpf/__init__.py
View file @
8cea2e95
...
...
@@ -39,14 +39,26 @@ lib.bpf_function_start.restype = ct.c_void_p
lib
.
bpf_function_start
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
]
lib
.
bpf_function_size
.
restype
=
ct
.
c_size_t
lib
.
bpf_function_size
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
]
lib
.
bpf_table_id
.
restype
=
ct
.
c_ulonglong
lib
.
bpf_table_id
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
]
lib
.
bpf_table_fd
.
restype
=
ct
.
c_int
lib
.
bpf_table_fd
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
]
lib
.
bpf_table_key_desc
.
restype
=
ct
.
c_char_p
lib
.
bpf_table_key_desc
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
]
lib
.
bpf_table_leaf_desc
.
restype
=
ct
.
c_char_p
lib
.
bpf_table_leaf_desc
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
]
lib
.
bpf_table_update
.
restype
=
ct
.
c_int
lib
.
bpf_table_update
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_char_p
,
ct
.
c_char_p
,
ct
.
c_char_p
]
lib
.
bpf_table_key_snprintf
.
restype
=
ct
.
c_int
lib
.
bpf_table_key_snprintf
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_ulonglong
,
ct
.
c_char_p
,
ct
.
c_ulonglong
,
ct
.
c_void_p
]
lib
.
bpf_table_leaf_snprintf
.
restype
=
ct
.
c_int
lib
.
bpf_table_leaf_snprintf
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_ulonglong
,
ct
.
c_char_p
,
ct
.
c_ulonglong
,
ct
.
c_void_p
]
lib
.
bpf_table_key_sscanf
.
restype
=
ct
.
c_int
lib
.
bpf_table_key_sscanf
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_ulonglong
,
ct
.
c_char_p
,
ct
.
c_void_p
]
lib
.
bpf_table_leaf_sscanf
.
restype
=
ct
.
c_int
lib
.
bpf_table_leaf_sscanf
.
argtypes
=
[
ct
.
c_void_p
,
ct
.
c_ulonglong
,
ct
.
c_char_p
,
ct
.
c_void_p
]
# keep in sync with libbpf.h
lib
.
bpf_get_next_key
.
restype
=
ct
.
c_int
...
...
@@ -92,12 +104,49 @@ class BPF(object):
self
.
fd
=
fd
class
Table
(
MutableMapping
):
def
__init__
(
self
,
bpf
,
map_fd
,
keytype
,
leaftype
):
def
__init__
(
self
,
bpf
,
map_
id
,
map_
fd
,
keytype
,
leaftype
):
self
.
bpf
=
bpf
self
.
map_id
=
map_id
self
.
map_fd
=
map_fd
self
.
Key
=
keytype
self
.
Leaf
=
leaftype
def
key_sprintf
(
self
,
key
):
key_p
=
ct
.
pointer
(
key
)
buf
=
ct
.
create_string_buffer
(
ct
.
sizeof
(
self
.
Key
)
*
8
)
res
=
lib
.
bpf_table_key_snprintf
(
self
.
bpf
.
module
,
self
.
map_id
,
buf
,
len
(
buf
),
key_p
)
if
res
<
0
:
raise
Exception
(
"Could not printf key"
)
return
buf
.
value
def
leaf_sprintf
(
self
,
leaf
):
leaf_p
=
ct
.
pointer
(
leaf
)
buf
=
ct
.
create_string_buffer
(
ct
.
sizeof
(
self
.
Leaf
)
*
8
)
res
=
lib
.
bpf_table_leaf_snprintf
(
self
.
bpf
.
module
,
self
.
map_id
,
buf
,
len
(
buf
),
leaf_p
)
if
res
<
0
:
raise
Exception
(
"Could not printf leaf"
)
return
buf
.
value
def
key_scanf
(
self
,
key_str
):
key
=
self
.
Key
()
key_p
=
ct
.
pointer
(
key
)
res
=
lib
.
bpf_table_key_sscanf
(
self
.
bpf
.
module
,
self
.
map_id
,
key_str
,
key_p
)
if
res
<
0
:
raise
Exception
(
"Could not scanf key"
)
return
key
def
leaf_scanf
(
self
,
leaf_str
):
leaf
=
self
.
Leaf
()
leaf_p
=
ct
.
pointer
(
leaf
)
res
=
lib
.
bpf_table_leaf_sscanf
(
self
.
bpf
.
module
,
self
.
map_id
,
leaf_str
,
leaf_p
)
if
res
<
0
:
raise
Exception
(
"Could not scanf leaf"
)
return
leaf
def
__getitem__
(
self
,
key
):
key_p
=
ct
.
pointer
(
key
)
leaf
=
self
.
Leaf
()
...
...
@@ -245,6 +294,7 @@ class BPF(object):
return
cls
def
get_table
(
self
,
name
,
keytype
=
None
,
leaftype
=
None
):
map_id
=
lib
.
bpf_table_id
(
self
.
module
,
name
.
encode
(
"ascii"
))
map_fd
=
lib
.
bpf_table_fd
(
self
.
module
,
name
.
encode
(
"ascii"
))
if
map_fd
<
0
:
raise
Exception
(
"Failed to find BPF Table %s"
%
name
)
...
...
@@ -258,13 +308,7 @@ class BPF(object):
if
not
leaf_desc
:
raise
Exception
(
"Failed to load BPF Table %s leaf desc"
%
name
)
leaftype
=
BPF
.
_decode_table_type
(
json
.
loads
(
leaf_desc
.
decode
()))
return
BPF
.
Table
(
self
,
map_fd
,
keytype
,
leaftype
)
def
update_table
(
self
,
name
,
key
,
leaf
):
res
=
lib
.
bpf_table_update
(
self
.
module
,
name
.
encode
(
"ascii"
),
key
.
encode
(
"ascii"
),
leaf
.
encode
(
"ascii"
))
if
res
<
0
:
raise
Exception
(
"update_table failed"
)
return
BPF
.
Table
(
self
,
map_id
,
map_fd
,
keytype
,
leaftype
)
@
staticmethod
def
attach_raw_socket
(
fn
,
dev
):
...
...
tests/cc/test_clang.py
View file @
8cea2e95
...
...
@@ -55,9 +55,11 @@ int foo(void *ctx) {
"""
b
=
BPF
(
text
=
text
,
debug
=
0
)
fn
=
b
.
load_func
(
"foo"
,
BPF
.
KPROBE
)
b
.
update_table
(
"stats"
,
"2"
,
"{ 2 3 0x1000000004 { 5 6 }}"
)
t
=
b
.
get_table
(
"stats"
)
l
=
t
[
t
.
Key
(
2
)]
s1
=
t
.
key_sprintf
(
t
.
Key
(
2
))
self
.
assertEqual
(
s1
,
b"0x2"
)
s2
=
t
.
leaf_sprintf
(
t
.
Leaf
(
2
,
3
,
4
,
1
,
(
5
,
6
)))
l
=
t
.
leaf_scanf
(
s2
)
self
.
assertEqual
(
l
.
a
,
2
)
self
.
assertEqual
(
l
.
b
,
3
)
self
.
assertEqual
(
l
.
c
,
4
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment