1 #ifndef __BPF_API__ 2 #define __BPF_API__ 3 4 /* Note: 5 * 6 * This file can be included into eBPF kernel programs. It contains 7 * a couple of useful helper functions, map/section ABI (bpf_elf.h), 8 * misc macros and some eBPF specific LLVM built-ins. 9 */ 10 11 #include <stdint.h> 12 13 #include <linux/pkt_cls.h> 14 #include <linux/bpf.h> 15 #include <linux/filter.h> 16 17 #include <asm/byteorder.h> 18 19 #include "bpf_elf.h" 20 21 /** Misc macros. */ 22 23 #ifndef __stringify 24 # define __stringify(X) #X 25 #endif 26 27 #ifndef __maybe_unused 28 # define __maybe_unused __attribute__((__unused__)) 29 #endif 30 31 #ifndef offsetof 32 # define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER) 33 #endif 34 35 #ifndef likely 36 # define likely(X) __builtin_expect(!!(X), 1) 37 #endif 38 39 #ifndef unlikely 40 # define unlikely(X) __builtin_expect(!!(X), 0) 41 #endif 42 43 #ifndef htons 44 # define htons(X) __constant_htons((X)) 45 #endif 46 47 #ifndef ntohs 48 # define ntohs(X) __constant_ntohs((X)) 49 #endif 50 51 #ifndef htonl 52 # define htonl(X) __constant_htonl((X)) 53 #endif 54 55 #ifndef ntohl 56 # define ntohl(X) __constant_ntohl((X)) 57 #endif 58 59 /** Section helper macros. */ 60 61 #ifndef __section 62 # define __section(NAME) \ 63 __attribute__((section(NAME), used)) 64 #endif 65 66 #ifndef __section_tail 67 # define __section_tail(ID, KEY) \ 68 __section(__stringify(ID) "/" __stringify(KEY)) 69 #endif 70 71 #ifndef __section_cls_entry 72 # define __section_cls_entry \ 73 __section(ELF_SECTION_CLASSIFIER) 74 #endif 75 76 #ifndef __section_act_entry 77 # define __section_act_entry \ 78 __section(ELF_SECTION_ACTION) 79 #endif 80 81 #ifndef __section_license 82 # define __section_license \ 83 __section(ELF_SECTION_LICENSE) 84 #endif 85 86 #ifndef __section_maps 87 # define __section_maps \ 88 __section(ELF_SECTION_MAPS) 89 #endif 90 91 /** Declaration helper macros. */ 92 93 #ifndef BPF_LICENSE 94 # define BPF_LICENSE(NAME) \ 95 char ____license[] __section_license = NAME 96 #endif 97 98 #ifndef __BPF_MAP 99 # define __BPF_MAP(NAME, TYPE, ID, SIZE_KEY, SIZE_VALUE, PIN, MAX_ELEM) \ 100 struct bpf_elf_map __section_maps NAME = { \ 101 .type = (TYPE), \ 102 .id = (ID), \ 103 .size_key = (SIZE_KEY), \ 104 .size_value = (SIZE_VALUE), \ 105 .pinning = (PIN), \ 106 .max_elem = (MAX_ELEM), \ 107 } 108 #endif 109 110 #ifndef BPF_HASH 111 # define BPF_HASH(NAME, ID, SIZE_KEY, SIZE_VALUE, PIN, MAX_ELEM) \ 112 __BPF_MAP(NAME, BPF_MAP_TYPE_HASH, ID, SIZE_KEY, SIZE_VALUE, \ 113 PIN, MAX_ELEM) 114 #endif 115 116 #ifndef BPF_ARRAY 117 # define BPF_ARRAY(NAME, ID, SIZE_VALUE, PIN, MAX_ELEM) \ 118 __BPF_MAP(NAME, BPF_MAP_TYPE_ARRAY, ID, sizeof(uint32_t), \ 119 SIZE_VALUE, PIN, MAX_ELEM) 120 #endif 121 122 #ifndef BPF_ARRAY2 123 # define BPF_ARRAY2(NAME, ID, PIN, MAX_ELEM) \ 124 BPF_ARRAY(NAME, ID, sizeof(uint16_t), PIN, MAX_ELEM) 125 #endif 126 127 #ifndef BPF_ARRAY4 128 # define BPF_ARRAY4(NAME, ID, PIN, MAX_ELEM) \ 129 BPF_ARRAY(NAME, ID, sizeof(uint32_t), PIN, MAX_ELEM) 130 #endif 131 132 #ifndef BPF_ARRAY8 133 # define BPF_ARRAY8(NAME, ID, PIN, MAX_ELEM) \ 134 BPF_ARRAY(NAME, ID, sizeof(uint64_t), PIN, MAX_ELEM) 135 #endif 136 137 #ifndef BPF_PROG_ARRAY 138 # define BPF_PROG_ARRAY(NAME, ID, PIN, MAX_ELEM) \ 139 __BPF_MAP(NAME, BPF_MAP_TYPE_PROG_ARRAY, ID, sizeof(uint32_t), \ 140 sizeof(uint32_t), PIN, MAX_ELEM) 141 #endif 142 143 /** Classifier helper */ 144 145 #ifndef BPF_H_DEFAULT 146 # define BPF_H_DEFAULT -1 147 #endif 148 149 /** BPF helper functions for tc. */ 150 151 #ifndef BPF_FUNC 152 # define BPF_FUNC(NAME, ...) \ 153 (* NAME)(__VA_ARGS__) __maybe_unused = (void *) BPF_FUNC_##NAME 154 #endif 155 156 /* Map access/manipulation */ 157 static void *BPF_FUNC(map_lookup_elem, void *map, const void *key); 158 static int BPF_FUNC(map_update_elem, void *map, const void *key, 159 const void *value, uint32_t flags); 160 static int BPF_FUNC(map_delete_elem, void *map, const void *key); 161 162 /* Time access */ 163 static uint64_t BPF_FUNC(ktime_get_ns); 164 165 /* Debugging */ 166 static void BPF_FUNC(trace_printk, const char *fmt, int fmt_size, ...); 167 168 /* Random numbers */ 169 static uint32_t BPF_FUNC(get_prandom_u32); 170 171 /* Tail calls */ 172 static void BPF_FUNC(tail_call, struct __sk_buff *skb, void *map, 173 uint32_t index); 174 175 /* System helpers */ 176 static uint32_t BPF_FUNC(get_smp_processor_id); 177 178 /* Packet misc meta data */ 179 static uint32_t BPF_FUNC(get_cgroup_classid, struct __sk_buff *skb); 180 static uint32_t BPF_FUNC(get_route_realm, struct __sk_buff *skb); 181 182 /* Packet redirection */ 183 static int BPF_FUNC(redirect, int ifindex, uint32_t flags); 184 static int BPF_FUNC(clone_redirect, struct __sk_buff *skb, int ifindex, 185 uint32_t flags); 186 187 /* Packet manipulation */ 188 #define BPF_PSEUDO_HDR 0x10 189 #define BPF_HAS_PSEUDO_HDR(flags) ((flags) & BPF_PSEUDO_HDR) 190 #define BPF_HDR_FIELD_SIZE(flags) ((flags) & 0x0f) 191 192 static int BPF_FUNC(skb_store_bytes, struct __sk_buff *skb, uint32_t off, 193 void *from, uint32_t len, uint32_t flags); 194 static int BPF_FUNC(l3_csum_replace, struct __sk_buff *skb, uint32_t off, 195 uint32_t from, uint32_t to, uint32_t flags); 196 static int BPF_FUNC(l4_csum_replace, struct __sk_buff *skb, uint32_t off, 197 uint32_t from, uint32_t to, uint32_t flags); 198 199 /* Packet vlan encap/decap */ 200 static int BPF_FUNC(skb_vlan_push, struct __sk_buff *skb, uint16_t proto, 201 uint16_t vlan_tci); 202 static int BPF_FUNC(skb_vlan_pop, struct __sk_buff *skb); 203 204 /* Packet tunnel encap/decap */ 205 static int BPF_FUNC(skb_get_tunnel_key, struct __sk_buff *skb, 206 struct bpf_tunnel_key *to, uint32_t size, uint32_t flags); 207 static int BPF_FUNC(skb_set_tunnel_key, struct __sk_buff *skb, 208 struct bpf_tunnel_key *from, uint32_t size, uint32_t flags); 209 210 /** LLVM built-ins */ 211 212 #ifndef lock_xadd 213 # define lock_xadd(ptr, val) ((void) __sync_fetch_and_add(ptr, val)) 214 #endif 215 216 unsigned long long load_byte(void *skb, unsigned long long off) 217 asm ("llvm.bpf.load.byte"); 218 219 unsigned long long load_half(void *skb, unsigned long long off) 220 asm ("llvm.bpf.load.half"); 221 222 unsigned long long load_word(void *skb, unsigned long long off) 223 asm ("llvm.bpf.load.word"); 224 225 #endif /* __BPF_API__ */ 226