From patchwork Wed Aug 27 02:29:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 383263 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 975E0140086 for ; Wed, 27 Aug 2014 12:33:45 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932114AbaH0Ca7 (ORCPT ); Tue, 26 Aug 2014 22:30:59 -0400 Received: from mail-pa0-f49.google.com ([209.85.220.49]:52653 "EHLO mail-pa0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756934AbaH0Cay (ORCPT ); Tue, 26 Aug 2014 22:30:54 -0400 Received: by mail-pa0-f49.google.com with SMTP id hz1so24617691pad.22 for ; Tue, 26 Aug 2014 19:30:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Ot6KMrTE9Uja1b0ADhIH12BD1Uxf/cGr2NibjEjFyo4=; b=fM0uQcGoNlnutii2oeofSj6BzoTWqB+6NYa+PlMarKqb5zYLOmElnzFnq9vYM8i4+a 1aLaZmabIv+k+ifS3J7P6Swz58gbv+qnDf0b9X1EmL9M17fey+5eznDtEMqgY+7x8IKD Nj4eQDhlZ+Diblkp/I7uHR/gPPJMsKtXxAbJT99HTpcYu0lTMPCa1haMcObB2LH48o4z eUn+y95UFnMbiY3W3fM8xFuONzm4i0uoHlrV9KE2FqSlnhUeOL6dj0UdPwVeB9L0yQfD YMz0QLi08z1COQEZJPSFSSaubR97H/mTS5OiHp/NDLR0b3J1rtePKjL92GyFzJc0/wFv zWBQ== X-Gm-Message-State: ALoCoQmZTNemVFNCgncAx91++9qHGGPvF9QkaoXOILGc/y4YPBqki447qHnvtQ9G4pow98NlGruH X-Received: by 10.70.128.105 with SMTP id nn9mr42351139pdb.23.1409106653850; Tue, 26 Aug 2014 19:30:53 -0700 (PDT) Received: from pg-vmw-gw1.plumgrid.com ([67.21.3.149]) by mx.google.com with ESMTPSA id xj5sm4755897pbb.7.2014.08.26.19.30.51 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 26 Aug 2014 19:30:52 -0700 (PDT) From: Alexei Starovoitov To: "David S. Miller" Cc: Ingo Molnar , Linus Torvalds , Andy Lutomirski , Steven Rostedt , Daniel Borkmann , Chema Gonzalez , Eric Dumazet , Peter Zijlstra , Brendan Gregg , Namhyung Kim , "H. Peter Anvin" , Andrew Morton , Kees Cook , linux-api@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC v7 net-next 21/28] samples: bpf: add mini eBPF library to manipulate maps and programs Date: Tue, 26 Aug 2014 19:29:35 -0700 Message-Id: <1409106582-10095-22-git-send-email-ast@plumgrid.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1409106582-10095-1-git-send-email-ast@plumgrid.com> References: <1409106582-10095-1-git-send-email-ast@plumgrid.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org the library includes a trivial set of BPF syscall wrappers: int bpf_create_map(int key_size, int value_size, int max_entries); int bpf_update_elem(int fd, void *key, void *value); int bpf_lookup_elem(int fd, void *key, void *value); int bpf_delete_elem(int fd, void *key); int bpf_get_next_key(int fd, void *key, void *next_key); int bpf_prog_load(enum bpf_prog_type prog_type, const struct sock_filter_int *insns, int insn_len, const char *license); bpf_prog_load() stores verifier log into global bpf_log_buf[] array Signed-off-by: Alexei Starovoitov --- samples/bpf/libbpf.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ samples/bpf/libbpf.h | 21 ++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 samples/bpf/libbpf.c create mode 100644 samples/bpf/libbpf.h diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c new file mode 100644 index 000000000000..cae0c734274c --- /dev/null +++ b/samples/bpf/libbpf.c @@ -0,0 +1,89 @@ +/* eBPF mini library */ +#include +#include +#include +#include +#include +#include +#include +#include +#include "libbpf.h" + +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, + int max_entries) +{ + union bpf_attr attr = { + .map_type = map_type, + .key_size = key_size, + .value_size = value_size, + .max_entries = max_entries + }; + + return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); +} + +int bpf_update_elem(int fd, void *key, void *value) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = key, + .value = value, + }; + + return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); +} + +int bpf_lookup_elem(int fd, void *key, void *value) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = key, + .value = value, + }; + + return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); +} + +int bpf_delete_elem(int fd, void *key) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = key, + }; + + return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); +} + +int bpf_get_next_key(int fd, void *key, void *next_key) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = key, + .next_key = next_key, + }; + + return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); +} + +#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u)) + +char bpf_log_buf[LOG_BUF_SIZE]; + +int bpf_prog_load(enum bpf_prog_type prog_type, + const struct bpf_insn *insns, int prog_len, + const char *license) +{ + union bpf_attr attr = { + .prog_type = prog_type, + .insns = insns, + .insn_cnt = prog_len / sizeof(struct bpf_insn), + .license = license, + .log_buf = bpf_log_buf, + .log_size = LOG_BUF_SIZE, + .log_level = 1, + }; + + bpf_log_buf[0] = 0; + + return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); +} diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h new file mode 100644 index 000000000000..b19e39794291 --- /dev/null +++ b/samples/bpf/libbpf.h @@ -0,0 +1,21 @@ +/* eBPF mini library */ +#ifndef __LIBBPF_H +#define __LIBBPF_H + +struct bpf_insn; + +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, + int max_entries); +int bpf_update_elem(int fd, void *key, void *value); +int bpf_lookup_elem(int fd, void *key, void *value); +int bpf_delete_elem(int fd, void *key); +int bpf_get_next_key(int fd, void *key, void *next_key); + +int bpf_prog_load(enum bpf_prog_type prog_type, + const struct bpf_insn *insns, int insn_len, + const char *license); + +#define LOG_BUF_SIZE 8192 +extern char bpf_log_buf[LOG_BUF_SIZE]; + +#endif