Debian Codegraph @

c_example_test.go

raw symbols SCIP
1 package cbpfc
2
3 import (
4 "bytes"
5 "os"
6 "text/template"
7
8 "github.com/cloudflare/cbpfc/clang"
9
10 "github.com/pkg/errors"
11 "golang.org/x/net/bpf"
12 )
13
14 var testTemplate = template.Must(template.New(entryPoint).Parse(`
15 #define __section(NAME) __attribute__((section(NAME), used))
16
17 char __license[] __section("license") = "BSD";
18
19 // Shim out all the definitions required by cbpfc
20 // Real programs should use the proper headers
21 typedef unsigned long long uint64_t;
22 typedef long long int64_t;
23 typedef unsigned int uint32_t;
24 typedef int int32_t;
25 typedef unsigned short uint16_t;
26 typedef unsigned char uint8_t;
27
28 typedef char bool;
29 #define false 0
30 #define true 1
31
32 #define ntohs __builtin_bswap16
33 #define ntohl __builtin_bswap32
34
35 struct xdp_md {
36 uint32_t data;
37 uint32_t data_end;
38 };
39
40 enum xdp_action {
41 XDP_DROP = 1,
42 XDP_PASS,
43 };
44
45 {{.Filter}}
46
47 __section("xdp") int {{.ProgramName}}(struct xdp_md *ctx) {
48 uint8_t *data = (uint8_t *)(long)ctx->data;
49 uint8_t const *data_end = (uint8_t *)(long)ctx->data_end;
50
51 if ({{.FilterName}}(data + {{.Offset}}, data_end)) {
52 return XDP_DROP;
53 }
54
55 return XDP_PASS;
56 }
57 `))
58
59 type testTemplateOpts struct {
60 // Definition of the filter
61 Filter string
62
63 // Function name of the filter
64 FilterName string
65
66 // Name of the eBPF program
67 ProgramName string
68
69 // Offset the packet start is advanced by before the filter is called.
70 // Must match the COpts.PacketStartMaxOffset the filter was compiled with.
71 Offset uint16
72 }
73
74 // ExampleToC demonstrates how to use ToC() to embed a cBPF filter
75 // in a C program, and compile it to eBPF.
76 func ExampleToC() {
77 // simple cBPF filter that matches all packets
78 filter := []bpf.Instruction{
79 bpf.RetConstant{Val: 1},
80 }
81
82 elf, err := buildC(filter, "example", COpts{FunctionName: "example_filter"})
83 if err != nil {
84 panic(err)
85 }
86
87 // ELF with a single eBPF program 'example'
88 // Can be loaded with cilium/ebpf or libbpf
89 _ = elf
90 }
91
92 // buildC compiles a cBPF filter to C, embeds it in a C template,
93 // and compiles the resulting C program to eBPF / XDP using clang.
94 // The XDP program XDP_DROP's incoming packets that match the filter.
95 // Returns the compiled ELF
96 func buildC(filter []bpf.Instruction, programName string, opts COpts) ([]byte, error) {
97 // convert filter to C
98 ebpfFilter, err := ToC(filter, opts)
99 if err != nil {
100 return nil, errors.Wrap(err, "converting filter to C")
101 }
102
103 // embed filter in C template
104 c := bytes.Buffer{}
105 err = testTemplate.Execute(&c, testTemplateOpts{
106 Filter: ebpfFilter,
107 FilterName: opts.FunctionName,
108 ProgramName: programName,
109 Offset: opts.PacketStartMaxOffset,
110 })
111 if err != nil {
112 return nil, errors.Wrap(err, "executing template with C filter")
113 }
114
115 // lookup clang binary to use
116 clangBin, ok := os.LookupEnv("CLANG")
117 if !ok {
118 clangBin = "/usr/bin/clang"
119 }
120
121 // compile C program
122 elf, err := clang.Compile(c.Bytes(), entryPoint, clang.Opts{
123 Clang: clangBin,
124 EmitDebug: true, // For BTF
125 })
126 if err != nil {
127 return nil, errors.Wrap(err, "compiling C")
128 }
129
130 return elf, nil
131 }