xref: /onnv-gate/usr/src/uts/common/fs/sockfs/nl7curi.c (revision 8171:ddfed5b00ad1)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51974Sbrutus  * Common Development and Distribution License (the "License").
61974Sbrutus  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8171SPrakash.Jalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/strsubr.h>
270Sstevel@tonic-gate #include <sys/strsun.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <vm/seg_map.h>
310Sstevel@tonic-gate #include <vm/seg_kpm.h>
320Sstevel@tonic-gate #include <sys/condvar_impl.h>
330Sstevel@tonic-gate #include <sys/sendfile.h>
340Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
350Sstevel@tonic-gate #include <fs/sockfs/nl7curi.h>
360Sstevel@tonic-gate 
371974Sbrutus #include <inet/common.h>
381974Sbrutus #include <inet/ip.h>
391974Sbrutus #include <inet/ip6.h>
401974Sbrutus #include <inet/tcp.h>
411974Sbrutus #include <inet/led.h>
421974Sbrutus #include <inet/mi.h>
431974Sbrutus 
441974Sbrutus #include <inet/nca/ncadoorhdr.h>
451974Sbrutus #include <inet/nca/ncalogd.h>
461974Sbrutus #include <inet/nca/ncandd.h>
471974Sbrutus 
481974Sbrutus #include <sys/promif.h>
491974Sbrutus 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Some externs:
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
541974Sbrutus extern boolean_t	nl7c_logd_enabled;
551974Sbrutus extern void		nl7c_logd_log(uri_desc_t *, uri_desc_t *,
561974Sbrutus 			    time_t, ipaddr_t);
571974Sbrutus extern boolean_t	nl7c_close_addr(struct sonode *);
581974Sbrutus extern struct sonode	*nl7c_addr2portso(void *);
591974Sbrutus extern uri_desc_t	*nl7c_http_cond(uri_desc_t *, uri_desc_t *);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Various global tuneables:
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate clock_t		nl7c_uri_ttl = -1;	/* TTL in seconds (-1 == infinite) */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate boolean_t	nl7c_use_kmem = B_FALSE; /* Force use of kmem (no segmap) */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate uint64_t	nl7c_file_prefetch = 1; /* File cache prefetch pages */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate uint64_t	nl7c_uri_max = 0;	/* Maximum bytes (0 == infinite) */
720Sstevel@tonic-gate uint64_t	nl7c_uri_bytes = 0;	/* Bytes of kmem used by URIs */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
751974Sbrutus  * Locals:
761974Sbrutus  */
771974Sbrutus 
781974Sbrutus static int	uri_rd_response(struct sonode *, uri_desc_t *,
791974Sbrutus 		    uri_rd_t *, boolean_t);
801974Sbrutus static int	uri_response(struct sonode *, uri_desc_t *);
811974Sbrutus 
821974Sbrutus /*
831974Sbrutus  * HTTP scheme functions called from nl7chttp.c:
841974Sbrutus  */
851974Sbrutus 
861974Sbrutus boolean_t nl7c_http_request(char **, char *, uri_desc_t *, struct sonode *);
871974Sbrutus boolean_t nl7c_http_response(char **, char *, uri_desc_t *, struct sonode *);
881974Sbrutus boolean_t nl7c_http_cmp(void *, void *);
891974Sbrutus mblk_t *nl7c_http_persist(struct sonode *);
901974Sbrutus void nl7c_http_free(void *arg);
911974Sbrutus void nl7c_http_init(void);
921974Sbrutus 
931974Sbrutus /*
940Sstevel@tonic-gate  * Counters that need to move to kstat and/or be removed:
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate volatile uint64_t nl7c_uri_request = 0;
980Sstevel@tonic-gate volatile uint64_t nl7c_uri_hit = 0;
990Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass = 0;
1000Sstevel@tonic-gate volatile uint64_t nl7c_uri_miss = 0;
1010Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp = 0;
1020Sstevel@tonic-gate volatile uint64_t nl7c_uri_more = 0;
1030Sstevel@tonic-gate volatile uint64_t nl7c_uri_data = 0;
1040Sstevel@tonic-gate volatile uint64_t nl7c_uri_sendfilev = 0;
1050Sstevel@tonic-gate volatile uint64_t nl7c_uri_reclaim_calls = 0;
1060Sstevel@tonic-gate volatile uint64_t nl7c_uri_reclaim_cnt = 0;
1070Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_urifail = 0;
1080Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_dupbfail = 0;
1090Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_get = 0;
1101974Sbrutus volatile uint64_t nl7c_uri_pass_method = 0;
1110Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_option = 0;
1120Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_eol = 0;
1130Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_http = 0;
1140Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_http = 0;
1150Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_addfail = 0;
1160Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_temp = 0;
1170Sstevel@tonic-gate volatile uint64_t nl7c_uri_expire = 0;
1181974Sbrutus volatile uint64_t nl7c_uri_purge = 0;
1190Sstevel@tonic-gate volatile uint64_t nl7c_uri_NULL1 = 0;
1200Sstevel@tonic-gate volatile uint64_t nl7c_uri_NULL2 = 0;
1210Sstevel@tonic-gate volatile uint64_t nl7c_uri_close = 0;
1220Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp_close = 0;
1230Sstevel@tonic-gate volatile uint64_t nl7c_uri_free = 0;
1240Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp_free = 0;
1251974Sbrutus volatile uint64_t nl7c_uri_temp_mk = 0;
1261974Sbrutus volatile uint64_t nl7c_uri_rd_EAGAIN = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Various kmem_cache_t's:
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate 
1321974Sbrutus kmem_cache_t *nl7c_uri_kmc;
1331974Sbrutus kmem_cache_t *nl7c_uri_rd_kmc;
1340Sstevel@tonic-gate static kmem_cache_t *uri_desb_kmc;
1350Sstevel@tonic-gate static kmem_cache_t *uri_segmap_kmc;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static void uri_kmc_reclaim(void *);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static void nl7c_uri_reclaim(void);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * The URI hash is a dynamically sized A/B bucket hash, when the current
1430Sstevel@tonic-gate  * hash's average bucket chain length exceeds URI_HASH_AVRG a new hash of
1440Sstevel@tonic-gate  * the next P2Ps[] size is created.
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  * All lookups are done in the current hash then the new hash (if any),
1470Sstevel@tonic-gate  * if there is a new has then when a current hash bucket chain is examined
1480Sstevel@tonic-gate  * any uri_desc_t members will be migrated to the new hash and when the
1490Sstevel@tonic-gate  * last uri_desc_t has been migrated then the new hash will become the
1500Sstevel@tonic-gate  * current and the previous current hash will be freed leaving a single
1510Sstevel@tonic-gate  * hash.
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * uri_hash_t - hash bucket (chain) type, contained in the uri_hash_ab[]
1540Sstevel@tonic-gate  * and can be accessed only after aquiring the uri_hash_access lock (for
1550Sstevel@tonic-gate  * READER or WRITER) then acquiring the lock uri_hash_t.lock, the uri_hash_t
1560Sstevel@tonic-gate  * and all linked uri_desc_t.hash members are protected. Note, a REF_HOLD()
1570Sstevel@tonic-gate  * is placed on all uri_desc_t uri_hash_t list members.
1580Sstevel@tonic-gate  *
1590Sstevel@tonic-gate  * uri_hash_access - rwlock for all uri_hash_* variables, READER for read
1600Sstevel@tonic-gate  * access and WRITER for write access. Note, WRITER is only required for
1610Sstevel@tonic-gate  * hash geometry changes.
1620Sstevel@tonic-gate  *
1630Sstevel@tonic-gate  * uri_hash_which - which uri_hash_ab[] is the current hash.
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  * uri_hash_n[] - the P2Ps[] index for each uri_hash_ab[].
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  * uri_hash_sz[] - the size for each uri_hash_ab[].
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * uri_hash_cnt[] - the total uri_desc_t members for each uri_hash_ab[].
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * uri_hash_overflow[] - the uri_hash_cnt[] for each uri_hash_ab[] when
1720Sstevel@tonic-gate  * a new uri_hash_ab[] needs to be created.
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * uri_hash_ab[] - the uri_hash_t entries.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * uri_hash_lru[] - the last uri_hash_ab[] walked for lru reclaim.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate typedef struct uri_hash_s {
1800Sstevel@tonic-gate 	struct uri_desc_s	*list;		/* List of uri_t(s) */
1810Sstevel@tonic-gate 	kmutex_t		lock;
1820Sstevel@tonic-gate } uri_hash_t;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #define	URI_HASH_AVRG	5	/* Desired average hash chain length */
1850Sstevel@tonic-gate #define	URI_HASH_N_INIT	9	/* P2Ps[] initial index */
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static krwlock_t	uri_hash_access;
1880Sstevel@tonic-gate static uint32_t		uri_hash_which = 0;
1890Sstevel@tonic-gate static uint32_t		uri_hash_n[2] = {URI_HASH_N_INIT, 0};
1900Sstevel@tonic-gate static uint32_t		uri_hash_sz[2] = {0, 0};
1910Sstevel@tonic-gate static uint32_t		uri_hash_cnt[2] = {0, 0};
1920Sstevel@tonic-gate static uint32_t		uri_hash_overflow[2] = {0, 0};
1930Sstevel@tonic-gate static uri_hash_t	*uri_hash_ab[2] = {NULL, NULL};
1940Sstevel@tonic-gate static uri_hash_t	*uri_hash_lru[2] = {NULL, NULL};
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * Primes for N of 3 - 24 where P is first prime less then (2^(N-1))+(2^(N-2))
1980Sstevel@tonic-gate  * these primes have been foud to be useful for prime sized hash tables.
1990Sstevel@tonic-gate  */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate static const int P2Ps[] = {
2020Sstevel@tonic-gate 	0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,
2030Sstevel@tonic-gate 	6143, 12281, 24571, 49139, 98299, 196597, 393209,
2040Sstevel@tonic-gate 	786431, 1572853, 3145721, 6291449, 12582893, 0};
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * Hash macros:
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  *    H2A(char *cp, char *ep, char c) - convert the escaped octet (ASCII)
2100Sstevel@tonic-gate  *    hex multichar of the format "%HH" pointeded to by *cp to a char and
2110Sstevel@tonic-gate  *    return in c, *ep points to past end of (char *), on return *cp will
2120Sstevel@tonic-gate  *    point to the last char consumed.
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  *    URI_HASH(unsigned hix, char *cp, char *ep) - hash the char(s) from
2150Sstevel@tonic-gate  *    *cp to *ep to the unsigned hix, cp nor ep are modified.
2160Sstevel@tonic-gate  *
2171974Sbrutus  *    URI_HASH_IX(unsigned hix, int which) - convert the hash value hix to
2181974Sbrutus  *    a hash index 0 - (uri_hash_sz[which] - 1).
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  *    URI_HASH_MIGRATE(from, hp, to) - migrate the uri_hash_t *hp list
2210Sstevel@tonic-gate  *    uri_desc_t members from hash from to hash to.
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  *    URI_HASH_UNLINK(cur, new, hp, puri, uri) - unlink the uri_desc_t
2240Sstevel@tonic-gate  *    *uri which is a member of the uri_hash_t *hp list with a previous
2250Sstevel@tonic-gate  *    list member of *puri for the uri_hash_ab[] cur. After unlinking
2260Sstevel@tonic-gate  *    check for cur hash empty, if so make new cur. Note, as this macro
2270Sstevel@tonic-gate  *    can change a hash chain it needs to be run under hash_access as
2280Sstevel@tonic-gate  *    RW_WRITER, futher as it can change the new hash to cur any access
2290Sstevel@tonic-gate  *    to the hash state must be done after either dropping locks and
2300Sstevel@tonic-gate  *    starting over or making sure the global state is consistent after
2310Sstevel@tonic-gate  *    as before.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #define	H2A(cp, ep, c) {						\
2350Sstevel@tonic-gate 	int	_h = 2;							\
2360Sstevel@tonic-gate 	int	_n = 0;							\
2370Sstevel@tonic-gate 	char	_hc;							\
2380Sstevel@tonic-gate 									\
2390Sstevel@tonic-gate 	while (_h > 0 && ++(cp) < (ep)) {				\
2400Sstevel@tonic-gate 		if (_h == 1)						\
2410Sstevel@tonic-gate 			_n *= 0x10;					\
2420Sstevel@tonic-gate 		_hc = *(cp);						\
2430Sstevel@tonic-gate 		if (_hc >= '0' && _hc <= '9')				\
2440Sstevel@tonic-gate 			_n += _hc - '0';				\
2450Sstevel@tonic-gate 		else if (_hc >= 'a' || _hc <= 'f')			\
2460Sstevel@tonic-gate 			_n += _hc - 'W';				\
2470Sstevel@tonic-gate 		else if (_hc >= 'A' || _hc <= 'F')			\
2480Sstevel@tonic-gate 			_n += _hc - '7';				\
2490Sstevel@tonic-gate 		_h--;							\
2500Sstevel@tonic-gate 	}								\
2510Sstevel@tonic-gate 	(c) = _n;							\
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #define	URI_HASH(hv, cp, ep) {						\
2550Sstevel@tonic-gate 	char	*_s = (cp);						\
2560Sstevel@tonic-gate 	char	_c;							\
2570Sstevel@tonic-gate 									\
2580Sstevel@tonic-gate 	while (_s < (ep)) {						\
2590Sstevel@tonic-gate 		if ((_c = *_s) == '%') {				\
2600Sstevel@tonic-gate 			H2A(_s, (ep), _c);				\
2610Sstevel@tonic-gate 		}							\
2621974Sbrutus 		CHASH(hv, _c);						\
2630Sstevel@tonic-gate 		_s++;							\
2640Sstevel@tonic-gate 	}								\
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2671974Sbrutus #define	URI_HASH_IX(hix, which) (hix) = (hix) % (uri_hash_sz[(which)])
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate #define	URI_HASH_MIGRATE(from, hp, to) {				\
2700Sstevel@tonic-gate 	uri_desc_t	*_nuri;						\
2710Sstevel@tonic-gate 	uint32_t	_nhix;						\
2720Sstevel@tonic-gate 	uri_hash_t	*_nhp;						\
2730Sstevel@tonic-gate 									\
2740Sstevel@tonic-gate 	mutex_enter(&(hp)->lock);					\
2750Sstevel@tonic-gate 	while ((_nuri = (hp)->list) != NULL) {				\
2760Sstevel@tonic-gate 		(hp)->list = _nuri->hash;				\
2770Sstevel@tonic-gate 		atomic_add_32(&uri_hash_cnt[(from)], -1);		\
2780Sstevel@tonic-gate 		atomic_add_32(&uri_hash_cnt[(to)], 1);			\
2791974Sbrutus 		_nhix = _nuri->hvalue;					\
2801974Sbrutus 		URI_HASH_IX(_nhix, to);					\
2810Sstevel@tonic-gate 		_nhp = &uri_hash_ab[(to)][_nhix];			\
2820Sstevel@tonic-gate 		mutex_enter(&_nhp->lock);				\
2830Sstevel@tonic-gate 		_nuri->hash = _nhp->list;				\
2840Sstevel@tonic-gate 		_nhp->list = _nuri;					\
2850Sstevel@tonic-gate 		_nuri->hit = 0;						\
2860Sstevel@tonic-gate 		mutex_exit(&_nhp->lock);				\
2870Sstevel@tonic-gate 	}								\
2880Sstevel@tonic-gate 	mutex_exit(&(hp)->lock);					\
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate #define	URI_HASH_UNLINK(cur, new, hp, puri, uri) {			\
2920Sstevel@tonic-gate 	if ((puri) != NULL) {						\
2930Sstevel@tonic-gate 		(puri)->hash = (uri)->hash;				\
2940Sstevel@tonic-gate 	} else {							\
2950Sstevel@tonic-gate 		(hp)->list = (uri)->hash;				\
2960Sstevel@tonic-gate 	}								\
2970Sstevel@tonic-gate 	if (atomic_add_32_nv(&uri_hash_cnt[(cur)], -1) == 0 &&		\
2980Sstevel@tonic-gate 	    uri_hash_ab[(new)] != NULL) {				\
2990Sstevel@tonic-gate 		kmem_free(uri_hash_ab[cur],				\
3000Sstevel@tonic-gate 		    sizeof (uri_hash_t) * uri_hash_sz[cur]);		\
3010Sstevel@tonic-gate 		uri_hash_ab[(cur)] = NULL;				\
3020Sstevel@tonic-gate 		uri_hash_lru[(cur)] = NULL;				\
3030Sstevel@tonic-gate 		uri_hash_which = (new);					\
3040Sstevel@tonic-gate 	} else {							\
3050Sstevel@tonic-gate 		uri_hash_lru[(cur)] = (hp);				\
3060Sstevel@tonic-gate 	}								\
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate void
3100Sstevel@tonic-gate nl7c_uri_init(void)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	uint32_t	cur = uri_hash_which;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	rw_init(&uri_hash_access, NULL, RW_DEFAULT, NULL);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	uri_hash_sz[cur] = P2Ps[URI_HASH_N_INIT];
3170Sstevel@tonic-gate 	uri_hash_overflow[cur] = P2Ps[URI_HASH_N_INIT] * URI_HASH_AVRG;
3180Sstevel@tonic-gate 	uri_hash_ab[cur] = kmem_zalloc(sizeof (uri_hash_t) * uri_hash_sz[cur],
3190Sstevel@tonic-gate 	    KM_SLEEP);
3200Sstevel@tonic-gate 	uri_hash_lru[cur] = uri_hash_ab[cur];
3210Sstevel@tonic-gate 
3221974Sbrutus 	nl7c_uri_kmc = kmem_cache_create("NL7C_uri_kmc", sizeof (uri_desc_t),
3231974Sbrutus 	    0, NULL, NULL, uri_kmc_reclaim, NULL, NULL, 0);
3240Sstevel@tonic-gate 
3251974Sbrutus 	nl7c_uri_rd_kmc = kmem_cache_create("NL7C_uri_rd_kmc",
3261974Sbrutus 	    sizeof (uri_rd_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	uri_desb_kmc = kmem_cache_create("NL7C_uri_desb_kmc",
3290Sstevel@tonic-gate 	    sizeof (uri_desb_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	uri_segmap_kmc = kmem_cache_create("NL7C_uri_segmap_kmc",
3320Sstevel@tonic-gate 	    sizeof (uri_segmap_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	nl7c_http_init();
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3371974Sbrutus #define	CV_SZ	16
3381974Sbrutus 
3391974Sbrutus void
3401974Sbrutus nl7c_mi_report_hash(mblk_t *mp)
3411974Sbrutus {
3421974Sbrutus 	uri_hash_t	*hp, *pend;
3431974Sbrutus 	uri_desc_t	*uri;
3441974Sbrutus 	uint32_t	cur;
3451974Sbrutus 	uint32_t	new;
3461974Sbrutus 	int		n, nz, tot;
3471974Sbrutus 	uint32_t	cv[CV_SZ + 1];
3481974Sbrutus 
3491974Sbrutus 	rw_enter(&uri_hash_access, RW_READER);
3501974Sbrutus 	cur = uri_hash_which;
3511974Sbrutus 	new = cur ? 0 : 1;
3521974Sbrutus next:
3531974Sbrutus 	for (n = 0; n <= CV_SZ; n++)
3541974Sbrutus 		cv[n] = 0;
3551974Sbrutus 	nz = 0;
3561974Sbrutus 	tot = 0;
3571974Sbrutus 	hp = &uri_hash_ab[cur][0];
3581974Sbrutus 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
3591974Sbrutus 	while (hp < pend) {
3601974Sbrutus 		n = 0;
3611974Sbrutus 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
3621974Sbrutus 			n++;
3631974Sbrutus 		}
3641974Sbrutus 		tot += n;
3651974Sbrutus 		if (n > 0)
3661974Sbrutus 			nz++;
3671974Sbrutus 		if (n > CV_SZ)
3681974Sbrutus 			n = CV_SZ;
3691974Sbrutus 		cv[n]++;
3701974Sbrutus 		hp++;
3711974Sbrutus 	}
3721974Sbrutus 
3731974Sbrutus 	(void) mi_mpprintf(mp, "\nHash=%s, Buckets=%d, "
3741974Sbrutus 	    "Avrg=%d\nCount by bucket:", cur != new ? "CUR" : "NEW",
3751974Sbrutus 	    uri_hash_sz[cur], nz != 0 ? ((tot * 10 + 5) / nz) / 10 : 0);
3761974Sbrutus 	(void) mi_mpprintf(mp, "Free=%d", cv[0]);
3771974Sbrutus 	for (n = 1; n < CV_SZ; n++) {
3781974Sbrutus 		int	pn = 0;
3791974Sbrutus 		char	pv[5];
3801974Sbrutus 		char	*pp = pv;
3811974Sbrutus 
3821974Sbrutus 		for (pn = n; pn < 1000; pn *= 10)
3831974Sbrutus 			*pp++ = ' ';
3841974Sbrutus 		*pp = 0;
3851974Sbrutus 		(void) mi_mpprintf(mp, "%s%d=%d", pv, n, cv[n]);
3861974Sbrutus 	}
3871974Sbrutus 	(void) mi_mpprintf(mp, "Long=%d", cv[CV_SZ]);
3881974Sbrutus 
3891974Sbrutus 	if (cur != new && uri_hash_ab[new] != NULL) {
3901974Sbrutus 		cur = new;
3911974Sbrutus 		goto next;
3921974Sbrutus 	}
3931974Sbrutus 	rw_exit(&uri_hash_access);
3941974Sbrutus }
3951974Sbrutus 
3961974Sbrutus void
3971974Sbrutus nl7c_mi_report_uri(mblk_t *mp)
3981974Sbrutus {
3991974Sbrutus 	uri_hash_t	*hp;
4001974Sbrutus 	uri_desc_t	*uri;
4011974Sbrutus 	uint32_t	cur;
4021974Sbrutus 	uint32_t	new;
4031974Sbrutus 	int		ix;
4041974Sbrutus 	int		ret;
4051974Sbrutus 	char		sc;
4061974Sbrutus 
4071974Sbrutus 	rw_enter(&uri_hash_access, RW_READER);
4081974Sbrutus 	cur = uri_hash_which;
4091974Sbrutus 	new = cur ? 0 : 1;
4101974Sbrutus next:
4111974Sbrutus 	for (ix = 0; ix < uri_hash_sz[cur]; ix++) {
4121974Sbrutus 		hp = &uri_hash_ab[cur][ix];
4131974Sbrutus 		mutex_enter(&hp->lock);
4141974Sbrutus 		uri = hp->list;
4151974Sbrutus 		while (uri != NULL) {
4161974Sbrutus 			sc = *(uri->path.ep);
4171974Sbrutus 			*(uri->path.ep) = 0;
4181974Sbrutus 			ret = mi_mpprintf(mp, "%s: %d %d %d",
4191974Sbrutus 			    uri->path.cp, (int)uri->resplen,
4201974Sbrutus 			    (int)uri->respclen, (int)uri->count);
4211974Sbrutus 			*(uri->path.ep) = sc;
4221974Sbrutus 			if (ret == -1) break;
4231974Sbrutus 			uri = uri->hash;
4241974Sbrutus 		}
4251974Sbrutus 		mutex_exit(&hp->lock);
4261974Sbrutus 		if (ret == -1) break;
4271974Sbrutus 	}
4281974Sbrutus 	if (ret != -1 && cur != new && uri_hash_ab[new] != NULL) {
4291974Sbrutus 		cur = new;
4301974Sbrutus 		goto next;
4311974Sbrutus 	}
4321974Sbrutus 	rw_exit(&uri_hash_access);
4331974Sbrutus }
4341974Sbrutus 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * The uri_desc_t ref_t inactive function called on the last REF_RELE(),
4370Sstevel@tonic-gate  * free all resources contained in the uri_desc_t. Note, the uri_desc_t
4380Sstevel@tonic-gate  * will be freed by REF_RELE() on return.
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate 
4411974Sbrutus void
4421974Sbrutus nl7c_uri_inactive(uri_desc_t *uri)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	int64_t	 bytes = 0;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (uri->tail) {
4470Sstevel@tonic-gate 		uri_rd_t *rdp = &uri->response;
4480Sstevel@tonic-gate 		uri_rd_t *free = NULL;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		while (rdp) {
4510Sstevel@tonic-gate 			if (rdp->off == -1) {
4520Sstevel@tonic-gate 				bytes += rdp->sz;
4530Sstevel@tonic-gate 				kmem_free(rdp->data.kmem, rdp->sz);
4540Sstevel@tonic-gate 			} else {
4550Sstevel@tonic-gate 				VN_RELE(rdp->data.vnode);
4560Sstevel@tonic-gate 			}
4570Sstevel@tonic-gate 			rdp = rdp->next;
4580Sstevel@tonic-gate 			if (free != NULL) {
4591974Sbrutus 				kmem_cache_free(nl7c_uri_rd_kmc, free);
4600Sstevel@tonic-gate 			}
4610Sstevel@tonic-gate 			free = rdp;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	if (bytes) {
4650Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, -bytes);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 	if (uri->scheme != NULL) {
4680Sstevel@tonic-gate 		nl7c_http_free(uri->scheme);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	if (uri->reqmp) {
4710Sstevel@tonic-gate 		freeb(uri->reqmp);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * The reclaim is called by the kmem subsystem when kmem is running
4770Sstevel@tonic-gate  * low. More work is needed to determine the best reclaim policy, for
4780Sstevel@tonic-gate  * now we just manipulate the nl7c_uri_max global maximum bytes threshold
4790Sstevel@tonic-gate  * value using a simple arithmetic backoff of the value every time this
4800Sstevel@tonic-gate  * function is called then call uri_reclaim() to enforce it.
4810Sstevel@tonic-gate  *
4820Sstevel@tonic-gate  * Note, this value remains in place and enforced for all subsequent
4830Sstevel@tonic-gate  * URI request/response processing.
4840Sstevel@tonic-gate  *
4850Sstevel@tonic-gate  * Note, nl7c_uri_max is currently initialized to 0 or infinite such that
4860Sstevel@tonic-gate  * the first call here set it to the current uri_bytes value then backoff
4870Sstevel@tonic-gate  * from there.
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  * XXX how do we determine when to increase nl7c_uri_max ???
4900Sstevel@tonic-gate  */
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate /*ARGSUSED*/
4930Sstevel@tonic-gate static void
4940Sstevel@tonic-gate uri_kmc_reclaim(void *arg)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate 	uint64_t new_max;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if ((new_max = nl7c_uri_max) == 0) {
4990Sstevel@tonic-gate 		/* Currently infinite, initialize to current bytes used */
5000Sstevel@tonic-gate 		nl7c_uri_max = nl7c_uri_bytes;
5010Sstevel@tonic-gate 		new_max = nl7c_uri_bytes;
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 	if (new_max > 1) {
5040Sstevel@tonic-gate 		/* Lower max_bytes to 93% of current value */
5050Sstevel@tonic-gate 		new_max >>= 1;			/* 50% */
5060Sstevel@tonic-gate 		new_max += (new_max >> 1);	/* 75% */
5070Sstevel@tonic-gate 		new_max += (new_max >> 2);	/* 93% */
5080Sstevel@tonic-gate 		if (new_max < nl7c_uri_max)
5090Sstevel@tonic-gate 			nl7c_uri_max = new_max;
5100Sstevel@tonic-gate 		else
5110Sstevel@tonic-gate 			nl7c_uri_max = 1;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 	nl7c_uri_reclaim();
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * Delete a uri_desc_t from the URI hash.
5180Sstevel@tonic-gate  */
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate static void
5210Sstevel@tonic-gate uri_delete(uri_desc_t *del)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	uint32_t	hix;
5240Sstevel@tonic-gate 	uri_hash_t	*hp;
5250Sstevel@tonic-gate 	uri_desc_t	*uri;
5260Sstevel@tonic-gate 	uri_desc_t	*puri;
5270Sstevel@tonic-gate 	uint32_t	cur;
5280Sstevel@tonic-gate 	uint32_t	new;
5290Sstevel@tonic-gate 
5301974Sbrutus 	ASSERT(del->hash != URI_TEMP);
5310Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
5320Sstevel@tonic-gate 	cur = uri_hash_which;
5330Sstevel@tonic-gate 	new = cur ? 0 : 1;
5340Sstevel@tonic-gate next:
5350Sstevel@tonic-gate 	puri = NULL;
5361974Sbrutus 	hix = del->hvalue;
5371974Sbrutus 	URI_HASH_IX(hix, cur);
5380Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
5390Sstevel@tonic-gate 	for (uri = hp->list; uri != NULL; uri = uri->hash) {
5400Sstevel@tonic-gate 		if (uri != del) {
5410Sstevel@tonic-gate 			puri = uri;
5420Sstevel@tonic-gate 			continue;
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 		/*
5450Sstevel@tonic-gate 		 * Found the URI, unlink from the hash chain,
5460Sstevel@tonic-gate 		 * drop locks, ref release it.
5470Sstevel@tonic-gate 		 */
5480Sstevel@tonic-gate 		URI_HASH_UNLINK(cur, new, hp, puri, uri);
5490Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
5500Sstevel@tonic-gate 		REF_RELE(uri);
5510Sstevel@tonic-gate 		return;
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
5540Sstevel@tonic-gate 		/*
5550Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
5560Sstevel@tonic-gate 		 * check the new hash next.
5570Sstevel@tonic-gate 		 */
5580Sstevel@tonic-gate 		cur = new;
5590Sstevel@tonic-gate 		goto next;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Add a uri_desc_t to the URI hash.
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate static void
5690Sstevel@tonic-gate uri_add(uri_desc_t *uri, krw_t rwlock, boolean_t nonblocking)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	uint32_t	hix;
5720Sstevel@tonic-gate 	uri_hash_t	*hp;
5730Sstevel@tonic-gate 	uint32_t	cur = uri_hash_which;
5740Sstevel@tonic-gate 	uint32_t	new = cur ? 0 : 1;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Caller of uri_add() must hold the uri_hash_access rwlock.
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	ASSERT((rwlock == RW_READER && RW_READ_HELD(&uri_hash_access)) ||
5800Sstevel@tonic-gate 	    (rwlock == RW_WRITER && RW_WRITE_HELD(&uri_hash_access)));
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * uri_add() always succeeds so add a hash ref to the URI now.
5830Sstevel@tonic-gate 	 */
5840Sstevel@tonic-gate 	REF_HOLD(uri);
5850Sstevel@tonic-gate again:
5861974Sbrutus 	hix = uri->hvalue;
5871974Sbrutus 	URI_HASH_IX(hix, cur);
5880Sstevel@tonic-gate 	if (uri_hash_ab[new] == NULL &&
5890Sstevel@tonic-gate 	    uri_hash_cnt[cur] < uri_hash_overflow[cur]) {
5900Sstevel@tonic-gate 		/*
5910Sstevel@tonic-gate 		 * Easy case, no new hash and current hasn't overflowed,
5920Sstevel@tonic-gate 		 * add URI to current hash and return.
5930Sstevel@tonic-gate 		 *
5940Sstevel@tonic-gate 		 * Note, the check for uri_hash_cnt[] above aren't done
5950Sstevel@tonic-gate 		 * atomictally, i.e. multiple threads can be in this code
5960Sstevel@tonic-gate 		 * as RW_READER and update the cnt[], this isn't a problem
5970Sstevel@tonic-gate 		 * as the check is only advisory.
5980Sstevel@tonic-gate 		 */
5990Sstevel@tonic-gate 	fast:
6000Sstevel@tonic-gate 		atomic_add_32(&uri_hash_cnt[cur], 1);
6010Sstevel@tonic-gate 		hp = &uri_hash_ab[cur][hix];
6020Sstevel@tonic-gate 		mutex_enter(&hp->lock);
6030Sstevel@tonic-gate 		uri->hash = hp->list;
6040Sstevel@tonic-gate 		hp->list = uri;
6050Sstevel@tonic-gate 		mutex_exit(&hp->lock);
6060Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
6070Sstevel@tonic-gate 		return;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	if (uri_hash_ab[new] == NULL) {
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * Need a new a or b hash, if not already RW_WRITER
6120Sstevel@tonic-gate 		 * try to upgrade our lock to writer.
6130Sstevel@tonic-gate 		 */
6140Sstevel@tonic-gate 		if (rwlock != RW_WRITER && ! rw_tryupgrade(&uri_hash_access)) {
6150Sstevel@tonic-gate 			/*
6160Sstevel@tonic-gate 			 * Upgrade failed, we can't simple exit and reenter
6170Sstevel@tonic-gate 			 * the lock as after the exit and before the reenter
6180Sstevel@tonic-gate 			 * the whole world can change so just wait for writer
6190Sstevel@tonic-gate 			 * then do everything again.
6200Sstevel@tonic-gate 			 */
6210Sstevel@tonic-gate 			if (nonblocking) {
6220Sstevel@tonic-gate 				/*
6230Sstevel@tonic-gate 				 * Can't block, use fast-path above.
6240Sstevel@tonic-gate 				 *
6250Sstevel@tonic-gate 				 * XXX should have a background thread to
6260Sstevel@tonic-gate 				 * handle new ab[] in this case so as to
6270Sstevel@tonic-gate 				 * not overflow the cur hash to much.
6280Sstevel@tonic-gate 				 */
6290Sstevel@tonic-gate 				goto fast;
6300Sstevel@tonic-gate 			}
6310Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
6320Sstevel@tonic-gate 			rwlock = RW_WRITER;
6330Sstevel@tonic-gate 			rw_enter(&uri_hash_access, rwlock);
6340Sstevel@tonic-gate 			cur = uri_hash_which;
6350Sstevel@tonic-gate 			new = cur ? 0 : 1;
6360Sstevel@tonic-gate 			goto again;
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 		rwlock = RW_WRITER;
6390Sstevel@tonic-gate 		if (uri_hash_ab[new] == NULL) {
6400Sstevel@tonic-gate 			/*
6410Sstevel@tonic-gate 			 * Still need a new hash, allocate and initialize
6420Sstevel@tonic-gate 			 * the new hash.
6430Sstevel@tonic-gate 			 */
6440Sstevel@tonic-gate 			uri_hash_n[new] = uri_hash_n[cur] + 1;
6450Sstevel@tonic-gate 			if (uri_hash_n[new] == 0) {
6460Sstevel@tonic-gate 				/*
6470Sstevel@tonic-gate 				 * No larger P2Ps[] value so use current,
6480Sstevel@tonic-gate 				 * i.e. 2 of the largest are better than 1 ?
6490Sstevel@tonic-gate 				 */
6500Sstevel@tonic-gate 				uri_hash_n[new] = uri_hash_n[cur];
6510Sstevel@tonic-gate 				cmn_err(CE_NOTE, "NL7C: hash index overflow");
6520Sstevel@tonic-gate 			}
6530Sstevel@tonic-gate 			uri_hash_sz[new] = P2Ps[uri_hash_n[new]];
6540Sstevel@tonic-gate 			ASSERT(uri_hash_cnt[new] == 0);
6550Sstevel@tonic-gate 			uri_hash_overflow[new] = uri_hash_sz[new] *
6560Sstevel@tonic-gate 			    URI_HASH_AVRG;
6570Sstevel@tonic-gate 			uri_hash_ab[new] = kmem_zalloc(sizeof (uri_hash_t) *
6580Sstevel@tonic-gate 			    uri_hash_sz[new], nonblocking ? KM_NOSLEEP :
6590Sstevel@tonic-gate 			    KM_SLEEP);
6600Sstevel@tonic-gate 			if (uri_hash_ab[new] == NULL) {
6610Sstevel@tonic-gate 				/*
6620Sstevel@tonic-gate 				 * Alloc failed, use fast-path above.
6630Sstevel@tonic-gate 				 *
6640Sstevel@tonic-gate 				 * XXX should have a background thread to
6650Sstevel@tonic-gate 				 * handle new ab[] in this case so as to
6660Sstevel@tonic-gate 				 * not overflow the cur hash to much.
6670Sstevel@tonic-gate 				 */
6680Sstevel@tonic-gate 				goto fast;
6690Sstevel@tonic-gate 			}
6700Sstevel@tonic-gate 			uri_hash_lru[new] = uri_hash_ab[new];
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * Hashed against current hash so migrate any current hash chain
6750Sstevel@tonic-gate 	 * members, if any.
6760Sstevel@tonic-gate 	 *
6770Sstevel@tonic-gate 	 * Note, the hash chain list can be checked for a non empty list
6780Sstevel@tonic-gate 	 * outside of the hash chain list lock as the hash chain struct
6790Sstevel@tonic-gate 	 * can't be destroyed while in the uri_hash_access rwlock, worst
6800Sstevel@tonic-gate 	 * case is that a non empty list is found and after acquiring the
6810Sstevel@tonic-gate 	 * lock another thread beats us to it (i.e. migrated the list).
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
6840Sstevel@tonic-gate 	if (hp->list != NULL) {
6850Sstevel@tonic-gate 		URI_HASH_MIGRATE(cur, hp, new);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * If new hash has overflowed before current hash has been
6890Sstevel@tonic-gate 	 * completely migrated then walk all current hash chains and
6900Sstevel@tonic-gate 	 * migrate list members now.
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	if (atomic_add_32_nv(&uri_hash_cnt[new], 1) >= uri_hash_overflow[new]) {
6930Sstevel@tonic-gate 		for (hix = 0; hix < uri_hash_sz[cur]; hix++) {
6940Sstevel@tonic-gate 			hp = &uri_hash_ab[cur][hix];
6950Sstevel@tonic-gate 			if (hp->list != NULL) {
6960Sstevel@tonic-gate 				URI_HASH_MIGRATE(cur, hp, new);
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	/*
7010Sstevel@tonic-gate 	 * Add URI to new hash.
7020Sstevel@tonic-gate 	 */
7031974Sbrutus 	hix = uri->hvalue;
7041974Sbrutus 	URI_HASH_IX(hix, new);
7050Sstevel@tonic-gate 	hp = &uri_hash_ab[new][hix];
7060Sstevel@tonic-gate 	mutex_enter(&hp->lock);
7070Sstevel@tonic-gate 	uri->hash = hp->list;
7080Sstevel@tonic-gate 	hp->list = uri;
7090Sstevel@tonic-gate 	mutex_exit(&hp->lock);
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * Last, check to see if last cur hash chain has been
7120Sstevel@tonic-gate 	 * migrated, if so free cur hash and make new hash cur.
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 	if (uri_hash_cnt[cur] == 0) {
7150Sstevel@tonic-gate 		/*
7160Sstevel@tonic-gate 		 * If we don't already hold the uri_hash_access rwlock for
7170Sstevel@tonic-gate 		 * RW_WRITE try to upgrade to RW_WRITE and if successful
7180Sstevel@tonic-gate 		 * check again and to see if still need to do the free.
7190Sstevel@tonic-gate 		 */
7200Sstevel@tonic-gate 		if ((rwlock == RW_WRITER || rw_tryupgrade(&uri_hash_access)) &&
7210Sstevel@tonic-gate 		    uri_hash_cnt[cur] == 0 && uri_hash_ab[new] != 0) {
7220Sstevel@tonic-gate 			kmem_free(uri_hash_ab[cur],
7230Sstevel@tonic-gate 			    sizeof (uri_hash_t) * uri_hash_sz[cur]);
7240Sstevel@tonic-gate 			uri_hash_ab[cur] = NULL;
7250Sstevel@tonic-gate 			uri_hash_lru[cur] = NULL;
7260Sstevel@tonic-gate 			uri_hash_which = new;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate  * Lookup a uri_desc_t in the URI hash, if found free the request uri_desc_t
7341974Sbrutus  * and return the found uri_desc_t with a REF_HOLD() placed on it. Else, if
7351974Sbrutus  * add B_TRUE use the request URI to create a new hash entry. Else if add
7361974Sbrutus  * B_FALSE ...
7370Sstevel@tonic-gate  */
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate static uri_desc_t *
7400Sstevel@tonic-gate uri_lookup(uri_desc_t *ruri, boolean_t add, boolean_t nonblocking)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	uint32_t	hix;
7430Sstevel@tonic-gate 	uri_hash_t	*hp;
7440Sstevel@tonic-gate 	uri_desc_t	*uri;
7450Sstevel@tonic-gate 	uri_desc_t	*puri;
7460Sstevel@tonic-gate 	uint32_t	cur;
7470Sstevel@tonic-gate 	uint32_t	new;
7480Sstevel@tonic-gate 	char		*rcp = ruri->path.cp;
7490Sstevel@tonic-gate 	char		*rep = ruri->path.ep;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate again:
7520Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_READER);
7530Sstevel@tonic-gate 	cur = uri_hash_which;
7540Sstevel@tonic-gate 	new = cur ? 0 : 1;
7550Sstevel@tonic-gate nexthash:
7560Sstevel@tonic-gate 	puri = NULL;
7571974Sbrutus 	hix = ruri->hvalue;
7581974Sbrutus 	URI_HASH_IX(hix, cur);
7590Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
7600Sstevel@tonic-gate 	mutex_enter(&hp->lock);
7610Sstevel@tonic-gate 	for (uri = hp->list; uri != NULL; uri = uri->hash) {
7620Sstevel@tonic-gate 		char	*ap = uri->path.cp;
7630Sstevel@tonic-gate 		char	*bp = rcp;
7640Sstevel@tonic-gate 		char	a, b;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		/* Compare paths */
7670Sstevel@tonic-gate 		while (bp < rep && ap < uri->path.ep) {
7680Sstevel@tonic-gate 			if ((a = *ap) == '%') {
7690Sstevel@tonic-gate 				/* Escaped hex multichar, convert it */
7700Sstevel@tonic-gate 				H2A(ap, uri->path.ep, a);
7710Sstevel@tonic-gate 			}
7720Sstevel@tonic-gate 			if ((b = *bp) == '%') {
7730Sstevel@tonic-gate 				/* Escaped hex multichar, convert it */
7740Sstevel@tonic-gate 				H2A(bp, rep, b);
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 			if (a != b) {
7770Sstevel@tonic-gate 				/* Char's don't match */
7780Sstevel@tonic-gate 				goto nexturi;
7790Sstevel@tonic-gate 			}
7800Sstevel@tonic-gate 			ap++;
7810Sstevel@tonic-gate 			bp++;
7820Sstevel@tonic-gate 		}
7830Sstevel@tonic-gate 		if (bp != rep || ap != uri->path.ep) {
7840Sstevel@tonic-gate 			/* Not same length */
7850Sstevel@tonic-gate 			goto nexturi;
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 		ap = uri->auth.cp;
7880Sstevel@tonic-gate 		bp = ruri->auth.cp;
7890Sstevel@tonic-gate 		if (ap != NULL) {
7900Sstevel@tonic-gate 			if (bp == NULL) {
7910Sstevel@tonic-gate 				/* URI has auth request URI doesn't */
7920Sstevel@tonic-gate 				goto nexturi;
7930Sstevel@tonic-gate 			}
7940Sstevel@tonic-gate 			while (bp < ruri->auth.ep && ap < uri->auth.ep) {
7950Sstevel@tonic-gate 				if ((a = *ap) == '%') {
7960Sstevel@tonic-gate 					/* Escaped hex multichar, convert it */
7970Sstevel@tonic-gate 					H2A(ap, uri->path.ep, a);
7980Sstevel@tonic-gate 				}
7990Sstevel@tonic-gate 				if ((b = *bp) == '%') {
8000Sstevel@tonic-gate 					/* Escaped hex multichar, convert it */
8010Sstevel@tonic-gate 					H2A(bp, rep, b);
8020Sstevel@tonic-gate 				}
8030Sstevel@tonic-gate 				if (a != b) {
8040Sstevel@tonic-gate 					/* Char's don't match */
8050Sstevel@tonic-gate 					goto nexturi;
8060Sstevel@tonic-gate 				}
8070Sstevel@tonic-gate 				ap++;
8080Sstevel@tonic-gate 				bp++;
8090Sstevel@tonic-gate 			}
8100Sstevel@tonic-gate 			if (bp != ruri->auth.ep || ap != uri->auth.ep) {
8110Sstevel@tonic-gate 				/* Not same length */
8120Sstevel@tonic-gate 				goto nexturi;
8130Sstevel@tonic-gate 			}
8140Sstevel@tonic-gate 		} else if (bp != NULL) {
8150Sstevel@tonic-gate 			/* URI doesn't have auth and request URI does */
8160Sstevel@tonic-gate 			goto nexturi;
8170Sstevel@tonic-gate 		}
8180Sstevel@tonic-gate 		/*
8191974Sbrutus 		 * Have a path/auth match so before any other processing
8201974Sbrutus 		 * of requested URI, check for expire or request no cache
8211974Sbrutus 		 * purge.
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		if (uri->expire >= 0 && uri->expire <= lbolt || ruri->nocache) {
8240Sstevel@tonic-gate 			/*
8250Sstevel@tonic-gate 			 * URI has expired or request specified to not use
8260Sstevel@tonic-gate 			 * the cached version, unlink the URI from the hash
8270Sstevel@tonic-gate 			 * chain, release all locks, release the hash ref
8280Sstevel@tonic-gate 			 * on the URI, and last look it up again.
8291974Sbrutus 			 *
8301974Sbrutus 			 * Note, this will cause all variants of the named
8311974Sbrutus 			 * URI to be purged.
8320Sstevel@tonic-gate 			 */
8330Sstevel@tonic-gate 			if (puri != NULL) {
8340Sstevel@tonic-gate 				puri->hash = uri->hash;
8350Sstevel@tonic-gate 			} else {
8360Sstevel@tonic-gate 				hp->list = uri->hash;
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8390Sstevel@tonic-gate 			atomic_add_32(&uri_hash_cnt[cur], -1);
8400Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8411974Sbrutus 			if (ruri->nocache)
8421974Sbrutus 				nl7c_uri_purge++;
8431974Sbrutus 			else
8441974Sbrutus 				nl7c_uri_expire++;
8450Sstevel@tonic-gate 			REF_RELE(uri);
8460Sstevel@tonic-gate 			goto again;
8470Sstevel@tonic-gate 		}
8481974Sbrutus 		if (uri->scheme != NULL) {
8491974Sbrutus 			/*
8501974Sbrutus 			 * URI has scheme private qualifier(s), if request
8511974Sbrutus 			 * URI doesn't or if no match skip this URI.
8521974Sbrutus 			 */
8531974Sbrutus 			if (ruri->scheme == NULL ||
8541974Sbrutus 			    ! nl7c_http_cmp(uri->scheme, ruri->scheme))
8551974Sbrutus 				goto nexturi;
8561974Sbrutus 		} else if (ruri->scheme != NULL) {
8571974Sbrutus 			/*
8581974Sbrutus 			 * URI doesn't have scheme private qualifiers but
8591974Sbrutus 			 * request URI does, no match, skip this URI.
8601974Sbrutus 			 */
8611974Sbrutus 			goto nexturi;
8621974Sbrutus 		}
8630Sstevel@tonic-gate 		/*
8641974Sbrutus 		 * Have a match, ready URI for return, first put a reference
8651974Sbrutus 		 * hold on the URI, if this URI is currently being processed
8661974Sbrutus 		 * then have to wait for the processing to be completed and
8671974Sbrutus 		 * redo the lookup, else return it.
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		REF_HOLD(uri);
8700Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
8710Sstevel@tonic-gate 		if (uri->proc != NULL) {
8720Sstevel@tonic-gate 			/* The URI is being processed, wait for completion */
8730Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8740Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8750Sstevel@tonic-gate 			if (! nonblocking &&
8760Sstevel@tonic-gate 			    cv_wait_sig(&uri->waiting, &uri->proclock)) {
8770Sstevel@tonic-gate 				/*
8780Sstevel@tonic-gate 				 * URI has been processed but things may
8790Sstevel@tonic-gate 				 * have changed while we were away so do
8800Sstevel@tonic-gate 				 * most everything again.
8810Sstevel@tonic-gate 				 */
8820Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8830Sstevel@tonic-gate 				REF_RELE(uri);
8840Sstevel@tonic-gate 				goto again;
8850Sstevel@tonic-gate 			} else {
8860Sstevel@tonic-gate 				/*
8870Sstevel@tonic-gate 				 * A nonblocking socket or an interrupted
8880Sstevel@tonic-gate 				 * cv_wait_sig() in the first case can't
8890Sstevel@tonic-gate 				 * block waiting for the processing of the
8900Sstevel@tonic-gate 				 * uri hash hit uri to complete, in both
8910Sstevel@tonic-gate 				 * cases just return failure to lookup.
8920Sstevel@tonic-gate 				 */
8930Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8940Sstevel@tonic-gate 				REF_RELE(uri);
8950Sstevel@tonic-gate 				return (NULL);
8960Sstevel@tonic-gate 			}
8970Sstevel@tonic-gate 		}
8981974Sbrutus 		mutex_exit(&uri->proclock);
8990Sstevel@tonic-gate 		uri->hit++;
9000Sstevel@tonic-gate 		mutex_exit(&hp->lock);
9010Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9020Sstevel@tonic-gate 		return (uri);
9030Sstevel@tonic-gate 	nexturi:
9040Sstevel@tonic-gate 		puri = uri;
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	mutex_exit(&hp->lock);
9070Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
9080Sstevel@tonic-gate 		/*
9090Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
9100Sstevel@tonic-gate 		 * check the new hash next.
9110Sstevel@tonic-gate 		 */
9120Sstevel@tonic-gate 		cur = new;
9130Sstevel@tonic-gate 		goto nexthash;
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate add:
9160Sstevel@tonic-gate 	if (! add) {
9171974Sbrutus 		/* Lookup only so return failure */
9180Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9190Sstevel@tonic-gate 		return (NULL);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 	/*
9220Sstevel@tonic-gate 	 * URI not hashed, finish intialization of the
9230Sstevel@tonic-gate 	 * request URI, add it to the hash, return it.
9240Sstevel@tonic-gate 	 */
9250Sstevel@tonic-gate 	ruri->hit = 0;
9260Sstevel@tonic-gate 	ruri->expire = -1;
9270Sstevel@tonic-gate 	ruri->response.sz = 0;
9281974Sbrutus 	ruri->proc = (struct sonode *)~NULL;
9290Sstevel@tonic-gate 	cv_init(&ruri->waiting, NULL, CV_DEFAULT, NULL);
9300Sstevel@tonic-gate 	mutex_init(&ruri->proclock, NULL, MUTEX_DEFAULT, NULL);
9310Sstevel@tonic-gate 	uri_add(ruri, RW_READER, nonblocking);
9320Sstevel@tonic-gate 	/* uri_add() has done rw_exit(&uri_hash_access) */
9330Sstevel@tonic-gate 	return (ruri);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate /*
9370Sstevel@tonic-gate  * Reclaim URIs until max cache size threshold has been reached.
9380Sstevel@tonic-gate  *
9390Sstevel@tonic-gate  * A CLOCK based reclaim modified with a history (hit counter) counter.
9400Sstevel@tonic-gate  */
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate static void
9430Sstevel@tonic-gate nl7c_uri_reclaim(void)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	uri_hash_t	*hp, *start, *pend;
9460Sstevel@tonic-gate 	uri_desc_t	*uri;
9470Sstevel@tonic-gate 	uri_desc_t	*puri;
9480Sstevel@tonic-gate 	uint32_t	cur;
9490Sstevel@tonic-gate 	uint32_t	new;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	nl7c_uri_reclaim_calls++;
9520Sstevel@tonic-gate again:
9530Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
9540Sstevel@tonic-gate 	cur = uri_hash_which;
9550Sstevel@tonic-gate 	new = cur ? 0 : 1;
9560Sstevel@tonic-gate next:
9570Sstevel@tonic-gate 	hp = uri_hash_lru[cur];
9580Sstevel@tonic-gate 	start = hp;
9590Sstevel@tonic-gate 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
9600Sstevel@tonic-gate 	while (nl7c_uri_bytes > nl7c_uri_max) {
9610Sstevel@tonic-gate 		puri = NULL;
9620Sstevel@tonic-gate 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
9630Sstevel@tonic-gate 			if (uri->hit != 0) {
9640Sstevel@tonic-gate 				/*
9650Sstevel@tonic-gate 				 * Decrement URI activity counter and skip.
9660Sstevel@tonic-gate 				 */
9670Sstevel@tonic-gate 				uri->hit--;
9680Sstevel@tonic-gate 				puri = uri;
9690Sstevel@tonic-gate 				continue;
9700Sstevel@tonic-gate 			}
9710Sstevel@tonic-gate 			if (uri->proc != NULL) {
9720Sstevel@tonic-gate 				/*
9730Sstevel@tonic-gate 				 * Currently being processed by a socket, skip.
9740Sstevel@tonic-gate 				 */
9750Sstevel@tonic-gate 				continue;
9760Sstevel@tonic-gate 			}
9770Sstevel@tonic-gate 			/*
9780Sstevel@tonic-gate 			 * Found a candidate, no hit(s) since added or last
9790Sstevel@tonic-gate 			 * reclaim pass, unlink from it's hash chain, update
9800Sstevel@tonic-gate 			 * lru scan pointer, drop lock, ref release it.
9810Sstevel@tonic-gate 			 */
9820Sstevel@tonic-gate 			URI_HASH_UNLINK(cur, new, hp, puri, uri);
9830Sstevel@tonic-gate 			if (cur == uri_hash_which) {
9840Sstevel@tonic-gate 				if (++hp == pend) {
9850Sstevel@tonic-gate 					/* Wrap pointer */
9860Sstevel@tonic-gate 					hp = uri_hash_ab[cur];
9870Sstevel@tonic-gate 				}
9880Sstevel@tonic-gate 				uri_hash_lru[cur] = hp;
9890Sstevel@tonic-gate 			}
9900Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
9910Sstevel@tonic-gate 			REF_RELE(uri);
9920Sstevel@tonic-gate 			nl7c_uri_reclaim_cnt++;
9930Sstevel@tonic-gate 			goto again;
9940Sstevel@tonic-gate 		}
9950Sstevel@tonic-gate 		if (++hp == pend) {
9960Sstevel@tonic-gate 			/* Wrap pointer */
9970Sstevel@tonic-gate 			hp = uri_hash_ab[cur];
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 		if (hp == start) {
10000Sstevel@tonic-gate 			if (cur != new && uri_hash_ab[new] != NULL) {
10010Sstevel@tonic-gate 				/*
10020Sstevel@tonic-gate 				 * Done with the current hash and have a
10030Sstevel@tonic-gate 				 * new hash so check the new hash next.
10040Sstevel@tonic-gate 				 */
10050Sstevel@tonic-gate 				cur = new;
10060Sstevel@tonic-gate 				goto next;
10070Sstevel@tonic-gate 			}
10080Sstevel@tonic-gate 		}
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate /*
10140Sstevel@tonic-gate  * Called for a socket which is being freed prior to close, e.g. errored.
10150Sstevel@tonic-gate  */
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate void
10180Sstevel@tonic-gate nl7c_urifree(struct sonode *so)
10190Sstevel@tonic-gate {
10200Sstevel@tonic-gate 	uri_desc_t *uri = (uri_desc_t *)so->so_nl7c_uri;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	so->so_nl7c_uri = NULL;
10230Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
10240Sstevel@tonic-gate 		uri_delete(uri);
10250Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
10260Sstevel@tonic-gate 		uri->proc = NULL;
10270Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
10280Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
10310Sstevel@tonic-gate 		nl7c_uri_free++;
10320Sstevel@tonic-gate 	} else {
10330Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
10340Sstevel@tonic-gate 		uri->proc = NULL;
10350Sstevel@tonic-gate 		nl7c_uri_temp_free++;
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 	REF_RELE(uri);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate /*
10411974Sbrutus  * ...
10421974Sbrutus  *
10431974Sbrutus  *	< 0	need more data
10441974Sbrutus  *
10451974Sbrutus  *	  0	parse complete
10461974Sbrutus  *
10471974Sbrutus  *	> 0	parse error
10480Sstevel@tonic-gate  */
10490Sstevel@tonic-gate 
10501974Sbrutus volatile uint64_t nl7c_resp_pfail = 0;
10511974Sbrutus volatile uint64_t nl7c_resp_ntemp = 0;
10521974Sbrutus volatile uint64_t nl7c_resp_pass = 0;
10530Sstevel@tonic-gate 
10541974Sbrutus static int
10551974Sbrutus nl7c_resp_parse(struct sonode *so, uri_desc_t *uri, char *data, int sz)
10561974Sbrutus {
10571974Sbrutus 	if (! nl7c_http_response(&data, &data[sz], uri, so)) {
10581974Sbrutus 		if (data == NULL) {
10591974Sbrutus 			/* Parse fail */
10601974Sbrutus 			goto pfail;
10611974Sbrutus 		}
10621974Sbrutus 		/* More data */
10631974Sbrutus 		data = NULL;
10641974Sbrutus 	} else if (data == NULL) {
10651974Sbrutus 		goto pass;
10661974Sbrutus 	}
10671974Sbrutus 	if (uri->hash != URI_TEMP && uri->nocache) {
10681974Sbrutus 		/*
10691974Sbrutus 		 * After response parse now no cache,
10701974Sbrutus 		 * delete it from cache, wakeup any
10711974Sbrutus 		 * waiters on this URI, make URI_TEMP.
10721974Sbrutus 		 */
10731974Sbrutus 		uri_delete(uri);
10741974Sbrutus 		mutex_enter(&uri->proclock);
10751974Sbrutus 		if (CV_HAS_WAITERS(&uri->waiting)) {
10761974Sbrutus 			cv_broadcast(&uri->waiting);
10771974Sbrutus 		}
10781974Sbrutus 		mutex_exit(&uri->proclock);
10791974Sbrutus 		uri->hash = URI_TEMP;
10801974Sbrutus 		nl7c_uri_temp_mk++;
10811974Sbrutus 	}
10821974Sbrutus 	if (data == NULL) {
10831974Sbrutus 		/* More data needed */
10841974Sbrutus 		return (-1);
10851974Sbrutus 	}
10861974Sbrutus 	/* Success */
10871974Sbrutus 	return (0);
10881974Sbrutus 
10891974Sbrutus pfail:
10901974Sbrutus 	nl7c_resp_pfail++;
10911974Sbrutus 	return (EINVAL);
10921974Sbrutus 
10931974Sbrutus pass:
10941974Sbrutus 	nl7c_resp_pass++;
10951974Sbrutus 	return (ENOTSUP);
10961974Sbrutus }
10971974Sbrutus 
10981974Sbrutus /*
10991974Sbrutus  * Called to sink application response data, the processing of the data
11001974Sbrutus  * is the same for a cached or temp URI (i.e. a URI for which we aren't
11011974Sbrutus  * going to cache the URI but want to parse it for detecting response
11021974Sbrutus  * data end such that for a persistent connection we can parse the next
11031974Sbrutus  * request).
11041974Sbrutus  *
11051974Sbrutus  * On return 0 is returned for sink success, > 0 on error, and < 0 on
11061974Sbrutus  * no so URI (note, data not sinked).
11071974Sbrutus  */
11081974Sbrutus 
11091974Sbrutus int
11101974Sbrutus nl7c_data(struct sonode *so, uio_t *uio)
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate 	uri_desc_t	*uri = (uri_desc_t *)so->so_nl7c_uri;
11131974Sbrutus 	iovec_t		*iov;
11141974Sbrutus 	int		cnt;
11151974Sbrutus 	int		sz = uio->uio_resid;
11161974Sbrutus 	char		*data, *alloc;
11170Sstevel@tonic-gate 	char		*bp;
11180Sstevel@tonic-gate 	uri_rd_t	*rdp;
11191974Sbrutus 	boolean_t	first;
11201974Sbrutus 	int		error, perror;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	nl7c_uri_data++;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	if (uri == NULL) {
11250Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
11260Sstevel@tonic-gate 		so->so_nl7c_flags = 0;
11270Sstevel@tonic-gate 		nl7c_uri_NULL1++;
11281974Sbrutus 		return (-1);
11291974Sbrutus 	}
11301974Sbrutus 
11311974Sbrutus 	if (so->so_nl7c_flags & NL7C_WAITWRITE) {
11321974Sbrutus 		so->so_nl7c_flags &= ~NL7C_WAITWRITE;
11331974Sbrutus 		first = B_TRUE;
11341974Sbrutus 	} else {
11351974Sbrutus 		first = B_FALSE;
11361974Sbrutus 	}
11371974Sbrutus 
11381974Sbrutus 	alloc = kmem_alloc(sz, KM_SLEEP);
11391974Sbrutus 	URI_RD_ADD(uri, rdp, sz, -1);
11401974Sbrutus 	if (rdp == NULL) {
11411974Sbrutus 		error = ENOMEM;
11421974Sbrutus 		goto fail;
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11451974Sbrutus 	if (uri->hash != URI_TEMP && uri->count > nca_max_cache_size) {
11461974Sbrutus 		uri_delete(uri);
11471974Sbrutus 		uri->hash = URI_TEMP;
11480Sstevel@tonic-gate 	}
11491974Sbrutus 	data = alloc;
11501974Sbrutus 	alloc = NULL;
11511974Sbrutus 	rdp->data.kmem = data;
11521974Sbrutus 	atomic_add_64(&nl7c_uri_bytes, sz);
11531974Sbrutus 
11540Sstevel@tonic-gate 	bp = data;
11551974Sbrutus 	while (uio->uio_resid > 0) {
11561974Sbrutus 		iov = uio->uio_iov;
11571974Sbrutus 		if ((cnt = iov->iov_len) == 0) {
11581974Sbrutus 			goto next;
11591974Sbrutus 		}
11601974Sbrutus 		cnt = MIN(cnt, uio->uio_resid);
11611974Sbrutus 		error = xcopyin(iov->iov_base, bp, cnt);
11621974Sbrutus 		if (error)
11630Sstevel@tonic-gate 			goto fail;
11641974Sbrutus 
11651974Sbrutus 		iov->iov_base += cnt;
11661974Sbrutus 		iov->iov_len -= cnt;
11671974Sbrutus 		uio->uio_resid -= cnt;
11681974Sbrutus 		uio->uio_loffset += cnt;
11690Sstevel@tonic-gate 		bp += cnt;
11701974Sbrutus 	next:
11711974Sbrutus 		uio->uio_iov++;
11721974Sbrutus 		uio->uio_iovcnt--;
11730Sstevel@tonic-gate 	}
11741974Sbrutus 
11751974Sbrutus 	/* Successfull sink of data, response parse the data */
11761974Sbrutus 	perror = nl7c_resp_parse(so, uri, data, sz);
11771974Sbrutus 
11781974Sbrutus 	/* Send the data out the connection */
11791974Sbrutus 	error = uri_rd_response(so, uri, rdp, first);
11801974Sbrutus 	if (error)
11811974Sbrutus 		goto fail;
11821974Sbrutus 
11831974Sbrutus 	/* Success */
11841974Sbrutus 	if (perror == 0 &&
11851974Sbrutus 	    ((uri->respclen == URI_LEN_NOVALUE &&
11861974Sbrutus 	    uri->resplen == URI_LEN_NOVALUE) ||
11871974Sbrutus 	    uri->count >= uri->resplen)) {
11880Sstevel@tonic-gate 		/*
11891974Sbrutus 		 * No more data needed and no pending response
11901974Sbrutus 		 * data or current data count >= response length
11911974Sbrutus 		 * so close the URI processing for this so.
11920Sstevel@tonic-gate 		 */
11931974Sbrutus 		nl7c_close(so);
11941974Sbrutus 		if (! (so->so_nl7c_flags & NL7C_SOPERSIST)) {
11951974Sbrutus 			/* Not a persistent connection */
11961974Sbrutus 			so->so_nl7c_flags = 0;
11971974Sbrutus 		}
11980Sstevel@tonic-gate 	}
11991974Sbrutus 
12001974Sbrutus 	return (0);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate fail:
12030Sstevel@tonic-gate 	if (alloc != NULL) {
12040Sstevel@tonic-gate 		kmem_free(alloc, sz);
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 	so->so_nl7c_flags = 0;
12070Sstevel@tonic-gate 	nl7c_urifree(so);
12081974Sbrutus 
12091974Sbrutus 	return (error);
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate /*
12130Sstevel@tonic-gate  * Called to read data from file "*fp" at offset "*off" of length "*len"
12140Sstevel@tonic-gate  * for a maximum of "*max_rem" bytes.
12150Sstevel@tonic-gate  *
12160Sstevel@tonic-gate  * On success a pointer to the kmem_alloc()ed file data is returned, "*off"
12170Sstevel@tonic-gate  * and "*len" are updated for the acutal number of bytes read and "*max_rem"
12180Sstevel@tonic-gate  * is updated with the number of bytes remaining to be read.
12190Sstevel@tonic-gate  *
12200Sstevel@tonic-gate  * Else, "NULL" is returned.
12210Sstevel@tonic-gate  */
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate static char *
12241974Sbrutus nl7c_readfile(file_t *fp, u_offset_t *off, int *len, int max, int *ret)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	vnode_t	*vp = fp->f_vnode;
12270Sstevel@tonic-gate 	int	flg = 0;
12281974Sbrutus 	size_t	size = MIN(*len, max);
12290Sstevel@tonic-gate 	char	*data;
12300Sstevel@tonic-gate 	int	error;
12310Sstevel@tonic-gate 	uio_t	uio;
12320Sstevel@tonic-gate 	iovec_t	iov;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, flg, NULL);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (*off > MAXOFFSET_T) {
12370Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, flg, NULL);
12381974Sbrutus 		*ret = EFBIG;
12390Sstevel@tonic-gate 		return (NULL);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	if (*off + size > MAXOFFSET_T)
12430Sstevel@tonic-gate 		size = (ssize32_t)(MAXOFFSET_T - *off);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	data = kmem_alloc(size, KM_SLEEP);
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	iov.iov_base = data;
12480Sstevel@tonic-gate 	iov.iov_len = size;
12490Sstevel@tonic-gate 	uio.uio_loffset = *off;
12500Sstevel@tonic-gate 	uio.uio_iov = &iov;
12510Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
12520Sstevel@tonic-gate 	uio.uio_resid = size;
12530Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
12540Sstevel@tonic-gate 	uio.uio_llimit = MAXOFFSET_T;
12550Sstevel@tonic-gate 	uio.uio_fmode = fp->f_flag;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	error = VOP_READ(vp, &uio, fp->f_flag, fp->f_cred, NULL);
12580Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, flg, NULL);
12591974Sbrutus 	*ret = error;
12600Sstevel@tonic-gate 	if (error) {
12610Sstevel@tonic-gate 		kmem_free(data, size);
12620Sstevel@tonic-gate 		return (NULL);
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 	*len = size;
12650Sstevel@tonic-gate 	*off += size;
12660Sstevel@tonic-gate 	return (data);
12670Sstevel@tonic-gate }
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate /*
12701974Sbrutus  * Called to sink application response sendfilev, as with nl7c_data() above
12711974Sbrutus  * all the data will be processed by NL7C unless there's an error.
12720Sstevel@tonic-gate  */
12730Sstevel@tonic-gate 
12741974Sbrutus int
12751974Sbrutus nl7c_sendfilev(struct sonode *so, u_offset_t *fileoff, sendfilevec_t *sfvp,
12761974Sbrutus 	int sfvc, ssize_t *xfer)
12770Sstevel@tonic-gate {
12780Sstevel@tonic-gate 	uri_desc_t	*uri = (uri_desc_t *)so->so_nl7c_uri;
12790Sstevel@tonic-gate 	file_t		*fp = NULL;
12800Sstevel@tonic-gate 	vnode_t		*vp = NULL;
12810Sstevel@tonic-gate 	char		*data = NULL;
12821974Sbrutus 	u_offset_t	off;
12830Sstevel@tonic-gate 	int		len;
12841974Sbrutus 	int		cnt;
12850Sstevel@tonic-gate 	int		total_count = 0;
12861974Sbrutus 	char		*alloc;
12870Sstevel@tonic-gate 	uri_rd_t	*rdp;
12881974Sbrutus 	int		max;
12891974Sbrutus 	int		perror;
12900Sstevel@tonic-gate 	int		error = 0;
12911974Sbrutus 	boolean_t	first = B_TRUE;
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	nl7c_uri_sendfilev++;
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	if (uri == NULL) {
12960Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
12970Sstevel@tonic-gate 		so->so_nl7c_flags = 0;
12980Sstevel@tonic-gate 		nl7c_uri_NULL2++;
12991974Sbrutus 		return (0);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	if (so->so_nl7c_flags & NL7C_WAITWRITE)
13030Sstevel@tonic-gate 		so->so_nl7c_flags &= ~NL7C_WAITWRITE;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	while (sfvc-- > 0) {
13060Sstevel@tonic-gate 		/*
13070Sstevel@tonic-gate 		 * off - the current sfv read file offset or user address.
13080Sstevel@tonic-gate 		 *
13091974Sbrutus 		 * len - the current sfv length in bytes.
13101974Sbrutus 		 *
13111974Sbrutus 		 * cnt - number of bytes kmem_alloc()ed.
13120Sstevel@tonic-gate 		 *
13131974Sbrutus 		 * alloc - the kmem_alloc()ed buffer of size "cnt".
13140Sstevel@tonic-gate 		 *
13151974Sbrutus 		 * data - copy of "alloc" used for post alloc references.
13160Sstevel@tonic-gate 		 *
13170Sstevel@tonic-gate 		 * fp - the current sfv file_t pointer.
13180Sstevel@tonic-gate 		 *
13190Sstevel@tonic-gate 		 * vp - the current "*vp" vnode_t pointer.
13200Sstevel@tonic-gate 		 *
13210Sstevel@tonic-gate 		 * Note, for "data" and "fp" and "vp" a NULL value is used
13220Sstevel@tonic-gate 		 * when not allocated such that the common failure path "fail"
13230Sstevel@tonic-gate 		 * is used.
13240Sstevel@tonic-gate 		 */
13250Sstevel@tonic-gate 		off = sfvp->sfv_off;
13260Sstevel@tonic-gate 		len = sfvp->sfv_len;
13271974Sbrutus 		cnt = len;
1328*8171SPrakash.Jalan@Sun.COM 
1329*8171SPrakash.Jalan@Sun.COM 		if (len == 0) {
1330*8171SPrakash.Jalan@Sun.COM 			sfvp++;
1331*8171SPrakash.Jalan@Sun.COM 			continue;
1332*8171SPrakash.Jalan@Sun.COM 		}
1333*8171SPrakash.Jalan@Sun.COM 
13341974Sbrutus 		if (sfvp->sfv_fd == SFV_FD_SELF) {
13350Sstevel@tonic-gate 			/*
13361974Sbrutus 			 * User memory, copyin() all the bytes.
13370Sstevel@tonic-gate 			 */
13381974Sbrutus 			alloc = kmem_alloc(cnt, KM_SLEEP);
13391974Sbrutus 			error = xcopyin((caddr_t)(uintptr_t)off, alloc, cnt);
13400Sstevel@tonic-gate 			if (error)
13410Sstevel@tonic-gate 				goto fail;
13421974Sbrutus 		} else {
13431974Sbrutus 			/*
13441974Sbrutus 			 * File descriptor, prefetch some bytes.
13451974Sbrutus 			 */
13461974Sbrutus 			if ((fp = getf(sfvp->sfv_fd)) == NULL) {
13471974Sbrutus 				error = EBADF;
13480Sstevel@tonic-gate 				goto fail;
13490Sstevel@tonic-gate 			}
13501974Sbrutus 			if ((fp->f_flag & FREAD) == 0) {
13511974Sbrutus 				error = EACCES;
13521974Sbrutus 				goto fail;
13530Sstevel@tonic-gate 			}
13541974Sbrutus 			vp = fp->f_vnode;
13551974Sbrutus 			if (vp->v_type != VREG) {
13561974Sbrutus 				error = EINVAL;
13570Sstevel@tonic-gate 				goto fail;
13581974Sbrutus 			}
13590Sstevel@tonic-gate 			VN_HOLD(vp);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 			/* Read max_rem bytes from file for prefetch */
13620Sstevel@tonic-gate 			if (nl7c_use_kmem) {
13631974Sbrutus 				max = cnt;
13640Sstevel@tonic-gate 			} else {
13651974Sbrutus 				max = MAXBSIZE * nl7c_file_prefetch;
13660Sstevel@tonic-gate 			}
13671974Sbrutus 			alloc = nl7c_readfile(fp, &off, &cnt, max, &error);
13681974Sbrutus 			if (alloc == NULL)
13690Sstevel@tonic-gate 				goto fail;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 			releasef(sfvp->sfv_fd);
13720Sstevel@tonic-gate 			fp = NULL;
13731974Sbrutus 		}
13741974Sbrutus 		URI_RD_ADD(uri, rdp, cnt, -1);
13751974Sbrutus 		if (rdp == NULL) {
13761974Sbrutus 			error = ENOMEM;
13771974Sbrutus 			goto fail;
13781974Sbrutus 		}
13791974Sbrutus 		data = alloc;
13801974Sbrutus 		alloc = NULL;
13811974Sbrutus 		rdp->data.kmem = data;
13821974Sbrutus 		total_count += cnt;
13831974Sbrutus 		if (uri->hash != URI_TEMP && total_count > nca_max_cache_size) {
13841974Sbrutus 			uri_delete(uri);
13851974Sbrutus 			uri->hash = URI_TEMP;
13861974Sbrutus 		}
13870Sstevel@tonic-gate 
13881974Sbrutus 		/* Response parse */
13891974Sbrutus 		perror = nl7c_resp_parse(so, uri, data, len);
13901974Sbrutus 
13911974Sbrutus 		/* Send kmem data out the connection */
13921974Sbrutus 		error = uri_rd_response(so, uri, rdp, first);
13931974Sbrutus 
13941974Sbrutus 		if (error)
13951974Sbrutus 			goto fail;
13960Sstevel@tonic-gate 
13971974Sbrutus 		if (sfvp->sfv_fd != SFV_FD_SELF) {
13981974Sbrutus 			/*
13991974Sbrutus 			 * File descriptor, if any bytes left save vnode_t.
14001974Sbrutus 			 */
14011974Sbrutus 			if (len > cnt) {
14021974Sbrutus 				/* More file data so add it */
14031974Sbrutus 				URI_RD_ADD(uri, rdp, len - cnt, off);
14041974Sbrutus 				if (rdp == NULL) {
14051974Sbrutus 					error = ENOMEM;
14061974Sbrutus 					goto fail;
14071974Sbrutus 				}
14081974Sbrutus 				rdp->data.vnode = vp;
14091974Sbrutus 
14101974Sbrutus 				/* Send vnode data out the connection */
14111974Sbrutus 				error = uri_rd_response(so, uri, rdp, first);
14120Sstevel@tonic-gate 			} else {
14131974Sbrutus 				/* All file data fit in the prefetch */
14141974Sbrutus 				VN_RELE(vp);
14150Sstevel@tonic-gate 			}
14161974Sbrutus 			*fileoff += len;
14171974Sbrutus 			vp = NULL;
14180Sstevel@tonic-gate 		}
14191974Sbrutus 		*xfer += len;
14200Sstevel@tonic-gate 		sfvp++;
14211974Sbrutus 
14221974Sbrutus 		if (first)
14231974Sbrutus 			first = B_FALSE;
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 	if (total_count > 0) {
14260Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, total_count);
14270Sstevel@tonic-gate 	}
14281974Sbrutus 	if (perror == 0 &&
14291974Sbrutus 	    ((uri->respclen == URI_LEN_NOVALUE &&
14301974Sbrutus 	    uri->resplen == URI_LEN_NOVALUE) ||
14311974Sbrutus 	    uri->count >= uri->resplen)) {
14321974Sbrutus 		/*
14331974Sbrutus 		 * No more data needed and no pending response
14341974Sbrutus 		 * data or current data count >= response length
14351974Sbrutus 		 * so close the URI processing for this so.
14361974Sbrutus 		 */
14370Sstevel@tonic-gate 		nl7c_close(so);
14381974Sbrutus 		if (! (so->so_nl7c_flags & NL7C_SOPERSIST)) {
14391974Sbrutus 			/* Not a persistent connection */
14401974Sbrutus 			so->so_nl7c_flags = 0;
14411974Sbrutus 		}
14420Sstevel@tonic-gate 	}
14431974Sbrutus 
14441974Sbrutus 	return (0);
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate fail:
14471974Sbrutus 	if (alloc != NULL)
14480Sstevel@tonic-gate 		kmem_free(data, len);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	if (vp != NULL)
14510Sstevel@tonic-gate 		VN_RELE(vp);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if (fp != NULL)
14540Sstevel@tonic-gate 		releasef(sfvp->sfv_fd);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	if (total_count > 0) {
14570Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, total_count);
14580Sstevel@tonic-gate 	}
14591974Sbrutus 
14600Sstevel@tonic-gate 	so->so_nl7c_flags = 0;
14610Sstevel@tonic-gate 	nl7c_urifree(so);
14621974Sbrutus 
14631974Sbrutus 	return (error);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate /*
14670Sstevel@tonic-gate  * Called for a socket which is closing or when an application has
14680Sstevel@tonic-gate  * completed sending all the response data (i.e. for a persistent
14690Sstevel@tonic-gate  * connection called once for each completed application response).
14700Sstevel@tonic-gate  */
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate void
14730Sstevel@tonic-gate nl7c_close(struct sonode *so)
14740Sstevel@tonic-gate {
14750Sstevel@tonic-gate 	uri_desc_t *uri = (uri_desc_t *)so->so_nl7c_uri;
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	if (uri == NULL) {
14780Sstevel@tonic-gate 		/*
14790Sstevel@tonic-gate 		 * No URI being processed so might be a listen()er
14800Sstevel@tonic-gate 		 * if so do any cleanup, else nothing more to do.
14810Sstevel@tonic-gate 		 */
14820Sstevel@tonic-gate 		if (so->so_state & SS_ACCEPTCONN) {
14830Sstevel@tonic-gate 			(void) nl7c_close_addr(so);
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 		return;
14860Sstevel@tonic-gate 	}
14870Sstevel@tonic-gate 	so->so_nl7c_uri = NULL;
14880Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
14890Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
14900Sstevel@tonic-gate 		uri->proc = NULL;
14910Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
14920Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
14950Sstevel@tonic-gate 		nl7c_uri_close++;
14960Sstevel@tonic-gate 	} else {
14970Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
14980Sstevel@tonic-gate 		uri->proc = NULL;
14990Sstevel@tonic-gate 		nl7c_uri_temp_close++;
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 	REF_RELE(uri);
15020Sstevel@tonic-gate 	if (nl7c_uri_max > 0 && nl7c_uri_bytes > nl7c_uri_max) {
15030Sstevel@tonic-gate 		nl7c_uri_reclaim();
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate /*
15080Sstevel@tonic-gate  * The uri_segmap_t ref_t inactive function called on the last REF_RELE(),
15090Sstevel@tonic-gate  * release the segmap mapping. Note, the uri_segmap_t will be freed by
15100Sstevel@tonic-gate  * REF_RELE() on return.
15110Sstevel@tonic-gate  */
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate void
15140Sstevel@tonic-gate uri_segmap_inactive(uri_segmap_t *smp)
15150Sstevel@tonic-gate {
15160Sstevel@tonic-gate 	if (!segmap_kpm) {
15170Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap, smp->base,
15180Sstevel@tonic-gate 		    smp->len, F_SOFTUNLOCK, S_OTHER);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 	(void) segmap_release(segkmap, smp->base, SM_DONTNEED);
15210Sstevel@tonic-gate 	VN_RELE(smp->vp);
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate /*
15250Sstevel@tonic-gate  * The call-back for desballoc()ed mblk_t's, if a segmap mapped mblk_t
15261974Sbrutus  * release the reference, one per desballoc() of a segmap page, if a rd_t
15271974Sbrutus  * mapped mblk_t release the reference, one per desballoc() of a uri_desc_t,
15281974Sbrutus  * last kmem free the uri_desb_t.
15290Sstevel@tonic-gate  */
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate static void
15320Sstevel@tonic-gate uri_desb_free(uri_desb_t *desb)
15330Sstevel@tonic-gate {
15340Sstevel@tonic-gate 	if (desb->segmap != NULL) {
15350Sstevel@tonic-gate 		REF_RELE(desb->segmap);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 	REF_RELE(desb->uri);
15380Sstevel@tonic-gate 	kmem_cache_free(uri_desb_kmc, desb);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate /*
15420Sstevel@tonic-gate  * Segmap map up to a page of a uri_rd_t file descriptor.
15430Sstevel@tonic-gate  */
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate uri_segmap_t *
15460Sstevel@tonic-gate uri_segmap_map(uri_rd_t *rdp, int bytes)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	uri_segmap_t	*segmap = kmem_cache_alloc(uri_segmap_kmc, KM_SLEEP);
15490Sstevel@tonic-gate 	int		len = MIN(rdp->sz, MAXBSIZE);
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	if (len > bytes)
15520Sstevel@tonic-gate 		len = bytes;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	REF_INIT(segmap, 1, uri_segmap_inactive, uri_segmap_kmc);
15550Sstevel@tonic-gate 	segmap->len = len;
15560Sstevel@tonic-gate 	VN_HOLD(rdp->data.vnode);
15570Sstevel@tonic-gate 	segmap->vp = rdp->data.vnode;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	segmap->base = segmap_getmapflt(segkmap, segmap->vp, rdp->off, len,
15600Sstevel@tonic-gate 	    segmap_kpm ? SM_FAULT : 0, S_READ);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	if (segmap_fault(kas.a_hat, segkmap, segmap->base, len,
15630Sstevel@tonic-gate 	    F_SOFTLOCK, S_READ) != 0) {
15640Sstevel@tonic-gate 		REF_RELE(segmap);
15650Sstevel@tonic-gate 		return (NULL);
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	return (segmap);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate  * Chop up the kernel virtual memory area *data of size *sz bytes for
15720Sstevel@tonic-gate  * a maximum of *bytes bytes into an besballoc()ed mblk_t chain using
15730Sstevel@tonic-gate  * the given template uri_desb_t *temp of max_mblk bytes per.
15740Sstevel@tonic-gate  *
15750Sstevel@tonic-gate  * The values of *data, *sz, and *bytes are updated on return, the
15760Sstevel@tonic-gate  * mblk_t chain is returned.
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate static mblk_t *
15800Sstevel@tonic-gate uri_desb_chop(
15810Sstevel@tonic-gate 	char 		**data,
15820Sstevel@tonic-gate 	size_t		*sz,
15830Sstevel@tonic-gate 	int 		*bytes,
15840Sstevel@tonic-gate 	uri_desb_t 	*temp,
15850Sstevel@tonic-gate 	int		max_mblk,
15860Sstevel@tonic-gate 	char		*eoh,
15870Sstevel@tonic-gate 	mblk_t		*persist
15880Sstevel@tonic-gate )
15890Sstevel@tonic-gate {
15900Sstevel@tonic-gate 	char		*ldata = *data;
15910Sstevel@tonic-gate 	size_t		lsz = *sz;
15920Sstevel@tonic-gate 	int		lbytes = bytes ? *bytes : lsz;
15930Sstevel@tonic-gate 	uri_desb_t	*desb;
15940Sstevel@tonic-gate 	mblk_t		*mp = NULL;
15951974Sbrutus 	mblk_t		*nmp, *pmp = NULL;
15960Sstevel@tonic-gate 	int		msz;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	if (lbytes == 0 && lsz == 0)
15990Sstevel@tonic-gate 		return (NULL);
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	while (lbytes > 0 && lsz > 0) {
16020Sstevel@tonic-gate 		msz = MIN(lbytes, max_mblk);
16030Sstevel@tonic-gate 		msz = MIN(msz, lsz);
16040Sstevel@tonic-gate 		if (persist && eoh >= ldata && eoh < &ldata[msz]) {
16050Sstevel@tonic-gate 			msz = (eoh - ldata);
16060Sstevel@tonic-gate 			pmp = persist;
16070Sstevel@tonic-gate 			persist = NULL;
16081974Sbrutus 			if (msz == 0) {
16091974Sbrutus 				nmp = pmp;
16101974Sbrutus 				pmp = NULL;
16111974Sbrutus 				goto zero;
16121974Sbrutus 			}
16130Sstevel@tonic-gate 		}
16140Sstevel@tonic-gate 		desb = kmem_cache_alloc(uri_desb_kmc, KM_SLEEP);
16150Sstevel@tonic-gate 		REF_HOLD(temp->uri);
16160Sstevel@tonic-gate 		if (temp->segmap) {
16170Sstevel@tonic-gate 			REF_HOLD(temp->segmap);
16180Sstevel@tonic-gate 		}
16190Sstevel@tonic-gate 		bcopy(temp, desb, sizeof (*desb));
16200Sstevel@tonic-gate 		desb->frtn.free_arg = (caddr_t)desb;
16210Sstevel@tonic-gate 		nmp = desballoc((uchar_t *)ldata, msz, BPRI_HI, &desb->frtn);
16220Sstevel@tonic-gate 		if (nmp == NULL) {
16230Sstevel@tonic-gate 			if (temp->segmap) {
16240Sstevel@tonic-gate 				REF_RELE(temp->segmap);
16250Sstevel@tonic-gate 			}
16260Sstevel@tonic-gate 			REF_RELE(temp->uri);
16270Sstevel@tonic-gate 			if (mp != NULL) {
16281974Sbrutus 				mp->b_next = NULL;
16290Sstevel@tonic-gate 				freemsg(mp);
16300Sstevel@tonic-gate 			}
16311974Sbrutus 			if (persist != NULL) {
16321974Sbrutus 				freeb(persist);
16331974Sbrutus 			}
16340Sstevel@tonic-gate 			return (NULL);
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate 		nmp->b_wptr += msz;
16371974Sbrutus 	zero:
16380Sstevel@tonic-gate 		if (mp != NULL) {
16391974Sbrutus 			mp->b_next->b_cont = nmp;
16400Sstevel@tonic-gate 		} else {
16410Sstevel@tonic-gate 			mp = nmp;
16420Sstevel@tonic-gate 		}
16431974Sbrutus 		if (pmp != NULL) {
16441974Sbrutus 			nmp->b_cont = pmp;
16451974Sbrutus 			nmp = pmp;
16461974Sbrutus 			pmp = NULL;
16471974Sbrutus 		}
16481974Sbrutus 		mp->b_next = nmp;
16490Sstevel@tonic-gate 		ldata += msz;
16500Sstevel@tonic-gate 		lsz -= msz;
16510Sstevel@tonic-gate 		lbytes -= msz;
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 	*data = ldata;
16540Sstevel@tonic-gate 	*sz = lsz;
16550Sstevel@tonic-gate 	if (bytes)
16560Sstevel@tonic-gate 		*bytes = lbytes;
16570Sstevel@tonic-gate 	return (mp);
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate /*
16610Sstevel@tonic-gate  * Experimential noqwait (i.e. no canput()/qwait() checks), just send
16620Sstevel@tonic-gate  * the entire mblk_t chain down without flow-control checks.
16630Sstevel@tonic-gate  */
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate static int
16660Sstevel@tonic-gate kstrwritempnoqwait(struct vnode *vp, mblk_t *mp)
16670Sstevel@tonic-gate {
16680Sstevel@tonic-gate 	struct stdata *stp;
16690Sstevel@tonic-gate 	int error = 0;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	ASSERT(vp->v_stream);
16720Sstevel@tonic-gate 	stp = vp->v_stream;
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/* Fast check of flags before acquiring the lock */
16750Sstevel@tonic-gate 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
16760Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
16770Sstevel@tonic-gate 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
16780Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
16790Sstevel@tonic-gate 		if (error != 0) {
16800Sstevel@tonic-gate 			if (!(stp->sd_flag & STPLEX) &&
16810Sstevel@tonic-gate 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
16820Sstevel@tonic-gate 				tsignal(curthread, SIGPIPE);
16830Sstevel@tonic-gate 				error = EPIPE;
16840Sstevel@tonic-gate 			}
16850Sstevel@tonic-gate 			return (error);
16860Sstevel@tonic-gate 		}
16870Sstevel@tonic-gate 	}
16880Sstevel@tonic-gate 	putnext(stp->sd_wrq, mp);
16890Sstevel@tonic-gate 	return (0);
16900Sstevel@tonic-gate }
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate /*
16931974Sbrutus  * Send the URI uri_desc_t *uri response uri_rd_t *rdp out the socket_t *so.
16940Sstevel@tonic-gate  */
16950Sstevel@tonic-gate 
16961974Sbrutus static int
16971974Sbrutus uri_rd_response(struct sonode *so,
16981974Sbrutus     uri_desc_t *uri,
16991974Sbrutus     uri_rd_t *rdp,
17001974Sbrutus     boolean_t first)
17010Sstevel@tonic-gate {
17020Sstevel@tonic-gate 	vnode_t		*vp = SOTOV(so);
17031974Sbrutus 	int		max_mblk = (int)((tcp_t *)so->so_priv)->tcp_mss;
17040Sstevel@tonic-gate 	int		wsz;
17050Sstevel@tonic-gate 	mblk_t		*mp, *wmp, *persist;
17060Sstevel@tonic-gate 	int		write_bytes;
17071974Sbrutus 	uri_rd_t	rd;
17080Sstevel@tonic-gate 	uri_desb_t	desb;
17090Sstevel@tonic-gate 	uri_segmap_t	*segmap = NULL;
17100Sstevel@tonic-gate 	char		*segmap_data;
17110Sstevel@tonic-gate 	size_t		segmap_sz;
17121974Sbrutus 	int		error;
17131974Sbrutus 	int		fflg = ((so->so_state & SS_NDELAY) ? FNDELAY : 0) |
1714*8171SPrakash.Jalan@Sun.COM 	    ((so->so_state & SS_NONBLOCK) ? FNONBLOCK : 0);
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	/* Initialize template uri_desb_t */
17180Sstevel@tonic-gate 	desb.frtn.free_func = uri_desb_free;
17190Sstevel@tonic-gate 	desb.frtn.free_arg = NULL;
17200Sstevel@tonic-gate 	desb.uri = uri;
17210Sstevel@tonic-gate 
17221974Sbrutus 	/* Get a local copy of the rd_t */
17231974Sbrutus 	bcopy(rdp, &rd, sizeof (rd));
17240Sstevel@tonic-gate 	do {
17251974Sbrutus 		if (first) {
17261974Sbrutus 			/*
17271974Sbrutus 			 * For first kstrwrite() enough data to get
17281974Sbrutus 			 * things going, note non blocking version of
17291974Sbrutus 			 * kstrwrite() will be used below.
17301974Sbrutus 			 */
17311974Sbrutus 			write_bytes = P2ROUNDUP((max_mblk * 4),
17321974Sbrutus 			    MAXBSIZE * nl7c_file_prefetch);
17331974Sbrutus 		} else {
17341974Sbrutus 			if ((write_bytes = so->so_sndbuf) == 0)
17351974Sbrutus 				write_bytes = vp->v_stream->sd_qn_maxpsz;
17361974Sbrutus 			ASSERT(write_bytes > 0);
17371974Sbrutus 			write_bytes = P2ROUNDUP(write_bytes, MAXBSIZE);
17381974Sbrutus 		}
17391974Sbrutus 		/*
17401974Sbrutus 		 * Chop up to a write_bytes worth of data.
17411974Sbrutus 		 */
17420Sstevel@tonic-gate 		wmp = NULL;
17430Sstevel@tonic-gate 		wsz = write_bytes;
17440Sstevel@tonic-gate 		do {
17451974Sbrutus 			if (rd.sz == 0)
17461974Sbrutus 				break;
17470Sstevel@tonic-gate 			if (rd.off == -1) {
17480Sstevel@tonic-gate 				if (uri->eoh >= rd.data.kmem &&
17490Sstevel@tonic-gate 				    uri->eoh < &rd.data.kmem[rd.sz]) {
17500Sstevel@tonic-gate 					persist = nl7c_http_persist(so);
17510Sstevel@tonic-gate 				} else {
17520Sstevel@tonic-gate 					persist = NULL;
17530Sstevel@tonic-gate 				}
17540Sstevel@tonic-gate 				desb.segmap = NULL;
17550Sstevel@tonic-gate 				mp = uri_desb_chop(&rd.data.kmem, &rd.sz,
17560Sstevel@tonic-gate 				    &wsz, &desb, max_mblk, uri->eoh, persist);
17571974Sbrutus 				if (mp == NULL) {
17581974Sbrutus 					error = ENOMEM;
17590Sstevel@tonic-gate 					goto invalidate;
17601974Sbrutus 				}
17610Sstevel@tonic-gate 			} else {
17620Sstevel@tonic-gate 				if (segmap == NULL) {
17630Sstevel@tonic-gate 					segmap = uri_segmap_map(&rd,
17640Sstevel@tonic-gate 					    write_bytes);
17651974Sbrutus 					if (segmap == NULL) {
17661974Sbrutus 						error = ENOMEM;
17670Sstevel@tonic-gate 						goto invalidate;
17681974Sbrutus 					}
17690Sstevel@tonic-gate 					desb.segmap = segmap;
17700Sstevel@tonic-gate 					segmap_data = segmap->base;
17710Sstevel@tonic-gate 					segmap_sz = segmap->len;
17720Sstevel@tonic-gate 				}
17730Sstevel@tonic-gate 				mp = uri_desb_chop(&segmap_data, &segmap_sz,
17740Sstevel@tonic-gate 				    &wsz, &desb, max_mblk, NULL, NULL);
17751974Sbrutus 				if (mp == NULL) {
17761974Sbrutus 					error = ENOMEM;
17770Sstevel@tonic-gate 					goto invalidate;
17781974Sbrutus 				}
17790Sstevel@tonic-gate 				if (segmap_sz == 0) {
17800Sstevel@tonic-gate 					rd.sz -= segmap->len;
17810Sstevel@tonic-gate 					rd.off += segmap->len;
17820Sstevel@tonic-gate 					REF_RELE(segmap);
17830Sstevel@tonic-gate 					segmap = NULL;
17840Sstevel@tonic-gate 				}
17850Sstevel@tonic-gate 			}
17860Sstevel@tonic-gate 			if (wmp == NULL) {
17870Sstevel@tonic-gate 				wmp = mp;
17880Sstevel@tonic-gate 			} else {
17890Sstevel@tonic-gate 				wmp->b_next->b_cont = mp;
17900Sstevel@tonic-gate 				wmp->b_next = mp->b_next;
17910Sstevel@tonic-gate 				mp->b_next = NULL;
17920Sstevel@tonic-gate 			}
17931974Sbrutus 		} while (wsz > 0 && rd.sz > 0);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		wmp->b_next = NULL;
17960Sstevel@tonic-gate 		if (first) {
17970Sstevel@tonic-gate 			/* First kstrwrite(), use noqwait */
17981974Sbrutus 			if ((error = kstrwritempnoqwait(vp, wmp)) != 0)
17990Sstevel@tonic-gate 				goto invalidate;
18000Sstevel@tonic-gate 			/*
18010Sstevel@tonic-gate 			 * For the rest of the kstrwrite()s use SO_SNDBUF
18020Sstevel@tonic-gate 			 * worth of data at a time, note these kstrwrite()s
18030Sstevel@tonic-gate 			 * may (will) block one or more times.
18040Sstevel@tonic-gate 			 */
18050Sstevel@tonic-gate 			first = B_FALSE;
18060Sstevel@tonic-gate 		} else {
18071974Sbrutus 			if ((error = kstrwritemp(vp, wmp, fflg)) != 0) {
18081974Sbrutus 				if (error == EAGAIN) {
18091974Sbrutus 					nl7c_uri_rd_EAGAIN++;
18101974Sbrutus 					if ((error =
18111974Sbrutus 					    kstrwritempnoqwait(vp, wmp)) != 0)
18121974Sbrutus 						goto invalidate;
18131974Sbrutus 				} else
18141974Sbrutus 					goto invalidate;
18151974Sbrutus 			}
18160Sstevel@tonic-gate 		}
18171974Sbrutus 	} while (rd.sz > 0);
18180Sstevel@tonic-gate 
18191974Sbrutus 	return (0);
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate invalidate:
18220Sstevel@tonic-gate 	if (segmap) {
18230Sstevel@tonic-gate 		REF_RELE(segmap);
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 	if (wmp)
18260Sstevel@tonic-gate 		freemsg(wmp);
18271974Sbrutus 
18281974Sbrutus 	return (error);
18291974Sbrutus }
18301974Sbrutus 
18311974Sbrutus /*
18321974Sbrutus  * Send the URI uri_desc_t *uri response out the socket_t *so.
18331974Sbrutus  */
18341974Sbrutus 
18351974Sbrutus static int
18361974Sbrutus uri_response(struct sonode *so, uri_desc_t *uri)
18371974Sbrutus {
18381974Sbrutus 	uri_rd_t	*rdp = &uri->response;
18391974Sbrutus 	boolean_t	first = B_TRUE;
18401974Sbrutus 	int		error;
18411974Sbrutus 
18421974Sbrutus 	while (rdp != NULL) {
18431974Sbrutus 		error = uri_rd_response(so, uri, rdp, first);
18441974Sbrutus 		if (error != 0) {
18451974Sbrutus 			goto invalidate;
18461974Sbrutus 		}
18471974Sbrutus 		first = B_FALSE;
18481974Sbrutus 		rdp = rdp->next;
18491974Sbrutus 	}
18501974Sbrutus 	return (0);
18511974Sbrutus 
18521974Sbrutus invalidate:
18530Sstevel@tonic-gate 	uri_delete(uri);
18541974Sbrutus 	return (error);
18550Sstevel@tonic-gate }
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate /*
18580Sstevel@tonic-gate  * The pchars[] array is indexed by a char to determine if it's a
18590Sstevel@tonic-gate  * valid URI path component chararcter where:
18600Sstevel@tonic-gate  *
18610Sstevel@tonic-gate  *    pchar       = unreserved | escaped |
18620Sstevel@tonic-gate  *                  ":" | "@" | "&" | "=" | "+" | "$" | ","
18630Sstevel@tonic-gate  *
18640Sstevel@tonic-gate  *    unreserved  = alphanum | mark
18650Sstevel@tonic-gate  *
18660Sstevel@tonic-gate  *    alphanum    = alpha | digit
18670Sstevel@tonic-gate  *
18680Sstevel@tonic-gate  *    alpha       = lowalpha | upalpha
18690Sstevel@tonic-gate  *
18700Sstevel@tonic-gate  *    lowalpha    = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
18710Sstevel@tonic-gate  *                  "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
18720Sstevel@tonic-gate  *                  "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
18730Sstevel@tonic-gate  *                  "y" | "z"
18740Sstevel@tonic-gate  *
18750Sstevel@tonic-gate  *    upalpha     = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
18760Sstevel@tonic-gate  *                  "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
18770Sstevel@tonic-gate  *                  "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
18780Sstevel@tonic-gate  *                  "Y" | "Z"
18790Sstevel@tonic-gate  *
18800Sstevel@tonic-gate  *    digit       = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
18810Sstevel@tonic-gate  *                  "8" | "9"
18820Sstevel@tonic-gate  *
18830Sstevel@tonic-gate  *    mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
18840Sstevel@tonic-gate  *
18850Sstevel@tonic-gate  *    escaped     = "%" hex hex
18860Sstevel@tonic-gate  *    hex         = digit | "A" | "B" | "C" | "D" | "E" | "F" |
18870Sstevel@tonic-gate  *                  "a" | "b" | "c" | "d" | "e" | "f"
18880Sstevel@tonic-gate  */
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate static char pchars[] = {
18910Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x07 */
18920Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x08 - 0x0F */
18930Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x17 */
18940Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x18 - 0x1F */
18950Sstevel@tonic-gate     0, 1, 0, 0, 1, 1, 1, 1,	/* 0x20 - 0x27 */
18960Sstevel@tonic-gate     0, 0, 1, 1, 1, 1, 1, 1,	/* 0x28 - 0x2F */
18970Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x30 - 0x37 */
18980Sstevel@tonic-gate     1, 1, 1, 0, 0, 1, 0, 0,	/* 0x38 - 0x3F */
18990Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x47 */
19000Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x48 - 0x4F */
19010Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x57 */
19020Sstevel@tonic-gate     1, 1, 1, 0, 0, 0, 0, 1,	/* 0x58 - 0x5F */
19030Sstevel@tonic-gate     0, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x67 */
19040Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x68 - 0x6F */
19050Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x77 */
19060Sstevel@tonic-gate     1, 1, 1, 0, 0, 0, 1, 0	/* 0x78 - 0x7F */
19070Sstevel@tonic-gate };
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate #define	PCHARS_MASK 0x7F
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate /*
19120Sstevel@tonic-gate  * This is the main L7 request message parse, we are called each time
19130Sstevel@tonic-gate  * new data is availble for a socket, each time a single buffer of the
19140Sstevel@tonic-gate  * entire message to date is given.
19150Sstevel@tonic-gate  *
19160Sstevel@tonic-gate  * Here we parse the request looking for the URI, parse it, and if a
19170Sstevel@tonic-gate  * supported scheme call the scheme parser to commplete the parse of any
19180Sstevel@tonic-gate  * headers which may further qualify the identity of the requested object
19190Sstevel@tonic-gate  * then lookup it up in the URI hash.
19200Sstevel@tonic-gate  *
19210Sstevel@tonic-gate  * Return B_TRUE for more processing.
19220Sstevel@tonic-gate  *
19230Sstevel@tonic-gate  * Note, at this time the parser supports the generic message format as
19240Sstevel@tonic-gate  * specified in RFC 822 with potentional limitations as specified in RFC
19250Sstevel@tonic-gate  * 2616 for HTTP messages.
19260Sstevel@tonic-gate  *
19270Sstevel@tonic-gate  * Note, the caller supports an mblk_t chain, for now the parser(s)
19280Sstevel@tonic-gate  * require the complete header in a single mblk_t. This is the common
19290Sstevel@tonic-gate  * case and certainly for high performance environments, if at a future
19300Sstevel@tonic-gate  * date mblk_t chains are important the parse can be reved to process
19310Sstevel@tonic-gate  * mblk_t chains.
19320Sstevel@tonic-gate  */
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate boolean_t
19351974Sbrutus nl7c_parse(struct sonode *so, boolean_t nonblocking, boolean_t *ret)
19360Sstevel@tonic-gate {
19370Sstevel@tonic-gate 	char	*cp = (char *)so->so_nl7c_rcv_mp->b_rptr;
19380Sstevel@tonic-gate 	char	*ep = (char *)so->so_nl7c_rcv_mp->b_wptr;
19390Sstevel@tonic-gate 	char	*get = "GET ";
19401974Sbrutus 	char	*post = "POST ";
19410Sstevel@tonic-gate 	char	c;
19420Sstevel@tonic-gate 	char	*uris;
19431974Sbrutus 	uri_desc_t *uri = NULL;
19440Sstevel@tonic-gate 	uri_desc_t *ruri = NULL;
19451974Sbrutus 	mblk_t	*reqmp;
19461974Sbrutus 	uint32_t hv = 0;
19470Sstevel@tonic-gate 
19481974Sbrutus 	if ((reqmp = dupb(so->so_nl7c_rcv_mp)) == NULL) {
19491974Sbrutus 		nl7c_uri_pass_dupbfail++;
19501974Sbrutus 		goto pass;
19511974Sbrutus 	}
19520Sstevel@tonic-gate 	/*
19530Sstevel@tonic-gate 	 * Allocate and initialize minimumal state for the request
19540Sstevel@tonic-gate 	 * uri_desc_t, in the cache hit case this uri_desc_t will
19550Sstevel@tonic-gate 	 * be freed.
19560Sstevel@tonic-gate 	 */
19571974Sbrutus 	uri = kmem_cache_alloc(nl7c_uri_kmc, KM_SLEEP);
19581974Sbrutus 	REF_INIT(uri, 1, nl7c_uri_inactive, nl7c_uri_kmc);
19590Sstevel@tonic-gate 	uri->hash = NULL;
19600Sstevel@tonic-gate 	uri->tail = NULL;
19610Sstevel@tonic-gate 	uri->scheme = NULL;
19620Sstevel@tonic-gate 	uri->count = 0;
19631974Sbrutus 	uri->reqmp = reqmp;
19641974Sbrutus 
19650Sstevel@tonic-gate 	/*
19660Sstevel@tonic-gate 	 * Set request time to current time.
19670Sstevel@tonic-gate 	 */
19680Sstevel@tonic-gate 	so->so_nl7c_rtime = gethrestime_sec();
19691974Sbrutus 
19700Sstevel@tonic-gate 	/*
19710Sstevel@tonic-gate 	 * Parse the Request-Line for the URI.
19720Sstevel@tonic-gate 	 *
19730Sstevel@tonic-gate 	 * For backwards HTTP version compatable reasons skip any leading
19740Sstevel@tonic-gate 	 * CRLF (or CR or LF) line terminator(s) preceding Request-Line.
19750Sstevel@tonic-gate 	 */
19760Sstevel@tonic-gate 	while (cp < ep && (*cp == '\r' || *cp == '\n')) {
19770Sstevel@tonic-gate 		cp++;
19780Sstevel@tonic-gate 	}
19790Sstevel@tonic-gate 	while (cp < ep && *get == *cp) {
19800Sstevel@tonic-gate 		get++;
19810Sstevel@tonic-gate 		cp++;
19820Sstevel@tonic-gate 	}
19830Sstevel@tonic-gate 	if (*get != 0) {
19841974Sbrutus 		/* Note a "GET", check for "POST" */
19851974Sbrutus 		while (cp < ep && *post == *cp) {
19861974Sbrutus 			post++;
19871974Sbrutus 			cp++;
19880Sstevel@tonic-gate 		}
19891974Sbrutus 		if (*post != 0) {
19901974Sbrutus 			if (cp == ep) {
19911974Sbrutus 				nl7c_uri_more_get++;
19921974Sbrutus 				goto more;
19931974Sbrutus 			}
19941974Sbrutus 			/* Not a "GET" or a "POST", just pass */
19951974Sbrutus 			nl7c_uri_pass_method++;
19961974Sbrutus 			goto pass;
19971974Sbrutus 		}
19981974Sbrutus 		/* "POST", don't cache but still may want to parse */
19991974Sbrutus 		uri->hash = URI_TEMP;
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 	/*
20020Sstevel@tonic-gate 	 * Skip over URI path char(s) and save start and past end pointers.
20030Sstevel@tonic-gate 	 */
20040Sstevel@tonic-gate 	uris = cp;
20050Sstevel@tonic-gate 	while (cp < ep && (c = *cp) != ' ' && c != '\r') {
20060Sstevel@tonic-gate 		if (c == '?') {
20070Sstevel@tonic-gate 			/* Don't cache but still may want to parse */
20080Sstevel@tonic-gate 			uri->hash = URI_TEMP;
20090Sstevel@tonic-gate 		}
20101974Sbrutus 		CHASH(hv, c);
20110Sstevel@tonic-gate 		cp++;
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate 	if (c != '\r' && cp == ep) {
20140Sstevel@tonic-gate 		nl7c_uri_more_eol++;
20150Sstevel@tonic-gate 		goto more;
20160Sstevel@tonic-gate 	}
20170Sstevel@tonic-gate 	/*
20180Sstevel@tonic-gate 	 * Request-Line URI parsed, pass the rest of the request on
20190Sstevel@tonic-gate 	 * to the the http scheme parse.
20200Sstevel@tonic-gate 	 */
20210Sstevel@tonic-gate 	uri->path.cp = uris;
20220Sstevel@tonic-gate 	uri->path.ep = cp;
20231974Sbrutus 	uri->hvalue = hv;
20241974Sbrutus 	if (! nl7c_http_request(&cp, ep, uri, so) || cp == NULL) {
20250Sstevel@tonic-gate 		/*
20261974Sbrutus 		 * Parse not successful or pass on request, the pointer
20271974Sbrutus 		 * to the parse pointer "cp" is overloaded such that ! NULL
20281974Sbrutus 		 * for more data and NULL for bad parse of request or pass.
20290Sstevel@tonic-gate 		 */
20300Sstevel@tonic-gate 		if (cp != NULL) {
20310Sstevel@tonic-gate 			nl7c_uri_more_http++;
20320Sstevel@tonic-gate 			goto more;
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 		nl7c_uri_pass_http++;
20350Sstevel@tonic-gate 		goto pass;
20360Sstevel@tonic-gate 	}
20371974Sbrutus 	if (uri->nocache) {
20381974Sbrutus 		uri->hash = URI_TEMP;
20391974Sbrutus 		(void) uri_lookup(uri, B_FALSE, nonblocking);
20401974Sbrutus 	} else if (uri->hash == URI_TEMP) {
20411974Sbrutus 		uri->nocache = B_TRUE;
20421974Sbrutus 		(void) uri_lookup(uri, B_FALSE, nonblocking);
20431974Sbrutus 	}
20441974Sbrutus 
20450Sstevel@tonic-gate 	if (uri->hash == URI_TEMP) {
20460Sstevel@tonic-gate 		if (so->so_nl7c_flags & NL7C_SOPERSIST) {
20470Sstevel@tonic-gate 			/* Temporary URI so skip hash processing */
20480Sstevel@tonic-gate 			nl7c_uri_request++;
20490Sstevel@tonic-gate 			nl7c_uri_temp++;
20500Sstevel@tonic-gate 			goto temp;
20510Sstevel@tonic-gate 		}
20520Sstevel@tonic-gate 		/* Not persistent so not interested in the response */
20530Sstevel@tonic-gate 		nl7c_uri_pass_temp++;
20540Sstevel@tonic-gate 		goto pass;
20550Sstevel@tonic-gate 	}
20560Sstevel@tonic-gate 	/*
20571974Sbrutus 	 * Check the URI hash for a cached response, save the request
20581974Sbrutus 	 * uri in case we need it below.
20590Sstevel@tonic-gate 	 */
20601974Sbrutus 	ruri = uri;
20610Sstevel@tonic-gate 	if ((uri = uri_lookup(uri, B_TRUE, nonblocking)) == NULL) {
20620Sstevel@tonic-gate 		/*
20630Sstevel@tonic-gate 		 * Failed to lookup due to nonblocking wait required,
20640Sstevel@tonic-gate 		 * interrupted cv_wait_sig(), KM_NOSLEEP memory alloc
20650Sstevel@tonic-gate 		 * failure, ... Just pass on this request.
20660Sstevel@tonic-gate 		 */
20670Sstevel@tonic-gate 		nl7c_uri_pass_addfail++;
20680Sstevel@tonic-gate 		goto pass;
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 	nl7c_uri_request++;
20710Sstevel@tonic-gate 	if (uri->response.sz > 0) {
20720Sstevel@tonic-gate 		/*
20730Sstevel@tonic-gate 		 * We have the response cached, update recv mblk rptr
20741974Sbrutus 		 * to reflect the data consumed in parse.
20750Sstevel@tonic-gate 		 */
20760Sstevel@tonic-gate 		mblk_t	*mp = so->so_nl7c_rcv_mp;
20770Sstevel@tonic-gate 
20781974Sbrutus 		if (cp == (char *)mp->b_wptr) {
20791974Sbrutus 			so->so_nl7c_rcv_mp = mp->b_cont;
20801974Sbrutus 			mp->b_cont = NULL;
20811974Sbrutus 			freeb(mp);
20821974Sbrutus 		} else {
20831974Sbrutus 			mp->b_rptr = (unsigned char *)cp;
20841974Sbrutus 		}
20851974Sbrutus 		nl7c_uri_hit++;
20861974Sbrutus 		/* If conditional request check for substitute response */
20871974Sbrutus 		if (ruri->conditional) {
20881974Sbrutus 			uri = nl7c_http_cond(ruri, uri);
20891974Sbrutus 		}
20901974Sbrutus 		/* If logging enabled log request */
20911974Sbrutus 		if (nl7c_logd_enabled) {
20920Sstevel@tonic-gate 			ipaddr_t faddr;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
20950Sstevel@tonic-gate 				/* Only support IPv4 addrs */
20960Sstevel@tonic-gate 				faddr = ((struct sockaddr_in *)
20970Sstevel@tonic-gate 				    so->so_faddr_sa) ->sin_addr.s_addr;
20980Sstevel@tonic-gate 			} else {
20990Sstevel@tonic-gate 				faddr = 0;
21000Sstevel@tonic-gate 			}
21011974Sbrutus 			/* XXX need to pass response type, e.g. 200, 304 */
21020Sstevel@tonic-gate 			nl7c_logd_log(ruri, uri, so->so_nl7c_rtime, faddr);
21030Sstevel@tonic-gate 		}
21041974Sbrutus 		/*
21051974Sbrutus 		 * Release reference on request URI, send the response out
21061974Sbrutus 		 * the socket, release reference on response uri, set the
21071974Sbrutus 		 * *ret value to B_TRUE to indicate request was consumed
21081974Sbrutus 		 * then return B_FALSE to indcate no more data needed.
21091974Sbrutus 		 */
21101974Sbrutus 		REF_RELE(ruri);
21111974Sbrutus 		(void) uri_response(so, uri);
21120Sstevel@tonic-gate 		REF_RELE(uri);
21130Sstevel@tonic-gate 		*ret = B_TRUE;
21140Sstevel@tonic-gate 		return (B_FALSE);
21150Sstevel@tonic-gate 	}
21160Sstevel@tonic-gate 	/*
21171974Sbrutus 	 * Miss the cache, the request URI is in the cache waiting for
21181974Sbrutus 	 * application write-side data to fill it.
21190Sstevel@tonic-gate 	 */
21200Sstevel@tonic-gate 	nl7c_uri_miss++;
21210Sstevel@tonic-gate temp:
21221974Sbrutus 	/*
21231974Sbrutus 	 * A miss or temp URI for which response data is needed, link
21241974Sbrutus 	 * uri to so and so to uri, set WAITWRITE in the so such that
21251974Sbrutus 	 * read-side processing is suspended (so the next read() gets
21261974Sbrutus 	 * the request data) until a write() is processed by NL7C.
21271974Sbrutus 	 *
21281974Sbrutus 	 * Note, so->so_nl7c_uri now owns the REF_INIT() ref.
21291974Sbrutus 	 */
21300Sstevel@tonic-gate 	uri->proc = so;
21310Sstevel@tonic-gate 	so->so_nl7c_uri = uri;
21320Sstevel@tonic-gate 	so->so_nl7c_flags |= NL7C_WAITWRITE;
21330Sstevel@tonic-gate 	*ret = B_FALSE;
21340Sstevel@tonic-gate 	return (B_FALSE);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate more:
21370Sstevel@tonic-gate 	/* More data is needed, note fragmented recv not supported */
21380Sstevel@tonic-gate 	nl7c_uri_more++;
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate pass:
21410Sstevel@tonic-gate 	/* Pass on this request */
21420Sstevel@tonic-gate 	nl7c_uri_pass++;
21430Sstevel@tonic-gate 	nl7c_uri_request++;
21440Sstevel@tonic-gate 	if (ruri != NULL) {
21450Sstevel@tonic-gate 		REF_RELE(ruri);
21460Sstevel@tonic-gate 	}
21470Sstevel@tonic-gate 	if (uri) {
21480Sstevel@tonic-gate 		REF_RELE(uri);
21490Sstevel@tonic-gate 	}
21500Sstevel@tonic-gate 	so->so_nl7c_flags = 0;
21510Sstevel@tonic-gate 	*ret = B_FALSE;
21520Sstevel@tonic-gate 	return (B_FALSE);
21530Sstevel@tonic-gate }
2154