blob: 2ad881219e68b977b80f3452ae55a8b1d2354b92 [file] [log] [blame]
Jiri Pirko1d129d12015-01-19 16:56:29 +01001/*
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 Pirko1d129d12015-01-19 16:56:29 +010016#include <linux/filter.h>
17#include <linux/netlink.h>
18#include <linux/rtnetlink.h>
Daniel Borkmann11c39b52015-03-16 19:37:41 +010019#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 Borkmann4bd62442015-04-16 21:20:06 +020026#include "bpf_scm.h"
Daniel Borkmann11c39b52015-03-16 19:37:41 +010027
Daniel Borkmann88eea532015-06-02 23:35:34 +020028#define BPF_ENV_UDS "TC_BPF_UDS"
29
Jiri Pirko1d129d12015-01-19 16:56:29 +010030int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
31 char **bpf_string, bool *need_release,
32 const char separator);
33int bpf_parse_ops(int argc, char **argv, struct sock_filter *bpf_ops,
34 bool from_file);
35void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len);
36
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020037const char *bpf_default_section(const enum bpf_prog_type type);
38
39#ifdef HAVE_ELF
40int bpf_open_object(const char *path, enum bpf_prog_type type,
Daniel Borkmannd937a742015-04-28 13:37:42 +020041 const char *sec, bool verbose);
Daniel Borkmann4bd62442015-04-16 21:20:06 +020042
43int bpf_send_map_fds(const char *path, const char *obj);
44int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
45 unsigned int entries);
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020046
Daniel Borkmann11c39b52015-03-16 19:37:41 +010047static inline __u64 bpf_ptr_to_u64(const void *ptr)
48{
49 return (__u64) (unsigned long) ptr;
50}
51
Daniel Borkmann11c39b52015-03-16 19:37:41 +010052static 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 Borkmann6256f8c2015-04-01 17:57:44 +020057 fprintf(stderr, "No bpf syscall, kernel headers too old?\n");
Daniel Borkmann11c39b52015-03-16 19:37:41 +010058 errno = ENOSYS;
59 return -1;
Jiri Pirko1d129d12015-01-19 16:56:29 +010060#endif
Daniel Borkmann11c39b52015-03-16 19:37:41 +010061}
62#else
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020063static inline int bpf_open_object(const char *path, enum bpf_prog_type type,
Daniel Borkmannd937a742015-04-28 13:37:42 +020064 const char *sec, bool verbose)
Daniel Borkmann11c39b52015-03-16 19:37:41 +010065{
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020066 fprintf(stderr, "No ELF library support compiled in.\n");
Daniel Borkmann11c39b52015-03-16 19:37:41 +010067 errno = ENOSYS;
68 return -1;
69}
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020070
Daniel Borkmann4bd62442015-04-16 21:20:06 +020071static inline int bpf_send_map_fds(const char *path, const char *obj)
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020072{
73 return 0;
74}
Daniel Borkmann4bd62442015-04-16 21:20:06 +020075
76static 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 Borkmann11c39b52015-03-16 19:37:41 +010082#endif /* HAVE_ELF */
83#endif /* _TC_BPF_H_ */