forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request iovisor#2994 from aspsk/tcpconnect-libbpf
Tcpconnect libbpf
- Loading branch information
Showing
10 changed files
with
830 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ | |
/opensnoop | ||
/runqslower | ||
/syscount | ||
/tcpconnect | ||
/vfsstat | ||
/xfsslower |
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,106 @@ | ||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
// Copyright (c) 2020 Anton Protopopov | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <errno.h> | ||
|
||
#include "map_helpers.h" | ||
|
||
#define warn(...) fprintf(stderr, __VA_ARGS__) | ||
|
||
static bool batch_map_ops = true; /* hope for the best */ | ||
|
||
static int | ||
dump_hash_iter(int map_fd, void *keys, __u32 key_size, | ||
void *values, __u32 value_size, __u32 *count, | ||
void *invalid_key) | ||
{ | ||
__u8 key[key_size], next_key[key_size]; | ||
__u32 n = 0; | ||
int err; | ||
|
||
/* First get keys */ | ||
__builtin_memcpy(key, invalid_key, key_size); | ||
while (n < *count) { | ||
err = bpf_map_get_next_key(map_fd, key, next_key); | ||
if (err && errno != ENOENT) { | ||
return -1; | ||
} else if (err) { | ||
break; | ||
} | ||
__builtin_memcpy(key, next_key, key_size); | ||
__builtin_memcpy(keys + key_size * n, next_key, key_size); | ||
n++; | ||
} | ||
|
||
/* Now read values */ | ||
for (int i = 0; i < n; i++) { | ||
err = bpf_map_lookup_elem(map_fd, keys + key_size * i, | ||
values + value_size * i); | ||
if (err) | ||
return -1; | ||
} | ||
|
||
*count = n; | ||
return 0; | ||
} | ||
|
||
static int | ||
dump_hash_batch(int map_fd, void *keys, __u32 key_size, | ||
void *values, __u32 value_size, __u32 *count) | ||
{ | ||
void *in = NULL, *out; | ||
__u32 n, n_read = 0; | ||
int err = 0; | ||
|
||
while (n_read < *count && !err) { | ||
n = *count - n_read; | ||
err = bpf_map_lookup_batch(map_fd, &in, &out, | ||
keys + n_read * key_size, | ||
values + n_read * value_size, | ||
&n, NULL); | ||
if (err && errno != ENOENT) { | ||
return -1; | ||
} | ||
n_read += n; | ||
in = out; | ||
} | ||
|
||
*count = n_read; | ||
return 0; | ||
} | ||
|
||
int dump_hash(int map_fd, | ||
void *keys, __u32 key_size, | ||
void *values, __u32 value_size, | ||
__u32 *count, void *invalid_key) | ||
{ | ||
int err; | ||
|
||
if (!keys || !values || !count || !key_size || !value_size) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
if (batch_map_ops) { | ||
err = dump_hash_batch(map_fd, keys, key_size, | ||
values, value_size, count); | ||
if (err) { | ||
if (errno != EINVAL) { | ||
return -1; | ||
|
||
/* assume that batch operations are not | ||
* supported and try non-batch mode */ | ||
batch_map_ops = false; | ||
} | ||
} | ||
} | ||
|
||
if (!invalid_key) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
return dump_hash_iter(map_fd, keys, key_size, | ||
values, value_size, count, invalid_key); | ||
} |
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 @@ | ||
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ | ||
/* Copyright (c) 2020 Anton Protopopov */ | ||
#ifndef __MAP_HELPERS_H | ||
#define __MAP_HELPERS_H | ||
|
||
#include <bpf/bpf.h> | ||
|
||
int dump_hash(int map_fd, void *keys, __u32 key_size, | ||
void *values, __u32 value_size, __u32 *count, void *invalid_key); | ||
|
||
#endif /* __MAP_HELPERS_H */ |
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,26 @@ | ||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
// Copyright (c) 2020 Anton Protopopov | ||
#ifndef __MAPS_BPF_H | ||
#define __MAPS_BPF_H | ||
|
||
#include <bpf/bpf_helpers.h> | ||
#include <errno.h> | ||
|
||
static __always_inline void * | ||
bpf_map_lookup_or_try_init(void *map, const void *key, const void *init) | ||
{ | ||
void *val; | ||
long err; | ||
|
||
val = bpf_map_lookup_elem(map, key); | ||
if (val) | ||
return val; | ||
|
||
err = bpf_map_update_elem(map, key, init, BPF_NOEXIST); | ||
if (err && err != -EEXIST) | ||
return 0; | ||
|
||
return bpf_map_lookup_elem(map, key); | ||
} | ||
|
||
#endif /* __MAPS_BPF_H */ |
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
Oops, something went wrong.