Skip to content

Commit

Permalink
Merge pull request #59 from Alan-Jowett/fix_xdp_return_code
Browse files Browse the repository at this point in the history
Return XDP_PASS on success not 0
  • Loading branch information
Alan-Jowett authored Apr 13, 2024
2 parents 8bd44f7 + 8cd8a89 commit a169f21
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions bpf/bpf.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#if defined(PLATFORM_WINDOWS)
#include <bpf_helpers.h>
#include <bpf_endian.h>
#include <ebpf_nethooks.h>
#define BPF_F_NO_PREALLOC 0
#elif defined(PLATFORM_LINUX)
#include <linux/bpf.h>
Expand Down
3 changes: 3 additions & 0 deletions bpf/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ tests:
iteration_count: 1000000
program_type: xdp
pass_data: true
expected_result: 1
program_cpu_assignment:
test_bpf_xdp_adjust_head_0: all

Expand All @@ -392,6 +393,7 @@ tests:
iteration_count: 1000000
program_type: xdp
pass_data: true
expected_result: 1
program_cpu_assignment:
test_bpf_xdp_adjust_head_plus_100: all

Expand All @@ -401,6 +403,7 @@ tests:
iteration_count: 1000000
program_type: xdp
pass_data: true
expected_result: 1
program_cpu_assignment:
test_bpf_xdp_adjust_head_minus_100: all

Expand Down
24 changes: 12 additions & 12 deletions bpf/xdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@

#include "bpf.h"

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

// Test cases for bpf_xdp_adjust_head

SEC("xdp/test_bpf_xdp_adjust_head_0") int test_bpf_xdp_adjust_head_0(void** ctx)
SEC("xdp/test_bpf_xdp_adjust_head_0") int test_bpf_xdp_adjust_head_0(void* ctx)
{
if (bpf_xdp_adjust_head(ctx, 0) < 0) {
return -1;
return XDP_DROP;
}
return 0;
return XDP_PASS;
}

SEC("xdp/test_bpf_xdp_adjust_head_plus_100") int test_bpf_xdp_adjust_head_plus_100(void** ctx)
SEC("xdp/test_bpf_xdp_adjust_head_plus_100") int test_bpf_xdp_adjust_head_plus_100(void* ctx)
{
if (bpf_xdp_adjust_head(ctx, 100) < 0) {
return -1;
return XDP_DROP;
}

if (bpf_xdp_adjust_head(ctx, -100) < 0) {
return -1;
return XDP_DROP;
}
return 0;
return XDP_PASS;
}

SEC("xdp/test_bpf_xdp_adjust_head_minus_100") int test_bpf_xdp_adjust_head_minus_100(void** ctx)
SEC("xdp/test_bpf_xdp_adjust_head_minus_100") int test_bpf_xdp_adjust_head_minus_100(void* ctx)
{
if (bpf_xdp_adjust_head(ctx, -100) < 0) {
return -1;
return XDP_DROP;
}
if (bpf_xdp_adjust_head(ctx, 100) < 0) {
return -1;
return XDP_DROP;
}
return 0;
return XDP_PASS;
}
18 changes: 12 additions & 6 deletions runner/runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ main(int argc, char** argv)
int batch_size;
bool pass_data = false;
bool pass_context = true;
uint32_t expected_result = 0;

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

// Check if expected_result is defined and use it.
if (test["expected_result"].IsDefined()) {
expected_result = test["expected_result"].as<uint32_t>();
}

// Override batch size if specified on command line.
if (batch_size_override.has_value()) {
batch_size = batch_size_override.value();
Expand Down Expand Up @@ -337,9 +343,9 @@ main(int argc, char** argv)
throw std::runtime_error("Failed to run map_state_preparation program " + prep_program_name);
}

if (opts.retval != 0) {
std::string message = "map_state_preparation program " + prep_program_name + " returned non-zero " +
std::to_string(opts.retval);
if (opts.retval != expected_result) {
std::string message = "map_state_preparation program " + prep_program_name + " returned unexpected value " +
std::to_string(opts.retval) + " expected " + std::to_string(expected_result);
if (ignore_return_code.value_or(false)) {
std::cout << message << std::endl;
} else {
Expand Down Expand Up @@ -457,11 +463,11 @@ main(int argc, char** argv)
thread.join();
}

// Check if any program returned non-zero.
// Check if any program returned unexpected result.
for (auto& opt : opts) {
if (opt.retval != 0) {
if (opt.retval != expected_result) {
std::string message =
"Program returned non-zero " + std::to_string(opt.retval) + " in test " + name;
"Program returned unexpected result " + std::to_string(opt.retval) + " in test " + name + " expected " + std::to_string(expected_result);
if (ignore_return_code.value_or(false)) {
std::cout << message << std::endl;
} else {
Expand Down

0 comments on commit a169f21

Please sign in to comment.