DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH 27.03 2/6] net/iavf: use stack storage for virtchnl messages
Date: Mon, 16 Nov 2026 16:09:28 +0000	[thread overview]
Message-ID: <20261116160932.1006354-3-bruce.richardson@intel.com> (raw)
In-Reply-To: <20261116160932.1006354-1-bruce.richardson@intel.com>

Rather than using malloc and free for the request and response buffers
of IPsec and traffic management virtchnl messages, define them on the
stack using RTE_DEFINE_FLEX. This removes the allocation failure and
cleanup paths, avoiding potential leaks on error.

For the traffic management messages, add checks that the number of TCs
and queue pairs do not exceed the maximum sizes of the buffers.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 drivers/net/intel/iavf/iavf_ipsec_crypto.c | 270 ++++++---------------
 drivers/net/intel/iavf/iavf_tm.c           |  26 +-
 2 files changed, 79 insertions(+), 217 deletions(-)

diff --git a/drivers/net/intel/iavf/iavf_ipsec_crypto.c b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
index be0a0d0df6..d5a6a48092 100644
--- a/drivers/net/intel/iavf/iavf_ipsec_crypto.c
+++ b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
@@ -460,28 +460,14 @@ static uint32_t
 iavf_ipsec_crypto_security_association_add(struct iavf_adapter *adapter,
 	struct rte_security_session_conf *conf)
 {
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	struct virtchnl_ipsec_sa_cfg *sa_cfg;
-	size_t request_len, response_len;
-
-	int rc;
-
-	request_len = sizeof(struct inline_ipsec_msg) +
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_cfg, 1);
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.sa_cfg_resp, 1);
+	const size_t request_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_sa_cfg);
-
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
-	response_len = sizeof(struct inline_ipsec_msg) +
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_sa_cfg_resp);
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	struct virtchnl_ipsec_sa_cfg *sa_cfg;
+	int rc;
 
 	/* set msg header params */
 	request->ipsec_opcode = INLINE_IPSEC_OP_SA_CREATE;
@@ -546,19 +532,14 @@ iavf_ipsec_crypto_security_association_add(struct iavf_adapter *adapter,
 			(uint8_t *)request, request_len,
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response id */
 	if (response->ipsec_opcode != request->ipsec_opcode ||
 		response->req_id != request->req_id)
-		rc = -EFAULT;
-	else
-		rc = response->ipsec_data.sa_cfg_resp->sa_handle;
-update_cleanup:
-	free(response);
-	free(request);
+		return -EFAULT;
 
-	return rc;
+	return response->ipsec_data.sa_cfg_resp->sa_handle;
 }
 
 static void
@@ -722,17 +703,13 @@ iavf_ipsec_crypto_inbound_security_policy_add(struct iavf_adapter *adapter,
 	bool is_udp,
 	uint16_t udp_port)
 {
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	size_t request_len, response_len;
-	int rc = 0;
-
-	request_len = sizeof(struct inline_ipsec_msg) +
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sp_cfg, 1);
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.sp_cfg_resp, 1);
+	const size_t request_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_sp_cfg);
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
+			sizeof(struct virtchnl_ipsec_sp_cfg_resp);
+	int rc = 0;
 
 	/* set msg header params */
 	request->ipsec_opcode = INLINE_IPSEC_OP_SP_CREATE;
