| 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 |
|
| 61 |
Filter string |
| 62 |
|
| 63 |
|
| 64 |
FilterName string |
| 65 |
|
| 66 |
|
| 67 |
ProgramName string |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
Offset uint16 |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
func ExampleToC() { |
| 77 |
|
| 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 |
|
| 88 |
|
| 89 |
_ = elf |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
func buildC(filter []bpf.Instruction, programName string, opts COpts) ([]byte, error) { |
| 97 |
|
| 98 |
ebpfFilter, err := ToC(filter, opts) |
| 99 |
if err != nil { |
| 100 |
return nil, errors.Wrap(err, "converting filter to C") |
| 101 |
} |
| 102 |
|
| 103 |
|
| 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 |
|
| 116 |
clangBin, ok := os.LookupEnv("CLANG") |
| 117 |
if !ok { |
| 118 |
clangBin = "/usr/bin/clang" |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
elf, err := clang.Compile(c.Bytes(), entryPoint, clang.Opts{ |
| 123 |
Clang: clangBin, |
| 124 |
EmitDebug: true, |
| 125 |
}) |
| 126 |
if err != nil { |
| 127 |
return nil, errors.Wrap(err, "compiling C") |
| 128 |
} |
| 129 |
|
| 130 |
return elf, nil |
| 131 |
} |