|
| 1 | +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
| 2 | + |
| 3 | +#define EEXIST 17 /* File exists */ |
| 4 | + |
| 5 | +#define BPF_MAP_TYPE_PIFO_GENERIC 31 |
| 6 | +#define BPF_MAP_TYPE_PIFO_XDP 32 |
| 7 | + |
| 8 | +/* |
| 9 | + * bpf_packet_dequeue |
| 10 | + * |
| 11 | + * Dequeue the packet at the head of the PIFO in *map* and return a pointer |
| 12 | + * to the packet (or NULL if the PIFO is empty). |
| 13 | + * |
| 14 | + * Returns |
| 15 | + * On success, a pointer to the packet, or NULL if the PIFO is empty. The |
| 16 | + * packet pointer must be freed using *bpf_packet_drop()* or returning |
| 17 | + * the packet pointer. The *rank* pointer will be set to the rank of |
| 18 | + * the dequeued packet on success, or a negative error code on error. |
| 19 | + */ |
| 20 | +static long (*bpf_packet_dequeue)(void *ctx, void *map, __u64 flags, __u64 *rank) = (void *) 194; |
| 21 | +static long (*bpf_packet_drop)(void *ctx, void *pkt) = (void *) 195; |
| 22 | + |
| 23 | +struct flow_address { |
| 24 | + struct in6_addr ip; |
| 25 | + __u16 port; |
| 26 | + __u16 reserved; |
| 27 | +}; |
| 28 | + |
| 29 | +struct network_tuple { |
| 30 | + struct flow_address saddr; |
| 31 | + struct flow_address daddr; |
| 32 | + __u16 proto; |
| 33 | + __u8 ipv; |
| 34 | + __u8 reserved; |
| 35 | +}; |
| 36 | + |
| 37 | +struct flow_state { |
| 38 | + __u32 pkts; |
| 39 | + __u32 finish_bytes; |
| 40 | +}; |
| 41 | + |
| 42 | + |
| 43 | +#pragma GCC diagnostic push |
| 44 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 45 | +static __always_inline void * |
| 46 | +bpf_map_lookup_or_try_init(void *map, const void *key, const void *init) |
| 47 | +{ |
| 48 | + void *val; |
| 49 | + long err; |
| 50 | + |
| 51 | + val = bpf_map_lookup_elem(map, key); |
| 52 | + if (val) |
| 53 | + return val; |
| 54 | + |
| 55 | + err = bpf_map_update_elem(map, key, init, BPF_NOEXIST); |
| 56 | + if (err && err != -EEXIST) |
| 57 | + return NULL; |
| 58 | + |
| 59 | + return bpf_map_lookup_elem(map, key); |
| 60 | +} |
| 61 | + |
| 62 | +static __always_inline int bpf_max(__u64 left, __u64 right) |
| 63 | +{ |
| 64 | + return right > left ? right : left; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +/* |
| 69 | + * Maps an IPv4 address into an IPv6 address according to RFC 4291 sec 2.5.5.2 |
| 70 | + */ |
| 71 | +static void map_ipv4_to_ipv6(struct in6_addr *ipv6, __be32 ipv4) |
| 72 | +{ |
| 73 | + __builtin_memset(&ipv6->in6_u.u6_addr8[0], 0x00, 10); |
| 74 | + __builtin_memset(&ipv6->in6_u.u6_addr8[10], 0xff, 2); |
| 75 | + ipv6->in6_u.u6_addr32[3] = ipv4; |
| 76 | +} |
| 77 | +#pragma GCC diagnostic pop |
0 commit comments