Skip to content

Commit 3896d65

Browse files
Alexei Starovoitovdavem330
Alexei Starovoitov
authored andcommitted
bpf: introduce bpf_clone_redirect() helper
Allow eBPF programs attached to classifier/actions to call bpf_clone_redirect(skb, ifindex, flags) helper which will mirror or redirect the packet by dynamic ifindex selection from within the program to a target device either at ingress or at egress. Can be used for various scenarios, for example, to load balance skbs into veths, split parts of the traffic to local taps, etc. Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 613d8c1 commit 3896d65

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Diff for: include/uapi/linux/bpf.h

+10
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ enum bpf_func_id {
220220
* Return: 0 on success
221221
*/
222222
BPF_FUNC_tail_call,
223+
224+
/**
225+
* bpf_clone_redirect(skb, ifindex, flags) - redirect to another netdev
226+
* @skb: pointer to skb
227+
* @ifindex: ifindex of the net device
228+
* @flags: bit 0 - if set, redirect to ingress instead of egress
229+
* other bits - reserved
230+
* Return: 0 on success
231+
*/
232+
BPF_FUNC_clone_redirect,
223233
__BPF_FUNC_MAX_ID,
224234
};
225235

Diff for: net/core/filter.c

+40
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <linux/seccomp.h>
4747
#include <linux/if_vlan.h>
4848
#include <linux/bpf.h>
49+
#include <net/sch_generic.h>
4950

5051
/**
5152
* sk_filter - run a packet through a socket filter
@@ -1407,6 +1408,43 @@ const struct bpf_func_proto bpf_l4_csum_replace_proto = {
14071408
.arg5_type = ARG_ANYTHING,
14081409
};
14091410

1411+
#define BPF_IS_REDIRECT_INGRESS(flags) ((flags) & 1)
1412+
1413+
static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1414+
{
1415+
struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1416+
struct net_device *dev;
1417+
1418+
dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1419+
if (unlikely(!dev))
1420+
return -EINVAL;
1421+
1422+
if (unlikely(!(dev->flags & IFF_UP)))
1423+
return -EINVAL;
1424+
1425+
skb2 = skb_clone(skb, GFP_ATOMIC);
1426+
if (unlikely(!skb2))
1427+
return -ENOMEM;
1428+
1429+
if (G_TC_AT(skb2->tc_verd) & AT_INGRESS)
1430+
skb_push(skb2, skb2->mac_len);
1431+
1432+
if (BPF_IS_REDIRECT_INGRESS(flags))
1433+
return dev_forward_skb(dev, skb2);
1434+
1435+
skb2->dev = dev;
1436+
return dev_queue_xmit(skb2);
1437+
}
1438+
1439+
const struct bpf_func_proto bpf_clone_redirect_proto = {
1440+
.func = bpf_clone_redirect,
1441+
.gpl_only = false,
1442+
.ret_type = RET_INTEGER,
1443+
.arg1_type = ARG_PTR_TO_CTX,
1444+
.arg2_type = ARG_ANYTHING,
1445+
.arg3_type = ARG_ANYTHING,
1446+
};
1447+
14101448
static const struct bpf_func_proto *
14111449
sk_filter_func_proto(enum bpf_func_id func_id)
14121450
{
@@ -1440,6 +1478,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
14401478
return &bpf_l3_csum_replace_proto;
14411479
case BPF_FUNC_l4_csum_replace:
14421480
return &bpf_l4_csum_replace_proto;
1481+
case BPF_FUNC_clone_redirect:
1482+
return &bpf_clone_redirect_proto;
14431483
default:
14441484
return sk_filter_func_proto(func_id);
14451485
}

0 commit comments

Comments
 (0)