<html><div>
<div><span style="font-family: 'courier new', courier;">From fe8850cb3b7c1051784e5587b9f79beeaf87a804 Mon Sep 17 00:00:00 2001</span><br /><span style="font-family: 'courier new', courier;">From: bullvolkan <volkan.atli@b-ulltech.com></span><br /><span style="font-family: 'courier new', courier;">Date: Fri, 20 Oct 2023 21:48:30 +0300</span><br /><span style="font-family: 'courier new', courier;">Subject: [PATCH] distributor: enhance error handling for consistency</span></div>
<div> </div>
<div><span style="font-family: 'courier new', courier;">This commit improves error handling in the distributor component to ensure</span><br /><span style="font-family: 'courier new', courier;">consistency and reliability. It addresses invalid 'num' values in 'lcore_worker',</span><br /><span style="font-family: 'courier new', courier;">corrects 'retptr64' initialization in 'rte_distributor_request_pkt', and checks</span><br /><span style="font-family: 'courier new', courier;">the validity of 'num' in 'rte_distributor_return_pkt'. These changes enhance</span><br /><span style="font-family: 'courier new', courier;">error handling and maintain code consistency.</span></div>
<div> </div>
<div><span style="font-family: 'courier new', courier;">Signed-off-by: Volkan Atli <volkan.atli@b-ulltech.com></span><br /><span style="font-family: 'courier new', courier;">---</span><br /><span style="font-family: 'courier new', courier;"> .mailmap                          |  2 +-</span><br /><span style="font-family: 'courier new', courier;"> examples/distributor/main.c       |  9 +++++++--</span><br /><span style="font-family: 'courier new', courier;"> lib/distributor/rte_distributor.c | 24 ++++++++++++++----------</span><br /><span style="font-family: 'courier new', courier;"> lib/distributor/rte_distributor.h | 11 ++++++++++-</span><br /><span style="font-family: 'courier new', courier;"> 4 files changed, 32 insertions(+), 14 deletions(-)</span></div>
<div> </div>
<div><span style="font-family: 'courier new', courier;">diff --git a/.mailmap b/.mailmap</span><br /><span style="font-family: 'courier new', courier;">index 3f5bab26a8..d0fe75fa8b 100644</span><br /><span style="font-family: 'courier new', courier;">--- a/.mailmap</span><br /><span style="font-family: 'courier new', courier;">+++ b/.mailmap</span><br /><span style="font-family: 'courier new', courier;">@@ -63,7 +63,7 @@ Alfredo Cardigliano <cardigliano@ntop.org></span><br /><span style="font-family: 'courier new', courier;"> Ali Alnubani <alialnu@nvidia.com> <alialnu@mellanox.com></span><br /><span style="font-family: 'courier new', courier;"> Alice Michael <alice.michael@intel.com></span><br /><span style="font-family: 'courier new', courier;"> Alin Rauta <alin.rauta@intel.com></span><br /><span style="font-family: 'courier new', courier;">-Ali Volkan Atli <volkan.atli@argela.com.tr></span><br /><span style="font-family: 'courier new', courier;">+Ali Volkan Atli <volkan.atli@b-ulltech.com></span><br /><span style="font-family: 'courier new', courier;"> Allain Legacy <allain.legacy@windriver.com></span><br /><span style="font-family: 'courier new', courier;"> Allen Hubbe <allen.hubbe@amd.com></span><br /><span style="font-family: 'courier new', courier;"> Alok Makhariya <alok.makhariya@nxp.com></span><br /><span style="font-family: 'courier new', courier;">diff --git a/examples/distributor/main.c b/examples/distributor/main.c</span><br /><span style="font-family: 'courier new', courier;">index 21304d6618..58369c3faa 100644</span><br /><span style="font-family: 'courier new', courier;">--- a/examples/distributor/main.c</span><br /><span style="font-family: 'courier new', courier;">+++ b/examples/distributor/main.c</span><br /><span style="font-family: 'courier new', courier;">@@ -634,8 +634,8 @@ lcore_worker(struct lcore_params *p)</span><br /><span style="font-family: 'courier new', courier;"> {</span><br /><span style="font-family: 'courier new', courier;">     struct rte_distributor *d = p->d;</span><br /><span style="font-family: 'courier new', courier;">     const unsigned id = p->worker_id;</span><br /><span style="font-family: 'courier new', courier;">-    unsigned int num = 0;</span><br /><span style="font-family: 'courier new', courier;">-    unsigned int i;</span><br /><span style="font-family: 'courier new', courier;">+    int num = 0;</span><br /><span style="font-family: 'courier new', courier;">+    int i;</span><br /><span style="font-family: 'courier new', courier;"> </span><br /><span style="font-family: 'courier new', courier;">     /*</span><br /><span style="font-family: 'courier new', courier;">      * for single port, xor_val will be zero so we won't modify the output</span><br /><span style="font-family: 'courier new', courier;">@@ -652,6 +652,11 @@ lcore_worker(struct lcore_params *p)</span><br /><span style="font-family: 'courier new', courier;">     printf("\nCore %u acting as worker core.\n", rte_lcore_id());</span><br /><span style="font-family: 'courier new', courier;">     while (!quit_signal_work) {</span><br /><span style="font-family: 'courier new', courier;">         num = rte_distributor_get_pkt(d, id, buf, buf, num);</span><br /><span style="font-family: 'courier new', courier;">+</span><br /><span style="font-family: 'courier new', courier;">+        if (unlikely(num < 0 || num > 8)) {</span><br /><span style="font-family: 'courier new', courier;">+            rte_exit(EXIT_FAILURE, "strange retcount %u\n", num);</span><br /><span style="font-family: 'courier new', courier;">+        }</span><br /><span style="font-family: 'courier new', courier;">+</span><br /><span style="font-family: 'courier new', courier;">         /* Do a little bit of work for each packet */</span><br /><span style="font-family: 'courier new', courier;">         for (i = 0; i < num; i++) {</span><br /><span style="font-family: 'courier new', courier;">             uint64_t t = rte_rdtsc()+100;</span><br /><span style="font-family: 'courier new', courier;">diff --git a/lib/distributor/rte_distributor.c b/lib/distributor/rte_distributor.c</span><br /><span style="font-family: 'courier new', courier;">index 5ca80dd44f..92653dd9ca 100644</span><br /><span style="font-family: 'courier new', courier;">--- a/lib/distributor/rte_distributor.c</span><br /><span style="font-family: 'courier new', courier;">+++ b/lib/distributor/rte_distributor.c</span><br /><span style="font-family: 'courier new', courier;">@@ -64,15 +64,16 @@ rte_distributor_request_pkt(struct rte_distributor *d,</span><br /><span style="font-family: 'courier new', courier;">      * handshake bits. Populate the retptrs with returning packets.</span><br /><span style="font-family: 'courier new', courier;">      */</span><br /><span style="font-family: 'courier new', courier;"> </span><br /><span style="font-family: 'courier new', courier;">-    for (i = count; i < RTE_DIST_BURST_SIZE; i++)</span><br /><span style="font-family: 'courier new', courier;">-        buf->retptr64[i] = 0;</span><br /><span style="font-family: 'courier new', courier;">-</span><br /><span style="font-family: 'courier new', courier;">     /* Set VALID_BUF bit for each packet returned */</span><br /><span style="font-family: 'courier new', courier;">-    for (i = count; i-- > 0; )</span><br /><span style="font-family: 'courier new', courier;">+    size_t arr_len = sizeof(buf->retptr64) / sizeof(buf->retptr64[0]);</span><br /><span style="font-family: 'courier new', courier;">+    for (i = 0; i < RTE_MIN(count, arr_len); ++i)</span><br /><span style="font-family: 'courier new', courier;">         buf->retptr64[i] =</span><br /><span style="font-family: 'courier new', courier;">             (((int64_t)(uintptr_t)(oldpkt[i])) <<</span><br /><span style="font-family: 'courier new', courier;">             RTE_DISTRIB_FLAG_BITS) | RTE_DISTRIB_VALID_BUF;</span><br /><span style="font-family: 'courier new', courier;"> </span><br /><span style="font-family: 'courier new', courier;">+    for (i = count; i < RTE_DIST_BURST_SIZE; i++)</span><br /><span style="font-family: 'courier new', courier;">+        buf->retptr64[i] = 0;</span><br /><span style="font-family: 'courier new', courier;">+</span><br /><span style="font-family: 'courier new', courier;">     /*</span><br /><span style="font-family: 'courier new', courier;">      * Finally, set the GET_BUF  to signal to distributor that cache</span><br /><span style="font-family: 'courier new', courier;">      * line is ready for processing</span><br /><span style="font-family: 'courier new', courier;">@@ -161,7 +162,7 @@ rte_distributor_return_pkt(struct rte_distributor *d,</span><br /><span style="font-family: 'courier new', courier;">         unsigned int worker_id, struct rte_mbuf **oldpkt, int num)</span><br /><span style="font-family: 'courier new', courier;"> {</span><br /><span style="font-family: 'courier new', courier;">     struct rte_distributor_buffer *buf = &d->bufs[worker_id];</span><br /><span style="font-family: 'courier new', courier;">-    unsigned int i;</span><br /><span style="font-family: 'courier new', courier;">+    int i;</span><br /><span style="font-family: 'courier new', courier;"> </span><br /><span style="font-family: 'courier new', courier;">     if (unlikely(d->alg_type == RTE_DIST_ALG_SINGLE)) {</span><br /><span style="font-family: 'courier new', courier;">         if (num == 1)</span><br /><span style="font-family: 'courier new', courier;">@@ -186,16 +187,19 @@ rte_distributor_return_pkt(struct rte_distributor *d,</span><br /><span style="font-family: 'courier new', courier;">             rte_pause();</span><br /><span style="font-family: 'courier new', courier;">     }</span><br /><span style="font-family: 'courier new', courier;"> </span><br /><span style="font-family: 'courier new', courier;">+    if (unlikely(num > RTE_DIST_BURST_SIZE || num < 0)) {</span><br /><span style="font-family: 'courier new', courier;">+        return -EINVAL;</span><br /><span style="font-family: 'courier new', courier;">+    }</span><br /><span style="font-family: 'courier new', courier;">+</span><br /><span style="font-family: 'courier new', courier;">     /* Sync with distributor to acquire retptrs */</span><br /><span style="font-family: 'courier new', courier;">     __atomic_thread_fence(__ATOMIC_ACQUIRE);</span><br /><span style="font-family: 'courier new', courier;">-    for (i = 0; i < RTE_DIST_BURST_SIZE; i++)</span><br /><span style="font-family: 'courier new', courier;">-        /* Switch off the return bit first */</span><br /><span style="font-family: 'courier new', courier;">-        buf->retptr64[i] = 0;</span><br /><span style="font-family: 'courier new', courier;">-</span><br /><span style="font-family: 'courier new', courier;">-    for (i = num; i-- > 0; )</span><br /><span style="font-family: 'courier new', courier;">+    for (i = 0; i < num; i++)</span><br /><span style="font-family: 'courier new', courier;">         buf->retptr64[i] = (((int64_t)(uintptr_t)oldpkt[i]) <<</span><br /><span style="font-family: 'courier new', courier;">             RTE_DISTRIB_FLAG_BITS) | RTE_DISTRIB_VALID_BUF;</span><br /><span style="font-family: 'courier new', courier;"> </span><br /><span style="font-family: 'courier new', courier;">+    for (i = num; i < RTE_DIST_BURST_SIZE; i++)</span><br /><span style="font-family: 'courier new', courier;">+        buf->retptr64[i] = 0;</span><br /><span style="font-family: 'courier new', courier;">+</span><br /><span style="font-family: 'courier new', courier;">     /* Use RETURN_BUF on bufptr64 to notify distributor that</span><br /><span style="font-family: 'courier new', courier;">      * we won't read any mbufs from there even if GET_BUF is set.</span><br /><span style="font-family: 'courier new', courier;">      * This allows distributor to retrieve in-flight already sent packets.</span><br /><span style="font-family: 'courier new', courier;">diff --git a/lib/distributor/rte_distributor.h b/lib/distributor/rte_distributor.h</span><br /><span style="font-family: 'courier new', courier;">index a073e64612..337ff8e0fe 100644</span><br /><span style="font-family: 'courier new', courier;">--- a/lib/distributor/rte_distributor.h</span><br /><span style="font-family: 'courier new', courier;">+++ b/lib/distributor/rte_distributor.h</span><br /><span style="font-family: 'courier new', courier;">@@ -160,7 +160,11 @@ rte_distributor_clear_returns(struct rte_distributor *d);</span><br /><span style="font-family: 'courier new', courier;">  *   The number of packets being returned</span><br /><span style="font-family: 'courier new', courier;">  *</span><br /><span style="font-family: 'courier new', courier;">  * @return</span><br /><span style="font-family: 'courier new', courier;">- *   The number of packets in the pkts array</span><br /><span style="font-family: 'courier new', courier;">+ *   The number of packets in the pkts array or</span><br /><span style="font-family: 'courier new', courier;">+ *   -EINVAL if num is greater than 1 (RTE_DIST_ALG_SINGLE)</span><br /><span style="font-family: 'courier new', courier;">+ *   -EINVAL if num is greater than RTE_DIST_BURST_SIZE or</span><br /><span style="font-family: 'courier new', courier;">+ *    less than zero (RTE_DIST_ALG_BURST)</span><br /><span style="font-family: 'courier new', courier;">+</span><br /><span style="font-family: 'courier new', courier;">  */</span><br /><span style="font-family: 'courier new', courier;"> int</span><br /><span style="font-family: 'courier new', courier;"> rte_distributor_get_pkt(struct rte_distributor *d,</span><br /><span style="font-family: 'courier new', courier;">@@ -180,6 +184,11 @@ rte_distributor_get_pkt(struct rte_distributor *d,</span><br /><span style="font-family: 'courier new', courier;">  *   The previous packets being processed by the worker</span><br /><span style="font-family: 'courier new', courier;">  * @param num</span><br /><span style="font-family: 'courier new', courier;">  *   The number of packets in the oldpkt array</span><br /><span style="font-family: 'courier new', courier;">+ *</span><br /><span style="font-family: 'courier new', courier;">+ * @return</span><br /><span style="font-family: 'courier new', courier;">+ *   The number of packets in the pkts array or</span><br /><span style="font-family: 'courier new', courier;">+ *   -EINVAL if retcount is greater than 1 (RTE_DIST_ALG_SINGLE)</span><br /><span style="font-family: 'courier new', courier;">+ *   -EINVAL if retcount is greater than RTE_DIST_BURST_SIZE (RTE_DIST_ALG_BURST)</span><br /><span style="font-family: 'courier new', courier;">  */</span><br /><span style="font-family: 'courier new', courier;"> int</span><br /><span style="font-family: 'courier new', courier;"> rte_distributor_return_pkt(struct rte_distributor *d,</span><br /><span style="font-family: 'courier new', courier;">-- </span><br /><span style="font-family: 'courier new', courier;">2.41.0</span></div>
</div></html>