summaryrefslogtreecommitdiff
path: root/samples/bpf/sock_example.h
diff options
context:
space:
mode:
authorJoe Stringer <joe@ovn.org>2016-12-09 05:46:20 +0300
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-12-20 18:00:40 +0300
commit9899694a7f67714216665b87318eb367e2c5c901 (patch)
tree54f29b8abd381ca36208b9cc6fe8de308800f881 /samples/bpf/sock_example.h
parent205c8ada314f78e6637342089e5b585a051d6cf5 (diff)
downloadlinux-9899694a7f67714216665b87318eb367e2c5c901.tar.xz
samples/bpf: Move open_raw_sock to separate header
This function was declared in libbpf.c and was the only remaining function in this library, but has nothing to do with BPF. Shift it out into a new header, sock_example.h, and include it from the relevant samples. Signed-off-by: Joe Stringer <joe@ovn.org> Cc: Alexei Starovoitov <ast@fb.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Wang Nan <wangnan0@huawei.com> Link: http://lkml.kernel.org/r/20161209024620.31660-8-joe@ovn.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'samples/bpf/sock_example.h')
-rw-r--r--samples/bpf/sock_example.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/samples/bpf/sock_example.h b/samples/bpf/sock_example.h
new file mode 100644
index 000000000000..09f7fe7e5fd7
--- /dev/null
+++ b/samples/bpf/sock_example.h
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <linux/unistd.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <linux/if_packet.h>
+#include <arpa/inet.h>
+#include "libbpf.h"
+
+static inline int open_raw_sock(const char *name)
+{
+ struct sockaddr_ll sll;
+ int sock;
+
+ sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL));
+ if (sock < 0) {
+ printf("cannot create raw socket\n");
+ return -1;
+ }
+
+ memset(&sll, 0, sizeof(sll));
+ sll.sll_family = AF_PACKET;
+ sll.sll_ifindex = if_nametoindex(name);
+ sll.sll_protocol = htons(ETH_P_ALL);
+ if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
+ printf("bind to %s: %s\n", name, strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ return sock;
+}