1 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 #include <uapi/linux/bpf.h>
8 #include <uapi/linux/in.h>
9 #include <uapi/linux/if.h>
10 #include <uapi/linux/if_ether.h>
11 #include <uapi/linux/ip.h>
12 #include <uapi/linux/ipv6.h>
13 #include <uapi/linux/if_tunnel.h>
14 #include <uapi/linux/mpls.h>
15 #include <bpf/bpf_helpers.h>
16 #include "bpf_legacy.h"
18 #define IP_OFFSET 0x1FFF
27 __be16 h_vlan_encapsulated_proto
;
30 struct flow_key_record
{
40 static inline void parse_eth_proto(struct __sk_buff
*skb
, u32 proto
);
42 static inline int ip_is_fragment(struct __sk_buff
*ctx
, __u64 nhoff
)
44 return load_half(ctx
, nhoff
+ offsetof(struct iphdr
, frag_off
))
45 & (IP_MF
| IP_OFFSET
);
48 static inline __u32
ipv6_addr_hash(struct __sk_buff
*ctx
, __u64 off
)
50 __u64 w0
= load_word(ctx
, off
);
51 __u64 w1
= load_word(ctx
, off
+ 4);
52 __u64 w2
= load_word(ctx
, off
+ 8);
53 __u64 w3
= load_word(ctx
, off
+ 12);
55 return (__u32
)(w0
^ w1
^ w2
^ w3
);
59 struct flow_key_record flow
;
63 __uint(type
, BPF_MAP_TYPE_ARRAY
);
65 __type(value
, struct globals
);
66 __uint(max_entries
, 32);
67 } percpu_map
SEC(".maps");
69 /* user poor man's per_cpu until native support is ready */
70 static struct globals
*this_cpu_globals(void)
72 u32 key
= bpf_get_smp_processor_id();
74 return bpf_map_lookup_elem(&percpu_map
, &key
);
77 /* some simple stats for user space consumption */
84 __uint(type
, BPF_MAP_TYPE_HASH
);
85 __type(key
, struct flow_key_record
);
86 __type(value
, struct pair
);
87 __uint(max_entries
, 1024);
88 } hash_map
SEC(".maps");
90 static void update_stats(struct __sk_buff
*skb
, struct globals
*g
)
92 struct flow_key_record key
= g
->flow
;
95 value
= bpf_map_lookup_elem(&hash_map
, &key
);
97 __sync_fetch_and_add(&value
->packets
, 1);
98 __sync_fetch_and_add(&value
->bytes
, skb
->len
);
100 struct pair val
= {1, skb
->len
};
102 bpf_map_update_elem(&hash_map
, &key
, &val
, BPF_ANY
);
106 static __always_inline
void parse_ip_proto(struct __sk_buff
*skb
,
107 struct globals
*g
, __u32 ip_proto
)
109 __u32 nhoff
= skb
->cb
[0];
119 __u32 gre_flags
= load_half(skb
,
120 nhoff
+ offsetof(struct gre_hdr
, flags
));
121 __u32 gre_proto
= load_half(skb
,
122 nhoff
+ offsetof(struct gre_hdr
, proto
));
124 if (gre_flags
& (GRE_VERSION
|GRE_ROUTING
))
128 if (gre_flags
& GRE_CSUM
)
130 if (gre_flags
& GRE_KEY
)
132 if (gre_flags
& GRE_SEQ
)
136 parse_eth_proto(skb
, gre_proto
);
140 parse_eth_proto(skb
, ETH_P_IP
);
143 parse_eth_proto(skb
, ETH_P_IPV6
);
147 g
->flow
.ports
= load_word(skb
, nhoff
);
149 g
->flow
.ip_proto
= ip_proto
;
150 update_stats(skb
, g
);
158 int bpf_func_ip(struct __sk_buff
*skb
)
160 struct globals
*g
= this_cpu_globals();
161 __u32 nhoff
, verlen
, ip_proto
;
168 if (unlikely(ip_is_fragment(skb
, nhoff
)))
171 ip_proto
= load_byte(skb
, nhoff
+ offsetof(struct iphdr
, protocol
));
173 if (ip_proto
!= IPPROTO_GRE
) {
174 g
->flow
.src
= load_word(skb
, nhoff
+ offsetof(struct iphdr
, saddr
));
175 g
->flow
.dst
= load_word(skb
, nhoff
+ offsetof(struct iphdr
, daddr
));
178 verlen
= load_byte(skb
, nhoff
+ 0/*offsetof(struct iphdr, ihl)*/);
179 nhoff
+= (verlen
& 0xF) << 2;
182 parse_ip_proto(skb
, g
, ip_proto
);
187 int bpf_func_ipv6(struct __sk_buff
*skb
)
189 struct globals
*g
= this_cpu_globals();
190 __u32 nhoff
, ip_proto
;
197 ip_proto
= load_byte(skb
,
198 nhoff
+ offsetof(struct ipv6hdr
, nexthdr
));
199 g
->flow
.src
= ipv6_addr_hash(skb
,
200 nhoff
+ offsetof(struct ipv6hdr
, saddr
));
201 g
->flow
.dst
= ipv6_addr_hash(skb
,
202 nhoff
+ offsetof(struct ipv6hdr
, daddr
));
203 nhoff
+= sizeof(struct ipv6hdr
);
206 parse_ip_proto(skb
, g
, ip_proto
);
211 int bpf_func_vlan(struct __sk_buff
*skb
)
217 proto
= load_half(skb
, nhoff
+ offsetof(struct vlan_hdr
,
218 h_vlan_encapsulated_proto
));
219 nhoff
+= sizeof(struct vlan_hdr
);
222 parse_eth_proto(skb
, proto
);
228 int bpf_func_mpls(struct __sk_buff
*skb
)
234 label
= load_word(skb
, nhoff
);
235 nhoff
+= sizeof(struct mpls_label
);
238 if (label
& MPLS_LS_S_MASK
) {
239 __u8 verlen
= load_byte(skb
, nhoff
);
240 if ((verlen
& 0xF0) == 4)
241 parse_eth_proto(skb
, ETH_P_IP
);
243 parse_eth_proto(skb
, ETH_P_IPV6
);
245 parse_eth_proto(skb
, ETH_P_MPLS_UC
);
252 __uint(type
, BPF_MAP_TYPE_PROG_ARRAY
);
253 __uint(key_size
, sizeof(u32
));
254 __uint(max_entries
, 8);
255 __array(values
, u32 (void *));
256 } prog_array_init
SEC(".maps") = {
258 [PARSE_VLAN
] = (void *)&bpf_func_vlan
,
259 [PARSE_IP
] = (void *)&bpf_func_ip
,
260 [PARSE_IPV6
] = (void *)&bpf_func_ipv6
,
261 [PARSE_MPLS
] = (void *)&bpf_func_mpls
,
265 /* Protocol dispatch routine. It tail-calls next BPF program depending
266 * on eth proto. Note, we could have used ...
268 * bpf_tail_call(skb, &prog_array_init, proto);
270 * ... but it would need large prog_array and cannot be optimised given
271 * the map key is not static.
273 static inline void parse_eth_proto(struct __sk_buff
*skb
, u32 proto
)
278 bpf_tail_call(skb
, &prog_array_init
, PARSE_VLAN
);
282 bpf_tail_call(skb
, &prog_array_init
, PARSE_MPLS
);
285 bpf_tail_call(skb
, &prog_array_init
, PARSE_IP
);
288 bpf_tail_call(skb
, &prog_array_init
, PARSE_IPV6
);
294 int main_prog(struct __sk_buff
*skb
)
296 __u32 nhoff
= ETH_HLEN
;
297 __u32 proto
= load_half(skb
, 12);
300 parse_eth_proto(skb
, proto
);
304 char _license
[] SEC("license") = "GPL";