blob: 05baeecda57f47f255088fc3bc7893be773ddb87 [file] [log] [blame]
Jiri Pirko1d129d12015-01-19 16:56:29 +01001/*
Daniel Borkmanne4225662016-11-10 01:20:59 +01002 * bpf_util.h BPF common code
Jiri Pirko1d129d12015-01-19 16:56:29 +01003 *
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 Borkmanne4225662016-11-10 01:20:59 +01009 * Authors: Daniel Borkmann <daniel@iogearbox.net>
Jiri Pirko1d129d12015-01-19 16:56:29 +010010 * Jiri Pirko <jiri@resnulli.us>
11 */
12
Daniel Borkmanne4225662016-11-10 01:20:59 +010013#ifndef __BPF_UTIL__
14#define __BPF_UTIL__
Jiri Pirko1d129d12015-01-19 16:56:29 +010015
Daniel Borkmann11c39b52015-03-16 19:37:41 +010016#include <linux/bpf.h>
Daniel Borkmanne4225662016-11-10 01:20:59 +010017#include <linux/filter.h>
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010018#include <linux/magic.h>
Daniel Borkmanne4225662016-11-10 01:20:59 +010019#include <linux/elf-em.h>
20#include <linux/if_alg.h>
Daniel Borkmann11c39b52015-03-16 19:37:41 +010021
22#include "utils.h"
Daniel Borkmann4bd62442015-04-16 21:20:06 +020023#include "bpf_scm.h"
Daniel Borkmann11c39b52015-03-16 19:37:41 +010024
Daniel Borkmann88eea532015-06-02 23:35:34 +020025#define BPF_ENV_UDS "TC_BPF_UDS"
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010026#define BPF_ENV_MNT "TC_BPF_MNT"
Daniel Borkmann88eea532015-06-02 23:35:34 +020027
Daniel Borkmannafc1a202016-04-09 00:32:04 +020028#ifndef BPF_MAX_LOG
29# define BPF_MAX_LOG 4096
30#endif
31
Daniel Borkmanne4225662016-11-10 01:20:59 +010032#define BPF_DIR_GLOBALS "globals"
33
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010034#ifndef BPF_FS_MAGIC
35# define BPF_FS_MAGIC 0xcafe4a11
36#endif
Jiri Pirko1d129d12015-01-19 16:56:29 +010037
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010038#define BPF_DIR_MNT "/sys/fs/bpf"
39
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010040#ifndef TRACEFS_MAGIC
41# define TRACEFS_MAGIC 0x74726163
42#endif
43
44#define TRACE_DIR_MNT "/sys/kernel/tracing"
45
Daniel Borkmanne4225662016-11-10 01:20:59 +010046#ifndef AF_ALG
47# define AF_ALG 38
48#endif
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020049
Daniel Borkmanne4225662016-11-10 01:20:59 +010050#ifndef EM_BPF
51# define EM_BPF 247
52#endif
53
54struct 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
59struct 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
68int bpf_parse_common(enum bpf_prog_type type, struct bpf_cfg_in *cfg,
69 const struct bpf_cfg_ops *ops, void *nl);
70
71const char *bpf_prog_to_default_section(enum bpf_prog_type type);
72
Daniel Borkmann91d88ee2015-11-26 15:38:45 +010073int bpf_graft_map(const char *map_path, uint32_t *key, int argc, char **argv);
Daniel Borkmanne4225662016-11-10 01:20:59 +010074int bpf_trace_pipe(void);
Daniel Borkmann4bd62442015-04-16 21:20:06 +020075
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010076void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len);
77
78#ifdef HAVE_ELF
Daniel Borkmann4bd62442015-04-16 21:20:06 +020079int bpf_send_map_fds(const char *path, const char *obj);
80int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
81 unsigned int entries);
Daniel Borkmann11c39b52015-03-16 19:37:41 +010082#else
Daniel Borkmann4bd62442015-04-16 21:20:06 +020083static inline int bpf_send_map_fds(const char *path, const char *obj)
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020084{
85 return 0;
86}
Daniel Borkmann4bd62442015-04-16 21:20:06 +020087
88static 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 Borkmann11c39b52015-03-16 19:37:41 +010094#endif /* HAVE_ELF */
Daniel Borkmanne4225662016-11-10 01:20:59 +010095#endif /* __BPF_UTIL__ */