Browse Source

tests: check bpf syscall decoder unconditionally

* configure.ac (AC_CHECK_HEADERS([linux/bpf.h])): Check for
struct bpf_insn.
* tests/bpf.c: Remove the test guard, include "scno.h", "bpf_attr.h",
"xlat.h", and "xlat/bpf_commands.h".  Conditionalize inclusion of
<linux/bpf.h> on HAVE_LINUX_BPF_H.  Remove all HAVE_UNION_* checks.
(BPF_ATTR_DATA_FIELD): New macro.
(bpf_attr_data): New union.  Note that it is larger than modern
union bpf_attr, this allows more checks to be implemented in the future.
(sizeof_attr, test_bpf_): Replace union bpf_attr with
union bpf_attr_data.
(DEF_BPF_INIT_FIRST): Replace union bpf_attr with the corresponding
structure.
(init_BPF_MAP_CREATE_attr): Replace union bpf_attr with
struct BPF_MAP_CREATE_struct.
(init_BPF_MAP_LOOKUP_ELEM_attr): Replace union bpf_attr with
struct BPF_MAP_LOOKUP_ELEM_struct.
(init_BPF_MAP_UPDATE_ELEM_attr): Replace union bpf_attr with
struct BPF_MAP_UPDATE_ELEM_struct.
(init_BPF_MAP_DELETE_ELEM_attr): Replace union bpf_attr with
struct BPF_MAP_DELETE_ELEM_struct.
(init_BPF_MAP_GET_NEXT_KEY_attr): Replace union bpf_attr with
struct BPF_MAP_GET_NEXT_KEY_struct.
(init_BPF_PROG_LOAD_attr): Replace union bpf_attr with
struct BPF_PROG_LOAD_struct.
(init_BPF_OBJ_PIN_attr): Replace union bpf_attr with
struct BPF_OBJ_PIN_struct.
(init_BPF_PROG_ATTACH_attr): Replace union bpf_attr with
struct BPF_PROG_ATTACH_struct.
(init_BPF_PROG_DETACH_attr): Replace union bpf_attr with
struct BPF_PROG_DETACH_struct.
(init_BPF_PROG_GET_NEXT_ID_attr): Replace union bpf_attr with
struct BPF_PROG_GET_NEXT_ID_struct.
(sample_BPF_PROG_TEST_RUN_attr): Change type from union bpf_attr to
struct BPF_PROG_TEST_RUN_struct, all users updated.
(sample_BPF_OBJ_GET_INFO_BY_FD_attr): Change type from union bpf_attr
to struct BPF_OBJ_GET_INFO_BY_FD_struct, all users updated.
Dmitry V. Levin 2 years ago
parent
commit
3428c8b164
2 changed files with 130 additions and 168 deletions
  1. 4
    1
      configure.ac
  2. 126
    167
      tests/bpf.c

+ 4
- 1
configure.ac View File

