Skip to content
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

Merged
merged 1 commit into from
Jun 15, 2022
Merged

examples: add example for XDP #706

merged 1 commit into from
Jun 15, 2022

Conversation

wedaly
Copy link
Member

@wedaly wedaly commented Jun 10, 2022

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

@wedaly
Copy link
Member Author

wedaly commented Jun 14, 2022

Working on fixing this error in the build:

In file included from /ebpf/examples/xdp/xdp.c:5:
In file included from /usr/include/linux/if_ether.h:25:
/usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
#include <asm/types.h>
         ^~~~~~~~~~~~~

@markpash
Copy link
Contributor

Working on fixing this error in the build:

In file included from /ebpf/examples/xdp/xdp.c:5:
In file included from /usr/include/linux/if_ether.h:25:
/usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
#include <asm/types.h>
         ^~~~~~~~~~~~~

@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.

@wedaly
Copy link
Member Author

wedaly commented Jun 14, 2022

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?

Copy link
Contributor

@markpash markpash left a 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.

Comment on lines 75 to 83
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Contributor

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!

Copy link
Member Author

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)

Copy link

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 :)

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?

examples/xdp/xdp.c Outdated Show resolved Hide resolved
examples/xdp/xdp.c Outdated Show resolved Hide resolved
examples/xdp/xdp.c Outdated Show resolved Hide resolved
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]>
@lmb
Copy link
Collaborator

lmb commented Jun 15, 2022

Thanks!

@lmb lmb merged commit 4bf78cd into cilium:master Jun 15, 2022
@wedaly wedaly deleted the xdp-example branch July 12, 2023 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants