| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/cilium/ebpf/internal/platform" |
| 5 |
"github.com/cilium/ebpf/internal/sys" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
type MapType uint32 |
| 13 |
|
| 14 |
|
| 15 |
const ( |
| 16 |
UnspecifiedMap MapType = MapType(platform.LinuxTag | iota) |
| 17 |
|
| 18 |
Hash |
| 19 |
|
| 20 |
Array |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
ProgramArray |
| 27 |
|
| 28 |
|
| 29 |
PerfEventArray |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
PerCPUHash |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
PerCPUArray |
| 40 |
|
| 41 |
|
| 42 |
StackTrace |
| 43 |
|
| 44 |
|
| 45 |
CGroupArray |
| 46 |
|
| 47 |
|
| 48 |
LRUHash |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
LRUCPUHash |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
LPMTrie |
| 59 |
|
| 60 |
|
| 61 |
ArrayOfMaps |
| 62 |
|
| 63 |
|
| 64 |
HashOfMaps |
| 65 |
|
| 66 |
DevMap |
| 67 |
|
| 68 |
SockMap |
| 69 |
|
| 70 |
CPUMap |
| 71 |
|
| 72 |
XSKMap |
| 73 |
|
| 74 |
SockHash |
| 75 |
|
| 76 |
CGroupStorage |
| 77 |
|
| 78 |
ReusePortSockArray |
| 79 |
|
| 80 |
PerCPUCGroupStorage |
| 81 |
|
| 82 |
Queue |
| 83 |
|
| 84 |
Stack |
| 85 |
|
| 86 |
SkStorage |
| 87 |
|
| 88 |
DevMapHash |
| 89 |
|
| 90 |
|
| 91 |
StructOpsMap |
| 92 |
|
| 93 |
RingBuf |
| 94 |
|
| 95 |
InodeStorage |
| 96 |
|
| 97 |
TaskStorage |
| 98 |
|
| 99 |
BloomFilter |
| 100 |
|
| 101 |
UserRingbuf |
| 102 |
|
| 103 |
CgroupStorage |
| 104 |
|
| 105 |
Arena |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
const ( |
| 110 |
WindowsHash MapType = MapType(platform.WindowsTag | iota + 1) |
| 111 |
WindowsArray |
| 112 |
WindowsProgramArray |
| 113 |
WindowsPerCPUHash |
| 114 |
WindowsPerCPUArray |
| 115 |
WindowsHashOfMaps |
| 116 |
WindowsArrayOfMaps |
| 117 |
WindowsLRUHash |
| 118 |
WindowsLPMTrie |
| 119 |
WindowsQueue |
| 120 |
WindowsLRUCPUHash |
| 121 |
WindowsStack |
| 122 |
WindowsRingBuf |
| 123 |
) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func MapTypeForPlatform(plat string, typ uint32) (MapType, error) { |
| 129 |
return platform.EncodeConstant[MapType](plat, typ) |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func (mt MapType) hasPerCPUValue() bool { |
| 134 |
switch mt { |
| 135 |
case PerCPUHash, PerCPUArray, LRUCPUHash, PerCPUCGroupStorage: |
| 136 |
return true |
| 137 |
case WindowsPerCPUHash, WindowsPerCPUArray, WindowsLRUCPUHash: |
| 138 |
return true |
| 139 |
default: |
| 140 |
return false |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
func (mt MapType) canStoreMapOrProgram() bool { |
| 147 |
return mt.canStoreMap() || mt.canStoreProgram() || mt == StructOpsMap |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func (mt MapType) canStoreMap() bool { |
| 153 |
return mt == ArrayOfMaps || mt == HashOfMaps || mt == WindowsArrayOfMaps || mt == WindowsHashOfMaps |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
func (mt MapType) canStoreProgram() bool { |
| 159 |
return mt == ProgramArray || mt == WindowsProgramArray |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
func (mt MapType) canHaveValueSize() bool { |
| 164 |
switch mt { |
| 165 |
case RingBuf, Arena: |
| 166 |
return false |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
case PerfEventArray: |
| 171 |
return false |
| 172 |
} |
| 173 |
|
| 174 |
return true |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (mt MapType) mustHaveNoPrealloc() bool { |
| 181 |
switch mt { |
| 182 |
case CgroupStorage, InodeStorage, TaskStorage, SkStorage: |
| 183 |
return true |
| 184 |
case LPMTrie: |
| 185 |
return true |
| 186 |
} |
| 187 |
|
| 188 |
return false |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
func (mt MapType) mustHaveZeroMaxEntries() bool { |
| 193 |
switch mt { |
| 194 |
case CgroupStorage, CGroupStorage, PerCPUCGroupStorage, InodeStorage, TaskStorage, SkStorage: |
| 195 |
return true |
| 196 |
} |
| 197 |
|
| 198 |
return false |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
type ProgramType uint32 |
| 203 |
|
| 204 |
|
| 205 |
const ( |
| 206 |
UnspecifiedProgram = ProgramType(sys.BPF_PROG_TYPE_UNSPEC) |
| 207 |
SocketFilter = ProgramType(sys.BPF_PROG_TYPE_SOCKET_FILTER) |
| 208 |
Kprobe = ProgramType(sys.BPF_PROG_TYPE_KPROBE) |
| 209 |
SchedCLS = ProgramType(sys.BPF_PROG_TYPE_SCHED_CLS) |
| 210 |
SchedACT = ProgramType(sys.BPF_PROG_TYPE_SCHED_ACT) |
| 211 |
TracePoint = ProgramType(sys.BPF_PROG_TYPE_TRACEPOINT) |
| 212 |
XDP = ProgramType(sys.BPF_PROG_TYPE_XDP) |
| 213 |
PerfEvent = ProgramType(sys.BPF_PROG_TYPE_PERF_EVENT) |
| 214 |
CGroupSKB = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SKB) |
| 215 |
CGroupSock = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SOCK) |
| 216 |
LWTIn = ProgramType(sys.BPF_PROG_TYPE_LWT_IN) |
| 217 |
LWTOut = ProgramType(sys.BPF_PROG_TYPE_LWT_OUT) |
| 218 |
LWTXmit = ProgramType(sys.BPF_PROG_TYPE_LWT_XMIT) |
| 219 |
SockOps = ProgramType(sys.BPF_PROG_TYPE_SOCK_OPS) |
| 220 |
SkSKB = ProgramType(sys.BPF_PROG_TYPE_SK_SKB) |
| 221 |
CGroupDevice = ProgramType(sys.BPF_PROG_TYPE_CGROUP_DEVICE) |
| 222 |
SkMsg = ProgramType(sys.BPF_PROG_TYPE_SK_MSG) |
| 223 |
RawTracepoint = ProgramType(sys.BPF_PROG_TYPE_RAW_TRACEPOINT) |
| 224 |
CGroupSockAddr = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SOCK_ADDR) |
| 225 |
LWTSeg6Local = ProgramType(sys.BPF_PROG_TYPE_LWT_SEG6LOCAL) |
| 226 |
LircMode2 = ProgramType(sys.BPF_PROG_TYPE_LIRC_MODE2) |
| 227 |
SkReuseport = ProgramType(sys.BPF_PROG_TYPE_SK_REUSEPORT) |
| 228 |
FlowDissector = ProgramType(sys.BPF_PROG_TYPE_FLOW_DISSECTOR) |
| 229 |
CGroupSysctl = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SYSCTL) |
| 230 |
RawTracepointWritable = ProgramType(sys.BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) |
| 231 |
CGroupSockopt = ProgramType(sys.BPF_PROG_TYPE_CGROUP_SOCKOPT) |
| 232 |
Tracing = ProgramType(sys.BPF_PROG_TYPE_TRACING) |
| 233 |
StructOps = ProgramType(sys.BPF_PROG_TYPE_STRUCT_OPS) |
| 234 |
Extension = ProgramType(sys.BPF_PROG_TYPE_EXT) |
| 235 |
LSM = ProgramType(sys.BPF_PROG_TYPE_LSM) |
| 236 |
SkLookup = ProgramType(sys.BPF_PROG_TYPE_SK_LOOKUP) |
| 237 |
Syscall = ProgramType(sys.BPF_PROG_TYPE_SYSCALL) |
| 238 |
Netfilter = ProgramType(sys.BPF_PROG_TYPE_NETFILTER) |
| 239 |
) |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
const ( |
| 245 |
WindowsXDP ProgramType = ProgramType(platform.WindowsTag) | (iota + 1) |
| 246 |
WindowsBind |
| 247 |
WindowsCGroupSockAddr |
| 248 |
WindowsSockOps |
| 249 |
WindowsXDPTest ProgramType = ProgramType(platform.WindowsTag) | 998 |
| 250 |
WindowsSample ProgramType = ProgramType(platform.WindowsTag) | 999 |
| 251 |
) |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
func ProgramTypeForPlatform(plat string, value uint32) (ProgramType, error) { |
| 257 |
return platform.EncodeConstant[ProgramType](plat, value) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
type AttachType uint32 |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
const AttachNone AttachType = 0 |
| 269 |
|
| 270 |
|
| 271 |
const ( |
| 272 |
AttachCGroupInetIngress = AttachType(sys.BPF_CGROUP_INET_INGRESS) |
| 273 |
AttachCGroupInetEgress = AttachType(sys.BPF_CGROUP_INET_EGRESS) |
| 274 |
AttachCGroupInetSockCreate = AttachType(sys.BPF_CGROUP_INET_SOCK_CREATE) |
| 275 |
AttachCGroupSockOps = AttachType(sys.BPF_CGROUP_SOCK_OPS) |
| 276 |
AttachSkSKBStreamParser = AttachType(sys.BPF_SK_SKB_STREAM_PARSER) |
| 277 |
AttachSkSKBStreamVerdict = AttachType(sys.BPF_SK_SKB_STREAM_VERDICT) |
| 278 |
AttachCGroupDevice = AttachType(sys.BPF_CGROUP_DEVICE) |
| 279 |
AttachSkMsgVerdict = AttachType(sys.BPF_SK_MSG_VERDICT) |
| 280 |
AttachCGroupInet4Bind = AttachType(sys.BPF_CGROUP_INET4_BIND) |
| 281 |
AttachCGroupInet6Bind = AttachType(sys.BPF_CGROUP_INET6_BIND) |
| 282 |
AttachCGroupInet4Connect = AttachType(sys.BPF_CGROUP_INET4_CONNECT) |
| 283 |
AttachCGroupInet6Connect = AttachType(sys.BPF_CGROUP_INET6_CONNECT) |
| 284 |
AttachCGroupInet4PostBind = AttachType(sys.BPF_CGROUP_INET4_POST_BIND) |
| 285 |
AttachCGroupInet6PostBind = AttachType(sys.BPF_CGROUP_INET6_POST_BIND) |
| 286 |
AttachCGroupUDP4Sendmsg = AttachType(sys.BPF_CGROUP_UDP4_SENDMSG) |
| 287 |
AttachCGroupUDP6Sendmsg = AttachType(sys.BPF_CGROUP_UDP6_SENDMSG) |
| 288 |
AttachLircMode2 = AttachType(sys.BPF_LIRC_MODE2) |
| 289 |
AttachFlowDissector = AttachType(sys.BPF_FLOW_DISSECTOR) |
| 290 |
AttachCGroupSysctl = AttachType(sys.BPF_CGROUP_SYSCTL) |
| 291 |
AttachCGroupUDP4Recvmsg = AttachType(sys.BPF_CGROUP_UDP4_RECVMSG) |
| 292 |
AttachCGroupUDP6Recvmsg = AttachType(sys.BPF_CGROUP_UDP6_RECVMSG) |
| 293 |
AttachCGroupGetsockopt = AttachType(sys.BPF_CGROUP_GETSOCKOPT) |
| 294 |
AttachCGroupSetsockopt = AttachType(sys.BPF_CGROUP_SETSOCKOPT) |
| 295 |
AttachTraceRawTp = AttachType(sys.BPF_TRACE_RAW_TP) |
| 296 |
AttachTraceFEntry = AttachType(sys.BPF_TRACE_FENTRY) |
| 297 |
AttachTraceFExit = AttachType(sys.BPF_TRACE_FEXIT) |
| 298 |
AttachModifyReturn = AttachType(sys.BPF_MODIFY_RETURN) |
| 299 |
AttachLSMMac = AttachType(sys.BPF_LSM_MAC) |
| 300 |
AttachTraceIter = AttachType(sys.BPF_TRACE_ITER) |
| 301 |
AttachCgroupInet4GetPeername = AttachType(sys.BPF_CGROUP_INET4_GETPEERNAME) |
| 302 |
AttachCgroupInet6GetPeername = AttachType(sys.BPF_CGROUP_INET6_GETPEERNAME) |
| 303 |
AttachCgroupInet4GetSockname = AttachType(sys.BPF_CGROUP_INET4_GETSOCKNAME) |
| 304 |
AttachCgroupInet6GetSockname = AttachType(sys.BPF_CGROUP_INET6_GETSOCKNAME) |
| 305 |
AttachXDPDevMap = AttachType(sys.BPF_XDP_DEVMAP) |
| 306 |
AttachCgroupInetSockRelease = AttachType(sys.BPF_CGROUP_INET_SOCK_RELEASE) |
| 307 |
AttachXDPCPUMap = AttachType(sys.BPF_XDP_CPUMAP) |
| 308 |
AttachSkLookup = AttachType(sys.BPF_SK_LOOKUP) |
| 309 |
AttachXDP = AttachType(sys.BPF_XDP) |
| 310 |
AttachSkSKBVerdict = AttachType(sys.BPF_SK_SKB_VERDICT) |
| 311 |
AttachSkReuseportSelect = AttachType(sys.BPF_SK_REUSEPORT_SELECT) |
| 312 |
AttachSkReuseportSelectOrMigrate = AttachType(sys.BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) |
| 313 |
AttachPerfEvent = AttachType(sys.BPF_PERF_EVENT) |
| 314 |
AttachTraceKprobeMulti = AttachType(sys.BPF_TRACE_KPROBE_MULTI) |
| 315 |
AttachTraceKprobeSession = AttachType(sys.BPF_TRACE_KPROBE_SESSION) |
| 316 |
AttachLSMCgroup = AttachType(sys.BPF_LSM_CGROUP) |
| 317 |
AttachStructOps = AttachType(sys.BPF_STRUCT_OPS) |
| 318 |
AttachNetfilter = AttachType(sys.BPF_NETFILTER) |
| 319 |
AttachTCXIngress = AttachType(sys.BPF_TCX_INGRESS) |
| 320 |
AttachTCXEgress = AttachType(sys.BPF_TCX_EGRESS) |
| 321 |
AttachTraceUprobeMulti = AttachType(sys.BPF_TRACE_UPROBE_MULTI) |
| 322 |
AttachCgroupUnixConnect = AttachType(sys.BPF_CGROUP_UNIX_CONNECT) |
| 323 |
AttachCgroupUnixSendmsg = AttachType(sys.BPF_CGROUP_UNIX_SENDMSG) |
| 324 |
AttachCgroupUnixRecvmsg = AttachType(sys.BPF_CGROUP_UNIX_RECVMSG) |
| 325 |
AttachCgroupUnixGetpeername = AttachType(sys.BPF_CGROUP_UNIX_GETPEERNAME) |
| 326 |
AttachCgroupUnixGetsockname = AttachType(sys.BPF_CGROUP_UNIX_GETSOCKNAME) |
| 327 |
AttachNetkitPrimary = AttachType(sys.BPF_NETKIT_PRIMARY) |
| 328 |
AttachNetkitPeer = AttachType(sys.BPF_NETKIT_PEER) |
| 329 |
) |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
const ( |
| 335 |
AttachWindowsXDP = AttachType(platform.WindowsTag | iota + 1) |
| 336 |
AttachWindowsBind |
| 337 |
AttachWindowsCGroupInet4Connect |
| 338 |
AttachWindowsCGroupInet6Connect |
| 339 |
AttachWindowsCgroupInet4RecvAccept |
| 340 |
AttachWindowsCgroupInet6RecvAccept |
| 341 |
AttachWindowsCGroupSockOps |
| 342 |
AttachWindowsSample |
| 343 |
AttachWindowsXDPTest |
| 344 |
) |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
func AttachTypeForPlatform(plat string, value uint32) (AttachType, error) { |
| 350 |
return platform.EncodeConstant[AttachType](plat, value) |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
type AttachFlags uint32 |
| 355 |
|
| 356 |
|
| 357 |
type PinType uint32 |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
const ( |
| 363 |
PinNone PinType = iota |
| 364 |
|
| 365 |
PinByName |
| 366 |
) |
| 367 |
|
| 368 |
|
| 369 |
type LoadPinOptions struct { |
| 370 |
|
| 371 |
|
| 372 |
ReadOnly bool |
| 373 |
WriteOnly bool |
| 374 |
|
| 375 |
|
| 376 |
Flags uint32 |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
func (lpo *LoadPinOptions) Marshal() uint32 { |
| 381 |
if lpo == nil { |
| 382 |
return 0 |
| 383 |
} |
| 384 |
|
| 385 |
flags := lpo.Flags |
| 386 |
if lpo.ReadOnly { |
| 387 |
flags |= sys.BPF_F_RDONLY |
| 388 |
} |
| 389 |
if lpo.WriteOnly { |
| 390 |
flags |= sys.BPF_F_WRONLY |
| 391 |
} |
| 392 |
return flags |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
type BatchOptions struct { |
| 401 |
ElemFlags uint64 |
| 402 |
Flags uint64 |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
type LogLevel = sys.LogLevel |
| 408 |
|
| 409 |
const ( |
| 410 |
|
| 411 |
LogLevelBranch = sys.BPF_LOG_LEVEL1 |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
LogLevelInstruction = sys.BPF_LOG_LEVEL2 |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
LogLevelStats = sys.BPF_LOG_STATS |
| 420 |
) |