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.
10 #include <linux/bpf.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 "
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");
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");
35 int main(int argc, char **argv)
47 if (!strcmp(argv[1], "-r")) {
49 cg_fd = open(cg_path, O_DIRECTORY, O_RDONLY);
50 error = bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
52 printf("ERROR: bpf_prog_detach: %d (%s)\n",
53 error, strerror(errno));
57 } else if (!strcmp(argv[1], "-h")) {
59 } else if (!strcmp(argv[1], "-l")) {
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");
71 cg_fd = open(cg_path, O_DIRECTORY, O_RDONLY);
73 if (!strcmp(prog + strlen(prog)-2, ".o"))
76 sprintf(fn, "%s_kern.o", prog);
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);
85 printf("TCP BPF Loaded %s\n", fn);
87 error = bpf_prog_attach(prog_fd[0], cg_fd, BPF_CGROUP_SOCK_OPS, 0);
89 printf("ERROR: bpf_prog_attach: %d (%s)\n",
90 error, strerror(errno));