Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 1 | /* |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 2 | * bpf_util.h BPF common code |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 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 | * |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 9 | * Authors: Daniel Borkmann <daniel@iogearbox.net> |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 10 | * Jiri Pirko <jiri@resnulli.us> |
| 11 | */ |
| 12 | |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 13 | #ifndef __BPF_UTIL__ |
| 14 | #define __BPF_UTIL__ |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 15 | |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 16 | #include <linux/bpf.h> |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 17 | #include <linux/filter.h> |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 18 | #include <linux/magic.h> |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 19 | #include <linux/elf-em.h> |
| 20 | #include <linux/if_alg.h> |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 21 | |
| 22 | #include "utils.h" |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 23 | #include "bpf_scm.h" |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 24 | |
Daniel Borkmann | 88eea53 | 2015-06-02 23:35:34 +0200 | [diff] [blame] | 25 | #define BPF_ENV_UDS "TC_BPF_UDS" |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 26 | #define BPF_ENV_MNT "TC_BPF_MNT" |
Daniel Borkmann | 88eea53 | 2015-06-02 23:35:34 +0200 | [diff] [blame] | 27 | |
Daniel Borkmann | afc1a20 | 2016-04-09 00:32:04 +0200 | [diff] [blame] | 28 | #ifndef BPF_MAX_LOG |
| 29 | # define BPF_MAX_LOG 4096 |
| 30 | #endif |
| 31 | |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 32 | #define BPF_DIR_GLOBALS "globals" |
| 33 | |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 34 | #ifndef BPF_FS_MAGIC |
| 35 | # define BPF_FS_MAGIC 0xcafe4a11 |
| 36 | #endif |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 37 | |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 38 | #define BPF_DIR_MNT "/sys/fs/bpf" |
| 39 | |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 40 | #ifndef TRACEFS_MAGIC |
| 41 | # define TRACEFS_MAGIC 0x74726163 |
| 42 | #endif |
| 43 | |
| 44 | #define TRACE_DIR_MNT "/sys/kernel/tracing" |
| 45 | |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 46 | #ifndef AF_ALG |
| 47 | # define AF_ALG 38 |
| 48 | #endif |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 49 | |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 50 | #ifndef EM_BPF |
| 51 | # define EM_BPF 247 |
| 52 | #endif |
| 53 | |
| 54 | struct bpf_cfg_ops { |
| 55 | void (*cbpf_cb)(void *nl, const struct sock_filter *ops, int ops_len); |
| 56 | void (*ebpf_cb)(void *nl, int fd, const char *annotation); |
| 57 | }; |
| 58 | |
| 59 | struct bpf_cfg_in { |
| 60 | const char *object; |
| 61 | const char *section; |
| 62 | const char *uds; |
| 63 | int argc; |
| 64 | char **argv; |
| 65 | struct sock_filter *ops; |
| 66 | }; |
| 67 | |
| 68 | int bpf_parse_common(enum bpf_prog_type type, struct bpf_cfg_in *cfg, |
| 69 | const struct bpf_cfg_ops *ops, void *nl); |
| 70 | |
| 71 | const char *bpf_prog_to_default_section(enum bpf_prog_type type); |
| 72 | |
Daniel Borkmann | 91d88ee | 2015-11-26 15:38:45 +0100 | [diff] [blame] | 73 | int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv); |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 74 | int bpf_trace_pipe(void); |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 75 | |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 76 | void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len); |
| 77 | |
| 78 | #ifdef HAVE_ELF |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 79 | int bpf_send_map_fds(const char *path, const char *obj); |
| 80 | int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux, |
| 81 | unsigned int entries); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 82 | #else |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 83 | 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] | 84 | { |
| 85 | return 0; |
| 86 | } |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 87 | |
| 88 | static inline int bpf_recv_map_fds(const char *path, int *fds, |
| 89 | struct bpf_map_aux *aux, |
| 90 | unsigned int entries) |
| 91 | { |
| 92 | return -1; |
| 93 | } |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 94 | #endif /* HAVE_ELF */ |
Daniel Borkmann | e422566 | 2016-11-10 01:20:59 +0100 | [diff] [blame^] | 95 | #endif /* __BPF_UTIL__ */ |