Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  *
0007  * This program is distributed in the hope that it will be useful, but
0008  * WITHOUT ANY WARRANTY; without even the implied warranty of
0009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0010  * General Public License for more details.
0011  */
0012 
0013 #include <uapi/linux/bpf.h>
0014 #include <uapi/linux/if_ether.h>
0015 #include <uapi/linux/ip.h>
0016 #include <uapi/linux/in.h>
0017 #include <bpf/bpf_helpers.h>
0018 
0019 struct bpf_elf_map {
0020     __u32 type;
0021     __u32 size_key;
0022     __u32 size_value;
0023     __u32 max_elem;
0024     __u32 flags;
0025     __u32 id;
0026     __u32 pinning;
0027 };
0028 
0029 struct bpf_elf_map SEC("maps") lwt_len_hist_map = {
0030     .type = BPF_MAP_TYPE_PERCPU_HASH,
0031     .size_key = sizeof(__u64),
0032     .size_value = sizeof(__u64),
0033     .pinning = 2,
0034     .max_elem = 1024,
0035 };
0036 
0037 static unsigned int log2(unsigned int v)
0038 {
0039     unsigned int r;
0040     unsigned int shift;
0041 
0042     r = (v > 0xFFFF) << 4; v >>= r;
0043     shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
0044     shift = (v > 0xF) << 2; v >>= shift; r |= shift;
0045     shift = (v > 0x3) << 1; v >>= shift; r |= shift;
0046     r |= (v >> 1);
0047     return r;
0048 }
0049 
0050 static unsigned int log2l(unsigned long v)
0051 {
0052     unsigned int hi = v >> 32;
0053     if (hi)
0054         return log2(hi) + 32;
0055     else
0056         return log2(v);
0057 }
0058 
0059 SEC("len_hist")
0060 int do_len_hist(struct __sk_buff *skb)
0061 {
0062     __u64 *value, key, init_val = 1;
0063 
0064     key = log2l(skb->len);
0065 
0066     value = bpf_map_lookup_elem(&lwt_len_hist_map, &key);
0067     if (value)
0068         __sync_fetch_and_add(value, 1);
0069     else
0070         bpf_map_update_elem(&lwt_len_hist_map, &key, &init_val, BPF_ANY);
0071 
0072     return BPF_OK;
0073 }
0074 
0075 char _license[] SEC("license") = "GPL";