Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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