Skip to content

Commit 2d1f0b6

Browse files
committed
ebpf: encode platform in type constants
Linux and Windows share the concept of program types, map types and so on. For example, XDP is supported on both platforms. Unfortunately the constant values used by both platforms are different. On Linux, XDP has value 0x6 while on Windows it is 0x1. This problem extends to a CollectionSpec parsed from an ELF. Because of the different values a Windows PerCPUHash aliases with a Linux ProgramArray. This causes all sorts of mayhem that is hard to avoid without making too many changes to the code base. Introduce a Platform type which enumerates the supported platforms. That type is then used to tag constant values. This means that there are distinct constants even when a type exists on both platforms. Instead of overloading the XDP constant we will introduce WindowsXDP. This will allow the ELF reader to remain fully deterministic as well: we can parse Linux ELF on Windows and vice versa. The value of the Platform tag is chosen so that on Linux the constant values do not change and are identical with the value in upstream headers. On Windows the constants have a fixed (but implementation defined) offset. The downside of this approach is that Windows constants will all require a prefix of some sort to distinguish them from Linux ones. This is probably most onerous for map types, because those tend to be created manually more frequently than programs, for example. We can add some behind the scenes translation of Linux map types to Windows ones if need be. Another downside is that constant values are now limited to 28 bits since we steal the top 4 bits to store the platform. The constants we're applying this to are all sequentially numbered from 0, so this is hopefully enough. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 6d6c5e3 commit 2d1f0b6

19 files changed

+522
-269
lines changed

asm/func.go

+12-230
Original file line numberDiff line numberDiff line change
@@ -1,239 +1,21 @@
11
package asm
22

3+
import (
4+
"github.com/cilium/ebpf/internal"
5+
)
6+
37
//go:generate go run golang.org/x/tools/cmd/stringer@latest -output func_string.go -type=BuiltinFunc
48

59
// BuiltinFunc is a built-in eBPF function.
6-
type BuiltinFunc int32
10+
type BuiltinFunc uint32
711

8-
// eBPF built-in functions
9-
//
10-
// You can regenerate this list using the following gawk script:
11-
//
12-
// /FN\(.+\),/ {
13-
// match($1, /\(([a-z_0-9]+),/, r)
14-
// split(r[1], p, "_")
15-
// printf "Fn"
16-
// for (i in p) {
17-
// printf "%s%s", toupper(substr(p[i], 1, 1)), substr(p[i], 2)
18-
// }
19-
// print ""
20-
// }
21-
//
22-
// The script expects include/uapi/linux/bpf.h as it's input.
23-
const (
24-
FnUnspec BuiltinFunc = iota
25-
FnMapLookupElem
26-
FnMapUpdateElem
27-
FnMapDeleteElem
28-
FnProbeRead
29-
FnKtimeGetNs
30-
FnTracePrintk
31-
FnGetPrandomU32
32-
FnGetSmpProcessorId
33-
FnSkbStoreBytes
34-
FnL3CsumReplace
35-
FnL4CsumReplace
36-
FnTailCall
37-
FnCloneRedirect
38-
FnGetCurrentPidTgid
39-
FnGetCurrentUidGid
40-
FnGetCurrentComm
41-
FnGetCgroupClassid
42-
FnSkbVlanPush
43-
FnSkbVlanPop
44-
FnSkbGetTunnelKey
45-
FnSkbSetTunnelKey
46-
FnPerfEventRead
47-
FnRedirect
48-
FnGetRouteRealm
49-
FnPerfEventOutput
50-
FnSkbLoadBytes
51-
FnGetStackid
52-
FnCsumDiff
53-
FnSkbGetTunnelOpt
54-
FnSkbSetTunnelOpt
55-
FnSkbChangeProto
56-
FnSkbChangeType
57-
FnSkbUnderCgroup
58-
FnGetHashRecalc
59-
FnGetCurrentTask
60-
FnProbeWriteUser
61-
FnCurrentTaskUnderCgroup
62-
FnSkbChangeTail
63-
FnSkbPullData
64-
FnCsumUpdate
65-
FnSetHashInvalid
66-
FnGetNumaNodeId
67-
FnSkbChangeHead
68-
FnXdpAdjustHead
69-
FnProbeReadStr
70-
FnGetSocketCookie
71-
FnGetSocketUid
72-
FnSetHash
73-
FnSetsockopt
74-
FnSkbAdjustRoom
75-
FnRedirectMap
76-
FnSkRedirectMap
77-
FnSockMapUpdate
78-
FnXdpAdjustMeta
79-
FnPerfEventReadValue
80-
FnPerfProgReadValue
81-
FnGetsockopt
82-
FnOverrideReturn
83-
FnSockOpsCbFlagsSet
84-
FnMsgRedirectMap
85-
FnMsgApplyBytes
86-
FnMsgCorkBytes
87-
FnMsgPullData
88-
FnBind
89-
FnXdpAdjustTail
90-
FnSkbGetXfrmState
91-
FnGetStack
92-
FnSkbLoadBytesRelative
93-
FnFibLookup
94-
FnSockHashUpdate
95-
FnMsgRedirectHash
96-
FnSkRedirectHash
97-
FnLwtPushEncap
98-
FnLwtSeg6StoreBytes
99-
FnLwtSeg6AdjustSrh
100-
FnLwtSeg6Action
101-
FnRcRepeat
102-
FnRcKeydown
103-
FnSkbCgroupId
104-
FnGetCurrentCgroupId
105-
FnGetLocalStorage
106-
FnSkSelectReuseport
107-
FnSkbAncestorCgroupId
108-
FnSkLookupTcp
109-
FnSkLookupUdp
110-
FnSkRelease
111-
FnMapPushElem
112-
FnMapPopElem
113-
FnMapPeekElem
114-
FnMsgPushData
115-
FnMsgPopData
116-
FnRcPointerRel
117-
FnSpinLock
118-
FnSpinUnlock
119-
FnSkFullsock
120-
FnTcpSock
121-
FnSkbEcnSetCe
122-
FnGetListenerSock
123-
FnSkcLookupTcp
124-
FnTcpCheckSyncookie
125-
FnSysctlGetName
126-
FnSysctlGetCurrentValue
127-
FnSysctlGetNewValue
128-
FnSysctlSetNewValue
129-
FnStrtol
130-
FnStrtoul
131-
FnSkStorageGet
132-
FnSkStorageDelete
133-
FnSendSignal
134-
FnTcpGenSyncookie
135-
FnSkbOutput
136-
FnProbeReadUser
137-
FnProbeReadKernel
138-
FnProbeReadUserStr
139-
FnProbeReadKernelStr
140-
FnTcpSendAck
141-
FnSendSignalThread
142-
FnJiffies64
143-
FnReadBranchRecords
144-
FnGetNsCurrentPidTgid
145-
FnXdpOutput
146-
FnGetNetnsCookie
147-
FnGetCurrentAncestorCgroupId
148-
FnSkAssign
149-
FnKtimeGetBootNs
150-
FnSeqPrintf
151-
FnSeqWrite
152-
FnSkCgroupId
153-
FnSkAncestorCgroupId
154-
FnRingbufOutput
155-
FnRingbufReserve
156-
FnRingbufSubmit
157-
FnRingbufDiscard
158-
FnRingbufQuery
159-
FnCsumLevel
160-
FnSkcToTcp6Sock
161-
FnSkcToTcpSock
162-
FnSkcToTcpTimewaitSock
163-
FnSkcToTcpRequestSock
164-
FnSkcToUdp6Sock
165-
FnGetTaskStack
166-
FnLoadHdrOpt
167-
FnStoreHdrOpt
168-
FnReserveHdrOpt
169-
FnInodeStorageGet
170-
FnInodeStorageDelete
171-
FnDPath
172-
FnCopyFromUser
173-
FnSnprintfBtf
174-
FnSeqPrintfBtf
175-
FnSkbCgroupClassid
176-
FnRedirectNeigh
177-
FnPerCpuPtr
178-
FnThisCpuPtr
179-
FnRedirectPeer
180-
FnTaskStorageGet
181-
FnTaskStorageDelete
182-
FnGetCurrentTaskBtf
183-
FnBprmOptsSet
184-
FnKtimeGetCoarseNs
185-
FnImaInodeHash
186-
FnSockFromFile
187-
FnCheckMtu
188-
FnForEachMapElem
189-
FnSnprintf
190-
FnSysBpf
191-
FnBtfFindByNameKind
192-
FnSysClose
193-
FnTimerInit
194-
FnTimerSetCallback
195-
FnTimerStart
196-
FnTimerCancel
197-
FnGetFuncIp
198-
FnGetAttachCookie
199-
FnTaskPtRegs
200-
FnGetBranchSnapshot
201-
FnTraceVprintk
202-
FnSkcToUnixSock
203-
FnKallsymsLookupName
204-
FnFindVma
205-
FnLoop
206-
FnStrncmp
207-
FnGetFuncArg
208-
FnGetFuncRet
209-
FnGetFuncArgCnt
210-
FnGetRetval
211-
FnSetRetval
212-
FnXdpGetBuffLen
213-
FnXdpLoadBytes
214-
FnXdpStoreBytes
215-
FnCopyFromUserTask
216-
FnSkbSetTstamp
217-
FnImaFileHash
218-
FnKptrXchg
219-
FnMapLookupPercpuElem
220-
FnSkcToMptcpSock
221-
FnDynptrFromMem
222-
FnRingbufReserveDynptr
223-
FnRingbufSubmitDynptr
224-
FnRingbufDiscardDynptr
225-
FnDynptrRead
226-
FnDynptrWrite
227-
FnDynptrData
228-
FnTcpRawGenSyncookieIpv4
229-
FnTcpRawGenSyncookieIpv6
230-
FnTcpRawCheckSyncookieIpv4
231-
FnTcpRawCheckSyncookieIpv6
232-
FnKtimeGetTaiNs
233-
FnUserRingbufDrain
234-
FnCgrpStorageGet
235-
FnCgrpStorageDelete
236-
)
12+
func BuiltinFuncForPlatform(p internal.Platform, value uint32) (BuiltinFunc, error) {
13+
return internal.EncodePlatformConstant[BuiltinFunc](p, value)
14+
}
15+
16+
func (fn BuiltinFunc) Decode() (internal.Platform, uint32) {
17+
return internal.DecodePlatformConstant(fn)
18+
}
23719

23820
// Call emits a function call.
23921
func (fn BuiltinFunc) Call() Instruction {

0 commit comments

Comments
 (0)