net/bnxt: add locks in flow database
authorKishore Padmanabha <kishore.padmanabha@broadcom.com>
Fri, 11 Sep 2020 01:56:01 +0000 (18:56 -0700)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 18 Sep 2020 16:55:12 +0000 (18:55 +0200)
Added support for mutex protection for the flow database to prevent
simultaneous access to flow database and protect flow creation and
deletion.

Signed-off-by: Kishore Padmanabha <kishore.padmanabha@broadcom.com>
Reviewed-by: Mike Baucom <michael.baucom@broadcom.com>
Reviewed-by: Shahaji Bhosle <sbhosle@broadcom.com>
Reviewed-by: Ajit Khaparde <ajit.khaparde@broadcom.com>
drivers/net/bnxt/tf_ulp/bnxt_ulp.c
drivers/net/bnxt/tf_ulp/bnxt_ulp.h
drivers/net/bnxt/tf_ulp/ulp_flow_db.c
drivers/net/bnxt/tf_ulp/ulp_mapper.c

index 364853a..e8927f6 100644 (file)
@@ -727,6 +727,9 @@ bnxt_ulp_deinit(struct bnxt *bp,
                                         TF_TUNNEL_ENCAP_NAT,
                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
 
+       /* free the flow db lock */
+       pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
+
        /* Delete the ulp context and tf session and free the ulp context */
        ulp_ctx_deinit(bp, session);
        BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
@@ -750,6 +753,12 @@ bnxt_ulp_init(struct bnxt *bp,
                goto jump_to_error;
        }
 
+       rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
+       if (rc) {
+               BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
+               goto jump_to_error;
+       }
+
        /* Initialize ulp dparms with values devargs passed */
        rc = ulp_dparms_init(bp, bp->ulp_ctx);
        if (rc) {
@@ -1235,3 +1244,27 @@ bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
 
        return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
 }
+
+/* Function to acquire the flow database lock from the ulp context. */
+int32_t
+bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context        *ulp_ctx)
+{
+       if (!ulp_ctx || !ulp_ctx->cfg_data)
+               return -1;
+
+       if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
+               BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
+               return -1;
+       }
+       return 0;
+}
+
+/* Function to release the flow database lock from the ulp context. */
+void
+bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context        *ulp_ctx)
+{
+       if (!ulp_ctx || !ulp_ctx->cfg_data)
+               return;
+
+       pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);
+}
index ed97873..36405ae 100644 (file)
@@ -47,6 +47,7 @@ struct bnxt_ulp_data {
        uint32_t                        dev_id; /* Hardware device id */
        uint32_t                        ref_cnt;
        struct bnxt_ulp_flow_db         *flow_db;
+       pthread_mutex_t                 flow_db_lock;
        void                            *mapper_data;
        struct bnxt_ulp_port_db         *port_db;
        struct bnxt_ulp_fc_info         *fc_info;
@@ -196,4 +197,10 @@ struct bnxt_ulp_vfr_rule_info*
 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
                                     uint32_t port_id);
 
+int32_t
+bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context        *ulp_ctx);
+
+void
+bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context        *ulp_ctx);
+
 #endif /* _BNXT_ULP_H_ */
index cbdf5df..9a2d375 100644 (file)
@@ -793,10 +793,17 @@ int32_t   ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
                BNXT_TF_DBG(ERR, "Flow database not found\n");
                return -EINVAL;
        }
+       if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
+               BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
+               return -EINVAL;
+       }
+
        flow_tbl = &flow_db->flow_tbl[idx];
        while (!ulp_flow_db_next_entry_get(flow_tbl, &fid))
                ulp_mapper_resources_free(ulp_ctx, fid, idx);
 
+       bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
+
        return 0;
 }
 
@@ -826,13 +833,17 @@ ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
                BNXT_TF_DBG(ERR, "Flow database not found\n");
                return -EINVAL;
        }
+       if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
+               BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
+               return -EINVAL;
+       }
        flow_tbl = &flow_db->flow_tbl[BNXT_ULP_REGULAR_FLOW_TABLE];
        while (!ulp_flow_db_next_entry_get(flow_tbl, &flow_id)) {
                if (flow_db->func_id_tbl[flow_id] == func_id)
                        ulp_mapper_resources_free(ulp_ctx, flow_id,
                                                  BNXT_ULP_REGULAR_FLOW_TABLE);
        }
-
+       bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
        return 0;
 }
 
index 7321411..85ae3b5 100644 (file)
@@ -2668,12 +2668,21 @@ int32_t
 ulp_mapper_flow_destroy(struct bnxt_ulp_context        *ulp_ctx, uint32_t fid,
                        enum bnxt_ulp_flow_db_tables flow_tbl_type)
 {
+       int32_t rc;
+
        if (!ulp_ctx) {
                BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
                return -EINVAL;
        }
+       if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
+               BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
+               return -EINVAL;
+       }
+
+       rc = ulp_mapper_resources_free(ulp_ctx, fid, flow_tbl_type);
+       bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
+       return rc;
 
-       return ulp_mapper_resources_free(ulp_ctx, fid, flow_tbl_type);
 }
 
 /* Function to handle the default global templates that are allocated during
@@ -2838,6 +2847,12 @@ ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
                return -EINVAL;
        }
 
+       /* Protect flow creation */
+       if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
+               BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
+               return -EINVAL;
+       }
+
        /* Allocate a Flow ID for attaching all resources for the flow to.
         * Once allocated, all errors have to walk the list of resources and
         * free each of them.
@@ -2848,6 +2863,7 @@ ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
                                   &parms.fid);
        if (rc) {
                BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
+               bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
                return rc;
        }
 
@@ -2871,10 +2887,12 @@ ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
        }
 
        *flowid = parms.fid;
+       bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
 
        return rc;
 
 flow_error:
+       bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
        /* Free all resources that were allocated during flow creation */
        trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid,
                                      BNXT_ULP_REGULAR_FLOW_TABLE);