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
ae059841
Commit
ae059841
authored
Jul 28, 2016
by
Brenden Blanco
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support to xdp_drop_count for clsact mode
Signed-off-by:
Brenden Blanco
<
bblanco@plumgrid.com
>
parent
8db68e81
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
8 deletions
+32
-8
examples/networking/xdp/xdp_drop_count.py
examples/networking/xdp/xdp_drop_count.py
+32
-8
No files found.
examples/networking/xdp/xdp_drop_count.py
View file @
ae059841
...
...
@@ -8,15 +8,26 @@
# Licensed under the Apache License, Version 2.0 (the "License")
from
bcc
import
BPF
import
pyroute2
import
time
import
sys
if
(
len
(
sys
.
argv
)
!=
2
)
:
if
len
(
sys
.
argv
)
!=
2
:
print
(
"Usage: {0} <ifdev>
\
n
\
n
e.g.: {0} eth0"
.
format
(
sys
.
argv
[
0
]))
exit
(
1
)
device
=
sys
.
argv
[
1
]
mode
=
BPF
.
XDP
#mode = BPF.SCHED_CLS
if
mode
==
BPF
.
XDP
:
ret
=
"XDP_DROP"
ctxtype
=
"xdp_md"
else
:
ret
=
"TC_ACT_SHOT"
ctxtype
=
"__sk_buff"
# load BPF program
b
=
BPF
(
text
=
"""
#define KBUILD_MODNAME "foo"
...
...
@@ -47,7 +58,7 @@ static inline int parse_ipv6(void *data, u64 nh_off, void *data_end) {
return ip6h->nexthdr;
}
int xdp_prog1(struct
xdp_md
*ctx) {
int xdp_prog1(struct
CTXTYPE
*ctx) {
void* data_end = (void*)(long)ctx->data_end;
void* data = (void*)(long)ctx->data;
...
...
@@ -55,7 +66,7 @@ int xdp_prog1(struct xdp_md *ctx) {
struct ethhdr *eth = data;
// drop packets
int rc =
XDP_DROP
; // let pass XDP_PASS or redirect to tx via XDP_TX
int rc =
RETURNCODE
; // let pass XDP_PASS or redirect to tx via XDP_TX
long *value;
uint16_t h_proto;
uint64_t nh_off = 0;
...
...
@@ -100,12 +111,21 @@ int xdp_prog1(struct xdp_md *ctx) {
return rc;
}
"""
,
cflags
=
[
"-w"
])
"""
,
cflags
=
[
"-w"
,
"-DRETURNCODE=%s"
%
ret
,
"-DCTXTYPE=%s"
%
ctxtype
])
xdp_func
=
b
.
load_func
(
"xdp_prog1"
,
BPF
.
XDP
)
b
.
attach_xdp
(
device
,
xdp_func
)
dropcnt
=
b
.
get_table
(
"dropcnt"
)
fn
=
b
.
load_func
(
"xdp_prog1"
,
mode
)
if
mode
==
BPF
.
XDP
:
b
.
attach_xdp
(
device
,
fn
)
else
:
ip
=
pyroute2
.
IPRoute
()
ipdb
=
pyroute2
.
IPDB
(
nl
=
ip
)
idx
=
ipdb
.
interfaces
[
device
].
index
ip
.
tc
(
"add"
,
"clsact"
,
idx
)
ip
.
tc
(
"add-filter"
,
"bpf"
,
idx
,
":1"
,
fd
=
fn
.
fd
,
name
=
fn
.
name
,
parent
=
"ffff:fff2"
,
classid
=
1
,
direct_action
=
True
)
dropcnt
=
b
.
get_table
(
"dropcnt"
)
prev
=
[
0
]
*
256
print
(
"Printing drops per IP protocol-number, hit CTRL+C to stop"
)
while
1
:
...
...
@@ -122,4 +142,8 @@ while 1:
print
(
"Removing filter from device"
)
break
;
b
.
remove_xdp
(
device
)
if
mode
==
BPF
.
XDP
:
b
.
remove_xdp
(
device
)
else
:
ip
.
tc
(
"del"
,
"clsact"
,
idx
)
ipdb
.
release
()
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