diff mbox

[RFC:,add,openvswitch,actions,using,BPF,5/9] bpf: add the first BPF program.

Message ID 1423090163-19902-6-git-send-email-azhou@nicira.com
State RFC, archived
Delegated to: David Miller
Headers show

Commit Message

Andy Zhou Feb. 4, 2015, 10:49 p.m. UTC
Added a BPF implementation of output action. The intention is to have
all OVS related BPF programs live under the 'bpf' folder.

Signed-off-by: Andy Zhou <azhou@nicira.com>
---
 Makefile.am           |  1 +
 bpf/automake.mk       | 27 +++++++++++++++++++++++++++
 bpf/bpf-shared.h      | 12 ++++++++++++
 bpf/ovs-actions.c     | 13 +++++++++++++
 bpf/ovs-bpf-helpers.h | 35 +++++++++++++++++++++++++++++++++++
 5 files changed, 88 insertions(+)
 create mode 100644 bpf/automake.mk
 create mode 100644 bpf/bpf-shared.h
 create mode 100644 bpf/ovs-actions.c
 create mode 100644 bpf/ovs-bpf-helpers.h

Comments

Thomas Graf Feb. 5, 2015, 2:18 p.m. UTC | #1
First of all, I *love* this. I have some questions on versioning of
helpers in the kernel. I will put comments in the respective kernel
patches directly.

On 02/04/15 at 02:49pm, Andy Zhou wrote:
> +EXTRA_DIST += $(srcdir)/bpf/ovs-bpf-helpers.h \
> +	      $(srcdir)/bpf/bpf-shared.h \
> +	      $(srcdir)/bpf/ovs-actions.c
> +
> +DEP_FILES  = $(srcdir)/bpf/ovs-bpf-helpers.h \
> +	     $(srcdir)/bpf/bpf-shared.h \
> +             $(srcdir)/datapath/linux/compat/include/linux/openvswitch.h

Some tab / space mixing here.

> +bpf/ovs-actions.bpf: $(srcdir)/bpf/ovs-actions.c $(DEP_FILES)
> +	$(AM_V_GEN)clang -DHAVE_CONFIG_H $(BPF_INCLUDES) $(NOSTDINC_FLAGS) \
> +		$(AM_CFLAGS) $(EXTRA_CFLAGS) -Wno-unused-value -Wno-pointer-sign \
> +		-O2 -emit-llvm -c $< -o -| $(LLC) -filetype=obj -o $@

I assume you will convert this to a Makefile template
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andy Zhou Feb. 5, 2015, 7:16 p.m. UTC | #2
On Thu, Feb 5, 2015 at 6:18 AM, Thomas Graf <tgraf@noironetworks.com> wrote:
> First of all, I *love* this. I have some questions on versioning of
> helpers in the kernel. I will put comments in the respective kernel
> patches directly.
>
Thanks for the feedbacks.
> On 02/04/15 at 02:49pm, Andy Zhou wrote:
>> +EXTRA_DIST += $(srcdir)/bpf/ovs-bpf-helpers.h \
>> +           $(srcdir)/bpf/bpf-shared.h \
>> +           $(srcdir)/bpf/ovs-actions.c
>> +
>> +DEP_FILES  = $(srcdir)/bpf/ovs-bpf-helpers.h \
>> +          $(srcdir)/bpf/bpf-shared.h \
>> +             $(srcdir)/datapath/linux/compat/include/linux/openvswitch.h
>
> Some tab / space mixing here.
Will fix.
>
>> +bpf/ovs-actions.bpf: $(srcdir)/bpf/ovs-actions.c $(DEP_FILES)
>> +     $(AM_V_GEN)clang -DHAVE_CONFIG_H $(BPF_INCLUDES) $(NOSTDINC_FLAGS) \
>> +             $(AM_CFLAGS) $(EXTRA_CFLAGS) -Wno-unused-value -Wno-pointer-sign \
>> +             -O2 -emit-llvm -c $< -o -| $(LLC) -filetype=obj -o $@
>
> I assume you will convert this to a Makefile template
Yes. It needs to be addressed.  Not sure if additional changes are
required now that
BPF backend is merged into LLVM upstream,
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/Makefile.am b/Makefile.am
index ee8fe03..dce9056 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -354,6 +354,7 @@  dist-docs:
 
 include m4/automake.mk
 include lib/automake.mk
+include bpf/automake.mk
 include ofproto/automake.mk
 include utilities/automake.mk
 include tests/automake.mk
