Signed-off-by: Alan Jowett <alanjo@microsoft.com>
This commit is contained in:
Alan Jowett 2022-02-22 18:01:54 -07:00 коммит произвёл GitHub
Родитель 407b57fb01
Коммит 1dafb123d6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 42 добавлений и 17 удалений

Просмотреть файл

@ -46,7 +46,9 @@ extern "C"
typedef struct _program_entry
{
uint64_t (*function)(void*);
const char* name;
const char* section_name;
const char* function_name;
size_t bpf_instruction_count;
} program_entry_t;
typedef struct _metadata_table

Просмотреть файл

@ -11,9 +11,9 @@
#include "bpf2c.h"
extern "C" metadata_table_t bindmonitor;
extern "C" metadata_table_t divide_by_zero;
extern "C" metadata_table_t droppacket;
extern "C" metadata_table_t bindmonitor_metadata_table;
extern "C" metadata_table_t divide_by_zero_metadata_table;
extern "C" metadata_table_t droppacket_metadata_table;
BOOL APIENTRY
DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
@ -38,7 +38,7 @@ division_by_zero(uint32_t address)
#define FIND_METADATA_ENTRTY(NAME, X) \
if (std::string(NAME) == #X) \
return &X;
return &X##_metadata_table;
extern "C" metadata_table_t*
get_metadata_table(const char* name)

Просмотреть файл

@ -14,8 +14,8 @@ extern "C"
#include "bpf2c.h"
}
#if !defined(SECTION)
#define C_NAME test
#if !defined(C_NAME)
#define C_NAME test_metadata_table
#endif
extern "C" metadata_table_t C_NAME;

Просмотреть файл

@ -1714,14 +1714,14 @@ TEST_CASE("bpf2c_droppacket", "[bpf2c]")
// Test that we drop the packet and increment the map
xdp_md_t ctx{packet.data(), packet.data() + packet.size()};
REQUIRE(table.invoke("xdp", &ctx) == XDP_DROP);
REQUIRE(table.invoke("DropPacket", &ctx) == XDP_DROP);
REQUIRE(bpf_map_lookup_elem(table.get_map("dropped_packet_map"), &key, &value) == 0);
REQUIRE(value == 1);
packet = prepare_udp_packet(10, ETHERNET_TYPE_IPV4);
xdp_md_t ctx2{packet.data(), packet.data() + packet.size()};
REQUIRE(table.invoke("xdp", &ctx2) == XDP_PASS);
REQUIRE(table.invoke("DropPacket", &ctx2) == XDP_PASS);
REQUIRE(bpf_map_lookup_elem(table.get_map("dropped_packet_map"), &key, &value) == 0);
REQUIRE(value == 1);
}
@ -1736,7 +1736,7 @@ TEST_CASE("bpf2c_divide_by_zero", "[bpf2c]")
xdp_md_t ctx{packet.data(), packet.data() + packet.size()};
// Verify the program doesn't crash
REQUIRE(table.invoke("xdp", &ctx) == 0);
REQUIRE(table.invoke("divide_by_zero", &ctx) == 0);
}
TEST_CASE("bpf2c_bindmonitor", "[bpf2c]")
@ -1755,7 +1755,7 @@ TEST_CASE("bpf2c_bindmonitor", "[bpf2c]")
set_bind_limit(limit_map_fd, 2);
std::function<ebpf_result_t(void*, int*)> invoke = [&table](void* context, int* result) -> ebpf_result_t {
*result = static_cast<int>(table.invoke("bind", context));
*result = static_cast<int>(table.invoke("BindMonitor", context));
return EBPF_SUCCESS;
};
// Bind first port - success

Просмотреть файл

@ -44,7 +44,7 @@ dll_metadata_table::dll_metadata_table(const std::string& dll_name, const std::s
size_t count;
table->programs(&programs, &count);
for (size_t i = 0; i < count; i++) {
loaded_programs[programs->name] = programs->function;
loaded_programs[programs->function_name] = programs->function;
}
}

Просмотреть файл

@ -173,6 +173,25 @@ bpf_code_generator::extract_program(const std::string& section_name)
reinterpret_cast<const ebpf_inst*>(program_section->get_data()),
reinterpret_cast<const ebpf_inst*>(program_section->get_data() + program_section->get_size())};
ELFIO::const_symbol_section_accessor symbols{reader, reader.sections[".symtab"]};
for (ELFIO::Elf_Xword index = 0; index < symbols.get_symbols_num(); index++) {
std::string name{};
ELFIO::Elf64_Addr value{};
ELFIO::Elf_Xword size{};
unsigned char bind{};
unsigned char symbol_type{};
ELFIO::Elf_Half section_index{};
unsigned char other{};
symbols.get_symbol(index, name, value, size, bind, symbol_type, section_index, other);
if (name.empty()) {
continue;
}
if (section_index == program_section->get_index() && value == 0) {
current_section->function_name = name;
break;
}
}
uint32_t offset = 0;
for (const auto& instruction : program) {
current_section->output.push_back({instruction, offset++});
@ -627,9 +646,9 @@ bpf_code_generator::emit_c_code(std::ostream& output_stream)
}
for (auto& [name, section] : sections) {
auto function_name = !section.function_name.empty() ? section.function_name : name;
// Emit entry point
output_stream << format_string("static uint64_t _%s_program_entry(void* context)", sanitize_name(name))
<< std::endl;
output_stream << format_string("static uint64_t %s(void* context)", sanitize_name(function_name)) << std::endl;
output_stream << "{" << std::endl;
// Emit prologue
@ -685,8 +704,10 @@ bpf_code_generator::emit_c_code(std::ostream& output_stream)
output_stream << "static program_entry_t _programs[] = {" << std::endl;
for (auto& [name, program] : sections) {
output_stream << "\t{ " << format_string("_%s_program_entry", sanitize_name(name)) << ", "
<< "\"" << name.c_str() << "\"}," << std::endl;
auto function_name = !program.function_name.empty() ? program.function_name : name;
output_stream << "\t{ " << sanitize_name(function_name) << ", "
<< "\"" << name.c_str() << "\", "
<< "\"" << program.function_name.c_str() << "\", " << program.output.size() << "}," << std::endl;
}
output_stream << "};" << std::endl;
output_stream << std::endl;
@ -699,7 +720,8 @@ bpf_code_generator::emit_c_code(std::ostream& output_stream)
output_stream << std::endl;
output_stream << format_string(
"metadata_table_t %s = { _get_programs, _get_maps, _get_helpers };\n", c_name.c_str());
"metadata_table_t %s = { _get_programs, _get_maps, _get_helpers };\n",
c_name.c_str() + std::string("_metadata_table"));
}
std::string

Просмотреть файл

@ -92,6 +92,7 @@ class bpf_code_generator
{
std::vector<output_instruction_t> output;
std::set<std::string> referenced_registers;
std::string function_name;
} section_t;
/**