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