diff mbox series

[ovs-dev,v7,1/2] util: Support checking for kernel versions.

Message ID ca03ca9969812c2074e9754079f2c072e1108b3e.1710154918.git.felix.huettner@mail.schwarz
State Accepted
Commit 6439d694ae05b07823b5ca21a089b4ef9ab6b2dd
Headers show
Series [ovs-dev,v7,1/2] util: Support checking for kernel versions. | expand

Checks

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

Commit Message

Felix Huettner March 11, 2024, 1:15 p.m. UTC
Extract checking for a given kernel version to a separate function.
It will be used also in the next patch.

Acked-by: Mike Pattrick <mkp@redhat.com>
Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Felix Huettner <felix.huettner@mail.schwarz>
---
v6->v7: none
v5->v6:
- fix ovs_kernel_is_version_or_newer returning false if major and minor
  are equal (thanks Mike)
v4->v5:
- fix wrong ifdef that broke on macos
- fix ovs_kernel_is_version_or_newer working in reverse than desired
- ovs_kernel_is_version_or_newer now always returns false if uname
  errors (Thanks Eelco)
v4:
- extract function to check kernel version

 lib/netdev-linux.c | 15 +++------------
 lib/util.c         | 27 +++++++++++++++++++++++++++
 lib/util.h         |  4 ++++
 3 files changed, 34 insertions(+), 12 deletions(-)


base-commit: 33f45ded67a2d524ccf54cf4bb79a38d8140f14b

Comments

Aaron Conole April 3, 2024, 1:14 p.m. UTC | #1
Felix Huettner via dev <ovs-dev@openvswitch.org> writes:

> Extract checking for a given kernel version to a separate function.
> It will be used also in the next patch.
>
> Acked-by: Mike Pattrick <mkp@redhat.com>
> Acked-by: Eelco Chaudron <echaudro@redhat.com>
> Signed-off-by: Felix Huettner <felix.huettner@mail.schwarz>
> ---

Acked-by: Aaron Conole <aconole@redhat.com>

> v6->v7: none
> v5->v6:
> - fix ovs_kernel_is_version_or_newer returning false if major and minor
>   are equal (thanks Mike)
> v4->v5:
> - fix wrong ifdef that broke on macos
> - fix ovs_kernel_is_version_or_newer working in reverse than desired
> - ovs_kernel_is_version_or_newer now always returns false if uname
>   errors (Thanks Eelco)
> v4:
> - extract function to check kernel version
>
>  lib/netdev-linux.c | 15 +++------------
>  lib/util.c         | 27 +++++++++++++++++++++++++++
>  lib/util.h         |  4 ++++
>  3 files changed, 34 insertions(+), 12 deletions(-)
>
> diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c
> index 1e904d8e6..25349c605 100644
> --- a/lib/netdev-linux.c
> +++ b/lib/netdev-linux.c
> @@ -40,7 +40,6 @@
>  #include <sys/ioctl.h>
>  #include <sys/socket.h>
>  #include <sys/uio.h>
> -#include <sys/utsname.h>
>  #include <net/if.h>
>  #include <net/if_arp.h>
>  #include <net/route.h>
> @@ -6428,18 +6427,10 @@ getqdisc_is_safe(void)
>      static bool safe = false;
>  
>      if (ovsthread_once_start(&once)) {
> -        struct utsname utsname;
> -        int major, minor;
> -
> -        if (uname(&utsname) == -1) {
> -            VLOG_WARN("uname failed (%s)", ovs_strerror(errno));
> -        } else if (!ovs_scan(utsname.release, "%d.%d", &major, &minor)) {
> -            VLOG_WARN("uname reported bad OS release (%s)", utsname.release);
> -        } else if (major < 2 || (major == 2 && minor < 35)) {
> -            VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel %s",
> -                      utsname.release);
> -        } else {
> +        if (ovs_kernel_is_version_or_newer(2, 35)) {
>              safe = true;
> +        } else {
> +            VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel");
>          }
>          ovsthread_once_done(&once);
>      }
> diff --git a/lib/util.c b/lib/util.c
> index 3fb3a4b40..5c31d983a 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -27,6 +27,7 @@
>  #include <string.h>
>  #ifdef __linux__
>  #include <sys/prctl.h>
> +#include <sys/utsname.h>
>  #endif
>  #include <sys/stat.h>
>  #include <unistd.h>
> @@ -2500,3 +2501,29 @@ OVS_CONSTRUCTOR(winsock_start) {
>     }
>  }
>  #endif
> +
> +#ifdef __linux__
> +bool
> +ovs_kernel_is_version_or_newer(int target_major, int target_minor)
> +{
> +    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
> +    static int current_major, current_minor = -1;
> +
> +    if (ovsthread_once_start(&once)) {
> +        struct utsname utsname;
> +
> +        if (uname(&utsname) == -1) {
> +            VLOG_WARN("uname failed (%s)", ovs_strerror(errno));
> +        } else if (!ovs_scan(utsname.release, "%d.%d",
> +                    &current_major, &current_minor)) {
> +            VLOG_WARN("uname reported bad OS release (%s)", utsname.release);
> +        }
> +        ovsthread_once_done(&once);
> +    }
> +    if (current_major == -1 || current_minor == -1) {
> +        return false;
> +    }
> +    return current_major > target_major || (
> +            current_major == target_major && current_minor >= target_minor);
> +}
> +#endif
> diff --git a/lib/util.h b/lib/util.h
> index f2d45bcac..55718fd87 100644
> --- a/lib/util.h
> +++ b/lib/util.h
> @@ -611,4 +611,8 @@ int ftruncate(int fd, off_t length);
>  }
>  #endif
>  
> +#ifdef __linux__
> +bool ovs_kernel_is_version_or_newer(int target_major, int target_minor);
> +#endif
> +
>  #endif /* util.h */
>
> base-commit: 33f45ded67a2d524ccf54cf4bb79a38d8140f14b
diff mbox series

