Skip to content

Commit

Permalink
tools: bpf: add bpftool
Browse files Browse the repository at this point in the history
Add a simple tool for querying and updating BPF objects on the system.

Signed-off-by: Jakub Kicinski <[email protected]>
Reviewed-by: Simon Horman <[email protected]>
Acked-by: Daniel Borkmann <[email protected]>
Acked-by: Alexei Starovoitov <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Jakub Kicinski authored and davem330 committed Oct 5, 2017
1 parent a92bb54 commit 71bb428
Show file tree
Hide file tree
Showing 8 changed files with 1,909 additions and 3 deletions.
18 changes: 15 additions & 3 deletions tools/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ prefix = /usr
CC = gcc
LEX = flex
YACC = bison
MAKE = make

CFLAGS += -Wall -O2
CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
Expand All @@ -13,7 +14,7 @@ CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
%.lex.c: %.l
$(LEX) -o $@ $<

all : bpf_jit_disasm bpf_dbg bpf_asm
all: bpf_jit_disasm bpf_dbg bpf_asm bpftool

bpf_jit_disasm : CFLAGS += -DPACKAGE='bpf_jit_disasm'
bpf_jit_disasm : LDLIBS = -lopcodes -lbfd -ldl
Expand All @@ -26,10 +27,21 @@ bpf_asm : LDLIBS =
bpf_asm : bpf_asm.o bpf_exp.yacc.o bpf_exp.lex.o
bpf_exp.lex.o : bpf_exp.yacc.c

clean :
clean: bpftool_clean
rm -rf *.o bpf_jit_disasm bpf_dbg bpf_asm bpf_exp.yacc.* bpf_exp.lex.*

install :
install: bpftool_install
install bpf_jit_disasm $(prefix)/bin/bpf_jit_disasm
install bpf_dbg $(prefix)/bin/bpf_dbg
install bpf_asm $(prefix)/bin/bpf_asm

bpftool:
$(MAKE) -C bpftool

bpftool_install:
$(MAKE) -C bpftool install

bpftool_clean:
$(MAKE) -C bpftool clean

.PHONY: bpftool FORCE
80 changes: 80 additions & 0 deletions tools/bpf/bpftool/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
include ../../scripts/Makefile.include

include ../../scripts/utilities.mak

ifeq ($(srctree),)
srctree := $(patsubst %/,%,$(dir $(CURDIR)))
srctree := $(patsubst %/,%,$(dir $(srctree)))
srctree := $(patsubst %/,%,$(dir $(srctree)))
#$(info Determined 'srctree' to be $(srctree))
endif

ifneq ($(objtree),)
#$(info Determined 'objtree' to be $(objtree))
endif

ifneq ($(OUTPUT),)
#$(info Determined 'OUTPUT' to be $(OUTPUT))
# Adding $(OUTPUT) as a directory to look for source files,
# because use generated output files as sources dependency
# for flex/bison parsers.
VPATH += $(OUTPUT)
export VPATH
endif

ifeq ($(V),1)
Q =
else
Q = @
endif

BPF_DIR = $(srctree)/tools/lib/bpf/

ifneq ($(OUTPUT),)
BPF_PATH=$(OUTPUT)
else
BPF_PATH=$(BPF_DIR)
endif

LIBBPF = $(BPF_PATH)libbpf.a

$(LIBBPF): FORCE
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) $(OUTPUT)libbpf.a FEATURES_DUMP=$(FEATURE_DUMP_EXPORT)

$(LIBBPF)-clean:
$(call QUIET_CLEAN, libbpf)
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null

prefix = /usr

CC = gcc

CFLAGS += -O2
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow
CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf
LIBS = -lelf -lbfd -lopcodes $(LIBBPF)

include $(wildcard *.d)

all: $(OUTPUT)bpftool

SRCS=$(wildcard *.c)
OBJS=$(patsubst %.c,$(OUTPUT)%.o,$(SRCS))

$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

$(OUTPUT)%.o: %.c
$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<

clean: $(LIBBPF)-clean
$(call QUIET_CLEAN, bpftool)
$(Q)rm -rf $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d

