2021-08-08 00:19:00 +03:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
// Test eBPF program for EBPF_PROGRAM_TYPE_SAMPLE implemented in
|
|
|
|
|
// the Test eBPF extension.
|
|
|
|
|
|
2021-09-03 03:23:16 +03:00
|
|
|
|
#include "sample_common_routines.h"
|
2021-08-08 00:19:00 +03:00
|
|
|
|
#include "sample_ext_helpers.h"
|
2021-09-03 03:23:16 +03:00
|
|
|
|
#include "sample_test_common.h"
|
2021-08-08 00:19:00 +03:00
|
|
|
|
|
|
|
|
|
#define VALUE_SIZE 32
|
|
|
|
|
|
2021-08-31 05:14:08 +03:00
|
|
|
|
SEC("maps")
|
|
|
|
|
ebpf_map_definition_in_file_t test_map = {
|
|
|
|
|
.size = sizeof(ebpf_map_definition_in_file_t),
|
2021-08-25 18:01:16 +03:00
|
|
|
|
.type = BPF_MAP_TYPE_ARRAY,
|
|
|
|
|
.key_size = sizeof(uint32_t),
|
|
|
|
|
.value_size = VALUE_SIZE,
|
|
|
|
|
.max_entries = 2};
|
2021-08-08 00:19:00 +03:00
|
|
|
|
|
2021-08-31 05:14:08 +03:00
|
|
|
|
SEC("sample_ext")
|
2021-08-08 00:19:00 +03:00
|
|
|
|
int
|
2021-09-03 03:23:16 +03:00
|
|
|
|
test_program_entry(sample_program_context_t* context)
|
2021-08-08 00:19:00 +03:00
|
|
|
|
{
|
|
|
|
|
int64_t result;
|
|
|
|
|
uint32_t keys[2] = {0, 1};
|
|
|
|
|
uint8_t* values[2] = {0};
|
|
|
|
|
|
|
|
|
|
values[0] = bpf_map_lookup_elem(&test_map, &keys[0]);
|
|
|
|
|
values[1] = bpf_map_lookup_elem(&test_map, &keys[1]);
|
|
|
|
|
|
|
|
|
|
if (context->data_end > context->data_start) {
|
2021-09-03 03:23:16 +03:00
|
|
|
|
int64_t position = 0;
|
2021-08-08 00:19:00 +03:00
|
|
|
|
|
2021-09-03 03:23:16 +03:00
|
|
|
|
if (values[0]) {
|
2021-08-08 00:19:00 +03:00
|
|
|
|
position = sample_ebpf_extension_find(
|
|
|
|
|
context->data_start, context->data_end - context->data_start, values[0], VALUE_SIZE);
|
2021-09-03 03:23:16 +03:00
|
|
|
|
if (values[1]) {
|
|
|
|
|
result = sample_ebpf_extension_replace(
|
|
|
|
|
context->data_start, context->data_end - context->data_start, position, values[1], VALUE_SIZE);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
goto Exit;
|
|
|
|
|
}
|
2021-08-08 00:19:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 03:23:16 +03:00
|
|
|
|
result = sample_ebpf_extension_helper_function1(context);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
goto Exit;
|
|
|
|
|
|
2021-08-08 00:19:00 +03:00
|
|
|
|
// "The answer to the question of life, the universe and everything".
|
|
|
|
|
// - Douglas Adams (The Hitchhiker’s Guide to the Galaxy).
|
|
|
|
|
result = 42;
|
|
|
|
|
Exit:
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-03 03:23:16 +03:00
|
|
|
|
|
|
|
|
|
SEC("maps")
|
|
|
|
|
struct bpf_map utility_map = {
|
|
|
|
|
.size = sizeof(struct bpf_map),
|
|
|
|
|
.type = BPF_MAP_TYPE_ARRAY,
|
|
|
|
|
.key_size = sizeof(uint32_t),
|
|
|
|
|
.value_size = sizeof(ebpf_utility_helpers_data_t),
|
|
|
|
|
.max_entries = UTILITY_MAP_SIZE};
|
|
|
|
|
|
|
|
|
|
SEC("sample_ext/utility")
|
|
|
|
|
int
|
|
|
|
|
test_utility_helpers(sample_program_context_t* context)
|
|
|
|
|
{
|
|
|
|
|
return test_utility_helper_functions(&utility_map);
|
|
|
|
|
}
|