Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 12 Nov 2010 13:02:26 +0000 (UTC)
From:      Luigi Rizzo <luigi@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r215178 - in head: lib/libc/sys sys/kern sys/sys
Message-ID:  <201011121302.oACD2Qjt009385@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: luigi
Date: Fri Nov 12 13:02:26 2010
New Revision: 215178
URL: http://svn.freebsd.org/changeset/base/215178

Log:
  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
  implementation).
  
  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
  harmless.
  
  See the discussion at
  http://lists.freebsd.org/pipermail/freebsd-ipfw/2009-October/004001.html
  
  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/sys/kern/uipc_socket.c
  head/sys/sys/socket.h
  head/sys/sys/socketvar.h

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)"
 .El
 .Pp
 .Dv SO_DEBUG
 enables debugging in the underlying protocol modules.
+.Pp
 .Dv SO_REUSEADDR
 indicates that the rules used in validating addresses supplied
 in a
 .Xr bind 2
 system call should allow reuse of local addresses.
+.Pp
 .Dv SO_REUSEPORT
 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.
+.Pp
 .Dv SO_KEEPALIVE
 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
 .Dv SIGPIPE
 signal when attempting to send data.
+.Pp
 .Dv SO_DONTROUTE
 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.
+.Pp
 With protocols that support out-of-band data, the
 .Dv SO_OOBINLINE
 option
@@ -256,6 +262,7 @@ calls without the
 .Dv MSG_OOB
 flag.
 Some protocols always behave as if this option is set.
+.Pp
 .Dv SO_SNDBUF
 and
 .Dv SO_RCVBUF
@@ -285,6 +292,7 @@ only if the low water mark amount could 
 The default value for
 .Dv SO_SNDLOWAT
 is set to a convenient size for network efficiency, often 1024.
+.Pp
 .Dv SO_RCVLOWAT
 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.
+.Pp
 .Dv SO_RCVTIMEO
 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 .
 .Pp
+.Dv SO_USER_COOKIE
+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 .
+.Pp
 .Dv SO_ACCEPTFILTER
 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;
 #endif
@@ -2461,6 +2462,15 @@ sosetopt(struct socket *so, struct socko
 				so->so_fibnum = 0;
 			}
 			break;
+
+		case SO_USER_COOKIE:
+			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.) */
 #endif
 
 /*

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;
 };
 
 /*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201011121302.oACD2Qjt009385>