Blame lib/bpf_glue.c

Packit Bot 867fae
/* SPDX-License-Identifier: GPL-2.0 */
Packit Bot 867fae
/*
Packit Bot 867fae
 * bpf_glue.c:	BPF code to call both legacy and libbpf code
Packit Bot 867fae
 * Authors:	Hangbin Liu <haliu@redhat.com>
Packit Bot 867fae
 *
Packit Bot 867fae
 */
Packit Bot 867fae
#include <limits.h>
Packit Bot 867fae
Packit Bot 867fae
#include "bpf_util.h"
Packit Bot 867fae
#ifdef HAVE_LIBBPF
Packit Bot 867fae
#include <bpf/bpf.h>
Packit Bot 867fae
#endif
Packit Bot 867fae
Packit Bot 867fae
int bpf_program_load(enum bpf_prog_type type, const struct bpf_insn *insns,
Packit Bot 867fae
		     size_t size_insns, const char *license, char *log,
Packit Bot 867fae
		     size_t size_log)
Packit Bot 867fae
{
Packit Bot 867fae
#ifdef HAVE_LIBBPF
Packit Bot 867fae
	return bpf_load_program(type, insns, size_insns / sizeof(struct bpf_insn),
Packit Bot 867fae
				license, 0, log, size_log);
Packit Bot 867fae
#else
Packit Bot 867fae
	return bpf_prog_load_dev(type, insns, size_insns, license, 0, log, size_log);
Packit Bot 867fae
#endif
Packit Bot 867fae
}
Packit Bot 867fae
Packit Bot 867fae
int bpf_program_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
Packit Bot 867fae
{
Packit Bot 867fae
#ifdef HAVE_LIBBPF
Packit Bot 867fae
	return bpf_prog_attach(prog_fd, target_fd, type, 0);
Packit Bot 867fae
#else
Packit Bot 867fae
	return bpf_prog_attach_fd(prog_fd, target_fd, type);
Packit Bot 867fae
#endif
Packit Bot 867fae
}
Packit Bot 867fae
Packit Bot 867fae
#ifdef HAVE_LIBBPF
Packit Bot 867fae
static const char *_libbpf_compile_version = LIBBPF_VERSION;
Packit Bot 867fae
static char _libbpf_version[10] = {};
Packit Bot 867fae
Packit Bot 867fae
const char *get_libbpf_version(void)
Packit Bot 867fae
{
Packit Bot 867fae
	/* Start by copying compile-time version into buffer so we have a
Packit Bot 867fae
	 * fallback value in case we are dynamically linked, or can't find a
Packit Bot 867fae
	 * version in /proc/self/maps below.
Packit Bot 867fae
	 */
Packit Bot 867fae
	strncpy(_libbpf_version, _libbpf_compile_version,
Packit Bot 867fae
		sizeof(_libbpf_version)-1);
Packit Bot 867fae
#ifdef LIBBPF_DYNAMIC
Packit Bot 867fae
	char buf[PATH_MAX], *s;
Packit Bot 867fae
	bool found = false;
Packit Bot 867fae
	FILE *fp;
Packit Bot 867fae
Packit Bot 867fae
	/* When dynamically linking against libbpf, we can't be sure that the
Packit Bot 867fae
	 * version we discovered at compile time is actually the one we are
Packit Bot 867fae
	 * using at runtime. This can lead to hard-to-debug errors, so we try to
Packit Bot 867fae
	 * discover the correct version at runtime.
Packit Bot 867fae
	 *
Packit Bot 867fae
	 * The simple solution to this would be if libbpf itself exported a
Packit Bot 867fae
	 * version in its API. But since it doesn't, we work around this by
Packit Bot 867fae
	 * parsing the mappings of the binary at runtime, looking for the full
Packit Bot 867fae
	 * filename of libbpf.so and using that.
Packit Bot 867fae
	 */
Packit Bot 867fae
	fp = fopen("/proc/self/maps", "r");
Packit Bot 867fae
	if (fp == NULL)
Packit Bot 867fae
		goto out;
Packit Bot 867fae
Packit Bot 867fae
	while ((s = fgets(buf, sizeof(buf), fp)) != NULL) {
Packit Bot 867fae
		if ((s = strstr(buf, "libbpf.so.")) != NULL) {
Packit Bot 867fae
			strncpy(_libbpf_version, s+10, sizeof(_libbpf_version)-1);
Packit Bot 867fae
			strtok(_libbpf_version, "\n");
Packit Bot 867fae
			found = true;
Packit Bot 867fae
			break;
Packit Bot 867fae
		}
Packit Bot 867fae
	}
Packit Bot 867fae
Packit Bot 867fae
	fclose(fp);
Packit Bot 867fae
out:
Packit Bot 867fae
	if (!found)
Packit Bot 867fae
		fprintf(stderr, "Couldn't find runtime libbpf version - falling back to compile-time value!\n");
Packit Bot 867fae
#endif /* LIBBPF_DYNAMIC */
Packit Bot 867fae
Packit Bot 867fae
	_libbpf_version[sizeof(_libbpf_version)-1] = '\0';
Packit Bot 867fae
	return _libbpf_version;
Packit Bot 867fae
}
Packit Bot 867fae
#else
Packit Bot 867fae
const char *get_libbpf_version(void)
Packit Bot 867fae
{
Packit Bot 867fae
	return NULL;
Packit Bot 867fae
}
Packit Bot 867fae
#endif /* HAVE_LIBBPF */