-
Notifications
You must be signed in to change notification settings - Fork 714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
examples: add example for XDP #706
Conversation
Working on fixing this error in the build:
|
@wedaly you can solve that by copying the types you need from the linux headers into your C code, or throw them in the common headers, and then not including header files from linux. |
Copied the types I needed into examples/common.h. Maybe I should pull in the header changes from #711 (or wait until after that PR merges?) to avoid conflicts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice example! Just a few non-critical suggestions.
examples/xdp/main.go
Outdated
var sb strings.Builder | ||
var key, val uint32 | ||
iter := m.Iterate() | ||
for iter.Next(&key, &val) { | ||
sourceIP := net.IPv4(byte(key>>24), byte(key>>16), byte(key>>8), byte(key)) | ||
packetCount := val |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var sb strings.Builder | |
var key, val uint32 | |
iter := m.Iterate() | |
for iter.Next(&key, &val) { | |
sourceIP := net.IPv4(byte(key>>24), byte(key>>16), byte(key>>8), byte(key)) | |
packetCount := val | |
var ( | |
sb strings.Builder | |
key []byte | |
val uint32 | |
iter = m.Iterate() | |
) | |
for iter.Next(&key, &val) { | |
sourceIP := net.IP(key) | |
packetCount := val |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you do the byte swap of the IP in BPF, we can just get key
in the form of []byte
and coerce it to a Go net.IP
. (This should be even easier with go1.18 and netip.Addr
since it implements encoding.BinaryUnmarshaler
!)
It would also make it super easy if someone wanted to adapt this to support IPv6!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call, updated! When I tested this, I noticed after switching to net.IP(key)
, the IP address octets were being printed in reverse order, so I also updated xdp.c
to store them in network byte order (removed the call to bpf_ntohl
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having a hard time getting the ipv6 network byte order thingy working, would be awesome if someone could share an example someday :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anyone care to share the ipv6 example?
Add an example for attaching an eBPF program to a network interface with XDP. The example program parses the IPv4 source address from packets (if available) and records packet counts by IP address in an LRU hash map. Issue: cilium#645 Signed-off-by: Will Daly <[email protected]>
Thanks! |
Add an example for attaching an eBPF program to a network
interface with XDP. The example program parses the IPv4
source address from packets (if available) and records
packet counts by IP address in an LRU hash map.
Issue: #645