eeprom: at24: fix reading from 24MAC402/24MAC602
[linux/fpc-iii.git] / samples / bpf / load_sock_ops.c
blobe5da6cf71a3e1e4fd8743727fa296e01d8bcba1b
1 /* Copyright (c) 2017 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <linux/bpf.h>
11 #include "libbpf.h"
12 #include "bpf_load.h"
13 #include <unistd.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <linux/unistd.h>
18 static void usage(char *pname)
20 printf("USAGE:\n %s [-l] <cg-path> <prog filename>\n", pname);
21 printf("\tLoad and attach a sock_ops program to the specified "
22 "cgroup\n");
23 printf("\tIf \"-l\" is used, the program will continue to run\n");
24 printf("\tprinting the BPF log buffer\n");
25 printf("\tIf the specified filename does not end in \".o\", it\n");
26 printf("\tappends \"_kern.o\" to the name\n");
27 printf("\n");
28 printf(" %s -r <cg-path>\n", pname);
29 printf("\tDetaches the currently attached sock_ops program\n");
30 printf("\tfrom the specified cgroup\n");
31 printf("\n");
32 exit(1);
35 int main(int argc, char **argv)
37 int logFlag = 0;
38 int error = 0;
39 char *cg_path;
40 char fn[500];
41 char *prog;
42 int cg_fd;
44 if (argc < 3)
45 usage(argv[0]);
47 if (!strcmp(argv[1], "-r")) {
48 cg_path = argv[2];
49 cg_fd = open(cg_path, O_DIRECTORY, O_RDONLY);
50 error = bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
51 if (error) {
52 printf("ERROR: bpf_prog_detach: %d (%s)\n",
53 error, strerror(errno));
54 return 2;
56 return 0;
57 } else if (!strcmp(argv[1], "-h")) {
58 usage(argv[0]);
59 } else if (!strcmp(argv[1], "-l")) {
60 logFlag = 1;
61 if (argc < 4)
62 usage(argv[0]);
65 prog = argv[argc - 1];
66 cg_path = argv[argc - 2];
67 if (strlen(prog) > 480) {
68 fprintf(stderr, "ERROR: program name too long (> 480 chars)\n");
69 return 3;
71 cg_fd = open(cg_path, O_DIRECTORY, O_RDONLY);
73 if (!strcmp(prog + strlen(prog)-2, ".o"))
74 strcpy(fn, prog);
75 else
76 sprintf(fn, "%s_kern.o", prog);
77 if (logFlag)
78 printf("loading bpf file:%s\n", fn);
79 if (load_bpf_file(fn)) {
80 printf("ERROR: load_bpf_file failed for: %s\n", fn);
81 printf("%s", bpf_log_buf);
82 return 4;
84 if (logFlag)
85 printf("TCP BPF Loaded %s\n", fn);
87 error = bpf_prog_attach(prog_fd[0], cg_fd, BPF_CGROUP_SOCK_OPS, 0);
88 if (error) {
89 printf("ERROR: bpf_prog_attach: %d (%s)\n",
90 error, strerror(errno));
91 return 5;
92 } else if (logFlag) {
93 read_trace_pipe();
96 return error;