net/tap: fix promiscuous rules double insertions
[dpdk.git] / drivers / net / tap / tap_flow.c
index 18cfb91..551b2d8 100644 (file)
@@ -1,34 +1,6 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright 2017 6WIND S.A.
- *   Copyright 2017 Mellanox.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of 6WIND S.A. nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2017 6WIND S.A.
+ * Copyright 2017 Mellanox.
  */
 
 #include <errno.h>
@@ -151,6 +123,7 @@ enum key_status_e {
 };
 
 #define ISOLATE_HANDLE 1
+#define REMOTE_PROMISCUOUS_HANDLE 2
 
 struct rte_flow {
        LIST_ENTRY(rte_flow) next; /* Pointer to the next rte_flow structure */
@@ -1720,9 +1693,15 @@ int tap_flow_implicit_create(struct pmd_internals *pmd,
         * The ISOLATE rule is always present and must have a static handle, as
         * the action is changed whether the feature is enabled (DROP) or
         * disabled (PASSTHRU).
+        * There is just one REMOTE_PROMISCUOUS rule in all cases. It should
+        * have a static handle such that adding it twice will fail with EEXIST
+        * with any kernel version. Remark: old kernels may falsely accept the
+        * same REMOTE_PROMISCUOUS rules if they had different handles.
         */
        if (idx == TAP_ISOLATE)
                remote_flow->msg.t.tcm_handle = ISOLATE_HANDLE;
+       else if (idx == TAP_REMOTE_PROMISC)
+               remote_flow->msg.t.tcm_handle = REMOTE_PROMISCUOUS_HANDLE;
        else
                tap_flow_set_handle(remote_flow);
        if (priv_flow_process(pmd, attr, items, actions, NULL,
@@ -1737,12 +1716,16 @@ int tap_flow_implicit_create(struct pmd_internals *pmd,
        }
        err = tap_nl_recv_ack(pmd->nlsk_fd);
        if (err < 0) {
+               /* Silently ignore re-entering remote promiscuous rule */
+               if (errno == EEXIST && idx == TAP_REMOTE_PROMISC)
+                       goto success;
                RTE_LOG(ERR, PMD,
                        "Kernel refused TC filter rule creation (%d): %s\n",
                        errno, strerror(errno));
                goto fail;
        }
        LIST_INSERT_HEAD(&pmd->implicit_flows, remote_flow, next);
+success:
        return 0;
 fail:
        if (remote_flow)
@@ -1958,6 +1941,7 @@ static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx)
        static __u32 num_used_keys;
        static __u32 rss_keys[MAX_RSS_KEYS] = {KEY_STAT_UNSPEC};
        static __u32 rss_keys_initialized;
+       __u32 key;
 
        switch (cmd) {
        case KEY_CMD_GET:
@@ -2003,7 +1987,7 @@ static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx)
                 * map index as an out-of-range value and the release operation
                 * will be silently ignored.
                 */
-               __u32 key = *key_idx - KEY_IDX_OFFSET;
+               key = *key_idx - KEY_IDX_OFFSET;
                if (key >= RTE_DIM(rss_keys))
                        break;