diff mbox series

[ovs-dev,v2,5/5] netdev-offload: Fix Clang's static analyzer 'Division by zero' warnings.

Message ID 169807096760.1031379.2177009568964291561.stgit@ebuild
State Accepted
Delegated to: Simon Horman
Headers show
Series Fix some of Clang's static analyzer warnings. | expand

Checks

Context Check Description
ovsrobot/apply-robot warning apply and check: warning
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Eelco Chaudron Oct. 23, 2023, 2:22 p.m. UTC
When enabling DPDK with the configure the below, ovs-vswitchd will crash.

  ovs-vsctl set Open_vSwitch . other_config:n-offload-threads=0
  ovs-vsctl set Open_vSwitch . other_config:hw-offload=true

This issue arises because setting the 'n-offload-threads' value to zero
is not a supported configuration. This fix addresses this by implementing
a check to ensure a valid 'n-offload-threads' value, both during
configuration and statistics gathering.

Fixes: 62c2d8a67543 ("netdev-offload: Add multi-thread API.")
Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
---
 lib/dpif-netdev.c       |    4 ++++
 lib/netdev-offload.c    |    3 ++-
 tests/test-id-fpool.c   |    2 +-
 tests/test-mpsc-queue.c |    2 +-
 4 files changed, 8 insertions(+), 3 deletions(-)

Comments

Simon Horman Oct. 25, 2023, 9:58 a.m. UTC | #1
On Mon, Oct 23, 2023 at 04:22:47PM +0200, Eelco Chaudron wrote:
> When enabling DPDK with the configure the below, ovs-vswitchd will crash.
> 
>   ovs-vsctl set Open_vSwitch . other_config:n-offload-threads=0
>   ovs-vsctl set Open_vSwitch . other_config:hw-offload=true
> 
> This issue arises because setting the 'n-offload-threads' value to zero
> is not a supported configuration. This fix addresses this by implementing
> a check to ensure a valid 'n-offload-threads' value, both during
> configuration and statistics gathering.
> 
> Fixes: 62c2d8a67543 ("netdev-offload: Add multi-thread API.")
> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>

Hi Eelco,

Thanks for addressing my concerns regarding the patch description in v1.
This looks good to me.

Acked-by: Simon Horman <horms@ovn.org>
diff mbox series

Patch

diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index 157694bcf..b8f065d1d 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -4748,6 +4748,10 @@  dpif_netdev_offload_stats_get(struct dpif *dpif,
     }
 
     nb_thread = netdev_offload_thread_nb();
+    if (!nb_thread) {
+        return EINVAL;
+    }
+
     /* nb_thread counters for the overall total as well. */
     stats->size = ARRAY_SIZE(hwol_stats) * (nb_thread + 1);
     stats->counters = xcalloc(stats->size, sizeof *stats->counters);
diff --git a/lib/netdev-offload.c b/lib/netdev-offload.c
index a5fa62487..931d634e1 100644
--- a/lib/netdev-offload.c
+++ b/lib/netdev-offload.c
@@ -872,7 +872,8 @@  netdev_set_flow_api_enabled(const struct smap *ovs_other_config)
             offload_thread_nb = smap_get_ullong(ovs_other_config,
                                                 "n-offload-threads",
                                                 DEFAULT_OFFLOAD_THREAD_NB);
-            if (offload_thread_nb > MAX_OFFLOAD_THREAD_NB) {
+            if (offload_thread_nb == 0 ||
+                offload_thread_nb > MAX_OFFLOAD_THREAD_NB) {
                 VLOG_WARN("netdev: Invalid number of threads requested: %u",
                           offload_thread_nb);
                 offload_thread_nb = DEFAULT_OFFLOAD_THREAD_NB;
diff --git a/tests/test-id-fpool.c b/tests/test-id-fpool.c
index 27800aa9b..7bdb8154d 100644
--- a/tests/test-id-fpool.c
+++ b/tests/test-id-fpool.c
@@ -237,7 +237,7 @@  print_result(const char *prefix)
     for (i = 0; i < n_threads; i++) {
         avg += thread_working_ms[i];
     }
-    avg /= n_threads;
+    avg /= n_threads ? n_threads : 1;
     printf("%s: ", prefix);
     for (i = 0; i < n_threads; i++) {
         if (thread_working_ms[i] >= TIMEOUT_MS) {
diff --git a/tests/test-mpsc-queue.c b/tests/test-mpsc-queue.c
index 16aa804a0..86a223caf 100644
--- a/tests/test-mpsc-queue.c
+++ b/tests/test-mpsc-queue.c
@@ -315,7 +315,7 @@  print_result(const char *prefix, int reader_elapsed)
     for (i = 0; i < n_threads; i++) {
         avg += thread_working_ms[i];
     }
-    avg /= n_threads;
+    avg /= n_threads ? n_threads : 1;
     printf("%s:  %6d", prefix, reader_elapsed);
     for (i = 0; i < n_threads; i++) {
         printf(" %6" PRIu64, thread_working_ms[i]);