Skip to contentSkip to conversation

usbipd: enable SO_KEEPALIVE socket option for accepted connection

PATCH Superseded Vadym Hrynchyshyn 2 participants 4 messages May 16, 2025 – May 22, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 usbipd: enable SO_KEEPALIVE socket option for accepted connection

- If usbip client does not close a TCP/IP connection to a daemon gracefully, the daemon will keep this connection open indefinitely.

- The issue is that the client cannot attach this device again until the device will be rebinded on server side by commands:
usbip unbind -b X-Y
usbip bind -b X-Y

- usbip client enables tcp keepalive by calling usbip_net_set_keepalive from usbip_net_tcp_connect. usbip daemon now enables this socket option too for accepted connection.

---
 tools/usb/usbip/src/usbipd.c | 1 +
 1 file changed, 1 insertion(+)
tools/usb/usbip/src/usbipd.c +1 -0
index 48398a78e88a..d89633d8f799 100644
@@ -119,6 +119,7 @@ static int recv_request_import(int sockfd)
 	if (found) {
 		/* should set TCP_NODELAY for usbip */
 		usbip_net_set_nodelay(sockfd);
+		usbip_net_set_keepalive(sockfd);
 
 		/* export device needs a TCP/IP socket descriptor */
 		status = usbip_export_device(edev, sockfd);
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 usbipd: add long options to set TCP_KEEPIDLE/TCP_KEEPCNT/TCP_KEEPINTVL socket options

- Add usbip daemon options "--tcp-keepidle SECS", "--tcp-keepcnt CNT", "--tcp-keepintvl SECS". A user can add these parameters if default values do not satisfy him/her.

- If the option is passed, set corresponding socket option for accepted connection. For example, if "--tcp-keepidle 60" is passed, call setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, ...).

- configure.ac handles presence of these socket options. If TCP_KEEPIDLE/TCP_KEEPCNT/TCP_KEEPINTVL are not available, corresponding program options will not be added.

---
 tools/usb/usbip/configure.ac        | 24 +++++++-
 tools/usb/usbip/src/usbip_network.c | 85 +++++++++++++++++++++++++++++
 tools/usb/usbip/src/usbip_network.h | 14 +++++
 tools/usb/usbip/src/usbipd.c        | 59 ++++++++++++++++++--
 4 files changed, 176 insertions(+), 6 deletions(-)
tools/usb/usbip/configure.ac +23 -1
index 8debf934f8b7..439377cfe23b 100644
@@ -29,7 +29,7 @@ AC_PROG_MAKE_SET
 AC_HEADER_DIRENT
 AC_HEADER_STDC
 AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h netinet/in.h stdint.h stdlib.h dnl
-		  string.h sys/socket.h syslog.h unistd.h])
+		  string.h sys/socket.h syslog.h unistd.h netinet/tcp.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_TYPE_INT32_T
@@ -107,5 +107,27 @@ AC_ARG_WITH([fortify],
 			    dnl [ACTION-IF-NOT-GIVEN]
 			    [AC_MSG_RESULT([default])])
 
