Skip to content

Commit

Permalink
bpf: refactor api.h and split into several headers
Browse files Browse the repository at this point in the history
Early spring cleanup to make them more useful and later better
extensible for XDP. Also remove iproute2 header which is not needed
anymore.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
borkmann committed Feb 26, 2020
1 parent 68c2bcf commit 66ca0bd
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 255 deletions.
4 changes: 2 additions & 2 deletions bpf/cilium-map-migrate.c
Expand Up @@ -49,7 +49,7 @@
#include "elf/libelf.h"
#include "elf/gelf.h"

#include "iproute2/bpf_elf.h"
#include "bpf/api.h"

#ifndef EM_BPF
# define EM_BPF 247
Expand Down Expand Up @@ -513,7 +513,7 @@ static int bpf_check_ancillary(struct bpf_elf_ctx *ctx, bpf_handle_state_t cb,
if (ret < 0)
continue;
if (data.sec_hdr.sh_type == SHT_PROGBITS &&
!strcmp(data.sec_name, ELF_SECTION_MAPS))
!strcmp(data.sec_name, "maps"))
ret = bpf_fetch_maps_begin(ctx, i, &data);
else if (data.sec_hdr.sh_type == SHT_SYMTAB &&
!strcmp(data.sec_name, ".symtab"))
Expand Down
246 changes: 39 additions & 207 deletions bpf/include/bpf/api.h
@@ -1,217 +1,49 @@
#ifndef __BPF_API__
#define __BPF_API__

/* Note:
/*
* Copyright (C) 2016-2020 Authors of Cilium
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This file can be included into eBPF kernel programs. It contains
* a couple of useful helper functions, map/section ABI (bpf_elf.h),
* misc macros and some eBPF specific LLVM built-ins.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __BPF_API__
#define __BPF_API__

#include <linux/types.h>
#include <linux/byteorder.h>
#include <linux/if_packet.h>
#include <linux/bpf.h>

#include <iproute2/bpf_elf.h>
#include <linux/if_packet.h>

#include "compiler.h"

#ifndef TC_ACT_OK
#define TC_ACT_OK 0
#define TC_ACT_RECLASSIFY 1
#define TC_ACT_SHOT 2
#define TC_ACT_PIPE 3
#define TC_ACT_STOLEN 4
#define TC_ACT_QUEUED 5
#define TC_ACT_REPEAT 6
#define TC_ACT_REDIRECT 7
#endif

/** Section helper macros. */

#ifndef __section
# define __section(NAME) \
__attribute__((section(NAME), used))
#endif

#ifndef __section_tail
# define __section_tail(ID, KEY) \
__section(__stringify(ID) "/" __stringify(KEY))
#endif

#ifndef __section_cls_entry
# define __section_cls_entry \
__section(ELF_SECTION_CLASSIFIER)
#endif

#ifndef __section_act_entry
# define __section_act_entry \
__section(ELF_SECTION_ACTION)
#endif

#ifndef __section_license
# define __section_license \
__section(ELF_SECTION_LICENSE)
#endif

#ifndef __section_maps
# define __section_maps \
__section(ELF_SECTION_MAPS)
#endif

/** Declaration helper macros. */

#ifndef BPF_LICENSE
# define BPF_LICENSE(NAME) \
char ____license[] __section_license = NAME
#endif

/** Classifier helper */

#ifndef BPF_H_DEFAULT
# define BPF_H_DEFAULT -1
#endif

/** BPF helper functions for tc. Individual flags are in linux/bpf.h */

#ifndef BPF_FUNC
# define BPF_FUNC(NAME, ...) \
(* NAME)(__VA_ARGS__) __maybe_unused = (void *) BPF_FUNC_##NAME
#endif

#ifndef BPF_FUNC2
# define BPF_FUNC2(NAME, ...) \
(* NAME)(__VA_ARGS__) __maybe_unused
#endif

/* Map access/manipulation */
static void *BPF_FUNC(map_lookup_elem, void *map, const void *key);
static int BPF_FUNC(map_update_elem, void *map, const void *key,
const void *value, __u32 flags);
static int BPF_FUNC(map_delete_elem, void *map, const void *key);

/* Time access */
static __u64 BPF_FUNC(ktime_get_ns);