@@ -464,7 +464,10 @@ AC_CHECK_HEADERS([linux/input.h], [
464 464
 	AC_CHECK_MEMBERS([struct input_absinfo.resolution],,, [#include <linux/input.h>])
465 465
 ])
466 466
 
467
-AC_CHECK_HEADERS([linux/bpf.h], [st_BPF_ATTR])
467
+AC_CHECK_HEADERS([linux/bpf.h], [
468
+	AC_CHECK_TYPES([struct bpf_insn],,, [#include <linux/bpf.h>])
469
+	st_BPF_ATTR
470
+])
468 471
 
469 472
 AC_CHECK_TYPES(m4_normalize([
470 473
 	struct br_mdb_entry,

+ 126
- 167
tests/bpf.c View File

@@ -28,29 +28,59 @@
28 28
  */
29 29
 
30 30
 #include "tests.h"
31
+
32
+#include <stddef.h>
33
+#include <stdio.h>
34
+#include <stdint.h>
35
+#include <string.h>
36
+#include <unistd.h>
37
+
31 38
 #include <asm/unistd.h>
39
+#include "scno.h"
32 40
 
33
-#if defined __NR_bpf				\
34
- && (defined HAVE_UNION_BPF_ATTR_ATTACH_FLAGS	\
35
-  || defined HAVE_UNION_BPF_ATTR_BPF_FD		\
36
-  || defined HAVE_UNION_BPF_ATTR_FLAGS		\
37
-  || defined HAVE_UNION_BPF_ATTR_INFO_INFO	\
38
-  || defined HAVE_UNION_BPF_ATTR_NEXT_ID	\
39
-  || defined HAVE_UNION_BPF_ATTR_NUMA_NODE	\
40
-  || defined HAVE_UNION_BPF_ATTR_PROG_FLAGS	\
41
-  || defined HAVE_UNION_BPF_ATTR_TEST_DURATION)
42
-
43
-# include <stddef.h>
44
-# include <stdio.h>
45
-# include <stdint.h>
46
-# include <string.h>
47
-# include <unistd.h>
41
+#ifdef HAVE_LINUX_BPF_H
48 42
 # include <linux/bpf.h>
49
-# include "print_fields.h"
43
+#endif
44
+
45
+#include "bpf_attr.h"
46
+#include "print_fields.h"
47
+
48
+#include "xlat.h"
49
+#include "xlat/bpf_commands.h"
50
+
51
+#ifndef HAVE_STRUCT_BPF_INSN
52
+struct bpf_insn {
53
+	uint8_t	code;
54
+	uint8_t	dst_reg:4;
55
+	uint8_t	src_reg:4;
56
+	int16_t	off;
57
+	int32_t	imm;
58
+};
59
+#endif
60
+
61
+#define BPF_ATTR_DATA_FIELD(cmd_) struct cmd_ ## _struct cmd_ ## _data
62
+
63
+union bpf_attr_data {
64
+	BPF_ATTR_DATA_FIELD(BPF_MAP_CREATE);
65
+	BPF_ATTR_DATA_FIELD(BPF_MAP_LOOKUP_ELEM);
66
+	BPF_ATTR_DATA_FIELD(BPF_MAP_UPDATE_ELEM);
67
+	BPF_ATTR_DATA_FIELD(BPF_MAP_DELETE_ELEM);
68
+	BPF_ATTR_DATA_FIELD(BPF_MAP_GET_NEXT_KEY);
69
+	BPF_ATTR_DATA_FIELD(BPF_PROG_LOAD);
70
+	BPF_ATTR_DATA_FIELD(BPF_OBJ_PIN);
71
+	BPF_ATTR_DATA_FIELD(BPF_PROG_ATTACH);
72
+	BPF_ATTR_DATA_FIELD(BPF_PROG_DETACH);
73
+	BPF_ATTR_DATA_FIELD(BPF_PROG_TEST_RUN);
74
+	BPF_ATTR_DATA_FIELD(BPF_PROG_GET_NEXT_ID);
75
+	BPF_ATTR_DATA_FIELD(BPF_PROG_GET_FD_BY_ID);
76
+	BPF_ATTR_DATA_FIELD(BPF_MAP_GET_FD_BY_ID);
77
+	BPF_ATTR_DATA_FIELD(BPF_OBJ_GET_INFO_BY_FD);
78
+	char char_data[256];
79
+};
50 80
 
51 81
 static const kernel_ulong_t long_bits = (kernel_ulong_t) 0xfacefeed00000000ULL;
52 82
 static const char *errstr;
53
-static unsigned int sizeof_attr = sizeof(union bpf_attr);
83
+static unsigned int sizeof_attr = sizeof(union bpf_attr_data);
54 84
 static unsigned int page_size;
55 85
 static unsigned long end_of_page;
56 86
 
@@ -62,15 +92,15 @@ sys_bpf(kernel_ulong_t cmd, kernel_ulong_t attr, kernel_ulong_t size)
62 92
 	return rc;
63 93
 }
64 94
 
65
-# if VERBOSE
66
-#  define print_extra_data(addr_, offs_, size_) \
95
+#if VERBOSE
96
+# define print_extra_data(addr_, offs_, size_) \
67 97
 	do { \
68 98
 		printf("/* bytes %u..%u */ ", (offs_), (size_) + (offs_) - 1); \
69 99
 		print_quoted_hex((addr_) + (offs_), (size_)); \
70 100
 	} while (0)
71
-# else
72
-#  define print_extra_data(addr_, offs_, size_) printf("...")
73
-# endif
101
+#else
102
+# define print_extra_data(addr_, offs_, size_) printf("...")
103
+#endif
74 104
 
75 105
 static void
76 106
 test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
@@ -80,7 +110,7 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
80 110
 	  void (*print_attr)(const unsigned long eop))
81 111
 {
82 112
 	/* zero addr */
83
-	sys_bpf(cmd, 0, long_bits | sizeof(union bpf_attr));
113
+	sys_bpf(cmd, 0, long_bits | sizeof(union bpf_attr_data));
84 114
 	printf("bpf(%s, NULL, %u) = %s\n",
85 115
 	       cmd_str, sizeof_attr, errstr);
86 116
 
@@ -103,7 +133,7 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
103 133
 	printf("bpf(%s, %#lx, %u) = %s\n",
104 134
 	       cmd_str, addr, offset + 1, errstr);
105 135
 
106
-	/* the relevant part of union bpf_attr */
136
+	/* the relevant part of bpf_attr_data */
107 137
 	offset = init_attr(end_of_page);
108 138
 	addr = end_of_page - offset;
109 139
 	sys_bpf(cmd, addr, offset);
@@ -111,13 +141,13 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
111 141
 	print_attr(addr);
112 142
 	printf("}, %u) = %s\n", offset, errstr);
113 143
 
114
-	/* short read of the relevant part of union bpf_attr */
144
+	/* short read of the relevant part of bpf_attr_data */
115 145
 	sys_bpf(cmd, addr + 1, offset);
116 146
 	printf("bpf(%s, %#lx, %u) = %s\n",
117 147
 	       cmd_str, addr + 1, offset, errstr);
118 148
 
119 149
 	if (offset < sizeof_attr) {
120
-		/* short read of the whole union bpf_attr */
150
+		/* short read of the whole bpf_attr_data */
121 151
 		memmove((void *) end_of_page - sizeof_attr + 1,
122 152
 			(void *) addr, offset);
123 153
 		addr = end_of_page - sizeof_attr + 1;
@@ -127,7 +157,7 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
127 157
 		printf("bpf(%s, %#lx, %u) = %s\n",
128 158
 		       cmd_str, addr, sizeof_attr, errstr);
129 159
 
130
-		/* the whole union bpf_attr */
160
+		/* the whole bpf_attr_data */
131 161
 		memmove((void *) end_of_page - sizeof_attr,
132 162
 			(void *) addr, offset);
133 163
 		addr = end_of_page - sizeof_attr;
@@ -170,7 +200,7 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
170 200
 	print_attr(addr);
171 201
 	printf("}, %u) = %s\n", page_size, errstr);
172 202
 
173
-	/* non-zero bytes after the whole union bpf_attr */
203
+	/* non-zero bytes after the whole bpf_attr_data */
174 204
 	fill_memory_ex((void *) addr + offset,
175 205
 		       page_size - offset, '0', 10);
176 206
 	sys_bpf(cmd, addr, page_size);
@@ -187,7 +217,7 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
187 217
 	       cmd_str, addr, page_size + 1, errstr);
188 218
 }
189 219
 
190
-# define TEST_BPF(cmd_)							\
220
+#define TEST_BPF(cmd_)							\
191 221
 	test_bpf_((cmd_), #cmd_,					\
192 222
 		  init_ ## cmd_ ## _first, print_ ## cmd_ ## _first,	\
193 223
 		  init_ ## cmd_ ## _attr, print_ ## cmd_ ## _attr)	\
@@ -197,7 +227,9 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
197 227
 	static unsigned int						\
198 228
 	init_ ## cmd_ ## _first(const unsigned long eop)		\
199 229
 	{								\
200
-		static const union bpf_attr attr = { .field_ = value_ };\
230
+		static const struct cmd_ ## _struct attr = {		\
231
+			.field_ = value_				\
232
+		};							\
201 233
 		static const unsigned int offset = sizeof(attr.field_);	\
202 234
 		const unsigned long addr = eop - offset;		\
203 235
 									\
@@ -206,8 +238,6 @@ test_bpf_(kernel_ulong_t cmd, const char *cmd_str,
206 238
 	}								\
207 239
 	/* End of DEF_INIT_FIRST definition. */
208 240
 
209
-# ifdef HAVE_UNION_BPF_ATTR_NUMA_NODE
210
-
211 241
 DEF_BPF_INIT_FIRST(BPF_MAP_CREATE, map_type, 2)
212 242
 
213 243
 static void
@@ -220,7 +250,7 @@ print_BPF_MAP_CREATE_first(const unsigned long addr)
220 250
 static unsigned int
221 251
 init_BPF_MAP_CREATE_attr(const unsigned long eop)
222 252
 {
223
-	static const union bpf_attr attr = {
253
+	static const struct BPF_MAP_CREATE_struct attr = {
224 254
 		.map_type = 1,
225 255
 		.key_size = 4,
226 256
 		.value_size = 8,
@@ -230,7 +260,7 @@ init_BPF_MAP_CREATE_attr(const unsigned long eop)
230 260
 		.numa_node = 42
231 261
 	};
232 262
 	static const unsigned int offset =
233
-		offsetofend(union bpf_attr, numa_node);
263
+		offsetofend(struct BPF_MAP_CREATE_struct, numa_node);
234 264
 	const unsigned long addr = eop - offset;
235 265
 
236 266
 	memcpy((void *) addr, &attr, offset);
@@ -246,10 +276,6 @@ print_BPF_MAP_CREATE_attr(const unsigned long addr)
246 276
 	       "|BPF_F_NUMA_NODE, inner_map_fd=-1, numa_node=42");
247 277
 }
248 278
 
249
-# endif /* HAVE_UNION_BPF_ATTR_NUMA_NODE */
250
-
251
-# ifdef HAVE_UNION_BPF_ATTR_FLAGS
252
-
253 279
 DEF_BPF_INIT_FIRST(BPF_MAP_LOOKUP_ELEM, map_fd, -1)
254 280
 
255 281
 static void
@@ -261,13 +287,13 @@ print_BPF_MAP_LOOKUP_ELEM_first(const unsigned long addr)
261 287
 static unsigned int
262 288
 init_BPF_MAP_LOOKUP_ELEM_attr(const unsigned long eop)
263 289
 {
264
-	static const union bpf_attr attr = {
290
+	static const struct BPF_MAP_LOOKUP_ELEM_struct attr = {
265 291
 		.map_fd = -1,
266 292
 		.key = 0xdeadbeef,
267 293
 		.value = 0xbadc0ded
268 294
 	};
269 295
 	static const unsigned int offset =
270
-		offsetofend(union bpf_attr, value);
296
+		offsetofend(struct BPF_MAP_LOOKUP_ELEM_struct, value);
271 297
 	const unsigned long addr = eop - offset;
272 298
 
273 299
 	memcpy((void *) addr, &attr, offset);
@@ -280,7 +306,7 @@ print_BPF_MAP_LOOKUP_ELEM_attr(const unsigned long addr)
280 306
 	printf("map_fd=-1, key=0xdeadbeef, value=0xbadc0ded");
281 307
 }
282 308
 
283
-#  define init_BPF_MAP_UPDATE_ELEM_first init_BPF_MAP_LOOKUP_ELEM_first
309
+#define init_BPF_MAP_UPDATE_ELEM_first init_BPF_MAP_LOOKUP_ELEM_first
284 310
 
285 311
 static void
286 312
 print_BPF_MAP_UPDATE_ELEM_first(const unsigned long addr)
@@ -291,14 +317,14 @@ print_BPF_MAP_UPDATE_ELEM_first(const unsigned long addr)
291 317
 static unsigned int
292 318
 init_BPF_MAP_UPDATE_ELEM_attr(const unsigned long eop)
293 319
 {
294
-	static const union bpf_attr attr = {
320
+	static const struct BPF_MAP_UPDATE_ELEM_struct attr = {
295 321
 		.map_fd = -1,
296 322
 		.key = 0xdeadbeef,
297 323
 		.value = 0xbadc0ded,
298 324
 		.flags = 2
299 325
 	};
300 326
 	static const unsigned int offset =
301
-		offsetofend(union bpf_attr, flags);
327
+		offsetofend(struct BPF_MAP_UPDATE_ELEM_struct, flags);
302 328
 	const unsigned long addr = eop - offset;
303 329
 
304 330
 	memcpy((void *) addr, &attr, offset);
@@ -311,7 +337,7 @@ print_BPF_MAP_UPDATE_ELEM_attr(const unsigned long addr)
311 337
 	printf("map_fd=-1, key=0xdeadbeef, value=0xbadc0ded, flags=BPF_EXIST");
312 338
 }
313 339
 
314
-#  define init_BPF_MAP_DELETE_ELEM_first init_BPF_MAP_LOOKUP_ELEM_first
340
+#define init_BPF_MAP_DELETE_ELEM_first init_BPF_MAP_LOOKUP_ELEM_first
315 341
 
316 342
 static void
317 343
 print_BPF_MAP_DELETE_ELEM_first(const unsigned long addr)
@@ -322,12 +348,12 @@ print_BPF_MAP_DELETE_ELEM_first(const unsigned long addr)
322 348
 static unsigned int
323 349
 init_BPF_MAP_DELETE_ELEM_attr(const unsigned long eop)
324 350
 {
325
-	static const union bpf_attr attr = {
351
+	static const struct BPF_MAP_DELETE_ELEM_struct attr = {
326 352
 		.map_fd = -1,
327 353
 		.key = 0xdeadbeef
328 354
 	};
329 355
 	static const unsigned int offset =
330
-		offsetofend(union bpf_attr, key);
356
+		offsetofend(struct BPF_MAP_DELETE_ELEM_struct, key);
331 357
 	const unsigned long addr = eop - offset;
332 358
 
333 359
 	memcpy((void *) addr, &attr, offset);
@@ -340,7 +366,7 @@ print_BPF_MAP_DELETE_ELEM_attr(const unsigned long addr)
340 366
 	printf("map_fd=-1, key=0xdeadbeef");
341 367
 }
342 368
 
343
-#  define init_BPF_MAP_GET_NEXT_KEY_first init_BPF_MAP_LOOKUP_ELEM_first
369
+#define init_BPF_MAP_GET_NEXT_KEY_first init_BPF_MAP_LOOKUP_ELEM_first
344 370
 
345 371
 static void
346 372
 print_BPF_MAP_GET_NEXT_KEY_first(const unsigned long addr)
@@ -351,13 +377,13 @@ print_BPF_MAP_GET_NEXT_KEY_first(const unsigned long addr)
351 377
 static unsigned int
352 378
 init_BPF_MAP_GET_NEXT_KEY_attr(const unsigned long eop)
353 379
 {
354
-	static const union bpf_attr attr = {
380
+	static const struct BPF_MAP_GET_NEXT_KEY_struct attr = {
355 381
 		.map_fd = -1,
356 382
 		.key = 0xdeadbeef,
357 383
 		.next_key = 0xbadc0ded
358 384
 	};
359 385
 	static const unsigned int offset =
360
-		offsetofend(union bpf_attr, next_key);
386
+		offsetofend(struct BPF_MAP_GET_NEXT_KEY_struct, next_key);
361 387
 	const unsigned long addr = eop - offset;
362 388
 
363 389
 	memcpy((void *) addr, &attr, offset);
@@ -370,10 +396,6 @@ print_BPF_MAP_GET_NEXT_KEY_attr(const unsigned long addr)
370 396
 	printf("map_fd=-1, key=0xdeadbeef, next_key=0xbadc0ded");
371 397
 }
372 398
 
373
-# endif /* HAVE_UNION_BPF_ATTR_FLAGS */
374
-
375
-# ifdef HAVE_UNION_BPF_ATTR_PROG_FLAGS
376
-
377 399
 DEF_BPF_INIT_FIRST(BPF_PROG_LOAD, prog_type, 1)
378 400
 
379 401
 static void
@@ -385,14 +407,14 @@ print_BPF_PROG_LOAD_first(const unsigned long addr)
385 407
 }
386 408
 
387 409
 static const struct bpf_insn insns[] = {
388
-	{ .code = BPF_JMP | BPF_EXIT }
410
+	{ .code = 0x95 }
389 411
 };
390 412
 static char log_buf[4096];
391 413
 
392 414
 static unsigned int
393 415
 init_BPF_PROG_LOAD_attr(const unsigned long eop)
394 416
 {
395
-	const union bpf_attr attr = {
417
+	const struct BPF_PROG_LOAD_struct attr = {
396 418
 		.prog_type = 1,
397 419
 		.insn_cnt = ARRAY_SIZE(insns),
398 420
 		.insns = (uintptr_t) insns,
@@ -404,7 +426,7 @@ init_BPF_PROG_LOAD_attr(const unsigned long eop)
404 426
 		.prog_flags = 1
405 427
 	};
406 428
 	static const unsigned int offset =
407
-		offsetofend(union bpf_attr, prog_flags);
429
+		offsetofend(struct BPF_PROG_LOAD_struct, prog_flags);
408 430
 	const unsigned long addr = eop - offset;
409 431
 
410 432
 	memcpy((void *) addr, &attr, offset);
@@ -422,17 +444,6 @@ print_BPF_PROG_LOAD_attr(const unsigned long addr)
422 444
 	       log_buf);
423 445
 }
424 446
 
425
-# endif /* HAVE_UNION_BPF_ATTR_PROG_FLAGS */
426
-
427
-/*
428
- * bpf() syscall and its first six commands were introduced in Linux kernel
429
- * 3.18. Some additional commands were added afterwards, so we need to take
430
- * precautions to make sure the tests compile.
431
- *
432
- * BPF_OBJ_PIN and BPF_OBJ_GET commands appear in kernel 4.4.
433
- */
434
-# ifdef HAVE_UNION_BPF_ATTR_BPF_FD
435
-
436 447
 DEF_BPF_INIT_FIRST(BPF_OBJ_PIN, pathname, 0)
437 448
 
438 449
 static void
@@ -445,12 +456,12 @@ print_BPF_OBJ_PIN_first(const unsigned long addr)
445 456
 static unsigned int
446 457
 init_BPF_OBJ_PIN_attr(const unsigned long eop)
447 458
 {
448
-	const union bpf_attr attr = {
459
+	const struct BPF_OBJ_PIN_struct attr = {
449 460
 		.pathname = (uintptr_t) "/sys/fs/bpf/foo/bar",
450 461
 		.bpf_fd = -1
451 462
 	};
452 463
 	static const unsigned int offset =
453
-		offsetofend(union bpf_attr, bpf_fd);
464
+		offsetofend(struct BPF_OBJ_PIN_struct, bpf_fd);
454 465
 	const unsigned long addr = eop - offset;
455 466
 
456 467
 	memcpy((void *) addr, &attr, offset);
@@ -463,15 +474,10 @@ print_BPF_OBJ_PIN_attr(const unsigned long addr)
463 474
 	printf("pathname=\"/sys/fs/bpf/foo/bar\", bpf_fd=-1");
464 475
 }
465 476
 
466
-#  define init_BPF_OBJ_GET_first init_BPF_OBJ_PIN_first
467
-#  define print_BPF_OBJ_GET_first print_BPF_OBJ_PIN_first
468
-#  define init_BPF_OBJ_GET_attr init_BPF_OBJ_PIN_attr
469
-#  define print_BPF_OBJ_GET_attr print_BPF_OBJ_PIN_attr
470
-
471
-# endif /* HAVE_UNION_BPF_ATTR_BPF_FD */
472
-
473
-/* BPF_PROG_ATTACH and BPF_PROG_DETACH commands appear in kernel 4.10. */
474
-# ifdef HAVE_UNION_BPF_ATTR_ATTACH_FLAGS
477
+#define init_BPF_OBJ_GET_first init_BPF_OBJ_PIN_first
478
+#define print_BPF_OBJ_GET_first print_BPF_OBJ_PIN_first
479
+#define init_BPF_OBJ_GET_attr init_BPF_OBJ_PIN_attr
480
+#define print_BPF_OBJ_GET_attr print_BPF_OBJ_PIN_attr
475 481
 
476 482
 DEF_BPF_INIT_FIRST(BPF_PROG_ATTACH, target_fd, -1)
477 483
 
@@ -485,14 +491,14 @@ print_BPF_PROG_ATTACH_first(const unsigned long addr)
485 491
 static unsigned int
486 492
 init_BPF_PROG_ATTACH_attr(const unsigned long eop)
487 493
 {
488
-	static const union bpf_attr attr = {
494
+	static const struct BPF_PROG_ATTACH_struct attr = {
489 495
 		.target_fd = -1,
490 496
 		.attach_bpf_fd = -2,
491 497
 		.attach_type = 2,
492 498
 		.attach_flags = 1
493 499
 	};
494 500
 	static const unsigned int offset =
495
-		offsetofend(union bpf_attr, attach_flags);
501
+		offsetofend(struct BPF_PROG_ATTACH_struct, attach_flags);
496 502
 	const unsigned long addr = eop - offset;
497 503
 
498 504
 	memcpy((void *) addr, &attr, offset);
@@ -507,17 +513,17 @@ print_BPF_PROG_ATTACH_attr(const unsigned long addr)
507 513
 	       ", attach_flags=BPF_F_ALLOW_OVERRIDE");
508 514
 }
509 515
 
510
-#  define init_BPF_PROG_DETACH_first init_BPF_PROG_ATTACH_first
516
+#define init_BPF_PROG_DETACH_first init_BPF_PROG_ATTACH_first
511 517
 
512 518
 static unsigned int
513 519
 init_BPF_PROG_DETACH_attr(const unsigned long eop)
514 520
 {
515
-	static const union bpf_attr attr = {
521
+	static const struct BPF_PROG_DETACH_struct attr = {
516 522
 		.target_fd = -1,
517 523
 		.attach_type = 2
518 524
 	};
519 525
 	static const unsigned int offset =
520
-		offsetofend(union bpf_attr, attach_type);
526
+		offsetofend(struct BPF_PROG_DETACH_struct, attach_type);
521 527
 	const unsigned long addr = eop - offset;
522 528
 
523 529
 	memcpy((void *) addr, &attr, offset);
@@ -537,12 +543,7 @@ print_BPF_PROG_DETACH_attr(const unsigned long addr)
537 543
 	printf("target_fd=-1, attach_type=BPF_CGROUP_INET_SOCK_CREATE");
538 544
 }
539 545
 
540
-# endif /* HAVE_UNION_BPF_ATTR_ATTACH_FLAGS */
541
-
542
-/* BPF_PROG_TEST_RUN command appears in kernel 4.12. */
543
-# ifdef HAVE_UNION_BPF_ATTR_TEST_DURATION
544
-
545
-DEF_BPF_INIT_FIRST(BPF_PROG_TEST_RUN, test.prog_fd, -1)
546
+DEF_BPF_INIT_FIRST(BPF_PROG_TEST_RUN, prog_fd, -1)
546 547
 
547 548
 static void
548 549
 print_BPF_PROG_TEST_RUN_first(const unsigned long addr)
@@ -551,23 +552,21 @@ print_BPF_PROG_TEST_RUN_first(const unsigned long addr)
551 552
 	       ", data_in=0, data_out=0, repeat=0, duration=0}");
552 553
 }
553 554
 
554
-static const union bpf_attr sample_BPF_PROG_TEST_RUN_attr = {
555
-	.test = {
556
-		.prog_fd = -1,
557
-		.retval = 0xfac1fed2,
558
-		.data_size_in = 0xfac3fed4,
559
-		.data_size_out = 0xfac5fed6,
560
-		.data_in = (uint64_t) 0xfacef11dbadc2ded,
561
-		.data_out = (uint64_t) 0xfacef33dbadc4ded,
562
-		.repeat = 0xfac7fed8,
563
-		.duration = 0xfac9feda
564
-	}
555
+static const struct BPF_PROG_TEST_RUN_struct sample_BPF_PROG_TEST_RUN_attr = {
556
+	.prog_fd = -1,
557
+	.retval = 0xfac1fed2,
558
+	.data_size_in = 0xfac3fed4,
559
+	.data_size_out = 0xfac5fed6,
560
+	.data_in = (uint64_t) 0xfacef11dbadc2ded,
561
+	.data_out = (uint64_t) 0xfacef33dbadc4ded,
562
+	.repeat = 0xfac7fed8,
563
+	.duration = 0xfac9feda
565 564
 };
566 565
 static unsigned int
567 566
 init_BPF_PROG_TEST_RUN_attr(const unsigned long eop)
568 567
 {
569 568
 	static const unsigned int offset =
570
-		offsetofend(union bpf_attr, test);
569
+		offsetofend(struct BPF_PROG_TEST_RUN_struct, duration);
571 570
 	const unsigned long addr = eop - offset;
572 571
 
573 572
 	memcpy((void *) addr, &sample_BPF_PROG_TEST_RUN_attr, offset);
@@ -577,21 +576,17 @@ init_BPF_PROG_TEST_RUN_attr(const unsigned long eop)
577 576
 static void
578 577
 print_BPF_PROG_TEST_RUN_attr(const unsigned long addr)
579 578
 {
580
-	PRINT_FIELD_D("test={", sample_BPF_PROG_TEST_RUN_attr.test, prog_fd);
581
-	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr.test, retval);
582
-	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr.test, data_size_in);
583
-	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr.test, data_size_out);
584
-	PRINT_FIELD_X(", ", sample_BPF_PROG_TEST_RUN_attr.test, data_in);
585
-	PRINT_FIELD_X(", ", sample_BPF_PROG_TEST_RUN_attr.test, data_out);
586
-	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr.test, repeat);
587
-	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr.test, duration);
579
+	PRINT_FIELD_D("test={", sample_BPF_PROG_TEST_RUN_attr, prog_fd);
580
+	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr, retval);
581
+	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr, data_size_in);
582
+	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr, data_size_out);
583
+	PRINT_FIELD_X(", ", sample_BPF_PROG_TEST_RUN_attr, data_in);
584
+	PRINT_FIELD_X(", ", sample_BPF_PROG_TEST_RUN_attr, data_out);
585
+	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr, repeat);
586
+	PRINT_FIELD_U(", ", sample_BPF_PROG_TEST_RUN_attr, duration);
588 587
 	printf("}");
589 588
 }
590 589
 
591
-# endif /* HAVE_UNION_BPF_ATTR_TEST_DURATION */
592
-
593
-# ifdef HAVE_UNION_BPF_ATTR_NEXT_ID
594
-
595 590
 DEF_BPF_INIT_FIRST(BPF_PROG_GET_NEXT_ID, start_id, 0xdeadbeef)
596 591
 
597 592
 static void
@@ -603,12 +598,12 @@ print_BPF_PROG_GET_NEXT_ID_first(const unsigned long addr)
603 598
 static unsigned int
604 599
 init_BPF_PROG_GET_NEXT_ID_attr(const unsigned long eop)
605 600
 {
606
-	static const union bpf_attr attr = {
601
+	static const struct BPF_PROG_GET_NEXT_ID_struct attr = {
607 602
 		.start_id = 0xbadc0ded,
608 603
 		.next_id = 0xcafef00d
609 604
 	};
610 605
 	static const unsigned int offset =
611
-		offsetofend(union bpf_attr, next_id);
606
+		offsetofend(struct BPF_PROG_GET_NEXT_ID_struct, next_id);
612 607
 	const unsigned long addr = eop - offset;
613 608
 
614 609
 	memcpy((void *) addr, &attr, offset);
@@ -621,13 +616,13 @@ print_BPF_PROG_GET_NEXT_ID_attr(const unsigned long addr)
621 616
 	printf("start_id=%u, next_id=%u", 0xbadc0ded, 0xcafef00d);
622 617
 }
623 618
 
624
-#  define init_BPF_MAP_GET_NEXT_ID_first init_BPF_PROG_GET_NEXT_ID_first
625
-#  define print_BPF_MAP_GET_NEXT_ID_first print_BPF_PROG_GET_NEXT_ID_first
626
-#  define init_BPF_MAP_GET_NEXT_ID_attr init_BPF_PROG_GET_NEXT_ID_attr
627
-#  define print_BPF_MAP_GET_NEXT_ID_attr print_BPF_PROG_GET_NEXT_ID_attr
619
+#define init_BPF_MAP_GET_NEXT_ID_first init_BPF_PROG_GET_NEXT_ID_first
620
+#define print_BPF_MAP_GET_NEXT_ID_first print_BPF_PROG_GET_NEXT_ID_first
621
+#define init_BPF_MAP_GET_NEXT_ID_attr init_BPF_PROG_GET_NEXT_ID_attr
622
+#define print_BPF_MAP_GET_NEXT_ID_attr print_BPF_PROG_GET_NEXT_ID_attr
628 623
 
629
-#  define init_BPF_PROG_GET_FD_BY_ID_first init_BPF_PROG_GET_NEXT_ID_first
630
-#  define init_BPF_PROG_GET_FD_BY_ID_attr init_BPF_PROG_GET_NEXT_ID_attr
624
+#define init_BPF_PROG_GET_FD_BY_ID_first init_BPF_PROG_GET_NEXT_ID_first
625
+#define init_BPF_PROG_GET_FD_BY_ID_attr init_BPF_PROG_GET_NEXT_ID_attr
631 626
 
632 627
 static void
633 628
 print_BPF_PROG_GET_FD_BY_ID_first(const unsigned long addr)
@@ -641,8 +636,8 @@ print_BPF_PROG_GET_FD_BY_ID_attr(const unsigned long addr)
641 636
 	printf("prog_id=%u, next_id=%u", 0xbadc0ded, 0xcafef00d);
642 637
 }
643 638
 
644
-#  define init_BPF_MAP_GET_FD_BY_ID_first init_BPF_PROG_GET_NEXT_ID_first
645
-#  define init_BPF_MAP_GET_FD_BY_ID_attr init_BPF_PROG_GET_NEXT_ID_attr
639
+#define init_BPF_MAP_GET_FD_BY_ID_first init_BPF_PROG_GET_NEXT_ID_first
640
+#define init_BPF_MAP_GET_FD_BY_ID_attr init_BPF_PROG_GET_NEXT_ID_attr
646 641
 
647 642
 static void
648 643
 print_BPF_MAP_GET_FD_BY_ID_first(const unsigned long addr)
@@ -656,11 +651,7 @@ print_BPF_MAP_GET_FD_BY_ID_attr(const unsigned long addr)
656 651
 	printf("map_id=%u, next_id=%u", 0xbadc0ded, 0xcafef00d);
657 652
 }
658 653
 
659
-# endif /* HAVE_UNION_BPF_ATTR_NEXT_ID */
660
-
661
-# ifdef HAVE_UNION_BPF_ATTR_INFO_INFO
662
-
663
-DEF_BPF_INIT_FIRST(BPF_OBJ_GET_INFO_BY_FD, info.bpf_fd, -1)
654
+DEF_BPF_INIT_FIRST(BPF_OBJ_GET_INFO_BY_FD, bpf_fd, -1)
664 655
 
665 656
 static void
666 657
 print_BPF_OBJ_GET_INFO_BY_FD_first(const unsigned long addr)
@@ -668,18 +659,17 @@ print_BPF_OBJ_GET_INFO_BY_FD_first(const unsigned long addr)
668 659
 	printf("info={bpf_fd=-1, info_len=0, info=0}");
669 660
 }
