svn commit: r215178 - in head: lib/libc/sys sys/kern sys/sys

Luigi Rizzo luigi at
Fri Nov 12 13:02:27 UTC 2010

Author: luigi
Date: Fri Nov 12 13:02:26 2010
New Revision: 215178

  This commit implements the SO_USER_COOKIE socket option, which lets
  you tag a socket with an uint32_t value. The cookie can then be
  used by the kernel for various purposes, e.g. setting the skipto
  rule or pipe number in ipfw (this is the reason SO_USER_COOKIE has
  been implemented; however there is nothing ipfw-specific in its
  The ipfw-related code that uses the optopn will be committed separately.
  This change adds a field to 'struct socket', but the struct is not
  part of any driver or userland-visible ABI so the change should be
  See the discussion at
  Idea and code from Paul Joe, small modifications and manpage
  changes by myself.
  Submitted by:	Paul Joe
  MFC after:	1 week


Modified: head/lib/libc/sys/getsockopt.2
--- head/lib/libc/sys/getsockopt.2	Fri Nov 12 12:48:41 2010	(r215177)
+++ head/lib/libc/sys/getsockopt.2	Fri Nov 12 13:02:26 2010	(r215178)
@@ -184,15 +184,18 @@ The following options are recognized in
 .It Dv SO_LISTENQLIMIT Ta "get backlog limit of the socket (get only)"
 .It Dv SO_LISTENQLEN Ta "get complete queue length of the socket (get only)"
 .It Dv SO_LISTENINCQLEN Ta "get incomplete queue length of the socket (get only)"
+.It Dv SO_USER_COOKIE Ta "set the 'so_user_cookie' value for the socket (uint32_t, set only)"
 enables debugging in the underlying protocol modules.
 indicates that the rules used in validating addresses supplied
 in a
 .Xr bind 2
 system call should allow reuse of local addresses.
 allows completely duplicate bindings by multiple processes
 if they all set
@@ -200,6 +203,7 @@ if they all set
 before binding the port.
 This option permits multiple instances of a program to each
 receive UDP/IP multicast or broadcast datagrams destined for the bound port.
 enables the
 periodic transmission of messages on a connected socket.
@@ -208,6 +212,7 @@ connected party fail to respond to these
 considered broken and processes using the socket are notified via a
 signal when attempting to send data.
 indicates that outgoing messages should
 bypass the standard routing facilities.
@@ -244,6 +249,7 @@ The option
 requests permission to send broadcast datagrams
 on the socket.
 Broadcast was a privileged operation in earlier versions of the system.
 With protocols that support out-of-band data, the
@@ -256,6 +262,7 @@ calls without the
 Some protocols always behave as if this option is set.
@@ -285,6 +292,7 @@ only if the low water mark amount could 
 The default value for
 is set to a convenient size for network efficiency, often 1024.
 is an option to set the minimum count for input operations.
 In general, receive calls will block until any (non-zero) amount of data
@@ -317,6 +325,7 @@ In the current implementation, this time
 data are delivered to the protocol,
 implying that the limit applies to output portions ranging in size
 from the low water mark to the high water mark for output.
 is an option to set a timeout value for input operations.
 It accepts a
@@ -338,6 +347,15 @@ The value must be from 0 to one less tha
 the sysctl
 .Em net.fibs .
+can be used to set the uint32_t so_user_cookie field in the socket.
+The value is an uint32_t, and can be used in the kernel code that
+manipulates traffic related to the socket.
+The default value for the field is 0.
+As an example, the value can be used as the skipto target or
+pipe number in
+.Nm ipfw/dummynet .
 places an
 .Xr accept_filter 9

Modified: head/sys/kern/uipc_socket.c
--- head/sys/kern/uipc_socket.c	Fri Nov 12 12:48:41 2010	(r215177)
+++ head/sys/kern/uipc_socket.c	Fri Nov 12 13:02:26 2010	(r215178)
@@ -2386,6 +2386,7 @@ sosetopt(struct socket *so, struct socko
 	struct	linger l;
 	struct	timeval tv;
 	u_long  val;
+	uint32_t val32;
 #ifdef MAC
 	struct mac extmac;
@@ -2461,6 +2462,15 @@ sosetopt(struct socket *so, struct socko
 				so->so_fibnum = 0;
+			error = sooptcopyin(sopt, &val32, sizeof val32,
+					    sizeof val32);
+			if (error)
+				goto bad;
+			so->so_user_cookie = val32;
+			break;
 		case SO_SNDBUF:
 		case SO_RCVBUF:
 		case SO_SNDLOWAT:

Modified: head/sys/sys/socket.h
--- head/sys/sys/socket.h	Fri Nov 12 12:48:41 2010	(r215177)
+++ head/sys/sys/socket.h	Fri Nov 12 13:02:26 2010	(r215178)
@@ -137,6 +137,7 @@ typedef	__uid_t		uid_t;
 #define	SO_LISTENQLEN	0x1012		/* socket's complete queue length */
 #define	SO_LISTENINCQLEN	0x1013	/* socket's incomplete queue length */
 #define	SO_SETFIB	0x1014		/* use this FIB to route */
+#define	SO_USER_COOKIE	0x1015		/* user cookie (dummynet etc.) */

Modified: head/sys/sys/socketvar.h
--- head/sys/sys/socketvar.h	Fri Nov 12 12:48:41 2010	(r215177)
+++ head/sys/sys/socketvar.h	Fri Nov 12 13:02:26 2010	(r215178)
@@ -117,7 +117,14 @@ struct socket {
 		void	*so_accept_filter_arg;	/* saved filter args */
 		char	*so_accept_filter_str;	/* saved user args */
 	} *so_accf;
+	/*
+	 * so_fibnum, so_user_cookie and friends can be used to attach
+	 * some user-specified metadata to a socket, which then can be
+	 * used by the kernel for various actions.
+	 * so_user_cookie is used by ipfw/dummynet.
+	 */
 	int so_fibnum;		/* routing domain for this socket */
+	uint32_t so_user_cookie;

More information about the svn-src-all mailing list