+AC_MSG_CHECKING([whether tcp keepalive options are available])
+AC_COMPILE_IFELSE(
+	[AC_LANG_PROGRAM(
+		[ #include <sys/socket.h>
+		  #include <arpa/inet.h>
+		  #include <netinet/tcp.h> ],
+		[
+			int n = 0;
+			setsockopt(-1, IPPROTO_TCP, TCP_KEEPIDLE, &n, sizeof(n));
+			setsockopt(-1, IPPROTO_TCP, TCP_KEEPCNT, &n, sizeof(n));
+			setsockopt(-1, IPPROTO_TCP, TCP_KEEPINTVL, &n, sizeof(n));
+		]
+	)],
+	[
+		AC_MSG_RESULT([yes])
+		AC_DEFINE([HAVE_KEEPALIVE_OPTS], [1],
+			  [Define to 1 if TCP_KEEPIDLE/TCP_KEEPCNT/TCP_KEEPINTVL
+			   socket-level options are available.])
+	],
+	[ AC_MSG_RESULT([no]) ],
+)
+
 AC_CONFIG_FILES([Makefile libsrc/Makefile src/Makefile])
 AC_OUTPUT
tools/usb/usbip/src/usbip_network.c +85 -0

97 lines not on this page · raw message

tools/usb/usbip/src/usbip_network.h +14 -0
index 83b4c5344f72..9611ddede0cf 100644
@@ -18,6 +18,17 @@ extern int usbip_port;
 extern char *usbip_port_string;
 void usbip_setup_port_number(char *arg);
 
+/**
+ * usbip_to_int - convert string to positive integer
+ * @name:	name of the @val for logging
+ * @val:	string to convert to integer using base 10
+ * @maxval:	max allowed result value
+ *
+ * Return: positive integer on success, zero if conversion
+ *	   is not possible or value is out of range [1...maxval].
+ */
+int usbip_to_int(const char *name, const char *val, int maxval);
+
 /* ---------------------------------------------------------------------- */
 /* Common header for all the kinds of PDUs. */
 struct op_common {
@@ -172,6 +183,9 @@ int usbip_net_recv_op_common(int sockfd, uint16_t *code, int *status);
 int usbip_net_set_reuseaddr(int sockfd);
 int usbip_net_set_nodelay(int sockfd);
 int usbip_net_set_keepalive(int sockfd);
+int usbip_net_set_keepidle(int sockfd, int seconds);
+int usbip_net_set_keepcnt(int sockfd, int cnt);
+int usbip_net_set_keepintvl(int sockfd, int seconds);
 int usbip_net_set_v6only(int sockfd);
 int usbip_net_tcp_connect(char *hostname, char *port);
 
tools/usb/usbip/src/usbipd.c +54 -5

115 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/3 usbipd: use usbip_to_int to simplify usbip_setup_port_number implementation

In reply to Vadym Hrynchyshyn

- usbip_to_int function is used for handling arguments of options --tcp-keepidle,--tcp-keepcnt,--tcp-keepintvl. It is now called from usbip_setup_port_number to handle tcp port number. This simplifies its implementation.

---
 tools/usb/usbip/src/usbip_network.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
tools/usb/usbip/src/usbip_network.c +5 -20
index 15787044a7c9..26559a11e648 100644
@@ -26,28 +26,13 @@ char *usbip_port_string = "3240";
 void usbip_setup_port_number(char *arg)
 {
 	dbg("parsing port arg '%s'", arg);
-	char *end;
-	unsigned long int port = strtoul(arg, &end, 10);
-
-	if (end == arg) {
-		err("port: could not parse '%s' as a decimal integer", arg);
-		return;
-	}
+	int port = usbip_to_int("port", arg, UINT16_MAX);
 
-	if (*end != '\0') {
-		err("port: garbage at end of '%s'", arg);
-		return;
+	if (port > 0) {
+		usbip_port = port;
+		usbip_port_string = arg;
+		info("using port %d (\"%s\")", usbip_port, usbip_port_string);
 	}
-
-	if (port > UINT16_MAX) {
-		err("port: %s too high (max=%d)",
-		    arg, UINT16_MAX);
-		return;
-	}
-
-	usbip_port = port;
-	usbip_port_string = arg;
-	info("using port %d (\"%s\")", usbip_port, usbip_port_string);
 }
 
 int usbip_to_int(const char *name, const char *val, int maxval)
Signature · 1 line
2.43.0
maintainer
#3 lore raw
In reply to Vadym Hrynchyshyn
Quoted 8 lines · Vadym Hrynchyshyn

- If usbip client does not close a TCP/IP connection to a daemon gracefully, the daemon will keep this connection open indefinitely.

- The issue is that the client cannot attach this device again until the device will be rebinded on server side by commands:
usbip unbind -b X-Y
usbip bind -b X-Y

Quoted 4 lines

- usbip client enables tcp keepalive by calling usbip_net_set_keepalive from usbip_net_tcp_connect. usbip daemon now enables this socket option too for accepted connection.

Please check a few change logs to for the correct format.

Quoted 11 lines
---
  tools/usb/usbip/src/usbipd.c | 1 +
  1 file changed, 1 insertion(+)
tools/usb/usbip/src/usbipd.c +0 -0
index 48398a78e88a..d89633d8f799 100644

tools/usb/usbip/src/usbipd.c static int recv_request_import(int sockfd) · line 125

Quoted 7 lines
tools/usb/usbip/src/usbipd.c +1 -0
@@ -119,6 +119,7 @@ static int recv_request_import(int sockfd)
  	if (found) {
  		/* should set TCP_NODELAY for usbip */
  		usbip_net_set_nodelay(sockfd);
+		usbip_net_set_keepalive(sockfd);
 
  		/* export device needs a TCP/IP socket descriptor */
  		status = usbip_export_device(edev, sockfd);

How did you find these problems - Also this series could use a cover letter explaining the changes - how these problems are found

thanks,
-- Shuah