cgroup.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (C) 2017 Facebook
  3. // Author: Roman Gushchin <guro@fb.com>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <bpf.h>
  11. #include "main.h"
  12. #define HELP_SPEC_ATTACH_FLAGS \
  13. "ATTACH_FLAGS := { multi | override }"
  14. #define HELP_SPEC_ATTACH_TYPES \
  15. "ATTACH_TYPE := { ingress | egress | sock_create | sock_ops | device }"
  16. static const char * const attach_type_strings[] = {
  17. [BPF_CGROUP_INET_INGRESS] = "ingress",
  18. [BPF_CGROUP_INET_EGRESS] = "egress",
  19. [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
  20. [BPF_CGROUP_SOCK_OPS] = "sock_ops",
  21. [BPF_CGROUP_DEVICE] = "device",
  22. [__MAX_BPF_ATTACH_TYPE] = NULL,
  23. };
  24. static enum bpf_attach_type parse_attach_type(const char *str)
  25. {
  26. enum bpf_attach_type type;
  27. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  28. if (attach_type_strings[type] &&
  29. is_prefix(str, attach_type_strings[type]))
  30. return type;
  31. }
  32. return __MAX_BPF_ATTACH_TYPE;
  33. }
  34. static int show_bpf_prog(int id, const char *attach_type_str,
  35. const char *attach_flags_str)
  36. {
  37. struct bpf_prog_info info = {};
  38. __u32 info_len = sizeof(info);
  39. int prog_fd;
  40. prog_fd = bpf_prog_get_fd_by_id(id);
  41. if (prog_fd < 0)
  42. return -1;
  43. if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) {
  44. close(prog_fd);
  45. return -1;
  46. }
  47. if (json_output) {
  48. jsonw_start_object(json_wtr);
  49. jsonw_uint_field(json_wtr, "id", info.id);
  50. jsonw_string_field(json_wtr, "attach_type",
  51. attach_type_str);
  52. jsonw_string_field(json_wtr, "attach_flags",
  53. attach_flags_str);
  54. jsonw_string_field(json_wtr, "name", info.name);
  55. jsonw_end_object(json_wtr);
  56. } else {
  57. printf("%-8u %-15s %-15s %-15s\n", info.id,
  58. attach_type_str,
  59. attach_flags_str,
  60. info.name);
  61. }
  62. close(prog_fd);
  63. return 0;
  64. }
  65. static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
  66. {
  67. __u32 prog_ids[1024] = {0};
  68. char *attach_flags_str;
  69. __u32 prog_cnt, iter;
  70. __u32 attach_flags;
  71. char buf[32];
  72. int ret;
  73. prog_cnt = ARRAY_SIZE(prog_ids);
  74. ret = bpf_prog_query(cgroup_fd, type, 0, &attach_flags, prog_ids,
  75. &prog_cnt);
  76. if (ret)
  77. return ret;
  78. if (prog_cnt == 0)
  79. return 0;
  80. switch (attach_flags) {
  81. case BPF_F_ALLOW_MULTI:
  82. attach_flags_str = "multi";
  83. break;
  84. case BPF_F_ALLOW_OVERRIDE:
  85. attach_flags_str = "override";
  86. break;
  87. case 0:
  88. attach_flags_str = "";
  89. break;
  90. default:
  91. snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
  92. attach_flags_str = buf;
  93. }
  94. for (iter = 0; iter < prog_cnt; iter++)
  95. show_bpf_prog(prog_ids[iter], attach_type_strings[type],
  96. attach_flags_str);
  97. return 0;
  98. }
  99. static int do_show(int argc, char **argv)
  100. {
  101. enum bpf_attach_type type;
  102. int cgroup_fd;
  103. int ret = -1;
  104. if (argc < 1) {
  105. p_err("too few parameters for cgroup show\n");
  106. goto exit;
  107. } else if (argc > 1) {
  108. p_err("too many parameters for cgroup show\n");
  109. goto exit;
  110. }
  111. cgroup_fd = open(argv[0], O_RDONLY);
  112. if (cgroup_fd < 0) {
  113. p_err("can't open cgroup %s\n", argv[1]);
  114. goto exit;
  115. }
  116. if (json_output)
  117. jsonw_start_array(json_wtr);
  118. else
  119. printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
  120. "AttachFlags", "Name");
  121. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  122. /*
  123. * Not all attach types may be supported, so it's expected,
  124. * that some requests will fail.
  125. * If we were able to get the show for at least one
  126. * attach type, let's return 0.
  127. */
  128. if (show_attached_bpf_progs(cgroup_fd, type) == 0)
  129. ret = 0;
  130. }
  131. if (json_output)
  132. jsonw_end_array(json_wtr);
  133. close(cgroup_fd);
  134. exit:
  135. return ret;
  136. }
  137. static int do_attach(int argc, char **argv)
  138. {
  139. enum bpf_attach_type attach_type;
  140. int cgroup_fd, prog_fd;
  141. int attach_flags = 0;
  142. int ret = -1;
  143. int i;
  144. if (argc < 4) {
  145. p_err("too few parameters for cgroup attach\n");
  146. goto exit;
  147. }
  148. cgroup_fd = open(argv[0], O_RDONLY);
  149. if (cgroup_fd < 0) {
  150. p_err("can't open cgroup %s\n", argv[1]);
  151. goto exit;
  152. }
  153. attach_type = parse_attach_type(argv[1]);
  154. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  155. p_err("invalid attach type\n");
  156. goto exit_cgroup;
  157. }
  158. argc -= 2;
  159. argv = &argv[2];
  160. prog_fd = prog_parse_fd(&argc, &argv);
  161. if (prog_fd < 0)
  162. goto exit_cgroup;
  163. for (i = 0; i < argc; i++) {
  164. if (is_prefix(argv[i], "multi")) {
  165. attach_flags |= BPF_F_ALLOW_MULTI;
  166. } else if (is_prefix(argv[i], "override")) {
  167. attach_flags |= BPF_F_ALLOW_OVERRIDE;
  168. } else {
  169. p_err("unknown option: %s\n", argv[i]);
  170. goto exit_cgroup;
  171. }
  172. }
  173. if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, attach_flags)) {
  174. p_err("failed to attach program");
  175. goto exit_prog;
  176. }
  177. if (json_output)
  178. jsonw_null(json_wtr);
  179. ret = 0;
  180. exit_prog:
  181. close(prog_fd);
  182. exit_cgroup:
  183. close(cgroup_fd);
  184. exit:
  185. return ret;
  186. }
  187. static int do_detach(int argc, char **argv)
  188. {
  189. enum bpf_attach_type attach_type;
  190. int prog_fd, cgroup_fd;
  191. int ret = -1;
  192. if (argc < 4) {
  193. p_err("too few parameters for cgroup detach\n");
  194. goto exit;
  195. }
  196. cgroup_fd = open(argv[0], O_RDONLY);
  197. if (cgroup_fd < 0) {
  198. p_err("can't open cgroup %s\n", argv[1]);
  199. goto exit;
  200. }
  201. attach_type = parse_attach_type(argv[1]);
  202. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  203. p_err("invalid attach type");
  204. goto exit_cgroup;
  205. }
  206. argc -= 2;
  207. argv = &argv[2];
  208. prog_fd = prog_parse_fd(&argc, &argv);
  209. if (prog_fd < 0)
  210. goto exit_cgroup;
  211. if (bpf_prog_detach2(prog_fd, cgroup_fd, attach_type)) {
  212. p_err("failed to detach program");
  213. goto exit_prog;
  214. }
  215. if (json_output)
  216. jsonw_null(json_wtr);
  217. ret = 0;
  218. exit_prog:
  219. close(prog_fd);
  220. exit_cgroup:
  221. close(cgroup_fd);
  222. exit:
  223. return ret;
  224. }
  225. static int do_help(int argc, char **argv)
  226. {
  227. if (json_output) {
  228. jsonw_null(json_wtr);
  229. return 0;
  230. }
  231. fprintf(stderr,
  232. "Usage: %s %s list CGROUP\n"
  233. " %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
  234. " %s %s detach CGROUP ATTACH_TYPE PROG\n"
  235. " %s %s help\n"
  236. "\n"
  237. " " HELP_SPEC_ATTACH_TYPES "\n"
  238. " " HELP_SPEC_ATTACH_FLAGS "\n"
  239. " " HELP_SPEC_PROGRAM "\n"
  240. " " HELP_SPEC_OPTIONS "\n"
  241. "",
  242. bin_name, argv[-2], bin_name, argv[-2],
  243. bin_name, argv[-2], bin_name, argv[-2]);
  244. return 0;
  245. }
  246. static const struct cmd cmds[] = {
  247. { "list", do_show },
  248. { "attach", do_attach },
  249. { "detach", do_detach },
  250. { "help", do_help },
  251. { 0 }
  252. };
  253. int do_cgroup(int argc, char **argv)
  254. {
  255. return cmd_select(cmds, argc, argv, do_help);
  256. }