Skip to content

Commit c4423a1

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Add source ip in bpf tunnel key'
Kaixi Fan says: ==================== From: Kaixi Fan <[email protected]> Now bpf code could not set tunnel source ip address of ip tunnel. So it could not support flow based tunnel mode completely. Because flow based tunnel mode could set tunnel source, destination ip address and tunnel key simultaneously. Flow based tunnel is useful for overlay networks. And by configuring tunnel source ip address, user could make their networks more elastic. For example, tunnel source ip could be used to select different egress nic interface for different flows with same tunnel destination ip. Another example, user could choose one of multiple ip address of the egress nic interface as the packet's tunnel source ip. Add tunnel and tunnel source testcases in test_progs. Other types of tunnel testcases would be moved to test_progs step by step in the future. v6: - use libbpf api to attach tc progs and remove some shell commands to reduce test runtime based on Alexei Starovoitov's suggestion v5: - fix some code format errors - use bpf kernel code at namespace at_ns0 to set tunnel metadata v4: - fix subject error of first patch v3: - move vxlan tunnel testcases to test_progs - replace bpf_trace_printk with bpf_printk - rename bpf kernel prog section name to tic v2: - merge vxlan tunnel and tunnel source ip testcases in test_tunnel.sh ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents bd2331b + 71b2ec2 commit c4423a1

File tree

6 files changed

+687
-248
lines changed

6 files changed

+687
-248
lines changed

include/uapi/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key {
56045604
__u8 tunnel_ttl;
56055605
__u16 tunnel_ext; /* Padding, future use. */
56065606
__u32 tunnel_label;
5607+
union {
5608+
__u32 local_ipv4;
5609+
__u32 local_ipv6[4];
5610+
};
56075611
};
56085612

56095613
/* user accessible mirror of in-kernel xfrm_state.

net/core/filter.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4498,6 +4498,7 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
44984498
if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
44994499
err = -EINVAL;
45004500
switch (size) {
4501+
case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
45014502
case offsetof(struct bpf_tunnel_key, tunnel_label):
45024503
case offsetof(struct bpf_tunnel_key, tunnel_ext):
45034504
goto set_compat;
@@ -4523,10 +4524,14 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
45234524
if (flags & BPF_F_TUNINFO_IPV6) {
45244525
memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
45254526
sizeof(to->remote_ipv6));
4527+
memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4528+
sizeof(to->local_ipv6));
45264529
to->tunnel_label = be32_to_cpu(info->key.label);
45274530
} else {
45284531
to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
45294532
memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4533+
to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4534+
memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
45304535
to->tunnel_label = 0;
45314536
}
45324537

@@ -4597,6 +4602,7 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
45974602
return -EINVAL;
45984603
if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
45994604
switch (size) {
4605+
case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
46004606
case offsetof(struct bpf_tunnel_key, tunnel_label):
46014607
case offsetof(struct bpf_tunnel_key, tunnel_ext):
46024608
case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
@@ -4639,10 +4645,13 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
46394645
info->mode |= IP_TUNNEL_INFO_IPV6;
46404646
memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
46414647
sizeof(from->remote_ipv6));
4648+
memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4649+
sizeof(from->local_ipv6));
46424650
info->key.label = cpu_to_be32(from->tunnel_label) &
46434651
IPV6_FLOWLABEL_MASK;
46444652
} else {
46454653
info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4654+
info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
46464655
}
46474656

46484657
return 0;

tools/include/uapi/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key {
56045604
__u8 tunnel_ttl;
56055605
__u16 tunnel_ext; /* Padding, future use. */
56065606
__u32 tunnel_label;
5607+
union {
5608+
__u32 local_ipv4;
5609+
__u32 local_ipv6[4];
5610+
};
56075611
};
56085612

56095613
/* user accessible mirror of in-kernel xfrm_state.

0 commit comments

Comments
 (0)