17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
52c9e429brutus * Common Development and Distribution License (the "License").
62c9e429brutus * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22b73114cAnil udupa * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bdstevel@tonic-gate */
247c478bdstevel@tonic-gate
257c478bdstevel@tonic-gate#include <sys/strsubr.h>
267c478bdstevel@tonic-gate#include <sys/strsun.h>
277c478bdstevel@tonic-gate#include <sys/param.h>
287c478bdstevel@tonic-gate#include <sys/sysmacros.h>
297c478bdstevel@tonic-gate#include <vm/seg_map.h>
307c478bdstevel@tonic-gate#include <vm/seg_kpm.h>
317c478bdstevel@tonic-gate#include <sys/condvar_impl.h>
327c478bdstevel@tonic-gate#include <sys/sendfile.h>
337c478bdstevel@tonic-gate#include <fs/sockfs/nl7c.h>
347c478bdstevel@tonic-gate#include <fs/sockfs/nl7curi.h>
350f1702cYu Xiangning<Eric.Yu@Sun.COM>#include <fs/sockfs/socktpi_impl.h>
367c478bdstevel@tonic-gate
372c9e429brutus#include <inet/common.h>
382c9e429brutus#include <inet/ip.h>
392c9e429brutus#include <inet/ip6.h>
402c9e429brutus#include <inet/tcp.h>
412c9e429brutus#include <inet/led.h>
422c9e429brutus#include <inet/mi.h>
432c9e429brutus
442c9e429brutus#include <inet/nca/ncadoorhdr.h>
452c9e429brutus#include <inet/nca/ncalogd.h>
462c9e429brutus#include <inet/nca/ncandd.h>
472c9e429brutus
482c9e429brutus#include <sys/promif.h>
492c9e429brutus
507c478bdstevel@tonic-gate/*
517c478bdstevel@tonic-gate * Some externs:
527c478bdstevel@tonic-gate */
537c478bdstevel@tonic-gate
542c9e429brutusextern boolean_t	nl7c_logd_enabled;
552c9e429brutusextern void		nl7c_logd_log(uri_desc_t *, uri_desc_t *,
562c9e429brutus			    time_t, ipaddr_t);
572c9e429brutusextern boolean_t	nl7c_close_addr(struct sonode *);
582c9e429brutusextern struct sonode	*nl7c_addr2portso(void *);
592c9e429brutusextern uri_desc_t	*nl7c_http_cond(uri_desc_t *, uri_desc_t *);
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate/*
627c478bdstevel@tonic-gate * Various global tuneables:
637c478bdstevel@tonic-gate */
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gateclock_t		nl7c_uri_ttl = -1;	/* TTL in seconds (-1 == infinite) */
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gateboolean_t	nl7c_use_kmem = B_FALSE; /* Force use of kmem (no segmap) */
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gateuint64_t	nl7c_file_prefetch = 1; /* File cache prefetch pages */
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gateuint64_t	nl7c_uri_max = 0;	/* Maximum bytes (0 == infinite) */
727c478bdstevel@tonic-gateuint64_t	nl7c_uri_bytes = 0;	/* Bytes of kmem used by URIs */
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate/*
752c9e429brutus * Locals:
762c9e429brutus */
772c9e429brutus
782c9e429brutusstatic int	uri_rd_response(struct sonode *, uri_desc_t *,
792c9e429brutus		    uri_rd_t *, boolean_t);
802c9e429brutusstatic int	uri_response(struct sonode *, uri_desc_t *);
812c9e429brutus
822c9e429brutus/*
832c9e429brutus * HTTP scheme functions called from nl7chttp.c:
842c9e429brutus */
852c9e429brutus
862c9e429brutusboolean_t nl7c_http_request(char **, char *, uri_desc_t *, struct sonode *);
872c9e429brutusboolean_t nl7c_http_response(char **, char *, uri_desc_t *, struct sonode *);
882c9e429brutusboolean_t nl7c_http_cmp(void *, void *);
892c9e429brutusmblk_t *nl7c_http_persist(struct sonode *);
902c9e429brutusvoid nl7c_http_free(void *arg);
912c9e429brutusvoid nl7c_http_init(void);
922c9e429brutus
932c9e429brutus/*
947c478bdstevel@tonic-gate * Counters that need to move to kstat and/or be removed:
957c478bdstevel@tonic-gate */
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_request = 0;
987c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_hit = 0;
997c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass = 0;
1007c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_miss = 0;
1017c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_temp = 0;
1027c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_more = 0;
1037c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_data = 0;
1047c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_sendfilev = 0;
1057c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_reclaim_calls = 0;
1067c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_reclaim_cnt = 0;
1077c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass_urifail = 0;
1087c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass_dupbfail = 0;
1097c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_more_get = 0;
1102c9e429brutusvolatile uint64_t nl7c_uri_pass_method = 0;
1117c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass_option = 0;
1127c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_more_eol = 0;
1137c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_more_http = 0;
1147c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass_http = 0;
1157c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass_addfail = 0;
1167c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_pass_temp = 0;
1177c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_expire = 0;
1182c9e429brutusvolatile uint64_t nl7c_uri_purge = 0;
1197c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_NULL1 = 0;
1207c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_NULL2 = 0;
1217c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_close = 0;
1227c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_temp_close = 0;
1237c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_free = 0;
1247c478bdstevel@tonic-gatevolatile uint64_t nl7c_uri_temp_free = 0;
1252c9e429brutusvolatile uint64_t nl7c_uri_temp_mk = 0;
1262c9e429brutusvolatile uint64_t nl7c_uri_rd_EAGAIN = 0;
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate/*
1297c478bdstevel@tonic-gate * Various kmem_cache_t's:
1307c478bdstevel@tonic-gate */
1317c478bdstevel@tonic-gate
1322c9e429brutuskmem_cache_t *nl7c_uri_kmc;
1332c9e429brutuskmem_cache_t *nl7c_uri_rd_kmc;
1347c478bdstevel@tonic-gatestatic kmem_cache_t *uri_desb_kmc;
1357c478bdstevel@tonic-gatestatic kmem_cache_t *uri_segmap_kmc;
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gatestatic void uri_kmc_reclaim(void *);
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gatestatic void nl7c_uri_reclaim(void);
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate/*
1427c478bdstevel@tonic-gate * The URI hash is a dynamically sized A/B bucket hash, when the current
1437c478bdstevel@tonic-gate * hash's average bucket chain length exceeds URI_HASH_AVRG a new hash of
1447c478bdstevel@tonic-gate * the next P2Ps[] size is created.
1457c478bdstevel@tonic-gate *
1467c478bdstevel@tonic-gate * All lookups are done in the current hash then the new hash (if any),
1477c478bdstevel@tonic-gate * if there is a new has then when a current hash bucket chain is examined
1487c478bdstevel@tonic-gate * any uri_desc_t members will be migrated to the new hash and when the
1497c478bdstevel@tonic-gate * last uri_desc_t has been migrated then the new hash will become the
1507c478bdstevel@tonic-gate * current and the previous current hash will be freed leaving a single
1517c478bdstevel@tonic-gate * hash.
1527c478bdstevel@tonic-gate *
1537c478bdstevel@tonic-gate * uri_hash_t - hash bucket (chain) type, contained in the uri_hash_ab[]
1547c478bdstevel@tonic-gate * and can be accessed only after aquiring the uri_hash_access lock (for
1557c478bdstevel@tonic-gate * READER or WRITER) then acquiring the lock uri_hash_t.lock, the uri_hash_t
1567c478bdstevel@tonic-gate * and all linked uri_desc_t.hash members are protected. Note, a REF_HOLD()
1577c478bdstevel@tonic-gate * is placed on all uri_desc_t uri_hash_t list members.
1587c478bdstevel@tonic-gate *
1597c478bdstevel@tonic-gate * uri_hash_access - rwlock for all uri_hash_* variables, READER for read
1607c478bdstevel@tonic-gate * access and WRITER for write access. Note, WRITER is only required for
1617c478bdstevel@tonic-gate * hash geometry changes.
1627c478bdstevel@tonic-gate *
1637c478bdstevel@tonic-gate * uri_hash_which - which uri_hash_ab[] is the current hash.
1647c478bdstevel@tonic-gate *
1657c478bdstevel@tonic-gate * uri_hash_n[] - the P2Ps[] index for each uri_hash_ab[].
1667c478bdstevel@tonic-gate *
1677c478bdstevel@tonic-gate * uri_hash_sz[] - the size for each uri_hash_ab[].
1687c478bdstevel@tonic-gate *
1697c478bdstevel@tonic-gate * uri_hash_cnt[] - the total uri_desc_t members for each uri_hash_ab[].
1707c478bdstevel@tonic-gate *
1717c478bdstevel@tonic-gate * uri_hash_overflow[] - the uri_hash_cnt[] for each uri_hash_ab[] when
1727c478bdstevel@tonic-gate * a new uri_hash_ab[] needs to be created.
1737c478bdstevel@tonic-gate *
1747c478bdstevel@tonic-gate * uri_hash_ab[] - the uri_hash_t entries.
1757c478bdstevel@tonic-gate *
1767c478bdstevel@tonic-gate * uri_hash_lru[] - the last uri_hash_ab[] walked for lru reclaim.
1777c478bdstevel@tonic-gate */
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gatetypedef struct uri_hash_s {
1807c478bdstevel@tonic-gate	struct uri_desc_s	*list;		/* List of uri_t(s) */
1817c478bdstevel@tonic-gate	kmutex_t		lock;
1827c478bdstevel@tonic-gate} uri_hash_t;
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate#define	URI_HASH_AVRG	5	/* Desired average hash chain length */
1857c478bdstevel@tonic-gate#define	URI_HASH_N_INIT	9	/* P2Ps[] initial index */
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gatestatic krwlock_t	uri_hash_access;
1887c478bdstevel@tonic-gatestatic uint32_t		uri_hash_which = 0;
1897c478bdstevel@tonic-gatestatic uint32_t		uri_hash_n[2] = {URI_HASH_N_INIT, 0};
1907c478bdstevel@tonic-gatestatic uint32_t		uri_hash_sz[2] = {0, 0};
1917c478bdstevel@tonic-gatestatic uint32_t		uri_hash_cnt[2] = {0, 0};
1927c478bdstevel@tonic-gatestatic uint32_t		uri_hash_overflow[2] = {0, 0};
1937c478bdstevel@tonic-gatestatic uri_hash_t	*uri_hash_ab[2] = {NULL, NULL};
1947c478bdstevel@tonic-gatestatic uri_hash_t	*uri_hash_lru[2] = {NULL, NULL};
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gate/*
1977c478bdstevel@tonic-gate * Primes for N of 3 - 24 where P is first prime less then (2^(N-1))+(2^(N-2))
1987c478bdstevel@tonic-gate * these primes have been foud to be useful for prime sized hash tables.
1997c478bdstevel@tonic-gate */
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gatestatic const int P2Ps[] = {
2027c478bdstevel@tonic-gate	0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,
2037c478bdstevel@tonic-gate	6143, 12281, 24571, 49139, 98299, 196597, 393209,
2047c478bdstevel@tonic-gate	786431, 1572853, 3145721, 6291449, 12582893, 0};
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate/*
2077c478bdstevel@tonic-gate * Hash macros:
2087c478bdstevel@tonic-gate *
2097c478bdstevel@tonic-gate *    H2A(char *cp, char *ep, char c) - convert the escaped octet (ASCII)
2107c478bdstevel@tonic-gate *    hex multichar of the format "%HH" pointeded to by *cp to a char and
2117c478bdstevel@tonic-gate *    return in c, *ep points to past end of (char *), on return *cp will
2127c478bdstevel@tonic-gate *    point to the last char consumed.
2137c478bdstevel@tonic-gate *
2147c478bdstevel@tonic-gate *    URI_HASH(unsigned hix, char *cp, char *ep) - hash the char(s) from
2157c478bdstevel@tonic-gate *    *cp to *ep to the unsigned hix, cp nor ep are modified.
2167c478bdstevel@tonic-gate *
2172c9e429brutus *    URI_HASH_IX(unsigned hix, int which) - convert the hash value hix to
2182c9e429brutus *    a hash index 0 - (uri_hash_sz[which] - 1).
2197c478bdstevel@tonic-gate *
2207c478bdstevel@tonic-gate *    URI_HASH_MIGRATE(from, hp, to) - migrate the uri_hash_t *hp list
2217c478bdstevel@tonic-gate *    uri_desc_t members from hash from to hash to.
2227c478bdstevel@tonic-gate *
2237c478bdstevel@tonic-gate *    URI_HASH_UNLINK(cur, new, hp, puri, uri) - unlink the uri_desc_t
2247c478bdstevel@tonic-gate *    *uri which is a member of the uri_hash_t *hp list with a previous
2257c478bdstevel@tonic-gate *    list member of *puri for the uri_hash_ab[] cur. After unlinking
2267c478bdstevel@tonic-gate *    check for cur hash empty, if so make new cur. Note, as this macro
2277c478bdstevel@tonic-gate *    can change a hash chain it needs to be run under hash_access as
2287c478bdstevel@tonic-gate *    RW_WRITER, futher as it can change the new hash to cur any access
2297c478bdstevel@tonic-gate *    to the hash state must be done after either dropping locks and
2307c478bdstevel@tonic-gate *    starting over or making sure the global state is consistent after
2317c478bdstevel@tonic-gate *    as before.
2327c478bdstevel@tonic-gate */
2337c478bdstevel@tonic-gate
2347c478bdstevel@tonic-gate#define	H2A(cp, ep, c) {						\
2357c478bdstevel@tonic-gate	int	_h = 2;							\
2367c478bdstevel@tonic-gate	int	_n = 0;							\
2377c478bdstevel@tonic-gate	char	_hc;							\
2387c478bdstevel@tonic-gate									\
2397c478bdstevel@tonic-gate	while (_h > 0 && ++(cp) < (ep)) {				\
2407c478bdstevel@tonic-gate		if (_h == 1)						\
2417c478bdstevel@tonic-gate			_n *= 0x10;					\
2427c478bdstevel@tonic-gate		_hc = *(cp);						\
2437c478bdstevel@tonic-gate		if (_hc >= '0' && _hc <= '9')				\
2447c478bdstevel@tonic-gate			_n += _hc - '0';				\
2457c478bdstevel@tonic-gate		else if (_hc >= 'a' || _hc <= 'f')			\
2467c478bdstevel@tonic-gate			_n += _hc - 'W';				\
2477c478bdstevel@tonic-gate		else if (_hc >= 'A' || _hc <= 'F')			\
2487c478bdstevel@tonic-gate			_n += _hc - '7';				\
2497c478bdstevel@tonic-gate		_h--;							\
2507c478bdstevel@tonic-gate	}								\
2517c478bdstevel@tonic-gate	(c) = _n;							\
2527c478bdstevel@tonic-gate}
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate#define	URI_HASH(hv, cp, ep) {						\
2557c478bdstevel@tonic-gate	char	*_s = (cp);						\
2567c478bdstevel@tonic-gate	char	_c;							\
2577c478bdstevel@tonic-gate									\
2587c478bdstevel@tonic-gate	while (_s < (ep)) {						\
2597c478bdstevel@tonic-gate		if ((_c = *_s) == '%') {				\
2607c478bdstevel@tonic-gate			H2A(_s, (ep), _c);				\
2617c478bdstevel@tonic-gate		}							\
2622c9e429brutus		CHASH(hv, _c);						\
2637c478bdstevel@tonic-gate		_s++;							\
2647c478bdstevel@tonic-gate	}								\
2657c478bdstevel@tonic-gate}
2667c478bdstevel@tonic-gate
2672c9e429brutus#define	URI_HASH_IX(hix, which) (hix) = (hix) % (uri_hash_sz[(which)])
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate#define	URI_HASH_MIGRATE(from, hp, to) {				\
2707c478bdstevel@tonic-gate	uri_desc_t	*_nuri;						\
2717c478bdstevel@tonic-gate	uint32_t	_nhix;						\
2727c478bdstevel@tonic-gate	uri_hash_t	*_nhp;						\
2737c478bdstevel@tonic-gate									\
2747c478bdstevel@tonic-gate	mutex_enter(&(hp)->lock);					\
2757c478bdstevel@tonic-gate	while ((_nuri = (hp)->list) != NULL) {				\
2767c478bdstevel@tonic-gate		(hp)->list = _nuri->hash;				\
2771a5e258Josef 'Jeff' Sipek		atomic_dec_32(&uri_hash_cnt[(from)]);		\
2781a5e258Josef 'Jeff' Sipek		atomic_inc_32(&uri_hash_cnt[(to)]);			\
2792c9e429brutus		_nhix = _nuri->hvalue;					\
2802c9e429brutus		URI_HASH_IX(_nhix, to);					\
2817c478bdstevel@tonic-gate		_nhp = &uri_hash_ab[(to)][_nhix];			\
2827c478bdstevel@tonic-gate		mutex_enter(&_nhp->lock);				\
2837c478bdstevel@tonic-gate		_nuri->hash = _nhp->list;				\
2847c478bdstevel@tonic-gate		_nhp->list = _nuri;					\
2857c478bdstevel@tonic-gate		_nuri->hit = 0;						\
2867c478bdstevel@tonic-gate		mutex_exit(&_nhp->lock);				\
2877c478bdstevel@tonic-gate	}								\
2887c478bdstevel@tonic-gate	mutex_exit(&(hp)->lock);					\
2897c478bdstevel@tonic-gate}
2907c478bdstevel@tonic-gate
2917c478bdstevel@tonic-gate#define	URI_HASH_UNLINK(cur, new, hp, puri, uri) {			\
2927c478bdstevel@tonic-gate	if ((puri) != NULL) {						\
2937c478bdstevel@tonic-gate		(puri)->hash = (uri)->hash;				\
2947c478bdstevel@tonic-gate	} else {							\
2957c478bdstevel@tonic-gate		(hp)->list = (uri)->hash;				\
2967c478bdstevel@tonic-gate	}								\
2971a5e258Josef 'Jeff' Sipek	if (atomic_dec_32_nv(&uri_hash_cnt[(cur)]) == 0 &&		\
2987c478bdstevel@tonic-gate	    uri_hash_ab[(new)] != NULL) {				\
2997c478bdstevel@tonic-gate		kmem_free(uri_hash_ab[cur],				\
3007c478bdstevel@tonic-gate		    sizeof (uri_hash_t) * uri_hash_sz[cur]);		\
3017c478bdstevel@tonic-gate		uri_hash_ab[(cur)] = NULL;				\
3027c478bdstevel@tonic-gate		uri_hash_lru[(cur)] = NULL;				\
3037c478bdstevel@tonic-gate		uri_hash_which = (new);					\
3047c478bdstevel@tonic-gate	} else {							\
3057c478bdstevel@tonic-gate		uri_hash_lru[(cur)] = (hp);				\
3067c478bdstevel@tonic-gate	}								\
3077c478bdstevel@tonic-gate}
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gatevoid
3107c478bdstevel@tonic-gatenl7c_uri_init(void)
3117c478bdstevel@tonic-gate{
3127c478bdstevel@tonic-gate	uint32_t	cur = uri_hash_which;
3137c478bdstevel@tonic-gate
3147c478bdstevel@tonic-gate	rw_init(&uri_hash_access, NULL, RW_DEFAULT, NULL);
3157c478bdstevel@tonic-gate
3167c478bdstevel@tonic-gate	uri_hash_sz[cur] = P2Ps[URI_HASH_N_INIT];
3177c478bdstevel@tonic-gate	uri_hash_overflow[cur] = P2Ps[URI_HASH_N_INIT] * URI_HASH_AVRG;
3187c478bdstevel@tonic-gate	uri_hash_ab[cur] = kmem_zalloc(sizeof (uri_hash_t) * uri_hash_sz[cur],
3197c478bdstevel@tonic-gate	    KM_SLEEP);
3207c478bdstevel@tonic-gate	uri_hash_lru[cur] = uri_hash_ab[cur];
3217c478bdstevel@tonic-gate
3222c9e429brutus	nl7c_uri_kmc = kmem_cache_create("NL7C_uri_kmc", sizeof (uri_desc_t),
3232c9e429brutus	    0, NULL, NULL, uri_kmc_reclaim, NULL, NULL, 0);
3247c478bdstevel@tonic-gate
3252c9e429brutus	nl7c_uri_rd_kmc = kmem_cache_create("NL7C_uri_rd_kmc",
3262c9e429brutus	    sizeof (uri_rd_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate	uri_desb_kmc = kmem_cache_create("NL7C_uri_desb_kmc",
3297c478bdstevel@tonic-gate	    sizeof (uri_desb_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate	uri_segmap_kmc = kmem_cache_create("NL7C_uri_segmap_kmc",
3327c478bdstevel@tonic-gate	    sizeof (uri_segmap_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate	nl7c_http_init();
3357c478bdstevel@tonic-gate}
3367c478bdstevel@tonic-gate
3372c9e429brutus#define	CV_SZ	16
3382c9e429brutus
3392c9e429brutusvoid
3402c9e429brutusnl7c_mi_report_hash(mblk_t *mp)
3412c9e429brutus{
3422c9e429brutus	uri_hash_t	*hp, *pend;
3432c9e429brutus	uri_desc_t	*uri;
3442c9e429brutus	uint32_t	cur;
3452c9e429brutus	uint32_t	new;
3462c9e429brutus	int		n, nz, tot;
3472c9e429brutus	uint32_t	cv[CV_SZ + 1];
3482c9e429brutus
3492c9e429brutus	rw_enter(&uri_hash_access, RW_READER);
3502c9e429brutus	cur = uri_hash_which;
3512c9e429brutus	new = cur ? 0 : 1;
3522c9e429brutusnext:
3532c9e429brutus	for (n = 0; n <= CV_SZ; n++)
3542c9e429brutus		cv[n] = 0;
3552c9e429brutus	nz = 0;
3562c9e429brutus	tot = 0;
3572c9e429brutus	hp = &uri_hash_ab[cur][0];
3582c9e429brutus	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
3592c9e429brutus	while (hp < pend) {
3602c9e429brutus		n = 0;
3612c9e429brutus		for (uri = hp->list; uri != NULL; uri = uri->hash) {
3622c9e429brutus			n++;
3632c9e429brutus		}
3642c9e429brutus		tot += n;
3652c9e429brutus		if (n > 0)
3662c9e429brutus			nz++;
3672c9e429brutus		if (n > CV_SZ)
3682c9e429brutus			n = CV_SZ;
3692c9e429brutus		cv[n]++;
3702c9e429brutus		hp++;
3712c9e429brutus	}
3722c9e429brutus
3732c9e429brutus	(void) mi_mpprintf(mp, "\nHash=%s, Buckets=%d, "
3742c9e429brutus	    "Avrg=%d\nCount by bucket:", cur != new ? "CUR" : "NEW",
3752c9e429brutus	    uri_hash_sz[cur], nz != 0 ? ((tot * 10 + 5) / nz) / 10 : 0);
3762c9e429brutus	(void) mi_mpprintf(mp, "Free=%d", cv[0]);
3772c9e429brutus	for (n = 1; n < CV_SZ; n++) {
3782c9e429brutus		int	pn = 0;
3792c9e429brutus		char	pv[5];
3802c9e429brutus		char	*pp = pv;
3812c9e429brutus
3822c9e429brutus		for (pn = n; pn < 1000; pn *= 10)
3832c9e429brutus			*pp++ = ' ';
3842c9e429brutus		*pp = 0;
3852c9e429brutus		(void) mi_mpprintf(mp, "%s%d=%d", pv, n, cv[n]);
3862c9e429brutus	}
3872c9e429brutus	(void) mi_mpprintf(mp, "Long=%d", cv[CV_SZ]);
3882c9e429brutus
3892c9e429brutus	if (cur != new && uri_hash_ab[new] != NULL) {
3902c9e429brutus		cur = new;
3912c9e429brutus		goto next;
3922c9e429brutus	}
3932c9e429brutus	rw_exit(&uri_hash_access);
3942c9e429brutus}
3952c9e429brutus
3962c9e429brutusvoid
3972c9e429brutusnl7c_mi_report_uri(mblk_t *mp)
3982c9e429brutus{
3992c9e429brutus	uri_hash_t	*hp;
4002c9e429brutus	uri_desc_t	*uri;
4012c9e429brutus	uint32_t	cur;
4022c9e429brutus	uint32_t	new;
4032c9e429brutus	int		ix;
4042c9e429brutus	int		ret;
4052c9e429brutus	char		sc;
4062c9e429brutus
4072c9e429brutus	rw_enter(&uri_hash_access, RW_READER);
4082c9e429brutus	cur = uri_hash_which;
4092c9e429brutus	new = cur ? 0 : 1;
4102c9e429brutusnext:
4112c9e429brutus	for (ix = 0; ix < uri_hash_sz[cur]; ix++) {
4122c9e429brutus		hp = &uri_hash_ab[cur][ix];
4132c9e429brutus		mutex_enter(&hp->lock);
4142c9e429brutus		uri = hp->list;
4152c9e429brutus		while (uri != NULL) {
4162c9e429brutus			sc = *(uri->path.ep);
4172c9e429brutus			*(uri->path.ep) = 0;
4182c9e429brutus			ret = mi_mpprintf(mp, "%s: %d %d %d",
4192c9e429brutus			    uri->path.cp, (int)uri->resplen,
4202c9e429brutus			    (int)uri->respclen, (int)uri->count);
4212c9e429brutus			*(uri->path.ep) = sc;
4222c9e429brutus			if (ret == -1) break;
4232c9e429brutus			uri = uri->hash;
4242c9e429brutus		}
4252c9e429brutus		mutex_exit(&hp->lock);
4262c9e429brutus		if (ret == -1) break;
4272c9e429brutus	}
4282c9e429brutus	if (ret != -1 && cur != new && uri_hash_ab[new] != NULL) {
4292c9e429brutus		cur = new;
4302c9e429brutus		goto next;
4312c9e429brutus	}
4322c9e429brutus	rw_exit(&uri_hash_access);
4332c9e429brutus}
4342c9e429brutus
4357c478bdstevel@tonic-gate/*
4367c478bdstevel@tonic-gate * The uri_desc_t ref_t inactive function called on the last REF_RELE(),
4377c478bdstevel@tonic-gate * free all resources contained in the uri_desc_t. Note, the uri_desc_t
4387c478bdstevel@tonic-gate * will be freed by REF_RELE() on return.
4397c478bdstevel@tonic-gate */
4407c478bdstevel@tonic-gate
4412c9e429brutusvoid
4422c9e429brutusnl7c_uri_inactive(uri_desc_t *uri)
4437c478bdstevel@tonic-gate{
4447c478bdstevel@tonic-gate	int64_t	 bytes = 0;
4457c478bdstevel@tonic-gate
4467c478bdstevel@tonic-gate	if (uri->tail) {
4477c478bdstevel@tonic-gate		uri_rd_t *rdp = &uri->response;
4487c478bdstevel@tonic-gate		uri_rd_t *free = NULL;
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate		while (rdp) {
4517c478bdstevel@tonic-gate			if (rdp->off == -1) {
4527c478bdstevel@tonic-gate				bytes += rdp->sz;
4537c478bdstevel@tonic-gate				kmem_free(rdp->data.kmem, rdp->sz);
4547c478bdstevel@tonic-gate			} else {
4557c478bdstevel@tonic-gate				VN_RELE(rdp->data.vnode);
4567c478bdstevel@tonic-gate			}
4577c478bdstevel@tonic-gate			rdp = rdp->next;
4587c478bdstevel@tonic-gate			if (free != NULL) {
4592c9e429brutus				kmem_cache_free(nl7c_uri_rd_kmc, free);
4607c478bdstevel@tonic-gate			}
4617c478bdstevel@tonic-gate			free = rdp;
4627c478bdstevel@tonic-gate		}
4637c478bdstevel@tonic-gate	}
4647c478bdstevel@tonic-gate	if (bytes) {
4657c478bdstevel@tonic-gate		atomic_add_64(&nl7c_uri_bytes, -bytes);
4667c478bdstevel@tonic-gate	}
4677c478bdstevel@tonic-gate	if (uri->scheme != NULL) {
4687c478bdstevel@tonic-gate		nl7c_http_free(uri->scheme);
4697c478bdstevel@tonic-gate	}
4707c478bdstevel@tonic-gate	if (uri->reqmp) {
4717c478bdstevel@tonic-gate		freeb(uri->reqmp);
4727c478bdstevel@tonic-gate	}
4737c478bdstevel@tonic-gate}
4747c478bdstevel@tonic-gate
4757c478bdstevel@tonic-gate/*
4767c478bdstevel@tonic-gate * The reclaim is called by the kmem subsystem when kmem is running
4777c478bdstevel@tonic-gate * low. More work is needed to determine the best reclaim policy, for
4787c478bdstevel@tonic-gate * now we just manipulate the nl7c_uri_max global maximum bytes threshold
4797c478bdstevel@tonic-gate * value using a simple arithmetic backoff of the value every time this
4807c478bdstevel@tonic-gate * function is called then call uri_reclaim() to enforce it.
4817c478bdstevel@tonic-gate *
4827c478bdstevel@tonic-gate * Note, this value remains in place and enforced for all subsequent
4837c478bdstevel@tonic-gate * URI request/response processing.
4847c478bdstevel@tonic-gate *
4857c478bdstevel@tonic-gate * Note, nl7c_uri_max is currently initialized to 0 or infinite such that
4867c478bdstevel@tonic-gate * the first call here set it to the current uri_bytes value then backoff
4877c478bdstevel@tonic-gate * from there.
4887c478bdstevel@tonic-gate *
4897c478bdstevel@tonic-gate * XXX how do we determine when to increase nl7c_uri_max ???
4907c478bdstevel@tonic-gate */
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gate/*ARGSUSED*/
4937c478bdstevel@tonic-gatestatic void
4947c478bdstevel@tonic-gateuri_kmc_reclaim(void *arg)
4957c478bdstevel@tonic-gate{
4967c478bdstevel@tonic-gate	uint64_t new_max;
4977c478bdstevel@tonic-gate
4987c478bdstevel@tonic-gate	if ((new_max = nl7c_uri_max) == 0) {
4997c478bdstevel@tonic-gate		/* Currently infinite, initialize to current bytes used */
5007c478bdstevel@tonic-gate		nl7c_uri_max = nl7c_uri_bytes;
5017c478bdstevel@tonic-gate		new_max = nl7c_uri_bytes;
5027c478bdstevel@tonic-gate	}
5037c478bdstevel@tonic-gate	if (new_max > 1) {
5047c478bdstevel@tonic-gate		/* Lower max_bytes to 93% of current value */
5057c478bdstevel@tonic-gate		new_max >>= 1;			/* 50% */
5067c478bdstevel@tonic-gate		new_max += (new_max >> 1);	/* 75% */
5077c478bdstevel@tonic-gate		new_max += (new_max >> 2);	/* 93% */
5087c478bdstevel@tonic-gate		if (new_max < nl7c_uri_max)
5097c478bdstevel@tonic-gate			nl7c_uri_max = new_max;
5107c478bdstevel@tonic-gate		else
5117c478bdstevel@tonic-gate			nl7c_uri_max = 1;
5127c478bdstevel@tonic-gate	}
5137c478bdstevel@tonic-gate	nl7c_uri_reclaim();
5147c478bdstevel@tonic-gate}
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate/*
5177c478bdstevel@tonic-gate * Delete a uri_desc_t from the URI hash.
5187c478bdstevel@tonic-gate */
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gatestatic void
5217c478bdstevel@tonic-gateuri_delete(uri_desc_t *del)
5227c478bdstevel@tonic-gate{
5237c478bdstevel@tonic-gate	uint32_t	hix;
5247c478bdstevel@tonic-gate	uri_hash_t	*hp;
5257c478bdstevel@tonic-gate	uri_desc_t	*uri;
5267c478bdstevel@tonic-gate	uri_desc_t	*puri;
5277c478bdstevel@tonic-gate	uint32_t	cur;
5287c478bdstevel@tonic-gate	uint32_t	new;
5297c478bdstevel@tonic-gate
5302c9e429brutus	ASSERT(del->hash != URI_TEMP);
5317c478bdstevel@tonic-gate	rw_enter(&uri_hash_access, RW_WRITER);
5327c478bdstevel@tonic-gate	cur = uri_hash_which;
5337c478bdstevel@tonic-gate	new = cur ? 0 : 1;
5347c478bdstevel@tonic-gatenext:
5357c478bdstevel@tonic-gate	puri = NULL;
5362c9e429brutus	hix = del->hvalue;
5372c9e429brutus	URI_HASH_IX(hix, cur);
5387c478bdstevel@tonic-gate	hp = &uri_hash_ab[cur][hix];
5397c478bdstevel@tonic-gate	for (uri = hp->list; uri != NULL; uri = uri->hash) {
5407c478bdstevel@tonic-gate		if (uri != del) {
5417c478bdstevel@tonic-gate			puri = uri;
5427c478bdstevel@tonic-gate			continue;
5437c478bdstevel@tonic-gate		}
5447c478bdstevel@tonic-gate		/*
5457c478bdstevel@tonic-gate		 * Found the URI, unlink from the hash chain,
5467c478bdstevel@tonic-gate		 * drop locks, ref release it.
5477c478bdstevel@tonic-gate		 */
5487c478bdstevel@tonic-gate		URI_HASH_UNLINK(cur, new, hp, puri, uri);
5497c478bdstevel@tonic-gate		rw_exit(&uri_hash_access);
5507c478bdstevel@tonic-gate		REF_RELE(uri);
5517c478bdstevel@tonic-gate		return;
5527c478bdstevel@tonic-gate	}
5537c478bdstevel@tonic-gate	if (cur != new && uri_hash_ab[new] != NULL) {
5547c478bdstevel@tonic-gate		/*
5557c478bdstevel@tonic-gate		 * Not found in current hash and have a new hash so
5567c478bdstevel@tonic-gate		 * check the new hash next.
5577c478bdstevel@tonic-gate		 */
5587c478bdstevel@tonic-gate		cur = new;
5597c478bdstevel@tonic-gate		goto next;
5607c478bdstevel@tonic-gate	}
5617c478bdstevel@tonic-gate	rw_exit(&uri_hash_access);
5627c478bdstevel@tonic-gate}
5637c478bdstevel@tonic-gate
5647c478bdstevel@tonic-gate/*
5657c478bdstevel@tonic-gate * Add a uri_desc_t to the URI hash.
5667c478bdstevel@tonic-gate */
5677c478bdstevel@tonic-gate
5687c478bdstevel@tonic-gatestatic void
5697c478bdstevel@tonic-gateuri_add(uri_desc_t *uri, krw_t rwlock, boolean_t nonblocking)
5707c478bdstevel@tonic-gate{
5717c478bdstevel@tonic-gate	uint32_t	hix;
5727c478bdstevel@tonic-gate	uri_hash_t	*hp;
5737c478bdstevel@tonic-gate	uint32_t	cur = uri_hash_which;
5747c478bdstevel@tonic-gate	uint32_t	new = cur ? 0 : 1;
5757c478bdstevel@tonic-gate
5767c478bdstevel@tonic-gate	/*
5777c478bdstevel@tonic-gate	 * Caller of uri_add() must hold the uri_hash_access rwlock.
5787c478bdstevel@tonic-gate	 */
5797c478bdstevel@tonic-gate	ASSERT((rwlock == RW_READER && RW_READ_HELD(&uri_hash_access)) ||
5807c478bdstevel@tonic-gate	    (rwlock == RW_WRITER && RW_WRITE_HELD(&uri_hash_access)));
5817c478bdstevel@tonic-gate	/*
5827c478bdstevel@tonic-gate	 * uri_add() always succeeds so add a hash ref to the URI now.
5837c478bdstevel@tonic-gate	 */
5847c478bdstevel@tonic-gate	REF_HOLD(uri);
5857c478bdstevel@tonic-gateagain:
5862c9e429brutus	hix = uri->hvalue;
5872c9e429brutus	URI_HASH_IX(hix, cur);
5887c478bdstevel@tonic-gate	if (uri_hash_ab[new] == NULL &&
5897c478bdstevel@tonic-gate	    uri_hash_cnt[cur] < uri_hash_overflow[cur]) {
5907c478bdstevel@tonic-gate		/*
5917c478bdstevel@tonic-gate		 * Easy case, no new hash and current hasn't overflowed,
5927c478bdstevel@tonic-gate		 * add URI to current hash and return.
5937c478bdstevel@tonic-gate		 *
5947c478bdstevel@tonic-gate		 * Note, the check for uri_hash_cnt[] above aren't done
5957c478bdstevel@tonic-gate		 * atomictally, i.e. multiple threads can be in this code
5967c478bdstevel@tonic-gate		 * as RW_READER and update the cnt[], this isn't a problem
5977c478bdstevel@tonic-gate		 * as the check is only advisory.
5987c478bdstevel@tonic-gate		 */
5997c478bdstevel@tonic-gate	fast:
6001a5e258Josef 'Jeff' Sipek		atomic_inc_32(&uri_hash_cnt[cur]);
6017c478bdstevel@tonic-gate		hp = &uri_hash_ab[cur][hix];
6027c478bdstevel@tonic-gate		mutex_enter(&hp->lock);
6037c478bdstevel@tonic-gate		uri->hash = hp->list;
6047c478bdstevel@tonic-gate		hp->list = uri;
6057c478bdstevel@tonic-gate		mutex_exit(&hp->lock);
6067c478bdstevel@tonic-gate		rw_exit(&uri_hash_access);
6077c478bdstevel@tonic-gate		return;
6087c478bdstevel@tonic-gate	}
6097c478bdstevel@tonic-gate	if (uri_hash_ab[new] == NULL) {
6107c478bdstevel@tonic-gate		/*
6117c478bdstevel@tonic-gate		 * Need a new a or b hash, if not already RW_WRITER
6127c478bdstevel@tonic-gate		 * try to upgrade our lock to writer.
6137c478bdstevel@tonic-gate		 */
6147c478bdstevel@tonic-gate		if (rwlock != RW_WRITER && ! rw_tryupgrade(&uri_hash_access)) {
6157c478bdstevel@tonic-gate			/*
6167c478bdstevel@tonic-gate			 * Upgrade failed, we can't simple exit and reenter
6177c478bdstevel@tonic-gate			 * the lock as after the exit and before the reenter
6187c478bdstevel@tonic-gate			 * the whole world can change so just wait for writer
6197c478bdstevel@tonic-gate			 * then do everything again.
6207c478bdstevel@tonic-gate			 */
6217c478bdstevel@tonic-gate			if (nonblocking) {
6227c478bdstevel@tonic-gate				/*
6237c478bdstevel@tonic-gate				 * Can't block, use fast-path above.
6247c478bdstevel@tonic-gate				 *
6257c478bdstevel@tonic-gate				 * XXX should have a background thread to
6267c478bdstevel@tonic-gate				 * handle new ab[] in this case so as to
6277c478bdstevel@tonic-gate				 * not overflow the cur hash to much.
6287c478bdstevel@tonic-gate				 */
6297c478bdstevel@tonic-gate				goto fast;
6307c478bdstevel@tonic-gate			}
6317c478bdstevel@tonic-gate			rw_exit(&uri_hash_access);
6327c478bdstevel@tonic-gate			rwlock = RW_WRITER;
6337c478bdstevel@tonic-gate			rw_enter(&uri_hash_access, rwlock);
6347c478bdstevel@tonic-gate			cur = uri_hash_which;
6357c478bdstevel@tonic-gate			new = cur ? 0 : 1;
6367c478bdstevel@tonic-gate			goto again;
6377c478bdstevel@tonic-gate		}
6387c478bdstevel@tonic-gate		rwlock = RW_WRITER;
6397c478bdstevel@tonic-gate		if (uri_hash_ab[new] == NULL) {
6407c478bdstevel@tonic-gate			/*
6417c478bdstevel@tonic-gate			 * Still need a new hash, allocate and initialize
6427c478bdstevel@tonic-gate			 * the new hash.
6437c478bdstevel@tonic-gate			 */
6447c478bdstevel@tonic-gate			uri_hash_n[new] = uri_hash_n[cur] + 1;
6457c478bdstevel@tonic-gate			if (uri_hash_n[new] == 0) {
6467c478bdstevel@tonic-gate				/*
6477c478bdstevel@tonic-gate				 * No larger P2Ps[] value so use current,
6487c478bdstevel@tonic-gate				 * i.e. 2 of the largest are better than 1 ?
6497c478bdstevel@tonic-gate				 */
6507c478bdstevel@tonic-gate				uri_hash_n[new] = uri_hash_n[cur];
6517c478bdstevel@tonic-gate				cmn_err(CE_NOTE, "NL7C: hash index overflow");
6527c478bdstevel@tonic-gate			}
6537c478bdstevel@tonic-gate			uri_hash_sz[new] = P2Ps[uri_hash_n[new]];
6547c478bdstevel@tonic-gate			ASSERT(uri_hash_cnt[new] == 0);
6557c478bdstevel@tonic-gate			uri_hash_overflow[new] = uri_hash_sz[new] *
6567c478bdstevel@tonic-gate			    URI_HASH_AVRG;
6577c478bdstevel@tonic-gate			uri_hash_ab[new] = kmem_zalloc(sizeof (uri_hash_t) *
6587c478bdstevel@tonic-gate			    uri_hash_sz[new], nonblocking ? KM_NOSLEEP :
6597c478bdstevel@tonic-gate			    KM_SLEEP);
6607c478bdstevel@tonic-gate			if (uri_hash_ab[new] == NULL) {
6617c478bdstevel@tonic-gate				/*
6627c478bdstevel@tonic-gate				 * Alloc failed, use fast-path above.
6637c478bdstevel@tonic-gate				 *
6647c478bdstevel@tonic-gate				 * XXX should have a background thread to
6657c478bdstevel@tonic-gate				 * handle new ab[] in this case so as to
6667c478bdstevel@tonic-gate				 * not overflow the cur hash to much.
6677c478bdstevel@tonic-gate				 */
6687c478bdstevel@tonic-gate				goto fast;
6697c478bdstevel@tonic-gate			}
6707c478bdstevel@tonic-gate			uri_hash_lru[new] = uri_hash_ab[new];
6717c478bdstevel@tonic-gate		}
6727c478bdstevel@tonic-gate	}
6737c478bdstevel@tonic-gate	/*
6747c478bdstevel@tonic-gate	 * Hashed against current hash so migrate any current hash chain
6757c478bdstevel@tonic-gate	 * members, if any.
6767c478bdstevel@tonic-gate	 *
6777c478bdstevel@tonic-gate	 * Note, the hash chain list can be checked for a non empty list
6787c478bdstevel@tonic-gate	 * outside of the hash chain list lock as the hash chain struct
6797c478bdstevel@tonic-gate	 * can't be destroyed while in the uri_hash_access rwlock, worst
6807c478bdstevel@tonic-gate	 * case is that a non empty list is found and after acquiring the
6817c478bdstevel@tonic-gate	 * lock another thread beats us to it (i.e. migrated the list).
6827c478bdstevel@tonic-gate	 */
6837c478bdstevel@tonic-gate	hp = &uri_hash_ab[cur][hix];
6847c478bdstevel@tonic-gate	if (hp->list != NULL) {
6857c478bdstevel@tonic-gate		URI_HASH_MIGRATE(cur, hp, new);
6867c478bdstevel@tonic-gate	}
6877c478bdstevel@tonic-gate	/*
6887c478bdstevel@tonic-gate	 * If new hash has overflowed before current hash has been
6897c478bdstevel@tonic-gate	 * completely migrated then walk all current hash chains and
6907c478bdstevel@tonic-gate	 * migrate list members now.
6917c478bdstevel@tonic-gate	 */
6921a5e258Josef 'Jeff' Sipek	if (atomic_inc_32_nv(&uri_hash_cnt[new]) >= uri_hash_overflow[new]) {
6937c478bdstevel@tonic-gate		for (hix = 0; hix < uri_hash_sz[cur]; hix++) {
6947c478bdstevel@tonic-gate			hp = &uri_hash_ab[cur][hix];
6957c478bdstevel@tonic-gate			if (hp->list != NULL) {
6967c478bdstevel@tonic-gate				URI_HASH_MIGRATE(cur, hp, new);
6977c478bdstevel@tonic-gate			}
6987c478bdstevel@tonic-gate		}
6997c478bdstevel@tonic-gate	}
7007c478bdstevel@tonic-gate	/*
7017c478bdstevel@tonic-gate	 * Add URI to new hash.
7027c478bdstevel@tonic-gate	 */
7032c9e429brutus	hix = uri->hvalue;
7042c9e429brutus	URI_HASH_IX(hix, new);
7057c478bdstevel@tonic-gate	hp = &uri_hash_ab[new][hix];
7067c478bdstevel@tonic-gate	mutex_enter(&hp->lock);
7077c478bdstevel@tonic-gate	uri->hash = hp->list;
7087c478bdstevel@tonic-gate	hp->list = uri;
7097c478bdstevel@tonic-gate	mutex_exit(&hp->lock);
7107c478bdstevel@tonic-gate	/*
7117c478bdstevel@tonic-gate	 * Last, check to see if last cur hash chain has been
7127c478bdstevel@tonic-gate	 * migrated, if so free cur hash and make new hash cur.
7137c478bdstevel@tonic-gate	 */
7147c478bdstevel@tonic-gate	if (uri_hash_cnt[cur] == 0) {
7157c478bdstevel@tonic-gate		/*
7167c478bdstevel@tonic-gate		 * If we don't already hold the uri_hash_access rwlock for
7177c478bdstevel@tonic-gate		 * RW_WRITE try to upgrade to RW_WRITE and if successful
7187c478bdstevel@tonic-gate		 * check again and to see if still need to do the free.
7197c478bdstevel@tonic-gate		 */
7207c478bdstevel@tonic-gate		if ((rwlock == RW_WRITER || rw_tryupgrade(&uri_hash_access)) &&
7217c478bdstevel@tonic-gate		    uri_hash_cnt[cur] == 0 && uri_hash_ab[new] != 0) {
7227c478bdstevel@tonic-gate			kmem_free(uri_hash_ab[cur],
7237c478bdstevel@tonic-gate			    sizeof (uri_hash_t) * uri_hash_sz[cur]);
7247c478bdstevel@tonic-gate			uri_hash_ab[cur] = NULL;
7257c478bdstevel@tonic-gate			uri_hash_lru[cur] = NULL;
7267c478bdstevel@tonic-gate			uri_hash_which = new;
7277c478bdstevel@tonic-gate		}
7287c478bdstevel@tonic-gate	}
7297c478bdstevel@tonic-gate	rw_exit(&uri_hash_access);
7307c478bdstevel@tonic-gate}
7317c478bdstevel@tonic-gate
7327c478bdstevel@tonic-gate/*
7337c478bdstevel@tonic-gate * Lookup a uri_desc_t in the URI hash, if found free the request uri_desc_t
7342c9e429brutus * and return the found uri_desc_t with a REF_HOLD() placed on it. Else, if
7352c9e429brutus * add B_TRUE use the request URI to create a new hash entry. Else if add
7362c9e429brutus * B_FALSE ...
7377c478bdstevel@tonic-gate */
7387c478bdstevel@tonic-gate
7397c478bdstevel@tonic-gatestatic uri_desc_t *
7407c478bdstevel@tonic-gateuri_lookup(uri_desc_t *ruri, boolean_t add, boolean_t nonblocking)
7417c478bdstevel@tonic-gate{
7427c478bdstevel@tonic-gate	uint32_t	hix;
7437c478bdstevel@tonic-gate	uri_hash_t	*hp;
7447c478bdstevel@tonic-gate	uri_desc_t	*uri;
7457c478bdstevel@tonic-gate	uri_desc_t	*puri;
7467c478bdstevel@tonic-gate	uint32_t	cur;
7477c478bdstevel@tonic-gate	uint32_t	new;
7487c478bdstevel@tonic-gate	char		*rcp = ruri->path.cp;
7497c478bdstevel@tonic-gate	char		*rep = ruri->path.ep;
7507c478bdstevel@tonic-gate
7517c478bdstevel@tonic-gateagain:
7527c478bdstevel@tonic-gate	rw_enter(&uri_hash_access, RW_READER);
7537c478bdstevel@tonic-gate	cur = uri_hash_which;
7547c478bdstevel@tonic-gate	new = cur ? 0 : 1;
7557c478bdstevel@tonic-gatenexthash:
7567c478bdstevel@tonic-gate	puri = NULL;
7572c9e429brutus	hix = ruri->hvalue;
7582c9e429brutus	URI_HASH_IX(hix, cur);
7597c478bdstevel@tonic-gate	hp = &uri_hash_ab[cur][hix];
7607c478bdstevel@tonic-gate	mutex_enter(&hp->lock);
7617c478bdstevel@tonic-gate	for (uri = hp->list; uri != NULL; uri = uri->hash) {
7627c478bdstevel@tonic-gate		char	*ap = uri->path.cp;
7637c478bdstevel@tonic-gate		char	*bp = rcp;
7647c478bdstevel@tonic-gate		char	a, b;
7657c478bdstevel@tonic-gate
7667c478bdstevel@tonic-gate		/* Compare paths */
7677c478bdstevel@tonic-gate		while (bp < rep && ap < uri->path.ep) {
7687c478bdstevel@tonic-gate			if ((a = *ap) == '%') {
7697c478bdstevel@tonic-gate				/* Escaped hex multichar, convert it */
7707c478bdstevel@tonic-gate				H2A(ap, uri->path.ep, a);
7717c478bdstevel@tonic-gate			}
7727c478bdstevel@tonic-gate			if ((b = *bp) == '%') {
7737c478bdstevel@tonic-gate				/* Escaped hex multichar, convert it */
7747c478bdstevel@tonic-gate				H2A(bp, rep, b);
7757c478bdstevel@tonic-gate			}
7767c478bdstevel@tonic-gate			if (a != b) {
7777c478bdstevel@tonic-gate				/* Char's don't match */
7787c478bdstevel@tonic-gate				goto nexturi;
7797c478bdstevel@tonic-gate			}
7807c478bdstevel@tonic-gate			ap++;
7817c478bdstevel@tonic-gate			bp++;
7827c478bdstevel@tonic-gate		}
7837c478bdstevel@tonic-gate		if (bp != rep || ap != uri->path.ep) {
7847c478bdstevel@tonic-gate			/* Not same length */
7857c478bdstevel@tonic-gate			goto nexturi;
7867c478bdstevel@tonic-gate		}
7877c478bdstevel@tonic-gate		ap = uri->auth.cp;
7887c478bdstevel@tonic-gate		bp = ruri->auth.cp;
7897c478bdstevel@tonic-gate		if (ap != NULL) {
7907c478bdstevel@tonic-gate			if (bp == NULL) {
7917c478bdstevel@tonic-gate				/* URI has auth request URI doesn't */
7927c478bdstevel@tonic-gate				goto nexturi;
7937c478bdstevel@tonic-gate			}
7947c478bdstevel@tonic-gate			while (bp < ruri->auth.ep && ap < uri->auth.ep) {
7957c478bdstevel@tonic-gate				if ((a = *ap) == '%') {
7967c478bdstevel@tonic-gate					/* Escaped hex multichar, convert it */
7977c478bdstevel@tonic-gate					H2A(ap, uri->path.ep, a);
7987c478bdstevel@tonic-gate				}
7997c478bdstevel@tonic-gate				if ((b = *bp) == '%') {
8007c478bdstevel@tonic-gate					/* Escaped hex multichar, convert it */
8017c478bdstevel@tonic-gate					H2A(bp, rep, b);
8027c478bdstevel@tonic-gate				}
8037c478bdstevel@tonic-gate				if (a != b) {
8047c478bdstevel@tonic-gate					/* Char's don't match */
8057c478bdstevel@tonic-gate					goto nexturi;
8067c478bdstevel@tonic-gate				}
8077c478bdstevel@tonic-gate				ap++;
8087c478bdstevel@tonic-gate				bp++;
8097c478bdstevel@tonic-gate			}
8107c478bdstevel@tonic-gate			if (bp != ruri->auth.ep || ap != uri->auth.ep) {
8117c478bdstevel@tonic-gate				/* Not same length */
8127c478bdstevel@tonic-gate				goto nexturi;
8137c478bdstevel@tonic-gate			}
8147c478bdstevel@tonic-gate		} else if (bp != NULL) {
8157c478bdstevel@tonic-gate			/* URI doesn't have auth and request URI does */
8167c478bdstevel@tonic-gate			goto nexturi;
8177c478bdstevel@tonic-gate		}
8187c478bdstevel@tonic-gate		/*
8192c9e429brutus		 * Have a path/auth match so before any other processing
8202c9e429brutus		 * of requested URI, check for expire or request no cache
8212c9e429brutus		 * purge.
8227c478bdstevel@tonic-gate		 */
823d3d5073Rafael Vanoni		if (uri->expire >= 0 && uri->expire <= ddi_get_lbolt() ||
824d3d5073Rafael Vanoni		    ruri->nocache) {
8257c478bdstevel@tonic-gate			/*
8267c478bdstevel@tonic-gate			 * URI has expired or request specified to not use
8277c478bdstevel@tonic-gate			 * the cached version, unlink the URI from the hash
8287c478bdstevel@tonic-gate			 * chain, release all locks, release the hash ref
8297c478bdstevel@tonic-gate			 * on the URI, and last look it up again.
8302c9e429brutus			 *
8312c9e429brutus			 * Note, this will cause all variants of the named
8322c9e429brutus			 * URI to be purged.
8337c478bdstevel@tonic-gate			 */
8347c478bdstevel@tonic-gate			if (puri != NULL) {
8357c478bdstevel@tonic-gate				puri->hash = uri->hash;
8367c478bdstevel@tonic-gate			} else {
8377c478bdstevel@tonic-gate				hp->list = uri->hash;
8387c478bdstevel@tonic-gate			}
8397c478bdstevel@tonic-gate			mutex_exit(&hp->lock);
8401a5e258Josef 'Jeff' Sipek			atomic_dec_32(&uri_hash_cnt[cur]);
8417c478bdstevel@tonic-gate			rw_exit(&uri_hash_access);
8422c9e429brutus			if (ruri->nocache)
8432c9e429brutus				nl7c_uri_purge++;
8442c9e429brutus			else
8452c9e429brutus				nl7c_uri_expire++;
8467c478bdstevel@tonic-gate			REF_RELE(uri);
8477c478bdstevel@tonic-gate			goto again;
8487c478bdstevel@tonic-gate		}
8492c9e429brutus		if (uri->scheme != NULL) {
8502c9e429brutus			/*
8512c9e429brutus			 * URI has scheme private qualifier(s), if request
8522c9e429brutus			 * URI doesn't or if no match skip this URI.
8532c9e429brutus			 */
8542c9e429brutus			if (ruri->scheme == NULL ||
8552c9e429brutus			    ! nl7c_http_cmp(uri->scheme, ruri->scheme))
8562c9e429brutus				goto nexturi;
8572c9e429brutus		} else if (ruri->scheme != NULL) {
8582c9e429brutus			/*
8592c9e429brutus			 * URI doesn't have scheme private qualifiers but
8602c9e429brutus			 * request URI does, no match, skip this URI.
8612c9e429brutus			 */
8622c9e429brutus			goto nexturi;
8632c9e429brutus		}
8647c478bdstevel@tonic-gate		/*
8652c9e429brutus		 * Have a match, ready URI for return, first put a reference
8662c9e429brutus		 * hold on the URI, if this URI is currently being processed
8672c9e429brutus		 * then have to wait for the processing to be completed and
8682c9e429brutus		 * redo the lookup, else return it.
8697c478bdstevel@tonic-gate		 */
8707c478bdstevel@tonic-gate		REF_HOLD(uri);
8717c478bdstevel@tonic-gate		mutex_enter(&uri->proclock);
8727c478bdstevel@tonic-gate		if (uri->proc != NULL) {
8737c478bdstevel@tonic-gate			/* The URI is being processed, wait for completion */
8747c478bdstevel@tonic-gate			mutex_exit(&hp->lock);
8757c478bdstevel@tonic-gate			rw_exit(&uri_hash_access);
8767c478bdstevel@tonic-gate			if (! nonblocking &&
8777c478bdstevel@tonic-gate			    cv_wait_sig(&uri->waiting, &uri->proclock)) {
8787c478bdstevel@tonic-gate				/*
8797c478bdstevel@tonic-gate				 * URI has been processed but things may
8807c478bdstevel@tonic-gate				 * have changed while we were away so do
8817c478bdstevel@tonic-gate				 * most everything again.
8827c478bdstevel@tonic-gate				 */
8837c478bdstevel@tonic-gate				mutex_exit(&uri->proclock);
8847c478bdstevel@tonic-gate				REF_RELE(uri);
8857c478bdstevel@tonic-gate				goto again;
8867c478bdstevel@tonic-gate			} else {
8877c478bdstevel@tonic-gate				/*
8887c478bdstevel@tonic-gate				 * A nonblocking socket or an interrupted
8897c478bdstevel@tonic-gate				 * cv_wait_sig() in the first case can't
8907c478bdstevel@tonic-gate				 * block waiting for the processing of the
8917c478bdstevel@tonic-gate				 * uri hash hit uri to complete, in both
8927c478bdstevel@tonic-gate				 * cases just return failure to lookup.
8937c478bdstevel@tonic-gate				 */
8947c478bdstevel@tonic-gate				mutex_exit(&uri->proclock);
8957c478bdstevel@tonic-gate				REF_RELE(uri);
8967c478bdstevel@tonic-gate				return (NULL);
8977c478bdstevel@tonic-gate			}
8987c478bdstevel@tonic-gate		}
8992c9e429brutus		mutex_exit(&uri->proclock);
9007c478bdstevel@tonic-gate		uri->hit++;
9017c478bdstevel@tonic-gate		mutex_exit(&hp->lock);
9027c478bdstevel@tonic-gate		rw_exit(&uri_hash_access);
9037c478bdstevel@tonic-gate		return (uri);
9047c478bdstevel@tonic-gate	nexturi:
9057c478bdstevel@tonic-gate		puri = uri;
9067c478bdstevel@tonic-gate	}
9077c478bdstevel@tonic-gate	mutex_exit(&hp->lock);
9087c478bdstevel@tonic-gate	if (cur != new && uri_hash_ab[new] != NULL) {
9097c478bdstevel@tonic-gate		/*
9107c478bdstevel@tonic-gate		 * Not found in current hash and have a new hash so
9117c478bdstevel@tonic-gate		 * check the new hash next.
9127c478bdstevel@tonic-gate		 */
9137c478bdstevel@tonic-gate		cur = new;
9147c478bdstevel@tonic-gate		goto nexthash;
9157c478bdstevel@tonic-gate	}
9167c478bdstevel@tonic-gateadd:
9177c478bdstevel@tonic-gate	if (! add) {
9182c9e429brutus		/* Lookup only so return failure */
9197c478bdstevel@tonic-gate		rw_exit(&uri_hash_access);
9207c478bdstevel@tonic-gate		return (NULL);
9217c478bdstevel@tonic-gate	}
9227c478bdstevel@tonic-gate	/*
9237c478bdstevel@tonic-gate	 * URI not hashed, finish intialization of the
9247c478bdstevel@tonic-gate	 * request URI, add it to the hash, return it.
9257c478bdstevel@tonic-gate	 */
9267c478bdstevel@tonic-gate	ruri->hit = 0;
9277c478bdstevel@tonic-gate	ruri->expire = -1;
9287c478bdstevel@tonic-gate	ruri->response.sz = 0;
929e9f74eaToomas Soome	ruri->proc = (struct sonode *)~0;
9307c478bdstevel@tonic-gate	cv_init(&ruri->waiting, NULL, CV_DEFAULT, NULL);
9317c478bdstevel@tonic-gate	mutex_init(&ruri->proclock, NULL, MUTEX_DEFAULT, NULL);
9327c478bdstevel@tonic-gate	uri_add(ruri, RW_READER, nonblocking);
9337c478bdstevel@tonic-gate	/* uri_add() has done rw_exit(&uri_hash_access) */
9347c478bdstevel@tonic-gate	return (ruri);
9357c478bdstevel@tonic-gate}
9367c478bdstevel@tonic-gate
9377c478bdstevel@tonic-gate/*
9387c478bdstevel@tonic-gate * Reclaim URIs until max cache size threshold has been reached.
9397c478bdstevel@tonic-gate *
9407c478bdstevel@tonic-gate * A CLOCK based reclaim modified with a history (hit counter) counter.
9417c478bdstevel@tonic-gate */
9427c478bdstevel@tonic-gate
9437c478bdstevel@tonic-gatestatic void
9447c478bdstevel@tonic-gatenl7c_uri_reclaim(void)
9457c478bdstevel@tonic-gate{
9467c478bdstevel@tonic-gate	uri_hash_t	*hp, *start, *pend;
9477c478bdstevel@tonic-gate	uri_desc_t	*uri;
9487c478bdstevel@tonic-gate	uri_desc_t	*puri;
9497c478bdstevel@tonic-gate	uint32_t	cur;
9507c478bdstevel@tonic-gate	uint32_t	new;
9517c478bdstevel@tonic-gate
9527c478bdstevel@tonic-gate	nl7c_uri_reclaim_calls++;
9537c478bdstevel@tonic-gateagain:
9547c478bdstevel@tonic-gate	rw_enter(&uri_hash_access, RW_WRITER);
9557c478bdstevel@tonic-gate	cur = uri_hash_which;
9567c478bdstevel@tonic-gate	new = cur ? 0 : 1;
9577c478bdstevel@tonic-gatenext:
9587c478bdstevel@tonic-gate	hp = uri_hash_lru[cur];
9597c478bdstevel@tonic-gate	start = hp;
9607c478bdstevel@tonic-gate	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
9617c478bdstevel@tonic-gate	while (nl7c_uri_bytes > nl7c_uri_max) {
9627c478bdstevel@tonic-gate		puri = NULL;
9637c478bdstevel@tonic-gate		for (uri = hp->list; uri != NULL; uri = uri->hash) {
9647c478bdstevel@tonic-gate			if (uri->hit != 0) {
9657c478bdstevel@tonic-gate				/*
9667c478bdstevel@tonic-gate				 * Decrement URI activity counter and skip.
9677c478bdstevel@tonic-gate				 */
9687c478bdstevel@tonic-gate				uri->hit--;
9697c478bdstevel@tonic-gate				puri = uri;
9707c478bdstevel@tonic-gate				continue;
9717c478bdstevel@tonic-gate			}
9727c478bdstevel@tonic-gate			if (uri->proc != NULL) {
9737c478bdstevel@tonic-gate				/*
9747c478bdstevel@tonic-gate				 * Currently being processed by a socket, skip.
9757c478bdstevel@tonic-gate				 */
9767c478bdstevel@tonic-gate				continue;
9777c478bdstevel@tonic-gate			}
9787c478bdstevel@tonic-gate			/*
9797c478bdstevel@tonic-gate			 * Found a candidate, no hit(s) since added or last
9807c478bdstevel@tonic-gate			 * reclaim pass, unlink from it's hash chain, update
9817c478bdstevel@tonic-gate			 * lru scan pointer, drop lock, ref release it.
9827c478bdstevel@tonic-gate			 */
9837c478bdstevel@tonic-gate			URI_HASH_UNLINK(cur, new, hp, puri, uri);
9847c478bdstevel@tonic-gate			if (cur == uri_hash_which) {
9857c478bdstevel@tonic-gate				if (++hp == pend) {
9867c478bdstevel@tonic-gate					/* Wrap pointer */
9877c478bdstevel@tonic-gate					hp = uri_hash_ab[cur];
9887c478bdstevel@tonic-gate				}
9897c478bdstevel@tonic-gate				uri_hash_lru[cur] = hp;
9907c478bdstevel@tonic-gate			}
9917c478bdstevel@tonic-gate			rw_exit(&uri_hash_access);
9927c478bdstevel@tonic-gate			REF_RELE(uri);
9937c478bdstevel@tonic-gate			nl7c_uri_reclaim_cnt++;
9947c478bdstevel@tonic-gate			goto again;
9957c478bdstevel@tonic-gate		}
9967c478bdstevel@tonic-gate		if (++hp == pend) {
9977c478bdstevel@tonic-gate			/* Wrap pointer */
9987c478bdstevel@tonic-gate			hp = uri_hash_ab[cur];
9997c478bdstevel@tonic-gate		}
10007c478bdstevel@tonic-gate		if (hp == start) {
10017c478bdstevel@tonic-gate			if (cur != new && uri_hash_ab[new] != NULL) {
10027c478bdstevel@tonic-gate				/*
10037c478bdstevel@tonic-gate				 * Done with the current hash and have a
10047c478bdstevel@tonic-gate				 * new hash so check the new hash next.
10057c478bdstevel@tonic-gate				 */
10067c478bdstevel@tonic-gate				cur = new;
10077c478bdstevel@tonic-gate				goto next;
10087c478bdstevel@tonic-gate			}
10097c478bdstevel@tonic-gate		}
10107c478bdstevel@tonic-gate	}
10117c478bdstevel@tonic-gate	rw_exit(&uri_hash_access);
10127c478bdstevel@tonic-gate}
10137c478bdstevel@tonic-gate
10147c478bdstevel@tonic-gate/*
10157c478bdstevel@tonic-gate * Called for a socket which is being freed prior to close, e.g. errored.
10167c478bdstevel@tonic-gate */
10177c478bdstevel@tonic-gate
10187c478bdstevel@tonic-gatevoid
10197c478bdstevel@tonic-gatenl7c_urifree(struct sonode *so)
10207c478bdstevel@tonic-gate{
10210f1702cYu Xiangning<Eric.Yu@Sun.COM>	sotpi_info_t *sti = SOTOTPI(so);
10220f1702cYu Xiangning<Eric.Yu@Sun.COM>	uri_desc_t *uri = (uri_desc_t *)sti->sti_nl7c_uri;
10237c478bdstevel@tonic-gate
10240f1702cYu Xiangning<Eric.Yu@Sun.COM>	sti->sti_nl7c_uri = NULL;
10257c478bdstevel@tonic-gate	if (uri->hash != URI_TEMP) {
10267c478bdstevel@tonic-gate		uri_delete(uri);
10277c478bdstevel@tonic-gate		mutex_enter(&uri->proclock);
10287c478bdstevel@tonic-gate		uri->proc = NULL;
10297c478bdstevel@tonic-gate		if (CV_HAS_WAITERS(&uri->waiting)) {
10307c478bdstevel@tonic-gate			cv_broadcast(&uri->waiting);
10317c478bdstevel@tonic-gate		}
10327c478bdstevel@tonic-gate		mutex_exit(&uri->proclock);
10337c478bdstevel@tonic-gate		nl7c_uri_free++;
10347c478bdstevel@tonic-gate	} else {
10357c478bdstevel@tonic-gate		/* No proclock as uri exclusively owned by so */
10367c478bdstevel@tonic-gate		uri->proc = NULL;
10377c478bdstevel@tonic-gate		nl7c_uri_temp_free++;
10387c478bdstevel@tonic-gate	}
10397c478bdstevel@tonic-gate	REF_RELE(uri);
10407c478bdstevel@tonic-gate}
10417c478bdstevel@tonic-gate
10427c478bdstevel@tonic-gate/*
10432c9e429brutus * ...
10442c9e429brutus *
10452c9e429brutus *	< 0	need more data
10462c9e429brutus *
10472c9e429brutus *	  0	parse complete
10482c9e429brutus *
10492c9e429brutus *	> 0	parse error
10507c478bdstevel@tonic-gate */
10517c478bdstevel@tonic-gate
10522c9e429brutusvolatile uint64_t nl7c_resp_pfail = 0;
10532c9e429brutusvolatile uint64_t nl7c_resp_ntemp = 0;
10542c9e429brutusvolatile uint64_t nl7c_resp_pass = 0;
10557c478bdstevel@tonic-gate
10562c9e429brutusstatic int
10572c9e429brutusnl7c_resp_parse(struct sonode *so, uri_desc_t *uri, char *data, int sz)
10582c9e429brutus{
10592c9e429brutus	if (! nl7c_http_response(&data, &data[sz], uri, so)) {
10602c9e429brutus		if (data == NULL) {
10612c9e429brutus			/* Parse fail */
10622c9e429brutus			goto pfail;
10632c9e429brutus		}
10642c9e429brutus		/* More data */
10652c9e429brutus		data = NULL;
10662c9e429brutus	} else if (data == NULL) {
10672c9e429brutus		goto pass;
10682c9e429brutus	}
10692c9e429brutus	if (uri->hash != URI_TEMP && uri->nocache) {
10702c9e429brutus		/*
10712c9e429brutus		 * After response parse now no cache,
10722c9e429brutus		 * delete it from cache, wakeup any
10732c9e429brutus		 * waiters on this URI, make URI_TEMP.
10742c9e429brutus		 */
10752c9e429brutus		uri_delete(uri);
10762c9e429brutus		mutex_enter(&uri->proclock);
10772c9e429brutus		if (CV_HAS_WAITERS(&uri->waiting)) {
10782c9e429brutus			cv_broadcast(&uri->waiting);
10792c9e429brutus		}
10802c9e429brutus		mutex_exit(&uri->proclock);
10812c9e429brutus		uri->hash = URI_TEMP;
10822c9e429brutus		nl7c_uri_temp_mk++;
10832c9e429brutus	}
10842c9e429brutus	if (data == NULL) {
10852c9e429brutus		/* More data needed */
10862c9e429brutus		return (-1);
10872c9e429brutus	}
10882c9e429brutus	/* Success */
10892c9e429brutus	return (0);
10902c9e429brutus
10912c9e429brutuspfail:
10922c9e429brutus	nl7c_resp_pfail++;
10932c9e429brutus	return (EINVAL);
10942c9e429brutus
10952c9e429brutuspass:
10962c9e429brutus	nl7c_resp_pass++;
10972c9e429brutus	return (ENOTSUP);
10982c9e429brutus}
10992c9e429brutus
11002c9e429brutus/*
11012c9e429brutus * Called to sink application response data, the processing of the data
11022c9e429brutus * is the same for a cached or temp URI (i.e. a URI for which we aren't
11032c9e429brutus * going to cache the URI but want to parse it for detecting response
11042c9e429brutus * data end such that for a persistent connection we can parse the next
11052c9e429brutus * request).
11062c9e429brutus *
11072c9e429brutus * On return 0 is returned for sink success, > 0 on error, and < 0 on
11082c9e429brutus * no so URI (note, data not sinked).
11092c9e429brutus */
11102c9e429brutus
11112c9e429brutusint
11122c9e429brutusnl7c_data(struct sonode *so, uio_t *uio)
11137c478bdstevel@tonic-gate{
11140f1702cYu Xiangning<Eric.Yu@Sun.COM>	sotpi_info_t	*sti = SOTOTPI(so);
11150f1702cYu Xiangning<Eric.Yu@Sun.COM>	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
11162c9e429brutus	iovec_t		*iov;
11172c9e429brutus	int		cnt;
11182c9e429brutus	int		sz = uio->uio_resid;
11192c9e429brutus	char		*data, *alloc;
11207c478bdstevel@tonic-gate	char		*bp;
11217c478bdstevel@tonic-gate	uri_rd_t	*rdp;
11222c9e429brutus	boolean_t	first;
11232c9e429brutus	int		error, perror;
11247c478bdstevel@tonic-gate
11257c478bdstevel@tonic-gate	nl7c_uri_data++;
11267c478bdstevel@tonic-gate
11277c478bdstevel@tonic-gate	if (uri == NULL) {
11287c478bdstevel@tonic-gate		/* Socket & NL7C out of sync, disable NL7C */
11290f1702cYu Xiangning<Eric.Yu@Sun.COM>		sti->sti_nl7c_flags = 0;
11307c478bdstevel@tonic-gate		nl7c_uri_NULL1++;
11312c9e429brutus		return (-1);
11327c478bdstevel@tonic-gate	}
11337c478bdstevel@tonic-gate
11340f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sti->sti_nl7c_flags & NL7C_WAITWRITE) {
11350f1702cYu Xiangning<Eric.Yu@Sun.COM>		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
11362c9e429brutus		first = B_TRUE;
11377c478bdstevel@tonic-gate	} else {
11382c9e429brutus		first = B_FALSE;
11397c478bdstevel@tonic-gate	}
11402c9e429brutus
11412c9e429brutus	alloc = kmem_alloc(sz, KM_SLEEP);
11422c9e429brutus	URI_RD_ADD(uri, rdp, sz, -1);
11432c9e429brutus
11442c9e429brutus	if (uri->hash != URI_TEMP && uri->count > nca_max_cache_size) {
11452c9e429brutus		uri_delete(uri);
11462c9e429brutus		uri->hash = URI_TEMP;
11477c478bdstevel@tonic-gate	}
11482c9e429brutus	data = alloc;
11492c9e429brutus	alloc = NULL;
11502c9e429brutus	rdp->data.kmem = data;
11512c9e429brutus	atomic_add_64(&nl7c_uri_bytes, sz);
11522c9e429brutus
11537c478bdstevel@tonic-gate	bp = data;
11542c9e429brutus	while (uio->uio_resid > 0) {
11552c9e429brutus		iov = uio->uio_iov;
11562c9e429brutus		if ((cnt = iov->iov_len) == 0) {
11572c9e429brutus			goto next;
11587c478bdstevel@tonic-gate		}
11592c9e429brutus		cnt = MIN(cnt, uio->uio_resid);
11602c9e429brutus		error = xcopyin(iov->iov_base, bp, cnt);
11612c9e429brutus		if (error)
11622c9e429brutus			goto fail;
11632c9e429brutus
11642c9e429brutus		iov->iov_base += cnt;
11652c9e429brutus		iov->iov_len -= cnt;
11662c9e429brutus		uio->uio_resid -= cnt;
11672c9e429brutus		uio->uio_loffset += cnt;
11687c478bdstevel@tonic-gate		bp += cnt;
11692c9e429brutus	next:
11702c9e429brutus		uio->uio_iov++;
11712c9e429brutus		uio->uio_iovcnt--;
11727c478bdstevel@tonic-gate	}
11732c9e429brutus
11742c9e429brutus	/* Successfull sink of data, response parse the data */
11752c9e429brutus	perror = nl7c_resp_parse(so, uri, data, sz);
11762c9e429brutus
11772c9e429brutus	/* Send the data out the connection */
11782c9e429brutus	error = uri_rd_response(so, uri, rdp, first);
11792c9e429brutus	if (error)
11802c9e429brutus		goto fail;
11812c9e429brutus
11822c9e429brutus	/* Success */
11832c9e429brutus	if (perror == 0 &&
11842c9e429brutus	    ((uri->respclen == URI_LEN_NOVALUE &&
11852c9e429brutus	    uri->resplen == URI_LEN_NOVALUE) ||
11862c9e429brutus	    uri->count >= uri->resplen)) {
11877c478bdstevel@tonic-gate		/*
11882c9e429brutus		 * No more data needed and no pending response
11892c9e429brutus		 * data or current data count >= response length
11902c9e429brutus		 * so close the URI processing for this so.
11917c478bdstevel@tonic-gate		 */
11927c478bdstevel@tonic-gate		nl7c_close(so);
11930f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (! (sti->sti_nl7c_flags & NL7C_SOPERSIST)) {
11942c9e429brutus			/* Not a persistent connection */
11950f1702cYu Xiangning<Eric.Yu@Sun.COM>			sti->sti_nl7c_flags = 0;
11962c9e429brutus		}
11977c478bdstevel@tonic-gate	}
11982c9e429brutus
11992c9e429brutus	return (0);
12007c478bdstevel@tonic-gate
12017c478bdstevel@tonic-gatefail:
12027c478bdstevel@tonic-gate	if (alloc != NULL) {
12037c478bdstevel@tonic-gate		kmem_free(alloc, sz);
12047c478bdstevel@tonic-gate	}
12050f1702cYu Xiangning<Eric.Yu@Sun.COM>	sti->sti_nl7c_flags = 0;
12067c478bdstevel@tonic-gate	nl7c_urifree(so);
12072c9e429brutus
12082c9e429brutus	return (error);
12097c478bdstevel@tonic-gate}
12107c478bdstevel@tonic-gate
12117c478bdstevel@tonic-gate/*
12127c478bdstevel@tonic-gate * Called to read data from file "*fp" at offset "*off" of length "*len"
12137c478bdstevel@tonic-gate * for a maximum of "*max_rem" bytes.
12147c478bdstevel@tonic-gate *
12157c478bdstevel@tonic-gate * On success a pointer to the kmem_alloc()ed file data is returned, "*off"
12167c478bdstevel@tonic-gate * and "*len" are updated for the acutal number of bytes read and "*max_rem"
12177c478bdstevel@tonic-gate * is updated with the number of bytes remaining to be read.
12187c478bdstevel@tonic-gate *
12197c478bdstevel@tonic-gate * Else, "NULL" is returned.
12207c478bdstevel@tonic-gate */
12217c478bdstevel@tonic-gate
12227c478bdstevel@tonic-gatestatic char *
12232c9e429brutusnl7c_readfile(file_t *fp, u_offset_t *off, int *len, int max, int *ret)
12247c478bdstevel@tonic-gate{
12257c478bdstevel@tonic-gate	vnode_t	*vp = fp->f_vnode;
12267c478bdstevel@tonic-gate	int	flg = 0;
12272c9e429brutus	size_t	size = MIN(*len, max);
12287c478bdstevel@tonic-gate	char	*data;
12297c478bdstevel@tonic-gate	int	error;
12307c478bdstevel@tonic-gate	uio_t	uio;
12317c478bdstevel@tonic-gate	iovec_t	iov;
12327c478bdstevel@tonic-gate
12337c478bdstevel@tonic-gate	(void) VOP_RWLOCK(vp, flg, NULL);
12347c478bdstevel@tonic-gate
12357c478bdstevel@tonic-gate	if (*off > MAXOFFSET_T) {
12367c478bdstevel@tonic-gate		VOP_RWUNLOCK(vp, flg, NULL);
12372c9e429brutus		*ret = EFBIG;
12387c478bdstevel@tonic-gate		return (NULL);
12397c478bdstevel@tonic-gate	}
12407c478bdstevel@tonic-gate
12417c478bdstevel@tonic-gate	if (*off + size > MAXOFFSET_T)
12427c478bdstevel@tonic-gate		size = (ssize32_t)(MAXOFFSET_T - *off);
12437c478bdstevel@tonic-gate
12447c478bdstevel@tonic-gate	data = kmem_alloc(size, KM_SLEEP);
12457c478bdstevel@tonic-gate
12467c478bdstevel@tonic-gate	iov.iov_base = data;
12477c478bdstevel@tonic-gate	iov.iov_len = size;
12487c478bdstevel@tonic-gate	uio.uio_loffset = *off;
12497c478bdstevel@tonic-gate	uio.uio_iov = &iov;
12507c478bdstevel@tonic-gate	uio.uio_iovcnt = 1;
12517c478bdstevel@tonic-gate	uio.uio_resid = size;
12527c478bdstevel@tonic-gate	uio.uio_segflg = UIO_SYSSPACE;
12537c478bdstevel@tonic-gate	uio.uio_llimit = MAXOFFSET_T;
12547c478bdstevel@tonic-gate	uio.uio_fmode = fp->f_flag;
12557c478bdstevel@tonic-gate
12567c478bdstevel@tonic-gate	error = VOP_READ(vp, &uio, fp->f_flag, fp->f_cred, NULL);
12577c478bdstevel@tonic-gate	VOP_RWUNLOCK(vp, flg, NULL);
12582c9e429brutus	*ret = error;
12597c478bdstevel@tonic-gate	if (error) {
12607c478bdstevel@tonic-gate		kmem_free(data, size);
12617c478bdstevel@tonic-gate		return (NULL);
12627c478bdstevel@tonic-gate	}
12637c478bdstevel@tonic-gate	*len = size;
12647c478bdstevel@tonic-gate	*off += size;
12657c478bdstevel@tonic-gate	return (data);
12667c478bdstevel@tonic-gate}
12677c478bdstevel@tonic-gate
12687c478bdstevel@tonic-gate/*
12692c9e429brutus * Called to sink application response sendfilev, as with nl7c_data() above
12702c9e429brutus * all the data will be processed by NL7C unless there's an error.
12717c478bdstevel@tonic-gate */
12727c478bdstevel@tonic-gate
12732c9e429brutusint
12742c9e429brutusnl7c_sendfilev(struct sonode *so, u_offset_t *fileoff, sendfilevec_t *sfvp,
1275e9f74eaToomas Soome    int sfvc, ssize_t *xfer)
12767c478bdstevel@tonic-gate{
12770f1702cYu Xiangning<Eric.Yu@Sun.COM>	sotpi_info_t	*sti = SOTOTPI(so);
12780f1702cYu Xiangning<Eric.Yu@Sun.COM>	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
12797c478bdstevel@tonic-gate	file_t		*fp = NULL;
12807c478bdstevel@tonic-gate	vnode_t		*vp = NULL;
12817c478bdstevel@tonic-gate	char		*data = NULL;
12822c9e429brutus	u_offset_t	off;
12837c478bdstevel@tonic-gate	int		len;
12842c9e429brutus	int		cnt;
12857c478bdstevel@tonic-gate	int		total_count = 0;
12862c9e429brutus	char		*alloc;
12877c478bdstevel@tonic-gate	uri_rd_t	*rdp;
12882c9e429brutus	int		max;
12892c9e429brutus	int		perror;
12907c478bdstevel@tonic-gate	int		error = 0;
12912c9e429brutus	boolean_t	first = B_TRUE;
12927c478bdstevel@tonic-gate
12937c478bdstevel@tonic-gate	nl7c_uri_sendfilev++;
12947c478bdstevel@tonic-gate
12957c478bdstevel@tonic-gate	if (uri == NULL) {
12967c478bdstevel@tonic-gate		/* Socket & NL7C out of sync, disable NL7C */
12970f1702cYu Xiangning<Eric.Yu@Sun.COM>		sti->sti_nl7c_flags = 0;
12987c478bdstevel@tonic-gate		nl7c_uri_NULL2++;
12992c9e429brutus		return (0);
13007c478bdstevel@tonic-gate	}
13017c478bdstevel@tonic-gate
13020f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (sti->sti_nl7c_flags & NL7C_WAITWRITE)
13030f1702cYu Xiangning<Eric.Yu@Sun.COM>		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
13047c478bdstevel@tonic-gate
13057c478bdstevel@tonic-gate	while (sfvc-- > 0) {
13067c478bdstevel@tonic-gate		/*
13077c478bdstevel@tonic-gate		 * off - the current sfv read file offset or user address.
13087c478bdstevel@tonic-gate		 *
13092c9e429brutus		 * len - the current sfv length in bytes.
13102c9e429brutus		 *
13112c9e429brutus		 * cnt - number of bytes kmem_alloc()ed.
13127c478bdstevel@tonic-gate		 *
13132c9e429brutus		 * alloc - the kmem_alloc()ed buffer of size "cnt".
13147c478bdstevel@tonic-gate		 *
13152c9e429brutus		 * data - copy of "alloc" used for post alloc references.
13167c478bdstevel@tonic-gate		 *
13177c478bdstevel@tonic-gate		 * fp - the current sfv file_t pointer.
13187c478bdstevel@tonic-gate		 *
13197c478bdstevel@tonic-gate		 * vp - the current "*vp" vnode_t pointer.
13207c478bdstevel@tonic-gate		 *
13217c478bdstevel@tonic-gate		 * Note, for "data" and "fp" and "vp" a NULL value is used
13227c478bdstevel@tonic-gate		 * when not allocated such that the common failure path "fail"
13237c478bdstevel@tonic-gate		 * is used.
13247c478bdstevel@tonic-gate		 */
13257c478bdstevel@tonic-gate		off = sfvp->sfv_off;
13267c478bdstevel@tonic-gate		len = sfvp->sfv_len;
13272c9e429brutus		cnt = len;
1328e116a42Prakash Jalan
1329e116a42Prakash Jalan		if (len == 0) {
1330e116a42Prakash Jalan			sfvp++;
1331e116a42Prakash Jalan			continue;
1332e116a42Prakash Jalan		}
1333e116a42Prakash Jalan
13342c9e429brutus		if (sfvp->sfv_fd == SFV_FD_SELF) {
13357c478bdstevel@tonic-gate			/*
13362c9e429brutus			 * User memory, copyin() all the bytes.
13377c478bdstevel@tonic-gate			 */
13382c9e429brutus			alloc = kmem_alloc(cnt, KM_SLEEP);
13392c9e429brutus			error = xcopyin((caddr_t)(uintptr_t)off, alloc, cnt);
13407c478bdstevel@tonic-gate			if (error)
13417c478bdstevel@tonic-gate				goto fail;
13427c478bdstevel@tonic-gate		} else {
13437c478bdstevel@tonic-gate			/*
13442c9e429brutus			 * File descriptor, prefetch some bytes.
13457c478bdstevel@tonic-gate			 */
13462c9e429brutus			if ((fp = getf(sfvp->sfv_fd)) == NULL) {
13472c9e429brutus				error = EBADF;
13487c478bdstevel@tonic-gate				goto fail;
13492c9e429brutus			}
13502c9e429brutus			if ((fp->f_flag & FREAD) == 0) {
13512c9e429brutus				error = EACCES;
13527c478bdstevel@tonic-gate				goto fail;
13532c9e429brutus			}
13547c478bdstevel@tonic-gate			vp = fp->f_vnode;
13552c9e429brutus			if (vp->v_type != VREG) {
13562c9e429brutus				error = EINVAL;
13577c478bdstevel@tonic-gate				goto fail;
13582c9e429brutus			}
13597c478bdstevel@tonic-gate			VN_HOLD(vp);
13607c478bdstevel@tonic-gate
13617c478bdstevel@tonic-gate			/* Read max_rem bytes from file for prefetch */
13627c478bdstevel@tonic-gate			if (nl7c_use_kmem) {
13632c9e429brutus				max = cnt;
13647c478bdstevel@tonic-gate			} else {
13652c9e429brutus				max = MAXBSIZE * nl7c_file_prefetch;
13667c478bdstevel@tonic-gate			}
13672c9e429brutus			alloc = nl7c_readfile(fp, &off, &cnt, max, &error);
13682c9e429brutus			if (alloc == NULL)
13697c478bdstevel@tonic-gate				goto fail;
13707c478bdstevel@tonic-gate
13717c478bdstevel@tonic-gate			releasef(sfvp->sfv_fd);
13727c478bdstevel@tonic-gate			fp = NULL;
13732c9e429brutus		}
13742c9e429brutus		URI_RD_ADD(uri, rdp, cnt, -1);
13752c9e429brutus		data = alloc;
13762c9e429brutus		alloc = NULL;
13772c9e429brutus		rdp->data.kmem = data;
13782c9e429brutus		total_count += cnt;
13792c9e429brutus		if (uri->hash != URI_TEMP && total_count > nca_max_cache_size) {
13802c9e429brutus			uri_delete(uri);
13812c9e429brutus			uri->hash = URI_TEMP;
13822c9e429brutus		}
13837c478bdstevel@tonic-gate
13842c9e429brutus		/* Response parse */
13852c9e429brutus		perror = nl7c_resp_parse(so, uri, data, len);
13867c478bdstevel@tonic-gate
13872c9e429brutus		/* Send kmem data out the connection */
13882c9e429brutus		error = uri_rd_response(so, uri, rdp, first);
13892c9e429brutus
13902c9e429brutus		if (error)
13912c9e429brutus			goto fail;
13922c9e429brutus
13932c9e429brutus		if (sfvp->sfv_fd != SFV_FD_SELF) {
13942c9e429brutus			/*
13952c9e429brutus			 * File descriptor, if any bytes left save vnode_t.
13962c9e429brutus			 */
13972c9e429brutus			if (len > cnt) {
13982c9e429brutus				/* More file data so add it */
13992c9e429brutus				URI_RD_ADD(uri, rdp, len - cnt, off);
14002c9e429brutus				rdp->data.vnode = vp;
14012c9e429brutus
14022c9e429brutus				/* Send vnode data out the connection */
14032c9e429brutus				error = uri_rd_response(so, uri, rdp, first);
14042c9e429brutus			} else {
14052c9e429brutus				/* All file data fit in the prefetch */
14062c9e429brutus				VN_RELE(vp);
14077c478bdstevel@tonic-gate			}
14082c9e429brutus			*fileoff += len;
14092c9e429brutus			vp = NULL;
14107c478bdstevel@tonic-gate		}
14112c9e429brutus		*xfer += len;
14127c478bdstevel@tonic-gate		sfvp++;
14132c9e429brutus
14142c9e429brutus		if (first)
14152c9e429brutus			first = B_FALSE;
14167c478bdstevel@tonic-gate	}
14177c478bdstevel@tonic-gate	if (total_count > 0) {
14187c478bdstevel@tonic-gate		atomic_add_64(&nl7c_uri_bytes, total_count);
14197c478bdstevel@tonic-gate	}
14202c9e429brutus	if (perror == 0 &&
14212c9e429brutus	    ((uri->respclen == URI_LEN_NOVALUE &&
14222c9e429brutus	    uri->resplen == URI_LEN_NOVALUE) ||
14232c9e429brutus	    uri->count >= uri->resplen)) {
14242c9e429brutus		/*
14252c9e429brutus		 * No more data needed and no pending response
14262c9e429brutus		 * data or current data count >= response length
14272c9e429brutus		 * so close the URI processing for this so.
14282c9e429brutus		 */
14297c478bdstevel@tonic-gate		nl7c_close(so);
14300f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (! (sti->sti_nl7c_flags & NL7C_SOPERSIST)) {
14312c9e429brutus			/* Not a persistent connection */
14320f1702cYu Xiangning<Eric.Yu@Sun.COM>			sti->sti_nl7c_flags = 0;
14332c9e429brutus		}
14347c478bdstevel@tonic-gate	}
14352c9e429brutus
14362c9e429brutus	return (0);
14377c478bdstevel@tonic-gate
14387c478bdstevel@tonic-gatefail:
14390f1702cYu Xiangning<Eric.Yu@Sun.COM>	if (error == EPIPE)
14400f1702cYu Xiangning<Eric.Yu@Sun.COM>		tsignal(curthread, SIGPIPE);
14410f1702cYu Xiangning<Eric.Yu@Sun.COM>
14422c9e429brutus	if (alloc != NULL)
14437c478bdstevel@tonic-gate		kmem_free(data, len);
14447c478bdstevel@tonic-gate
14457c478bdstevel@tonic-gate	if (vp != NULL)
14467c478bdstevel@tonic-gate		VN_RELE(vp);
14477c478bdstevel@tonic-gate
14487c478bdstevel@tonic-gate	if (fp != NULL)
14497c478bdstevel@tonic-gate		releasef(sfvp->sfv_fd);
14507c478bdstevel@tonic-gate
14517c478bdstevel@tonic-gate	if (total_count > 0) {
14527c478bdstevel@tonic-gate		atomic_add_64(&nl7c_uri_bytes, total_count);
14537c478bdstevel@tonic-gate	}
14542c9e429brutus
14550f1702cYu Xiangning<Eric.Yu@Sun.COM>	sti->sti_nl7c_flags = 0;
14567c478bdstevel@tonic-gate	nl7c_urifree(so);
14572c9e429brutus
14582c9e429brutus	return (error);
14597c478bdstevel@tonic-gate}
14607c478bdstevel@tonic-gate
14617c478bdstevel@tonic-gate/*
14627c478bdstevel@tonic-gate * Called for a socket which is closing or when an application has
14637c478bdstevel@tonic-gate * completed sending all the response data (i.e. for a persistent
14647c478bdstevel@tonic-gate * connection called once for each completed application response).
14657c478bdstevel@tonic-gate */
14667c478bdstevel@tonic-gate
14677c478bdstevel@tonic-gatevoid
14687c478bdstevel@tonic-gatenl7c_close(struct sonode *so)
14697c478bdstevel@tonic-gate{
14700f1702cYu Xiangning<Eric.Yu@Sun.COM>	sotpi_info_t	*sti = SOTOTPI(so);
1471e9f74eaToomas Soome	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
14727c478bdstevel@tonic-gate
14737c478bdstevel@tonic-gate	if (uri == NULL) {
14747c478bdstevel@tonic-gate		/*
14757c478bdstevel@tonic-gate		 * No URI being processed so might be a listen()er
14767c478bdstevel@tonic-gate		 * if so do any cleanup, else nothing more to do.
14777c478bdstevel@tonic-gate		 */
14787c478bdstevel@tonic-gate		if (so->so_state & SS_ACCEPTCONN) {
14797c478bdstevel@tonic-gate			(void) nl7c_close_addr(so);
14807c478bdstevel@tonic-gate		}
14817c478bdstevel@tonic-gate		return;
14827c478bdstevel@tonic-gate	}
14830f1702cYu Xiangning<Eric.Yu@Sun.COM>	sti->sti_nl7c_uri = NULL;
14847c478bdstevel@tonic-gate	if (uri->hash != URI_TEMP) {
14857c478bdstevel@tonic-gate		mutex_enter(&uri->proclock);
14867c478bdstevel@tonic-gate		uri->proc = NULL;
14877c478bdstevel@tonic-gate		if (CV_HAS_WAITERS(&uri->waiting)) {
14887c478bdstevel@tonic-gate			cv_broadcast(&uri->waiting);
14897c478bdstevel@tonic-gate		}
14907c478bdstevel@tonic-gate		mutex_exit(&uri->proclock);
14917c478bdstevel@tonic-gate		nl7c_uri_close++;
14927c478bdstevel@tonic-gate	} else {
14937c478bdstevel@tonic-gate		/* No proclock as uri exclusively owned by so */
14947c478bdstevel@tonic-gate		uri->proc = NULL;
14957c478bdstevel@tonic-gate		nl7c_uri_temp_close++;
14967c478bdstevel@tonic-gate	}
14977c478bdstevel@tonic-gate	REF_RELE(uri);
14987c478bdstevel@tonic-gate	if (nl7c_uri_max > 0 && nl7c_uri_bytes > nl7c_uri_max) {
14997c478bdstevel@tonic-gate		nl7c_uri_reclaim();
15007c478bdstevel@tonic-gate	}
15017c478bdstevel@tonic-gate}
15027c478bdstevel@tonic-gate
15037c478bdstevel@tonic-gate/*
15047c478bdstevel@tonic-gate * The uri_segmap_t ref_t inactive function called on the last REF_RELE(),
15057c478bdstevel@tonic-gate * release the segmap mapping. Note, the uri_segmap_t will be freed by
15067c478bdstevel@tonic-gate * REF_RELE() on return.
15077c478bdstevel@tonic-gate */
15087c478bdstevel@tonic-gate
15097c478bdstevel@tonic-gatevoid
15107c478bdstevel@tonic-gateuri_segmap_inactive(uri_segmap_t *smp)
15117c478bdstevel@tonic-gate{
15127c478bdstevel@tonic-gate	if (!segmap_kpm) {
15137c478bdstevel@tonic-gate		(void) segmap_fault(kas.a_hat, segkmap, smp->base,
15147c478bdstevel@tonic-gate		    smp->len, F_SOFTUNLOCK, S_OTHER);
15157c478bdstevel@tonic-gate	}
15167c478bdstevel@tonic-gate	(void) segmap_release(segkmap, smp->base, SM_DONTNEED);
15177c478bdstevel@tonic-gate	VN_RELE(smp->vp);
15187c478bdstevel@tonic-gate}
15197c478bdstevel@tonic-gate
15207c478bdstevel@tonic-gate/*
15217c478bdstevel@tonic-gate * The call-back for desballoc()ed mblk_t's, if a segmap mapped mblk_t
15222c9e429brutus * release the reference, one per desballoc() of a segmap page, if a rd_t
15232c9e429brutus * mapped mblk_t release the reference, one per desballoc() of a uri_desc_t,
15242c9e429brutus * last kmem free the uri_desb_t.
15257c478bdstevel@tonic-gate */
15267c478bdstevel@tonic-gate
15277c478bdstevel@tonic-gatestatic void
15287c478bdstevel@tonic-gateuri_desb_free(uri_desb_t *desb)
15297c478bdstevel@tonic-gate{
15307c478bdstevel@tonic-gate	if (desb->segmap != NULL) {
15317c478bdstevel@tonic-gate		REF_RELE(desb->segmap);
15327c478bdstevel@tonic-gate	}
15337c478bdstevel@tonic-gate	REF_RELE(desb->uri);
15347c478bdstevel@tonic-gate	kmem_cache_free(uri_desb_kmc, desb);
15357c478bdstevel@tonic-gate}
15367c478bdstevel@tonic-gate
15377c478bdstevel@tonic-gate/*
15387c478bdstevel@tonic-gate * Segmap map up to a page of a uri_rd_t file descriptor.
15397c478bdstevel@tonic-gate */
15407c478bdstevel@tonic-gate
15417c478bdstevel@tonic-gateuri_segmap_t *
15427c478bdstevel@tonic-gateuri_segmap_map(uri_rd_t *rdp, int bytes)
15437c478bdstevel@tonic-gate{
15447c478bdstevel@tonic-gate	uri_segmap_t	*segmap = kmem_cache_alloc(uri_segmap_kmc, KM_SLEEP);
15457c478bdstevel@tonic-gate	int		len = MIN(rdp->sz, MAXBSIZE);
15467c478bdstevel@tonic-gate
15477c478bdstevel@tonic-gate	if (len > bytes)
15487c478bdstevel@tonic-gate		len = bytes;
15497c478bdstevel@tonic-gate
15507c478bdstevel@tonic-gate	REF_INIT(segmap, 1, uri_segmap_inactive, uri_segmap_kmc);
15517c478bdstevel@tonic-gate	segmap->len = len;
15527c478bdstevel@tonic-gate	VN_HOLD(rdp->data.vnode);
15537c478bdstevel@tonic-gate	segmap->vp = rdp->data.vnode;
15547c478bdstevel@tonic-gate
15557c478bdstevel@tonic-gate	segmap->base = segmap_getmapflt(segkmap, segmap->vp, rdp->off, len,
15567c478bdstevel@tonic-gate	    segmap_kpm ? SM_FAULT : 0, S_READ);
15577c478bdstevel@tonic-gate
15587c478bdstevel@tonic-gate	if (segmap_fault(kas.a_hat, segkmap, segmap->base, len,
15597c478bdstevel@tonic-gate	    F_SOFTLOCK, S_READ) != 0) {
15607c478bdstevel@tonic-gate		REF_RELE(segmap);
15617c478bdstevel@tonic-gate		return (NULL);
15627c478bdstevel@tonic-gate	}
15637c478bdstevel@tonic-gate	return (segmap);
15647c478bdstevel@tonic-gate}
15657c478bdstevel@tonic-gate
15667c478bdstevel@tonic-gate/*
15677c478bdstevel@tonic-gate * Chop up the kernel virtual memory area *data of size *sz bytes for
15687c478bdstevel@tonic-gate * a maximum of *bytes bytes into an besballoc()ed mblk_t chain using
15697c478bdstevel@tonic-gate * the given template uri_desb_t *temp of max_mblk bytes per.
15707c478bdstevel@tonic-gate *
15717c478bdstevel@tonic-gate * The values of *data, *sz, and *bytes are updated on return, the
15727c478bdstevel@tonic-gate * mblk_t chain is returned.
15737c478bdstevel@tonic-gate */
15747c478bdstevel@tonic-gate
15757c478bdstevel@tonic-gatestatic mblk_t *
1576e9f74eaToomas Soomeuri_desb_chop(char **data, size_t *sz, int *bytes, uri_desb_t *temp,
1577e9f74eaToomas Soome    int max_mblk, char *eoh, mblk_t *persist)
15787c478bdstevel@tonic-gate{
15797c478bdstevel@tonic-gate	char		*ldata = *data;
15807c478bdstevel@tonic-gate	size_t		lsz = *sz;
15817c478bdstevel@tonic-gate	int		lbytes = bytes ? *bytes : lsz;
15827c478bdstevel@tonic-gate	uri_desb_t	*desb;
15837c478bdstevel@tonic-gate	mblk_t		*mp = NULL;
15842c9e429brutus	mblk_t		*nmp, *pmp = NULL;
15857c478bdstevel@tonic-gate	int		msz;
15867c478bdstevel@tonic-gate
15877c478bdstevel@tonic-gate	if (lbytes == 0 && lsz == 0)
15887c478bdstevel@tonic-gate		return (NULL);
15897c478bdstevel@tonic-gate
15907c478bdstevel@tonic-gate	while (lbytes > 0 && lsz > 0) {
15917c478bdstevel@tonic-gate		msz = MIN(lbytes, max_mblk);
15927c478bdstevel@tonic-gate		msz = MIN(msz, lsz);
15937c478bdstevel@tonic-gate		if (persist && eoh >= ldata && eoh < &ldata[msz]) {
15947c478bdstevel@tonic-gate			msz = (eoh - ldata);
15957c478bdstevel@tonic-gate			pmp = persist;
15967c478bdstevel@tonic-gate			persist = NULL;
15972c9e429brutus			if (msz == 0) {
15982c9e429brutus				nmp = pmp;
15992c9e429brutus				pmp = NULL;
16002c9e429brutus				goto zero;
16012c9e429brutus			}
16027c478bdstevel@tonic-gate		}
16037c478bdstevel@tonic-gate		desb = kmem_cache_alloc(uri_desb_kmc, KM_SLEEP);
16047c478bdstevel@tonic-gate		REF_HOLD(temp->uri);
16057c478bdstevel@tonic-gate		if (temp->segmap) {
16067c478bdstevel@tonic-gate			REF_HOLD(temp->segmap);
16077c478bdstevel@tonic-gate		}
16087c478bdstevel@tonic-gate		bcopy(temp, desb, sizeof (*desb));
16097c478bdstevel@tonic-gate		desb->frtn.free_arg = (caddr_t)desb;
16107c478bdstevel@tonic-gate		nmp = desballoc((uchar_t *)ldata, msz, BPRI_HI, &desb->frtn);
16117c478bdstevel@tonic-gate		if (nmp == NULL) {
16127c478bdstevel@tonic-gate			if (temp->segmap) {
16137c478bdstevel@tonic-gate				REF_RELE(temp->segmap);
16147c478bdstevel@tonic-gate			}
16157c478bdstevel@tonic-gate			REF_RELE(temp->uri);
16167c478bdstevel@tonic-gate			if (mp != NULL) {
16172c9e429brutus				mp->b_next = NULL;
16187c478bdstevel@tonic-gate				freemsg(mp);
16197c478bdstevel@tonic-gate			}
16202c9e429brutus			if (persist != NULL) {
16212c9e429brutus				freeb(persist);
16222c9e429brutus			}
16237c478bdstevel@tonic-gate			return (NULL);
16247c478bdstevel@tonic-gate		}
16257c478bdstevel@tonic-gate		nmp->b_wptr += msz;
16262c9e429brutus	zero:
16277c478bdstevel@tonic-gate		if (mp != NULL) {
16282c9e429brutus			mp->b_next->b_cont = nmp;
16297c478bdstevel@tonic-gate		} else {
16307c478bdstevel@tonic-gate			mp = nmp;
16317c478bdstevel@tonic-gate		}
16322c9e429brutus		if (pmp != NULL) {
16332c9e429brutus			nmp->b_cont = pmp;
16342c9e429brutus			nmp = pmp;
16352c9e429brutus			pmp = NULL;
16362c9e429brutus		}
16372c9e429brutus		mp->b_next = nmp;
16387c478bdstevel@tonic-gate		ldata += msz;
16397c478bdstevel@tonic-gate		lsz -= msz;
16407c478bdstevel@tonic-gate		lbytes -= msz;
16417c478bdstevel@tonic-gate	}
16427c478bdstevel@tonic-gate	*data = ldata;
16437c478bdstevel@tonic-gate	*sz = lsz;
16447c478bdstevel@tonic-gate	if (bytes)
16457c478bdstevel@tonic-gate		*bytes = lbytes;
16467c478bdstevel@tonic-gate	return (mp);
16477c478bdstevel@tonic-gate}
16487c478bdstevel@tonic-gate
16497c478bdstevel@tonic-gate/*
16507c478bdstevel@tonic-gate * Experimential noqwait (i.e. no canput()/qwait() checks), just send
16517c478bdstevel@tonic-gate * the entire mblk_t chain down without flow-control checks.
16527c478bdstevel@tonic-gate */
16537c478bdstevel@tonic-gate
16547c478bdstevel@tonic-gatestatic int
16557c478bdstevel@tonic-gatekstrwritempnoqwait(struct vnode *vp, mblk_t *mp)
16567c478bdstevel@tonic-gate{
16577c478bdstevel@tonic-gate	struct stdata *stp;
16587c478bdstevel@tonic-gate	int error = 0;
16597c478bdstevel@tonic-gate
16607c478bdstevel@tonic-gate	ASSERT(vp->v_stream);
16617c478bdstevel@tonic-gate	stp = vp->v_stream;
16627c478bdstevel@tonic-gate
16637c478bdstevel@tonic-gate	/* Fast check of flags before acquiring the lock */
16647c478bdstevel@tonic-gate	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
16657c478bdstevel@tonic-gate		mutex_enter(&stp->sd_lock);
16667c478bdstevel@tonic-gate		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
16677c478bdstevel@tonic-gate		mutex_exit(&stp->sd_lock);
16687c478bdstevel@tonic-gate		if (error != 0) {
16697c478bdstevel@tonic-gate			if (!(stp->sd_flag & STPLEX) &&
16707c478bdstevel@tonic-gate			    (stp->sd_wput_opt & SW_SIGPIPE)) {
16717c478bdstevel@tonic-gate				error = EPIPE;
16727c478bdstevel@tonic-gate			}
16737c478bdstevel@tonic-gate			return (error);
16747c478bdstevel@tonic-gate		}
16757c478bdstevel@tonic-gate	}
16767c478bdstevel@tonic-gate	putnext(stp->sd_wrq, mp);
16777c478bdstevel@tonic-gate	return (0);
16787c478bdstevel@tonic-gate}
16797c478bdstevel@tonic-gate
16807c478bdstevel@tonic-gate/*
16812c9e429brutus * Send the URI uri_desc_t *uri response uri_rd_t *rdp out the socket_t *so.
16827c478bdstevel@tonic-gate */
16837c478bdstevel@tonic-gate
16842c9e429brutusstatic int
16852c9e429brutusuri_rd_response(struct sonode *so,
16862c9e429brutus    uri_desc_t *uri,
16872c9e429brutus    uri_rd_t *rdp,
16882c9e429brutus    boolean_t first)
16897c478bdstevel@tonic-gate{
16907c478bdstevel@tonic-gate	vnode_t		*vp = SOTOV(so);
16910f1702cYu Xiangning<Eric.Yu@Sun.COM>	int		max_mblk = (int)vp->v_stream->sd_maxblk;
16927c478bdstevel@tonic-gate	int		wsz;
16937c478bdstevel@tonic-gate	mblk_t		*mp, *wmp, *persist;
16947c478bdstevel@tonic-gate	int		write_bytes;
16952c9e429brutus	uri_rd_t	rd;
16967c478bdstevel@tonic-gate	uri_desb_t	desb;
16977c478bdstevel@tonic-gate	uri_segmap_t	*segmap = NULL;
16987c478bdstevel@tonic-gate	char		*segmap_data;
16997c478bdstevel@tonic-gate	size_t		segmap_sz;
17002c9e429brutus	int		error;
17012c9e429brutus	int		fflg = ((so->so_state & SS_NDELAY) ? FNDELAY : 0) |
1702e116a42Prakash Jalan	    ((so->so_state & SS_NONBLOCK) ? FNONBLOCK : 0);
17037c478bdstevel@tonic-gate
17047c478bdstevel@tonic-gate
17057c478bdstevel@tonic-gate	/* Initialize template uri_desb_t */
17067c478bdstevel@tonic-gate	desb.frtn.free_func = uri_desb_free;
17077c478bdstevel@tonic-gate	desb.frtn.free_arg = NULL;
17087c478bdstevel@tonic-gate	desb.uri = uri;
17097c478bdstevel@tonic-gate
17102c9e429brutus	/* Get a local copy of the rd_t */
17112c9e429brutus	bcopy(rdp, &rd, sizeof (rd));
17127c478bdstevel@tonic-gate	do {
17132c9e429brutus		if (first) {
17142c9e429brutus			/*
17152c9e429brutus			 * For first kstrwrite() enough data to get
17162c9e429brutus			 * things going, note non blocking version of
17172c9e429brutus			 * kstrwrite() will be used below.
17182c9e429brutus			 */
17192c9e429brutus			write_bytes = P2ROUNDUP((max_mblk * 4),
17202c9e429brutus			    MAXBSIZE * nl7c_file_prefetch);
17212c9e429brutus		} else {
17222c9e429brutus			if ((write_bytes = so->so_sndbuf) == 0)
17232c9e429brutus				write_bytes = vp->v_stream->sd_qn_maxpsz;
17242c9e429brutus			ASSERT(write_bytes > 0);
17252c9e429brutus			write_bytes = P2ROUNDUP(write_bytes, MAXBSIZE);
17262c9e429brutus		}
17272c9e429brutus		/*
17282c9e429brutus		 * Chop up to a write_bytes worth of data.
17292c9e429brutus		 */
17307c478bdstevel@tonic-gate		wmp = NULL;
17317c478bdstevel@tonic-gate		wsz = write_bytes;
17327c478bdstevel@tonic-gate		do {
17332c9e429brutus			if (rd.sz == 0)
17342c9e429brutus				break;
17357c478bdstevel@tonic-gate			if (rd.off == -1) {
17367c478bdstevel@tonic-gate				if (uri->eoh >= rd.data.kmem &&
17377c478bdstevel@tonic-gate				    uri->eoh < &rd.data.kmem[rd.sz]) {
17387c478bdstevel@tonic-gate					persist = nl7c_http_persist(so);
17397c478bdstevel@tonic-gate				} else {
17407c478bdstevel@tonic-gate					persist = NULL;
17417c478bdstevel@tonic-gate				}
17427c478bdstevel@tonic-gate				desb.segmap = NULL;
17437c478bdstevel@tonic-gate				mp = uri_desb_chop(&rd.data.kmem, &rd.sz,
17447c478bdstevel@tonic-gate				    &wsz, &desb, max_mblk, uri->eoh, persist);
17452c9e429brutus				if (mp == NULL) {
17462c9e429brutus					error = ENOMEM;
17477c478bdstevel@tonic-gate					goto invalidate;
17482c9e429brutus				}
17497c478bdstevel@tonic-gate			} else {
17507c478bdstevel@tonic-gate				if (segmap == NULL) {
17517c478bdstevel@tonic-gate					segmap = uri_segmap_map(&rd,
17527c478bdstevel@tonic-gate					    write_bytes);
17532c9e429brutus					if (segmap == NULL) {
17542c9e429brutus						error = ENOMEM;
17557c478bdstevel@tonic-gate						goto invalidate;
17562c9e429brutus					}
17577c478bdstevel@tonic-gate					desb.segmap = segmap;
17587c478bdstevel@tonic-gate					segmap_data = segmap->base;
17597c478bdstevel@tonic-gate					segmap_sz = segmap->len;
17607c478bdstevel@tonic-gate				}
17617c478bdstevel@tonic-gate				mp = uri_desb_chop(&segmap_data, &segmap_sz,
17627c478bdstevel@tonic-gate				    &wsz, &desb, max_mblk, NULL, NULL);
17632c9e429brutus				if (mp == NULL) {
17642c9e429brutus					error = ENOMEM;
17657c478bdstevel@tonic-gate					goto invalidate;
17662c9e429brutus				}
17677c478bdstevel@tonic-gate				if (segmap_sz == 0) {
17687c478bdstevel@tonic-gate					rd.sz -= segmap->len;
17697c478bdstevel@tonic-gate					rd.off += segmap->len;
17707c478bdstevel@tonic-gate					REF_RELE(segmap);
17717c478bdstevel@tonic-gate					segmap = NULL;
17727c478bdstevel@tonic-gate				}
17737c478bdstevel@tonic-gate			}
17747c478bdstevel@tonic-gate			if (wmp == NULL) {
17757c478bdstevel@tonic-gate				wmp = mp;
17767c478bdstevel@tonic-gate			} else {
17777c478bdstevel@tonic-gate				wmp->b_next->b_cont = mp;
17787c478bdstevel@tonic-gate				wmp->b_next = mp->b_next;
17797c478bdstevel@tonic-gate				mp->b_next = NULL;
17807c478bdstevel@tonic-gate			}
17812c9e429brutus		} while (wsz > 0 && rd.sz > 0);
17827c478bdstevel@tonic-gate
17837c478bdstevel@tonic-gate		wmp->b_next = NULL;
17847c478bdstevel@tonic-gate		if (first) {
17857c478bdstevel@tonic-gate			/* First kstrwrite(), use noqwait */
17862c9e429brutus			if ((error = kstrwritempnoqwait(vp, wmp)) != 0)
17877c478bdstevel@tonic-gate				goto invalidate;
17887c478bdstevel@tonic-gate			/*
17897c478bdstevel@tonic-gate			 * For the rest of the kstrwrite()s use SO_SNDBUF
17907c478bdstevel@tonic-gate			 * worth of data at a time, note these kstrwrite()s
17917c478bdstevel@tonic-gate			 * may (will) block one or more times.
17927c478bdstevel@tonic-gate			 */
17937c478bdstevel@tonic-gate			first = B_FALSE;
17947c478bdstevel@tonic-gate		} else {
17952c9e429brutus			if ((error = kstrwritemp(vp, wmp, fflg)) != 0) {
17962c9e429brutus				if (error == EAGAIN) {
17972c9e429brutus					nl7c_uri_rd_EAGAIN++;
17982c9e429brutus					if ((error =
17992c9e429brutus					    kstrwritempnoqwait(vp, wmp)) != 0)
18002c9e429brutus						goto invalidate;
18012c9e429brutus				} else
18022c9e429brutus					goto invalidate;
18032c9e429brutus			}
18047c478bdstevel@tonic-gate		}
18052c9e429brutus	} while (rd.sz > 0);
18067c478bdstevel@tonic-gate
18072c9e429brutus	return (0);
18087c478bdstevel@tonic-gate
18097c478bdstevel@tonic-gateinvalidate:
18107c478bdstevel@tonic-gate	if (segmap) {
18117c478bdstevel@tonic-gate		REF_RELE(segmap);
18127c478bdstevel@tonic-gate	}
18137c478bdstevel@tonic-gate	if (wmp)
18147c478bdstevel@tonic-gate		freemsg(wmp);
18152c9e429brutus
18162c9e429brutus	return (error);
18172c9e429brutus}
18182c9e429brutus
18192c9e429brutus/*
18202c9e429brutus * Send the URI uri_desc_t *uri response out the socket_t *so.
18212c9e429brutus */
18222c9e429brutus
18232c9e429brutusstatic int
18242c9e429brutusuri_response(struct sonode *so, uri_desc_t *uri)
18252c9e429brutus{
18262c9e429brutus	uri_rd_t	*rdp = &uri->response;
18272c9e429brutus	boolean_t	first = B_TRUE;
18282c9e429brutus	int		error;
18292c9e429brutus
18302c9e429brutus	while (rdp != NULL) {
18312c9e429brutus		error = uri_rd_response(so, uri, rdp, first);
18322c9e429brutus		if (error != 0) {
18332c9e429brutus			goto invalidate;
18342c9e429brutus		}
18352c9e429brutus		first = B_FALSE;
18362c9e429brutus		rdp = rdp->next;
18372c9e429brutus	}
18382c9e429brutus	return (0);
18392c9e429brutus
18402c9e429brutusinvalidate:
1841b73114cAnil udupa	if (uri->hash != URI_TEMP)
1842b73114cAnil udupa		uri_delete(uri);
18432c9e429brutus	return (error);
18447c478bdstevel@tonic-gate}
18457c478bdstevel@tonic-gate
18467c478bdstevel@tonic-gate/*
18477c478bdstevel@tonic-gate * The pchars[] array is indexed by a char to determine if it's a
18487c478bdstevel@tonic-gate * valid URI path component chararcter where:
18497c478bdstevel@tonic-gate *
18507c478bdstevel@tonic-gate *    pchar       = unreserved | escaped |
18517c478bdstevel@tonic-gate *                  ":" | "@" | "&" | "=" | "+" | "$" | ","
18527c478bdstevel@tonic-gate *
18537c478bdstevel@tonic-gate *    unreserved  = alphanum | mark
18547c478bdstevel@tonic-gate *
18557c478bdstevel@tonic-gate *    alphanum    = alpha | digit
18567c478bdstevel@tonic-gate *
18577c478bdstevel@tonic-gate *    alpha       = lowalpha | upalpha
18587c478bdstevel@tonic-gate *
18597c478bdstevel@tonic-gate *    lowalpha    = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
18607c478bdstevel@tonic-gate *                  "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
18617c478bdstevel@tonic-gate *                  "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
18627c478bdstevel@tonic-gate *                  "y" | "z"
18637c478bdstevel@tonic-gate *
18647c478bdstevel@tonic-gate *    upalpha     = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
18657c478bdstevel@tonic-gate *                  "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
18667c478bdstevel@tonic-gate *                  "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
18677c478bdstevel@tonic-gate *                  "Y" | "Z"
18687c478bdstevel@tonic-gate *
18697c478bdstevel@tonic-gate *    digit       = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
18707c478bdstevel@tonic-gate *                  "8" | "9"
18717c478bdstevel@tonic-gate *
18727c478bdstevel@tonic-gate *    mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
18737c478bdstevel@tonic-gate *
18747c478bdstevel@tonic-gate *    escaped     = "%" hex hex
18757c478bdstevel@tonic-gate *    hex         = digit | "A" | "B" | "C" | "D" | "E" | "F" |
18767c478bdstevel@tonic-gate *                  "a" | "b" | "c" | "d" | "e" | "f"
18777c478bdstevel@tonic-gate */
18787c478bdstevel@tonic-gate
18797c478bdstevel@tonic-gatestatic char pchars[] = {
18807c478bdstevel@tonic-gate    0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x07 */
18817c478bdstevel@tonic-gate    0, 0, 0, 0, 0, 0, 0, 0,	/* 0x08 - 0x0F */
18827c478bdstevel@tonic-gate    0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x17 */
18837c478bdstevel@tonic-gate    0, 0, 0, 0, 0, 0, 0, 0,	/* 0x18 - 0x1F */
18847c478bdstevel@tonic-gate    0, 1, 0, 0, 1, 1, 1, 1,	/* 0x20 - 0x27 */
18857c478bdstevel@tonic-gate    0, 0, 1, 1, 1, 1, 1, 1,	/* 0x28 - 0x2F */
18867c478bdstevel@tonic-gate    1, 1, 1, 1, 1, 1, 1, 1,	/* 0x30 - 0x37 */
18877c478bdstevel@tonic-gate    1, 1, 1, 0, 0, 1, 0, 0,	/* 0x38 - 0x3F */
18887c478bdstevel@tonic-gate    1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x47 */
18897c478bdstevel@tonic-gate    1, 1, 1, 1, 1, 1, 1, 1,	/* 0x48 - 0x4F */
18907c478bdstevel@tonic-gate    1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x57 */
18917c478bdstevel@tonic-gate    1, 1, 1, 0, 0, 0, 0, 1,	/* 0x58 - 0x5F */
18927c478bdstevel@tonic-gate    0, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x67 */
18937c478bdstevel@tonic-gate    1, 1, 1, 1, 1, 1, 1, 1,	/* 0x68 - 0x6F */
18947c478bdstevel@tonic-gate    1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x77 */
18957c478bdstevel@tonic-gate    1, 1, 1, 0, 0, 0, 1, 0	/* 0x78 - 0x7F */
18967c478bdstevel@tonic-gate};
18977c478bdstevel@tonic-gate
18987c478bdstevel@tonic-gate#define	PCHARS_MASK 0x7F
18997c478bdstevel@tonic-gate
19007c478bdstevel@tonic-gate/*
19017c478bdstevel@tonic-gate * This is the main L7 request message parse, we are called each time
19027c478bdstevel@tonic-gate * new data is availble for a socket, each time a single buffer of the
19037c478bdstevel@tonic-gate * entire message to date is given.
19047c478bdstevel@tonic-gate *
19057c478bdstevel@tonic-gate * Here we parse the request looking for the URI, parse it, and if a
19067c478bdstevel@tonic-gate * supported scheme call the scheme parser to commplete the parse of any
19077c478bdstevel@tonic-gate * headers which may further qualify the identity of the requested object
19087c478bdstevel@tonic-gate * then lookup it up in the URI hash.
19097c478bdstevel@tonic-gate *
19107c478bdstevel@tonic-gate * Return B_TRUE for more processing.
19117c478bdstevel@tonic-gate *
19127c478bdstevel@tonic-gate * Note, at this time the parser supports the generic message format as
19137c478bdstevel@tonic-gate * specified in RFC 822 with potentional limitations as specified in RFC
19147c478bdstevel@tonic-gate * 2616 for HTTP messages.
19157c478bdstevel@tonic-gate *
19167c478bdstevel@tonic-gate * Note, the caller supports an mblk_t chain, for now the parser(s)
19177c478bdstevel@tonic-gate * require the complete header in a single mblk_t. This is the common
19187c478bdstevel@tonic-gate * case and certainly for high performance environments, if at a future
19197c478bdstevel@tonic-gate * date mblk_t chains are important the parse can be reved to process
19207c478bdstevel@tonic-gate * mblk_t chains.
19217c478bdstevel@tonic-gate */
19227c478bdstevel@tonic-gate
19237c478bdstevel@tonic-gateboolean_t
19242c9e429brutusnl7c_parse(struct sonode *so, boolean_t nonblocking, boolean_t *ret)
19257c478bdstevel@tonic-gate{
19260f1702cYu Xiangning<Eric.Yu@Sun.COM>	sotpi_info_t *sti = SOTOTPI(so);
19270f1702cYu Xiangning<Eric.Yu@Sun.COM>	char	*cp = (char *)sti->sti_nl7c_rcv_mp->b_rptr;
19280f1702cYu Xiangning<Eric.Yu@Sun.COM>	char	*ep = (char *)sti->sti_nl7c_rcv_mp->b_wptr;
19297c478bdstevel@tonic-gate	char	*get = "GET ";
19302c9e429brutus	char	*post = "POST ";
19317c478bdstevel@tonic-gate	char	c;
19327c478bdstevel@tonic-gate	char	*uris;
19332c9e429brutus	uri_desc_t *uri = NULL;
19347c478bdstevel@tonic-gate	uri_desc_t *ruri = NULL;
19352c9e429brutus	mblk_t	*reqmp;
19362c9e429brutus	uint32_t hv = 0;
19377c478bdstevel@tonic-gate
19380f1702cYu Xiangning<Eric.Yu@Sun.COM>	if ((reqmp = dupb(sti->sti_nl7c_rcv_mp)) == NULL) {
19392c9e429brutus		nl7c_uri_pass_dupbfail++;
19402c9e429brutus		goto pass;
19412c9e429brutus	}
19427c478bdstevel@tonic-gate	/*
19437c478bdstevel@tonic-gate	 * Allocate and initialize minimumal state for the request
19447c478bdstevel@tonic-gate	 * uri_desc_t, in the cache hit case this uri_desc_t will
19457c478bdstevel@tonic-gate	 * be freed.
19467c478bdstevel@tonic-gate	 */
19472c9e429brutus	uri = kmem_cache_alloc(nl7c_uri_kmc, KM_SLEEP);
19482c9e429brutus	REF_INIT(uri, 1, nl7c_uri_inactive, nl7c_uri_kmc);
19497c478bdstevel@tonic-gate	uri->hash = NULL;
19507c478bdstevel@tonic-gate	uri->tail = NULL;
19517c478bdstevel@tonic-gate	uri->scheme = NULL;
19527c478bdstevel@tonic-gate	uri->count = 0;
19532c9e429brutus	uri->reqmp = reqmp;
19542c9e429brutus
19557c478bdstevel@tonic-gate	/*
19567c478bdstevel@tonic-gate	 * Set request time to current time.
19577c478bdstevel@tonic-gate	 */
19580f1702cYu Xiangning<Eric.Yu@Sun.COM>	sti->sti_nl7c_rtime = gethrestime_sec();
19592c9e429brutus
19607c478bdstevel@tonic-gate	/*
19617c478bdstevel@tonic-gate	 * Parse the Request-Line for the URI.
19627c478bdstevel@tonic-gate	 *
19637c478bdstevel@tonic-gate	 * For backwards HTTP version compatable reasons skip any leading
19647c478bdstevel@tonic-gate	 * CRLF (or CR or LF) line terminator(s) preceding Request-Line.
19657c478bdstevel@tonic-gate	 */
19667c478bdstevel@tonic-gate	while (cp < ep && (*cp == '\r' || *cp == '\n')) {
19677c478bdstevel@tonic-gate		cp++;
19687c478bdstevel@tonic-gate	}
19697c478bdstevel@tonic-gate	while (cp < ep && *get == *cp) {
19707c478bdstevel@tonic-gate		get++;
19717c478bdstevel@tonic-gate		cp++;
19727c478bdstevel@tonic-gate	}
19737c478bdstevel@tonic-gate	if (*get != 0) {
19742c9e429brutus		/* Note a "GET", check for "POST" */
19752c9e429brutus		while (cp < ep && *post == *cp) {
19762c9e429brutus			post++;
19772c9e429brutus			cp++;
19787c478bdstevel@tonic-gate		}
19792c9e429brutus		if (*post != 0) {
19802c9e429brutus			if (cp == ep) {
19812c9e429brutus				nl7c_uri_more_get++;
19822c9e429brutus				goto more;
19832c9e429brutus			}
19842c9e429brutus			/* Not a "GET" or a "POST", just pass */
19852c9e429brutus			nl7c_uri_pass_method++;
19862c9e429brutus			goto pass;
19872c9e429brutus		}
19882c9e429brutus		/* "POST", don't cache but still may want to parse */
19892c9e429brutus		uri->hash = URI_TEMP;
19907c478bdstevel@tonic-gate	}
19917c478bdstevel@tonic-gate	/*
19927c478bdstevel@tonic-gate	 * Skip over URI path char(s) and save start and past end pointers.
19937c478bdstevel@tonic-gate	 */
19947c478bdstevel@tonic-gate	uris = cp;
19957c478bdstevel@tonic-gate	while (cp < ep && (c = *cp) != ' ' && c != '\r') {
19967c478bdstevel@tonic-gate		if (c == '?') {
19977c478bdstevel@tonic-gate			/* Don't cache but still may want to parse */
19987c478bdstevel@tonic-gate			uri->hash = URI_TEMP;
19997c478bdstevel@tonic-gate		}
20002c9e429brutus		CHASH(hv, c);
20017c478bdstevel@tonic-gate		cp++;
20027c478bdstevel@tonic-gate	}
20037c478bdstevel@tonic-gate	if (c != '\r' && cp == ep) {
20047c478bdstevel@tonic-gate		nl7c_uri_more_eol++;
20057c478bdstevel@tonic-gate		goto more;
20067c478bdstevel@tonic-gate	}
20077c478bdstevel@tonic-gate	/*
20087c478bdstevel@tonic-gate	 * Request-Line URI parsed, pass the rest of the request on
20097c478bdstevel@tonic-gate	 * to the the http scheme parse.
20107c478bdstevel@tonic-gate	 */
20117c478bdstevel@tonic-gate	uri->path.cp = uris;
20127c478bdstevel@tonic-gate	uri->path.ep = cp;
20132c9e429brutus	uri->hvalue = hv;
20142c9e429brutus	if (! nl7c_http_request(&cp, ep, uri, so) || cp == NULL) {
20157c478bdstevel@tonic-gate		/*
20162c9e429brutus		 * Parse not successful or pass on request, the pointer
20172c9e429brutus		 * to the parse pointer "cp" is overloaded such that ! NULL
20182c9e429brutus		 * for more data and NULL for bad parse of request or pass.
20197c478bdstevel@tonic-gate		 */
20207c478bdstevel@tonic-gate		if (cp != NULL) {
20217c478bdstevel@tonic-gate			nl7c_uri_more_http++;
20227c478bdstevel@tonic-gate			goto more;
20237c478bdstevel@tonic-gate		}
20247c478bdstevel@tonic-gate		nl7c_uri_pass_http++;
20257c478bdstevel@tonic-gate		goto pass;
20267c478bdstevel@tonic-gate	}
20272c9e429brutus	if (uri->nocache) {
20282c9e429brutus		uri->hash = URI_TEMP;
20292c9e429brutus		(void) uri_lookup(uri, B_FALSE, nonblocking);
20302c9e429brutus	} else if (uri->hash == URI_TEMP) {
20312c9e429brutus		uri->nocache = B_TRUE;
20322c9e429brutus		(void) uri_lookup(uri, B_FALSE, nonblocking);
20332c9e429brutus	}
20342c9e429brutus
20357c478bdstevel@tonic-gate	if (uri->hash == URI_TEMP) {
20360f1702cYu Xiangning<Eric.Yu@Sun.COM>		if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
20377c478bdstevel@tonic-gate			/* Temporary URI so skip hash processing */
20387c478bdstevel@tonic-gate			nl7c_uri_request++;
20397c478bdstevel@tonic-gate			nl7c_uri_temp++;
20407c478bdstevel@tonic-gate			goto temp;
20417c478bdstevel@tonic-gate		}
20427c478bdstevel@tonic-gate		/* Not persistent so not interested in the response */
20437c478bdstevel@tonic-gate		nl7c_uri_pass_temp++;
20447c478bdstevel@tonic-gate		goto pass;
20457c478bdstevel@tonic-gate	}
20467c478bdstevel@tonic-gate	/*
20472c9e429brutus	 * Check the URI hash for a cached response, save the request
20482c9e429brutus	 * uri in case we need it below.
20497c478bdstevel@tonic-gate	 */
20502c9e429brutus	ruri = uri;
20517c478bdstevel@tonic-gate	if ((uri = uri_lookup(uri, B_TRUE, nonblocking)) == NULL) {
20527c478bdstevel@tonic-gate		/*
20537c478bdstevel@tonic-gate		 * Failed to lookup due to nonblocking wait required,
20547c478bdstevel@tonic-gate		 * interrupted cv_wait_sig(), KM_NOSLEEP memory alloc
20557c478bdstevel@tonic-gate		 * failure, ... Just pass on this request.
20567c478bdstevel@tonic-gate		 */
20577c478bdstevel@tonic-gate		nl7c_uri_pass_addfail++;
20587c478bdstevel@tonic-gate		goto pass;
20597c478bdstevel@tonic-gate	}
20607c478bdstevel@tonic-gate	nl7c_uri_request++;
20617c478bdstevel@tonic-gate	if (uri->response.sz > 0) {
20627c478bdstevel@tonic-gate		/*
20637c478bdstevel@tonic-gate		 * We have the response cached, update recv mblk rptr
20642c9e429brutus		 * to reflect the data consumed in parse.
20657c478bdstevel@tonic-gate		 */
20660f1702cYu Xiangning<Eric.Yu@Sun.COM>		mblk_t	*mp = sti->sti_nl7c_rcv_mp;
20677c478bdstevel@tonic-gate
20682c9e429brutus		if (cp == (char *)mp->b_wptr) {
20690f1702cYu Xiangning<Eric.Yu@Sun.COM>			sti->sti_nl7c_rcv_mp = mp->b_cont;
20702c9e429brutus			mp->b_cont = NULL;
20712c9e429brutus			freeb(mp);
20722c9e429brutus		} else {
20732c9e429brutus			mp->b_rptr = (unsigned char *)cp;
20742c9e429brutus		}
20752c9e429brutus		nl7c_uri_hit++;
20762c9e429brutus		/* If logging enabled log request */
20772c9e429brutus		if (nl7c_logd_enabled) {
20787c478bdstevel@tonic-gate			ipaddr_t faddr;
20797c478bdstevel@tonic-gate
20807c478bdstevel@tonic-gate			if (so->so_family == AF_INET) {
20817c478bdstevel@tonic-gate				/* Only support IPv4 addrs */
20827c478bdstevel@tonic-gate				faddr = ((struct sockaddr_in *)
20830f1702cYu Xiangning<Eric.Yu@Sun.COM>				    sti->sti_faddr_sa) ->sin_addr.s_addr;
20847c478bdstevel@tonic-gate			} else {
20857c478bd