@@ -767,58 +744,32 @@ iavf_ipsec_crypto_inbound_security_policy_add(struct iavf_adapter *adapter,
 	request->ipsec_data.sp_cfg->is_udp = is_udp;
 	request->ipsec_data.sp_cfg->udp_port = htons(udp_port);
 
-	response_len = sizeof(struct inline_ipsec_msg) +
-			sizeof(struct virtchnl_ipsec_sp_cfg_resp);
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
 	/* send virtual channel request to add SA to hardware database */
 	rc = iavf_ipsec_crypto_request(adapter,
 			(uint8_t *)request, request_len,
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response */
 	if (response->ipsec_opcode != request->ipsec_opcode ||
 		response->req_id != request->req_id)
-		rc = -EFAULT;
-	else
-		rc = response->ipsec_data.sp_cfg_resp->rule_id;
-
-update_cleanup:
-	free(request);
-	free(response);
+		return -EFAULT;
 
-	return rc;
+	return response->ipsec_data.sp_cfg_resp->rule_id;
 }
 
 static uint32_t
 iavf_ipsec_crypto_sa_update_esn(struct iavf_adapter *adapter,
 	struct iavf_security_session *sess)
 {
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	size_t request_len, response_len;
-	int rc = 0;
-
-	request_len = sizeof(struct inline_ipsec_msg) +
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_update, 1);
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_resp, 1);
+	const size_t request_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_sa_update);
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
-	response_len = sizeof(struct inline_ipsec_msg) +
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_resp);
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	int rc = 0;
 
 	/* set msg header params */
 	request->ipsec_opcode = INLINE_IPSEC_OP_SA_UPDATE;
@@ -834,20 +785,14 @@ iavf_ipsec_crypto_sa_update_esn(struct iavf_adapter *adapter,
 			(uint8_t *)request, request_len,
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response */
 	if (response->ipsec_opcode != request->ipsec_opcode ||
 		response->req_id != request->req_id)
-		rc = -EFAULT;
-	else
-		rc = response->ipsec_data.ipsec_resp->resp;
-
-update_cleanup:
-	free(request);
-	free(response);
+		return -EFAULT;
 
-	return rc;
+	return response->ipsec_data.ipsec_resp->resp;
 }
 
 static int
@@ -897,25 +842,13 @@ int
 iavf_ipsec_crypto_security_policy_delete(struct iavf_adapter *adapter,
 	uint8_t is_v4, uint32_t flow_id)
 {
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	size_t request_len, response_len;
-	int rc = 0;
-
-	request_len = sizeof(struct inline_ipsec_msg) +
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sp_destroy, 1);
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_resp, 1);
+	const size_t request_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_sp_destroy);
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
-	response_len = sizeof(struct inline_ipsec_msg) +
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_resp);
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	int rc = 0;
 
 	/* set msg header params */
 	request->ipsec_opcode = INLINE_IPSEC_OP_SP_DESTROY;
@@ -932,48 +865,27 @@ iavf_ipsec_crypto_security_policy_delete(struct iavf_adapter *adapter,
 			(uint8_t *)request, request_len,
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response */
 	if (response->ipsec_opcode != request->ipsec_opcode ||
 		response->req_id != request->req_id)
-		rc = -EFAULT;
-	else
-		rc = response->ipsec_data.ipsec_status->status;
-
-update_cleanup:
-	free(request);
-	free(response);
+		return -EFAULT;
 
-	return rc;
+	return response->ipsec_data.ipsec_status->status;
 }
 
 static uint32_t
 iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
 	struct iavf_security_session *sess)
 {
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	size_t request_len, response_len;
-
-	int rc = 0;
-
-	request_len = sizeof(struct inline_ipsec_msg) +
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_destroy, 1);
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_resp, 1);
+	const size_t request_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_sa_destroy);
-
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
-	response_len = sizeof(struct inline_ipsec_msg) +
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_resp);
-
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	int rc = 0;
 
 	/* set msg header params */
 	request->ipsec_opcode = INLINE_IPSEC_OP_SA_DESTROY;
@@ -995,12 +907,12 @@ iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
 			(uint8_t *)request, request_len,
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response */
 	if (response->ipsec_opcode != request->ipsec_opcode ||
 		response->req_id != request->req_id)
-		rc = -EFAULT;
+		return -EFAULT;
 
 	/**
 	 * Delete status will be the same bitmask as sa_destroy request flag if
@@ -1008,13 +920,9 @@ iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
 	 */
 	if (request->ipsec_data.sa_destroy->flag !=
 			response->ipsec_data.ipsec_status->status)
