Skip to content

Commit 67f3ea8

Browse files
tohojokernel-patches-bot
authored andcommitted
bpf_fib_lookup: optionally skip neighbour lookup
The bpf_fib_lookup() helper performs a neighbour lookup for the destination IP and returns BPF_FIB_LKUP_NO_NEIGH if this fails, with the expectation that the BPF program will pass the packet up the stack in this case. However, with the addition of bpf_redirect_neigh() that can be used instead to perform the neighbour lookup, at the cost of a bit of duplicated work. For that we still need the target ifindex, and since bpf_fib_lookup() already has that at the time it performs the neighbour lookup, there is really no reason why it can't just return it in any case. So let's just always return the ifindex, and also add a flag that lets the caller turn off the neighbour lookup entirely in bpf_fib_lookup(). v2: - Add flag (Daniel) - Remove misleading code example from commit message (David) Cc: David Ahern <[email protected]> Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
1 parent 624f270 commit 67f3ea8

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

include/uapi/linux/bpf.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4813,12 +4813,14 @@ struct bpf_raw_tracepoint_args {
48134813
__u64 args[0];
48144814
};
48154815

4816-
/* DIRECT: Skip the FIB rules and go to FIB table associated with device
4817-
* OUTPUT: Do lookup from egress perspective; default is ingress
4816+
/* DIRECT: Skip the FIB rules and go to FIB table associated with device
4817+
* OUTPUT: Do lookup from egress perspective; default is ingress
4818+
* SKIP_NEIGH: Skip neighbour lookup and return BPF_FIB_LKUP_RET_NO_NEIGH on success
48184819
*/
48194820
enum {
4820-
BPF_FIB_LOOKUP_DIRECT = (1U << 0),
4821-
BPF_FIB_LOOKUP_OUTPUT = (1U << 1),
4821+
BPF_FIB_LOOKUP_DIRECT = (1U << 0),
4822+
BPF_FIB_LOOKUP_OUTPUT = (1U << 1),
4823+
BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
48224824
};
48234825

48244826
enum {

net/core/filter.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5196,7 +5196,6 @@ static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
51965196
memcpy(params->smac, dev->dev_addr, ETH_ALEN);
51975197
params->h_vlan_TCI = 0;
51985198
params->h_vlan_proto = 0;
5199-
params->ifindex = dev->ifindex;
52005199

52015200
return 0;
52025201
}
@@ -5293,6 +5292,10 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
52935292
dev = nhc->nhc_dev;
52945293

52955294
params->rt_metric = res.fi->fib_priority;
5295+
params->ifindex = dev->ifindex;
5296+
5297+
if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
5298+
return BPF_FIB_LKUP_RET_NO_NEIGH;
52965299

52975300
/* xdp and cls_bpf programs are run in RCU-bh so
52985301
* rcu_read_lock_bh is not needed here
@@ -5418,6 +5421,10 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
54185421

54195422
dev = res.nh->fib_nh_dev;
54205423
params->rt_metric = res.f6i->fib6_metric;
5424+
params->ifindex = dev->ifindex;
5425+
5426+
if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
5427+
return BPF_FIB_LKUP_RET_NO_NEIGH;
54215428

54225429
/* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
54235430
* not needed here.
@@ -5436,7 +5443,8 @@ BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
54365443
if (plen < sizeof(*params))
54375444
return -EINVAL;
54385445

5439-
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5446+
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT |
5447+
BPF_FIB_LOOKUP_SKIP_NEIGH))
54405448
return -EINVAL;
54415449

54425450
switch (params->family) {
@@ -5473,7 +5481,8 @@ BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
54735481
if (plen < sizeof(*params))
54745482
return -EINVAL;
54755483

5476-
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5484+
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT |
5485+
BPF_FIB_LOOKUP_SKIP_NEIGH))
54775486
return -EINVAL;
54785487

54795488
switch (params->family) {

tools/include/uapi/linux/bpf.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4813,12 +4813,14 @@ struct bpf_raw_tracepoint_args {
48134813
__u64 args[0];
48144814
};
48154815

4816-
/* DIRECT: Skip the FIB rules and go to FIB table associated with device
4817-
* OUTPUT: Do lookup from egress perspective; default is ingress
4816+
/* DIRECT: Skip the FIB rules and go to FIB table associated with device
4817+
* OUTPUT: Do lookup from egress perspective; default is ingress
4818+
* SKIP_NEIGH: Skip neighbour lookup and return BPF_FIB_LKUP_RET_NO_NEIGH on success
48184819
*/
48194820
enum {
4820-
BPF_FIB_LOOKUP_DIRECT = (1U << 0),
4821-
BPF_FIB_LOOKUP_OUTPUT = (1U << 1),
4821+
BPF_FIB_LOOKUP_DIRECT = (1U << 0),
4822+
BPF_FIB_LOOKUP_OUTPUT = (1U << 1),
4823+
BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
48224824
};
48234825

48244826
enum {

0 commit comments

Comments
 (0)