Skip to contentSkip to conversation

linux-user: Add sockopts for SO_RCVTIMEO_NEW and SO_SNDTIMEO_NEW

PATCH In review Helge Deller 4 participants 8 messages Apr 24 – Apr 30

lore
Share & tools
lore
#1 lore raw

PATCH 1/4 linux-user: Use int64_t in target__kernel_sock_timeval

From: Helge Deller <[email protected]>

The Linux kernel uses the __s64 type for the tv_sec and tv_usec members of target__kernel_sock_timeval, so prefer the native type here as well.

---
 linux-user/syscall_defs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/syscall_defs.h +2 -2
index 6967306be4..138ce6abb1 100644
@@ -238,8 +238,8 @@ struct target_timeval {
 };
 
 struct target__kernel_sock_timeval {
-    abi_llong tv_sec;
-    abi_llong tv_usec;
+    int64_t tv_sec;
+    int64_t tv_usec;
 };
 #endif
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH 3/4 linux-user: Add setsockopt() for SO_RCVTIMEO_NEW and SO_SNDTIMEO_NEW

In reply to Helge Deller

From: Helge Deller <[email protected]>

Add handlers for both sockopts which use 64-bit time_t from userspace.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/885

---
 linux-user/syscall.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
linux-user/syscall.c +19 -2
index 077ecb7554..7bd5ad548b 100644
@@ -1143,7 +1143,6 @@ static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
     return 0;
 }
 
-#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
 static inline abi_long copy_from_user_timeval64(struct timeval *tv,
                                                 abi_ulong target_tv_addr)
 {
@@ -1160,7 +1159,6 @@ static inline abi_long copy_from_user_timeval64(struct timeval *tv,
 
     return 0;
 }
-#endif
 
 static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
                                               const struct timeval *tv)
@@ -2391,6 +2389,25 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
                                 &tv, sizeof(tv)));
                 return ret;
         }
+        case TARGET_SO_RCVTIMEO_NEW:
+        case TARGET_SO_SNDTIMEO_NEW:
+        {
+                struct timeval tv;
+
+                if (optlen != sizeof(struct target__kernel_sock_timeval)) {
+                    return -TARGET_EINVAL;
+                }
+
+                if (copy_from_user_timeval64(&tv, optval_addr)) {
+                    return -TARGET_EFAULT;
+                }
+
+                ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
+                                optname == TARGET_SO_RCVTIMEO_NEW ?
+                                    SO_RCVTIMEO : SO_SNDTIMEO,
+                                &tv, sizeof(tv)));
+                return ret;
+        }
         case TARGET_SO_ATTACH_FILTER:
         {
                 struct target_sock_fprog *tfprog;
Signature · 1 line
2.53.0
#6 lore raw

PATCH 4/4 linux-user: Add getsockopt() for SO_RCVTIMEO_NEW and SO_SNDTIMEO_NEW

In reply to Helge Deller

From: Helge Deller <[email protected]>

Add handlers for both sockopts which use 64-bit time_t from userspace.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/885

---
 linux-user/syscall.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
linux-user/syscall.c +14 -6
index 7bd5ad548b..a75dbceb5c 100644
@@ -2621,7 +2621,8 @@ static abi_long do_getsockopt(int sockfd, int level, int optname,
         /* These don't just return a single integer */
         case TARGET_SO_PEERNAME:
             goto unimplemented;
-        case TARGET_SO_RCVTIMEO: {
+        case TARGET_SO_RCVTIMEO:
+        case TARGET_SO_RCVTIMEO_NEW: {
             struct timeval tv;
             socklen_t tvlen;
 
@@ -2641,11 +2642,17 @@ get_timeout:
             if (ret < 0) {
                 return ret;
             }
-            if (len > sizeof(struct target_timeval)) {
-                len = sizeof(struct target_timeval);
-            }
-            if (copy_to_user_timeval(optval_addr, &tv)) {
-                return -TARGET_EFAULT;
+            if (len == sizeof(struct target__kernel_sock_timeval)) {
+                if (copy_to_user_timeval64(optval_addr, &tv)) {
+                    return -TARGET_EFAULT;
+                }
+            } else {
+                if (len >= sizeof(struct target_timeval)) {
+                    len = sizeof(struct target_timeval);
+                    if (copy_to_user_timeval(optval_addr, &tv)) {
+                        return -TARGET_EFAULT;
+                    }
+                }
             }
             if (put_user_u32(len, optlen)) {
                 return -TARGET_EFAULT;
@@ -2653,6 +2660,7 @@ get_timeout:
             break;
         }
         case TARGET_SO_SNDTIMEO:
+        case TARGET_SO_SNDTIMEO_NEW:
             optname = SO_SNDTIMEO;
             goto get_timeout;
         case TARGET_SO_PEERCRED: {
Signature · 1 line
2.53.0
#4 lore raw

PATCH 2/4 linux-user: Define SO_TIMESTAMP*_NEW and SO_RCVTIMEIO_NEW

In reply to Helge Deller

From: Helge Deller <[email protected]>

Define the entries which always use the 64-bit timestamps.

---
 linux-user/alpha/sockbits.h   | 7 +++++++
 linux-user/generic/sockbits.h | 8 ++++++++
 linux-user/hppa/sockbits.h    | 7 +++++++
 linux-user/mips/sockbits.h    | 7 +++++++
 linux-user/sparc/sockbits.h   | 7 +++++++
 5 files changed, 36 insertions(+)
linux-user/alpha/sockbits.h +7 -0
index d54dc98c09..0201ab9374 100644
@@ -75,6 +75,13 @@
 /* Instruct lower device to use last 4-bytes of skb data as FCS */
 #define TARGET_SO_NOFCS     43
 
+#define TARGET_SO_TIMESTAMP_NEW        63
+#define TARGET_SO_TIMESTAMPNS_NEW      64
+#define TARGET_SO_TIMESTAMPING_NEW     65
+
+#define TARGET_SO_RCVTIMEO_NEW         66
+#define TARGET_SO_SNDTIMEO_NEW         67
+
 /* TARGET_O_NONBLOCK clashes with the bits used for socket types.  Therefore we
  * have to define SOCK_NONBLOCK to a different value here.
  */
linux-user/generic/sockbits.h +8 -0
index b3b4a8e44c..33e6c3a572 100644
@@ -58,4 +58,12 @@
 
 #define TARGET_SO_PROTOCOL             38
 #define TARGET_SO_DOMAIN               39
+
+#define TARGET_SO_TIMESTAMP_NEW        63
+#define TARGET_SO_TIMESTAMPNS_NEW      64
+#define TARGET_SO_TIMESTAMPING_NEW     65
+
+#define TARGET_SO_RCVTIMEO_NEW         66
+#define TARGET_SO_SNDTIMEO_NEW         67
+
 #endif
linux-user/hppa/sockbits.h +7 -0
index 23f69a3293..2304dbbf79 100644
@@ -67,6 +67,13 @@
 
 #define TARGET_SO_CNX_ADVICE           0x402E
 
+#define TARGET_SO_TIMESTAMP_NEW        0x4038
+#define TARGET_SO_TIMESTAMPNS_NEW      0x4039
+#define TARGET_SO_TIMESTAMPING_NEW     0x403A
+
+#define TARGET_SO_RCVTIMEO_NEW         0x4040
+#define TARGET_SO_SNDTIMEO_NEW         0x4041
+
 /* TARGET_O_NONBLOCK clashes with the bits used for socket types.  Therefore we
  * have to define SOCK_NONBLOCK to a different value here.
  */
linux-user/mips/sockbits.h +7 -0
index 562cad88e2..1f479d54aa 100644
@@ -71,6 +71,13 @@
 #define TARGET_SO_RCVBUFFORCE          33
 #define TARGET_SO_PASSSEC              34
 
+#define TARGET_SO_TIMESTAMP_NEW        63
+#define TARGET_SO_TIMESTAMPNS_NEW      64
+#define TARGET_SO_TIMESTAMPING_NEW     65
+
+#define TARGET_SO_RCVTIMEO_NEW         66
+#define TARGET_SO_SNDTIMEO_NEW         67
+
 /** sock_type - Socket types
  *
  * Please notice that for binary compat reasons MIPS has to
linux-user/sparc/sockbits.h +7 -0
index 0a822e3e1f..42ecfdc8f9 100644
@@ -61,6 +61,13 @@
 #define TARGET_SO_TIMESTAMPING         0x0023
 #define TARGET_SCM_TIMESTAMPING        TARGET_SO_TIMESTAMPING
 
+#define TARGET_SO_TIMESTAMP_NEW        0x0046
+#define TARGET_SO_TIMESTAMPNS_NEW      0x0042
+#define TARGET_SO_TIMESTAMPING_NEW     0x0043
+
+#define TARGET_SO_RCVTIMEO_NEW         0x0044
+#define TARGET_SO_SNDTIMEO_NEW         0x0045
+
 #define TARGET_SO_RXQ_OVFL             0x0024
 
 #define TARGET_SO_WIFI_STATUS          0x0025
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/4] linux-user: Use int64_t in target__kernel_sock_timeval

In reply to Helge Deller
Quoted 15 lines · Helge Deller

From: Helge Deller <[email protected]>

The Linux kernel uses the __s64 type for the tv_sec and tv_usec members of target__kernel_sock_timeval, so prefer the native type here as well.

---
  linux-user/syscall_defs.h | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/syscall_defs.h +0 -0
index 6967306be4..138ce6abb1 100644

linux-user/syscall_defs.h struct target_timeval { · line 245

Quoted 10 lines
linux-user/syscall_defs.h +2 -2
@@ -238,8 +238,8 @@ struct target_timeval {
  };
 
  struct target__kernel_sock_timeval {
-    abi_llong tv_sec;
-    abi_llong tv_usec;
+    int64_t tv_sec;
+    int64_t tv_usec;
  };
  #endif
 

Nack. This has target alignment implications.

r~

#3 lore raw

Re: [PATCH 1/4] linux-user: Use int64_t in target__kernel_sock_timeval

Quoted 11 lines · Richard Henderson
Quoted 10 lines · Helge Deller

From: Helge Deller <[email protected]>

The Linux kernel uses the __s64 type for the tv_sec and tv_usec members of target__kernel_sock_timeval, so prefer the native type here as well.

---
  linux-user/syscall_defs.h | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 15 lines

linux-user/syscall_defs.h struct target_timeval { · line 243

Quoted 13 lines
linux-user/syscall_defs.h +2 -2
index 6967306be4..138ce6abb1 100644
@@ -238,8 +238,8 @@ struct target_timeval {
   };
   struct target__kernel_sock_timeval {
-    abi_llong tv_sec;
-    abi_llong tv_usec;
+    int64_t tv_sec;
+    int64_t tv_usec;
   };
   #endif

Nack. This has target alignment implications.

Oh, now I see:
typedef uint64_t abi_ullong __attribute__((aligned(ABI_LLONG_ALIGNMENT)));

I drop that patch.

Thanks!
Helge

#7 lore raw
In reply to Helge Deller
Quoted 4 lines · Helge Deller

This series should fix
https://gitlab.com/qemu-project/qemu/-/work_items/885

Please review.

I'm picking this series up for qemu-stable as well, despite it being a "new feature", so to say, since with the situation before this series, things will break for more recent linux kernel and userspace.

Hopefully it will work in qemu 10.0.x series too, which still had support for 32bit hosts.. do we have testcase for the whole thing?

Sigh, that was very unfortunate naming, why didn't they used something like SO_RCVTIMEO_T64??.. But this has nothing to do with this patch series..

Thanks,

/mjt