Skip to content

Commit 9fe3158

Browse files
xdp-forward: splict fwd-mode in fib-mode and fwd-mode
Signed-off-by: Lorenzo Bianconi <[email protected]>
1 parent 12e7241 commit 9fe3158

File tree

5 files changed

+98
-42
lines changed

5 files changed

+98
-42
lines changed

xdp-forward/README.org

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ Specifies which forwarding mode =xdp-forward= should operate in. Depending on
5656
the mode selected, =xdp-forward= will perform forwarding in different ways,
5757
which can lead to different behaviour, including which subset of kernel
5858
configuration (such as firewall rules) is respected during forwarding. See the
59-
section *OPERATING MODES* below for a full description of each mode.
59+
section *FORWARDING MODES* below for a full description of each mode.
60+
61+
** -F, --fib-mode <mode>
62+
Specifies how =xdp-forward= performs routing table lookup in the linux kernel.
63+
See the section *FIB MODES* below for a full description of each mode.
6064

6165
** -m, --mode <mode>
6266
Specifies which mode to load the XDP program to be loaded in. The valid values
@@ -98,12 +102,12 @@ Enable debug logging. Specify twice for even more verbosity.
98102
** -h, --help
99103
Display a summary of the available options
100104

101-
* OPERATING MODES
102-
The =xdp-forward= utility supports the following operating modes (selected by
105+
* FORWARDING MODES
106+
The =xdp-forward= utility supports the following forwarding modes (selected by
103107
the =--fwd-mode= parameter to =xdp-forward load=.
104108

105-
** fib-full (default)
106-
In the =fib-full= operating mode, =xdp-forward= will perform a full lookup in
109+
** fib (default)
110+
In the =fib= forwarding mode, =xdp-forward= will perform a lookup in
107111
the kernel routing table (or FIB) for each packet, and forward packets between
108112
the configured interfaces based on the result of the lookup. Any packet where
109113
the lookup fails will be passed up to the stack. This includes packets that
@@ -115,19 +119,29 @@ Note that no checks other than the FIB lookup is performed; in particular, this
115119
completely bypasses the netfilter subsystem, so firewall rules will not be
116120
checked before forwarding.
117121

118-
** fib-direct
119-
The =fib-direct= mode functions like =fib-full=, except it passes the
120-
=BPF_FIB_LOOKUP_DIRECT= flag to the FIB lookup routine. This means that any
121-
policy routing rules configured will be skipped during the lookup, which can
122-
improve performance (but won't obey the policy of those rules, obviously).
123-
124122
** flowtable
125123
The =flowtable= operating mode offloads netfilter sw flowtable logic in
126124
the XDP layer if the hardware flowtable is not available.
127125
At the moment =xdp-forward= is able to offload just TCP or UDP netfilter
128126
flowtable entries to XDP. The user is supposed to configure the flowtable
129127
separately.
130128

129+
* FIB MODES
130+
The =xdp-forward= utility supports the following fib modes (selected by
131+
the =--fib-mode= parameter to =xdp-forward load=.
132+
133+
** full (default)
134+
In the =full= operating mode, =xdp-forward= will perform a full lookup in
135+
the kernel routing table (or FIB) for each packet, and forward packets between
136+
the configured interfaces based on the result of the lookup. In particular,
137+
it will apply any policy routing rules configured by the user.
138+
139+
** direct
140+
The =direct= mode functions like =full=, except it passes the
141+
=BPF_FIB_LOOKUP_DIRECT= flag to the FIB lookup routine. This means that any
142+
policy routing rules configured will be skipped during the lookup, which can
143+
improve performance (but won't obey the policy of those rules, obviously).
144+
131145
* Examples
132146

133147
In order to enable flowtable offloading for tcp and udp traffic between NICs

xdp-forward/tests/test-xdp-forward.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test_fwd_full()
2727
# veth NAPI GRO support added this symbol; forwarding won't work without it
2828
skip_if_missing_kernel_symbol veth_set_features
2929

30-
check_run $XDP_FORWARD load -f fib-full ${NS_NAMES[@]}
30+
check_run $XDP_FORWARD load -f fib -F full ${NS_NAMES[@]}
3131
for ip in "${ALL_INSIDE_IP4[@]}"; do
3232
check_run ns_exec ping -c 1 -W 2 $ip
3333
done
@@ -42,7 +42,7 @@ test_fwd_direct()
4242
# veth NAPI GRO support added this symbol; forwarding won't work without it
4343
skip_if_missing_kernel_symbol veth_set_features
4444

45-
check_run $XDP_FORWARD load -f fib-direct ${NS_NAMES[@]}
45+
check_run $XDP_FORWARD load -f fib -F direct ${NS_NAMES[@]}
4646
for ip in "${ALL_INSIDE_IP4[@]}"; do
4747
check_run ns_exec ping -c 1 -W 2 $ip
4848
done

xdp-forward/xdp-forward.8

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH "xdp-forward" "8" "SEPTEMBER 27, 2024" "V1.4.3" "XDP program loader"
1+
.TH "xdp-forward" "8" "OCTOBER 2, 2024" "V1.4.3" "XDP program loader"
22

33
.SH "NAME"
44
xdp-forward \- the XDP forwarding plane
@@ -62,7 +62,12 @@ Specifies which forwarding mode \fIxdp\-forward\fP should operate in. Depending
6262
the mode selected, \fIxdp\-forward\fP will perform forwarding in different ways,
6363
which can lead to different behaviour, including which subset of kernel
6464
configuration (such as firewall rules) is respected during forwarding. See the
65-
section \fBOPERATING MODES\fP below for a full description of each mode.
65+
section \fBFORWARDING MODES\fP below for a full description of each mode.
66+
67+
.SS "-F, --fib-mode <mode>"
68+
.PP
69+
Specifies how \fIxdp\-forward\fP performs routing table lookup in the linux kernel.
70+
See the section \fBFIB MODES\fP below for a full description of each mode.
6671

6772
.SS "-m, --mode <mode>"
6873
.PP
@@ -114,14 +119,14 @@ Enable debug logging. Specify twice for even more verbosity.
114119
.PP
115120
Display a summary of the available options
116121

117-
.SH "OPERATING MODES"
122+
.SH "FORWARDING MODES"
118123
.PP
119-
The \fIxdp\-forward\fP utility supports the following operating modes (selected by
124+
The \fIxdp\-forward\fP utility supports the following forwarding modes (selected by
120125
the \fI\-\-fwd\-mode\fP parameter to \fIxdp\-forward load\fP.
121126

122-
.SS "fib-full (default)"
127+
.SS "fib (default)"
123128
.PP
124-
In the \fIfib\-full\fP operating mode, \fIxdp\-forward\fP will perform a full lookup in
129+
In the \fIfib\fP forwarding mode, \fIxdp\-forward\fP will perform a lookup in
125130
the kernel routing table (or FIB) for each packet, and forward packets between
126131
the configured interfaces based on the result of the lookup. Any packet where
127132
the lookup fails will be passed up to the stack. This includes packets that
@@ -134,13 +139,6 @@ Note that no checks other than the FIB lookup is performed; in particular, this
134139
completely bypasses the netfilter subsystem, so firewall rules will not be
135140
checked before forwarding.
136141

137-
.SS "fib-direct"
138-
.PP
139-
The \fIfib\-direct\fP mode functions like \fIfib\-full\fP, except it passes the
140-
\fIBPF_FIB_LOOKUP_DIRECT\fP flag to the FIB lookup routine. This means that any
141-
policy routing rules configured will be skipped during the lookup, which can
142-
improve performance (but won't obey the policy of those rules, obviously).
143-
144142
.SS "flowtable"
145143
.PP
146144
The \fIflowtable\fP operating mode offloads netfilter sw flowtable logic in
@@ -149,6 +147,25 @@ At the moment \fIxdp\-forward\fP is able to offload just TCP or UDP netfilter
149147
flowtable entries to XDP. The user is supposed to configure the flowtable
150148
separately.
151149

150+
.SH "FIB MODES"
151+
.PP
152+
The \fIxdp\-forward\fP utility supports the following fib modes (selected by
153+
the \fI\-\-fib\-mode\fP parameter to \fIxdp\-forward load\fP.
154+
155+
.SS "full (default)"
156+
.PP
157+
In the \fIfull\fP operating mode, \fIxdp\-forward\fP will perform a full lookup in
158+
the kernel routing table (or FIB) for each packet, and forward packets between
159+
the configured interfaces based on the result of the lookup. In particular,
160+
it will apply any policy routing rules configured by the user.
161+
162+
.SS "direct"
163+
.PP
164+
The \fIdirect\fP mode functions like \fIfull\fP, except it passes the
165+
\fIBPF_FIB_LOOKUP_DIRECT\fP flag to the FIB lookup routine. This means that any
166+
policy routing rules configured will be skipped during the lookup, which can
167+
improve performance (but won't obey the policy of those rules, obviously).
168+
152169
.SH "Examples"
153170
.PP
154171
In order to enable flowtable offloading for tcp and udp traffic between NICs

xdp-forward/xdp-forward.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ struct enum_val xdp_modes[] = { { "native", XDP_MODE_NATIVE },
3737
{ NULL, 0 } };
3838

3939
enum fwd_mode {
40-
FWD_FIB_DIRECT,
41-
FWD_FIB_FULL,
40+
FWD_FIB,
4241
FWD_FLOWTABLE,
4342
};
4443

45-
struct enum_val fwd_modes[] = { { "fib-direct", FWD_FIB_DIRECT },
46-
{ "fib-full", FWD_FIB_FULL },
44+
struct enum_val fwd_modes[] = { { "fib", FWD_FIB },
4745
{ "flowtable", FWD_FLOWTABLE },
4846
{ NULL, 0 } };
4947

48+
enum fib_mode {
49+
FIB_DIRECT,
50+
FIB_FULL,
51+
};
52+
53+
struct enum_val fib_modes[] = { { "direct", FIB_DIRECT },
54+
{ "full", FIB_FULL },
55+
{ NULL, 0 } };
56+
5057
static int find_prog(struct iface *iface, bool detach)
5158
{
5259
struct xdp_program *prog = NULL;
@@ -88,16 +95,22 @@ static int find_prog(struct iface *iface, bool detach)
8895

8996
struct load_opts {
9097
enum fwd_mode fwd_mode;
98+
enum fib_mode fib_mode;
9199
enum xdp_attach_mode xdp_mode;
92100
struct iface *ifaces;
93-
} defaults_load = { .fwd_mode = FWD_FIB_FULL };
101+
} defaults_load = { .fwd_mode = FWD_FIB, .fib_mode = FIB_FULL, };
94102

95103
struct prog_option load_options[] = {
96104
DEFINE_OPTION("fwd-mode", OPT_ENUM, struct load_opts, fwd_mode,
97105
.short_opt = 'f',
98106
.typearg = fwd_modes,
99-
.metavar = "<mode>",
100-
.help = "Forward mode to run in; see man page. Default fib-full"),
107+
.metavar = "<fwd-mode>",
108+
.help = "Forward mode to run in; see man page. Default fib"),
109+
DEFINE_OPTION("fib-mode", OPT_ENUM, struct load_opts, fib_mode,
110+
.short_opt = 'F',
111+
.typearg = fib_modes,
112+
.metavar = "<fib-mode>",
113+
.help = "Fib mode to run in; see man page. Default full"),
101114
DEFINE_OPTION("xdp-mode", OPT_ENUM, struct load_opts, xdp_mode,
102115
.short_opt = 'm',
103116
.typearg = xdp_modes,
@@ -138,14 +151,14 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
138151
void *skel;
139152

140153
switch (opt->fwd_mode) {
141-
case FWD_FIB_FULL:
142-
opts.prog_name = "xdp_fwd_fib_full";
143-
break;
144-
case FWD_FIB_DIRECT:
145-
opts.prog_name = "xdp_fwd_fib_direct";
154+
case FWD_FIB:
155+
opts.prog_name = opt->fib_mode == FIB_DIRECT
156+
? "xdp_fwd_fib_direct" : "xdp_fwd_fib_full";
146157
break;
147158
case FWD_FLOWTABLE:
148-
opts.prog_name = "xdp_fwd_flowtable";
159+
opts.prog_name = opt->fib_mode == FIB_DIRECT
160+
? "xdp_fwd_flowtable_direct"
161+
: "xdp_fwd_flowtable_full";
149162
break;
150163
default:
151164
goto end;

xdp-forward/xdp_flowtable.bpf.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ xdp_flowtable_forward_ipv6(const struct flow_offload *flow, void *data,
471471
ip6h->hop_limit--;
472472
}
473473

474-
SEC("xdp")
475-
int xdp_fwd_flowtable(struct xdp_md *ctx)
474+
static __always_inline int xdp_flowtable_flags(struct xdp_md *ctx,
475+
__u32 fib_flags)
476476
{
477477
void *data_end = (void *)(long)ctx->data_end;
478478
struct flow_offload_tuple_rhash *tuplehash;
@@ -578,7 +578,7 @@ int xdp_fwd_flowtable(struct xdp_md *ctx)
578578
xdp_flowtable_get_dnat_ip(&tuple.ipv4_dst, flow, dir);
579579
}
580580

581-
if (bpf_fib_lookup(ctx, &tuple, sizeof(tuple), 0) !=
581+
if (bpf_fib_lookup(ctx, &tuple, sizeof(tuple), fib_flags) !=
582582
BPF_FIB_LKUP_RET_SUCCESS)
583583
return XDP_PASS;
584584

@@ -595,4 +595,16 @@ int xdp_fwd_flowtable(struct xdp_md *ctx)
595595
return bpf_redirect(tuple.ifindex, 0);
596596
}
597597

598+
SEC("xdp")
599+
int xdp_fwd_flowtable_full(struct xdp_md *ctx)
600+
{
601+
return xdp_flowtable_flags(ctx, 0);
602+
}
603+
604+
SEC("xdp")
605+
int xdp_fwd_flowtable_direct(struct xdp_md *ctx)
606+
{
607+
return xdp_flowtable_flags(ctx, BPF_FIB_LOOKUP_DIRECT);
608+
}
609+
598610
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)