blob: 8b214b83f74fce51aa1c9b44d51e5ce4b4da82e8 [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"
26
Jiri Pirko1d129d12015-01-19 16:56:29 +010027int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
28 char **bpf_string, bool *need_release,
29 const char separator);
30int bpf_parse_ops(int argc, char **argv, struct sock_filter *bpf_ops,
31 bool from_file);
32void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len);
33
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020034const char *bpf_default_section(const enum bpf_prog_type type);
35
36#ifdef HAVE_ELF
37int bpf_open_object(const char *path, enum bpf_prog_type type,
38 const char *sec);
39int bpf_handoff_map_fds(const char *path, const char *obj);
40
Daniel Borkmann11c39b52015-03-16 19:37:41 +010041static inline __u64 bpf_ptr_to_u64(const void *ptr)
42{
43 return (__u64) (unsigned long) ptr;
44}
45
Daniel Borkmann11c39b52015-03-16 19:37:41 +010046static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size)
47{
48#ifdef __NR_bpf
49 return syscall(__NR_bpf, cmd, attr, size);
50#else
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020051 fprintf(stderr, "No bpf syscall, kernel headers too old?\n");
Daniel Borkmann11c39b52015-03-16 19:37:41 +010052 errno = ENOSYS;
53 return -1;
Jiri Pirko1d129d12015-01-19 16:56:29 +010054#endif
Daniel Borkmann11c39b52015-03-16 19:37:41 +010055}
56#else
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020057static inline int bpf_open_object(const char *path, enum bpf_prog_type type,
58 const char *sec)
Daniel Borkmann11c39b52015-03-16 19:37:41 +010059{
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020060 fprintf(stderr, "No ELF library support compiled in.\n");
Daniel Borkmann11c39b52015-03-16 19:37:41 +010061 errno = ENOSYS;
62 return -1;
63}
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020064
65static inline int bpf_handoff_map_fds(const char *path, const char *obj)
66{
67 return 0;
68}
Daniel Borkmann11c39b52015-03-16 19:37:41 +010069#endif /* HAVE_ELF */
70#endif /* _TC_BPF_H_ */