Skip to content

Commit 2c22542

Browse files
borkmannMartin KaFai Lau
authored and
Martin KaFai Lau
committed
bpf, netkit: Add indirect call wrapper for fetching peer dev
ndo_get_peer_dev is used in tcx BPF fast path, therefore make use of indirect call wrapper and therefore optimize the bpf_redirect_peer() internal handling a bit. Add a small skb_get_peer_dev() wrapper which utilizes the INDIRECT_CALL_1() macro instead of open coding. Future work could potentially add a peer pointer directly into struct net_device in future and convert veth and netkit over to use it so that eventually ndo_get_peer_dev can be removed. Co-developed-by: Nikolay Aleksandrov <razor@blackwall.org> Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Stanislav Fomichev <sdf@google.com> Link: https://lore.kernel.org/r/20231114004220.6495-7-daniel@iogearbox.net Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 024ee93 commit 2c22542

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

drivers/net/netkit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/filter.h>
88
#include <linux/netfilter_netdev.h>
99
#include <linux/bpf_mprog.h>
10+
#include <linux/indirect_call_wrapper.h>
1011

1112
#include <net/netkit.h>
1213
#include <net/dst.h>
@@ -177,7 +178,7 @@ static void netkit_set_headroom(struct net_device *dev, int headroom)
177178
rcu_read_unlock();
178179
}
179180

180-
static struct net_device *netkit_peer_dev(struct net_device *dev)
181+
INDIRECT_CALLABLE_SCOPE struct net_device *netkit_peer_dev(struct net_device *dev)
181182
{
182183
return rcu_dereference(netkit_priv(dev)->peer);
183184
}

include/net/netkit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ int netkit_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1010
int netkit_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1111
int netkit_prog_detach(const union bpf_attr *attr, struct bpf_prog *prog);
1212
int netkit_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr);
13+
INDIRECT_CALLABLE_DECLARE(struct net_device *netkit_peer_dev(struct net_device *dev));
1314
#else
1415
static inline int netkit_prog_attach(const union bpf_attr *attr,
1516
struct bpf_prog *prog)
@@ -34,5 +35,10 @@ static inline int netkit_prog_query(const union bpf_attr *attr,
3435
{
3536
return -EINVAL;
3637
}
38+
39+
static inline struct net_device *netkit_peer_dev(struct net_device *dev)
40+
{
41+
return NULL;
42+
}
3743
#endif /* CONFIG_NETKIT */
3844
#endif /* __NET_NETKIT_H */

net/core/filter.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include <net/xdp.h>
8282
#include <net/mptcp.h>
8383
#include <net/netfilter/nf_conntrack_bpf.h>
84+
#include <net/netkit.h>
8485
#include <linux/un.h>
8586

8687
#include "dev.h"
@@ -2468,6 +2469,16 @@ static const struct bpf_func_proto bpf_clone_redirect_proto = {
24682469
DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
24692470
EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
24702471

2472+
static struct net_device *skb_get_peer_dev(struct net_device *dev)
2473+
{
2474+
const struct net_device_ops *ops = dev->netdev_ops;
2475+
2476+
if (likely(ops->ndo_get_peer_dev))
2477+
return INDIRECT_CALL_1(ops->ndo_get_peer_dev,
2478+
netkit_peer_dev, dev);
2479+
return NULL;
2480+
}
2481+
24712482
int skb_do_redirect(struct sk_buff *skb)
24722483
{
24732484
struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
@@ -2481,12 +2492,9 @@ int skb_do_redirect(struct sk_buff *skb)
24812492
if (unlikely(!dev))
24822493
goto out_drop;
24832494
if (flags & BPF_F_PEER) {
2484-
const struct net_device_ops *ops = dev->netdev_ops;
2485-
2486-
if (unlikely(!ops->ndo_get_peer_dev ||
2487-
!skb_at_tc_ingress(skb)))
2495+
if (unlikely(!skb_at_tc_ingress(skb)))
24882496
goto out_drop;
2489-
dev = ops->ndo_get_peer_dev(dev);
2497+
dev = skb_get_peer_dev(dev);
24902498
if (unlikely(!dev ||
24912499
!(dev->flags & IFF_UP) ||
24922500
net_eq(net, dev_net(dev))))

0 commit comments

Comments
 (0)