diff --git a/bpf/automake.mk b/bpf/automake.mk
new file mode 100644
index 0000000..20fc776
--- /dev/null
+++ b/bpf/automake.mk
@@ -0,0 +1,27 @@ 
+# Copyright (C) 2015 Nicira, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.  This file is offered as-is,
+# without warranty of any kind.
+
+if LINUX
+sbin_PROGRAMS += bpf/ovs-actions.bpf
+
+
+EXTRA_DIST += $(srcdir)/bpf/ovs-bpf-helpers.h \
+	      $(srcdir)/bpf/bpf-shared.h \
+	      $(srcdir)/bpf/ovs-actions.c
+
+DEP_FILES  = $(srcdir)/bpf/ovs-bpf-helpers.h \
+	     $(srcdir)/bpf/bpf-shared.h \
+             $(srcdir)/datapath/linux/compat/include/linux/openvswitch.h
+
+BPF_INCLUDES=-I. -I$(srcdir)/datapath/linux/compat/include -I/usr/include
+
+bpf/ovs-actions.bpf: $(srcdir)/bpf/ovs-actions.c $(DEP_FILES)
+	$(AM_V_GEN)clang -DHAVE_CONFIG_H $(BPF_INCLUDES) $(NOSTDINC_FLAGS) \
+		$(AM_CFLAGS) $(EXTRA_CFLAGS) -Wno-unused-value -Wno-pointer-sign \
+		-O2 -emit-llvm -c $< -o -| $(LLC) -filetype=obj -o $@
+
+endif
diff --git a/bpf/bpf-shared.h b/bpf/bpf-shared.h
new file mode 100644
index 0000000..a710c3c
--- /dev/null
+++ b/bpf/bpf-shared.h
@@ -0,0 +1,12 @@ 
+
+/* Shared data structures between bpf and C programs. */
+
+/* a helper structure used by eBPF C program
+ * to describe map attributes to elf_bpf loader
+ */
+struct bpf_map_def {
+	unsigned int type;
+	unsigned int key_size;
+	unsigned int value_size;
+	unsigned int max_entries;
+};
diff --git a/bpf/ovs-actions.c b/bpf/ovs-actions.c
new file mode 100644
index 0000000..05a0a71
--- /dev/null
+++ b/bpf/ovs-actions.c
@@ -0,0 +1,13 @@ 
+#include <config.h>
+#include "ovs-bpf-helpers.h"
+
+int output_action(struct ovs_bpf_action_ctxt *ctxt);
+
+SEC("ovs/output")
+int
+output_action(struct ovs_bpf_action_ctxt *ctxt)
+{
+	return ovs_bpf_helper_output(ctxt->skb, ctxt->arg0);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/bpf/ovs-bpf-helpers.h b/bpf/ovs-bpf-helpers.h
new file mode 100644
index 0000000..ba10084
--- /dev/null
+++ b/bpf/ovs-bpf-helpers.h
@@ -0,0 +1,35 @@ 
+#ifndef __OVS_BPF_HELPERS_H
+#define __OVS_BPF_HELPERS_H
+
+#include <stdint.h>
+#include <linux/openvswitch.h>
+struct sk_buff;
+
+/* helper macro to place programs, maps, license in
+ * different sections in elf_bpf file. Section names
+ * are interpreted by elf_bpf loader
+ */
+#define SEC(NAME) __attribute__((section(NAME), used))
+
+/* helper functions called from eBPF programs written in C */
+static void *(*bpf_map_lookup_elem)(void *map, void *key) =
+	(void *) OVS_BPF_FUNC_map_lookup_elem;
+static int (*bpf_map_update_elem)(void *map, void *key, void *value,
+				  unsigned long long flags) =
+	(void *) OVS_BPF_FUNC_map_update_elem;
+static int (*bpf_map_delete_elem)(void *map, void *key) =
+	(void *) OVS_BPF_FUNC_map_delete_elem;
+static int (*ovs_bpf_helper_output)(struct sk_buff *skb, uint32_t out_port) =
+	(void *) OVS_BPF_FUNC_output;
+
+/* llvm builtin functions that eBPF C program may use to
+ * emit BPF_LD_ABS and BPF_LD_IND instructions
+ */
+unsigned long long load_byte(void *skb,
+			     unsigned long long off) asm("llvm.bpf.load.byte");
+unsigned long long load_half(void *skb,
+			     unsigned long long off) asm("llvm.bpf.load.half");
+unsigned long long load_word(void *skb,
+			     unsigned long long off) asm("llvm.bpf.load.word");
+
+#endif