summaryrefslogtreecommitdiff
path: root/src/util/virbpf.h
blob: cf21ac2d407a14eefd1f9ceead9c3dc81b5a493f (plain)
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
246
247
248
249
250
251
252
253
254
255
256
/*
 * virbpf.h: methods for eBPF
 *
 * 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, see
 * <http://www.gnu.org/licenses/>.
 */

#pragma once

#ifdef __linux__

# include <linux/bpf.h>

/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */

# define VIR_BPF_ALU64_REG(op, dst, src) \
    ((struct bpf_insn) { \
     .code = BPF_ALU64 | BPF_OP(op) | BPF_X, \
     .dst_reg = dst, \
     .src_reg = src, \
     .off = 0, \
     .imm = 0, \
     })

/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */

# define VIR_BPF_ALU64_IMM(op, dst, immval) \
    ((struct bpf_insn) { \
     .code = BPF_ALU64 | BPF_OP(op) | BPF_K, \
     .dst_reg = dst, \
     .src_reg = 0, \
     .off = 0, \
     .imm = immval, \
     })

/* mov of registers, dst_reg = src_reg */

# define VIR_BPF_MOV64_REG(dst, src) \
    ((struct bpf_insn) { \
     .code = BPF_ALU64 | BPF_MOV | BPF_X, \
     .dst_reg = dst, \
     .src_reg = src, \
     .off = 0, \
     .imm = 0, \
     })

/* mov of immediates, dst_reg = imm32 */

# define VIR_BPF_MOV64_IMM(dst, immval) \
    ((struct bpf_insn) { \
     .code = BPF_ALU64 | BPF_MOV | BPF_K, \
     .dst_reg = dst, \
     .src_reg = 0, \
     .off = 0, \
     .imm = immval, \
     })

/* helper to encode 16 byte instruction */

# define _VIR_BPF_LD_IMM64_RAW(dst, src, immval) \
    ((struct bpf_insn) { \
     .code = BPF_LD | BPF_DW | BPF_IMM, \
     .dst_reg = dst, \
     .src_reg = src, \
     .off = 0, \
     .imm = (uint32_t)immval, \
     }), \
    ((struct bpf_insn) { \
     .code = 0, \
     .dst_reg = 0, \
     .src_reg = 0, \
     .off = 0, \
     .imm = ((uint64_t)immval) >> 32, \
     })

/* encodes single 'load 64-bit immediate' insn, dst_reg = imm ll */

# define VIR_BPF_LD_IMM64(dst, imm) \
    _VIR_BPF_LD_IMM64_RAW(dst, 0, imm)

/* pseudo VIR_BPF_LD_IMM64 insn used to refer to process-local map_fd */

# define VIR_BPF_LD_MAP_FD(dst, mapfd) \
    _VIR_BPF_LD_IMM64_RAW(dst, 1, mapfd)

/* memory load, dst_reg = *(size *) (src_reg + off16) */

# define VIR_BPF_LDX_MEM(size, dst, src, offval) \
    ((struct bpf_insn) { \
     .code = BPF_LDX | BPF_SIZE(size) | BPF_MEM, \
     .dst_reg = dst, \
     .src_reg = src, \
     .off = offval, \
     .imm = 0, \
     })

/* memory store of registers, *(size *) (dst_reg + off16) = src_reg */

# define VIR_BPF_STX_MEM(size, dst, src, offval) \
    ((struct bpf_insn) { \
     .code = BPF_STX | BPF_SIZE(size) | BPF_MEM, \
     .dst_reg = dst, \
     .src_reg = src, \
     .off = offval, \
     .imm = 0, \
     })

/* memory store of immediates, *(size *) (dst_reg + off16) = imm32 */

# define VIR_BPF_ST_MEM(size, dst, immval, offval) \
    ((struct bpf_insn) { \
     .code = BPF_ST | BPF_SIZE(size) | BPF_MEM, \
     .dst_reg = dst, \
     .src_reg = 0, \
     .off = offval, \
     .imm = immval, \
     })

/* conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */

# define VIR_BPF_JMP_REG(op, dst, src, offval) \
    ((struct bpf_insn) { \
     .code = BPF_JMP | BPF_OP(op) | BPF_X, \
     .dst_reg = dst, \
     .src_reg = src, \
     .off = offval, \
     .imm = 0, \
     })

/* conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */

# define VIR_BPF_JMP_IMM(op, dst, immval, offval) \
    ((struct bpf_insn) { \
     .code = BPF_JMP | BPF_OP(op) | BPF_K, \
     .dst_reg = dst, \
     .src_reg = 0, \
     .off = offval, \
     .imm = immval, \
     })

/* call eBPF function, call imm32 */

# define VIR_BPF_CALL_INSN(func) \
    ((struct bpf_insn) { \
     .code = BPF_JMP | BPF_CALL, \
     .dst_reg = 0, \
     .src_reg = 0, \
     .off = 0, \
     .imm = func, \
     })

/* program exit */

# define VIR_BPF_EXIT_INSN() \
    ((struct bpf_insn) { \
     .code = BPF_JMP | BPF_EXIT, \
     .dst_reg = 0, \
     .src_reg = 0, \
     .off = 0, \
     .imm = 0, \
     })

#else /* ! __linux__ */

struct bpf_prog_info;
struct bpf_map_info;
struct bpf_insn;

# define VIR_BPF_ALU64_REG(op, dst, src)
# define VIR_BPF_ALU64_IMM(op, dst, immval)
# define VIR_BPF_MOV64_REG(dst, src)
# define VIR_BPF_MOV64_IMM(dst, immval)
# define VIR_BPF_LD_IMM64(dst, imm)
# define VIR_BPF_LD_MAP_FD(dst, mapfd)
# define VIR_BPF_LDX_MEM(size, dst, src, offval)
# define VIR_BPF_STX_MEM(size, dst, src, offval)
# define VIR_BPF_ST_MEM(size, dst, immval, offval)
# define VIR_BPF_JMP_REG(op, dst, src, offval)
# define VIR_BPF_JMP_IMM(op, dst, immval, offval)
# define VIR_BPF_CALL_INSN(func)
# define VIR_BPF_EXIT_INSN()

#endif /* ! __linux__ */

int
virBPFCreateMap(unsigned int mapType,
                unsigned int keySize,
                unsigned int valSize,
                unsigned int maxEntries);

int
virBPFGetMapInfo(int mapfd,
                 struct bpf_map_info *info);

int
virBPFLoadProg(struct bpf_insn *insns,
               int progType,
               unsigned int insnCnt);

int
virBPFAttachProg(int progfd,
                 int targetfd,
                 int attachType);

int
virBPFDetachProg(int progfd,
                 int targetfd,
                 int attachType);

int
virBPFQueryProg(int targetfd,
                unsigned int maxprogids,
                int attachType,
                unsigned int *progcnt,
                void *progids);

int
virBPFGetProg(unsigned int id);

int
virBPFGetProgInfo(int progfd,
                  struct bpf_prog_info *info,
                  unsigned int **mapIDs);

int
virBPFGetMap(unsigned int id);

int
virBPFLookupElem(int mapfd,
                 void *key,
                 void *val);

int
virBPFGetNextElem(int mapfd,
                  void *key,
                  void *nextKey);

int
virBPFUpdateElem(int mapfd,
                 void *key,
                 void *val);

int
virBPFDeleteElem(int mapfd,
                 void *key);