Skip to content

Commit

Permalink
Merge pull request iovisor#2994 from aspsk/tcpconnect-libbpf
Browse files Browse the repository at this point in the history
Tcpconnect libbpf
  • Loading branch information
yonghong-song committed Jul 15, 2020
2 parents 4efe7fe + 3ef7531 commit 2b99f69
Show file tree
Hide file tree
Showing 10 changed files with 830 additions and 18 deletions.
1 change: 1 addition & 0 deletions libbpf-tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
/opensnoop
/runqslower
/syscount
/tcpconnect
/vfsstat
/xfsslower
2 changes: 2 additions & 0 deletions libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ APPS = \
opensnoop \
runqslower \
syscount \
tcpconnect \
vfsstat \
xfsslower \
#
Expand All @@ -26,6 +27,7 @@ COMMON_OBJ = \
$(OUTPUT)/trace_helpers.o \
$(OUTPUT)/syscall_helpers.o \
$(OUTPUT)/errno_helpers.o \
$(OUTPUT)/map_helpers.o \
#

.PHONY: all
Expand Down
106 changes: 106 additions & 0 deletions libbpf-tools/map_helpers.c
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);
}
11 changes: 11 additions & 0 deletions libbpf-tools/map_helpers.h
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 */
26 changes: 26 additions & 0 deletions libbpf-tools/maps.bpf.h
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 */
18 changes: 1 addition & 17 deletions libbpf-tools/syscount.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "syscount.h"
#include "maps.bpf.h"

const volatile bool count_by_process = false;
const volatile bool measure_latency = false;
Expand All @@ -30,23 +31,6 @@ struct {
__uint(map_flags, BPF_F_NO_PREALLOC);
} data SEC(".maps");

static __always_inline
void *bpf_map_lookup_or_try_init(void *map, void *key, const void *init)
{
void *val;
int err;

val = bpf_map_lookup_elem(map, key);
if (val)
return val;

err = bpf_map_update_elem(map, key, init, 0);
if (err)
return (void *) 0;

return bpf_map_lookup_elem(map, key);
}

static __always_inline
void save_proc_name(struct data_t *val)
{
Expand Down
2 changes: 1 addition & 1 deletion libbpf-tools/syscount.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ int main(int argc, char **argv)
}

if (signal(SIGINT, sig_int) == SIG_ERR) {
warn("can't set signal handler: %s\n", strerror(-errno));
warn("can't set signal handler: %s\n", strerror(errno));
goto cleanup_obj;
}

Expand Down
Loading

0 comments on commit 2b99f69

Please sign in to comment.