Commit e4a8e817 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by David S. Miller

bpf: misc xdp redirect cleanups

Few cleanups including: bpf_redirect_map() is really XDP only due to
the return code. Move it to a more appropriate location where we do
the XDP redirect handling and change it's name into bpf_xdp_redirect_map()
to make it consistent to the bpf_xdp_redirect() helper.

xdp_do_redirect_map() helper can be static since only used out of filter.c
file. Drop the goto in xdp_do_generic_redirect() and only return errors
directly. In xdp_do_flush_map() only clear ri->map_to_flush which is the
arg we're using in that function, ri->map is cleared earlier along with
ri->ifindex.
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent cd36c3a2
...@@ -1835,29 +1835,6 @@ static const struct bpf_func_proto bpf_redirect_proto = { ...@@ -1835,29 +1835,6 @@ static const struct bpf_func_proto bpf_redirect_proto = {
.arg2_type = ARG_ANYTHING, .arg2_type = ARG_ANYTHING,
}; };
BPF_CALL_3(bpf_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags)
{
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
if (unlikely(flags))
return XDP_ABORTED;
ri->ifindex = ifindex;
ri->flags = flags;
ri->map = map;
return XDP_REDIRECT;
}
static const struct bpf_func_proto bpf_redirect_map_proto = {
.func = bpf_redirect_map,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_CONST_MAP_PTR,
.arg2_type = ARG_ANYTHING,
.arg3_type = ARG_ANYTHING,
};
BPF_CALL_3(bpf_sk_redirect_map, struct bpf_map *, map, u32, key, u64, flags) BPF_CALL_3(bpf_sk_redirect_map, struct bpf_map *, map, u32, key, u64, flags)
{ {
struct redirect_info *ri = this_cpu_ptr(&redirect_info); struct redirect_info *ri = this_cpu_ptr(&redirect_info);
...@@ -2506,13 +2483,11 @@ static int __bpf_tx_xdp(struct net_device *dev, ...@@ -2506,13 +2483,11 @@ static int __bpf_tx_xdp(struct net_device *dev,
err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp); err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
if (err) if (err)
return err; return err;
if (map) if (map)
__dev_map_insert_ctx(map, index); __dev_map_insert_ctx(map, index);
else else
dev->netdev_ops->ndo_xdp_flush(dev); dev->netdev_ops->ndo_xdp_flush(dev);
return 0;
return err;
} }
void xdp_do_flush_map(void) void xdp_do_flush_map(void)
...@@ -2520,15 +2495,13 @@ void xdp_do_flush_map(void) ...@@ -2520,15 +2495,13 @@ void xdp_do_flush_map(void)
struct redirect_info *ri = this_cpu_ptr(&redirect_info); struct redirect_info *ri = this_cpu_ptr(&redirect_info);
struct bpf_map *map = ri->map_to_flush; struct bpf_map *map = ri->map_to_flush;
ri->map = NULL;
ri->map_to_flush = NULL; ri->map_to_flush = NULL;
if (map) if (map)
__dev_map_flush(map); __dev_map_flush(map);
} }
EXPORT_SYMBOL_GPL(xdp_do_flush_map); EXPORT_SYMBOL_GPL(xdp_do_flush_map);
int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp, static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
struct bpf_prog *xdp_prog) struct bpf_prog *xdp_prog)
{ {
struct redirect_info *ri = this_cpu_ptr(&redirect_info); struct redirect_info *ri = this_cpu_ptr(&redirect_info);
...@@ -2545,14 +2518,12 @@ int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp, ...@@ -2545,14 +2518,12 @@ int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
err = -EINVAL; err = -EINVAL;
goto out; goto out;
} }
if (ri->map_to_flush && ri->map_to_flush != map)
if (ri->map_to_flush && (ri->map_to_flush != map))
xdp_do_flush_map(); xdp_do_flush_map();
err = __bpf_tx_xdp(fwd, map, xdp, index); err = __bpf_tx_xdp(fwd, map, xdp, index);
if (likely(!err)) if (likely(!err))
ri->map_to_flush = map; ri->map_to_flush = map;
out: out:
trace_xdp_redirect(dev, fwd, xdp_prog, XDP_REDIRECT, err); trace_xdp_redirect(dev, fwd, xdp_prog, XDP_REDIRECT, err);
return err; return err;
...@@ -2594,20 +2565,17 @@ int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb) ...@@ -2594,20 +2565,17 @@ int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb)
ri->ifindex = 0; ri->ifindex = 0;
if (unlikely(!dev)) { if (unlikely(!dev)) {
bpf_warn_invalid_xdp_redirect(index); bpf_warn_invalid_xdp_redirect(index);
goto err; return -EINVAL;
} }
if (unlikely(!(dev->flags & IFF_UP))) if (unlikely(!(dev->flags & IFF_UP)))
goto err; return -ENETDOWN;
len = dev->mtu + dev->hard_header_len + VLAN_HLEN; len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
if (skb->len > len) if (skb->len > len)
goto err; return -E2BIG;
skb->dev = dev; skb->dev = dev;
return 0; return 0;
err:
return -EINVAL;
} }
EXPORT_SYMBOL_GPL(xdp_do_generic_redirect); EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
...@@ -2620,6 +2588,7 @@ BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags) ...@@ -2620,6 +2588,7 @@ BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
ri->ifindex = ifindex; ri->ifindex = ifindex;
ri->flags = flags; ri->flags = flags;
return XDP_REDIRECT; return XDP_REDIRECT;
} }
...@@ -2631,6 +2600,29 @@ static const struct bpf_func_proto bpf_xdp_redirect_proto = { ...@@ -2631,6 +2600,29 @@ static const struct bpf_func_proto bpf_xdp_redirect_proto = {
.arg2_type = ARG_ANYTHING, .arg2_type = ARG_ANYTHING,
}; };
BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags)
{
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
if (unlikely(flags))
return XDP_ABORTED;
ri->ifindex = ifindex;
ri->flags = flags;
ri->map = map;
return XDP_REDIRECT;
}
static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
.func = bpf_xdp_redirect_map,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_CONST_MAP_PTR,
.arg2_type = ARG_ANYTHING,
.arg3_type = ARG_ANYTHING,
};
bool bpf_helper_changes_pkt_data(void *func) bool bpf_helper_changes_pkt_data(void *func)
{ {
if (func == bpf_skb_vlan_push || if (func == bpf_skb_vlan_push ||
...@@ -3233,7 +3225,7 @@ xdp_func_proto(enum bpf_func_id func_id) ...@@ -3233,7 +3225,7 @@ xdp_func_proto(enum bpf_func_id func_id)
case BPF_FUNC_redirect: case BPF_FUNC_redirect:
return &bpf_xdp_redirect_proto; return &bpf_xdp_redirect_proto;
case BPF_FUNC_redirect_map: case BPF_FUNC_redirect_map:
return &bpf_redirect_map_proto; return &bpf_xdp_redirect_map_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment