From 6c7cbc8077dd25abd6baf264cd598252ecd9fb3b Mon Sep 17 00:00:00 2001 From: Kishore Padmanabha Date: Thu, 10 Sep 2020 18:56:01 -0700 Subject: [PATCH] net/bnxt: add locks in flow database 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 Reviewed-by: Mike Baucom Reviewed-by: Shahaji Bhosle Reviewed-by: Ajit Khaparde --- drivers/net/bnxt/tf_ulp/bnxt_ulp.c | 33 +++++++++++++++++++++++++++ drivers/net/bnxt/tf_ulp/bnxt_ulp.h | 7 ++++++ drivers/net/bnxt/tf_ulp/ulp_flow_db.c | 13 ++++++++++- drivers/net/bnxt/tf_ulp/ulp_mapper.c | 20 +++++++++++++++- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/drivers/net/bnxt/tf_ulp/bnxt_ulp.c b/drivers/net/bnxt/tf_ulp/bnxt_ulp.c index 364853a6e5..e8927f6299 100644 --- a/drivers/net/bnxt/tf_ulp/bnxt_ulp.c +++ b/drivers/net/bnxt/tf_ulp/bnxt_ulp.c @@ -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); +} diff --git a/drivers/net/bnxt/tf_ulp/bnxt_ulp.h b/drivers/net/bnxt/tf_ulp/bnxt_ulp.h index ed978734ac..36405ae1e6 100644 --- a/drivers/net/bnxt/tf_ulp/bnxt_ulp.h +++ b/drivers/net/bnxt/tf_ulp/bnxt_ulp.h @@ -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_ */ diff --git a/drivers/net/bnxt/tf_ulp/ulp_flow_db.c b/drivers/net/bnxt/tf_ulp/ulp_flow_db.c index cbdf5df68a..9a2d3758d3 100644 --- a/drivers/net/bnxt/tf_ulp/ulp_flow_db.c +++ b/drivers/net/bnxt/tf_ulp/ulp_flow_db.c @@ -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; } diff --git a/drivers/net/bnxt/tf_ulp/ulp_mapper.c b/drivers/net/bnxt/tf_ulp/ulp_mapper.c index 7321411669..85ae3b5c47 100644 --- a/drivers/net/bnxt/tf_ulp/ulp_mapper.c +++ b/drivers/net/bnxt/tf_ulp/ulp_mapper.c @@ -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); -- 2.20.1