xref: /illumos-gate/usr/src/uts/common/sys/stream.h (revision 9b664393d4fdda96221e6ea9ea95790d3c15be70)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
25  * Copyright 2015 Joyent, Inc.  All rights reserved.
26  * Copyright 2022 Garrett D'Amore
27  */
28 
29 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30 /*	  All Rights Reserved	*/
31 
32 
33 #ifndef _SYS_STREAM_H
34 #define	_SYS_STREAM_H
35 
36 /*
37  * For source compatibility
38  */
39 #include <sys/isa_defs.h>
40 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
41 #include <sys/kmem.h>
42 #include <sys/uio.h>
43 #endif
44 #include <sys/poll.h>
45 #include <sys/strmdep.h>
46 #include <sys/cred.h>
47 #include <sys/t_lock.h>
48 #include <sys/model.h>
49 
50 #ifdef	__cplusplus
51 extern "C" {
52 #endif
53 
54 /*
55  * Data queue.
56  *
57  * NOTE: The *only* public fields are documented in queue(9S).
58  *       Everything else is implementation-private.
59  *
60  * The locking rules for the queue_t structure are extremely subtle and vary
61  * widely depending on the field in question.  As such, each field is
62  * annotated according to the following legend:
63  *
64  *   Q9S: The field is documented in queue(9S) and may be accessed without
65  *        locks by a STREAMS module when inside an entry point (e.g., put(9E)).
66  *        However, no fields can be directly modified unless q_lock is held
67  *        (which is not possible in a DDI compliant STREAMS module), with the
68  *        following exceptions:
69  *
70  *	   - q_ptr: can be modified as per the rules of the STREAMS module.
71  *		    The STREAMS framework ignores q_ptr and thus imposes *no*
72  *		    locking rules on it.
73  *         - q_qinfo: can be modified before qprocson().
74  *
75  *	   - q_minpsz, q_maxpsz, q_hiwat, q_lowat: can be modified as per the
76  *		    rules of the STREAMS module.  The STREAMS framework never
77  *		    modifies these fields, and is tolerant of temporarily
78  *		    stale field values.
79  *
80  *	  In general, the STREAMS framework employs one of the following
81  *	  techniques to ensure STREAMS modules can safely access Q9S fields:
82  *
83  *	   - The field is only modified by the framework when the stream is
84  *	     locked with strlock() (q_next).
85  *
86  *	   - The field is modified by the framework, but the modifies are
87  *	     atomic, and temporarily stale values are harmless (q_count,
88  *	     q_first, q_last).
89  *
90  *	   - The field is modified by the framework, but the field's visible
91  *	     values are either constant or directly under the control
92  *	     of the STREAMS module itself (q_flag).
93  *
94  *   QLK: The field must be accessed or modified under q_lock, except when
95  *        the stream has been locked with strlock().  If multiple q_locks must
96  *        be acquired, q_locks at higher addresses must be taken first.
97  *
98  *   STR: The field can be accessed without a lock, but must be modified under
99  *	  strlock().
100  *
101  *   SQLK: The field must be accessed or modified under SQLOCK().
102  *
103  *   NOLK: The field can be accessed without a lock, but can only be modified
104  *	   when the queue_t is not known to any other threads.
105  *
106  *   SVLK: The field must be accessed or modified under the service_queue lock.
107  *         Note that service_lock must be taken after any needed q_locks,
108  *	   and that no other lock should be taken while service_lock is held.
109  *
110  * In addition, it is always acceptable to modify a field that is not yet
111  * known to any other threads -- and other special case exceptions exist in
112  * the code.  Also, q_lock is used with q_wait to implement a stream head
113  * monitor for reads and writes.
114  */
115 typedef struct queue {
116 	struct qinit	*q_qinfo;	/* Q9S: Q processing procedure  */
117 	struct msgb	*q_first;	/* Q9S: first message in Q	*/
118 	struct msgb	*q_last;	/* Q9S: last message in Q	*/
119 	struct queue	*q_next;	/* Q9S: next Q in stream	*/
120 	struct queue	*q_link;	/* SVLK: next Q for scheduling	*/
121 	void		*q_ptr;		/* Q9S: module-specific data	*/
122 	size_t		q_count;	/* Q9S: number of bytes on Q	*/
123 	uint_t		q_flag;		/* Q9S: Q state			*/
124 	ssize_t		q_minpsz;	/* Q9S: smallest packet OK on Q */
125 	ssize_t		q_maxpsz;	/* Q9S: largest packet OK on Q	*/
126 	size_t		q_hiwat;	/* Q9S: Q high water mark	*/
127 	size_t		q_lowat;	/* Q9S: Q low water mark	*/
128 	struct qband	*q_bandp;	/* QLK: band flow information	*/
129 	kmutex_t	q_lock;		/* NOLK: structure lock		*/
130 	struct stdata	*q_stream;	/* NOLK: stream backpointer	*/
131 	struct syncq	*q_syncq;	/* NOLK: associated syncq	*/
132 	unsigned char	q_nband;	/* QLK: number of bands		*/
133 	kcondvar_t	q_wait;		/* NOLK: read/write sleep CV	*/
134 	struct queue	*q_nfsrv;	/* STR: next Q with svc routine */
135 	ushort_t	q_draining;	/* QLK: Q is draining		*/
136 	short		q_struiot;	/* QLK: sync streams Q UIO mode	*/
137 	clock_t		q_qtstamp;	/* QLK: when Q was enabled	*/
138 	size_t		q_mblkcnt;	/* QLK: mblk count		*/
139 	uint_t		q_syncqmsgs;	/* QLK: syncq message count	*/
140 	size_t		q_rwcnt;	/* QLK: # threads in rwnext()	*/
141 	pri_t		q_spri;		/* QLK: Q scheduling priority	*/
142 
143 	/*
144 	 * Syncq scheduling
145 	 */
146 	struct msgb	*q_sqhead;	/* QLK: first syncq message	*/
147 	struct msgb	*q_sqtail;	/* QLK: last syncq message	*/
148 	struct queue	*q_sqnext;	/* SQLK: next Q on syncq list	*/
149 	struct queue	*q_sqprev;	/* SQLK: prev Q on syncq list	*/
150 	uint_t		q_sqflags;	/* SQLK: syncq flags		*/
151 	clock_t		q_sqtstamp;	/* SQLK: when Q was scheduled for sq */
152 
153 	/*
154 	 * NOLK: Reference to the queue's module's implementation
155 	 * structure. This will be NULL for queues associated with drivers.
156 	 */
157 	struct fmodsw_impl	*q_fp;
158 } queue_t;
159 
160 /*
161  * Queue flags; unused flags not documented in queue(9S) can be recycled.
162  */
163 #define	QENAB		0x00000001	/* Queue is already enabled to run */
164 #define	QWANTR		0x00000002	/* Someone wants to read Q	*/
165 #define	QWANTW		0x00000004	/* Someone wants to write Q	*/
166 #define	QFULL		0x00000008	/* Q is considered full		*/
167 #define	QREADR		0x00000010	/* This is the reader (first) Q	*/
168 #define	QUSE		0x00000020	/* This queue in use (allocation) */
169 #define	QNOENB		0x00000040	/* Don't enable Q via putq	*/
170 #define	QWANTRMQSYNC	0x00000080	/* Want to remove sync stream Q */
171 #define	QBACK		0x00000100	/* queue has been back-enabled	*/
172 /*	UNUSED		0x00000200	   was QHLIST			*/
173 /*	UNUSED		0x00000400	   was QUNSAFE			*/
174 #define	QPAIR		0x00000800	/* per queue-pair syncq		*/
175 #define	QPERQ		0x00001000	/* per queue-instance syncq	*/
176 #define	QPERMOD		0x00002000	/* per module syncq		*/
177 #define	QMTSAFE		0x00004000	/* stream module is MT-safe	*/
178 #define	QMTOUTPERIM	0x00008000	/* Has outer perimeter		*/
179 #define	QMT_TYPEMASK	(QPAIR|QPERQ|QPERMOD|QMTSAFE|QMTOUTPERIM)
180 					/* all MT type flags		*/
181 #define	QINSERVICE	0x00010000	/* service routine executing	*/
182 #define	QWCLOSE		0x00020000	/* will not be enabled		*/
183 #define	QEND		0x00040000	/* last queue in stream		*/
184 #define	QWANTWSYNC	0x00080000	/* Streamhead wants to write Q	*/
185 #define	QSYNCSTR	0x00100000	/* Q supports Synchronous STREAMS */
186 #define	QISDRV		0x00200000	/* the Queue is attached to a driver */
187 /*	UNUSED		0x00400000	   was QHOT			*/
188 /*	UNUSED		0x00800000	   was QNEXTHOT			*/
189 /*	UNUSED		0x01000000	   was _QNEXTLESS		*/
190 #define	_QINSERTING	0x04000000	/* Private, module is being inserted */
191 #define	_QREMOVING	0x08000000	/* Private, module is being removed */
192 #define	_QASSOCIATED	0x10000000	/* queue is associated with a device */
193 #define	_QDIRECT	0x20000000	/* Private; transport module uses */
194 					/* direct interface to/from sockfs */
195 #define	_QSINGLE_INSTANCE	0x40000000	/* Private; module may only */
196 						/* be pushed once */
197 
198 /* queue sqflags (protected by SQLOCK). */
199 #define	Q_SQQUEUED	0x01		/* Queue is in the syncq list */
200 #define	Q_SQDRAINING	0x02		/* Servicing syncq msgs.	*/
201 					/* This is also noted by the	*/
202 					/* q_draining field, but this one is */
203 					/* protected by SQLOCK */
204 
205 /*
206  * Structure that describes the separate information
207  * for each priority band in the queue.
208  */
209 typedef struct qband {
210 	struct qband	*qb_next;	/* next band's info */
211 	size_t		qb_count;	/* number of bytes in band */
212 	struct msgb	*qb_first;	/* beginning of band's data */
213 	struct msgb	*qb_last;	/* end of band's data */
214 	size_t		qb_hiwat;	/* high water mark for band */
215 	size_t		qb_lowat;	/* low water mark for band */
216 	uint_t		qb_flag;	/* see below */
217 	size_t		qb_mblkcnt;	/* mblk counter for runaway msgs */
218 } qband_t;
219 
220 /*
221  * qband flags
222  */
223 #define	QB_FULL		0x01		/* band is considered full */
224 #define	QB_WANTW	0x02		/* Someone wants to write to band */
225 #define	QB_BACK		0x04		/* queue has been back-enabled */
226 
227 /*
228  * Maximum number of bands.
229  */
230 #define	NBAND	256
231 
232 /*
233  * Fields that can be manipulated through strqset() and strqget().
234  */
235 typedef enum qfields {
236 	QHIWAT	= 0,		/* q_hiwat or qb_hiwat */
237 	QLOWAT	= 1,		/* q_lowat or qb_lowat */
238 	QMAXPSZ	= 2,		/* q_maxpsz */
239 	QMINPSZ	= 3,		/* q_minpsz */
240 	QCOUNT	= 4,		/* q_count or qb_count */
241 	QFIRST	= 5,		/* q_first or qb_first */
242 	QLAST	= 6,		/* q_last or qb_last */
243 	QFLAG	= 7,		/* q_flag or qb_flag */
244 	QSTRUIOT = 8,		/* q_struiot */
245 	QBAD	= 9
246 } qfields_t;
247 
248 /*
249  * Module information structure
250  */
251 struct module_info {
252 	ushort_t mi_idnum;		/* module id number */
253 	char	*mi_idname;		/* module name */
254 	ssize_t	mi_minpsz;		/* min packet size accepted */
255 	ssize_t	mi_maxpsz;		/* max packet size accepted */
256 	size_t	mi_hiwat;		/* hi-water mark */
257 	size_t	mi_lowat;		/* lo-water mark */
258 };
259 
260 /*
261  * queue information structure (with Synchronous STREAMS extensions)
262  */
263 
264 typedef struct msgb mblk_t;
265 typedef struct struiod struiod_t;
266 typedef struct infod infod_t;
267 
268 typedef	int (*qi_putp_t)(queue_t *, mblk_t *);
269 typedef	int (*qi_srvp_t)(queue_t *);
270 typedef	int (*qi_qopen_t)(queue_t *, dev_t *, int, int, cred_t *);
271 typedef	int (*qi_qclose_t)(queue_t *, int, cred_t *);
272 typedef	int (*qi_qadmin_t)(void);
273 typedef	int (*qi_rwp_t)(queue_t *, struiod_t *);
274 typedef	int (*qi_infop_t)(queue_t *, infod_t *);
275 
276 struct	qinit {
277 	qi_putp_t	qi_putp;	/* put procedure */
278 	qi_srvp_t	qi_srvp;	/* service procedure */
279 	qi_qopen_t	qi_qopen;	/* called on startup */
280 	qi_qclose_t	qi_qclose;	/* called on finish */
281 	qi_qadmin_t	qi_qadmin;	/* for future use */
282 	struct module_info *qi_minfo;	/* module information structure */
283 	struct module_stat *qi_mstat;	/* module statistics structure */
284 	qi_rwp_t	qi_rwp;		/* r/w procedure */
285 	qi_infop_t	qi_infop;	/* information procedure */
286 	int		qi_struiot;	/* stream uio type for struio() */
287 };
288 
289 /*
290  * Values for qi_struiot and q_struiot:
291  */
292 #define	STRUIOT_NONE		-1	/* doesn't support struio() */
293 #define	STRUIOT_DONTCARE	0	/* use current uiomove() (default) */
294 #define	STRUIOT_STANDARD	1	/* use standard uiomove() */
295 
296 /*
297  * Streamtab (used in cdevsw and fmodsw to point to module or driver)
298  */
299 struct streamtab {
300 	struct qinit *st_rdinit;
301 	struct qinit *st_wrinit;
302 	struct qinit *st_muxrinit;
303 	struct qinit *st_muxwinit;
304 };
305 
306 /*
307  * Structure sent to mux drivers to indicate a link.
308  */
309 struct linkblk {
310 	queue_t *l_qtop;	/* lowest level write queue of upper stream */
311 				/* (set to NULL for persistent links) */
312 	queue_t *l_qbot;	/* highest level write queue of lower stream */
313 	int	l_index;	/* index for lower stream. */
314 };
315 
316 /*
317  * Esballoc data buffer freeing routine
318  */
319 typedef struct free_rtn {
320 	void	(*free_func)();
321 	caddr_t	free_arg;
322 } frtn_t;
323 
324 /*
325  * Data block descriptor
326  *
327  * NOTE: db_base, db_lim, db_ref and db_type are the *only* public fields,
328  * as described in datab(9S).  Everything else is implementation-private.
329  */
330 
331 #define	DBLK_REFMAX	255U
332 
333 typedef struct datab {
334 	frtn_t		*db_frtnp;
335 	unsigned char	*db_base;
336 	unsigned char	*db_lim;
337 	unsigned char	db_ref;
338 	unsigned char	db_type;
339 	unsigned char	db_flags;
340 	unsigned char	db_struioflag;
341 	pid_t		db_cpid;	/* cached pid, needs verification */
342 	void		*db_cache;	/* kmem cache descriptor */
343 	struct msgb	*db_mblk;
344 	void		(*db_free)(struct msgb *, struct datab *);
345 	void		(*db_lastfree)(struct msgb *, struct datab *);
346 	intptr_t	db_cksumstart;
347 	intptr_t	db_cksumend;
348 	intptr_t	db_cksumstuff;
349 	union {
350 		double enforce_alignment;
351 		unsigned char data[8];
352 		struct {
353 			union {
354 				uint32_t u32;
355 				uint16_t u16;
356 			} cksum_val;    /* used to store calculated cksum */
357 			uint16_t flags;
358 			uint16_t pad;
359 		} cksum;
360 		/*
361 		 * Union used for future extensions (pointer to data ?).
362 		 */
363 	} db_struioun;
364 	struct fthdr	*db_fthdr;
365 	cred_t		*db_credp;	/* credential */
366 } dblk_t;
367 
368 #define	db_cksum16	db_struioun.cksum.cksum_val.u16
369 #define	db_cksum32	db_struioun.cksum.cksum_val.u32
370 
371 /*
372  * Accessor macros for private dblk_t fields (the rest are in <sys/strsun.h>).
373  */
374 #define	DB_CPID(mp)		((mp)->b_datap->db_cpid)
375 #define	DB_CRED(mp)		((mp)->b_datap->db_credp)
376 #define	DB_FTHDR(mp)		((mp)->b_datap->db_fthdr)
377 /*
378  * Used by GLDv2 to store the TCI information.
379  */
380 #define	DB_TCI(mp)		((mp)->b_datap->db_struioun.cksum.pad)
381 
382 /*
383  * Message block descriptor
384  */
385 struct	msgb {
386 	struct	msgb	*b_next;
387 	struct  msgb	*b_prev;
388 	struct	msgb	*b_cont;
389 	unsigned char	*b_rptr;
390 	unsigned char	*b_wptr;
391 	struct datab	*b_datap;
392 	unsigned char	b_band;
393 	unsigned char	b_tag;
394 	unsigned short	b_flag;
395 	queue_t		*b_queue;	/* for sync queues */
396 };
397 
398 /*
399  * bcache descriptor
400  */
401 typedef	struct	bcache {
402 	kmutex_t		mutex;
403 	struct kmem_cache	*buffer_cache;
404 	struct kmem_cache	*dblk_cache;
405 	int			alloc;
406 	int			destroy;
407 	size_t			size;
408 	uint_t			align;
409 } bcache_t;
410 
411 /*
412  * db_flags values (all implementation private!)
413  */
414 #define	DBLK_REFMIN		0x01	/* min refcnt stored in low bit */
415 #define	DBLK_COOKED		0x02	/* message has been processed once */
416 #define	DBLK_UIOA		0x04	/* uioamove() is pending */
417 
418 /*
419  * db_struioflag values:
420  */
421 #define	STRUIO_SPEC	0x01	/* struio{get,put}() special mblk */
422 #define	STRUIO_DONE	0x02	/* struio done (could be partial) */
423 #define	STRUIO_IP	0x04	/* IP checksum stored in db_struioun */
424 #define	STRUIO_ZC	0x08	/* mblk eligible for zero-copy */
425 #define	STRUIO_ZCNOTIFY	0x10	/* notify stream head when mblk acked */
426 
427 /*
428  * Message flags.  These are interpreted by the stream head.
429  */
430 #define	MSGMARK		0x01	/* last byte of message is "marked" */
431 #define	MSGNOLOOP	0x02	/* don't loop message around to */
432 				/* write side of stream */
433 #define	MSGDELIM	0x04	/* message is delimited */
434 /*	UNUSED		0x08	   was MSGNOGET (can be recycled) */
435 #define	MSGMARKNEXT	0x10	/* Private: first byte of next msg marked */
436 #define	MSGNOTMARKNEXT	0x20	/* Private: ... not marked */
437 #define	MSGWAITSYNC	0x40	/* Private: waiting for sync squeue enter */
438 
439 /*
440  * Streams message types.
441  */
442 
443 /*
444  * Data and protocol messages (regular and priority)
445  */
446 #define	M_DATA		0x00		/* regular data */
447 #define	M_PROTO		0x01		/* protocol control */
448 #define	M_MULTIDATA	0x02		/* obsolete, do not use */
449 
450 /*
451  * Control messages (regular and priority)
452  */
453 #define	M_BREAK		0x08		/* line break */
454 #define	M_PASSFP	0x09		/* pass file pointer */
455 #define	M_EVENT		0x0a		/* Obsoleted: do not use */
456 #define	M_SIG		0x0b		/* generate process signal */
457 #define	M_DELAY		0x0c		/* real-time xmit delay (1 param) */
458 #define	M_CTL		0x0d		/* device-specific control message */
459 #define	M_IOCTL		0x0e		/* ioctl; set/get params */
460 #define	M_SETOPTS	0x10		/* set various stream head options */
461 #define	M_RSE		0x11		/* reserved for RSE use only */
462 
463 /*
464  * Control messages (high priority; go to head of queue)
465  */
466 #define	M_IOCACK	0x81		/* acknowledge ioctl */
467 #define	M_IOCNAK	0x82		/* negative ioctl acknowledge */
468 #define	M_PCPROTO	0x83		/* priority proto message */
469 #define	M_PCSIG		0x84		/* generate process signal */
470 #define	M_READ		0x85		/* generate read notification */
471 #define	M_FLUSH		0x86		/* flush your queues */
472 #define	M_STOP		0x87		/* stop transmission immediately */
473 #define	M_START		0x88		/* restart transmission after stop */
474 #define	M_HANGUP	0x89		/* line disconnect */
475 #define	M_ERROR		0x8a		/* send error to stream head */
476 #define	M_COPYIN	0x8b		/* request to copyin data */
477 #define	M_COPYOUT	0x8c		/* request to copyout data */
478 #define	M_IOCDATA	0x8d		/* response to M_COPYIN and M_COPYOUT */
479 #define	M_PCRSE		0x8e		/* reserved for RSE use only */
480 #define	M_STOPI		0x8f		/* stop reception immediately */
481 #define	M_STARTI	0x90		/* restart reception after stop */
482 #define	M_PCEVENT	0x91		/* Obsoleted: do not use */
483 #define	M_UNHANGUP	0x92		/* line reconnect, sigh */
484 #define	M_CMD		0x93		/* out-of-band ioctl command */
485 
486 /*
487  * Queue message class definitions.
488  */
489 #define	QNORM		0x00		/* normal priority messages */
490 #define	QPCTL		0x80		/* high priority cntrl messages */
491 
492 /*
493  *  IOCTL structure - this structure is the format of the M_IOCTL message type.
494  */
495 #if	defined(_LP64)
496 struct iocblk {
497 	int	ioc_cmd;		/* ioctl command type */
498 	cred_t	*ioc_cr;		/* full credentials */
499 	uint_t	ioc_id;			/* ioctl id */
500 	uint_t	ioc_flag;		/* see below */
501 	size_t	ioc_count;		/* count of bytes in data field */
502 	int	ioc_rval;		/* return value  */
503 	int	ioc_error;		/* error code */
504 };
505 #else
506 struct iocblk {
507 	int	ioc_cmd;		/* ioctl command type */
508 	cred_t	*ioc_cr;		/* full credentials */
509 	uint_t	ioc_id;			/* ioctl id */
510 	size_t	ioc_count;		/* count of bytes in data field */
511 	int	ioc_error;		/* error code */
512 	int	ioc_rval;		/* return value  */
513 	int	ioc_fill1;
514 	uint_t	ioc_flag;		/* see below */
515 	int	ioc_filler[2];		/* reserved for future use */
516 };
517 #endif	/* _LP64 */
518 
519 typedef	struct iocblk	*IOCP;
520 
521 /* {ioc,cp}_flags values */
522 
523 #define	IOC_MODELS	DATAMODEL_MASK	/* Note: 0x0FF00000 */
524 #define	IOC_ILP32	DATAMODEL_ILP32	/* ioctl origin is ILP32 */
525 #define	IOC_LP64	DATAMODEL_LP64	/* ioctl origin is LP64 */
526 #define	IOC_NATIVE	DATAMODEL_NATIVE
527 #define	IOC_NONE	DATAMODEL_NONE	/* dummy comparison value */
528 
529 /*
530  *	Is the ioctl data formatted for our native model?
531  */
532 #define	IOC_CONVERT_FROM(iocp)	ddi_model_convert_from( \
533 				    ((struct iocblk *)iocp)->ioc_flag)
534 
535 /*
536  * structure for the M_COPYIN and M_COPYOUT message types.
537  */
538 #if	defined(_LP64)
539 struct copyreq {
540 	int	cq_cmd;			/* ioctl command (from ioc_cmd) */
541 	cred_t	*cq_cr;			/* full credentials (from ioc_cmd) */
542 	uint_t	cq_id;			/* ioctl id (from ioc_id) */
543 	uint_t	cq_flag;		/* must be zero */
544 	mblk_t	*cq_private;		/* private state information */
545 	caddr_t	cq_addr;		/* address to copy data to/from */
546 	size_t	cq_size;		/* number of bytes to copy */
547 };
548 #else
549 struct copyreq {
550 	int	cq_cmd;			/* ioctl command (from ioc_cmd) */
551 	cred_t	*cq_cr;			/* full credentials */
552 	uint_t	cq_id;			/* ioctl id (from ioc_id) */
553 	caddr_t	cq_addr;		/* address to copy data to/from */
554 	size_t	cq_size;		/* number of bytes to copy */
555 	uint_t	cq_flag;		/* must be zero */
556 	mblk_t	*cq_private;		/* private state information */
557 	int	cq_filler[4];		/* reserved for future use */
558 };
559 #endif	/* _LP64 */
560 
561 /*
562  * structure for the M_IOCDATA message type.
563  */
564 #if	defined(_LP64)
565 struct copyresp {
566 	int	cp_cmd;			/* ioctl command (from ioc_cmd) */
567 	cred_t	*cp_cr;			/* full credentials (from ioc_cmd) */
568 	uint_t	cp_id;			/* ioctl id (from ioc_id) */
569 	uint_t	cp_flag;		/* datamodel IOC_ flags; see above */
570 	mblk_t *cp_private;		/* private state information */
571 	caddr_t	cp_rval;		/* status of request: 0 -> success */
572 					/*		non-zero -> failure */
573 };
574 #else
575 struct copyresp {
576 	int	cp_cmd;			/* ioctl command (from ioc_cmd) */
577 	cred_t	*cp_cr;			/* full credentials */
578 	uint_t	cp_id;			/* ioctl id (from ioc_id) */
579 	caddr_t	cp_rval;		/* status of request: 0 -> success */
580 					/*		non-zero -> failure */
581 	size_t	cp_pad1;
582 	uint_t	cp_pad2;
583 	mblk_t *cp_private;		/* private state information */
584 	uint_t	cp_flag;		/* datamodel IOC_ flags; see above */
585 	int	cp_filler[3];
586 };
587 #endif	/* _LP64 */
588 
589 /*
590  * Since these structures are all intended to travel in the same message
591  * at different stages of a STREAMS ioctl, this union is used to determine
592  * the message size in strdoioctl().
593  */
594 union ioctypes {
595 	struct iocblk	iocblk;
596 	struct copyreq	copyreq;
597 	struct copyresp	copyresp;
598 };
599 
600 /*
601  * Options structure for M_SETOPTS message.  This is sent upstream
602  * by a module or driver to set stream head options.
603  */
604 struct stroptions {
605 	uint_t	so_flags;		/* options to set */
606 	short	so_readopt;		/* read option */
607 	ushort_t so_wroff;		/* write offset */
608 	ssize_t	so_minpsz;		/* minimum read packet size */
609 	ssize_t	so_maxpsz;		/* maximum read packet size */
610 	size_t	so_hiwat;		/* read queue high water mark */
611 	size_t	so_lowat;		/* read queue low water mark */
612 	unsigned char so_band;		/* band for water marks */
613 	ushort_t so_erropt;		/* error option */
614 	ssize_t	so_maxblk;		/* maximum message block size */
615 	ushort_t so_copyopt;		/* copy options (see stropts.h) */
616 	ushort_t so_tail;		/* space available at the end */
617 };
618 
619 /* flags for stream options set message */
620 
621 #define	SO_ALL		0x003f	/* set all old options */
622 #define	SO_READOPT	0x0001	/* set read option */
623 #define	SO_WROFF	0x0002	/* set write offset */
624 #define	SO_MINPSZ	0x0004	/* set min packet size */
625 #define	SO_MAXPSZ	0x0008	/* set max packet size */
626 #define	SO_HIWAT	0x0010	/* set high water mark */
627 #define	SO_LOWAT	0x0020	/* set low water mark */
628 #define	SO_MREADON	0x0040	/* set read notification ON */
629 #define	SO_MREADOFF	0x0080	/* set read notification OFF */
630 #define	SO_NDELON	0x0100	/* old TTY semantics for NDELAY reads/writes */
631 #define	SO_NDELOFF	0x0200	/* STREAMS semantics for NDELAY reads/writes */
632 #define	SO_ISTTY	0x0400	/* the stream is acting as a terminal */
633 #define	SO_ISNTTY	0x0800	/* the stream is not acting as a terminal */
634 #define	SO_TOSTOP	0x1000	/* stop on background writes to this stream */
635 #define	SO_TONSTOP	0x2000	/* do not stop on background writes to stream */
636 #define	SO_BAND		0x4000	/* water marks affect band */
637 #define	SO_DELIM	0x8000	/* messages are delimited */
638 #define	SO_NODELIM	0x010000	/* turn off delimiters */
639 #define	SO_STRHOLD	0x020000	/* No longer implemented */
640 #define	SO_ERROPT	0x040000	/* set error option */
641 #define	SO_COPYOPT	0x080000	/* copy option(s) present */
642 #define	SO_MAXBLK	0x100000	/* set maximum message block size */
643 #define	SO_TAIL		0x200000	/* set the extra allocated space */
644 
645 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
646 /*
647  * Structure for rw (read/write) procedure calls. A pointer
648  * to a struiod_t is passed as a parameter to the rwnext() call.
649  */
650 struct struiod {
651 	mblk_t		*d_mp;		/* pointer to mblk (chain) */
652 	uio_t		d_uio;		/* uio info */
653 	iovec_t		*d_iov;		/* iov referenced by uio */
654 };
655 
656 /*
657  * Structure for information procedure calls.
658  */
659 struct infod {
660 	unsigned char	d_cmd;		/* info info request command */
661 	unsigned char	d_res;		/* info info command results */
662 	int		d_bytes;	/* mblk(s) byte count */
663 	int		d_count;	/* count of mblk(s) */
664 	uio_t		*d_uiop;	/* pointer to uio struct */
665 };
666 /*
667  * Values for d_cmd & d_res.
668  */
669 #define	INFOD_FIRSTBYTES	0x02	/* return msgbsize() of first mblk */
670 #define	INFOD_BYTES		0x04	/* return msgbsize() of all mblk(s) */
671 #define	INFOD_COUNT		0x08	/* return count of mblk(s) */
672 #define	INFOD_COPYOUT		0x10	/* copyout any M_DATA mblk(s) */
673 
674 /*
675  * Structure used by _I_CMD mechanism, similar in spirit to iocblk.
676  */
677 typedef struct cmdblk {
678 	int		cb_cmd;		/* ioctl command type */
679 	cred_t		*cb_cr;		/* full credentials */
680 	uint_t		cb_len;		/* payload size */
681 	int		cb_error;	/* error code */
682 } cmdblk_t;
683 
684 #endif /* _KERNEL */
685 
686 /*
687  * Miscellaneous parameters and flags.
688  */
689 
690 /*
691  * Values for stream flag in open to indicate module open, clone open,
692  * and the return value for failure.
693  */
694 #define	MODOPEN		0x1		/* open as a module */
695 #define	CLONEOPEN	0x2		/* clone open; pick own minor dev */
696 #define	OPENFAIL	-1		/* returned for open failure */
697 
698 /*
699  * Priority definitions for block allocation.
700  */
701 #define	BPRI_LO		1
702 #define	BPRI_MED	2
703 #define	BPRI_HI		3
704 
705 /*
706  * Value for packet size that denotes infinity
707  */
708 #define	INFPSZ		-1
709 
710 /*
711  * Flags for flushq()
712  */
713 #define	FLUSHALL	1	/* flush all messages */
714 #define	FLUSHDATA	0	/* don't flush control messages */
715 
716 /*
717  * Flag for transparent ioctls
718  */
719 #define	TRANSPARENT	(unsigned int)(-1)
720 
721 /*
722  * Stream head default high/low water marks
723  */
724 #define	STRHIGH 5120
725 #define	STRLOW	1024
726 
727 /*
728  * qwriter perimeter types
729  */
730 #define	PERIM_INNER	1		/* The inner perimeter */
731 #define	PERIM_OUTER	2		/* The outer perimeter */
732 
733 /*
734  * Definitions of Streams macros and function interfaces.
735  */
736 
737 /*
738  * canenable - check if queue can be enabled by putq().
739  */
740 #define	canenable(q)	!((q)->q_flag & QNOENB)
741 
742 /*
743  * Test if data block type is one of the data messages (i.e. not a control
744  * message).
745  */
746 #define	datamsg(type) \
747 		((type) == M_DATA || \
748 		    (type) == M_PROTO || \
749 		    (type) == M_PCPROTO || \
750 		    (type) == M_DELAY)
751 
752 /*
753  * Extract queue class of message block.
754  */
755 #define	queclass(bp) (((bp)->b_datap->db_type >= QPCTL) ? QPCTL : QNORM)
756 
757 /*
758  * Align address on next lower word boundary.
759  */
760 #define	straln(a)	(caddr_t)((intptr_t)(a) & -(sizeof (int)-1))
761 
762 /*
763  * Find the max size of data block.
764  */
765 #define	bpsize(bp) ((unsigned int)(bp->b_datap->db_lim - bp->b_datap->db_base))
766 
767 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
768 
769 /*
770  * For two-byte M_ERROR messages: indication that a side does not have an error
771  */
772 #define	NOERROR	((unsigned char)-1)
773 
774 /*
775  * declarations of common routines
776  */
777 
778 extern mblk_t *allocb(size_t, uint_t);
779 extern mblk_t *desballoc(unsigned char *, size_t, uint_t, frtn_t *);
780 extern mblk_t *esballoc(unsigned char *, size_t, uint_t, frtn_t *);
781 extern bcache_t *bcache_create(char *, size_t, uint_t);
782 extern void bcache_destroy(bcache_t *);
783 extern mblk_t *bcache_allocb(bcache_t *, uint_t);
784 extern mblk_t *mkiocb(uint_t);
785 extern int testb(size_t, uint_t);
786 extern bufcall_id_t bufcall(size_t, uint_t, void (*)(void *), void *);
787 extern bufcall_id_t esbbcall(uint_t, void (*)(void *), void *);
788 extern void freeb(struct msgb *);
789 extern void freemsg(mblk_t *);
790 extern mblk_t *dupb(mblk_t *);
791 extern mblk_t *dupmsg(mblk_t *);
792 extern mblk_t *dupmsg_noloan(mblk_t *);
793 extern mblk_t *copyb(mblk_t *);
794 extern mblk_t *copymsg(mblk_t *);
795 extern void linkb(mblk_t *, mblk_t *);
796 extern mblk_t *unlinkb(mblk_t *);
797 extern mblk_t *reallocb(mblk_t *, size_t, uint_t);	/* private */
798 extern mblk_t *rmvb(mblk_t *, mblk_t *);
799 extern int pullupmsg(struct msgb *, ssize_t);
800 extern mblk_t *msgpullup(struct msgb *, ssize_t);
801 extern int adjmsg(struct msgb *, ssize_t);
802 extern size_t msgdsize(struct msgb *);
803 extern mblk_t *getq(queue_t *);
804 extern void rmvq(queue_t *, mblk_t *);
805 extern void flushq(queue_t *, int);
806 extern void flushq_common(queue_t *, int, int);
807 extern void flushband(queue_t *, unsigned char, int);
808 extern int canput(queue_t *);
809 extern int bcanput(queue_t *, unsigned char);
810 extern int canputnext(queue_t *);
811 extern int bcanputnext(queue_t *, unsigned char);
812 extern int putq(queue_t *, mblk_t *);
813 extern int putbq(queue_t *, mblk_t *);
814 extern int insq(queue_t *, mblk_t *, mblk_t *);
815 extern void put(queue_t *, mblk_t *);
816 extern void putnext(queue_t *, mblk_t *);
817 extern int putctl(queue_t *, int);
818 extern int putctl1(queue_t *, int, int);
819 extern int putnextctl(queue_t *, int);
820 extern int putnextctl1(queue_t *, int, int);
821 extern queue_t *backq(queue_t *);
822 extern void qreply(queue_t *, mblk_t *);
823 extern void qenable(queue_t *);
824 extern int qsize(queue_t *);
825 extern void noenable(queue_t *);
826 extern void enableok(queue_t *);
827 extern int strqset(queue_t *, qfields_t, unsigned char, intptr_t);
828 extern int strqget(queue_t *, qfields_t, unsigned char, void *);
829 extern void unbufcall(bufcall_id_t);
830 extern void qprocson(queue_t *);
831 extern void qprocsoff(queue_t *);
832 extern void freezestr(queue_t *);
833 extern void unfreezestr(queue_t *);
834 extern void qwait(queue_t *);
835 extern int qwait_sig(queue_t *);
836 extern boolean_t qwait_rw(queue_t *);
837 extern void qwriter(queue_t *, mblk_t *, void (*func)(), int);
838 extern timeout_id_t qtimeout(queue_t *, void (*func)(void *), void *, clock_t);
839 extern bufcall_id_t qbufcall(queue_t *, size_t, uint_t,
840     void (*)(void *), void *);
841 extern clock_t quntimeout(queue_t *, timeout_id_t);
842 extern void qunbufcall(queue_t *, bufcall_id_t);
843 extern void strwakeq(queue_t *, int);
844 extern int struioget(queue_t *, mblk_t *, struiod_t *, int);
845 extern int rwnext(queue_t *, struiod_t *);
846 extern int infonext(queue_t *, infod_t *);
847 extern int isuioq(queue_t *);
848 extern void create_putlocks(queue_t *, int);
849 extern int mp_cont_len(mblk_t *, int *);
850 
851 /*
852  * shared or externally configured data structures
853  */
854 extern int nstrpush;			/* maximum number of pushes allowed */
855 
856 #endif /* _KERNEL */
857 
858 #ifdef	__cplusplus
859 }
860 #endif
861 
862 #endif	/* _SYS_STREAM_H */
863