Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Libbpf is incorporated as a submodule just for the header file. As discussed in issue #84, we cannot currently use the implementation since it is very Linux and GCC specific. This PR also fixes a bug where the user-mode API was calling CloseHandle directly instead of Platform::CloseHandle which is needed to make tests work with the mock platform. Addresses #84 Some code will be cleaner once issue #81 is done Signed-off-by: Dave Thaler <dthaler@microsoft.com>
- Loading branch information
Showing
18 changed files
with
879 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// // SPDX-License-Identifier: MIT | ||
#pragma once | ||
|
||
// This file is needed since bpf.h includes it. | ||
// It should be thought of as platform/bpf.h not Linux per se. | ||
|
||
#include "ebpf_program_types.h" | ||
#include "ebpf_api.h" | ||
#define LIBBPF_API | ||
#include "libbpf_common.h" | ||
#undef LIBBPF_DEPRECATED | ||
#define LIBBPF_DEPRECATED(x) | ||
|
||
typedef uint8_t __u8; | ||
typedef uint32_t __u32; | ||
typedef uint64_t __u64; | ||
typedef uint32_t pid_t; | ||
|
||
#define bpf_map _ebpf_map | ||
#define bpf_map_type _ebpf_map_type | ||
#define bpf_object _ebpf_object | ||
#define bpf_program _ebpf_program | ||
#define bpf_prog_info _ebpf_program_info | ||
#define BPF_MAP_TYPE_ARRAY EBPF_MAP_TYPE_ARRAY | ||
|
||
enum bpf_prog_type | ||
{ | ||
BPF_PROG_TYPE_UNKNOWN, | ||
BPF_PROG_TYPE_XDP, | ||
}; | ||
|
||
enum bpf_attach_type | ||
{ | ||
BPF_ATTACH_TYPE_UNKNOWN, | ||
BPF_ATTACH_TYPE_XDP, | ||
}; | ||
|
||
enum bpf_func_id | ||
{ | ||
BPF_FUNC_ID_UNKNOWN | ||
}; | ||
|
||
enum bpf_stats_type | ||
{ | ||
BPF_STATS_TYPE_UNKNOWN | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// SPDX-License-Identifier: MIT | ||
#pragma once | ||
|
||
static inline int | ||
libbpf_err(int ret) | ||
{ | ||
if (ret < 0) | ||
errno = -ret; | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "api_internal.h" | ||
#pragma warning(push) | ||
#pragma warning(disable : 4200) | ||
#include "libbpf.h" | ||
#pragma warning(pop) | ||
#include "libbpf_internal.h" | ||
|
||
// This file implements APIs in LibBPF's bpf.h | ||
// and is based on code in libbpf.c. | ||
|
||
struct bpf_map* | ||
bpf_map__next(const struct bpf_map* previous, const struct bpf_object* object) | ||
{ | ||
return ebpf_map_next(previous, object); | ||
} | ||
|
||
struct bpf_map* | ||
bpf_map__prev(const struct bpf_map* next, const struct bpf_object* object) | ||
{ | ||
return ebpf_map_previous(next, object); | ||
} | ||
|
||
int | ||
bpf_map__unpin(struct bpf_map* map, const char* path) | ||
{ | ||
char fullpath[MAX_PATH]; | ||
int length = snprintf(fullpath, sizeof(fullpath), "%s/%s", path, map->name); | ||
if (length < 0) { | ||
return -EINVAL; | ||
} else if (length > sizeof(fullpath)) { | ||
return -ENAMETOOLONG; | ||
} | ||
|
||
uint32_t result = ebpf_api_unpin_object((const uint8_t*)fullpath, (uint32_t)strlen(fullpath)); | ||
if (result) { | ||
return libbpf_err(result); | ||
} | ||
|
||
// TODO(issue #81): ebpf_api_unpin_object should set this. | ||
map->pinned = false; | ||
|
||
return 0; | ||
} | ||
|
||
static void | ||
_unpin_previous_maps(_In_ ebpf_map_t* map, _In_ struct bpf_object* object, const char* path) | ||
{ | ||
for (map = bpf_map__prev(map, object); map; map = bpf_map__prev(map, object)) { | ||
bpf_map__unpin(map, path); | ||
} | ||
} | ||
|
||
int | ||
bpf_map__pin(struct bpf_map* map, const char* path) | ||
{ | ||
char fullpath[MAX_PATH]; | ||
|
||
int length = snprintf(fullpath, sizeof(fullpath), "%s/%s", path, map->name); | ||
if (length < 0) { | ||
return libbpf_err(-EINVAL); | ||
} | ||
|
||
int result = ebpf_api_pin_object(map->map_handle, (const uint8_t*)fullpath, (uint32_t)strlen(fullpath)); | ||
if (result) { | ||
return libbpf_err(result); | ||
} | ||
|
||
// TODO(issue #81): ebpf_api_pin_object should set this. | ||
map->pinned = true; | ||
|
||
return 0; | ||
} | ||
|
||
int | ||
bpf_object__pin_maps(struct bpf_object* object, const char* path) | ||
{ | ||
struct bpf_map* map; | ||
|
||
if (!object) { | ||
return libbpf_err(-ENOENT); | ||
} | ||
|
||
bpf_object__for_each_map(map, object) | ||
{ | ||
int result = bpf_map__pin(map, path); | ||
if (result) { | ||
_unpin_previous_maps(map, object, path); | ||
return result; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int | ||
bpf_object__unpin_maps(struct bpf_object* object, const char* path) | ||
{ | ||
struct bpf_map* map; | ||
|
||
if (!object) { | ||
return libbpf_err(-ENOENT); | ||
} | ||
|
||
bpf_object__for_each_map(map, object) | ||
{ | ||
int result = bpf_map__unpin(map, path); | ||
if (result) { | ||
return libbpf_err(result); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
enum bpf_map_type | ||
bpf_map__type(const struct bpf_map* map) | ||
{ | ||
return map->map_definition.type; | ||
} | ||
|
||
__u32 | ||
bpf_map__key_size(const struct bpf_map* map) | ||
{ | ||
return map->map_definition.key_size; | ||
} | ||
|
||
__u32 | ||
bpf_map__value_size(const struct bpf_map* map) | ||
{ | ||
return map->map_definition.value_size; | ||
} | ||
|
||
__u32 | ||
bpf_map__max_entries(const struct bpf_map* map) | ||
{ | ||
return map->map_definition.max_entries; | ||
} | ||
|
||
bool | ||
bpf_map__is_pinned(const struct bpf_map* map) | ||
{ | ||
return map->pinned; | ||
} | ||
|
||
int | ||
bpf_map__fd(const struct bpf_map* map) | ||
{ | ||
return map ? map->map_fd : libbpf_err(-EINVAL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "api_internal.h" | ||
#pragma warning(push) | ||
#pragma warning(disable : 4200) | ||
#include "libbpf.h" | ||
#pragma warning(pop) | ||
#include "libbpf_internal.h" | ||
|
||
// This file implements APIs in LibBPF's bpf.h | ||
// and is based on code in libbpf.c. | ||
|
||
const char* | ||
bpf_object__name(const struct bpf_object* object) | ||
{ | ||
return object->file_name; | ||
} | ||
|
||
int | ||
bpf_object__pin(struct bpf_object* obj, const char* path) | ||
{ | ||
int err; | ||
|
||
err = bpf_object__pin_maps(obj, path); | ||
if (err) | ||
return libbpf_err(err); | ||
|
||
err = bpf_object__pin_programs(obj, path); | ||
if (err) { | ||
bpf_object__unpin_maps(obj, path); | ||
return libbpf_err(err); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void | ||
bpf_object__close(struct bpf_object* object) | ||
{ | ||
ebpf_object_close(object); | ||
} | ||
|
||
struct bpf_program* | ||
bpf_object__find_program_by_name(const struct bpf_object* object, const char* name) | ||
{ | ||
struct bpf_program* program; | ||
|
||
bpf_object__for_each_program(program, object) | ||
{ | ||
if (!strcmp(program->program_name, name)) | ||
return program; | ||
} | ||
errno = ENOENT; | ||
return nullptr; | ||
} |
Oops, something went wrong.