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
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ jobs:
VSIX_FILE=$(ls *.vsix | head -n 1)
echo "Uploading $VSIX_FILE to release ${{ github.event.release.tag_name }}"
gh release upload "${{ github.event.release.tag_name }}" "$VSIX_FILE" --clobber

- name: Publish to VSCode Marketplace
env:
VSCE_PAT: ${{ secrets.VSCE_PAT }}
run: |
vsce publish -p "$VSCE_PAT"
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# eBPF Snippets

Code-snippets for developing eBPF programs
Code snippets for developing **eBPF** programs.

![alt text](./media/usage.gif)
<p align="left">
<img src="./media/usage.gif" alt="Usage Demo" width="70%">
</p>

---

## Installation

1. Go to the latest [release](https://github.com/h0x0er/ebpf-snippets/releases) and download the `.vsix` file.

1. GOTO latest [release](https://github.com/h0x0er/ebpf-snippets/releases) and download the `vsix` file.
<p align="center">
<img src="./media/release.png" alt="Release Page" >
</p>

![alt text](./media/release.png)
1. In VS Code:
1. Press `Ctrl + Shift + P`
2. Type **Install from VSIX** and press **Enter**
3. Select the downloaded `.vsix` file

2. In vscode, hit `ctrl+shift+p`,
1. type `install from vsix` and hit-enter, then
2. choose the downloaded `vsix file`
<p align="center">
<img src="./media/install-vscode.png" alt="Install in VSCode" width="70%">
</p>

![alt text](./media/install-vscode.png)
**Enjoy 🙂**

**Enjoy :)**
---

## References


### References

- https://docs.ebpf.io/
- https://docs.kernel.org/bpf/libbpf/program_types.html

- [eBPF Documentation](https://docs.ebpf.io/)
- [Linux Kernel BPF Program Types](https://docs.kernel.org/bpf/libbpf/program_types.html)
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"engines": {
"vscode": "^1.0.0"
},
"publisher": "h0x0er",
"publisher": "jatin-experiment-01",
"categories": [
"Snippets"
],
Expand All @@ -31,6 +31,10 @@
{
"language": "c",
"path": "./snippets/progs-tracing.code-snippets"
},
{
"language": "c",
"path": "./snippets/progs-network.code-snippets"
}
]
},
Expand Down
18 changes: 10 additions & 8 deletions snippets/maps.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
"prefix": "bpf-map-generic",
"body": [
"struct {",
"\t__uint(type, ${1|BPF_MAP_TYPE_HASH,BPF_MAP_TYPE_PERCPU_ARRAY,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|});",
"\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__type(value, ${3:u8});",
"\t__uint(max_entries, ${4:100});",
"\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__array(values, struct my_value); // remove if not needed",
"\t__uint(map_flags, ${5|BPF_F_NO_PREALLOC,BPF_F_NUMA_NODE,BPF_F_RDONLY,BPF_F_RDONLY_PROG,BPF_F_WRONLY_PROG|}); // 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\");"
]
},
Expand All @@ -39,10 +41,10 @@
"body": [
"struct {",
"\t__uint(type, ${1|BPF_MAP_TYPE_ARRAY_OF_MAPS,BPF_MAP_TYPE_HASH_OF_MAPS|});",
"\t__uint(max_entries, ${4:100});",
"\t__type(key, ${2:u64});",
"\t__array(values, struct my_value);",
"} ${5:my_map_name} SEC(\".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\");"
]
},
"create bpf prog_array map": {
Expand Down
9 changes: 9 additions & 0 deletions snippets/progs-cgroup.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@
"int handle_sysctl(struct bpf_sysctl *ctx){ return 1; }"
]
},
"create cgroup sock-ops prog": {
"scope": "c",
"description": "",
"prefix": "bpf-prog-cgroup-sock-ops",
"body": [
"SEC(\"sockops\")",
"int handle_sockops(struct bpf_sock_ops *ctx){ return 1; }"
]
},
}
74 changes: 74 additions & 0 deletions snippets/progs-network.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"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": [
"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": [
"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": [
"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": [
"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": [
"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": [
"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": [
"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": [
"SEC(\"netfilter\")",
"int do_netfilter(struct bpf_nf_ctx *ctx){ return NF_ACCEPT; }"
]
},
}