Skip to content

Commit 814abfa

Browse files
jrfastabdavem330
authored andcommitted
xdp: add bpf_redirect helper function
This adds support for a bpf_redirect helper function to the XDP infrastructure. For now this only supports redirecting to the egress path of a port. In order to support drivers handling a xdp_buff natively this patches uses a new ndo operation ndo_xdp_xmit() that takes pushes a xdp_buff to the specified device. If the program specifies either (a) an unknown device or (b) a device that does not support the operation a BPF warning is thrown and the XDP_ABORTED error code is returned. Signed-off-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d445516 commit 814abfa

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

include/linux/filter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,11 @@ bool bpf_helper_changes_pkt_data(void *func);
711711

712712
struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
713713
const struct bpf_insn *patch, u32 len);
714+
715+
int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp);
716+
714717
void bpf_warn_invalid_xdp_action(u32 act);
718+
void bpf_warn_invalid_xdp_redirect(u32 ifindex);
715719

716720
#ifdef CONFIG_BPF_JIT
717721
extern int bpf_jit_enable;

include/linux/netdevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct mpls_dev;
6666
/* UDP Tunnel offloads */
6767
struct udp_tunnel_info;
6868
struct bpf_prog;
69+
struct xdp_buff;
6970

7071
void netdev_set_default_ethtool_ops(struct net_device *dev,
7172
const struct ethtool_ops *ops);
@@ -1138,6 +1139,9 @@ struct xfrmdev_ops {
11381139
* int (*ndo_xdp)(struct net_device *dev, struct netdev_xdp *xdp);
11391140
* This function is used to set or query state related to XDP on the
11401141
* netdevice. See definition of enum xdp_netdev_command for details.
1142+
* int (*ndo_xdp_xmit)(struct net_device *dev, struct xdp_buff *xdp);
1143+
* This function is used to submit a XDP packet for transmit on a
1144+
* netdevice.
11411145
*
11421146
*/
11431147
struct net_device_ops {
@@ -1323,6 +1327,8 @@ struct net_device_ops {
13231327
int needed_headroom);
13241328
int (*ndo_xdp)(struct net_device *dev,
13251329
struct netdev_xdp *xdp);
1330+
int (*ndo_xdp_xmit)(struct net_device *dev,
1331+
struct xdp_buff *xdp);
13261332
};
13271333

13281334
/**

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ enum xdp_action {
717717
XDP_DROP,
718718
XDP_PASS,
719719
XDP_TX,
720+
XDP_REDIRECT,
720721
};
721722

722723
/* user accessible metadata for XDP packet hook

net/core/filter.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,51 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
24122412
.arg2_type = ARG_ANYTHING,
24132413
};
24142414

2415+
static int __bpf_tx_xdp(struct net_device *dev, struct xdp_buff *xdp)
2416+
{
2417+
if (dev->netdev_ops->ndo_xdp_xmit) {
2418+
dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2419+
return 0;
2420+
}
2421+
bpf_warn_invalid_xdp_redirect(dev->ifindex);
2422+
return -EOPNOTSUPP;
2423+
}
2424+
2425+
int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp)
2426+
{
2427+
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2428+
2429+
dev = dev_get_by_index_rcu(dev_net(dev), ri->ifindex);
2430+
ri->ifindex = 0;
2431+
if (unlikely(!dev)) {
2432+
bpf_warn_invalid_xdp_redirect(ri->ifindex);
2433+
return -EINVAL;
2434+
}
2435+
2436+
return __bpf_tx_xdp(dev, xdp);
2437+
}
2438+
EXPORT_SYMBOL_GPL(xdp_do_redirect);
2439+
2440+
BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2441+
{
2442+
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2443+
2444+
if (unlikely(flags))
2445+
return XDP_ABORTED;
2446+
2447+
ri->ifindex = ifindex;
2448+
ri->flags = flags;
2449+
return XDP_REDIRECT;
2450+
}
2451+
2452+
static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2453+
.func = bpf_xdp_redirect,
2454+
.gpl_only = false,
2455+
.ret_type = RET_INTEGER,
2456+
.arg1_type = ARG_ANYTHING,
2457+
.arg2_type = ARG_ANYTHING,
2458+
};
2459+
24152460
bool bpf_helper_changes_pkt_data(void *func)
24162461
{
24172462
if (func == bpf_skb_vlan_push ||
@@ -3011,6 +3056,8 @@ xdp_func_proto(enum bpf_func_id func_id)
30113056
return &bpf_get_smp_processor_id_proto;
30123057
case BPF_FUNC_xdp_adjust_head:
30133058
return &bpf_xdp_adjust_head_proto;
3059+
case BPF_FUNC_redirect:
3060+
return &bpf_xdp_redirect_proto;
30143061
default:
30153062
return bpf_base_func_proto(func_id);
30163063
}
@@ -3310,6 +3357,11 @@ void bpf_warn_invalid_xdp_action(u32 act)
33103357
}
33113358
EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
33123359

3360+
void bpf_warn_invalid_xdp_redirect(u32 ifindex)
3361+
{
3362+
WARN_ONCE(1, "Illegal XDP redirect to unsupported device ifindex(%i)\n", ifindex);
3363+
}
3364+
33133365
static bool __is_valid_sock_ops_access(int off, int size)
33143366
{
33153367
if (off < 0 || off >= sizeof(struct bpf_sock_ops))

0 commit comments

Comments
 (0)