xref: /onnv-gate/usr/src/uts/common/fs/sockfs/nl7curi.c (revision 12714:711e35fbf43b)
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*12714Sanil.udupa@sun.com  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/strsubr.h>
260Sstevel@tonic-gate #include <sys/strsun.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/sysmacros.h>
290Sstevel@tonic-gate #include <vm/seg_map.h>
300Sstevel@tonic-gate #include <vm/seg_kpm.h>
310Sstevel@tonic-gate #include <sys/condvar_impl.h>
320Sstevel@tonic-gate #include <sys/sendfile.h>
330Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
340Sstevel@tonic-gate #include <fs/sockfs/nl7curi.h>
358348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.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
nl7c_uri_init(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
nl7c_mi_report_hash(mblk_t * mp)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
nl7c_mi_report_uri(mblk_t * mp)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
nl7c_uri_inactive(uri_desc_t * uri)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
uri_kmc_reclaim(void * arg)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
uri_delete(uri_desc_t * del)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
uri_add(uri_desc_t * uri,krw_t rwlock,boolean_t nonblocking)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 *
uri_lookup(uri_desc_t * ruri,boolean_t add,boolean_t nonblocking)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 		 */
82311066Srafael.vanoni@sun.com 		if (uri->expire >= 0 && uri->expire <= ddi_get_lbolt() ||
82411066Srafael.vanoni@sun.com 		    ruri->nocache) {
8250Sstevel@tonic-gate 			/*
8260Sstevel@tonic-gate 			 * URI has expired or request specified to not use
8270Sstevel@tonic-gate 			 * the cached version, unlink the URI from the hash
8280Sstevel@tonic-gate 			 * chain, release all locks, release the hash ref
8290Sstevel@tonic-gate 			 * on the URI, and last look it up again.
8301974Sbrutus 			 *
8311974Sbrutus 			 * Note, this will cause all variants of the named
8321974Sbrutus 			 * URI to be purged.
8330Sstevel@tonic-gate 			 */
8340Sstevel@tonic-gate 			if (puri != NULL) {
8350Sstevel@tonic-gate 				puri->hash = uri->hash;
8360Sstevel@tonic-gate 			} else {
8370Sstevel@tonic-gate 				hp->list = uri->hash;
8380Sstevel@tonic-gate 			}
8390Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8400Sstevel@tonic-gate 			atomic_add_32(&uri_hash_cnt[cur], -1);
8410Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8421974Sbrutus 			if (ruri->nocache)
8431974Sbrutus 				nl7c_uri_purge++;
8441974Sbrutus 			else
8451974Sbrutus 				nl7c_uri_expire++;
8460Sstevel@tonic-gate 			REF_RELE(uri);
8470Sstevel@tonic-gate 			goto again;
8480Sstevel@tonic-gate 		}
8491974Sbrutus 		if (uri->scheme != NULL) {
8501974Sbrutus 			/*
8511974Sbrutus 			 * URI has scheme private qualifier(s), if request
8521974Sbrutus 			 * URI doesn't or if no match skip this URI.
8531974Sbrutus 			 */
8541974Sbrutus 			if (ruri->scheme == NULL ||
8551974Sbrutus 			    ! nl7c_http_cmp(uri->scheme, ruri->scheme))
8561974Sbrutus 				goto nexturi;
8571974Sbrutus 		} else if (ruri->scheme != NULL) {
8581974Sbrutus 			/*
8591974Sbrutus 			 * URI doesn't have scheme private qualifiers but
8601974Sbrutus 			 * request URI does, no match, skip this URI.
8611974Sbrutus 			 */
8621974Sbrutus 			goto nexturi;
8631974Sbrutus 		}
8640Sstevel@tonic-gate 		/*
8651974Sbrutus 		 * Have a match, ready URI for return, first put a reference
8661974Sbrutus 		 * hold on the URI, if this URI is currently being processed
8671974Sbrutus 		 * then have to wait for the processing to be completed and
8681974Sbrutus 		 * redo the lookup, else return it.
8690Sstevel@tonic-gate 		 */
8700Sstevel@tonic-gate 		REF_HOLD(uri);
8710Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
8720Sstevel@tonic-gate 		if (uri->proc != NULL) {
8730Sstevel@tonic-gate 			/* The URI is being processed, wait for completion */
8740Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8750Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8760Sstevel@tonic-gate 			if (! nonblocking &&
8770Sstevel@tonic-gate 			    cv_wait_sig(&uri->waiting, &uri->proclock)) {
8780Sstevel@tonic-gate 				/*
8790Sstevel@tonic-gate 				 * URI has been processed but things may
8800Sstevel@tonic-gate 				 * have changed while we were away so do
8810Sstevel@tonic-gate 				 * most everything again.
8820Sstevel@tonic-gate 				 */
8830Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8840Sstevel@tonic-gate 				REF_RELE(uri);
8850Sstevel@tonic-gate 				goto again;
8860Sstevel@tonic-gate 			} else {
8870Sstevel@tonic-gate 				/*
8880Sstevel@tonic-gate 				 * A nonblocking socket or an interrupted
8890Sstevel@tonic-gate 				 * cv_wait_sig() in the first case can't
8900Sstevel@tonic-gate 				 * block waiting for the processing of the
8910Sstevel@tonic-gate 				 * uri hash hit uri to complete, in both
8920Sstevel@tonic-gate 				 * cases just return failure to lookup.
8930Sstevel@tonic-gate 				 */
8940Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8950Sstevel@tonic-gate 				REF_RELE(uri);
8960Sstevel@tonic-gate 				return (NULL);
8970Sstevel@tonic-gate 			}
8980Sstevel@tonic-gate 		}
8991974Sbrutus 		mutex_exit(&uri->proclock);
9000Sstevel@tonic-gate 		uri->hit++;
9010Sstevel@tonic-gate 		mutex_exit(&hp->lock);
9020Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9030Sstevel@tonic-gate 		return (uri);
9040Sstevel@tonic-gate 	nexturi:
9050Sstevel@tonic-gate 		puri = uri;
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 	mutex_exit(&hp->lock);
9080Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
9090Sstevel@tonic-gate 		/*
9100Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
9110Sstevel@tonic-gate 		 * check the new hash next.
9120Sstevel@tonic-gate 		 */
9130Sstevel@tonic-gate 		cur = new;
9140Sstevel@tonic-gate 		goto nexthash;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate add:
9170Sstevel@tonic-gate 	if (! add) {
9181974Sbrutus 		/* Lookup only so return failure */
9190Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9200Sstevel@tonic-gate 		return (NULL);
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 	/*
9230Sstevel@tonic-gate 	 * URI not hashed, finish intialization of the
9240Sstevel@tonic-gate 	 * request URI, add it to the hash, return it.
9250Sstevel@tonic-gate 	 */
9260Sstevel@tonic-gate 	ruri->hit = 0;
9270Sstevel@tonic-gate 	ruri->expire = -1;
9280Sstevel@tonic-gate 	ruri->response.sz = 0;
9291974Sbrutus 	ruri->proc = (struct sonode *)~NULL;
9300Sstevel@tonic-gate 	cv_init(&ruri->waiting, NULL, CV_DEFAULT, NULL);
9310Sstevel@tonic-gate 	mutex_init(&ruri->proclock, NULL, MUTEX_DEFAULT, NULL);
9320Sstevel@tonic-gate 	uri_add(ruri, RW_READER, nonblocking);
9330Sstevel@tonic-gate 	/* uri_add() has done rw_exit(&uri_hash_access) */
9340Sstevel@tonic-gate 	return (ruri);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate  * Reclaim URIs until max cache size threshold has been reached.
9390Sstevel@tonic-gate  *
9400Sstevel@tonic-gate  * A CLOCK based reclaim modified with a history (hit counter) counter.
9410Sstevel@tonic-gate  */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate static void
nl7c_uri_reclaim(void)9440Sstevel@tonic-gate nl7c_uri_reclaim(void)
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	uri_hash_t	*hp, *start, *pend;
9470Sstevel@tonic-gate 	uri_desc_t	*uri;
9480Sstevel@tonic-gate 	uri_desc_t	*puri;
9490Sstevel@tonic-gate 	uint32_t	cur;
9500Sstevel@tonic-gate 	uint32_t	new;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	nl7c_uri_reclaim_calls++;
9530Sstevel@tonic-gate again:
9540Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
9550Sstevel@tonic-gate 	cur = uri_hash_which;
9560Sstevel@tonic-gate 	new = cur ? 0 : 1;
9570Sstevel@tonic-gate next:
9580Sstevel@tonic-gate 	hp = uri_hash_lru[cur];
9590Sstevel@tonic-gate 	start = hp;
9600Sstevel@tonic-gate 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
9610Sstevel@tonic-gate 	while (nl7c_uri_bytes > nl7c_uri_max) {
9620Sstevel@tonic-gate 		puri = NULL;
9630Sstevel@tonic-gate 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
9640Sstevel@tonic-gate 			if (uri->hit != 0) {
9650Sstevel@tonic-gate 				/*
9660Sstevel@tonic-gate 				 * Decrement URI activity counter and skip.
9670Sstevel@tonic-gate 				 */
9680Sstevel@tonic-gate 				uri->hit--;
9690Sstevel@tonic-gate 				puri = uri;
9700Sstevel@tonic-gate 				continue;
9710Sstevel@tonic-gate 			}
9720Sstevel@tonic-gate 			if (uri->proc != NULL) {
9730Sstevel@tonic-gate 				/*
9740Sstevel@tonic-gate 				 * Currently being processed by a socket, skip.
9750Sstevel@tonic-gate 				 */
9760Sstevel@tonic-gate 				continue;
9770Sstevel@tonic-gate 			}
9780Sstevel@tonic-gate 			/*
9790Sstevel@tonic-gate 			 * Found a candidate, no hit(s) since added or last
9800Sstevel@tonic-gate 			 * reclaim pass, unlink from it's hash chain, update
9810Sstevel@tonic-gate 			 * lru scan pointer, drop lock, ref release it.
9820Sstevel@tonic-gate 			 */
9830Sstevel@tonic-gate 			URI_HASH_UNLINK(cur, new, hp, puri, uri);
9840Sstevel@tonic-gate 			if (cur == uri_hash_which) {
9850Sstevel@tonic-gate 				if (++hp == pend) {
9860Sstevel@tonic-gate 					/* Wrap pointer */
9870Sstevel@tonic-gate 					hp = uri_hash_ab[cur];
9880Sstevel@tonic-gate 				}
9890Sstevel@tonic-gate 				uri_hash_lru[cur] = hp;
9900Sstevel@tonic-gate 			}
9910Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
9920Sstevel@tonic-gate 			REF_RELE(uri);
9930Sstevel@tonic-gate 			nl7c_uri_reclaim_cnt++;
9940Sstevel@tonic-gate 			goto again;
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 		if (++hp == pend) {
9970Sstevel@tonic-gate 			/* Wrap pointer */
9980Sstevel@tonic-gate 			hp = uri_hash_ab[cur];
9990Sstevel@tonic-gate 		}
10000Sstevel@tonic-gate 		if (hp == start) {
10010Sstevel@tonic-gate 			if (cur != new && uri_hash_ab[new] != NULL) {
10020Sstevel@tonic-gate 				/*
10030Sstevel@tonic-gate 				 * Done with the current hash and have a
10040Sstevel@tonic-gate 				 * new hash so check the new hash next.
10050Sstevel@tonic-gate 				 */
10060Sstevel@tonic-gate 				cur = new;
10070Sstevel@tonic-gate 				goto next;
10080Sstevel@tonic-gate 			}
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate /*
10150Sstevel@tonic-gate  * Called for a socket which is being freed prior to close, e.g. errored.
10160Sstevel@tonic-gate  */
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate void
nl7c_urifree(struct sonode * so)10190Sstevel@tonic-gate nl7c_urifree(struct sonode *so)
10200Sstevel@tonic-gate {
10218348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
10228348SEric.Yu@Sun.COM 	uri_desc_t *uri = (uri_desc_t *)sti->sti_nl7c_uri;
10230Sstevel@tonic-gate 
10248348SEric.Yu@Sun.COM 	sti->sti_nl7c_uri = NULL;
10250Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
10260Sstevel@tonic-gate 		uri_delete(uri);
10270Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
10280Sstevel@tonic-gate 		uri->proc = NULL;
10290Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
10300Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
10310Sstevel@tonic-gate 		}
10320Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
10330Sstevel@tonic-gate 		nl7c_uri_free++;
10340Sstevel@tonic-gate 	} else {
10350Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
10360Sstevel@tonic-gate 		uri->proc = NULL;
10370Sstevel@tonic-gate 		nl7c_uri_temp_free++;
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	REF_RELE(uri);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate /*
10431974Sbrutus  * ...
10441974Sbrutus  *
10451974Sbrutus  *	< 0	need more data
10461974Sbrutus  *
10471974Sbrutus  *	  0	parse complete
10481974Sbrutus  *
10491974Sbrutus  *	> 0	parse error
10500Sstevel@tonic-gate  */
10510Sstevel@tonic-gate 
10521974Sbrutus volatile uint64_t nl7c_resp_pfail = 0;
10531974Sbrutus volatile uint64_t nl7c_resp_ntemp = 0;
10541974Sbrutus volatile uint64_t nl7c_resp_pass = 0;
10550Sstevel@tonic-gate 
10561974Sbrutus static int
nl7c_resp_parse(struct sonode * so,uri_desc_t * uri,char * data,int sz)10571974Sbrutus nl7c_resp_parse(struct sonode *so, uri_desc_t *uri, char *data, int sz)
10581974Sbrutus {
10591974Sbrutus 	if (! nl7c_http_response(&data, &data[sz], uri, so)) {
10601974Sbrutus 		if (data == NULL) {
10611974Sbrutus 			/* Parse fail */
10621974Sbrutus 			goto pfail;
10631974Sbrutus 		}
10641974Sbrutus 		/* More data */
10651974Sbrutus 		data = NULL;
10661974Sbrutus 	} else if (data == NULL) {
10671974Sbrutus 		goto pass;
10681974Sbrutus 	}
10691974Sbrutus 	if (uri->hash != URI_TEMP && uri->nocache) {
10701974Sbrutus 		/*
10711974Sbrutus 		 * After response parse now no cache,
10721974Sbrutus 		 * delete it from cache, wakeup any
10731974Sbrutus 		 * waiters on this URI, make URI_TEMP.
10741974Sbrutus 		 */
10751974Sbrutus 		uri_delete(uri);
10761974Sbrutus 		mutex_enter(&uri->proclock);
10771974Sbrutus 		if (CV_HAS_WAITERS(&uri->waiting)) {
10781974Sbrutus 			cv_broadcast(&uri->waiting);
10791974Sbrutus 		}
10801974Sbrutus 		mutex_exit(&uri->proclock);
10811974Sbrutus 		uri->hash = URI_TEMP;
10821974Sbrutus 		nl7c_uri_temp_mk++;
10831974Sbrutus 	}
10841974Sbrutus 	if (data == NULL) {
10851974Sbrutus 		/* More data needed */
10861974Sbrutus 		return (-1);
10871974Sbrutus 	}
10881974Sbrutus 	/* Success */
10891974Sbrutus 	return (0);
10901974Sbrutus 
10911974Sbrutus pfail:
10921974Sbrutus 	nl7c_resp_pfail++;
10931974Sbrutus 	return (EINVAL);
10941974Sbrutus 
10951974Sbrutus pass:
10961974Sbrutus 	nl7c_resp_pass++;
10971974Sbrutus 	return (ENOTSUP);
10981974Sbrutus }
10991974Sbrutus 
11001974Sbrutus /*
11011974Sbrutus  * Called to sink application response data, the processing of the data
11021974Sbrutus  * is the same for a cached or temp URI (i.e. a URI for which we aren't
11031974Sbrutus  * going to cache the URI but want to parse it for detecting response
11041974Sbrutus  * data end such that for a persistent connection we can parse the next
11051974Sbrutus  * request).
11061974Sbrutus  *
11071974Sbrutus  * On return 0 is returned for sink success, > 0 on error, and < 0 on
11081974Sbrutus  * no so URI (note, data not sinked).
11091974Sbrutus  */
11101974Sbrutus 
11111974Sbrutus int
nl7c_data(struct sonode * so,uio_t * uio)11121974Sbrutus nl7c_data(struct sonode *so, uio_t *uio)
11130Sstevel@tonic-gate {
11148348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
11158348SEric.Yu@Sun.COM 	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
11161974Sbrutus 	iovec_t		*iov;
11171974Sbrutus 	int		cnt;
11181974Sbrutus 	int		sz = uio->uio_resid;
11191974Sbrutus 	char		*data, *alloc;
11200Sstevel@tonic-gate 	char		*bp;
11210Sstevel@tonic-gate 	uri_rd_t	*rdp;
11221974Sbrutus 	boolean_t	first;
11231974Sbrutus 	int		error, perror;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	nl7c_uri_data++;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if (uri == NULL) {
11280Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
11298348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags = 0;
11300Sstevel@tonic-gate 		nl7c_uri_NULL1++;
11311974Sbrutus 		return (-1);
11321974Sbrutus 	}
11331974Sbrutus 
11348348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_flags & NL7C_WAITWRITE) {
11358348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
11361974Sbrutus 		first = B_TRUE;
11371974Sbrutus 	} else {
11381974Sbrutus 		first = B_FALSE;
11391974Sbrutus 	}
11401974Sbrutus 
11411974Sbrutus 	alloc = kmem_alloc(sz, KM_SLEEP);
11421974Sbrutus 	URI_RD_ADD(uri, rdp, sz, -1);
11431974Sbrutus 	if (rdp == NULL) {
11441974Sbrutus 		error = ENOMEM;
11451974Sbrutus 		goto fail;
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 
11481974Sbrutus 	if (uri->hash != URI_TEMP && uri->count > nca_max_cache_size) {
11491974Sbrutus 		uri_delete(uri);
11501974Sbrutus 		uri->hash = URI_TEMP;
11510Sstevel@tonic-gate 	}
11521974Sbrutus 	data = alloc;
11531974Sbrutus 	alloc = NULL;
11541974Sbrutus 	rdp->data.kmem = data;
11551974Sbrutus 	atomic_add_64(&nl7c_uri_bytes, sz);
11561974Sbrutus 
11570Sstevel@tonic-gate 	bp = data;
11581974Sbrutus 	while (uio->uio_resid > 0) {
11591974Sbrutus 		iov = uio->uio_iov;
11601974Sbrutus 		if ((cnt = iov->iov_len) == 0) {
11611974Sbrutus 			goto next;
11621974Sbrutus 		}
11631974Sbrutus 		cnt = MIN(cnt, uio->uio_resid);
11641974Sbrutus 		error = xcopyin(iov->iov_base, bp, cnt);
11651974Sbrutus 		if (error)
11660Sstevel@tonic-gate 			goto fail;
11671974Sbrutus 
11681974Sbrutus 		iov->iov_base += cnt;
11691974Sbrutus 		iov->iov_len -= cnt;
11701974Sbrutus 		uio->uio_resid -= cnt;
11711974Sbrutus 		uio->uio_loffset += cnt;
11720Sstevel@tonic-gate 		bp += cnt;
11731974Sbrutus 	next:
11741974Sbrutus 		uio->uio_iov++;
11751974Sbrutus 		uio->uio_iovcnt--;
11760Sstevel@tonic-gate 	}
11771974Sbrutus 
11781974Sbrutus 	/* Successfull sink of data, response parse the data */
11791974Sbrutus 	perror = nl7c_resp_parse(so, uri, data, sz);
11801974Sbrutus 
11811974Sbrutus 	/* Send the data out the connection */
11821974Sbrutus 	error = uri_rd_response(so, uri, rdp, first);
11831974Sbrutus 	if (error)
11841974Sbrutus 		goto fail;
11851974Sbrutus 
11861974Sbrutus 	/* Success */
11871974Sbrutus 	if (perror == 0 &&
11881974Sbrutus 	    ((uri->respclen == URI_LEN_NOVALUE &&
11891974Sbrutus 	    uri->resplen == URI_LEN_NOVALUE) ||
11901974Sbrutus 	    uri->count >= uri->resplen)) {
11910Sstevel@tonic-gate 		/*
11921974Sbrutus 		 * No more data needed and no pending response
11931974Sbrutus 		 * data or current data count >= response length
11941974Sbrutus 		 * so close the URI processing for this so.
11950Sstevel@tonic-gate 		 */
11961974Sbrutus 		nl7c_close(so);
11978348SEric.Yu@Sun.COM 		if (! (sti->sti_nl7c_flags & NL7C_SOPERSIST)) {
11981974Sbrutus 			/* Not a persistent connection */
11998348SEric.Yu@Sun.COM 			sti->sti_nl7c_flags = 0;
12001974Sbrutus 		}
12010Sstevel@tonic-gate 	}
12021974Sbrutus 
12031974Sbrutus 	return (0);
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate fail:
12060Sstevel@tonic-gate 	if (alloc != NULL) {
12070Sstevel@tonic-gate 		kmem_free(alloc, sz);
12080Sstevel@tonic-gate 	}
12098348SEric.Yu@Sun.COM 	sti->sti_nl7c_flags = 0;
12100Sstevel@tonic-gate 	nl7c_urifree(so);
12111974Sbrutus 
12121974Sbrutus 	return (error);
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate  * Called to read data from file "*fp" at offset "*off" of length "*len"
12170Sstevel@tonic-gate  * for a maximum of "*max_rem" bytes.
12180Sstevel@tonic-gate  *
12190Sstevel@tonic-gate  * On success a pointer to the kmem_alloc()ed file data is returned, "*off"
12200Sstevel@tonic-gate  * and "*len" are updated for the acutal number of bytes read and "*max_rem"
12210Sstevel@tonic-gate  * is updated with the number of bytes remaining to be read.
12220Sstevel@tonic-gate  *
12230Sstevel@tonic-gate  * Else, "NULL" is returned.
12240Sstevel@tonic-gate  */
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate static char *
nl7c_readfile(file_t * fp,u_offset_t * off,int * len,int max,int * ret)12271974Sbrutus nl7c_readfile(file_t *fp, u_offset_t *off, int *len, int max, int *ret)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	vnode_t	*vp = fp->f_vnode;
12300Sstevel@tonic-gate 	int	flg = 0;
12311974Sbrutus 	size_t	size = MIN(*len, max);
12320Sstevel@tonic-gate 	char	*data;
12330Sstevel@tonic-gate 	int	error;
12340Sstevel@tonic-gate 	uio_t	uio;
12350Sstevel@tonic-gate 	iovec_t	iov;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, flg, NULL);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	if (*off > MAXOFFSET_T) {
12400Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, flg, NULL);
12411974Sbrutus 		*ret = EFBIG;
12420Sstevel@tonic-gate 		return (NULL);
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	if (*off + size > MAXOFFSET_T)
12460Sstevel@tonic-gate 		size = (ssize32_t)(MAXOFFSET_T - *off);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	data = kmem_alloc(size, KM_SLEEP);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	iov.iov_base = data;
12510Sstevel@tonic-gate 	iov.iov_len = size;
12520Sstevel@tonic-gate 	uio.uio_loffset = *off;
12530Sstevel@tonic-gate 	uio.uio_iov = &iov;
12540Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
12550Sstevel@tonic-gate 	uio.uio_resid = size;
12560Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
12570Sstevel@tonic-gate 	uio.uio_llimit = MAXOFFSET_T;
12580Sstevel@tonic-gate 	uio.uio_fmode = fp->f_flag;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	error = VOP_READ(vp, &uio, fp->f_flag, fp->f_cred, NULL);
12610Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, flg, NULL);
12621974Sbrutus 	*ret = error;
12630Sstevel@tonic-gate 	if (error) {
12640Sstevel@tonic-gate 		kmem_free(data, size);
12650Sstevel@tonic-gate 		return (NULL);
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 	*len = size;
12680Sstevel@tonic-gate 	*off += size;
12690Sstevel@tonic-gate 	return (data);
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate /*
12731974Sbrutus  * Called to sink application response sendfilev, as with nl7c_data() above
12741974Sbrutus  * all the data will be processed by NL7C unless there's an error.
12750Sstevel@tonic-gate  */
12760Sstevel@tonic-gate 
12771974Sbrutus int
nl7c_sendfilev(struct sonode * so,u_offset_t * fileoff,sendfilevec_t * sfvp,int sfvc,ssize_t * xfer)12781974Sbrutus nl7c_sendfilev(struct sonode *so, u_offset_t *fileoff, sendfilevec_t *sfvp,
12791974Sbrutus 	int sfvc, ssize_t *xfer)
12800Sstevel@tonic-gate {
12818348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
12828348SEric.Yu@Sun.COM 	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
12830Sstevel@tonic-gate 	file_t		*fp = NULL;
12840Sstevel@tonic-gate 	vnode_t		*vp = NULL;
12850Sstevel@tonic-gate 	char		*data = NULL;
12861974Sbrutus 	u_offset_t	off;
12870Sstevel@tonic-gate 	int		len;
12881974Sbrutus 	int		cnt;
12890Sstevel@tonic-gate 	int		total_count = 0;
12901974Sbrutus 	char		*alloc;
12910Sstevel@tonic-gate 	uri_rd_t	*rdp;
12921974Sbrutus 	int		max;
12931974Sbrutus 	int		perror;
12940Sstevel@tonic-gate 	int		error = 0;
12951974Sbrutus 	boolean_t	first = B_TRUE;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	nl7c_uri_sendfilev++;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (uri == NULL) {
13000Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
13018348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags = 0;
13020Sstevel@tonic-gate 		nl7c_uri_NULL2++;
13031974Sbrutus 		return (0);
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 
13068348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_flags & NL7C_WAITWRITE)
13078348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	while (sfvc-- > 0) {
13100Sstevel@tonic-gate 		/*
13110Sstevel@tonic-gate 		 * off - the current sfv read file offset or user address.
13120Sstevel@tonic-gate 		 *
13131974Sbrutus 		 * len - the current sfv length in bytes.
13141974Sbrutus 		 *
13151974Sbrutus 		 * cnt - number of bytes kmem_alloc()ed.
13160Sstevel@tonic-gate 		 *
13171974Sbrutus 		 * alloc - the kmem_alloc()ed buffer of size "cnt".
13180Sstevel@tonic-gate 		 *
13191974Sbrutus 		 * data - copy of "alloc" used for post alloc references.
13200Sstevel@tonic-gate 		 *
13210Sstevel@tonic-gate 		 * fp - the current sfv file_t pointer.
13220Sstevel@tonic-gate 		 *
13230Sstevel@tonic-gate 		 * vp - the current "*vp" vnode_t pointer.
13240Sstevel@tonic-gate 		 *
13250Sstevel@tonic-gate 		 * Note, for "data" and "fp" and "vp" a NULL value is used
13260Sstevel@tonic-gate 		 * when not allocated such that the common failure path "fail"
13270Sstevel@tonic-gate 		 * is used.
13280Sstevel@tonic-gate 		 */
13290Sstevel@tonic-gate 		off = sfvp->sfv_off;
13300Sstevel@tonic-gate 		len = sfvp->sfv_len;
13311974Sbrutus 		cnt = len;
13328171SPrakash.Jalan@Sun.COM 
13338171SPrakash.Jalan@Sun.COM 		if (len == 0) {
13348171SPrakash.Jalan@Sun.COM 			sfvp++;
13358171SPrakash.Jalan@Sun.COM 			continue;
13368171SPrakash.Jalan@Sun.COM 		}
13378171SPrakash.Jalan@Sun.COM 
13381974Sbrutus 		if (sfvp->sfv_fd == SFV_FD_SELF) {
13390Sstevel@tonic-gate 			/*
13401974Sbrutus 			 * User memory, copyin() all the bytes.
13410Sstevel@tonic-gate 			 */
13421974Sbrutus 			alloc = kmem_alloc(cnt, KM_SLEEP);
13431974Sbrutus 			error = xcopyin((caddr_t)(uintptr_t)off, alloc, cnt);
13440Sstevel@tonic-gate 			if (error)
13450Sstevel@tonic-gate 				goto fail;
13461974Sbrutus 		} else {
13471974Sbrutus 			/*
13481974Sbrutus 			 * File descriptor, prefetch some bytes.
13491974Sbrutus 			 */
13501974Sbrutus 			if ((fp = getf(sfvp->sfv_fd)) == NULL) {
13511974Sbrutus 				error = EBADF;
13520Sstevel@tonic-gate 				goto fail;
13530Sstevel@tonic-gate 			}
13541974Sbrutus 			if ((fp->f_flag & FREAD) == 0) {
13551974Sbrutus 				error = EACCES;
13561974Sbrutus 				goto fail;
13570Sstevel@tonic-gate 			}
13581974Sbrutus 			vp = fp->f_vnode;
13591974Sbrutus 			if (vp->v_type != VREG) {
13601974Sbrutus 				error = EINVAL;
13610Sstevel@tonic-gate 				goto fail;
13621974Sbrutus 			}
13630Sstevel@tonic-gate 			VN_HOLD(vp);
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 			/* Read max_rem bytes from file for prefetch */
13660Sstevel@tonic-gate 			if (nl7c_use_kmem) {
13671974Sbrutus 				max = cnt;
13680Sstevel@tonic-gate 			} else {
13691974Sbrutus 				max = MAXBSIZE * nl7c_file_prefetch;
13700Sstevel@tonic-gate 			}
13711974Sbrutus 			alloc = nl7c_readfile(fp, &off, &cnt, max, &error);
13721974Sbrutus 			if (alloc == NULL)
13730Sstevel@tonic-gate 				goto fail;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 			releasef(sfvp->sfv_fd);
13760Sstevel@tonic-gate 			fp = NULL;
13771974Sbrutus 		}
13781974Sbrutus 		URI_RD_ADD(uri, rdp, cnt, -1);
13791974Sbrutus 		if (rdp == NULL) {
13801974Sbrutus 			error = ENOMEM;
13811974Sbrutus 			goto fail;
13821974Sbrutus 		}
13831974Sbrutus 		data = alloc;
13841974Sbrutus 		alloc = NULL;
13851974Sbrutus 		rdp->data.kmem = data;
13861974Sbrutus 		total_count += cnt;
13871974Sbrutus 		if (uri->hash != URI_TEMP && total_count > nca_max_cache_size) {
13881974Sbrutus 			uri_delete(uri);
13891974Sbrutus 			uri->hash = URI_TEMP;
13901974Sbrutus 		}
13910Sstevel@tonic-gate 
13921974Sbrutus 		/* Response parse */
13931974Sbrutus 		perror = nl7c_resp_parse(so, uri, data, len);
13941974Sbrutus 
13951974Sbrutus 		/* Send kmem data out the connection */
13961974Sbrutus 		error = uri_rd_response(so, uri, rdp, first);
13971974Sbrutus 
13981974Sbrutus 		if (error)
13991974Sbrutus 			goto fail;
14000Sstevel@tonic-gate 
14011974Sbrutus 		if (sfvp->sfv_fd != SFV_FD_SELF) {
14021974Sbrutus 			/*
14031974Sbrutus 			 * File descriptor, if any bytes left save vnode_t.
14041974Sbrutus 			 */
14051974Sbrutus 			if (len > cnt) {
14061974Sbrutus 				/* More file data so add it */
14071974Sbrutus 				URI_RD_ADD(uri, rdp, len - cnt, off);
14081974Sbrutus 				if (rdp == NULL) {
14091974Sbrutus 					error = ENOMEM;
14101974Sbrutus 					goto fail;
14111974Sbrutus 				}
14121974Sbrutus 				rdp->data.vnode = vp;
14131974Sbrutus 
14141974Sbrutus 				/* Send vnode data out the connection */
14151974Sbrutus 				error = uri_rd_response(so, uri, rdp, first);
14160Sstevel@tonic-gate 			} else {
14171974Sbrutus 				/* All file data fit in the prefetch */
14181974Sbrutus 				VN_RELE(vp);
14190Sstevel@tonic-gate 			}
14201974Sbrutus 			*fileoff += len;
14211974Sbrutus 			vp = NULL;
14220Sstevel@tonic-gate 		}
14231974Sbrutus 		*xfer += len;
14240Sstevel@tonic-gate 		sfvp++;
14251974Sbrutus 
14261974Sbrutus 		if (first)
14271974Sbrutus 			first = B_FALSE;
14280Sstevel@tonic-gate 	}
14290Sstevel@tonic-gate 	if (total_count > 0) {
14300Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, total_count);
14310Sstevel@tonic-gate 	}
14321974Sbrutus 	if (perror == 0 &&
14331974Sbrutus 	    ((uri->respclen == URI_LEN_NOVALUE &&
14341974Sbrutus 	    uri->resplen == URI_LEN_NOVALUE) ||
14351974Sbrutus 	    uri->count >= uri->resplen)) {
14361974Sbrutus 		/*
14371974Sbrutus 		 * No more data needed and no pending response
14381974Sbrutus 		 * data or current data count >= response length
14391974Sbrutus 		 * so close the URI processing for this so.
14401974Sbrutus 		 */
14410Sstevel@tonic-gate 		nl7c_close(so);
14428348SEric.Yu@Sun.COM 		if (! (sti->sti_nl7c_flags & NL7C_SOPERSIST)) {
14431974Sbrutus 			/* Not a persistent connection */
14448348SEric.Yu@Sun.COM 			sti->sti_nl7c_flags = 0;
14451974Sbrutus 		}
14460Sstevel@tonic-gate 	}
14471974Sbrutus 
14481974Sbrutus 	return (0);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate fail:
14518348SEric.Yu@Sun.COM 	if (error == EPIPE)
14528348SEric.Yu@Sun.COM 		tsignal(curthread, SIGPIPE);
14538348SEric.Yu@Sun.COM 
14541974Sbrutus 	if (alloc != NULL)
14550Sstevel@tonic-gate 		kmem_free(data, len);
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	if (vp != NULL)
14580Sstevel@tonic-gate 		VN_RELE(vp);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	if (fp != NULL)
14610Sstevel@tonic-gate 		releasef(sfvp->sfv_fd);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	if (total_count > 0) {
14640Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, total_count);
14650Sstevel@tonic-gate 	}
14661974Sbrutus 
14678348SEric.Yu@Sun.COM 	sti->sti_nl7c_flags = 0;
14680Sstevel@tonic-gate 	nl7c_urifree(so);
14691974Sbrutus 
14701974Sbrutus 	return (error);
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate /*
14740Sstevel@tonic-gate  * Called for a socket which is closing or when an application has
14750Sstevel@tonic-gate  * completed sending all the response data (i.e. for a persistent
14760Sstevel@tonic-gate  * connection called once for each completed application response).
14770Sstevel@tonic-gate  */
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate void
nl7c_close(struct sonode * so)14800Sstevel@tonic-gate nl7c_close(struct sonode *so)
14810Sstevel@tonic-gate {
14828348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
14838348SEric.Yu@Sun.COM 	uri_desc_t 	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	if (uri == NULL) {
14860Sstevel@tonic-gate 		/*
14870Sstevel@tonic-gate 		 * No URI being processed so might be a listen()er
14880Sstevel@tonic-gate 		 * if so do any cleanup, else nothing more to do.
14890Sstevel@tonic-gate 		 */
14900Sstevel@tonic-gate 		if (so->so_state & SS_ACCEPTCONN) {
14910Sstevel@tonic-gate 			(void) nl7c_close_addr(so);
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 		return;
14940Sstevel@tonic-gate 	}
14958348SEric.Yu@Sun.COM 	sti->sti_nl7c_uri = NULL;
14960Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
14970Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
14980Sstevel@tonic-gate 		uri->proc = NULL;
14990Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
15000Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
15030Sstevel@tonic-gate 		nl7c_uri_close++;
15040Sstevel@tonic-gate 	} else {
15050Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
15060Sstevel@tonic-gate 		uri->proc = NULL;
15070Sstevel@tonic-gate 		nl7c_uri_temp_close++;
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 	REF_RELE(uri);
15100Sstevel@tonic-gate 	if (nl7c_uri_max > 0 && nl7c_uri_bytes > nl7c_uri_max) {
15110Sstevel@tonic-gate 		nl7c_uri_reclaim();
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate /*
15160Sstevel@tonic-gate  * The uri_segmap_t ref_t inactive function called on the last REF_RELE(),
15170Sstevel@tonic-gate  * release the segmap mapping. Note, the uri_segmap_t will be freed by
15180Sstevel@tonic-gate  * REF_RELE() on return.
15190Sstevel@tonic-gate  */
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate void
uri_segmap_inactive(uri_segmap_t * smp)15220Sstevel@tonic-gate uri_segmap_inactive(uri_segmap_t *smp)
15230Sstevel@tonic-gate {
15240Sstevel@tonic-gate 	if (!segmap_kpm) {
15250Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap, smp->base,
15260Sstevel@tonic-gate 		    smp->len, F_SOFTUNLOCK, S_OTHER);
15270Sstevel@tonic-gate 	}
15280Sstevel@tonic-gate 	(void) segmap_release(segkmap, smp->base, SM_DONTNEED);
15290Sstevel@tonic-gate 	VN_RELE(smp->vp);
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate  * The call-back for desballoc()ed mblk_t's, if a segmap mapped mblk_t
15341974Sbrutus  * release the reference, one per desballoc() of a segmap page, if a rd_t
15351974Sbrutus  * mapped mblk_t release the reference, one per desballoc() of a uri_desc_t,
15361974Sbrutus  * last kmem free the uri_desb_t.
15370Sstevel@tonic-gate  */
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate static void
uri_desb_free(uri_desb_t * desb)15400Sstevel@tonic-gate uri_desb_free(uri_desb_t *desb)
15410Sstevel@tonic-gate {
15420Sstevel@tonic-gate 	if (desb->segmap != NULL) {
15430Sstevel@tonic-gate 		REF_RELE(desb->segmap);
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 	REF_RELE(desb->uri);
15460Sstevel@tonic-gate 	kmem_cache_free(uri_desb_kmc, desb);
15470Sstevel@tonic-gate }
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate /*
15500Sstevel@tonic-gate  * Segmap map up to a page of a uri_rd_t file descriptor.
15510Sstevel@tonic-gate  */
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate uri_segmap_t *
uri_segmap_map(uri_rd_t * rdp,int bytes)15540Sstevel@tonic-gate uri_segmap_map(uri_rd_t *rdp, int bytes)
15550Sstevel@tonic-gate {
15560Sstevel@tonic-gate 	uri_segmap_t	*segmap = kmem_cache_alloc(uri_segmap_kmc, KM_SLEEP);
15570Sstevel@tonic-gate 	int		len = MIN(rdp->sz, MAXBSIZE);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	if (len > bytes)
15600Sstevel@tonic-gate 		len = bytes;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	REF_INIT(segmap, 1, uri_segmap_inactive, uri_segmap_kmc);
15630Sstevel@tonic-gate 	segmap->len = len;
15640Sstevel@tonic-gate 	VN_HOLD(rdp->data.vnode);
15650Sstevel@tonic-gate 	segmap->vp = rdp->data.vnode;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	segmap->base = segmap_getmapflt(segkmap, segmap->vp, rdp->off, len,
15680Sstevel@tonic-gate 	    segmap_kpm ? SM_FAULT : 0, S_READ);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	if (segmap_fault(kas.a_hat, segkmap, segmap->base, len,
15710Sstevel@tonic-gate 	    F_SOFTLOCK, S_READ) != 0) {
15720Sstevel@tonic-gate 		REF_RELE(segmap);
15730Sstevel@tonic-gate 		return (NULL);
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	return (segmap);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate /*
15790Sstevel@tonic-gate  * Chop up the kernel virtual memory area *data of size *sz bytes for
15800Sstevel@tonic-gate  * a maximum of *bytes bytes into an besballoc()ed mblk_t chain using
15810Sstevel@tonic-gate  * the given template uri_desb_t *temp of max_mblk bytes per.
15820Sstevel@tonic-gate  *
15830Sstevel@tonic-gate  * The values of *data, *sz, and *bytes are updated on return, the
15840Sstevel@tonic-gate  * mblk_t chain is returned.
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate static mblk_t *
uri_desb_chop(char ** data,size_t * sz,int * bytes,uri_desb_t * temp,int max_mblk,char * eoh,mblk_t * persist)15880Sstevel@tonic-gate uri_desb_chop(
15890Sstevel@tonic-gate 	char 		**data,
15900Sstevel@tonic-gate 	size_t		*sz,
15910Sstevel@tonic-gate 	int 		*bytes,
15920Sstevel@tonic-gate 	uri_desb_t 	*temp,
15930Sstevel@tonic-gate 	int		max_mblk,
15940Sstevel@tonic-gate 	char		*eoh,
15950Sstevel@tonic-gate 	mblk_t		*persist
15960Sstevel@tonic-gate )
15970Sstevel@tonic-gate {
15980Sstevel@tonic-gate 	char		*ldata = *data;
15990Sstevel@tonic-gate 	size_t		lsz = *sz;
16000Sstevel@tonic-gate 	int		lbytes = bytes ? *bytes : lsz;
16010Sstevel@tonic-gate 	uri_desb_t	*desb;
16020Sstevel@tonic-gate 	mblk_t		*mp = NULL;
16031974Sbrutus 	mblk_t		*nmp, *pmp = NULL;
16040Sstevel@tonic-gate 	int		msz;
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	if (lbytes == 0 && lsz == 0)
16070Sstevel@tonic-gate 		return (NULL);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	while (lbytes > 0 && lsz > 0) {
16100Sstevel@tonic-gate 		msz = MIN(lbytes, max_mblk);
16110Sstevel@tonic-gate 		msz = MIN(msz, lsz);
16120Sstevel@tonic-gate 		if (persist && eoh >= ldata && eoh < &ldata[msz]) {
16130Sstevel@tonic-gate 			msz = (eoh - ldata);
16140Sstevel@tonic-gate 			pmp = persist;
16150Sstevel@tonic-gate 			persist = NULL;
16161974Sbrutus 			if (msz == 0) {
16171974Sbrutus 				nmp = pmp;
16181974Sbrutus 				pmp = NULL;
16191974Sbrutus 				goto zero;
16201974Sbrutus 			}
16210Sstevel@tonic-gate 		}
16220Sstevel@tonic-gate 		desb = kmem_cache_alloc(uri_desb_kmc, KM_SLEEP);
16230Sstevel@tonic-gate 		REF_HOLD(temp->uri);
16240Sstevel@tonic-gate 		if (temp->segmap) {
16250Sstevel@tonic-gate 			REF_HOLD(temp->segmap);
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 		bcopy(temp, desb, sizeof (*desb));
16280Sstevel@tonic-gate 		desb->frtn.free_arg = (caddr_t)desb;
16290Sstevel@tonic-gate 		nmp = desballoc((uchar_t *)ldata, msz, BPRI_HI, &desb->frtn);
16300Sstevel@tonic-gate 		if (nmp == NULL) {
16310Sstevel@tonic-gate 			if (temp->segmap) {
16320Sstevel@tonic-gate 				REF_RELE(temp->segmap);
16330Sstevel@tonic-gate 			}
16340Sstevel@tonic-gate 			REF_RELE(temp->uri);
16350Sstevel@tonic-gate 			if (mp != NULL) {
16361974Sbrutus 				mp->b_next = NULL;
16370Sstevel@tonic-gate 				freemsg(mp);
16380Sstevel@tonic-gate 			}
16391974Sbrutus 			if (persist != NULL) {
16401974Sbrutus 				freeb(persist);
16411974Sbrutus 			}
16420Sstevel@tonic-gate 			return (NULL);
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 		nmp->b_wptr += msz;
16451974Sbrutus 	zero:
16460Sstevel@tonic-gate 		if (mp != NULL) {
16471974Sbrutus 			mp->b_next->b_cont = nmp;
16480Sstevel@tonic-gate 		} else {
16490Sstevel@tonic-gate 			mp = nmp;
16500Sstevel@tonic-gate 		}
16511974Sbrutus 		if (pmp != NULL) {
16521974Sbrutus 			nmp->b_cont = pmp;
16531974Sbrutus 			nmp = pmp;
16541974Sbrutus 			pmp = NULL;
16551974Sbrutus 		}
16561974Sbrutus 		mp->b_next = nmp;
16570Sstevel@tonic-gate 		ldata += msz;
16580Sstevel@tonic-gate 		lsz -= msz;
16590Sstevel@tonic-gate 		lbytes -= msz;
16600Sstevel@tonic-gate 	}
16610Sstevel@tonic-gate 	*data = ldata;
16620Sstevel@tonic-gate 	*sz = lsz;
16630Sstevel@tonic-gate 	if (bytes)
16640Sstevel@tonic-gate 		*bytes = lbytes;
16650Sstevel@tonic-gate 	return (mp);
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate  * Experimential noqwait (i.e. no canput()/qwait() checks), just send
16700Sstevel@tonic-gate  * the entire mblk_t chain down without flow-control checks.
16710Sstevel@tonic-gate  */
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate static int
kstrwritempnoqwait(struct vnode * vp,mblk_t * mp)16740Sstevel@tonic-gate kstrwritempnoqwait(struct vnode *vp, mblk_t *mp)
16750Sstevel@tonic-gate {
16760Sstevel@tonic-gate 	struct stdata *stp;
16770Sstevel@tonic-gate 	int error = 0;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	ASSERT(vp->v_stream);
16800Sstevel@tonic-gate 	stp = vp->v_stream;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	/* Fast check of flags before acquiring the lock */
16830Sstevel@tonic-gate 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
16840Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
16850Sstevel@tonic-gate 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
16860Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
16870Sstevel@tonic-gate 		if (error != 0) {
16880Sstevel@tonic-gate 			if (!(stp->sd_flag & STPLEX) &&
16890Sstevel@tonic-gate 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
16900Sstevel@tonic-gate 				error = EPIPE;
16910Sstevel@tonic-gate 			}
16920Sstevel@tonic-gate 			return (error);
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 	}
16950Sstevel@tonic-gate 	putnext(stp->sd_wrq, mp);
16960Sstevel@tonic-gate 	return (0);
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate /*
17001974Sbrutus  * Send the URI uri_desc_t *uri response uri_rd_t *rdp out the socket_t *so.
17010Sstevel@tonic-gate  */
17020Sstevel@tonic-gate 
17031974Sbrutus static int
uri_rd_response(struct sonode * so,uri_desc_t * uri,uri_rd_t * rdp,boolean_t first)17041974Sbrutus uri_rd_response(struct sonode *so,
17051974Sbrutus     uri_desc_t *uri,
17061974Sbrutus     uri_rd_t *rdp,
17071974Sbrutus     boolean_t first)
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate 	vnode_t		*vp = SOTOV(so);
17108348SEric.Yu@Sun.COM 	int		max_mblk = (int)vp->v_stream->sd_maxblk;
17110Sstevel@tonic-gate 	int		wsz;
17120Sstevel@tonic-gate 	mblk_t		*mp, *wmp, *persist;
17130Sstevel@tonic-gate 	int		write_bytes;
17141974Sbrutus 	uri_rd_t	rd;
17150Sstevel@tonic-gate 	uri_desb_t	desb;
17160Sstevel@tonic-gate 	uri_segmap_t	*segmap = NULL;
17170Sstevel@tonic-gate 	char		*segmap_data;
17180Sstevel@tonic-gate 	size_t		segmap_sz;
17191974Sbrutus 	int		error;
17201974Sbrutus 	int		fflg = ((so->so_state & SS_NDELAY) ? FNDELAY : 0) |
17218171SPrakash.Jalan@Sun.COM 	    ((so->so_state & SS_NONBLOCK) ? FNONBLOCK : 0);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	/* Initialize template uri_desb_t */
17250Sstevel@tonic-gate 	desb.frtn.free_func = uri_desb_free;
17260Sstevel@tonic-gate 	desb.frtn.free_arg = NULL;
17270Sstevel@tonic-gate 	desb.uri = uri;
17280Sstevel@tonic-gate 
17291974Sbrutus 	/* Get a local copy of the rd_t */
17301974Sbrutus 	bcopy(rdp, &rd, sizeof (rd));
17310Sstevel@tonic-gate 	do {
17321974Sbrutus 		if (first) {
17331974Sbrutus 			/*
17341974Sbrutus 			 * For first kstrwrite() enough data to get
17351974Sbrutus 			 * things going, note non blocking version of
17361974Sbrutus 			 * kstrwrite() will be used below.
17371974Sbrutus 			 */
17381974Sbrutus 			write_bytes = P2ROUNDUP((max_mblk * 4),
17391974Sbrutus 			    MAXBSIZE * nl7c_file_prefetch);
17401974Sbrutus 		} else {
17411974Sbrutus 			if ((write_bytes = so->so_sndbuf) == 0)
17421974Sbrutus 				write_bytes = vp->v_stream->sd_qn_maxpsz;
17431974Sbrutus 			ASSERT(write_bytes > 0);
17441974Sbrutus 			write_bytes = P2ROUNDUP(write_bytes, MAXBSIZE);
17451974Sbrutus 		}
17461974Sbrutus 		/*
17471974Sbrutus 		 * Chop up to a write_bytes worth of data.
17481974Sbrutus 		 */
17490Sstevel@tonic-gate 		wmp = NULL;
17500Sstevel@tonic-gate 		wsz = write_bytes;
17510Sstevel@tonic-gate 		do {
17521974Sbrutus 			if (rd.sz == 0)
17531974Sbrutus 				break;
17540Sstevel@tonic-gate 			if (rd.off == -1) {
17550Sstevel@tonic-gate 				if (uri->eoh >= rd.data.kmem &&
17560Sstevel@tonic-gate 				    uri->eoh < &rd.data.kmem[rd.sz]) {
17570Sstevel@tonic-gate 					persist = nl7c_http_persist(so);
17580Sstevel@tonic-gate 				} else {
17590Sstevel@tonic-gate 					persist = NULL;
17600Sstevel@tonic-gate 				}
17610Sstevel@tonic-gate 				desb.segmap = NULL;
17620Sstevel@tonic-gate 				mp = uri_desb_chop(&rd.data.kmem, &rd.sz,
17630Sstevel@tonic-gate 				    &wsz, &desb, max_mblk, uri->eoh, persist);
17641974Sbrutus 				if (mp == NULL) {
17651974Sbrutus 					error = ENOMEM;
17660Sstevel@tonic-gate 					goto invalidate;
17671974Sbrutus 				}
17680Sstevel@tonic-gate 			} else {
17690Sstevel@tonic-gate 				if (segmap == NULL) {
17700Sstevel@tonic-gate 					segmap = uri_segmap_map(&rd,
17710Sstevel@tonic-gate 					    write_bytes);
17721974Sbrutus 					if (segmap == NULL) {
17731974Sbrutus 						error = ENOMEM;
17740Sstevel@tonic-gate 						goto invalidate;
17751974Sbrutus 					}
17760Sstevel@tonic-gate 					desb.segmap = segmap;
17770Sstevel@tonic-gate 					segmap_data = segmap->base;
17780Sstevel@tonic-gate 					segmap_sz = segmap->len;
17790Sstevel@tonic-gate 				}
17800Sstevel@tonic-gate 				mp = uri_desb_chop(&segmap_data, &segmap_sz,
17810Sstevel@tonic-gate 				    &wsz, &desb, max_mblk, NULL, NULL);
17821974Sbrutus 				if (mp == NULL) {
17831974Sbrutus 					error = ENOMEM;
17840Sstevel@tonic-gate 					goto invalidate;
17851974Sbrutus 				}
17860Sstevel@tonic-gate 				if (segmap_sz == 0) {
17870Sstevel@tonic-gate 					rd.sz -= segmap->len;
17880Sstevel@tonic-gate 					rd.off += segmap->len;
17890Sstevel@tonic-gate 					REF_RELE(segmap);
17900Sstevel@tonic-gate 					segmap = NULL;
17910Sstevel@tonic-gate 				}
17920Sstevel@tonic-gate 			}
17930Sstevel@tonic-gate 			if (wmp == NULL) {
17940Sstevel@tonic-gate 				wmp = mp;
17950Sstevel@tonic-gate 			} else {
17960Sstevel@tonic-gate 				wmp->b_next->b_cont = mp;
17970Sstevel@tonic-gate 				wmp->b_next = mp->b_next;
17980Sstevel@tonic-gate 				mp->b_next = NULL;
17990Sstevel@tonic-gate 			}
18001974Sbrutus 		} while (wsz > 0 && rd.sz > 0);
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 		wmp->b_next = NULL;
18030Sstevel@tonic-gate 		if (first) {
18040Sstevel@tonic-gate 			/* First kstrwrite(), use noqwait */
18051974Sbrutus 			if ((error = kstrwritempnoqwait(vp, wmp)) != 0)
18060Sstevel@tonic-gate 				goto invalidate;
18070Sstevel@tonic-gate 			/*
18080Sstevel@tonic-gate 			 * For the rest of the kstrwrite()s use SO_SNDBUF
18090Sstevel@tonic-gate 			 * worth of data at a time, note these kstrwrite()s
18100Sstevel@tonic-gate 			 * may (will) block one or more times.
18110Sstevel@tonic-gate 			 */
18120Sstevel@tonic-gate 			first = B_FALSE;
18130Sstevel@tonic-gate 		} else {
18141974Sbrutus 			if ((error = kstrwritemp(vp, wmp, fflg)) != 0) {
18151974Sbrutus 				if (error == EAGAIN) {
18161974Sbrutus 					nl7c_uri_rd_EAGAIN++;
18171974Sbrutus 					if ((error =
18181974Sbrutus 					    kstrwritempnoqwait(vp, wmp)) != 0)
18191974Sbrutus 						goto invalidate;
18201974Sbrutus 				} else
18211974Sbrutus 					goto invalidate;
18221974Sbrutus 			}
18230Sstevel@tonic-gate 		}
18241974Sbrutus 	} while (rd.sz > 0);
18250Sstevel@tonic-gate 
18261974Sbrutus 	return (0);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate invalidate:
18290Sstevel@tonic-gate 	if (segmap) {
18300Sstevel@tonic-gate 		REF_RELE(segmap);
18310Sstevel@tonic-gate 	}
18320Sstevel@tonic-gate 	if (wmp)
18330Sstevel@tonic-gate 		freemsg(wmp);
18341974Sbrutus 
18351974Sbrutus 	return (error);
18361974Sbrutus }
18371974Sbrutus 
18381974Sbrutus /*
18391974Sbrutus  * Send the URI uri_desc_t *uri response out the socket_t *so.
18401974Sbrutus  */
18411974Sbrutus 
18421974Sbrutus static int
uri_response(struct sonode * so,uri_desc_t * uri)18431974Sbrutus uri_response(struct sonode *so, uri_desc_t *uri)
18441974Sbrutus {
18451974Sbrutus 	uri_rd_t	*rdp = &uri->response;
18461974Sbrutus 	boolean_t	first = B_TRUE;
18471974Sbrutus 	int		error;
18481974Sbrutus 
18491974Sbrutus 	while (rdp != NULL) {
18501974Sbrutus 		error = uri_rd_response(so, uri, rdp, first);
18511974Sbrutus 		if (error != 0) {
18521974Sbrutus 			goto invalidate;
18531974Sbrutus 		}
18541974Sbrutus 		first = B_FALSE;
18551974Sbrutus 		rdp = rdp->next;
18561974Sbrutus 	}
18571974Sbrutus 	return (0);
18581974Sbrutus 
18591974Sbrutus invalidate:
1860*12714Sanil.udupa@sun.com 	if (uri->hash != URI_TEMP)
1861*12714Sanil.udupa@sun.com 		uri_delete(uri);
18621974Sbrutus 	return (error);
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate /*
18660Sstevel@tonic-gate  * The pchars[] array is indexed by a char to determine if it's a
18670Sstevel@tonic-gate  * valid URI path component chararcter where:
18680Sstevel@tonic-gate  *
18690Sstevel@tonic-gate  *    pchar       = unreserved | escaped |
18700Sstevel@tonic-gate  *                  ":" | "@" | "&" | "=" | "+" | "$" | ","
18710Sstevel@tonic-gate  *
18720Sstevel@tonic-gate  *    unreserved  = alphanum | mark
18730Sstevel@tonic-gate  *
18740Sstevel@tonic-gate  *    alphanum    = alpha | digit
18750Sstevel@tonic-gate  *
18760Sstevel@tonic-gate  *    alpha       = lowalpha | upalpha
18770Sstevel@tonic-gate  *
18780Sstevel@tonic-gate  *    lowalpha    = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
18790Sstevel@tonic-gate  *                  "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
18800Sstevel@tonic-gate  *                  "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
18810Sstevel@tonic-gate  *                  "y" | "z"
18820Sstevel@tonic-gate  *
18830Sstevel@tonic-gate  *    upalpha     = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
18840Sstevel@tonic-gate  *                  "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
18850Sstevel@tonic-gate  *                  "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
18860Sstevel@tonic-gate  *                  "Y" | "Z"
18870Sstevel@tonic-gate  *
18880Sstevel@tonic-gate  *    digit       = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
18890Sstevel@tonic-gate  *                  "8" | "9"
18900Sstevel@tonic-gate  *
18910Sstevel@tonic-gate  *    mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
18920Sstevel@tonic-gate  *
18930Sstevel@tonic-gate  *    escaped     = "%" hex hex
18940Sstevel@tonic-gate  *    hex         = digit | "A" | "B" | "C" | "D" | "E" | "F" |
18950Sstevel@tonic-gate  *                  "a" | "b" | "c" | "d" | "e" | "f"
18960Sstevel@tonic-gate  */
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate static char pchars[] = {
18990Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x07 */
19000Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x08 - 0x0F */
19010Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x17 */
19020Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0,	/* 0x18 - 0x1F */
19030Sstevel@tonic-gate     0, 1, 0, 0, 1, 1, 1, 1,	/* 0x20 - 0x27 */
19040Sstevel@tonic-gate     0, 0, 1, 1, 1, 1, 1, 1,	/* 0x28 - 0x2F */
19050Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x30 - 0x37 */
19060Sstevel@tonic-gate     1, 1, 1, 0, 0, 1, 0, 0,	/* 0x38 - 0x3F */
19070Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x47 */
19080Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x48 - 0x4F */
19090Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x57 */
19100Sstevel@tonic-gate     1, 1, 1, 0, 0, 0, 0, 1,	/* 0x58 - 0x5F */
19110Sstevel@tonic-gate     0, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x67 */
19120Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x68 - 0x6F */
19130Sstevel@tonic-gate     1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x77 */
19140Sstevel@tonic-gate     1, 1, 1, 0, 0, 0, 1, 0	/* 0x78 - 0x7F */
19150Sstevel@tonic-gate };
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate #define	PCHARS_MASK 0x7F
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate /*
19200Sstevel@tonic-gate  * This is the main L7 request message parse, we are called each time
19210Sstevel@tonic-gate  * new data is availble for a socket, each time a single buffer of the
19220Sstevel@tonic-gate  * entire message to date is given.
19230Sstevel@tonic-gate  *
19240Sstevel@tonic-gate  * Here we parse the request looking for the URI, parse it, and if a
19250Sstevel@tonic-gate  * supported scheme call the scheme parser to commplete the parse of any
19260Sstevel@tonic-gate  * headers which may further qualify the identity of the requested object
19270Sstevel@tonic-gate  * then lookup it up in the URI hash.
19280Sstevel@tonic-gate  *
19290Sstevel@tonic-gate  * Return B_TRUE for more processing.
19300Sstevel@tonic-gate  *
19310Sstevel@tonic-gate  * Note, at this time the parser supports the generic message format as
19320Sstevel@tonic-gate  * specified in RFC 822 with potentional limitations as specified in RFC
19330Sstevel@tonic-gate  * 2616 for HTTP messages.
19340Sstevel@tonic-gate  *
19350Sstevel@tonic-gate  * Note, the caller supports an mblk_t chain, for now the parser(s)
19360Sstevel@tonic-gate  * require the complete header in a single mblk_t. This is the common
19370Sstevel@tonic-gate  * case and certainly for high performance environments, if at a future
19380Sstevel@tonic-gate  * date mblk_t chains are important the parse can be reved to process
19390Sstevel@tonic-gate  * mblk_t chains.
19400Sstevel@tonic-gate  */
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate boolean_t
nl7c_parse(struct sonode * so,boolean_t nonblocking,boolean_t * ret)19431974Sbrutus nl7c_parse(struct sonode *so, boolean_t nonblocking, boolean_t *ret)
19440Sstevel@tonic-gate {
19458348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
19468348SEric.Yu@Sun.COM 	char	*cp = (char *)sti->sti_nl7c_rcv_mp->b_rptr;
19478348SEric.Yu@Sun.COM 	char	*ep = (char *)sti->sti_nl7c_rcv_mp->b_wptr;
19480Sstevel@tonic-gate 	char	*get = "GET ";
19491974Sbrutus 	char	*post = "POST ";
19500Sstevel@tonic-gate 	char	c;
19510Sstevel@tonic-gate 	char	*uris;
19521974Sbrutus 	uri_desc_t *uri = NULL;
19530Sstevel@tonic-gate 	uri_desc_t *ruri = NULL;
19541974Sbrutus 	mblk_t	*reqmp;
19551974Sbrutus 	uint32_t hv = 0;
19560Sstevel@tonic-gate 
19578348SEric.Yu@Sun.COM 	if ((reqmp = dupb(sti->sti_nl7c_rcv_mp)) == NULL) {
19581974Sbrutus 		nl7c_uri_pass_dupbfail++;
19591974Sbrutus 		goto pass;
19601974Sbrutus 	}
19610Sstevel@tonic-gate 	/*
19620Sstevel@tonic-gate 	 * Allocate and initialize minimumal state for the request
19630Sstevel@tonic-gate 	 * uri_desc_t, in the cache hit case this uri_desc_t will
19640Sstevel@tonic-gate 	 * be freed.
19650Sstevel@tonic-gate 	 */
19661974Sbrutus 	uri = kmem_cache_alloc(nl7c_uri_kmc, KM_SLEEP);
19671974Sbrutus 	REF_INIT(uri, 1, nl7c_uri_inactive, nl7c_uri_kmc);
19680Sstevel@tonic-gate 	uri->hash = NULL;
19690Sstevel@tonic-gate 	uri->tail = NULL;
19700Sstevel@tonic-gate 	uri->scheme = NULL;
19710Sstevel@tonic-gate 	uri->count = 0;
19721974Sbrutus 	uri->reqmp = reqmp;
19731974Sbrutus 
19740Sstevel@tonic-gate 	/*
19750Sstevel@tonic-gate 	 * Set request time to current time.
19760Sstevel@tonic-gate 	 */
19778348SEric.Yu@Sun.COM 	sti->sti_nl7c_rtime = gethrestime_sec();
19781974Sbrutus 
19790Sstevel@tonic-gate 	/*
19800Sstevel@tonic-gate 	 * Parse the Request-Line for the URI.
19810Sstevel@tonic-gate 	 *
19820Sstevel@tonic-gate 	 * For backwards HTTP version compatable reasons skip any leading
19830Sstevel@tonic-gate 	 * CRLF (or CR or LF) line terminator(s) preceding Request-Line.
19840Sstevel@tonic-gate 	 */
19850Sstevel@tonic-gate 	while (cp < ep && (*cp == '\r' || *cp == '\n')) {
19860Sstevel@tonic-gate 		cp++;
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate 	while (cp < ep && *get == *cp) {
19890Sstevel@tonic-gate 		get++;
19900Sstevel@tonic-gate 		cp++;
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 	if (*get != 0) {
19931974Sbrutus 		/* Note a "GET", check for "POST" */
19941974Sbrutus 		while (cp < ep && *post == *cp) {
19951974Sbrutus 			post++;
19961974Sbrutus 			cp++;
19970Sstevel@tonic-gate 		}
19981974Sbrutus 		if (*post != 0) {
19991974Sbrutus 			if (cp == ep) {
20001974Sbrutus 				nl7c_uri_more_get++;
20011974Sbrutus 				goto more;
20021974Sbrutus 			}
20031974Sbrutus 			/* Not a "GET" or a "POST", just pass */
20041974Sbrutus 			nl7c_uri_pass_method++;
20051974Sbrutus 			goto pass;
20061974Sbrutus 		}
20071974Sbrutus 		/* "POST", don't cache but still may want to parse */
20081974Sbrutus 		uri->hash = URI_TEMP;
20090Sstevel@tonic-gate 	}
20100Sstevel@tonic-gate 	/*
20110Sstevel@tonic-gate 	 * Skip over URI path char(s) and save start and past end pointers.
20120Sstevel@tonic-gate 	 */
20130Sstevel@tonic-gate 	uris = cp;
20140Sstevel@tonic-gate 	while (cp < ep && (c = *cp) != ' ' && c != '\r') {
20150Sstevel@tonic-gate 		if (c == '?') {
20160Sstevel@tonic-gate 			/* Don't cache but still may want to parse */
20170Sstevel@tonic-gate 			uri->hash = URI_TEMP;
20180Sstevel@tonic-gate 		}
20191974Sbrutus 		CHASH(hv, c);
20200Sstevel@tonic-gate 		cp++;
20210Sstevel@tonic-gate 	}
20220Sstevel@tonic-gate 	if (c != '\r' && cp == ep) {
20230Sstevel@tonic-gate 		nl7c_uri_more_eol++;
20240Sstevel@tonic-gate 		goto more;
20250Sstevel@tonic-gate 	}
20260Sstevel@tonic-gate 	/*
20270Sstevel@tonic-gate 	 * Request-Line URI parsed, pass the rest of the request on
20280Sstevel@tonic-gate 	 * to the the http scheme parse.
20290Sstevel@tonic-gate 	 */
20300Sstevel@tonic-gate 	uri->path.cp = uris;
20310Sstevel@tonic-gate 	uri->path.ep = cp;
20321974Sbrutus 	uri->hvalue = hv;
20331974Sbrutus 	if (! nl7c_http_request(&cp, ep, uri, so) || cp == NULL) {
20340Sstevel@tonic-gate 		/*
20351974Sbrutus 		 * Parse not successful or pass on request, the pointer
20361974Sbrutus 		 * to the parse pointer "cp" is overloaded such that ! NULL
20371974Sbrutus 		 * for more data and NULL for bad parse of request or pass.
20380Sstevel@tonic-gate 		 */
20390Sstevel@tonic-gate 		if (cp != NULL) {
20400Sstevel@tonic-gate 			nl7c_uri_more_http++;
20410Sstevel@tonic-gate 			goto more;
20420Sstevel@tonic-gate 		}
20430Sstevel@tonic-gate 		nl7c_uri_pass_http++;
20440Sstevel@tonic-gate 		goto pass;
20450Sstevel@tonic-gate 	}
20461974Sbrutus 	if (uri->nocache) {
20471974Sbrutus 		uri->hash = URI_TEMP;
20481974Sbrutus 		(void) uri_lookup(uri, B_FALSE, nonblocking);
20491974Sbrutus 	} else if (uri->hash == URI_TEMP) {
20501974Sbrutus 		uri->nocache = B_TRUE;
20511974Sbrutus 		(void) uri_lookup(uri, B_FALSE, nonblocking);
20521974Sbrutus 	}
20531974Sbrutus 
20540Sstevel@tonic-gate 	if (uri->hash == URI_TEMP) {
20558348SEric.Yu@Sun.COM 		if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
20560Sstevel@tonic-gate 			/* Temporary URI so skip hash processing */
20570Sstevel@tonic-gate 			nl7c_uri_request++;
20580Sstevel@tonic-gate 			nl7c_uri_temp++;
20590Sstevel@tonic-gate 			goto temp;
20600Sstevel@tonic-gate 		}
20610Sstevel@tonic-gate 		/* Not persistent so not interested in the response */
20620Sstevel@tonic-gate 		nl7c_uri_pass_temp++;
20630Sstevel@tonic-gate 		goto pass;
20640Sstevel@tonic-gate 	}
20650Sstevel@tonic-gate 	/*
20661974Sbrutus 	 * Check the URI hash for a cached response, save the request
20671974Sbrutus 	 * uri in case we need it below.
20680Sstevel@tonic-gate 	 */
20691974Sbrutus 	ruri = uri;
20700Sstevel@tonic-gate 	if ((uri = uri_lookup(uri, B_TRUE, nonblocking)) == NULL) {
20710Sstevel@tonic-gate 		/*
20720Sstevel@tonic-gate 		 * Failed to lookup due to nonblocking wait required,
20730Sstevel@tonic-gate 		 * interrupted cv_wait_sig(), KM_NOSLEEP memory alloc
20740Sstevel@tonic-gate 		 * failure, ... Just pass on this request.
20750Sstevel@tonic-gate 		 */
20760Sstevel@tonic-gate 		nl7c_uri_pass_addfail++;
20770Sstevel@tonic-gate 		goto pass;
20780Sstevel@tonic-gate 	}
20790Sstevel@tonic-gate 	nl7c_uri_request++;
20800Sstevel@tonic-gate 	if (uri->response.sz > 0) {
20810Sstevel@tonic-gate 		/*
20820Sstevel@tonic-gate 		 * We have the response cached, update recv mblk rptr
20831974Sbrutus 		 * to reflect the data consumed in parse.
20840Sstevel@tonic-gate 		 */
20858348SEric.Yu@Sun.COM 		mblk_t	*mp = sti->sti_nl7c_rcv_mp;
20860Sstevel@tonic-gate 
20871974Sbrutus 		if (cp == (char *)mp->b_wptr) {
20888348SEric.Yu@Sun.COM 			sti->sti_nl7c_rcv_mp = mp->b_cont;
20891974Sbrutus 			mp->b_cont = NULL;
20901974Sbrutus 			freeb(mp);
20911974Sbrutus 		} else {
20921974Sbrutus 			mp->b_rptr = (unsigned char *)cp;
20931974Sbrutus 		}
20941974Sbrutus 		nl7c_uri_hit++;
20951974Sbrutus 		/* If logging enabled log request */
20961974Sbrutus 		if (nl7c_logd_enabled) {
20970Sstevel@tonic-gate 			ipaddr_t faddr;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
21000Sstevel@tonic-gate 				/* Only support IPv4 addrs */
21010Sstevel@tonic-gate 				faddr = ((struct sockaddr_in *)
21028348SEric.Yu@Sun.COM 				    sti->sti_faddr_sa) ->sin_addr.s_addr;
21030Sstevel@tonic-gate 			} else {
21040Sstevel@tonic-gate 				faddr = 0;
21050Sstevel@tonic-gate 			}
21061974Sbrutus 			/* XXX need to pass response type, e.g. 200, 304 */
21078348SEric.Yu@Sun.COM 			nl7c_logd_log(ruri, uri, sti->sti_nl7c_rtime, faddr);
21080Sstevel@tonic-gate 		}
210910415Sanil.udupa@sun.com 
211010415Sanil.udupa@sun.com 		/* If conditional request check for substitute response */
211110415Sanil.udupa@sun.com 		if (ruri->conditional) {
211210415Sanil.udupa@sun.com 			uri = nl7c_http_cond(ruri, uri);
211310415Sanil.udupa@sun.com 		}
211410415Sanil.udupa@sun.com 
21151974Sbrutus 		/*
21161974Sbrutus 		 * Release reference on request URI, send the response out
21171974Sbrutus 		 * the socket, release reference on response uri, set the
21181974Sbrutus 		 * *ret value to B_TRUE to indicate request was consumed
21191974Sbrutus 		 * then return B_FALSE to indcate no more data needed.
21201974Sbrutus 		 */
21211974Sbrutus 		REF_RELE(ruri);
21221974Sbrutus 		(void) uri_response(so, uri);
21230Sstevel@tonic-gate 		REF_RELE(uri);
21240Sstevel@tonic-gate 		*ret = B_TRUE;
21250Sstevel@tonic-gate 		return (B_FALSE);
21260Sstevel@tonic-gate 	}
21270Sstevel@tonic-gate 	/*
21281974Sbrutus 	 * Miss the cache, the request URI is in the cache waiting for
21291974Sbrutus 	 * application write-side data to fill it.
21300Sstevel@tonic-gate 	 */
21310Sstevel@tonic-gate 	nl7c_uri_miss++;
21320Sstevel@tonic-gate temp:
21331974Sbrutus 	/*
21341974Sbrutus 	 * A miss or temp URI for which response data is needed, link
21351974Sbrutus 	 * uri to so and so to uri, set WAITWRITE in the so such that
21361974Sbrutus 	 * read-side processing is suspended (so the next read() gets
21371974Sbrutus 	 * the request data) until a write() is processed by NL7C.
21381974Sbrutus 	 *
21398348SEric.Yu@Sun.COM 	 * Note, sti->sti_nl7c_uri now owns the REF_INIT() ref.
21401974Sbrutus 	 */
21410Sstevel@tonic-gate 	uri->proc = so;
21428348SEric.Yu@Sun.COM 	sti->sti_nl7c_uri = uri;
21438348SEric.Yu@Sun.COM 	sti->sti_nl7c_flags |= NL7C_WAITWRITE;
21440Sstevel@tonic-gate 	*ret = B_FALSE;
21450Sstevel@tonic-gate 	return (B_FALSE);
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate more:
21480Sstevel@tonic-gate 	/* More data is needed, note fragmented recv not supported */
21490Sstevel@tonic-gate 	nl7c_uri_more++;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate pass:
21520Sstevel@tonic-gate 	/* Pass on this request */
21530Sstevel@tonic-gate 	nl7c_uri_pass++;
21540Sstevel@tonic-gate 	nl7c_uri_request++;
21550Sstevel@tonic-gate 	if (ruri != NULL) {
21560Sstevel@tonic-gate 		REF_RELE(ruri);
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 	if (uri) {
21590Sstevel@tonic-gate 		REF_RELE(uri);
21600Sstevel@tonic-gate 	}
21618348SEric.Yu@Sun.COM 	sti->sti_nl7c_flags = 0;
21620Sstevel@tonic-gate 	*ret = B_FALSE;
21630Sstevel@tonic-gate 	return (B_FALSE);
21640Sstevel@tonic-gate }
2165