cls_bpf no longer takes care of offload tracking.  Make sure
netdevsim performs necessary checks.  This fixes a warning
caused by TC trying to remove a filter it has not added.

Signed-off-by: Jakub Kicinski <[email protected]>
Reviewed-by: Quentin Monnet <[email protected]>
---
Dave, this will have to be applied to net-next as soon as
the previous series comes in via net.  netdevsim doesn't
exist in net/master, so I can't fix it there :(  I'm not
sure how to handle this situation best...
---
 drivers/net/netdevsim/bpf.c                 | 25 ++++++++++++++-----------
 tools/testing/selftests/bpf/test_offload.py |  4 ++--
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/drivers/net/netdevsim/bpf.c b/drivers/net/netdevsim/bpf.c
index 7799942ed349..c977fece64a3 100644
--- a/drivers/net/netdevsim/bpf.c
+++ b/drivers/net/netdevsim/bpf.c
@@ -107,6 +107,7 @@ int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
        struct tc_cls_bpf_offload *cls_bpf = type_data;
        struct bpf_prog *prog = cls_bpf->prog;
        struct netdevsim *ns = cb_priv;
+       struct bpf_prog *oldprog;
 
        if (type != TC_SETUP_CLSBPF ||
            !tc_can_offload(ns->netdev) ||
@@ -114,25 +115,27 @@ int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
            cls_bpf->common.chain_index)
                return -EOPNOTSUPP;
 
-       if (nsim_xdp_offload_active(ns))
-               return -EBUSY;
-
        if (!ns->bpf_tc_accept)
                return -EOPNOTSUPP;
        /* Note: progs without skip_sw will probably not be dev bound */
        if (prog && !prog->aux->offload && !ns->bpf_tc_non_bound_accept)
                return -EOPNOTSUPP;
 
-       switch (cls_bpf->command) {
-       case TC_CLSBPF_REPLACE:
-               return nsim_bpf_offload(ns, prog, true);
-       case TC_CLSBPF_ADD:
-               return nsim_bpf_offload(ns, prog, false);
-       case TC_CLSBPF_DESTROY:
-               return nsim_bpf_offload(ns, NULL, true);
-       default:
+       if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
                return -EOPNOTSUPP;
+
+       oldprog = cls_bpf->oldprog;
+
+       /* Don't remove if oldprog doesn't match driver's state */
+       if (ns->bpf_offloaded != oldprog) {
+               oldprog = NULL;
+               if (!cls_bpf->prog)
+                       return 0;
+               if (ns->bpf_offloaded)
+                       return -EBUSY;
        }
+
+       return nsim_bpf_offload(ns, cls_bpf->prog, oldprog);
 }
 
 int nsim_bpf_disable_tc(struct netdevsim *ns)
diff --git a/tools/testing/selftests/bpf/test_offload.py 
b/tools/testing/selftests/bpf/test_offload.py
index 3914f7a4585a..c940505c2978 100755
--- a/tools/testing/selftests/bpf/test_offload.py
+++ b/tools/testing/selftests/bpf/test_offload.py
@@ -647,8 +647,8 @@ samples = ["sample_ret0.o"]
 
     start_test("Test asking for TC offload of two filters...")
     sim.cls_bpf_add_filter(obj, da=True, skip_sw=True)
-    sim.cls_bpf_add_filter(obj, da=True, skip_sw=True)
-    # The above will trigger a splat until TC cls_bpf drivers are fixed
+    ret, _ = sim.cls_bpf_add_filter(obj, da=True, skip_sw=True, fail=False)
+    fail(ret == 0, "Managed to offload two TC filters at the same time")
 
     sim.tc_flush_filters(bound=2, total=2)
 
-- 
2.15.1

Reply via email to