forked from dropbox/goebpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_types.go
107 lines (99 loc) · 3.07 KB
/
program_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package goebpf
// ProgramType is eBPF program types enum
type ProgramType int
// Must be in sync with enum bpf_prog_type from <linux/bpf.h>
const (
ProgramTypeUnspec ProgramType = iota
ProgramTypeSocketFilter
ProgramTypeKprobe
ProgramTypeSchedCls
ProgramTypeSchedAct
ProgramTypeTracepoint
ProgramTypeXdp
ProgramTypePerfEvent
ProgramTypeCgroupSkb
ProgramTypeCgroupSock
ProgramTypeLwtIn
ProgramTypeLwtOut
ProgramTypeLwtXmit
ProgramTypeSockOps
)
func (t ProgramType) String() string {
switch t {
case ProgramTypeSocketFilter:
return "SocketFilter"
case ProgramTypeKprobe:
return "Kprobe"
case ProgramTypeSchedCls:
return "SchedCLS"
case ProgramTypeSchedAct:
return "SchedACT"
case ProgramTypeTracepoint:
return "Tracepoint"
case ProgramTypeXdp:
return "XDP"
case ProgramTypePerfEvent:
return "PerfEvent"
case ProgramTypeCgroupSkb:
return "CgroupSkb"
case ProgramTypeCgroupSock:
return "CgroupSock"
case ProgramTypeLwtIn:
return "LWTin"
case ProgramTypeLwtOut:
return "LWTout"
case ProgramTypeLwtXmit:
return "LWTxmit"
case ProgramTypeSockOps:
return "SockOps"
}
return "Unknown"
}
// Map defines interface to interact with eBPF maps
type Map interface {
Create() error
GetFd() int
GetName() string
GetType() MapType
Close() error
// Makes a copy of map definition. This will NOT create map, just copies definition, "template".
// Useful for array/map of maps use case
CloneTemplate() Map
// Generic lookup. Accepts any type which will be
// converted to []byte eventually, returns bytes
Lookup(interface{}) ([]byte, error)
// The same, but does casting of return value to int / uint64
LookupInt(interface{}) (int, error)
LookupUint64(interface{}) (uint64, error)
// The same, but does casting of return value to string
LookupString(interface{}) (string, error)
Insert(interface{}, interface{}) error
Update(interface{}, interface{}) error
Upsert(interface{}, interface{}) error
Delete(interface{}) error
}
// BaseProgram is common shared fields of eBPF programs
type BaseProgram struct {
fd int // File Descriptor
name string
programType ProgramType
license string // License
bytecode []byte // eBPF instructions (each instruction - 8 bytes)
kernelVersion int // Kernel requires version to match running for "kprobe" programs
}
// XdpAttachMode selects a way how XDP program will be attached to interface
type XdpAttachMode int
const (
// XdpAttachModeNone stands for "best effort" - kernel automatically
// selects best mode (would try Drv first, then fallback to Generic).
// NOTE: Kernel will not fallback to Generic XDP if NIC driver failed
// to install XDP program.
XdpAttachModeNone XdpAttachMode = 0
// XdpAttachModeSkb is "generic", kernel mode, less performant comparing to native,
// but does not requires driver support.
XdpAttachModeSkb XdpAttachMode = (1 << 1)
// XdpAttachModeDrv is native, driver mode (support from driver side required)
XdpAttachModeDrv XdpAttachMode = (1 << 2)
// XdpAttachModeHw suitable for NICs with hardware XDP support
XdpAttachModeHw XdpAttachMode = (1 << 3)
)