/* Sockets */
static __u64 BPF_FUNC(get_socket_cookie, void *ctx);

/* Debugging */

__attribute__((__format__(__printf__, 1, 3)))
static void BPF_FUNC(trace_printk, const char *fmt, int fmt_size, ...);

#ifndef printt
# define printt(fmt, ...) \
({ \
trace_printk(____fmt, ##__VA_ARGS__); \
})
#endif

/* Random numbers */
static __u32 BPF_FUNC(get_prandom_u32);

/* Tail calls */
static void BPF_FUNC(tail_call, struct __sk_buff *skb, void *map,
__u32 index);

/* System helpers */
static __u32 BPF_FUNC(get_smp_processor_id);

/* Packet misc meta data */
static __u32 BPF_FUNC(get_cgroup_classid, struct __sk_buff *skb);
static __u32 BPF_FUNC(get_route_realm, struct __sk_buff *skb);
static __u32 BPF_FUNC(get_hash_recalc, struct __sk_buff *skb);
static __u32 BPF_FUNC(set_hash_invalid, struct __sk_buff *skb);

static int BPF_FUNC(skb_under_cgroup, void *map, __u32 index);

/* Packet redirection */
static int BPF_FUNC(redirect, int ifindex, __u32 flags);
static int BPF_FUNC(clone_redirect, struct __sk_buff *skb, int ifindex,
__u32 flags);

/* Packet manipulation */
static int BPF_FUNC(skb_load_bytes_relative, struct __sk_buff *skb, __u32 off,
void *to, __u32 len, __u32 hdr);
static int BPF_FUNC(skb_load_bytes, struct __sk_buff *skb, __u32 off,
void *to, __u32 len);
static int BPF_FUNC(skb_store_bytes, struct __sk_buff *skb, __u32 off,
const void *from, __u32 len, __u32 flags);
static int BPF_FUNC(skb_adjust_room, struct __sk_buff *skb, __s32 len_diff,
__u32 mode, __u64 flags);

static int BPF_FUNC(l3_csum_replace, struct __sk_buff *skb, __u32 off,
__u32 from, __u32 to, __u32 flags);
static int BPF_FUNC(l4_csum_replace, struct __sk_buff *skb, __u32 off,
__u32 from, __u32 to, __u32 flags);
static int BPF_FUNC(csum_diff, void *from, __u32 from_size, void *to,
__u32 to_size, __u32 seed);

static int BPF_FUNC(skb_change_type, struct __sk_buff *skb, __u32 type);
static int BPF_FUNC(skb_change_proto, struct __sk_buff *skb, __u32 proto,
__u32 flags);
static int BPF_FUNC(skb_change_tail, struct __sk_buff *skb, __u32 nlen,
__u32 flags);
static int BPF_FUNC(skb_pull_data, struct __sk_buff *skb, __u32 len);

/* Packet vlan encap/decap */
static int BPF_FUNC(skb_vlan_push, struct __sk_buff *skb, __u16 proto,
__u16 vlan_tci);
static int BPF_FUNC(skb_vlan_pop, struct __sk_buff *skb);

/* Packet tunnel encap/decap */
static int BPF_FUNC(skb_get_tunnel_key, struct __sk_buff *skb,
struct bpf_tunnel_key *to, __u32 size, __u32 flags);
static int BPF_FUNC(skb_set_tunnel_key, struct __sk_buff *skb,
const struct bpf_tunnel_key *from, __u32 size,
__u32 flags);

static int BPF_FUNC(skb_get_tunnel_opt, struct __sk_buff *skb,
void *to, __u32 size);
static int BPF_FUNC(skb_set_tunnel_opt, struct __sk_buff *skb,
const void *from, __u32 size);

/* Events for user space */
static int BPF_FUNC2(skb_event_output, struct __sk_buff *skb, void *map, __u64 index,
const void *data, __u32 size) = (void *)BPF_FUNC_perf_event_output;
static int BPF_FUNC2(xdp_event_output, struct xdp_md *xdp, void *map, __u64 index,
const void *data, __u32 size) = (void *)BPF_FUNC_perf_event_output;

/* Sockops and SK_MSG helpers */
static int BPF_FUNC(sock_map_update, struct bpf_sock_ops *skops, void *map, __u32 key, __u64 flags);
static int BPF_FUNC(sock_hash_update, struct bpf_sock_ops *skops, void *map, void *key, __u64 flags);
static int BPF_FUNC(msg_redirect_hash, struct sk_msg_md *md, void *map, void *key, __u64 flags);

static int BPF_FUNC(fib_lookup, void *ctx, struct bpf_fib_lookup *params, __u32 plen, __u32 flags);

/** LLVM built-ins, mem*() routines work for constant size */

#ifndef lock_xadd
# define lock_xadd(ptr, val) ((void) __sync_fetch_and_add(ptr, val))
#endif

#ifndef memset
# define memset(s, c, n) __builtin_memset((s), (c), (n))
#endif

#ifndef memcpy
# define memcpy(d, s, n) __builtin_memcpy((d), (s), (n))
#endif

#ifndef memmove
# define memmove(d, s, n) __builtin_memmove((d), (s), (n))
#endif

/* FIXME: __builtin_memcmp() is not yet fully useable unless llvm bug
* https://llvm.org/bugs/show_bug.cgi?id=26218 gets resolved. Also
* this one would generate a reloc entry (non-map), otherwise.
*/
#if 0
#ifndef memcmp
# define memcmp(a, b, n) __builtin_memcmp((a), (b), (n))
#endif
#endif
#include "section.h"
#include "helpers.h"
#include "builtins.h"

#define PIN_NONE 0
#define PIN_OBJECT_NS 1
#define PIN_GLOBAL_NS 2

struct bpf_elf_map {
__u32 type;
__u32 size_key;
__u32 size_value;
__u32 max_elem;
__u32 flags;
__u32 id;
__u32 pinning;
#ifdef SOCKMAP
__u32 inner_id;
__u32 inner_idx;
#endif
};

#endif /* __BPF_API__ */
44 changes: 44 additions & 0 deletions bpf/include/bpf/builtins.h
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2016-2020 Authors of Cilium
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __BPF_BUILTINS__
#define __BPF_BUILTINS__

#include "compiler.h"

#ifndef lock_xadd
# define lock_xadd(P, V) ((void) __sync_fetch_and_add((P), (V)))
#endif

#ifndef memset
# define memset(S, C, N) __builtin_memset((S), (C), (N))
#endif

#ifndef memcpy
# define memcpy(D, S, N) __builtin_memcpy((D), (S), (N))
#endif

#ifndef memmove
# define memmove(D, S, N) __builtin_memmove((D), (S), (N))
#endif

/* NOTE: https://llvm.org/bugs/show_bug.cgi?id=26218 */
#ifndef memcmp
# define memcmp(A, B, N) __builtin_memcmp((A), (B), (N))
#endif

#endif /* __BPF_BUILTINS__ */
10 changes: 9 additions & 1 deletion bpf/include/bpf/compiler.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Authors of Cilium
* Copyright (C) 2016-2020 Authors of Cilium
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +21,10 @@
#include <stdbool.h>
#include <stddef.h>

#ifndef __section
# define __section(X) __attribute__((section(X), used))
#endif

#ifndef __maybe_unused
# define __maybe_unused __attribute__((__unused__))
#endif
Expand Down Expand Up @@ -56,6 +60,10 @@
# define build_bug_on(E) ((void)sizeof(char[1 - 2*!!(E)]))
#endif

#ifndef __printf
# define __printf(X, Y) __attribute__((__format__(printf, X, Y)))
#endif

static __always_inline void bpf_barrier(void)
{
/* Workaround to avoid verifier complaint:
Expand Down
12 changes: 12 additions & 0 deletions bpf/include/bpf/ctx/skb.h
Expand Up @@ -20,6 +20,18 @@

#include "common.h"

#ifndef TC_ACT_OK
# define TC_ACT_OK 0
#endif

#ifndef TC_ACT_SHOT
# define TC_ACT_SHOT 2
#endif

#ifndef TC_ACT_REDIRECT
# define TC_ACT_REDIRECT 7
#endif

#define __ctx_buff __sk_buff

#define CTX_ACT_OK TC_ACT_OK
Expand Down

0 comments on commit 66ca0bd

Please sign in to comment.