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 #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/sdt.h> 34 #include <sys/cmn_err.h> 35 #include <sys/tihdr.h> 36 37 #include <inet/common.h> 38 #include <inet/optcom.h> 39 #include <inet/ip.h> 40 #include <inet/ip_impl.h> 41 #include <inet/tcp.h> 42 #include <inet/tcp_impl.h> 43 #include <inet/ipsec_impl.h> 44 #include <inet/ipclassifier.h> 45 #include <inet/ipp_common.h> 46 47 /* 48 * This file implements TCP fusion - a protocol-less data path for TCP 49 * loopback connections. The fusion of two local TCP endpoints occurs 50 * at connection establishment time. Various conditions (see details 51 * in tcp_fuse()) need to be met for fusion to be successful. If it 52 * fails, we fall back to the regular TCP data path; if it succeeds, 53 * both endpoints proceed to use tcp_fuse_output() as the transmit path. 54 * tcp_fuse_output() enqueues application data directly onto the peer's 55 * receive queue; no protocol processing is involved. After enqueueing 56 * the data, the sender can either push (putnext) data up the receiver's 57 * read queue; or the sender can simply return and let the receiver 58 * retrieve the enqueued data via the synchronous streams entry point 59 * tcp_fuse_rrw(). The latter path is taken if synchronous streams is 60 * enabled (the default). It is disabled if sockfs no longer resides 61 * directly on top of tcp module due to a module insertion or removal. 62 * It also needs to be temporarily disabled when sending urgent data 63 * because the tcp_fuse_rrw() path bypasses the M_PROTO processing done 64 * by strsock_proto() hook. 65 * 66 * Sychronization is handled by squeue and the mutex tcp_non_sq_lock. 67 * One of the requirements for fusion to succeed is that both endpoints 68 * need to be using the same squeue. This ensures that neither side 69 * can disappear while the other side is still sending data. By itself, 70 * squeue is not sufficient for guaranteeing safety when synchronous 71 * streams is enabled. The reason is that tcp_fuse_rrw() doesn't enter 72 * the squeue and its access to tcp_rcv_list and other fusion-related 73 * fields needs to be sychronized with the sender. tcp_non_sq_lock is 74 * used for this purpose. When there is urgent data, the sender needs 75 * to push the data up the receiver's streams read queue. In order to 76 * avoid holding the tcp_non_sq_lock across putnext(), the sender sets 77 * the peer tcp's tcp_fuse_syncstr_plugged bit and releases tcp_non_sq_lock 78 * (see macro TCP_FUSE_SYNCSTR_PLUG_DRAIN()). If tcp_fuse_rrw() enters 79 * after this point, it will see that synchronous streams is plugged and 80 * will wait on tcp_fuse_plugcv. After the sender has finished pushing up 81 * all urgent data, it will clear the tcp_fuse_syncstr_plugged bit using 82 * TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(). This will cause any threads waiting 83 * on tcp_fuse_plugcv to return EBUSY, and in turn cause strget() to call 84 * getq_noenab() to dequeue data from the stream head instead. Once the 85 * data on the stream head has been consumed, tcp_fuse_rrw() may again 86 * be used to process tcp_rcv_list. However, if TCP_FUSE_SYNCSTR_STOP() 87 * has been called, all future calls to tcp_fuse_rrw() will return EBUSY, 88 * effectively disabling synchronous streams. 89 * 90 * The following note applies only to the synchronous streams mode. 91 * 92 * Flow control is done by checking the size of receive buffer and 93 * the number of data blocks, both set to different limits. This is 94 * different than regular streams flow control where cumulative size 95 * check dominates block count check -- streams queue high water mark 96 * typically represents bytes. Each enqueue triggers notifications 97 * to the receiving process; a build up of data blocks indicates a 98 * slow receiver and the sender should be blocked or informed at the 99 * earliest moment instead of further wasting system resources. In 100 * effect, this is equivalent to limiting the number of outstanding 101 * segments in flight. 102 */ 103 104 /* 105 * Setting this to false means we disable fusion altogether and 106 * loopback connections would go through the protocol paths. 107 */ 108 boolean_t do_tcp_fusion = B_TRUE; 109 110 /* 111 * Enabling this flag allows sockfs to retrieve data directly 112 * from a fused tcp endpoint using synchronous streams interface. 113 */ 114 boolean_t do_tcp_direct_sockfs = B_TRUE; 115 116 /* 117 * This is the minimum amount of outstanding writes allowed on 118 * a synchronous streams-enabled receiving endpoint before the 119 * sender gets flow-controlled. Setting this value to 0 means 120 * that the data block limit is equivalent to the byte count 121 * limit, which essentially disables the check. 122 */ 123 #define TCP_FUSION_RCV_UNREAD_MIN 8 124 uint_t tcp_fusion_rcv_unread_min = TCP_FUSION_RCV_UNREAD_MIN; 125 126 static void tcp_fuse_syncstr_enable(tcp_t *); 127 static void tcp_fuse_syncstr_disable(tcp_t *); 128 static void strrput_sig(queue_t *, boolean_t); 129 130 /* 131 * Return true if this connection needs some IP functionality 132 */ 133 static boolean_t 134 tcp_loopback_needs_ip(tcp_t *tcp, netstack_t *ns) 135 { 136 ipsec_stack_t *ipss = ns->netstack_ipsec; 137 138 /* 139 * If ire is not cached, do not use fusion 140 */ 141 if (tcp->tcp_connp->conn_ire_cache == NULL) { 142 /* 143 * There is no need to hold conn_lock here because when called 144 * from tcp_fuse() there can be no window where conn_ire_cache 145 * can change. This is not true whe called from 146 * tcp_fuse_output(). conn_ire_cache can become null just 147 * after the check, but it's ok if a few packets are delivered 148 * in the fused state. 149 */ 150 return (B_TRUE); 151 } 152 if (tcp->tcp_ipversion == IPV4_VERSION) { 153 if (tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) 154 return (B_TRUE); 155 if (CONN_OUTBOUND_POLICY_PRESENT(tcp->tcp_connp, ipss)) 156 return (B_TRUE); 157 if (CONN_INBOUND_POLICY_PRESENT(tcp->tcp_connp, ipss)) 158 return (B_TRUE); 159 } else { 160 if (tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) 161 return (B_TRUE); 162 if (CONN_OUTBOUND_POLICY_PRESENT_V6(tcp->tcp_connp, ipss)) 163 return (B_TRUE); 164 if (CONN_INBOUND_POLICY_PRESENT_V6(tcp->tcp_connp, ipss)) 165 return (B_TRUE); 166 } 167 if (!CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)) 168 return (B_TRUE); 169 return (B_FALSE); 170 } 171 172 173 /* 174 * This routine gets called by the eager tcp upon changing state from 175 * SYN_RCVD to ESTABLISHED. It fuses a direct path between itself 176 * and the active connect tcp such that the regular tcp processings 177 * may be bypassed under allowable circumstances. Because the fusion 178 * requires both endpoints to be in the same squeue, it does not work 179 * for simultaneous active connects because there is no easy way to 180 * switch from one squeue to another once the connection is created. 181 * This is different from the eager tcp case where we assign it the 182 * same squeue as the one given to the active connect tcp during open. 183 */ 184 void 185 tcp_fuse(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph) 186 { 187 conn_t *peer_connp, *connp = tcp->tcp_connp; 188 tcp_t *peer_tcp; 189 tcp_stack_t *tcps = tcp->tcp_tcps; 190 netstack_t *ns; 191 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 192 193 ASSERT(!tcp->tcp_fused); 194 ASSERT(tcp->tcp_loopback); 195 ASSERT(tcp->tcp_loopback_peer == NULL); 196 /* 197 * We need to inherit q_hiwat of the listener tcp, but we can't 198 * really use tcp_listener since we get here after sending up 199 * T_CONN_IND and tcp_wput_accept() may be called independently, 200 * at which point tcp_listener is cleared; this is why we use 201 * tcp_saved_listener. The listener itself is guaranteed to be 202 * around until tcp_accept_finish() is called on this eager -- 203 * this won't happen until we're done since we're inside the 204 * eager's perimeter now. 205 */ 206 ASSERT(tcp->tcp_saved_listener != NULL); 207 208 /* 209 * Lookup peer endpoint; search for the remote endpoint having 210 * the reversed address-port quadruplet in ESTABLISHED state, 211 * which is guaranteed to be unique in the system. Zone check 212 * is applied accordingly for loopback address, but not for 213 * local address since we want fusion to happen across Zones. 214 */ 215 if (tcp->tcp_ipversion == IPV4_VERSION) { 216 peer_connp = ipcl_conn_tcp_lookup_reversed_ipv4(connp, 217 (ipha_t *)iphdr, tcph, ipst); 218 } else { 219 peer_connp = ipcl_conn_tcp_lookup_reversed_ipv6(connp, 220 (ip6_t *)iphdr, tcph, ipst); 221 } 222 223 /* 224 * We can only proceed if peer exists, resides in the same squeue 225 * as our conn and is not raw-socket. The squeue assignment of 226 * this eager tcp was done earlier at the time of SYN processing 227 * in ip_fanout_tcp{_v6}. Note that similar squeues by itself 228 * doesn't guarantee a safe condition to fuse, hence we perform 229 * additional tests below. 230 */ 231 ASSERT(peer_connp == NULL || peer_connp != connp); 232 if (peer_connp == NULL || peer_connp->conn_sqp != connp->conn_sqp || 233 !IPCL_IS_TCP(peer_connp)) { 234 if (peer_connp != NULL) { 235 TCP_STAT(tcps, tcp_fusion_unqualified); 236 CONN_DEC_REF(peer_connp); 237 } 238 return; 239 } 240 peer_tcp = peer_connp->conn_tcp; /* active connect tcp */ 241 242 ASSERT(peer_tcp != NULL && peer_tcp != tcp && !peer_tcp->tcp_fused); 243 ASSERT(peer_tcp->tcp_loopback && peer_tcp->tcp_loopback_peer == NULL); 244 ASSERT(peer_connp->conn_sqp == connp->conn_sqp); 245 246 /* 247 * Fuse the endpoints; we perform further checks against both 248 * tcp endpoints to ensure that a fusion is allowed to happen. 249 * In particular we bail out for non-simple TCP/IP or if IPsec/ 250 * IPQoS policy/kernel SSL exists. 251 */ 252 ns = tcps->tcps_netstack; 253 ipst = ns->netstack_ip; 254 255 if (!tcp->tcp_unfusable && !peer_tcp->tcp_unfusable && 256 !tcp_loopback_needs_ip(tcp, ns) && 257 !tcp_loopback_needs_ip(peer_tcp, ns) && 258 tcp->tcp_kssl_ent == NULL && 259 !IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst)) { 260 mblk_t *mp; 261 struct stroptions *stropt; 262 queue_t *peer_rq = peer_tcp->tcp_rq; 263 264 ASSERT(!TCP_IS_DETACHED(peer_tcp) && peer_rq != NULL); 265 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 266 ASSERT(peer_tcp->tcp_fused_sigurg_mp == NULL); 267 ASSERT(tcp->tcp_kssl_ctx == NULL); 268 269 /* 270 * We need to drain data on both endpoints during unfuse. 271 * If we need to send up SIGURG at the time of draining, 272 * we want to be sure that an mblk is readily available. 273 * This is why we pre-allocate the M_PCSIG mblks for both 274 * endpoints which will only be used during/after unfuse. 275 */ 276 if ((mp = allocb(1, BPRI_HI)) == NULL) 277 goto failed; 278 279 tcp->tcp_fused_sigurg_mp = mp; 280 281 if ((mp = allocb(1, BPRI_HI)) == NULL) 282 goto failed; 283 284 peer_tcp->tcp_fused_sigurg_mp = mp; 285 286 /* Allocate M_SETOPTS mblk */ 287 if ((mp = allocb(sizeof (*stropt), BPRI_HI)) == NULL) 288 goto failed; 289 290 /* If peer sodirect enabled then disable */ 291 ASSERT(tcp->tcp_sodirect == NULL); 292 if (peer_tcp->tcp_sodirect != NULL) { 293 mutex_enter(peer_tcp->tcp_sodirect->sod_lock); 294 SOD_DISABLE(peer_tcp->tcp_sodirect); 295 mutex_exit(peer_tcp->tcp_sodirect->sod_lock); 296 peer_tcp->tcp_sodirect = NULL; 297 } 298 299 /* Fuse both endpoints */ 300 peer_tcp->tcp_loopback_peer = tcp; 301 tcp->tcp_loopback_peer = peer_tcp; 302 peer_tcp->tcp_fused = tcp->tcp_fused = B_TRUE; 303 304 /* 305 * We never use regular tcp paths in fusion and should 306 * therefore clear tcp_unsent on both endpoints. Having 307 * them set to non-zero values means asking for trouble 308 * especially after unfuse, where we may end up sending 309 * through regular tcp paths which expect xmit_list and 310 * friends to be correctly setup. 311 */ 312 peer_tcp->tcp_unsent = tcp->tcp_unsent = 0; 313 314 tcp_timers_stop(tcp); 315 tcp_timers_stop(peer_tcp); 316 317 /* 318 * At this point we are a detached eager tcp and therefore 319 * don't have a queue assigned to us until accept happens. 320 * In the mean time the peer endpoint may immediately send 321 * us data as soon as fusion is finished, and we need to be 322 * able to flow control it in case it sends down huge amount 323 * of data while we're still detached. To prevent that we 324 * inherit the listener's q_hiwat value; this is temporary 325 * since we'll repeat the process in tcp_accept_finish(). 326 */ 327 (void) tcp_fuse_set_rcv_hiwat(tcp, 328 tcp->tcp_saved_listener->tcp_rq->q_hiwat); 329 330 /* 331 * Set the stream head's write offset value to zero since we 332 * won't be needing any room for TCP/IP headers; tell it to 333 * not break up the writes (this would reduce the amount of 334 * work done by kmem); and configure our receive buffer. 335 * Note that we can only do this for the active connect tcp 336 * since our eager is still detached; it will be dealt with 337 * later in tcp_accept_finish(). 338 */ 339 DB_TYPE(mp) = M_SETOPTS; 340 mp->b_wptr += sizeof (*stropt); 341 342 stropt = (struct stroptions *)mp->b_rptr; 343 stropt->so_flags = SO_MAXBLK | SO_WROFF | SO_HIWAT; 344 stropt->so_maxblk = tcp_maxpsz_set(peer_tcp, B_FALSE); 345 stropt->so_wroff = 0; 346 347 /* 348 * Record the stream head's high water mark for 349 * peer endpoint; this is used for flow-control 350 * purposes in tcp_fuse_output(). 351 */ 352 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(peer_tcp, 353 peer_rq->q_hiwat); 354 355 /* Send the options up */ 356 putnext(peer_rq, mp); 357 } else { 358 TCP_STAT(tcps, tcp_fusion_unqualified); 359 } 360 CONN_DEC_REF(peer_connp); 361 return; 362 363 failed: 364 if (tcp->tcp_fused_sigurg_mp != NULL) { 365 freeb(tcp->tcp_fused_sigurg_mp); 366 tcp->tcp_fused_sigurg_mp = NULL; 367 } 368 if (peer_tcp->tcp_fused_sigurg_mp != NULL) { 369 freeb(peer_tcp->tcp_fused_sigurg_mp); 370 peer_tcp->tcp_fused_sigurg_mp = NULL; 371 } 372 CONN_DEC_REF(peer_connp); 373 } 374 375 /* 376 * Unfuse a previously-fused pair of tcp loopback endpoints. 377 */ 378 void 379 tcp_unfuse(tcp_t *tcp) 380 { 381 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 382 383 ASSERT(tcp->tcp_fused && peer_tcp != NULL); 384 ASSERT(peer_tcp->tcp_fused && peer_tcp->tcp_loopback_peer == tcp); 385 ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp); 386 ASSERT(tcp->tcp_unsent == 0 && peer_tcp->tcp_unsent == 0); 387 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 388 ASSERT(peer_tcp->tcp_fused_sigurg_mp != NULL); 389 390 /* 391 * We disable synchronous streams, drain any queued data and 392 * clear tcp_direct_sockfs. The synchronous streams entry 393 * points will become no-ops after this point. 394 */ 395 tcp_fuse_disable_pair(tcp, B_TRUE); 396 397 /* 398 * Update th_seq and th_ack in the header template 399 */ 400 U32_TO_ABE32(tcp->tcp_snxt, tcp->tcp_tcph->th_seq); 401 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 402 U32_TO_ABE32(peer_tcp->tcp_snxt, peer_tcp->tcp_tcph->th_seq); 403 U32_TO_ABE32(peer_tcp->tcp_rnxt, peer_tcp->tcp_tcph->th_ack); 404 405 /* Unfuse the endpoints */ 406 peer_tcp->tcp_fused = tcp->tcp_fused = B_FALSE; 407 peer_tcp->tcp_loopback_peer = tcp->tcp_loopback_peer = NULL; 408 } 409 410 /* 411 * Fusion output routine for urgent data. This routine is called by 412 * tcp_fuse_output() for handling non-M_DATA mblks. 413 */ 414 void 415 tcp_fuse_output_urg(tcp_t *tcp, mblk_t *mp) 416 { 417 mblk_t *mp1; 418 struct T_exdata_ind *tei; 419 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 420 mblk_t *head, *prev_head = NULL; 421 tcp_stack_t *tcps = tcp->tcp_tcps; 422 423 ASSERT(tcp->tcp_fused); 424 ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp); 425 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 426 ASSERT(mp->b_cont != NULL && DB_TYPE(mp->b_cont) == M_DATA); 427 ASSERT(MBLKL(mp) >= sizeof (*tei) && MBLKL(mp->b_cont) > 0); 428 429 /* 430 * Urgent data arrives in the form of T_EXDATA_REQ from above. 431 * Each occurence denotes a new urgent pointer. For each new 432 * urgent pointer we signal (SIGURG) the receiving app to indicate 433 * that it needs to go into urgent mode. This is similar to the 434 * urgent data handling in the regular tcp. We don't need to keep 435 * track of where the urgent pointer is, because each T_EXDATA_REQ 436 * "advances" the urgent pointer for us. 437 * 438 * The actual urgent data carried by T_EXDATA_REQ is then prepended 439 * by a T_EXDATA_IND before being enqueued behind any existing data 440 * destined for the receiving app. There is only a single urgent 441 * pointer (out-of-band mark) for a given tcp. If the new urgent 442 * data arrives before the receiving app reads some existing urgent 443 * data, the previous marker is lost. This behavior is emulated 444 * accordingly below, by removing any existing T_EXDATA_IND messages 445 * and essentially converting old urgent data into non-urgent. 446 */ 447 ASSERT(tcp->tcp_valid_bits & TCP_URG_VALID); 448 /* Let sender get out of urgent mode */ 449 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 450 451 /* 452 * This flag indicates that a signal needs to be sent up. 453 * This flag will only get cleared once SIGURG is delivered and 454 * is not affected by the tcp_fused flag -- delivery will still 455 * happen even after an endpoint is unfused, to handle the case 456 * where the sending endpoint immediately closes/unfuses after 457 * sending urgent data and the accept is not yet finished. 458 */ 459 peer_tcp->tcp_fused_sigurg = B_TRUE; 460 461 /* Reuse T_EXDATA_REQ mblk for T_EXDATA_IND */ 462 DB_TYPE(mp) = M_PROTO; 463 tei = (struct T_exdata_ind *)mp->b_rptr; 464 tei->PRIM_type = T_EXDATA_IND; 465 tei->MORE_flag = 0; 466 mp->b_wptr = (uchar_t *)&tei[1]; 467 468 TCP_STAT(tcps, tcp_fusion_urg); 469 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 470 471 head = peer_tcp->tcp_rcv_list; 472 while (head != NULL) { 473 /* 474 * Remove existing T_EXDATA_IND, keep the data which follows 475 * it and relink our list. Note that we don't modify the 476 * tcp_rcv_last_tail since it never points to T_EXDATA_IND. 477 */ 478 if (DB_TYPE(head) != M_DATA) { 479 mp1 = head; 480 481 ASSERT(DB_TYPE(mp1->b_cont) == M_DATA); 482 head = mp1->b_cont; 483 mp1->b_cont = NULL; 484 head->b_next = mp1->b_next; 485 mp1->b_next = NULL; 486 if (prev_head != NULL) 487 prev_head->b_next = head; 488 if (peer_tcp->tcp_rcv_list == mp1) 489 peer_tcp->tcp_rcv_list = head; 490 if (peer_tcp->tcp_rcv_last_head == mp1) 491 peer_tcp->tcp_rcv_last_head = head; 492 freeb(mp1); 493 } 494 prev_head = head; 495 head = head->b_next; 496 } 497 } 498 499 /* 500 * Fusion output routine, called by tcp_output() and tcp_wput_proto(). 501 * If we are modifying any member that can be changed outside the squeue, 502 * like tcp_flow_stopped, we need to take tcp_non_sq_lock. 503 */ 504 boolean_t 505 tcp_fuse_output(tcp_t *tcp, mblk_t *mp, uint32_t send_size) 506 { 507 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 508 uint_t max_unread; 509 boolean_t flow_stopped, peer_data_queued = B_FALSE; 510 boolean_t urgent = (DB_TYPE(mp) != M_DATA); 511 mblk_t *mp1 = mp; 512 ill_t *ilp, *olp; 513 ipha_t *ipha; 514 ip6_t *ip6h; 515 tcph_t *tcph; 516 uint_t ip_hdr_len; 517 uint32_t seq; 518 uint32_t recv_size = send_size; 519 tcp_stack_t *tcps = tcp->tcp_tcps; 520 netstack_t *ns = tcps->tcps_netstack; 521 ip_stack_t *ipst = ns->netstack_ip; 522 523 ASSERT(tcp->tcp_fused); 524 ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp); 525 ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp); 526 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO || 527 DB_TYPE(mp) == M_PCPROTO); 528 529 max_unread = peer_tcp->tcp_fuse_rcv_unread_hiwater; 530 531 /* If this connection requires IP, unfuse and use regular path */ 532 if (tcp_loopback_needs_ip(tcp, ns) || 533 tcp_loopback_needs_ip(peer_tcp, ns) || 534 IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst)) { 535 TCP_STAT(tcps, tcp_fusion_aborted); 536 goto unfuse; 537 } 538 539 if (send_size == 0) { 540 freemsg(mp); 541 return (B_TRUE); 542 } 543 544 /* 545 * Handle urgent data; we either send up SIGURG to the peer now 546 * or do it later when we drain, in case the peer is detached 547 * or if we're short of memory for M_PCSIG mblk. 548 */ 549 if (urgent) { 550 /* 551 * We stop synchronous streams when we have urgent data 552 * queued to prevent tcp_fuse_rrw() from pulling it. If 553 * for some reasons the urgent data can't be delivered 554 * below, synchronous streams will remain stopped until 555 * someone drains the tcp_rcv_list. 556 */ 557 TCP_FUSE_SYNCSTR_PLUG_DRAIN(peer_tcp); 558 tcp_fuse_output_urg(tcp, mp); 559 560 mp1 = mp->b_cont; 561 } 562 563 if (tcp->tcp_ipversion == IPV4_VERSION && 564 (HOOKS4_INTERESTED_LOOPBACK_IN(ipst) || 565 HOOKS4_INTERESTED_LOOPBACK_OUT(ipst)) || 566 tcp->tcp_ipversion == IPV6_VERSION && 567 (HOOKS6_INTERESTED_LOOPBACK_IN(ipst) || 568 HOOKS6_INTERESTED_LOOPBACK_OUT(ipst))) { 569 /* 570 * Build ip and tcp header to satisfy FW_HOOKS. 571 * We only build it when any hook is present. 572 */ 573 if ((mp1 = tcp_xmit_mp(tcp, mp1, tcp->tcp_mss, NULL, NULL, 574 tcp->tcp_snxt, B_TRUE, NULL, B_FALSE)) == NULL) 575 /* If tcp_xmit_mp fails, use regular path */ 576 goto unfuse; 577 578 ASSERT(peer_tcp->tcp_connp->conn_ire_cache->ire_ipif != NULL); 579 olp = peer_tcp->tcp_connp->conn_ire_cache->ire_ipif->ipif_ill; 580 /* PFHooks: LOOPBACK_OUT */ 581 if (tcp->tcp_ipversion == IPV4_VERSION) { 582 ipha = (ipha_t *)mp1->b_rptr; 583 584 DTRACE_PROBE4(ip4__loopback__out__start, 585 ill_t *, NULL, ill_t *, olp, 586 ipha_t *, ipha, mblk_t *, mp1); 587 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 588 ipst->ips_ipv4firewall_loopback_out, 589 NULL, olp, ipha, mp1, mp1, 0, ipst); 590 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, mp1); 591 } else { 592 ip6h = (ip6_t *)mp1->b_rptr; 593 594 DTRACE_PROBE4(ip6__loopback__out__start, 595 ill_t *, NULL, ill_t *, olp, 596 ip6_t *, ip6h, mblk_t *, mp1); 597 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 598 ipst->ips_ipv6firewall_loopback_out, 599 NULL, olp, ip6h, mp1, mp1, 0, ipst); 600 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, mp1); 601 } 602 if (mp1 == NULL) 603 goto unfuse; 604 605 606 /* PFHooks: LOOPBACK_IN */ 607 ASSERT(tcp->tcp_connp->conn_ire_cache->ire_ipif != NULL); 608 ilp = tcp->tcp_connp->conn_ire_cache->ire_ipif->ipif_ill; 609 610 if (tcp->tcp_ipversion == IPV4_VERSION) { 611 DTRACE_PROBE4(ip4__loopback__in__start, 612 ill_t *, ilp, ill_t *, NULL, 613 ipha_t *, ipha, mblk_t *, mp1); 614 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 615 ipst->ips_ipv4firewall_loopback_in, 616 ilp, NULL, ipha, mp1, mp1, 0, ipst); 617 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, mp1); 618 if (mp1 == NULL) 619 goto unfuse; 620 621 ip_hdr_len = IPH_HDR_LENGTH(ipha); 622 } else { 623 DTRACE_PROBE4(ip6__loopback__in__start, 624 ill_t *, ilp, ill_t *, NULL, 625 ip6_t *, ip6h, mblk_t *, mp1); 626 FW_HOOKS6(ipst->ips_ip6_loopback_in_event, 627 ipst->ips_ipv6firewall_loopback_in, 628 ilp, NULL, ip6h, mp1, mp1, 0, ipst); 629 DTRACE_PROBE1(ip6__loopback__in__end, mblk_t *, mp1); 630 if (mp1 == NULL) 631 goto unfuse; 632 633 ip_hdr_len = ip_hdr_length_v6(mp1, ip6h); 634 } 635 636 /* Data length might be changed by FW_HOOKS */ 637 tcph = (tcph_t *)&mp1->b_rptr[ip_hdr_len]; 638 seq = ABE32_TO_U32(tcph->th_seq); 639 recv_size += seq - tcp->tcp_snxt; 640 641 /* 642 * The message duplicated by tcp_xmit_mp is freed. 643 * Note: the original message passed in remains unchanged. 644 */ 645 freemsg(mp1); 646 } 647 648 mutex_enter(&peer_tcp->tcp_non_sq_lock); 649 /* 650 * Wake up and signal the peer; it is okay to do this before 651 * enqueueing because we are holding the lock. One of the 652 * advantages of synchronous streams is the ability for us to 653 * find out when the application performs a read on the socket, 654 * by way of tcp_fuse_rrw() entry point being called. Every 655 * data that gets enqueued onto the receiver is treated as if 656 * it has arrived at the receiving endpoint, thus generating 657 * SIGPOLL/SIGIO for asynchronous socket just as in the strrput() 658 * case. However, we only wake up the application when necessary, 659 * i.e. during the first enqueue. When tcp_fuse_rrw() is called 660 * it will send everything upstream. 661 */ 662 if (peer_tcp->tcp_direct_sockfs && !urgent && 663 !TCP_IS_DETACHED(peer_tcp)) { 664 if (peer_tcp->tcp_rcv_list == NULL) 665 STR_WAKEUP_SET(STREAM(peer_tcp->tcp_rq)); 666 /* Update poll events and send SIGPOLL/SIGIO if necessary */ 667 STR_SENDSIG(STREAM(peer_tcp->tcp_rq)); 668 } 669 670 /* 671 * Enqueue data into the peer's receive list; we may or may not 672 * drain the contents depending on the conditions below. 673 */ 674 tcp_rcv_enqueue(peer_tcp, mp, recv_size); 675 676 /* In case it wrapped around and also to keep it constant */ 677 peer_tcp->tcp_rwnd += recv_size; 678 679 /* 680 * Exercise flow-control when needed; we will get back-enabled 681 * in either tcp_accept_finish(), tcp_unfuse(), or tcp_fuse_rrw(). 682 * If tcp_direct_sockfs is on or if the peer endpoint is detached, 683 * we emulate streams flow control by checking the peer's queue 684 * size and high water mark; otherwise we simply use canputnext() 685 * to decide if we need to stop our flow. 686 * 687 * The outstanding unread data block check does not apply for a 688 * detached receiver; this is to avoid unnecessary blocking of the 689 * sender while the accept is currently in progress and is quite 690 * similar to the regular tcp. 691 */ 692 if (TCP_IS_DETACHED(peer_tcp) || max_unread == 0) 693 max_unread = UINT_MAX; 694 695 /* 696 * Since we are accessing our tcp_flow_stopped and might modify it, 697 * we need to take tcp->tcp_non_sq_lock. The lock for the highest 698 * address is held first. Dropping peer_tcp->tcp_non_sq_lock should 699 * not be an issue here since we are within the squeue and the peer 700 * won't disappear. 701 */ 702 if (tcp > peer_tcp) { 703 mutex_exit(&peer_tcp->tcp_non_sq_lock); 704 mutex_enter(&tcp->tcp_non_sq_lock); 705 mutex_enter(&peer_tcp->tcp_non_sq_lock); 706 } else { 707 mutex_enter(&tcp->tcp_non_sq_lock); 708 } 709 flow_stopped = tcp->tcp_flow_stopped; 710 if (((peer_tcp->tcp_direct_sockfs || TCP_IS_DETACHED(peer_tcp)) && 711 (peer_tcp->tcp_rcv_cnt >= peer_tcp->tcp_fuse_rcv_hiwater || 712 ++peer_tcp->tcp_fuse_rcv_unread_cnt >= max_unread)) || 713 (!peer_tcp->tcp_direct_sockfs && 714 !TCP_IS_DETACHED(peer_tcp) && !canputnext(peer_tcp->tcp_rq))) { 715 peer_data_queued = B_TRUE; 716 } 717 718 if (!flow_stopped && (peer_data_queued || 719 (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater))) { 720 tcp_setqfull(tcp); 721 flow_stopped = B_TRUE; 722 TCP_STAT(tcps, tcp_fusion_flowctl); 723 DTRACE_PROBE4(tcp__fuse__output__flowctl, tcp_t *, tcp, 724 uint_t, send_size, uint_t, peer_tcp->tcp_rcv_cnt, 725 uint_t, peer_tcp->tcp_fuse_rcv_unread_cnt); 726 } else if (flow_stopped && !peer_data_queued && 727 (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater)) { 728 tcp_clrqfull(tcp); 729 flow_stopped = B_FALSE; 730 } 731 mutex_exit(&tcp->tcp_non_sq_lock); 732 ipst->ips_loopback_packets++; 733 tcp->tcp_last_sent_len = send_size; 734 735 /* Need to adjust the following SNMP MIB-related variables */ 736 tcp->tcp_snxt += send_size; 737 tcp->tcp_suna = tcp->tcp_snxt; 738 peer_tcp->tcp_rnxt += recv_size; 739 peer_tcp->tcp_rack = peer_tcp->tcp_rnxt; 740 741 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 742 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, send_size); 743 744 BUMP_MIB(&tcps->tcps_mib, tcpInSegs); 745 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 746 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, send_size); 747 748 BUMP_LOCAL(tcp->tcp_obsegs); 749 BUMP_LOCAL(peer_tcp->tcp_ibsegs); 750 751 mutex_exit(&peer_tcp->tcp_non_sq_lock); 752 753 DTRACE_PROBE2(tcp__fuse__output, tcp_t *, tcp, uint_t, send_size); 754 755 if (!TCP_IS_DETACHED(peer_tcp)) { 756 /* 757 * Drain the peer's receive queue it has urgent data or if 758 * we're not flow-controlled. There is no need for draining 759 * normal data when tcp_direct_sockfs is on because the peer 760 * will pull the data via tcp_fuse_rrw(). 761 */ 762 if (urgent || (!flow_stopped && !peer_tcp->tcp_direct_sockfs)) { 763 ASSERT(peer_tcp->tcp_rcv_list != NULL); 764 /* 765 * For TLI-based streams, a thread in tcp_accept_swap() 766 * can race with us. That thread will ensure that the 767 * correct peer_tcp->tcp_rq is globally visible before 768 * peer_tcp->tcp_detached is visible as clear, but we 769 * must also ensure that the load of tcp_rq cannot be 770 * reordered to be before the tcp_detached check. 771 */ 772 membar_consumer(); 773 (void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp, 774 NULL); 775 /* 776 * If synchronous streams was stopped above due 777 * to the presence of urgent data, re-enable it. 778 */ 779 if (urgent) 780 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(peer_tcp); 781 } 782 } 783 return (B_TRUE); 784 unfuse: 785 tcp_unfuse(tcp); 786 return (B_FALSE); 787 } 788 789 /* 790 * This routine gets called to deliver data upstream on a fused or 791 * previously fused tcp loopback endpoint; the latter happens only 792 * when there is a pending SIGURG signal plus urgent data that can't 793 * be sent upstream in the past. 794 */ 795 boolean_t 796 tcp_fuse_rcv_drain(queue_t *q, tcp_t *tcp, mblk_t **sigurg_mpp) 797 { 798 mblk_t *mp; 799 #ifdef DEBUG 800 uint_t cnt = 0; 801 #endif 802 tcp_stack_t *tcps = tcp->tcp_tcps; 803 804 ASSERT(tcp->tcp_loopback); 805 ASSERT(tcp->tcp_fused || tcp->tcp_fused_sigurg); 806 ASSERT(!tcp->tcp_fused || tcp->tcp_loopback_peer != NULL); 807 ASSERT(sigurg_mpp != NULL || tcp->tcp_fused); 808 809 /* No need for the push timer now, in case it was scheduled */ 810 if (tcp->tcp_push_tid != 0) { 811 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 812 tcp->tcp_push_tid = 0; 813 } 814 /* 815 * If there's urgent data sitting in receive list and we didn't 816 * get a chance to send up a SIGURG signal, make sure we send 817 * it first before draining in order to ensure that SIOCATMARK 818 * works properly. 819 */ 820 if (tcp->tcp_fused_sigurg) { 821 /* 822 * sigurg_mpp is normally NULL, i.e. when we're still 823 * fused and didn't get here because of tcp_unfuse(). 824 * In this case try hard to allocate the M_PCSIG mblk. 825 */ 826 if (sigurg_mpp == NULL && 827 (mp = allocb(1, BPRI_HI)) == NULL && 828 (mp = allocb_tryhard(1)) == NULL) { 829 /* Alloc failed; try again next time */ 830 tcp->tcp_push_tid = TCP_TIMER(tcp, tcp_push_timer, 831 MSEC_TO_TICK(tcps->tcps_push_timer_interval)); 832 return (B_TRUE); 833 } else if (sigurg_mpp != NULL) { 834 /* 835 * Use the supplied M_PCSIG mblk; it means we're 836 * either unfused or in the process of unfusing, 837 * and the drain must happen now. 838 */ 839 mp = *sigurg_mpp; 840 *sigurg_mpp = NULL; 841 } 842 ASSERT(mp != NULL); 843 844 tcp->tcp_fused_sigurg = B_FALSE; 845 /* Send up the signal */ 846 DB_TYPE(mp) = M_PCSIG; 847 *mp->b_wptr++ = (uchar_t)SIGURG; 848 putnext(q, mp); 849 /* 850 * Let the regular tcp_rcv_drain() path handle 851 * draining the data if we're no longer fused. 852 */ 853 if (!tcp->tcp_fused) 854 return (B_FALSE); 855 } 856 857 /* 858 * In the synchronous streams case, we generate SIGPOLL/SIGIO for 859 * each M_DATA that gets enqueued onto the receiver. At this point 860 * we are about to drain any queued data via putnext(). In order 861 * to avoid extraneous signal generation from strrput(), we set 862 * STRGETINPROG flag at the stream head prior to the draining and 863 * restore it afterwards. This masks out signal generation only 864 * for M_DATA messages and does not affect urgent data. 865 */ 866 if (tcp->tcp_direct_sockfs) 867 strrput_sig(q, B_FALSE); 868 869 /* Drain the data */ 870 while ((mp = tcp->tcp_rcv_list) != NULL) { 871 tcp->tcp_rcv_list = mp->b_next; 872 mp->b_next = NULL; 873 #ifdef DEBUG 874 cnt += msgdsize(mp); 875 #endif 876 putnext(q, mp); 877 TCP_STAT(tcps, tcp_fusion_putnext); 878 } 879 880 if (tcp->tcp_direct_sockfs) 881 strrput_sig(q, B_TRUE); 882 883 ASSERT(cnt == tcp->tcp_rcv_cnt); 884 tcp->tcp_rcv_last_head = NULL; 885 tcp->tcp_rcv_last_tail = NULL; 886 tcp->tcp_rcv_cnt = 0; 887 tcp->tcp_fuse_rcv_unread_cnt = 0; 888 tcp->tcp_rwnd = q->q_hiwat; 889 890 return (B_TRUE); 891 } 892 893 /* 894 * Synchronous stream entry point for sockfs to retrieve 895 * data directly from tcp_rcv_list. 896 * tcp_fuse_rrw() might end up modifying the peer's tcp_flow_stopped, 897 * for which it must take the tcp_non_sq_lock of the peer as well 898 * making any change. The order of taking the locks is based on 899 * the TCP pointer itself. Before we get the peer we need to take 900 * our tcp_non_sq_lock so that the peer doesn't disappear. However, 901 * we cannot drop the lock if we have to grab the peer's lock (because 902 * of ordering), since the peer might disappear in the interim. So, 903 * we take our tcp_non_sq_lock, get the peer, increment the ref on the 904 * peer's conn, drop all the locks and then take the tcp_non_sq_lock in the 905 * desired order. Incrementing the conn ref on the peer means that the 906 * peer won't disappear when we drop our tcp_non_sq_lock. 907 */ 908 int 909 tcp_fuse_rrw(queue_t *q, struiod_t *dp) 910 { 911 tcp_t *tcp = Q_TO_CONN(q)->conn_tcp; 912 mblk_t *mp; 913 tcp_t *peer_tcp; 914 tcp_stack_t *tcps = tcp->tcp_tcps; 915 916 mutex_enter(&tcp->tcp_non_sq_lock); 917 918 /* 919 * If tcp_fuse_syncstr_plugged is set, then another thread is moving 920 * the underlying data to the stream head. We need to wait until it's 921 * done, then return EBUSY so that strget() will dequeue data from the 922 * stream head to ensure data is drained in-order. 923 */ 924 plugged: 925 if (tcp->tcp_fuse_syncstr_plugged) { 926 do { 927 cv_wait(&tcp->tcp_fuse_plugcv, &tcp->tcp_non_sq_lock); 928 } while (tcp->tcp_fuse_syncstr_plugged); 929 930 mutex_exit(&tcp->tcp_non_sq_lock); 931 TCP_STAT(tcps, tcp_fusion_rrw_plugged); 932 TCP_STAT(tcps, tcp_fusion_rrw_busy); 933 return (EBUSY); 934 } 935 936 peer_tcp = tcp->tcp_loopback_peer; 937 938 /* 939 * If someone had turned off tcp_direct_sockfs or if synchronous 940 * streams is stopped, we return EBUSY. This causes strget() to 941 * dequeue data from the stream head instead. 942 */ 943 if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped) { 944 mutex_exit(&tcp->tcp_non_sq_lock); 945 TCP_STAT(tcps, tcp_fusion_rrw_busy); 946 return (EBUSY); 947 } 948 949 /* 950 * Grab lock in order. The highest addressed tcp is locked first. 951 * We don't do this within the tcp_rcv_list check since if we 952 * have to drop the lock, for ordering, then the tcp_rcv_list 953 * could change. 954 */ 955 if (peer_tcp > tcp) { 956 CONN_INC_REF(peer_tcp->tcp_connp); 957 mutex_exit(&tcp->tcp_non_sq_lock); 958 mutex_enter(&peer_tcp->tcp_non_sq_lock); 959 mutex_enter(&tcp->tcp_non_sq_lock); 960 /* 961 * This might have changed in the interim 962 * Once read-side tcp_non_sq_lock is dropped above 963 * anything can happen, we need to check all 964 * known conditions again once we reaquire 965 * read-side tcp_non_sq_lock. 966 */ 967 if (tcp->tcp_fuse_syncstr_plugged) { 968 mutex_exit(&peer_tcp->tcp_non_sq_lock); 969 CONN_DEC_REF(peer_tcp->tcp_connp); 970 goto plugged; 971 } 972 if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped) { 973 mutex_exit(&tcp->tcp_non_sq_lock); 974 mutex_exit(&peer_tcp->tcp_non_sq_lock); 975 CONN_DEC_REF(peer_tcp->tcp_connp); 976 TCP_STAT(tcps, tcp_fusion_rrw_busy); 977 return (EBUSY); 978 } 979 CONN_DEC_REF(peer_tcp->tcp_connp); 980 } else { 981 mutex_enter(&peer_tcp->tcp_non_sq_lock); 982 } 983 984 if ((mp = tcp->tcp_rcv_list) != NULL) { 985 986 DTRACE_PROBE3(tcp__fuse__rrw, tcp_t *, tcp, 987 uint32_t, tcp->tcp_rcv_cnt, ssize_t, dp->d_uio.uio_resid); 988 989 tcp->tcp_rcv_list = NULL; 990 TCP_STAT(tcps, tcp_fusion_rrw_msgcnt); 991 992 /* 993 * At this point nothing should be left in tcp_rcv_list. 994 * The only possible case where we would have a chain of 995 * b_next-linked messages is urgent data, but we wouldn't 996 * be here if that's true since urgent data is delivered 997 * via putnext() and synchronous streams is stopped until 998 * tcp_fuse_rcv_drain() is finished. 999 */ 1000 ASSERT(DB_TYPE(mp) == M_DATA && mp->b_next == NULL); 1001 1002 tcp->tcp_rcv_last_head = NULL; 1003 tcp->tcp_rcv_last_tail = NULL; 1004 tcp->tcp_rcv_cnt = 0; 1005 tcp->tcp_fuse_rcv_unread_cnt = 0; 1006 1007 if (peer_tcp->tcp_flow_stopped && 1008 (TCP_UNSENT_BYTES(peer_tcp) <= 1009 peer_tcp->tcp_xmit_lowater)) { 1010 tcp_clrqfull(peer_tcp); 1011 TCP_STAT(tcps, tcp_fusion_backenabled); 1012 } 1013 } 1014 mutex_exit(&peer_tcp->tcp_non_sq_lock); 1015 /* 1016 * Either we just dequeued everything or we get here from sockfs 1017 * and have nothing to return; in this case clear RSLEEP. 1018 */ 1019 ASSERT(tcp->tcp_rcv_last_head == NULL); 1020 ASSERT(tcp->tcp_rcv_last_tail == NULL); 1021 ASSERT(tcp->tcp_rcv_cnt == 0); 1022 ASSERT(tcp->tcp_fuse_rcv_unread_cnt == 0); 1023 STR_WAKEUP_CLEAR(STREAM(q)); 1024 1025 mutex_exit(&tcp->tcp_non_sq_lock); 1026 dp->d_mp = mp; 1027 return (0); 1028 } 1029 1030 /* 1031 * Synchronous stream entry point used by certain ioctls to retrieve 1032 * information about or peek into the tcp_rcv_list. 1033 */ 1034 int 1035 tcp_fuse_rinfop(queue_t *q, infod_t *dp) 1036 { 1037 tcp_t *tcp = Q_TO_CONN(q)->conn_tcp; 1038 mblk_t *mp; 1039 uint_t cmd = dp->d_cmd; 1040 int res = 0; 1041 int error = 0; 1042 struct stdata *stp = STREAM(q); 1043 1044 mutex_enter(&tcp->tcp_non_sq_lock); 1045 /* If shutdown on read has happened, return nothing */ 1046 mutex_enter(&stp->sd_lock); 1047 if (stp->sd_flag & STREOF) { 1048 mutex_exit(&stp->sd_lock); 1049 goto done; 1050 } 1051 mutex_exit(&stp->sd_lock); 1052 1053 /* 1054 * It is OK not to return an answer if tcp_rcv_list is 1055 * currently not accessible. 1056 */ 1057 if (!tcp->tcp_direct_sockfs || tcp->tcp_fuse_syncstr_stopped || 1058 tcp->tcp_fuse_syncstr_plugged || (mp = tcp->tcp_rcv_list) == NULL) 1059 goto done; 1060 1061 if (cmd & INFOD_COUNT) { 1062 /* 1063 * We have at least one message and 1064 * could return only one at a time. 1065 */ 1066 dp->d_count++; 1067 res |= INFOD_COUNT; 1068 } 1069 if (cmd & INFOD_BYTES) { 1070 /* 1071 * Return size of all data messages. 1072 */ 1073 dp->d_bytes += tcp->tcp_rcv_cnt; 1074 res |= INFOD_BYTES; 1075 } 1076 if (cmd & INFOD_FIRSTBYTES) { 1077 /* 1078 * Return size of first data message. 1079 */ 1080 dp->d_bytes = msgdsize(mp); 1081 res |= INFOD_FIRSTBYTES; 1082 dp->d_cmd &= ~INFOD_FIRSTBYTES; 1083 } 1084 if (cmd & INFOD_COPYOUT) { 1085 mblk_t *mp1; 1086 int n; 1087 1088 if (DB_TYPE(mp) == M_DATA) { 1089 mp1 = mp; 1090 } else { 1091 mp1 = mp->b_cont; 1092 ASSERT(mp1 != NULL); 1093 } 1094 1095 /* 1096 * Return data contents of first message. 1097 */ 1098 ASSERT(DB_TYPE(mp1) == M_DATA); 1099 while (mp1 != NULL && dp->d_uiop->uio_resid > 0) { 1100 n = MIN(dp->d_uiop->uio_resid, MBLKL(mp1)); 1101 if (n != 0 && (error = uiomove((char *)mp1->b_rptr, n, 1102 UIO_READ, dp->d_uiop)) != 0) { 1103 goto done; 1104 } 1105 mp1 = mp1->b_cont; 1106 } 1107 res |= INFOD_COPYOUT; 1108 dp->d_cmd &= ~INFOD_COPYOUT; 1109 } 1110 done: 1111 mutex_exit(&tcp->tcp_non_sq_lock); 1112 1113 dp->d_res |= res; 1114 1115 return (error); 1116 } 1117 1118 /* 1119 * Enable synchronous streams on a fused tcp loopback endpoint. 1120 */ 1121 static void 1122 tcp_fuse_syncstr_enable(tcp_t *tcp) 1123 { 1124 queue_t *rq = tcp->tcp_rq; 1125 struct stdata *stp = STREAM(rq); 1126 1127 /* We can only enable synchronous streams for sockfs mode */ 1128 tcp->tcp_direct_sockfs = tcp->tcp_issocket && do_tcp_direct_sockfs; 1129 1130 if (!tcp->tcp_direct_sockfs) 1131 return; 1132 1133 mutex_enter(&stp->sd_lock); 1134 mutex_enter(QLOCK(rq)); 1135 1136 /* 1137 * We replace our q_qinfo with one that has the qi_rwp entry point. 1138 * Clear SR_SIGALLDATA because we generate the equivalent signal(s) 1139 * for every enqueued data in tcp_fuse_output(). 1140 */ 1141 rq->q_qinfo = &tcp_loopback_rinit; 1142 rq->q_struiot = tcp_loopback_rinit.qi_struiot; 1143 stp->sd_struiordq = rq; 1144 stp->sd_rput_opt &= ~SR_SIGALLDATA; 1145 1146 mutex_exit(QLOCK(rq)); 1147 mutex_exit(&stp->sd_lock); 1148 } 1149 1150 /* 1151 * Disable synchronous streams on a fused tcp loopback endpoint. 1152 */ 1153 static void 1154 tcp_fuse_syncstr_disable(tcp_t *tcp) 1155 { 1156 queue_t *rq = tcp->tcp_rq; 1157 struct stdata *stp = STREAM(rq); 1158 1159 if (!tcp->tcp_direct_sockfs) 1160 return; 1161 1162 mutex_enter(&stp->sd_lock); 1163 mutex_enter(QLOCK(rq)); 1164 1165 /* 1166 * Reset q_qinfo to point to the default tcp entry points. 1167 * Also restore SR_SIGALLDATA so that strrput() can generate 1168 * the signals again for future M_DATA messages. 1169 */ 1170 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 1171 rq->q_struiot = tcp_rinitv4.qi_struiot; 1172 stp->sd_struiordq = NULL; 1173 stp->sd_rput_opt |= SR_SIGALLDATA; 1174 tcp->tcp_direct_sockfs = B_FALSE; 1175 1176 mutex_exit(QLOCK(rq)); 1177 mutex_exit(&stp->sd_lock); 1178 } 1179 1180 /* 1181 * Enable synchronous streams on a pair of fused tcp endpoints. 1182 */ 1183 void 1184 tcp_fuse_syncstr_enable_pair(tcp_t *tcp) 1185 { 1186 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 1187 1188 ASSERT(tcp->tcp_fused); 1189 ASSERT(peer_tcp != NULL); 1190 1191 tcp_fuse_syncstr_enable(tcp); 1192 tcp_fuse_syncstr_enable(peer_tcp); 1193 } 1194 1195 /* 1196 * Allow or disallow signals to be generated by strrput(). 1197 */ 1198 static void 1199 strrput_sig(queue_t *q, boolean_t on) 1200 { 1201 struct stdata *stp = STREAM(q); 1202 1203 mutex_enter(&stp->sd_lock); 1204 if (on) 1205 stp->sd_flag &= ~STRGETINPROG; 1206 else 1207 stp->sd_flag |= STRGETINPROG; 1208 mutex_exit(&stp->sd_lock); 1209 } 1210 1211 /* 1212 * Disable synchronous streams on a pair of fused tcp endpoints and drain 1213 * any queued data; called either during unfuse or upon transitioning from 1214 * a socket to a stream endpoint due to _SIOCSOCKFALLBACK. 1215 */ 1216 void 1217 tcp_fuse_disable_pair(tcp_t *tcp, boolean_t unfusing) 1218 { 1219 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 1220 tcp_stack_t *tcps = tcp->tcp_tcps; 1221 1222 ASSERT(tcp->tcp_fused); 1223 ASSERT(peer_tcp != NULL); 1224 1225 /* 1226 * Force any tcp_fuse_rrw() calls to block until we've moved the data 1227 * onto the stream head. 1228 */ 1229 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 1230 TCP_FUSE_SYNCSTR_PLUG_DRAIN(peer_tcp); 1231 1232 /* 1233 * Drain any pending data; the detached check is needed because 1234 * we may be called as a result of a tcp_unfuse() triggered by 1235 * tcp_fuse_output(). Note that in case of a detached tcp, the 1236 * draining will happen later after the tcp is unfused. For non- 1237 * urgent data, this can be handled by the regular tcp_rcv_drain(). 1238 * If we have urgent data sitting in the receive list, we will 1239 * need to send up a SIGURG signal first before draining the data. 1240 * All of these will be handled by the code in tcp_fuse_rcv_drain() 1241 * when called from tcp_rcv_drain(). 1242 */ 1243 if (!TCP_IS_DETACHED(tcp)) { 1244 (void) tcp_fuse_rcv_drain(tcp->tcp_rq, tcp, 1245 (unfusing ? &tcp->tcp_fused_sigurg_mp : NULL)); 1246 } 1247 if (!TCP_IS_DETACHED(peer_tcp)) { 1248 (void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp, 1249 (unfusing ? &peer_tcp->tcp_fused_sigurg_mp : NULL)); 1250 } 1251 1252 /* 1253 * Make all current and future tcp_fuse_rrw() calls fail with EBUSY. 1254 * To ensure threads don't sneak past the checks in tcp_fuse_rrw(), 1255 * a given stream must be stopped prior to being unplugged (but the 1256 * ordering of operations between the streams is unimportant). 1257 */ 1258 TCP_FUSE_SYNCSTR_STOP(tcp); 1259 TCP_FUSE_SYNCSTR_STOP(peer_tcp); 1260 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 1261 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(peer_tcp); 1262 1263 /* Lift up any flow-control conditions */ 1264 if (tcp->tcp_flow_stopped) { 1265 tcp_clrqfull(tcp); 1266 TCP_STAT(tcps, tcp_fusion_backenabled); 1267 } 1268 if (peer_tcp->tcp_flow_stopped) { 1269 tcp_clrqfull(peer_tcp); 1270 TCP_STAT(tcps, tcp_fusion_backenabled); 1271 } 1272 1273 /* Disable synchronous streams */ 1274 tcp_fuse_syncstr_disable(tcp); 1275 tcp_fuse_syncstr_disable(peer_tcp); 1276 } 1277 1278 /* 1279 * Calculate the size of receive buffer for a fused tcp endpoint. 1280 */ 1281 size_t 1282 tcp_fuse_set_rcv_hiwat(tcp_t *tcp, size_t rwnd) 1283 { 1284 tcp_stack_t *tcps = tcp->tcp_tcps; 1285 1286 ASSERT(tcp->tcp_fused); 1287 1288 /* Ensure that value is within the maximum upper bound */ 1289 if (rwnd > tcps->tcps_max_buf) 1290 rwnd = tcps->tcps_max_buf; 1291 1292 /* Obey the absolute minimum tcp receive high water mark */ 1293 if (rwnd < tcps->tcps_sth_rcv_hiwat) 1294 rwnd = tcps->tcps_sth_rcv_hiwat; 1295 1296 /* 1297 * Round up to system page size in case SO_RCVBUF is modified 1298 * after SO_SNDBUF; the latter is also similarly rounded up. 1299 */ 1300 rwnd = P2ROUNDUP_TYPED(rwnd, PAGESIZE, size_t); 1301 tcp->tcp_fuse_rcv_hiwater = rwnd; 1302 return (rwnd); 1303 } 1304 1305 /* 1306 * Calculate the maximum outstanding unread data block for a fused tcp endpoint. 1307 */ 1308 int 1309 tcp_fuse_maxpsz_set(tcp_t *tcp) 1310 { 1311 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 1312 uint_t sndbuf = tcp->tcp_xmit_hiwater; 1313 uint_t maxpsz = sndbuf; 1314 1315 ASSERT(tcp->tcp_fused); 1316 ASSERT(peer_tcp != NULL); 1317 ASSERT(peer_tcp->tcp_fuse_rcv_hiwater != 0); 1318 /* 1319 * In the fused loopback case, we want the stream head to split 1320 * up larger writes into smaller chunks for a more accurate flow- 1321 * control accounting. Our maxpsz is half of the sender's send 1322 * buffer or the receiver's receive buffer, whichever is smaller. 1323 * We round up the buffer to system page size due to the lack of 1324 * TCP MSS concept in Fusion. 1325 */ 1326 if (maxpsz > peer_tcp->tcp_fuse_rcv_hiwater) 1327 maxpsz = peer_tcp->tcp_fuse_rcv_hiwater; 1328 maxpsz = P2ROUNDUP_TYPED(maxpsz, PAGESIZE, uint_t) >> 1; 1329 1330 /* 1331 * Calculate the peer's limit for the number of outstanding unread 1332 * data block. This is the amount of data blocks that are allowed 1333 * to reside in the receiver's queue before the sender gets flow 1334 * controlled. It is used only in the synchronous streams mode as 1335 * a way to throttle the sender when it performs consecutive writes 1336 * faster than can be read. The value is derived from SO_SNDBUF in 1337 * order to give the sender some control; we divide it with a large 1338 * value (16KB) to produce a fairly low initial limit. 1339 */ 1340 if (tcp_fusion_rcv_unread_min == 0) { 1341 /* A value of 0 means that we disable the check */ 1342 peer_tcp->tcp_fuse_rcv_unread_hiwater = 0; 1343 } else { 1344 peer_tcp->tcp_fuse_rcv_unread_hiwater = 1345 MAX(sndbuf >> 14, tcp_fusion_rcv_unread_min); 1346 } 1347 return (maxpsz); 1348 } 1349