check only the critical attributes for eBPF maps
@@ -1645,6 +1645,16 @@ static int return_map_fd = -1; // for h2o_return
int h2o_socket_ebpf_setup(void)
{
+ const struct {
+ int type;
+ uint32_t key_size;
+ uint32_t value_size;
+ } map_attr = {
+ .type = BPF_MAP_TYPE_LRU_HASH,
+ .key_size = sizeof(pid_t),
+ .value_size = sizeof(uint64_t),
+ };
+
int success = 0;
int fd = -1;
if (getuid() != 0) {
@@ -1654,7 +1664,7 @@ int h2o_socket_ebpf_setup(void)
if (!file_exist(H2O_EBPF_RETURN_MAP_PATH)) {
// creates a map and pinns the map to BPF fs
- fd = ebpf_map_create(BPF_MAP_TYPE_LRU_HASH, sizeof(pid_t), sizeof(uint64_t), H2O_EBPF_RETURN_MAP_SIZE,
+ fd = ebpf_map_create(map_attr.type, map_attr.key_size, map_attr.value_size, H2O_EBPF_RETURN_MAP_SIZE,
H2O_EBPF_RETURN_MAP_NAME);
if (fd < 0) {
if (errno == EPERM) {
@@ -1682,19 +1692,24 @@ int h2o_socket_ebpf_setup(void)
h2o_perror("BPF_MAP_CREATE failed");
goto Exit;
}
- // make sure the map type and map name are as expected
- struct bpf_map_info map_info;
- if (ebpf_obj_get_info_by_fd(fd, &map_info) != 0) {
+ // make sure the critical attributes (type, key size, value size) are correct.
+ // otherwise usdt-selective-tracing does not work.
+ struct bpf_map_info m;
+ if (ebpf_obj_get_info_by_fd(fd, &m) != 0) {
h2o_perror("BPF_OBJ_GET_INFO_BY_FD failed");
goto Exit;
}
- if (map_info.type != BPF_MAP_TYPE_LRU_HASH) {
- h2o_error_printf("Unexpected map type: expected BPF_MAP_TYPE_LRU_HASH (%d) but got %d\n", BPF_MAP_TYPE_LRU_HASH,
- map_info.type);
+ if (m.type != map_attr.type) {
+ h2o_error_printf("Unexpected map type: expected %d but got %d\n", map_attr.type, m.type);
+ goto Exit;
+ }
+ if (m.key_size != map_attr.key_size) {
+ h2o_error_printf("Unexpected map key size: expected %" PRIu32 " but got %" PRIu32 "\n", map_attr.key_size, m.key_size);
goto Exit;
}
- if (!h2o_memis(map_info.name, strlen(map_info.name), H2O_EBPF_RETURN_MAP_NAME, strlen(H2O_EBPF_RETURN_MAP_NAME))) {
- h2o_error_printf("Unexpected map name: expected %s but got %s\n", H2O_EBPF_RETURN_MAP_NAME, map_info.name);
+ if (m.key_size != map_attr.key_size) {
+ h2o_error_printf("Unexpected map value size: expected %" PRIu32 " but got %" PRIu32 "\n", map_attr.value_size,
+ m.key_size);
goto Exit;
}
success = 1;
