diff --git a/.vscode/quick-snips.code-snippets b/.vscode/quick-snips.code-snippets new file mode 100644 index 0000000..0391a74 --- /dev/null +++ b/.vscode/quick-snips.code-snippets @@ -0,0 +1,23 @@ +{ + // Place your bpf-snippets workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and + // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope + // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is + // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: + // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. + // Placeholders with the same ids are connected. + // Example: + // "Print to console": { + // "scope": "javascript,typescript", + // "prefix": "log", + // "body": [ + // "console.log('$1');", + // "$2" + // ], + // "description": "Log output to console" + // } + "add doc link": { + "scope": "snippets", + "prefix": "snip-add-inline-docs", + "body": "\"\\${${1:idx}| ,// ${2|Refer,Usage|}: ${3:link}|}\"," + } +} \ No newline at end of file diff --git a/README.md b/README.md index 3292436..9f8a740 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ OR ![alt text](./media/release.png) 2. In vscode, hit `ctrl+shift+p`, - 1. type `install from vsixt-enter, then + 1. type `install from vsix` hit-enter, then 2. choose the downloaded `vsix file` ![alt text](./media/install-vscode.png) diff --git a/package.json b/package.json index 07b4d35..11a1298 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,10 @@ { "language": "c", "path": "./snippets/progs-network.code-snippets" + }, + { + "language": "c", + "path": "./snippets/misc.code-snippets" } ] }, diff --git a/snippets/maps.code-snippets b/snippets/maps.code-snippets index c9f2cbf..58282e7 100644 --- a/snippets/maps.code-snippets +++ b/snippets/maps.code-snippets @@ -8,12 +8,25 @@ "scope": "c", "prefix": "bpf-map-ringbuff", "body": [ + "${3| ,// Refer: https://docs.ebpf.io/linux/map-type/BPF_MAP_TYPE_RINGBUF/|}", "struct {", "\t__uint(type, BPF_MAP_TYPE_RINGBUF);", "\t__uint(max_entries, ${1:1 << 26});", "} ${2:events} SEC(\".maps\");" ] }, + "create perf-event-array map": { + "scope": "c", + "prefix": "bpf-map-perf-array", + "body": [ + "${4| ,// Refer: https://docs.ebpf.io/linux/map-type/BPF_MAP_TYPE_PERF_EVENT_ARRAY/|}", + "struct {", + "\t__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);", + "\t__type(key, ${1:int});", + "\t__type(value, ${2:struct event});", + "} ${3:map_name} SEC(\".maps\");" + ] + }, "add map_flags": { "scope": "c", "prefix": "bpf-map-add-flags", @@ -23,40 +36,44 @@ "scope": "c", "prefix": "bpf-map-generic", "body": [ + "${9| ,// Refer: https://docs.ebpf.io/linux/map-type/|}", "struct {", "\t__uint(type, ${1|BPF_MAP_TYPE_HASH,BPF_MAP_TYPE_PERCPU_HASH,BPF_MAP_TYPE_LPM_TRIE,BPF_MAP_TYPE_ARRAY,BPF_MAP_TYPE_PERCPU_ARRAY,BPF_MAP_TYPE_LRU_HASH,BPF_MAP_TYPE_LRU_PERCPU_HASH,BPF_MAP_TYPE_LPM_TRIE,BPF_MAP_TYPE_BLOOM_FILTER,BPF_MAP_TYPE_ARENA,BPF_MAP_TYPE_QUEUE,BPF_MAP_TYPE_STACK,BPF_MAP_TYPE_PERF_EVENT_ARRAY|});", "\t__type(key, ${2:u64});", - "\t__uint(key_size, ${3:sizeof(u64)}); // remove if not needed", "\t__type(value, ${4:u8});", - "\t__uint(value_size, ${5:sizeof(u64)}); // remove if not needed", "\t__uint(max_entries, ${6:100});", + "\t__uint(pinning, LIBBPF_PIN_BY_NAME) // remove if not needed", + "\t__uint(key_size, ${3:sizeof(u64)}); // remove if not needed", + "\t__uint(value_size, ${5:sizeof(u64)}); // remove if not needed", "\t__array(values, struct my_value); // remove if not needed", "\t__uint(map_flags, ${7|BPF_F_NO_PREALLOC,BPF_F_NUMA_NODE,BPF_F_RDONLY,BPF_F_RDONLY_PROG,BPF_F_WRONLY_PROG|}); // remove if not needed", - "} ${0:my_map_name} SEC(\".maps\");" + "} ${8:my_map_name} SEC(\".maps\");" ] }, "create generic map-in-map": { "scope": "c", "prefix": "bpf-map-in-map", "body": [ + "${6| ,// Refer: https://docs.ebpf.io/linux/map-type/BPF_MAP_TYPE_ARRAY_OF_MAPS/|}", "struct {", "\t__uint(type, ${1|BPF_MAP_TYPE_ARRAY_OF_MAPS,BPF_MAP_TYPE_HASH_OF_MAPS|});", "\t__uint(max_entries, ${2:100});", "\t__type(key, ${3:u64});", "\t__array(values, ${4:struct my_value});", - "} ${0:my_map_name} SEC(\".maps\");" + "} ${5:my_map_name} SEC(\".maps\");" ] }, "create bpf prog_array map": { "scope": "c", "prefix": "bpf-map-prog-array", "body": [ + "${4| ,// Refer: https://github.com/cilium/tetragon/blob/c51dd078bfb568075ba1fb287f2447f29f709073/bpf/process/bpf_generic_rawtp.c#L17-L45|}", "struct {", "\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);", "\t__type(key, __u32);", "\t__uint(max_entries, ${1:100});", "\t__array(values, ${2:int(struct pt_regs *)}); // func signature", - "} ${0:tail_programs} SEC(\".maps\") = {", + "} ${3:tail_programs} SEC(\".maps\") = {", "\t.values = {", "\t\t[0] = (void*)&some_func,", "\t\t[1] = (void*)&some_func2,", diff --git a/snippets/misc.code-snippets b/snippets/misc.code-snippets new file mode 100644 index 0000000..5bfba3a --- /dev/null +++ b/snippets/misc.code-snippets @@ -0,0 +1,18 @@ +{ + "create kernel config": { + "scope": "c", + "prefix": "bpf-misc-kconfig", + "body": [ + "${2| ,// Refer: https://docs.ebpf.io/ebpf-library/libbpf/ebpf/__kconfig/|}", + "extern unsigned ${1:CONFIG_HZ} __kconfig __weak;" + ] + }, + "create linux version variable": { + "scope": "c", + "prefix": "bpf-misc-kernel-version", + "body": [ + "${1| ,// Refer: https://docs.ebpf.io/ebpf-library/libbpf/ebpf/KERNEL_VERSION/|}", + "extern int LINUX_KERNEL_VERSION __kconfig;" + ] + }, +} \ No newline at end of file diff --git a/snippets/progs-cgroup.code-snippets b/snippets/progs-cgroup.code-snippets index cd0ce9f..71c323e 100644 --- a/snippets/progs-cgroup.code-snippets +++ b/snippets/progs-cgroup.code-snippets @@ -1,18 +1,18 @@ { "bpf-prog-cgroup-skb": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SKB/", "prefix": "bpf-prog-cgroup-skb", "body": [ + "${2| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SKB/|}", "SEC(\"cgroup_skb/${1|egress,ingress|}\")", "int handle_$1(struct __sk_buff *skb){ return SK_PASS; }" ] }, "bpf-prog-cgroup-sock": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SOCK/", "prefix": "bpf-prog-cgroup-sock", "body": [ + "${2| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SOCK/|}", "SEC(\"cgroup/${1|sock_create,sock_release,post_bind4,post_bind6|}\")", "int handle_$1(struct bpf_sock *ctx){ return 1; }" ] @@ -20,26 +20,26 @@ "bpf-prog-cgroup-dev": { "scope": "c", "prefix": "bpf-prog-cgroup-dev", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_DEVICE ", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_DEVICE|}", "SEC(\"cgroup/dev\")", "int handle_dev(struct bpf_cgroup_dev_ctx *ctx){ return 0; }" ] }, "bpf-prog-cgroup-sock-addr": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SOCK_ADDR/", "prefix": "bpf-prog-cgroup-sock-addr", "body": [ + "${2| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SOCK_ADDR/|}", "SEC(\"cgroup/${1|connect,connect6,bind4,bind6,sendmsg4,sendmsg6,recvmsg4,recvmsg6,getpeername4,getpeername6,getsockname4,getsockname6|}\")", "int handle_$1(struct bpf_sock_addr *ctx){ return 1; }" ] }, "bpf-prog-cgroup-sockopt": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SOCKOPT/", "prefix": "bpf-prog-cgroup-sockopt", "body": [ + "${2| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SOCKOPT/|}", "SEC(\"cgroup/${1|getsockopt,setsockopt|}\")", "int handle_$1(struct bpf_sockopt *ctx){ return 1; }" ] @@ -47,8 +47,8 @@ "bpf-prog-cgroup-sysctl": { "scope": "c", "prefix": "bpf-prog-cgroup-sysctl", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SYSCTL/", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_CGROUP_SYSCTL/|}", "SEC(\"cgroup/sysctl\")", "int handle_sysctl(struct bpf_sysctl *ctx){ return 1; }" ] @@ -58,6 +58,7 @@ "description": "", "prefix": "bpf-prog-cgroup-sock-ops", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SOCK_OPS/|}", "SEC(\"sockops\")", "int handle_sockops(struct bpf_sock_ops *ctx){ return 1; }" ] diff --git a/snippets/progs-network.code-snippets b/snippets/progs-network.code-snippets index b83468d..ef92bb9 100644 --- a/snippets/progs-network.code-snippets +++ b/snippets/progs-network.code-snippets @@ -1,72 +1,72 @@ { "create classifier prog (tc/tcx)": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SCHED_CLS/", "prefix": "bpf-prog-tc", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SCHED_CLS|}", "SEC(\"${1|tc/egress,tc/ingress,tcx/egress,tcx/ingress|}\")", "int handle_${1/[\\/]/_/}(struct __sk_buff *skb){ return -1; }" ] }, "create xdp prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_XDP/#usage", "prefix": "bpf-prog-xdp", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_XDP|}", "SEC(\"xdp\")", "int handle_xdp(struct xdp_md *ctx){ return XDP_PASS; }" ] }, "create sk_skb prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_SKB/", "prefix": "bpf-prog-sk-skb", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_SKB/|}", "SEC(\"sk_skb/${1|stream_parser,stream_verdict|}\")", "int handle_$1(struct __sk_buff *skb){ return 1; }" ] }, "create sk_msg prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_MSG/", "prefix": "bpf-prog-sk-msg", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_MSG/|}", "SEC(\"sk_msg\")", "int handle_skmsg(struct sk_msg_md *msg){ return SK_PASS; }" ] }, "create sk_lookup prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_LOOKUP/", "prefix": "bpf-prog-sk-lookup", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_LOOKUP/|}", "SEC(\"sk_lookup\")", "int handle_sklookup(struct bpf_sk_lookup *ctx){ return SK_PASS; }" ] }, "create sk_reuseport prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_REUSEPORT/#usage", "prefix": "bpf-prog-sk-reuseport", "body": [ + "${2| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SK_REUSEPORT|}", "SEC(\"${1|sk_reuseport,sk_reuseport/migrate|}\")", "int handle_sk_reuseport(struct sk_reuseport_md *ctx){ return SK_PASS; }" ] }, "create flow_dissector prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_FLOW_DISSECTOR/#context", "prefix": "bpf-prog-flow-dissector", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_FLOW_DISSECTOR/|}", "SEC(\"flow_dissector\")", "int handle_dissector(struct __sk_buff *skb){ return BPF_OK; }" ] }, "create netfliter prog": { "scope": "c", - "description": "Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_NETFILTER/", "prefix": "bpf-prog-netfilter", "body": [ + "${1| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_NETFILTER/|}", "SEC(\"netfilter\")", "int do_netfilter(struct bpf_nf_ctx *ctx){ return NF_ACCEPT; }" ] diff --git a/snippets/progs-tracing.code-snippets b/snippets/progs-tracing.code-snippets index 492ca15..5f491f8 100644 --- a/snippets/progs-tracing.code-snippets +++ b/snippets/progs-tracing.code-snippets @@ -2,8 +2,8 @@ "raw tracepoint": { "scope": "c", "prefix": "bpf-prog-raw-tracepoint", - "description": "https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_RAW_TRACEPOINT", "body": [ + "${2| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_RAW_TRACEPOINT|}", "SEC(\"raw_tp/${1:sched_switch}\")", "int BPF_PROG(handle_$1){ return 0; }" ] @@ -11,19 +11,19 @@ "fentry/fexit/fmodify programs": { "scope": "c", "prefix": "bpf-prog-new-tracing", - "description": "https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_TRACING/", "body": [ + "${3| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_TRACING/|}", "SEC(\"${1|fentry,fexit,fmod_ret,iter|}/${2:func_name}\")", - "int BPF_PROG(handle_$2){ return 0; }" + "int BPF_PROG2(handle_$2){ return 0; }" ] }, "kprobe/uprobe programs": { "scope": "c", "prefix": "bpf-prog-probe", - "description": "https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_KPROBE/", "body": [ + "${4| ,// Refer: https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_KPROBE/|}", "SEC(\"${1|kprobe,kretprobe,uprobe,uretprobe|}/${2:func_name}\")", - "int ${3|BPF_KPROBE,BPF_KSYSCALL,BPF_KRETPROBE|}(handle_$2, args...){ return 0; }" + "int ${3|BPF_KPROBE,BPF_UPROBE,BPF_KSYSCALL,BPF_KRETPROBE,BPF_URETPROBE,BPF_KPROBE_SYSCALL|}(handle_$2, args...){ return 0; }$0", ] } } \ No newline at end of file