Patch

diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c
index 1e904d8e6..25349c605 100644
--- a/lib/netdev-linux.c
+++ b/lib/netdev-linux.c
@@ -40,7 +40,6 @@ 
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/uio.h>
-#include <sys/utsname.h>
 #include <net/if.h>
 #include <net/if_arp.h>
 #include <net/route.h>
@@ -6428,18 +6427,10 @@  getqdisc_is_safe(void)
     static bool safe = false;
 
     if (ovsthread_once_start(&once)) {
-        struct utsname utsname;
-        int major, minor;
-
-        if (uname(&utsname) == -1) {
-            VLOG_WARN("uname failed (%s)", ovs_strerror(errno));
-        } else if (!ovs_scan(utsname.release, "%d.%d", &major, &minor)) {
-            VLOG_WARN("uname reported bad OS release (%s)", utsname.release);
-        } else if (major < 2 || (major == 2 && minor < 35)) {
-            VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel %s",
-                      utsname.release);
-        } else {
+        if (ovs_kernel_is_version_or_newer(2, 35)) {
             safe = true;
+        } else {
+            VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel");
         }
         ovsthread_once_done(&once);
     }
diff --git a/lib/util.c b/lib/util.c
index 3fb3a4b40..5c31d983a 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -27,6 +27,7 @@ 
 #include <string.h>
 #ifdef __linux__
 #include <sys/prctl.h>
+#include <sys/utsname.h>
 #endif
 #include <sys/stat.h>
 #include <unistd.h>
@@ -2500,3 +2501,29 @@  OVS_CONSTRUCTOR(winsock_start) {
    }
 }
 #endif
+
+#ifdef __linux__
+bool
+ovs_kernel_is_version_or_newer(int target_major, int target_minor)
+{
+    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+    static int current_major, current_minor = -1;
+
+    if (ovsthread_once_start(&once)) {
+        struct utsname utsname;
+
+        if (uname(&utsname) == -1) {
+            VLOG_WARN("uname failed (%s)", ovs_strerror(errno));
+        } else if (!ovs_scan(utsname.release, "%d.%d",
+                    &current_major, &current_minor)) {
+            VLOG_WARN("uname reported bad OS release (%s)", utsname.release);
+        }
+        ovsthread_once_done(&once);
+    }
+    if (current_major == -1 || current_minor == -1) {
+        return false;
+    }
+    return current_major > target_major || (
+            current_major == target_major && current_minor >= target_minor);
+}
+#endif
diff --git a/lib/util.h b/lib/util.h
index f2d45bcac..55718fd87 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -611,4 +611,8 @@  int ftruncate(int fd, off_t length);
 }
 #endif
 
+#ifdef __linux__
+bool ovs_kernel_is_version_or_newer(int target_major, int target_minor);
+#endif
+
 #endif /* util.h */