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
15 changes: 7 additions & 8 deletions c.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"text/template"

"github.com/pkg/errors"
"golang.org/x/net/bpf"
)

Expand Down Expand Up @@ -103,7 +102,7 @@ type COpts struct {
// non 0 if the packet does match.
func ToC(filter []bpf.Instruction, opts COpts) (string, error) {
if !funcNameRegex.MatchString(opts.FunctionName) {
return "", errors.Errorf("invalid FunctionName %q", opts.FunctionName)
return "", fmt.Errorf("invalid FunctionName %q", opts.FunctionName)
}

blocks, err := compile(filter, compileOpts{
Expand All @@ -130,13 +129,13 @@ func ToC(filter []bpf.Instruction, opts COpts) (string, error) {
// Fill in the template
tmpl, err := template.New("cbfp_func").Parse(funcTemplate)
if err != nil {
return "", errors.Wrapf(err, "unable to parse func template")
return "", fmt.Errorf("unable to parse func template: %w", err)
}

c := strings.Builder{}

if err := tmpl.Execute(&c, fun); err != nil {
return "", errors.Wrapf(err, "unable to execute func template")
return "", fmt.Errorf("unable to execute func template: %w", err)
}

return c.String(), nil
Expand All @@ -151,7 +150,7 @@ func blockToC(blk *block) (cBlock, error) {
for _, insn := range blk.insns {
stat, err := insnToC(insn, blk)
if err != nil {
return cBlk, errors.Wrapf(err, "unable to compile %v", insn)
return cBlk, fmt.Errorf("unable to compile %v: %w", insn, err)
}

cBlk.Statements = append(cBlk.Statements, stat...)
Expand Down Expand Up @@ -180,7 +179,7 @@ func insnToC(insn instruction, blk *block) ([]string, error) {

case bpf.LoadExtension:
if i.Num != bpf.ExtLen {
return nil, errors.Errorf("unsupported BPF extension %v", i)
return nil, fmt.Errorf("unsupported BPF extension %v", i)
}

return stat("a = data_end - data;")
Expand Down Expand Up @@ -227,7 +226,7 @@ func insnToC(insn instruction, blk *block) ([]string, error) {
return stat("if (x == 0) return 0;")

default:
return nil, errors.Errorf("unsupported instruction %v", insn)
return nil, fmt.Errorf("unsupported instruction %v", insn)
}
}

Expand All @@ -243,7 +242,7 @@ func packetLoadToC(size int, offsetFmt string, offsetArgs ...interface{}) ([]str
return stat("a = ntohl(*((uint32_t *) (%s)));", offset)
}

return nil, errors.Errorf("unsupported load size %d", size)
return nil, fmt.Errorf("unsupported load size %d", size)
}

func condToC(skipTrue, skipFalse skip, blk *block, condFmt string, condArgs ...interface{}) ([]string, error) {
Expand Down
8 changes: 4 additions & 4 deletions c_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cbpfc

import (
"bytes"
"fmt"
"os"
"text/template"

"github.com/cloudflare/cbpfc/clang"

"github.com/pkg/errors"
"golang.org/x/net/bpf"
)

Expand Down Expand Up @@ -97,7 +97,7 @@ func buildC(filter []bpf.Instruction, programName string, opts COpts) ([]byte, e
// convert filter to C
ebpfFilter, err := ToC(filter, opts)
if err != nil {
return nil, errors.Wrap(err, "converting filter to C")
return nil, fmt.Errorf("converting filter to C: %w", err)
}

// embed filter in C template
Expand All @@ -109,7 +109,7 @@ func buildC(filter []bpf.Instruction, programName string, opts COpts) ([]byte, e
Offset: opts.PacketStartMaxOffset,
})
if err != nil {
return nil, errors.Wrap(err, "executing template with C filter")
return nil, fmt.Errorf("executing template with C filter: %w", err)
}

// lookup clang binary to use
Expand All @@ -124,7 +124,7 @@ func buildC(filter []bpf.Instruction, programName string, opts COpts) ([]byte, e
EmitDebug: true, // For BTF
})
if err != nil {
return nil, errors.Wrap(err, "compiling C")
return nil, fmt.Errorf("compiling C: %w", err)
}

return elf, nil
Expand Down
26 changes: 13 additions & 13 deletions cbpfc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
package cbpfc

import (
"errors"
"fmt"
"sort"

"github.com/pkg/errors"
"golang.org/x/net/bpf"
)

Expand Down Expand Up @@ -160,7 +160,7 @@ func (a packetGuardAbsolute) adjustInsns(insns []instruction) {}

// Assemble implements the Instruction Assemble method.
func (p packetGuardAbsolute) Assemble() (bpf.RawInstruction, error) {
return bpf.RawInstruction{}, errors.Errorf("unsupported")
return bpf.RawInstruction{}, fmt.Errorf("unsupported")
}

// packetGuardIndirect checks packet bounds for indirect packet loads (RegX + constant offset).
Expand Down Expand Up @@ -294,7 +294,7 @@ func (p packetGuardIndirect) adjustInsns(insns []instruction) {

// Assemble implements the Instruction Assemble method.
func (p packetGuardIndirect) Assemble() (bpf.RawInstruction, error) {
return bpf.RawInstruction{}, errors.Errorf("unsupported")
return bpf.RawInstruction{}, fmt.Errorf("unsupported")
}

// checksXNotZero is a "fake" instruction
Expand All @@ -304,7 +304,7 @@ type checkXNotZero struct {

// Assemble implements the Instruction Assemble method.
func (c checkXNotZero) Assemble() (bpf.RawInstruction, error) {
return bpf.RawInstruction{}, errors.Errorf("unsupported")
return bpf.RawInstruction{}, fmt.Errorf("unsupported")
}

type compileOpts struct {
Expand All @@ -329,7 +329,7 @@ func compile(insns []bpf.Instruction, opts compileOpts) ([]*block, error) {
// Split into blocks
blocks, err := splitBlocks(instructions)
if err != nil {
return nil, errors.Wrapf(err, "unable to compute blocks")
return nil, fmt.Errorf("unable to compute blocks: %w", err)
}

// Initialize registers
Expand Down Expand Up @@ -364,30 +364,30 @@ func validateInstructions(insns []bpf.Instruction) error {
// Assemble does some input validation
_, err := insn.Assemble()
if err != nil {
return errors.Errorf("can't assemble instruction %d: %v", pc, insn)
return fmt.Errorf("can't assemble instruction %d: %v", pc, insn)
}

switch i := insn.(type) {
case bpf.RawInstruction:
return errors.Errorf("unsupported instruction %d: %v", pc, insn)
return fmt.Errorf("unsupported instruction %d: %v", pc, insn)

// Negative constant offsets are used for extensions (and if they're supported, x/net/bpf will parse them)
// and other packet addressing modes we don't support: https://elixir.bootlin.com/linux/v5.14.10/source/kernel/bpf/core.c#L65
case bpf.LoadAbsolute:
if int32(i.Off) < 0 {
return errors.Errorf("LoadAbsolute negative offset %v", int32(i.Off))
return fmt.Errorf("LoadAbsolute negative offset %v", int32(i.Off))
}
case bpf.LoadMemShift:
if int32(i.Off) < 0 {
return errors.Errorf("LoadMemShift negative offset %v", int32(i.Off))
return fmt.Errorf("LoadMemShift negative offset %v", int32(i.Off))
}

case bpf.LoadExtension:
switch i.Num {
case bpf.ExtLen:
break
default:
return errors.Errorf("unsupported BPF extension %d: %v", pc, insn)
return fmt.Errorf("unsupported BPF extension %d: %v", pc, insn)
}
}
}
Expand Down Expand Up @@ -504,7 +504,7 @@ func splitBlocks(instructions []instruction) ([]*block, error) {
t := next.skipToPos(s)

if t >= pos(len(instructions)) {
return nil, errors.Errorf("instruction %v flows past last instruction", next.last())
return nil, fmt.Errorf("instruction %v flows past last instruction", next.last())
}

targets[t] = append(targets[t], next)
Expand Down Expand Up @@ -563,7 +563,7 @@ func addDivideByZeroGuards(blocks []*block) error {
switch i := insn.Instruction.(type) {
case bpf.ALUOpConstant:
if isDivision(i.Op) && i.Val == 0 {
return errors.Errorf("instruction %v divides by 0", insn)
return fmt.Errorf("instruction %v divides by 0", insn)
}
case bpf.ALUOpX:
if isDivision(i.Op) && !notZero {
Expand Down Expand Up @@ -947,7 +947,7 @@ func initializeMemory(blocks []*block) error {
// Check no uninitialized scratch registers are read
for scratch, uninit := range insnUninitialized.scratch {
if uninit {
return errors.Errorf("instruction %v reads potentially uninitialized scratch register M[%d]", insn, scratch)
return fmt.Errorf("instruction %v reads potentially uninitialized scratch register M[%d]", insn, scratch)
}
}

Expand Down
10 changes: 4 additions & 6 deletions clang/clang.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
)

// Opts configure how an XDP program is compiled / built
Expand Down Expand Up @@ -56,7 +54,7 @@ func (o Opts) cmd(inputFile string, outputFile string) (*exec.Cmd, error) {
// debug build script will be in a different directory, relative imports won't work
absInclude, err := filepath.Abs(include)
if err != nil {
return nil, errors.Wrapf(err, "can't get absolute path to include %s", include)
return nil, fmt.Errorf("can't get absolute path to include %s: %w", include, err)
}

flags = append(flags, "-I", absInclude)
Expand Down Expand Up @@ -99,7 +97,7 @@ func CompileRes(source []byte, name string, opts Opts) (Res, error) {
cmdline := cmd.Path + " " + strings.Join(cmd.Args, " ") + "\n"
err := os.WriteFile(filepath.Join(opts.Output, "build"), []byte(cmdline), 0644)
if err != nil {
return Res{}, errors.Wrap(err, "can't write build cmdline")
return Res{}, fmt.Errorf("can't write build cmdline: %w", err)
}
} else {
cmd.Stdin = bytes.NewReader(source)
Expand All @@ -113,9 +111,9 @@ func compileRes(cmd *exec.Cmd, output func(stdout []byte) ([]byte, error)) (Res,
if err != nil {
switch e := err.(type) {
case *exec.ExitError:
return Res{}, errors.Wrapf(e, "unable to compile C:\n%s", string(e.Stderr))
return Res{}, fmt.Errorf("unable to compile C:\n%s: %w", string(e.Stderr), e)
default:
return Res{}, errors.Wrapf(e, "unable to compile C")
return Res{}, fmt.Errorf("unable to compile C: %w", e)
}
}
elf, err := output(stdout)
Expand Down
13 changes: 6 additions & 7 deletions ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math"

"github.com/cilium/ebpf/asm"
"github.com/pkg/errors"
"golang.org/x/net/bpf"
)

Expand Down Expand Up @@ -134,7 +133,7 @@ func ToEBPF(filter []bpf.Instruction, opts EBPFOpts) (asm.Instructions, error) {
}

if eOpts.StackOffset&1 == 1 {
return nil, errors.Errorf("unaligned stack offset")
return nil, fmt.Errorf("unaligned stack offset")
}

eInsns := asm.Instructions{}
Expand All @@ -143,7 +142,7 @@ func ToEBPF(filter []bpf.Instruction, opts EBPFOpts) (asm.Instructions, error) {
for i, insn := range block.insns {
eInsn, err := insnToEBPF(insn, block, eOpts)
if err != nil {
return nil, errors.Wrapf(err, "unable to compile %v", insn)
return nil, fmt.Errorf("unable to compile %v: %w", insn, err)
}

// First insn of the block, add symbol so it can be referenced in jumps
Expand Down Expand Up @@ -176,7 +175,7 @@ func registersUnique(regs ...asm.Register) error {
}

if _, ok := seen[reg]; ok {
return errors.Errorf("register %v used twice", reg)
return fmt.Errorf("register %v used twice", reg)
}
seen[reg] = struct{}{}
}
Expand All @@ -187,7 +186,7 @@ func registersUnique(regs ...asm.Register) error {
// registerValid ensures that a register is a valid ebpf register
func registerValid(reg asm.Register) error {
if reg > asm.R9 {
return errors.Errorf("invalid register %v", reg)
return fmt.Errorf("invalid register %v", reg)
}

return nil
Expand Down Expand Up @@ -231,7 +230,7 @@ func insnToEBPF(insn instruction, blk *block, opts ebpfOpts) (asm.Instructions,

case bpf.LoadExtension:
if i.Num != bpf.ExtLen {
return nil, errors.Errorf("unsupported BPF extension %v", i)
return nil, fmt.Errorf("unsupported BPF extension %v", i)
}

return ebpfInsn(
Expand Down Expand Up @@ -312,7 +311,7 @@ func insnToEBPF(insn instruction, blk *block, opts ebpfOpts) (asm.Instructions,
return ebpfInsn(asm.JEq.Imm(opts.regX, 0, opts.label(noMatchLabel)))

default:
return nil, errors.Errorf("unsupported instruction %v", insn)
return nil, fmt.Errorf("unsupported instruction %v", insn)
}

}
Expand Down
5 changes: 3 additions & 2 deletions ebpf_example_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cbpfc

import (
"fmt"

"github.com/cilium/ebpf/asm"
"github.com/pkg/errors"
"golang.org/x/net/bpf"
)

Expand Down Expand Up @@ -41,7 +42,7 @@ func buildEBPF(filter []bpf.Instruction, offset uint16) (asm.Instructions, error
LabelPrefix: "filter",
})
if err != nil {
return nil, errors.Wrap(err, "converting filter to eBPF")
return nil, fmt.Errorf("converting filter to eBPF: %w", err)
}

prog := asm.Instructions{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module github.com/cloudflare/cbpfc

require (
github.com/cilium/ebpf v0.22.0
github.com/pkg/errors v0.9.1
golang.org/x/net v0.57.0
golang.org/x/sys v0.47.0
)

require golang.org/x/sys v0.47.0 // indirect

go 1.25.0
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE=
Expand Down
Loading
Loading