blob: 431c956460ad4a51f1ea8f3ad62f185d103e88a3 [file] [log] [blame]
Alexei Starovoitova8085782014-12-01 15:06:38 -08001#include <uapi/linux/bpf.h>
2#include <uapi/linux/if_ether.h>
Alexei Starovoitov614cd3b2015-03-13 11:57:43 -07003#include <uapi/linux/if_packet.h>
Alexei Starovoitova8085782014-12-01 15:06:38 -08004#include <uapi/linux/ip.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +01005#include <bpf/bpf_helpers.h>
Andrii Nakryiko36b5d472019-10-08 10:59:37 -07006#include "bpf_legacy.h"
Alexei Starovoitova8085782014-12-01 15:06:38 -08007
Daniel T. Lee451d1dc2019-11-07 09:51:53 +09008struct {
9 __uint(type, BPF_MAP_TYPE_ARRAY);
10 __type(key, u32);
11 __type(value, long);
12 __uint(max_entries, 256);
13} my_map SEC(".maps");
Alexei Starovoitova8085782014-12-01 15:06:38 -080014
15SEC("socket1")
Alexei Starovoitov614cd3b2015-03-13 11:57:43 -070016int bpf_prog1(struct __sk_buff *skb)
Alexei Starovoitova8085782014-12-01 15:06:38 -080017{
18 int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
19 long *value;
20
Alexei Starovoitov614cd3b2015-03-13 11:57:43 -070021 if (skb->pkt_type != PACKET_OUTGOING)
22 return 0;
23
Alexei Starovoitova8085782014-12-01 15:06:38 -080024 value = bpf_map_lookup_elem(&my_map, &index);
25 if (value)
Alexei Starovoitov614cd3b2015-03-13 11:57:43 -070026 __sync_fetch_and_add(value, skb->len);
Alexei Starovoitova8085782014-12-01 15:06:38 -080027
28 return 0;
29}
30char _license[] SEC("license") = "GPL";