Skip to content

Commit a169f21

Browse files
authored
Merge pull request #59 from Alan-Jowett/fix_xdp_return_code
Return XDP_PASS on success not 0
2 parents 8bd44f7 + 8cd8a89 commit a169f21

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

bpf/bpf.h.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#if defined(PLATFORM_WINDOWS)
1111
#include <bpf_helpers.h>
1212
#include <bpf_endian.h>
13+
#include <ebpf_nethooks.h>
1314
#define BPF_F_NO_PREALLOC 0
1415
#elif defined(PLATFORM_LINUX)
1516
#include <linux/bpf.h>

bpf/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ tests:
383383
iteration_count: 1000000
384384
program_type: xdp
385385
pass_data: true
386+
expected_result: 1
386387
program_cpu_assignment:
387388
test_bpf_xdp_adjust_head_0: all
388389

@@ -392,6 +393,7 @@ tests:
392393
iteration_count: 1000000
393394
program_type: xdp
394395
pass_data: true
396+
expected_result: 1
395397
program_cpu_assignment:
396398
test_bpf_xdp_adjust_head_plus_100: all
397399

@@ -401,6 +403,7 @@ tests:
401403
iteration_count: 1000000
402404
program_type: xdp
403405
pass_data: true
406+
expected_result: 1
404407
program_cpu_assignment:
405408
test_bpf_xdp_adjust_head_minus_100: all
406409

bpf/xdp.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@
33

44
#include "bpf.h"
55

6-
SEC("xdp/baseline") int test_xdp_baseline(void** ctx) { return 0; }
6+
SEC("xdp/baseline") int test_xdp_baseline(void* ctx) { return XDP_PASS; }
77

88
// Test cases for bpf_xdp_adjust_head
99

10-
SEC("xdp/test_bpf_xdp_adjust_head_0") int test_bpf_xdp_adjust_head_0(void** ctx)
10+
SEC("xdp/test_bpf_xdp_adjust_head_0") int test_bpf_xdp_adjust_head_0(void* ctx)
1111
{
1212
if (bpf_xdp_adjust_head(ctx, 0) < 0) {
13-
return -1;
13+
return XDP_DROP;
1414
}
15-
return 0;
15+
return XDP_PASS;
1616
}
1717

18-
SEC("xdp/test_bpf_xdp_adjust_head_plus_100") int test_bpf_xdp_adjust_head_plus_100(void** ctx)
18+
SEC("xdp/test_bpf_xdp_adjust_head_plus_100") int test_bpf_xdp_adjust_head_plus_100(void* ctx)
1919
{
2020
if (bpf_xdp_adjust_head(ctx, 100) < 0) {
21-
return -1;
21+
return XDP_DROP;
2222
}
2323

2424
if (bpf_xdp_adjust_head(ctx, -100) < 0) {
25-
return -1;
25+
return XDP_DROP;
2626
}
27-
return 0;
27+
return XDP_PASS;
2828
}
2929

30-
SEC("xdp/test_bpf_xdp_adjust_head_minus_100") int test_bpf_xdp_adjust_head_minus_100(void** ctx)
30+
SEC("xdp/test_bpf_xdp_adjust_head_minus_100") int test_bpf_xdp_adjust_head_minus_100(void* ctx)
3131
{
3232
if (bpf_xdp_adjust_head(ctx, -100) < 0) {
33-
return -1;
33+
return XDP_DROP;
3434
}
3535
if (bpf_xdp_adjust_head(ctx, 100) < 0) {
36-
return -1;
36+
return XDP_DROP;
3737
}
38-
return 0;
38+
return XDP_PASS;
3939
}

runner/runner.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ main(int argc, char** argv)
206206
int batch_size;
207207
bool pass_data = false;
208208
bool pass_context = true;
209+
uint32_t expected_result = 0;
209210

210211
// Check if value "platform" is defined and matches the current platform.
211212
if (test["platform"].IsDefined()) {
@@ -238,6 +239,11 @@ main(int argc, char** argv)
238239
pass_context = test["pass_context"].as<bool>();
239240
}
240241

242+
// Check if expected_result is defined and use it.
243+
if (test["expected_result"].IsDefined()) {
244+
expected_result = test["expected_result"].as<uint32_t>();
245+
}
246+
241247
// Override batch size if specified on command line.
242248
if (batch_size_override.has_value()) {
243249
batch_size = batch_size_override.value();
@@ -337,9 +343,9 @@ main(int argc, char** argv)
337343
throw std::runtime_error("Failed to run map_state_preparation program " + prep_program_name);
338344
}
339345

340-
if (opts.retval != 0) {
341-
std::string message = "map_state_preparation program " + prep_program_name + " returned non-zero " +
342-
std::to_string(opts.retval);
346+
if (opts.retval != expected_result) {
347+
std::string message = "map_state_preparation program " + prep_program_name + " returned unexpected value " +
348+
std::to_string(opts.retval) + " expected " + std::to_string(expected_result);
343349
if (ignore_return_code.value_or(false)) {
344350
std::cout << message << std::endl;
345351
} else {
@@ -457,11 +463,11 @@ main(int argc, char** argv)
457463
thread.join();
458464
}
459465

460-
// Check if any program returned non-zero.
466+
// Check if any program returned unexpected result.
461467
for (auto& opt : opts) {
462-
if (opt.retval != 0) {
468+
if (opt.retval != expected_result) {
463469
std::string message =
464-
"Program returned non-zero " + std::to_string(opt.retval) + " in test " + name;
470+
"Program returned unexpected result " + std::to_string(opt.retval) + " in test " + name + " expected " + std::to_string(expected_result);
465471
if (ignore_return_code.value_or(false)) {
466472
std::cout << message << std::endl;
467473
} else {

0 commit comments

Comments
 (0)