Skip to content

Commit c19baac

Browse files
committed
xdp-forward: Add queueing capability (WiP)
Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
1 parent bad5f36 commit c19baac

File tree

4 files changed

+310
-18
lines changed

4 files changed

+310
-18
lines changed

headers/bpf/vmlinux.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)
66
#endif
77

8+
typedef signed char __s8;
9+
typedef unsigned char __u8;
10+
typedef short int __s16;
11+
typedef short unsigned int __u16;
12+
typedef int __s32;
13+
typedef unsigned int __u32;
14+
typedef long long int __s64;
15+
typedef long long unsigned int __u64;
16+
17+
typedef __s8 s8;
18+
typedef __u8 u8;
19+
typedef __s16 s16;
20+
typedef __u16 u16;
21+
typedef __s32 s32;
22+
typedef __u32 u32;
23+
typedef __s64 s64;
24+
typedef __u64 u64;
25+
26+
827
struct net_device {
928
int ifindex;
1029
};
@@ -21,6 +40,22 @@ struct bpf_prog {
2140
struct bpf_map {
2241
};
2342

43+
struct xdp_mem_info {
44+
u32 type;
45+
u32 id;
46+
};
47+
48+
struct xdp_frame {
49+
void *data;
50+
u16 len;
51+
u16 headroom;
52+
u32 metasize;
53+
struct xdp_mem_info mem;
54+
struct net_device *dev_rx;
55+
u32 frame_sz;
56+
u32 flags;
57+
};
58+
2459
#ifndef BPF_NO_PRESERVE_ACCESS_INDEX
2560
#pragma clang attribute pop
2661
#endif

xdp-forward/xdp-forward.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#include <errno.h>
44
#include <string.h>
55

6+
#include <bpf/libbpf.h>
67
#include <xdp/libxdp.h>
78

89
#include "params.h"
910
#include "util.h"
1011
#include "logging.h"
1112
#include "compat.h"
1213

14+
#include "xdp-forward.h"
1315
#include "xdp_forward.skel.h"
1416

1517
#define MAX_IFACE_NUM 32
@@ -81,6 +83,32 @@ static int find_prog(struct iface *iface, bool detach)
8183
return ret;
8284
}
8385

86+
int init_tx_port(struct xdp_program *init_prog, int ifindex)
87+
{
88+
struct port_init_config port_cfg = {.ifindex = ifindex};
89+
struct xdp_md ctx_in = {
90+
.data_end = sizeof(port_cfg),
91+
};
92+
DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, .data_in = &port_cfg,
93+
.data_size_in = sizeof(port_cfg), .ctx_in = &ctx_in,
94+
.ctx_size_in = sizeof(ctx_in), );
95+
int nr_cpus = libbpf_num_possible_cpus();
96+
int err, i;
97+
98+
for (i = 0; i < nr_cpus; i++) {
99+
port_cfg.cpu = i;
100+
101+
err = xdp_program__test_run(init_prog, &opts, 0);
102+
if (err)
103+
return -errno;
104+
105+
if (opts.retval != XDP_PASS)
106+
return -1;
107+
}
108+
109+
return 0;
110+
}
111+
84112
struct load_opts {
85113
enum fwd_mode fwd_mode;
86114
enum xdp_attach_mode xdp_mode;
@@ -110,10 +138,9 @@ struct prog_option load_options[] = {
110138

111139
static int do_load(const void *cfg, __unused const char *pin_root_path)
112140
{
141+
struct xdp_program *xdp_prog = NULL, *init_prog = NULL;
113142
DECLARE_LIBBPF_OPTS(xdp_program_opts, opts);
114-
struct xdp_program *xdp_prog = NULL;
115143
const struct load_opts *opt = cfg;
116-
struct bpf_program *prog = NULL;
117144
struct xdp_forward *skel;
118145
int ret = EXIT_FAILURE;
119146
struct iface *iface;
@@ -135,12 +162,6 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
135162
goto end;
136163
}
137164

138-
/* Make sure we only load the one XDP program we are interested in */
139-
while ((prog = bpf_object__next_program(skel->obj, prog)) != NULL)
140-
if (bpf_program__type(prog) == BPF_PROG_TYPE_XDP &&
141-
bpf_program__expected_attach_type(prog) == BPF_XDP)
142-
bpf_program__set_autoload(prog, false);
143-
144165
opts.obj = skel->obj;
145166
xdp_prog = xdp_program__create(&opts);
146167
if (!xdp_prog) {
@@ -150,6 +171,15 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
150171
goto end_destroy;
151172
}
152173

174+
opts.prog_name = "init_port";
175+
init_prog = xdp_program__create(&opts);
176+
if (!init_prog) {
177+
ret = -errno;
178+
pr_warn("Couldn't open XDP program: %s\n",
179+
strerror(-ret));
180+
goto end_destroy;
181+
}
182+
153183
/* We always set the frags support bit: nothing the program does is
154184
* incompatible with multibuf, and it's perfectly fine to load a program
155185
* with frags support on an interface with a small MTU. We don't risk
@@ -177,13 +207,13 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
177207
goto end_detach;
178208
}
179209

180-
ret = bpf_map_update_elem(bpf_map__fd(skel->maps.xdp_tx_ports),
181-
&iface->ifindex, &iface->ifindex, 0);
210+
ret = init_tx_port(init_prog, iface->ifindex);
182211
if (ret) {
183-
pr_warn("Failed to update devmap value: %s\n",
212+
pr_warn("Failed to initiate TX port: %s\n",
184213
strerror(errno));
185214
goto end_detach;
186215
}
216+
187217
pr_info("Loaded on interface %s\n", iface->ifname);
188218
}
189219

xdp-forward/xdp-forward.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
struct port_init_config {
4+
int ifindex;
5+
int cpu;
6+
};

0 commit comments

Comments
 (0)