@@ -14,7 +14,8 @@ package goebpf
1414
1515// Load eBPF program into kernel
1616static 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+
79144func (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 {
205285func (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+ }
0 commit comments