cgroup.c 12 KB

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