History

bpf: add layer 2 encap support to bpf_skb_adjust_room

commit 868d523535c2 ("bpf: add bpf_skb_adjust_room encap flags") introduced support to bpf_skb_adjust_room for GSO-friendly GRE and UDP encapsulation. For GSO to work for skbs, the inner headers (mac and network) need to be marked. For L3 encapsulation using bpf_skb_adjust_room, the mac and network headers are identical. Here we provide a way of specifying the inner mac header length for cases where L2 encap is desired. Such an approach can support encapsulated ethernet headers, MPLS headers etc. For example to convert from a packet of form [eth][ip][tcp] to [eth][ip][udp][inner mac][ip][tcp], something like the following could be done: headroom = sizeof(iph) + sizeof(struct udphdr) + inner_maclen; ret = bpf_skb_adjust_room(skb, headroom, BPF_ADJ_ROOM_MAC, BPF_F_ADJ_ROOM_ENCAP_L4_UDP | BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | BPF_F_ADJ_ROOM_ENCAP_L2(inner_maclen));

Alan Maguire committed 7 years ago

Commit 58dfc901 parent 166b5a72 files · +18 −4Browse files

Unified · Split

2 files
  1. include/uapi/linux/bpf.h +10 −0
  2. net/core/filter.c +8 −4
include/uapi/linux/bpf.h+10−0
@@ -1523,6 +1523,10 @@ union bpf_attr { · 1522 unmodified lines
1523 * * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP **:1523 * * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP **:
1524 * Use with ENCAP_L3 flags to further specify the tunnel type.1524 * Use with ENCAP_L3 flags to further specify the tunnel type.
1525 *1525 *
1526 * * **BPF_F_ADJ_ROOM_ENCAP_L2(len) **:
1527 * Use with ENCAP_L3/L4 flags to further specify the tunnel
1528 * type; **len** is the length of the inner MAC header.
1529 *
1526 * A call to this helper is susceptible to change the underlaying1530 * A call to this helper is susceptible to change the underlaying
1527 * packet buffer. Therefore, at load time, all checks on pointers1531 * packet buffer. Therefore, at load time, all checks on pointers
1528 * previously done by the verifier are invalidated and must be1532 * previously done by the verifier are invalidated and must be
@@ -2664,10 +2668,16 @@ enum bpf_func_id { · 1135 unmodified lines
2664/* BPF_FUNC_skb_adjust_room flags. */2668/* BPF_FUNC_skb_adjust_room flags. */
2665#define BPF_F_ADJ_ROOM_FIXED_GSO (1ULL << 0)2669#define BPF_F_ADJ_ROOM_FIXED_GSO (1ULL << 0)
2666 2670
2671#define BPF_ADJ_ROOM_ENCAP_L2_MASK 0xff
2672#define BPF_ADJ_ROOM_ENCAP_L2_SHIFT 56
2673
2667#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 (1ULL << 1)2674#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 (1ULL << 1)
2668#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 (1ULL << 2)2675#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 (1ULL << 2)
2669#define BPF_F_ADJ_ROOM_ENCAP_L4_GRE (1ULL << 3)2676#define BPF_F_ADJ_ROOM_ENCAP_L4_GRE (1ULL << 3)
2670#define BPF_F_ADJ_ROOM_ENCAP_L4_UDP (1ULL << 4)2677#define BPF_F_ADJ_ROOM_ENCAP_L4_UDP (1ULL << 4)
2678#define BPF_F_ADJ_ROOM_ENCAP_L2(len) (((__u64)len & \
2679 BPF_ADJ_ROOM_ENCAP_L2_MASK) \
2680 << BPF_ADJ_ROOM_ENCAP_L2_SHIFT)
2671 2681
2672/* Mode for BPF_FUNC_skb_adjust_room helper. */2682/* Mode for BPF_FUNC_skb_adjust_room helper. */
2673enum bpf_adj_room_mode {2683enum bpf_adj_room_mode {
net/core/filter.c+8−4
@@ -2969,11 +2969,14 @@ static u32 bpf_skb_net_base_len(const struct sk_buff *skb) · 2968 unmodified lines
2969#define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \2969#define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
2970 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \2970 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
2971 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \2971 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
2972 BPF_F_ADJ_ROOM_ENCAP_L4_UDP)2972 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
2973 BPF_F_ADJ_ROOM_ENCAP_L2( \
2974 BPF_ADJ_ROOM_ENCAP_L2_MASK))
2973 2975
2974static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,2976static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
2975 u64 flags)2977 u64 flags)
2976{2978{
2979 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
2977 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;2980 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
2978 u16 mac_len = 0, inner_net = 0, inner_trans = 0;2981 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
2979 unsigned int gso_type = SKB_GSO_DODGY;2982 unsigned int gso_type = SKB_GSO_DODGY;
@@ -3008,6 +3011,8 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff, · 28 unmodified lines
3008 3011
3009 mac_len = skb->network_header - skb->mac_header;3012 mac_len = skb->network_header - skb->mac_header;
3010 inner_net = skb->network_header;3013 inner_net = skb->network_header;
3014 if (inner_mac_len > len_diff)
3015 return -EINVAL;
3011 inner_trans = skb->transport_header;3016 inner_trans = skb->transport_header;
3012 }3017 }
3013 3018
@@ -3016,8 +3021,7 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff, · 2 unmodified lines
3016 return ret;3021 return ret;
3017 3022
3018 if (encap) {3023 if (encap) {
3019 /* inner mac == inner_net on l3 encap */3024 skb->inner_mac_header = inner_net - inner_mac_len;
3020 skb->inner_mac_header = inner_net;
3021 skb->inner_network_header = inner_net;3025 skb->inner_network_header = inner_net;
3022 skb->inner_transport_header = inner_trans;3026 skb->inner_transport_header = inner_trans;
3023 skb_set_inner_protocol(skb, skb->protocol);3027 skb_set_inner_protocol(skb, skb->protocol);
@@ -3031,7 +3035,7 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff, · 7 unmodified lines
3031 gso_type |= SKB_GSO_GRE;3035 gso_type |= SKB_GSO_GRE;
3032 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)3036 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3033 gso_type |= SKB_GSO_IPXIP6;3037 gso_type |= SKB_GSO_IPXIP6;
3034 else3038 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3035 gso_type |= SKB_GSO_IPXIP4;3039 gso_type |= SKB_GSO_IPXIP4;
3036 3040
3037 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||3041 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t