xref: /titanic_52/usr/src/uts/common/inet/nca/nca.h (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_INET_NCA_H
27 #define	_INET_NCA_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 #include <sys/thread.h>
36 #include <sys/door.h>
37 #include <sys/disp.h>
38 #include <sys/systm.h>
39 #include <sys/processor.h>
40 #include <sys/socket.h>
41 #include <inet/common.h>
42 #include <inet/ip.h>
43 #include <inet/tcp.h>
44 #include <inet/nca/ncadoorhdr.h>
45 
46 /*
47  * The NCA debugging facilities provided via ADB and MDB depend on a
48  * number of NCA implementation details.  In particular, note that:
49  *
50  *	* ADB macros *must* be revised whenever members are added or
51  *	  removed from the following structures:
52  *
53  *		nca_conn_t connf_t nca_cpu_t dcb_t hcb_t nca_if_t nca_io2_t
54  *		node_t nodef_t sqfan_t nca_squeue_t tb_t te_t ti_t tw_t
55  *
56  *	* ADB macros should be added when new core data structures are
57  *	  added to NCA.  Generally, if you had to put it in here, you
58  *	  need to write a macro for it.
59  *
60  *	* MDB has many dependencies on the way core data structures
61  *	  are connected.  In general, if you break these dependencies,
62  *	  the MDB NCA module will fail to build.  However, breakage
63  *	  may go undetected (for instance, changing a linked list
64  *	  into a circularly linked list).  If you have any doubts,
65  *	  inspect the NCA module source before committing your changes.
66  *
67  *	* MDB depends on the following variables (and their current
68  *	  semantics) in order to function correctly:
69  *
70  *		nca_conn_fanout nca_conn_fanout_size nca_gv nca_lru
71  *		urihash filehash
72  *
73  *	  If you change the names or *semantics* of these variables,
74  *	  you must modify the MDB module accordingly.
75  *
76  *	  In addition, you should consider whether the changes you've
77  *	  made should be reflected in the MDB dcmds themselves.
78  */
79 
80 /* The queue to make upcall on for NCAfs */
81 extern queue_t *ncaupcallq;
82 extern kmutex_t ncaupcallq_lock;
83 
84 extern int nca_logging_on;
85 extern int nca_conn_fanout_size;
86 extern boolean_t nca_deferred_oq_if;
87 extern boolean_t nca_fanout_iq_if;
88 
89 /* Checksum pointer for no checksum */
90 
91 #define	NO_CKSUM (void *)-1
92 
93 /* undef any tcp.h:tcp_t members overloaded by the Solaris 8 tcp.h */
94 
95 #undef	tcp_last_rcv_lbolt
96 #undef	tcp_state
97 #undef	tcp_rto
98 #undef	tcp_snd_ts_ok
99 #undef	tcp_snd_ws_ok
100 #undef	tcp_snxt
101 #undef	tcp_swnd
102 #undef	tcp_mss
103 #undef	tcp_iss
104 #undef	tcp_rnxt
105 #undef	tcp_rwnd
106 #undef	tcp_lport
107 #undef	tcp_fport
108 #undef	tcp_ports
109 
110 /* the iph_t is no longer defined in ip.h for Solaris 8 ? */
111 
112 /* Unaligned IP header */
113 typedef struct iph_s {
114 	uchar_t	iph_version_and_hdr_length;
115 	uchar_t	iph_type_of_service;
116 	uchar_t	iph_length[2];
117 	uchar_t	iph_ident[2];
118 	uchar_t	iph_fragment_offset_and_flags[2];
119 	uchar_t	iph_ttl;
120 	uchar_t	iph_protocol;
121 	uchar_t	iph_hdr_checksum[2];
122 	uchar_t	iph_src[4];
123 	uchar_t	iph_dst[4];
124 } iph_t;
125 
126 
127 #define	true	B_TRUE			/* used with type boolean_t */
128 #define	false	B_FALSE			/* used with type boolean_t */
129 
130 /*
131  * Power of 2^N Primes useful for hashing for N of 0-28,
132  * these primes are the nearest prime <= 2^N - 2^(N-2).
133  */
134 
135 #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
136 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
137 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
138 		50331599, 100663291, 201326557, 0}
139 
140 /*
141  * Serialization queue type (move to strsubr.h (stream.h?) as a general
142  * purpose lightweight mechanism for mblk_t serialization ?).
143  */
144 typedef struct nca_squeue_s {
145 	uint16_t	sq_state;	/* state flags */
146 	uint16_t	sq_count;	/* message count */
147 	uint32_t	sq_type;	/* type flags */
148 	processorid_t	sq_bind;	/* processor to bind to */
149 	ddi_softintr_t	sq_softid;	/* softintr() id */
150 	void		(*sq_init)();	/* initialize function */
151 	void		*sq_init_arg;	/* initialize argument */
152 	void		(*sq_proc)();	/* process function */
153 	mblk_t		*sq_first;	/* first mblk chain or NULL */
154 	mblk_t		*sq_last;	/* last mblk chain or NULL */
155 	clock_t		sq_wait;	/* lbolts to wait after a fill() */
156 	clock_t		sq_iwait;	/* lbolt after nointr() */
157 	clock_t		sq_pwait;	/* lbolt after pause() */
158 	int		sq_isintr;	/* is being or was serviced by */
159 	timeout_id_t	sq_tid;		/* timer id of pending timeout() */
160 	kcondvar_t	sq_async;	/* async thread blocks on */
161 	kmutex_t	sq_lock;	/* lock before using any member */
162 	clock_t		sq_awaken;	/* time async thread was awakened */
163 	void		*sq_priv;	/* user defined private */
164 	kt_did_t	sq_ktid;	/* kernel thread id */
165 } nca_squeue_t;
166 
167 /*
168  * State flags and message count (i.e. properties that change)
169  * Note: The MDB NCA module depends on the values of these flags.
170  */
171 
172 #define	SQS_CNT_TOOMANY	0x8000	/* message count toomany */
173 
174 /* nca_squeue_t state flags now only 16 bits */
175 
176 #define	SQS_PROC	0x0001	/* being processed */
177 #define	SQS_WORKER	0x0002	/* worker thread */
178 #define	SQS_ENTER	0x0004	/* enter thread */
179 #define	SQS_FAST	0x0008	/* enter-fast thread */
180 #define	SQS_PROXY	0x0010	/* proxy thread */
181 #define	SQS_SOFTINTR	0x0020	/* softint thread */
182 				/* 0x00C0 bits not used */
183 
184 #define	SQS_NOINTR	0x0100	/* no interrupt processing */
185 #define	SQS_PAUSE	0x0200	/* paused */
186 #define	SQS_INTRWAIT	0x0400	/* interrupt waiting */
187 #define	SQS_NOPROC	0x0800	/* no processing */
188 				/* 0x7000 bits not used */
189 #define	SQS_EXIT	0x8000	/* worker(s) exit */
190 
191 /*
192  * Type flags (i.e. properties that don't change).
193  * Note: The MDB NCA module depends on the values of these flags.
194  */
195 
196 #define	SQT_BIND_MASK	0xFF000000	/* bind flags mask */
197 
198 #define	SQT_KMEM	0x00000001	/* was kmem_alloc()ed */
199 #define	SQT_DEFERRED	0x00000002	/* deferred processing */
200 #define	SQT_SOFTINTR	0x00000004	/* use softintr() */
201 
202 #define	SQT_BIND_ANY	0x01000000	/* bind worker thread to any CPU */
203 #define	SQT_BIND_TO	0x02000000	/* bind worker thread to speced CPU */
204 
205 #define	SQ_STATE_IS(sqp, flags) ((sqp)->sq_state & (flags))
206 #define	SQ_TYPE_IS(sqp, flags) ((sqp)->sq_type & (flags))
207 
208 
209 typedef struct sqfan_s {
210 	uint32_t	flg;		/* flags only */
211 	uint32_t	cnt;		/* vector count */
212 	uint32_t	ix;		/* next sqv[] to process */
213 	uint32_t	drain;		/* max mblk(s) draind per */
214 	nca_squeue_t	**sqv;	/* pointer to nca_squeue_t pointer vector */
215 } sqfan_t;
216 
217 #define	SQF_DIST_CNT	0x0001	/* sqfan_t dist by queue count */
218 #define	SQF_DIST_IPv4	0x0002	/* sqfan_t dist by IPv4 src addr */
219 
220 /*
221  * A multiphase timer is implemented using the te_t, tb_t, and ti_t structs.
222  *
223  * The multiple phases of timer entry execution are:
224  *
225  * 1) resource, execution is done from resource reclaim when the timer event
226  *    is the freeing of the timed resource.
227  *
228  * 2) process, execution is done from process thread yield (idle/return).
229  *
230  * 3) time, execution is done from a timeout callback thread.
231  *
232  * Each of the phases have a seperate timer fire time represented by the
233  * the ti_t members lbolt1, lbolt2, and lbolt3. Each lbolt is an absolute
234  * lbolt value with lbolt1 <= lbolt2 <= lbolt3.
235  */
236 
237 /*
238  * te_t - timer entry.
239  */
240 
241 typedef struct te_s {
242 	struct te_s *prev;	/* prev te_t */
243 	struct te_s *next;	/* next te_t */
244 	struct tb_s *tbp;	/* pointer to timer bucket */
245 	void	*ep;		/* pointer to encapsulating struct */
246 } te_t;
247 
248 /*
249  * tb_t - timer bucket.
250  */
251 
252 typedef struct tb_s {
253 	struct tb_s *next;	/* next tb_t in ascending time order */
254 	clock_t	exec;		/* te_t lbolt exec value for bucket */
255 	te_t	*head;		/* head of te_t list (first timer) */
256 	te_t	*tail;		/* tail of te_t list (last timer) */
257 } tb_t;
258 
259 /*
260  * ti_t - timer state.
261  */
262 
263 typedef struct ti_s {
264 	clock_t	exec;		/* next te_t exec value (0 = NONE) */
265 	clock_t	lbolt1;		/* phase1 lbolt1 (0 = NONE) */
266 	clock_t	lbolt2;		/* phase2 lbolt2 (0 = NONE) */
267 	clock_t	lbolt3;		/* phase3 lbolt3 (0 = NONE) */
268 	tb_t	*head;		/* head of tb_t list (first timer bucket) */
269 	tb_t	*tail;		/* tail of tb_t list (last timer bucket) */
270 	timeout_id_t tid;	/* timer id of pending timeout() (0 = NONE) */
271 	void	*ep;		/* pointer to encapsulating struct */
272 } ti_t;
273 
274 #define	NCA_TI_INPROC	-1	/* Processing going on */
275 #define	NCA_TI_NONE	0	/* no lbolt */
276 
277 /*
278  * TIME_WAIT grounded doubly linked list of nca_conn_t's awaiting TIME_WAIT
279  * expiration for. This list is used for reclaim, reap, and timer based
280  * processing.
281  *
282  * A multiphase timer is used:
283  *
284  * phase 1) reclaim of connections during connection allocation
285  *
286  * phase 2) reaping of connections during nca_squeue_t inq thread unwind
287  *
288  * phase 3) timeout of connections as a result of a timeout().
289  *
290  * Each of the phases have a seperate timer fire lbolt represented by the
291  * the members lbolt1, lbolt2, and lbolt3, each is an absolute lbolt value
292  * with lbolt1 <= lbolt2 <= lbolt3.
293  */
294 
295 typedef struct tw_s {
296 	clock_t	lbolt1;		/* phase1 lbolt value (0 = NONE) */
297 	clock_t	lbolt2;		/* phase2 lbolt value  */
298 	clock_t	lbolt3;		/* phase3 lbolt value  */
299 	struct conn_s *head;	/* Head of nca_conn_t list */
300 	struct conn_s *tail;	/* Tail of nca_conn_t list */
301 	timeout_id_t tid;	/* Timer id of pending timeout() (0 = NONE) */
302 	void	*ep;		/* pointer to encapsulating struct */
303 } tw_t;
304 
305 #define	NCA_TW_NONE	0	/* no lbolt */
306 
307 #define	NCA_TW_MS	1000
308 
309 #define	NCA_TW_LBOLT MSEC_TO_TICK(NCA_TW_MS)
310 
311 #define	NCA_TW_LBOLTS(twp, future) {					\
312 	clock_t	_lbolt = (future);					\
313 	clock_t	_mod = _lbolt % NCA_TW_LBOLT;				\
314 									\
315 	if (_mod) {							\
316 		/* Roundup to next TIME_WAIT bucket */			\
317 		_lbolt += NCA_TW_LBOLT - _mod;				\
318 	}								\
319 	if ((twp)->lbolt1 != _lbolt) {					\
320 		(twp)->lbolt1 = _lbolt;					\
321 		_lbolt += NCA_TW_LBOLT;					\
322 		(twp)->lbolt2 = _lbolt;					\
323 		_lbolt += NCA_TW_LBOLT;					\
324 		(twp)->lbolt3 = _lbolt;					\
325 		if ((twp)->tid != 0) {					\
326 			(void) untimeout((twp)->tid);			\
327 			(twp)->tid = 0;					\
328 		}							\
329 		if ((_lbolt) != NCA_TW_NONE) {				\
330 			(twp)->tid = timeout((pfv_t)nca_tw_fire, (twp),	\
331 			    (twp)->lbolt3 - lbolt);			\
332 		}							\
333 	}								\
334 }
335 
336 /*
337  * The Node Fanout structure.
338  *
339  * The hash tables and their linkage (hashnext) are protected by the
340  * per-bucket lock. Each node_t inserted in the list points back at
341  * the nodef_t that heads the bucket (hashfanout).
342  */
343 
344 typedef struct nodef_s {
345 	struct node_s	*head;
346 	kmutex_t	lock;
347 } nodef_t;
348 
349 /*
350  * A node_t is used to represent a cached byte-stream object. A node_t is
351  * in one of four active states:
352  *
353  * 1) path != NULL, member of a node_t hash list with an object description
354  *    (hashnext, size, path, pathsz members valid).
355  *
356  * 2) pp != NULL, 1) + phys pages allocated (pp, plrupn, plrunn members valid).
357  *
358  * 3) data != NULL, 2) + virt mapping allocated (data, datasz, vlrupn, vlrunn
359  *    members valid).
360  *
361  * 4) cksum != NULL 3) + checksum mapping allocated
362  */
363 
364 typedef struct node_s {
365 	uint32_t 	ref;		/* ref (see below) state */
366 	uint32_t 	cnt;		/* ref count */
367 	int32_t		size;		/* object size (-1 = UNKNOWN) */
368 	uint32_t	mss;		/* mblk(s) in size mss */
369 	uint64_t	ctag;		/* usr defined cache tag, 0 => no tag */
370 	ipaddr_t	laddr;		/* local IP, for virtual hosting */
371 	uint16_t	lport;		/* local port, for virtual hosting */
372 
373 	struct node_s	*plrunn;	/* Phys LRU list next node_t */
374 	struct node_s	*plrupn;	/* Phys LRU list previous node_t */
375 	struct node_s	*vlrunn;	/* Virt LRU list next node_t */
376 	struct node_s	*vlrupn;	/* Virt LRU list previous node_t */
377 
378 	nodef_t	*hashfanout;		/* hash bucket we're part of */
379 	nodef_t	*ctaghashfanout;	/* ctaghash bucket we're part off */
380 	struct node_s *hashnext;	/* hash list next node_t */
381 	struct node_s *ctaghashnext;	/* ctaghash list next node_t */
382 	struct conn_s *connhead;	/* head of list of conn(s) in miss */
383 	struct conn_s *conntail;	/* tail of list of conn(s) in miss */
384 	struct node_s *next;		/* needed if data is in chunks */
385 	struct node_s *back;		/* needed if data is in chunks */
386 
387 	clock_t	expire;		/* lbolt node_t expires (0 = NOW, -1 = NEVER) */
388 	time_t	lastmod;	/* HTTP "Last-Modified:" value */
389 
390 	mblk_t	*req;		/* whole HTTP request (including headers) */
391 	int	reqsz;		/* size of above */
392 	int	reqcontl;	/* HTTP "Content-Length:" value */
393 	uint32_t rcv_cnt;	/* rcv_list byte count */
394 	mblk_t	*rcv_head;	/* rcv_list head */
395 	mblk_t	*rcv_tail;	/* rcv_list tail */
396 	mblk_t	*rcv_ptr;	/* rcv_list pointer */
397 
398 	nca_squeue_t *sqp;	/* squeue node_t is being processed from */
399 	char	*path;		/* URI path component */
400 	int	pathsz;		/* size of above */
401 	uint_t	method;		/* HTTP request method */
402 	uint_t	version;	/* HTTP request version */
403 	char	*reqhdr;	/* HTTP request header(s) */
404 	int	reqhdrsz;	/* size of above */
405 	char	*reqhost;	/* HTTP "Host:" string */
406 	int	reqhostsz;	/* size of above */
407 	char	*reqaccept;	/* HTTP "Accept:" string */
408 	int	reqacceptsz;	/* size of above */
409 	char	*reqacceptl;	/* HTTP "Accept-Language:" string */
410 	int	reqacceptlsz;	/* size of above */
411 
412 	page_t	**pp;		/* page pointer vector for data */
413 	char	*data;		/* data buffer */
414 	int	datasz;		/* size of above */
415 	uint16_t *cksum;	/* cksum() vector for data by mss */
416 	size_t	cksumlen;	/* length of memory block for above vector */
417 	uint_t	resbody;	/* HTTP response body at &data[resbody] */
418 
419 	int	hlen;		/* data buffer split header len */
420 	int	fileoff;	/* file include offset */
421 	int	filelen;	/* length of file */
422 	struct node_s *fileback; /* head node_t of a file list (-1 for death) */
423 	struct node_s *filenext; /* next node_t of a file list */
424 	struct node_s *ctagback; /* head node_t of a ctag list */
425 	struct node_s *ctagnext; /* next node_t of a ctag list */
426 	vnode_t	*filevp;	/* vnode for the file */
427 
428 	kmutex_t lock;		/* serializes access to node_t */
429 	frtn_t	frtn;		/* STREAMS free routine; always node_freeb() */
430 	boolean_t headchunk;	/* true if this node is the head chunk */
431 
432 	/*
433 	 * The following 4 fields are used to record node states when
434 	 * upcalls are preempted. When preempted upcalls are not relevant,
435 	 * these fields should have default value 0.
436 	 */
437 	uint8_t advise;		/* an interpreted advise from http */
438 	boolean_t last_advisory; /* preempted upcall state -- advisory bit */
439 	boolean_t advisory;	/* need advisory from httpd before use */
440 	boolean_t first_upcall;	/* node in first upcall, a internal state */
441 
442 	kcondvar_t cv;		/* sync upcall/downcall process on a node */
443 	int	onqueue;	/* == 1 if on miss_queue, debug aid */
444 } node_t;
445 
446 /* Note: The MDB NCA module depends on the values of these flags. */
447 
448 #define	REF_URI		0x80000000 /* & ref = node_t URI hashed */
449 #define	REF_PHYS	0x40000000 /* & ref = phys mapping in-use */
450 #define	REF_VIRT	0x20000000 /* & ref = virt mapping in-use */
451 #define	REF_CKSUM	0x10000000 /* & ref = checksum mapping in-use */
452 #define	REF_KMEM	0x08000000 /* & ref = kmem mapped (PHYS|VIRT) */
453 #define	REF_DONE	0x04000000 /* & ref = node_t fill is done */
454 #define	REF_SAFED	0x02000000 /* & ref = node_t not safe for use */
455 #define	REF_FILE	0x01000000 /* & ref = node_t filename hashed */
456 #define	REF_RESP	0x00800000 /* & ref = node_t response header parsed */
457 #define	REF_NOLRU	0x00400000 /* & ref = node_t not safe for lru reclaim */
458 #define	REF_MISS	0x00200000 /* & ref = node_t is/will missed() proc */
459 #define	REF_ONPLRU	0x00100000 /* & ref = node_t is on Phys LRU */
460 #define	REF_ONVLRU	0x00080000 /* & ref = node_t is on Virt LRU */
461 #define	REF_PREEMPT	0x00040000 /* & ref = node_t processing preempted */
462 #define	REF_CTAG	0x00020000 /* & ref = node_t CTAG hashed */
463 #define	REF_UPCALL	0x00010000 /* & ref = node_t upcall not yet complete */
464 #define	REF_OWNED	0x00008000 /* & ref = node_t owned (won't be freed) */
465 #define	REF_ERROR	0x00004000 /* & ref = node_t errored */
466 #define	REF_VNODE	0x00002000 /* & ref = node_t vnode hashed */
467 #define	REF_NCAFS	0x00001000 /* & ref = node_t is NCAfs required */
468 #define	REF_SEGMAP	0x00000800 /* & ref = segmapped (PHYS|VIRT) */
469 #define	REF_UNUSED	0x000007FF /* & ref = UNUSED */
470 /*
471  * Mappings where no seperate PHYS and VIRT, i.e. single mapping with a
472  * virtual address e.g. REF_KMEM and REF_SEGMAP.
473  */
474 #define	REF_NOVIRT	(REF_KMEM | REF_SEGMAP)
475 
476 /* Is this node safe for reclaim ? */
477 #define	REF_RECLAIM	(REF_SAFED | REF_NOLRU | REF_MISS)
478 
479 /*
480  * NCA node_t reference counting is more complicated than nca_conn_t reference
481  * counting because we pass parts of node_t's (masquerading as dblk
482  * buffers) into the STREAMS subsystem which eventually get freed by
483  * network drivers just like regular dblk buffers.  Also, unlike nca_conn_t's,
484  * we may wish to keep a node_t around even after there are no outstanding
485  * references, since it's possible that it will be requested again.
486  *
487  * Thus, the node_t reference count reflects the number of active codepaths
488  * in Solaris making use of a given node_t -- each codepath that requires
489  * that the node_t stick around once it drops the node_t lock must acquire
490  * a reference via NODE_REFHOLD and drop that reference via NODE_REFRELE
491  * when done.  Note that following a NODE_REFRELE the node that was
492  * released may no longer exist and thus it should not be referenced unless
493  * the codepath has another outstanding reference.  When a node_t is passed
494  * into the STREAMS subsystem via desballoc() and related interfaces, a
495  * NODE_REFHOLD should be placed on the node_t and the free routine should
496  * be set to node_freeb(), which will in turn call NODE_REFRELE.
497  *
498  * The concept of node ownership allows NCA to express that it would like
499  * this node to hang around, even if there are no "explicit" references to
500  * it (the ownership counts as an implicit reference).  All "headchunk"
501  * hashed nodes are owned when they are created.  If they subsequently
502  * become disowned (currently via nca_node_del() or nca_reclaim_vlru()),
503  * they may have some or all their resources freed (via node_fr()) as soon
504  * as the last reference to them is removed.  Note that it's possible that
505  * a disowned node may become of interest again before some or all of its
506  * resources were reclaimed -- in this case, it must be reowned via
507  * NODE_OWN.  Note that an unhashed node should never be owned, though it
508  * of course may be held and released; this is because there is no sense
509  * in owning a node which is merely temporary (i.e., not hashed somewhere).
510  * Note that the corollary of this statement is not true -- that is, just
511  * because a node is hashed does not mean it is owned (it may have been
512  * disowned via nca_reclaim_vlru()) -- this is why code must always reown
513  * hashed nodes if it's desirable to have them stick around.
514  *
515  * All four macros *must* be called with the node lock held.  However,
516  * NODE_DISOWN and NODE_REFRELE return with the lock unlocked (if there is
517  * still a lock at all), because the operation may have just removed the
518  * final reference to a node and it may no longer exist.
519  *
520  * A version of NODE_REFRELE is provided which doesn't unlock the lock but
521  * can only be used when the caller can gaurantee that it's not the last ref
522  * (e.g. the caller has another outstanding reference) as if it's the last
523  * ref the node_t may no longer exist. The new macro is NODE_REFRELE_LOCKED.
524  */
525 
526 #define	NODE_DISOWN(np) {						\
527 									\
528 	NODE_T_TRACE((np), NODE_T_TRACE_DISOWN);			\
529 	ASSERT(mutex_owned(&(np)->lock));				\
530 									\
531 	if ((np)->ref & REF_OWNED) {					\
532 		if ((np)->cnt == 0)	{				\
533 			panic("nca NODE_DISOWN: %p has no references",	\
534 			    (void *)(np));				\
535 		}							\
536 		(np)->ref &= ~REF_OWNED;				\
537 		NODE_REFRELE(np);					\
538 	} else {							\
539 		mutex_exit(&(np)->lock);				\
540 	}								\
541 }
542 
543 #define	NODE_OWN(np) {							\
544 									\
545 	NODE_T_TRACE((np), NODE_T_TRACE_OWN);				\
546 	ASSERT(mutex_owned(&(np)->lock));				\
547 									\
548 	if (!((np)->ref & REF_OWNED)) {					\
549 		if ((np)->cnt == UINT_MAX)				\
550 			panic(						\
551 			    "nca NODE_OWN: %p has too many references",	\
552 			    (void *)(np));				\
553 		(np)->ref |= REF_OWNED;					\
554 		(np)->cnt++;						\
555 	}								\
556 }
557 
558 #define	NODE_REFHOLD(np) {						\
559 									\
560 	NODE_T_TRACE((np), NODE_T_TRACE_REFHOLD | ((np)->cnt + 1));	\
561 	ASSERT(mutex_owned(&(np)->lock));				\
562 									\
563 	if ((np)->cnt == UINT_MAX)					\
564 		panic("nca NODE_REFHOLD: %p has too many references",	\
565 		    (void *)(np));					\
566 	(np)->cnt++;							\
567 }
568 
569 #define	NODE_REFRELE(np) {						\
570 									\
571 	NODE_T_TRACE((np), NODE_T_TRACE_REFRELE | ((np)->cnt - 1));	\
572 	ASSERT(mutex_owned(&(np)->lock));				\
573 									\
574 	if (((np)->ref & REF_OWNED) && (np)->cnt == 1)			\
575 		panic(							\
576 		    "nca NODE_REFRELE: %p has only OWNED reference",	\
577 		    (void *)(np));					\
578 	if ((np)->cnt == 0)						\
579 		panic("nca NODE_REFRELE: %p has no references",		\
580 		    (void *)(np));					\
581 	(np)->cnt--;							\
582 	if ((np)->cnt == 0) {						\
583 		ASSERT(((np)->ref & REF_OWNED) == 0);			\
584 		node_fr(np);		/* node_fr unlocks the lock */	\
585 	} else {							\
586 		mutex_exit(&(np)->lock);				\
587 	}								\
588 }
589 
590 #define	NODE_REFRELE_LOCKED(np) {					\
591 	uint_t	_cnt = (np)->cnt;					\
592 									\
593 	NODE_T_TRACE((np), NODE_T_TRACE_REFRELE | (_cnt - 1));		\
594 	ASSERT(mutex_owned(&(np)->lock));				\
595 									\
596 	if ((np)->ref & REF_OWNED)					\
597 		_cnt--;							\
598 	if (((np)->ref & REF_OWNED) && _cnt == 0)			\
599 		panic("nca NODE_REFRELE_LOCKED: "			\
600 		    "%p has only OWNED reference", (void *)(np));	\
601 	if (_cnt == 0)							\
602 		panic("nca NODE_REFRELEL_LOCKED: "			\
603 		    "%p has no references", (void *)(np));		\
604 	if (_cnt == 1)							\
605 		panic("nca NODE_REFRELEL_LOCKED: "			\
606 		    "%p has only one reference", (void *)(np));		\
607 	(np)->cnt--;							\
608 }
609 
610 
611 /*
612  * NODE_T_TRACE - trace node_t events.
613  *
614  * adb:
615  * 32 bit
616  *	*node_tp,0t8192-(((*node_tp)-node_tv)%0t48)/PXXDDnPnPnPnPnPnPnPnn
617  *	node_tv,((*node_tp)-node_tv)%0t48/PXXDDnPnPnPnPnPnPnPnn
618  *
619  * 64 bit
620  *	*node_tp,0t8192-(((*node_tp)-node_tv)%0t56)/PXXDDnXnXnXnXnXnXnXnn
621  *	node_tv,((*node_tp)-node_tv)%0t56/PXXDDnXnXnXnXnXnXnXnn
622  *
623  * For incremental node tracing, note the value of node_tp (node_tp/X) after
624  * a run, then replace that in the 2nd line for node_tv.
625  */
626 
627 #define	NODE_T_STK_DEPTH	6
628 
629 struct node_ts {
630 	node_t	*node;
631 	unsigned action;
632 	unsigned ref;
633 	unsigned cnt;
634 	int	cpu;
635 	pc_t	stk[NODE_T_STK_DEPTH + 1];
636 };
637 
638 #undef	NODE_T_TRACE_ON
639 
640 #ifdef	NODE_T_TRACE_ON
641 
642 #define	NODE_T_TRACE_ALLOC	0xFF000000	/* kmem_alloc() of */
643 #define	NODE_T_TRACE_ADD	0xFE000000	/* node_add() */
644 
645 #define	NODE_T_TRACE_OWN	0xEF000000	/* node has been owned */
646 #define	NODE_T_TRACE_DISOWN	0xEE000000	/* node has been disowned */
647 #define	NODE_T_TRACE_DESBALLOC	0xED000000	/* desballoc() */
648 #define	NODE_T_TRACE_REFRELE	0xEC000000	/* refrele */
649 #define	NODE_T_TRACE_REFHOLD	0xEB000000	/* refhold */
650 #define	NODE_T_TRACE_NODE_FR	0xEA000000	/* node_fr() */
651 
652 #define	NODE_T_TRACE_TEMPNODE	0xDF000000	/* node_temp() */
653 #define	NODE_T_TRACE_REPLACE	0xDE000000	/* node_replace() */
654 #define	NODE_T_TRACE_FLUSH	0xDD000000	/* node_flush() */
655 #define	NODE_T_TRACE_DOWNCALL	0xDC000000	/* downcall_service() */
656 #define	NODE_T_TRACE_DOWNCALL_2	0xDB000000	/* dcall_service->httpd_data */
657 
658 #define	NODE_T_TRACE_DATA	0xCF000000	/* httpd_data() */
659 
660 #define	NODE_T_TRACE_LRU	0xAF000000	/* nca_lru insert */
661 #define	NODE_T_TRACE_HTTPD	0xAE000000	/* call nca_httpd() */
662 #define	NODE_T_TRACE_MISS	0xAD000000	/* http_miss() */
663 #define	NODE_T_TRACE_TEMP	0xAC000000	/* np != *npp */
664 #define	NODE_T_TRACE_XMIT	0xAB000000	/* tcp_xmit() */
665 #define	NODE_T_TRACE_MISSED	0xAA000000	/* nca_missed() */
666 
667 #define	NODE_T_TRACE_DEL	0x00000000	/* node_del() */
668 
669 #if defined(__i386) || defined(__amd64)
670 #define	NODE_T_TRACE_STK() {						\
671 	_ix = getpcstack(&_p->stk[0], NODE_T_STK_DEPTH + 1);		\
672 	if (_ix < NODE_T_STK_DEPTH + 1) {				\
673 		_p->stk[_ix + 1] = 0;					\
674 	}								\
675 }
676 #else
677 #define	NODE_T_TRACE_STK() {						\
678 	_p->stk[0] = (pc_t)callee();					\
679 	_ix = getpcstack(&_p->stk[1], NODE_T_STK_DEPTH);		\
680 	if (_ix < NODE_T_STK_DEPTH) {					\
681 		_p->stk[_ix + 1] = 0;					\
682 	}								\
683 }
684 #endif
685 
686 #define	NODE_TV_SZ 8192
687 
688 extern struct node_ts node_tv[NODE_TV_SZ];
689 extern struct node_ts *node_tp;
690 
691 #define	NODE_T_TRACE(p, a) {						\
692 	struct node_ts *_p;						\
693 	struct node_ts *_np;						\
694 	int    _ix;							\
695 									\
696 	do {								\
697 		_p = node_tp;						\
698 		if ((_np = _p + 1) == &node_tv[NODE_TV_SZ])		\
699 			_np = node_tv;					\
700 	} while (casptr(&node_tp, _p, _np) != _p);			\
701 	_p->node = (p);							\
702 	_p->action = (a);						\
703 	_p->ref = (p) ? (p)->ref : 0;					\
704 	_p->cnt = (p) ? (p)->cnt : 0;					\
705 	_p->cpu = CPU->cpu_seqid;					\
706 	NODE_T_TRACE_STK();						\
707 }
708 
709 #else	/* NODE_T_TRACE_ON */
710 
711 #define	NODE_T_TRACE(p, a)
712 
713 #endif	/* NODE_T_TRACE_ON */
714 
715 /*
716  * DOOR_TRACE - trace door node_t events.
717  *
718  * adb:
719  * 32 bit
720  *	*door_tp,0t8192-(((*door_tp)-door_tv)%0t112)/5XnPnPnPnPnPnPnPn64cnn
721  *	door_tv,((*door_tp)-door_tv)%0t112/5XnPnPnPnPnPnPnPn64cnn
722  * 64 bit
723  *	*door_tp,0t8192-(((*door_tp)-door_tv)%0t128)/PXPXXnXnXnXnXnXnXnXn64cnn
724  *	door_tv,((*door_tp)-door_tv)%0t128/PXPXXnXnXnXnXnXnXnXn64cnn
725  */
726 
727 #define	DOOR_STK_DEPTH	6
728 
729 struct door_ts {
730 	struct conn_s *cp;
731 	unsigned action;
732 	node_t	*np;
733 	int	ref;
734 	unsigned state;
735 	pc_t	stk[DOOR_STK_DEPTH + 1];
736 	char	data[64];
737 };
738 
739 #undef	DOOR_TRACE_ON
740 
741 #ifdef	DOOR_TRACE_ON
742 
743 #define	DOOR_TRACE_UPCALL	0xF0000000	/* upcall() */
744 #define	DOOR_TRACE_UPCALL_RAW	0xF1000000	/* upcall() RAW ? */
745 #define	DOOR_TRACE_UPCALL_RET	0xFF000000	/* upcall() return */
746 
747 #define	DOOR_TRACE_DOWNCALL	0xE0000000	/* downcall() */
748 #define	DOOR_TRACE_CONNECT	0xE1000000	/* connect() */
749 #define	DOOR_TRACE_CONNECT_DATA	0xE2000000	/* connect() */
750 #define	DOOR_TRACE_DIRECTFROM	0xE3000000	/* tee_splice() from */
751 #define	DOOR_TRACE_DIRECTTO	0xE4000000	/* tee_splice() to */
752 #define	DOOR_TRACE_DOWNCALL_RET	0xEF000000	/* downcall() return */
753 
754 #define	DOOR_TRACE_INIT		0x80000000	/* doorcall_init() */
755 #define	DOOR_TRACE_INIT_RET	0x88000000	/* doorcall_init() return */
756 
757 #if defined(__i386) || defined(__amd64)
758 #define	DOOR_TRACE_STK() {						\
759 	_ix = getpcstack(&_p->stk[0], DOOR_STK_DEPTH + 1);		\
760 	if (_ix < DOOR_STK_DEPTH + 1) {					\
761 		_p->stk[_ix] = 0;					\
762 	}								\
763 }
764 #else
765 #define	DOOR_TRACE_STK() {						\
766 	_p->stk[0] = (pc_t)callee();					\
767 	_ix = getpcstack(&_p->stk[1], DOOR_STK_DEPTH);			\
768 	if (_ix < DOOR_STK_DEPTH) {					\
769 		_p->stk[_ix + 1] = 0;					\
770 	}								\
771 }
772 #endif
773 
774 #define	DOOR_TV_SZ 8192
775 
776 extern struct door_ts door_tv[DOOR_TV_SZ];
777 extern struct door_ts *door_tp;
778 
779 #define	DOOR_TRACE(io, d, d_sz, a) {				\
780 	nca_conn_t *_cp = (io) ? (nca_conn_t *)(io)->cid : (nca_conn_t *)NULL; \
781 	node_t *_req_np = _cp ? _cp->req_np : (node_t *)NULL;		\
782 	struct door_ts *_p;						\
783 	struct door_ts *_np;						\
784 	int    _ix;							\
785 									\
786 	do {								\
787 		_p = door_tp;						\
788 		if ((_np = _p + 1) == &door_tv[DOOR_TV_SZ])		\
789 			_np = door_tv;					\
790 	} while (casptr(&door_tp, _p, _np) != _p);			\
791 	_p->cp = _cp;							\
792 	_p->np = _req_np;						\
793 	_p->action = (a);						\
794 	_p->ref = _req_np ? _req_np->ref : 0;				\
795 	if ((io)) {							\
796 		_p->state = ((io)->op == http_op ? 0x80000000 : 0) |	\
797 			    ((io)->more ? 0x40000000 : 0) |		\
798 			    ((io)->first ? 0x20000000 : 0) |		\
799 			    ((io)->advisory ? 0x10000000 : 0) |		\
800 			    ((io)->nocache ? 0x08000000 : 0) |		\
801 			    ((io)->preempt ? 0x04000000 : 0) |		\
802 			    ((io)->peer_len ? 0x02000000 : 0) |		\
803 			    ((io)->local_len ? 0x01000000 : 0) |	\
804 			    ((io)->data_len ? 0x00800000 : 0) |		\
805 			    (((io)->direct_type << 20) & 0x00700000) |	\
806 			    ((io)->direct_len ? 0x00080000 : 0) |	\
807 			    ((io)->trailer_len ? 0x00040000 : 0) |	\
808 			    (((io)->peer_len + (io)->local_len +	\
809 			    (io)->data_len + (io)->direct_len +		\
810 			    (io)->trailer_len) & 0x3FFFF);		\
811 	} else {							\
812 		_p->state = 0;						\
813 	}								\
814 	if ((d_sz)) {							\
815 		int _n = MIN((d_sz), 63);				\
816 									\
817 		bcopy((d), _p->data, _n);				\
818 		bzero(&_p->data[_n], 64 - _n);				\
819 	} else {							\
820 		bzero(_p->data, 64);					\
821 	}								\
822 	DOOR_TRACE_STK();						\
823 }
824 
825 #else	/* DOOR_TRACE_ON */
826 
827 #define	DOOR_TRACE(io, d, d_sz, a)
828 
829 #endif	/* DOOR_TRACE_ON */
830 
831 /*
832  * NCA node LRU cache.  Defined here so that the NCA mdb module can use it.
833  */
834 typedef struct lru_s {
835 	node_t		*phead;	/* Phys LRU list head (MRU) */
836 	node_t		*ptail;	/* Phys LRU list tail (LRU) */
837 	node_t		*vhead;	/* Virt LRU list head (MRU) */
838 	node_t 		*vtail;	/* Virt LRU list tail (LRU) */
839 
840 	uint32_t	pcount;	/* Phys count of node_t members */
841 	uint32_t	vcount;	/* Virt count of node_t members */
842 
843 	kmutex_t	lock;	/* Guarantee atomic access of above */
844 } lru_t;
845 
846 /*
847  * Per CPU instance structure.
848  *
849  * 32-bit adb: XXXnnDnnXXnnXXnnXDnnXXnn228+na
850  * 64-bit adb: PPPnnD4+nnPPnnPPnnJDnnJ180+na
851  */
852 
853 typedef struct nca_cpu_s {
854 
855 	node_t *persist_hdr_none;
856 	node_t *persist_hdr_close;
857 	node_t *persist_hdr_ka;
858 
859 	uint32_t dcb_readers;	/* count of dcb_list readers for this CPU */
860 
861 	nca_squeue_t *if_inq;	/* if_t input nca_squeue_t */
862 	nca_squeue_t *if_ouq;	/* if_t output nca_squeue_t */
863 
864 	ti_t	*tcp_ti;	/* TCP TIMER list */
865 	tw_t	*tcp_tw;	/* TCP TIME_WAIT list */
866 
867 	ddi_softintr_t soft_id;	/* soft interrupt id for if_inq worker */
868 	int	if_inq_cnt;	/* count of if_t.inq references */
869 
870 	char	pad[256 - sizeof (node_t *) - sizeof (node_t *) -
871 		    sizeof (node_t *) - sizeof (uint32_t) -
872 		    sizeof (nca_squeue_t *) - sizeof (nca_squeue_t *) -
873 		    sizeof (ti_t *) - sizeof (tw_t *) -
874 		    sizeof (ddi_softintr_t) - sizeof (int)];
875 } nca_cpu_t;
876 
877 extern nca_cpu_t *nca_gv;	/* global per CPU state indexed by cpu_seqid */
878 
879 /*
880  * hcb_t - host control block.
881  *
882  * Used early on in packet switching to select packets to be serviced by NCA
883  * and optionally later on by the HTTP protocol layer to further select HTTP
884  * request to be serviced.
885  *
886  * dcb_t - door control block.
887  *
888  * Used to associate one or more hcb_t(s) with a given httpd door instance.
889  *
890  * dcb_list - dcb_t global list, a singly linked grounded list of dcb_t's.
891  *
892  * Used to search for a hcb_t match, currently a singly linked grounded list
893  * of dcb_t's with a linear walk of the list. While this is adequate for the
894  * current httpd support (i.e. a single door) a move to either a hash or tree
895  * will be required for multiple httpd instance support (i.e. multiple doors).
896  *
897  * The dcb_list is protected by a custom reader/writer lock, the motivation
898  * for using a custom lock instead of a krwlock_t is that this lock is the
899  * single hot spot in NCA (i.e. all in-bound packets must acquire this lock)
900  * and a nonlocking atomic readers count scheme is used in the common case
901  * (i.e. reader lock) with a fall-back to a conventional kmutex_t for writer
902  * (i.e. ndd list add/delete).
903  */
904 
905 typedef struct hcb_s {
906 	struct hcb_s	*next;		/* Next hcb_t (none: NULL) */
907 	ipaddr_t	addr;		/* IP address (any: INADDR_ANY or 0) */
908 	uint16_t	port;		/* TCP port number */
909 	char		*host;		/* Host: name (any: NULL) */
910 	ssize_t		hostsz;		/* Size of above */
911 	char		*root;		/* Document root ("/": NULL) */
912 	ssize_t		rootsz;		/* Size of above */
913 } hcb_t;
914 
915 typedef struct dcb_s {
916 	struct dcb_s	*next;		/* Next dcb_t (none: NULL) */
917 	char		*door;		/* Door file (default: NULL) */
918 	ssize_t		doorsz;		/* Size of above */
919 	door_handle_t	hand;		/* Door handle (default: NULL) */
920 	hcb_t		list;		/* Head of a hcb_t list (any: NULL) */
921 } dcb_t;
922 
923 extern dcb_t dcb_list;
924 extern kmutex_t nca_dcb_lock;
925 extern kcondvar_t nca_dcb_wait;
926 extern kmutex_t nca_dcb_readers;
927 
928 #define	NOHANDLE ((door_handle_t)-1)
929 
930 #define	DCB_COUNT_USELOCK	0x80000000
931 #define	DCB_COUNT_MASK		0x3FFFFFFF
932 
933 #define	DCB_RD_ENTER(cpu) {						\
934 	uint32_t *rp;							\
935 									\
936 	cpu = CPU->cpu_seqid;						\
937 	rp = &nca_gv[cpu].dcb_readers;					\
938 	while (atomic_add_32_nv(rp, 1) & DCB_COUNT_USELOCK) {		\
939 		/* Need to use the lock, so do the dance */		\
940 		mutex_enter(&nca_dcb_lock);				\
941 		if (atomic_add_32_nv(rp, -1) == DCB_COUNT_USELOCK &&	\
942 		    CV_HAS_WAITERS(&nca_dcb_wait)) {			\
943 			/* May be the last reader for this CPU */	\
944 			cv_signal(&nca_dcb_wait);			\
945 		}							\
946 		mutex_exit(&nca_dcb_lock);				\
947 		mutex_enter(&nca_dcb_readers);				\
948 		/*							\
949 		 * We block above waiting for the writer to exit the	\
950 		 * readers lock, if we didn't block then while we were	\
951 		 * away in the nca_dcb_lock enter the writer exited,	\
952 		 * we could optimize for this case by checking USELOCK	\
953 		 * after the decrement, but as this is an exceptional	\
954 		 * case not in the fast-path we'll just take the hit	\
955 		 * of a needless readers enter/exit.			\
956 		 */							\
957 		mutex_exit(&nca_dcb_readers);				\
958 	}								\
959 }
960 
961 #define	DCB_RD_EXIT(cpu) {						\
962 	uint32_t *rp = &nca_gv[cpu].dcb_readers;			\
963 									\
964 	if (atomic_add_32_nv(rp, -1) == DCB_COUNT_USELOCK) {		\
965 		mutex_enter(&nca_dcb_lock);				\
966 		if (CV_HAS_WAITERS(&nca_dcb_wait)) {			\
967 			/* May be the last reader for this CPU */	\
968 			cv_signal(&nca_dcb_wait);			\
969 		}							\
970 		mutex_exit(&nca_dcb_lock);				\
971 	}								\
972 }
973 
974 #define	DCB_WR_ENTER() {						\
975 	int cpu;							\
976 	int readers;							\
977 									\
978 	mutex_enter(&nca_dcb_readers);					\
979 	mutex_enter(&nca_dcb_lock);					\
980 	for (;;) {							\
981 		readers = 0;						\
982 		for (cpu = 0; cpu < max_ncpus; cpu++) {			\
983 			int new;					\
984 			uint32_t *rp = &nca_gv[cpu].dcb_readers;	\
985 			int old = *rp;					\
986 									\
987 			if (old & DCB_COUNT_USELOCK) {			\
988 				readers += old & DCB_COUNT_MASK;	\
989 				continue;				\
990 			}						\
991 			new = old | DCB_COUNT_USELOCK;			\
992 			while (cas32(rp, old, new) != old) {		\
993 				old = *rp;				\
994 				new = old | DCB_COUNT_USELOCK;		\
995 			}						\
996 			readers += (new & DCB_COUNT_MASK);		\
997 		}							\
998 		if (readers == 0)					\
999 			break;						\
1000 		cv_wait(&nca_dcb_wait, &nca_dcb_lock);			\
1001 	}								\
1002 	mutex_exit(&nca_dcb_lock);					\
1003 }
1004 
1005 #define	DCB_WR_EXIT() {							\
1006 	int cpu;							\
1007 									\
1008 	mutex_enter(&nca_dcb_lock);					\
1009 	for (cpu = 0; cpu < max_ncpus; cpu++) {				\
1010 		int new;						\
1011 		uint32_t *rp = &nca_gv[cpu].dcb_readers;		\
1012 		int old = *rp;						\
1013 									\
1014 		new = old & ~DCB_COUNT_USELOCK;				\
1015 		while (cas32(rp, old, new) != old) {			\
1016 			old = *rp;					\
1017 			new = old & ~DCB_COUNT_USELOCK;			\
1018 		}							\
1019 	}								\
1020 	mutex_exit(&nca_dcb_lock);					\
1021 	mutex_exit(&nca_dcb_readers);					\
1022 }
1023 
1024 typedef struct nca_door_s {
1025 	door_handle_t	handle;		/* The door handle */
1026 	char		*name;		/* The door name */
1027 	kmutex_t	lock;		/* The door lock */
1028 	kcondvar_t	cv_writer;	/* condvar for thread waiting */
1029 					/* to do door_init */
1030 	kcondvar_t	cv_reader;	/* condvar for thread waiting */
1031 					/* for a door_init to finish */
1032 	uint32_t	upcalls;	/* Number of upcalls in progress */
1033 	boolean_t	init_waiting;	/* door_init thread wanting to */
1034 					/* be exclusive */
1035 } nca_door_t;
1036 
1037 /*
1038  * if_t - interface per instance data.
1039  */
1040 
1041 typedef struct if_s {
1042 
1043 	boolean_t dev;		/* is a device instance */
1044 
1045 	queue_t	*rqp;		/* our read-side STREAMS queue */
1046 	queue_t	*wqp;		/* our write-side STREAMS queue */
1047 
1048 	/* DLPI M_DATA IP fastpath template */
1049 	size_t	mac_length;
1050 	mblk_t	*mac_mp;
1051 	int32_t	mac_mtu;
1052 	int32_t	mac_addr_len;
1053 
1054 	uint32_t ip_ident;	/* our IP ident value */
1055 
1056 	boolean_t hwcksum;	/* underlying NIC supports checksum offload */
1057 
1058 	nca_squeue_t *inq;		/* in-bound nca_squeue_t */
1059 	nca_squeue_t *ouq;		/* out-bound nca_squeue_t */
1060 
1061 	/*
1062 	 * All if_t are associated with a CPU and have a default
1063 	 * router on link are chained in a circular linked list.
1064 	 */
1065 	struct if_s *next_if;
1066 	struct if_s *prev_if;
1067 	ipaddr_t local_addr;	/* This interface's IP address. */
1068 	uchar_t router_ether_addr[6];
1069 
1070 	uint_t	hdr_ioc_id;	/* id of DL_IOC_HDR_INFO M_IOCTL sent down */
1071 	boolean_t info_req_pending;
1072 
1073 	int32_t	capab_state;	/* Capability probe state */
1074 
1075 	/* Bound local address of a NCAfs instance. */
1076 	struct sockaddr_in	bound_addr;
1077 } if_t;
1078 
1079 /*
1080  * connf_t - connection fanout data.
1081  *
1082  * The hash tables and their linkage (hashnextp, hashprevp) are protected
1083  * by the per-bucket lock. Each nca_conn_t inserted in the list points back at
1084  * the connf_t that heads the bucket.
1085  */
1086 
1087 typedef struct connf_s {
1088 	uint32_t	max;
1089 	struct conn_s	*head;
1090 	kmutex_t	lock;
1091 } connf_t;
1092 
1093 #ifdef	CONNP_T_TRACE_ON
1094 
1095 #define	CONNP_TV_SZ 32
1096 
1097 /*
1098  * Per nca_conn_t packet tracing.
1099  */
1100 typedef struct connp_s {
1101 	clock_t		lbolt;
1102 	clock_t		tcp_ti;
1103 	int32_t		len : 16,
1104 			dir : 1,
1105 			state : 4,
1106 			flags : 6,
1107 			xmit_np : 1,
1108 			xmit_head : 1,
1109 			unsent : 1,
1110 			tail_unsent : 1,
1111 			direct : 1;
1112 	uint32_t	state1;
1113 	uint32_t	state2;
1114 	uint32_t	seq;
1115 	uint32_t	ack;
1116 	uint32_t	snxt;
1117 	uint32_t	swnd;
1118 } connp_t;
1119 
1120 #endif	/* CONNP_T_TRACE_ON */
1121 
1122 /*
1123  * nca_conn_t - connection per instance data.
1124  *
1125  * Note: hashlock is used to provide atomic access to all nca_conn_t members
1126  * above it. All other members are protected by the per CPU inq nca_squeue_t
1127  * which is used to serialize access to all nca_conn_t's per interface.
1128  *
1129  * Note: the nca_conn_t can have up to 3 NODE_REFHOLDs:
1130  *
1131  *	1) if req_np != NULL then a NODE_REFHOLD(req_np) was done:
1132  *
1133  *	    1.1) if http_refed then a NODE_REFHOLD(req_np) was done
1134  *
1135  *	    1.2) if http_frefed then a NODE_REFHOLD(req_np->fileback) was done
1136  *
1137  *
1138  * TODO: reorder elements in fast-path code access order.
1139  *
1140  * Dnn4XnXXDnnDnnXXXnnXXXnnUXnnXXXnnXXnnDDXXXDXDXDXnnDnnXXDDnXXXDDnnXXXDDnn
1141  * XXXDDnnXXXDDnnXXXDDnnXXnnDXXnn
1142  * b+++DDnAnDDDDDnnDnnUnnUUDXDUnnDnn20xnnXnnddnnUUUnnXXUnXXnnUUUnn
1143  * DDDDDDnnUUnnXXUXUnn4UD4Unn4UnUUnn
1144  * 64-bit: Xnn4+4pnnppEnEnn3pnn3pnnEJnnXXnnuunn4+ppnnXX3pD4+pD4+pD4+pnnEnnppnnD
1145  */
1146 
1147 #define	TCP_XMIT_MAX_IX	5		/* Max xmit descriptors */
1148 
1149 typedef struct conn_s {
1150 
1151 	int32_t ref;			/* Reference counter */
1152 
1153 	te_t	tcp_ti;			/* TCP TIMER timer entry */
1154 
1155 	struct conn_s	*twnext;	/* TIME_WAIT next */
1156 	struct conn_s	*twprev;	/* TIME_WAIT prev */
1157 	clock_t	twlbolt;		/* TIME_WAIT lbolt */
1158 
1159 	clock_t create;			/* Create lbolt time */
1160 
1161 	connf_t	*hashfanout;		/* Hash bucket we're part of */
1162 	struct conn_s	*hashnext;	/* Hash chain next */
1163 	struct conn_s	*hashprev;	/* Hash chain prev */
1164 
1165 	struct conn_s	*bindnext;	/* Next conn_s in bind list. */
1166 	struct conn_s	*bindprev;	/* Prev conn_s in bind list. */
1167 	void		*tbf;		/* Pointer to bind hash list struct. */
1168 	/*
1169 	 * Note: atomic access of memebers above is guaranteed by the
1170 	 * hashfanout->lock of the hash bucket that the nca_conn_t is in.
1171 	 */
1172 
1173 	size_t	mac_length;		/* MAC prepend length */
1174 	mblk_t	*mac_mp;		/* MAC prepend data */
1175 
1176 	ipaddr_t	laddr;		/* Local address */
1177 	ipaddr_t	faddr;		/* Remote address. 0 => not connected */
1178 
1179 	union {
1180 		struct {
1181 			uint16_t u_fport; /* Remote port */
1182 			uint16_t u_lport; /* Local port */
1183 		} u_ports1;
1184 		uint32_t u_ports2;	/* Rem port, local port */
1185 					/* Used for TCP_MATCH performance */
1186 	} u_port;
1187 #define	conn_lport	u_port.u_ports1.u_lport
1188 #define	conn_fport	u_port.u_ports1.u_fport
1189 #define	conn_ports	u_port.u_ports2
1190 
1191 	if_t	*ifp;			/* Interface for this connection */
1192 	nca_squeue_t *inq;		/* Per CPU inq for this connection */
1193 
1194 	uint32_t req_tag;		/* nca_io_t request tag (0 == NONE) */
1195 	int	req_parse;		/* HTTP request parse state */
1196 	node_t	*req_np;		/* HTTP request node_t */
1197 	mblk_t	*req_mp;		/* HTTP request mblk_t */
1198 	char	*reqpath;		/* HTTP request URI path component */
1199 	int	reqpathsz;		/* size of above */
1200 	char	*reqrefer;		/* HTTP "Referer:" string */
1201 	int	reqrefersz;		/* size of above */
1202 	char	*requagent;		/* HTTP "User-Agent:" string */
1203 	int	requagentsz;		/* size of above */
1204 	struct conn_s *nodenext;	/* Node_t nca_conn_t list */
1205 
1206 	clock_t	http_count;		/* HTTP Keep-Alive request count */
1207 
1208 	/*
1209 	 * req_np xmit state used accross calls to tcp_xmit(). A reference
1210 	 * to the req_np and to any inderect node_t (i.e. file/ctag) ...
1211 	 */
1212 	node_t	*xmit_refed;		/* have a ref to the uri node_t */
1213 	node_t	*xmit_cur;		/* current node to transmit */
1214 
1215 	int	xmit_ix;		/* current xmit[] index */
1216 	int	xmit_pix;		/* past end xmit[] index */
1217 
1218 	struct {
1219 		node_t	*np;		/* node_t pointer for ref */
1220 		char	*dp;		/* data pointer */
1221 		uint16_t *cp;		/* cksum array */
1222 		int	sz;		/* remaining data to xmit */
1223 		int	iso;		/* initial segment offset (if any) */
1224 		node_t	*refed;		/* have a ref to the node_t */
1225 		int	dsz;		/* remaining data for current segment */
1226 		caddr_t	*dvp;		/* data segment virtual pointer */
1227 	} xmit[TCP_XMIT_MAX_IX];
1228 
1229 	/*
1230 	 * Connection NCA_IO_DIRECT_SPLICE & NCA_IO_DIRECT_TEE reference,
1231 	 * see direct_splice and direct_tee below for type of send too.
1232 	 */
1233 	struct conn_s	*direct;	/* nca_conn_t to send recv data too */
1234 	mblk_t		*direct_mp;	/* mblk_t to use for tcp_close() */
1235 
1236 	/*
1237 	 * nca_conn_t state.
1238 	 */
1239 
1240 	int32_t	tcp_state;
1241 
1242 	uint32_t
1243 		tcp_urp_last_valid : 1,	/* Is tcp_urp_last valid? */
1244 		tcp_hard_binding : 1,	/* If we've started a full bind */
1245 		tcp_hard_bound : 1,	/* If we've done a full bind with IP */
1246 		tcp_fin_acked : 1,	/* Has our FIN been acked? */
1247 
1248 		tcp_fin_rcvd : 1,	/* Have we seen a FIN? */
1249 		tcp_fin_sent : 1,	/* Have we sent our FIN yet? */
1250 		tcp_ordrel_done : 1,	/* Have we sent the ord_rel upstream? */
1251 		tcp_flow_stopped : 1,	/* Have we flow controlled xmitter? */
1252 
1253 		tcp_debug : 1,		/* SO_DEBUG "socket" option. */
1254 		tcp_dontroute : 1,	/* SO_DONTROUTE "socket" option. */
1255 		tcp_broadcast : 1,	/* SO_BROADCAST "socket" option. */
1256 		tcp_useloopback : 1,	/* SO_USELOOPBACK "socket" option. */
1257 
1258 		tcp_oobinline : 1,	/* SO_OOBINLINE "socket" option. */
1259 		tcp_dgram_errind : 1,	/* SO_DGRAM_ERRIND option */
1260 		tcp_detached : 1,	/* If we're detached from a stream */
1261 		tcp_bind_pending : 1,	/* Client is waiting for bind ack */
1262 
1263 		tcp_unbind_pending : 1, /* Client sent T_UNBIND_REQ */
1264 		tcp_deferred_clean_death : 1,
1265 					/* defer tcp endpoint cleanup etc. */
1266 		tcp_co_wakeq_done : 1,	/* A strwakeq() has been done */
1267 		tcp_co_wakeq_force : 1,	/* A strwakeq() must be done */
1268 
1269 		tcp_co_norm : 1,	/* In normal mode, putnext() done */
1270 		tcp_co_wakeq_need : 1,	/* A strwakeq() needs to be done */
1271 		tcp_snd_ws_ok : 1,	/* Received WSCALE from peer */
1272 		tcp_snd_ts_ok : 1,	/* Received TSTAMP from peer */
1273 
1274 		tcp_linger : 1,		/* SO_LINGER turned on */
1275 		tcp_zero_win_probe: 1,	/* Zero win probing is in progress */
1276 		tcp_loopback: 1,	/* src and dst are the same machine */
1277 		tcp_localnet: 1,	/* src and dst are on the same subnet */
1278 
1279 		tcp_syn_defense: 1,	/* For defense against SYN attack */
1280 #define	tcp_dontdrop	tcp_syn_defense
1281 		tcp_set_timer : 1,
1282 		tcp_1_junk_fill_thru_bit_31 : 2;
1283 
1284 	uint32_t
1285 		tcp_active_open: 1,	/* This is a active open */
1286 		tcp_timeout : 1,	/* qbufcall failed, qtimeout pending */
1287 		tcp_rexmit : 1,		/* TCP is retransmitting */
1288 		tcp_snd_sack_ok : 1,	/* Can use SACK for this connection */
1289 
1290 		tcp_bind_proxy_addr : 1,	/* proxy addr is being used */
1291 		tcp_recvdstaddr : 1,	/* return T_EXTCONN_IND with dst addr */
1292 		tcp_refed : 1,		/* nca_conn_t refed by TCP */
1293 		tcp_time_wait_comp : 1, /* TIME_WAIT compressed nca_conn_t */
1294 
1295 		tcp_close : 1,		/* nca_conn_t close */
1296 		http_persist : 3,	/* HTTP persistent connection state */
1297 
1298 		deferred_xmit_end : 1,	/* xmit_end() deferred to xmit() */
1299 		http_direct_splice : 1,	/* have a connection to splice too */
1300 		http_direct_tee : 1,	/* have a connection to tee too */
1301 
1302 		tcp_2_junk_fill_thru_bit_31 : 17;
1303 /*
1304  * Note: all nca_conn_t members to be accessed by a tcp_time_wait_comp
1305  * nca_conn_t must be above this point !!!
1306  */
1307 
1308 	uchar_t	tcp_timer_backoff;	/* Backoff shift count. */
1309 	clock_t tcp_last_recv_time;	/* Last time we receive a segment. */
1310 	clock_t	tcp_dack_set_time;	/* When delayed ACK timer is set. */
1311 
1312 	int	tcp_ip_hdr_len;		/* Byte len of our current IP header */
1313 	clock_t	tcp_first_timer_threshold;  /* When to prod IP */
1314 	clock_t	tcp_second_timer_threshold; /* When to give up completely */
1315 	clock_t	tcp_first_ctimer_threshold; /* 1st threshold while connecting */
1316 	clock_t tcp_second_ctimer_threshold; /* 2nd ... while connecting */
1317 
1318 	clock_t	tcp_last_rcv_lbolt; /* lbolt on last packet, used for PAWS */
1319 
1320 
1321 	uint32_t tcp_obsegs;		/* Outbound segments on this stream */
1322 
1323 	uint32_t tcp_mss;		/* Max segment size */
1324 	uint32_t tcp_naglim;		/* Tunable nagle limit */
1325 	int32_t	tcp_hdr_len;		/* Byte len of combined TCP/IP hdr */
1326 	tcph_t	*tcp_tcph;		/* tcp header within combined hdr */
1327 	int32_t	tcp_tcp_hdr_len;	/* tcp header len within combined */
1328 	uint32_t	tcp_valid_bits;
1329 #define	TCP_ISS_VALID	0x1	/* Is the tcp_iss seq num active? */
1330 #define	TCP_FSS_VALID	0x2	/* Is the tcp_fss seq num active? */
1331 #define	TCP_URG_VALID	0x4	/* If the tcp_urg seq num active? */
1332 
1333 	int32_t	tcp_xmit_hiwater;	/* Send buffer high water mark. */
1334 
1335 	union {				/* template ip header */
1336 		ipha_t	tcp_u_ipha;
1337 		char	tcp_u_buf[IP_SIMPLE_HDR_LENGTH+TCP_MIN_HEADER_LENGTH];
1338 		double	tcp_u_aligner;
1339 	} tcp_u;
1340 #define	tcp_ipha	tcp_u.tcp_u_ipha
1341 #define	tcp_iphc	tcp_u.tcp_u_buf
1342 
1343 	uint32_t tcp_sum;		/* checksum to compensate for source */
1344 					/* routed packets. Host byte order */
1345 
1346 	uint16_t tcp_last_sent_len;	/* Record length for nagle */
1347 	uint16_t tcp_dupack_cnt;	/* # of consequtive duplicate acks */
1348 
1349 	uint32_t tcp_rnxt;		/* Seq we expect to recv next */
1350 	uint32_t tcp_rwnd;		/* Current receive window */
1351 	uint32_t tcp_rwnd_max;		/* Maximum receive window */
1352 
1353 	mblk_t	*tcp_rcv_head;		/* Queued until push, urgent data or */
1354 	mblk_t	*tcp_rcv_tail;		/* the count exceeds */
1355 	uint32_t tcp_rcv_cnt;		/* tcp_rcv_push_wait. */
1356 
1357 	mblk_t	*tcp_reass_head;	/* Out of order reassembly list head */
1358 	mblk_t	*tcp_reass_tail;	/* Out of order reassembly list tail */
1359 
1360 	uint32_t tcp_cwnd_ssthresh;	/* Congestion window */
1361 	uint32_t tcp_cwnd_max;
1362 	uint32_t tcp_csuna;		/* Clear (no rexmits in window) suna */
1363 
1364 	int	tcp_rttv_updates;
1365 	clock_t	tcp_rto;		/* Round trip timeout */
1366 	clock_t	tcp_rtt_sa;		/* Round trip smoothed average */
1367 	clock_t	tcp_rtt_sd;		/* Round trip smoothed deviation */
1368 	clock_t	tcp_rtt_update;		/* Round trip update(s) */
1369 	clock_t tcp_ms_we_have_waited;	/* Total retrans time */
1370 
1371 	uint32_t tcp_swl1;		/* These help us avoid using stale */
1372 	uint32_t tcp_swl2;		/*  packets to update state */
1373 
1374 	mblk_t	*tcp_xmit_head;		/* Head of rexmit list */
1375 	mblk_t	*tcp_xmit_last;		/* last valid data seen by tcp_wput */
1376 	uint32_t tcp_unsent;		/* # of bytes in hand that are unsent */
1377 	mblk_t	*tcp_xmit_tail;		/* Last rexmit data sent */
1378 	uint32_t tcp_xmit_tail_unsent;	/* # of unsent bytes in xmit_tail */
1379 
1380 	uint32_t tcp_snxt;		/* Senders next seq num */
1381 	uint32_t tcp_suna;		/* Sender unacknowledged */
1382 	uint32_t tcp_rexmit_nxt;	/* Next rexmit seq num */
1383 	uint32_t tcp_rexmit_max;	/* Max retran seq num */
1384 	int32_t	tcp_snd_burst;		/* Send burst factor */
1385 	uint32_t tcp_swnd;		/* Senders window (relative to suna) */
1386 	uint32_t tcp_cwnd;		/* Congestion window */
1387 	int32_t tcp_cwnd_cnt;		/* cwnd cnt in congestion avoidance */
1388 	uint32_t tcp_ackonly;		/* Senders last ack seq num */
1389 
1390 	uint32_t tcp_irs;		/* Initial recv seq num */
1391 	uint32_t tcp_iss;		/* Initial send seq num */
1392 	uint32_t tcp_fss;		/* Final/fin send seq num */
1393 	uint32_t tcp_urg;		/* Urgent data seq num */
1394 
1395 	uint32_t tcp_rack;		/* Seq # we have acked */
1396 	uint32_t tcp_rack_cnt;		/* # of bytes we have deferred ack */
1397 
1398 	uint32_t tcp_max_swnd;		/* Maximum swnd we have seen */
1399 	int64_t	tcp_rexmit_fire_time;
1400 	int64_t	tcp_dack_fire_time;
1401 	int64_t tcp_ka_fire_time;
1402 	int64_t	tcp_http_ka_fire_time;
1403 
1404 	int32_t	tcp_keepalive_intrvl;	/* Zero means don't bother */
1405 	int32_t	tcp_ka_probe_sent;
1406 	int32_t tcp_ka_last_intrvl;
1407 
1408 #define	TCP_DACK_TIMER		0x1
1409 #define	TCP_REXMIT_TIMER	0x2
1410 #define	TCP_KA_TIMER		0x4
1411 #define	TCP_HTTP_KA_TIMER	0x8
1412 	int16_t		tcp_running_timer;
1413 	int16_t		tcp_pending_timer;
1414 
1415 #ifdef	CONNP_T_TRACE_ON
1416 	connp_t *pkt_tp;		/* Packet tracing pointer */
1417 	connp_t	pkt_tv[CONNP_TV_SZ];	/* Packet tracing vector */
1418 #endif	/* CONNP_T_TRACE_ON */
1419 
1420 } nca_conn_t;
1421 
1422 /*
1423  * Active stack support parameters to control what ports NCA can use.
1424  * They are declared in ncaproto.c
1425  */
1426 extern struct nca_tbf_s *nca_tcp_port;
1427 extern in_port_t tcp_lo_port;
1428 extern in_port_t tcp_hi_port;
1429 
1430 /*
1431  * nca_conn_t.http_persist values and corresponding HTTP header strings are
1432  * used to determine the connection persistent state of a connection and
1433  * any HTTP header which needs to be sent.
1434  */
1435 
1436 #define	PERSIST_NONE		0	/* Not persistent */
1437 
1438 #define	PERSIST_CLOSE		1	/* Was persistent, send close header */
1439 #define	PERSIST_TRUE		2	/* Connection is HTTP persistent */
1440 #define	PERSIST_KA		3	/* Persistent, send Keep-Alive header */
1441 #define	PERSIST_UPCALL		4	/* Insert "Connection: close" on */
1442 					/* upcall and clear flag */
1443 
1444 #define	PERSIST_HDR_NONE	"\r\n"
1445 #define	PERSIST_HDR_CLOSE	"Connection: close\r\n\r\n"
1446 #define	PERSIST_HDR_KA		"Connection: Keep-Alive\r\n\r\n"
1447 
1448 /*
1449  * nca_conn_t nca_squeue_ctl() flag values:
1450  */
1451 
1452 #define	CONN_MISS_DONE		0x0001	/* The conn miss processing is done */
1453 #define	IF_TIME_WAIT		0x0002	/* A TIME_WAIT has fired */
1454 #define	IF_TCP_TIMER		0x0003	/* A TCP TIMER has fired */
1455 #define	NCA_CONN_TCP_TIMER	0x0004	/* A TCP TIMER needs to be execed */
1456 #define	IF_TCP_CONNECT		0x0005	/* TCP connection request */
1457 #define	IF_TCP_SEND		0x0006	/* A new send request. */
1458 
1459 #define	IF_TCP_DIRECT_TO	0x0010	/* A TCP direct i/o, step 1 */
1460 #define	IF_TCP_DIRECT_FROM	0x0012	/* A TCP direct i/o, step 2 */
1461 #define	IF_TCP_DIRECT_TEE	0x0001	/* If a tee else a splice */
1462 #define	IF_TCP_DIRECT_CLOSE	0x001F	/* A TCP direct i/o close */
1463 
1464 #define	NCA_CONN_T_STK_DEPTH	7	/* max stack backtrace depth */
1465 
1466 struct conn_ts {
1467 	nca_conn_t	*conn;
1468 	unsigned action;
1469 	int	ref;
1470 	int	cpu;
1471 	pc_t	stk[NCA_CONN_T_STK_DEPTH + 1];
1472 };
1473 
1474 #undef	NCA_CONN_T_TRACE_ON
1475 
1476 #ifdef	NCA_CONN_T_TRACE_ON
1477 
1478 /*
1479  * adb:
1480  * 32 bit
1481  *	*conn_tp,0t4096-(((*conn_tp)-con_tv)%0t48)/PXDDnPnPnPnPnPnPnPnPnn
1482  *	con_tv,((*conn_tp)-con_tv)%0t48/PXDDnPnPnPnPnPnPnPnPnn
1483  * 64 bit
1484  *	*conn_tp,0t4096-(((*conn_tp)-con_tv)%0t56)/PXDDnXnXnXnXnXnXnXnXnn
1485  *	con_tv,((*conn_tp)-con_tv)%0t56/PXDDnXnXnXnXnXnXnXnXnn
1486  */
1487 
1488 #define	NCA_CONN_T_REFINIT	0x10000000	/* CONN_REF init() |ref value */
1489 #define	NCA_CONN_T_REFINIT1	0x11000000	/* CONN_REF init() |ref value */
1490 #define	NCA_CONN_T_REFINIT2	0x12000000	/* CONN_REF init() |ref value */
1491 #define	NCA_CONN_T_REFNOTCP	0x13000000 /* CONN_REF no longer tcp_refed */
1492 #define	NCA_CONN_T_REFHOLD	0x1A000000	/* CONN_REFHOLD() | ref value */
1493 #define	NCA_CONN_T_REFRELE	0x1F000000	/* CONN_REFRELE() | ref value */
1494 
1495 #define	NCA_CONN_T_HTTPCALL	0x20000000	/* call http() | rbytes */
1496 #define	NCA_CONN_T_HTTPRET1	0x21000000	/* return http() */
1497 #define	NCA_CONN_T_HTTPRET2	0x22000000	/* return ! http() */
1498 
1499 #define	NCA_CONN_T_MISSDONE	0x30000000	/* CONN_MISS_DONE */
1500 #define	NCA_CONN_T_TCPTIMER	0x31000000	/* NCA_CONN_TCP_TIMER */
1501 #define	NCA_CONN_T_XMIT_END	0x32000000	/* xmit_end() | tcp_unsent */
1502 #define	NCA_CONN_T_XMIT_BAD	0x33000000 /* xmit_end() bad state |tcp_state */
1503 #define	NCA_CONN_T_XMIT_DEF	0x34000000	/* xmit_end() deferred */
1504 #define	NCA_CONN_T_TIME_WAIT 0x35000000	/* done: tcp_state == TCPS_TIME_WAIT */
1505 #define	NCA_CONN_T_PKT_IN	0x36000000	/* tcp_input() | flags */
1506 #define	NCA_CONN_T_PKT_OUT	0x37000000	/* tcp_input() | flags */
1507 
1508 #define	NCA_CONN_T_DIRECT	0x40000000	/* tcp_direct() from conn_t */
1509 #define	NCA_CONN_T_DIRECT1	0x41000000	/* tcp_direct() to conn_t */
1510 #define	NCA_CONN_T_DIRECT2	0x42000000	/* IF_TCP_DIRECT_TO | TEE */
1511 #define	NCA_CONN_T_DIRECT3	0x43000000	/* IF_TCP_DIRECT_FROM | TEE */
1512 #define	NCA_CONN_T_DIRECT4	0x44000000	/* tcp_close() */
1513 #define	NCA_CONN_T_DIRECT5	0x45000000	/* IF_TCP_DIRECT_CLOSE */
1514 						/* from|tcp_state */
1515 #define	NCA_CONN_T_DIRECT6	0x46000000	/* IF_TCP_DIRECT_CLOSE to */
1516 
1517 #if defined(__i386) || defined(__amd64)
1518 #define	NCA_CONN_T_TRACE_STK() {					\
1519 	_ix = getpcstack(&_p->stk[0], NCA_CONN_T_STK_DEPTH + 1);	\
1520 	if (_ix < NCA_CONN_T_STK_DEPTH + 1) {				\
1521 		_p->stk[_ix + 1] = 0;					\
1522 	}								\
1523 }
1524 #else
1525 #define	NCA_CONN_T_TRACE_STK() {					\
1526 	_p->stk[0] = (pc_t)callee();					\
1527 	_ix = getpcstack(&_p->stk[1], NCA_CONN_T_STK_DEPTH);		\
1528 	if (_ix < NCA_CONN_T_STK_DEPTH) {				\
1529 		_p->stk[_ix + 1] = 0;					\
1530 	}								\
1531 }
1532 #endif
1533 
1534 #define	CON_TV_SZ 4096
1535 
1536 extern struct conn_ts con_tv[CON_TV_SZ];
1537 extern struct conn_ts *conn_tp;
1538 
1539 #define	NCA_CONN_T_TRACE(p, a) {					\
1540 	struct conn_ts *_p;						\
1541 	struct conn_ts *_np;						\
1542 	int    _ix;							\
1543 									\
1544 	do {								\
1545 		_p = conn_tp;					\
1546 		if ((_np = _p + 1) == &con_tv[CON_TV_SZ])	\
1547 			_np = con_tv;				\
1548 	} while (casptr(&conn_tp, _p, _np) != _p);			\
1549 	_p->conn = (p);							\
1550 	_p->action = (a);						\
1551 	_p->ref = (p)->ref;						\
1552 	_p->cpu = CPU->cpu_seqid;					\
1553 	NCA_CONN_T_TRACE_STK();						\
1554 }
1555 
1556 #else	/* NCA_CONN_T_TRACE_ON */
1557 
1558 #define	NCA_CONN_T_TRACE(p, a)
1559 
1560 #endif	/* NCA_CONN_T_TRACE_ON */
1561 
1562 
1563 #define	CONN_REFHOLD(connp) {						\
1564 									\
1565 	NCA_CONN_T_TRACE((connp), NCA_CONN_T_REFHOLD | ((connp)->ref + 1)); \
1566 									\
1567 	if ((connp)->ref <= 0)						\
1568 		panic("nca CONN_REFHOLD: %p has no references",		\
1569 		    (void *)(connp));					\
1570 	(connp)->ref++;							\
1571 }
1572 
1573 #define	CONN_REFRELE(connp) {						\
1574 									\
1575 	NCA_CONN_T_TRACE((connp), NCA_CONN_T_REFRELE | ((connp)->ref - 1)); \
1576 									\
1577 	if ((connp)->tcp_refed) {					\
1578 		if ((connp)->ref == 1)					\
1579 			panic("nca CONN_REFRELE: %p "			\
1580 			    "has only tcp_refed reference",		\
1581 			    (void *)(connp));				\
1582 		if ((connp)->ref < 1)					\
1583 			panic("nca CONN_REFRELE: %p has no references",	\
1584 			    (void *)(connp));				\
1585 	} else {							\
1586 		if ((connp)->ref <= 0)					\
1587 			panic("nca CONN_REFRELE: %p has no references",	\
1588 			    (void *)(connp));				\
1589 	}								\
1590 	(connp)->ref--;							\
1591 	if ((connp)->ref == 0) {					\
1592 		/* Last ref of a nca_conn_t, so free it */		\
1593 		kmutex_t *lock = &(connp)->hashfanout->lock;		\
1594 		mutex_enter(lock);					\
1595 		nca_conn_free(connp);					\
1596 		/* Note: nca_conn_free exits lock */			\
1597 	}								\
1598 }
1599 
1600 /*
1601  * The nca_io2_shadow_t is used by the kernel to contian a copy of a user-
1602  * land nca_io2_t and the the user-land nca_io2_t address and size.
1603  */
1604 
1605 typedef struct nca_io2_shadow_s {
1606 	nca_io2_t	io;		/* copy of user-land nca_io2_t */
1607 	void		*data_ptr;	/* copy of door_arg_t.data_ptr */
1608 	size_t		data_size;	/* copy of door_arg_t.data_size */
1609 } nca_io2_shadow_t;
1610 
1611 #define	SHADOW_NONE	0x00		/* nca_io2_t.shadow NONE */
1612 #define	SHADOW_DOORSRV	0x01		/* nca_io2_t.shadow door_srv() */
1613 #define	SHADOW_NCAFS	0x02		/* nca_io2_t.shadow NCAfs */
1614 
1615 
1616 /*
1617  * Given a ptr to a nca_io2_t, a field and the field_length, write data
1618  * into buffer (Note: word aligned offsets).
1619  */
1620 #define	NCA_IO_WDATA(val, vsize, p, n_used, len, off)		\
1621 	/*CONSTCOND*/						\
1622 	if ((val) == NULL) {					\
1623 		(p)->len = vsize;				\
1624 		(p)->off = 0;					\
1625 	} else {						\
1626 		(p)->len = (vsize);				\
1627 		(p)->off = ((n_used) + sizeof (uint32_t) - 1) &	\
1628 				(~(sizeof (uint32_t) - 1));	\
1629 		bcopy((char *)(val),				\
1630 		    ((char *)(p) + (p)->off), (vsize));		\
1631 		(n_used) = (p)->off + (p)->len;			\
1632 	}
1633 
1634 /*
1635  * Given a ptr to an nca_io2_t, a field length member name, append data to
1636  * it in the buffer. Note: must be the last field a WDATA() was done for.
1637  *
1638  * Note: a NULL NCA_IO_WDATA() can be followed by a NCA_IO_ADATA() only if
1639  *		vsize was == -1.
1640  *
1641  */
1642 #define	NCA_IO_ADATA(val, vsize, p, n_used, len, off)		\
1643 	if ((p)->len == -1) {					\
1644 		(p)->len = 0;					\
1645 		(p)->off = ((n_used) + sizeof (uint32_t) - 1) &	\
1646 		(~(sizeof (uint32_t) - 1));			\
1647 	}							\
1648 	bcopy((char *)(val), ((char *)(p) + \
1649 	    (p)->off + (p)->len), (vsize));			\
1650 	(p)->len += (vsize);					\
1651 	(n_used) += (vsize);
1652 
1653 /*
1654  * Given a ptr to a nca_io2_t and a field construct a pointer.
1655  */
1656 #define	NCA_IO_PDATA(p, off) ((char *)(p) + (p)->off)
1657 
1658 
1659 #ifndef	isdigit
1660 #define	isdigit(c) ((c) >= '0' && (c) <= '9')
1661 #endif
1662 
1663 #ifndef	tolower
1664 #define	tolower(c) ((c) >= 'A' && (c) <= 'Z' ? (c) | 0x20 : (c))
1665 #endif
1666 
1667 #ifndef	isalpha
1668 #define	isalpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
1669 #endif
1670 
1671 #ifndef	isspace
1672 #define	isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || \
1673 		    (c) == '\r' || (c) == '\f' || (c) == '\013')
1674 #endif
1675 
1676 extern char *strnchr(const char *, int, size_t);
1677 extern char *strnstr(const char *, const char *, size_t);
1678 extern char *strncasestr(const char *, const char *, size_t);
1679 extern char *strrncasestr(const char *, const char *, size_t);
1680 extern int atoin(const char *, size_t);
1681 extern int digits(int);
1682 
1683 extern void nca_conn_free(nca_conn_t *);
1684 extern void nca_logit_off(void);
1685 extern void node_fr(node_t *);
1686 
1687 extern nca_squeue_t *nca_squeue_init(nca_squeue_t *, uint32_t,
1688     processorid_t, void (*)(), void *, void (*)(), clock_t, pri_t);
1689 extern void nca_squeue_fini(nca_squeue_t *);
1690 extern void nca_squeue_enter(nca_squeue_t *, mblk_t *, void *);
1691 extern void nca_squeue_fill(nca_squeue_t *, mblk_t *, void *);
1692 extern mblk_t *nca_squeue_remove(nca_squeue_t *);
1693 extern void nca_squeue_worker(nca_squeue_t *);
1694 extern mblk_t *nca_squeue_ctl(mblk_t *, void *, unsigned short);
1695 extern void nca_squeue_signal(nca_squeue_t *);
1696 extern void nca_squeue_exit(nca_squeue_t *);
1697 extern void sqfan_init(sqfan_t *, uint32_t, uint32_t, uint32_t);
1698 extern nca_squeue_t *sqfan_ixinit(sqfan_t *, uint32_t, nca_squeue_t *, uint32_t,
1699     processorid_t, void (*)(), void *, void (*)(), clock_t, pri_t);
1700 extern void sqfan_fini(sqfan_t *);
1701 extern void sqfan_fill(sqfan_t *, mblk_t *, void *);
1702 extern mblk_t *sqfan_remove(sqfan_t *);
1703 extern void nca_squeue_nointr(nca_squeue_t *, mblk_t *, void *, int);
1704 extern void nca_squeue_pause(nca_squeue_t *, mblk_t *, void *, int, boolean_t);
1705 extern void nca_squeue_willproxy(nca_squeue_t *);
1706 extern void nca_squeue_proxy(nca_squeue_t *, nca_squeue_t *);
1707 extern void nca_squeue_bind(nca_squeue_t *, uint32_t, processorid_t);
1708 
1709 extern int nca_tcp_clean_death(nca_conn_t *, int);
1710 extern nca_conn_t *nca_tcp_connect(ipaddr_t, in_port_t, boolean_t);
1711 extern void nca_tcp_send(nca_conn_t *, mblk_t *);
1712 extern void nca_tcp_direct(nca_conn_t *, nca_conn_t *, uint32_t);
1713 
1714 /* Functions prototypes from ncadoorsrv.c */
1715 extern node_t *nca_node_flush(node_t *);
1716 extern void nca_downcall_service(void *, door_arg_t *, void (**)(void *,
1717     void *), void **, int *);
1718 extern node_t *ctag_lookup(uint64_t, unsigned *);
1719 extern node_t *node_replace(node_t *, nca_conn_t *);
1720 extern node_t *node_temp(node_t *, nca_conn_t *);
1721 extern void find_ctags(node_t *, nca_io2_t *, int *);
1722 extern void nca_ncafs_srv(nca_io2_t *, struct uio *, queue_t *);
1723 extern boolean_t nca_reclaim_vlru(void);
1724 extern boolean_t nca_reclaim_plru(boolean_t, boolean_t);
1725 
1726 /*
1727  * NCA_COUNTER() is used to add a signed long value to a unsigned long
1728  * counter, in general these counters are used to maintain NCA state.
1729  *
1730  * NCA_DEBUG_COUNTER() is used like NCA_COUNTER() but for counters used
1731  * to maintain additional debug state, by default these counters aren't
1732  * updated unless the global value nca_debug_counter is set to a value
1733  * other then zero.
1734  *
1735  * Also, if NCA_COUNTER_TRACE is defined a time ordered wrapping trace
1736  * buffer is maintained with hrtime_t stamps, counter address, value to
1737  * add, and new value entries for all NCA_COUNTER() and NCA_DEBUG_COUNTER()
1738  * use.
1739  */
1740 
1741 #undef	NCA_COUNTER_TRACE
1742 
1743 #ifdef	NCA_COUNTER_TRACE
1744 
1745 #define	NCA_COUNTER_TRACE_SZ	1024
1746 
1747 typedef struct nca_counter_s {
1748 	hrtime_t	t;
1749 	unsigned long	*p;
1750 	unsigned long	v;
1751 	unsigned long	nv;
1752 } nca_counter_t;
1753 
1754 extern nca_counter_t nca_counter_tv[];
1755 extern nca_counter_t *nca_counter_tp;
1756 
1757 #define	NCA_COUNTER(_p, _v) {						\
1758 	unsigned long	*p = _p;					\
1759 	long		v = _v;						\
1760 	unsigned long	_nv;						\
1761 	nca_counter_t	*_otp;						\
1762 	nca_counter_t	*_ntp;						\
1763 									\
1764 	_nv = atomic_add_long_nv(p, v);					\
1765 	do {								\
1766 		_otp = nca_counter_tp;					\
1767 		_ntp = _otp + 1;					\
1768 		if (_ntp == &nca_counter_tv[NCA_COUNTER_TRACE_SZ])	\
1769 			_ntp = nca_counter_tv;				\
1770 	} while (casptr((void *)&nca_counter_tp, (void *)_otp,		\
1771 	    (void *)_ntp) != (void *)_otp);				\
1772 	_ntp->t = gethrtime();						\
1773 	_ntp->p = p;							\
1774 	_ntp->v = v;							\
1775 	_ntp->nv = _nv;							\
1776 }
1777 
1778 #else	/* NCA_COUNTER_TRACE */
1779 
1780 #define	NCA_COUNTER(p, v) atomic_add_long((p), (v))
1781 
1782 #endif	/* NCA_COUNTER_TRACE */
1783 
1784 
1785 /*
1786  * This is the buf used in upcall to httpd.
1787  */
1788 typedef struct {
1789 	uintptr_t	tid;
1790 	char		*buf;
1791 } http_buf_table_t;
1792 
1793 /*
1794  * URI and filename hash, a simple static hash bucket array of singly
1795  * linked grounded lists is used with a hashing algorithm which has
1796  * proven to have good distribution properities for strings of ...
1797  *
1798  * Note: NCA_HASH_SZ must be a prime number.
1799  */
1800 
1801 #define	NCA_HASH_SZ	8053
1802 #define	NCA_HASH_MASK	0xFFFFFF
1803 #define	HASH_IX(s, l, hix, hsz) { \
1804 	char *cp = (s); \
1805 	int len = (l); \
1806 			\
1807 	(hix) = 0; \
1808 	while (len-- > 0) { \
1809 		(hix) = (hix) * 33 + *cp++; \
1810 		(hix) &= NCA_HASH_MASK; \
1811 	} \
1812 	(hix) %= (hsz); \
1813 }
1814 
1815 /*
1816  * CTAG hash.
1817  */
1818 #define	NCA_CTAGHASH_SZ	4096
1819 #define	CTAGHASH_IX(t, ix) ((ix) = (t) % NCA_CTAGHASH_SZ)
1820 
1821 /*
1822  * VNODE hash.
1823  *
1824  * Note: NCA_VNODEHASH_SZ must be a P2Ps() value.
1825  */
1826 #define	NCA_VNODEHASH_SZ 12281
1827 #define	VNODEHASH_IX(p, ix) ((ix) = (((uintptr_t)p >> 27) ^ \
1828 	((uintptr_t)p >> 17) ^ ((uintptr_t)p >> 11) ^ (uintptr_t)p) % \
1829 	ncavnodehash_sz)
1830 
1831 extern pgcnt_t nca_ppmax;
1832 extern pgcnt_t nca_vpmax;
1833 extern pgcnt_t nca_pplim;
1834 extern pgcnt_t nca_vplim;
1835 extern pgcnt_t nca_ppmem;
1836 extern pgcnt_t nca_vpmem;
1837 extern ssize_t nca_kbmem;
1838 extern ssize_t nca_spmem;
1839 extern ssize_t nca_ckmem;
1840 extern ssize_t nca_mbmem;
1841 extern ssize_t nca_cbmem;
1842 extern ssize_t nca_lbmem;
1843 extern size_t  nca_maxkmem;
1844 extern uint32_t nca_use_segmap;
1845 
1846 extern ulong_t nca_hits;
1847 extern ulong_t nca_file;
1848 extern ulong_t nca_ctag;
1849 extern ulong_t nca_miss;
1850 
1851 extern ulong_t nca_hit304;
1852 extern ulong_t nca_hitnoV;
1853 extern ulong_t nca_hitnoVfast;
1854 extern ulong_t nca_hitnoVtemp;
1855 
1856 extern ulong_t nca_filehits;
1857 extern ulong_t nca_filenoV;
1858 extern ulong_t nca_filenoVfast;
1859 extern ulong_t nca_filemiss;
1860 
1861 extern ulong_t nca_missURI;
1862 extern ulong_t nca_missQ;
1863 extern ulong_t nca_missSAFE;
1864 extern ulong_t nca_missnoV;
1865 extern ulong_t nca_missnotcp;
1866 extern ulong_t nca_missfail;
1867 extern ulong_t nca_misstemp;
1868 extern ulong_t nca_missnohash;
1869 extern ulong_t nca_missclean;
1870 extern ulong_t nca_missadvisory;
1871 extern ulong_t nca_missadvNoA;
1872 extern ulong_t nca_missERROR;
1873 
1874 extern ulong_t nca_ERROR;
1875 extern ulong_t nca_flushnode;
1876 extern ulong_t nca_replacenode;
1877 extern ulong_t nca_tempnode;
1878 
1879 extern ulong_t nca_fail304;
1880 
1881 extern ulong_t nca_nocache1;
1882 extern ulong_t nca_nocache2;
1883 extern ulong_t nca_nocache3;
1884 extern ulong_t nca_nocache4;
1885 extern ulong_t nca_nocache5;
1886 extern ulong_t nca_nocache6;
1887 extern ulong_t nca_nocache6nomp;
1888 extern ulong_t nca_nocache7;
1889 extern ulong_t nca_nocache8;
1890 extern ulong_t nca_nocache9;
1891 extern ulong_t nca_nocache10;
1892 extern ulong_t nca_nocache11;
1893 extern ulong_t nca_nocache12;
1894 extern ulong_t nca_nocache13;
1895 extern ulong_t nca_nocache14;
1896 extern ulong_t nca_nocache15;
1897 extern ulong_t nca_nodes;
1898 extern ulong_t nca_desballoc;
1899 
1900 extern ulong_t nca_plrucnt;
1901 extern ulong_t nca_vlrucnt;
1902 extern ulong_t nca_rpcall;
1903 extern ulong_t nca_rvcall;
1904 extern ulong_t nca_rpbusy;
1905 extern ulong_t nca_rvbusy;
1906 extern ulong_t nca_rpfail;
1907 extern ulong_t nca_rpempty;
1908 extern ulong_t nca_rvempty;
1909 extern ulong_t nca_rpdone;
1910 extern ulong_t nca_rvdone;
1911 extern ulong_t nca_rmdone;
1912 extern ulong_t nca_rkdone;
1913 extern ulong_t nca_rsdone;
1914 extern ulong_t nca_rndone;
1915 extern ulong_t nca_rpnone;
1916 extern ulong_t nca_rvnone;
1917 extern ulong_t nca_rmnone;
1918 extern ulong_t nca_rknone;
1919 extern ulong_t nca_rsnone;
1920 extern ulong_t nca_rnh;
1921 extern ulong_t nca_ref[];
1922 extern ulong_t nca_vmap_rpcall;
1923 
1924 extern ulong_t nca_node_kmem_fail1;
1925 extern ulong_t nca_node_kmem_fail2;
1926 
1927 extern ulong_t doorsrv_nopreempt;
1928 extern ulong_t doorsrv_badconnect;
1929 extern ulong_t doorsrv_invaladvise;
1930 extern ulong_t doorsrv_notupcall;
1931 extern ulong_t doorsrv_badadvise;
1932 extern ulong_t doorsrv_cksum;
1933 extern ulong_t doorsrv_error;
1934 extern ulong_t doorsrv_op;
1935 extern ulong_t doorsrv_badtee;
1936 extern ulong_t doorsrv_badio;
1937 extern ulong_t doorsrv_sz;
1938 
1939 extern ulong_t nca_allocfail;
1940 extern ulong_t nca_mapinfail;
1941 extern ulong_t nca_mapinfail1;
1942 extern ulong_t nca_mapinfail2;
1943 extern ulong_t nca_mapinfail3;
1944 
1945 extern ulong_t nca_httpd_http;
1946 extern ulong_t nca_httpd_badsz;
1947 extern ulong_t nca_httpd_nosz;
1948 extern ulong_t nca_httpd_filename;
1949 extern ulong_t nca_httpd_filename1;
1950 extern ulong_t nca_httpd_filename2;
1951 extern ulong_t nca_httpd_trailer;
1952 extern ulong_t nca_httpd_preempt;
1953 extern ulong_t nca_httpd_downcall;
1954 extern ulong_t nca_early_downcall;
1955 extern ulong_t nca_httpd_more;
1956 
1957 ulong_t nca_logit_noupcall;
1958 
1959 ulong_t nca_logit;
1960 ulong_t nca_logit_nomp;
1961 ulong_t nca_logit_no;
1962 ulong_t nca_logit_NULL;
1963 ulong_t nca_logit_fail;
1964 
1965 ulong_t nca_logit_flush_NULL1;
1966 ulong_t nca_logit_flush_NULL2;
1967 
1968 ulong_t nca_logger_NULL1;
1969 ulong_t nca_logger_NULL2;
1970 
1971 ulong_t nca_log_buf_alloc_NULL;
1972 ulong_t nca_log_buf_alloc_fail;
1973 ulong_t nca_log_buf_alloc_part;
1974 
1975 ulong_t nca_log_buf_dup;
1976 
1977 extern ulong_t nca_upcalls;
1978 extern ulong_t nca_ncafs_upcalls;
1979 
1980 extern ulong_t nca_conn_count;
1981 extern ulong_t nca_conn_kmem;
1982 extern ulong_t nca_conn_kmem_fail;
1983 extern ulong_t nca_conn_allocb_fail;
1984 extern ulong_t nca_conn_tw;
1985 extern ulong_t nca_conn_tw1;
1986 extern ulong_t nca_conn_tw2;
1987 extern ulong_t nca_conn_reinit_cnt;
1988 extern ulong_t nca_conn_NULL1;
1989 extern ulong_t nca_conn_Q0;
1990 extern ulong_t nca_conn_FLAGS;
1991 
1992 extern ulong_t tcpwronginq;
1993 extern ulong_t ipsendup;
1994 extern ulong_t ipwrongcpu;
1995 extern ulong_t iponcpu;
1996 
1997 extern ulong_t nca_tcp_xmit_null;
1998 extern ulong_t nca_tcp_xmit_null1;
1999 
2000 extern ulong_t tw_on;
2001 extern ulong_t tw_fire;
2002 extern ulong_t tw_fire1;
2003 extern ulong_t tw_fire2;
2004 extern ulong_t tw_fire3;
2005 extern ulong_t tw_add;
2006 extern ulong_t tw_add1;
2007 extern ulong_t tw_delete;
2008 extern ulong_t tw_reclaim;
2009 extern ulong_t tw_reap;
2010 extern ulong_t tw_reap1;
2011 extern ulong_t tw_reap2;
2012 extern ulong_t tw_reap3;
2013 extern ulong_t tw_reap4;
2014 extern ulong_t tw_reap5;
2015 extern ulong_t tw_timer;
2016 extern ulong_t tw_timer1;
2017 extern ulong_t tw_timer2;
2018 extern ulong_t tw_timer3;
2019 extern ulong_t tw_timer4;
2020 extern ulong_t tw_timer5;
2021 
2022 extern ulong_t ti_on;
2023 extern ulong_t ti_fire;
2024 extern ulong_t ti_fire1;
2025 extern ulong_t ti_fire2;
2026 extern ulong_t ti_fire3;
2027 extern ulong_t ti_fire4;
2028 extern ulong_t ti_add;
2029 extern ulong_t ti_add1;
2030 extern ulong_t ti_add2;
2031 extern ulong_t ti_add3;
2032 extern ulong_t ti_add4;
2033 extern ulong_t ti_add5;
2034 extern ulong_t ti_add_reuse;
2035 extern ulong_t ti_delete;
2036 extern ulong_t ti_delete1;
2037 extern ulong_t ti_delete2;
2038 extern ulong_t ti_reap;
2039 extern ulong_t ti_reap1;
2040 extern ulong_t ti_reap2;
2041 extern ulong_t ti_reap3;
2042 extern ulong_t ti_reap4;
2043 extern ulong_t ti_reap5;
2044 extern ulong_t ti_timer;
2045 extern ulong_t ti_timer1;
2046 extern ulong_t ti_timer2;
2047 extern ulong_t ti_timer3;
2048 extern ulong_t ti_timer4;
2049 extern ulong_t ti_timer5;
2050 extern ulong_t ti_timer6;
2051 
2052 extern uint32_t nca_conn_q;
2053 extern uint32_t nca_conn_q0;
2054 extern uint32_t nca_conn_req_max_q;
2055 extern uint32_t nca_conn_req_max_q0;
2056 
2057 extern char nca_resp_500[];
2058 extern ssize_t nca_resp_500_sz;
2059 
2060 extern uint32_t ncaurihash_sz;
2061 extern uint32_t ncafilehash_sz;
2062 extern uint32_t ncactaghash_sz;
2063 extern uint32_t ncavnodehash_sz;
2064 extern nodef_t *ncaurihash;
2065 extern nodef_t *ncafilehash;
2066 extern nodef_t *ncavnodehash;
2067 extern nodef_t *ncactaghash;
2068 extern char nca_httpd_door_path[];
2069 extern char nca_httpd_downdoor_path[];
2070 extern door_handle_t nca_downcall_door_hand;
2071 extern uint32_t n_http_buf_size;
2072 extern door_handle_t nca_httpd_door_hand;
2073 extern sqfan_t nca_miss_fanout1;
2074 extern sqfan_t nca_miss_fanout2;
2075 extern nca_door_t nca_httpd_door;
2076 extern int nca_downdoor_created;
2077 extern int n_http_buf_table;
2078 extern http_buf_table_t *g_http_buf_table;
2079 extern struct kmem_cache *node_cache;
2080 #ifdef DEBUG
2081 extern node_t *nca_http_response(nca_conn_t *, const char *, int, char *, int,
2082 		    uint_t, const char *);
2083 extern node_t *nca_http_response_node(nca_conn_t *, const char *, int, node_t *,
2084 		    const char *);
2085 #else
2086 extern node_t *nca_http_response(nca_conn_t *, const char *, int, char *, int,
2087 		    uint_t);
2088 extern node_t *nca_http_response_node(nca_conn_t *, const char *, int,
2089     node_t *);
2090 #endif
2091 extern void nca_node_del(node_t *);
2092 extern void nca_node_uncache(node_t *);
2093 extern node_t *nca_node_add(char *, int, nodef_t *, int);
2094 extern node_t *node_create(int, boolean_t, char *, int);
2095 extern void nca_reclaim_phys(node_t *, boolean_t, boolean_t);
2096 extern boolean_t nca_http_pmap(node_t *);
2097 extern boolean_t nca_http_vmap(node_t *, int);
2098 extern time_t nca_http_date(char *);
2099 extern node_t *nca_httpd_data(node_t *, nca_conn_t *, nca_io2_t *, int);
2100 extern void nca_missed(node_t *, mblk_t *, nca_squeue_t *);
2101 extern void nca_miss_conn_mv(node_t *, nca_conn_t *);
2102 extern void nca_miss_conn_fr(node_t *, nca_conn_t *);
2103 extern void nca_http_logit(nca_conn_t *);
2104 extern void nca_http_error(nca_conn_t *);
2105 extern void nca_node_xmit(node_t *, nca_conn_t *);
2106 
2107 /*
2108  * It contains data for forwarding data to application programs.
2109  * For door case, doorhandle is the upcall door handle and listenerq
2110  * is NULL; for ncafs, listenerq is the upcall listener queue and
2111  * doorhandle is NULL. listenning is always B_TRUE for door and it is
2112  * B_TRUE for ncafs only after the listen system call has been issued.
2113  */
2114 typedef struct nca_listener_s {
2115 	boolean_t	listenning;	/* is ready for accepting connection */
2116 	door_handle_t	doorhandle;	/* door handle or NULL for ncafs */
2117 	queue_t		*listenerq;	/* upcall queue or NULL for door */
2118 } nca_listener_t;
2119 
2120 /*
2121  * Returned values of nca_isnca_data.
2122  * NOT_NCA_DATA:	not NCA data.
2123  * NCA_DATA_ANY_ADDR:	NCA data, matches INADDR_ANY.
2124  * NCA_DATA_ADDR:	NCA data, match an IP address.
2125  */
2126 #define	NOT_NCA_DATA		0
2127 #define	NCA_DATA_ANY_ADDR	1
2128 #define	NCA_DATA_ADDR		2
2129 
2130 extern uint32_t ipportrehashcount1;
2131 extern uint32_t ipportrehashcount2;
2132 extern uint32_t ipportbucketcnt;
2133 extern uint32_t ipporttablesize;
2134 extern uint32_t ncafscount;
2135 extern uint32_t doorcount;
2136 extern int	ip_virtual_hosting;
2137 
2138 extern nca_listener_t *nca_listener_find(ipaddr_t, uint16_t);
2139 extern nca_listener_t *nca_listener_find2(ipaddr_t, uint16_t);
2140 extern int		nca_isnca_data(ipaddr_t, uint16_t);
2141 extern int		nca_listener_add(ipaddr_t, uint16_t, void *, boolean_t);
2142 extern int		nca_listener_del(ipaddr_t, uint16_t);
2143 extern void		nca_listener_report(mblk_t *);
2144 
2145 #ifdef	__cplusplus
2146 }
2147 #endif
2148 
2149 #endif	/* _INET_NCA_H */
2150