xref: /titanic_51/usr/src/uts/common/fs/sockfs/nl7curi.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52c9e429eSbrutus  * Common Development and Distribution License (the "License").
62c9e429eSbrutus  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b73114cbSAnil udupa  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
267c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
307c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
317c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
327c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
337c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
347c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7curi.h>
350f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h>
367c478bd9Sstevel@tonic-gate 
372c9e429eSbrutus #include <inet/common.h>
382c9e429eSbrutus #include <inet/ip.h>
392c9e429eSbrutus #include <inet/ip6.h>
402c9e429eSbrutus #include <inet/tcp.h>
412c9e429eSbrutus #include <inet/led.h>
422c9e429eSbrutus #include <inet/mi.h>
432c9e429eSbrutus 
442c9e429eSbrutus #include <inet/nca/ncadoorhdr.h>
452c9e429eSbrutus #include <inet/nca/ncalogd.h>
462c9e429eSbrutus #include <inet/nca/ncandd.h>
472c9e429eSbrutus 
482c9e429eSbrutus #include <sys/promif.h>
492c9e429eSbrutus 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Some externs:
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate extern boolean_t	nl7c_logd_enabled;
552c9e429eSbrutus extern void		nl7c_logd_log(uri_desc_t *, uri_desc_t *,
562c9e429eSbrutus 			    time_t, ipaddr_t);
572c9e429eSbrutus extern boolean_t	nl7c_close_addr(struct sonode *);
582c9e429eSbrutus extern struct sonode	*nl7c_addr2portso(void *);
592c9e429eSbrutus extern uri_desc_t	*nl7c_http_cond(uri_desc_t *, uri_desc_t *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Various global tuneables:
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate clock_t		nl7c_uri_ttl = -1;	/* TTL in seconds (-1 == infinite) */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate boolean_t	nl7c_use_kmem = B_FALSE; /* Force use of kmem (no segmap) */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate uint64_t	nl7c_file_prefetch = 1; /* File cache prefetch pages */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate uint64_t	nl7c_uri_max = 0;	/* Maximum bytes (0 == infinite) */
727c478bd9Sstevel@tonic-gate uint64_t	nl7c_uri_bytes = 0;	/* Bytes of kmem used by URIs */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
752c9e429eSbrutus  * Locals:
762c9e429eSbrutus  */
772c9e429eSbrutus 
782c9e429eSbrutus static int	uri_rd_response(struct sonode *, uri_desc_t *,
792c9e429eSbrutus 		    uri_rd_t *, boolean_t);
802c9e429eSbrutus static int	uri_response(struct sonode *, uri_desc_t *);
812c9e429eSbrutus 
822c9e429eSbrutus /*
832c9e429eSbrutus  * HTTP scheme functions called from nl7chttp.c:
842c9e429eSbrutus  */
852c9e429eSbrutus 
862c9e429eSbrutus boolean_t nl7c_http_request(char **, char *, uri_desc_t *, struct sonode *);
872c9e429eSbrutus boolean_t nl7c_http_response(char **, char *, uri_desc_t *, struct sonode *);
882c9e429eSbrutus boolean_t nl7c_http_cmp(void *, void *);
892c9e429eSbrutus mblk_t *nl7c_http_persist(struct sonode *);
902c9e429eSbrutus void nl7c_http_free(void *arg);
912c9e429eSbrutus void nl7c_http_init(void);
922c9e429eSbrutus 
932c9e429eSbrutus /*
947c478bd9Sstevel@tonic-gate  * Counters that need to move to kstat and/or be removed:
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_request = 0;
987c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_hit = 0;
997c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass = 0;
1007c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_miss = 0;
1017c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp = 0;
1027c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more = 0;
1037c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_data = 0;
1047c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_sendfilev = 0;
1057c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_reclaim_calls = 0;
1067c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_reclaim_cnt = 0;
1077c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_urifail = 0;
1087c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_dupbfail = 0;
1097c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_get = 0;
1102c9e429eSbrutus volatile uint64_t nl7c_uri_pass_method = 0;
1117c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_option = 0;
1127c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_eol = 0;
1137c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_http = 0;
1147c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_http = 0;
1157c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_addfail = 0;
1167c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_temp = 0;
1177c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_expire = 0;
1182c9e429eSbrutus volatile uint64_t nl7c_uri_purge = 0;
1197c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_NULL1 = 0;
1207c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_NULL2 = 0;
1217c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_close = 0;
1227c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp_close = 0;
1237c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_free = 0;
1247c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp_free = 0;
1252c9e429eSbrutus volatile uint64_t nl7c_uri_temp_mk = 0;
1262c9e429eSbrutus volatile uint64_t nl7c_uri_rd_EAGAIN = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Various kmem_cache_t's:
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate 
1322c9e429eSbrutus kmem_cache_t *nl7c_uri_kmc;
1332c9e429eSbrutus kmem_cache_t *nl7c_uri_rd_kmc;
1347c478bd9Sstevel@tonic-gate static kmem_cache_t *uri_desb_kmc;
1357c478bd9Sstevel@tonic-gate static kmem_cache_t *uri_segmap_kmc;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void uri_kmc_reclaim(void *);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void nl7c_uri_reclaim(void);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * The URI hash is a dynamically sized A/B bucket hash, when the current
1437c478bd9Sstevel@tonic-gate  * hash's average bucket chain length exceeds URI_HASH_AVRG a new hash of
1447c478bd9Sstevel@tonic-gate  * the next P2Ps[] size is created.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * All lookups are done in the current hash then the new hash (if any),
1477c478bd9Sstevel@tonic-gate  * if there is a new has then when a current hash bucket chain is examined
1487c478bd9Sstevel@tonic-gate  * any uri_desc_t members will be migrated to the new hash and when the
1497c478bd9Sstevel@tonic-gate  * last uri_desc_t has been migrated then the new hash will become the
1507c478bd9Sstevel@tonic-gate  * current and the previous current hash will be freed leaving a single
1517c478bd9Sstevel@tonic-gate  * hash.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * uri_hash_t - hash bucket (chain) type, contained in the uri_hash_ab[]
1547c478bd9Sstevel@tonic-gate  * and can be accessed only after aquiring the uri_hash_access lock (for
1557c478bd9Sstevel@tonic-gate  * READER or WRITER) then acquiring the lock uri_hash_t.lock, the uri_hash_t
1567c478bd9Sstevel@tonic-gate  * and all linked uri_desc_t.hash members are protected. Note, a REF_HOLD()
1577c478bd9Sstevel@tonic-gate  * is placed on all uri_desc_t uri_hash_t list members.
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * uri_hash_access - rwlock for all uri_hash_* variables, READER for read
1607c478bd9Sstevel@tonic-gate  * access and WRITER for write access. Note, WRITER is only required for
1617c478bd9Sstevel@tonic-gate  * hash geometry changes.
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * uri_hash_which - which uri_hash_ab[] is the current hash.
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  * uri_hash_n[] - the P2Ps[] index for each uri_hash_ab[].
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * uri_hash_sz[] - the size for each uri_hash_ab[].
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * uri_hash_cnt[] - the total uri_desc_t members for each uri_hash_ab[].
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * uri_hash_overflow[] - the uri_hash_cnt[] for each uri_hash_ab[] when
1727c478bd9Sstevel@tonic-gate  * a new uri_hash_ab[] needs to be created.
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * uri_hash_ab[] - the uri_hash_t entries.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * uri_hash_lru[] - the last uri_hash_ab[] walked for lru reclaim.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate typedef struct uri_hash_s {
1807c478bd9Sstevel@tonic-gate 	struct uri_desc_s	*list;		/* List of uri_t(s) */
1817c478bd9Sstevel@tonic-gate 	kmutex_t		lock;
1827c478bd9Sstevel@tonic-gate } uri_hash_t;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate #define	URI_HASH_AVRG	5	/* Desired average hash chain length */
1857c478bd9Sstevel@tonic-gate #define	URI_HASH_N_INIT	9	/* P2Ps[] initial index */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static krwlock_t	uri_hash_access;
1887c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_which = 0;
1897c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_n[2] = {URI_HASH_N_INIT, 0};
1907c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_sz[2] = {0, 0};
1917c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_cnt[2] = {0, 0};
1927c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_overflow[2] = {0, 0};
1937c478bd9Sstevel@tonic-gate static uri_hash_t	*uri_hash_ab[2] = {NULL, NULL};
1947c478bd9Sstevel@tonic-gate static uri_hash_t	*uri_hash_lru[2] = {NULL, NULL};
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Primes for N of 3 - 24 where P is first prime less then (2^(N-1))+(2^(N-2))
1987c478bd9Sstevel@tonic-gate  * these primes have been foud to be useful for prime sized hash tables.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static const int P2Ps[] = {
2027c478bd9Sstevel@tonic-gate 	0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,
2037c478bd9Sstevel@tonic-gate 	6143, 12281, 24571, 49139, 98299, 196597, 393209,
2047c478bd9Sstevel@tonic-gate 	786431, 1572853, 3145721, 6291449, 12582893, 0};
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Hash macros:
2087c478bd9Sstevel@tonic-gate  *
2097c478bd9Sstevel@tonic-gate  *    H2A(char *cp, char *ep, char c) - convert the escaped octet (ASCII)
2107c478bd9Sstevel@tonic-gate  *    hex multichar of the format "%HH" pointeded to by *cp to a char and
2117c478bd9Sstevel@tonic-gate  *    return in c, *ep points to past end of (char *), on return *cp will
2127c478bd9Sstevel@tonic-gate  *    point to the last char consumed.
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  *    URI_HASH(unsigned hix, char *cp, char *ep) - hash the char(s) from
2157c478bd9Sstevel@tonic-gate  *    *cp to *ep to the unsigned hix, cp nor ep are modified.
2167c478bd9Sstevel@tonic-gate  *
2172c9e429eSbrutus  *    URI_HASH_IX(unsigned hix, int which) - convert the hash value hix to
2182c9e429eSbrutus  *    a hash index 0 - (uri_hash_sz[which] - 1).
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  *    URI_HASH_MIGRATE(from, hp, to) - migrate the uri_hash_t *hp list
2217c478bd9Sstevel@tonic-gate  *    uri_desc_t members from hash from to hash to.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  *    URI_HASH_UNLINK(cur, new, hp, puri, uri) - unlink the uri_desc_t
2247c478bd9Sstevel@tonic-gate  *    *uri which is a member of the uri_hash_t *hp list with a previous
2257c478bd9Sstevel@tonic-gate  *    list member of *puri for the uri_hash_ab[] cur. After unlinking
2267c478bd9Sstevel@tonic-gate  *    check for cur hash empty, if so make new cur. Note, as this macro
2277c478bd9Sstevel@tonic-gate  *    can change a hash chain it needs to be run under hash_access as
2287c478bd9Sstevel@tonic-gate  *    RW_WRITER, futher as it can change the new hash to cur any access
2297c478bd9Sstevel@tonic-gate  *    to the hash state must be done after either dropping locks and
2307c478bd9Sstevel@tonic-gate  *    starting over or making sure the global state is consistent after
2317c478bd9Sstevel@tonic-gate  *    as before.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #define	H2A(cp, ep, c) {						\
2357c478bd9Sstevel@tonic-gate 	int	_h = 2;							\
2367c478bd9Sstevel@tonic-gate 	int	_n = 0;							\
2377c478bd9Sstevel@tonic-gate 	char	_hc;							\
2387c478bd9Sstevel@tonic-gate 									\
2397c478bd9Sstevel@tonic-gate 	while (_h > 0 && ++(cp) < (ep)) {				\
2407c478bd9Sstevel@tonic-gate 		if (_h == 1)						\
2417c478bd9Sstevel@tonic-gate 			_n *= 0x10;					\
2427c478bd9Sstevel@tonic-gate 		_hc = *(cp);						\
2437c478bd9Sstevel@tonic-gate 		if (_hc >= '0' && _hc <= '9')				\
2447c478bd9Sstevel@tonic-gate 			_n += _hc - '0';				\
2457c478bd9Sstevel@tonic-gate 		else if (_hc >= 'a' || _hc <= 'f')			\
2467c478bd9Sstevel@tonic-gate 			_n += _hc - 'W';				\
2477c478bd9Sstevel@tonic-gate 		else if (_hc >= 'A' || _hc <= 'F')			\
2487c478bd9Sstevel@tonic-gate 			_n += _hc - '7';				\
2497c478bd9Sstevel@tonic-gate 		_h--;							\
2507c478bd9Sstevel@tonic-gate 	}								\
2517c478bd9Sstevel@tonic-gate 	(c) = _n;							\
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #define	URI_HASH(hv, cp, ep) {						\
2557c478bd9Sstevel@tonic-gate 	char	*_s = (cp);						\
2567c478bd9Sstevel@tonic-gate 	char	_c;							\
2577c478bd9Sstevel@tonic-gate 									\
2587c478bd9Sstevel@tonic-gate 	while (_s < (ep)) {						\
2597c478bd9Sstevel@tonic-gate 		if ((_c = *_s) == '%') {				\
2607c478bd9Sstevel@tonic-gate 			H2A(_s, (ep), _c);				\
2617c478bd9Sstevel@tonic-gate 		}							\
2622c9e429eSbrutus 		CHASH(hv, _c);						\
2637c478bd9Sstevel@tonic-gate 		_s++;							\
2647c478bd9Sstevel@tonic-gate 	}								\
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2672c9e429eSbrutus #define	URI_HASH_IX(hix, which) (hix) = (hix) % (uri_hash_sz[(which)])
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate #define	URI_HASH_MIGRATE(from, hp, to) {				\
2707c478bd9Sstevel@tonic-gate 	uri_desc_t	*_nuri;						\
2717c478bd9Sstevel@tonic-gate 	uint32_t	_nhix;						\
2727c478bd9Sstevel@tonic-gate 	uri_hash_t	*_nhp;						\
2737c478bd9Sstevel@tonic-gate 									\
2747c478bd9Sstevel@tonic-gate 	mutex_enter(&(hp)->lock);					\
2757c478bd9Sstevel@tonic-gate 	while ((_nuri = (hp)->list) != NULL) {				\
2767c478bd9Sstevel@tonic-gate 		(hp)->list = _nuri->hash;				\
277*1a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&uri_hash_cnt[(from)]);		\
278*1a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&uri_hash_cnt[(to)]);			\
2792c9e429eSbrutus 		_nhix = _nuri->hvalue;					\
2802c9e429eSbrutus 		URI_HASH_IX(_nhix, to);					\
2817c478bd9Sstevel@tonic-gate 		_nhp = &uri_hash_ab[(to)][_nhix];			\
2827c478bd9Sstevel@tonic-gate 		mutex_enter(&_nhp->lock);				\
2837c478bd9Sstevel@tonic-gate 		_nuri->hash = _nhp->list;				\
2847c478bd9Sstevel@tonic-gate 		_nhp->list = _nuri;					\
2857c478bd9Sstevel@tonic-gate 		_nuri->hit = 0;						\
2867c478bd9Sstevel@tonic-gate 		mutex_exit(&_nhp->lock);				\
2877c478bd9Sstevel@tonic-gate 	}								\
2887c478bd9Sstevel@tonic-gate 	mutex_exit(&(hp)->lock);					\
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate #define	URI_HASH_UNLINK(cur, new, hp, puri, uri) {			\
2927c478bd9Sstevel@tonic-gate 	if ((puri) != NULL) {						\
2937c478bd9Sstevel@tonic-gate 		(puri)->hash = (uri)->hash;				\
2947c478bd9Sstevel@tonic-gate 	} else {							\
2957c478bd9Sstevel@tonic-gate 		(hp)->list = (uri)->hash;				\
2967c478bd9Sstevel@tonic-gate 	}								\
297*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&uri_hash_cnt[(cur)]) == 0 &&		\
2987c478bd9Sstevel@tonic-gate 	    uri_hash_ab[(new)] != NULL) {				\
2997c478bd9Sstevel@tonic-gate 		kmem_free(uri_hash_ab[cur],				\
3007c478bd9Sstevel@tonic-gate 		    sizeof (uri_hash_t) * uri_hash_sz[cur]);		\
3017c478bd9Sstevel@tonic-gate 		uri_hash_ab[(cur)] = NULL;				\
3027c478bd9Sstevel@tonic-gate 		uri_hash_lru[(cur)] = NULL;				\
3037c478bd9Sstevel@tonic-gate 		uri_hash_which = (new);					\
3047c478bd9Sstevel@tonic-gate 	} else {							\
3057c478bd9Sstevel@tonic-gate 		uri_hash_lru[(cur)] = (hp);				\
3067c478bd9Sstevel@tonic-gate 	}								\
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate void
3107c478bd9Sstevel@tonic-gate nl7c_uri_init(void)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	uint32_t	cur = uri_hash_which;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	rw_init(&uri_hash_access, NULL, RW_DEFAULT, NULL);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	uri_hash_sz[cur] = P2Ps[URI_HASH_N_INIT];
3177c478bd9Sstevel@tonic-gate 	uri_hash_overflow[cur] = P2Ps[URI_HASH_N_INIT] * URI_HASH_AVRG;
3187c478bd9Sstevel@tonic-gate 	uri_hash_ab[cur] = kmem_zalloc(sizeof (uri_hash_t) * uri_hash_sz[cur],
3197c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
3207c478bd9Sstevel@tonic-gate 	uri_hash_lru[cur] = uri_hash_ab[cur];
3217c478bd9Sstevel@tonic-gate 
3222c9e429eSbrutus 	nl7c_uri_kmc = kmem_cache_create("NL7C_uri_kmc", sizeof (uri_desc_t),
3232c9e429eSbrutus 	    0, NULL, NULL, uri_kmc_reclaim, NULL, NULL, 0);
3247c478bd9Sstevel@tonic-gate 
3252c9e429eSbrutus 	nl7c_uri_rd_kmc = kmem_cache_create("NL7C_uri_rd_kmc",
3262c9e429eSbrutus 	    sizeof (uri_rd_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	uri_desb_kmc = kmem_cache_create("NL7C_uri_desb_kmc",
3297c478bd9Sstevel@tonic-gate 	    sizeof (uri_desb_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	uri_segmap_kmc = kmem_cache_create("NL7C_uri_segmap_kmc",
3327c478bd9Sstevel@tonic-gate 	    sizeof (uri_segmap_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	nl7c_http_init();
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3372c9e429eSbrutus #define	CV_SZ	16
3382c9e429eSbrutus 
3392c9e429eSbrutus void
3402c9e429eSbrutus nl7c_mi_report_hash(mblk_t *mp)
3412c9e429eSbrutus {
3422c9e429eSbrutus 	uri_hash_t	*hp, *pend;
3432c9e429eSbrutus 	uri_desc_t	*uri;
3442c9e429eSbrutus 	uint32_t	cur;
3452c9e429eSbrutus 	uint32_t	new;
3462c9e429eSbrutus 	int		n, nz, tot;
3472c9e429eSbrutus 	uint32_t	cv[CV_SZ + 1];
3482c9e429eSbrutus 
3492c9e429eSbrutus 	rw_enter(&uri_hash_access, RW_READER);
3502c9e429eSbrutus 	cur = uri_hash_which;
3512c9e429eSbrutus 	new = cur ? 0 : 1;
3522c9e429eSbrutus next:
3532c9e429eSbrutus 	for (n = 0; n <= CV_SZ; n++)
3542c9e429eSbrutus 		cv[n] = 0;
3552c9e429eSbrutus 	nz = 0;
3562c9e429eSbrutus 	tot = 0;
3572c9e429eSbrutus 	hp = &uri_hash_ab[cur][0];
3582c9e429eSbrutus 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
3592c9e429eSbrutus 	while (hp < pend) {
3602c9e429eSbrutus 		n = 0;
3612c9e429eSbrutus 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
3622c9e429eSbrutus 			n++;
3632c9e429eSbrutus 		}
3642c9e429eSbrutus 		tot += n;
3652c9e429eSbrutus 		if (n > 0)
3662c9e429eSbrutus 			nz++;
3672c9e429eSbrutus 		if (n > CV_SZ)
3682c9e429eSbrutus 			n = CV_SZ;
3692c9e429eSbrutus 		cv[n]++;
3702c9e429eSbrutus 		hp++;
3712c9e429eSbrutus 	}
3722c9e429eSbrutus 
3732c9e429eSbrutus 	(void) mi_mpprintf(mp, "\nHash=%s, Buckets=%d, "
3742c9e429eSbrutus 	    "Avrg=%d\nCount by bucket:", cur != new ? "CUR" : "NEW",
3752c9e429eSbrutus 	    uri_hash_sz[cur], nz != 0 ? ((tot * 10 + 5) / nz) / 10 : 0);
3762c9e429eSbrutus 	(void) mi_mpprintf(mp, "Free=%d", cv[0]);
3772c9e429eSbrutus 	for (n = 1; n < CV_SZ; n++) {
3782c9e429eSbrutus 		int	pn = 0;
3792c9e429eSbrutus 		char	pv[5];
3802c9e429eSbrutus 		char	*pp = pv;
3812c9e429eSbrutus 
3822c9e429eSbrutus 		for (pn = n; pn < 1000; pn *= 10)
3832c9e429eSbrutus 			*pp++ = ' ';
3842c9e429eSbrutus 		*pp = 0;
3852c9e429eSbrutus 		(void) mi_mpprintf(mp, "%s%d=%d", pv, n, cv[n]);
3862c9e429eSbrutus 	}
3872c9e429eSbrutus 	(void) mi_mpprintf(mp, "Long=%d", cv[CV_SZ]);
3882c9e429eSbrutus 
3892c9e429eSbrutus 	if (cur != new && uri_hash_ab[new] != NULL) {
3902c9e429eSbrutus 		cur = new;
3912c9e429eSbrutus 		goto next;
3922c9e429eSbrutus 	}
3932c9e429eSbrutus 	rw_exit(&uri_hash_access);
3942c9e429eSbrutus }
3952c9e429eSbrutus 
3962c9e429eSbrutus void
3972c9e429eSbrutus nl7c_mi_report_uri(mblk_t *mp)
3982c9e429eSbrutus {
3992c9e429eSbrutus 	uri_hash_t	*hp;
4002c9e429eSbrutus 	uri_desc_t	*uri;
4012c9e429eSbrutus 	uint32_t	cur;
4022c9e429eSbrutus 	uint32_t	new;
4032c9e429eSbrutus 	int		ix;
4042c9e429eSbrutus 	int		ret;
4052c9e429eSbrutus 	char		sc;
4062c9e429eSbrutus 
4072c9e429eSbrutus 	rw_enter(&uri_hash_access, RW_READER);
4082c9e429eSbrutus 	cur = uri_hash_which;
4092c9e429eSbrutus 	new = cur ? 0 : 1;
4102c9e429eSbrutus next:
4112c9e429eSbrutus 	for (ix = 0; ix < uri_hash_sz[cur]; ix++) {
4122c9e429eSbrutus 		hp = &uri_hash_ab[cur][ix];
4132c9e429eSbrutus 		mutex_enter(&hp->lock);
4142c9e429eSbrutus 		uri = hp->list;
4152c9e429eSbrutus 		while (uri != NULL) {
4162c9e429eSbrutus 			sc = *(uri->path.ep);
4172c9e429eSbrutus 			*(uri->path.ep) = 0;
4182c9e429eSbrutus 			ret = mi_mpprintf(mp, "%s: %d %d %d",
4192c9e429eSbrutus 			    uri->path.cp, (int)uri->resplen,
4202c9e429eSbrutus 			    (int)uri->respclen, (int)uri->count);
4212c9e429eSbrutus 			*(uri->path.ep) = sc;
4222c9e429eSbrutus 			if (ret == -1) break;
4232c9e429eSbrutus 			uri = uri->hash;
4242c9e429eSbrutus 		}
4252c9e429eSbrutus 		mutex_exit(&hp->lock);
4262c9e429eSbrutus 		if (ret == -1) break;
4272c9e429eSbrutus 	}
4282c9e429eSbrutus 	if (ret != -1 && cur != new && uri_hash_ab[new] != NULL) {
4292c9e429eSbrutus 		cur = new;
4302c9e429eSbrutus 		goto next;
4312c9e429eSbrutus 	}
4322c9e429eSbrutus 	rw_exit(&uri_hash_access);
4332c9e429eSbrutus }
4342c9e429eSbrutus 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * The uri_desc_t ref_t inactive function called on the last REF_RELE(),
4377c478bd9Sstevel@tonic-gate  * free all resources contained in the uri_desc_t. Note, the uri_desc_t
4387c478bd9Sstevel@tonic-gate  * will be freed by REF_RELE() on return.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate 
4412c9e429eSbrutus void
4422c9e429eSbrutus nl7c_uri_inactive(uri_desc_t *uri)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int64_t	 bytes = 0;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (uri->tail) {
4477c478bd9Sstevel@tonic-gate 		uri_rd_t *rdp = &uri->response;
4487c478bd9Sstevel@tonic-gate 		uri_rd_t *free = NULL;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		while (rdp) {
4517c478bd9Sstevel@tonic-gate 			if (rdp->off == -1) {
4527c478bd9Sstevel@tonic-gate 				bytes += rdp->sz;
4537c478bd9Sstevel@tonic-gate 				kmem_free(rdp->data.kmem, rdp->sz);
4547c478bd9Sstevel@tonic-gate 			} else {
4557c478bd9Sstevel@tonic-gate 				VN_RELE(rdp->data.vnode);
4567c478bd9Sstevel@tonic-gate 			}
4577c478bd9Sstevel@tonic-gate 			rdp = rdp->next;
4587c478bd9Sstevel@tonic-gate 			if (free != NULL) {
4592c9e429eSbrutus 				kmem_cache_free(nl7c_uri_rd_kmc, free);
4607c478bd9Sstevel@tonic-gate 			}
4617c478bd9Sstevel@tonic-gate 			free = rdp;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	if (bytes) {
4657c478bd9Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, -bytes);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if (uri->scheme != NULL) {
4687c478bd9Sstevel@tonic-gate 		nl7c_http_free(uri->scheme);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	if (uri->reqmp) {
4717c478bd9Sstevel@tonic-gate 		freeb(uri->reqmp);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * The reclaim is called by the kmem subsystem when kmem is running
4777c478bd9Sstevel@tonic-gate  * low. More work is needed to determine the best reclaim policy, for
4787c478bd9Sstevel@tonic-gate  * now we just manipulate the nl7c_uri_max global maximum bytes threshold
4797c478bd9Sstevel@tonic-gate  * value using a simple arithmetic backoff of the value every time this
4807c478bd9Sstevel@tonic-gate  * function is called then call uri_reclaim() to enforce it.
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * Note, this value remains in place and enforced for all subsequent
4837c478bd9Sstevel@tonic-gate  * URI request/response processing.
4847c478bd9Sstevel@tonic-gate  *
4857c478bd9Sstevel@tonic-gate  * Note, nl7c_uri_max is currently initialized to 0 or infinite such that
4867c478bd9Sstevel@tonic-gate  * the first call here set it to the current uri_bytes value then backoff
4877c478bd9Sstevel@tonic-gate  * from there.
4887c478bd9Sstevel@tonic-gate  *
4897c478bd9Sstevel@tonic-gate  * XXX how do we determine when to increase nl7c_uri_max ???
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4937c478bd9Sstevel@tonic-gate static void
4947c478bd9Sstevel@tonic-gate uri_kmc_reclaim(void *arg)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	uint64_t new_max;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if ((new_max = nl7c_uri_max) == 0) {
4997c478bd9Sstevel@tonic-gate 		/* Currently infinite, initialize to current bytes used */
5007c478bd9Sstevel@tonic-gate 		nl7c_uri_max = nl7c_uri_bytes;
5017c478bd9Sstevel@tonic-gate 		new_max = nl7c_uri_bytes;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	if (new_max > 1) {
5047c478bd9Sstevel@tonic-gate 		/* Lower max_bytes to 93% of current value */
5057c478bd9Sstevel@tonic-gate 		new_max >>= 1;			/* 50% */
5067c478bd9Sstevel@tonic-gate 		new_max += (new_max >> 1);	/* 75% */
5077c478bd9Sstevel@tonic-gate 		new_max += (new_max >> 2);	/* 93% */
5087c478bd9Sstevel@tonic-gate 		if (new_max < nl7c_uri_max)
5097c478bd9Sstevel@tonic-gate 			nl7c_uri_max = new_max;
5107c478bd9Sstevel@tonic-gate 		else
5117c478bd9Sstevel@tonic-gate 			nl7c_uri_max = 1;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	nl7c_uri_reclaim();
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  * Delete a uri_desc_t from the URI hash.
5187c478bd9Sstevel@tonic-gate  */
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static void
5217c478bd9Sstevel@tonic-gate uri_delete(uri_desc_t *del)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	uint32_t	hix;
5247c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp;
5257c478bd9Sstevel@tonic-gate 	uri_desc_t	*uri;
5267c478bd9Sstevel@tonic-gate 	uri_desc_t	*puri;
5277c478bd9Sstevel@tonic-gate 	uint32_t	cur;
5287c478bd9Sstevel@tonic-gate 	uint32_t	new;
5297c478bd9Sstevel@tonic-gate 
5302c9e429eSbrutus 	ASSERT(del->hash != URI_TEMP);
5317c478bd9Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
5327c478bd9Sstevel@tonic-gate 	cur = uri_hash_which;
5337c478bd9Sstevel@tonic-gate 	new = cur ? 0 : 1;
5347c478bd9Sstevel@tonic-gate next:
5357c478bd9Sstevel@tonic-gate 	puri = NULL;
5362c9e429eSbrutus 	hix = del->hvalue;
5372c9e429eSbrutus 	URI_HASH_IX(hix, cur);
5387c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
5397c478bd9Sstevel@tonic-gate 	for (uri = hp->list; uri != NULL; uri = uri->hash) {
5407c478bd9Sstevel@tonic-gate 		if (uri != del) {
5417c478bd9Sstevel@tonic-gate 			puri = uri;
5427c478bd9Sstevel@tonic-gate 			continue;
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 		/*
5457c478bd9Sstevel@tonic-gate 		 * Found the URI, unlink from the hash chain,
5467c478bd9Sstevel@tonic-gate 		 * drop locks, ref release it.
5477c478bd9Sstevel@tonic-gate 		 */
5487c478bd9Sstevel@tonic-gate 		URI_HASH_UNLINK(cur, new, hp, puri, uri);
5497c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
5507c478bd9Sstevel@tonic-gate 		REF_RELE(uri);
5517c478bd9Sstevel@tonic-gate 		return;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
5567c478bd9Sstevel@tonic-gate 		 * check the new hash next.
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		cur = new;
5597c478bd9Sstevel@tonic-gate 		goto next;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Add a uri_desc_t to the URI hash.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate static void
5697c478bd9Sstevel@tonic-gate uri_add(uri_desc_t *uri, krw_t rwlock, boolean_t nonblocking)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	uint32_t	hix;
5727c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp;
5737c478bd9Sstevel@tonic-gate 	uint32_t	cur = uri_hash_which;
5747c478bd9Sstevel@tonic-gate 	uint32_t	new = cur ? 0 : 1;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * Caller of uri_add() must hold the uri_hash_access rwlock.
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	ASSERT((rwlock == RW_READER && RW_READ_HELD(&uri_hash_access)) ||
5807c478bd9Sstevel@tonic-gate 	    (rwlock == RW_WRITER && RW_WRITE_HELD(&uri_hash_access)));
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * uri_add() always succeeds so add a hash ref to the URI now.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	REF_HOLD(uri);
5857c478bd9Sstevel@tonic-gate again:
5862c9e429eSbrutus 	hix = uri->hvalue;
5872c9e429eSbrutus 	URI_HASH_IX(hix, cur);
5887c478bd9Sstevel@tonic-gate 	if (uri_hash_ab[new] == NULL &&
5897c478bd9Sstevel@tonic-gate 	    uri_hash_cnt[cur] < uri_hash_overflow[cur]) {
5907c478bd9Sstevel@tonic-gate 		/*
5917c478bd9Sstevel@tonic-gate 		 * Easy case, no new hash and current hasn't overflowed,
5927c478bd9Sstevel@tonic-gate 		 * add URI to current hash and return.
5937c478bd9Sstevel@tonic-gate 		 *
5947c478bd9Sstevel@tonic-gate 		 * Note, the check for uri_hash_cnt[] above aren't done
5957c478bd9Sstevel@tonic-gate 		 * atomictally, i.e. multiple threads can be in this code
5967c478bd9Sstevel@tonic-gate 		 * as RW_READER and update the cnt[], this isn't a problem
5977c478bd9Sstevel@tonic-gate 		 * as the check is only advisory.
5987c478bd9Sstevel@tonic-gate 		 */
5997c478bd9Sstevel@tonic-gate 	fast:
600*1a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&uri_hash_cnt[cur]);
6017c478bd9Sstevel@tonic-gate 		hp = &uri_hash_ab[cur][hix];
6027c478bd9Sstevel@tonic-gate 		mutex_enter(&hp->lock);
6037c478bd9Sstevel@tonic-gate 		uri->hash = hp->list;
6047c478bd9Sstevel@tonic-gate 		hp->list = uri;
6057c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->lock);
6067c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
6077c478bd9Sstevel@tonic-gate 		return;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	if (uri_hash_ab[new] == NULL) {
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * Need a new a or b hash, if not already RW_WRITER
6127c478bd9Sstevel@tonic-gate 		 * try to upgrade our lock to writer.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		if (rwlock != RW_WRITER && ! rw_tryupgrade(&uri_hash_access)) {
6157c478bd9Sstevel@tonic-gate 			/*
6167c478bd9Sstevel@tonic-gate 			 * Upgrade failed, we can't simple exit and reenter
6177c478bd9Sstevel@tonic-gate 			 * the lock as after the exit and before the reenter
6187c478bd9Sstevel@tonic-gate 			 * the whole world can change so just wait for writer
6197c478bd9Sstevel@tonic-gate 			 * then do everything again.
6207c478bd9Sstevel@tonic-gate 			 */
6217c478bd9Sstevel@tonic-gate 			if (nonblocking) {
6227c478bd9Sstevel@tonic-gate 				/*
6237c478bd9Sstevel@tonic-gate 				 * Can't block, use fast-path above.
6247c478bd9Sstevel@tonic-gate 				 *
6257c478bd9Sstevel@tonic-gate 				 * XXX should have a background thread to
6267c478bd9Sstevel@tonic-gate 				 * handle new ab[] in this case so as to
6277c478bd9Sstevel@tonic-gate 				 * not overflow the cur hash to much.
6287c478bd9Sstevel@tonic-gate 				 */
6297c478bd9Sstevel@tonic-gate 				goto fast;
6307c478bd9Sstevel@tonic-gate 			}
6317c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
6327c478bd9Sstevel@tonic-gate 			rwlock = RW_WRITER;
6337c478bd9Sstevel@tonic-gate 			rw_enter(&uri_hash_access, rwlock);
6347c478bd9Sstevel@tonic-gate 			cur = uri_hash_which;
6357c478bd9Sstevel@tonic-gate 			new = cur ? 0 : 1;
6367c478bd9Sstevel@tonic-gate 			goto again;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		rwlock = RW_WRITER;
6397c478bd9Sstevel@tonic-gate 		if (uri_hash_ab[new] == NULL) {
6407c478bd9Sstevel@tonic-gate 			/*
6417c478bd9Sstevel@tonic-gate 			 * Still need a new hash, allocate and initialize
6427c478bd9Sstevel@tonic-gate 			 * the new hash.
6437c478bd9Sstevel@tonic-gate 			 */
6447c478bd9Sstevel@tonic-gate 			uri_hash_n[new] = uri_hash_n[cur] + 1;
6457c478bd9Sstevel@tonic-gate 			if (uri_hash_n[new] == 0) {
6467c478bd9Sstevel@tonic-gate 				/*
6477c478bd9Sstevel@tonic-gate 				 * No larger P2Ps[] value so use current,
6487c478bd9Sstevel@tonic-gate 				 * i.e. 2 of the largest are better than 1 ?
6497c478bd9Sstevel@tonic-gate 				 */
6507c478bd9Sstevel@tonic-gate 				uri_hash_n[new] = uri_hash_n[cur];
6517c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "NL7C: hash index overflow");
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			uri_hash_sz[new] = P2Ps[uri_hash_n[new]];
6547c478bd9Sstevel@tonic-gate 			ASSERT(uri_hash_cnt[new] == 0);
6557c478bd9Sstevel@tonic-gate 			uri_hash_overflow[new] = uri_hash_sz[new] *
6567c478bd9Sstevel@tonic-gate 			    URI_HASH_AVRG;
6577c478bd9Sstevel@tonic-gate 			uri_hash_ab[new] = kmem_zalloc(sizeof (uri_hash_t) *
6587c478bd9Sstevel@tonic-gate 			    uri_hash_sz[new], nonblocking ? KM_NOSLEEP :
6597c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6607c478bd9Sstevel@tonic-gate 			if (uri_hash_ab[new] == NULL) {
6617c478bd9Sstevel@tonic-gate 				/*
6627c478bd9Sstevel@tonic-gate 				 * Alloc failed, use fast-path above.
6637c478bd9Sstevel@tonic-gate 				 *
6647c478bd9Sstevel@tonic-gate 				 * XXX should have a background thread to
6657c478bd9Sstevel@tonic-gate 				 * handle new ab[] in this case so as to
6667c478bd9Sstevel@tonic-gate 				 * not overflow the cur hash to much.
6677c478bd9Sstevel@tonic-gate 				 */
6687c478bd9Sstevel@tonic-gate 				goto fast;
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 			uri_hash_lru[new] = uri_hash_ab[new];
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Hashed against current hash so migrate any current hash chain
6757c478bd9Sstevel@tonic-gate 	 * members, if any.
6767c478bd9Sstevel@tonic-gate 	 *
6777c478bd9Sstevel@tonic-gate 	 * Note, the hash chain list can be checked for a non empty list
6787c478bd9Sstevel@tonic-gate 	 * outside of the hash chain list lock as the hash chain struct
6797c478bd9Sstevel@tonic-gate 	 * can't be destroyed while in the uri_hash_access rwlock, worst
6807c478bd9Sstevel@tonic-gate 	 * case is that a non empty list is found and after acquiring the
6817c478bd9Sstevel@tonic-gate 	 * lock another thread beats us to it (i.e. migrated the list).
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
6847c478bd9Sstevel@tonic-gate 	if (hp->list != NULL) {
6857c478bd9Sstevel@tonic-gate 		URI_HASH_MIGRATE(cur, hp, new);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	/*
6887c478bd9Sstevel@tonic-gate 	 * If new hash has overflowed before current hash has been
6897c478bd9Sstevel@tonic-gate 	 * completely migrated then walk all current hash chains and
6907c478bd9Sstevel@tonic-gate 	 * migrate list members now.
6917c478bd9Sstevel@tonic-gate 	 */
692*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_inc_32_nv(&uri_hash_cnt[new]) >= uri_hash_overflow[new]) {
6937c478bd9Sstevel@tonic-gate 		for (hix = 0; hix < uri_hash_sz[cur]; hix++) {
6947c478bd9Sstevel@tonic-gate 			hp = &uri_hash_ab[cur][hix];
6957c478bd9Sstevel@tonic-gate 			if (hp->list != NULL) {
6967c478bd9Sstevel@tonic-gate 				URI_HASH_MIGRATE(cur, hp, new);
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Add URI to new hash.
7027c478bd9Sstevel@tonic-gate 	 */
7032c9e429eSbrutus 	hix = uri->hvalue;
7042c9e429eSbrutus 	URI_HASH_IX(hix, new);
7057c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[new][hix];
7067c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->lock);
7077c478bd9Sstevel@tonic-gate 	uri->hash = hp->list;
7087c478bd9Sstevel@tonic-gate 	hp->list = uri;
7097c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->lock);
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Last, check to see if last cur hash chain has been
7127c478bd9Sstevel@tonic-gate 	 * migrated, if so free cur hash and make new hash cur.
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (uri_hash_cnt[cur] == 0) {
7157c478bd9Sstevel@tonic-gate 		/*
7167c478bd9Sstevel@tonic-gate 		 * If we don't already hold the uri_hash_access rwlock for
7177c478bd9Sstevel@tonic-gate 		 * RW_WRITE try to upgrade to RW_WRITE and if successful
7187c478bd9Sstevel@tonic-gate 		 * check again and to see if still need to do the free.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		if ((rwlock == RW_WRITER || rw_tryupgrade(&uri_hash_access)) &&
7217c478bd9Sstevel@tonic-gate 		    uri_hash_cnt[cur] == 0 && uri_hash_ab[new] != 0) {
7227c478bd9Sstevel@tonic-gate 			kmem_free(uri_hash_ab[cur],
7237c478bd9Sstevel@tonic-gate 			    sizeof (uri_hash_t) * uri_hash_sz[cur]);
7247c478bd9Sstevel@tonic-gate 			uri_hash_ab[cur] = NULL;
7257c478bd9Sstevel@tonic-gate 			uri_hash_lru[cur] = NULL;
7267c478bd9Sstevel@tonic-gate 			uri_hash_which = new;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate /*
7337c478bd9Sstevel@tonic-gate  * Lookup a uri_desc_t in the URI hash, if found free the request uri_desc_t
7342c9e429eSbrutus  * and return the found uri_desc_t with a REF_HOLD() placed on it. Else, if
7352c9e429eSbrutus  * add B_TRUE use the request URI to create a new hash entry. Else if add
7362c9e429eSbrutus  * B_FALSE ...
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate static uri_desc_t *
7407c478bd9Sstevel@tonic-gate uri_lookup(uri_desc_t *ruri, boolean_t add, boolean_t nonblocking)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	uint32_t	hix;
7437c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp;
7447c478bd9Sstevel@tonic-gate 	uri_desc_t	*uri;
7457c478bd9Sstevel@tonic-gate 	uri_desc_t	*puri;
7467c478bd9Sstevel@tonic-gate 	uint32_t	cur;
7477c478bd9Sstevel@tonic-gate 	uint32_t	new;
7487c478bd9Sstevel@tonic-gate 	char		*rcp = ruri->path.cp;
7497c478bd9Sstevel@tonic-gate 	char		*rep = ruri->path.ep;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate again:
7527c478bd9Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_READER);
7537c478bd9Sstevel@tonic-gate 	cur = uri_hash_which;
7547c478bd9Sstevel@tonic-gate 	new = cur ? 0 : 1;
7557c478bd9Sstevel@tonic-gate nexthash:
7567c478bd9Sstevel@tonic-gate 	puri = NULL;
7572c9e429eSbrutus 	hix = ruri->hvalue;
7582c9e429eSbrutus 	URI_HASH_IX(hix, cur);
7597c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
7607c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->lock);
7617c478bd9Sstevel@tonic-gate 	for (uri = hp->list; uri != NULL; uri = uri->hash) {
7627c478bd9Sstevel@tonic-gate 		char	*ap = uri->path.cp;
7637c478bd9Sstevel@tonic-gate 		char	*bp = rcp;
7647c478bd9Sstevel@tonic-gate 		char	a, b;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		/* Compare paths */
7677c478bd9Sstevel@tonic-gate 		while (bp < rep && ap < uri->path.ep) {
7687c478bd9Sstevel@tonic-gate 			if ((a = *ap) == '%') {
7697c478bd9Sstevel@tonic-gate 				/* Escaped hex multichar, convert it */
7707c478bd9Sstevel@tonic-gate 				H2A(ap, uri->path.ep, a);
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 			if ((b = *bp) == '%') {
7737c478bd9Sstevel@tonic-gate 				/* Escaped hex multichar, convert it */
7747c478bd9Sstevel@tonic-gate 				H2A(bp, rep, b);
7757c478bd9Sstevel@tonic-gate 			}
7767c478bd9Sstevel@tonic-gate 			if (a != b) {
7777c478bd9Sstevel@tonic-gate 				/* Char's don't match */
7787c478bd9Sstevel@tonic-gate 				goto nexturi;
7797c478bd9Sstevel@tonic-gate 			}
7807c478bd9Sstevel@tonic-gate 			ap++;
7817c478bd9Sstevel@tonic-gate 			bp++;
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 		if (bp != rep || ap != uri->path.ep) {
7847c478bd9Sstevel@tonic-gate 			/* Not same length */
7857c478bd9Sstevel@tonic-gate 			goto nexturi;
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 		ap = uri->auth.cp;
7887c478bd9Sstevel@tonic-gate 		bp = ruri->auth.cp;
7897c478bd9Sstevel@tonic-gate 		if (ap != NULL) {
7907c478bd9Sstevel@tonic-gate 			if (bp == NULL) {
7917c478bd9Sstevel@tonic-gate 				/* URI has auth request URI doesn't */
7927c478bd9Sstevel@tonic-gate 				goto nexturi;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 			while (bp < ruri->auth.ep && ap < uri->auth.ep) {
7957c478bd9Sstevel@tonic-gate 				if ((a = *ap) == '%') {
7967c478bd9Sstevel@tonic-gate 					/* Escaped hex multichar, convert it */
7977c478bd9Sstevel@tonic-gate 					H2A(ap, uri->path.ep, a);
7987c478bd9Sstevel@tonic-gate 				}
7997c478bd9Sstevel@tonic-gate 				if ((b = *bp) == '%') {
8007c478bd9Sstevel@tonic-gate 					/* Escaped hex multichar, convert it */
8017c478bd9Sstevel@tonic-gate 					H2A(bp, rep, b);
8027c478bd9Sstevel@tonic-gate 				}
8037c478bd9Sstevel@tonic-gate 				if (a != b) {
8047c478bd9Sstevel@tonic-gate 					/* Char's don't match */
8057c478bd9Sstevel@tonic-gate 					goto nexturi;
8067c478bd9Sstevel@tonic-gate 				}
8077c478bd9Sstevel@tonic-gate 				ap++;
8087c478bd9Sstevel@tonic-gate 				bp++;
8097c478bd9Sstevel@tonic-gate 			}
8107c478bd9Sstevel@tonic-gate 			if (bp != ruri->auth.ep || ap != uri->auth.ep) {
8117c478bd9Sstevel@tonic-gate 				/* Not same length */
8127c478bd9Sstevel@tonic-gate 				goto nexturi;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		} else if (bp != NULL) {
8157c478bd9Sstevel@tonic-gate 			/* URI doesn't have auth and request URI does */
8167c478bd9Sstevel@tonic-gate 			goto nexturi;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		/*
8192c9e429eSbrutus 		 * Have a path/auth match so before any other processing
8202c9e429eSbrutus 		 * of requested URI, check for expire or request no cache
8212c9e429eSbrutus 		 * purge.
8227c478bd9Sstevel@tonic-gate 		 */
823d3d50737SRafael Vanoni 		if (uri->expire >= 0 && uri->expire <= ddi_get_lbolt() ||
824d3d50737SRafael Vanoni 		    ruri->nocache) {
8257c478bd9Sstevel@tonic-gate 			/*
8267c478bd9Sstevel@tonic-gate 			 * URI has expired or request specified to not use
8277c478bd9Sstevel@tonic-gate 			 * the cached version, unlink the URI from the hash
8287c478bd9Sstevel@tonic-gate 			 * chain, release all locks, release the hash ref
8297c478bd9Sstevel@tonic-gate 			 * on the URI, and last look it up again.
8302c9e429eSbrutus 			 *
8312c9e429eSbrutus 			 * Note, this will cause all variants of the named
8322c9e429eSbrutus 			 * URI to be purged.
8337c478bd9Sstevel@tonic-gate 			 */
8347c478bd9Sstevel@tonic-gate 			if (puri != NULL) {
8357c478bd9Sstevel@tonic-gate 				puri->hash = uri->hash;
8367c478bd9Sstevel@tonic-gate 			} else {
8377c478bd9Sstevel@tonic-gate 				hp->list = uri->hash;
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->lock);
840*1a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&uri_hash_cnt[cur]);
8417c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8422c9e429eSbrutus 			if (ruri->nocache)
8432c9e429eSbrutus 				nl7c_uri_purge++;
8442c9e429eSbrutus 			else
8457c478bd9Sstevel@tonic-gate 				nl7c_uri_expire++;
8467c478bd9Sstevel@tonic-gate 			REF_RELE(uri);
8477c478bd9Sstevel@tonic-gate 			goto again;
8487c478bd9Sstevel@tonic-gate 		}
8492c9e429eSbrutus 		if (uri->scheme != NULL) {
8507c478bd9Sstevel@tonic-gate 			/*
8512c9e429eSbrutus 			 * URI has scheme private qualifier(s), if request
8522c9e429eSbrutus 			 * URI doesn't or if no match skip this URI.
8532c9e429eSbrutus 			 */
8542c9e429eSbrutus 			if (ruri->scheme == NULL ||
8552c9e429eSbrutus 			    ! nl7c_http_cmp(uri->scheme, ruri->scheme))
8562c9e429eSbrutus 				goto nexturi;
8572c9e429eSbrutus 		} else if (ruri->scheme != NULL) {
8582c9e429eSbrutus 			/*
8592c9e429eSbrutus 			 * URI doesn't have scheme private qualifiers but
8602c9e429eSbrutus 			 * request URI does, no match, skip this URI.
8612c9e429eSbrutus 			 */
8622c9e429eSbrutus 			goto nexturi;
8632c9e429eSbrutus 		}
8642c9e429eSbrutus 		/*
8652c9e429eSbrutus 		 * Have a match, ready URI for return, first put a reference
8662c9e429eSbrutus 		 * hold on the URI, if this URI is currently being processed
8672c9e429eSbrutus 		 * then have to wait for the processing to be completed and
8682c9e429eSbrutus 		 * redo the lookup, else return it.
8697c478bd9Sstevel@tonic-gate 		 */
8707c478bd9Sstevel@tonic-gate 		REF_HOLD(uri);
8717c478bd9Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
8727c478bd9Sstevel@tonic-gate 		if (uri->proc != NULL) {
8737c478bd9Sstevel@tonic-gate 			/* The URI is being processed, wait for completion */
8747c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8757c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8767c478bd9Sstevel@tonic-gate 			if (! nonblocking &&
8777c478bd9Sstevel@tonic-gate 			    cv_wait_sig(&uri->waiting, &uri->proclock)) {
8787c478bd9Sstevel@tonic-gate 				/*
8797c478bd9Sstevel@tonic-gate 				 * URI has been processed but things may
8807c478bd9Sstevel@tonic-gate 				 * have changed while we were away so do
8817c478bd9Sstevel@tonic-gate 				 * most everything again.
8827c478bd9Sstevel@tonic-gate 				 */
8837c478bd9Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8847c478bd9Sstevel@tonic-gate 				REF_RELE(uri);
8857c478bd9Sstevel@tonic-gate 				goto again;
8867c478bd9Sstevel@tonic-gate 			} else {
8877c478bd9Sstevel@tonic-gate 				/*
8887c478bd9Sstevel@tonic-gate 				 * A nonblocking socket or an interrupted
8897c478bd9Sstevel@tonic-gate 				 * cv_wait_sig() in the first case can't
8907c478bd9Sstevel@tonic-gate 				 * block waiting for the processing of the
8917c478bd9Sstevel@tonic-gate 				 * uri hash hit uri to complete, in both
8927c478bd9Sstevel@tonic-gate 				 * cases just return failure to lookup.
8937c478bd9Sstevel@tonic-gate 				 */
8947c478bd9Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8957c478bd9Sstevel@tonic-gate 				REF_RELE(uri);
8967c478bd9Sstevel@tonic-gate 				return (NULL);
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 		}
8992c9e429eSbrutus 		mutex_exit(&uri->proclock);
9007c478bd9Sstevel@tonic-gate 		uri->hit++;
9017c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->lock);
9027c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9037c478bd9Sstevel@tonic-gate 		return (uri);
9047c478bd9Sstevel@tonic-gate 	nexturi:
9057c478bd9Sstevel@tonic-gate 		puri = uri;
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->lock);
9087c478bd9Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
9117c478bd9Sstevel@tonic-gate 		 * check the new hash next.
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		cur = new;
9147c478bd9Sstevel@tonic-gate 		goto nexthash;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate add:
9177c478bd9Sstevel@tonic-gate 	if (! add) {
9182c9e429eSbrutus 		/* Lookup only so return failure */
9197c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9207c478bd9Sstevel@tonic-gate 		return (NULL);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	/*
9237c478bd9Sstevel@tonic-gate 	 * URI not hashed, finish intialization of the
9247c478bd9Sstevel@tonic-gate 	 * request URI, add it to the hash, return it.
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate 	ruri->hit = 0;
9277c478bd9Sstevel@tonic-gate 	ruri->expire = -1;
9287c478bd9Sstevel@tonic-gate 	ruri->response.sz = 0;
9292c9e429eSbrutus 	ruri->proc = (struct sonode *)~NULL;
9307c478bd9Sstevel@tonic-gate 	cv_init(&ruri->waiting, NULL, CV_DEFAULT, NULL);
9317c478bd9Sstevel@tonic-gate 	mutex_init(&ruri->proclock, NULL, MUTEX_DEFAULT, NULL);
9327c478bd9Sstevel@tonic-gate 	uri_add(ruri, RW_READER, nonblocking);
9337c478bd9Sstevel@tonic-gate 	/* uri_add() has done rw_exit(&uri_hash_access) */
9347c478bd9Sstevel@tonic-gate 	return (ruri);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Reclaim URIs until max cache size threshold has been reached.
9397c478bd9Sstevel@tonic-gate  *
9407c478bd9Sstevel@tonic-gate  * A CLOCK based reclaim modified with a history (hit counter) counter.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate static void
9447c478bd9Sstevel@tonic-gate nl7c_uri_reclaim(void)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp, *start, *pend;
9477c478bd9Sstevel@tonic-gate 	uri_desc_t	*uri;
9487c478bd9Sstevel@tonic-gate 	uri_desc_t	*puri;
9497c478bd9Sstevel@tonic-gate 	uint32_t	cur;
9507c478bd9Sstevel@tonic-gate 	uint32_t	new;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	nl7c_uri_reclaim_calls++;
9537c478bd9Sstevel@tonic-gate again:
9547c478bd9Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
9557c478bd9Sstevel@tonic-gate 	cur = uri_hash_which;
9567c478bd9Sstevel@tonic-gate 	new = cur ? 0 : 1;
9577c478bd9Sstevel@tonic-gate next:
9587c478bd9Sstevel@tonic-gate 	hp = uri_hash_lru[cur];
9597c478bd9Sstevel@tonic-gate 	start = hp;
9607c478bd9Sstevel@tonic-gate 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
9617c478bd9Sstevel@tonic-gate 	while (nl7c_uri_bytes > nl7c_uri_max) {
9627c478bd9Sstevel@tonic-gate 		puri = NULL;
9637c478bd9Sstevel@tonic-gate 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
9647c478bd9Sstevel@tonic-gate 			if (uri->hit != 0) {
9657c478bd9Sstevel@tonic-gate 				/*
9667c478bd9Sstevel@tonic-gate 				 * Decrement URI activity counter and skip.
9677c478bd9Sstevel@tonic-gate 				 */
9687c478bd9Sstevel@tonic-gate 				uri->hit--;
9697c478bd9Sstevel@tonic-gate 				puri = uri;
9707c478bd9Sstevel@tonic-gate 				continue;
9717c478bd9Sstevel@tonic-gate 			}
9727c478bd9Sstevel@tonic-gate 			if (uri->proc != NULL) {
9737c478bd9Sstevel@tonic-gate 				/*
9747c478bd9Sstevel@tonic-gate 				 * Currently being processed by a socket, skip.
9757c478bd9Sstevel@tonic-gate 				 */
9767c478bd9Sstevel@tonic-gate 				continue;
9777c478bd9Sstevel@tonic-gate 			}
9787c478bd9Sstevel@tonic-gate 			/*
9797c478bd9Sstevel@tonic-gate 			 * Found a candidate, no hit(s) since added or last
9807c478bd9Sstevel@tonic-gate 			 * reclaim pass, unlink from it's hash chain, update
9817c478bd9Sstevel@tonic-gate 			 * lru scan pointer, drop lock, ref release it.
9827c478bd9Sstevel@tonic-gate 			 */
9837c478bd9Sstevel@tonic-gate 			URI_HASH_UNLINK(cur, new, hp, puri, uri);
9847c478bd9Sstevel@tonic-gate 			if (cur == uri_hash_which) {
9857c478bd9Sstevel@tonic-gate 				if (++hp == pend) {
9867c478bd9Sstevel@tonic-gate 					/* Wrap pointer */
9877c478bd9Sstevel@tonic-gate 					hp = uri_hash_ab[cur];
9887c478bd9Sstevel@tonic-gate 				}
9897c478bd9Sstevel@tonic-gate 				uri_hash_lru[cur] = hp;
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
9927c478bd9Sstevel@tonic-gate 			REF_RELE(uri);
9937c478bd9Sstevel@tonic-gate 			nl7c_uri_reclaim_cnt++;
9947c478bd9Sstevel@tonic-gate 			goto again;
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		if (++hp == pend) {
9977c478bd9Sstevel@tonic-gate 			/* Wrap pointer */
9987c478bd9Sstevel@tonic-gate 			hp = uri_hash_ab[cur];
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 		if (hp == start) {
10017c478bd9Sstevel@tonic-gate 			if (cur != new && uri_hash_ab[new] != NULL) {
10027c478bd9Sstevel@tonic-gate 				/*
10037c478bd9Sstevel@tonic-gate 				 * Done with the current hash and have a
10047c478bd9Sstevel@tonic-gate 				 * new hash so check the new hash next.
10057c478bd9Sstevel@tonic-gate 				 */
10067c478bd9Sstevel@tonic-gate 				cur = new;
10077c478bd9Sstevel@tonic-gate 				goto next;
10087c478bd9Sstevel@tonic-gate 			}
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate  * Called for a socket which is being freed prior to close, e.g. errored.
10167c478bd9Sstevel@tonic-gate  */
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate void
10197c478bd9Sstevel@tonic-gate nl7c_urifree(struct sonode *so)
10207c478bd9Sstevel@tonic-gate {
10210f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
10220f1702c5SYu Xiangning 	uri_desc_t *uri = (uri_desc_t *)sti->sti_nl7c_uri;
10237c478bd9Sstevel@tonic-gate 
10240f1702c5SYu Xiangning 	sti->sti_nl7c_uri = NULL;
10257c478bd9Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
10267c478bd9Sstevel@tonic-gate 		uri_delete(uri);
10277c478bd9Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
10287c478bd9Sstevel@tonic-gate 		uri->proc = NULL;
10297c478bd9Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
10307c478bd9Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
10337c478bd9Sstevel@tonic-gate 		nl7c_uri_free++;
10347c478bd9Sstevel@tonic-gate 	} else {
10357c478bd9Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
10367c478bd9Sstevel@tonic-gate 		uri->proc = NULL;
10377c478bd9Sstevel@tonic-gate 		nl7c_uri_temp_free++;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 	REF_RELE(uri);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate /*
10432c9e429eSbrutus  * ...
10442c9e429eSbrutus  *
10452c9e429eSbrutus  *	< 0	need more data
10462c9e429eSbrutus  *
10472c9e429eSbrutus  *	  0	parse complete
10482c9e429eSbrutus  *
10492c9e429eSbrutus  *	> 0	parse error
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate 
10522c9e429eSbrutus volatile uint64_t nl7c_resp_pfail = 0;
10532c9e429eSbrutus volatile uint64_t nl7c_resp_ntemp = 0;
10542c9e429eSbrutus volatile uint64_t nl7c_resp_pass = 0;
10557c478bd9Sstevel@tonic-gate 
10562c9e429eSbrutus static int
10572c9e429eSbrutus nl7c_resp_parse(struct sonode *so, uri_desc_t *uri, char *data, int sz)
10582c9e429eSbrutus {
10592c9e429eSbrutus 	if (! nl7c_http_response(&data, &data[sz], uri, so)) {
10602c9e429eSbrutus 		if (data == NULL) {
10612c9e429eSbrutus 			/* Parse fail */
10622c9e429eSbrutus 			goto pfail;
10632c9e429eSbrutus 		}
10642c9e429eSbrutus 		/* More data */
10652c9e429eSbrutus 		data = NULL;
10662c9e429eSbrutus 	} else if (data == NULL) {
10672c9e429eSbrutus 		goto pass;
10682c9e429eSbrutus 	}
10692c9e429eSbrutus 	if (uri->hash != URI_TEMP && uri->nocache) {
10702c9e429eSbrutus 		/*
10712c9e429eSbrutus 		 * After response parse now no cache,
10722c9e429eSbrutus 		 * delete it from cache, wakeup any
10732c9e429eSbrutus 		 * waiters on this URI, make URI_TEMP.
10742c9e429eSbrutus 		 */
10752c9e429eSbrutus 		uri_delete(uri);
10762c9e429eSbrutus 		mutex_enter(&uri->proclock);
10772c9e429eSbrutus 		if (CV_HAS_WAITERS(&uri->waiting)) {
10782c9e429eSbrutus 			cv_broadcast(&uri->waiting);
10792c9e429eSbrutus 		}
10802c9e429eSbrutus 		mutex_exit(&uri->proclock);
10812c9e429eSbrutus 		uri->hash = URI_TEMP;
10822c9e429eSbrutus 		nl7c_uri_temp_mk++;
10832c9e429eSbrutus 	}
10842c9e429eSbrutus 	if (data == NULL) {
10852c9e429eSbrutus 		/* More data needed */
10862c9e429eSbrutus 		return (-1);
10872c9e429eSbrutus 	}
10882c9e429eSbrutus 	/* Success */
10892c9e429eSbrutus 	return (0);
10902c9e429eSbrutus 
10912c9e429eSbrutus pfail:
10922c9e429eSbrutus 	nl7c_resp_pfail++;
10932c9e429eSbrutus 	return (EINVAL);
10942c9e429eSbrutus 
10952c9e429eSbrutus pass:
10962c9e429eSbrutus 	nl7c_resp_pass++;
10972c9e429eSbrutus 	return (ENOTSUP);
10982c9e429eSbrutus }
10992c9e429eSbrutus 
11002c9e429eSbrutus /*
11012c9e429eSbrutus  * Called to sink application response data, the processing of the data
11022c9e429eSbrutus  * is the same for a cached or temp URI (i.e. a URI for which we aren't
11032c9e429eSbrutus  * going to cache the URI but want to parse it for detecting response
11042c9e429eSbrutus  * data end such that for a persistent connection we can parse the next
11052c9e429eSbrutus  * request).
11062c9e429eSbrutus  *
11072c9e429eSbrutus  * On return 0 is returned for sink success, > 0 on error, and < 0 on
11082c9e429eSbrutus  * no so URI (note, data not sinked).
11092c9e429eSbrutus  */
11102c9e429eSbrutus 
11112c9e429eSbrutus int
11122c9e429eSbrutus nl7c_data(struct sonode *so, uio_t *uio)
11137c478bd9Sstevel@tonic-gate {
11140f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
11150f1702c5SYu Xiangning 	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
11162c9e429eSbrutus 	iovec_t		*iov;
11172c9e429eSbrutus 	int		cnt;
11182c9e429eSbrutus 	int		sz = uio->uio_resid;
11192c9e429eSbrutus 	char		*data, *alloc;
11207c478bd9Sstevel@tonic-gate 	char		*bp;
11217c478bd9Sstevel@tonic-gate 	uri_rd_t	*rdp;
11222c9e429eSbrutus 	boolean_t	first;
11232c9e429eSbrutus 	int		error, perror;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	nl7c_uri_data++;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (uri == NULL) {
11287c478bd9Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
11290f1702c5SYu Xiangning 		sti->sti_nl7c_flags = 0;
11307c478bd9Sstevel@tonic-gate 		nl7c_uri_NULL1++;
11312c9e429eSbrutus 		return (-1);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11340f1702c5SYu Xiangning 	if (sti->sti_nl7c_flags & NL7C_WAITWRITE) {
11350f1702c5SYu Xiangning 		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
11362c9e429eSbrutus 		first = B_TRUE;
11372c9e429eSbrutus 	} else {
11382c9e429eSbrutus 		first = B_FALSE;
11392c9e429eSbrutus 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	alloc = kmem_alloc(sz, KM_SLEEP);
11427c478bd9Sstevel@tonic-gate 	URI_RD_ADD(uri, rdp, sz, -1);
11432c9e429eSbrutus 	if (rdp == NULL) {
11442c9e429eSbrutus 		error = ENOMEM;
11457c478bd9Sstevel@tonic-gate 		goto fail;
11462c9e429eSbrutus 	}
11472c9e429eSbrutus 
11482c9e429eSbrutus 	if (uri->hash != URI_TEMP && uri->count > nca_max_cache_size) {
11492c9e429eSbrutus 		uri_delete(uri);
11502c9e429eSbrutus 		uri->hash = URI_TEMP;
11512c9e429eSbrutus 	}
11527c478bd9Sstevel@tonic-gate 	data = alloc;
11537c478bd9Sstevel@tonic-gate 	alloc = NULL;
11542c9e429eSbrutus 	rdp->data.kmem = data;
11557c478bd9Sstevel@tonic-gate 	atomic_add_64(&nl7c_uri_bytes, sz);
11562c9e429eSbrutus 
11572c9e429eSbrutus 	bp = data;
11582c9e429eSbrutus 	while (uio->uio_resid > 0) {
11592c9e429eSbrutus 		iov = uio->uio_iov;
11602c9e429eSbrutus 		if ((cnt = iov->iov_len) == 0) {
11612c9e429eSbrutus 			goto next;
11627c478bd9Sstevel@tonic-gate 		}
11632c9e429eSbrutus 		cnt = MIN(cnt, uio->uio_resid);
11642c9e429eSbrutus 		error = xcopyin(iov->iov_base, bp, cnt);
11652c9e429eSbrutus 		if (error)
11662c9e429eSbrutus 			goto fail;
11672c9e429eSbrutus 
11682c9e429eSbrutus 		iov->iov_base += cnt;
11692c9e429eSbrutus 		iov->iov_len -= cnt;
11702c9e429eSbrutus 		uio->uio_resid -= cnt;
11712c9e429eSbrutus 		uio->uio_loffset += cnt;
11722c9e429eSbrutus 		bp += cnt;
11732c9e429eSbrutus 	next:
11742c9e429eSbrutus 		uio->uio_iov++;
11752c9e429eSbrutus 		uio->uio_iovcnt--;
11762c9e429eSbrutus 	}
11772c9e429eSbrutus 
11782c9e429eSbrutus 	/* Successfull sink of data, response parse the data */
11792c9e429eSbrutus 	perror = nl7c_resp_parse(so, uri, data, sz);
11802c9e429eSbrutus 
11812c9e429eSbrutus 	/* Send the data out the connection */
11822c9e429eSbrutus 	error = uri_rd_response(so, uri, rdp, first);
11832c9e429eSbrutus 	if (error)
11842c9e429eSbrutus 		goto fail;
11852c9e429eSbrutus 
11862c9e429eSbrutus 	/* Success */
11872c9e429eSbrutus 	if (perror == 0 &&
11882c9e429eSbrutus 	    ((uri->respclen == URI_LEN_NOVALUE &&
11892c9e429eSbrutus 	    uri->resplen == URI_LEN_NOVALUE) ||
11902c9e429eSbrutus 	    uri->count >= uri->resplen)) {
11912c9e429eSbrutus 		/*
11922c9e429eSbrutus 		 * No more data needed and no pending response
11932c9e429eSbrutus 		 * data or current data count >= response length
11942c9e429eSbrutus 		 * so close the URI processing for this so.
11952c9e429eSbrutus 		 */
11962c9e429eSbrutus 		nl7c_close(so);
11970f1702c5SYu Xiangning 		if (! (sti->sti_nl7c_flags & NL7C_SOPERSIST)) {
11982c9e429eSbrutus 			/* Not a persistent connection */
11990f1702c5SYu Xiangning 			sti->sti_nl7c_flags = 0;
12002c9e429eSbrutus 		}
12012c9e429eSbrutus 	}
12022c9e429eSbrutus 
12032c9e429eSbrutus 	return (0);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate fail:
12067c478bd9Sstevel@tonic-gate 	if (alloc != NULL) {
12077c478bd9Sstevel@tonic-gate 		kmem_free(alloc, sz);
12087c478bd9Sstevel@tonic-gate 	}
12090f1702c5SYu Xiangning 	sti->sti_nl7c_flags = 0;
12107c478bd9Sstevel@tonic-gate 	nl7c_urifree(so);
12112c9e429eSbrutus 
12122c9e429eSbrutus 	return (error);
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate /*
12167c478bd9Sstevel@tonic-gate  * Called to read data from file "*fp" at offset "*off" of length "*len"
12177c478bd9Sstevel@tonic-gate  * for a maximum of "*max_rem" bytes.
12187c478bd9Sstevel@tonic-gate  *
12197c478bd9Sstevel@tonic-gate  * On success a pointer to the kmem_alloc()ed file data is returned, "*off"
12207c478bd9Sstevel@tonic-gate  * and "*len" are updated for the acutal number of bytes read and "*max_rem"
12217c478bd9Sstevel@tonic-gate  * is updated with the number of bytes remaining to be read.
12227c478bd9Sstevel@tonic-gate  *
12237c478bd9Sstevel@tonic-gate  * Else, "NULL" is returned.
12247c478bd9Sstevel@tonic-gate  */
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate static char *
12272c9e429eSbrutus nl7c_readfile(file_t *fp, u_offset_t *off, int *len, int max, int *ret)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	vnode_t	*vp = fp->f_vnode;
12307c478bd9Sstevel@tonic-gate 	int	flg = 0;
12312c9e429eSbrutus 	size_t	size = MIN(*len, max);
12327c478bd9Sstevel@tonic-gate 	char	*data;
12337c478bd9Sstevel@tonic-gate 	int	error;
12347c478bd9Sstevel@tonic-gate 	uio_t	uio;
12357c478bd9Sstevel@tonic-gate 	iovec_t	iov;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, flg, NULL);
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	if (*off > MAXOFFSET_T) {
12407c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, flg, NULL);
12412c9e429eSbrutus 		*ret = EFBIG;
12427c478bd9Sstevel@tonic-gate 		return (NULL);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	if (*off + size > MAXOFFSET_T)
12467c478bd9Sstevel@tonic-gate 		size = (ssize32_t)(MAXOFFSET_T - *off);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	data = kmem_alloc(size, KM_SLEEP);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	iov.iov_base = data;
12517c478bd9Sstevel@tonic-gate 	iov.iov_len = size;
12527c478bd9Sstevel@tonic-gate 	uio.uio_loffset = *off;
12537c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
12547c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
12557c478bd9Sstevel@tonic-gate 	uio.uio_resid = size;
12567c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
12577c478bd9Sstevel@tonic-gate 	uio.uio_llimit = MAXOFFSET_T;
12587c478bd9Sstevel@tonic-gate 	uio.uio_fmode = fp->f_flag;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &uio, fp->f_flag, fp->f_cred, NULL);
12617c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, flg, NULL);
12622c9e429eSbrutus 	*ret = error;
12637c478bd9Sstevel@tonic-gate 	if (error) {
12647c478bd9Sstevel@tonic-gate 		kmem_free(data, size);
12657c478bd9Sstevel@tonic-gate 		return (NULL);
12667c478bd9Sstevel@tonic-gate 	}
12677c478bd9Sstevel@tonic-gate 	*len = size;
12687c478bd9Sstevel@tonic-gate 	*off += size;
12697c478bd9Sstevel@tonic-gate 	return (data);
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate /*
12732c9e429eSbrutus  * Called to sink application response sendfilev, as with nl7c_data() above
12742c9e429eSbrutus  * all the data will be processed by NL7C unless there's an error.
12757c478bd9Sstevel@tonic-gate  */
12767c478bd9Sstevel@tonic-gate 
12772c9e429eSbrutus int
12782c9e429eSbrutus nl7c_sendfilev(struct sonode *so, u_offset_t *fileoff, sendfilevec_t *sfvp,
12792c9e429eSbrutus 	int sfvc, ssize_t *xfer)
12807c478bd9Sstevel@tonic-gate {
12810f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
12820f1702c5SYu Xiangning 	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
12837c478bd9Sstevel@tonic-gate 	file_t		*fp = NULL;
12847c478bd9Sstevel@tonic-gate 	vnode_t		*vp = NULL;
12857c478bd9Sstevel@tonic-gate 	char		*data = NULL;
12862c9e429eSbrutus 	u_offset_t	off;
12877c478bd9Sstevel@tonic-gate 	int		len;
12882c9e429eSbrutus 	int		cnt;
12897c478bd9Sstevel@tonic-gate 	int		total_count = 0;
12902c9e429eSbrutus 	char		*alloc;
12917c478bd9Sstevel@tonic-gate 	uri_rd_t	*rdp;
12922c9e429eSbrutus 	int		max;
12932c9e429eSbrutus 	int		perror;
12947c478bd9Sstevel@tonic-gate 	int		error = 0;
12952c9e429eSbrutus 	boolean_t	first = B_TRUE;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	nl7c_uri_sendfilev++;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (uri == NULL) {
13007c478bd9Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
13010f1702c5SYu Xiangning 		sti->sti_nl7c_flags = 0;
13027c478bd9Sstevel@tonic-gate 		nl7c_uri_NULL2++;
13032c9e429eSbrutus 		return (0);
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13060f1702c5SYu Xiangning 	if (sti->sti_nl7c_flags & NL7C_WAITWRITE)
13070f1702c5SYu Xiangning 		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	while (sfvc-- > 0) {
13107c478bd9Sstevel@tonic-gate 		/*
13117c478bd9Sstevel@tonic-gate 		 * off - the current sfv read file offset or user address.
13127c478bd9Sstevel@tonic-gate 		 *
13132c9e429eSbrutus 		 * len - the current sfv length in bytes.
13147c478bd9Sstevel@tonic-gate 		 *
13152c9e429eSbrutus 		 * cnt - number of bytes kmem_alloc()ed.
13167c478bd9Sstevel@tonic-gate 		 *
13172c9e429eSbrutus 		 * alloc - the kmem_alloc()ed buffer of size "cnt".
13182c9e429eSbrutus 		 *
13192c9e429eSbrutus 		 * data - copy of "alloc" used for post alloc references.
13207c478bd9Sstevel@tonic-gate 		 *
13217c478bd9Sstevel@tonic-gate 		 * fp - the current sfv file_t pointer.
13227c478bd9Sstevel@tonic-gate 		 *
13237c478bd9Sstevel@tonic-gate 		 * vp - the current "*vp" vnode_t pointer.
13247c478bd9Sstevel@tonic-gate 		 *
13257c478bd9Sstevel@tonic-gate 		 * Note, for "data" and "fp" and "vp" a NULL value is used
13267c478bd9Sstevel@tonic-gate 		 * when not allocated such that the common failure path "fail"
13277c478bd9Sstevel@tonic-gate 		 * is used.
13287c478bd9Sstevel@tonic-gate 		 */
13297c478bd9Sstevel@tonic-gate 		off = sfvp->sfv_off;
13307c478bd9Sstevel@tonic-gate 		len = sfvp->sfv_len;
13312c9e429eSbrutus 		cnt = len;
1332e116a42fSPrakash Jalan 
1333e116a42fSPrakash Jalan 		if (len == 0) {
1334e116a42fSPrakash Jalan 			sfvp++;
1335e116a42fSPrakash Jalan 			continue;
1336e116a42fSPrakash Jalan 		}
1337e116a42fSPrakash Jalan 
13382c9e429eSbrutus 		if (sfvp->sfv_fd == SFV_FD_SELF) {
13397c478bd9Sstevel@tonic-gate 			/*
13402c9e429eSbrutus 			 * User memory, copyin() all the bytes.
13417c478bd9Sstevel@tonic-gate 			 */
13422c9e429eSbrutus 			alloc = kmem_alloc(cnt, KM_SLEEP);
13432c9e429eSbrutus 			error = xcopyin((caddr_t)(uintptr_t)off, alloc, cnt);
13447c478bd9Sstevel@tonic-gate 			if (error)
13457c478bd9Sstevel@tonic-gate 				goto fail;
13467c478bd9Sstevel@tonic-gate 		} else {
13477c478bd9Sstevel@tonic-gate 			/*
13482c9e429eSbrutus 			 * File descriptor, prefetch some bytes.
13497c478bd9Sstevel@tonic-gate 			 */
13502c9e429eSbrutus 			if ((fp = getf(sfvp->sfv_fd)) == NULL) {
13512c9e429eSbrutus 				error = EBADF;
13527c478bd9Sstevel@tonic-gate 				goto fail;
13532c9e429eSbrutus 			}
13542c9e429eSbrutus 			if ((fp->f_flag & FREAD) == 0) {
13552c9e429eSbrutus 				error = EACCES;
13567c478bd9Sstevel@tonic-gate 				goto fail;
13572c9e429eSbrutus 			}
13587c478bd9Sstevel@tonic-gate 			vp = fp->f_vnode;
13592c9e429eSbrutus 			if (vp->v_type != VREG) {
13602c9e429eSbrutus 				error = EINVAL;
13617c478bd9Sstevel@tonic-gate 				goto fail;
13622c9e429eSbrutus 			}
13637c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 			/* Read max_rem bytes from file for prefetch */
13667c478bd9Sstevel@tonic-gate 			if (nl7c_use_kmem) {
13672c9e429eSbrutus 				max = cnt;
13687c478bd9Sstevel@tonic-gate 			} else {
13692c9e429eSbrutus 				max = MAXBSIZE * nl7c_file_prefetch;
13707c478bd9Sstevel@tonic-gate 			}
13712c9e429eSbrutus 			alloc = nl7c_readfile(fp, &off, &cnt, max, &error);
13722c9e429eSbrutus 			if (alloc == NULL)
13737c478bd9Sstevel@tonic-gate 				goto fail;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 			releasef(sfvp->sfv_fd);
13767c478bd9Sstevel@tonic-gate 			fp = NULL;
13772c9e429eSbrutus 		}
13782c9e429eSbrutus 		URI_RD_ADD(uri, rdp, cnt, -1);
13792c9e429eSbrutus 		if (rdp == NULL) {
13802c9e429eSbrutus 			error = ENOMEM;
13817c478bd9Sstevel@tonic-gate 			goto fail;
13827c478bd9Sstevel@tonic-gate 		}
13832c9e429eSbrutus 		data = alloc;
13842c9e429eSbrutus 		alloc = NULL;
13857c478bd9Sstevel@tonic-gate 		rdp->data.kmem = data;
13862c9e429eSbrutus 		total_count += cnt;
13872c9e429eSbrutus 		if (uri->hash != URI_TEMP && total_count > nca_max_cache_size) {
13882c9e429eSbrutus 			uri_delete(uri);
13892c9e429eSbrutus 			uri->hash = URI_TEMP;
13902c9e429eSbrutus 		}
13912c9e429eSbrutus 
13922c9e429eSbrutus 		/* Response parse */
13932c9e429eSbrutus 		perror = nl7c_resp_parse(so, uri, data, len);
13942c9e429eSbrutus 
13952c9e429eSbrutus 		/* Send kmem data out the connection */
13962c9e429eSbrutus 		error = uri_rd_response(so, uri, rdp, first);
13972c9e429eSbrutus 
13982c9e429eSbrutus 		if (error)
13997c478bd9Sstevel@tonic-gate 			goto fail;
14002c9e429eSbrutus 
14012c9e429eSbrutus 		if (sfvp->sfv_fd != SFV_FD_SELF) {
14022c9e429eSbrutus 			/*
14032c9e429eSbrutus 			 * File descriptor, if any bytes left save vnode_t.
14042c9e429eSbrutus 			 */
14052c9e429eSbrutus 			if (len > cnt) {
14062c9e429eSbrutus 				/* More file data so add it */
14072c9e429eSbrutus 				URI_RD_ADD(uri, rdp, len - cnt, off);
14082c9e429eSbrutus 				if (rdp == NULL) {
14092c9e429eSbrutus 					error = ENOMEM;
14102c9e429eSbrutus 					goto fail;
14112c9e429eSbrutus 				}
14127c478bd9Sstevel@tonic-gate 				rdp->data.vnode = vp;
14132c9e429eSbrutus 
14142c9e429eSbrutus 				/* Send vnode data out the connection */
14152c9e429eSbrutus 				error = uri_rd_response(so, uri, rdp, first);
14167c478bd9Sstevel@tonic-gate 			} else {
14177c478bd9Sstevel@tonic-gate 				/* All file data fit in the prefetch */
14187c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
14197c478bd9Sstevel@tonic-gate 			}
14202c9e429eSbrutus 			*fileoff += len;
14217c478bd9Sstevel@tonic-gate 			vp = NULL;
14227c478bd9Sstevel@tonic-gate 		}
14232c9e429eSbrutus 		*xfer += len;
14247c478bd9Sstevel@tonic-gate 		sfvp++;
14252c9e429eSbrutus 
14262c9e429eSbrutus 		if (first)
14272c9e429eSbrutus 			first = B_FALSE;
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 	if (total_count > 0) {
14307c478bd9Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, total_count);
14317c478bd9Sstevel@tonic-gate 	}
14322c9e429eSbrutus 	if (perror == 0 &&
14332c9e429eSbrutus 	    ((uri->respclen == URI_LEN_NOVALUE &&
14342c9e429eSbrutus 	    uri->resplen == URI_LEN_NOVALUE) ||
14352c9e429eSbrutus 	    uri->count >= uri->resplen)) {
14362c9e429eSbrutus 		/*
14372c9e429eSbrutus 		 * No more data needed and no pending response
14382c9e429eSbrutus 		 * data or current data count >= response length
14392c9e429eSbrutus 		 * so close the URI processing for this so.
14402c9e429eSbrutus 		 */
14417c478bd9Sstevel@tonic-gate 		nl7c_close(so);
14420f1702c5SYu Xiangning 		if (! (sti->sti_nl7c_flags & NL7C_SOPERSIST)) {
14432c9e429eSbrutus 			/* Not a persistent connection */
14440f1702c5SYu Xiangning 			sti->sti_nl7c_flags = 0;
14457c478bd9Sstevel@tonic-gate 		}
14462c9e429eSbrutus 	}
14472c9e429eSbrutus 
14482c9e429eSbrutus 	return (0);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate fail:
14510f1702c5SYu Xiangning 	if (error == EPIPE)
14520f1702c5SYu Xiangning 		tsignal(curthread, SIGPIPE);
14530f1702c5SYu Xiangning 
14542c9e429eSbrutus 	if (alloc != NULL)
14557c478bd9Sstevel@tonic-gate 		kmem_free(data, len);
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	if (vp != NULL)
14587c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	if (fp != NULL)
14617c478bd9Sstevel@tonic-gate 		releasef(sfvp->sfv_fd);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	if (total_count > 0) {
14647c478bd9Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, total_count);
14657c478bd9Sstevel@tonic-gate 	}
14662c9e429eSbrutus 
14670f1702c5SYu Xiangning 	sti->sti_nl7c_flags = 0;
14687c478bd9Sstevel@tonic-gate 	nl7c_urifree(so);
14692c9e429eSbrutus 
14702c9e429eSbrutus 	return (error);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate /*
14747c478bd9Sstevel@tonic-gate  * Called for a socket which is closing or when an application has
14757c478bd9Sstevel@tonic-gate  * completed sending all the response data (i.e. for a persistent
14767c478bd9Sstevel@tonic-gate  * connection called once for each completed application response).
14777c478bd9Sstevel@tonic-gate  */
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate void
14807c478bd9Sstevel@tonic-gate nl7c_close(struct sonode *so)
14817c478bd9Sstevel@tonic-gate {
14820f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
14830f1702c5SYu Xiangning 	uri_desc_t 	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	if (uri == NULL) {
14867c478bd9Sstevel@tonic-gate 		/*
14877c478bd9Sstevel@tonic-gate 		 * No URI being processed so might be a listen()er
14887c478bd9Sstevel@tonic-gate 		 * if so do any cleanup, else nothing more to do.
14897c478bd9Sstevel@tonic-gate 		 */
14907c478bd9Sstevel@tonic-gate 		if (so->so_state & SS_ACCEPTCONN) {
14917c478bd9Sstevel@tonic-gate 			(void) nl7c_close_addr(so);
14927c478bd9Sstevel@tonic-gate 		}
14937c478bd9Sstevel@tonic-gate 		return;
14947c478bd9Sstevel@tonic-gate 	}
14950f1702c5SYu Xiangning 	sti->sti_nl7c_uri = NULL;
14967c478bd9Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
14977c478bd9Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
14987c478bd9Sstevel@tonic-gate 		uri->proc = NULL;
14997c478bd9Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
15007c478bd9Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
15017c478bd9Sstevel@tonic-gate 		}
15027c478bd9Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
15037c478bd9Sstevel@tonic-gate 		nl7c_uri_close++;
15047c478bd9Sstevel@tonic-gate 	} else {
15057c478bd9Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
15067c478bd9Sstevel@tonic-gate 		uri->proc = NULL;
15077c478bd9Sstevel@tonic-gate 		nl7c_uri_temp_close++;
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 	REF_RELE(uri);
15107c478bd9Sstevel@tonic-gate 	if (nl7c_uri_max > 0 && nl7c_uri_bytes > nl7c_uri_max) {
15117c478bd9Sstevel@tonic-gate 		nl7c_uri_reclaim();
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate /*
15167c478bd9Sstevel@tonic-gate  * The uri_segmap_t ref_t inactive function called on the last REF_RELE(),
15177c478bd9Sstevel@tonic-gate  * release the segmap mapping. Note, the uri_segmap_t will be freed by
15187c478bd9Sstevel@tonic-gate  * REF_RELE() on return.
15197c478bd9Sstevel@tonic-gate  */
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate void
15227c478bd9Sstevel@tonic-gate uri_segmap_inactive(uri_segmap_t *smp)
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate 	if (!segmap_kpm) {
15257c478bd9Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap, smp->base,
15267c478bd9Sstevel@tonic-gate 		    smp->len, F_SOFTUNLOCK, S_OTHER);
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 	(void) segmap_release(segkmap, smp->base, SM_DONTNEED);
15297c478bd9Sstevel@tonic-gate 	VN_RELE(smp->vp);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate  * The call-back for desballoc()ed mblk_t's, if a segmap mapped mblk_t
15342c9e429eSbrutus  * release the reference, one per desballoc() of a segmap page, if a rd_t
15352c9e429eSbrutus  * mapped mblk_t release the reference, one per desballoc() of a uri_desc_t,
15362c9e429eSbrutus  * last kmem free the uri_desb_t.
15377c478bd9Sstevel@tonic-gate  */
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate static void
15407c478bd9Sstevel@tonic-gate uri_desb_free(uri_desb_t *desb)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	if (desb->segmap != NULL) {
15437c478bd9Sstevel@tonic-gate 		REF_RELE(desb->segmap);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 	REF_RELE(desb->uri);
15467c478bd9Sstevel@tonic-gate 	kmem_cache_free(uri_desb_kmc, desb);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate  * Segmap map up to a page of a uri_rd_t file descriptor.
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate uri_segmap_t *
15547c478bd9Sstevel@tonic-gate uri_segmap_map(uri_rd_t *rdp, int bytes)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	uri_segmap_t	*segmap = kmem_cache_alloc(uri_segmap_kmc, KM_SLEEP);
15577c478bd9Sstevel@tonic-gate 	int		len = MIN(rdp->sz, MAXBSIZE);
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (len > bytes)
15607c478bd9Sstevel@tonic-gate 		len = bytes;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	REF_INIT(segmap, 1, uri_segmap_inactive, uri_segmap_kmc);
15637c478bd9Sstevel@tonic-gate 	segmap->len = len;
15647c478bd9Sstevel@tonic-gate 	VN_HOLD(rdp->data.vnode);
15657c478bd9Sstevel@tonic-gate 	segmap->vp = rdp->data.vnode;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	segmap->base = segmap_getmapflt(segkmap, segmap->vp, rdp->off, len,
15687c478bd9Sstevel@tonic-gate 	    segmap_kpm ? SM_FAULT : 0, S_READ);
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	if (segmap_fault(kas.a_hat, segkmap, segmap->base, len,
15717c478bd9Sstevel@tonic-gate 	    F_SOFTLOCK, S_READ) != 0) {
15727c478bd9Sstevel@tonic-gate 		REF_RELE(segmap);
15737c478bd9Sstevel@tonic-gate 		return (NULL);
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 	return (segmap);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate /*
15797c478bd9Sstevel@tonic-gate  * Chop up the kernel virtual memory area *data of size *sz bytes for
15807c478bd9Sstevel@tonic-gate  * a maximum of *bytes bytes into an besballoc()ed mblk_t chain using
15817c478bd9Sstevel@tonic-gate  * the given template uri_desb_t *temp of max_mblk bytes per.
15827c478bd9Sstevel@tonic-gate  *
15837c478bd9Sstevel@tonic-gate  * The values of *data, *sz, and *bytes are updated on return, the
15847c478bd9Sstevel@tonic-gate  * mblk_t chain is returned.
15857c478bd9Sstevel@tonic-gate  */
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate static mblk_t *
15887c478bd9Sstevel@tonic-gate uri_desb_chop(
15897c478bd9Sstevel@tonic-gate 	char 		**data,
15907c478bd9Sstevel@tonic-gate 	size_t		*sz,
15917c478bd9Sstevel@tonic-gate 	int 		*bytes,
15927c478bd9Sstevel@tonic-gate 	uri_desb_t 	*temp,
15937c478bd9Sstevel@tonic-gate 	int		max_mblk,
15947c478bd9Sstevel@tonic-gate 	char		*eoh,
15957c478bd9Sstevel@tonic-gate 	mblk_t		*persist
15967c478bd9Sstevel@tonic-gate )
15977c478bd9Sstevel@tonic-gate {
15987c478bd9Sstevel@tonic-gate 	char		*ldata = *data;
15997c478bd9Sstevel@tonic-gate 	size_t		lsz = *sz;
16007c478bd9Sstevel@tonic-gate 	int		lbytes = bytes ? *bytes : lsz;
16017c478bd9Sstevel@tonic-gate 	uri_desb_t	*desb;
16027c478bd9Sstevel@tonic-gate 	mblk_t		*mp = NULL;
16032c9e429eSbrutus 	mblk_t		*nmp, *pmp = NULL;
16047c478bd9Sstevel@tonic-gate 	int		msz;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (lbytes == 0 && lsz == 0)
16077c478bd9Sstevel@tonic-gate 		return (NULL);
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	while (lbytes > 0 && lsz > 0) {
16107c478bd9Sstevel@tonic-gate 		msz = MIN(lbytes, max_mblk);
16117c478bd9Sstevel@tonic-gate 		msz = MIN(msz, lsz);
16127c478bd9Sstevel@tonic-gate 		if (persist && eoh >= ldata && eoh < &ldata[msz]) {
16137c478bd9Sstevel@tonic-gate 			msz = (eoh - ldata);
16147c478bd9Sstevel@tonic-gate 			pmp = persist;
16157c478bd9Sstevel@tonic-gate 			persist = NULL;
16162c9e429eSbrutus 			if (msz == 0) {
16172c9e429eSbrutus 				nmp = pmp;
16182c9e429eSbrutus 				pmp = NULL;
16192c9e429eSbrutus 				goto zero;
16202c9e429eSbrutus 			}
16217c478bd9Sstevel@tonic-gate 		}
16227c478bd9Sstevel@tonic-gate 		desb = kmem_cache_alloc(uri_desb_kmc, KM_SLEEP);
16237c478bd9Sstevel@tonic-gate 		REF_HOLD(temp->uri);
16247c478bd9Sstevel@tonic-gate 		if (temp->segmap) {
16257c478bd9Sstevel@tonic-gate 			REF_HOLD(temp->segmap);
16267c478bd9Sstevel@tonic-gate 		}
16277c478bd9Sstevel@tonic-gate 		bcopy(temp, desb, sizeof (*desb));
16287c478bd9Sstevel@tonic-gate 		desb->frtn.free_arg = (caddr_t)desb;
16297c478bd9Sstevel@tonic-gate 		nmp = desballoc((uchar_t *)ldata, msz, BPRI_HI, &desb->frtn);
16307c478bd9Sstevel@tonic-gate 		if (nmp == NULL) {
16317c478bd9Sstevel@tonic-gate 			if (temp->segmap) {
16327c478bd9Sstevel@tonic-gate 				REF_RELE(temp->segmap);
16337c478bd9Sstevel@tonic-gate 			}
16347c478bd9Sstevel@tonic-gate 			REF_RELE(temp->uri);
16357c478bd9Sstevel@tonic-gate 			if (mp != NULL) {
16362c9e429eSbrutus 				mp->b_next = NULL;
16377c478bd9Sstevel@tonic-gate 				freemsg(mp);
16387c478bd9Sstevel@tonic-gate 			}
16392c9e429eSbrutus 			if (persist != NULL) {
16402c9e429eSbrutus 				freeb(persist);
16412c9e429eSbrutus 			}
16427c478bd9Sstevel@tonic-gate 			return (NULL);
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 		nmp->b_wptr += msz;
16452c9e429eSbrutus 	zero:
16467c478bd9Sstevel@tonic-gate 		if (mp != NULL) {
16472c9e429eSbrutus 			mp->b_next->b_cont = nmp;
16487c478bd9Sstevel@tonic-gate 		} else {
16497c478bd9Sstevel@tonic-gate 			mp = nmp;
16507c478bd9Sstevel@tonic-gate 		}
16512c9e429eSbrutus 		if (pmp != NULL) {
16522c9e429eSbrutus 			nmp->b_cont = pmp;
16532c9e429eSbrutus 			nmp = pmp;
16542c9e429eSbrutus 			pmp = NULL;
16552c9e429eSbrutus 		}
16562c9e429eSbrutus 		mp->b_next = nmp;
16577c478bd9Sstevel@tonic-gate 		ldata += msz;
16587c478bd9Sstevel@tonic-gate 		lsz -= msz;
16597c478bd9Sstevel@tonic-gate 		lbytes -= msz;
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	*data = ldata;
16627c478bd9Sstevel@tonic-gate 	*sz = lsz;
16637c478bd9Sstevel@tonic-gate 	if (bytes)
16647c478bd9Sstevel@tonic-gate 		*bytes = lbytes;
16657c478bd9Sstevel@tonic-gate 	return (mp);
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate /*
16697c478bd9Sstevel@tonic-gate  * Experimential noqwait (i.e. no canput()/qwait() checks), just send
16707c478bd9Sstevel@tonic-gate  * the entire mblk_t chain down without flow-control checks.
16717c478bd9Sstevel@tonic-gate  */
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate static int
16747c478bd9Sstevel@tonic-gate kstrwritempnoqwait(struct vnode *vp, mblk_t *mp)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate 	struct stdata *stp;
16777c478bd9Sstevel@tonic-gate 	int error = 0;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
16807c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	/* Fast check of flags before acquiring the lock */
16837c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
16847c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
16857c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
16867c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
16877c478bd9Sstevel@tonic-gate 		if (error != 0) {
16887c478bd9Sstevel@tonic-gate 			if (!(stp->sd_flag & STPLEX) &&
16897c478bd9Sstevel@tonic-gate 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
16907c478bd9Sstevel@tonic-gate 				error = EPIPE;
16917c478bd9Sstevel@tonic-gate 			}
16927c478bd9Sstevel@tonic-gate 			return (error);
16937c478bd9Sstevel@tonic-gate 		}
16947c478bd9Sstevel@tonic-gate 	}
16957c478bd9Sstevel@tonic-gate 	putnext(stp->sd_wrq, mp);
16967c478bd9Sstevel@tonic-gate 	return (0);
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate /*
17002c9e429eSbrutus  * Send the URI uri_desc_t *uri response uri_rd_t *rdp out the socket_t *so.
17017c478bd9Sstevel@tonic-gate  */
17027c478bd9Sstevel@tonic-gate 
17032c9e429eSbrutus static int
17042c9e429eSbrutus uri_rd_response(struct sonode *so,
17052c9e429eSbrutus     uri_desc_t *uri,
17062c9e429eSbrutus     uri_rd_t *rdp,
17072c9e429eSbrutus     boolean_t first)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	vnode_t		*vp = SOTOV(so);
17100f1702c5SYu Xiangning 	int		max_mblk = (int)vp->v_stream->sd_maxblk;
17117c478bd9Sstevel@tonic-gate 	int		wsz;
17127c478bd9Sstevel@tonic-gate 	mblk_t		*mp, *wmp, *persist;
17137c478bd9Sstevel@tonic-gate 	int		write_bytes;
17142c9e429eSbrutus 	uri_rd_t	rd;
17157c478bd9Sstevel@tonic-gate 	uri_desb_t	desb;
17167c478bd9Sstevel@tonic-gate 	uri_segmap_t	*segmap = NULL;
17177c478bd9Sstevel@tonic-gate 	char		*segmap_data;
17187c478bd9Sstevel@tonic-gate 	size_t		segmap_sz;
17192c9e429eSbrutus 	int		error;
17202c9e429eSbrutus 	int		fflg = ((so->so_state & SS_NDELAY) ? FNDELAY : 0) |
17212c9e429eSbrutus 	    ((so->so_state & SS_NONBLOCK) ? FNONBLOCK : 0);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	/* Initialize template uri_desb_t */
17257c478bd9Sstevel@tonic-gate 	desb.frtn.free_func = uri_desb_free;
17267c478bd9Sstevel@tonic-gate 	desb.frtn.free_arg = NULL;
17277c478bd9Sstevel@tonic-gate 	desb.uri = uri;
17287c478bd9Sstevel@tonic-gate 
17292c9e429eSbrutus 	/* Get a local copy of the rd_t */
17302c9e429eSbrutus 	bcopy(rdp, &rd, sizeof (rd));
17317c478bd9Sstevel@tonic-gate 	do {
17322c9e429eSbrutus 		if (first) {
17332c9e429eSbrutus 			/*
17342c9e429eSbrutus 			 * For first kstrwrite() enough data to get
17352c9e429eSbrutus 			 * things going, note non blocking version of
17362c9e429eSbrutus 			 * kstrwrite() will be used below.
17372c9e429eSbrutus 			 */
17382c9e429eSbrutus 			write_bytes = P2ROUNDUP((max_mblk * 4),
17392c9e429eSbrutus 			    MAXBSIZE * nl7c_file_prefetch);
17402c9e429eSbrutus 		} else {
17412c9e429eSbrutus 			if ((write_bytes = so->so_sndbuf) == 0)
17422c9e429eSbrutus 				write_bytes = vp->v_stream->sd_qn_maxpsz;
17432c9e429eSbrutus 			ASSERT(write_bytes > 0);
17442c9e429eSbrutus 			write_bytes = P2ROUNDUP(write_bytes, MAXBSIZE);
17452c9e429eSbrutus 		}
17462c9e429eSbrutus 		/*
17472c9e429eSbrutus 		 * Chop up to a write_bytes worth of data.
17482c9e429eSbrutus 		 */
17497c478bd9Sstevel@tonic-gate 		wmp = NULL;
17507c478bd9Sstevel@tonic-gate 		wsz = write_bytes;
17517c478bd9Sstevel@tonic-gate 		do {
17522c9e429eSbrutus 			if (rd.sz == 0)
17532c9e429eSbrutus 				break;
17547c478bd9Sstevel@tonic-gate 			if (rd.off == -1) {
17557c478bd9Sstevel@tonic-gate 				if (uri->eoh >= rd.data.kmem &&
17567c478bd9Sstevel@tonic-gate 				    uri->eoh < &rd.data.kmem[rd.sz]) {
17577c478bd9Sstevel@tonic-gate 					persist = nl7c_http_persist(so);
17587c478bd9Sstevel@tonic-gate 				} else {
17597c478bd9Sstevel@tonic-gate 					persist = NULL;
17607c478bd9Sstevel@tonic-gate 				}
17617c478bd9Sstevel@tonic-gate 				desb.segmap = NULL;
17627c478bd9Sstevel@tonic-gate 				mp = uri_desb_chop(&rd.data.kmem, &rd.sz,
17637c478bd9Sstevel@tonic-gate 				    &wsz, &desb, max_mblk, uri->eoh, persist);
17642c9e429eSbrutus 				if (mp == NULL) {
17652c9e429eSbrutus 					error = ENOMEM;
17667c478bd9Sstevel@tonic-gate 					goto invalidate;
17672c9e429eSbrutus 				}
17687c478bd9Sstevel@tonic-gate 			} else {
17697c478bd9Sstevel@tonic-gate 				if (segmap == NULL) {
17707c478bd9Sstevel@tonic-gate 					segmap = uri_segmap_map(&rd,
17717c478bd9Sstevel@tonic-gate 					    write_bytes);
17722c9e429eSbrutus 					if (segmap == NULL) {
17732c9e429eSbrutus 						error = ENOMEM;
17747c478bd9Sstevel@tonic-gate 						goto invalidate;
17752c9e429eSbrutus 					}
17767c478bd9Sstevel@tonic-gate 					desb.segmap = segmap;
17777c478bd9Sstevel@tonic-gate 					segmap_data = segmap->base;
17787c478bd9Sstevel@tonic-gate 					segmap_sz = segmap->len;
17797c478bd9Sstevel@tonic-gate 				}
17807c478bd9Sstevel@tonic-gate 				mp = uri_desb_chop(&segmap_data, &segmap_sz,
17817c478bd9Sstevel@tonic-gate 				    &wsz, &desb, max_mblk, NULL, NULL);
17822c9e429eSbrutus 				if (mp == NULL) {
17832c9e429eSbrutus 					error = ENOMEM;
17847c478bd9Sstevel@tonic-gate 					goto invalidate;
17852c9e429eSbrutus 				}
17867c478bd9Sstevel@tonic-gate 				if (segmap_sz == 0) {
17877c478bd9Sstevel@tonic-gate 					rd.sz -= segmap->len;
17887c478bd9Sstevel@tonic-gate 					rd.off += segmap->len;
17897c478bd9Sstevel@tonic-gate 					REF_RELE(segmap);
17907c478bd9Sstevel@tonic-gate 					segmap = NULL;
17917c478bd9Sstevel@tonic-gate 				}
17927c478bd9Sstevel@tonic-gate 			}
17937c478bd9Sstevel@tonic-gate 			if (wmp == NULL) {
17947c478bd9Sstevel@tonic-gate 				wmp = mp;
17957c478bd9Sstevel@tonic-gate 			} else {
17967c478bd9Sstevel@tonic-gate 				wmp->b_next->b_cont = mp;
17977c478bd9Sstevel@tonic-gate 				wmp->b_next = mp->b_next;
17987c478bd9Sstevel@tonic-gate 				mp->b_next = NULL;
17997c478bd9Sstevel@tonic-gate 			}
18002c9e429eSbrutus 		} while (wsz > 0 && rd.sz > 0);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 		wmp->b_next = NULL;
18037c478bd9Sstevel@tonic-gate 		if (first) {
18047c478bd9Sstevel@tonic-gate 			/* First kstrwrite(), use noqwait */
18052c9e429eSbrutus 			if ((error = kstrwritempnoqwait(vp, wmp)) != 0)
18067c478bd9Sstevel@tonic-gate 				goto invalidate;
18077c478bd9Sstevel@tonic-gate 			/*
18087c478bd9Sstevel@tonic-gate 			 * For the rest of the kstrwrite()s use SO_SNDBUF
18097c478bd9Sstevel@tonic-gate 			 * worth of data at a time, note these kstrwrite()s
18107c478bd9Sstevel@tonic-gate 			 * may (will) block one or more times.
18117c478bd9Sstevel@tonic-gate 			 */
18127c478bd9Sstevel@tonic-gate 			first = B_FALSE;
18137c478bd9Sstevel@tonic-gate 		} else {
18142c9e429eSbrutus 			if ((error = kstrwritemp(vp, wmp, fflg)) != 0) {
18152c9e429eSbrutus 				if (error == EAGAIN) {
18162c9e429eSbrutus 					nl7c_uri_rd_EAGAIN++;
18172c9e429eSbrutus 					if ((error =
18182c9e429eSbrutus 					    kstrwritempnoqwait(vp, wmp)) != 0)
18192c9e429eSbrutus 						goto invalidate;
18202c9e429eSbrutus 				} else
18217c478bd9Sstevel@tonic-gate 					goto invalidate;
18227c478bd9Sstevel@tonic-gate 			}
18232c9e429eSbrutus 		}
18242c9e429eSbrutus 	} while (rd.sz > 0);
18257c478bd9Sstevel@tonic-gate 
18262c9e429eSbrutus 	return (0);
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate invalidate:
18297c478bd9Sstevel@tonic-gate 	if (segmap) {
18307c478bd9Sstevel@tonic-gate 		REF_RELE(segmap);
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 	if (wmp)
18337c478bd9Sstevel@tonic-gate 		freemsg(wmp);
18342c9e429eSbrutus 
18352c9e429eSbrutus 	return (error);
18362c9e429eSbrutus }
18372c9e429eSbrutus 
18382c9e429eSbrutus /*
18392c9e429eSbrutus  * Send the URI uri_desc_t *uri response out the socket_t *so.
18402c9e429eSbrutus  */
18412c9e429eSbrutus 
18422c9e429eSbrutus static int
18432c9e429eSbrutus uri_response(struct sonode *so, uri_desc_t *uri)
18442c9e429eSbrutus {
18452c9e429eSbrutus 	uri_rd_t	*rdp = &uri->response;
18462c9e429eSbrutus 	boolean_t	first = B_TRUE;
18472c9e429eSbrutus 	int		error;
18482c9e429eSbrutus 
18492c9e429eSbrutus 	while (rdp != NULL) {
18502c9e429eSbrutus 		error = uri_rd_response(so, uri, rdp, first);
18512c9e429eSbrutus 		if (error != 0) {
18522c9e429eSbrutus 			goto invalidate;
18532c9e429eSbrutus 		}
18542c9e429eSbrutus 		first = B_FALSE;
18552c9e429eSbrutus 		rdp = rdp->next;
18562c9e429eSbrutus 	}
18572c9e429eSbrutus 	return (0);
18582c9e429eSbrutus 
18592c9e429eSbrutus invalidate:
1860b73114cbSAnil udupa 	if (uri->hash != URI_TEMP)
18617c478bd9Sstevel@tonic-gate 		uri_delete(uri);
18622c9e429eSbrutus 	return (error);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate /*
18667c478bd9Sstevel@tonic-gate  * The pchars[] array is indexed by a char to determine if it's a
18677c478bd9Sstevel@tonic-gate  * valid URI path component chararcter where:
18687c478bd9Sstevel@tonic-gate  *
18697c478bd9Sstevel@tonic-gate  *    pchar       = unreserved | escaped |
18707c478bd9Sstevel@tonic-gate  *                  ":" | "@" | "&" | "=" | "+" | "$" | ","
18717c478bd9Sstevel@tonic-gate  *
18727c478bd9Sstevel@tonic-gate  *    unreserved  = alphanum | mark
18737c478bd9Sstevel@tonic-gate  *
18747c478bd9Sstevel@tonic-gate  *    alphanum    = alpha | digit
18757c478bd9Sstevel@tonic-gate  *
18767c478bd9Sstevel@tonic-gate  *    alpha       = lowalpha | upalpha
18777c478bd9Sstevel@tonic-gate  *
18787c478bd9Sstevel@tonic-gate  *    lowalpha    = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
18797c478bd9Sstevel@tonic-gate  *                  "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
18807c478bd9Sstevel@tonic-gate  *                  "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
18817c478bd9Sstevel@tonic-gate  *                  "y" | "z"
18827c478bd9Sstevel@tonic-gate  *
18837c478bd9Sstevel@tonic-gate  *    upalpha     = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
18847c478bd9Sstevel@tonic-gate  *                  "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
18857c478bd9Sstevel@tonic-gate  *                  "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
18867c478bd9Sstevel@tonic-gate  *                  "Y" | "Z"
18877c478bd9Sstevel@tonic-gate  *
18887c478bd9Sstevel@tonic-gate  *    digit       = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
18897c478bd9Sstevel@tonic-gate  *                  "8" | "9"
18907c478bd9Sstevel@tonic-gate  *
18917c478bd9Sstevel@tonic-gate  *    mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
18927c478bd9Sstevel@tonic-gate  *
18937c478bd9Sstevel@tonic-gate  *    escaped     = "%" hex hex
18947c478bd9Sstevel@tonic-gate  *    hex         = digit | "A" | "B" | "C" | "D" | "E" | "F" |
18957c478bd9Sstevel@tonic-gate  *                  "a" | "b" | "c" | "d" | "e" | "f"
18967c478bd9Sstevel@tonic-gate  */
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate static char pchars[] = {
18997c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x07 */
19007c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x08 - 0x0F */
19017c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x17 */
19027c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x18 - 0x1F */
19037c478bd9Sstevel@tonic-gate     0, 1, 0, 0, 1, 1, 1, 1,	/* 0x20 - 0x27 */
19047c478bd9Sstevel@tonic-gate     0, 0, 1, 1, 1, 1, 1, 1,	/* 0x28 - 0x2F */
19057c478bd9Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x30 - 0x37 */
19067c478bd9Sstevel@tonic-gate     1, 1, 1, 0, 0, 1, 0, 0,	/* 0x38 - 0x3F */
19077c478bd9Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x47 */
19087c478bd9Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x48 - 0x4F */
19097c478bd9Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x57 */
19107c478bd9Sstevel@tonic-gate     1, 1, 1, 0, 0, 0, 0, 1,	/* 0x58 - 0x5F */
19117c478bd9Sstevel@tonic-gate     0, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x67 */
19127c478bd9Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x68 - 0x6F */
19137c478bd9Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x77 */
19147c478bd9Sstevel@tonic-gate     1, 1, 1, 0, 0, 0, 1, 0	/* 0x78 - 0x7F */
19157c478bd9Sstevel@tonic-gate };
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate #define	PCHARS_MASK 0x7F
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate /*
19207c478bd9Sstevel@tonic-gate  * This is the main L7 request message parse, we are called each time
19217c478bd9Sstevel@tonic-gate  * new data is availble for a socket, each time a single buffer of the
19227c478bd9Sstevel@tonic-gate  * entire message to date is given.
19237c478bd9Sstevel@tonic-gate  *
19247c478bd9Sstevel@tonic-gate  * Here we parse the request looking for the URI, parse it, and if a
19257c478bd9Sstevel@tonic-gate  * supported scheme call the scheme parser to commplete the parse of any
19267c478bd9Sstevel@tonic-gate  * headers which may further qualify the identity of the requested object
19277c478bd9Sstevel@tonic-gate  * then lookup it up in the URI hash.
19287c478bd9Sstevel@tonic-gate  *
19297c478bd9Sstevel@tonic-gate  * Return B_TRUE for more processing.
19307c478bd9Sstevel@tonic-gate  *
19317c478bd9Sstevel@tonic-gate  * Note, at this time the parser supports the generic message format as
19327c478bd9Sstevel@tonic-gate  * specified in RFC 822 with potentional limitations as specified in RFC
19337c478bd9Sstevel@tonic-gate  * 2616 for HTTP messages.
19347c478bd9Sstevel@tonic-gate  *
19357c478bd9Sstevel@tonic-gate  * Note, the caller supports an mblk_t chain, for now the parser(s)
19367c478bd9Sstevel@tonic-gate  * require the complete header in a single mblk_t. This is the common
19377c478bd9Sstevel@tonic-gate  * case and certainly for high performance environments, if at a future
19387c478bd9Sstevel@tonic-gate  * date mblk_t chains are important the parse can be reved to process
19397c478bd9Sstevel@tonic-gate  * mblk_t chains.
19407c478bd9Sstevel@tonic-gate  */
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate boolean_t
19432c9e429eSbrutus nl7c_parse(struct sonode *so, boolean_t nonblocking, boolean_t *ret)
19447c478bd9Sstevel@tonic-gate {
19450f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
19460f1702c5SYu Xiangning 	char	*cp = (char *)sti->sti_nl7c_rcv_mp->b_rptr;
19470f1702c5SYu Xiangning 	char	*ep = (char *)sti->sti_nl7c_rcv_mp->b_wptr;
19487c478bd9Sstevel@tonic-gate 	char	*get = "GET ";
19492c9e429eSbrutus 	char	*post = "POST ";
19507c478bd9Sstevel@tonic-gate 	char	c;
19517c478bd9Sstevel@tonic-gate 	char	*uris;
19522c9e429eSbrutus 	uri_desc_t *uri = NULL;
19537c478bd9Sstevel@tonic-gate 	uri_desc_t *ruri = NULL;
19542c9e429eSbrutus 	mblk_t	*reqmp;
19552c9e429eSbrutus 	uint32_t hv = 0;
19567c478bd9Sstevel@tonic-gate 
19570f1702c5SYu Xiangning 	if ((reqmp = dupb(sti->sti_nl7c_rcv_mp)) == NULL) {
19582c9e429eSbrutus 		nl7c_uri_pass_dupbfail++;
19592c9e429eSbrutus 		goto pass;
19602c9e429eSbrutus 	}
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * Allocate and initialize minimumal state for the request
19637c478bd9Sstevel@tonic-gate 	 * uri_desc_t, in the cache hit case this uri_desc_t will
19647c478bd9Sstevel@tonic-gate 	 * be freed.
19657c478bd9Sstevel@tonic-gate 	 */
19662c9e429eSbrutus 	uri = kmem_cache_alloc(nl7c_uri_kmc, KM_SLEEP);
19672c9e429eSbrutus 	REF_INIT(uri, 1, nl7c_uri_inactive, nl7c_uri_kmc);
19687c478bd9Sstevel@tonic-gate 	uri->hash = NULL;
19697c478bd9Sstevel@tonic-gate 	uri->tail = NULL;
19707c478bd9Sstevel@tonic-gate 	uri->scheme = NULL;
19717c478bd9Sstevel@tonic-gate 	uri->count = 0;
19722c9e429eSbrutus 	uri->reqmp = reqmp;
19732c9e429eSbrutus 
19747c478bd9Sstevel@tonic-gate 	/*
19757c478bd9Sstevel@tonic-gate 	 * Set request time to current time.
19767c478bd9Sstevel@tonic-gate 	 */
19770f1702c5SYu Xiangning 	sti->sti_nl7c_rtime = gethrestime_sec();
19782c9e429eSbrutus 
19797c478bd9Sstevel@tonic-gate 	/*
19807c478bd9Sstevel@tonic-gate 	 * Parse the Request-Line for the URI.
19817c478bd9Sstevel@tonic-gate 	 *
19827c478bd9Sstevel@tonic-gate 	 * For backwards HTTP version compatable reasons skip any leading
19837c478bd9Sstevel@tonic-gate 	 * CRLF (or CR or LF) line terminator(s) preceding Request-Line.
19847c478bd9Sstevel@tonic-gate 	 */
19857c478bd9Sstevel@tonic-gate 	while (cp < ep && (*cp == '\r' || *cp == '\n')) {
19867c478bd9Sstevel@tonic-gate 		cp++;
19877c478bd9Sstevel@tonic-gate 	}
19887c478bd9Sstevel@tonic-gate 	while (cp < ep && *get == *cp) {
19897c478bd9Sstevel@tonic-gate 		get++;
19907c478bd9Sstevel@tonic-gate 		cp++;
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 	if (*get != 0) {
19932c9e429eSbrutus 		/* Note a "GET", check for "POST" */
19942c9e429eSbrutus 		while (cp < ep && *post == *cp) {
19952c9e429eSbrutus 			post++;
19962c9e429eSbrutus 			cp++;
19972c9e429eSbrutus 		}
19982c9e429eSbrutus 		if (*post != 0) {
19997c478bd9Sstevel@tonic-gate 			if (cp == ep) {
20007c478bd9Sstevel@tonic-gate 				nl7c_uri_more_get++;
20017c478bd9Sstevel@tonic-gate 				goto more;
20027c478bd9Sstevel@tonic-gate 			}
20032c9e429eSbrutus 			/* Not a "GET" or a "POST", just pass */
20042c9e429eSbrutus 			nl7c_uri_pass_method++;
20057c478bd9Sstevel@tonic-gate 			goto pass;
20067c478bd9Sstevel@tonic-gate 		}
20072c9e429eSbrutus 		/* "POST", don't cache but still may want to parse */
20082c9e429eSbrutus 		uri->hash = URI_TEMP;
20092c9e429eSbrutus 	}
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * Skip over URI path char(s) and save start and past end pointers.
20127c478bd9Sstevel@tonic-gate 	 */
20137c478bd9Sstevel@tonic-gate 	uris = cp;
20147c478bd9Sstevel@tonic-gate 	while (cp < ep && (c = *cp) != ' ' && c != '\r') {
20157c478bd9Sstevel@tonic-gate 		if (c == '?') {
20167c478bd9Sstevel@tonic-gate 			/* Don't cache but still may want to parse */
20177c478bd9Sstevel@tonic-gate 			uri->hash = URI_TEMP;
20187c478bd9Sstevel@tonic-gate 		}
20192c9e429eSbrutus 		CHASH(hv, c);
20207c478bd9Sstevel@tonic-gate 		cp++;
20217c478bd9Sstevel@tonic-gate 	}
20227c478bd9Sstevel@tonic-gate 	if (c != '\r' && cp == ep) {
20237c478bd9Sstevel@tonic-gate 		nl7c_uri_more_eol++;
20247c478bd9Sstevel@tonic-gate 		goto more;
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 	/*
20277c478bd9Sstevel@tonic-gate 	 * Request-Line URI parsed, pass the rest of the request on
20287c478bd9Sstevel@tonic-gate 	 * to the the http scheme parse.
20297c478bd9Sstevel@tonic-gate 	 */
20307c478bd9Sstevel@tonic-gate 	uri->path.cp = uris;
20317c478bd9Sstevel@tonic-gate 	uri->path.ep = cp;
20322c9e429eSbrutus 	uri->hvalue = hv;
20332c9e429eSbrutus 	if (! nl7c_http_request(&cp, ep, uri, so) || cp == NULL) {
20347c478bd9Sstevel@tonic-gate 		/*
20352c9e429eSbrutus 		 * Parse not successful or pass on request, the pointer
20362c9e429eSbrutus 		 * to the parse pointer "cp" is overloaded such that ! NULL
20372c9e429eSbrutus 		 * for more data and NULL for bad parse of request or pass.
20387c478bd9Sstevel@tonic-gate 		 */
20397c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
20407c478bd9Sstevel@tonic-gate 			nl7c_uri_more_http++;
20417c478bd9Sstevel@tonic-gate 			goto more;
20427c478bd9Sstevel@tonic-gate 		}
20437c478bd9Sstevel@tonic-gate 		nl7c_uri_pass_http++;
20447c478bd9Sstevel@tonic-gate 		goto pass;
20457c478bd9Sstevel@tonic-gate 	}
20462c9e429eSbrutus 	if (uri->nocache) {
20472c9e429eSbrutus 		uri->hash = URI_TEMP;
20482c9e429eSbrutus 		(void) uri_lookup(uri, B_FALSE, nonblocking);
20492c9e429eSbrutus 	} else if (uri->hash == URI_TEMP) {
20502c9e429eSbrutus 		uri->nocache = B_TRUE;
20512c9e429eSbrutus 		(void) uri_lookup(uri, B_FALSE, nonblocking);
20522c9e429eSbrutus 	}
20532c9e429eSbrutus 
20547c478bd9Sstevel@tonic-gate 	if (uri->hash == URI_TEMP) {
20550f1702c5SYu Xiangning 		if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
20567c478bd9Sstevel@tonic-gate 			/* Temporary URI so skip hash processing */
20577c478bd9Sstevel@tonic-gate 			nl7c_uri_request++;
20587c478bd9Sstevel@tonic-gate 			nl7c_uri_temp++;
20597c478bd9Sstevel@tonic-gate 			goto temp;
20607c478bd9Sstevel@tonic-gate 		}
20617c478bd9Sstevel@tonic-gate 		/* Not persistent so not interested in the response */
20627c478bd9Sstevel@tonic-gate 		nl7c_uri_pass_temp++;
20637c478bd9Sstevel@tonic-gate 		goto pass;
20647c478bd9Sstevel@tonic-gate 	}
20657c478bd9Sstevel@tonic-gate 	/*
20662c9e429eSbrutus 	 * Check the URI hash for a cached response, save the request
20672c9e429eSbrutus 	 * uri in case we need it below.
20687c478bd9Sstevel@tonic-gate 	 */
20697c478bd9Sstevel@tonic-gate 	ruri = uri;
20707c478bd9Sstevel@tonic-gate 	if ((uri = uri_lookup(uri, B_TRUE, nonblocking)) == NULL) {
20717c478bd9Sstevel@tonic-gate 		/*
20727c478bd9Sstevel@tonic-gate 		 * Failed to lookup due to nonblocking wait required,
20737c478bd9Sstevel@tonic-gate 		 * interrupted cv_wait_sig(), KM_NOSLEEP memory alloc
20747c478bd9Sstevel@tonic-gate 		 * failure, ... Just pass on this request.
20757c478bd9Sstevel@tonic-gate 		 */
20767c478bd9Sstevel@tonic-gate 		nl7c_uri_pass_addfail++;
20777c478bd9Sstevel@tonic-gate 		goto pass;
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 	nl7c_uri_request++;
20807c478bd9Sstevel@tonic-gate 	if (uri->response.sz > 0) {
20817c478bd9Sstevel@tonic-gate 		/*
20827c478bd9Sstevel@tonic-gate 		 * We have the response cached, update recv mblk rptr
20832c9e429eSbrutus 		 * to reflect the data consumed in parse.
20847c478bd9Sstevel@tonic-gate 		 */
20850f1702c5SYu Xiangning 		mblk_t	*mp = sti->sti_nl7c_rcv_mp;
20867c478bd9Sstevel@tonic-gate 
20872c9e429eSbrutus 		if (cp == (char *)mp->b_wptr) {
20880f1702c5SYu Xiangning 			sti->sti_nl7c_rcv_mp = mp->b_cont;
20892c9e429eSbrutus 			mp->b_cont = NULL;
20902c9e429eSbrutus 			freeb(mp);
20912c9e429eSbrutus 		} else {
20922c9e429eSbrutus 			mp->b_rptr = (unsigned char *)cp;
20932c9e429eSbrutus 		}
20942c9e429eSbrutus 		nl7c_uri_hit++;
20952c9e429eSbrutus 		/* If logging enabled log request */
20962c9e429eSbrutus 		if (nl7c_logd_enabled) {
20977c478bd9Sstevel@tonic-gate 			ipaddr_t faddr;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
21007c478bd9Sstevel@tonic-gate 				/* Only support IPv4 addrs */
21017c478bd9Sstevel@tonic-gate 				faddr = ((struct sockaddr_in *)
21020f1702c5SYu Xiangning 				    sti->sti_faddr_sa) ->sin_addr.s_addr;
21037c478bd9Sstevel@tonic-gate 			} else {
21047c478bd9Sstevel@tonic-gate 				faddr = 0;
21057c478bd9Sstevel@tonic-gate 			}
21062c9e429eSbrutus 			/* XXX need to pass response type, e.g. 200, 304 */
21070f1702c5SYu Xiangning 			nl7c_logd_log(ruri, uri, sti->sti_nl7c_rtime, faddr);
21082c9e429eSbrutus 		}
21095a7d610eSAnil udupa 
21105a7d610eSAnil udupa 		/* If conditional request check for substitute response */
21115a7d610eSAnil udupa 		if (ruri->conditional) {
21125a7d610eSAnil udupa 			uri = nl7c_http_cond(ruri, uri);
21135a7d610eSAnil udupa 		}
21145a7d610eSAnil udupa 
21152c9e429eSbrutus 		/*
21162c9e429eSbrutus 		 * Release reference on request URI, send the response out
21172c9e429eSbrutus 		 * the socket, release reference on response uri, set the
21182c9e429eSbrutus 		 * *ret value to B_TRUE to indicate request was consumed
21192c9e429eSbrutus 		 * then return B_FALSE to indcate no more data needed.
21202c9e429eSbrutus 		 */
21217c478bd9Sstevel@tonic-gate 		REF_RELE(ruri);
21222c9e429eSbrutus 		(void) uri_response(so, uri);
21237c478bd9Sstevel@tonic-gate 		REF_RELE(uri);
21247c478bd9Sstevel@tonic-gate 		*ret = B_TRUE;
21257c478bd9Sstevel@tonic-gate 		return (B_FALSE);
21267c478bd9Sstevel@tonic-gate 	}
21277c478bd9Sstevel@tonic-gate 	/*
21282c9e429eSbrutus 	 * Miss the cache, the request URI is in the cache waiting for
21292c9e429eSbrutus 	 * application write-side data to fill it.
21307c478bd9Sstevel@tonic-gate 	 */
21317c478bd9Sstevel@tonic-gate 	nl7c_uri_miss++;
21327c478bd9Sstevel@tonic-gate temp:
21332c9e429eSbrutus 	/*
21342c9e429eSbrutus 	 * A miss or temp URI for which response data is needed, link
21352c9e429eSbrutus 	 * uri to so and so to uri, set WAITWRITE in the so such that
21362c9e429eSbrutus 	 * read-side processing is suspended (so the next read() gets
21372c9e429eSbrutus 	 * the request data) until a write() is processed by NL7C.
21382c9e429eSbrutus 	 *
21390f1702c5SYu Xiangning 	 * Note, sti->sti_nl7c_uri now owns the REF_INIT() ref.
21402c9e429eSbrutus 	 */
21417c478bd9Sstevel@tonic-gate 	uri->proc = so;
21420f1702c5SYu Xiangning 	sti->sti_nl7c_uri = uri;
21430f1702c5SYu Xiangning 	sti->sti_nl7c_flags |= NL7C_WAITWRITE;
21447c478bd9Sstevel@tonic-gate 	*ret = B_FALSE;
21457c478bd9Sstevel@tonic-gate 	return (B_FALSE);
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate more:
21487c478bd9Sstevel@tonic-gate 	/* More data is needed, note fragmented recv not supported */
21497c478bd9Sstevel@tonic-gate 	nl7c_uri_more++;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate pass:
21527c478bd9Sstevel@tonic-gate 	/* Pass on this request */
21537c478bd9Sstevel@tonic-gate 	nl7c_uri_pass++;
21547c478bd9Sstevel@tonic-gate 	nl7c_uri_request++;
21557c478bd9Sstevel@tonic-gate 	if (ruri != NULL) {
21567c478bd9Sstevel@tonic-gate 		REF_RELE(ruri);
21577c478bd9Sstevel@tonic-gate 	}
21587c478bd9Sstevel@tonic-gate 	if (uri) {
21597c478bd9Sstevel@tonic-gate 		REF_RELE(uri);
21607c478bd9Sstevel@tonic-gate 	}
21610f1702c5SYu Xiangning 	sti->sti_nl7c_flags = 0;
21627c478bd9Sstevel@tonic-gate 	*ret = B_FALSE;
21637c478bd9Sstevel@tonic-gate 	return (B_FALSE);
21647c478bd9Sstevel@tonic-gate }
2165