nfp: bpf: prepare for parsing BPF FW capabilities
authorJakub Kicinski <jakub.kicinski@netronome.com>
Fri, 15 Dec 2017 05:29:16 +0000 (21:29 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 15 Dec 2017 13:18:18 +0000 (14:18 +0100)
BPF FW creates a run time symbol called bpf_capabilities which
contains TLV-formatted capability information.  Allocate app
private structure to store parsed capabilities and add a skeleton
of parsing logic.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
drivers/net/ethernet/netronome/nfp/bpf/main.c
drivers/net/ethernet/netronome/nfp/bpf/main.h
drivers/net/ethernet/netronome/nfp/bpf/offload.c

index 54bfd7846f6d8c1a8165c5ec40384740d3ca3f41..f76bb40c20bc698362cf153b80c76ac293d1e4ba 100644 (file)
@@ -34,6 +34,7 @@
 #include <net/pkt_cls.h>
 
 #include "../nfpcore/nfp_cpp.h"
+#include "../nfpcore/nfp_nffw.h"
 #include "../nfp_app.h"
 #include "../nfp_main.h"
 #include "../nfp_net.h"
@@ -155,10 +156,84 @@ static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
        return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
 }
 
+static int nfp_bpf_parse_capabilities(struct nfp_app *app)
+{
+       struct nfp_cpp *cpp = app->pf->cpp;
+       struct nfp_cpp_area *area;
+       u8 __iomem *mem, *start;
+
+       mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
+                           8, &area);
+       if (IS_ERR(mem))
+               return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
+
+       start = mem;
+       while (mem - start + 8 < nfp_cpp_area_size(area)) {
+               u32 type, length;
+
+               type = readl(mem);
+               length = readl(mem + 4);
+
+               mem += 8 + length;
+               if (mem - start > nfp_cpp_area_size(area))
+                       goto err_release_free;
+
+               switch (type) {
+               default:
+                       nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
+                       break;
+               }
+       }
+       if (mem - start != nfp_cpp_area_size(area)) {
+               nfp_err(cpp, "BPF capabilities left after parsing, parsed:%lu total length:%lu\n",
+                       mem - start, nfp_cpp_area_size(area));
+               goto err_release_free;
+       }
+
+       nfp_cpp_area_release_free(area);
+
+       return 0;
+
+err_release_free:
+       nfp_err(cpp, "invalid BPF capabilities at offset:%ld\n", mem - start);
+       nfp_cpp_area_release_free(area);
+       return -EINVAL;
+}
+
+static int nfp_bpf_init(struct nfp_app *app)
+{
+       struct nfp_app_bpf *bpf;
+       int err;
+
+       bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
+       if (!bpf)
+               return -ENOMEM;
+       bpf->app = app;
+       app->priv = bpf;
+
+       err = nfp_bpf_parse_capabilities(app);
+       if (err)
+               goto err_free_bpf;
+
+       return 0;
+
+err_free_bpf:
+       kfree(bpf);
+       return err;
+}
+
+static void nfp_bpf_clean(struct nfp_app *app)
+{
+       kfree(app->priv);
+}
+
 const struct nfp_app_type app_bpf = {
        .id             = NFP_APP_BPF_NIC,
        .name           = "ebpf",
 
+       .init           = nfp_bpf_init,
+       .clean          = nfp_bpf_clean,
+
        .extra_cap      = nfp_bpf_extra_cap,
 
        .vnic_alloc     = nfp_app_nic_vnic_alloc,
index 5884291ddba52d34edd92e0f8d1e83509bccaa52..a1a3f96353df08b69e10f5e76c0d178035d6f0e9 100644 (file)
@@ -78,6 +78,14 @@ enum pkt_vec {
 #define NFP_BPF_ABI_FLAGS      reg_imm(0)
 #define   NFP_BPF_ABI_FLAG_MARK        1
 
+/**
+ * struct nfp_app_bpf - bpf app priv structure
+ * @app:               backpointer to the app
+ */
+struct nfp_app_bpf {
+       struct nfp_app *app;
+};
+
 struct nfp_prog;
 struct nfp_insn_meta;
 typedef int (*instr_cb_t)(struct nfp_prog *, struct nfp_insn_meta *);
@@ -160,6 +168,7 @@ static inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
 
 /**
  * struct nfp_prog - nfp BPF program
+ * @bpf: backpointer to the bpf app priv structure
  * @prog: machine code
  * @prog_len: number of valid instructions in @prog array
  * @__prog_alloc_len: alloc size of @prog array
@@ -176,6 +185,8 @@ static inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
  * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
  */
 struct nfp_prog {
+       struct nfp_app_bpf *bpf;
+
        u64 *prog;
        unsigned int prog_len;
        unsigned int __prog_alloc_len;
index 377976ce92dd0c8518ebf2d94fc8279e36003afe..fa2905e67b073c3853e3741d7fccecd26586a532 100644 (file)
@@ -48,6 +48,7 @@
 #include <net/tc_act/tc_mirred.h>
 
 #include "main.h"
+#include "../nfp_app.h"
 #include "../nfp_net_ctrl.h"
 #include "../nfp_net.h"
 
@@ -115,6 +116,7 @@ int nfp_bpf_verifier_prep(struct nfp_app *app, struct nfp_net *nn,
 
        INIT_LIST_HEAD(&nfp_prog->insns);
        nfp_prog->type = prog->type;
+       nfp_prog->bpf = app->priv;
 
        ret = nfp_prog_prepare(nfp_prog, prog->insnsi, prog->len);
        if (ret)