Home | History | Annotate | Download | only in tcp
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #include <sys/types.h>
     27 #include <sys/stream.h>
     28 #include <sys/strsun.h>
     29 #include <sys/strsubr.h>
     30 #include <sys/debug.h>
     31 #include <sys/sdt.h>
     32 #include <sys/cmn_err.h>
     33 #include <sys/tihdr.h>
     34 
     35 #include <inet/common.h>
     36 #include <inet/optcom.h>
     37 #include <inet/ip.h>
     38 #include <inet/ip_impl.h>
     39 #include <inet/tcp.h>
     40 #include <inet/tcp_impl.h>
     41 #include <inet/ipsec_impl.h>
     42 #include <inet/ipclassifier.h>
     43 #include <inet/ipp_common.h>
     44 
     45 /*
     46  * This file implements TCP fusion - a protocol-less data path for TCP
     47  * loopback connections.  The fusion of two local TCP endpoints occurs
     48  * at connection establishment time.  Various conditions (see details
     49  * in tcp_fuse()) need to be met for fusion to be successful.  If it
     50  * fails, we fall back to the regular TCP data path; if it succeeds,
     51  * both endpoints proceed to use tcp_fuse_output() as the transmit path.
     52  * tcp_fuse_output() enqueues application data directly onto the peer's
     53  * receive queue; no protocol processing is involved.  After enqueueing
     54  * the data, the sender can either push (putnext) data up the receiver's
     55  * read queue; or the sender can simply return and let the receiver
     56  * retrieve the enqueued data via the synchronous streams entry point
     57  * tcp_fuse_rrw().  The latter path is taken if synchronous streams is
     58  * enabled (the default).  It is disabled if sockfs no longer resides
     59  * directly on top of tcp module due to a module insertion or removal.
     60  * It also needs to be temporarily disabled when sending urgent data
     61  * because the tcp_fuse_rrw() path bypasses the M_PROTO processing done
     62  * by strsock_proto() hook.
     63  *
     64  * Sychronization is handled by squeue and the mutex tcp_non_sq_lock.
     65  * One of the requirements for fusion to succeed is that both endpoints
     66  * need to be using the same squeue.  This ensures that neither side
     67  * can disappear while the other side is still sending data.  By itself,
     68  * squeue is not sufficient for guaranteeing safety when synchronous
     69  * streams is enabled.  The reason is that tcp_fuse_rrw() doesn't enter
     70  * the squeue and its access to tcp_rcv_list and other fusion-related
     71  * fields needs to be sychronized with the sender.  tcp_non_sq_lock is
     72  * used for this purpose.  When there is urgent data, the sender needs
     73  * to push the data up the receiver's streams read queue.  In order to
     74  * avoid holding the tcp_non_sq_lock across putnext(), the sender sets
     75  * the peer tcp's tcp_fuse_syncstr_plugged bit and releases tcp_non_sq_lock
     76  * (see macro TCP_FUSE_SYNCSTR_PLUG_DRAIN()).  If tcp_fuse_rrw() enters
     77  * after this point, it will see that synchronous streams is plugged and
     78  * will wait on tcp_fuse_plugcv.  After the sender has finished pushing up
     79  * all urgent data, it will clear the tcp_fuse_syncstr_plugged bit using
     80  * TCP_FUSE_SYNCSTR_UNPLUG_DRAIN().  This will cause any threads waiting
     81  * on tcp_fuse_plugcv to return EBUSY, and in turn cause strget() to call
     82  * getq_noenab() to dequeue data from the stream head instead.  Once the
     83  * data on the stream head has been consumed, tcp_fuse_rrw() may again
     84  * be used to process tcp_rcv_list.  However, if TCP_FUSE_SYNCSTR_STOP()
     85  * has been called, all future calls to tcp_fuse_rrw() will return EBUSY,
     86  * effectively disabling synchronous streams.
     87  *
     88  * The following note applies only to the synchronous streams mode.
     89  *
     90  * Flow control is done by checking the size of receive buffer and
     91  * the number of data blocks, both set to different limits.  This is
     92  * different than regular streams flow control where cumulative size
     93  * check dominates block count check -- streams queue high water mark
     94  * typically represents bytes.  Each enqueue triggers notifications
     95  * to the receiving process; a build up of data blocks indicates a
     96  * slow receiver and the sender should be blocked or informed at the
     97  * earliest moment instead of further wasting system resources.  In
     98  * effect, this is equivalent to limiting the number of outstanding
     99  * segments in flight.
    100  */
    101 
    102 /*
    103  * Setting this to false means we disable fusion altogether and
    104  * loopback connections would go through the protocol paths.
    105  */
    106 boolean_t do_tcp_fusion = B_TRUE;
    107 
    108 /*
    109  * Enabling this flag allows sockfs to retrieve data directly
    110  * from a fused tcp endpoint using synchronous streams interface.
    111  */
    112 boolean_t do_tcp_direct_sockfs = B_TRUE;
    113 
    114 /*
    115  * This is the minimum amount of outstanding writes allowed on
    116  * a synchronous streams-enabled receiving endpoint before the
    117  * sender gets flow-controlled.  Setting this value to 0 means
    118  * that the data block limit is equivalent to the byte count
    119  * limit, which essentially disables the check.
    120  */
    121 #define	TCP_FUSION_RCV_UNREAD_MIN	8
    122 uint_t tcp_fusion_rcv_unread_min = TCP_FUSION_RCV_UNREAD_MIN;
    123 
    124 static void		tcp_fuse_syncstr_enable(tcp_t *);
    125 static void		tcp_fuse_syncstr_disable(tcp_t *);
    126 static boolean_t	strrput_sig(queue_t *, boolean_t);
    127 
    128 /*
    129  * Return true if this connection needs some IP functionality
    130  */
    131 static boolean_t
    132 tcp_loopback_needs_ip(tcp_t *tcp, netstack_t *ns)
    133 {
    134 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
    135 
    136 	/*
    137 	 * If ire is not cached, do not use fusion
    138 	 */
    139 	if (tcp->tcp_connp->conn_ire_cache == NULL) {
    140 		/*
    141 		 * There is no need to hold conn_lock here because when called
    142 		 * from tcp_fuse() there can be no window where conn_ire_cache
    143 		 * can change. This is not true whe called from
    144 		 * tcp_fuse_output(). conn_ire_cache can become null just
    145 		 * after the check, but it's ok if a few packets are delivered
    146 		 * in the fused state.
    147 		 */
    148 		return (B_TRUE);
    149 	}
    150 	if (tcp->tcp_ipversion == IPV4_VERSION) {
    151 		if (tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH)
    152 			return (B_TRUE);
    153 		if (CONN_OUTBOUND_POLICY_PRESENT(tcp->tcp_connp, ipss))
    154 			return (B_TRUE);
    155 		if (CONN_INBOUND_POLICY_PRESENT(tcp->tcp_connp, ipss))
    156 			return (B_TRUE);
    157 	} else {
    158 		if (tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)
    159 			return (B_TRUE);
    160 		if (CONN_OUTBOUND_POLICY_PRESENT_V6(tcp->tcp_connp, ipss))
    161 			return (B_TRUE);
    162 		if (CONN_INBOUND_POLICY_PRESENT_V6(tcp->tcp_connp, ipss))
    163 			return (B_TRUE);
    164 	}
    165 	if (!CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp))
    166 		return (B_TRUE);
    167 	return (B_FALSE);
    168 }
    169 
    170 
    171 /*
    172  * This routine gets called by the eager tcp upon changing state from
    173  * SYN_RCVD to ESTABLISHED.  It fuses a direct path between itself
    174  * and the active connect tcp such that the regular tcp processings
    175  * may be bypassed under allowable circumstances.  Because the fusion
    176  * requires both endpoints to be in the same squeue, it does not work
    177  * for simultaneous active connects because there is no easy way to
    178  * switch from one squeue to another once the connection is created.
    179  * This is different from the eager tcp case where we assign it the
    180  * same squeue as the one given to the active connect tcp during open.
    181  */
    182 void
    183 tcp_fuse(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph)
    184 {
    185 	conn_t *peer_connp, *connp = tcp->tcp_connp;
    186 	tcp_t *peer_tcp;
    187 	tcp_stack_t	*tcps = tcp->tcp_tcps;
    188 	netstack_t	*ns;
    189 	ip_stack_t	*ipst = tcps->tcps_netstack->netstack_ip;
    190 
    191 	ASSERT(!tcp->tcp_fused);
    192 	ASSERT(tcp->tcp_loopback);
    193 	ASSERT(tcp->tcp_loopback_peer == NULL);
    194 	/*
    195 	 * We need to inherit q_hiwat of the listener tcp, but we can't
    196 	 * really use tcp_listener since we get here after sending up
    197 	 * T_CONN_IND and tcp_wput_accept() may be called independently,
    198 	 * at which point tcp_listener is cleared; this is why we use
    199 	 * tcp_saved_listener.  The listener itself is guaranteed to be
    200 	 * around until tcp_accept_finish() is called on this eager --
    201 	 * this won't happen until we're done since we're inside the
    202 	 * eager's perimeter now.
    203 	 */
    204 	ASSERT(tcp->tcp_saved_listener != NULL);
    205 
    206 	/*
    207 	 * Lookup peer endpoint; search for the remote endpoint having
    208 	 * the reversed address-port quadruplet in ESTABLISHED state,
    209 	 * which is guaranteed to be unique in the system.  Zone check
    210 	 * is applied accordingly for loopback address, but not for
    211 	 * local address since we want fusion to happen across Zones.
    212 	 */
    213 	if (tcp->tcp_ipversion == IPV4_VERSION) {
    214 		peer_connp = ipcl_conn_tcp_lookup_reversed_ipv4(connp,
    215 		    (ipha_t *)iphdr, tcph, ipst);
    216 	} else {
    217 		peer_connp = ipcl_conn_tcp_lookup_reversed_ipv6(connp,
    218 		    (ip6_t *)iphdr, tcph, ipst);
    219 	}
    220 
    221 	/*
    222 	 * We can only proceed if peer exists, resides in the same squeue
    223 	 * as our conn and is not raw-socket.  The squeue assignment of
    224 	 * this eager tcp was done earlier at the time of SYN processing
    225 	 * in ip_fanout_tcp{_v6}.  Note that similar squeues by itself
    226 	 * doesn't guarantee a safe condition to fuse, hence we perform
    227 	 * additional tests below.
    228 	 */
    229 	ASSERT(peer_connp == NULL || peer_connp != connp);
    230 	if (peer_connp == NULL || peer_connp->conn_sqp != connp->conn_sqp ||
    231 	    !IPCL_IS_TCP(peer_connp)) {
    232 		if (peer_connp != NULL) {
    233 			TCP_STAT(tcps, tcp_fusion_unqualified);
    234 			CONN_DEC_REF(peer_connp);
    235 		}
    236 		return;
    237 	}
    238 	peer_tcp = peer_connp->conn_tcp;	/* active connect tcp */
    239 
    240 	ASSERT(peer_tcp != NULL && peer_tcp != tcp && !peer_tcp->tcp_fused);
    241 	ASSERT(peer_tcp->tcp_loopback && peer_tcp->tcp_loopback_peer == NULL);
    242 	ASSERT(peer_connp->conn_sqp == connp->conn_sqp);
    243 
    244 	/*
    245 	 * Fuse the endpoints; we perform further checks against both
    246 	 * tcp endpoints to ensure that a fusion is allowed to happen.
    247 	 * In particular we bail out for non-simple TCP/IP or if IPsec/
    248 	 * IPQoS policy/kernel SSL exists.
    249 	 */
    250 	ns = tcps->tcps_netstack;
    251 	ipst = ns->netstack_ip;
    252 
    253 	if (!tcp->tcp_unfusable && !peer_tcp->tcp_unfusable &&
    254 	    !tcp_loopback_needs_ip(tcp, ns) &&
    255 	    !tcp_loopback_needs_ip(peer_tcp, ns) &&
    256 	    tcp->tcp_kssl_ent == NULL &&
    257 	    !IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst)) {
    258 		mblk_t *mp;
    259 		struct stroptions *stropt;
    260 		queue_t *peer_rq = peer_tcp->tcp_rq;
    261 
    262 		ASSERT(!TCP_IS_DETACHED(peer_tcp) && peer_rq != NULL);
    263 		ASSERT(tcp->tcp_fused_sigurg_mp == NULL);
    264 		ASSERT(peer_tcp->tcp_fused_sigurg_mp == NULL);
    265 		ASSERT(tcp->tcp_kssl_ctx == NULL);
    266 
    267 		/*
    268 		 * We need to drain data on both endpoints during unfuse.
    269 		 * If we need to send up SIGURG at the time of draining,
    270 		 * we want to be sure that an mblk is readily available.
    271 		 * This is why we pre-allocate the M_PCSIG mblks for both
    272 		 * endpoints which will only be used during/after unfuse.
    273 		 */
    274 		if ((mp = allocb(1, BPRI_HI)) == NULL)
    275 			goto failed;
    276 
    277 		tcp->tcp_fused_sigurg_mp = mp;
    278 
    279 		if ((mp = allocb(1, BPRI_HI)) == NULL)
    280 			goto failed;
    281 
    282 		peer_tcp->tcp_fused_sigurg_mp = mp;
    283 
    284 		/* Allocate M_SETOPTS mblk */
    285 		if ((mp = allocb(sizeof (*stropt), BPRI_HI)) == NULL)
    286 			goto failed;
    287 
    288 		/* If either tcp or peer_tcp sodirect enabled then disable */
    289 		if (tcp->tcp_sodirect != NULL) {
    290 			mutex_enter(tcp->tcp_sodirect->sod_lockp);
    291 			SOD_DISABLE(tcp->tcp_sodirect);
    292 			mutex_exit(tcp->tcp_sodirect->sod_lockp);
    293 			tcp->tcp_sodirect = NULL;
    294 		}
    295 		if (peer_tcp->tcp_sodirect != NULL) {
    296 			mutex_enter(peer_tcp->tcp_sodirect->sod_lockp);
    297 			SOD_DISABLE(peer_tcp->tcp_sodirect);
    298 			mutex_exit(peer_tcp->tcp_sodirect->sod_lockp);
    299 			peer_tcp->tcp_sodirect = NULL;
    300 		}
    301 
    302 		/* Fuse both endpoints */
    303 		peer_tcp->tcp_loopback_peer = tcp;
    304 		tcp->tcp_loopback_peer = peer_tcp;
    305 		peer_tcp->tcp_fused = tcp->tcp_fused = B_TRUE;
    306 
    307 		/*
    308 		 * We never use regular tcp paths in fusion and should
    309 		 * therefore clear tcp_unsent on both endpoints.  Having
    310 		 * them set to non-zero values means asking for trouble
    311 		 * especially after unfuse, where we may end up sending
    312 		 * through regular tcp paths which expect xmit_list and
    313 		 * friends to be correctly setup.
    314 		 */
    315 		peer_tcp->tcp_unsent = tcp->tcp_unsent = 0;
    316 
    317 		tcp_timers_stop(tcp);
    318 		tcp_timers_stop(peer_tcp);
    319 
    320 		/*
    321 		 * At this point we are a detached eager tcp and therefore
    322 		 * don't have a queue assigned to us until accept happens.
    323 		 * In the mean time the peer endpoint may immediately send
    324 		 * us data as soon as fusion is finished, and we need to be
    325 		 * able to flow control it in case it sends down huge amount
    326 		 * of data while we're still detached.  To prevent that we
    327 		 * inherit the listener's q_hiwat value; this is temporary
    328 		 * since we'll repeat the process in tcp_accept_finish().
    329 		 */
    330 		(void) tcp_fuse_set_rcv_hiwat(tcp,
    331 		    tcp->tcp_saved_listener->tcp_rq->q_hiwat);
    332 
    333 		/*
    334 		 * Set the stream head's write offset value to zero since we
    335 		 * won't be needing any room for TCP/IP headers; tell it to
    336 		 * not break up the writes (this would reduce the amount of
    337 		 * work done by kmem); and configure our receive buffer.
    338 		 * Note that we can only do this for the active connect tcp
    339 		 * since our eager is still detached; it will be dealt with
    340 		 * later in tcp_accept_finish().
    341 		 */
    342 		DB_TYPE(mp) = M_SETOPTS;
    343 		mp->b_wptr += sizeof (*stropt);
    344 
    345 		stropt = (struct stroptions *)mp->b_rptr;
    346 		stropt->so_flags = SO_MAXBLK | SO_WROFF | SO_HIWAT;
    347 		stropt->so_maxblk = tcp_maxpsz_set(peer_tcp, B_FALSE);
    348 		stropt->so_wroff = 0;
    349 
    350 		/*
    351 		 * Record the stream head's high water mark for
    352 		 * peer endpoint; this is used for flow-control
    353 		 * purposes in tcp_fuse_output().
    354 		 */
    355 		stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(peer_tcp,
    356 		    peer_rq->q_hiwat);
    357 
    358 		/* Send the options up */
    359 		putnext(peer_rq, mp);
    360 	} else {
    361 		TCP_STAT(tcps, tcp_fusion_unqualified);
    362 	}
    363 	CONN_DEC_REF(peer_connp);
    364 	return;
    365 
    366 failed:
    367 	if (tcp->tcp_fused_sigurg_mp != NULL) {
    368 		freeb(tcp->tcp_fused_sigurg_mp);
    369 		tcp->tcp_fused_sigurg_mp = NULL;
    370 	}
    371 	if (peer_tcp->tcp_fused_sigurg_mp != NULL) {
    372 		freeb(peer_tcp->tcp_fused_sigurg_mp);
    373 		peer_tcp->tcp_fused_sigurg_mp = NULL;
    374 	}
    375 	CONN_DEC_REF(peer_connp);
    376 }
    377 
    378 /*
    379  * Unfuse a previously-fused pair of tcp loopback endpoints.
    380  */
    381 void
    382 tcp_unfuse(tcp_t *tcp)
    383 {
    384 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
    385 
    386 	ASSERT(tcp->tcp_fused && peer_tcp != NULL);
    387 	ASSERT(peer_tcp->tcp_fused && peer_tcp->tcp_loopback_peer == tcp);
    388 	ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp);
    389 	ASSERT(tcp->tcp_unsent == 0 && peer_tcp->tcp_unsent == 0);
    390 	ASSERT(tcp->tcp_fused_sigurg_mp != NULL);
    391 	ASSERT(peer_tcp->tcp_fused_sigurg_mp != NULL);
    392 
    393 	/*
    394 	 * We disable synchronous streams, drain any queued data and
    395 	 * clear tcp_direct_sockfs.  The synchronous streams entry
    396 	 * points will become no-ops after this point.
    397 	 */
    398 	tcp_fuse_disable_pair(tcp, B_TRUE);
    399 
    400 	/*
    401 	 * Update th_seq and th_ack in the header template
    402 	 */
    403 	U32_TO_ABE32(tcp->tcp_snxt, tcp->tcp_tcph->th_seq);
    404 	U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
    405 	U32_TO_ABE32(peer_tcp->tcp_snxt, peer_tcp->tcp_tcph->th_seq);
    406 	U32_TO_ABE32(peer_tcp->tcp_rnxt, peer_tcp->tcp_tcph->th_ack);
    407 
    408 	/* Unfuse the endpoints */
    409 	peer_tcp->tcp_fused = tcp->tcp_fused = B_FALSE;
    410 	peer_tcp->tcp_loopback_peer = tcp->tcp_loopback_peer = NULL;
    411 }
    412 
    413 /*
    414  * Fusion output routine for urgent data.  This routine is called by
    415  * tcp_fuse_output() for handling non-M_DATA mblks.
    416  */
    417 void
    418 tcp_fuse_output_urg(tcp_t *tcp, mblk_t *mp)
    419 {
    420 	mblk_t *mp1;
    421 	struct T_exdata_ind *tei;
    422 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
    423 	mblk_t *head, *prev_head = NULL;
    424 	tcp_stack_t	*tcps = tcp->tcp_tcps;
    425 
    426 	ASSERT(tcp->tcp_fused);
    427 	ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp);
    428 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
    429 	ASSERT(mp->b_cont != NULL && DB_TYPE(mp->b_cont) == M_DATA);
    430 	ASSERT(MBLKL(mp) >= sizeof (*tei) && MBLKL(mp->b_cont) > 0);
    431 
    432 	/*
    433 	 * Urgent data arrives in the form of T_EXDATA_REQ from above.
    434 	 * Each occurence denotes a new urgent pointer.  For each new
    435 	 * urgent pointer we signal (SIGURG) the receiving app to indicate
    436 	 * that it needs to go into urgent mode.  This is similar to the
    437 	 * urgent data handling in the regular tcp.  We don't need to keep
    438 	 * track of where the urgent pointer is, because each T_EXDATA_REQ
    439 	 * "advances" the urgent pointer for us.
    440 	 *
    441 	 * The actual urgent data carried by T_EXDATA_REQ is then prepended
    442 	 * by a T_EXDATA_IND before being enqueued behind any existing data
    443 	 * destined for the receiving app.  There is only a single urgent
    444 	 * pointer (out-of-band mark) for a given tcp.  If the new urgent
    445 	 * data arrives before the receiving app reads some existing urgent
    446 	 * data, the previous marker is lost.  This behavior is emulated
    447 	 * accordingly below, by removing any existing T_EXDATA_IND messages
    448 	 * and essentially converting old urgent data into non-urgent.
    449 	 */
    450 	ASSERT(tcp->tcp_valid_bits & TCP_URG_VALID);
    451 	/* Let sender get out of urgent mode */
    452 	tcp->tcp_valid_bits &= ~TCP_URG_VALID;
    453 
    454 	/*
    455 	 * This flag indicates that a signal needs to be sent up.
    456 	 * This flag will only get cleared once SIGURG is delivered and
    457 	 * is not affected by the tcp_fused flag -- delivery will still
    458 	 * happen even after an endpoint is unfused, to handle the case
    459 	 * where the sending endpoint immediately closes/unfuses after
    460 	 * sending urgent data and the accept is not yet finished.
    461 	 */
    462 	peer_tcp->tcp_fused_sigurg = B_TRUE;
    463 
    464 	/* Reuse T_EXDATA_REQ mblk for T_EXDATA_IND */
    465 	DB_TYPE(mp) = M_PROTO;
    466 	tei = (struct T_exdata_ind *)mp->b_rptr;
    467 	tei->PRIM_type = T_EXDATA_IND;
    468 	tei->MORE_flag = 0;
    469 	mp->b_wptr = (uchar_t *)&tei[1];
    470 
    471 	TCP_STAT(tcps, tcp_fusion_urg);
    472 	BUMP_MIB(&tcps->tcps_mib, tcpOutUrg);
    473 
    474 	head = peer_tcp->tcp_rcv_list;
    475 	while (head != NULL) {
    476 		/*
    477 		 * Remove existing T_EXDATA_IND, keep the data which follows
    478 		 * it and relink our list.  Note that we don't modify the
    479 		 * tcp_rcv_last_tail since it never points to T_EXDATA_IND.
    480 		 */
    481 		if (DB_TYPE(head) != M_DATA) {
    482 			mp1 = head;
    483 
    484 			ASSERT(DB_TYPE(mp1->b_cont) == M_DATA);
    485 			head = mp1->b_cont;
    486 			mp1->b_cont = NULL;
    487 			head->b_next = mp1->b_next;
    488 			mp1->b_next = NULL;
    489 			if (prev_head != NULL)
    490 				prev_head->b_next = head;
    491 			if (peer_tcp->tcp_rcv_list == mp1)
    492 				peer_tcp->tcp_rcv_list = head;
    493 			if (peer_tcp->tcp_rcv_last_head == mp1)
    494 				peer_tcp->tcp_rcv_last_head = head;
    495 			freeb(mp1);
    496 		}
    497 		prev_head = head;
    498 		head = head->b_next;
    499 	}
    500 }
    501 
    502 /*
    503  * Fusion output routine, called by tcp_output() and tcp_wput_proto().
    504  * If we are modifying any member that can be changed outside the squeue,
    505  * like tcp_flow_stopped, we need to take tcp_non_sq_lock.
    506  */
    507 boolean_t
    508 tcp_fuse_output(tcp_t *tcp, mblk_t *mp, uint32_t send_size)
    509 {
    510 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
    511 	uint_t max_unread;
    512 	boolean_t flow_stopped, peer_data_queued = B_FALSE;
    513 	boolean_t urgent = (DB_TYPE(mp) != M_DATA);
    514 	mblk_t *mp1 = mp;
    515 	ill_t *ilp, *olp;
    516 	ipha_t *ipha;
    517 	ip6_t *ip6h;
    518 	tcph_t *tcph;
    519 	uint_t ip_hdr_len;
    520 	uint32_t seq;
    521 	uint32_t recv_size = send_size;
    522 	tcp_stack_t	*tcps = tcp->tcp_tcps;
    523 	netstack_t	*ns = tcps->tcps_netstack;
    524 	ip_stack_t	*ipst = ns->netstack_ip;
    525 
    526 	ASSERT(tcp->tcp_fused);
    527 	ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp);
    528 	ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp);
    529 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO ||
    530 	    DB_TYPE(mp) == M_PCPROTO);
    531 
    532 
    533 	/* If this connection requires IP, unfuse and use regular path */
    534 	if (tcp_loopback_needs_ip(tcp, ns) ||
    535 	    tcp_loopback_needs_ip(peer_tcp, ns) ||
    536 	    IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst)) {
    537 		TCP_STAT(tcps, tcp_fusion_aborted);
    538 		goto unfuse;
    539 	}
    540 
    541 	if (send_size == 0) {
    542 		freemsg(mp);
    543 		return (B_TRUE);
    544 	}
    545 	max_unread = peer_tcp->tcp_fuse_rcv_unread_hiwater;
    546 
    547 	/*
    548 	 * Handle urgent data; we either send up SIGURG to the peer now
    549 	 * or do it later when we drain, in case the peer is detached
    550 	 * or if we're short of memory for M_PCSIG mblk.
    551 	 */
    552 	if (urgent) {
    553 		/*
    554 		 * We stop synchronous streams when we have urgent data
    555 		 * queued to prevent tcp_fuse_rrw() from pulling it.  If
    556 		 * for some reasons the urgent data can't be delivered
    557 		 * below, synchronous streams will remain stopped until
    558 		 * someone drains the tcp_rcv_list.
    559 		 */
    560 		TCP_FUSE_SYNCSTR_PLUG_DRAIN(peer_tcp);
    561 		tcp_fuse_output_urg(tcp, mp);
    562 
    563 		mp1 = mp->b_cont;
    564 	}
    565 
    566 	if (tcp->tcp_ipversion == IPV4_VERSION &&
    567 	    (HOOKS4_INTERESTED_LOOPBACK_IN(ipst) ||
    568 	    HOOKS4_INTERESTED_LOOPBACK_OUT(ipst)) ||
    569 	    tcp->tcp_ipversion == IPV6_VERSION &&
    570 	    (HOOKS6_INTERESTED_LOOPBACK_IN(ipst) ||
    571 	    HOOKS6_INTERESTED_LOOPBACK_OUT(ipst))) {
    572 		/*
    573 		 * Build ip and tcp header to satisfy FW_HOOKS.
    574 		 * We only build it when any hook is present.
    575 		 */
    576 		if ((mp1 = tcp_xmit_mp(tcp, mp1, tcp->tcp_mss, NULL, NULL,
    577 		    tcp->tcp_snxt, B_TRUE, NULL, B_FALSE)) == NULL)
    578 			/* If tcp_xmit_mp fails, use regular path */
    579 			goto unfuse;
    580 
    581 		ASSERT(peer_tcp->tcp_connp->conn_ire_cache->ire_ipif != NULL);
    582 		olp = peer_tcp->tcp_connp->conn_ire_cache->ire_ipif->ipif_ill;
    583 		/* PFHooks: LOOPBACK_OUT */
    584 		if (tcp->tcp_ipversion == IPV4_VERSION) {
    585 			ipha = (ipha_t *)mp1->b_rptr;
    586 
    587 			DTRACE_PROBE4(ip4__loopback__out__start,
    588 			    ill_t *, NULL, ill_t *, olp,
    589 			    ipha_t *, ipha, mblk_t *, mp1);
    590 			FW_HOOKS(ipst->ips_ip4_loopback_out_event,
    591 			    ipst->ips_ipv4firewall_loopback_out,
    592 			    NULL, olp, ipha, mp1, mp1, 0, ipst);
    593 			DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, mp1);
    594 		} else {
    595 			ip6h = (ip6_t *)mp1->b_rptr;
    596 
    597 			DTRACE_PROBE4(ip6__loopback__out__start,
    598 			    ill_t *, NULL, ill_t *, olp,
    599 			    ip6_t *, ip6h, mblk_t *, mp1);
    600 			FW_HOOKS6(ipst->ips_ip6_loopback_out_event,
    601 			    ipst->ips_ipv6firewall_loopback_out,
    602 			    NULL, olp, ip6h, mp1, mp1, 0, ipst);
    603 			DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, mp1);
    604 		}
    605 		if (mp1 == NULL)
    606 			goto unfuse;
    607 
    608 
    609 		/* PFHooks: LOOPBACK_IN */
    610 		ASSERT(tcp->tcp_connp->conn_ire_cache->ire_ipif != NULL);
    611 		ilp = tcp->tcp_connp->conn_ire_cache->ire_ipif->ipif_ill;
    612 
    613 		if (tcp->tcp_ipversion == IPV4_VERSION) {
    614 			DTRACE_PROBE4(ip4__loopback__in__start,
    615 			    ill_t *, ilp, ill_t *, NULL,
    616 			    ipha_t *, ipha, mblk_t *, mp1);
    617 			FW_HOOKS(ipst->ips_ip4_loopback_in_event,
    618 			    ipst->ips_ipv4firewall_loopback_in,
    619 			    ilp, NULL, ipha, mp1, mp1, 0, ipst);
    620 			DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, mp1);
    621 			if (mp1 == NULL)
    622 				goto unfuse;
    623 
    624 			ip_hdr_len = IPH_HDR_LENGTH(ipha);
    625 		} else {
    626 			DTRACE_PROBE4(ip6__loopback__in__start,
    627 			    ill_t *, ilp, ill_t *, NULL,
    628 			    ip6_t *, ip6h, mblk_t *, mp1);
    629 			FW_HOOKS6(ipst->ips_ip6_loopback_in_event,
    630 			    ipst->ips_ipv6firewall_loopback_in,
    631 			    ilp, NULL, ip6h, mp1, mp1, 0, ipst);
    632 			DTRACE_PROBE1(ip6__loopback__in__end, mblk_t *, mp1);
    633 			if (mp1 == NULL)
    634 				goto unfuse;
    635 
    636 			ip_hdr_len = ip_hdr_length_v6(mp1, ip6h);
    637 		}
    638 
    639 		/* Data length might be changed by FW_HOOKS */
    640 		tcph = (tcph_t *)&mp1->b_rptr[ip_hdr_len];
    641 		seq = ABE32_TO_U32(tcph->th_seq);
    642 		recv_size += seq - tcp->tcp_snxt;
    643 
    644 		/*
    645 		 * The message duplicated by tcp_xmit_mp is freed.
    646 		 * Note: the original message passed in remains unchanged.
    647 		 */
    648 		freemsg(mp1);
    649 	}
    650 
    651 	mutex_enter(&peer_tcp->tcp_non_sq_lock);
    652 	/*
    653 	 * Wake up and signal the peer; it is okay to do this before
    654 	 * enqueueing because we are holding the lock.  One of the
    655 	 * advantages of synchronous streams is the ability for us to
    656 	 * find out when the application performs a read on the socket,
    657 	 * by way of tcp_fuse_rrw() entry point being called.  Every
    658 	 * data that gets enqueued onto the receiver is treated as if
    659 	 * it has arrived at the receiving endpoint, thus generating
    660 	 * SIGPOLL/SIGIO for asynchronous socket just as in the strrput()
    661 	 * case.  However, we only wake up the application when necessary,
    662 	 * i.e. during the first enqueue.  When tcp_fuse_rrw() is called
    663 	 * it will send everything upstream.
    664 	 */
    665 	if (peer_tcp->tcp_direct_sockfs && !urgent &&
    666 	    !TCP_IS_DETACHED(peer_tcp)) {
    667 		/* Update poll events and send SIGPOLL/SIGIO if necessary */
    668 		STR_WAKEUP_SENDSIG(STREAM(peer_tcp->tcp_rq),
    669 		    peer_tcp->tcp_rcv_list);
    670 	}
    671 
    672 	/*
    673 	 * Enqueue data into the peer's receive list; we may or may not
    674 	 * drain the contents depending on the conditions below.
    675 	 */
    676 	tcp_rcv_enqueue(peer_tcp, mp, recv_size);
    677 
    678 	/* In case it wrapped around and also to keep it constant */
    679 	peer_tcp->tcp_rwnd += recv_size;
    680 	/*
    681 	 * We increase the peer's unread message count here whilst still
    682 	 * holding it's tcp_non_sq_lock. This ensures that the increment
    683 	 * occurs in the same lock acquisition perimeter as the enqueue.
    684 	 * Depending on lock hierarchy, we can release these locks which
    685 	 * creates a window in which we can race with tcp_fuse_rrw()
    686 	 */
    687 	peer_tcp->tcp_fuse_rcv_unread_cnt++;
    688 
    689 	/*
    690 	 * Exercise flow-control when needed; we will get back-enabled
    691 	 * in either tcp_accept_finish(), tcp_unfuse(), or tcp_fuse_rrw().
    692 	 * If tcp_direct_sockfs is on or if the peer endpoint is detached,
    693 	 * we emulate streams flow control by checking the peer's queue
    694 	 * size and high water mark; otherwise we simply use canputnext()
    695 	 * to decide if we need to stop our flow.
    696 	 *
    697 	 * The outstanding unread data block check does not apply for a
    698 	 * detached receiver; this is to avoid unnecessary blocking of the
    699 	 * sender while the accept is currently in progress and is quite
    700 	 * similar to the regular tcp.
    701 	 */
    702 	if (TCP_IS_DETACHED(peer_tcp) || max_unread == 0)
    703 		max_unread = UINT_MAX;
    704 
    705 	/*
    706 	 * Since we are accessing our tcp_flow_stopped and might modify it,
    707 	 * we need to take tcp->tcp_non_sq_lock. The lock for the highest
    708 	 * address is held first. Dropping peer_tcp->tcp_non_sq_lock should
    709 	 * not be an issue here since we are within the squeue and the peer
    710 	 * won't disappear.
    711 	 */
    712 	if (tcp > peer_tcp) {
    713 		mutex_exit(&peer_tcp->tcp_non_sq_lock);
    714 		mutex_enter(&tcp->tcp_non_sq_lock);
    715 		mutex_enter(&peer_tcp->tcp_non_sq_lock);
    716 	} else {
    717 		mutex_enter(&tcp->tcp_non_sq_lock);
    718 	}
    719 	flow_stopped = tcp->tcp_flow_stopped;
    720 	if (((peer_tcp->tcp_direct_sockfs || TCP_IS_DETACHED(peer_tcp)) &&
    721 	    (peer_tcp->tcp_rcv_cnt >= peer_tcp->tcp_fuse_rcv_hiwater ||
    722 	    peer_tcp->tcp_fuse_rcv_unread_cnt >= max_unread)) ||
    723 	    (!peer_tcp->tcp_direct_sockfs && !TCP_IS_DETACHED(peer_tcp) &&
    724 	    !canputnext(peer_tcp->tcp_rq))) {
    725 		peer_data_queued = B_TRUE;
    726 	}
    727 
    728 	if (!flow_stopped && (peer_data_queued ||
    729 	    (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater))) {
    730 		tcp_setqfull(tcp);
    731 		flow_stopped = B_TRUE;
    732 		TCP_STAT(tcps, tcp_fusion_flowctl);
    733 		DTRACE_PROBE4(tcp__fuse__output__flowctl, tcp_t *, tcp,
    734 		    uint_t, send_size, uint_t, peer_tcp->tcp_rcv_cnt,
    735 		    uint_t, peer_tcp->tcp_fuse_rcv_unread_cnt);
    736 	} else if (flow_stopped && !peer_data_queued &&
    737 	    (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater)) {
    738 		tcp_clrqfull(tcp);
    739 		TCP_STAT(tcps, tcp_fusion_backenabled);
    740 		flow_stopped = B_FALSE;
    741 	}
    742 	mutex_exit(&tcp->tcp_non_sq_lock);
    743 
    744 	/*
    745 	 * If we are in synchronous streams mode and the peer read queue is
    746 	 * not full then schedule a push timer if one is not scheduled
    747 	 * already. This is needed for applications which use MSG_PEEK to
    748 	 * determine the number of bytes available before issuing a 'real'
    749 	 * read. It also makes flow control more deterministic, particularly
    750 	 * for smaller message sizes.
    751 	 */
    752 	if (!urgent && peer_tcp->tcp_direct_sockfs &&
    753 	    peer_tcp->tcp_push_tid == 0 && !TCP_IS_DETACHED(peer_tcp) &&
    754 	    canputnext(peer_tcp->tcp_rq)) {
    755 		peer_tcp->tcp_push_tid = TCP_TIMER(peer_tcp, tcp_push_timer,
    756 		    MSEC_TO_TICK(tcps->tcps_push_timer_interval));
    757 	}
    758 	mutex_exit(&peer_tcp->tcp_non_sq_lock);
    759 	ipst->ips_loopback_packets++;
    760 	tcp->tcp_last_sent_len = send_size;
    761 
    762 	/* Need to adjust the following SNMP MIB-related variables */
    763 	tcp->tcp_snxt += send_size;
    764 	tcp->tcp_suna = tcp->tcp_snxt;
    765 	peer_tcp->tcp_rnxt += recv_size;
    766 	peer_tcp->tcp_rack = peer_tcp->tcp_rnxt;
    767 
    768 	BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs);
    769 	UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, send_size);
    770 
    771 	BUMP_MIB(&tcps->tcps_mib, tcpInSegs);
    772 	BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs);
    773 	UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, send_size);
    774 
    775 	BUMP_LOCAL(tcp->tcp_obsegs);
    776 	BUMP_LOCAL(peer_tcp->tcp_ibsegs);
    777 
    778 	DTRACE_PROBE2(tcp__fuse__output, tcp_t *, tcp, uint_t, send_size);
    779 
    780 	if (!TCP_IS_DETACHED(peer_tcp)) {
    781 		/*
    782 		 * Drain the peer's receive queue it has urgent data or if
    783 		 * we're not flow-controlled.  There is no need for draining
    784 		 * normal data when tcp_direct_sockfs is on because the peer
    785 		 * will pull the data via tcp_fuse_rrw().
    786 		 */
    787 		if (urgent || (!flow_stopped && !peer_tcp->tcp_direct_sockfs)) {
    788 			ASSERT(peer_tcp->tcp_rcv_list != NULL);
    789 			/*
    790 			 * For TLI-based streams, a thread in tcp_accept_swap()
    791 			 * can race with us.  That thread will ensure that the
    792 			 * correct peer_tcp->tcp_rq is globally visible before
    793 			 * peer_tcp->tcp_detached is visible as clear, but we
    794 			 * must also ensure that the load of tcp_rq cannot be
    795 			 * reordered to be before the tcp_detached check.
    796 			 */
    797 			membar_consumer();
    798 			(void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp,
    799 			    NULL);
    800 			/*
    801 			 * If synchronous streams was stopped above due
    802 			 * to the presence of urgent data, re-enable it.
    803 			 */
    804 			if (urgent)
    805 				TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(peer_tcp);
    806 		}
    807 	}
    808 	return (B_TRUE);
    809 unfuse:
    810 	tcp_unfuse(tcp);
    811 	return (B_FALSE);
    812 }
    813 
    814 /*
    815  * This routine gets called to deliver data upstream on a fused or
    816  * previously fused tcp loopback endpoint; the latter happens only
    817  * when there is a pending SIGURG signal plus urgent data that can't
    818  * be sent upstream in the past.
    819  */
    820 boolean_t
    821 tcp_fuse_rcv_drain(queue_t *q, tcp_t *tcp, mblk_t **sigurg_mpp)
    822 {
    823 	mblk_t *mp;
    824 #ifdef DEBUG
    825 	uint_t cnt = 0;
    826 #endif
    827 	tcp_stack_t	*tcps = tcp->tcp_tcps;
    828 	tcp_t		*peer_tcp = tcp->tcp_loopback_peer;
    829 	boolean_t	sd_rd_eof = B_FALSE;
    830 
    831 	ASSERT(tcp->tcp_loopback);
    832 	ASSERT(tcp->tcp_fused || tcp->tcp_fused_sigurg);
    833 	ASSERT(!tcp->tcp_fused || tcp->tcp_loopback_peer != NULL);
    834 	ASSERT(sigurg_mpp != NULL || tcp->tcp_fused);
    835 
    836 	/* No need for the push timer now, in case it was scheduled */
    837 	if (tcp->tcp_push_tid != 0) {
    838 		(void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid);
    839 		tcp->tcp_push_tid = 0;
    840 	}
    841 	/*
    842 	 * If there's urgent data sitting in receive list and we didn't
    843 	 * get a chance to send up a SIGURG signal, make sure we send
    844 	 * it first before draining in order to ensure that SIOCATMARK
    845 	 * works properly.
    846 	 */
    847 	if (tcp->tcp_fused_sigurg) {
    848 		/*
    849 		 * sigurg_mpp is normally NULL, i.e. when we're still
    850 		 * fused and didn't get here because of tcp_unfuse().
    851 		 * In this case try hard to allocate the M_PCSIG mblk.
    852 		 */
    853 		if (sigurg_mpp == NULL &&
    854 		    (mp = allocb(1, BPRI_HI)) == NULL &&
    855 		    (mp = allocb_tryhard(1)) == NULL) {
    856 			/* Alloc failed; try again next time */
    857 			tcp->tcp_push_tid = TCP_TIMER(tcp, tcp_push_timer,
    858 			    MSEC_TO_TICK(tcps->tcps_push_timer_interval));
    859 			return (B_TRUE);
    860 		} else if (sigurg_mpp != NULL) {
    861 			/*
    862 			 * Use the supplied M_PCSIG mblk; it means we're
    863 			 * either unfused or in the process of unfusing,
    864 			 * and the drain must happen now.
    865 			 */
    866 			mp = *sigurg_mpp;
    867 			*sigurg_mpp = NULL;
    868 		}
    869 		ASSERT(mp != NULL);
    870 
    871 		tcp->tcp_fused_sigurg = B_FALSE;
    872 		/* Send up the signal */
    873 		DB_TYPE(mp) = M_PCSIG;
    874 		*mp->b_wptr++ = (uchar_t)SIGURG;
    875 		putnext(q, mp);
    876 		/*
    877 		 * Let the regular tcp_rcv_drain() path handle
    878 		 * draining the data if we're no longer fused.
    879 		 */
    880 		if (!tcp->tcp_fused)
    881 			return (B_FALSE);
    882 	}
    883 
    884 	/*
    885 	 * In the synchronous streams case, we generate SIGPOLL/SIGIO for
    886 	 * each M_DATA that gets enqueued onto the receiver.  At this point
    887 	 * we are about to drain any queued data via putnext().  In order
    888 	 * to avoid extraneous signal generation from strrput(), we set
    889 	 * STRGETINPROG flag at the stream head prior to the draining and
    890 	 * restore it afterwards.  This masks out signal generation only
    891 	 * for M_DATA messages and does not affect urgent data. We only do
    892 	 * this if the STREOF flag is not set which can happen if the
    893 	 * application shuts down the read side of a stream. In this case
    894 	 * we simply free these messages to approximate the flushq behavior
    895 	 * which normally occurs when STREOF is on the stream head read queue.
    896 	 */
    897 	if (tcp->tcp_direct_sockfs)
    898 		sd_rd_eof = strrput_sig(q, B_FALSE);
    899 
    900 	/* Drain the data */
    901 	while ((mp = tcp->tcp_rcv_list) != NULL) {
    902 		tcp->tcp_rcv_list = mp->b_next;
    903 		mp->b_next = NULL;
    904 #ifdef DEBUG
    905 		cnt += msgdsize(mp);
    906 #endif
    907 		if (sd_rd_eof) {
    908 			freemsg(mp);
    909 		} else {
    910 			putnext(q, mp);
    911 			TCP_STAT(tcps, tcp_fusion_putnext);
    912 		}
    913 	}
    914 
    915 	if (tcp->tcp_direct_sockfs && !sd_rd_eof)
    916 		(void) strrput_sig(q, B_TRUE);
    917 
    918 	ASSERT(cnt == tcp->tcp_rcv_cnt);
    919 	tcp->tcp_rcv_last_head = NULL;
    920 	tcp->tcp_rcv_last_tail = NULL;
    921 	tcp->tcp_rcv_cnt = 0;
    922 	tcp->tcp_fuse_rcv_unread_cnt = 0;
    923 	tcp->tcp_rwnd = q->q_hiwat;
    924 
    925 	if (peer_tcp->tcp_flow_stopped && (TCP_UNSENT_BYTES(peer_tcp) <=
    926 	    peer_tcp->tcp_xmit_lowater)) {
    927 		tcp_clrqfull(peer_tcp);
    928 		TCP_STAT(tcps, tcp_fusion_backenabled);
    929 	}
    930 
    931 	return (B_TRUE);
    932 }
    933 
    934 /*
    935  * Synchronous stream entry point for sockfs to retrieve
    936  * data directly from tcp_rcv_list.
    937  * tcp_fuse_rrw() might end up modifying the peer's tcp_flow_stopped,
    938  * for which it  must take the tcp_non_sq_lock of the peer as well
    939  * making any change. The order of taking the locks is based on
    940  * the TCP pointer itself. Before we get the peer we need to take
    941  * our tcp_non_sq_lock so that the peer doesn't disappear. However,
    942  * we cannot drop the lock if we have to grab the peer's lock (because
    943  * of ordering), since the peer might disappear in the interim. So,
    944  * we take our tcp_non_sq_lock, get the peer, increment the ref on the
    945  * peer's conn, drop all the locks and then take the tcp_non_sq_lock in the
    946  * desired order. Incrementing the conn ref on the peer means that the
    947  * peer won't disappear when we drop our tcp_non_sq_lock.
    948  */
    949 int
    950 tcp_fuse_rrw(queue_t *q, struiod_t *dp)
    951 {
    952 	tcp_t *tcp = Q_TO_CONN(q)->conn_tcp;
    953 	mblk_t *mp;
    954 	tcp_t *peer_tcp;
    955 	tcp_stack_t	*tcps = tcp->tcp_tcps;
    956 
    957 	mutex_enter(&tcp->tcp_non_sq_lock);
    958 
    959 	/*
    960 	 * If tcp_fuse_syncstr_plugged is set, then another thread is moving
    961 	 * the underlying data to the stream head.  We need to wait until it's
    962 	 * done, then return EBUSY so that strget() will dequeue data from the
    963 	 * stream head to ensure data is drained in-order.
    964 	 */
    965 plugged:
    966 	if (tcp->tcp_fuse_syncstr_plugged) {
    967 		do {
    968 			cv_wait(&tcp->tcp_fuse_plugcv, &tcp->tcp_non_sq_lock);
    969 		} while (tcp->tcp_fuse_syncstr_plugged);
    970 
    971 		mutex_exit(&tcp->tcp_non_sq_lock);
    972 		TCP_STAT(tcps, tcp_fusion_rrw_plugged);
    973 		TCP_STAT(tcps, tcp_fusion_rrw_busy);
    974 		return (EBUSY);
    975 	}
    976 
    977 	peer_tcp = tcp->tcp_loopback_peer;
    978 
    979 	/*
    980 	 * If someone had turned off tcp_direct_sockfs or if synchronous
    981 	 * streams is stopped, we return EBUSY.  This causes strget() to
    982 	 * dequeue data from the stream head instead.
    983 	 */
    984 	if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped) {
    985 		mutex_exit(&tcp->tcp_non_sq_lock);
    986 		TCP_STAT(tcps, tcp_fusion_rrw_busy);
    987 		return (EBUSY);
    988 	}
    989 
    990 	/*
    991 	 * Grab lock in order. The highest addressed tcp is locked first.
    992 	 * We don't do this within the tcp_rcv_list check since if we
    993 	 * have to drop the lock, for ordering, then the tcp_rcv_list
    994 	 * could change.
    995 	 */
    996 	if (peer_tcp > tcp) {
    997 		CONN_INC_REF(peer_tcp->tcp_connp);
    998 		mutex_exit(&tcp->tcp_non_sq_lock);
    999 		mutex_enter(&peer_tcp->tcp_non_sq_lock);
   1000 		mutex_enter(&tcp->tcp_non_sq_lock);
   1001 		/*
   1002 		 * This might have changed in the interim
   1003 		 * Once read-side tcp_non_sq_lock is dropped above
   1004 		 * anything can happen, we need to check all
   1005 		 * known conditions again once we reaquire
   1006 		 * read-side tcp_non_sq_lock.
   1007 		 */
   1008 		if (tcp->tcp_fuse_syncstr_plugged) {
   1009 			mutex_exit(&peer_tcp->tcp_non_sq_lock);
   1010 			CONN_DEC_REF(peer_tcp->tcp_connp);
   1011 			goto plugged;
   1012 		}
   1013 		if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped) {
   1014 			mutex_exit(&tcp->tcp_non_sq_lock);
   1015 			mutex_exit(&peer_tcp->tcp_non_sq_lock);
   1016 			CONN_DEC_REF(peer_tcp->tcp_connp);
   1017 			TCP_STAT(tcps, tcp_fusion_rrw_busy);
   1018 			return (