Skip to content

Add AF_XDP samples #30

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kernel/samples/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TARGETS += bpf_tail_calls01
TARGETS += napi_monitor
TARGETS += xdp_monitor
TARGETS += xdp_redirect_err
TARGETS += xdpsock

# Linking with libbpf and libpcap
TARGETS_PCAP += xdp_tcpdump
Expand Down Expand Up @@ -76,7 +77,7 @@ CFLAGS += -I./kernel-usr-include/
# Interacting with libbpf
CFLAGS += -I$(TOOLS_PATH)/lib

LDFLAGS= -lelf
LDFLAGS= -lelf -lpthread

# Objects that xxx_user program is linked with:
OBJECT_LOADBPF = bpf_load.o
Expand Down
11 changes: 11 additions & 0 deletions kernel/samples/bpf/xdpsock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef XDPSOCK_H_
#define XDPSOCK_H_

/* Power-of-2 number of sockets */
#define MAX_SOCKS 4

/* Round-robin receive */
#define RR_LB 0

#endif /* XDPSOCK_H_ */
56 changes: 56 additions & 0 deletions kernel/samples/bpf/xdpsock_kern.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0
#define KBUILD_MODNAME "foo"
#include <uapi/linux/bpf.h>
#include "bpf_helpers.h"

#include "xdpsock.h"

struct bpf_map_def SEC("maps") qidconf_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 1,
};

struct bpf_map_def SEC("maps") xsks_map = {
.type = BPF_MAP_TYPE_XSKMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = MAX_SOCKS,
};

struct bpf_map_def SEC("maps") rr_map = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(unsigned int),
.max_entries = 1,
};

SEC("xdp_sock")
int xdp_sock_prog(struct xdp_md *ctx)
{
int *qidconf, key = 0, idx;
//unsigned int *rr;

qidconf = bpf_map_lookup_elem(&qidconf_map, &key);
if (!qidconf)
return XDP_ABORTED;

if (*qidconf != ctx->rx_queue_index)
return XDP_PASS;

#if RR_LB /* NB! RR_LB is configured in xdpsock.h */
rr = bpf_map_lookup_elem(&rr_map, &key);
if (!rr)
return XDP_ABORTED;

*rr = (*rr + 1) & (MAX_SOCKS - 1);
idx = *rr;
#else
idx = 0;
#endif

return bpf_redirect_map(&xsks_map, idx, 0);
}

char _license[] SEC("license") = "GPL";
Loading