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" |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 26 | #include "bpf_scm.h" |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 27 | |
Daniel Borkmann | 88eea53 | 2015-06-02 23:35:34 +0200 | [diff] [blame^] | 28 | #define BPF_ENV_UDS "TC_BPF_UDS" |
| 29 | |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 30 | int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len, |
| 31 | char **bpf_string, bool *need_release, |
| 32 | const char separator); |
| 33 | int bpf_parse_ops(int argc, char **argv, struct sock_filter *bpf_ops, |
| 34 | bool from_file); |
| 35 | void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len); |
| 36 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 37 | const char *bpf_default_section(const enum bpf_prog_type type); |
| 38 | |
| 39 | #ifdef HAVE_ELF |
| 40 | int bpf_open_object(const char *path, enum bpf_prog_type type, |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame] | 41 | const char *sec, bool verbose); |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 42 | |
| 43 | int bpf_send_map_fds(const char *path, const char *obj); |
| 44 | int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux, |
| 45 | unsigned int entries); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 46 | |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 47 | static inline __u64 bpf_ptr_to_u64(const void *ptr) |
| 48 | { |
| 49 | return (__u64) (unsigned long) ptr; |
| 50 | } |
| 51 | |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 52 | static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size) |
| 53 | { |
| 54 | #ifdef __NR_bpf |
| 55 | return syscall(__NR_bpf, cmd, attr, size); |
| 56 | #else |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 57 | fprintf(stderr, "No bpf syscall, kernel headers too old?\n"); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 58 | errno = ENOSYS; |
| 59 | return -1; |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 60 | #endif |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 61 | } |
| 62 | #else |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 63 | static inline int bpf_open_object(const char *path, enum bpf_prog_type type, |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame] | 64 | const char *sec, bool verbose) |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 65 | { |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 66 | fprintf(stderr, "No ELF library support compiled in.\n"); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 67 | errno = ENOSYS; |
| 68 | return -1; |
| 69 | } |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 70 | |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 71 | static inline int bpf_send_map_fds(const char *path, const char *obj) |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 72 | { |
| 73 | return 0; |
| 74 | } |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 75 | |
| 76 | static inline int bpf_recv_map_fds(const char *path, int *fds, |
| 77 | struct bpf_map_aux *aux, |
| 78 | unsigned int entries) |
| 79 | { |
| 80 | return -1; |
| 81 | } |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 82 | #endif /* HAVE_ELF */ |
| 83 | #endif /* _TC_BPF_H_ */ |