Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .vscode/quick-snips.code-snippets
Original file line number Diff line number Diff line change
@@ -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}|}\","
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
{
"language": "c",
"path": "./snippets/progs-network.code-snippets"
},
{
"language": "c",
"path": "./snippets/misc.code-snippets"
}
]
},
Expand Down
27 changes: 22 additions & 5 deletions snippets/maps.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,",
Expand Down
18 changes: 18 additions & 0 deletions snippets/misc.code-snippets
Original file line number Diff line number Diff line change
@@ -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;"
]
},
}
13 changes: 7 additions & 6 deletions snippets/progs-cgroup.code-snippets
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
{
"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; }"
]
},
"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; }"
]
},
"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; }"
]
Expand All @@ -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; }"
]
Expand Down
16 changes: 8 additions & 8 deletions snippets/progs-network.code-snippets
Original file line number Diff line number Diff line change
@@ -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; }"
]
Expand Down
10 changes: 5 additions & 5 deletions snippets/progs-tracing.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
"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; }"
]
},
"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",
]
}
}