-		rc = -EFAULT;
-
-update_cleanup:
-	free(response);
-	free(request);
+		return -EFAULT;
 
-	return rc;
+	return 0;
 }
 
 static int
@@ -1160,50 +1068,30 @@ iavf_ipsec_crypto_device_capabilities_get(struct iavf_adapter *adapter,
 		struct virtchnl_ipsec_cap *capability)
 {
 	/* Perform pf-vf comms */
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	size_t request_len, response_len;
-	int rc;
-
-	request_len = sizeof(struct inline_ipsec_msg);
-
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
-	response_len = sizeof(struct inline_ipsec_msg) +
+	struct inline_ipsec_msg request = {0};
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_cap, 1);
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_cap);
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	int rc;
 
 	/* set msg header params */
-	request->ipsec_opcode = INLINE_IPSEC_OP_GET_CAP;
-	request->req_id = (uint16_t)0xDEADBEEF;
+	request.ipsec_opcode = INLINE_IPSEC_OP_GET_CAP;
+	request.req_id = (uint16_t)0xDEADBEEF;
 
 	/* send virtual channel request to add SA to hardware database */
 	rc = iavf_ipsec_crypto_request(adapter,
-			(uint8_t *)request, request_len,
+			(uint8_t *)&request, sizeof(request),
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response id */
-	if (response->ipsec_opcode != request->ipsec_opcode ||
-		response->req_id != request->req_id){
-		rc = -EFAULT;
-		goto update_cleanup;
-	}
-	memcpy(capability, response->ipsec_data.ipsec_cap, sizeof(*capability));
-
-update_cleanup:
-	free(response);
-	free(request);
+	if (response->ipsec_opcode != request.ipsec_opcode ||
+		response->req_id != request.req_id)
+		return -EFAULT;
 
-	return rc;
+	memcpy(capability, response->ipsec_data.ipsec_cap, sizeof(*capability));
+	return 0;
 }
 
 enum rte_crypto_auth_algorithm auth_maptbl[] = {
@@ -1588,50 +1476,30 @@ iavf_ipsec_crypto_status_get(struct iavf_adapter *adapter,
 		struct virtchnl_ipsec_status *status)
 {
 	/* Perform pf-vf comms */
-	struct inline_ipsec_msg *request = NULL, *response = NULL;
-	size_t request_len, response_len;
-	int rc;
-
-	request_len = sizeof(struct inline_ipsec_msg);
-
-	request = calloc(1, request_len);
-	if (request == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
-
-	response_len = sizeof(struct inline_ipsec_msg) +
+	struct inline_ipsec_msg request = {0};
+	RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_status, 1);
+	const size_t response_len = sizeof(struct inline_ipsec_msg) +
 			sizeof(struct virtchnl_ipsec_status);
-	response = calloc(1, response_len);
-	if (response == NULL) {
-		rc = -ENOMEM;
-		goto update_cleanup;
-	}
+	int rc;
 
 	/* set msg header params */
-	request->ipsec_opcode = INLINE_IPSEC_OP_GET_STATUS;
-	request->req_id = (uint16_t)0xDEADBEEF;
+	request.ipsec_opcode = INLINE_IPSEC_OP_GET_STATUS;
+	request.req_id = (uint16_t)0xDEADBEEF;
 
 	/* send virtual channel request to add SA to hardware database */
 	rc = iavf_ipsec_crypto_request(adapter,
-			(uint8_t *)request, request_len,
+			(uint8_t *)&request, sizeof(request),
 			(uint8_t *)response, response_len);
 	if (rc)
-		goto update_cleanup;
+		return rc;
 
 	/* verify response id */
-	if (response->ipsec_opcode != request->ipsec_opcode ||
-		response->req_id != request->req_id){
-		rc = -EFAULT;
-		goto update_cleanup;
-	}
-	memcpy(status, response->ipsec_data.ipsec_status, sizeof(*status));
-
-update_cleanup:
-	free(response);
-	free(request);
+	if (response->ipsec_opcode != request.ipsec_opcode ||
+		response->req_id != request.req_id)
+		return -EFAULT;
 
-	return rc;
+	memcpy(status, response->ipsec_data.ipsec_status, sizeof(*status));
+	return 0;
 }
 
 
diff --git a/drivers/net/intel/iavf/iavf_tm.c b/drivers/net/intel/iavf/iavf_tm.c
index 07ba58f6f5..c7b904e9d7 100644
--- a/drivers/net/intel/iavf/iavf_tm.c
+++ b/drivers/net/intel/iavf/iavf_tm.c
@@ -833,8 +833,9 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
 	struct iavf_adapter *adapter =
 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
-	struct virtchnl_queue_tc_mapping *q_tc_mapping = NULL;
-	struct virtchnl_queues_bw_cfg *q_bw = NULL;
+	RTE_DEFINE_FLEX(struct virtchnl_queue_tc_mapping, q_tc_mapping, tc,
+			IAVF_MAX_TRAFFIC_CLASS - 1);
+	RTE_DEFINE_FLEX(struct virtchnl_queues_bw_cfg, q_bw, cfg, IAVF_MAX_NUM_QUEUES_LV - 1);
 	struct iavf_tm_node_list *queue_list = &vf->tm_conf.queue_list;
 	struct iavf_tm_node *tm_node;
 	struct iavf_qtc_map *qtc_map = NULL;
@@ -869,21 +870,18 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
 		goto fail_clear;
 	}
 