install:
install $(OUTPUT)bpftool $(prefix)/sbin/bpftool

FORCE:

.PHONY: all clean FORCE
.DEFAULT_GOAL := all
216 changes: 216 additions & 0 deletions tools/bpf/bpftool/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* Copyright (C) 2017 Netronome Systems, Inc.
*
* This software is dual licensed under the GNU General License Version 2,
* June 1991 as shown in the file COPYING in the top-level directory of this
* source tree or the BSD 2-Clause License provided below. You have the
* option to license this software under the complete terms of either license.
*
* The BSD 2-Clause License:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/* Author: Jakub Kicinski <[email protected]> */

#include <errno.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/limits.h>
#include <linux/magic.h>
#include <sys/types.h>
#include <sys/vfs.h>

#include <bpf.h>

#include "main.h"

static bool is_bpffs(char *path)
{
struct statfs st_fs;

if (statfs(path, &st_fs) < 0)
return false;

return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
}

int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
{
enum bpf_obj_type type;
int fd;

fd = bpf_obj_get(path);
if (fd < 0) {
err("bpf obj get (%s): %s\n", path,
errno == EACCES && !is_bpffs(dirname(path)) ?
"directory not in bpf file system (bpffs)" :
strerror(errno));
return -1;
}

type = get_fd_type(fd);
if (type < 0) {
close(fd);
return type;
}
if (type != exp_type) {
err("incorrect object type: %s\n", get_fd_type_name(type));
close(fd);
return -1;
}

return fd;
}

int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
{
unsigned int id;
char *endptr;
int err;
int fd;

if (!is_prefix(*argv, "id")) {
err("expected 'id' got %s\n", *argv);
return -1;
}
NEXT_ARG();

id = strtoul(*argv, &endptr, 0);
if (*endptr) {
err("can't parse %s as ID\n", *argv);
return -1;
}
NEXT_ARG();

if (argc != 1)
usage();

fd = get_fd_by_id(id);
if (fd < 0) {
err("can't get prog by id (%u): %s\n", id, strerror(errno));
return -1;
}

err = bpf_obj_pin(fd, *argv);
close(fd);
if (err) {
err("can't pin the object (%s): %s\n", *argv,
errno == EACCES && !is_bpffs(dirname(*argv)) ?
"directory not in bpf file system (bpffs)" :
strerror(errno));
return -1;
}

return 0;
}

const char *get_fd_type_name(enum bpf_obj_type type)
{
static const char * const names[] = {
[BPF_OBJ_UNKNOWN] = "unknown",
[BPF_OBJ_PROG] = "prog",
[BPF_OBJ_MAP] = "map",
};

if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
return names[BPF_OBJ_UNKNOWN];

return names[type];
}

int get_fd_type(int fd)
{
char path[PATH_MAX];
char buf[512];
ssize_t n;

snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);

n = readlink(path, buf, sizeof(buf));
if (n < 0) {
err("can't read link type: %s\n", strerror(errno));
return -1;
}
if (n == sizeof(path)) {
err("can't read link type: path too long!\n");
return -1;
}

if (strstr(buf, "bpf-map"))
return BPF_OBJ_MAP;
else if (strstr(buf, "bpf-prog"))
return BPF_OBJ_PROG;

return BPF_OBJ_UNKNOWN;
}

char *get_fdinfo(int fd, const char *key)
{
char path[PATH_MAX];
char *line = NULL;
size_t line_n = 0;
ssize_t n;
FILE *fdi;

snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);

fdi = fopen(path, "r");
if (!fdi) {
err("can't open fdinfo: %s\n", strerror(errno));
return NULL;
}

while ((n = getline(&line, &line_n, fdi))) {
char *value;
int len;

if (!strstr(line, key))
continue;

fclose(fdi);

value = strchr(line, '\t');
if (!value || !value[1]) {
err("malformed fdinfo!?\n");
free(line);
return NULL;
}
value++;

len = strlen(value);
memmove(line, value, len);
line[len - 1] = '\0';

return line;
}

err("key '%s' not found in fdinfo\n", key);
free(line);
fclose(fdi);
return NULL;
}
Loading

0 comments on commit 71bb428

Please sign in to comment.