xref: /titanic_54/usr/src/uts/common/sys/socketvar.h (revision 4117443759eb8485e3cfd93459f86a41ea241d20)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #ifndef _SYS_SOCKETVAR_H
41 #define	_SYS_SOCKETVAR_H
42 
43 #include <sys/types.h>
44 #include <sys/stream.h>
45 #include <sys/t_lock.h>
46 #include <sys/cred.h>
47 #include <sys/vnode.h>
48 #include <sys/file.h>
49 #include <sys/param.h>
50 #include <sys/zone.h>
51 #include <sys/sdt.h>
52 #include <sys/modctl.h>
53 #include <sys/atomic.h>
54 #include <sys/socket.h>
55 #include <sys/ksocket.h>
56 #include <sys/sodirect.h>
57 
58 #ifdef	__cplusplus
59 extern "C" {
60 #endif
61 
62 /*
63  * Internal representation of the address used to represent addresses
64  * in the loopback transport for AF_UNIX. While the sockaddr_un is used
65  * as the sockfs layer address for AF_UNIX the pathnames contained in
66  * these addresses are not unique (due to relative pathnames) thus can not
67  * be used in the transport.
68  *
69  * The transport level address consists of a magic number (used to separate the
70  * name space for specific and implicit binds). For a specific bind
71  * this is followed by a "vnode *" which ensures that all specific binds
72  * have a unique transport level address. For implicit binds the latter
73  * part of the address is a byte string (of the same length as a pointer)
74  * that is assigned by the loopback transport.
75  *
76  * The uniqueness assumes that the loopback transport has a separate namespace
77  * for sockets in order to avoid name conflicts with e.g. TLI use of the
78  * same transport.
79  */
80 struct so_ux_addr {
81 	void	*soua_vp;	/* vnode pointer or assigned by tl */
82 	uint_t	soua_magic;	/* See below */
83 };
84 
85 #define	SOU_MAGIC_EXPLICIT	0x75787670	/* "uxvp" */
86 #define	SOU_MAGIC_IMPLICIT	0x616e6f6e	/* "anon" */
87 
88 struct sockaddr_ux {
89 	sa_family_t		sou_family;	/* AF_UNIX */
90 	struct so_ux_addr	sou_addr;
91 };
92 
93 #if defined(_KERNEL) || defined(_KMEMUSER)
94 
95 #include <sys/socket_proto.h>
96 
97 typedef struct sonodeops sonodeops_t;
98 typedef struct sonode sonode_t;
99 
100 /*
101  * The sonode represents a socket. A sonode never exist in the file system
102  * name space and can not be opened using open() - only the socket, socketpair
103  * and accept calls create sonodes.
104  *
105  * The locking of sockfs uses the so_lock mutex plus the SOLOCKED and
106  * SOREADLOCKED flags in so_flag. The mutex protects all the state in the
107  * sonode. It is expected that the underlying transport protocol serializes
108  * socket operations, so sockfs will not normally not single-thread
109  * operations. However, certain sockets, including TPI based ones, can only
110  * handle one control operation at a time. The SOLOCKED flag is used to
111  * single-thread operations from sockfs users to prevent e.g. multiple bind()
112  * calls to operate on the same sonode concurrently. The SOREADLOCKED flag is
113  * used to ensure that only one thread sleeps in kstrgetmsg for a given
114  * sonode. This is needed to ensure atomic operation for things like
115  * MSG_WAITALL.
116  *
117  * The so_fallback_rwlock is used to ensure that for sockets that can
118  * fall back to TPI, the fallback is not initiated until all pending
119  * operations have completed.
120  *
121  * Note that so_lock is sometimes held across calls that might go to sleep
122  * (kmem_alloc and soallocproto*). This implies that no other lock in
123  * the system should be held when calling into sockfs; from the system call
124  * side or from strrput (in case of TPI based sockets). If locks are held
125  * while calling into sockfs the system might hang when running low on memory.
126  */
127 struct sonode {
128 	struct	vnode	*so_vnode;	/* vnode associated with this sonode */
129 
130 	sonodeops_t 	*so_ops;	/* operations vector for this sonode */
131 	void		*so_priv;	/* sonode private data */
132 
133 	krwlock_t	so_fallback_rwlock;
134 	kmutex_t	so_lock;	/* protects sonode fields */
135 
136 	kcondvar_t	so_state_cv;	/* synchronize state changes */
137 	kcondvar_t	so_want_cv;	/* wait due to SOLOCKED */
138 
139 	/* These fields are protected by so_lock */
140 
141 	uint_t		so_state;	/* internal state flags SS_*, below */
142 	uint_t		so_mode;	/* characteristics on socket. SM_* */
143 	ushort_t 	so_flag;	/* flags, see below */
144 	int		so_count;	/* count of opened references */
145 
146 	sock_connid_t	so_proto_connid; /* protocol generation number */
147 
148 	ushort_t 	so_error;	/* error affecting connection */
149 
150 	struct sockparams *so_sockparams;	/* vnode or socket module */
151 	/* Needed to recreate the same socket for accept */
152 	short	so_family;
153 	short	so_type;
154 	short	so_protocol;
155 	short	so_version;		/* From so_socket call */
156 
157 	/* Accept queue */
158 	kmutex_t	so_acceptq_lock;	/* protects accept queue */
159 	struct sonode	*so_acceptq_next;	/* acceptq list node */
160 	struct sonode 	*so_acceptq_head;
161 	struct sonode	**so_acceptq_tail;
162 	unsigned int	so_acceptq_len;
163 	unsigned int	so_backlog;		/* Listen backlog */
164 	kcondvar_t	so_acceptq_cv;		/* wait for new conn. */
165 
166 	/* Options */
167 	short	so_options;		/* From socket call, see socket.h */
168 	struct linger	so_linger;	/* SO_LINGER value */
169 #define	so_sndbuf	so_proto_props.sopp_txhiwat	/* SO_SNDBUF value */
170 #define	so_sndlowat	so_proto_props.sopp_txlowat	/* tx low water mark */
171 #define	so_rcvbuf	so_proto_props.sopp_rxhiwat	/* SO_RCVBUF value */
172 #define	so_rcvlowat	so_proto_props.sopp_rxlowat	/* rx low water mark */
173 #define	so_max_addr_len	so_proto_props.sopp_maxaddrlen
174 #define	so_minpsz	so_proto_props.sopp_minpsz
175 #define	so_maxpsz	so_proto_props.sopp_maxpsz
176 
177 	int	so_xpg_rcvbuf;		/* SO_RCVBUF value for XPG4 socket */
178 	clock_t	so_sndtimeo;		/* send timeout */
179 	clock_t	so_rcvtimeo;		/* recv timeout */
180 
181 	mblk_t	*so_oobmsg;		/* outofline oob data */
182 	ssize_t	so_oobmark;		/* offset of the oob data */
183 
184 	pid_t	so_pgrp;		/* pgrp for signals */
185 
186 	cred_t		*so_peercred;	/* connected socket peer cred */
187 	pid_t		so_cpid;	/* connected socket peer cached pid */
188 	zoneid_t	so_zoneid;	/* opener's zoneid */
189 
190 	struct pollhead	so_poll_list;	/* common pollhead */
191 	short		so_pollev;	/* events that should be generated */
192 
193 	/* Receive */
194 	unsigned int	so_rcv_queued;	/* # bytes on both rcv lists */
195 	mblk_t		*so_rcv_q_head;	/* processing/copyout rcv queue */
196 	mblk_t		*so_rcv_q_last_head;
197 	mblk_t		*so_rcv_head;	/* protocol prequeue */
198 	mblk_t		*so_rcv_last_head;	/* last mblk in b_next chain */
199 	kcondvar_t	so_rcv_cv;	/* wait for data */
200 	uint_t		so_rcv_wanted;	/* # of bytes wanted by app */
201 	timeout_id_t	so_rcv_timer_tid;
202 
203 #define	so_rcv_thresh	so_proto_props.sopp_rcvthresh
204 #define	so_rcv_timer_interval so_proto_props.sopp_rcvtimer
205 
206 	kcondvar_t	so_snd_cv;	/* wait for snd buffers */
207 	uint32_t
208 		so_snd_qfull: 1,	/* Transmit full */
209 		so_rcv_wakeup: 1,
210 		so_snd_wakeup: 1,
211 		so_not_str: 1,	/* B_TRUE if not streams based socket */
212 		so_pad_to_bit_31: 28;
213 
214 	/* Communication channel with protocol */
215 	sock_lower_handle_t	so_proto_handle;
216 	sock_downcalls_t 	*so_downcalls;
217 
218 	struct sock_proto_props	so_proto_props; /* protocol settings */
219 	boolean_t		so_flowctrld;	/* Flow controlled */
220 	uint_t			so_copyflag;	/* Copy related flag */
221 	kcondvar_t		so_copy_cv;	/* Copy cond variable */
222 
223 	/* kernel sockets */
224 	ksocket_callbacks_t 	so_ksock_callbacks;
225 	void			*so_ksock_cb_arg;	/* callback argument */
226 	kcondvar_t		so_closing_cv;
227 
228 	/* != NULL for sodirect_t enabled socket */
229 	sodirect_t		*so_direct;
230 };
231 
232 #define	SO_HAVE_DATA(so)						\
233 	/*								\
234 	 * For the (tid == 0) case we must check so_rcv_{q_,}head	\
235 	 * rather than (so_rcv_queued > 0), since the latter does not	\
236 	 * take into account mblks with only control/name information.	\
237 	 */								\
238 	((so)->so_rcv_timer_tid == 0 && ((so)->so_rcv_head != NULL ||	\
239 	(so)->so_rcv_q_head != NULL)) ||				\
240 	((so)->so_state & SS_CANTRCVMORE)
241 
242 /*
243  * Events handled by the protocol (in case sd_poll is set)
244  */
245 #define	SO_PROTO_POLLEV		(POLLIN|POLLRDNORM|POLLRDBAND)
246 
247 
248 #endif /* _KERNEL || _KMEMUSER */
249 
250 /* flags */
251 #define	SOMOD		0x0001		/* update socket modification time */
252 #define	SOACC		0x0002		/* update socket access time */
253 
254 #define	SOLOCKED	0x0010		/* use to serialize open/closes */
255 #define	SOREADLOCKED	0x0020		/* serialize kstrgetmsg calls */
256 #define	SOWANT		0x0040		/* some process waiting on lock */
257 #define	SOCLONE		0x0080		/* child of clone driver */
258 #define	SOASYNC_UNBIND	0x0100		/* wait for ACK of async unbind */
259 
260 #define	SOCK_IS_NONSTR(so)	((so)->so_not_str)
261 
262 /*
263  * Socket state bits.
264  */
265 #define	SS_ISCONNECTED		0x00000001 /* socket connected to a peer */
266 #define	SS_ISCONNECTING		0x00000002 /* in process, connecting to peer */
267 #define	SS_ISDISCONNECTING	0x00000004 /* in process of disconnecting */
268 #define	SS_CANTSENDMORE		0x00000008 /* can't send more data to peer */
269 
270 #define	SS_CANTRCVMORE		0x00000010 /* can't receive more data */
271 #define	SS_ISBOUND		0x00000020 /* socket is bound */
272 #define	SS_NDELAY		0x00000040 /* FNDELAY non-blocking */
273 #define	SS_NONBLOCK		0x00000080 /* O_NONBLOCK non-blocking */
274 
275 #define	SS_ASYNC		0x00000100 /* async i/o notify */
276 #define	SS_ACCEPTCONN		0x00000200 /* listen done */
277 /*	unused			0x00000400 */	/* was SS_HASCONNIND */
278 #define	SS_SAVEDEOR		0x00000800 /* Saved MSG_EOR rcv side state */
279 
280 #define	SS_RCVATMARK		0x00001000 /* at mark on input */
281 #define	SS_OOBPEND		0x00002000 /* OOB pending or present - poll */
282 #define	SS_HAVEOOBDATA		0x00004000 /* OOB data present */
283 #define	SS_HADOOBDATA		0x00008000 /* OOB data consumed */
284 #define	SS_CLOSING		0x00010000 /* in process of closing */
285 
286 /*	unused			0x00020000 */	/* was SS_FADDR_NOXLATE */
287 /*	unused			0x00040000 */	/* was SS_HASDATA */
288 /*	unused 			0x00080000 */	/* was SS_DONEREAD */
289 /*	unused 			0x00100000 */	/* was SS_MOREDATA */
290 /*	unused 			0x00200000 */	/* was SS_DIRECT */
291 
292 #define	SS_SODIRECT		0x00400000 /* transport supports sodirect */
293 
294 #define	SS_SENTLASTREADSIG	0x01000000 /* last rx signal has been sent */
295 #define	SS_SENTLASTWRITESIG	0x02000000 /* last tx signal has been sent */
296 
297 #define	SS_FALLBACK_DRAIN	0x20000000 /* data was/is being drained */
298 #define	SS_FALLBACK_PENDING	0x40000000 /* fallback is pending */
299 #define	SS_FALLBACK_COMP	0x80000000 /* fallback has completed */
300 
301 
302 /* Set of states when the socket can't be rebound */
303 #define	SS_CANTREBIND	(SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING|\
304 			    SS_CANTSENDMORE|SS_CANTRCVMORE|SS_ACCEPTCONN)
305 
306 /*
307  * Sockets that can fall back to TPI must ensure that fall back is not
308  * initiated while a thread is using a socket.
309  */
310 #define	SO_BLOCK_FALLBACK(so, fn) {			\
311 	ASSERT(MUTEX_NOT_HELD(&(so)->so_lock));		\
312 	rw_enter(&(so)->so_fallback_rwlock, RW_READER);	\
313 	if ((so)->so_state & SS_FALLBACK_COMP) {	\
314 		rw_exit(&(so)->so_fallback_rwlock);	\
315 		return (fn);				\
316 	}						\
317 }
318 
319 #define	SO_UNBLOCK_FALLBACK(so)	{			\
320 	rw_exit(&(so)->so_fallback_rwlock);		\
321 }
322 
323 /* Poll events */
324 #define	SO_POLLEV_IN		0x1	/* POLLIN wakeup needed */
325 #define	SO_POLLEV_ALWAYS	0x2	/* wakeups */
326 
327 /*
328  * Characteristics of sockets. Not changed after the socket is created.
329  */
330 #define	SM_PRIV			0x001	/* privileged for broadcast, raw... */
331 #define	SM_ATOMIC		0x002	/* atomic data transmission */
332 #define	SM_ADDR			0x004	/* addresses given with messages */
333 #define	SM_CONNREQUIRED		0x008	/* connection required by protocol */
334 
335 #define	SM_FDPASSING		0x010	/* passes file descriptors */
336 #define	SM_EXDATA		0x020	/* Can handle T_EXDATA_REQ */
337 #define	SM_OPTDATA		0x040	/* Can handle T_OPTDATA_REQ */
338 #define	SM_BYTESTREAM		0x080	/* Byte stream - can use M_DATA */
339 
340 #define	SM_ACCEPTOR_ID		0x100	/* so_acceptor_id is valid */
341 
342 #define	SM_KERNEL		0x200	/* kernel socket */
343 
344 /* The modes below are only for non-streams sockets */
345 #define	SM_ACCEPTSUPP		0x400	/* can handle accept() */
346 #define	SM_SENDFILESUPP		0x800	/* Private: proto supp sendfile  */
347 
348 /*
349  * Socket versions. Used by the socket library when calling _so_socket().
350  */
351 #define	SOV_STREAM	0	/* Not a socket - just a stream */
352 #define	SOV_DEFAULT	1	/* Select based on so_default_version */
353 #define	SOV_SOCKSTREAM	2	/* Socket plus streams operations */
354 #define	SOV_SOCKBSD	3	/* Socket with no streams operations */
355 #define	SOV_XPG4_2	4	/* Xnet socket */
356 
357 #if defined(_KERNEL) || defined(_KMEMUSER)
358 
359 /*
360  * sonode create and destroy functions.
361  */
362 typedef struct sonode *(*so_create_func_t)(struct sockparams *,
363     int, int, int, int, int, int *, cred_t *);
364 typedef void (*so_destroy_func_t)(struct sonode *);
365 
366 /* STREAM device information */
367 typedef struct sdev_info {
368 	char	*sd_devpath;
369 	int	sd_devpathlen; /* Is 0 if sp_devpath is a static string */
370 	vnode_t	*sd_vnode;
371 } sdev_info_t;
372 
373 #define	SOCKMOD_VERSION		1
374 /* name of the TPI pseudo socket module */
375 #define	SOTPI_SMOD_NAME		"socktpi"
376 
377 typedef struct __smod_priv_s {
378 	so_create_func_t	smodp_sock_create_func;
379 	so_destroy_func_t	smodp_sock_destroy_func;
380 	so_proto_fallback_func_t smodp_proto_fallback_func;
381 } __smod_priv_t;
382 
383 /*
384  * Socket module register information
385  */
386 typedef struct smod_reg_s {
387 	int		smod_version;
388 	char		*smod_name;
389 	size_t		smod_uc_version;
390 	size_t		smod_dc_version;
391 	so_proto_create_func_t	smod_proto_create_func;
392 
393 	/* __smod_priv_data must be NULL */
394 	__smod_priv_t	*__smod_priv;
395 } smod_reg_t;
396 
397 /*
398  * Socket module information
399  */
400 typedef struct smod_info {
401 	int		smod_version;
402 	char		*smod_name;
403 	uint_t		smod_refcnt;		/* # of entries */
404 	size_t		smod_uc_version; 	/* upcall version */
405 	size_t		smod_dc_version;	/* down call version */
406 	so_proto_create_func_t	smod_proto_create_func;
407 	so_proto_fallback_func_t smod_proto_fallback_func;
408 	so_create_func_t	smod_sock_create_func;
409 	so_destroy_func_t	smod_sock_destroy_func;
410 	list_node_t	smod_node;
411 } smod_info_t;
412 
413 /*
414  * sockparams
415  *
416  * Used for mapping family/type/protocol to module
417  */
418 struct sockparams {
419 	/*
420 	 * The family, type, protocol, sdev_info and smod_info are
421 	 * set when the entry is created, and they will never change
422 	 * thereafter.
423 	 */
424 	int		sp_family;
425 	int		sp_type;
426 	int		sp_protocol;
427 
428 	sdev_info_t	sp_sdev_info;	/* STREAM device */
429 	char		*sp_smod_name;	/* socket module name */
430 	smod_info_t	*sp_smod_info;	/* socket module */
431 
432 	kmutex_t	sp_lock;	/* lock for refcnt */
433 	uint64_t	sp_refcnt;	/* entry reference count */
434 
435 	/*
436 	 * The entries below are only modified while holding
437 	 * splist_lock as a writer.
438 	 */
439 	int		sp_flags;	/* see below */
440 	list_node_t	sp_node;
441 };
442 
443 
444 /*
445  * sockparams flags
446  */
447 #define	SOCKPARAMS_EPHEMERAL	0x1	/* temp. entry, not on global list */
448 
449 extern void sockparams_init(void);
450 extern struct sockparams *sockparams_hold_ephemeral_bydev(int, int, int,
451     const char *, int, int *);
452 extern struct sockparams *sockparams_hold_ephemeral_bymod(int, int, int,
453     const char *, int, int *);
454 extern void sockparams_ephemeral_drop_last_ref(struct sockparams *);
455 
456 extern void smod_init(void);
457 extern void smod_add(smod_info_t *);
458 extern int smod_register(const smod_reg_t *);
459 extern int smod_unregister(const char *);
460 extern smod_info_t *smod_lookup_byname(const char *);
461 
462 #define	SOCKPARAMS_HAS_DEVICE(sp)					\
463 	((sp)->sp_sdev_info.sd_devpath != NULL)
464 
465 /* Increase the smod_info_t reference count */
466 #define	SMOD_INC_REF(smodp) {						\
467 	ASSERT((smodp) != NULL);					\
468 	DTRACE_PROBE1(smodinfo__inc__ref, struct smod_info *, (smodp));	\
469 	atomic_inc_uint(&(smodp)->smod_refcnt);				\
470 }
471 
472 /*
473  * Decreace the socket module entry reference count.
474  * When no one mapping to the entry, we try to unload the module from the
475  * kernel. If the module can't unload, just leave the module entry with
476  * a zero refcnt.
477  */
478 #define	SMOD_DEC_REF(sp, smodp) {					\
479 	ASSERT((smodp) != NULL);					\
480 	ASSERT((smodp)->smod_refcnt != 0);				\
481 	atomic_dec_uint(&(smodp)->smod_refcnt);				\
482 	/*								\
483 	 * No need to atomically check the return value because the	\
484 	 * socket module framework will verify that no one is using	\
485 	 * the module before unloading. Worst thing that can happen	\
486 	 * here is multiple calls to mod_remove_by_name(), which is OK.	\
487 	 */								\
488 	if ((smodp)->smod_refcnt == 0)					\
489 		(void) mod_remove_by_name((sp)->sp_smod_name);		\
490 }
491 
492 /* Increase the reference count */
493 #define	SOCKPARAMS_INC_REF(sp) {					\
494 	ASSERT((sp) != NULL);						\
495 	DTRACE_PROBE1(sockparams__inc__ref, struct sockparams *, (sp));	\
496 	mutex_enter(&(sp)->sp_lock);					\
497 	(sp)->sp_refcnt++;						\
498 	ASSERT((sp)->sp_refcnt != 0);					\
499 	mutex_exit(&(sp)->sp_lock);					\
500 }
501 
502 /*
503  * Decrease the reference count.
504  *
505  * If the sockparams is ephemeral, then the thread dropping the last ref
506  * count will destroy the entry.
507  */
508 #define	SOCKPARAMS_DEC_REF(sp) {					\
509 	ASSERT((sp) != NULL);						\
510 	DTRACE_PROBE1(sockparams__dec__ref, struct sockparams *, (sp));	\
511 	mutex_enter(&(sp)->sp_lock);					\
512 	ASSERT((sp)->sp_refcnt > 0);					\
513 	if ((sp)->sp_refcnt == 1) {					\
514 		if ((sp)->sp_flags & SOCKPARAMS_EPHEMERAL) {		\
515 			mutex_exit(&(sp)->sp_lock);			\
516 			sockparams_ephemeral_drop_last_ref((sp));	\
517 		} else {						\
518 			(sp)->sp_refcnt--;				\
519 			if ((sp)->sp_smod_info != NULL)			\
520 				SMOD_DEC_REF(sp, (sp)->sp_smod_info);	\
521 			(sp)->sp_smod_info = NULL;			\
522 			mutex_exit(&(sp)->sp_lock);			\
523 		}							\
524 	} else {							\
525 		(sp)->sp_refcnt--;					\
526 		mutex_exit(&(sp)->sp_lock);				\
527 	}								\
528 }
529 
530 /*
531  * Used to traverse the list of AF_UNIX sockets to construct the kstat
532  * for netstat(1m).
533  */
534 struct socklist {
535 	kmutex_t	sl_lock;
536 	struct sonode	*sl_list;
537 };
538 
539 extern struct socklist socklist;
540 /*
541  * ss_full_waits is the number of times the reader thread
542  * waits when the queue is full and ss_empty_waits is the number
543  * of times the consumer thread waits when the queue is empty.
544  * No locks for these as they are just indicators of whether
545  * disk or network or both is slow or fast.
546  */
547 struct sendfile_stats {
548 	uint32_t ss_file_cached;
549 	uint32_t ss_file_not_cached;
550 	uint32_t ss_full_waits;
551 	uint32_t ss_empty_waits;
552 	uint32_t ss_file_segmap;
553 };
554 
555 /*
556  * A single sendfile request is represented by snf_req.
557  */
558 typedef struct snf_req {
559 	struct snf_req	*sr_next;
560 	mblk_t		*sr_mp_head;
561 	mblk_t		*sr_mp_tail;
562 	kmutex_t	sr_lock;
563 	kcondvar_t	sr_cv;
564 	uint_t		sr_qlen;
565 	int		sr_hiwat;
566 	int		sr_lowat;
567 	int		sr_operation;
568 	struct vnode	*sr_vp;
569 	file_t 		*sr_fp;
570 	ssize_t		sr_maxpsz;
571 	u_offset_t	sr_file_off;
572 	u_offset_t	sr_file_size;
573 #define	SR_READ_DONE	0x80000000
574 	int		sr_read_error;
575 	int		sr_write_error;
576 } snf_req_t;
577 
578 /* A queue of sendfile requests */
579 struct sendfile_queue {
580 	snf_req_t	*snfq_req_head;
581 	snf_req_t	*snfq_req_tail;
582 	kmutex_t	snfq_lock;
583 	kcondvar_t	snfq_cv;
584 	int		snfq_svc_threads;	/* # of service threads */
585 	int		snfq_idle_cnt;		/* # of idling threads */
586 	int		snfq_max_threads;
587 	int		snfq_req_cnt;		/* Number of requests */
588 };
589 
590 #define	READ_OP			1
591 #define	SNFQ_TIMEOUT		(60 * 5 * hz)	/* 5 minutes */
592 
593 /* Socket network operations switch */
594 struct sonodeops {
595 	int 	(*sop_init)(struct sonode *, struct sonode *, cred_t *,
596 		    int);
597 	int	(*sop_accept)(struct sonode *, int, cred_t *, struct sonode **);
598 	int	(*sop_bind)(struct sonode *, struct sockaddr *, socklen_t,
599 		    int, cred_t *);
600 	int	(*sop_listen)(struct sonode *, int, cred_t *);
601 	int	(*sop_connect)(struct sonode *, const struct sockaddr *,
602 		    socklen_t, int, int, cred_t *);
603 	int	(*sop_recvmsg)(struct sonode *, struct msghdr *,
604 		    struct uio *, cred_t *);
605 	int	(*sop_sendmsg)(struct sonode *, struct msghdr *,
606 		    struct uio *, cred_t *);
607 	int	(*sop_sendmblk)(struct sonode *, struct msghdr *, int,
608 		    cred_t *, mblk_t **);
609 	int	(*sop_getpeername)(struct sonode *, struct sockaddr *,
610 		    socklen_t *, boolean_t, cred_t *);
611 	int	(*sop_getsockname)(struct sonode *, struct sockaddr *,
612 		    socklen_t *, cred_t *);
613 	int	(*sop_shutdown)(struct sonode *, int, cred_t *);
614 	int	(*sop_getsockopt)(struct sonode *, int, int, void *,
615 		    socklen_t *, int, cred_t *);
616 	int 	(*sop_setsockopt)(struct sonode *, int, int, const void *,
617 		    socklen_t, cred_t *);
618 	int 	(*sop_ioctl)(struct sonode *, int, intptr_t, int,
619 		    cred_t *, int32_t *);
620 	int 	(*sop_poll)(struct sonode *, short, int, short *,
621 		    struct pollhead **);
622 	int 	(*sop_close)(struct sonode *, int, cred_t *);
623 };
624 
625 #define	SOP_INIT(so, flag, cr, flags)	\
626 	((so)->so_ops->sop_init((so), (flag), (cr), (flags)))
627 #define	SOP_ACCEPT(so, fflag, cr, nsop)	\
628 	((so)->so_ops->sop_accept((so), (fflag), (cr), (nsop)))
629 #define	SOP_BIND(so, name, namelen, flags, cr)	\
630 	((so)->so_ops->sop_bind((so), (name), (namelen), (flags), (cr)))
631 #define	SOP_LISTEN(so, backlog, cr)	\
632 	((so)->so_ops->sop_listen((so), (backlog), (cr)))
633 #define	SOP_CONNECT(so, name, namelen, fflag, flags, cr)	\
634 	((so)->so_ops->sop_connect((so), (name), (namelen), (fflag), (flags), \
635 	(cr)))
636 #define	SOP_RECVMSG(so, msg, uiop, cr)	\
637 	((so)->so_ops->sop_recvmsg((so), (msg), (uiop), (cr)))
638 #define	SOP_SENDMSG(so, msg, uiop, cr)	\
639 	((so)->so_ops->sop_sendmsg((so), (msg), (uiop), (cr)))
640 #define	SOP_SENDMBLK(so, msg, size, cr, mpp)	\
641 	((so)->so_ops->sop_sendmblk((so), (msg), (size), (cr), (mpp)))
642 #define	SOP_GETPEERNAME(so, addr, addrlen, accept, cr)	\
643 	((so)->so_ops->sop_getpeername((so), (addr), (addrlen), (accept), (cr)))
644 #define	SOP_GETSOCKNAME(so, addr, addrlen, cr)	\
645 	((so)->so_ops->sop_getsockname((so), (addr), (addrlen), (cr)))
646 #define	SOP_SHUTDOWN(so, how, cr)	\
647 	((so)->so_ops->sop_shutdown((so), (how), (cr)))
648 #define	SOP_GETSOCKOPT(so, level, optionname, optval, optlenp, flags, cr) \
649 	((so)->so_ops->sop_getsockopt((so), (level), (optionname),	\
650 	    (optval), (optlenp), (flags), (cr)))
651 #define	SOP_SETSOCKOPT(so, level, optionname, optval, optlen, cr)	\
652 	((so)->so_ops->sop_setsockopt((so), (level), (optionname),	\
653 	    (optval), (optlen), (cr)))
654 #define	SOP_IOCTL(so, cmd, arg, mode, cr, rvalp)	\
655 	((so)->so_ops->sop_ioctl((so), (cmd), (arg), (mode), (cr), (rvalp)))
656 #define	SOP_POLL(so, events, anyyet, reventsp, phpp) \
657 	((so)->so_ops->sop_poll((so), (events), (anyyet), (reventsp), (phpp)))
658 #define	SOP_CLOSE(so, flag, cr)	\
659 	((so)->so_ops->sop_close((so), (flag), (cr)))
660 
661 #endif /* defined(_KERNEL) || defined(_KMEMUSER) */
662 
663 #ifdef _KERNEL
664 
665 #define	ISALIGNED_cmsghdr(addr) \
666 		(((uintptr_t)(addr) & (_CMSG_HDR_ALIGNMENT - 1)) == 0)
667 
668 #define	ROUNDUP_cmsglen(len) \
669 	(((len) + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1))
670 
671 #define	IS_NON_STREAM_SOCK(vp) \
672 	((vp)->v_type == VSOCK && (vp)->v_stream == NULL)
673 /*
674  * Macros that operate on struct cmsghdr.
675  * Used in parsing msg_control.
676  * The CMSG_VALID macro does not assume that the last option buffer is padded.
677  */
678 #define	CMSG_NEXT(cmsg)						\
679 	(struct cmsghdr *)((uintptr_t)(cmsg) +			\
680 	    ROUNDUP_cmsglen((cmsg)->cmsg_len))
681 #define	CMSG_CONTENT(cmsg)	(&((cmsg)[1]))
682 #define	CMSG_CONTENTLEN(cmsg)	((cmsg)->cmsg_len - sizeof (struct cmsghdr))
683 #define	CMSG_VALID(cmsg, start, end)					\
684 	(ISALIGNED_cmsghdr(cmsg) &&					\
685 	((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&			\
686 	((uintptr_t)(cmsg) < (uintptr_t)(end)) &&			\
687 	((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&	\
688 	((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
689 
690 /*
691  * Maximum size of any argument that is copied in (addresses, options,
692  * access rights). MUST be at least MAXPATHLEN + 3.
693  * BSD and SunOS 4.X limited this to MLEN or MCLBYTES.
694  */
695 #define	SO_MAXARGSIZE	8192
696 
697 /*
698  * Convert between vnode and sonode
699  */
700 #define	VTOSO(vp)	((struct sonode *)((vp)->v_data))
701 #define	SOTOV(sp)	((sp)->so_vnode)
702 
703 /*
704  * Internal flags for sobind()
705  */
706 #define	_SOBIND_REBIND		0x01	/* Bind to existing local address */
707 #define	_SOBIND_UNSPEC		0x02	/* Bind to unspecified address */
708 #define	_SOBIND_LOCK_HELD	0x04	/* so_excl_lock held by caller */
709 #define	_SOBIND_NOXLATE		0x08	/* No addr translation for AF_UNIX */
710 #define	_SOBIND_XPG4_2		0x10	/* xpg4.2 semantics */
711 #define	_SOBIND_SOCKBSD		0x20	/* BSD semantics */
712 #define	_SOBIND_LISTEN		0x40	/* Make into SS_ACCEPTCONN */
713 #define	_SOBIND_SOCKETPAIR	0x80	/* Internal flag for so_socketpair() */
714 					/* to enable listen with backlog = 1 */
715 
716 /*
717  * Internal flags for sounbind()
718  */
719 #define	_SOUNBIND_REBIND	0x01	/* Don't clear fields - will rebind */
720 
721 /*
722  * Internal flags for soconnect()
723  */
724 #define	_SOCONNECT_NOXLATE	0x01	/* No addr translation for AF_UNIX */
725 #define	_SOCONNECT_DID_BIND	0x02	/* Unbind when connect fails */
726 #define	_SOCONNECT_XPG4_2	0x04	/* xpg4.2 semantics */
727 
728 /*
729  * Internal flags for sodisconnect()
730  */
731 #define	_SODISCONNECT_LOCK_HELD	0x01	/* so_excl_lock held by caller */
732 
733 /*
734  * Internal flags for sotpi_getsockopt().
735  */
736 #define	_SOGETSOCKOPT_XPG4_2	0x01	/* xpg4.2 semantics */
737 
738 /*
739  * Internal flags for soallocproto*()
740  */
741 #define	_ALLOC_NOSLEEP		0	/* Don't sleep for memory */
742 #define	_ALLOC_INTR		1	/* Sleep until interrupt */
743 #define	_ALLOC_SLEEP		2	/* Sleep forever */
744 
745 /*
746  * Internal structure for handling AF_UNIX file descriptor passing
747  */
748 struct fdbuf {
749 	int		fd_size;	/* In bytes, for kmem_free */
750 	int		fd_numfd;	/* Number of elements below */
751 	char		*fd_ebuf;	/* Extra buffer to free  */
752 	int		fd_ebuflen;
753 	frtn_t		fd_frtn;
754 	struct file	*fd_fds[1];	/* One or more */
755 };
756 #define	FDBUF_HDRSIZE	(sizeof (struct fdbuf) - sizeof (struct file *))
757 
758 /*
759  * Variable that can be patched to set what version of socket socket()
760  * will create.
761  */
762 extern int so_default_version;
763 
764 #ifdef DEBUG
765 /* Turn on extra testing capabilities */
766 #define	SOCK_TEST
767 #endif /* DEBUG */
768 
769 #ifdef DEBUG
770 char	*pr_state(uint_t, uint_t);
771 char	*pr_addr(int, struct sockaddr *, t_uscalar_t);
772 int	so_verify_oobstate(struct sonode *);
773 #endif /* DEBUG */
774 
775 /*
776  * DEBUG macros
777  */
778 #if defined(DEBUG)
779 #define	SOCK_DEBUG
780 
781 extern int sockdebug;
782 extern int sockprinterr;
783 
784 #define	eprint(args)	printf args
785 #define	eprintso(so, args) \
786 { if (sockprinterr && ((so)->so_options & SO_DEBUG)) printf args; }
787 #define	eprintline(error)					\
788 {								\
789 	if (error != EINTR && (sockprinterr || sockdebug > 0))	\
790 		printf("socket error %d: line %d file %s\n",	\
791 			(error), __LINE__, __FILE__);		\
792 }
793 
794 #define	eprintsoline(so, error)					\
795 { if (sockprinterr && ((so)->so_options & SO_DEBUG))		\
796 	printf("socket(%p) error %d: line %d file %s\n",	\
797 		(void *)(so), (error), __LINE__, __FILE__);	\
798 }
799 #define	dprint(level, args)	{ if (sockdebug > (level)) printf args; }
800 #define	dprintso(so, level, args) \
801 { if (sockdebug > (level) && ((so)->so_options & SO_DEBUG)) printf args; }
802 
803 #else /* define(DEBUG) */
804 
805 #define	eprint(args)		{}
806 #define	eprintso(so, args)	{}
807 #define	eprintline(error)	{}
808 #define	eprintsoline(so, error)	{}
809 #define	dprint(level, args)	{}
810 #define	dprintso(so, level, args) {}
811 
812 #endif /* defined(DEBUG) */
813 
814 extern struct vfsops			sock_vfsops;
815 extern struct vnodeops			*socket_vnodeops;
816 extern const struct fs_operation_def	socket_vnodeops_template[];
817 
818 extern dev_t				sockdev;
819 
820 /*
821  * sockfs functions
822  */
823 extern int	sock_getmsg(vnode_t *, struct strbuf *, struct strbuf *,
824 			uchar_t *, int *, int, rval_t *);
825 extern int	sock_putmsg(vnode_t *, struct strbuf *, struct strbuf *,
826 			uchar_t, int, int);
827 extern int	sogetvp(char *, vnode_t **, int);
828 extern int	sockinit(int, char *);
829 extern int	soconfig(int, int, int,	char *, int, char *);
830 extern int	solookup(int, int, int, struct sockparams **);
831 extern void	so_lock_single(struct sonode *);
832 extern void	so_unlock_single(struct sonode *, int);
833 extern int	so_lock_read(struct sonode *, int);
834 extern int	so_lock_read_intr(struct sonode *, int);
835 extern void	so_unlock_read(struct sonode *);
836 extern void	*sogetoff(mblk_t *, t_uscalar_t, t_uscalar_t, uint_t);
837 extern void	so_getopt_srcaddr(void *, t_uscalar_t,
838 			void **, t_uscalar_t *);
839 extern int	so_getopt_unix_close(void *, t_uscalar_t);
840 extern void	fdbuf_free(struct fdbuf *);
841 extern mblk_t	*fdbuf_allocmsg(int, struct fdbuf *);
842 extern int	fdbuf_create(void *, int, struct fdbuf **);
843 extern void	so_closefds(void *, t_uscalar_t, int, int);
844 extern int	so_getfdopt(void *, t_uscalar_t, int, void **, int *);
845 t_uscalar_t	so_optlen(void *, t_uscalar_t, int);
846 extern void	so_cmsg2opt(void *, t_uscalar_t, int, mblk_t *);
847 extern t_uscalar_t
848 		so_cmsglen(mblk_t *, void *, t_uscalar_t, int);
849 extern int	so_opt2cmsg(mblk_t *, void *, t_uscalar_t, int,
850 			void *, t_uscalar_t);
851 extern void	soisconnecting(struct sonode *);
852 extern void	soisconnected(struct sonode *);
853 extern void	soisdisconnected(struct sonode *, int);
854 extern void	socantsendmore(struct sonode *);
855 extern void	socantrcvmore(struct sonode *);
856 extern void	soseterror(struct sonode *, int);
857 extern int	sogeterr(struct sonode *, boolean_t);
858 extern int	sowaitconnected(struct sonode *, int, int);
859 
860 extern ssize_t	soreadfile(file_t *, uchar_t *, u_offset_t, int *, size_t);
861 extern void	*sock_kstat_init(zoneid_t);
862 extern void	sock_kstat_fini(zoneid_t, void *);
863 extern struct sonode *getsonode(int, int *, file_t **);
864 /*
865  * Function wrappers (mostly around the sonode switch) for
866  * backward compatibility.
867  */
868 extern int	soaccept(struct sonode *, int, struct sonode **);
869 extern int	sobind(struct sonode *, struct sockaddr *, socklen_t,
870 		    int, int);
871 extern int	solisten(struct sonode *, int);
872 extern int	soconnect(struct sonode *, const struct sockaddr *, socklen_t,
873 		    int, int);
874 extern int	sorecvmsg(struct sonode *, struct nmsghdr *, struct uio *);
875 extern int	sosendmsg(struct sonode *, struct nmsghdr *, struct uio *);
876 extern int	soshutdown(struct sonode *, int);
877 extern int	sogetsockopt(struct sonode *, int, int, void *, socklen_t *,
878 		    int);
879 extern int	sosetsockopt(struct sonode *, int, int, const void *,
880 		    t_uscalar_t);
881 
882 extern struct sonode	*socreate(struct sockparams *, int, int, int, int,
883 			    int *);
884 
885 extern int	so_copyin(const void *, void *, size_t, int);
886 extern int	so_copyout(const void *, void *, size_t, int);
887 
888 #endif
889 
890 /*
891  * Internal structure for obtaining sonode information from the socklist.
892  * These types match those corresponding in the sonode structure.
893  * This is not a published interface, and may change at any time.
894  */
895 struct sockinfo {
896 	uint_t		si_size;		/* real length of this struct */
897 	short		si_family;
898 	short		si_type;
899 	ushort_t	si_flag;
900 	uint_t		si_state;
901 	uint_t		si_ux_laddr_sou_magic;
902 	uint_t		si_ux_faddr_sou_magic;
903 	t_scalar_t	si_serv_type;
904 	t_uscalar_t	si_laddr_soa_len;
905 	t_uscalar_t	si_faddr_soa_len;
906 	uint16_t	si_laddr_family;
907 	uint16_t	si_faddr_family;
908 	char		si_laddr_sun_path[MAXPATHLEN + 1]; /* NULL terminated */
909 	char		si_faddr_sun_path[MAXPATHLEN + 1];
910 	boolean_t	si_faddr_noxlate;
911 	zoneid_t	si_szoneid;
912 };
913 
914 #define	SOCKMOD_PATH	"socketmod"	/* dir where sockmods are stored */
915 
916 #ifdef	__cplusplus
917 }
918 #endif
919 
920 #endif	/* _SYS_SOCKETVAR_H */
921