Skip to content

Commit f3e2cfe

Browse files
committed
internal/sys: add DEFINEd constants
Some constants in bpf.h are defined using the C preprocessor and are therefore not available in BTF. Add them manually. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent e547e58 commit f3e2cfe

File tree

8 files changed

+36
-29
lines changed

8 files changed

+36
-29
lines changed

asm/instruction.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strings"
1313

1414
"github.com/cilium/ebpf/internal/sys"
15-
"github.com/cilium/ebpf/internal/unix"
1615
)
1716

1817
// InstructionSize is the size of a BPF instruction in bytes
@@ -804,7 +803,7 @@ func (insns Instructions) Tag(bo binary.ByteOrder) (string, error) {
804803
return "", fmt.Errorf("instruction %d: %w", i, err)
805804
}
806805
}
807-
return hex.EncodeToString(h.Sum(nil)[:unix.BPF_TAG_SIZE]), nil
806+
return hex.EncodeToString(h.Sum(nil)[:sys.BPF_TAG_SIZE]), nil
808807
}
809808

810809
// encodeFunctionReferences populates the Offset (or Constant, depending on

elf_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,10 @@ func getProgType(sectionName string) (ProgramType, AttachType, uint32, string) {
12661266

12671267
var flags uint32
12681268
if t.flags&_SEC_SLEEPABLE > 0 {
1269-
flags |= unix.BPF_F_SLEEPABLE
1269+
flags |= sys.BPF_F_SLEEPABLE
12701270
}
12711271
if t.flags&_SEC_XDP_FRAGS > 0 {
1272-
flags |= unix.BPF_F_XDP_HAS_FRAGS
1272+
flags |= sys.BPF_F_XDP_HAS_FRAGS
12731273
}
12741274
if t.flags&_SEC_EXP_ATTACH_OPT > 0 {
12751275
if programType == XDP {

elf_reader_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/cilium/ebpf/internal"
1717
"github.com/cilium/ebpf/internal/sys"
1818
"github.com/cilium/ebpf/internal/testutils"
19-
"github.com/cilium/ebpf/internal/unix"
2019

2120
"github.com/google/go-cmp/cmp"
2221
"github.com/google/go-cmp/cmp/cmpopts"
@@ -1188,22 +1187,22 @@ func TestELFSectionProgramTypes(t *testing.T) {
11881187
{"fentry/", Tracing, AttachTraceFEntry, 0, ""},
11891188
{"fmod_ret/", Tracing, AttachModifyReturn, 0, ""},
11901189
{"fexit/", Tracing, AttachTraceFExit, 0, ""},
1191-
{"fentry.s/", Tracing, AttachTraceFEntry, unix.BPF_F_SLEEPABLE, ""},
1192-
{"fmod_ret.s/", Tracing, AttachModifyReturn, unix.BPF_F_SLEEPABLE, ""},
1193-
{"fexit.s/", Tracing, AttachTraceFExit, unix.BPF_F_SLEEPABLE, ""},
1190+
{"fentry.s/", Tracing, AttachTraceFEntry, sys.BPF_F_SLEEPABLE, ""},
1191+
{"fmod_ret.s/", Tracing, AttachModifyReturn, sys.BPF_F_SLEEPABLE, ""},
1192+
{"fexit.s/", Tracing, AttachTraceFExit, sys.BPF_F_SLEEPABLE, ""},
11941193
{"freplace/", Extension, AttachNone, 0, ""},
11951194
{"lsm/foo", LSM, AttachLSMMac, 0, "foo"},
1196-
{"lsm.s/foo", LSM, AttachLSMMac, unix.BPF_F_SLEEPABLE, "foo"},
1195+
{"lsm.s/foo", LSM, AttachLSMMac, sys.BPF_F_SLEEPABLE, "foo"},
11971196
{"iter/bpf_map", Tracing, AttachTraceIter, 0, "bpf_map"},
1198-
{"iter.s/", Tracing, AttachTraceIter, unix.BPF_F_SLEEPABLE, ""},
1197+
{"iter.s/", Tracing, AttachTraceIter, sys.BPF_F_SLEEPABLE, ""},
11991198
// Was missing sleepable.
1200-
{"syscall", Syscall, AttachNone, unix.BPF_F_SLEEPABLE, ""},
1201-
{"xdp.frags_devmap/foo", XDP, AttachXDPDevMap, unix.BPF_F_XDP_HAS_FRAGS, "foo"},
1199+
{"syscall", Syscall, AttachNone, sys.BPF_F_SLEEPABLE, ""},
1200+
{"xdp.frags_devmap/foo", XDP, AttachXDPDevMap, sys.BPF_F_XDP_HAS_FRAGS, "foo"},
12021201
{"xdp_devmap/foo", XDP, AttachXDPDevMap, 0, "foo"},
1203-
{"xdp.frags_cpumap/", XDP, AttachXDPCPUMap, unix.BPF_F_XDP_HAS_FRAGS, ""},
1202+
{"xdp.frags_cpumap/", XDP, AttachXDPCPUMap, sys.BPF_F_XDP_HAS_FRAGS, ""},
12041203
{"xdp_cpumap/", XDP, AttachXDPCPUMap, 0, ""},
12051204
// Used incorrect attach type.
1206-
{"xdp.frags/foo", XDP, AttachXDP, unix.BPF_F_XDP_HAS_FRAGS, ""},
1205+
{"xdp.frags/foo", XDP, AttachXDP, sys.BPF_F_XDP_HAS_FRAGS, ""},
12071206
{"xdp/foo", XDP, AttachNone, 0, ""},
12081207
{"perf_event", PerfEvent, AttachNone, 0, ""},
12091208
{"lwt_in", LWTIn, AttachNone, 0, ""},

features/prog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ var haveProgramTypeMatrix = internal.FeatureMatrix[ebpf.ProgramType]{
185185
Fn: func() error {
186186
return probeProgram(&ebpf.ProgramSpec{
187187
Type: ebpf.Syscall,
188-
Flags: unix.BPF_F_SLEEPABLE,
188+
Flags: sys.BPF_F_SLEEPABLE,
189189
})
190190
},
191191
},
@@ -263,7 +263,7 @@ func haveProgramHelper(pt ebpf.ProgramType, helper asm.BuiltinFunc) error {
263263
case ebpf.SkLookup:
264264
spec.AttachType = ebpf.AttachSkLookup
265265
case ebpf.Syscall:
266-
spec.Flags = unix.BPF_F_SLEEPABLE
266+
spec.Flags = sys.BPF_F_SLEEPABLE
267267
}
268268

269269
prog, err := ebpf.NewProgramWithOptions(spec, ebpf.ProgramOptions{

info_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/cilium/ebpf/internal"
1616
"github.com/cilium/ebpf/internal/sys"
1717
"github.com/cilium/ebpf/internal/testutils"
18-
"github.com/cilium/ebpf/internal/unix"
1918
)
2019

2120
func TestMapInfoFromProc(t *testing.T) {
@@ -302,7 +301,7 @@ func BenchmarkStats(b *testing.B) {
302301
func testStats(prog *Program) error {
303302
in := internal.EmptyBPFContext
304303

305-
stats, err := EnableStats(uint32(unix.BPF_STATS_RUN_TIME))
304+
stats, err := EnableStats(uint32(sys.BPF_STATS_RUN_TIME))
306305
if err != nil {
307306
return fmt.Errorf("failed to enable stats: %v", err)
308307
}

internal/sys/syscall.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ func ObjInfo(fd *FD, info Info) error {
133133

134134
// BPFObjName is a null-terminated string made up of
135135
// 'A-Za-z0-9_' characters.
136-
type ObjName [unix.BPF_OBJ_NAME_LEN]byte
136+
type ObjName [BPF_OBJ_NAME_LEN]byte
137137

138138
// NewObjName truncates the result if it is too long.
139139
func NewObjName(name string) ObjName {
140140
var result ObjName
141-
copy(result[:unix.BPF_OBJ_NAME_LEN-1], name)
141+
copy(result[:BPF_OBJ_NAME_LEN-1], name)
142142
return result
143143
}
144144

@@ -169,6 +169,16 @@ const (
169169
BPF_F_LINK_MPROG = 1 << 13 // aka BPF_F_LINK
170170
)
171171

172+
// Flags used by BPF_PROG_LOAD.
173+
const (
174+
BPF_F_SLEEPABLE = 1 << 4
175+
BPF_F_XDP_HAS_FRAGS = 1 << 5
176+
BPF_F_XDP_DEV_BOUND_ONLY = 1 << 6
177+
)
178+
179+
const BPF_TAG_SIZE = 8
180+
const BPF_OBJ_NAME_LEN = 16
181+
172182
// wrappedErrno wraps syscall.Errno to prevent direct comparisons with
173183
// syscall.E* or unix.E* constants.
174184
//

internal/sys/syscall_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func TestObjName(t *testing.T) {
1414
if name[len(name)-1] != 0 {
1515
t.Error("NewBPFObjName doesn't null terminate")
1616
}
17-
if len(name) != unix.BPF_OBJ_NAME_LEN {
18-
t.Errorf("Name is %d instead of %d bytes long", len(name), unix.BPF_OBJ_NAME_LEN)
17+
if len(name) != BPF_OBJ_NAME_LEN {
18+
t.Errorf("Name is %d instead of %d bytes long", len(name), BPF_OBJ_NAME_LEN)
1919
}
2020
}
2121

ringbuf/reader_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"github.com/cilium/ebpf"
1414
"github.com/cilium/ebpf/asm"
1515
"github.com/cilium/ebpf/internal"
16+
"github.com/cilium/ebpf/internal/sys"
1617
"github.com/cilium/ebpf/internal/testutils"
1718
"github.com/cilium/ebpf/internal/testutils/fdtrace"
18-
"github.com/cilium/ebpf/internal/unix"
1919
)
2020

2121
type sampleMessage struct {
@@ -259,9 +259,9 @@ func TestReaderNoWakeup(t *testing.T) {
259259
testutils.SkipOnOldKernel(t, "5.8", "BPF ring buffer")
260260

261261
prog, events := mustOutputSamplesProg(t,
262-
sampleMessage{size: 5, flags: unix.BPF_RB_NO_WAKEUP}, // Read after timeout
263-
sampleMessage{size: 6, flags: unix.BPF_RB_NO_WAKEUP}, // Discard
264-
sampleMessage{size: 7, flags: unix.BPF_RB_NO_WAKEUP}) // Read won't block
262+
sampleMessage{size: 5, flags: sys.BPF_RB_NO_WAKEUP}, // Read after timeout
263+
sampleMessage{size: 6, flags: sys.BPF_RB_NO_WAKEUP}, // Discard
264+
sampleMessage{size: 7, flags: sys.BPF_RB_NO_WAKEUP}) // Read won't block
265265

266266
rd, err := NewReader(events)
267267
if err != nil {
@@ -308,9 +308,9 @@ func TestReaderFlushPendingEvents(t *testing.T) {
308308
testutils.SkipOnOldKernel(t, "5.8", "BPF ring buffer")
309309

310310
prog, events := mustOutputSamplesProg(t,
311-
sampleMessage{size: 5, flags: unix.BPF_RB_NO_WAKEUP}, // Read after Flush
312-
sampleMessage{size: 6, flags: unix.BPF_RB_NO_WAKEUP}, // Discard
313-
sampleMessage{size: 7, flags: unix.BPF_RB_NO_WAKEUP}) // Read won't block
311+
sampleMessage{size: 5, flags: sys.BPF_RB_NO_WAKEUP}, // Read after Flush
312+
sampleMessage{size: 6, flags: sys.BPF_RB_NO_WAKEUP}, // Discard
313+
sampleMessage{size: 7, flags: sys.BPF_RB_NO_WAKEUP}) // Read won't block
314314

315315
rd, err := NewReader(events)
316316
if err != nil {

0 commit comments

Comments
 (0)