cgroup.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (C) 2017 Facebook
  3. // Author: Roman Gushchin <guro@fb.com>
  4. #define _XOPEN_SOURCE 500
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <ftw.h>
  8. #include <mntent.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include <bpf.h>
  16. #include "main.h"
  17. #define HELP_SPEC_ATTACH_FLAGS \
  18. "ATTACH_FLAGS := { multi | override }"
  19. #define HELP_SPEC_ATTACH_TYPES \
  20. " ATTACH_TYPE := { ingress | egress | sock_create |\n" \
  21. " sock_ops | device | bind4 | bind6 |\n" \
  22. " post_bind4 | post_bind6 | connect4 |\n" \
  23. " connect6 | sendmsg4 | sendmsg6 }"
  24. static const char * const attach_type_strings[] = {
  25. [BPF_CGROUP_INET_INGRESS] = "ingress",
  26. [BPF_CGROUP_INET_EGRESS] = "egress",
  27. [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
  28. [BPF_CGROUP_SOCK_OPS] = "sock_ops",
  29. [BPF_CGROUP_DEVICE] = "device",
  30. [BPF_CGROUP_INET4_BIND] = "bind4",
  31. [BPF_CGROUP_INET6_BIND] = "bind6",
  32. [BPF_CGROUP_INET4_CONNECT] = "connect4",
  33. [BPF_CGROUP_INET6_CONNECT] = "connect6",
  34. [BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
  35. [BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
  36. [BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
  37. [BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
  38. [__MAX_BPF_ATTACH_TYPE] = NULL,
  39. };
  40. static enum bpf_attach_type parse_attach_type(const char *str)
  41. {
  42. enum bpf_attach_type type;
  43. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  44. if (attach_type_strings[type] &&
  45. is_prefix(str, attach_type_strings[type]))
  46. return type;
  47. }
  48. return __MAX_BPF_ATTACH_TYPE;
  49. }
  50. static int show_bpf_prog(int id, const char *attach_type_str,
  51. const char *attach_flags_str,
  52. int level)
  53. {
  54. struct bpf_prog_info info = {};
  55. __u32 info_len = sizeof(info);
  56. int prog_fd;
  57. prog_fd = bpf_prog_get_fd_by_id(id);
  58. if (prog_fd < 0)
  59. return -1;
  60. if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) {
  61. close(prog_fd);
  62. return -1;
  63. }
  64. if (json_output) {
  65. jsonw_start_object(json_wtr);
  66. jsonw_uint_field(json_wtr, "id", info.id);
  67. jsonw_string_field(json_wtr, "attach_type",
  68. attach_type_str);
  69. jsonw_string_field(json_wtr, "attach_flags",
  70. attach_flags_str);
  71. jsonw_string_field(json_wtr, "name", info.name);
  72. jsonw_end_object(json_wtr);
  73. } else {
  74. printf("%s%-8u %-15s %-15s %-15s\n", level ? " " : "",
  75. info.id,
  76. attach_type_str,
  77. attach_flags_str,
  78. info.name);
  79. }
  80. close(prog_fd);
  81. return 0;
  82. }
  83. static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
  84. {
  85. __u32 prog_cnt = 0;
  86. int ret;
  87. ret = bpf_prog_query(cgroup_fd, type, 0, NULL, NULL, &prog_cnt);
  88. if (ret)
  89. return -1;
  90. return prog_cnt;
  91. }
  92. static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
  93. int level)
  94. {
  95. __u32 prog_ids[1024] = {0};
  96. char *attach_flags_str;
  97. __u32 prog_cnt, iter;
  98. __u32 attach_flags;
  99. char buf[32];
  100. int ret;
  101. prog_cnt = ARRAY_SIZE(prog_ids);
  102. ret = bpf_prog_query(cgroup_fd, type, 0, &attach_flags, prog_ids,
  103. &prog_cnt);
  104. if (ret)
  105. return ret;
  106. if (prog_cnt == 0)
  107. return 0;
  108. switch (attach_flags) {
  109. case BPF_F_ALLOW_MULTI:
  110. attach_flags_str = "multi";
  111. break;
  112. case BPF_F_ALLOW_OVERRIDE:
  113. attach_flags_str = "override";
  114. break;
  115. case 0:
  116. attach_flags_str = "";
  117. break;
  118. default:
  119. snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
  120. attach_flags_str = buf;
  121. }
  122. for (iter = 0; iter < prog_cnt; iter++)
  123. show_bpf_prog(prog_ids[iter], attach_type_strings[type],
  124. attach_flags_str, level);
  125. return 0;
  126. }
  127. static int do_show(int argc, char **argv)
  128. {
  129. enum bpf_attach_type type;
  130. int cgroup_fd;
  131. int ret = -1;
  132. if (argc < 1) {
  133. p_err("too few parameters for cgroup show");
  134. goto exit;
  135. } else if (argc > 1) {
  136. p_err("too many parameters for cgroup show");
  137. goto exit;
  138. }
  139. cgroup_fd = open(argv[0], O_RDONLY);
  140. if (cgroup_fd < 0) {
  141. p_err("can't open cgroup %s", argv[0]);
  142. goto exit;
  143. }
  144. if (json_output)
  145. jsonw_start_array(json_wtr);
  146. else
  147. printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
  148. "AttachFlags", "Name");
  149. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  150. /*
  151. * Not all attach types may be supported, so it's expected,
  152. * that some requests will fail.
  153. * If we were able to get the show for at least one
  154. * attach type, let's return 0.
  155. */
  156. if (show_attached_bpf_progs(cgroup_fd, type, 0) == 0)
  157. ret = 0;
  158. }
  159. if (json_output)
  160. jsonw_end_array(json_wtr);
  161. close(cgroup_fd);
  162. exit:
  163. return ret;
  164. }
  165. /*
  166. * To distinguish nftw() errors and do_show_tree_fn() errors
  167. * and avoid duplicating error messages, let's return -2
  168. * from do_show_tree_fn() in case of error.
  169. */
  170. #define NFTW_ERR -1
  171. #define SHOW_TREE_FN_ERR -2
  172. static int do_show_tree_fn(const char *fpath, const struct stat *sb,
  173. int typeflag, struct FTW *ftw)
  174. {
  175. enum bpf_attach_type type;
  176. bool skip = true;
  177. int cgroup_fd;
  178. if (typeflag != FTW_D)
  179. return 0;
  180. cgroup_fd = open(fpath, O_RDONLY);
  181. if (cgroup_fd < 0) {
  182. p_err("can't open cgroup %s: %s", fpath, strerror(errno));
  183. return SHOW_TREE_FN_ERR;
  184. }
  185. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  186. int count = count_attached_bpf_progs(cgroup_fd, type);
  187. if (count < 0 && errno != EINVAL) {
  188. p_err("can't query bpf programs attached to %s: %s",
  189. fpath, strerror(errno));
  190. close(cgroup_fd);
  191. return SHOW_TREE_FN_ERR;
  192. }
  193. if (count > 0) {
  194. skip = false;
  195. break;
  196. }
  197. }
  198. if (skip) {
  199. close(cgroup_fd);
  200. return 0;
  201. }
  202. if (json_output) {
  203. jsonw_start_object(json_wtr);
  204. jsonw_string_field(json_wtr, "cgroup", fpath);
  205. jsonw_name(json_wtr, "programs");
  206. jsonw_start_array(json_wtr);
  207. } else {
  208. printf("%s\n", fpath);
  209. }
  210. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++)
  211. show_attached_bpf_progs(cgroup_fd, type, ftw->level);
  212. if (json_output) {
  213. jsonw_end_array(json_wtr);
  214. jsonw_end_object(json_wtr);
  215. }
  216. close(cgroup_fd);
  217. return 0;
  218. }
  219. static char *find_cgroup_root(void)
  220. {
  221. struct mntent *mnt;
  222. FILE *f;
  223. f = fopen("/proc/mounts", "r");
  224. if (f == NULL)
  225. return NULL;
  226. while ((mnt = getmntent(f))) {
  227. if (strcmp(mnt->mnt_type, "cgroup2") == 0) {
  228. fclose(f);
  229. return strdup(mnt->mnt_dir);
  230. }
  231. }
  232. fclose(f);
  233. return NULL;
  234. }
  235. static int do_show_tree(int argc, char **argv)
  236. {
  237. char *cgroup_root;
  238. int ret;
  239. switch (argc) {
  240. case 0:
  241. cgroup_root = find_cgroup_root();
  242. if (!cgroup_root) {
  243. p_err("cgroup v2 isn't mounted");
  244. return -1;
  245. }
  246. break;
  247. case 1:
  248. cgroup_root = argv[0];
  249. break;
  250. default:
  251. p_err("too many parameters for cgroup tree");
  252. return -1;
  253. }
  254. if (json_output)
  255. jsonw_start_array(json_wtr);
  256. else
  257. printf("%s\n"
  258. "%-8s %-15s %-15s %-15s\n",
  259. "CgroupPath",
  260. "ID", "AttachType", "AttachFlags", "Name");
  261. switch (nftw(cgroup_root, do_show_tree_fn, 1024, FTW_MOUNT)) {
  262. case NFTW_ERR:
  263. p_err("can't iterate over %s: %s", cgroup_root,
  264. strerror(errno));
  265. ret = -1;
  266. break;
  267. case SHOW_TREE_FN_ERR:
  268. ret = -1;
  269. break;
  270. default:
  271. ret = 0;
  272. }
  273. if (json_output)
  274. jsonw_end_array(json_wtr);
  275. if (argc == 0)
  276. free(cgroup_root);
  277. return ret;
  278. }
  279. static int do_attach(int argc, char **argv)
  280. {
  281. enum bpf_attach_type attach_type;
  282. int cgroup_fd, prog_fd;
  283. int attach_flags = 0;
  284. int ret = -1;
  285. int i;
  286. if (argc < 4) {
  287. p_err("too few parameters for cgroup attach");
  288. goto exit;
  289. }
  290. cgroup_fd = open(argv[0], O_RDONLY);
  291. if (cgroup_fd < 0) {
  292. p_err("can't open cgroup %s", argv[0]);
  293. goto exit;
  294. }
  295. attach_type = parse_attach_type(argv[1]);
  296. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  297. p_err("invalid attach type");
  298. goto exit_cgroup;
  299. }
  300. argc -= 2;
  301. argv = &argv[2];
  302. prog_fd = prog_parse_fd(&argc, &argv);
  303. if (prog_fd < 0)
  304. goto exit_cgroup;
  305. for (i = 0; i < argc; i++) {
  306. if (is_prefix(argv[i], "multi")) {
  307. attach_flags |= BPF_F_ALLOW_MULTI;
  308. } else if (is_prefix(argv[i], "override")) {
  309. attach_flags |= BPF_F_ALLOW_OVERRIDE;
  310. } else {
  311. p_err("unknown option: %s", argv[i]);
  312. goto exit_cgroup;
  313. }
  314. }
  315. if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, attach_flags)) {
  316. p_err("failed to attach program");
  317. goto exit_prog;
  318. }
  319. if (json_output)
  320. jsonw_null(json_wtr);
  321. ret = 0;
  322. exit_prog:
  323. close(prog_fd);
  324. exit_cgroup:
  325. close(cgroup_fd);
  326. exit:
  327. return ret;
  328. }
  329. static int do_detach(int argc, char **argv)
  330. {
  331. enum bpf_attach_type attach_type;
  332. int prog_fd, cgroup_fd;
  333. int ret = -1;
  334. if (argc < 4) {
  335. p_err("too few parameters for cgroup detach");
  336. goto exit;
  337. }
  338. cgroup_fd = open(argv[0], O_RDONLY);
  339. if (cgroup_fd < 0) {
  340. p_err("can't open cgroup %s", argv[0]);
  341. goto exit;
  342. }
  343. attach_type = parse_attach_type(argv[1]);
  344. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  345. p_err("invalid attach type");
  346. goto exit_cgroup;
  347. }
  348. argc -= 2;
  349. argv = &argv[2];
  350. prog_fd = prog_parse_fd(&argc, &argv);
  351. if (prog_fd < 0)
  352. goto exit_cgroup;
  353. if (bpf_prog_detach2(prog_fd, cgroup_fd, attach_type)) {
  354. p_err("failed to detach program");
  355. goto exit_prog;
  356. }
  357. if (json_output)
  358. jsonw_null(json_wtr);
  359. ret = 0;
  360. exit_prog:
  361. close(prog_fd);
  362. exit_cgroup:
  363. close(cgroup_fd);
  364. exit:
  365. return ret;
  366. }
  367. static int do_help(int argc, char **argv)
  368. {
  369. if (json_output) {
  370. jsonw_null(json_wtr);
  371. return 0;
  372. }
  373. fprintf(stderr,
  374. "Usage: %s %s { show | list } CGROUP\n"
  375. " %s %s tree [CGROUP_ROOT]\n"
  376. " %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
  377. " %s %s detach CGROUP ATTACH_TYPE PROG\n"
  378. " %s %s help\n"
  379. "\n"
  380. HELP_SPEC_ATTACH_TYPES "\n"
  381. " " HELP_SPEC_ATTACH_FLAGS "\n"
  382. " " HELP_SPEC_PROGRAM "\n"
  383. " " HELP_SPEC_OPTIONS "\n"
  384. "",
  385. bin_name, argv[-2],
  386. bin_name, argv[-2], bin_name, argv[-2],
  387. bin_name, argv[-2], bin_name, argv[-2]);
  388. return 0;
  389. }
  390. static const struct cmd cmds[] = {
  391. { "show", do_show },
  392. { "list", do_show },
  393. { "tree", do_show_tree },
  394. { "attach", do_attach },
  395. { "detach", do_detach },
  396. { "help", do_help },
  397. { 0 }
  398. };
  399. int do_cgroup(int argc, char **argv)
  400. {
  401. return cmd_select(cmds, argc, argv, do_help);
  402. }