Skip to content

Commit e4a8e81

Browse files
borkmanndavem330
authored andcommitted
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: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cd36c3a commit e4a8e81

1 file changed

Lines changed: 32 additions & 40 deletions

File tree

‎net/core/filter.c‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,29 +1835,6 @@ static const struct bpf_func_proto bpf_redirect_proto = {
18351835
.arg2_type = ARG_ANYTHING,
18361836
};
18371837

1838-
BPF_CALL_3(bpf_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags)
1839-
{
1840-
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1841-
1842-
if (unlikely(flags))
1843-
return XDP_ABORTED;
1844-
1845-
ri->ifindex = ifindex;
1846-
ri->flags = flags;
1847-
ri->map = map;
1848-
1849-
return XDP_REDIRECT;
1850-
}
1851-
1852-
static const struct bpf_func_proto bpf_redirect_map_proto = {
1853-
.func = bpf_redirect_map,
1854-
.gpl_only = false,
1855-
.ret_type = RET_INTEGER,
1856-
.arg1_type = ARG_CONST_MAP_PTR,
1857-
.arg2_type = ARG_ANYTHING,
1858-
.arg3_type = ARG_ANYTHING,
1859-
};
1860-
18611838
BPF_CALL_3(bpf_sk_redirect_map, struct bpf_map *, map, u32, key, u64, flags)
18621839
{
18631840
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
@@ -2506,30 +2483,26 @@ static int __bpf_tx_xdp(struct net_device *dev,
25062483
err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
25072484
if (err)
25082485
return err;
2509-
25102486
if (map)
25112487
__dev_map_insert_ctx(map, index);
25122488
else
25132489
dev->netdev_ops->ndo_xdp_flush(dev);
2514-
2515-
return err;
2490+
return 0;
25162491
}
25172492

25182493
void xdp_do_flush_map(void)
25192494
{
25202495
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
25212496
struct bpf_map *map = ri->map_to_flush;
25222497

2523-
ri->map = NULL;
25242498
ri->map_to_flush = NULL;
2525-
25262499
if (map)
25272500
__dev_map_flush(map);
25282501
}
25292502
EXPORT_SYMBOL_GPL(xdp_do_flush_map);
25302503

2531-
int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2532-
struct bpf_prog *xdp_prog)
2504+
static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2505+
struct bpf_prog *xdp_prog)
25332506
{
25342507
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
25352508
struct bpf_map *map = ri->map;
@@ -2545,14 +2518,12 @@ int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
25452518
err = -EINVAL;
25462519
goto out;
25472520
}
2548-
2549-
if (ri->map_to_flush && (ri->map_to_flush != map))
2521+
if (ri->map_to_flush && ri->map_to_flush != map)
25502522
xdp_do_flush_map();
25512523

25522524
err = __bpf_tx_xdp(fwd, map, xdp, index);
25532525
if (likely(!err))
25542526
ri->map_to_flush = map;
2555-
25562527
out:
25572528
trace_xdp_redirect(dev, fwd, xdp_prog, XDP_REDIRECT, err);
25582529
return err;
@@ -2594,20 +2565,17 @@ int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb)
25942565
ri->ifindex = 0;
25952566
if (unlikely(!dev)) {
25962567
bpf_warn_invalid_xdp_redirect(index);
2597-
goto err;
2568+
return -EINVAL;
25982569
}
25992570

26002571
if (unlikely(!(dev->flags & IFF_UP)))
2601-
goto err;
2602-
2572+
return -ENETDOWN;
26032573
len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
26042574
if (skb->len > len)
2605-
goto err;
2575+
return -E2BIG;
26062576

26072577
skb->dev = dev;
26082578
return 0;
2609-
err:
2610-
return -EINVAL;
26112579
}
26122580
EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
26132581

@@ -2620,6 +2588,7 @@ BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
26202588

26212589
ri->ifindex = ifindex;
26222590
ri->flags = flags;
2591+
26232592
return XDP_REDIRECT;
26242593
}
26252594

@@ -2631,6 +2600,29 @@ static const struct bpf_func_proto bpf_xdp_redirect_proto = {
26312600
.arg2_type = ARG_ANYTHING,
26322601
};
26332602

2603+
BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags)
2604+
{
2605+
struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2606+
2607+
if (unlikely(flags))
2608+
return XDP_ABORTED;
2609+
2610+
ri->ifindex = ifindex;
2611+
ri->flags = flags;
2612+
ri->map = map;
2613+
2614+
return XDP_REDIRECT;
2615+
}
2616+
2617+
static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2618+
.func = bpf_xdp_redirect_map,
2619+
.gpl_only = false,
2620+
.ret_type = RET_INTEGER,
2621+
.arg1_type = ARG_CONST_MAP_PTR,
2622+
.arg2_type = ARG_ANYTHING,
2623+
.arg3_type = ARG_ANYTHING,
2624+
};
2625+
26342626
bool bpf_helper_changes_pkt_data(void *func)
26352627
{
26362628
if (func == bpf_skb_vlan_push ||
@@ -3233,7 +3225,7 @@ xdp_func_proto(enum bpf_func_id func_id)
32333225
case BPF_FUNC_redirect:
32343226
return &bpf_xdp_redirect_proto;
32353227
case BPF_FUNC_redirect_map:
3236-
return &bpf_redirect_map_proto;
3228+
return &bpf_xdp_redirect_map_proto;
32373229
default:
32383230
return bpf_base_func_proto(func_id);
32393231
}

0 commit comments

Comments
 (0)