cfg.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (C) 2018 Netronome Systems, Inc. */
  3. #include <linux/list.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "cfg.h"
  7. #include "main.h"
  8. #include "xlated_dumper.h"
  9. struct cfg {
  10. struct list_head funcs;
  11. int func_num;
  12. };
  13. struct func_node {
  14. struct list_head l;
  15. struct list_head bbs;
  16. struct bpf_insn *start;
  17. struct bpf_insn *end;
  18. int idx;
  19. int bb_num;
  20. };
  21. struct bb_node {
  22. struct list_head l;
  23. struct list_head e_prevs;
  24. struct list_head e_succs;
  25. struct bpf_insn *head;
  26. struct bpf_insn *tail;
  27. int idx;
  28. };
  29. #define EDGE_FLAG_EMPTY 0x0
  30. #define EDGE_FLAG_FALLTHROUGH 0x1
  31. #define EDGE_FLAG_JUMP 0x2
  32. struct edge_node {
  33. struct list_head l;
  34. struct bb_node *src;
  35. struct bb_node *dst;
  36. int flags;
  37. };
  38. #define ENTRY_BLOCK_INDEX 0
  39. #define EXIT_BLOCK_INDEX 1
  40. #define NUM_FIXED_BLOCKS 2
  41. #define func_prev(func) list_prev_entry(func, l)
  42. #define func_next(func) list_next_entry(func, l)
  43. #define bb_prev(bb) list_prev_entry(bb, l)
  44. #define bb_next(bb) list_next_entry(bb, l)
  45. #define entry_bb(func) func_first_bb(func)
  46. #define exit_bb(func) func_last_bb(func)
  47. #define cfg_first_func(cfg) \
  48. list_first_entry(&cfg->funcs, struct func_node, l)
  49. #define cfg_last_func(cfg) \
  50. list_last_entry(&cfg->funcs, struct func_node, l)
  51. #define func_first_bb(func) \
  52. list_first_entry(&func->bbs, struct bb_node, l)
  53. #define func_last_bb(func) \
  54. list_last_entry(&func->bbs, struct bb_node, l)
  55. static struct func_node *cfg_append_func(struct cfg *cfg, struct bpf_insn *insn)
  56. {
  57. struct func_node *new_func, *func;
  58. list_for_each_entry(func, &cfg->funcs, l) {
  59. if (func->start == insn)
  60. return func;
  61. else if (func->start > insn)
  62. break;
  63. }
  64. func = func_prev(func);
  65. new_func = calloc(1, sizeof(*new_func));
  66. if (!new_func) {
  67. p_err("OOM when allocating FUNC node");
  68. return NULL;
  69. }
  70. new_func->start = insn;
  71. new_func->idx = cfg->func_num;
  72. list_add(&new_func->l, &func->l);
  73. cfg->func_num++;
  74. return new_func;
  75. }
  76. static struct bb_node *func_append_bb(struct func_node *func,
  77. struct bpf_insn *insn)
  78. {
  79. struct bb_node *new_bb, *bb;
  80. list_for_each_entry(bb, &func->bbs, l) {
  81. if (bb->head == insn)
  82. return bb;
  83. else if (bb->head > insn)
  84. break;
  85. }
  86. bb = bb_prev(bb);
  87. new_bb = calloc(1, sizeof(*new_bb));
  88. if (!new_bb) {
  89. p_err("OOM when allocating BB node");
  90. return NULL;
  91. }
  92. new_bb->head = insn;
  93. INIT_LIST_HEAD(&new_bb->e_prevs);
  94. INIT_LIST_HEAD(&new_bb->e_succs);
  95. list_add(&new_bb->l, &bb->l);
  96. return new_bb;
  97. }
  98. static struct bb_node *func_insert_dummy_bb(struct list_head *after)
  99. {
  100. struct bb_node *bb;
  101. bb = calloc(1, sizeof(*bb));
  102. if (!bb) {
  103. p_err("OOM when allocating BB node");
  104. return NULL;
  105. }
  106. INIT_LIST_HEAD(&bb->e_prevs);
  107. INIT_LIST_HEAD(&bb->e_succs);
  108. list_add(&bb->l, after);
  109. return bb;
  110. }
  111. static bool cfg_partition_funcs(struct cfg *cfg, struct bpf_insn *cur,
  112. struct bpf_insn *end)
  113. {
  114. struct func_node *func, *last_func;
  115. func = cfg_append_func(cfg, cur);
  116. if (!func)
  117. return true;
  118. for (; cur < end; cur++) {
  119. if (cur->code != (BPF_JMP | BPF_CALL))
  120. continue;
  121. if (cur->src_reg != BPF_PSEUDO_CALL)
  122. continue;
  123. func = cfg_append_func(cfg, cur + cur->off + 1);
  124. if (!func)
  125. return true;
  126. }
  127. last_func = cfg_last_func(cfg);
  128. last_func->end = end - 1;
  129. func = cfg_first_func(cfg);
  130. list_for_each_entry_from(func, &last_func->l, l) {
  131. func->end = func_next(func)->start - 1;
  132. }
  133. return false;
  134. }
  135. static bool is_jmp_insn(__u8 code)
  136. {
  137. return BPF_CLASS(code) == BPF_JMP || BPF_CLASS(code) == BPF_JMP32;
  138. }
  139. static bool func_partition_bb_head(struct func_node *func)
  140. {
  141. struct bpf_insn *cur, *end;
  142. struct bb_node *bb;
  143. cur = func->start;
  144. end = func->end;
  145. INIT_LIST_HEAD(&func->bbs);
  146. bb = func_append_bb(func, cur);
  147. if (!bb)
  148. return true;
  149. for (; cur <= end; cur++) {
  150. if (is_jmp_insn(cur->code)) {
  151. __u8 opcode = BPF_OP(cur->code);
  152. if (opcode == BPF_EXIT || opcode == BPF_CALL)
  153. continue;
  154. bb = func_append_bb(func, cur + cur->off + 1);
  155. if (!bb)
  156. return true;
  157. if (opcode != BPF_JA) {
  158. bb = func_append_bb(func, cur + 1);
  159. if (!bb)
  160. return true;
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. static void func_partition_bb_tail(struct func_node *func)
  167. {
  168. unsigned int bb_idx = NUM_FIXED_BLOCKS;
  169. struct bb_node *bb, *last;
  170. last = func_last_bb(func);
  171. last->tail = func->end;
  172. bb = func_first_bb(func);
  173. list_for_each_entry_from(bb, &last->l, l) {
  174. bb->tail = bb_next(bb)->head - 1;
  175. bb->idx = bb_idx++;
  176. }
  177. last->idx = bb_idx++;
  178. func->bb_num = bb_idx;
  179. }
  180. static bool func_add_special_bb(struct func_node *func)
  181. {
  182. struct bb_node *bb;
  183. bb = func_insert_dummy_bb(&func->bbs);
  184. if (!bb)
  185. return true;
  186. bb->idx = ENTRY_BLOCK_INDEX;
  187. bb = func_insert_dummy_bb(&func_last_bb(func)->l);
  188. if (!bb)
  189. return true;
  190. bb->idx = EXIT_BLOCK_INDEX;
  191. return false;
  192. }
  193. static bool func_partition_bb(struct func_node *func)
  194. {
  195. if (func_partition_bb_head(func))
  196. return true;
  197. func_partition_bb_tail(func);
  198. return false;
  199. }
  200. static struct bb_node *func_search_bb_with_head(struct func_node *func,
  201. struct bpf_insn *insn)
  202. {
  203. struct bb_node *bb;
  204. list_for_each_entry(bb, &func->bbs, l) {
  205. if (bb->head == insn)
  206. return bb;
  207. }
  208. return NULL;
  209. }
  210. static struct edge_node *new_edge(struct bb_node *src, struct bb_node *dst,
  211. int flags)
  212. {
  213. struct edge_node *e;
  214. e = calloc(1, sizeof(*e));
  215. if (!e) {
  216. p_err("OOM when allocating edge node");
  217. return NULL;
  218. }
  219. if (src)
  220. e->src = src;
  221. if (dst)
  222. e->dst = dst;
  223. e->flags |= flags;
  224. return e;
  225. }
  226. static bool func_add_bb_edges(struct func_node *func)
  227. {
  228. struct bpf_insn *insn;
  229. struct edge_node *e;
  230. struct bb_node *bb;
  231. bb = entry_bb(func);
  232. e = new_edge(bb, bb_next(bb), EDGE_FLAG_FALLTHROUGH);
  233. if (!e)
  234. return true;
  235. list_add_tail(&e->l, &bb->e_succs);
  236. bb = exit_bb(func);
  237. e = new_edge(bb_prev(bb), bb, EDGE_FLAG_FALLTHROUGH);
  238. if (!e)
  239. return true;
  240. list_add_tail(&e->l, &bb->e_prevs);
  241. bb = entry_bb(func);
  242. bb = bb_next(bb);
  243. list_for_each_entry_from(bb, &exit_bb(func)->l, l) {
  244. e = new_edge(bb, NULL, EDGE_FLAG_EMPTY);
  245. if (!e)
  246. return true;
  247. e->src = bb;
  248. insn = bb->tail;
  249. if (!is_jmp_insn(insn->code) ||
  250. BPF_OP(insn->code) == BPF_EXIT) {
  251. e->dst = bb_next(bb);
  252. e->flags |= EDGE_FLAG_FALLTHROUGH;
  253. list_add_tail(&e->l, &bb->e_succs);
  254. continue;
  255. } else if (BPF_OP(insn->code) == BPF_JA) {
  256. e->dst = func_search_bb_with_head(func,
  257. insn + insn->off + 1);
  258. e->flags |= EDGE_FLAG_JUMP;
  259. list_add_tail(&e->l, &bb->e_succs);
  260. continue;
  261. }
  262. e->dst = bb_next(bb);
  263. e->flags |= EDGE_FLAG_FALLTHROUGH;
  264. list_add_tail(&e->l, &bb->e_succs);
  265. e = new_edge(bb, NULL, EDGE_FLAG_JUMP);
  266. if (!e)
  267. return true;
  268. e->src = bb;
  269. e->dst = func_search_bb_with_head(func, insn + insn->off + 1);
  270. list_add_tail(&e->l, &bb->e_succs);
  271. }
  272. return false;
  273. }
  274. static bool cfg_build(struct cfg *cfg, struct bpf_insn *insn, unsigned int len)
  275. {
  276. int cnt = len / sizeof(*insn);
  277. struct func_node *func;
  278. INIT_LIST_HEAD(&cfg->funcs);
  279. if (cfg_partition_funcs(cfg, insn, insn + cnt))
  280. return true;
  281. list_for_each_entry(func, &cfg->funcs, l) {
  282. if (func_partition_bb(func) || func_add_special_bb(func))
  283. return true;
  284. if (func_add_bb_edges(func))
  285. return true;
  286. }
  287. return false;
  288. }
  289. static void cfg_destroy(struct cfg *cfg)
  290. {
  291. struct func_node *func, *func2;
  292. list_for_each_entry_safe(func, func2, &cfg->funcs, l) {
  293. struct bb_node *bb, *bb2;
  294. list_for_each_entry_safe(bb, bb2, &func->bbs, l) {
  295. struct edge_node *e, *e2;
  296. list_for_each_entry_safe(e, e2, &bb->e_prevs, l) {
  297. list_del(&e->l);
  298. free(e);
  299. }
  300. list_for_each_entry_safe(e, e2, &bb->e_succs, l) {
  301. list_del(&e->l);
  302. free(e);
  303. }
  304. list_del(&bb->l);
  305. free(bb);
  306. }
  307. list_del(&func->l);
  308. free(func);
  309. }
  310. }
  311. static void draw_bb_node(struct func_node *func, struct bb_node *bb)
  312. {
  313. const char *shape;
  314. if (bb->idx == ENTRY_BLOCK_INDEX || bb->idx == EXIT_BLOCK_INDEX)
  315. shape = "Mdiamond";
  316. else
  317. shape = "record";
  318. printf("\tfn_%d_bb_%d [shape=%s,style=filled,label=\"",
  319. func->idx, bb->idx, shape);
  320. if (bb->idx == ENTRY_BLOCK_INDEX) {
  321. printf("ENTRY");
  322. } else if (bb->idx == EXIT_BLOCK_INDEX) {
  323. printf("EXIT");
  324. } else {
  325. unsigned int start_idx;
  326. struct dump_data dd = {};
  327. printf("{");
  328. kernel_syms_load(&dd);
  329. start_idx = bb->head - func->start;
  330. dump_xlated_for_graph(&dd, bb->head, bb->tail, start_idx);
  331. kernel_syms_destroy(&dd);
  332. printf("}");
  333. }
  334. printf("\"];\n\n");
  335. }
  336. static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
  337. {
  338. const char *style = "\"solid,bold\"";
  339. const char *color = "black";
  340. int func_idx = func->idx;
  341. struct edge_node *e;
  342. int weight = 10;
  343. if (list_empty(&bb->e_succs))
  344. return;
  345. list_for_each_entry(e, &bb->e_succs, l) {
  346. printf("\tfn_%d_bb_%d:s -> fn_%d_bb_%d:n [style=%s, color=%s, weight=%d, constraint=true",
  347. func_idx, e->src->idx, func_idx, e->dst->idx,
  348. style, color, weight);
  349. printf("];\n");
  350. }
  351. }
  352. static void func_output_bb_def(struct func_node *func)
  353. {
  354. struct bb_node *bb;
  355. list_for_each_entry(bb, &func->bbs, l) {
  356. draw_bb_node(func, bb);
  357. }
  358. }
  359. static void func_output_edges(struct func_node *func)
  360. {
  361. int func_idx = func->idx;
  362. struct bb_node *bb;
  363. list_for_each_entry(bb, &func->bbs, l) {
  364. draw_bb_succ_edges(func, bb);
  365. }
  366. /* Add an invisible edge from ENTRY to EXIT, this is to
  367. * improve the graph layout.
  368. */
  369. printf("\tfn_%d_bb_%d:s -> fn_%d_bb_%d:n [style=\"invis\", constraint=true];\n",
  370. func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
  371. }
  372. static void cfg_dump(struct cfg *cfg)
  373. {
  374. struct func_node *func;
  375. printf("digraph \"DOT graph for eBPF program\" {\n");
  376. list_for_each_entry(func, &cfg->funcs, l) {
  377. printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
  378. func->idx, func->idx);
  379. func_output_bb_def(func);
  380. func_output_edges(func);
  381. printf("}\n");
  382. }
  383. printf("}\n");
  384. }
  385. void dump_xlated_cfg(void *buf, unsigned int len)
  386. {
  387. struct bpf_insn *insn = buf;
  388. struct cfg cfg;
  389. memset(&cfg, 0, sizeof(cfg));
  390. if (cfg_build(&cfg, insn, len))
  391. return;
  392. cfg_dump(&cfg);
  393. cfg_destroy(&cfg);
  394. }