Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * tc_bpf.h BPF common code |
| 3 | * |
| 4 | * This program is free software; you can distribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Daniel Borkmann <dborkman@redhat.com> |
| 10 | * Jiri Pirko <jiri@resnulli.us> |
| 11 | */ |
| 12 | |
| 13 | #ifndef _TC_BPF_H_ |
| 14 | #define _TC_BPF_H_ 1 |
| 15 | |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 16 | #include <linux/filter.h> |
| 17 | #include <linux/netlink.h> |
| 18 | #include <linux/rtnetlink.h> |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 19 | #include <linux/bpf.h> |
| 20 | #include <sys/syscall.h> |
| 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include "utils.h" |
| 26 | |
| 27 | /* Note: |
| 28 | * |
| 29 | * Below ELF section names and bpf_elf_map structure definition |
| 30 | * are not (!) kernel ABI. It's rather a "contract" between the |
| 31 | * application and the BPF loader in tc. For compatibility, the |
| 32 | * section names should stay as-is. Introduction of aliases, if |
| 33 | * needed, are a possibility, though. |
| 34 | */ |
| 35 | |
| 36 | /* ELF section names, etc */ |
| 37 | #define ELF_SECTION_LICENSE "license" |
| 38 | #define ELF_SECTION_MAPS "maps" |
| 39 | #define ELF_SECTION_CLASSIFIER "classifier" |
| 40 | #define ELF_SECTION_ACTION "action" |
| 41 | |
| 42 | #define ELF_MAX_MAPS 64 |
| 43 | #define ELF_MAX_LICENSE_LEN 128 |
| 44 | |
| 45 | /* ELF map definition */ |
| 46 | struct bpf_elf_map { |
| 47 | __u32 type; |
| 48 | __u32 size_key; |
| 49 | __u32 size_value; |
| 50 | __u32 max_elem; |
| 51 | }; |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 52 | |
| 53 | int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len, |
| 54 | char **bpf_string, bool *need_release, |
| 55 | const char separator); |
| 56 | int bpf_parse_ops(int argc, char **argv, struct sock_filter *bpf_ops, |
| 57 | bool from_file); |
| 58 | void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len); |
| 59 | |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 60 | static inline __u64 bpf_ptr_to_u64(const void *ptr) |
| 61 | { |
| 62 | return (__u64) (unsigned long) ptr; |
| 63 | } |
| 64 | |
| 65 | #ifdef HAVE_ELF |
| 66 | int bpf_open_object(const char *path, enum bpf_prog_type type); |
| 67 | |
| 68 | static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size) |
| 69 | { |
| 70 | #ifdef __NR_bpf |
| 71 | return syscall(__NR_bpf, cmd, attr, size); |
| 72 | #else |
| 73 | errno = ENOSYS; |
| 74 | return -1; |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 75 | #endif |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 76 | } |
| 77 | #else |
| 78 | static inline int bpf_open_object(const char *path, enum bpf_prog_type type) |
| 79 | { |
| 80 | errno = ENOSYS; |
| 81 | return -1; |
| 82 | } |
| 83 | #endif /* HAVE_ELF */ |
| 84 | #endif /* _TC_BPF_H_ */ |