xref: /titanic_51/usr/src/uts/common/inet/tcp/tcp_fusion.c (revision 32232bf4531ddb458488ba01c9df68685921ebf4)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/strsun.h>
31 #include <sys/strsubr.h>
32 #include <sys/debug.h>
33 #include <sys/cmn_err.h>
34 #include <sys/tihdr.h>
35 
36 #include <inet/common.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_fuse_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_fuse_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_fuse_lock across putnext(), the sender sets
75  * the peer tcp's tcp_fuse_syncstr_stopped bit and releases tcp_fuse_lock
76  * (see macro TCP_FUSE_SYNCSTR_STOP()).  If tcp_fuse_rrw() enters after
77  * this point, it will see that synchronous streams is temporarily
78  * stopped and it will immediately return EBUSY without accessing the
79  * tcp_rcv_list or other fields protected by the tcp_fuse_lock.  This
80  * will result in strget() calling getq_noenab() to dequeue data from
81  * the stream head instead.  After the sender has finished pushing up
82  * all urgent data, it will clear the tcp_fuse_syncstr_stopped bit using
83  * TCP_FUSE_SYNCSTR_RESUME and the receiver may then resume using
84  * tcp_fuse_rrw() to retrieve data from tcp_rcv_list.
85  *
86  * The following note applies only to the synchronous streams mode.
87  *
88  * Flow control is done by checking the size of receive buffer and
89  * the number of data blocks, both set to different limits.  This is
90  * different than regular streams flow control where cumulative size
91  * check dominates block count check -- streams queue high water mark
92  * typically represents bytes.  Each enqueue triggers notifications
93  * to the receiving process; a build up of data blocks indicates a
94  * slow receiver and the sender should be blocked or informed at the
95  * earliest moment instead of further wasting system resources.  In
96  * effect, this is equivalent to limiting the number of outstanding
97  * segments in flight.
98  */
99 
100 /*
101  * Macros that determine whether or not IP processing is needed for TCP.
102  */
103 #define	TCP_IPOPT_POLICY_V4(tcp)					\
104 	((tcp)->tcp_ipversion == IPV4_VERSION &&			\
105 	((tcp)->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH ||		\
106 	CONN_OUTBOUND_POLICY_PRESENT((tcp)->tcp_connp) ||		\
107 	CONN_INBOUND_POLICY_PRESENT((tcp)->tcp_connp)))
108 
109 #define	TCP_IPOPT_POLICY_V6(tcp)					\
110 	((tcp)->tcp_ipversion == IPV6_VERSION &&			\
111 	((tcp)->tcp_ip_hdr_len != IPV6_HDR_LEN ||			\
112 	CONN_OUTBOUND_POLICY_PRESENT_V6((tcp)->tcp_connp) ||		\
113 	CONN_INBOUND_POLICY_PRESENT_V6((tcp)->tcp_connp)))
114 
115 #define	TCP_LOOPBACK_IP(tcp)						\
116 	(TCP_IPOPT_POLICY_V4(tcp) || TCP_IPOPT_POLICY_V6(tcp) ||	\
117 	!CONN_IS_MD_FASTPATH((tcp)->tcp_connp))
118 
119 /*
120  * Setting this to false means we disable fusion altogether and
121  * loopback connections would go through the protocol paths.
122  */
123 boolean_t do_tcp_fusion = B_TRUE;
124 
125 /*
126  * Enabling this flag allows sockfs to retrieve data directly
127  * from a fused tcp endpoint using synchronous streams interface.
128  */
129 boolean_t do_tcp_direct_sockfs = B_TRUE;
130 
131 /*
132  * This is the minimum amount of outstanding writes allowed on
133  * a synchronous streams-enabled receiving endpoint before the
134  * sender gets flow-controlled.  Setting this value to 0 means
135  * that the data block limit is equivalent to the byte count
136  * limit, which essentially disables the check.
137  */
138 #define	TCP_FUSION_RCV_UNREAD_MIN	8
139 uint_t tcp_fusion_rcv_unread_min = TCP_FUSION_RCV_UNREAD_MIN;
140 
141 static void	tcp_fuse_syncstr_enable(tcp_t *);
142 static void	tcp_fuse_syncstr_disable(tcp_t *);
143 static void	strrput_sig(queue_t *, boolean_t);
144 
145 /*
146  * This routine gets called by the eager tcp upon changing state from
147  * SYN_RCVD to ESTABLISHED.  It fuses a direct path between itself
148  * and the active connect tcp such that the regular tcp processings
149  * may be bypassed under allowable circumstances.  Because the fusion
150  * requires both endpoints to be in the same squeue, it does not work
151  * for simultaneous active connects because there is no easy way to
152  * switch from one squeue to another once the connection is created.
153  * This is different from the eager tcp case where we assign it the
154  * same squeue as the one given to the active connect tcp during open.
155  */
156 void
157 tcp_fuse(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph)
158 {
159 	conn_t *peer_connp, *connp = tcp->tcp_connp;
160 	tcp_t *peer_tcp;
161 
162 	ASSERT(!tcp->tcp_fused);
163 	ASSERT(tcp->tcp_loopback);
164 	ASSERT(tcp->tcp_loopback_peer == NULL);
165 	/*
166 	 * We need to inherit q_hiwat of the listener tcp, but we can't
167 	 * really use tcp_listener since we get here after sending up
168 	 * T_CONN_IND and tcp_wput_accept() may be called independently,
169 	 * at which point tcp_listener is cleared; this is why we use
170 	 * tcp_saved_listener.  The listener itself is guaranteed to be
171 	 * around until tcp_accept_finish() is called on this eager --
172 	 * this won't happen until we're done since we're inside the
173 	 * eager's perimeter now.
174 	 */
175 	ASSERT(tcp->tcp_saved_listener != NULL);
176 
177 	/*
178 	 * Lookup peer endpoint; search for the remote endpoint having
179 	 * the reversed address-port quadruplet in ESTABLISHED state,
180 	 * which is guaranteed to be unique in the system.  Zone check
181 	 * is applied accordingly for loopback address, but not for
182 	 * local address since we want fusion to happen across Zones.
183 	 */
184 	if (tcp->tcp_ipversion == IPV4_VERSION) {
185 		peer_connp = ipcl_conn_tcp_lookup_reversed_ipv4(connp,
186 		    (ipha_t *)iphdr, tcph);
187 	} else {
188 		peer_connp = ipcl_conn_tcp_lookup_reversed_ipv6(connp,
189 		    (ip6_t *)iphdr, tcph);
190 	}
191 
192 	/*
193 	 * We can only proceed if peer exists, resides in the same squeue
194 	 * as our conn and is not raw-socket.  The squeue assignment of
195 	 * this eager tcp was done earlier at the time of SYN processing
196 	 * in ip_fanout_tcp{_v6}.  Note that similar squeues by itself
197 	 * doesn't guarantee a safe condition to fuse, hence we perform
198 	 * additional tests below.
199 	 */
200 	ASSERT(peer_connp == NULL || peer_connp != connp);
201 	if (peer_connp == NULL || peer_connp->conn_sqp != connp->conn_sqp ||
202 	    !IPCL_IS_TCP(peer_connp)) {
203 		if (peer_connp != NULL) {
204 			TCP_STAT(tcp_fusion_unqualified);
205 			CONN_DEC_REF(peer_connp);
206 		}
207 		return;
208 	}
209 	peer_tcp = peer_connp->conn_tcp;	/* active connect tcp */
210 
211 	ASSERT(peer_tcp != NULL && peer_tcp != tcp && !peer_tcp->tcp_fused);
212 	ASSERT(peer_tcp->tcp_loopback && peer_tcp->tcp_loopback_peer == NULL);
213 	ASSERT(peer_connp->conn_sqp == connp->conn_sqp);
214 
215 	/*
216 	 * Fuse the endpoints; we perform further checks against both
217 	 * tcp endpoints to ensure that a fusion is allowed to happen.
218 	 * In particular we bail out for non-simple TCP/IP or if IPsec/
219 	 * IPQoS policy/kernel SSL exists.
220 	 */
221 	if (!tcp->tcp_unfusable && !peer_tcp->tcp_unfusable &&
222 	    !TCP_LOOPBACK_IP(tcp) && !TCP_LOOPBACK_IP(peer_tcp) &&
223 	    tcp->tcp_kssl_ent == NULL &&
224 	    !IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN)) {
225 		mblk_t *mp;
226 		struct stroptions *stropt;
227 		queue_t *peer_rq = peer_tcp->tcp_rq;
228 
229 		ASSERT(!TCP_IS_DETACHED(peer_tcp) && peer_rq != NULL);
230 		ASSERT(tcp->tcp_fused_sigurg_mp == NULL);
231 		ASSERT(peer_tcp->tcp_fused_sigurg_mp == NULL);
232 		ASSERT(tcp->tcp_kssl_ctx == NULL);
233 
234 		/*
235 		 * We need to drain data on both endpoints during unfuse.
236 		 * If we need to send up SIGURG at the time of draining,
237 		 * we want to be sure that an mblk is readily available.
238 		 * This is why we pre-allocate the M_PCSIG mblks for both
239 		 * endpoints which will only be used during/after unfuse.
240 		 */
241 		if ((mp = allocb(1, BPRI_HI)) == NULL)
242 			goto failed;
243 
244 		tcp->tcp_fused_sigurg_mp = mp;
245 
246 		if ((mp = allocb(1, BPRI_HI)) == NULL)
247 			goto failed;
248 
249 		peer_tcp->tcp_fused_sigurg_mp = mp;
250 
251 		/* Allocate M_SETOPTS mblk */
252 		if ((mp = allocb(sizeof (*stropt), BPRI_HI)) == NULL)
253 			goto failed;
254 
255 		/* Fuse both endpoints */
256 		peer_tcp->tcp_loopback_peer = tcp;
257 		tcp->tcp_loopback_peer = peer_tcp;
258 		peer_tcp->tcp_fused = tcp->tcp_fused = B_TRUE;
259 
260 		/*
261 		 * We never use regular tcp paths in fusion and should
262 		 * therefore clear tcp_unsent on both endpoints.  Having
263 		 * them set to non-zero values means asking for trouble
264 		 * especially after unfuse, where we may end up sending
265 		 * through regular tcp paths which expect xmit_list and
266 		 * friends to be correctly setup.
267 		 */
268 		peer_tcp->tcp_unsent = tcp->tcp_unsent = 0;
269 
270 		tcp_timers_stop(tcp);
271 		tcp_timers_stop(peer_tcp);
272 
273 		/*
274 		 * At this point we are a detached eager tcp and therefore
275 		 * don't have a queue assigned to us until accept happens.
276 		 * In the mean time the peer endpoint may immediately send
277 		 * us data as soon as fusion is finished, and we need to be
278 		 * able to flow control it in case it sends down huge amount
279 		 * of data while we're still detached.  To prevent that we
280 		 * inherit the listener's q_hiwat value; this is temporary
281 		 * since we'll repeat the process in tcp_accept_finish().
282 		 */
283 		(void) tcp_fuse_set_rcv_hiwat(tcp,
284 		    tcp->tcp_saved_listener->tcp_rq->q_hiwat);
285 
286 		/*
287 		 * Set the stream head's write offset value to zero since we
288 		 * won't be needing any room for TCP/IP headers; tell it to
289 		 * not break up the writes (this would reduce the amount of
290 		 * work done by kmem); and configure our receive buffer.
291 		 * Note that we can only do this for the active connect tcp
292 		 * since our eager is still detached; it will be dealt with
293 		 * later in tcp_accept_finish().
294 		 */
295 		DB_TYPE(mp) = M_SETOPTS;
296 		mp->b_wptr += sizeof (*stropt);
297 
298 		stropt = (struct stroptions *)mp->b_rptr;
299 		stropt->so_flags = SO_MAXBLK | SO_WROFF | SO_HIWAT;
300 		stropt->so_maxblk = tcp_maxpsz_set(peer_tcp, B_FALSE);
301 		stropt->so_wroff = 0;
302 
303 		/*
304 		 * Record the stream head's high water mark for
305 		 * peer endpoint; this is used for flow-control
306 		 * purposes in tcp_fuse_output().
307 		 */
308 		stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(peer_tcp,
309 		    peer_rq->q_hiwat);
310 
311 		/* Send the options up */
312 		putnext(peer_rq, mp);
313 	} else {
314 		TCP_STAT(tcp_fusion_unqualified);
315 	}
316 	CONN_DEC_REF(peer_connp);
317 	return;
318 
319 failed:
320 	if (tcp->tcp_fused_sigurg_mp != NULL) {
321 		freeb(tcp->tcp_fused_sigurg_mp);
322 		tcp->tcp_fused_sigurg_mp = NULL;
323 	}
324 	if (peer_tcp->tcp_fused_sigurg_mp != NULL) {
325 		freeb(peer_tcp->tcp_fused_sigurg_mp);
326 		peer_tcp->tcp_fused_sigurg_mp = NULL;
327 	}
328 	CONN_DEC_REF(peer_connp);
329 }
330 
331 /*
332  * Unfuse a previously-fused pair of tcp loopback endpoints.
333  */
334 void
335 tcp_unfuse(tcp_t *tcp)
336 {
337 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
338 
339 	ASSERT(tcp->tcp_fused && peer_tcp != NULL);
340 	ASSERT(peer_tcp->tcp_fused && peer_tcp->tcp_loopback_peer == tcp);
341 	ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp);
342 	ASSERT(tcp->tcp_unsent == 0 && peer_tcp->tcp_unsent == 0);
343 	ASSERT(tcp->tcp_fused_sigurg_mp != NULL);
344 	ASSERT(peer_tcp->tcp_fused_sigurg_mp != NULL);
345 
346 	/*
347 	 * We disable synchronous streams, drain any queued data and
348 	 * clear tcp_direct_sockfs.  The synchronous streams entry
349 	 * points will become no-ops after this point.
350 	 */
351 	tcp_fuse_disable_pair(tcp, B_TRUE);
352 
353 	/*
354 	 * Update th_seq and th_ack in the header template
355 	 */
356 	U32_TO_ABE32(tcp->tcp_snxt, tcp->tcp_tcph->th_seq);
357 	U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
358 	U32_TO_ABE32(peer_tcp->tcp_snxt, peer_tcp->tcp_tcph->th_seq);
359 	U32_TO_ABE32(peer_tcp->tcp_rnxt, peer_tcp->tcp_tcph->th_ack);
360 
361 	/* Unfuse the endpoints */
362 	peer_tcp->tcp_fused = tcp->tcp_fused = B_FALSE;
363 	peer_tcp->tcp_loopback_peer = tcp->tcp_loopback_peer = NULL;
364 }
365 
366 /*
367  * Fusion output routine for urgent data.  This routine is called by
368  * tcp_fuse_output() for handling non-M_DATA mblks.
369  */
370 void
371 tcp_fuse_output_urg(tcp_t *tcp, mblk_t *mp)
372 {
373 	mblk_t *mp1;
374 	struct T_exdata_ind *tei;
375 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
376 	mblk_t *head, *prev_head = NULL;
377 
378 	ASSERT(tcp->tcp_fused);
379 	ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp);
380 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
381 	ASSERT(mp->b_cont != NULL && DB_TYPE(mp->b_cont) == M_DATA);
382 	ASSERT(MBLKL(mp) >= sizeof (*tei) && MBLKL(mp->b_cont) > 0);
383 
384 	/*
385 	 * Urgent data arrives in the form of T_EXDATA_REQ from above.
386 	 * Each occurence denotes a new urgent pointer.  For each new
387 	 * urgent pointer we signal (SIGURG) the receiving app to indicate
388 	 * that it needs to go into urgent mode.  This is similar to the
389 	 * urgent data handling in the regular tcp.  We don't need to keep
390 	 * track of where the urgent pointer is, because each T_EXDATA_REQ
391 	 * "advances" the urgent pointer for us.
392 	 *
393 	 * The actual urgent data carried by T_EXDATA_REQ is then prepended
394 	 * by a T_EXDATA_IND before being enqueued behind any existing data
395 	 * destined for the receiving app.  There is only a single urgent
396 	 * pointer (out-of-band mark) for a given tcp.  If the new urgent
397 	 * data arrives before the receiving app reads some existing urgent
398 	 * data, the previous marker is lost.  This behavior is emulated
399 	 * accordingly below, by removing any existing T_EXDATA_IND messages
400 	 * and essentially converting old urgent data into non-urgent.
401 	 */
402 	ASSERT(tcp->tcp_valid_bits & TCP_URG_VALID);
403 	/* Let sender get out of urgent mode */
404 	tcp->tcp_valid_bits &= ~TCP_URG_VALID;
405 
406 	/*
407 	 * This flag indicates that a signal needs to be sent up.
408 	 * This flag will only get cleared once SIGURG is delivered and
409 	 * is not affected by the tcp_fused flag -- delivery will still
410 	 * happen even after an endpoint is unfused, to handle the case
411 	 * where the sending endpoint immediately closes/unfuses after
412 	 * sending urgent data and the accept is not yet finished.
413 	 */
414 	peer_tcp->tcp_fused_sigurg = B_TRUE;
415 
416 	/* Reuse T_EXDATA_REQ mblk for T_EXDATA_IND */
417 	DB_TYPE(mp) = M_PROTO;
418 	tei = (struct T_exdata_ind *)mp->b_rptr;
419 	tei->PRIM_type = T_EXDATA_IND;
420 	tei->MORE_flag = 0;
421 	mp->b_wptr = (uchar_t *)&tei[1];
422 
423 	TCP_STAT(tcp_fusion_urg);
424 	BUMP_MIB(&tcp_mib, tcpOutUrg);
425 
426 	head = peer_tcp->tcp_rcv_list;
427 	while (head != NULL) {
428 		/*
429 		 * Remove existing T_EXDATA_IND, keep the data which follows
430 		 * it and relink our list.  Note that we don't modify the
431 		 * tcp_rcv_last_tail since it never points to T_EXDATA_IND.
432 		 */
433 		if (DB_TYPE(head) != M_DATA) {
434 			mp1 = head;
435 
436 			ASSERT(DB_TYPE(mp1->b_cont) == M_DATA);
437 			head = mp1->b_cont;
438 			mp1->b_cont = NULL;
439 			head->b_next = mp1->b_next;
440 			mp1->b_next = NULL;
441 			if (prev_head != NULL)
442 				prev_head->b_next = head;
443 			if (peer_tcp->tcp_rcv_list == mp1)
444 				peer_tcp->tcp_rcv_list = head;
445 			if (peer_tcp->tcp_rcv_last_head == mp1)
446 				peer_tcp->tcp_rcv_last_head = head;
447 			freeb(mp1);
448 		}
449 		prev_head = head;
450 		head = head->b_next;
451 	}
452 }
453 
454 /*
455  * Fusion output routine, called by tcp_output() and tcp_wput_proto().
456  */
457 boolean_t
458 tcp_fuse_output(tcp_t *tcp, mblk_t *mp, uint32_t send_size)
459 {
460 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
461 	uint_t max_unread;
462 	boolean_t flow_stopped;
463 	boolean_t urgent = (DB_TYPE(mp) != M_DATA);
464 
465 	ASSERT(tcp->tcp_fused);
466 	ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp);
467 	ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp);
468 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO ||
469 	    DB_TYPE(mp) == M_PCPROTO);
470 
471 	max_unread = peer_tcp->tcp_fuse_rcv_unread_hiwater;
472 
473 	/* If this connection requires IP, unfuse and use regular path */
474 	if (TCP_LOOPBACK_IP(tcp) || TCP_LOOPBACK_IP(peer_tcp) ||
475 	    IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN)) {
476 		TCP_STAT(tcp_fusion_aborted);
477 		tcp_unfuse(tcp);
478 		return (B_FALSE);
479 	}
480 
481 	if (send_size == 0) {
482 		freemsg(mp);
483 		return (B_TRUE);
484 	}
485 
486 	/*
487 	 * Handle urgent data; we either send up SIGURG to the peer now
488 	 * or do it later when we drain, in case the peer is detached
489 	 * or if we're short of memory for M_PCSIG mblk.
490 	 */
491 	if (urgent) {
492 		/*
493 		 * We stop synchronous streams when we have urgent data
494 		 * queued to prevent tcp_fuse_rrw() from pulling it.  If
495 		 * for some reasons the urgent data can't be delivered
496 		 * below, synchronous streams will remain stopped until
497 		 * someone drains the tcp_rcv_list.
498 		 */
499 		TCP_FUSE_SYNCSTR_STOP(peer_tcp);
500 		tcp_fuse_output_urg(tcp, mp);
501 	}
502 
503 	mutex_enter(&peer_tcp->tcp_fuse_lock);
504 	/*
505 	 * Wake up and signal the peer; it is okay to do this before
506 	 * enqueueing because we are holding the lock.  One of the
507 	 * advantages of synchronous streams is the ability for us to
508 	 * find out when the application performs a read on the socket,
509 	 * by way of tcp_fuse_rrw() entry point being called.  Every
510 	 * data that gets enqueued onto the receiver is treated as if
511 	 * it has arrived at the receiving endpoint, thus generating
512 	 * SIGPOLL/SIGIO for asynchronous socket just as in the strrput()
513 	 * case.  However, we only wake up the application when necessary,
514 	 * i.e. during the first enqueue.  When tcp_fuse_rrw() is called
515 	 * it will send everything upstream.
516 	 */
517 	if (peer_tcp->tcp_direct_sockfs && !urgent &&
518 	    !TCP_IS_DETACHED(peer_tcp)) {
519 		if (peer_tcp->tcp_rcv_list == NULL)
520 			STR_WAKEUP_SET(STREAM(peer_tcp->tcp_rq));
521 		/* Update poll events and send SIGPOLL/SIGIO if necessary */
522 		STR_SENDSIG(STREAM(peer_tcp->tcp_rq));
523 	}
524 
525 	/*
526 	 * Enqueue data into the peer's receive list; we may or may not
527 	 * drain the contents depending on the conditions below.
528 	 */
529 	tcp_rcv_enqueue(peer_tcp, mp, send_size);
530 
531 	/* In case it wrapped around and also to keep it constant */
532 	peer_tcp->tcp_rwnd += send_size;
533 
534 	/*
535 	 * Exercise flow-control when needed; we will get back-enabled
536 	 * in either tcp_accept_finish(), tcp_unfuse(), or tcp_fuse_rrw().
537 	 * If tcp_direct_sockfs is on or if the peer endpoint is detached,
538 	 * we emulate streams flow control by checking the peer's queue
539 	 * size and high water mark; otherwise we simply use canputnext()
540 	 * to decide if we need to stop our flow.
541 	 *
542 	 * The outstanding unread data block check does not apply for a
543 	 * detached receiver; this is to avoid unnecessary blocking of the
544 	 * sender while the accept is currently in progress and is quite
545 	 * similar to the regular tcp.
546 	 */
547 	if (TCP_IS_DETACHED(peer_tcp) || max_unread == 0)
548 		max_unread = UINT_MAX;
549 
550 	flow_stopped = tcp->tcp_flow_stopped;
551 	if (!flow_stopped &&
552 	    (((peer_tcp->tcp_direct_sockfs || TCP_IS_DETACHED(peer_tcp)) &&
553 	    (peer_tcp->tcp_rcv_cnt >= peer_tcp->tcp_fuse_rcv_hiwater ||
554 	    ++peer_tcp->tcp_fuse_rcv_unread_cnt >= max_unread)) ||
555 	    (!peer_tcp->tcp_direct_sockfs &&
556 	    !TCP_IS_DETACHED(peer_tcp) && !canputnext(peer_tcp->tcp_rq)))) {
557 		tcp_setqfull(tcp);
558 		flow_stopped = B_TRUE;
559 		TCP_STAT(tcp_fusion_flowctl);
560 		DTRACE_PROBE4(tcp__fuse__output__flowctl, tcp_t *, tcp,
561 		    uint_t, send_size, uint_t, peer_tcp->tcp_rcv_cnt,
562 		    uint_t, peer_tcp->tcp_fuse_rcv_unread_cnt);
563 	} else if (flow_stopped &&
564 	    TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) {
565 		tcp_clrqfull(tcp);
566 	}
567 
568 	loopback_packets++;
569 	tcp->tcp_last_sent_len = send_size;
570 
571 	/* Need to adjust the following SNMP MIB-related variables */
572 	tcp->tcp_snxt += send_size;
573 	tcp->tcp_suna = tcp->tcp_snxt;
574 	peer_tcp->tcp_rnxt += send_size;
575 	peer_tcp->tcp_rack = peer_tcp->tcp_rnxt;
576 
577 	BUMP_MIB(&tcp_mib, tcpOutDataSegs);
578 	UPDATE_MIB(&tcp_mib, tcpOutDataBytes, send_size);
579 
580 	BUMP_MIB(&tcp_mib, tcpInSegs);
581 	BUMP_MIB(&tcp_mib, tcpInDataInorderSegs);
582 	UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, send_size);
583 
584 	BUMP_LOCAL(tcp->tcp_obsegs);
585 	BUMP_LOCAL(peer_tcp->tcp_ibsegs);
586 
587 	mutex_exit(&peer_tcp->tcp_fuse_lock);
588 
589 	DTRACE_PROBE2(tcp__fuse__output, tcp_t *, tcp, uint_t, send_size);
590 
591 	if (!TCP_IS_DETACHED(peer_tcp)) {
592 		/*
593 		 * Drain the peer's receive queue it has urgent data or if
594 		 * we're not flow-controlled.  There is no need for draining
595 		 * normal data when tcp_direct_sockfs is on because the peer
596 		 * will pull the data via tcp_fuse_rrw().
597 		 */
598 		if (urgent || (!flow_stopped && !peer_tcp->tcp_direct_sockfs)) {
599 			ASSERT(peer_tcp->tcp_rcv_list != NULL);
600 			/*
601 			 * For TLI-based streams, a thread in tcp_accept_swap()
602 			 * can race with us.  That thread will ensure that the
603 			 * correct peer_tcp->tcp_rq is globally visible before
604 			 * peer_tcp->tcp_detached is visible as clear, but we
605 			 * must also ensure that the load of tcp_rq cannot be
606 			 * reordered to be before the tcp_detached check.
607 			 */
608 			membar_consumer();
609 			(void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp,
610 			    NULL);
611 			/*
612 			 * If synchronous streams was stopped above due
613 			 * to the presence of urgent data, re-enable it.
614 			 */
615 			if (urgent)
616 				TCP_FUSE_SYNCSTR_RESUME(peer_tcp);
617 		}
618 	}
619 	return (B_TRUE);
620 }
621 
622 /*
623  * This routine gets called to deliver data upstream on a fused or
624  * previously fused tcp loopback endpoint; the latter happens only
625  * when there is a pending SIGURG signal plus urgent data that can't
626  * be sent upstream in the past.
627  */
628 boolean_t
629 tcp_fuse_rcv_drain(queue_t *q, tcp_t *tcp, mblk_t **sigurg_mpp)
630 {
631 	mblk_t *mp;
632 #ifdef DEBUG
633 	uint_t cnt = 0;
634 #endif
635 
636 	ASSERT(tcp->tcp_loopback);
637 	ASSERT(tcp->tcp_fused || tcp->tcp_fused_sigurg);
638 	ASSERT(!tcp->tcp_fused || tcp->tcp_loopback_peer != NULL);
639 	ASSERT(sigurg_mpp != NULL || tcp->tcp_fused);
640 
641 	/* No need for the push timer now, in case it was scheduled */
642 	if (tcp->tcp_push_tid != 0) {
643 		(void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid);
644 		tcp->tcp_push_tid = 0;
645 	}
646 	/*
647 	 * If there's urgent data sitting in receive list and we didn't
648 	 * get a chance to send up a SIGURG signal, make sure we send
649 	 * it first before draining in order to ensure that SIOCATMARK
650 	 * works properly.
651 	 */
652 	if (tcp->tcp_fused_sigurg) {
653 		/*
654 		 * sigurg_mpp is normally NULL, i.e. when we're still
655 		 * fused and didn't get here because of tcp_unfuse().
656 		 * In this case try hard to allocate the M_PCSIG mblk.
657 		 */
658 		if (sigurg_mpp == NULL &&
659 		    (mp = allocb(1, BPRI_HI)) == NULL &&
660 		    (mp = allocb_tryhard(1)) == NULL) {
661 			/* Alloc failed; try again next time */
662 			tcp->tcp_push_tid = TCP_TIMER(tcp, tcp_push_timer,
663 			    MSEC_TO_TICK(tcp_push_timer_interval));
664 			return (B_TRUE);
665 		} else if (sigurg_mpp != NULL) {
666 			/*
667 			 * Use the supplied M_PCSIG mblk; it means we're
668 			 * either unfused or in the process of unfusing,
669 			 * and the drain must happen now.
670 			 */
671 			mp = *sigurg_mpp;
672 			*sigurg_mpp = NULL;
673 		}
674 		ASSERT(mp != NULL);
675 
676 		tcp->tcp_fused_sigurg = B_FALSE;
677 		/* Send up the signal */
678 		DB_TYPE(mp) = M_PCSIG;
679 		*mp->b_wptr++ = (uchar_t)SIGURG;
680 		putnext(q, mp);
681 		/*
682 		 * Let the regular tcp_rcv_drain() path handle
683 		 * draining the data if we're no longer fused.
684 		 */
685 		if (!tcp->tcp_fused)
686 			return (B_FALSE);
687 	}
688 
689 	/*
690 	 * In the synchronous streams case, we generate SIGPOLL/SIGIO for
691 	 * each M_DATA that gets enqueued onto the receiver.  At this point
692 	 * we are about to drain any queued data via putnext().  In order
693 	 * to avoid extraneous signal generation from strrput(), we set
694 	 * STRGETINPROG flag at the stream head prior to the draining and
695 	 * restore it afterwards.  This masks out signal generation only
696 	 * for M_DATA messages and does not affect urgent data.
697 	 */
698 	if (tcp->tcp_direct_sockfs)
699 		strrput_sig(q, B_FALSE);
700 
701 	/* Drain the data */
702 	while ((mp = tcp->tcp_rcv_list) != NULL) {
703 		tcp->tcp_rcv_list = mp->b_next;
704 		mp->b_next = NULL;
705 #ifdef DEBUG
706 		cnt += msgdsize(mp);
707 #endif
708 		putnext(q, mp);
709 		TCP_STAT(tcp_fusion_putnext);
710 	}
711 
712 	if (tcp->tcp_direct_sockfs)
713 		strrput_sig(q, B_TRUE);
714 
715 	ASSERT(cnt == tcp->tcp_rcv_cnt);
716 	tcp->tcp_rcv_last_head = NULL;
717 	tcp->tcp_rcv_last_tail = NULL;
718 	tcp->tcp_rcv_cnt = 0;
719 	tcp->tcp_fuse_rcv_unread_cnt = 0;
720 	tcp->tcp_rwnd = q->q_hiwat;
721 
722 	return (B_TRUE);
723 }
724 
725 /*
726  * Synchronous stream entry point for sockfs to retrieve
727  * data directly from tcp_rcv_list.
728  */
729 int
730 tcp_fuse_rrw(queue_t *q, struiod_t *dp)
731 {
732 	tcp_t *tcp = Q_TO_CONN(q)->conn_tcp;
733 	mblk_t *mp;
734 
735 	mutex_enter(&tcp->tcp_fuse_lock);
736 	/*
737 	 * If someone had turned off tcp_direct_sockfs or if synchronous
738 	 * streams is temporarily disabled, we return EBUSY.  This causes
739 	 * strget() to dequeue data from the stream head instead.
740 	 */
741 	if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped) {
742 		mutex_exit(&tcp->tcp_fuse_lock);
743 		TCP_STAT(tcp_fusion_rrw_busy);
744 		return (EBUSY);
745 	}
746 
747 	if ((mp = tcp->tcp_rcv_list) != NULL) {
748 		tcp_t *peer_tcp = tcp->tcp_loopback_peer;
749 
750 		DTRACE_PROBE3(tcp__fuse__rrw, tcp_t *, tcp,
751 		    uint32_t, tcp->tcp_rcv_cnt, ssize_t, dp->d_uio.uio_resid);
752 
753 		tcp->tcp_rcv_list = NULL;
754 		TCP_STAT(tcp_fusion_rrw_msgcnt);
755 
756 		/*
757 		 * At this point nothing should be left in tcp_rcv_list.
758 		 * The only possible case where we would have a chain of
759 		 * b_next-linked messages is urgent data, but we wouldn't
760 		 * be here if that's true since urgent data is delivered
761 		 * via putnext() and synchronous streams is stopped until
762 		 * tcp_fuse_rcv_drain() is finished.
763 		 */
764 		ASSERT(DB_TYPE(mp) == M_DATA && mp->b_next == NULL);
765 
766 		tcp->tcp_rcv_last_head = NULL;
767 		tcp->tcp_rcv_last_tail = NULL;
768 		tcp->tcp_rcv_cnt = 0;
769 		tcp->tcp_fuse_rcv_unread_cnt = 0;
770 
771 		if (peer_tcp->tcp_flow_stopped) {
772 			tcp_clrqfull(peer_tcp);
773 			TCP_STAT(tcp_fusion_backenabled);
774 		}
775 	}
776 
777 	/*
778 	 * Either we just dequeued everything or we get here from sockfs
779 	 * and have nothing to return; in this case clear RSLEEP.
780 	 */
781 	ASSERT(tcp->tcp_rcv_last_head == NULL);
782 	ASSERT(tcp->tcp_rcv_last_tail == NULL);
783 	ASSERT(tcp->tcp_rcv_cnt == 0);
784 	ASSERT(tcp->tcp_fuse_rcv_unread_cnt == 0);
785 	STR_WAKEUP_CLEAR(STREAM(q));
786 
787 	mutex_exit(&tcp->tcp_fuse_lock);
788 	dp->d_mp = mp;
789 	return (0);
790 }
791 
792 /*
793  * Synchronous stream entry point used by certain ioctls to retrieve
794  * information about or peek into the tcp_rcv_list.
795  */
796 int
797 tcp_fuse_rinfop(queue_t *q, infod_t *dp)
798 {
799 	tcp_t	*tcp = Q_TO_CONN(q)->conn_tcp;
800 	mblk_t	*mp;
801 	uint_t	cmd = dp->d_cmd;
802 	int	res = 0;
803 	int	error = 0;
804 	struct stdata *stp = STREAM(q);
805 
806 	mutex_enter(&tcp->tcp_fuse_lock);
807 	/* If shutdown on read has happened, return nothing */
808 	mutex_enter(&stp->sd_lock);
809 	if (stp->sd_flag & STREOF) {
810 		mutex_exit(&stp->sd_lock);
811 		goto done;
812 	}
813 	mutex_exit(&stp->sd_lock);
814 
815 	/*
816 	 * It is OK not to return an answer if tcp_rcv_list is
817 	 * currently not accessible.
818 	 */
819 	if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped ||
820 	    (mp = tcp->tcp_rcv_list) == NULL)
821 		goto done;
822 
823 	if (cmd & INFOD_COUNT) {
824 		/*
825 		 * We have at least one message and
826 		 * could return only one at a time.
827 		 */
828 		dp->d_count++;
829 		res |= INFOD_COUNT;
830 	}
831 	if (cmd & INFOD_BYTES) {
832 		/*
833 		 * Return size of all data messages.
834 		 */
835 		dp->d_bytes += tcp->tcp_rcv_cnt;
836 		res |= INFOD_BYTES;
837 	}
838 	if (cmd & INFOD_FIRSTBYTES) {
839 		/*
840 		 * Return size of first data message.
841 		 */
842 		dp->d_bytes = msgdsize(mp);
843 		res |= INFOD_FIRSTBYTES;
844 		dp->d_cmd &= ~INFOD_FIRSTBYTES;
845 	}
846 	if (cmd & INFOD_COPYOUT) {
847 		mblk_t *mp1;
848 		int n;
849 
850 		if (DB_TYPE(mp) == M_DATA) {
851 			mp1 = mp;
852 		} else {
853 			mp1 = mp->b_cont;
854 			ASSERT(mp1 != NULL);
855 		}
856 
857 		/*
858 		 * Return data contents of first message.
859 		 */
860 		ASSERT(DB_TYPE(mp1) == M_DATA);
861 		while (mp1 != NULL && dp->d_uiop->uio_resid > 0) {
862 			n = MIN(dp->d_uiop->uio_resid, MBLKL(mp1));
863 			if (n != 0 && (error = uiomove((char *)mp1->b_rptr, n,
864 			    UIO_READ, dp->d_uiop)) != 0) {
865 				goto done;
866 			}
867 			mp1 = mp1->b_cont;
868 		}
869 		res |= INFOD_COPYOUT;
870 		dp->d_cmd &= ~INFOD_COPYOUT;
871 	}
872 done:
873 	mutex_exit(&tcp->tcp_fuse_lock);
874 
875 	dp->d_res |= res;
876 
877 	return (error);
878 }
879 
880 /*
881  * Enable synchronous streams on a fused tcp loopback endpoint.
882  */
883 static void
884 tcp_fuse_syncstr_enable(tcp_t *tcp)
885 {
886 	queue_t *rq = tcp->tcp_rq;
887 	struct stdata *stp = STREAM(rq);
888 
889 	/* We can only enable synchronous streams for sockfs mode */
890 	tcp->tcp_direct_sockfs = tcp->tcp_issocket && do_tcp_direct_sockfs;
891 
892 	if (!tcp->tcp_direct_sockfs)
893 		return;
894 
895 	mutex_enter(&stp->sd_lock);
896 	mutex_enter(QLOCK(rq));
897 
898 	/*
899 	 * We replace our q_qinfo with one that has the qi_rwp entry point.
900 	 * Clear SR_SIGALLDATA because we generate the equivalent signal(s)
901 	 * for every enqueued data in tcp_fuse_output().
902 	 */
903 	rq->q_qinfo = &tcp_loopback_rinit;
904 	rq->q_struiot = tcp_loopback_rinit.qi_struiot;
905 	stp->sd_struiordq = rq;
906 	stp->sd_rput_opt &= ~SR_SIGALLDATA;
907 
908 	mutex_exit(QLOCK(rq));
909 	mutex_exit(&stp->sd_lock);
910 }
911 
912 /*
913  * Disable synchronous streams on a fused tcp loopback endpoint.
914  */
915 static void
916 tcp_fuse_syncstr_disable(tcp_t *tcp)
917 {
918 	queue_t *rq = tcp->tcp_rq;
919 	struct stdata *stp = STREAM(rq);
920 
921 	if (!tcp->tcp_direct_sockfs)
922 		return;
923 
924 	mutex_enter(&stp->sd_lock);
925 	mutex_enter(QLOCK(rq));
926 
927 	/*
928 	 * Reset q_qinfo to point to the default tcp entry points.
929 	 * Also restore SR_SIGALLDATA so that strrput() can generate
930 	 * the signals again for future M_DATA messages.
931 	 */
932 	rq->q_qinfo = &tcp_rinit;
933 	rq->q_struiot = tcp_rinit.qi_struiot;
934 	stp->sd_struiordq = NULL;
935 	stp->sd_rput_opt |= SR_SIGALLDATA;
936 	tcp->tcp_direct_sockfs = B_FALSE;
937 
938 	mutex_exit(QLOCK(rq));
939 	mutex_exit(&stp->sd_lock);
940 }
941 
942 /*
943  * Enable synchronous streams on a pair of fused tcp endpoints.
944  */
945 void
946 tcp_fuse_syncstr_enable_pair(tcp_t *tcp)
947 {
948 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
949 
950 	ASSERT(tcp->tcp_fused);
951 	ASSERT(peer_tcp != NULL);
952 
953 	tcp_fuse_syncstr_enable(tcp);
954 	tcp_fuse_syncstr_enable(peer_tcp);
955 }
956 
957 /*
958  * Allow or disallow signals to be generated by strrput().
959  */
960 static void
961 strrput_sig(queue_t *q, boolean_t on)
962 {
963 	struct stdata *stp = STREAM(q);
964 
965 	mutex_enter(&stp->sd_lock);
966 	if (on)
967 		stp->sd_flag &= ~STRGETINPROG;
968 	else
969 		stp->sd_flag |= STRGETINPROG;
970 	mutex_exit(&stp->sd_lock);
971 }
972 
973 /*
974  * Disable synchronous streams on a pair of fused tcp endpoints and drain
975  * any queued data; called either during unfuse or upon transitioning from
976  * a socket to a stream endpoint due to _SIOCSOCKFALLBACK.
977  */
978 void
979 tcp_fuse_disable_pair(tcp_t *tcp, boolean_t unfusing)
980 {
981 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
982 
983 	ASSERT(tcp->tcp_fused);
984 	ASSERT(peer_tcp != NULL);
985 
986 	/*
987 	 * We need to prevent tcp_fuse_rrw() from entering before
988 	 * we can disable synchronous streams.
989 	 */
990 	TCP_FUSE_SYNCSTR_STOP(tcp);
991 	TCP_FUSE_SYNCSTR_STOP(peer_tcp);
992 
993 	/*
994 	 * Drain any pending data; the detached check is needed because
995 	 * we may be called as a result of a tcp_unfuse() triggered by
996 	 * tcp_fuse_output().  Note that in case of a detached tcp, the
997 	 * draining will happen later after the tcp is unfused.  For non-
998 	 * urgent data, this can be handled by the regular tcp_rcv_drain().
999 	 * If we have urgent data sitting in the receive list, we will
1000 	 * need to send up a SIGURG signal first before draining the data.
1001 	 * All of these will be handled by the code in tcp_fuse_rcv_drain()
1002 	 * when called from tcp_rcv_drain().
1003 	 */
1004 	if (!TCP_IS_DETACHED(tcp)) {
1005 		(void) tcp_fuse_rcv_drain(tcp->tcp_rq, tcp,
1006 		    (unfusing ? &tcp->tcp_fused_sigurg_mp : NULL));
1007 	}
1008 	if (!TCP_IS_DETACHED(peer_tcp)) {
1009 		(void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp,
1010 		    (unfusing ? &peer_tcp->tcp_fused_sigurg_mp : NULL));
1011 	}
1012 
1013 	/* Lift up any flow-control conditions */
1014 	if (tcp->tcp_flow_stopped) {
1015 		tcp_clrqfull(tcp);
1016 		TCP_STAT(tcp_fusion_backenabled);
1017 	}
1018 	if (peer_tcp->tcp_flow_stopped) {
1019 		tcp_clrqfull(peer_tcp);
1020 		TCP_STAT(tcp_fusion_backenabled);
1021 	}
1022 
1023 	/* Disable synchronous streams */
1024 	tcp_fuse_syncstr_disable(tcp);
1025 	tcp_fuse_syncstr_disable(peer_tcp);
1026 }
1027 
1028 /*
1029  * Calculate the size of receive buffer for a fused tcp endpoint.
1030  */
1031 size_t
1032 tcp_fuse_set_rcv_hiwat(tcp_t *tcp, size_t rwnd)
1033 {
1034 	ASSERT(tcp->tcp_fused);
1035 
1036 	/* Ensure that value is within the maximum upper bound */
1037 	if (rwnd > tcp_max_buf)
1038 		rwnd = tcp_max_buf;
1039 
1040 	/* Obey the absolute minimum tcp receive high water mark */
1041 	if (rwnd < tcp_sth_rcv_hiwat)
1042 		rwnd = tcp_sth_rcv_hiwat;
1043 
1044 	/*
1045 	 * Round up to system page size in case SO_RCVBUF is modified
1046 	 * after SO_SNDBUF; the latter is also similarly rounded up.
1047 	 */
1048 	rwnd = P2ROUNDUP_TYPED(rwnd, PAGESIZE, size_t);
1049 	tcp->tcp_fuse_rcv_hiwater = rwnd;
1050 	return (rwnd);
1051 }
1052 
1053 /*
1054  * Calculate the maximum outstanding unread data block for a fused tcp endpoint.
1055  */
1056 int
1057 tcp_fuse_maxpsz_set(tcp_t *tcp)
1058 {
1059 	tcp_t *peer_tcp = tcp->tcp_loopback_peer;
1060 	uint_t sndbuf = tcp->tcp_xmit_hiwater;
1061 	uint_t maxpsz = sndbuf;
1062 
1063 	ASSERT(tcp->tcp_fused);
1064 	ASSERT(peer_tcp != NULL);
1065 	ASSERT(peer_tcp->tcp_fuse_rcv_hiwater != 0);
1066 	/*
1067 	 * In the fused loopback case, we want the stream head to split
1068 	 * up larger writes into smaller chunks for a more accurate flow-
1069 	 * control accounting.  Our maxpsz is half of the sender's send
1070 	 * buffer or the receiver's receive buffer, whichever is smaller.
1071 	 * We round up the buffer to system page size due to the lack of
1072 	 * TCP MSS concept in Fusion.
1073 	 */
1074 	if (maxpsz > peer_tcp->tcp_fuse_rcv_hiwater)
1075 		maxpsz = peer_tcp->tcp_fuse_rcv_hiwater;
1076 	maxpsz = P2ROUNDUP_TYPED(maxpsz, PAGESIZE, uint_t) >> 1;
1077 
1078 	/*
1079 	 * Calculate the peer's limit for the number of outstanding unread
1080 	 * data block.  This is the amount of data blocks that are allowed
1081 	 * to reside in the receiver's queue before the sender gets flow
1082 	 * controlled.  It is used only in the synchronous streams mode as
1083 	 * a way to throttle the sender when it performs consecutive writes
1084 	 * faster than can be read.  The value is derived from SO_SNDBUF in
1085 	 * order to give the sender some control; we divide it with a large
1086 	 * value (16KB) to produce a fairly low initial limit.
1087 	 */
1088 	if (tcp_fusion_rcv_unread_min == 0) {
1089 		/* A value of 0 means that we disable the check */
1090 		peer_tcp->tcp_fuse_rcv_unread_hiwater = 0;
1091 	} else {
1092 		peer_tcp->tcp_fuse_rcv_unread_hiwater =
1093 		    MAX(sndbuf >> 14, tcp_fusion_rcv_unread_min);
1094 	}
1095 	return (maxpsz);
1096 }
1097