670 661
 
671
-static const union bpf_attr sample_BPF_OBJ_GET_INFO_BY_FD_attr = {
672
-	.info = {
662
+static const struct BPF_OBJ_GET_INFO_BY_FD_struct
663
+	sample_BPF_OBJ_GET_INFO_BY_FD_attr = {
673 664
 		.bpf_fd = -1,
674 665
 		.info_len = 0xdeadbeef,
675 666
 		.info = (uint64_t) 0xfacefeedbadc0ded
676
-	}
677
-};
667
+	};
678 668
 static unsigned int
679 669
 init_BPF_OBJ_GET_INFO_BY_FD_attr(const unsigned long eop)
680 670
 {
681 671
 	static const unsigned int offset =
682
-		offsetofend(union bpf_attr, info);
672
+		offsetofend(struct BPF_OBJ_GET_INFO_BY_FD_struct, info);
683 673
 	const unsigned long addr = eop - offset;
684 674
 
685 675
 	memcpy((void *) addr, &sample_BPF_OBJ_GET_INFO_BY_FD_attr, offset);
@@ -689,59 +679,34 @@ init_BPF_OBJ_GET_INFO_BY_FD_attr(const unsigned long eop)
689 679
 static void
690 680
 print_BPF_OBJ_GET_INFO_BY_FD_attr(const unsigned long addr)
691 681
 {
692
-	PRINT_FIELD_D("info={", sample_BPF_OBJ_GET_INFO_BY_FD_attr.info, bpf_fd);
693
-	PRINT_FIELD_U(", ", sample_BPF_OBJ_GET_INFO_BY_FD_attr.info, info_len);
694
-	PRINT_FIELD_X(", ", sample_BPF_OBJ_GET_INFO_BY_FD_attr.info, info);
682
+	PRINT_FIELD_D("info={", sample_BPF_OBJ_GET_INFO_BY_FD_attr, bpf_fd);
683
+	PRINT_FIELD_U(", ", sample_BPF_OBJ_GET_INFO_BY_FD_attr, info_len);
684
+	PRINT_FIELD_X(", ", sample_BPF_OBJ_GET_INFO_BY_FD_attr, info);
695 685
 	printf("}");
696 686
 }
697 687
 
698
-# endif /* HAVE_UNION_BPF_ATTR_INFO_INFO */
699
-
700 688
 int
701 689
 main(void)
702 690
 {
703 691
 	page_size = get_page_size();
704 692
 	end_of_page = (unsigned long) tail_alloc(1) + 1;
705 693
 
706
-# ifdef HAVE_UNION_BPF_ATTR_NUMA_NODE
707 694
 	TEST_BPF(BPF_MAP_CREATE);
708
-# endif
709
-
710
-# ifdef HAVE_UNION_BPF_ATTR_FLAGS
711 695
 	TEST_BPF(BPF_MAP_LOOKUP_ELEM);
712 696
 	TEST_BPF(BPF_MAP_UPDATE_ELEM);
713 697
 	TEST_BPF(BPF_MAP_DELETE_ELEM);
714 698
 	TEST_BPF(BPF_MAP_GET_NEXT_KEY);
715
-# endif
716
-
717
-# ifdef HAVE_UNION_BPF_ATTR_PROG_FLAGS
718 699
 	TEST_BPF(BPF_PROG_LOAD);
719
-# endif
720
-
721
-# ifdef HAVE_UNION_BPF_ATTR_BPF_FD
722 700
 	TEST_BPF(BPF_OBJ_PIN);
723 701
 	TEST_BPF(BPF_OBJ_GET);
724
-# endif
725
-
726
-# ifdef HAVE_UNION_BPF_ATTR_ATTACH_FLAGS
727 702
 	TEST_BPF(BPF_PROG_ATTACH);
728 703
 	TEST_BPF(BPF_PROG_DETACH);
729
-# endif
730
-
731
-# ifdef HAVE_UNION_BPF_ATTR_TEST_DURATION
732 704
 	TEST_BPF(BPF_PROG_TEST_RUN);
733
-# endif
734
-
735
-# ifdef HAVE_UNION_BPF_ATTR_NEXT_ID
736 705
 	TEST_BPF(BPF_PROG_GET_NEXT_ID);
737 706
 	TEST_BPF(BPF_MAP_GET_NEXT_ID);
738 707
 	TEST_BPF(BPF_PROG_GET_FD_BY_ID);
739 708
 	TEST_BPF(BPF_MAP_GET_FD_BY_ID);
740
-# endif
741
-
742
-# ifdef HAVE_UNION_BPF_ATTR_INFO_INFO
743 709
 	TEST_BPF(BPF_OBJ_GET_INFO_BY_FD);
744
-# endif
745 710
 
746 711
 	sys_bpf(0xfacefeed, 0, (kernel_ulong_t) 0xfacefeedbadc0dedULL);
747 712
 	printf("bpf(0xfacefeed /* BPF_??? */, NULL, %u) = %s\n",
@@ -754,9 +719,3 @@ main(void)
754 719
 	puts("+++ exited with 0 +++");
755 720
 	return 0;
756 721
 }
757
-
758
-#else
759
-
760
-SKIP_MAIN_UNDEFINED("__NR_bpf && HAVE_UNION_BPF_ATTR_*")
761
-
762
-#endif

Loading…
Cancel
Save