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 2009 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_if.h> 39 #include <inet/ip_impl.h> 40 #include <inet/tcp.h> 41 #include <inet/tcp_impl.h> 42 #include <inet/ipsec_impl.h> 43 #include <inet/ipclassifier.h> 44 #include <inet/ipp_common.h> 45 #include <inet/ip_if.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. 56 * 57 * Sychronization is handled by squeue and the mutex tcp_non_sq_lock. 58 * One of the requirements for fusion to succeed is that both endpoints 59 * need to be using the same squeue. This ensures that neither side 60 * can disappear while the other side is still sending data. Flow 61 * control information is manipulated outside the squeue, so the 62 * tcp_non_sq_lock must be held when touching tcp_flow_stopped. 63 */ 64 65 /* 66 * Setting this to false means we disable fusion altogether and 67 * loopback connections would go through the protocol paths. 68 */ 69 boolean_t do_tcp_fusion = B_TRUE; 70 71 /* 72 * Return true if this connection needs some IP functionality 73 */ 74 static boolean_t 75 tcp_loopback_needs_ip(tcp_t *tcp, netstack_t *ns) 76 { 77 ipsec_stack_t *ipss = ns->netstack_ipsec; 78 79 /* 80 * If ire is not cached, do not use fusion 81 */ 82 if (tcp->tcp_connp->conn_ire_cache == NULL) { 83 /* 84 * There is no need to hold conn_lock here because when called 85 * from tcp_fuse() there can be no window where conn_ire_cache 86 * can change. This is not true when called from 87 * tcp_fuse_output() as conn_ire_cache can become null just 88 * after the check. It will be necessary to recheck for a NULL 89 * conn_ire_cache in tcp_fuse_output() to avoid passing a 90 * stale ill pointer to FW_HOOKS. 91 */ 92 return (B_TRUE); 93 } 94 if (tcp->tcp_ipversion == IPV4_VERSION) { 95 if (tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) 96 return (B_TRUE); 97 if (CONN_OUTBOUND_POLICY_PRESENT(tcp->tcp_connp, ipss)) 98 return (B_TRUE); 99 if (CONN_INBOUND_POLICY_PRESENT(tcp->tcp_connp, ipss)) 100 return (B_TRUE); 101 } else { 102 if (tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) 103 return (B_TRUE); 104 if (CONN_OUTBOUND_POLICY_PRESENT_V6(tcp->tcp_connp, ipss)) 105 return (B_TRUE); 106 if (CONN_INBOUND_POLICY_PRESENT_V6(tcp->tcp_connp, ipss)) 107 return (B_TRUE); 108 } 109 if (!CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)) 110 return (B_TRUE); 111 return (B_FALSE); 112 } 113 114 115 /* 116 * This routine gets called by the eager tcp upon changing state from 117 * SYN_RCVD to ESTABLISHED. It fuses a direct path between itself 118 * and the active connect tcp such that the regular tcp processings 119 * may be bypassed under allowable circumstances. Because the fusion 120 * requires both endpoints to be in the same squeue, it does not work 121 * for simultaneous active connects because there is no easy way to 122 * switch from one squeue to another once the connection is created. 123 * This is different from the eager tcp case where we assign it the 124 * same squeue as the one given to the active connect tcp during open. 125 */ 126 void 127 tcp_fuse(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph) 128 { 129 conn_t *peer_connp, *connp = tcp->tcp_connp; 130 tcp_t *peer_tcp; 131 tcp_stack_t *tcps = tcp->tcp_tcps; 132 netstack_t *ns; 133 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 134 135 ASSERT(!tcp->tcp_fused); 136 ASSERT(tcp->tcp_loopback); 137 ASSERT(tcp->tcp_loopback_peer == NULL); 138 /* 139 * We need to inherit q_hiwat of the listener tcp, but we can't 140 * really use tcp_listener since we get here after sending up 141 * T_CONN_IND and tcp_wput_accept() may be called independently, 142 * at which point tcp_listener is cleared; this is why we use 143 * tcp_saved_listener. The listener itself is guaranteed to be 144 * around until tcp_accept_finish() is called on this eager -- 145 * this won't happen until we're done since we're inside the 146 * eager's perimeter now. 147 * 148 * We can also get called in the case were a connection needs 149 * to be re-fused. In this case tcp_saved_listener will be 150 * NULL but tcp_refuse will be true. 151 */ 152 ASSERT(tcp->tcp_saved_listener != NULL || tcp->tcp_refuse); 153 /* 154 * Lookup peer endpoint; search for the remote endpoint having 155 * the reversed address-port quadruplet in ESTABLISHED state, 156 * which is guaranteed to be unique in the system. Zone check 157 * is applied accordingly for loopback address, but not for 158 * local address since we want fusion to happen across Zones. 159 */ 160 if (tcp->tcp_ipversion == IPV4_VERSION) { 161 peer_connp = ipcl_conn_tcp_lookup_reversed_ipv4(connp, 162 (ipha_t *)iphdr, tcph, ipst); 163 } else { 164 peer_connp = ipcl_conn_tcp_lookup_reversed_ipv6(connp, 165 (ip6_t *)iphdr, tcph, ipst); 166 } 167 168 /* 169 * We can only proceed if peer exists, resides in the same squeue 170 * as our conn and is not raw-socket. We also restrict fusion to 171 * endpoints of the same type (STREAMS or non-STREAMS). The squeue 172 * assignment of this eager tcp was done earlier at the time of SYN 173 * processing in ip_fanout_tcp{_v6}. Note that similar squeues by 174 * itself doesn't guarantee a safe condition to fuse, hence we perform 175 * additional tests below. 176 */ 177 ASSERT(peer_connp == NULL || peer_connp != connp); 178 if (peer_connp == NULL || peer_connp->conn_sqp != connp->conn_sqp || 179 !IPCL_IS_TCP(peer_connp) || 180 IPCL_IS_NONSTR(connp) != IPCL_IS_NONSTR(peer_connp)) { 181 if (peer_connp != NULL) { 182 TCP_STAT(tcps, tcp_fusion_unqualified); 183 CONN_DEC_REF(peer_connp); 184 } 185 return; 186 } 187 peer_tcp = peer_connp->conn_tcp; /* active connect tcp */ 188 189 ASSERT(peer_tcp != NULL && peer_tcp != tcp && !peer_tcp->tcp_fused); 190 ASSERT(peer_tcp->tcp_loopback_peer == NULL); 191 ASSERT(peer_connp->conn_sqp == connp->conn_sqp); 192 193 /* 194 * Due to IRE changes the peer and us might not agree on tcp_loopback. 195 * We bail in that case. 196 */ 197 if (!peer_tcp->tcp_loopback) { 198 TCP_STAT(tcps, tcp_fusion_unqualified); 199 CONN_DEC_REF(peer_connp); 200 return; 201 } 202 /* 203 * Fuse the endpoints; we perform further checks against both 204 * tcp endpoints to ensure that a fusion is allowed to happen. 205 * In particular we bail out for non-simple TCP/IP or if IPsec/ 206 * IPQoS policy/kernel SSL exists. We also need to check if 207 * the connection is quiescent to cover the case when we are 208 * trying to re-enable fusion after IPobservability is turned off. 209 */ 210 ns = tcps->tcps_netstack; 211 ipst = ns->netstack_ip; 212 213 if (!tcp->tcp_unfusable && !peer_tcp->tcp_unfusable && 214 !tcp_loopback_needs_ip(tcp, ns) && 215 !tcp_loopback_needs_ip(peer_tcp, ns) && 216 tcp->tcp_kssl_ent == NULL && 217 tcp->tcp_xmit_head == NULL && peer_tcp->tcp_xmit_head == NULL && 218 !IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst)) { 219 mblk_t *mp; 220 queue_t *peer_rq = peer_tcp->tcp_rq; 221 222 ASSERT(!TCP_IS_DETACHED(peer_tcp)); 223 ASSERT(tcp->tcp_fused_sigurg_mp == NULL || 224 (!IPCL_IS_NONSTR(connp) && tcp->tcp_refuse)); 225 ASSERT(peer_tcp->tcp_fused_sigurg_mp == NULL || 226 (!IPCL_IS_NONSTR(peer_connp) && peer_tcp->tcp_refuse)); 227 ASSERT(tcp->tcp_kssl_ctx == NULL); 228 229 /* 230 * We need to drain data on both endpoints during unfuse. 231 * If we need to send up SIGURG at the time of draining, 232 * we want to be sure that an mblk is readily available. 233 * This is why we pre-allocate the M_PCSIG mblks for both 234 * endpoints which will only be used during/after unfuse. 235 * The mblk might already exist if we are doing a re-fuse. 236 */ 237 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) { 238 ASSERT(!IPCL_IS_NONSTR(peer_tcp->tcp_connp)); 239 240 if (tcp->tcp_fused_sigurg_mp == NULL) { 241 if ((mp = allocb(1, BPRI_HI)) == NULL) 242 goto failed; 243 tcp->tcp_fused_sigurg_mp = mp; 244 } 245 246 if (peer_tcp->tcp_fused_sigurg_mp == NULL) { 247 if ((mp = allocb(1, BPRI_HI)) == NULL) 248 goto failed; 249 peer_tcp->tcp_fused_sigurg_mp = mp; 250 } 251 252 if ((mp = allocb(sizeof (struct stroptions), 253 BPRI_HI)) == NULL) 254 goto failed; 255 } 256 257 /* Fuse both endpoints */ 258 peer_tcp->tcp_loopback_peer = tcp; 259 tcp->tcp_loopback_peer = peer_tcp; 260 peer_tcp->tcp_fused = tcp->tcp_fused = B_TRUE; 261 262 /* 263 * We never use regular tcp paths in fusion and should 264 * therefore clear tcp_unsent on both endpoints. Having 265 * them set to non-zero values means asking for trouble 266 * especially after unfuse, where we may end up sending 267 * through regular tcp paths which expect xmit_list and 268 * friends to be correctly setup. 269 */ 270 peer_tcp->tcp_unsent = tcp->tcp_unsent = 0; 271 272 tcp_timers_stop(tcp); 273 tcp_timers_stop(peer_tcp); 274 275 /* 276 * At this point we are a detached eager tcp and therefore 277 * don't have a queue assigned to us until accept happens. 278 * In the mean time the peer endpoint may immediately send 279 * us data as soon as fusion is finished, and we need to be 280 * able to flow control it in case it sends down huge amount 281 * of data while we're still detached. To prevent that we 282 * inherit the listener's recv_hiwater value; this is temporary 283 * since we'll repeat the process in tcp_accept_finish(). 284 */ 285 if (!tcp->tcp_refuse) { 286 (void) tcp_fuse_set_rcv_hiwat(tcp, 287 tcp->tcp_saved_listener->tcp_recv_hiwater); 288 289 /* 290 * Set the stream head's write offset value to zero 291 * since we won't be needing any room for TCP/IP 292 * headers; tell it to not break up the writes (this 293 * would reduce the amount of work done by kmem); and 294 * configure our receive buffer. Note that we can only 295 * do this for the active connect tcp since our eager is 296 * still detached; it will be dealt with later in 297 * tcp_accept_finish(). 298 */ 299 if (!IPCL_IS_NONSTR(peer_tcp->tcp_connp)) { 300 struct stroptions *stropt; 301 302 DB_TYPE(mp) = M_SETOPTS; 303 mp->b_wptr += sizeof (*stropt); 304 305 stropt = (struct stroptions *)mp->b_rptr; 306 stropt->so_flags = SO_MAXBLK|SO_WROFF|SO_HIWAT; 307 stropt->so_maxblk = tcp_maxpsz_set(peer_tcp, 308 B_FALSE); 309 stropt->so_wroff = 0; 310 311 /* 312 * Record the stream head's high water mark for 313 * peer endpoint; this is used for flow-control 314 * purposes in tcp_fuse_output(). 315 */ 316 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat( 317 peer_tcp, peer_rq->q_hiwat); 318 319 /* Send the options up */ 320 putnext(peer_rq, mp); 321 } else { 322 struct sock_proto_props sopp; 323 324 /* The peer is a non-STREAMS end point */ 325 ASSERT(IPCL_IS_TCP(peer_connp)); 326 327 (void) tcp_fuse_set_rcv_hiwat(tcp, 328 tcp->tcp_saved_listener->tcp_recv_hiwater); 329 330 sopp.sopp_flags = SOCKOPT_MAXBLK | 331 SOCKOPT_WROFF | SOCKOPT_RCVHIWAT; 332 sopp.sopp_maxblk = tcp_maxpsz_set(peer_tcp, 333 B_FALSE); 334 sopp.sopp_wroff = 0; 335 sopp.sopp_rxhiwat = tcp_fuse_set_rcv_hiwat( 336 peer_tcp, peer_tcp->tcp_recv_hiwater); 337 (*peer_connp->conn_upcalls->su_set_proto_props) 338 (peer_connp->conn_upper_handle, &sopp); 339 } 340 } else { 341 /* 342 * Endpoints are being re-fused, so options will not 343 * be sent up. In case of STREAMS, free the stroptions 344 * mblk. 345 */ 346 if (!IPCL_IS_NONSTR(connp)) 347 freemsg(mp); 348 } 349 tcp->tcp_refuse = B_FALSE; 350 peer_tcp->tcp_refuse = B_FALSE; 351 } else { 352 TCP_STAT(tcps, tcp_fusion_unqualified); 353 } 354 CONN_DEC_REF(peer_connp); 355 return; 356 357 failed: 358 if (tcp->tcp_fused_sigurg_mp != NULL) { 359 freeb(tcp->tcp_fused_sigurg_mp); 360 tcp->tcp_fused_sigurg_mp = NULL; 361 } 362 if (peer_tcp->tcp_fused_sigurg_mp != NULL) { 363 freeb(peer_tcp->tcp_fused_sigurg_mp); 364 peer_tcp->tcp_fused_sigurg_mp = NULL; 365 } 366 CONN_DEC_REF(peer_connp); 367 } 368 369 /* 370 * Unfuse a previously-fused pair of tcp loopback endpoints. 371 */ 372 void 373 tcp_unfuse(tcp_t *tcp) 374 { 375 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 376 tcp_stack_t *tcps = tcp->tcp_tcps; 377 378 ASSERT(tcp->tcp_fused && peer_tcp != NULL); 379 ASSERT(peer_tcp->tcp_fused && peer_tcp->tcp_loopback_peer == tcp); 380 ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp); 381 ASSERT(tcp->tcp_unsent == 0 && peer_tcp->tcp_unsent == 0); 382 383 /* 384 * Cancel any pending push timers. 385 */ 386 if (tcp->tcp_push_tid != 0) { 387 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 388 tcp->tcp_push_tid = 0; 389 } 390 if (peer_tcp->tcp_push_tid != 0) { 391 (void) TCP_TIMER_CANCEL(peer_tcp, peer_tcp->tcp_push_tid); 392 peer_tcp->tcp_push_tid = 0; 393 } 394 395 /* 396 * Drain any pending data; Note that in case of a detached tcp, the 397 * draining will happen later after the tcp is unfused. For non- 398 * urgent data, this can be handled by the regular tcp_rcv_drain(). 399 * If we have urgent data sitting in the receive list, we will 400 * need to send up a SIGURG signal first before draining the data. 401 * All of these will be handled by the code in tcp_fuse_rcv_drain() 402 * when called from tcp_rcv_drain(). 403 */ 404 if (!TCP_IS_DETACHED(tcp)) { 405 (void) tcp_fuse_rcv_drain(tcp->tcp_rq, tcp, 406 &tcp->tcp_fused_sigurg_mp); 407 } 408 if (!TCP_IS_DETACHED(peer_tcp)) { 409 (void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp, 410 &peer_tcp->tcp_fused_sigurg_mp); 411 } 412 413 /* Lift up any flow-control conditions */ 414 mutex_enter(&tcp->tcp_non_sq_lock); 415 if (tcp->tcp_flow_stopped) { 416 tcp_clrqfull(tcp); 417 TCP_STAT(tcps, tcp_fusion_backenabled); 418 } 419 mutex_exit(&tcp->tcp_non_sq_lock); 420 421 mutex_enter(&peer_tcp->tcp_non_sq_lock); 422 if (peer_tcp->tcp_flow_stopped) { 423 tcp_clrqfull(peer_tcp); 424 TCP_STAT(tcps, tcp_fusion_backenabled); 425 } 426 mutex_exit(&peer_tcp->tcp_non_sq_lock); 427 428 /* 429 * Update th_seq and th_ack in the header template 430 */ 431 U32_TO_ABE32(tcp->tcp_snxt, tcp->tcp_tcph->th_seq); 432 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 433 U32_TO_ABE32(peer_tcp->tcp_snxt, peer_tcp->tcp_tcph->th_seq); 434 U32_TO_ABE32(peer_tcp->tcp_rnxt, peer_tcp->tcp_tcph->th_ack); 435 436 /* Unfuse the endpoints */ 437 peer_tcp->tcp_fused = tcp->tcp_fused = B_FALSE; 438 peer_tcp->tcp_loopback_peer = tcp->tcp_loopback_peer = NULL; 439 } 440 441 /* 442 * Fusion output routine used to handle urgent data sent by STREAMS based 443 * endpoints. This routine is called by tcp_fuse_output() for handling 444 * non-M_DATA mblks. 445 */ 446 void 447 tcp_fuse_output_urg(tcp_t *tcp, mblk_t *mp) 448 { 449 mblk_t *mp1; 450 struct T_exdata_ind *tei; 451 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 452 mblk_t *head, *prev_head = NULL; 453 tcp_stack_t *tcps = tcp->tcp_tcps; 454 455 ASSERT(tcp->tcp_fused); 456 ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp); 457 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 458 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 459 ASSERT(mp->b_cont != NULL && DB_TYPE(mp->b_cont) == M_DATA); 460 ASSERT(MBLKL(mp) >= sizeof (*tei) && MBLKL(mp->b_cont) > 0); 461 462 /* 463 * Urgent data arrives in the form of T_EXDATA_REQ from above. 464 * Each occurence denotes a new urgent pointer. For each new 465 * urgent pointer we signal (SIGURG) the receiving app to indicate 466 * that it needs to go into urgent mode. This is similar to the 467 * urgent data handling in the regular tcp. We don't need to keep 468 * track of where the urgent pointer is, because each T_EXDATA_REQ 469 * "advances" the urgent pointer for us. 470 * 471 * The actual urgent data carried by T_EXDATA_REQ is then prepended 472 * by a T_EXDATA_IND before being enqueued behind any existing data 473 * destined for the receiving app. There is only a single urgent 474 * pointer (out-of-band mark) for a given tcp. If the new urgent 475 * data arrives before the receiving app reads some existing urgent 476 * data, the previous marker is lost. This behavior is emulated 477 * accordingly below, by removing any existing T_EXDATA_IND messages 478 * and essentially converting old urgent data into non-urgent. 479 */ 480 ASSERT(tcp->tcp_valid_bits & TCP_URG_VALID); 481 /* Let sender get out of urgent mode */ 482 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 483 484 /* 485 * This flag indicates that a signal needs to be sent up. 486 * This flag will only get cleared once SIGURG is delivered and 487 * is not affected by the tcp_fused flag -- delivery will still 488 * happen even after an endpoint is unfused, to handle the case 489 * where the sending endpoint immediately closes/unfuses after 490 * sending urgent data and the accept is not yet finished. 491 */ 492 peer_tcp->tcp_fused_sigurg = B_TRUE; 493 494 /* Reuse T_EXDATA_REQ mblk for T_EXDATA_IND */ 495 DB_TYPE(mp) = M_PROTO; 496 tei = (struct T_exdata_ind *)mp->b_rptr; 497 tei->PRIM_type = T_EXDATA_IND; 498 tei->MORE_flag = 0; 499 mp->b_wptr = (uchar_t *)&tei[1]; 500 501 TCP_STAT(tcps, tcp_fusion_urg); 502 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 503 504 head = peer_tcp->tcp_rcv_list; 505 while (head != NULL) { 506 /* 507 * Remove existing T_EXDATA_IND, keep the data which follows 508 * it and relink our list. Note that we don't modify the 509 * tcp_rcv_last_tail since it never points to T_EXDATA_IND. 510 */ 511 if (DB_TYPE(head) != M_DATA) { 512 mp1 = head; 513 514 ASSERT(DB_TYPE(mp1->b_cont) == M_DATA); 515 head = mp1->b_cont; 516 mp1->b_cont = NULL; 517 head->b_next = mp1->b_next; 518 mp1->b_next = NULL; 519 if (prev_head != NULL) 520 prev_head->b_next = head; 521 if (peer_tcp->tcp_rcv_list == mp1) 522 peer_tcp->tcp_rcv_list = head; 523 if (peer_tcp->tcp_rcv_last_head == mp1) 524 peer_tcp->tcp_rcv_last_head = head; 525 freeb(mp1); 526 } 527 prev_head = head; 528 head = head->b_next; 529 } 530 } 531 532 /* 533 * Fusion output routine, called by tcp_output() and tcp_wput_proto(). 534 * If we are modifying any member that can be changed outside the squeue, 535 * like tcp_flow_stopped, we need to take tcp_non_sq_lock. 536 */ 537 boolean_t 538 tcp_fuse_output(tcp_t *tcp, mblk_t *mp, uint32_t send_size) 539 { 540 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 541 boolean_t flow_stopped, peer_data_queued = B_FALSE; 542 boolean_t urgent = (DB_TYPE(mp) != M_DATA); 543 boolean_t push = B_TRUE; 544 mblk_t *mp1 = mp; 545 ill_t *ilp, *olp; 546 ipif_t *iifp, *oifp; 547 ipha_t *ipha; 548 ip6_t *ip6h; 549 tcph_t *tcph; 550 uint_t ip_hdr_len; 551 uint32_t seq; 552 uint32_t recv_size = send_size; 553 tcp_stack_t *tcps = tcp->tcp_tcps; 554 netstack_t *ns = tcps->tcps_netstack; 555 ip_stack_t *ipst = ns->netstack_ip; 556 557 ASSERT(tcp->tcp_fused); 558 ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp); 559 ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp); 560 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO || 561 DB_TYPE(mp) == M_PCPROTO); 562 563 /* If this connection requires IP, unfuse and use regular path */ 564 if (tcp_loopback_needs_ip(tcp, ns) || 565 tcp_loopback_needs_ip(peer_tcp, ns) || 566 IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst) || 567 list_head(&ipst->ips_ipobs_cb_list) != NULL) { 568 TCP_STAT(tcps, tcp_fusion_aborted); 569 tcp->tcp_refuse = B_TRUE; 570 peer_tcp->tcp_refuse = B_TRUE; 571 572 bcopy(peer_tcp->tcp_tcph, &tcp->tcp_saved_tcph, 573 sizeof (tcph_t)); 574 bcopy(tcp->tcp_tcph, &peer_tcp->tcp_saved_tcph, 575 sizeof (tcph_t)); 576 if (tcp->tcp_ipversion == IPV4_VERSION) { 577 bcopy(peer_tcp->tcp_ipha, &tcp->tcp_saved_ipha, 578 sizeof (ipha_t)); 579 bcopy(tcp->tcp_ipha, &peer_tcp->tcp_saved_ipha, 580 sizeof (ipha_t)); 581 } else { 582 bcopy(peer_tcp->tcp_ip6h, &tcp->tcp_saved_ip6h, 583 sizeof (ip6_t)); 584 bcopy(tcp->tcp_ip6h, &peer_tcp->tcp_saved_ip6h, 585 sizeof (ip6_t)); 586 } 587 goto unfuse; 588 } 589 590 if (send_size == 0) { 591 freemsg(mp); 592 return (B_TRUE); 593 } 594 595 /* 596 * Handle urgent data; we either send up SIGURG to the peer now 597 * or do it later when we drain, in case the peer is detached 598 * or if we're short of memory for M_PCSIG mblk. 599 */ 600 if (urgent) { 601 tcp_fuse_output_urg(tcp, mp); 602 603 mp1 = mp->b_cont; 604 } 605 606 if (tcp->tcp_ipversion == IPV4_VERSION && 607 (HOOKS4_INTERESTED_LOOPBACK_IN(ipst) || 608 HOOKS4_INTERESTED_LOOPBACK_OUT(ipst)) || 609 tcp->tcp_ipversion == IPV6_VERSION && 610 (HOOKS6_INTERESTED_LOOPBACK_IN(ipst) || 611 HOOKS6_INTERESTED_LOOPBACK_OUT(ipst))) { 612 /* 613 * Build ip and tcp header to satisfy FW_HOOKS. 614 * We only build it when any hook is present. 615 */ 616 if ((mp1 = tcp_xmit_mp(tcp, mp1, tcp->tcp_mss, NULL, NULL, 617 tcp->tcp_snxt, B_TRUE, NULL, B_FALSE)) == NULL) 618 /* If tcp_xmit_mp fails, use regular path */ 619 goto unfuse; 620 621 /* 622 * The ipif and ill can be safely referenced under the 623 * protection of conn_lock - see head of function comment for 624 * conn_get_held_ipif(). It is necessary to check that both 625 * the ipif and ill can be looked up (i.e. not condemned). If 626 * not, bail out and unfuse this connection. 627 */ 628 mutex_enter(&peer_tcp->tcp_connp->conn_lock); 629 if ((peer_tcp->tcp_connp->conn_ire_cache == NULL) || 630 (peer_tcp->tcp_connp->conn_ire_cache->ire_marks & 631 IRE_MARK_CONDEMNED) || 632 ((oifp = peer_tcp->tcp_connp->conn_ire_cache->ire_ipif) 633 == NULL) || 634 (!IPIF_CAN_LOOKUP(oifp)) || 635 ((olp = oifp->ipif_ill) == NULL) || 636 (ill_check_and_refhold(olp) != 0)) { 637 mutex_exit(&peer_tcp->tcp_connp->conn_lock); 638 goto unfuse; 639 } 640 mutex_exit(&peer_tcp->tcp_connp->conn_lock); 641 642 /* PFHooks: LOOPBACK_OUT */ 643 if (tcp->tcp_ipversion == IPV4_VERSION) { 644 ipha = (ipha_t *)mp1->b_rptr; 645 646 DTRACE_PROBE4(ip4__loopback__out__start, 647 ill_t *, NULL, ill_t *, olp, 648 ipha_t *, ipha, mblk_t *, mp1); 649 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 650 ipst->ips_ipv4firewall_loopback_out, 651 NULL, olp, ipha, mp1, mp1, 0, ipst); 652 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, mp1); 653 } else { 654 ip6h = (ip6_t *)mp1->b_rptr; 655 656 DTRACE_PROBE4(ip6__loopback__out__start, 657 ill_t *, NULL, ill_t *, olp, 658 ip6_t *, ip6h, mblk_t *, mp1); 659 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 660 ipst->ips_ipv6firewall_loopback_out, 661 NULL, olp, ip6h, mp1, mp1, 0, ipst); 662 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, mp1); 663 } 664 ill_refrele(olp); 665 666 if (mp1 == NULL) 667 goto unfuse; 668 669 /* 670 * The ipif and ill can be safely referenced under the 671 * protection of conn_lock - see head of function comment for 672 * conn_get_held_ipif(). It is necessary to check that both 673 * the ipif and ill can be looked up (i.e. not condemned). If 674 * not, bail out and unfuse this connection. 675 */ 676 mutex_enter(&tcp->tcp_connp->conn_lock); 677 if ((tcp->tcp_connp->conn_ire_cache == NULL) || 678 (tcp->tcp_connp->conn_ire_cache->ire_marks & 679 IRE_MARK_CONDEMNED) || 680 ((iifp = tcp->tcp_connp->conn_ire_cache->ire_ipif) 681 == NULL) || 682 (!IPIF_CAN_LOOKUP(iifp)) || 683 ((ilp = iifp->ipif_ill) == NULL) || 684 (ill_check_and_refhold(ilp) != 0)) { 685 mutex_exit(&tcp->tcp_connp->conn_lock); 686 goto unfuse; 687 } 688 mutex_exit(&tcp->tcp_connp->conn_lock); 689 690 /* PFHooks: LOOPBACK_IN */ 691 if (tcp->tcp_ipversion == IPV4_VERSION) { 692 DTRACE_PROBE4(ip4__loopback__in__start, 693 ill_t *, ilp, ill_t *, NULL, 694 ipha_t *, ipha, mblk_t *, mp1); 695 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 696 ipst->ips_ipv4firewall_loopback_in, 697 ilp, NULL, ipha, mp1, mp1, 0, ipst); 698 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, mp1); 699 ill_refrele(ilp); 700 if (mp1 == NULL) 701 goto unfuse; 702 703 ip_hdr_len = IPH_HDR_LENGTH(ipha); 704 } else { 705 DTRACE_PROBE4(ip6__loopback__in__start, 706 ill_t *, ilp, ill_t *, NULL, 707 ip6_t *, ip6h, mblk_t *, mp1); 708 FW_HOOKS6(ipst->ips_ip6_loopback_in_event, 709 ipst->ips_ipv6firewall_loopback_in, 710 ilp, NULL, ip6h, mp1, mp1, 0, ipst); 711 DTRACE_PROBE1(ip6__loopback__in__end, mblk_t *, mp1); 712 ill_refrele(ilp); 713 if (mp1 == NULL) 714 goto unfuse; 715 716 ip_hdr_len = ip_hdr_length_v6(mp1, ip6h); 717 } 718 719 /* Data length might be changed by FW_HOOKS */ 720 tcph = (tcph_t *)&mp1->b_rptr[ip_hdr_len]; 721 seq = ABE32_TO_U32(tcph->th_seq); 722 recv_size += seq - tcp->tcp_snxt; 723 724 /* 725 * The message duplicated by tcp_xmit_mp is freed. 726 * Note: the original message passed in remains unchanged. 727 */ 728 freemsg(mp1); 729 } 730 731 /* 732 * Enqueue data into the peer's receive list; we may or may not 733 * drain the contents depending on the conditions below. 734 * 735 * For non-STREAMS sockets we normally queue data directly in the 736 * socket by calling the su_recv upcall. However, if the peer is 737 * detached we use tcp_rcv_enqueue() instead. Queued data will be 738 * drained when the accept completes (in tcp_accept_finish()). 739 */ 740 if (IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 741 !TCP_IS_DETACHED(peer_tcp)) { 742 int error; 743 int flags = 0; 744 745 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 746 (tcp->tcp_urg == tcp->tcp_snxt)) { 747 flags = MSG_OOB; 748 (*peer_tcp->tcp_connp->conn_upcalls->su_signal_oob) 749 (peer_tcp->tcp_connp->conn_upper_handle, 0); 750 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 751 } 752 if ((*peer_tcp->tcp_connp->conn_upcalls->su_recv)( 753 peer_tcp->tcp_connp->conn_upper_handle, mp, recv_size, 754 flags, &error, &push) < 0) { 755 ASSERT(error != EOPNOTSUPP); 756 peer_data_queued = B_TRUE; 757 } 758 } else { 759 if (IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 760 (tcp->tcp_valid_bits & TCP_URG_VALID) && 761 (tcp->tcp_urg == tcp->tcp_snxt)) { 762 /* 763 * Can not deal with urgent pointers 764 * that arrive before the connection has been 765 * accept()ed. 766 */ 767 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 768 freemsg(mp); 769 return (B_TRUE); 770 } 771 772 tcp_rcv_enqueue(peer_tcp, mp, recv_size); 773 774 /* In case it wrapped around and also to keep it constant */ 775 peer_tcp->tcp_rwnd += recv_size; 776 } 777 778 /* 779 * Exercise flow-control when needed; we will get back-enabled 780 * in either tcp_accept_finish(), tcp_unfuse(), or when data is 781 * consumed. If peer endpoint is detached, we emulate streams flow 782 * control by checking the peer's queue size and high water mark; 783 * otherwise we simply use canputnext() to decide if we need to stop 784 * our flow. 785 * 786 * Since we are accessing our tcp_flow_stopped and might modify it, 787 * we need to take tcp->tcp_non_sq_lock. 788 */ 789 mutex_enter(&tcp->tcp_non_sq_lock); 790 flow_stopped = tcp->tcp_flow_stopped; 791 if ((TCP_IS_DETACHED(peer_tcp) && 792 (peer_tcp->tcp_rcv_cnt >= peer_tcp->tcp_fuse_rcv_hiwater)) || 793 (!TCP_IS_DETACHED(peer_tcp) && 794 !IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 795 !canputnext(peer_tcp->tcp_rq))) { 796 peer_data_queued = B_TRUE; 797 } 798 799 if (!flow_stopped && (peer_data_queued || 800 (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater))) { 801 tcp_setqfull(tcp); 802 flow_stopped = B_TRUE; 803 TCP_STAT(tcps, tcp_fusion_flowctl); 804 DTRACE_PROBE3(tcp__fuse__output__flowctl, tcp_t *, tcp, 805 uint_t, send_size, uint_t, peer_tcp->tcp_rcv_cnt); 806 } else if (flow_stopped && !peer_data_queued && 807 (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater)) { 808 tcp_clrqfull(tcp); 809 TCP_STAT(tcps, tcp_fusion_backenabled); 810 flow_stopped = B_FALSE; 811 } 812 mutex_exit(&tcp->tcp_non_sq_lock); 813 814 ipst->ips_loopback_packets++; 815 tcp->tcp_last_sent_len = send_size; 816 817 /* Need to adjust the following SNMP MIB-related variables */ 818 tcp->tcp_snxt += send_size; 819 tcp->tcp_suna = tcp->tcp_snxt; 820 peer_tcp->tcp_rnxt += recv_size; 821 peer_tcp->tcp_rack = peer_tcp->tcp_rnxt; 822 823 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 824 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, send_size); 825 826 BUMP_MIB(&tcps->tcps_mib, tcpInSegs); 827 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 828 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, send_size); 829 830 BUMP_LOCAL(tcp->tcp_obsegs); 831 BUMP_LOCAL(peer_tcp->tcp_ibsegs); 832 833 DTRACE_PROBE2(tcp__fuse__output, tcp_t *, tcp, uint_t, send_size); 834 835 if (!IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 836 !TCP_IS_DETACHED(peer_tcp)) { 837 /* 838 * Drain the peer's receive queue it has urgent data or if 839 * we're not flow-controlled. 840 */ 841 if (urgent || !flow_stopped) { 842 ASSERT(peer_tcp->tcp_rcv_list != NULL); 843 /* 844 * For TLI-based streams, a thread in tcp_accept_swap() 845 * can race with us. That thread will ensure that the 846 * correct peer_tcp->tcp_rq is globally visible before 847 * peer_tcp->tcp_detached is visible as clear, but we 848 * must also ensure that the load of tcp_rq cannot be 849 * reordered to be before the tcp_detached check. 850 */ 851 membar_consumer(); 852 (void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp, 853 NULL); 854 } 855 } 856 return (B_TRUE); 857 unfuse: 858 tcp_unfuse(tcp); 859 return (B_FALSE); 860 } 861 862 /* 863 * This routine gets called to deliver data upstream on a fused or 864 * previously fused tcp loopback endpoint; the latter happens only 865 * when there is a pending SIGURG signal plus urgent data that can't 866 * be sent upstream in the past. 867 */ 868 boolean_t 869 tcp_fuse_rcv_drain(queue_t *q, tcp_t *tcp, mblk_t **sigurg_mpp) 870 { 871 mblk_t *mp; 872 conn_t *connp = tcp->tcp_connp; 873 874 #ifdef DEBUG 875 uint_t cnt = 0; 876 #endif 877 tcp_stack_t *tcps = tcp->tcp_tcps; 878 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 879 880 ASSERT(tcp->tcp_loopback); 881 ASSERT(tcp->tcp_fused || tcp->tcp_fused_sigurg); 882 ASSERT(!tcp->tcp_fused || tcp->tcp_loopback_peer != NULL); 883 ASSERT(IPCL_IS_NONSTR(connp) || sigurg_mpp != NULL || tcp->tcp_fused); 884 885 /* No need for the push timer now, in case it was scheduled */ 886 if (tcp->tcp_push_tid != 0) { 887 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 888 tcp->tcp_push_tid = 0; 889 } 890 /* 891 * If there's urgent data sitting in receive list and we didn't 892 * get a chance to send up a SIGURG signal, make sure we send 893 * it first before draining in order to ensure that SIOCATMARK 894 * works properly. 895 */ 896 if (tcp->tcp_fused_sigurg) { 897 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 898 899 tcp->tcp_fused_sigurg = B_FALSE; 900 /* 901 * sigurg_mpp is normally NULL, i.e. when we're still 902 * fused and didn't get here because of tcp_unfuse(). 903 * In this case try hard to allocate the M_PCSIG mblk. 904 */ 905 if (sigurg_mpp == NULL && 906 (mp = allocb(1, BPRI_HI)) == NULL && 907 (mp = allocb_tryhard(1)) == NULL) { 908 /* Alloc failed; try again next time */ 909 tcp->tcp_push_tid = TCP_TIMER(tcp, 910 tcp_push_timer, 911 MSEC_TO_TICK( 912 tcps->tcps_push_timer_interval)); 913 return (B_TRUE); 914 } else if (sigurg_mpp != NULL) { 915 /* 916 * Use the supplied M_PCSIG mblk; it means we're 917 * either unfused or in the process of unfusing, 918 * and the drain must happen now. 919 */ 920 mp = *sigurg_mpp; 921 *sigurg_mpp = NULL; 922 } 923 ASSERT(mp != NULL); 924 925 /* Send up the signal */ 926 DB_TYPE(mp) = M_PCSIG; 927 *mp->b_wptr++ = (uchar_t)SIGURG; 928 putnext(q, mp); 929 930 /* 931 * Let the regular tcp_rcv_drain() path handle 932 * draining the data if we're no longer fused. 933 */ 934 if (!tcp->tcp_fused) 935 return (B_FALSE); 936 } 937 938 /* Drain the data */ 939 while ((mp = tcp->tcp_rcv_list) != NULL) { 940 tcp->tcp_rcv_list = mp->b_next; 941 mp->b_next = NULL; 942 #ifdef DEBUG 943 cnt += msgdsize(mp); 944 #endif 945 ASSERT(!IPCL_IS_NONSTR(connp)); 946 putnext(q, mp); 947 TCP_STAT(tcps, tcp_fusion_putnext); 948 } 949 950 #ifdef DEBUG 951 ASSERT(cnt == tcp->tcp_rcv_cnt); 952 #endif 953 tcp->tcp_rcv_last_head = NULL; 954 tcp->tcp_rcv_last_tail = NULL; 955 tcp->tcp_rcv_cnt = 0; 956 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 957 958 mutex_enter(&peer_tcp->tcp_non_sq_lock); 959 if (peer_tcp->tcp_flow_stopped && (TCP_UNSENT_BYTES(peer_tcp) <= 960 peer_tcp->tcp_xmit_lowater)) { 961 tcp_clrqfull(peer_tcp); 962 TCP_STAT(tcps, tcp_fusion_backenabled); 963 } 964 mutex_exit(&peer_tcp->tcp_non_sq_lock); 965 966 return (B_TRUE); 967 } 968 969 /* 970 * Calculate the size of receive buffer for a fused tcp endpoint. 971 */ 972 size_t 973 tcp_fuse_set_rcv_hiwat(tcp_t *tcp, size_t rwnd) 974 { 975 tcp_stack_t *tcps = tcp->tcp_tcps; 976 977 ASSERT(tcp->tcp_fused); 978 979 /* Ensure that value is within the maximum upper bound */ 980 if (rwnd > tcps->tcps_max_buf) 981 rwnd = tcps->tcps_max_buf; 982 983 /* Obey the absolute minimum tcp receive high water mark */ 984 if (rwnd < tcps->tcps_sth_rcv_hiwat) 985 rwnd = tcps->tcps_sth_rcv_hiwat; 986 987 /* 988 * Round up to system page size in case SO_RCVBUF is modified 989 * after SO_SNDBUF; the latter is also similarly rounded up. 990 */ 991 rwnd = P2ROUNDUP_TYPED(rwnd, PAGESIZE, size_t); 992 tcp->tcp_fuse_rcv_hiwater = rwnd; 993 return (rwnd); 994 } 995 996 /* 997 * Calculate the maximum outstanding unread data block for a fused tcp endpoint. 998 */ 999 int 1000 tcp_fuse_maxpsz_set(tcp_t *tcp) 1001 { 1002 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 1003 uint_t sndbuf = tcp->tcp_xmit_hiwater; 1004 uint_t maxpsz = sndbuf; 1005 1006 ASSERT(tcp->tcp_fused); 1007 ASSERT(peer_tcp != NULL); 1008 ASSERT(peer_tcp->tcp_fuse_rcv_hiwater != 0); 1009 /* 1010 * In the fused loopback case, we want the stream head to split 1011 * up larger writes into smaller chunks for a more accurate flow- 1012 * control accounting. Our maxpsz is half of the sender's send 1013 * buffer or the receiver's receive buffer, whichever is smaller. 1014 * We round up the buffer to system page size due to the lack of 1015 * TCP MSS concept in Fusion. 1016 */ 1017 if (maxpsz > peer_tcp->tcp_fuse_rcv_hiwater) 1018 maxpsz = peer_tcp->tcp_fuse_rcv_hiwater; 1019 maxpsz = P2ROUNDUP_TYPED(maxpsz, PAGESIZE, uint_t) >> 1; 1020 1021 return (maxpsz); 1022 } 1023 1024 /* 1025 * Called to release flow control. 1026 */ 1027 void 1028 tcp_fuse_backenable(tcp_t *tcp) 1029 { 1030 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 1031 1032 ASSERT(tcp->tcp_fused); 1033 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 1034 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 1035 ASSERT(!TCP_IS_DETACHED(tcp)); 1036 ASSERT(tcp->tcp_connp->conn_sqp == 1037 peer_tcp->tcp_connp->conn_sqp); 1038 1039 if (tcp->tcp_rcv_list != NULL) 1040 (void) tcp_fuse_rcv_drain(tcp->tcp_rq, tcp, NULL); 1041 1042 mutex_enter(&peer_tcp->tcp_non_sq_lock); 1043 if (peer_tcp->tcp_flow_stopped && 1044 (TCP_UNSENT_BYTES(peer_tcp) <= 1045 peer_tcp->tcp_xmit_lowater)) { 1046 tcp_clrqfull(peer_tcp); 1047 } 1048 mutex_exit(&peer_tcp->tcp_non_sq_lock); 1049 1050 TCP_STAT(tcp->tcp_tcps, tcp_fusion_backenabled); 1051 } 1052