Skip to content

Commit 6362619

Browse files
committed
Merge branch 'add-expected-attach-type' (#91)
2 parents e5e17f5 + 3935b1e commit 6362619

4 files changed

Lines changed: 95 additions & 2 deletions

File tree

bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ union bpf_attr {
9292
__u32 prog_flags;
9393
char prog_name[BPF_OBJ_NAME_LEN];
9494
__u32 prog_ifindex; /* ifindex of netdev to prep for */
95+
/* For some prog types expected attach type must be known at
96+
* load time to verify attach type specific parts of prog
97+
* (context accesses, allowed helpers, etc).
98+
*/
99+
__u32 expected_attach_type;
95100
};
96101

97102
struct { /* anonymous struct used by BPF_OBJ_* commands */

ebpf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Program interface {
4949
GetLicense() string
5050
// Returns program type
5151
GetType() ProgramType
52+
// Returns expected attach type
53+
GetExpectedAttachType() AttachType
5254
}
5355

5456
// Map defines interface to interact with eBPF maps

program_base.go

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ package goebpf
1414
1515
// Load eBPF program into kernel
1616
static int ebpf_prog_load(const char *name, __u32 prog_type, const void *insns, __u32 insns_cnt,
17-
const char *license, __u32 kern_version, void *log_buf, size_t log_size)
17+
const char *license, __u32 kern_version, void *log_buf, size_t log_size,
18+
__u32 expected_attach_type)
1819
{
1920
union bpf_attr attr = {};
2021
@@ -29,6 +30,7 @@ static int ebpf_prog_load(const char *name, __u32 prog_type, const void *insns,
2930
attr.log_size = 0;
3031
attr.log_level = 0;
3132
attr.kern_version = kern_version;
33+
attr.expected_attach_type = expected_attach_type;
3234
// program name
3335
strncpy((char*)&attr.prog_name, name, BPF_OBJ_NAME_LEN - 1);
3436
@@ -76,6 +78,69 @@ const (
7678
ProgramTypeSockOps
7779
)
7880

81+
// AttachType is eBPF program attach type
82+
type AttachType int
83+
84+
// Must be in sync with enum bpf_attach_type from <linux/bpf.h>
85+
const (
86+
AttachTypeCgroupInetIngress AttachType = iota
87+
AttachTypeCgroupInetEgress
88+
AttachTypeCgroupInetSockCreate
89+
AttachTypeCgroupSockOps
90+
AttachTypeSkSkbStreamParser
91+
AttachTypeSkSkbStreamVerdict
92+
AttachTypeCgroupDevice
93+
AttachTypeSkMsgVerdict
94+
AttachTypeCgroupInet4Bind
95+
AttachTypeCgroupInet6Bind
96+
AttachTypeCgroupInet4Connect
97+
AttachTypeCgroupInet6Connect
98+
AttachTypeCgroupInet4PostBind
99+
AttachTypeCgroupInet6PostBind
100+
AttachTypeCgroupUdp4Sendmsg
101+
AttachTypeCgroupUdp6Sendmsg
102+
AttachTypeLircMode2
103+
AttachTypeFlowDissector
104+
AttachTypeCgroupSysctl
105+
AttachTypeCgroupUdp4Recvmsg
106+
AttachTypeCgroupUdp6Recvmsg
107+
AttachTypeCgroupGetsockopt
108+
AttachTypeCgroupSetsockopt
109+
AttachTypeTraceRawTp
110+
AttachTypeTraceFentry
111+
AttachTypeTraceFexit
112+
AttachTypeModifyReturn
113+
AttachTypeLsmMac
114+
AttachTypeTraceIter
115+
AttachTypeCgroupInet4Getpeername
116+
AttachTypeCgroupInet6Getpeername
117+
AttachTypeCgroupInet4Getsockname
118+
AttachTypeCgroupInet6Getsockname
119+
AttachTypeXdpDevmap
120+
AttachTypeCgroupInetSockRelease
121+
AttachTypeXdpCpumap
122+
AttachTypeSkLookup
123+
AttachTypeXdp
124+
AttachTypeSkSkbVerdict
125+
AttachTypeSkReuseportSelect
126+
AttachTypeSkReuseportSelectOrMigrate
127+
AttachTypePerfEvent
128+
AttachTypeTraceKprobeMulti
129+
AttachTypeLsmCgroup
130+
AttachTypeStructOps
131+
AttachTypeNetfilter
132+
AttachTypeTcxIngress
133+
AttachTypeTcxEgress
134+
AttachTypeTraceUprobeMulti
135+
AttachTypeCgroupUnixConnect
136+
AttachTypeCgroupUnixSendmsg
137+
AttachTypeCgroupUnixRecvmsg
138+
AttachTypeCgroupUnixGetpeername
139+
AttachTypeCgroupUnixGetsockname
140+
AttachTypeNetkitPrimary
141+
AttachTypeNetkitPeer
142+
)
143+
79144
func (t ProgramType) String() string {
80145
switch t {
81146
case ProgramTypeSocketFilter:
@@ -118,6 +183,7 @@ type BaseProgram struct {
118183
license string // License
119184
bytecode []byte // eBPF instructions (each instruction - 8 bytes)
120185
kernelVersion int // Kernel requires version to match running for "kprobe" programs
186+
attachType AttachType
121187
}
122188

123189
// Load loads program into linux kernel
@@ -144,8 +210,22 @@ func (prog *BaseProgram) Load() error {
144210
license,
145211
C.__u32(prog.kernelVersion),
146212
unsafe.Pointer(&logBuf[0]),
147-
C.size_t(unsafe.Sizeof(logBuf))))
213+
C.size_t(unsafe.Sizeof(logBuf)),
214+
C.__u32(prog.attachType)))
148215

216+
if res == -1 && int(prog.attachType) != 0 {
217+
/* retry with expected_attach_type=0 in case kernel doesn't support it */
218+
res = int(C.ebpf_prog_load(
219+
name,
220+
C.__u32(prog.GetType()),
221+
unsafe.Pointer(&prog.bytecode[0]),
222+
C.__u32(prog.GetSize())/bpfInstructionLen,
223+
license,
224+
C.__u32(prog.kernelVersion),
225+
unsafe.Pointer(&logBuf[0]),
226+
C.size_t(unsafe.Sizeof(logBuf)),
227+
C.__u32(0)))
228+
}
149229
if res == -1 {
150230
return fmt.Errorf("ebpf_prog_load() failed: %s",
151231
NullTerminatedStringToString(logBuf[:]))
@@ -205,3 +285,8 @@ func (prog *BaseProgram) GetSize() int {
205285
func (prog *BaseProgram) GetLicense() string {
206286
return prog.license
207287
}
288+
289+
// GetAttachType returns program's expected attach type
290+
func (prog *BaseProgram) GetExpectedAttachType() AttachType {
291+
return prog.attachType
292+
}

program_xdp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type xdpProgram struct {
7979

8080
func newXdpProgram(bp BaseProgram) Program {
8181
bp.programType = ProgramTypeXdp
82+
bp.attachType = AttachTypeXdp
8283
return &xdpProgram{
8384
BaseProgram: bp,
8485
}

0 commit comments

Comments
 (0)