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 tcp->tcp_refuse = B_FALSE; 320 peer_tcp->tcp_refuse = B_FALSE; 321 /* Send the options up */ 322 putnext(peer_rq, mp); 323 } else { 324 struct sock_proto_props sopp; 325 326 /* The peer is a non-STREAMS end point */ 327 ASSERT(IPCL_IS_TCP(peer_connp)); 328 329 (void) tcp_fuse_set_rcv_hiwat(tcp, 330 tcp->tcp_saved_listener->tcp_recv_hiwater); 331 332 sopp.sopp_flags = SOCKOPT_MAXBLK | 333 SOCKOPT_WROFF | SOCKOPT_RCVHIWAT; 334 sopp.sopp_maxblk = tcp_maxpsz_set(peer_tcp, 335 B_FALSE); 336 sopp.sopp_wroff = 0; 337 sopp.sopp_rxhiwat = tcp_fuse_set_rcv_hiwat( 338 peer_tcp, peer_tcp->tcp_recv_hiwater); 339 (*peer_connp->conn_upcalls->su_set_proto_props) 340 (peer_connp->conn_upper_handle, &sopp); 341 } 342 } 343 tcp->tcp_refuse = B_FALSE; 344 peer_tcp->tcp_refuse = B_FALSE; 345 } else { 346 TCP_STAT(tcps, tcp_fusion_unqualified); 347 } 348 CONN_DEC_REF(peer_connp); 349 return; 350 351 failed: 352 if (tcp->tcp_fused_sigurg_mp != NULL) { 353 freeb(tcp->tcp_fused_sigurg_mp); 354 tcp->tcp_fused_sigurg_mp = NULL; 355 } 356 if (peer_tcp->tcp_fused_sigurg_mp != NULL) { 357 freeb(peer_tcp->tcp_fused_sigurg_mp); 358 peer_tcp->tcp_fused_sigurg_mp = NULL; 359 } 360 CONN_DEC_REF(peer_connp); 361 } 362 363 /* 364 * Unfuse a previously-fused pair of tcp loopback endpoints. 365 */ 366 void 367 tcp_unfuse(tcp_t *tcp) 368 { 369 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 370 tcp_stack_t *tcps = tcp->tcp_tcps; 371 372 ASSERT(tcp->tcp_fused && peer_tcp != NULL); 373 ASSERT(peer_tcp->tcp_fused && peer_tcp->tcp_loopback_peer == tcp); 374 ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp); 375 ASSERT(tcp->tcp_unsent == 0 && peer_tcp->tcp_unsent == 0); 376 377 /* 378 * Cancel any pending push timers. 379 */ 380 if (tcp->tcp_push_tid != 0) { 381 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 382 tcp->tcp_push_tid = 0; 383 } 384 if (peer_tcp->tcp_push_tid != 0) { 385 (void) TCP_TIMER_CANCEL(peer_tcp, peer_tcp->tcp_push_tid); 386 peer_tcp->tcp_push_tid = 0; 387 } 388 389 /* 390 * Drain any pending data; Note that in case of a detached tcp, the 391 * draining will happen later after the tcp is unfused. For non- 392 * urgent data, this can be handled by the regular tcp_rcv_drain(). 393 * If we have urgent data sitting in the receive list, we will 394 * need to send up a SIGURG signal first before draining the data. 395 * All of these will be handled by the code in tcp_fuse_rcv_drain() 396 * when called from tcp_rcv_drain(). 397 */ 398 if (!TCP_IS_DETACHED(tcp)) { 399 (void) tcp_fuse_rcv_drain(tcp->tcp_rq, tcp, 400 &tcp->tcp_fused_sigurg_mp); 401 } 402 if (!TCP_IS_DETACHED(peer_tcp)) { 403 (void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp, 404 &peer_tcp->tcp_fused_sigurg_mp); 405 } 406 407 /* Lift up any flow-control conditions */ 408 mutex_enter(&tcp->tcp_non_sq_lock); 409 if (tcp->tcp_flow_stopped) { 410 tcp_clrqfull(tcp); 411 TCP_STAT(tcps, tcp_fusion_backenabled); 412 } 413 mutex_exit(&tcp->tcp_non_sq_lock); 414 415 mutex_enter(&peer_tcp->tcp_non_sq_lock); 416 if (peer_tcp->tcp_flow_stopped) { 417 tcp_clrqfull(peer_tcp); 418 TCP_STAT(tcps, tcp_fusion_backenabled); 419 } 420 mutex_exit(&peer_tcp->tcp_non_sq_lock); 421 422 /* 423 * Update th_seq and th_ack in the header template 424 */ 425 U32_TO_ABE32(tcp->tcp_snxt, tcp->tcp_tcph->th_seq); 426 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 427 U32_TO_ABE32(peer_tcp->tcp_snxt, peer_tcp->tcp_tcph->th_seq); 428 U32_TO_ABE32(peer_tcp->tcp_rnxt, peer_tcp->tcp_tcph->th_ack); 429 430 /* Unfuse the endpoints */ 431 peer_tcp->tcp_fused = tcp->tcp_fused = B_FALSE; 432 peer_tcp->tcp_loopback_peer = tcp->tcp_loopback_peer = NULL; 433 } 434 435 /* 436 * Fusion output routine used to handle urgent data sent by STREAMS based 437 * endpoints. This routine is called by tcp_fuse_output() for handling 438 * non-M_DATA mblks. 439 */ 440 void 441 tcp_fuse_output_urg(tcp_t *tcp, mblk_t *mp) 442 { 443 mblk_t *mp1; 444 struct T_exdata_ind *tei; 445 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 446 mblk_t *head, *prev_head = NULL; 447 tcp_stack_t *tcps = tcp->tcp_tcps; 448 449 ASSERT(tcp->tcp_fused); 450 ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp); 451 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 452 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 453 ASSERT(mp->b_cont != NULL && DB_TYPE(mp->b_cont) == M_DATA); 454 ASSERT(MBLKL(mp) >= sizeof (*tei) && MBLKL(mp->b_cont) > 0); 455 456 /* 457 * Urgent data arrives in the form of T_EXDATA_REQ from above. 458 * Each occurence denotes a new urgent pointer. For each new 459 * urgent pointer we signal (SIGURG) the receiving app to indicate 460 * that it needs to go into urgent mode. This is similar to the 461 * urgent data handling in the regular tcp. We don't need to keep 462 * track of where the urgent pointer is, because each T_EXDATA_REQ 463 * "advances" the urgent pointer for us. 464 * 465 * The actual urgent data carried by T_EXDATA_REQ is then prepended 466 * by a T_EXDATA_IND before being enqueued behind any existing data 467 * destined for the receiving app. There is only a single urgent 468 * pointer (out-of-band mark) for a given tcp. If the new urgent 469 * data arrives before the receiving app reads some existing urgent 470 * data, the previous marker is lost. This behavior is emulated 471 * accordingly below, by removing any existing T_EXDATA_IND messages 472 * and essentially converting old urgent data into non-urgent. 473 */ 474 ASSERT(tcp->tcp_valid_bits & TCP_URG_VALID); 475 /* Let sender get out of urgent mode */ 476 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 477 478 /* 479 * This flag indicates that a signal needs to be sent up. 480 * This flag will only get cleared once SIGURG is delivered and 481 * is not affected by the tcp_fused flag -- delivery will still 482 * happen even after an endpoint is unfused, to handle the case 483 * where the sending endpoint immediately closes/unfuses after 484 * sending urgent data and the accept is not yet finished. 485 */ 486 peer_tcp->tcp_fused_sigurg = B_TRUE; 487 488 /* Reuse T_EXDATA_REQ mblk for T_EXDATA_IND */ 489 DB_TYPE(mp) = M_PROTO; 490 tei = (struct T_exdata_ind *)mp->b_rptr; 491 tei->PRIM_type = T_EXDATA_IND; 492 tei->MORE_flag = 0; 493 mp->b_wptr = (uchar_t *)&tei[1]; 494 495 TCP_STAT(tcps, tcp_fusion_urg); 496 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 497 498 head = peer_tcp->tcp_rcv_list; 499 while (head != NULL) { 500 /* 501 * Remove existing T_EXDATA_IND, keep the data which follows 502 * it and relink our list. Note that we don't modify the 503 * tcp_rcv_last_tail since it never points to T_EXDATA_IND. 504 */ 505 if (DB_TYPE(head) != M_DATA) { 506 mp1 = head; 507 508 ASSERT(DB_TYPE(mp1->b_cont) == M_DATA); 509 head = mp1->b_cont; 510 mp1->b_cont = NULL; 511 head->b_next = mp1->b_next; 512 mp1->b_next = NULL; 513 if (prev_head != NULL) 514 prev_head->b_next = head; 515 if (peer_tcp->tcp_rcv_list == mp1) 516 peer_tcp->tcp_rcv_list = head; 517 if (peer_tcp->tcp_rcv_last_head == mp1) 518 peer_tcp->tcp_rcv_last_head = head; 519 freeb(mp1); 520 } 521 prev_head = head; 522 head = head->b_next; 523 } 524 } 525 526 /* 527 * Fusion output routine, called by tcp_output() and tcp_wput_proto(). 528 * If we are modifying any member that can be changed outside the squeue, 529 * like tcp_flow_stopped, we need to take tcp_non_sq_lock. 530 */ 531 boolean_t 532 tcp_fuse_output(tcp_t *tcp, mblk_t *mp, uint32_t send_size) 533 { 534 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 535 boolean_t flow_stopped, peer_data_queued = B_FALSE; 536 boolean_t urgent = (DB_TYPE(mp) != M_DATA); 537 boolean_t push = B_TRUE; 538 mblk_t *mp1 = mp; 539 ill_t *ilp, *olp; 540 ipif_t *iifp, *oifp; 541 ipha_t *ipha; 542 ip6_t *ip6h; 543 tcph_t *tcph; 544 uint_t ip_hdr_len; 545 uint32_t seq; 546 uint32_t recv_size = send_size; 547 tcp_stack_t *tcps = tcp->tcp_tcps; 548 netstack_t *ns = tcps->tcps_netstack; 549 ip_stack_t *ipst = ns->netstack_ip; 550 551 ASSERT(tcp->tcp_fused); 552 ASSERT(peer_tcp != NULL && peer_tcp->tcp_loopback_peer == tcp); 553 ASSERT(tcp->tcp_connp->conn_sqp == peer_tcp->tcp_connp->conn_sqp); 554 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_PROTO || 555 DB_TYPE(mp) == M_PCPROTO); 556 557 /* If this connection requires IP, unfuse and use regular path */ 558 if (tcp_loopback_needs_ip(tcp, ns) || 559 tcp_loopback_needs_ip(peer_tcp, ns) || 560 IPP_ENABLED(IPP_LOCAL_OUT|IPP_LOCAL_IN, ipst) || 561 list_head(&ipst->ips_ipobs_cb_list) != NULL) { 562 TCP_STAT(tcps, tcp_fusion_aborted); 563 tcp->tcp_refuse = B_TRUE; 564 peer_tcp->tcp_refuse = B_TRUE; 565 566 bcopy(peer_tcp->tcp_tcph, &tcp->tcp_saved_tcph, 567 sizeof (tcph_t)); 568 bcopy(tcp->tcp_tcph, &peer_tcp->tcp_saved_tcph, 569 sizeof (tcph_t)); 570 if (tcp->tcp_ipversion == IPV4_VERSION) { 571 bcopy(peer_tcp->tcp_ipha, &tcp->tcp_saved_ipha, 572 sizeof (ipha_t)); 573 bcopy(tcp->tcp_ipha, &peer_tcp->tcp_saved_ipha, 574 sizeof (ipha_t)); 575 } else { 576 bcopy(peer_tcp->tcp_ip6h, &tcp->tcp_saved_ip6h, 577 sizeof (ip6_t)); 578 bcopy(tcp->tcp_ip6h, &peer_tcp->tcp_saved_ip6h, 579 sizeof (ip6_t)); 580 } 581 goto unfuse; 582 } 583 584 if (send_size == 0) { 585 freemsg(mp); 586 return (B_TRUE); 587 } 588 589 /* 590 * Handle urgent data; we either send up SIGURG to the peer now 591 * or do it later when we drain, in case the peer is detached 592 * or if we're short of memory for M_PCSIG mblk. 593 */ 594 if (urgent) { 595 tcp_fuse_output_urg(tcp, mp); 596 597 mp1 = mp->b_cont; 598 } 599 600 if (tcp->tcp_ipversion == IPV4_VERSION && 601 (HOOKS4_INTERESTED_LOOPBACK_IN(ipst) || 602 HOOKS4_INTERESTED_LOOPBACK_OUT(ipst)) || 603 tcp->tcp_ipversion == IPV6_VERSION && 604 (HOOKS6_INTERESTED_LOOPBACK_IN(ipst) || 605 HOOKS6_INTERESTED_LOOPBACK_OUT(ipst))) { 606 /* 607 * Build ip and tcp header to satisfy FW_HOOKS. 608 * We only build it when any hook is present. 609 */ 610 if ((mp1 = tcp_xmit_mp(tcp, mp1, tcp->tcp_mss, NULL, NULL, 611 tcp->tcp_snxt, B_TRUE, NULL, B_FALSE)) == NULL) 612 /* If tcp_xmit_mp fails, use regular path */ 613 goto unfuse; 614 615 /* 616 * The ipif and ill can be safely referenced under the 617 * protection of conn_lock - see head of function comment for 618 * conn_get_held_ipif(). It is necessary to check that both 619 * the ipif and ill can be looked up (i.e. not condemned). If 620 * not, bail out and unfuse this connection. 621 */ 622 mutex_enter(&peer_tcp->tcp_connp->conn_lock); 623 if ((peer_tcp->tcp_connp->conn_ire_cache == NULL) || 624 (peer_tcp->tcp_connp->conn_ire_cache->ire_marks & 625 IRE_MARK_CONDEMNED) || 626 ((oifp = peer_tcp->tcp_connp->conn_ire_cache->ire_ipif) 627 == NULL) || 628 (!IPIF_CAN_LOOKUP(oifp)) || 629 ((olp = oifp->ipif_ill) == NULL) || 630 (ill_check_and_refhold(olp) != 0)) { 631 mutex_exit(&peer_tcp->tcp_connp->conn_lock); 632 goto unfuse; 633 } 634 mutex_exit(&peer_tcp->tcp_connp->conn_lock); 635 636 /* PFHooks: LOOPBACK_OUT */ 637 if (tcp->tcp_ipversion == IPV4_VERSION) { 638 ipha = (ipha_t *)mp1->b_rptr; 639 640 DTRACE_PROBE4(ip4__loopback__out__start, 641 ill_t *, NULL, ill_t *, olp, 642 ipha_t *, ipha, mblk_t *, mp1); 643 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 644 ipst->ips_ipv4firewall_loopback_out, 645 NULL, olp, ipha, mp1, mp1, 0, ipst); 646 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, mp1); 647 } else { 648 ip6h = (ip6_t *)mp1->b_rptr; 649 650 DTRACE_PROBE4(ip6__loopback__out__start, 651 ill_t *, NULL, ill_t *, olp, 652 ip6_t *, ip6h, mblk_t *, mp1); 653 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 654 ipst->ips_ipv6firewall_loopback_out, 655 NULL, olp, ip6h, mp1, mp1, 0, ipst); 656 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, mp1); 657 } 658 ill_refrele(olp); 659 660 if (mp1 == NULL) 661 goto unfuse; 662 663 /* 664 * The ipif and ill can be safely referenced under the 665 * protection of conn_lock - see head of function comment for 666 * conn_get_held_ipif(). It is necessary to check that both 667 * the ipif and ill can be looked up (i.e. not condemned). If 668 * not, bail out and unfuse this connection. 669 */ 670 mutex_enter(&tcp->tcp_connp->conn_lock); 671 if ((tcp->tcp_connp->conn_ire_cache == NULL) || 672 (tcp->tcp_connp->conn_ire_cache->ire_marks & 673 IRE_MARK_CONDEMNED) || 674 ((iifp = tcp->tcp_connp->conn_ire_cache->ire_ipif) 675 == NULL) || 676 (!IPIF_CAN_LOOKUP(iifp)) || 677 ((ilp = iifp->ipif_ill) == NULL) || 678 (ill_check_and_refhold(ilp) != 0)) { 679 mutex_exit(&tcp->tcp_connp->conn_lock); 680 goto unfuse; 681 } 682 mutex_exit(&tcp->tcp_connp->conn_lock); 683 684 /* PFHooks: LOOPBACK_IN */ 685 if (tcp->tcp_ipversion == IPV4_VERSION) { 686 DTRACE_PROBE4(ip4__loopback__in__start, 687 ill_t *, ilp, ill_t *, NULL, 688 ipha_t *, ipha, mblk_t *, mp1); 689 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 690 ipst->ips_ipv4firewall_loopback_in, 691 ilp, NULL, ipha, mp1, mp1, 0, ipst); 692 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, mp1); 693 ill_refrele(ilp); 694 if (mp1 == NULL) 695 goto unfuse; 696 697 ip_hdr_len = IPH_HDR_LENGTH(ipha); 698 } else { 699 DTRACE_PROBE4(ip6__loopback__in__start, 700 ill_t *, ilp, ill_t *, NULL, 701 ip6_t *, ip6h, mblk_t *, mp1); 702 FW_HOOKS6(ipst->ips_ip6_loopback_in_event, 703 ipst->ips_ipv6firewall_loopback_in, 704 ilp, NULL, ip6h, mp1, mp1, 0, ipst); 705 DTRACE_PROBE1(ip6__loopback__in__end, mblk_t *, mp1); 706 ill_refrele(ilp); 707 if (mp1 == NULL) 708 goto unfuse; 709 710 ip_hdr_len = ip_hdr_length_v6(mp1, ip6h); 711 } 712 713 /* Data length might be changed by FW_HOOKS */ 714 tcph = (tcph_t *)&mp1->b_rptr[ip_hdr_len]; 715 seq = ABE32_TO_U32(tcph->th_seq); 716 recv_size += seq - tcp->tcp_snxt; 717 718 /* 719 * The message duplicated by tcp_xmit_mp is freed. 720 * Note: the original message passed in remains unchanged. 721 */ 722 freemsg(mp1); 723 } 724 725 /* 726 * Enqueue data into the peer's receive list; we may or may not 727 * drain the contents depending on the conditions below. 728 * 729 * For non-STREAMS sockets we normally queue data directly in the 730 * socket by calling the su_recv upcall. However, if the peer is 731 * detached we use tcp_rcv_enqueue() instead. Queued data will be 732 * drained when the accept completes (in tcp_accept_finish()). 733 */ 734 if (IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 735 !TCP_IS_DETACHED(peer_tcp)) { 736 int error; 737 int flags = 0; 738 739 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 740 (tcp->tcp_urg == tcp->tcp_snxt)) { 741 flags = MSG_OOB; 742 (*peer_tcp->tcp_connp->conn_upcalls->su_signal_oob) 743 (peer_tcp->tcp_connp->conn_upper_handle, 0); 744 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 745 } 746 if ((*peer_tcp->tcp_connp->conn_upcalls->su_recv)( 747 peer_tcp->tcp_connp->conn_upper_handle, mp, recv_size, 748 flags, &error, &push) < 0) { 749 ASSERT(error != EOPNOTSUPP); 750 peer_data_queued = B_TRUE; 751 } 752 } else { 753 if (IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 754 (tcp->tcp_valid_bits & TCP_URG_VALID) && 755 (tcp->tcp_urg == tcp->tcp_snxt)) { 756 /* 757 * Can not deal with urgent pointers 758 * that arrive before the connection has been 759 * accept()ed. 760 */ 761 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 762 freemsg(mp); 763 return (B_TRUE); 764 } 765 766 tcp_rcv_enqueue(peer_tcp, mp, recv_size); 767 768 /* In case it wrapped around and also to keep it constant */ 769 peer_tcp->tcp_rwnd += recv_size; 770 } 771 772 /* 773 * Exercise flow-control when needed; we will get back-enabled 774 * in either tcp_accept_finish(), tcp_unfuse(), or when data is 775 * consumed. If peer endpoint is detached, we emulate streams flow 776 * control by checking the peer's queue size and high water mark; 777 * otherwise we simply use canputnext() to decide if we need to stop 778 * our flow. 779 * 780 * Since we are accessing our tcp_flow_stopped and might modify it, 781 * we need to take tcp->tcp_non_sq_lock. 782 */ 783 mutex_enter(&tcp->tcp_non_sq_lock); 784 flow_stopped = tcp->tcp_flow_stopped; 785 if ((TCP_IS_DETACHED(peer_tcp) && 786 (peer_tcp->tcp_rcv_cnt >= peer_tcp->tcp_fuse_rcv_hiwater)) || 787 (!TCP_IS_DETACHED(peer_tcp) && 788 !IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 789 !canputnext(peer_tcp->tcp_rq))) { 790 peer_data_queued = B_TRUE; 791 } 792 793 if (!flow_stopped && (peer_data_queued || 794 (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater))) { 795 tcp_setqfull(tcp); 796 flow_stopped = B_TRUE; 797 TCP_STAT(tcps, tcp_fusion_flowctl); 798 DTRACE_PROBE3(tcp__fuse__output__flowctl, tcp_t *, tcp, 799 uint_t, send_size, uint_t, peer_tcp->tcp_rcv_cnt); 800 } else if (flow_stopped && !peer_data_queued && 801 (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater)) { 802 tcp_clrqfull(tcp); 803 TCP_STAT(tcps, tcp_fusion_backenabled); 804 flow_stopped = B_FALSE; 805 } 806 mutex_exit(&tcp->tcp_non_sq_lock); 807 808 ipst->ips_loopback_packets++; 809 tcp->tcp_last_sent_len = send_size; 810 811 /* Need to adjust the following SNMP MIB-related variables */ 812 tcp->tcp_snxt += send_size; 813 tcp->tcp_suna = tcp->tcp_snxt; 814 peer_tcp->tcp_rnxt += recv_size; 815 peer_tcp->tcp_rack = peer_tcp->tcp_rnxt; 816 817 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 818 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, send_size); 819 820 BUMP_MIB(&tcps->tcps_mib, tcpInSegs); 821 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 822 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, send_size); 823 824 BUMP_LOCAL(tcp->tcp_obsegs); 825 BUMP_LOCAL(peer_tcp->tcp_ibsegs); 826 827 DTRACE_PROBE2(tcp__fuse__output, tcp_t *, tcp, uint_t, send_size); 828 829 if (!IPCL_IS_NONSTR(peer_tcp->tcp_connp) && 830 !TCP_IS_DETACHED(peer_tcp)) { 831 /* 832 * Drain the peer's receive queue it has urgent data or if 833 * we're not flow-controlled. 834 */ 835 if (urgent || !flow_stopped) { 836 ASSERT(peer_tcp->tcp_rcv_list != NULL); 837 /* 838 * For TLI-based streams, a thread in tcp_accept_swap() 839 * can race with us. That thread will ensure that the 840 * correct peer_tcp->tcp_rq is globally visible before 841 * peer_tcp->tcp_detached is visible as clear, but we 842 * must also ensure that the load of tcp_rq cannot be 843 * reordered to be before the tcp_detached check. 844 */ 845 membar_consumer(); 846 (void) tcp_fuse_rcv_drain(peer_tcp->tcp_rq, peer_tcp, 847 NULL); 848 } 849 } 850 return (B_TRUE); 851 unfuse: 852 tcp_unfuse(tcp); 853 return (B_FALSE); 854 } 855 856 /* 857 * This routine gets called to deliver data upstream on a fused or 858 * previously fused tcp loopback endpoint; the latter happens only 859 * when there is a pending SIGURG signal plus urgent data that can't 860 * be sent upstream in the past. 861 */ 862 boolean_t 863 tcp_fuse_rcv_drain(queue_t *q, tcp_t *tcp, mblk_t **sigurg_mpp) 864 { 865 mblk_t *mp; 866 conn_t *connp = tcp->tcp_connp; 867 868 #ifdef DEBUG 869 uint_t cnt = 0; 870 #endif 871 tcp_stack_t *tcps = tcp->tcp_tcps; 872 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 873 874 ASSERT(tcp->tcp_loopback); 875 ASSERT(tcp->tcp_fused || tcp->tcp_fused_sigurg); 876 ASSERT(!tcp->tcp_fused || tcp->tcp_loopback_peer != NULL); 877 ASSERT(IPCL_IS_NONSTR(connp) || sigurg_mpp != NULL || tcp->tcp_fused); 878 879 /* No need for the push timer now, in case it was scheduled */ 880 if (tcp->tcp_push_tid != 0) { 881 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 882 tcp->tcp_push_tid = 0; 883 } 884 /* 885 * If there's urgent data sitting in receive list and we didn't 886 * get a chance to send up a SIGURG signal, make sure we send 887 * it first before draining in order to ensure that SIOCATMARK 888 * works properly. 889 */ 890 if (tcp->tcp_fused_sigurg) { 891 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 892 893 tcp->tcp_fused_sigurg = B_FALSE; 894 /* 895 * sigurg_mpp is normally NULL, i.e. when we're still 896 * fused and didn't get here because of tcp_unfuse(). 897 * In this case try hard to allocate the M_PCSIG mblk. 898 */ 899 if (sigurg_mpp == NULL && 900 (mp = allocb(1, BPRI_HI)) == NULL && 901 (mp = allocb_tryhard(1)) == NULL) { 902 /* Alloc failed; try again next time */ 903 tcp->tcp_push_tid = TCP_TIMER(tcp, 904 tcp_push_timer, 905 MSEC_TO_TICK( 906 tcps->tcps_push_timer_interval)); 907 return (B_TRUE); 908 } else if (sigurg_mpp != NULL) { 909 /* 910 * Use the supplied M_PCSIG mblk; it means we're 911 * either unfused or in the process of unfusing, 912 * and the drain must happen now. 913 */ 914 mp = *sigurg_mpp; 915 *sigurg_mpp = NULL; 916 } 917 ASSERT(mp != NULL); 918 919 /* Send up the signal */ 920 DB_TYPE(mp) = M_PCSIG; 921 *mp->b_wptr++ = (uchar_t)SIGURG; 922 putnext(q, mp); 923 924 /* 925 * Let the regular tcp_rcv_drain() path handle 926 * draining the data if we're no longer fused. 927 */ 928 if (!tcp->tcp_fused) 929 return (B_FALSE); 930 } 931 932 /* Drain the data */ 933 while ((mp = tcp->tcp_rcv_list) != NULL) { 934 tcp->tcp_rcv_list = mp->b_next; 935 mp->b_next = NULL; 936 #ifdef DEBUG 937 cnt += msgdsize(mp); 938 #endif 939 ASSERT(!IPCL_IS_NONSTR(connp)); 940 putnext(q, mp); 941 TCP_STAT(tcps, tcp_fusion_putnext); 942 } 943 944 #ifdef DEBUG 945 ASSERT(cnt == tcp->tcp_rcv_cnt); 946 #endif 947 tcp->tcp_rcv_last_head = NULL; 948 tcp->tcp_rcv_last_tail = NULL; 949 tcp->tcp_rcv_cnt = 0; 950 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 951 952 mutex_enter(&peer_tcp->tcp_non_sq_lock); 953 if (peer_tcp->tcp_flow_stopped && (TCP_UNSENT_BYTES(peer_tcp) <= 954 peer_tcp->tcp_xmit_lowater)) { 955 tcp_clrqfull(peer_tcp); 956 TCP_STAT(tcps, tcp_fusion_backenabled); 957 } 958 mutex_exit(&peer_tcp->tcp_non_sq_lock); 959 960 return (B_TRUE); 961 } 962 963 /* 964 * Calculate the size of receive buffer for a fused tcp endpoint. 965 */ 966 size_t 967 tcp_fuse_set_rcv_hiwat(tcp_t *tcp, size_t rwnd) 968 { 969 tcp_stack_t *tcps = tcp->tcp_tcps; 970 971 ASSERT(tcp->tcp_fused); 972 973 /* Ensure that value is within the maximum upper bound */ 974 if (rwnd > tcps->tcps_max_buf) 975 rwnd = tcps->tcps_max_buf; 976 977 /* Obey the absolute minimum tcp receive high water mark */ 978 if (rwnd < tcps->tcps_sth_rcv_hiwat) 979 rwnd = tcps->tcps_sth_rcv_hiwat; 980 981 /* 982 * Round up to system page size in case SO_RCVBUF is modified 983 * after SO_SNDBUF; the latter is also similarly rounded up. 984 */ 985 rwnd = P2ROUNDUP_TYPED(rwnd, PAGESIZE, size_t); 986 tcp->tcp_fuse_rcv_hiwater = rwnd; 987 return (rwnd); 988 } 989 990 /* 991 * Calculate the maximum outstanding unread data block for a fused tcp endpoint. 992 */ 993 int 994 tcp_fuse_maxpsz_set(tcp_t *tcp) 995 { 996 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 997 uint_t sndbuf = tcp->tcp_xmit_hiwater; 998 uint_t maxpsz = sndbuf; 999 1000 ASSERT(tcp->tcp_fused); 1001 ASSERT(peer_tcp != NULL); 1002 ASSERT(peer_tcp->tcp_fuse_rcv_hiwater != 0); 1003 /* 1004 * In the fused loopback case, we want the stream head to split 1005 * up larger writes into smaller chunks for a more accurate flow- 1006 * control accounting. Our maxpsz is half of the sender's send 1007 * buffer or the receiver's receive buffer, whichever is smaller. 1008 * We round up the buffer to system page size due to the lack of 1009 * TCP MSS concept in Fusion. 1010 */ 1011 if (maxpsz > peer_tcp->tcp_fuse_rcv_hiwater) 1012 maxpsz = peer_tcp->tcp_fuse_rcv_hiwater; 1013 maxpsz = P2ROUNDUP_TYPED(maxpsz, PAGESIZE, uint_t) >> 1; 1014 1015 return (maxpsz); 1016 } 1017 1018 /* 1019 * Called to release flow control. 1020 */ 1021 void 1022 tcp_fuse_backenable(tcp_t *tcp) 1023 { 1024 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 1025 1026 ASSERT(tcp->tcp_fused); 1027 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 1028 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 1029 ASSERT(!TCP_IS_DETACHED(tcp)); 1030 ASSERT(tcp->tcp_connp->conn_sqp == 1031 peer_tcp->tcp_connp->conn_sqp); 1032 1033 if (tcp->tcp_rcv_list != NULL) 1034 (void) tcp_fuse_rcv_drain(tcp->tcp_rq, tcp, NULL); 1035 1036 mutex_enter(&peer_tcp->tcp_non_sq_lock); 1037 if (peer_tcp->tcp_flow_stopped && 1038 (TCP_UNSENT_BYTES(peer_tcp) <= 1039 peer_tcp->tcp_xmit_lowater)) { 1040 tcp_clrqfull(peer_tcp); 1041 } 1042 mutex_exit(&peer_tcp->tcp_non_sq_lock); 1043 1044 TCP_STAT(tcp->tcp_tcps, tcp_fusion_backenabled); 1045 } 1046