Skip to content

Commit cc31f11

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 aec1a55 commit cc31f11

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
@@ -5192,7 +5192,6 @@ static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
51925192
memcpy(params->smac, dev->dev_addr, ETH_ALEN);
51935193
params->h_vlan_TCI = 0;
51945194
params->h_vlan_proto = 0;
5195-
params->ifindex = dev->ifindex;
51965195

51975196
return 0;
51985197
}
@@ -5289,6 +5288,10 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
52895288
dev = nhc->nhc_dev;
52905289

52915290
params->rt_metric = res.fi->fib_priority;
5291+
params->ifindex = dev->ifindex;
5292+
5293+
if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
5294+
return BPF_FIB_LKUP_RET_NO_NEIGH;
52925295

52935296
/* xdp and cls_bpf programs are run in RCU-bh so
52945297
* rcu_read_lock_bh is not needed here
@@ -5414,6 +5417,10 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
54145417

54155418
dev = res.nh->fib_nh_dev;
54165419
params->rt_metric = res.f6i->fib6_metric;
5420+
params->ifindex = dev->ifindex;
5421+
5422+
if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
5423+
return BPF_FIB_LKUP_RET_NO_NEIGH;
54175424

54185425
/* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
54195426
* not needed here.
@@ -5432,7 +5439,8 @@ BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
54325439
if (plen < sizeof(*params))
54335440
return -EINVAL;
54345441

5435-
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5442+
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT |
5443+
BPF_FIB_LOOKUP_SKIP_NEIGH))
54365444
return -EINVAL;
54375445

54385446
switch (params->family) {
@@ -5469,7 +5477,8 @@ BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
54695477
if (plen < sizeof(*params))
54705478
return -EINVAL;
54715479

5472-
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5480+
if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT |
5481+
BPF_FIB_LOOKUP_SKIP_NEIGH))
54735482
return -EINVAL;
54745483

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