-	size = sizeof(*q_tc_mapping) + sizeof(q_tc_mapping->tc[0]) *
-		(vf->qos_cap->num_elem - 1);
-	q_tc_mapping = calloc(1, size);
-	if (!q_tc_mapping) {
-		ret_val = IAVF_ERR_NO_MEMORY;
+	if (vf->qos_cap->num_elem > IAVF_MAX_TRAFFIC_CLASS ||
+			vf->num_queue_pairs > IAVF_MAX_NUM_QUEUES_LV) {
+		PMD_DRV_LOG(ERR, "Too many TCs (%u) or queues (%u) for TM configuration",
+			vf->qos_cap->num_elem, vf->num_queue_pairs);
+		ret_val = IAVF_ERR_PARAM;
 		goto fail_clear;
 	}
 
+	size = sizeof(*q_tc_mapping) + sizeof(q_tc_mapping->tc[0]) *
+		(vf->qos_cap->num_elem - 1);
 	size_q = sizeof(*q_bw) + sizeof(q_bw->cfg[0]) *
 		(vf->num_queue_pairs - 1);
-	q_bw = calloc(1, size_q);
-	if (!q_bw) {
-		ret_val = IAVF_ERR_NO_MEMORY;
-		goto fail_clear;
-	}
 
 	q_tc_mapping->vsi_id = vf->vsi.vsi_id;
 	q_tc_mapping->num_tc = vf->qos_cap->num_elem;
@@ -950,8 +948,6 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
 	free(old_qtc_map);
 	if (adapter->stopped == 1)
 		vf->tm_conf.committed = true;
-	free(q_bw);
-	free(q_tc_mapping);
 	return ret_val;
 
 fail_clear:
@@ -961,8 +957,6 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
 		iavf_tm_conf_init(dev);
 	}
 err:
-	free(q_bw);
-	free(q_tc_mapping);
 	free(qtc_map);
 	return ret_val;
 }
-- 
2.55.0


  parent reply	other threads:[~2026-11-16 16:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 1/6] eal: add macros for local variable-length structure buffers Bruce Richardson
2026-11-16 16:09 ` Bruce Richardson [this message]
2026-11-16 16:09 ` [PATCH 27.03 3/6] net/ice: use stack storage for function-local buffers Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 4/6] net/idpf: use stack storage for virtchnl messages Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 5/6] net/cpfl: use stack storage for function-local buffers Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 6/6] net/i40e: " Bruce Richardson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116160932.1006354-3-bruce.richardson@intel.com \
    --to=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox