-
Notifications
You must be signed in to change notification settings - Fork 34
/
libsysinternalsEBPF.h
246 lines (213 loc) · 8.79 KB
/
libsysinternalsEBPF.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
SysinternalsEBPF
Copyright (c) Microsoft Corporation
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//====================================================================
//
// libsysinternalsEBPF.h
//
// SysinternalsEBPF API
//
//====================================================================
#ifndef LIBSYSINTERNALSEBPF_H
#define LIBSYSINTERNALSEBPF_H
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#define SYSCALL_MAX 335
#define EBPF_GENERIC_SYSCALL 0xFFFF
// error codes
#define E_EBPF_SUCCESS 0
#define E_EBPF_CATASTROPHIC 1
#define E_EBPF_NOTSUPPORTED 2
#define E_EBPF_NOPROG 3
#define E_EBPF_NOMAP 4
#define E_EBPF_NOATTACH 5
#define E_EBPF_NORB 6
#define E_EBPF_NOFILEPATH 7
#define E_EBPF_INVALIDPARAMS 8
#define E_EBPF_NOTP 9
#define E_EBPF_NORTP 10
#define E_EBPF_NOOTHTP 11
#define E_EBPF_NOLOAD 12
#define E_EBPF_CONFIGFAIL 13
#define E_EBPF_MAPUPDATEFAIL 14
#define E_EBPF_NORAWSOCK 15
#define E_DISC_CATASTROPHIC 1001
#define E_DISC_NOTSUPPORTED 1002
#define E_DISC_NOPROG 1003
#define E_DISC_NOMAP 1004
#define E_DISC_NOATTACH 1005
#define E_DISC_NORB 1006
#define E_DISC_NOPDEATH 1007
#define E_DISC_NOTASK 1008
#define E_DISC_GET_COMM 1009
#define E_DISC_PID_OFFSET 1010
#define E_DISC_START_TIME_OFFSET 1011
#define E_DISC_COMM_OFFSET 1012
#define E_DISC_CREDS_OFFSET 1013
#define E_DISC_PWD_PATH_OFFSET 1014
#define E_DISC_DENTRY_NAME_OFFSET 1015
#define E_DISC_DENTRY_PARENT_OFFSET 1016
#define E_DISC_DENTRY_INODE_OFFSET 1017
#define E_DISC_MOUNT_OFFSET 1018
#define E_DISC_FD_OFFSET 1019
#define E_DISC_TTY_OFFSET 1020
#define E_DISC_MM_OFFSET 1021
#define E_DISC_EXE_PATH_OFFSET 1022
#define E_DISC_SKBUFF_OFFSET 1023
typedef struct {
int error;
const char *str;
} eBPFerrorString;
typedef enum {
MAP_UPDATE_CREATE,
MAP_UPDATE_OVERWRITE,
MAP_UPDATE_CREATE_OR_OVERWRITE
} ebpfUpdateMapMode;
//
// Specify a kernel version
//
typedef struct {
const unsigned int major;
const unsigned int minor;
} ebpfKernelVersion;
//
// Specify EBPF tracepoint enter and exit programs and the syscall they are for;
// use EBPF_GENERIC_SYSCALL for the syscall to attach 'programN' to every syscall,
// where N specifies number of input arguments from 0 to 7. Rather than assuming
// 'programN' exists in object, it assumes 'program0', 'program1', ... 'program6'
// exist in object. Note, the last character will be replaced with the number
// representing the number of arguments, so make sure it ends in 'N' or '#'!
//
typedef struct {
const unsigned int syscall;
const char *program;
} ebpfSyscallTPprog;
//
// Specify EBPF raw tracepoint enter and exit programs and the syscalls they are
// for; use EBPF_GENERIC_SYSCALL for the syscall to attach regardless of whether
// a specific syscall is active or not. Specify multiple syscalls for the same
// program by placing each in a separate element, with the same program name
// but each with a different syscall. These must be collected together to
// prevent a program being attached more than once.
//
typedef struct {
const char *program;
const unsigned int syscall;
} ebpfSyscallRTPprog;
//
// Specify non-syscall tracepoint programs and the tracepoint to attach them to;
// specify pseudo syscall to check against in the activeSyscall array; use
// EBPF_GENERIC_SYSCALL to attach regardless. Specify multiple psuedo syscalls
// for the same program as per above.
//
typedef struct {
const char *family;
const char *tracepoint;
const char *program;
const unsigned int pseudoSyscall;
} ebpfTracepointProg;
typedef struct {
const char *filename;
const ebpfKernelVersion minKernel;
const ebpfKernelVersion lessthanKernel;
const bool rawSyscallTracepoints;
const unsigned int numSyscallTPenterProgs;
const ebpfSyscallTPprog *syscallTPenterProgs;
const unsigned int numSyscallTPexitProgs;
const ebpfSyscallTPprog *syscallTPexitProgs;
const unsigned int numSyscallRTPenterProgs;
const ebpfSyscallRTPprog *syscallRTPenterProgs;
const unsigned int numSyscallRTPexitProgs;
const ebpfSyscallRTPprog *syscallRTPexitProgs;
const bool *activeSyscalls;
const unsigned int numOtherTPprogs;
const ebpfTracepointProg *otherTPprogs;
} ebpfTelemetryObject;
typedef struct {
const char *name;
const unsigned int numElements;
const void **keys;
const void **values;
} ebpfTelemetryMapObject;
typedef struct {
const double bootSecSinceEpoch;
const bool enableRawSockCapture;
const unsigned int numEBPFobjects;
const ebpfTelemetryObject *objects;
const unsigned int numDefaultPaths;
const char **defaultPaths;
const unsigned int numMapObjects;
const ebpfTelemetryMapObject *mapObjects;
const char *btfFile;
const bool debug;
} ebpfTelemetryConfig;
typedef struct {
char name[PATH_MAX];
size_t size;
} ebpfProgramSizes;
//
// EventCallback and EventLostCallback handle the events produced by the perf
// ring buffer.
// TelemetryReadyCallback is called when the telemetry has been started.
// TelemetryReloadConfig is called when SIGHUP is received, *in between*
// handling events (e.g. it won't interrupt the EventCallback).
//
typedef void (EventCallback)(void *ctx, int cpu, void *data, uint32_t size);
typedef void (EventLostCallback)(void *ctx, int cpu, uint64_t lostCnt);
typedef void (TelemetryReadyCallback)(void);
typedef void (TelemetryReloadConfig)(void);
typedef void (*log_callback)(const char *format, va_list args);
//
// telemetryCloseAll can be called to shut everything down.
// telemetrySignalInterrupt should be called when a signal is received, other
// than SIGHUP, to inform the polling loop to treat the abrupt (error) exit
// of epoll() as a non-error.
// telemetryUpdateSyscalls should be called to change which syscalls are
// active; expected to be called from telemetryReloadConfig.
// telemetryMapLookupElem, telemetryMapUpdateElem and telemetryMapDeleteElem are
// wrappers for the bpf map functions.
//
int telemetryStart(
const ebpfTelemetryConfig *ebpfConfig,
EventCallback *eventCb,
EventLostCallback *eventsLostCb,
TelemetryReadyCallback *telemetryReady,
TelemetryReloadConfig *telemetryReloadConfig,
void *context,
const char *argv[],
int *fds
);
void telemetryCancel();
void setLogCallback(log_callback callback);
void logMessage(const char* format, ...);
unsigned int getEbpfProgramSizes(char* objectPath, ebpfProgramSizes** progs);
const char *eBPFstrerror(int error);
void telemetryCloseAll(void);
void telemetrySignalInterrupt(int code);
void telemetryUpdateSyscalls(bool *activeSyscalls);
long telemetryMapLookupElem(int fd, const void *key, void *value);
long telemetryMapUpdateElem(int fd, const void *key, const void *value, ebpfUpdateMapMode mode);
long telemetryMapDeleteElem(int fd, const void *key);
bool fileExists(const char *filepath);
bool dirExists(const char *dirpath);
bool createDir(const char *dir, mode_t perms);
bool dropFile(const char *filepath, const char *start, const char *end, bool force, mode_t perms);
char* getLibInstallPath();
bool fileDelete(const char *filepath);
#endif