ebpf-for-windows/libs/api_common/windows_platform_common.cpp

123 строки
3.8 KiB
C++

// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#include <cassert>
#include <stdexcept>
#include "api_internal.h"
#include "api_common.hpp"
#include "crab_verifier_wrapper.hpp"
#include "ebpf_api.h"
#include "ebpf_nethooks.h"
#include "helpers.hpp"
#include "map_descriptors.hpp"
#include "platform.hpp"
#include "spec_type_descriptors.hpp"
#include "windows_program_type.h"
#include "windows_platform.hpp"
const EbpfProgramType&
get_program_type_windows(const GUID& program_type)
{
// TODO: (Issue #67) Make an IOCTL call to fetch the program context
// info and then fill the EbpfProgramType struct.
for (const EbpfProgramType& t : windows_program_types) {
if (t.platform_specific_data != 0) {
ebpf_program_type_t* program_type_uuid = (ebpf_program_type_t*)t.platform_specific_data;
if (IsEqualGUID(*program_type_uuid, program_type)) {
return t;
}
}
}
return windows_xdp_program_type;
}
EbpfProgramType
get_program_type_windows(const std::string& section, const std::string&)
{
// Check if a global program type is set.
const ebpf_program_type_t* program_type = get_global_program_type();
// TODO: (Issue #223) Read the registry to fetch all the section
// prefixes and corresponding program and attach types.
for (const EbpfProgramType& t : windows_program_types) {
if (program_type != nullptr) {
if (t.platform_specific_data != 0) {
ebpf_program_type_t* program_type_uuid = (ebpf_program_type_t*)t.platform_specific_data;
if (IsEqualGUID(*program_type_uuid, *program_type)) {
return t;
}
}
} else {
for (const std::string prefix : t.section_prefixes) {
if (section.find(prefix) == 0)
return t;
}
}
}
return windows_xdp_program_type;
}
#define BPF_MAP_TYPE(x) BPF_MAP_TYPE_##x, #x
static const EbpfMapType windows_map_types[] = {
{BPF_MAP_TYPE(UNSPEC)},
{BPF_MAP_TYPE(HASH)},
{BPF_MAP_TYPE(ARRAY), true},
{BPF_MAP_TYPE(PROG_ARRAY), true, EbpfMapValueType::PROGRAM},
{BPF_MAP_TYPE(PERCPU_HASH)},
{BPF_MAP_TYPE(PERCPU_ARRAY), true},
{BPF_MAP_TYPE(HASH_OF_MAPS), false, EbpfMapValueType::MAP},
{BPF_MAP_TYPE(ARRAY_OF_MAPS), true, EbpfMapValueType::MAP},
};
EbpfMapType
get_map_type_windows(uint32_t platform_specific_type)
{
uint32_t index = platform_specific_type;
if ((index == 0) || (index >= sizeof(windows_map_types) / sizeof(windows_map_types[0]))) {
return windows_map_types[0];
}
EbpfMapType type = windows_map_types[index];
assert(type.platform_specific_type == platform_specific_type);
return type;
}
EbpfMapDescriptor&
get_map_descriptor_windows(int original_fd)
{
// First check if we already have the map descriptor cached.
EbpfMapDescriptor* map = find_map_descriptor(original_fd);
if (map != nullptr) {
return *map;
}
return get_map_descriptor(original_fd);
}
const ebpf_attach_type_t*
get_attach_type_windows(const std::string& section)
{
// TODO: (Issue #223) Read the registry to fetch all the section
// prefixes and corresponding program and attach types.
for (const ebpf_section_definition_t& t : windows_section_definitions) {
if (section.find(t.section_prefix) == 0)
return t.attach_type;
}
return &EBPF_ATTACH_TYPE_UNSPECIFIED;
}
_Ret_maybenull_z_ const char*
get_attach_type_name(_In_ const ebpf_attach_type_t* attach_type)
{
// TODO: (Issue #223) Read the registry to fetch attach types.
auto it = windows_section_names.find(*attach_type);
if (it != windows_section_names.end())
return it->second;
return nullptr;
}