Back to home page

OSCL-LXR

 
 

    


0001 #include <uapi/linux/bpf.h>
0002 #include <uapi/linux/if_ether.h>
0003 #include <uapi/linux/if_packet.h>
0004 #include <uapi/linux/ip.h>
0005 #include <bpf/bpf_helpers.h>
0006 #include "bpf_legacy.h"
0007 
0008 struct {
0009     __uint(type, BPF_MAP_TYPE_ARRAY);
0010     __type(key, u32);
0011     __type(value, long);
0012     __uint(max_entries, 256);
0013 } my_map SEC(".maps");
0014 
0015 SEC("socket1")
0016 int bpf_prog1(struct __sk_buff *skb)
0017 {
0018     int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
0019     long *value;
0020 
0021     if (skb->pkt_type != PACKET_OUTGOING)
0022         return 0;
0023 
0024     value = bpf_map_lookup_elem(&my_map, &index);
0025     if (value)
0026         __sync_fetch_and_add(value, skb->len);
0027 
0028     return 0;
0029 }
0030 char _license[] SEC("license") = "GPL";