Print this page
11547 Want connstat(1M) command to display per-connection TCP statistics
Portions contributed by: Cody Peter Mello <cody.mello@joyent.com>
Portions contributed by: Ahmed G <ahmedg@delphix.com>
Reviewed by: Jason King <jason.king@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Dan McDonald <danmcd@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/tcp/tcp_output.c
+++ new/usr/src/uts/common/inet/tcp/tcp_output.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
25 25 * Copyright 2019 Joyent, Inc.
26 26 */
27 27
28 28 /* This file contains all TCP output processing functions. */
29 29
30 30 #include <sys/types.h>
31 31 #include <sys/stream.h>
32 32 #include <sys/strsun.h>
33 33 #include <sys/strsubr.h>
34 34 #include <sys/stropts.h>
35 35 #include <sys/strlog.h>
36 36 #define _SUN_TPI_VERSION 2
37 37 #include <sys/tihdr.h>
38 38 #include <sys/suntpi.h>
39 39 #include <sys/xti_inet.h>
40 40 #include <sys/timod.h>
41 41 #include <sys/pattr.h>
42 42 #include <sys/squeue_impl.h>
43 43 #include <sys/squeue.h>
44 44 #include <sys/sockio.h>
45 45 #include <sys/tsol/tnet.h>
46 46
47 47 #include <inet/common.h>
48 48 #include <inet/ip.h>
49 49 #include <inet/tcp.h>
50 50 #include <inet/tcp_impl.h>
51 51 #include <inet/snmpcom.h>
52 52 #include <inet/proto_set.h>
53 53 #include <inet/ipsec_impl.h>
54 54 #include <inet/ip_ndp.h>
55 55
56 56 static mblk_t *tcp_get_seg_mp(tcp_t *, uint32_t, int32_t *);
57 57 static void tcp_wput_cmdblk(queue_t *, mblk_t *);
58 58 static void tcp_wput_flush(tcp_t *, mblk_t *);
59 59 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp);
60 60 static int tcp_xmit_end(tcp_t *);
61 61 static int tcp_send(tcp_t *, const int, const int, const int,
62 62 const int, int *, uint32_t *, int *, mblk_t **, mblk_t *);
63 63 static void tcp_xmit_early_reset(char *, mblk_t *, uint32_t, uint32_t,
64 64 int, ip_recv_attr_t *, ip_stack_t *, conn_t *);
65 65 static boolean_t tcp_send_rst_chk(tcp_stack_t *);
66 66 static void tcp_process_shrunk_swnd(tcp_t *, uint32_t);
67 67 static void tcp_fill_header(tcp_t *, uchar_t *, int);
68 68
69 69 /*
70 70 * Functions called directly via squeue having a prototype of edesc_t.
71 71 */
72 72 static void tcp_wput_nondata(void *, mblk_t *, void *, ip_recv_attr_t *);
73 73 static void tcp_wput_ioctl(void *, mblk_t *, void *, ip_recv_attr_t *);
74 74 static void tcp_wput_proto(void *, mblk_t *, void *, ip_recv_attr_t *);
75 75
76 76 /*
77 77 * This controls how tiny a write must be before we try to copy it
78 78 * into the mblk on the tail of the transmit queue. Not much
79 79 * speedup is observed for values larger than sixteen. Zero will
80 80 * disable the optimisation.
81 81 */
82 82 static int tcp_tx_pull_len = 16;
83 83
84 84 int
85 85 tcp_wput(queue_t *q, mblk_t *mp)
86 86 {
87 87 conn_t *connp = Q_TO_CONN(q);
88 88 tcp_t *tcp;
89 89 void (*output_proc)();
90 90 t_scalar_t type;
91 91 uchar_t *rptr;
92 92 struct iocblk *iocp;
93 93 size_t size;
94 94
95 95 ASSERT(connp->conn_ref >= 2);
96 96
97 97 switch (DB_TYPE(mp)) {
98 98 case M_DATA:
99 99 tcp = connp->conn_tcp;
100 100 ASSERT(tcp != NULL);
101 101
102 102 size = msgdsize(mp);
103 103
104 104 mutex_enter(&tcp->tcp_non_sq_lock);
105 105 tcp->tcp_squeue_bytes += size;
106 106 if (TCP_UNSENT_BYTES(tcp) > connp->conn_sndbuf) {
107 107 tcp_setqfull(tcp);
108 108 }
109 109 mutex_exit(&tcp->tcp_non_sq_lock);
110 110
111 111 CONN_INC_REF(connp);
112 112 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp,
113 113 NULL, tcp_squeue_flag, SQTAG_TCP_OUTPUT);
114 114 return (0);
115 115
116 116 case M_CMD:
117 117 tcp_wput_cmdblk(q, mp);
118 118 return (0);
119 119
120 120 case M_PROTO:
121 121 case M_PCPROTO:
122 122 /*
123 123 * if it is a snmp message, don't get behind the squeue
124 124 */
125 125 tcp = connp->conn_tcp;
126 126 rptr = mp->b_rptr;
127 127 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
128 128 type = ((union T_primitives *)rptr)->type;
129 129 } else {
130 130 if (connp->conn_debug) {
131 131 (void) strlog(TCP_MOD_ID, 0, 1,
132 132 SL_ERROR|SL_TRACE,
133 133 "tcp_wput_proto, dropping one...");
134 134 }
135 135 freemsg(mp);
136 136 return (0);
137 137 }
138 138 if (type == T_SVR4_OPTMGMT_REQ) {
139 139 /*
140 140 * All Solaris components should pass a db_credp
141 141 * for this TPI message, hence we ASSERT.
142 142 * But in case there is some other M_PROTO that looks
143 143 * like a TPI message sent by some other kernel
144 144 * component, we check and return an error.
145 145 */
146 146 cred_t *cr = msg_getcred(mp, NULL);
147 147
148 148 ASSERT(cr != NULL);
149 149 if (cr == NULL) {
150 150 tcp_err_ack(tcp, mp, TSYSERR, EINVAL);
151 151 return (0);
152 152 }
153 153 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get,
154 154 cr)) {
155 155 /*
156 156 * This was a SNMP request
157 157 */
158 158 return (0);
159 159 } else {
160 160 output_proc = tcp_wput_proto;
161 161 }
162 162 } else {
163 163 output_proc = tcp_wput_proto;
164 164 }
165 165 break;
166 166 case M_IOCTL:
167 167 /*
168 168 * Most ioctls can be processed right away without going via
169 169 * squeues - process them right here. Those that do require
170 170 * squeue (currently _SIOCSOCKFALLBACK)
171 171 * are processed by tcp_wput_ioctl().
172 172 */
173 173 iocp = (struct iocblk *)mp->b_rptr;
174 174 tcp = connp->conn_tcp;
175 175
176 176 switch (iocp->ioc_cmd) {
177 177 case TCP_IOC_ABORT_CONN:
178 178 tcp_ioctl_abort_conn(q, mp);
179 179 return (0);
180 180 case TI_GETPEERNAME:
181 181 case TI_GETMYNAME:
182 182 mi_copyin(q, mp, NULL,
183 183 SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
184 184 return (0);
185 185
186 186 default:
187 187 output_proc = tcp_wput_ioctl;
188 188 break;
189 189 }
190 190 break;
191 191 default:
192 192 output_proc = tcp_wput_nondata;
193 193 break;
194 194 }
195 195
196 196 CONN_INC_REF(connp);
197 197 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp,
198 198 NULL, tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER);
199 199 return (0);
200 200 }
201 201
202 202 /*
203 203 * The TCP normal data output path.
204 204 * NOTE: the logic of the fast path is duplicated from this function.
205 205 */
206 206 void
207 207 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent)
208 208 {
209 209 int len;
210 210 mblk_t *local_time;
211 211 mblk_t *mp1;
212 212 uint32_t snxt;
213 213 int tail_unsent;
214 214 int tcpstate;
215 215 int usable = 0;
216 216 mblk_t *xmit_tail;
217 217 int32_t mss;
218 218 int32_t num_sack_blk = 0;
219 219 int32_t total_hdr_len;
220 220 int32_t tcp_hdr_len;
221 221 int rc;
222 222 tcp_stack_t *tcps = tcp->tcp_tcps;
223 223 conn_t *connp = tcp->tcp_connp;
224 224 clock_t now = LBOLT_FASTPATH;
225 225
226 226 tcpstate = tcp->tcp_state;
227 227 if (mp == NULL) {
228 228 /*
229 229 * tcp_wput_data() with NULL mp should only be called when
230 230 * there is unsent data.
231 231 */
232 232 ASSERT(tcp->tcp_unsent > 0);
233 233 /* Really tacky... but we need this for detached closes. */
234 234 len = tcp->tcp_unsent;
235 235 goto data_null;
236 236 }
237 237
238 238 ASSERT(mp->b_datap->db_type == M_DATA);
239 239 /*
240 240 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
241 241 * or before a connection attempt has begun.
242 242 */
243 243 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT ||
244 244 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
245 245 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
246 246 #ifdef DEBUG
247 247 cmn_err(CE_WARN,
248 248 "tcp_wput_data: data after ordrel, %s",
249 249 tcp_display(tcp, NULL,
250 250 DISP_ADDR_AND_PORT));
251 251 #else
252 252 if (connp->conn_debug) {
253 253 (void) strlog(TCP_MOD_ID, 0, 1,
254 254 SL_TRACE|SL_ERROR,
255 255 "tcp_wput_data: data after ordrel, %s\n",
256 256 tcp_display(tcp, NULL,
257 257 DISP_ADDR_AND_PORT));
258 258 }
259 259 #endif /* DEBUG */
260 260 }
261 261 if (tcp->tcp_snd_zcopy_aware &&
262 262 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
263 263 tcp_zcopy_notify(tcp);
264 264 freemsg(mp);
265 265 mutex_enter(&tcp->tcp_non_sq_lock);
266 266 if (tcp->tcp_flow_stopped &&
267 267 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
268 268 tcp_clrqfull(tcp);
269 269 }
270 270 mutex_exit(&tcp->tcp_non_sq_lock);
271 271 return;
272 272 }
273 273
274 274 /* Strip empties */
275 275 for (;;) {
276 276 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
277 277 (uintptr_t)INT_MAX);
278 278 len = (int)(mp->b_wptr - mp->b_rptr);
279 279 if (len > 0)
280 280 break;
281 281 mp1 = mp;
282 282 mp = mp->b_cont;
283 283 freeb(mp1);
284 284 if (mp == NULL) {
285 285 return;
286 286 }
287 287 }
288 288
289 289 /* If we are the first on the list ... */
290 290 if (tcp->tcp_xmit_head == NULL) {
291 291 tcp->tcp_xmit_head = mp;
292 292 tcp->tcp_xmit_tail = mp;
293 293 tcp->tcp_xmit_tail_unsent = len;
294 294 } else {
295 295 /* If tiny tx and room in txq tail, pullup to save mblks. */
296 296 struct datab *dp;
297 297
298 298 mp1 = tcp->tcp_xmit_last;
299 299 if (len < tcp_tx_pull_len &&
300 300 (dp = mp1->b_datap)->db_ref == 1 &&
301 301 dp->db_lim - mp1->b_wptr >= len) {
302 302 ASSERT(len > 0);
303 303 ASSERT(!mp1->b_cont);
304 304 if (len == 1) {
305 305 *mp1->b_wptr++ = *mp->b_rptr;
306 306 } else {
307 307 bcopy(mp->b_rptr, mp1->b_wptr, len);
308 308 mp1->b_wptr += len;
309 309 }
310 310 if (mp1 == tcp->tcp_xmit_tail)
311 311 tcp->tcp_xmit_tail_unsent += len;
312 312 mp1->b_cont = mp->b_cont;
313 313 if (tcp->tcp_snd_zcopy_aware &&
314 314 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY))
315 315 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
316 316 freeb(mp);
317 317 mp = mp1;
318 318 } else {
319 319 tcp->tcp_xmit_last->b_cont = mp;
320 320 }
321 321 len += tcp->tcp_unsent;
322 322 }
323 323
324 324 /* Tack on however many more positive length mblks we have */
325 325 if ((mp1 = mp->b_cont) != NULL) {
326 326 do {
327 327 int tlen;
328 328 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <=
329 329 (uintptr_t)INT_MAX);
330 330 tlen = (int)(mp1->b_wptr - mp1->b_rptr);
331 331 if (tlen <= 0) {
332 332 mp->b_cont = mp1->b_cont;
333 333 freeb(mp1);
334 334 } else {
335 335 len += tlen;
336 336 mp = mp1;
337 337 }
338 338 } while ((mp1 = mp->b_cont) != NULL);
339 339 }
340 340 tcp->tcp_xmit_last = mp;
341 341 tcp->tcp_unsent = len;
342 342
343 343 if (urgent)
344 344 usable = 1;
345 345
346 346 data_null:
347 347 snxt = tcp->tcp_snxt;
348 348 xmit_tail = tcp->tcp_xmit_tail;
349 349 tail_unsent = tcp->tcp_xmit_tail_unsent;
350 350
351 351 /*
352 352 * Note that tcp_mss has been adjusted to take into account the
353 353 * timestamp option if applicable. Because SACK options do not
354 354 * appear in every TCP segments and they are of variable lengths,
355 355 * they cannot be included in tcp_mss. Thus we need to calculate
356 356 * the actual segment length when we need to send a segment which
357 357 * includes SACK options.
358 358 */
359 359 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
360 360 int32_t opt_len;
361 361
362 362 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
363 363 tcp->tcp_num_sack_blk);
364 364 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN *
365 365 2 + TCPOPT_HEADER_LEN;
366 366 mss = tcp->tcp_mss - opt_len;
367 367 total_hdr_len = connp->conn_ht_iphc_len + opt_len;
368 368 tcp_hdr_len = connp->conn_ht_ulp_len + opt_len;
369 369 } else {
370 370 mss = tcp->tcp_mss;
371 371 total_hdr_len = connp->conn_ht_iphc_len;
372 372 tcp_hdr_len = connp->conn_ht_ulp_len;
373 373 }
374 374
375 375 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet &&
376 376 (TICK_TO_MSEC(now - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) {
377 377 TCP_SET_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle);
378 378 }
379 379 if (tcpstate == TCPS_SYN_RCVD) {
380 380 /*
381 381 * The three-way connection establishment handshake is not
382 382 * complete yet. We want to queue the data for transmission
383 383 * after entering ESTABLISHED state (RFC793). A jump to
384 384 * "done" label effectively leaves data on the queue.
385 385 */
386 386 goto done;
387 387 } else {
388 388 int usable_r;
389 389
390 390 /*
391 391 * In the special case when cwnd is zero, which can only
392 392 * happen if the connection is ECN capable, return now.
393 393 * New segments is sent using tcp_timer(). The timer
394 394 * is set in tcp_input_data().
395 395 */
396 396 if (tcp->tcp_cwnd == 0) {
397 397 /*
398 398 * Note that tcp_cwnd is 0 before 3-way handshake is
399 399 * finished.
400 400 */
401 401 ASSERT(tcp->tcp_ecn_ok ||
402 402 tcp->tcp_state < TCPS_ESTABLISHED);
403 403 return;
404 404 }
405 405
406 406 /* NOTE: trouble if xmitting while SYN not acked? */
407 407 usable_r = snxt - tcp->tcp_suna;
408 408 usable_r = tcp->tcp_swnd - usable_r;
409 409
410 410 /*
411 411 * Check if the receiver has shrunk the window. If
412 412 * tcp_wput_data() with NULL mp is called, tcp_fin_sent
413 413 * cannot be set as there is unsent data, so FIN cannot
414 414 * be sent out. Otherwise, we need to take into account
415 415 * of FIN as it consumes an "invisible" sequence number.
416 416 */
417 417 ASSERT(tcp->tcp_fin_sent == 0);
418 418 if (usable_r < 0) {
419 419 /*
420 420 * The receiver has shrunk the window and we have sent
421 421 * -usable_r date beyond the window, re-adjust.
422 422 *
423 423 * If TCP window scaling is enabled, there can be
424 424 * round down error as the advertised receive window
425 425 * is actually right shifted n bits. This means that
426 426 * the lower n bits info is wiped out. It will look
427 427 * like the window is shrunk. Do a check here to
428 428 * see if the shrunk amount is actually within the
429 429 * error in window calculation. If it is, just
430 430 * return. Note that this check is inside the
431 431 * shrunk window check. This makes sure that even
432 432 * though tcp_process_shrunk_swnd() is not called,
433 433 * we will stop further processing.
434 434 */
435 435 if ((-usable_r >> tcp->tcp_snd_ws) > 0) {
436 436 tcp_process_shrunk_swnd(tcp, -usable_r);
437 437 }
438 438 return;
439 439 }
440 440
441 441 /* usable = MIN(swnd, cwnd) - unacked_bytes */
442 442 if (tcp->tcp_swnd > tcp->tcp_cwnd)
443 443 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd;
444 444
445 445 /* usable = MIN(usable, unsent) */
446 446 if (usable_r > len)
447 447 usable_r = len;
448 448
449 449 /* usable = MAX(usable, {1 for urgent, 0 for data}) */
450 450 if (usable_r > 0) {
451 451 usable = usable_r;
452 452 } else {
453 453 /* Bypass all other unnecessary processing. */
454 454 goto done;
455 455 }
456 456 }
457 457
458 458 local_time = (mblk_t *)(intptr_t)gethrtime();
459 459
460 460 /*
461 461 * "Our" Nagle Algorithm. This is not the same as in the old
462 462 * BSD. This is more in line with the true intent of Nagle.
463 463 *
464 464 * The conditions are:
465 465 * 1. The amount of unsent data (or amount of data which can be
466 466 * sent, whichever is smaller) is less than Nagle limit.
467 467 * 2. The last sent size is also less than Nagle limit.
468 468 * 3. There is unack'ed data.
469 469 * 4. Urgent pointer is not set. Send urgent data ignoring the
470 470 * Nagle algorithm. This reduces the probability that urgent
471 471 * bytes get "merged" together.
472 472 * 5. The app has not closed the connection. This eliminates the
473 473 * wait time of the receiving side waiting for the last piece of
474 474 * (small) data.
475 475 *
476 476 * If all are satisified, exit without sending anything. Note
477 477 * that Nagle limit can be smaller than 1 MSS. Nagle limit is
478 478 * the smaller of 1 MSS and global tcp_naglim_def (default to be
479 479 * 4095).
480 480 */
481 481 if (usable < (int)tcp->tcp_naglim &&
482 482 tcp->tcp_naglim > tcp->tcp_last_sent_len &&
483 483 snxt != tcp->tcp_suna &&
484 484 !(tcp->tcp_valid_bits & TCP_URG_VALID) &&
485 485 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) {
486 486 goto done;
487 487 }
488 488
489 489 /*
490 490 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option
491 491 * is set, then we have to force TCP not to send partial segment
492 492 * (smaller than MSS bytes). We are calculating the usable now
493 493 * based on full mss and will save the rest of remaining data for
494 494 * later. When tcp_zero_win_probe is set, TCP needs to send out
495 495 * something to do zero window probe.
496 496 */
497 497 if (tcp->tcp_cork && !tcp->tcp_zero_win_probe) {
498 498 if (usable < mss)
499 499 goto done;
500 500 usable = (usable / mss) * mss;
501 501 }
502 502
503 503 /* Update the latest receive window size in TCP header. */
504 504 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
505 505
506 506 /* Send the packet. */
507 507 rc = tcp_send(tcp, mss, total_hdr_len, tcp_hdr_len,
508 508 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail,
509 509 local_time);
510 510
511 511 /* Pretend that all we were trying to send really got sent */
512 512 if (rc < 0 && tail_unsent < 0) {
513 513 do {
514 514 xmit_tail = xmit_tail->b_cont;
515 515 xmit_tail->b_prev = local_time;
516 516 ASSERT((uintptr_t)(xmit_tail->b_wptr -
517 517 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
518 518 tail_unsent += (int)(xmit_tail->b_wptr -
519 519 xmit_tail->b_rptr);
520 520 } while (tail_unsent < 0);
521 521 }
522 522 done:;
523 523 tcp->tcp_xmit_tail = xmit_tail;
524 524 tcp->tcp_xmit_tail_unsent = tail_unsent;
525 525 len = tcp->tcp_snxt - snxt;
526 526 if (len) {
527 527 /*
528 528 * If new data was sent, need to update the notsack
529 529 * list, which is, afterall, data blocks that have
530 530 * not been sack'ed by the receiver. New data is
531 531 * not sack'ed.
532 532 */
533 533 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
534 534 /* len is a negative value. */
535 535 tcp->tcp_pipe -= len;
536 536 tcp_notsack_update(&(tcp->tcp_notsack_list),
537 537 tcp->tcp_snxt, snxt,
538 538 &(tcp->tcp_num_notsack_blk),
539 539 &(tcp->tcp_cnt_notsack_list));
540 540 }
541 541 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent;
542 542 tcp->tcp_rack = tcp->tcp_rnxt;
543 543 tcp->tcp_rack_cnt = 0;
544 544 if ((snxt + len) == tcp->tcp_suna) {
545 545 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
546 546 }
547 547 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) {
548 548 /*
549 549 * Didn't send anything. Make sure the timer is running
550 550 * so that we will probe a zero window.
551 551 */
552 552 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
553 553 }
554 554 /* Note that len is the amount we just sent but with a negative sign */
555 555 tcp->tcp_unsent += len;
556 556 mutex_enter(&tcp->tcp_non_sq_lock);
557 557 if (tcp->tcp_flow_stopped) {
558 558 if (TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
559 559 tcp_clrqfull(tcp);
560 560 }
561 561 } else if (TCP_UNSENT_BYTES(tcp) >= connp->conn_sndbuf) {
562 562 if (!(tcp->tcp_detached))
563 563 tcp_setqfull(tcp);
564 564 }
565 565 mutex_exit(&tcp->tcp_non_sq_lock);
566 566 }
567 567
568 568 /*
569 569 * Initial STREAMS write side put() procedure for sockets. It tries to
570 570 * handle the T_CAPABILITY_REQ which sockfs sends down while setting
571 571 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages
572 572 * are handled by tcp_wput() as usual.
573 573 *
574 574 * All further messages will also be handled by tcp_wput() because we cannot
575 575 * be sure that the above short cut is safe later.
576 576 */
577 577 int
578 578 tcp_wput_sock(queue_t *wq, mblk_t *mp)
579 579 {
580 580 conn_t *connp = Q_TO_CONN(wq);
581 581 tcp_t *tcp = connp->conn_tcp;
582 582 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr;
583 583
584 584 ASSERT(wq->q_qinfo == &tcp_sock_winit);
585 585 wq->q_qinfo = &tcp_winit;
586 586
587 587 ASSERT(IPCL_IS_TCP(connp));
588 588 ASSERT(TCP_IS_SOCKET(tcp));
589 589
590 590 if (DB_TYPE(mp) == M_PCPROTO &&
591 591 MBLKL(mp) == sizeof (struct T_capability_req) &&
592 592 car->PRIM_type == T_CAPABILITY_REQ) {
593 593 tcp_capability_req(tcp, mp);
594 594 return (0);
595 595 }
596 596
597 597 tcp_wput(wq, mp);
598 598 return (0);
599 599 }
600 600
601 601 /* ARGSUSED */
602 602 int
603 603 tcp_wput_fallback(queue_t *wq, mblk_t *mp)
604 604 {
605 605 #ifdef DEBUG
606 606 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n");
607 607 #endif
608 608 freemsg(mp);
609 609 return (0);
610 610 }
611 611
612 612 /*
613 613 * Call by tcp_wput() to handle misc non M_DATA messages.
614 614 */
615 615 /* ARGSUSED */
616 616 static void
617 617 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
618 618 {
619 619 conn_t *connp = (conn_t *)arg;
620 620 tcp_t *tcp = connp->conn_tcp;
621 621
622 622 ASSERT(DB_TYPE(mp) != M_IOCTL);
623 623 /*
624 624 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close.
625 625 * Once the close starts, streamhead and sockfs will not let any data
626 626 * packets come down (close ensures that there are no threads using the
627 627 * queue and no new threads will come down) but since qprocsoff()
628 628 * hasn't happened yet, a M_FLUSH or some non data message might
629 629 * get reflected back (in response to our own FLUSHRW) and get
630 630 * processed after tcp_close() is done. The conn would still be valid
631 631 * because a ref would have added but we need to check the state
632 632 * before actually processing the packet.
633 633 */
634 634 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) {
635 635 freemsg(mp);
636 636 return;
637 637 }
638 638
639 639 switch (DB_TYPE(mp)) {
640 640 case M_IOCDATA:
641 641 tcp_wput_iocdata(tcp, mp);
642 642 break;
643 643 case M_FLUSH:
644 644 tcp_wput_flush(tcp, mp);
645 645 break;
646 646 default:
647 647 ip_wput_nondata(connp->conn_wq, mp);
648 648 break;
649 649 }
650 650 }
651 651
652 652 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */
653 653 static void
654 654 tcp_wput_flush(tcp_t *tcp, mblk_t *mp)
655 655 {
656 656 uchar_t fval = *mp->b_rptr;
657 657 mblk_t *tail;
658 658 conn_t *connp = tcp->tcp_connp;
659 659 queue_t *q = connp->conn_wq;
660 660
661 661 /* TODO: How should flush interact with urgent data? */
662 662 if ((fval & FLUSHW) && tcp->tcp_xmit_head != NULL &&
663 663 !(tcp->tcp_valid_bits & TCP_URG_VALID)) {
664 664 /*
665 665 * Flush only data that has not yet been put on the wire. If
666 666 * we flush data that we have already transmitted, life, as we
667 667 * know it, may come to an end.
668 668 */
669 669 tail = tcp->tcp_xmit_tail;
670 670 tail->b_wptr -= tcp->tcp_xmit_tail_unsent;
671 671 tcp->tcp_xmit_tail_unsent = 0;
672 672 tcp->tcp_unsent = 0;
673 673 if (tail->b_wptr != tail->b_rptr)
674 674 tail = tail->b_cont;
675 675 if (tail) {
676 676 mblk_t **excess = &tcp->tcp_xmit_head;
677 677 for (;;) {
678 678 mblk_t *mp1 = *excess;
679 679 if (mp1 == tail)
680 680 break;
681 681 tcp->tcp_xmit_tail = mp1;
682 682 tcp->tcp_xmit_last = mp1;
683 683 excess = &mp1->b_cont;
684 684 }
685 685 *excess = NULL;
686 686 tcp_close_mpp(&tail);
687 687 if (tcp->tcp_snd_zcopy_aware)
688 688 tcp_zcopy_notify(tcp);
689 689 }
690 690 /*
691 691 * We have no unsent data, so unsent must be less than
692 692 * conn_sndlowat, so re-enable flow.
693 693 */
694 694 mutex_enter(&tcp->tcp_non_sq_lock);
695 695 if (tcp->tcp_flow_stopped) {
696 696 tcp_clrqfull(tcp);
697 697 }
698 698 mutex_exit(&tcp->tcp_non_sq_lock);
699 699 }
700 700 /*
701 701 * TODO: you can't just flush these, you have to increase rwnd for one
702 702 * thing. For another, how should urgent data interact?
703 703 */
704 704 if (fval & FLUSHR) {
705 705 *mp->b_rptr = fval & ~FLUSHW;
706 706 /* XXX */
707 707 qreply(q, mp);
708 708 return;
709 709 }
710 710 freemsg(mp);
711 711 }
712 712
713 713 /*
714 714 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA
715 715 * messages.
716 716 */
717 717 static void
718 718 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp)
719 719 {
720 720 mblk_t *mp1;
721 721 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
722 722 STRUCT_HANDLE(strbuf, sb);
723 723 uint_t addrlen;
724 724 conn_t *connp = tcp->tcp_connp;
725 725 queue_t *q = connp->conn_wq;
726 726
727 727 /* Make sure it is one of ours. */
728 728 switch (iocp->ioc_cmd) {
729 729 case TI_GETMYNAME:
730 730 case TI_GETPEERNAME:
731 731 break;
732 732 default:
733 733 /*
734 734 * If the conn is closing, then error the ioctl here. Otherwise
735 735 * use the CONN_IOCTLREF_* macros to hold off tcp_close until
736 736 * we're done here.
737 737 */
738 738 mutex_enter(&connp->conn_lock);
739 739 if (connp->conn_state_flags & CONN_CLOSING) {
740 740 mutex_exit(&connp->conn_lock);
741 741 iocp->ioc_error = EINVAL;
742 742 mp->b_datap->db_type = M_IOCNAK;
743 743 iocp->ioc_count = 0;
744 744 qreply(q, mp);
745 745 return;
746 746 }
747 747
748 748 CONN_INC_IOCTLREF_LOCKED(connp);
749 749 ip_wput_nondata(q, mp);
750 750 CONN_DEC_IOCTLREF(connp);
751 751 return;
752 752 }
753 753 switch (mi_copy_state(q, mp, &mp1)) {
754 754 case -1:
755 755 return;
756 756 case MI_COPY_CASE(MI_COPY_IN, 1):
757 757 break;
758 758 case MI_COPY_CASE(MI_COPY_OUT, 1):
759 759 /* Copy out the strbuf. */
760 760 mi_copyout(q, mp);
761 761 return;
762 762 case MI_COPY_CASE(MI_COPY_OUT, 2):
763 763 /* All done. */
764 764 mi_copy_done(q, mp, 0);
765 765 return;
766 766 default:
767 767 mi_copy_done(q, mp, EPROTO);
768 768 return;
769 769 }
770 770 /* Check alignment of the strbuf */
771 771 if (!OK_32PTR(mp1->b_rptr)) {
772 772 mi_copy_done(q, mp, EINVAL);
773 773 return;
774 774 }
775 775
776 776 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
777 777
778 778 if (connp->conn_family == AF_INET)
779 779 addrlen = sizeof (sin_t);
780 780 else
781 781 addrlen = sizeof (sin6_t);
782 782
783 783 if (STRUCT_FGET(sb, maxlen) < addrlen) {
784 784 mi_copy_done(q, mp, EINVAL);
785 785 return;
786 786 }
787 787
788 788 switch (iocp->ioc_cmd) {
789 789 case TI_GETMYNAME:
790 790 break;
791 791 case TI_GETPEERNAME:
792 792 if (tcp->tcp_state < TCPS_SYN_RCVD) {
793 793 mi_copy_done(q, mp, ENOTCONN);
794 794 return;
795 795 }
796 796 break;
797 797 }
798 798 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
799 799 if (!mp1)
800 800 return;
801 801
802 802 STRUCT_FSET(sb, len, addrlen);
803 803 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
804 804 case TI_GETMYNAME:
805 805 (void) conn_getsockname(connp, (struct sockaddr *)mp1->b_wptr,
806 806 &addrlen);
807 807 break;
808 808 case TI_GETPEERNAME:
809 809 (void) conn_getpeername(connp, (struct sockaddr *)mp1->b_wptr,
810 810 &addrlen);
811 811 break;
812 812 }
813 813 mp1->b_wptr += addrlen;
814 814 /* Copy out the address */
815 815 mi_copyout(q, mp);
816 816 }
817 817
818 818 /*
819 819 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL
820 820 * messages.
821 821 */
822 822 /* ARGSUSED */
823 823 static void
824 824 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
825 825 {
826 826 conn_t *connp = (conn_t *)arg;
827 827 tcp_t *tcp = connp->conn_tcp;
828 828 queue_t *q = connp->conn_wq;
829 829 struct iocblk *iocp;
830 830
831 831 ASSERT(DB_TYPE(mp) == M_IOCTL);
832 832 /*
833 833 * Try and ASSERT the minimum possible references on the
834 834 * conn early enough. Since we are executing on write side,
835 835 * the connection is obviously not detached and that means
836 836 * there is a ref each for TCP and IP. Since we are behind
837 837 * the squeue, the minimum references needed are 3. If the
838 838 * conn is in classifier hash list, there should be an
839 839 * extra ref for that (we check both the possibilities).
840 840 */
841 841 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
842 842 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
843 843
844 844 iocp = (struct iocblk *)mp->b_rptr;
845 845 switch (iocp->ioc_cmd) {
846 846 case _SIOCSOCKFALLBACK:
847 847 /*
848 848 * Either sockmod is about to be popped and the socket
849 849 * would now be treated as a plain stream, or a module
850 850 * is about to be pushed so we could no longer use read-
851 851 * side synchronous streams for fused loopback tcp.
852 852 * Drain any queued data and disable direct sockfs
853 853 * interface from now on.
854 854 */
855 855 if (!tcp->tcp_issocket) {
856 856 DB_TYPE(mp) = M_IOCNAK;
857 857 iocp->ioc_error = EINVAL;
858 858 } else {
859 859 tcp_use_pure_tpi(tcp);
860 860 DB_TYPE(mp) = M_IOCACK;
861 861 iocp->ioc_error = 0;
862 862 }
863 863 iocp->ioc_count = 0;
864 864 iocp->ioc_rval = 0;
865 865 qreply(q, mp);
866 866 return;
867 867 }
868 868
869 869 /*
870 870 * If the conn is closing, then error the ioctl here. Otherwise bump the
871 871 * conn_ioctlref to hold off tcp_close until we're done here.
872 872 */
873 873 mutex_enter(&(connp)->conn_lock);
874 874 if ((connp)->conn_state_flags & CONN_CLOSING) {
875 875 mutex_exit(&(connp)->conn_lock);
876 876 iocp->ioc_error = EINVAL;
877 877 mp->b_datap->db_type = M_IOCNAK;
878 878 iocp->ioc_count = 0;
879 879 qreply(q, mp);
880 880 return;
881 881 }
882 882
883 883 CONN_INC_IOCTLREF_LOCKED(connp);
884 884 ip_wput_nondata(q, mp);
885 885 CONN_DEC_IOCTLREF(connp);
886 886 }
887 887
888 888 /*
889 889 * This routine is called by tcp_wput() to handle all TPI requests.
890 890 */
891 891 /* ARGSUSED */
892 892 static void
893 893 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
894 894 {
895 895 conn_t *connp = (conn_t *)arg;
896 896 tcp_t *tcp = connp->conn_tcp;
897 897 union T_primitives *tprim = (union T_primitives *)mp->b_rptr;
898 898 uchar_t *rptr;
899 899 t_scalar_t type;
900 900 cred_t *cr;
901 901
902 902 /*
903 903 * Try and ASSERT the minimum possible references on the
904 904 * conn early enough. Since we are executing on write side,
905 905 * the connection is obviously not detached and that means
906 906 * there is a ref each for TCP and IP. Since we are behind
907 907 * the squeue, the minimum references needed are 3. If the
908 908 * conn is in classifier hash list, there should be an
909 909 * extra ref for that (we check both the possibilities).
910 910 */
911 911 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
912 912 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
913 913
914 914 rptr = mp->b_rptr;
915 915 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
916 916 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
917 917 type = ((union T_primitives *)rptr)->type;
918 918 if (type == T_EXDATA_REQ) {
919 919 tcp_output_urgent(connp, mp, arg2, NULL);
920 920 } else if (type != T_DATA_REQ) {
921 921 goto non_urgent_data;
922 922 } else {
923 923 /* TODO: options, flags, ... from user */
924 924 /* Set length to zero for reclamation below */
925 925 tcp_wput_data(tcp, mp->b_cont, B_TRUE);
926 926 freeb(mp);
927 927 }
928 928 return;
929 929 } else {
930 930 if (connp->conn_debug) {
931 931 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
932 932 "tcp_wput_proto, dropping one...");
933 933 }
934 934 freemsg(mp);
935 935 return;
936 936 }
937 937
938 938 non_urgent_data:
939 939
940 940 switch ((int)tprim->type) {
941 941 case O_T_BIND_REQ: /* bind request */
942 942 case T_BIND_REQ: /* new semantics bind request */
943 943 tcp_tpi_bind(tcp, mp);
944 944 break;
945 945 case T_UNBIND_REQ: /* unbind request */
946 946 tcp_tpi_unbind(tcp, mp);
947 947 break;
948 948 case O_T_CONN_RES: /* old connection response XXX */
949 949 case T_CONN_RES: /* connection response */
950 950 tcp_tli_accept(tcp, mp);
951 951 break;
952 952 case T_CONN_REQ: /* connection request */
953 953 tcp_tpi_connect(tcp, mp);
954 954 break;
955 955 case T_DISCON_REQ: /* disconnect request */
956 956 tcp_disconnect(tcp, mp);
957 957 break;
958 958 case T_CAPABILITY_REQ:
959 959 tcp_capability_req(tcp, mp); /* capability request */
960 960 break;
961 961 case T_INFO_REQ: /* information request */
962 962 tcp_info_req(tcp, mp);
963 963 break;
964 964 case T_SVR4_OPTMGMT_REQ: /* manage options req */
965 965 case T_OPTMGMT_REQ:
966 966 /*
967 967 * Note: no support for snmpcom_req() through new
968 968 * T_OPTMGMT_REQ. See comments in ip.c
969 969 */
970 970
971 971 /*
972 972 * All Solaris components should pass a db_credp
973 973 * for this TPI message, hence we ASSERT.
974 974 * But in case there is some other M_PROTO that looks
975 975 * like a TPI message sent by some other kernel
976 976 * component, we check and return an error.
977 977 */
978 978 cr = msg_getcred(mp, NULL);
979 979 ASSERT(cr != NULL);
980 980 if (cr == NULL) {
981 981 tcp_err_ack(tcp, mp, TSYSERR, EINVAL);
982 982 return;
983 983 }
984 984 /*
985 985 * If EINPROGRESS is returned, the request has been queued
986 986 * for subsequent processing by ip_restart_optmgmt(), which
987 987 * will do the CONN_DEC_REF().
988 988 */
989 989 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) {
990 990 svr4_optcom_req(connp->conn_wq, mp, cr, &tcp_opt_obj);
991 991 } else {
992 992 tpi_optcom_req(connp->conn_wq, mp, cr, &tcp_opt_obj);
993 993 }
994 994 break;
995 995
996 996 case T_UNITDATA_REQ: /* unitdata request */
997 997 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0);
998 998 break;
999 999 case T_ORDREL_REQ: /* orderly release req */
1000 1000 freemsg(mp);
1001 1001
1002 1002 if (tcp->tcp_fused)
1003 1003 tcp_unfuse(tcp);
1004 1004
1005 1005 if (tcp_xmit_end(tcp) != 0) {
1006 1006 /*
1007 1007 * We were crossing FINs and got a reset from
1008 1008 * the other side. Just ignore it.
1009 1009 */
1010 1010 if (connp->conn_debug) {
1011 1011 (void) strlog(TCP_MOD_ID, 0, 1,
1012 1012 SL_ERROR|SL_TRACE,
1013 1013 "tcp_wput_proto, T_ORDREL_REQ out of "
1014 1014 "state %s",
1015 1015 tcp_display(tcp, NULL,
1016 1016 DISP_ADDR_AND_PORT));
1017 1017 }
1018 1018 }
1019 1019 break;
1020 1020 case T_ADDR_REQ:
1021 1021 tcp_addr_req(tcp, mp);
1022 1022 break;
1023 1023 default:
1024 1024 if (connp->conn_debug) {
1025 1025 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
1026 1026 "tcp_wput_proto, bogus TPI msg, type %d",
1027 1027 tprim->type);
1028 1028 }
1029 1029 /*
1030 1030 * We used to M_ERROR. Sending TNOTSUPPORT gives the user
1031 1031 * to recover.
1032 1032 */
1033 1033 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0);
1034 1034 break;
1035 1035 }
1036 1036 }
1037 1037
1038 1038 /*
1039 1039 * Handle special out-of-band ioctl requests (see PSARC/2008/265).
1040 1040 */
1041 1041 static void
1042 1042 tcp_wput_cmdblk(queue_t *q, mblk_t *mp)
1043 1043 {
1044 1044 void *data;
1045 1045 mblk_t *datamp = mp->b_cont;
1046 1046 conn_t *connp = Q_TO_CONN(q);
1047 1047 tcp_t *tcp = connp->conn_tcp;
1048 1048 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr;
1049 1049
1050 1050 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) {
1051 1051 cmdp->cb_error = EPROTO;
1052 1052 qreply(q, mp);
1053 1053 return;
1054 1054 }
1055 1055
1056 1056 data = datamp->b_rptr;
1057 1057
1058 1058 switch (cmdp->cb_cmd) {
1059 1059 case TI_GETPEERNAME:
1060 1060 if (tcp->tcp_state < TCPS_SYN_RCVD)
1061 1061 cmdp->cb_error = ENOTCONN;
1062 1062 else
1063 1063 cmdp->cb_error = conn_getpeername(connp, data,
1064 1064 &cmdp->cb_len);
1065 1065 break;
1066 1066 case TI_GETMYNAME:
1067 1067 cmdp->cb_error = conn_getsockname(connp, data, &cmdp->cb_len);
1068 1068 break;
1069 1069 default:
1070 1070 cmdp->cb_error = EINVAL;
1071 1071 break;
1072 1072 }
1073 1073
1074 1074 qreply(q, mp);
1075 1075 }
1076 1076
1077 1077 /*
1078 1078 * The TCP fast path write put procedure.
1079 1079 * NOTE: the logic of the fast path is duplicated from tcp_wput_data()
1080 1080 */
1081 1081 /* ARGSUSED */
1082 1082 void
1083 1083 tcp_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1084 1084 {
1085 1085 int len;
1086 1086 int hdrlen;
1087 1087 int plen;
1088 1088 mblk_t *mp1;
1089 1089 uchar_t *rptr;
1090 1090 uint32_t snxt;
1091 1091 tcpha_t *tcpha;
1092 1092 struct datab *db;
1093 1093 uint32_t suna;
1094 1094 uint32_t mss;
1095 1095 ipaddr_t *dst;
1096 1096 ipaddr_t *src;
1097 1097 uint32_t sum;
1098 1098 int usable;
1099 1099 conn_t *connp = (conn_t *)arg;
1100 1100 tcp_t *tcp = connp->conn_tcp;
1101 1101 uint32_t msize;
1102 1102 tcp_stack_t *tcps = tcp->tcp_tcps;
1103 1103 ip_xmit_attr_t *ixa;
1104 1104 clock_t now;
1105 1105
1106 1106 /*
1107 1107 * Try and ASSERT the minimum possible references on the
1108 1108 * conn early enough. Since we are executing on write side,
1109 1109 * the connection is obviously not detached and that means
1110 1110 * there is a ref each for TCP and IP. Since we are behind
1111 1111 * the squeue, the minimum references needed are 3. If the
1112 1112 * conn is in classifier hash list, there should be an
1113 1113 * extra ref for that (we check both the possibilities).
1114 1114 */
1115 1115 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) ||
1116 1116 (connp->conn_fanout == NULL && connp->conn_ref >= 3));
1117 1117
1118 1118 ASSERT(DB_TYPE(mp) == M_DATA);
1119 1119 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp);
1120 1120
1121 1121 mutex_enter(&tcp->tcp_non_sq_lock);
1122 1122 tcp->tcp_squeue_bytes -= msize;
1123 1123 mutex_exit(&tcp->tcp_non_sq_lock);
1124 1124
1125 1125 /* Bypass tcp protocol for fused tcp loopback */
1126 1126 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize))
1127 1127 return;
1128 1128
1129 1129 mss = tcp->tcp_mss;
1130 1130 /*
1131 1131 * If ZEROCOPY has turned off, try not to send any zero-copy message
1132 1132 * down. Do backoff, now.
1133 1133 */
1134 1134 if (tcp->tcp_snd_zcopy_aware && !tcp->tcp_snd_zcopy_on)
1135 1135 mp = tcp_zcopy_backoff(tcp, mp, B_FALSE);
1136 1136
1137 1137
1138 1138 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
1139 1139 len = (int)(mp->b_wptr - mp->b_rptr);
1140 1140
1141 1141 /*
1142 1142 * Criteria for fast path:
1143 1143 *
1144 1144 * 1. no unsent data
1145 1145 * 2. single mblk in request
1146 1146 * 3. connection established
1147 1147 * 4. data in mblk
1148 1148 * 5. len <= mss
1149 1149 * 6. no tcp_valid bits
1150 1150 */
1151 1151 if ((tcp->tcp_unsent != 0) ||
1152 1152 (tcp->tcp_cork) ||
1153 1153 (mp->b_cont != NULL) ||
1154 1154 (tcp->tcp_state != TCPS_ESTABLISHED) ||
1155 1155 (len == 0) ||
1156 1156 (len > mss) ||
1157 1157 (tcp->tcp_valid_bits != 0)) {
1158 1158 tcp_wput_data(tcp, mp, B_FALSE);
1159 1159 return;
1160 1160 }
1161 1161
1162 1162 ASSERT(tcp->tcp_xmit_tail_unsent == 0);
1163 1163 ASSERT(tcp->tcp_fin_sent == 0);
1164 1164
1165 1165 /* queue new packet onto retransmission queue */
1166 1166 if (tcp->tcp_xmit_head == NULL) {
1167 1167 tcp->tcp_xmit_head = mp;
1168 1168 } else {
1169 1169 tcp->tcp_xmit_last->b_cont = mp;
1170 1170 }
1171 1171 tcp->tcp_xmit_last = mp;
1172 1172 tcp->tcp_xmit_tail = mp;
1173 1173
1174 1174 /* find out how much we can send */
1175 1175 /* BEGIN CSTYLED */
1176 1176 /*
1177 1177 * un-acked usable
1178 1178 * |--------------|-----------------|
1179 1179 * tcp_suna tcp_snxt tcp_suna+tcp_swnd
1180 1180 */
1181 1181 /* END CSTYLED */
1182 1182
1183 1183 /* start sending from tcp_snxt */
1184 1184 snxt = tcp->tcp_snxt;
1185 1185
1186 1186 /*
1187 1187 * Check to see if this connection has been idle for some time and no
1188 1188 * ACK is expected. If so, then the congestion window size is no longer
1189 1189 * meaningfully tied to current network conditions.
1190 1190 *
1191 1191 * We reinitialize tcp_cwnd, and slow start again to get back the
1192 1192 * connection's "self-clock" as described in Van Jacobson's 1988 paper
1193 1193 * "Congestion avoidance and control".
1194 1194 */
1195 1195 now = LBOLT_FASTPATH;
1196 1196 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet &&
1197 1197 (TICK_TO_MSEC(now - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) {
1198 1198 TCP_SET_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle);
1199 1199 }
1200 1200
1201 1201 usable = tcp->tcp_swnd; /* tcp window size */
1202 1202 if (usable > tcp->tcp_cwnd)
1203 1203 usable = tcp->tcp_cwnd; /* congestion window smaller */
1204 1204 usable -= snxt; /* subtract stuff already sent */
1205 1205 suna = tcp->tcp_suna;
1206 1206 usable += suna;
1207 1207 /* usable can be < 0 if the congestion window is smaller */
1208 1208 if (len > usable) {
1209 1209 /* Can't send complete M_DATA in one shot */
1210 1210 goto slow;
1211 1211 }
1212 1212
1213 1213 mutex_enter(&tcp->tcp_non_sq_lock);
1214 1214 if (tcp->tcp_flow_stopped &&
1215 1215 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1216 1216 tcp_clrqfull(tcp);
1217 1217 }
1218 1218 mutex_exit(&tcp->tcp_non_sq_lock);
1219 1219
1220 1220 /*
1221 1221 * determine if anything to send (Nagle).
1222 1222 *
1223 1223 * 1. len < tcp_mss (i.e. small)
1224 1224 * 2. unacknowledged data present
1225 1225 * 3. len < nagle limit
1226 1226 * 4. last packet sent < nagle limit (previous packet sent)
1227 1227 */
1228 1228 if ((len < mss) && (snxt != suna) &&
1229 1229 (len < (int)tcp->tcp_naglim) &&
1230 1230 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) {
1231 1231 /*
1232 1232 * This was the first unsent packet and normally
1233 1233 * mss < xmit_hiwater so there is no need to worry
1234 1234 * about flow control. The next packet will go
1235 1235 * through the flow control check in tcp_wput_data().
1236 1236 */
1237 1237 /* leftover work from above */
1238 1238 tcp->tcp_unsent = len;
1239 1239 tcp->tcp_xmit_tail_unsent = len;
1240 1240
1241 1241 return;
1242 1242 }
1243 1243
1244 1244 /*
1245 1245 * len <= tcp->tcp_mss && len == unsent so no sender silly window. Can
1246 1246 * send now.
1247 1247 */
1248 1248
1249 1249 if (snxt == suna) {
1250 1250 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
1251 1251 }
1252 1252
1253 1253 /* we have always sent something */
1254 1254 tcp->tcp_rack_cnt = 0;
1255 1255
1256 1256 tcp->tcp_snxt = snxt + len;
1257 1257 tcp->tcp_rack = tcp->tcp_rnxt;
1258 1258
1259 1259 if ((mp1 = dupb(mp)) == 0)
1260 1260 goto no_memory;
1261 1261 mp->b_prev = (mblk_t *)(intptr_t)gethrtime();
1262 1262 mp->b_next = (mblk_t *)(uintptr_t)snxt;
1263 1263
1264 1264 /* adjust tcp header information */
1265 1265 tcpha = tcp->tcp_tcpha;
↓ open down ↓ |
1265 lines elided |
↑ open up ↑ |
1266 1266 tcpha->tha_flags = (TH_ACK|TH_PUSH);
1267 1267
1268 1268 sum = len + connp->conn_ht_ulp_len + connp->conn_sum;
1269 1269 sum = (sum >> 16) + (sum & 0xFFFF);
1270 1270 tcpha->tha_sum = htons(sum);
1271 1271
1272 1272 tcpha->tha_seq = htonl(snxt);
1273 1273
1274 1274 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1275 1275 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1276 - BUMP_LOCAL(tcp->tcp_obsegs);
1276 + TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
1277 + tcp->tcp_cs.tcp_out_data_segs++;
1278 + tcp->tcp_cs.tcp_out_data_bytes += len;
1277 1279
1278 1280 /* Update the latest receive window size in TCP header. */
1279 1281 tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
1280 1282
1281 1283 tcp->tcp_last_sent_len = (ushort_t)len;
1282 1284
1283 1285 plen = len + connp->conn_ht_iphc_len;
1284 1286
1285 1287 ixa = connp->conn_ixa;
1286 1288 ixa->ixa_pktlen = plen;
1287 1289
1288 1290 if (ixa->ixa_flags & IXAF_IS_IPV4) {
1289 1291 tcp->tcp_ipha->ipha_length = htons(plen);
1290 1292 } else {
1291 1293 tcp->tcp_ip6h->ip6_plen = htons(plen - IPV6_HDR_LEN);
1292 1294 }
1293 1295
1294 1296 /* see if we need to allocate a mblk for the headers */
1295 1297 hdrlen = connp->conn_ht_iphc_len;
1296 1298 rptr = mp1->b_rptr - hdrlen;
1297 1299 db = mp1->b_datap;
1298 1300 if ((db->db_ref != 2) || rptr < db->db_base ||
1299 1301 (!OK_32PTR(rptr))) {
1300 1302 /* NOTE: we assume allocb returns an OK_32PTR */
1301 1303 mp = allocb(hdrlen + tcps->tcps_wroff_xtra, BPRI_MED);
1302 1304 if (!mp) {
1303 1305 freemsg(mp1);
1304 1306 goto no_memory;
1305 1307 }
1306 1308 mp->b_cont = mp1;
1307 1309 mp1 = mp;
1308 1310 /* Leave room for Link Level header */
1309 1311 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra];
1310 1312 mp1->b_wptr = &rptr[hdrlen];
1311 1313 }
1312 1314 mp1->b_rptr = rptr;
1313 1315
1314 1316 /* Fill in the timestamp option. */
1315 1317 if (tcp->tcp_snd_ts_ok) {
1316 1318 U32_TO_BE32(now,
1317 1319 (char *)tcpha + TCP_MIN_HEADER_LENGTH + 4);
1318 1320 U32_TO_BE32(tcp->tcp_ts_recent,
1319 1321 (char *)tcpha + TCP_MIN_HEADER_LENGTH + 8);
1320 1322 } else {
1321 1323 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
1322 1324 }
1323 1325
1324 1326 /* copy header into outgoing packet */
1325 1327 dst = (ipaddr_t *)rptr;
1326 1328 src = (ipaddr_t *)connp->conn_ht_iphc;
1327 1329 dst[0] = src[0];
1328 1330 dst[1] = src[1];
1329 1331 dst[2] = src[2];
1330 1332 dst[3] = src[3];
1331 1333 dst[4] = src[4];
1332 1334 dst[5] = src[5];
1333 1335 dst[6] = src[6];
1334 1336 dst[7] = src[7];
1335 1337 dst[8] = src[8];
1336 1338 dst[9] = src[9];
1337 1339 if (hdrlen -= 40) {
1338 1340 hdrlen >>= 2;
1339 1341 dst += 10;
1340 1342 src += 10;
1341 1343 do {
1342 1344 *dst++ = *src++;
1343 1345 } while (--hdrlen);
1344 1346 }
1345 1347
1346 1348 /*
1347 1349 * Set the ECN info in the TCP header. Note that this
1348 1350 * is not the template header.
1349 1351 */
1350 1352 if (tcp->tcp_ecn_ok) {
1351 1353 TCP_SET_ECT(tcp, rptr);
1352 1354
1353 1355 tcpha = (tcpha_t *)(rptr + ixa->ixa_ip_hdr_length);
1354 1356 if (tcp->tcp_ecn_echo_on)
1355 1357 tcpha->tha_flags |= TH_ECE;
1356 1358 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
1357 1359 tcpha->tha_flags |= TH_CWR;
1358 1360 tcp->tcp_ecn_cwr_sent = B_TRUE;
1359 1361 }
1360 1362 }
1361 1363
1362 1364 if (tcp->tcp_ip_forward_progress) {
1363 1365 tcp->tcp_ip_forward_progress = B_FALSE;
1364 1366 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
1365 1367 } else {
1366 1368 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
1367 1369 }
1368 1370 tcp_send_data(tcp, mp1);
1369 1371 return;
1370 1372
1371 1373 /*
1372 1374 * If we ran out of memory, we pretend to have sent the packet
1373 1375 * and that it was lost on the wire.
1374 1376 */
1375 1377 no_memory:
1376 1378 return;
1377 1379
1378 1380 slow:
1379 1381 /* leftover work from above */
1380 1382 tcp->tcp_unsent = len;
1381 1383 tcp->tcp_xmit_tail_unsent = len;
1382 1384 tcp_wput_data(tcp, NULL, B_FALSE);
1383 1385 }
1384 1386
1385 1387 /* ARGSUSED2 */
1386 1388 void
1387 1389 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1388 1390 {
1389 1391 int len;
1390 1392 uint32_t msize;
1391 1393 conn_t *connp = (conn_t *)arg;
1392 1394 tcp_t *tcp = connp->conn_tcp;
1393 1395
1394 1396 msize = msgdsize(mp);
1395 1397
1396 1398 len = msize - 1;
1397 1399 if (len < 0) {
1398 1400 freemsg(mp);
1399 1401 return;
1400 1402 }
1401 1403
1402 1404 /*
1403 1405 * Try to force urgent data out on the wire. Even if we have unsent
1404 1406 * data this will at least send the urgent flag.
1405 1407 * XXX does not handle more flag correctly.
1406 1408 */
1407 1409 len += tcp->tcp_unsent;
1408 1410 len += tcp->tcp_snxt;
1409 1411 tcp->tcp_urg = len;
1410 1412 tcp->tcp_valid_bits |= TCP_URG_VALID;
1411 1413
1412 1414 /* Bypass tcp protocol for fused tcp loopback */
1413 1415 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize))
1414 1416 return;
1415 1417
1416 1418 /* Strip off the T_EXDATA_REQ if the data is from TPI */
1417 1419 if (DB_TYPE(mp) != M_DATA) {
1418 1420 mblk_t *mp1 = mp;
1419 1421 ASSERT(!IPCL_IS_NONSTR(connp));
1420 1422 mp = mp->b_cont;
1421 1423 freeb(mp1);
1422 1424 }
1423 1425 tcp_wput_data(tcp, mp, B_TRUE);
1424 1426 }
1425 1427
1426 1428 /*
1427 1429 * Called by streams close routine via squeues when our client blows off its
1428 1430 * descriptor, we take this to mean: "close the stream state NOW, close the tcp
1429 1431 * connection politely" When SO_LINGER is set (with a non-zero linger time and
1430 1432 * it is not a nonblocking socket) then this routine sleeps until the FIN is
1431 1433 * acked.
1432 1434 *
1433 1435 * NOTE: tcp_close potentially returns error when lingering.
1434 1436 * However, the stream head currently does not pass these errors
1435 1437 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK
1436 1438 * errors to the application (from tsleep()) and not errors
1437 1439 * like ECONNRESET caused by receiving a reset packet.
1438 1440 */
1439 1441
1440 1442 /* ARGSUSED */
1441 1443 void
1442 1444 tcp_close_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1443 1445 {
1444 1446 char *msg;
1445 1447 conn_t *connp = (conn_t *)arg;
1446 1448 tcp_t *tcp = connp->conn_tcp;
1447 1449 clock_t delta = 0;
1448 1450 tcp_stack_t *tcps = tcp->tcp_tcps;
1449 1451
1450 1452 /*
1451 1453 * When a non-STREAMS socket is being closed, it does not always
1452 1454 * stick around waiting for tcp_close_output to run and can therefore
1453 1455 * have dropped a reference already. So adjust the asserts accordingly.
1454 1456 */
1455 1457 ASSERT((connp->conn_fanout != NULL &&
1456 1458 connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 3 : 4)) ||
1457 1459 (connp->conn_fanout == NULL &&
1458 1460 connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 2 : 3)));
1459 1461
1460 1462 mutex_enter(&tcp->tcp_eager_lock);
1461 1463 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
1462 1464 /*
1463 1465 * Cleanup for listener. For non-STREAM sockets sockfs will
1464 1466 * close all the eagers on 'q', so in that case only deal
1465 1467 * with 'q0'.
1466 1468 */
1467 1469 tcp_eager_cleanup(tcp, IPCL_IS_NONSTR(connp) ? 1 : 0);
1468 1470 tcp->tcp_wait_for_eagers = 1;
1469 1471 }
1470 1472 mutex_exit(&tcp->tcp_eager_lock);
1471 1473
1472 1474 tcp->tcp_lso = B_FALSE;
1473 1475
1474 1476 msg = NULL;
1475 1477 switch (tcp->tcp_state) {
1476 1478 case TCPS_CLOSED:
1477 1479 case TCPS_IDLE:
1478 1480 break;
1479 1481 case TCPS_BOUND:
1480 1482 if (tcp->tcp_listener != NULL) {
1481 1483 ASSERT(IPCL_IS_NONSTR(connp));
1482 1484 /*
1483 1485 * Unlink from the listener and drop the reference
1484 1486 * put on it by the eager. tcp_closei_local will not
1485 1487 * do it because tcp_tconnind_started is TRUE.
1486 1488 */
1487 1489 mutex_enter(&tcp->tcp_saved_listener->tcp_eager_lock);
1488 1490 tcp_eager_unlink(tcp);
1489 1491 mutex_exit(&tcp->tcp_saved_listener->tcp_eager_lock);
1490 1492 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp);
1491 1493 }
1492 1494 break;
1493 1495 case TCPS_LISTEN:
1494 1496 break;
1495 1497 case TCPS_SYN_SENT:
1496 1498 msg = "tcp_close, during connect";
1497 1499 break;
1498 1500 case TCPS_SYN_RCVD:
1499 1501 /*
1500 1502 * Close during the connect 3-way handshake
1501 1503 * but here there may or may not be pending data
1502 1504 * already on queue. Process almost same as in
1503 1505 * the ESTABLISHED state.
1504 1506 */
1505 1507 /* FALLTHRU */
1506 1508 default:
1507 1509 if (tcp->tcp_fused)
1508 1510 tcp_unfuse(tcp);
1509 1511
1510 1512 /*
1511 1513 * If SO_LINGER has set a zero linger time, abort the
1512 1514 * connection with a reset.
1513 1515 */
1514 1516 if (connp->conn_linger && connp->conn_lingertime == 0) {
1515 1517 msg = "tcp_close, zero lingertime";
1516 1518 break;
1517 1519 }
1518 1520
1519 1521 /*
1520 1522 * Abort connection if there is unread data queued.
1521 1523 */
1522 1524 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) {
1523 1525 msg = "tcp_close, unread data";
1524 1526 break;
1525 1527 }
1526 1528
1527 1529 /*
1528 1530 * Abort connection if it is being closed without first
1529 1531 * being accepted. This can happen if a listening non-STREAM
1530 1532 * socket wants to get rid of the socket, for example, if the
1531 1533 * listener is closing.
1532 1534 */
1533 1535 if (tcp->tcp_listener != NULL) {
1534 1536 ASSERT(IPCL_IS_NONSTR(connp));
1535 1537 msg = "tcp_close, close before accept";
1536 1538
1537 1539 /*
1538 1540 * Unlink from the listener and drop the reference
1539 1541 * put on it by the eager. tcp_closei_local will not
1540 1542 * do it because tcp_tconnind_started is TRUE.
1541 1543 */
1542 1544 mutex_enter(&tcp->tcp_saved_listener->tcp_eager_lock);
1543 1545 tcp_eager_unlink(tcp);
1544 1546 mutex_exit(&tcp->tcp_saved_listener->tcp_eager_lock);
1545 1547 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp);
1546 1548 break;
1547 1549 }
1548 1550
1549 1551 /*
1550 1552 * Transmit the FIN before detaching the tcp_t.
1551 1553 * After tcp_detach returns this queue/perimeter
1552 1554 * no longer owns the tcp_t thus others can modify it.
1553 1555 */
1554 1556 (void) tcp_xmit_end(tcp);
1555 1557
1556 1558 /*
1557 1559 * If lingering on close then wait until the fin is acked,
1558 1560 * the SO_LINGER time passes, or a reset is sent/received.
1559 1561 */
1560 1562 if (connp->conn_linger && connp->conn_lingertime > 0 &&
1561 1563 !(tcp->tcp_fin_acked) &&
1562 1564 tcp->tcp_state >= TCPS_ESTABLISHED) {
1563 1565 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) {
1564 1566 tcp->tcp_client_errno = EWOULDBLOCK;
1565 1567 } else if (tcp->tcp_client_errno == 0) {
1566 1568
1567 1569 ASSERT(tcp->tcp_linger_tid == 0);
1568 1570
1569 1571 /* conn_lingertime is in sec. */
1570 1572 tcp->tcp_linger_tid = TCP_TIMER(tcp,
1571 1573 tcp_close_linger_timeout,
1572 1574 connp->conn_lingertime * MILLISEC);
1573 1575
1574 1576 /* tcp_close_linger_timeout will finish close */
1575 1577 if (tcp->tcp_linger_tid == 0)
1576 1578 tcp->tcp_client_errno = ENOSR;
1577 1579 else
1578 1580 return;
1579 1581 }
1580 1582
1581 1583 /*
1582 1584 * Check if we need to detach or just close
1583 1585 * the instance.
1584 1586 */
1585 1587 if (tcp->tcp_state <= TCPS_LISTEN)
1586 1588 break;
1587 1589 }
1588 1590
1589 1591 /*
1590 1592 * Make sure that no other thread will access the conn_rq of
1591 1593 * this instance (through lookups etc.) as conn_rq will go
1592 1594 * away shortly.
1593 1595 */
1594 1596 tcp_acceptor_hash_remove(tcp);
1595 1597
1596 1598 mutex_enter(&tcp->tcp_non_sq_lock);
1597 1599 if (tcp->tcp_flow_stopped) {
1598 1600 tcp_clrqfull(tcp);
1599 1601 }
1600 1602 mutex_exit(&tcp->tcp_non_sq_lock);
1601 1603
1602 1604 if (tcp->tcp_timer_tid != 0) {
1603 1605 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
1604 1606 tcp->tcp_timer_tid = 0;
1605 1607 }
1606 1608 /*
1607 1609 * Need to cancel those timers which will not be used when
1608 1610 * TCP is detached. This has to be done before the conn_wq
1609 1611 * is set to NULL.
1610 1612 */
1611 1613 tcp_timers_stop(tcp);
1612 1614
1613 1615 tcp->tcp_detached = B_TRUE;
1614 1616 if (tcp->tcp_state == TCPS_TIME_WAIT) {
1615 1617 tcp_time_wait_append(tcp);
1616 1618 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
1617 1619 ASSERT(connp->conn_ref >=
1618 1620 (IPCL_IS_NONSTR(connp) ? 2 : 3));
1619 1621 goto finish;
1620 1622 }
1621 1623
1622 1624 /*
1623 1625 * If delta is zero the timer event wasn't executed and was
1624 1626 * successfully canceled. In this case we need to restart it
1625 1627 * with the minimal delta possible.
1626 1628 */
1627 1629 if (delta >= 0)
1628 1630 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
1629 1631 delta ? delta : 1);
1630 1632
1631 1633 ASSERT(connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 2 : 3));
1632 1634 goto finish;
1633 1635 }
1634 1636
1635 1637 /* Detach did not complete. Still need to remove q from stream. */
1636 1638 if (msg) {
1637 1639 if (tcp->tcp_state == TCPS_ESTABLISHED ||
1638 1640 tcp->tcp_state == TCPS_CLOSE_WAIT)
1639 1641 TCPS_BUMP_MIB(tcps, tcpEstabResets);
1640 1642 if (tcp->tcp_state == TCPS_SYN_SENT ||
1641 1643 tcp->tcp_state == TCPS_SYN_RCVD)
1642 1644 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1643 1645 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST);
1644 1646 }
1645 1647
1646 1648 tcp_closei_local(tcp);
1647 1649 CONN_DEC_REF(connp);
1648 1650 ASSERT(connp->conn_ref >= (IPCL_IS_NONSTR(connp) ? 1 : 2));
1649 1651
1650 1652 finish:
1651 1653 /*
1652 1654 * Don't change the queues in the case of a listener that has
1653 1655 * eagers in its q or q0. It could surprise the eagers.
1654 1656 * Instead wait for the eagers outside the squeue.
1655 1657 *
1656 1658 * For non-STREAMS sockets tcp_wait_for_eagers implies that
1657 1659 * we should delay the su_closed upcall until all eagers have
1658 1660 * dropped their references.
1659 1661 */
1660 1662 if (!tcp->tcp_wait_for_eagers) {
1661 1663 tcp->tcp_detached = B_TRUE;
1662 1664 connp->conn_rq = NULL;
1663 1665 connp->conn_wq = NULL;
1664 1666
1665 1667 /* non-STREAM socket, release the upper handle */
1666 1668 if (IPCL_IS_NONSTR(connp)) {
1667 1669 ASSERT(connp->conn_upper_handle != NULL);
1668 1670 (*connp->conn_upcalls->su_closed)
1669 1671 (connp->conn_upper_handle);
1670 1672 connp->conn_upper_handle = NULL;
1671 1673 connp->conn_upcalls = NULL;
1672 1674 }
1673 1675 }
1674 1676
1675 1677 /* Signal tcp_close() to finish closing. */
1676 1678 mutex_enter(&tcp->tcp_closelock);
1677 1679 tcp->tcp_closed = 1;
1678 1680 cv_signal(&tcp->tcp_closecv);
1679 1681 mutex_exit(&tcp->tcp_closelock);
1680 1682 }
1681 1683
1682 1684 /* ARGSUSED */
1683 1685 void
1684 1686 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1685 1687 {
1686 1688 conn_t *connp = (conn_t *)arg;
1687 1689 tcp_t *tcp = connp->conn_tcp;
1688 1690
1689 1691 freemsg(mp);
1690 1692
1691 1693 if (tcp->tcp_fused)
1692 1694 tcp_unfuse(tcp);
1693 1695
1694 1696 if (tcp_xmit_end(tcp) != 0) {
1695 1697 /*
1696 1698 * We were crossing FINs and got a reset from
1697 1699 * the other side. Just ignore it.
1698 1700 */
1699 1701 if (connp->conn_debug) {
1700 1702 (void) strlog(TCP_MOD_ID, 0, 1,
1701 1703 SL_ERROR|SL_TRACE,
1702 1704 "tcp_shutdown_output() out of state %s",
1703 1705 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
1704 1706 }
1705 1707 }
1706 1708 }
1707 1709
1708 1710 #pragma inline(tcp_send_data)
1709 1711
1710 1712 void
1711 1713 tcp_send_data(tcp_t *tcp, mblk_t *mp)
1712 1714 {
1713 1715 conn_t *connp = tcp->tcp_connp;
1714 1716
1715 1717 /*
1716 1718 * Check here to avoid sending zero-copy message down to IP when
1717 1719 * ZEROCOPY capability has turned off. We only need to deal with
1718 1720 * the race condition between sockfs and the notification here.
1719 1721 * Since we have tried to backoff the tcp_xmit_head when turning
1720 1722 * zero-copy off and new messages in tcp_output(), we simply drop
1721 1723 * the dup'ed packet here and let tcp retransmit, if tcp_xmit_zc_clean
1722 1724 * is not true.
1723 1725 */
1724 1726 if (tcp->tcp_snd_zcopy_aware && !tcp->tcp_snd_zcopy_on &&
1725 1727 !tcp->tcp_xmit_zc_clean) {
1726 1728 ip_drop_output("TCP ZC was disabled but not clean", mp, NULL);
1727 1729 freemsg(mp);
1728 1730 return;
1729 1731 }
1730 1732
1731 1733 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa,
1732 1734 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, tcp,
1733 1735 __dtrace_tcp_tcph_t *,
1734 1736 &mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
1735 1737
1736 1738 ASSERT(connp->conn_ixa->ixa_notify_cookie == connp->conn_tcp);
1737 1739 (void) conn_ip_output(mp, connp->conn_ixa);
1738 1740 }
1739 1741
1740 1742 /* ARGSUSED2 */
1741 1743 void
1742 1744 tcp_send_synack(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1743 1745 {
1744 1746 conn_t *econnp = (conn_t *)arg;
1745 1747 tcp_t *tcp = econnp->conn_tcp;
1746 1748 ip_xmit_attr_t *ixa = econnp->conn_ixa;
1747 1749
1748 1750 /* Guard against a RST having blown it away while on the squeue */
1749 1751 if (tcp->tcp_state == TCPS_CLOSED) {
1750 1752 freemsg(mp);
1751 1753 return;
1752 1754 }
1753 1755
1754 1756 /*
1755 1757 * In the off-chance that the eager received and responded to
1756 1758 * some other packet while the SYN|ACK was queued, we recalculate
1757 1759 * the ixa_pktlen. It would be better to fix the SYN/accept
1758 1760 * multithreading scheme to avoid this complexity.
1759 1761 */
1760 1762 ixa->ixa_pktlen = msgdsize(mp);
1761 1763 (void) conn_ip_output(mp, ixa);
1762 1764 }
1763 1765
1764 1766 /*
1765 1767 * tcp_send() is called by tcp_wput_data() and returns one of the following:
1766 1768 *
1767 1769 * -1 = failed allocation.
1768 1770 * 0 = We've either successfully sent data, or our usable send window is too
1769 1771 * small and we'd rather wait until later before sending again.
1770 1772 */
1771 1773 static int
1772 1774 tcp_send(tcp_t *tcp, const int mss, const int total_hdr_len,
1773 1775 const int tcp_hdr_len, const int num_sack_blk, int *usable,
1774 1776 uint32_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time)
1775 1777 {
1776 1778 int num_lso_seg = 1;
1777 1779 uint_t lso_usable;
1778 1780 boolean_t do_lso_send = B_FALSE;
1779 1781 tcp_stack_t *tcps = tcp->tcp_tcps;
1780 1782 conn_t *connp = tcp->tcp_connp;
1781 1783 ip_xmit_attr_t *ixa = connp->conn_ixa;
1782 1784
1783 1785 /*
1784 1786 * Check LSO possibility. The value of tcp->tcp_lso indicates whether
1785 1787 * the underlying connection is LSO capable. Will check whether having
1786 1788 * enough available data to initiate LSO transmission in the for(){}
1787 1789 * loops.
1788 1790 */
1789 1791 if (tcp->tcp_lso && (tcp->tcp_valid_bits & ~TCP_FSS_VALID) == 0)
1790 1792 do_lso_send = B_TRUE;
1791 1793
1792 1794 for (;;) {
1793 1795 struct datab *db;
1794 1796 tcpha_t *tcpha;
1795 1797 uint32_t sum;
1796 1798 mblk_t *mp, *mp1;
1797 1799 uchar_t *rptr;
1798 1800 int len;
1799 1801
1800 1802 /*
1801 1803 * Calculate the maximum payload length we can send at one
1802 1804 * time.
1803 1805 */
1804 1806 if (do_lso_send) {
1805 1807 /*
1806 1808 * Determine whether or not it's possible to do LSO,
1807 1809 * and if so, how much data we can send.
1808 1810 */
1809 1811 if ((*usable - 1) / mss >= 1) {
1810 1812 lso_usable = MIN(tcp->tcp_lso_max, *usable);
1811 1813 num_lso_seg = lso_usable / mss;
1812 1814 if (lso_usable % mss) {
1813 1815 num_lso_seg++;
1814 1816 tcp->tcp_last_sent_len = (ushort_t)
1815 1817 (lso_usable % mss);
1816 1818 } else {
1817 1819 tcp->tcp_last_sent_len = (ushort_t)mss;
1818 1820 }
1819 1821 } else {
1820 1822 do_lso_send = B_FALSE;
1821 1823 num_lso_seg = 1;
1822 1824 lso_usable = mss;
1823 1825 }
1824 1826 }
1825 1827
1826 1828 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1);
1827 1829
1828 1830 len = mss;
1829 1831 if (len > *usable) {
1830 1832 ASSERT(do_lso_send == B_FALSE);
1831 1833
1832 1834 len = *usable;
1833 1835 if (len <= 0) {
1834 1836 /* Terminate the loop */
1835 1837 break; /* success; too small */
1836 1838 }
1837 1839 /*
1838 1840 * Sender silly-window avoidance.
1839 1841 * Ignore this if we are going to send a
1840 1842 * zero window probe out.
1841 1843 *
1842 1844 * TODO: force data into microscopic window?
1843 1845 * ==> (!pushed || (unsent > usable))
1844 1846 */
1845 1847 if (len < (tcp->tcp_max_swnd >> 1) &&
1846 1848 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len &&
1847 1849 !((tcp->tcp_valid_bits & TCP_URG_VALID) &&
1848 1850 len == 1) && (! tcp->tcp_zero_win_probe)) {
1849 1851 /*
1850 1852 * If the retransmit timer is not running
1851 1853 * we start it so that we will retransmit
1852 1854 * in the case when the receiver has
1853 1855 * decremented the window.
1854 1856 */
1855 1857 if (*snxt == tcp->tcp_snxt &&
1856 1858 *snxt == tcp->tcp_suna) {
1857 1859 /*
1858 1860 * We are not supposed to send
1859 1861 * anything. So let's wait a little
1860 1862 * bit longer before breaking SWS
1861 1863 * avoidance.
1862 1864 *
1863 1865 * What should the value be?
1864 1866 * Suggestion: MAX(init rexmit time,
1865 1867 * tcp->tcp_rto)
1866 1868 */
1867 1869 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
1868 1870 }
1869 1871 break; /* success; too small */
1870 1872 }
1871 1873 }
1872 1874
1873 1875 tcpha = tcp->tcp_tcpha;
1874 1876
1875 1877 /*
1876 1878 * The reason to adjust len here is that we need to set flags
1877 1879 * and calculate checksum.
1878 1880 */
1879 1881 if (do_lso_send)
1880 1882 len = lso_usable;
1881 1883
1882 1884 *usable -= len; /* Approximate - can be adjusted later */
1883 1885 if (*usable > 0)
1884 1886 tcpha->tha_flags = TH_ACK;
1885 1887 else
1886 1888 tcpha->tha_flags = (TH_ACK | TH_PUSH);
1887 1889
1888 1890 /*
1889 1891 * Prime pump for IP's checksumming on our behalf.
1890 1892 * Include the adjustment for a source route if any.
1891 1893 * In case of LSO, the partial pseudo-header checksum should
1892 1894 * exclusive TCP length, so zero tha_sum before IP calculate
1893 1895 * pseudo-header checksum for partial checksum offload.
1894 1896 */
1895 1897 if (do_lso_send) {
1896 1898 sum = 0;
1897 1899 } else {
1898 1900 sum = len + tcp_hdr_len + connp->conn_sum;
1899 1901 sum = (sum >> 16) + (sum & 0xFFFF);
1900 1902 }
1901 1903 tcpha->tha_sum = htons(sum);
1902 1904 tcpha->tha_seq = htonl(*snxt);
1903 1905
1904 1906 /*
1905 1907 * Branch off to tcp_xmit_mp() if any of the VALID bits is
1906 1908 * set. For the case when TCP_FSS_VALID is the only valid
1907 1909 * bit (normal active close), branch off only when we think
1908 1910 * that the FIN flag needs to be set. Note for this case,
1909 1911 * that (snxt + len) may not reflect the actual seg_len,
1910 1912 * as len may be further reduced in tcp_xmit_mp(). If len
1911 1913 * gets modified, we will end up here again.
1912 1914 */
1913 1915 if (tcp->tcp_valid_bits != 0 &&
1914 1916 (tcp->tcp_valid_bits != TCP_FSS_VALID ||
1915 1917 ((*snxt + len) == tcp->tcp_fss))) {
1916 1918 uchar_t *prev_rptr;
1917 1919 uint32_t prev_snxt = tcp->tcp_snxt;
1918 1920
1919 1921 if (*tail_unsent == 0) {
1920 1922 ASSERT((*xmit_tail)->b_cont != NULL);
1921 1923 *xmit_tail = (*xmit_tail)->b_cont;
1922 1924 prev_rptr = (*xmit_tail)->b_rptr;
1923 1925 *tail_unsent = (int)((*xmit_tail)->b_wptr -
1924 1926 (*xmit_tail)->b_rptr);
1925 1927 } else {
1926 1928 prev_rptr = (*xmit_tail)->b_rptr;
1927 1929 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr -
1928 1930 *tail_unsent;
1929 1931 }
1930 1932 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL,
1931 1933 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE);
1932 1934 /* Restore tcp_snxt so we get amount sent right. */
1933 1935 tcp->tcp_snxt = prev_snxt;
1934 1936 if (prev_rptr == (*xmit_tail)->b_rptr) {
1935 1937 /*
1936 1938 * If the previous timestamp is still in use,
1937 1939 * don't stomp on it.
1938 1940 */
1939 1941 if ((*xmit_tail)->b_next == NULL) {
1940 1942 (*xmit_tail)->b_prev = local_time;
1941 1943 (*xmit_tail)->b_next =
1942 1944 (mblk_t *)(uintptr_t)(*snxt);
1943 1945 }
1944 1946 } else
1945 1947 (*xmit_tail)->b_rptr = prev_rptr;
1946 1948
1947 1949 if (mp == NULL) {
1948 1950 return (-1);
1949 1951 }
1950 1952 mp1 = mp->b_cont;
1951 1953
1952 1954 if (len <= mss) /* LSO is unusable (!do_lso_send) */
↓ open down ↓ |
666 lines elided |
↑ open up ↑ |
1953 1955 tcp->tcp_last_sent_len = (ushort_t)len;
1954 1956 while (mp1->b_cont) {
1955 1957 *xmit_tail = (*xmit_tail)->b_cont;
1956 1958 (*xmit_tail)->b_prev = local_time;
1957 1959 (*xmit_tail)->b_next =
1958 1960 (mblk_t *)(uintptr_t)(*snxt);
1959 1961 mp1 = mp1->b_cont;
1960 1962 }
1961 1963 *snxt += len;
1962 1964 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr;
1963 - BUMP_LOCAL(tcp->tcp_obsegs);
1965 + TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
1964 1966 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1965 1967 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1968 + tcp->tcp_cs.tcp_out_data_segs++;
1969 + tcp->tcp_cs.tcp_out_data_bytes += len;
1966 1970 tcp_send_data(tcp, mp);
1967 1971 continue;
1968 1972 }
1969 1973
1970 1974 *snxt += len; /* Adjust later if we don't send all of len */
1975 + TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
1971 1976 TCPS_BUMP_MIB(tcps, tcpOutDataSegs);
1972 1977 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, len);
1978 + tcp->tcp_cs.tcp_out_data_segs++;
1979 + tcp->tcp_cs.tcp_out_data_bytes += len;
1973 1980
1974 1981 if (*tail_unsent) {
1975 1982 /* Are the bytes above us in flight? */
1976 1983 rptr = (*xmit_tail)->b_wptr - *tail_unsent;
1977 1984 if (rptr != (*xmit_tail)->b_rptr) {
1978 1985 *tail_unsent -= len;
1979 1986 if (len <= mss) /* LSO is unusable */
1980 1987 tcp->tcp_last_sent_len = (ushort_t)len;
1981 1988 len += total_hdr_len;
1982 1989 ixa->ixa_pktlen = len;
1983 1990
1984 1991 if (ixa->ixa_flags & IXAF_IS_IPV4) {
1985 1992 tcp->tcp_ipha->ipha_length = htons(len);
1986 1993 } else {
1987 1994 tcp->tcp_ip6h->ip6_plen =
1988 1995 htons(len - IPV6_HDR_LEN);
1989 1996 }
1990 1997
1991 1998 mp = dupb(*xmit_tail);
1992 1999 if (mp == NULL) {
1993 2000 return (-1); /* out_of_mem */
1994 2001 }
1995 2002 mp->b_rptr = rptr;
1996 2003 /*
1997 2004 * If the old timestamp is no longer in use,
1998 2005 * sample a new timestamp now.
1999 2006 */
2000 2007 if ((*xmit_tail)->b_next == NULL) {
2001 2008 (*xmit_tail)->b_prev = local_time;
2002 2009 (*xmit_tail)->b_next =
2003 2010 (mblk_t *)(uintptr_t)(*snxt-len);
2004 2011 }
2005 2012 goto must_alloc;
2006 2013 }
2007 2014 } else {
2008 2015 *xmit_tail = (*xmit_tail)->b_cont;
2009 2016 ASSERT((uintptr_t)((*xmit_tail)->b_wptr -
2010 2017 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX);
2011 2018 *tail_unsent = (int)((*xmit_tail)->b_wptr -
2012 2019 (*xmit_tail)->b_rptr);
2013 2020 }
2014 2021
2015 2022 (*xmit_tail)->b_prev = local_time;
2016 2023 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len);
2017 2024
2018 2025 *tail_unsent -= len;
2019 2026 if (len <= mss) /* LSO is unusable (!do_lso_send) */
2020 2027 tcp->tcp_last_sent_len = (ushort_t)len;
2021 2028
2022 2029 len += total_hdr_len;
2023 2030 ixa->ixa_pktlen = len;
2024 2031
2025 2032 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2026 2033 tcp->tcp_ipha->ipha_length = htons(len);
2027 2034 } else {
2028 2035 tcp->tcp_ip6h->ip6_plen = htons(len - IPV6_HDR_LEN);
2029 2036 }
2030 2037
2031 2038 mp = dupb(*xmit_tail);
2032 2039 if (mp == NULL) {
2033 2040 return (-1); /* out_of_mem */
2034 2041 }
2035 2042
2036 2043 len = total_hdr_len;
2037 2044 /*
2038 2045 * There are four reasons to allocate a new hdr mblk:
2039 2046 * 1) The bytes above us are in use by another packet
2040 2047 * 2) We don't have good alignment
2041 2048 * 3) The mblk is being shared
2042 2049 * 4) We don't have enough room for a header
2043 2050 */
2044 2051 rptr = mp->b_rptr - len;
2045 2052 if (!OK_32PTR(rptr) ||
2046 2053 ((db = mp->b_datap), db->db_ref != 2) ||
2047 2054 rptr < db->db_base) {
2048 2055 /* NOTE: we assume allocb returns an OK_32PTR */
2049 2056
2050 2057 must_alloc:;
2051 2058 mp1 = allocb(connp->conn_ht_iphc_allocated +
2052 2059 tcps->tcps_wroff_xtra, BPRI_MED);
2053 2060 if (mp1 == NULL) {
2054 2061 freemsg(mp);
2055 2062 return (-1); /* out_of_mem */
2056 2063 }
2057 2064 mp1->b_cont = mp;
2058 2065 mp = mp1;
2059 2066 /* Leave room for Link Level header */
2060 2067 len = total_hdr_len;
2061 2068 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra];
2062 2069 mp->b_wptr = &rptr[len];
2063 2070 }
2064 2071
2065 2072 /*
2066 2073 * Fill in the header using the template header, and add
2067 2074 * options such as time-stamp, ECN and/or SACK, as needed.
2068 2075 */
2069 2076 tcp_fill_header(tcp, rptr, num_sack_blk);
2070 2077
2071 2078 mp->b_rptr = rptr;
2072 2079
2073 2080 if (*tail_unsent) {
2074 2081 int spill = *tail_unsent;
2075 2082
2076 2083 mp1 = mp->b_cont;
2077 2084 if (mp1 == NULL)
2078 2085 mp1 = mp;
2079 2086
2080 2087 /*
2081 2088 * If we're a little short, tack on more mblks until
2082 2089 * there is no more spillover.
2083 2090 */
2084 2091 while (spill < 0) {
2085 2092 mblk_t *nmp;
2086 2093 int nmpsz;
2087 2094
2088 2095 nmp = (*xmit_tail)->b_cont;
2089 2096 nmpsz = MBLKL(nmp);
2090 2097
2091 2098 /*
2092 2099 * Excess data in mblk; can we split it?
2093 2100 * If LSO is enabled for the connection,
2094 2101 * keep on splitting as this is a transient
2095 2102 * send path.
2096 2103 */
2097 2104 if (!do_lso_send && (spill + nmpsz > 0)) {
2098 2105 /*
2099 2106 * Don't split if stream head was
2100 2107 * told to break up larger writes
2101 2108 * into smaller ones.
2102 2109 */
2103 2110 if (tcp->tcp_maxpsz_multiplier > 0)
2104 2111 break;
2105 2112
2106 2113 /*
2107 2114 * Next mblk is less than SMSS/2
2108 2115 * rounded up to nearest 64-byte;
2109 2116 * let it get sent as part of the
2110 2117 * next segment.
2111 2118 */
2112 2119 if (tcp->tcp_localnet &&
2113 2120 !tcp->tcp_cork &&
2114 2121 (nmpsz < roundup((mss >> 1), 64)))
2115 2122 break;
2116 2123 }
2117 2124
2118 2125 *xmit_tail = nmp;
2119 2126 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX);
2120 2127 /* Stash for rtt use later */
2121 2128 (*xmit_tail)->b_prev = local_time;
2122 2129 (*xmit_tail)->b_next =
2123 2130 (mblk_t *)(uintptr_t)(*snxt - len);
2124 2131 mp1->b_cont = dupb(*xmit_tail);
2125 2132 mp1 = mp1->b_cont;
2126 2133
2127 2134 spill += nmpsz;
2128 2135 if (mp1 == NULL) {
2129 2136 *tail_unsent = spill;
2130 2137 freemsg(mp);
2131 2138 return (-1); /* out_of_mem */
2132 2139 }
2133 2140 }
2134 2141
2135 2142 /* Trim back any surplus on the last mblk */
2136 2143 if (spill >= 0) {
2137 2144 mp1->b_wptr -= spill;
↓ open down ↓ |
155 lines elided |
↑ open up ↑ |
2138 2145 *tail_unsent = spill;
2139 2146 } else {
2140 2147 /*
2141 2148 * We did not send everything we could in
2142 2149 * order to remain within the b_cont limit.
2143 2150 */
2144 2151 *usable -= spill;
2145 2152 *snxt += spill;
2146 2153 tcp->tcp_last_sent_len += spill;
2147 2154 TCPS_UPDATE_MIB(tcps, tcpOutDataBytes, spill);
2155 + tcp->tcp_cs.tcp_out_data_bytes += spill;
2148 2156 /*
2149 2157 * Adjust the checksum
2150 2158 */
2151 2159 tcpha = (tcpha_t *)(rptr +
2152 2160 ixa->ixa_ip_hdr_length);
2153 2161 sum += spill;
2154 2162 sum = (sum >> 16) + (sum & 0xFFFF);
2155 2163 tcpha->tha_sum = htons(sum);
2156 2164 if (connp->conn_ipversion == IPV4_VERSION) {
2157 2165 sum = ntohs(
2158 2166 ((ipha_t *)rptr)->ipha_length) +
2159 2167 spill;
2160 2168 ((ipha_t *)rptr)->ipha_length =
2161 2169 htons(sum);
2162 2170 } else {
2163 2171 sum = ntohs(
2164 2172 ((ip6_t *)rptr)->ip6_plen) +
2165 2173 spill;
2166 2174 ((ip6_t *)rptr)->ip6_plen =
2167 2175 htons(sum);
2168 2176 }
2169 2177 ixa->ixa_pktlen += spill;
2170 2178 *tail_unsent = 0;
2171 2179 }
2172 2180 }
2173 2181 if (tcp->tcp_ip_forward_progress) {
2174 2182 tcp->tcp_ip_forward_progress = B_FALSE;
2175 2183 ixa->ixa_flags |= IXAF_REACH_CONF;
2176 2184 } else {
2177 2185 ixa->ixa_flags &= ~IXAF_REACH_CONF;
2178 2186 }
2179 2187
2180 2188 if (do_lso_send) {
2181 2189 /* Append LSO information to the mp. */
2182 2190 lso_info_set(mp, mss, HW_LSO);
2183 2191 ixa->ixa_fragsize = IP_MAXPACKET;
2184 2192 ixa->ixa_extra_ident = num_lso_seg - 1;
2185 2193
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
2186 2194 DTRACE_PROBE2(tcp_send_lso, int, num_lso_seg,
2187 2195 boolean_t, B_TRUE);
2188 2196
2189 2197 tcp_send_data(tcp, mp);
2190 2198
2191 2199 /*
2192 2200 * Restore values of ixa_fragsize and ixa_extra_ident.
2193 2201 */
2194 2202 ixa->ixa_fragsize = ixa->ixa_pmtu;
2195 2203 ixa->ixa_extra_ident = 0;
2196 - tcp->tcp_obsegs += num_lso_seg;
2204 + TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2197 2205 TCP_STAT(tcps, tcp_lso_times);
2198 2206 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg);
2199 2207 } else {
2200 2208 /*
2201 2209 * Make sure to clean up LSO information. Wherever a
2202 2210 * new mp uses the prepended header room after dupb(),
2203 2211 * lso_info_cleanup() should be called.
2204 2212 */
2205 2213 lso_info_cleanup(mp);
2206 2214 tcp_send_data(tcp, mp);
2207 - BUMP_LOCAL(tcp->tcp_obsegs);
2215 + TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2208 2216 }
2209 2217 }
2210 2218
2211 2219 return (0);
2212 2220 }
2213 2221
2214 2222 /*
2215 2223 * Initiate closedown sequence on an active connection. (May be called as
2216 2224 * writer.) Return value zero for OK return, non-zero for error return.
2217 2225 */
2218 2226 static int
2219 2227 tcp_xmit_end(tcp_t *tcp)
2220 2228 {
2221 2229 mblk_t *mp;
2222 2230 tcp_stack_t *tcps = tcp->tcp_tcps;
2223 2231 iulp_t uinfo;
2224 2232 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
2225 2233 conn_t *connp = tcp->tcp_connp;
2226 2234
2227 2235 if (tcp->tcp_state < TCPS_SYN_RCVD ||
2228 2236 tcp->tcp_state > TCPS_CLOSE_WAIT) {
2229 2237 /*
2230 2238 * Invalid state, only states TCPS_SYN_RCVD,
2231 2239 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
2232 2240 */
2233 2241 return (-1);
2234 2242 }
2235 2243
2236 2244 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent;
2237 2245 tcp->tcp_valid_bits |= TCP_FSS_VALID;
2238 2246 /*
2239 2247 * If there is nothing more unsent, send the FIN now.
2240 2248 * Otherwise, it will go out with the last segment.
2241 2249 */
2242 2250 if (tcp->tcp_unsent == 0) {
2243 2251 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
2244 2252 tcp->tcp_fss, B_FALSE, NULL, B_FALSE);
2245 2253
2246 2254 if (mp) {
2247 2255 tcp_send_data(tcp, mp);
2248 2256 } else {
2249 2257 /*
2250 2258 * Couldn't allocate msg. Pretend we got it out.
2251 2259 * Wait for rexmit timeout.
2252 2260 */
2253 2261 tcp->tcp_snxt = tcp->tcp_fss + 1;
2254 2262 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
2255 2263 }
2256 2264
2257 2265 /*
2258 2266 * If needed, update tcp_rexmit_snxt as tcp_snxt is
2259 2267 * changed.
2260 2268 */
2261 2269 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) {
2262 2270 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
2263 2271 }
2264 2272 } else {
2265 2273 /*
2266 2274 * If tcp->tcp_cork is set, then the data will not get sent,
2267 2275 * so we have to check that and unset it first.
2268 2276 */
2269 2277 if (tcp->tcp_cork)
2270 2278 tcp->tcp_cork = B_FALSE;
2271 2279 tcp_wput_data(tcp, NULL, B_FALSE);
2272 2280 }
2273 2281
2274 2282 /*
2275 2283 * If TCP does not get enough samples of RTT or tcp_rtt_updates
2276 2284 * is 0, don't update the cache.
2277 2285 */
2278 2286 if (tcps->tcps_rtt_updates == 0 ||
2279 2287 tcp->tcp_rtt_update < tcps->tcps_rtt_updates)
2280 2288 return (0);
2281 2289
2282 2290 /*
2283 2291 * We do not have a good algorithm to update ssthresh at this time.
2284 2292 * So don't do any update.
2285 2293 */
2286 2294 bzero(&uinfo, sizeof (uinfo));
2287 2295 uinfo.iulp_rtt = NSEC2MSEC(tcp->tcp_rtt_sa);
2288 2296 uinfo.iulp_rtt_sd = NSEC2MSEC(tcp->tcp_rtt_sd);
2289 2297
2290 2298 /*
2291 2299 * Note that uinfo is kept for conn_faddr in the DCE. Could update even
2292 2300 * if source routed but we don't.
2293 2301 */
2294 2302 if (connp->conn_ipversion == IPV4_VERSION) {
2295 2303 if (connp->conn_faddr_v4 != tcp->tcp_ipha->ipha_dst) {
2296 2304 return (0);
2297 2305 }
2298 2306 (void) dce_update_uinfo_v4(connp->conn_faddr_v4, &uinfo, ipst);
2299 2307 } else {
2300 2308 uint_t ifindex;
2301 2309
2302 2310 if (!(IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6,
2303 2311 &tcp->tcp_ip6h->ip6_dst))) {
2304 2312 return (0);
2305 2313 }
2306 2314 ifindex = 0;
2307 2315 if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_faddr_v6)) {
2308 2316 ip_xmit_attr_t *ixa = connp->conn_ixa;
2309 2317
2310 2318 /*
2311 2319 * If we are going to create a DCE we'd better have
2312 2320 * an ifindex
2313 2321 */
2314 2322 if (ixa->ixa_nce != NULL) {
2315 2323 ifindex = ixa->ixa_nce->nce_common->ncec_ill->
2316 2324 ill_phyint->phyint_ifindex;
2317 2325 } else {
2318 2326 return (0);
2319 2327 }
2320 2328 }
2321 2329
2322 2330 (void) dce_update_uinfo(&connp->conn_faddr_v6, ifindex, &uinfo,
2323 2331 ipst);
2324 2332 }
2325 2333 return (0);
2326 2334 }
2327 2335
2328 2336 /*
2329 2337 * Send out a control packet on the tcp connection specified. This routine
2330 2338 * is typically called where we need a simple ACK or RST generated.
2331 2339 */
2332 2340 void
2333 2341 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl)
2334 2342 {
2335 2343 uchar_t *rptr;
2336 2344 tcpha_t *tcpha;
2337 2345 ipha_t *ipha = NULL;
2338 2346 ip6_t *ip6h = NULL;
2339 2347 uint32_t sum;
2340 2348 int total_hdr_len;
2341 2349 int ip_hdr_len;
2342 2350 mblk_t *mp;
2343 2351 tcp_stack_t *tcps = tcp->tcp_tcps;
2344 2352 conn_t *connp = tcp->tcp_connp;
2345 2353 ip_xmit_attr_t *ixa = connp->conn_ixa;
2346 2354
2347 2355 /*
2348 2356 * Save sum for use in source route later.
2349 2357 */
2350 2358 sum = connp->conn_ht_ulp_len + connp->conn_sum;
2351 2359 total_hdr_len = connp->conn_ht_iphc_len;
2352 2360 ip_hdr_len = ixa->ixa_ip_hdr_length;
2353 2361
2354 2362 /* If a text string is passed in with the request, pass it to strlog. */
2355 2363 if (str != NULL && connp->conn_debug) {
2356 2364 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2357 2365 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x",
2358 2366 str, seq, ack, ctl);
2359 2367 }
2360 2368 mp = allocb(connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra,
2361 2369 BPRI_MED);
2362 2370 if (mp == NULL) {
2363 2371 return;
2364 2372 }
2365 2373 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra];
2366 2374 mp->b_rptr = rptr;
2367 2375 mp->b_wptr = &rptr[total_hdr_len];
2368 2376 bcopy(connp->conn_ht_iphc, rptr, total_hdr_len);
2369 2377
2370 2378 ixa->ixa_pktlen = total_hdr_len;
2371 2379
2372 2380 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2373 2381 ipha = (ipha_t *)rptr;
2374 2382 ipha->ipha_length = htons(total_hdr_len);
2375 2383 } else {
2376 2384 ip6h = (ip6_t *)rptr;
2377 2385 ip6h->ip6_plen = htons(total_hdr_len - IPV6_HDR_LEN);
2378 2386 }
2379 2387 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2380 2388 tcpha->tha_flags = (uint8_t)ctl;
2381 2389 if (ctl & TH_RST) {
2382 2390 TCPS_BUMP_MIB(tcps, tcpOutRsts);
2383 2391 TCPS_BUMP_MIB(tcps, tcpOutControl);
2384 2392 /*
2385 2393 * Don't send TSopt w/ TH_RST packets per RFC 1323.
2386 2394 */
2387 2395 if (tcp->tcp_snd_ts_ok &&
2388 2396 tcp->tcp_state > TCPS_SYN_SENT) {
2389 2397 mp->b_wptr = &rptr[total_hdr_len - TCPOPT_REAL_TS_LEN];
2390 2398 *(mp->b_wptr) = TCPOPT_EOL;
2391 2399
2392 2400 ixa->ixa_pktlen = total_hdr_len - TCPOPT_REAL_TS_LEN;
2393 2401
2394 2402 if (connp->conn_ipversion == IPV4_VERSION) {
2395 2403 ipha->ipha_length = htons(total_hdr_len -
2396 2404 TCPOPT_REAL_TS_LEN);
2397 2405 } else {
2398 2406 ip6h->ip6_plen = htons(total_hdr_len -
2399 2407 IPV6_HDR_LEN - TCPOPT_REAL_TS_LEN);
2400 2408 }
2401 2409 tcpha->tha_offset_and_reserved -= (3 << 4);
2402 2410 sum -= TCPOPT_REAL_TS_LEN;
2403 2411 }
2404 2412 }
2405 2413 if (ctl & TH_ACK) {
2406 2414 if (tcp->tcp_snd_ts_ok) {
2407 2415 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2408 2416
2409 2417 U32_TO_BE32(llbolt,
2410 2418 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
2411 2419 U32_TO_BE32(tcp->tcp_ts_recent,
2412 2420 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
↓ open down ↓ |
195 lines elided |
↑ open up ↑ |
2413 2421 }
2414 2422
2415 2423 /* Update the latest receive window size in TCP header. */
2416 2424 tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2417 2425 /* Track what we sent to the peer */
2418 2426 tcp->tcp_tcpha->tha_win = tcpha->tha_win;
2419 2427 tcp->tcp_rack = ack;
2420 2428 tcp->tcp_rack_cnt = 0;
2421 2429 TCPS_BUMP_MIB(tcps, tcpOutAck);
2422 2430 }
2423 - BUMP_LOCAL(tcp->tcp_obsegs);
2431 + TCPS_BUMP_MIB(tcps, tcpHCOutSegs);
2424 2432 tcpha->tha_seq = htonl(seq);
2425 2433 tcpha->tha_ack = htonl(ack);
2426 2434 /*
2427 2435 * Include the adjustment for a source route if any.
2428 2436 */
2429 2437 sum = (sum >> 16) + (sum & 0xFFFF);
2430 2438 tcpha->tha_sum = htons(sum);
2431 2439 tcp_send_data(tcp, mp);
2432 2440 }
2433 2441
2434 2442 /*
2435 2443 * Generate a reset based on an inbound packet, connp is set by caller
2436 2444 * when RST is in response to an unexpected inbound packet for which
2437 2445 * there is active tcp state in the system.
2438 2446 *
2439 2447 * IPSEC NOTE : Try to send the reply with the same protection as it came
2440 2448 * in. We have the ip_recv_attr_t which is reversed to form the ip_xmit_attr_t.
2441 2449 * That way the packet will go out at the same level of protection as it
2442 2450 * came in with.
2443 2451 */
2444 2452 static void
2445 2453 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, uint32_t ack, int ctl,
2446 2454 ip_recv_attr_t *ira, ip_stack_t *ipst, conn_t *connp)
2447 2455 {
2448 2456 ipha_t *ipha = NULL;
2449 2457 ip6_t *ip6h = NULL;
2450 2458 ushort_t len;
2451 2459 tcpha_t *tcpha;
2452 2460 int i;
2453 2461 ipaddr_t v4addr;
2454 2462 in6_addr_t v6addr;
2455 2463 netstack_t *ns = ipst->ips_netstack;
2456 2464 tcp_stack_t *tcps = ns->netstack_tcp;
2457 2465 ip_xmit_attr_t ixas, *ixa;
2458 2466 uint_t ip_hdr_len = ira->ira_ip_hdr_length;
2459 2467 boolean_t need_refrele = B_FALSE; /* ixa_refrele(ixa) */
2460 2468 ushort_t port;
2461 2469
2462 2470 if (!tcp_send_rst_chk(tcps)) {
2463 2471 TCP_STAT(tcps, tcp_rst_unsent);
2464 2472 freemsg(mp);
2465 2473 return;
2466 2474 }
2467 2475
2468 2476 /*
2469 2477 * If connp != NULL we use conn_ixa to keep IP_NEXTHOP and other
2470 2478 * options from the listener. In that case the caller must ensure that
2471 2479 * we are running on the listener = connp squeue.
2472 2480 *
2473 2481 * We get a safe copy of conn_ixa so we don't need to restore anything
2474 2482 * we or ip_output_simple might change in the ixa.
2475 2483 */
2476 2484 if (connp != NULL) {
2477 2485 ASSERT(connp->conn_on_sqp);
2478 2486
2479 2487 ixa = conn_get_ixa_exclusive(connp);
2480 2488 if (ixa == NULL) {
2481 2489 TCP_STAT(tcps, tcp_rst_unsent);
2482 2490 freemsg(mp);
2483 2491 return;
2484 2492 }
2485 2493 need_refrele = B_TRUE;
2486 2494 } else {
2487 2495 bzero(&ixas, sizeof (ixas));
2488 2496 ixa = &ixas;
2489 2497 /*
2490 2498 * IXAF_VERIFY_SOURCE is overkill since we know the
2491 2499 * packet was for us.
2492 2500 */
2493 2501 ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE;
2494 2502 ixa->ixa_protocol = IPPROTO_TCP;
2495 2503 ixa->ixa_zoneid = ira->ira_zoneid;
2496 2504 ixa->ixa_ifindex = 0;
2497 2505 ixa->ixa_ipst = ipst;
2498 2506 ixa->ixa_cred = kcred;
2499 2507 ixa->ixa_cpid = NOPID;
2500 2508 }
2501 2509
2502 2510 if (str && tcps->tcps_dbg) {
2503 2511 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE,
2504 2512 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
2505 2513 "flags 0x%x",
2506 2514 str, seq, ack, ctl);
2507 2515 }
2508 2516 if (mp->b_datap->db_ref != 1) {
2509 2517 mblk_t *mp1 = copyb(mp);
2510 2518 freemsg(mp);
2511 2519 mp = mp1;
2512 2520 if (mp == NULL)
2513 2521 goto done;
2514 2522 } else if (mp->b_cont) {
2515 2523 freemsg(mp->b_cont);
2516 2524 mp->b_cont = NULL;
2517 2525 DB_CKSUMFLAGS(mp) = 0;
2518 2526 }
2519 2527 /*
2520 2528 * We skip reversing source route here.
2521 2529 * (for now we replace all IP options with EOL)
2522 2530 */
2523 2531 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2524 2532 ipha = (ipha_t *)mp->b_rptr;
2525 2533 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
2526 2534 mp->b_rptr[i] = IPOPT_EOL;
2527 2535 /*
2528 2536 * Make sure that src address isn't flagrantly invalid.
2529 2537 * Not all broadcast address checking for the src address
2530 2538 * is possible, since we don't know the netmask of the src
2531 2539 * addr. No check for destination address is done, since
2532 2540 * IP will not pass up a packet with a broadcast dest
2533 2541 * address to TCP. Similar checks are done below for IPv6.
2534 2542 */
2535 2543 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST ||
2536 2544 CLASSD(ipha->ipha_src)) {
2537 2545 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
2538 2546 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
2539 2547 freemsg(mp);
2540 2548 goto done;
2541 2549 }
2542 2550 } else {
2543 2551 ip6h = (ip6_t *)mp->b_rptr;
2544 2552
2545 2553 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) ||
2546 2554 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) {
2547 2555 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards);
2548 2556 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
2549 2557 freemsg(mp);
2550 2558 goto done;
2551 2559 }
2552 2560
2553 2561 /* Remove any extension headers assuming partial overlay */
2554 2562 if (ip_hdr_len > IPV6_HDR_LEN) {
2555 2563 uint8_t *to;
2556 2564
2557 2565 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN;
2558 2566 ovbcopy(ip6h, to, IPV6_HDR_LEN);
2559 2567 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN;
2560 2568 ip_hdr_len = IPV6_HDR_LEN;
2561 2569 ip6h = (ip6_t *)mp->b_rptr;
2562 2570 ip6h->ip6_nxt = IPPROTO_TCP;
2563 2571 }
2564 2572 }
2565 2573 tcpha = (tcpha_t *)&mp->b_rptr[ip_hdr_len];
2566 2574 if (tcpha->tha_flags & TH_RST) {
2567 2575 freemsg(mp);
2568 2576 goto done;
2569 2577 }
2570 2578 tcpha->tha_offset_and_reserved = (5 << 4);
2571 2579 len = ip_hdr_len + sizeof (tcpha_t);
2572 2580 mp->b_wptr = &mp->b_rptr[len];
2573 2581 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2574 2582 ipha->ipha_length = htons(len);
2575 2583 /* Swap addresses */
2576 2584 v4addr = ipha->ipha_src;
2577 2585 ipha->ipha_src = ipha->ipha_dst;
2578 2586 ipha->ipha_dst = v4addr;
2579 2587 ipha->ipha_ident = 0;
2580 2588 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl;
2581 2589 ixa->ixa_flags |= IXAF_IS_IPV4;
2582 2590 ixa->ixa_ip_hdr_length = ip_hdr_len;
2583 2591 } else {
2584 2592 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN);
2585 2593 /* Swap addresses */
2586 2594 v6addr = ip6h->ip6_src;
2587 2595 ip6h->ip6_src = ip6h->ip6_dst;
2588 2596 ip6h->ip6_dst = v6addr;
2589 2597 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit;
2590 2598 ixa->ixa_flags &= ~IXAF_IS_IPV4;
2591 2599
2592 2600 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_dst)) {
2593 2601 ixa->ixa_flags |= IXAF_SCOPEID_SET;
2594 2602 ixa->ixa_scopeid = ira->ira_ruifindex;
2595 2603 }
2596 2604 ixa->ixa_ip_hdr_length = IPV6_HDR_LEN;
2597 2605 }
2598 2606 ixa->ixa_pktlen = len;
2599 2607
2600 2608 /* Swap the ports */
2601 2609 port = tcpha->tha_fport;
2602 2610 tcpha->tha_fport = tcpha->tha_lport;
2603 2611 tcpha->tha_lport = port;
2604 2612
2605 2613 tcpha->tha_ack = htonl(ack);
2606 2614 tcpha->tha_seq = htonl(seq);
2607 2615 tcpha->tha_win = 0;
2608 2616 tcpha->tha_sum = htons(sizeof (tcpha_t));
2609 2617 tcpha->tha_flags = (uint8_t)ctl;
2610 2618 if (ctl & TH_RST) {
2611 2619 if (ctl & TH_ACK) {
2612 2620 /*
2613 2621 * Probe connection rejection here.
2614 2622 * tcp_xmit_listeners_reset() drops non-SYN segments
2615 2623 * that do not specify TH_ACK in their flags without
2616 2624 * calling this function. As a consequence, if this
2617 2625 * function is called with a TH_RST|TH_ACK ctl argument,
2618 2626 * it is being called in response to a SYN segment
2619 2627 * and thus the tcp:::accept-refused probe point
2620 2628 * is valid here.
2621 2629 */
2622 2630 DTRACE_TCP5(accept__refused, mblk_t *, NULL,
2623 2631 void, NULL, void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2624 2632 tcph_t *, tcpha);
2625 2633 }
2626 2634 TCPS_BUMP_MIB(tcps, tcpOutRsts);
2627 2635 TCPS_BUMP_MIB(tcps, tcpOutControl);
2628 2636 }
2629 2637
2630 2638 /* Discard any old label */
2631 2639 if (ixa->ixa_free_flags & IXA_FREE_TSL) {
2632 2640 ASSERT(ixa->ixa_tsl != NULL);
2633 2641 label_rele(ixa->ixa_tsl);
2634 2642 ixa->ixa_free_flags &= ~IXA_FREE_TSL;
2635 2643 }
2636 2644 ixa->ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2637 2645
2638 2646 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2639 2647 /*
2640 2648 * Apply IPsec based on how IPsec was applied to
2641 2649 * the packet that caused the RST.
2642 2650 */
2643 2651 if (!ipsec_in_to_out(ira, ixa, mp, ipha, ip6h)) {
2644 2652 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2645 2653 /* Note: mp already consumed and ip_drop_packet done */
2646 2654 goto done;
2647 2655 }
2648 2656 } else {
2649 2657 /*
2650 2658 * This is in clear. The RST message we are building
2651 2659 * here should go out in clear, independent of our policy.
2652 2660 */
2653 2661 ixa->ixa_flags |= IXAF_NO_IPSEC;
2654 2662 }
2655 2663
2656 2664 DTRACE_TCP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa,
2657 2665 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2658 2666 __dtrace_tcp_tcph_t *, tcpha);
2659 2667
2660 2668 /*
2661 2669 * NOTE: one might consider tracing a TCP packet here, but
2662 2670 * this function has no active TCP state and no tcp structure
2663 2671 * that has a trace buffer. If we traced here, we would have
2664 2672 * to keep a local trace buffer in tcp_record_trace().
2665 2673 */
2666 2674
2667 2675 (void) ip_output_simple(mp, ixa);
2668 2676 done:
2669 2677 ixa_cleanup(ixa);
2670 2678 if (need_refrele) {
2671 2679 ASSERT(ixa != &ixas);
2672 2680 ixa_refrele(ixa);
2673 2681 }
2674 2682 }
2675 2683
2676 2684 /*
2677 2685 * Generate a "no listener here" RST in response to an "unknown" segment.
2678 2686 * connp is set by caller when RST is in response to an unexpected
2679 2687 * inbound packet for which there is active tcp state in the system.
2680 2688 * Note that we are reusing the incoming mp to construct the outgoing RST.
2681 2689 */
2682 2690 void
2683 2691 tcp_xmit_listeners_reset(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst,
2684 2692 conn_t *connp)
2685 2693 {
2686 2694 uchar_t *rptr;
2687 2695 uint32_t seg_len;
2688 2696 tcpha_t *tcpha;
2689 2697 uint32_t seg_seq;
2690 2698 uint32_t seg_ack;
2691 2699 uint_t flags;
2692 2700 ipha_t *ipha;
2693 2701 ip6_t *ip6h;
2694 2702 boolean_t policy_present;
2695 2703 netstack_t *ns = ipst->ips_netstack;
2696 2704 tcp_stack_t *tcps = ns->netstack_tcp;
2697 2705 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec;
2698 2706 uint_t ip_hdr_len = ira->ira_ip_hdr_length;
2699 2707
2700 2708 TCP_STAT(tcps, tcp_no_listener);
2701 2709
2702 2710 /*
2703 2711 * DTrace this "unknown" segment as a tcp:::receive, as we did
2704 2712 * just receive something that was TCP.
2705 2713 */
2706 2714 DTRACE_TCP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, NULL,
2707 2715 __dtrace_tcp_void_ip_t *, mp->b_rptr, tcp_t *, NULL,
2708 2716 __dtrace_tcp_tcph_t *, &mp->b_rptr[ip_hdr_len]);
2709 2717
2710 2718 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
2711 2719 policy_present = ipss->ipsec_inbound_v4_policy_present;
2712 2720 ipha = (ipha_t *)mp->b_rptr;
2713 2721 ip6h = NULL;
2714 2722 } else {
2715 2723 policy_present = ipss->ipsec_inbound_v6_policy_present;
2716 2724 ipha = NULL;
2717 2725 ip6h = (ip6_t *)mp->b_rptr;
2718 2726 }
2719 2727
2720 2728 if (policy_present) {
2721 2729 /*
2722 2730 * The conn_t parameter is NULL because we already know
2723 2731 * nobody's home.
2724 2732 */
2725 2733 mp = ipsec_check_global_policy(mp, (conn_t *)NULL, ipha, ip6h,
2726 2734 ira, ns);
2727 2735 if (mp == NULL)
2728 2736 return;
2729 2737 }
2730 2738 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
2731 2739 DTRACE_PROBE2(
2732 2740 tx__ip__log__error__nolistener__tcp,
2733 2741 char *, "Could not reply with RST to mp(1)",
2734 2742 mblk_t *, mp);
2735 2743 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n"));
2736 2744 freemsg(mp);
2737 2745 return;
2738 2746 }
2739 2747
2740 2748 rptr = mp->b_rptr;
2741 2749
2742 2750 tcpha = (tcpha_t *)&rptr[ip_hdr_len];
2743 2751 seg_seq = ntohl(tcpha->tha_seq);
2744 2752 seg_ack = ntohl(tcpha->tha_ack);
2745 2753 flags = tcpha->tha_flags;
2746 2754
2747 2755 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcpha) + ip_hdr_len);
2748 2756 if (flags & TH_RST) {
2749 2757 freemsg(mp);
2750 2758 } else if (flags & TH_ACK) {
2751 2759 tcp_xmit_early_reset("no tcp, reset", mp, seg_ack, 0, TH_RST,
2752 2760 ira, ipst, connp);
2753 2761 } else {
2754 2762 if (flags & TH_SYN) {
2755 2763 seg_len++;
2756 2764 } else {
2757 2765 /*
2758 2766 * Here we violate the RFC. Note that a normal
2759 2767 * TCP will never send a segment without the ACK
2760 2768 * flag, except for RST or SYN segment. This
2761 2769 * segment is neither. Just drop it on the
2762 2770 * floor.
2763 2771 */
2764 2772 freemsg(mp);
2765 2773 TCP_STAT(tcps, tcp_rst_unsent);
2766 2774 return;
2767 2775 }
2768 2776
2769 2777 tcp_xmit_early_reset("no tcp, reset/ack", mp, 0,
2770 2778 seg_seq + seg_len, TH_RST | TH_ACK, ira, ipst, connp);
2771 2779 }
2772 2780 }
2773 2781
2774 2782 /*
2775 2783 * Helper function for tcp_xmit_mp() in handling connection set up flag
2776 2784 * options setting.
2777 2785 */
2778 2786 static void
2779 2787 tcp_xmit_mp_aux_iss(tcp_t *tcp, conn_t *connp, tcpha_t *tcpha, mblk_t *mp,
2780 2788 uint_t *flags)
2781 2789 {
2782 2790 uint32_t u1;
2783 2791 uint8_t *wptr = mp->b_wptr;
2784 2792 tcp_stack_t *tcps = tcp->tcp_tcps;
2785 2793 boolean_t add_sack = B_FALSE;
2786 2794
2787 2795 /*
2788 2796 * If TCP_ISS_VALID and the seq number is tcp_iss,
2789 2797 * TCP can only be in SYN-SENT, SYN-RCVD or
2790 2798 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if
2791 2799 * our SYN is not ack'ed but the app closes this
2792 2800 * TCP connection.
2793 2801 */
2794 2802 ASSERT(tcp->tcp_state == TCPS_SYN_SENT ||
2795 2803 tcp->tcp_state == TCPS_SYN_RCVD ||
2796 2804 tcp->tcp_state == TCPS_FIN_WAIT_1);
2797 2805
2798 2806 /*
2799 2807 * Tack on the MSS option. It is always needed
2800 2808 * for both active and passive open.
2801 2809 *
2802 2810 * MSS option value should be interface MTU - MIN
2803 2811 * TCP/IP header according to RFC 793 as it means
2804 2812 * the maximum segment size TCP can receive. But
2805 2813 * to get around some broken middle boxes/end hosts
2806 2814 * out there, we allow the option value to be the
2807 2815 * same as the MSS option size on the peer side.
2808 2816 * In this way, the other side will not send
2809 2817 * anything larger than they can receive.
2810 2818 *
2811 2819 * Note that for SYN_SENT state, the ndd param
2812 2820 * tcp_use_smss_as_mss_opt has no effect as we
2813 2821 * don't know the peer's MSS option value. So
2814 2822 * the only case we need to take care of is in
2815 2823 * SYN_RCVD state, which is done later.
2816 2824 */
2817 2825 wptr[0] = TCPOPT_MAXSEG;
2818 2826 wptr[1] = TCPOPT_MAXSEG_LEN;
2819 2827 wptr += 2;
2820 2828 u1 = tcp->tcp_initial_pmtu - (connp->conn_ipversion == IPV4_VERSION ?
2821 2829 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - TCP_MIN_HEADER_LENGTH;
2822 2830 U16_TO_BE16(u1, wptr);
2823 2831 wptr += 2;
2824 2832
2825 2833 /* Update the offset to cover the additional word */
2826 2834 tcpha->tha_offset_and_reserved += (1 << 4);
2827 2835
2828 2836 switch (tcp->tcp_state) {
2829 2837 case TCPS_SYN_SENT:
2830 2838 *flags = TH_SYN;
2831 2839
2832 2840 if (tcp->tcp_snd_sack_ok)
2833 2841 add_sack = B_TRUE;
2834 2842
2835 2843 if (tcp->tcp_snd_ts_ok) {
2836 2844 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
2837 2845
2838 2846 if (add_sack) {
2839 2847 wptr[0] = TCPOPT_SACK_PERMITTED;
2840 2848 wptr[1] = TCPOPT_SACK_OK_LEN;
2841 2849 add_sack = B_FALSE;
2842 2850 } else {
2843 2851 wptr[0] = TCPOPT_NOP;
2844 2852 wptr[1] = TCPOPT_NOP;
2845 2853 }
2846 2854 wptr[2] = TCPOPT_TSTAMP;
2847 2855 wptr[3] = TCPOPT_TSTAMP_LEN;
2848 2856 wptr += 4;
2849 2857 U32_TO_BE32(llbolt, wptr);
2850 2858 wptr += 4;
2851 2859 ASSERT(tcp->tcp_ts_recent == 0);
2852 2860 U32_TO_BE32(0L, wptr);
2853 2861 wptr += 4;
2854 2862 tcpha->tha_offset_and_reserved += (3 << 4);
2855 2863 }
2856 2864
2857 2865 /*
2858 2866 * Set up all the bits to tell other side
2859 2867 * we are ECN capable.
2860 2868 */
2861 2869 if (tcp->tcp_ecn_ok)
2862 2870 *flags |= (TH_ECE | TH_CWR);
2863 2871
2864 2872 break;
2865 2873
2866 2874 case TCPS_SYN_RCVD:
2867 2875 *flags |= TH_SYN;
2868 2876
2869 2877 /*
2870 2878 * Reset the MSS option value to be SMSS
2871 2879 * We should probably add back the bytes
2872 2880 * for timestamp option and IPsec. We
2873 2881 * don't do that as this is a workaround
2874 2882 * for broken middle boxes/end hosts, it
2875 2883 * is better for us to be more cautious.
2876 2884 * They may not take these things into
2877 2885 * account in their SMSS calculation. Thus
2878 2886 * the peer's calculated SMSS may be smaller
2879 2887 * than what it can be. This should be OK.
2880 2888 */
2881 2889 if (tcps->tcps_use_smss_as_mss_opt) {
2882 2890 u1 = tcp->tcp_mss;
2883 2891 /*
2884 2892 * Note that wptr points just past the MSS
2885 2893 * option value.
2886 2894 */
2887 2895 U16_TO_BE16(u1, wptr - 2);
2888 2896 }
2889 2897
2890 2898 /*
2891 2899 * tcp_snd_ts_ok can only be set in TCPS_SYN_RCVD
2892 2900 * when the peer also uses timestamps option. And
2893 2901 * the TCP header template must have already been
2894 2902 * updated to include the timestamps option.
2895 2903 */
2896 2904 if (tcp->tcp_snd_sack_ok) {
2897 2905 if (tcp->tcp_snd_ts_ok) {
2898 2906 uint8_t *tmp_wptr;
2899 2907
2900 2908 /*
2901 2909 * Use the NOP in the header just
2902 2910 * before timestamps opton.
2903 2911 */
2904 2912 tmp_wptr = (uint8_t *)tcpha +
2905 2913 TCP_MIN_HEADER_LENGTH;
2906 2914 ASSERT(tmp_wptr[0] == TCPOPT_NOP &&
2907 2915 tmp_wptr[1] == TCPOPT_NOP);
2908 2916 tmp_wptr[0] = TCPOPT_SACK_PERMITTED;
2909 2917 tmp_wptr[1] = TCPOPT_SACK_OK_LEN;
2910 2918 } else {
2911 2919 add_sack = B_TRUE;
2912 2920 }
2913 2921 }
2914 2922
2915 2923
2916 2924 /*
2917 2925 * If the other side is ECN capable, reply
2918 2926 * that we are also ECN capable.
2919 2927 */
2920 2928 if (tcp->tcp_ecn_ok)
2921 2929 *flags |= TH_ECE;
2922 2930 break;
2923 2931
2924 2932 default:
2925 2933 /*
2926 2934 * The above ASSERT() makes sure that this
2927 2935 * must be FIN-WAIT-1 state. Our SYN has
2928 2936 * not been ack'ed so retransmit it.
2929 2937 */
2930 2938 *flags |= TH_SYN;
2931 2939 break;
2932 2940 }
2933 2941
2934 2942 if (add_sack) {
2935 2943 wptr[0] = TCPOPT_NOP;
2936 2944 wptr[1] = TCPOPT_NOP;
2937 2945 wptr[2] = TCPOPT_SACK_PERMITTED;
2938 2946 wptr[3] = TCPOPT_SACK_OK_LEN;
2939 2947 wptr += TCPOPT_REAL_SACK_OK_LEN;
2940 2948 tcpha->tha_offset_and_reserved += (1 << 4);
2941 2949 }
2942 2950
2943 2951 if (tcp->tcp_snd_ws_ok) {
2944 2952 wptr[0] = TCPOPT_NOP;
2945 2953 wptr[1] = TCPOPT_WSCALE;
2946 2954 wptr[2] = TCPOPT_WS_LEN;
2947 2955 wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
2948 2956 wptr += TCPOPT_REAL_WS_LEN;
2949 2957 tcpha->tha_offset_and_reserved += (1 << 4);
2950 2958 }
2951 2959
2952 2960 mp->b_wptr = wptr;
2953 2961 u1 = (int)(mp->b_wptr - mp->b_rptr);
2954 2962 /*
2955 2963 * Get IP set to checksum on our behalf
2956 2964 * Include the adjustment for a source route if any.
2957 2965 */
2958 2966 u1 += connp->conn_sum;
2959 2967 u1 = (u1 >> 16) + (u1 & 0xFFFF);
2960 2968 tcpha->tha_sum = htons(u1);
2961 2969 TCPS_BUMP_MIB(tcps, tcpOutControl);
2962 2970 }
2963 2971
2964 2972 /*
2965 2973 * Helper function for tcp_xmit_mp() in handling connection tear down
2966 2974 * flag setting and state changes.
2967 2975 */
2968 2976 static void
2969 2977 tcp_xmit_mp_aux_fss(tcp_t *tcp, ip_xmit_attr_t *ixa, uint_t *flags)
2970 2978 {
2971 2979 if (!tcp->tcp_fin_acked) {
2972 2980 *flags |= TH_FIN;
2973 2981 TCPS_BUMP_MIB(tcp->tcp_tcps, tcpOutControl);
2974 2982 }
2975 2983 if (!tcp->tcp_fin_sent) {
2976 2984 tcp->tcp_fin_sent = B_TRUE;
2977 2985 switch (tcp->tcp_state) {
2978 2986 case TCPS_SYN_RCVD:
2979 2987 tcp->tcp_state = TCPS_FIN_WAIT_1;
2980 2988 DTRACE_TCP6(state__change, void, NULL,
2981 2989 ip_xmit_attr_t *, ixa, void, NULL,
2982 2990 tcp_t *, tcp, void, NULL,
2983 2991 int32_t, TCPS_SYN_RCVD);
2984 2992 break;
2985 2993 case TCPS_ESTABLISHED:
2986 2994 tcp->tcp_state = TCPS_FIN_WAIT_1;
2987 2995 DTRACE_TCP6(state__change, void, NULL,
2988 2996 ip_xmit_attr_t *, ixa, void, NULL,
2989 2997 tcp_t *, tcp, void, NULL,
2990 2998 int32_t, TCPS_ESTABLISHED);
2991 2999 break;
2992 3000 case TCPS_CLOSE_WAIT:
2993 3001 tcp->tcp_state = TCPS_LAST_ACK;
2994 3002 DTRACE_TCP6(state__change, void, NULL,
2995 3003 ip_xmit_attr_t *, ixa, void, NULL,
2996 3004 tcp_t *, tcp, void, NULL,
2997 3005 int32_t, TCPS_CLOSE_WAIT);
2998 3006 break;
2999 3007 }
3000 3008 if (tcp->tcp_suna == tcp->tcp_snxt)
3001 3009 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3002 3010 tcp->tcp_snxt = tcp->tcp_fss + 1;
3003 3011 }
3004 3012 }
3005 3013
3006 3014 /*
3007 3015 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
3008 3016 * ip and tcp header ready to pass down to IP. If the mp passed in is
3009 3017 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
3010 3018 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
3011 3019 * otherwise it will dup partial mblks.)
3012 3020 * Otherwise, an appropriate ACK packet will be generated. This
3013 3021 * routine is not usually called to send new data for the first time. It
3014 3022 * is mostly called out of the timer for retransmits, and to generate ACKs.
3015 3023 *
3016 3024 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
3017 3025 * be adjusted by *offset. And after dupb(), the offset and the ending mblk
3018 3026 * of the original mblk chain will be returned in *offset and *end_mp.
3019 3027 */
3020 3028 mblk_t *
3021 3029 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset,
3022 3030 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len,
3023 3031 boolean_t rexmit)
3024 3032 {
3025 3033 int data_length;
3026 3034 int32_t off = 0;
3027 3035 uint_t flags;
3028 3036 mblk_t *mp1;
3029 3037 mblk_t *mp2;
3030 3038 uchar_t *rptr;
3031 3039 tcpha_t *tcpha;
3032 3040 int32_t num_sack_blk = 0;
3033 3041 int32_t sack_opt_len = 0;
3034 3042 tcp_stack_t *tcps = tcp->tcp_tcps;
3035 3043 conn_t *connp = tcp->tcp_connp;
3036 3044 ip_xmit_attr_t *ixa = connp->conn_ixa;
3037 3045
3038 3046 /* Allocate for our maximum TCP header + link-level */
3039 3047 mp1 = allocb(connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra,
3040 3048 BPRI_MED);
3041 3049 if (mp1 == NULL)
3042 3050 return (NULL);
3043 3051 data_length = 0;
3044 3052
3045 3053 /*
3046 3054 * Note that tcp_mss has been adjusted to take into account the
3047 3055 * timestamp option if applicable. Because SACK options do not
3048 3056 * appear in every TCP segments and they are of variable lengths,
3049 3057 * they cannot be included in tcp_mss. Thus we need to calculate
3050 3058 * the actual segment length when we need to send a segment which
3051 3059 * includes SACK options.
3052 3060 */
3053 3061 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
3054 3062 num_sack_blk = MIN(tcp->tcp_max_sack_blk,
3055 3063 tcp->tcp_num_sack_blk);
3056 3064 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
3057 3065 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
3058 3066 if (max_to_send + sack_opt_len > tcp->tcp_mss)
3059 3067 max_to_send -= sack_opt_len;
3060 3068 }
3061 3069
3062 3070 if (offset != NULL) {
3063 3071 off = *offset;
3064 3072 /* We use offset as an indicator that end_mp is not NULL. */
3065 3073 *end_mp = NULL;
3066 3074 }
3067 3075 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) {
3068 3076 /* This could be faster with cooperation from downstream */
3069 3077 if (mp2 != mp1 && !sendall &&
3070 3078 data_length + (int)(mp->b_wptr - mp->b_rptr) >
3071 3079 max_to_send)
3072 3080 /*
3073 3081 * Don't send the next mblk since the whole mblk
3074 3082 * does not fit.
3075 3083 */
3076 3084 break;
3077 3085 mp2->b_cont = dupb(mp);
3078 3086 mp2 = mp2->b_cont;
3079 3087 if (!mp2) {
3080 3088 freemsg(mp1);
3081 3089 return (NULL);
3082 3090 }
3083 3091 mp2->b_rptr += off;
3084 3092 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
3085 3093 (uintptr_t)INT_MAX);
3086 3094
3087 3095 data_length += (int)(mp2->b_wptr - mp2->b_rptr);
3088 3096 if (data_length > max_to_send) {
3089 3097 mp2->b_wptr -= data_length - max_to_send;
3090 3098 data_length = max_to_send;
3091 3099 off = mp2->b_wptr - mp->b_rptr;
3092 3100 break;
3093 3101 } else {
3094 3102 off = 0;
3095 3103 }
3096 3104 }
3097 3105 if (offset != NULL) {
3098 3106 *offset = off;
3099 3107 *end_mp = mp;
3100 3108 }
3101 3109 if (seg_len != NULL) {
3102 3110 *seg_len = data_length;
3103 3111 }
3104 3112
3105 3113 /* Update the latest receive window size in TCP header. */
3106 3114 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3107 3115
3108 3116 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra;
3109 3117 mp1->b_rptr = rptr;
3110 3118 mp1->b_wptr = rptr + connp->conn_ht_iphc_len + sack_opt_len;
3111 3119 bcopy(connp->conn_ht_iphc, rptr, connp->conn_ht_iphc_len);
3112 3120 tcpha = (tcpha_t *)&rptr[ixa->ixa_ip_hdr_length];
3113 3121 tcpha->tha_seq = htonl(seq);
3114 3122
3115 3123 /*
3116 3124 * Use tcp_unsent to determine if the PUSH bit should be used assumes
3117 3125 * that this function was called from tcp_wput_data. Thus, when called
3118 3126 * to retransmit data the setting of the PUSH bit may appear some
3119 3127 * what random in that it might get set when it should not. This
3120 3128 * should not pose any performance issues.
3121 3129 */
3122 3130 if (data_length != 0 && (tcp->tcp_unsent == 0 ||
3123 3131 tcp->tcp_unsent == data_length)) {
3124 3132 flags = TH_ACK | TH_PUSH;
3125 3133 } else {
3126 3134 flags = TH_ACK;
3127 3135 }
3128 3136
3129 3137 if (tcp->tcp_ecn_ok) {
3130 3138 if (tcp->tcp_ecn_echo_on)
3131 3139 flags |= TH_ECE;
3132 3140
3133 3141 /*
3134 3142 * Only set ECT bit and ECN_CWR if a segment contains new data.
3135 3143 * There is no TCP flow control for non-data segments, and
3136 3144 * only data segment is transmitted reliably.
3137 3145 */
3138 3146 if (data_length > 0 && !rexmit) {
3139 3147 TCP_SET_ECT(tcp, rptr);
3140 3148 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
3141 3149 flags |= TH_CWR;
3142 3150 tcp->tcp_ecn_cwr_sent = B_TRUE;
3143 3151 }
3144 3152 }
3145 3153 }
3146 3154
3147 3155 /* Check if there is any special processing needs to be done. */
3148 3156 if (tcp->tcp_valid_bits) {
3149 3157 uint32_t u1;
3150 3158
3151 3159 /* We don't allow having SYN and FIN in the same segment... */
3152 3160 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) &&
3153 3161 seq == tcp->tcp_iss) {
3154 3162 /* Need to do connection set up processing. */
3155 3163 tcp_xmit_mp_aux_iss(tcp, connp, tcpha, mp1, &flags);
3156 3164 } else if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
3157 3165 (seq + data_length) == tcp->tcp_fss) {
3158 3166 /* Need to do connection tear down processing. */
3159 3167 tcp_xmit_mp_aux_fss(tcp, ixa, &flags);
3160 3168 }
3161 3169
3162 3170 /*
3163 3171 * Need to do urgent pointer processing.
3164 3172 *
3165 3173 * Note the trick here. u1 is unsigned. When tcp_urg
3166 3174 * is smaller than seq, u1 will become a very huge value.
3167 3175 * So the comparison will fail. Also note that tcp_urp
3168 3176 * should be positive, see RFC 793 page 17.
3169 3177 */
3170 3178 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION;
3171 3179 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 &&
3172 3180 u1 < (uint32_t)(64 * 1024)) {
3173 3181 flags |= TH_URG;
3174 3182 TCPS_BUMP_MIB(tcps, tcpOutUrg);
3175 3183 tcpha->tha_urp = htons(u1);
3176 3184 }
3177 3185 }
3178 3186 tcpha->tha_flags = (uchar_t)flags;
3179 3187 tcp->tcp_rack = tcp->tcp_rnxt;
3180 3188 tcp->tcp_rack_cnt = 0;
3181 3189
3182 3190 /* Fill in the current value of timestamps option. */
3183 3191 if (tcp->tcp_snd_ts_ok) {
3184 3192 if (tcp->tcp_state != TCPS_SYN_SENT) {
3185 3193 uint32_t llbolt = (uint32_t)LBOLT_FASTPATH;
3186 3194
3187 3195 U32_TO_BE32(llbolt,
3188 3196 (char *)tcpha + TCP_MIN_HEADER_LENGTH+4);
3189 3197 U32_TO_BE32(tcp->tcp_ts_recent,
3190 3198 (char *)tcpha + TCP_MIN_HEADER_LENGTH+8);
3191 3199 }
3192 3200 }
3193 3201
3194 3202 /* Fill in the SACK blocks. */
3195 3203 if (num_sack_blk > 0) {
3196 3204 uchar_t *wptr = (uchar_t *)tcpha + connp->conn_ht_ulp_len;
3197 3205 sack_blk_t *tmp;
3198 3206 int32_t i;
3199 3207
3200 3208 wptr[0] = TCPOPT_NOP;
3201 3209 wptr[1] = TCPOPT_NOP;
3202 3210 wptr[2] = TCPOPT_SACK;
3203 3211 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
3204 3212 sizeof (sack_blk_t);
3205 3213 wptr += TCPOPT_REAL_SACK_LEN;
3206 3214
3207 3215 tmp = tcp->tcp_sack_list;
3208 3216 for (i = 0; i < num_sack_blk; i++) {
3209 3217 U32_TO_BE32(tmp[i].begin, wptr);
3210 3218 wptr += sizeof (tcp_seq);
3211 3219 U32_TO_BE32(tmp[i].end, wptr);
3212 3220 wptr += sizeof (tcp_seq);
3213 3221 }
3214 3222 tcpha->tha_offset_and_reserved += ((num_sack_blk * 2 + 1) << 4);
3215 3223 }
3216 3224 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX);
3217 3225 data_length += (int)(mp1->b_wptr - rptr);
3218 3226
3219 3227 ixa->ixa_pktlen = data_length;
3220 3228
3221 3229 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3222 3230 ((ipha_t *)rptr)->ipha_length = htons(data_length);
3223 3231 } else {
3224 3232 ip6_t *ip6 = (ip6_t *)rptr;
3225 3233
3226 3234 ip6->ip6_plen = htons(data_length - IPV6_HDR_LEN);
3227 3235 }
3228 3236
3229 3237 /*
3230 3238 * Prime pump for IP
3231 3239 * Include the adjustment for a source route if any.
3232 3240 */
3233 3241 data_length -= ixa->ixa_ip_hdr_length;
3234 3242 data_length += connp->conn_sum;
3235 3243 data_length = (data_length >> 16) + (data_length & 0xFFFF);
3236 3244 tcpha->tha_sum = htons(data_length);
3237 3245 if (tcp->tcp_ip_forward_progress) {
3238 3246 tcp->tcp_ip_forward_progress = B_FALSE;
3239 3247 connp->conn_ixa->ixa_flags |= IXAF_REACH_CONF;
3240 3248 } else {
3241 3249 connp->conn_ixa->ixa_flags &= ~IXAF_REACH_CONF;
3242 3250 }
3243 3251 return (mp1);
3244 3252 }
3245 3253
3246 3254 /*
3247 3255 * If this routine returns B_TRUE, TCP can generate a RST in response
3248 3256 * to a segment. If it returns B_FALSE, TCP should not respond.
3249 3257 */
3250 3258 static boolean_t
3251 3259 tcp_send_rst_chk(tcp_stack_t *tcps)
3252 3260 {
3253 3261 int64_t now;
3254 3262
3255 3263 /*
3256 3264 * TCP needs to protect itself from generating too many RSTs.
3257 3265 * This can be a DoS attack by sending us random segments
3258 3266 * soliciting RSTs.
3259 3267 *
3260 3268 * What we do here is to have a limit of tcp_rst_sent_rate RSTs
3261 3269 * in each 1 second interval. In this way, TCP still generate
3262 3270 * RSTs in normal cases but when under attack, the impact is
3263 3271 * limited.
3264 3272 */
3265 3273 if (tcps->tcps_rst_sent_rate_enabled != 0) {
3266 3274 now = ddi_get_lbolt64();
3267 3275 if (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) >
3268 3276 1*SECONDS) {
3269 3277 tcps->tcps_last_rst_intrvl = now;
3270 3278 tcps->tcps_rst_cnt = 1;
3271 3279 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) {
3272 3280 return (B_FALSE);
3273 3281 }
3274 3282 }
3275 3283 return (B_TRUE);
3276 3284 }
3277 3285
3278 3286 /*
3279 3287 * This function handles all retransmissions if SACK is enabled for this
3280 3288 * connection. First it calculates how many segments can be retransmitted
3281 3289 * based on tcp_pipe. Then it goes thru the notsack list to find eligible
3282 3290 * segments. A segment is eligible if sack_cnt for that segment is greater
3283 3291 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
3284 3292 * all eligible segments, it checks to see if TCP can send some new segments
3285 3293 * (fast recovery). If it can, set the appropriate flag for tcp_input_data().
3286 3294 *
3287 3295 * Parameters:
3288 3296 * tcp_t *tcp: the tcp structure of the connection.
3289 3297 * uint_t *flags: in return, appropriate value will be set for
3290 3298 * tcp_input_data().
3291 3299 */
3292 3300 void
3293 3301 tcp_sack_rexmit(tcp_t *tcp, uint_t *flags)
3294 3302 {
3295 3303 notsack_blk_t *notsack_blk;
3296 3304 int32_t usable_swnd;
3297 3305 int32_t mss;
3298 3306 uint32_t seg_len;
3299 3307 mblk_t *xmit_mp;
3300 3308 tcp_stack_t *tcps = tcp->tcp_tcps;
3301 3309
3302 3310 ASSERT(tcp->tcp_notsack_list != NULL);
3303 3311 ASSERT(tcp->tcp_rexmit == B_FALSE);
3304 3312
3305 3313 /* Defensive coding in case there is a bug... */
3306 3314 if (tcp->tcp_notsack_list == NULL) {
3307 3315 return;
3308 3316 }
3309 3317 notsack_blk = tcp->tcp_notsack_list;
3310 3318 mss = tcp->tcp_mss;
3311 3319
3312 3320 /*
3313 3321 * Limit the num of outstanding data in the network to be
3314 3322 * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
3315 3323 */
3316 3324 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
3317 3325
3318 3326 /* At least retransmit 1 MSS of data. */
3319 3327 if (usable_swnd <= 0) {
3320 3328 usable_swnd = mss;
3321 3329 }
3322 3330
3323 3331 /* Make sure no new RTT samples will be taken. */
3324 3332 tcp->tcp_csuna = tcp->tcp_snxt;
3325 3333
3326 3334 notsack_blk = tcp->tcp_notsack_list;
3327 3335 while (usable_swnd > 0) {
3328 3336 mblk_t *snxt_mp, *tmp_mp;
3329 3337 tcp_seq begin = tcp->tcp_sack_snxt;
3330 3338 tcp_seq end;
3331 3339 int32_t off;
3332 3340
3333 3341 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) {
3334 3342 if (SEQ_GT(notsack_blk->end, begin) &&
3335 3343 (notsack_blk->sack_cnt >=
3336 3344 tcps->tcps_dupack_fast_retransmit)) {
3337 3345 end = notsack_blk->end;
3338 3346 if (SEQ_LT(begin, notsack_blk->begin)) {
3339 3347 begin = notsack_blk->begin;
3340 3348 }
3341 3349 break;
3342 3350 }
3343 3351 }
3344 3352 /*
3345 3353 * All holes are filled. Manipulate tcp_cwnd to send more
3346 3354 * if we can. Note that after the SACK recovery, tcp_cwnd is
3347 3355 * set to tcp_cwnd_ssthresh.
3348 3356 */
3349 3357 if (notsack_blk == NULL) {
3350 3358 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
3351 3359 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) {
3352 3360 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna;
3353 3361 ASSERT(tcp->tcp_cwnd > 0);
3354 3362 return;
3355 3363 } else {
3356 3364 usable_swnd = usable_swnd / mss;
3357 3365 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna +
3358 3366 MAX(usable_swnd * mss, mss);
3359 3367 *flags |= TH_XMIT_NEEDED;
3360 3368 return;
3361 3369 }
3362 3370 }
3363 3371
3364 3372 /*
3365 3373 * Note that we may send more than usable_swnd allows here
3366 3374 * because of round off, but no more than 1 MSS of data.
3367 3375 */
3368 3376 seg_len = end - begin;
3369 3377 if (seg_len > mss)
3370 3378 seg_len = mss;
3371 3379 snxt_mp = tcp_get_seg_mp(tcp, begin, &off);
3372 3380 ASSERT(snxt_mp != NULL);
3373 3381 /* This should not happen. Defensive coding again... */
3374 3382 if (snxt_mp == NULL) {
3375 3383 return;
3376 3384 }
3377 3385
3378 3386 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off,
3379 3387 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE);
3380 3388 if (xmit_mp == NULL)
3381 3389 return;
3382 3390
3383 3391 usable_swnd -= seg_len;
3384 3392 tcp->tcp_pipe += seg_len;
3385 3393 tcp->tcp_sack_snxt = begin + seg_len;
3386 3394
↓ open down ↓ |
953 lines elided |
↑ open up ↑ |
3387 3395 tcp_send_data(tcp, xmit_mp);
3388 3396
3389 3397 /*
3390 3398 * Update the send timestamp to avoid false retransmission.
3391 3399 */
3392 3400 snxt_mp->b_prev = (mblk_t *)(intptr_t)gethrtime();
3393 3401
3394 3402 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
3395 3403 TCPS_UPDATE_MIB(tcps, tcpRetransBytes, seg_len);
3396 3404 TCPS_BUMP_MIB(tcps, tcpOutSackRetransSegs);
3405 + tcp->tcp_cs.tcp_out_retrans_segs++;
3406 + tcp->tcp_cs.tcp_out_retrans_bytes += seg_len;
3397 3407 /*
3398 3408 * Update tcp_rexmit_max to extend this SACK recovery phase.
3399 3409 * This happens when new data sent during fast recovery is
3400 3410 * also lost. If TCP retransmits those new data, it needs
3401 3411 * to extend SACK recover phase to avoid starting another
3402 3412 * fast retransmit/recovery unnecessarily.
3403 3413 */
3404 3414 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) {
3405 3415 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt;
3406 3416 }
3407 3417 }
3408 3418 }
3409 3419
3410 3420 /*
3411 3421 * tcp_ss_rexmit() is called to do slow start retransmission after a timeout
3412 3422 * or ICMP errors.
3413 3423 */
3414 3424 void
3415 3425 tcp_ss_rexmit(tcp_t *tcp)
3416 3426 {
3417 3427 uint32_t snxt;
3418 3428 uint32_t smax;
3419 3429 int32_t win;
3420 3430 int32_t mss;
3421 3431 int32_t off;
3422 3432 mblk_t *snxt_mp;
3423 3433 tcp_stack_t *tcps = tcp->tcp_tcps;
3424 3434
3425 3435 /*
3426 3436 * Note that tcp_rexmit can be set even though TCP has retransmitted
3427 3437 * all unack'ed segments.
3428 3438 */
3429 3439 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) {
3430 3440 smax = tcp->tcp_rexmit_max;
3431 3441 snxt = tcp->tcp_rexmit_nxt;
3432 3442 if (SEQ_LT(snxt, tcp->tcp_suna)) {
3433 3443 snxt = tcp->tcp_suna;
3434 3444 }
3435 3445 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd);
3436 3446 win -= snxt - tcp->tcp_suna;
3437 3447 mss = tcp->tcp_mss;
3438 3448 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off);
3439 3449
3440 3450 while (SEQ_LT(snxt, smax) && (win > 0) && (snxt_mp != NULL)) {
3441 3451 mblk_t *xmit_mp;
3442 3452 mblk_t *old_snxt_mp = snxt_mp;
3443 3453 uint32_t cnt = mss;
3444 3454
3445 3455 if (win < cnt) {
3446 3456 cnt = win;
3447 3457 }
3448 3458 if (SEQ_GT(snxt + cnt, smax)) {
3449 3459 cnt = smax - snxt;
3450 3460 }
3451 3461 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off,
3452 3462 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE);
3453 3463 if (xmit_mp == NULL)
3454 3464 return;
3455 3465
3456 3466 tcp_send_data(tcp, xmit_mp);
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
3457 3467
3458 3468 snxt += cnt;
3459 3469 win -= cnt;
3460 3470 /*
3461 3471 * Update the send timestamp to avoid false
3462 3472 * retransmission.
3463 3473 */
3464 3474 old_snxt_mp->b_prev = (mblk_t *)(intptr_t)gethrtime();
3465 3475 TCPS_BUMP_MIB(tcps, tcpRetransSegs);
3466 3476 TCPS_UPDATE_MIB(tcps, tcpRetransBytes, cnt);
3477 + tcp->tcp_cs.tcp_out_retrans_segs++;
3478 + tcp->tcp_cs.tcp_out_retrans_bytes += cnt;
3467 3479
3468 3480 tcp->tcp_rexmit_nxt = snxt;
3469 3481 }
3470 3482 /*
3471 3483 * If we have transmitted all we have at the time
3472 3484 * we started the retranmission, we can leave
3473 3485 * the rest of the job to tcp_wput_data(). But we
3474 3486 * need to check the send window first. If the
3475 3487 * win is not 0, go on with tcp_wput_data().
3476 3488 */
3477 3489 if (SEQ_LT(snxt, smax) || win == 0) {
3478 3490 return;
3479 3491 }
3480 3492 }
3481 3493 /* Only call tcp_wput_data() if there is data to be sent. */
3482 3494 if (tcp->tcp_unsent) {
3483 3495 tcp_wput_data(tcp, NULL, B_FALSE);
3484 3496 }
3485 3497 }
3486 3498
3487 3499 /*
3488 3500 * Do slow start retransmission after ICMP errors of PMTU changes.
3489 3501 */
3490 3502 void
3491 3503 tcp_rexmit_after_error(tcp_t *tcp)
3492 3504 {
3493 3505 /*
3494 3506 * All sent data has been acknowledged or no data left to send, just
3495 3507 * to return.
3496 3508 */
3497 3509 if (!SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) ||
3498 3510 (tcp->tcp_xmit_head == NULL))
3499 3511 return;
3500 3512
3501 3513 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && (tcp->tcp_unsent == 0))
3502 3514 tcp->tcp_rexmit_max = tcp->tcp_fss;
3503 3515 else
3504 3516 tcp->tcp_rexmit_max = tcp->tcp_snxt;
3505 3517
3506 3518 tcp->tcp_rexmit_nxt = tcp->tcp_suna;
3507 3519 tcp->tcp_rexmit = B_TRUE;
3508 3520 tcp->tcp_dupack_cnt = 0;
3509 3521 tcp_ss_rexmit(tcp);
3510 3522 }
3511 3523
3512 3524 /*
3513 3525 * tcp_get_seg_mp() is called to get the pointer to a segment in the
3514 3526 * send queue which starts at the given sequence number. If the given
3515 3527 * sequence number is equal to last valid sequence number (tcp_snxt), the
3516 3528 * returned mblk is the last valid mblk, and off is set to the length of
3517 3529 * that mblk.
3518 3530 *
3519 3531 * send queue which starts at the given seq. no.
3520 3532 *
3521 3533 * Parameters:
3522 3534 * tcp_t *tcp: the tcp instance pointer.
3523 3535 * uint32_t seq: the starting seq. no of the requested segment.
3524 3536 * int32_t *off: after the execution, *off will be the offset to
3525 3537 * the returned mblk which points to the requested seq no.
3526 3538 * It is the caller's responsibility to send in a non-null off.
3527 3539 *
3528 3540 * Return:
3529 3541 * A mblk_t pointer pointing to the requested segment in send queue.
3530 3542 */
3531 3543 static mblk_t *
3532 3544 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off)
3533 3545 {
3534 3546 int32_t cnt;
3535 3547 mblk_t *mp;
3536 3548
3537 3549 /* Defensive coding. Make sure we don't send incorrect data. */
3538 3550 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GT(seq, tcp->tcp_snxt))
3539 3551 return (NULL);
3540 3552
3541 3553 cnt = seq - tcp->tcp_suna;
3542 3554 mp = tcp->tcp_xmit_head;
3543 3555 while (cnt > 0 && mp != NULL) {
3544 3556 cnt -= mp->b_wptr - mp->b_rptr;
3545 3557 if (cnt <= 0) {
3546 3558 cnt += mp->b_wptr - mp->b_rptr;
3547 3559 break;
3548 3560 }
3549 3561 mp = mp->b_cont;
3550 3562 }
3551 3563 ASSERT(mp != NULL);
3552 3564 *off = cnt;
3553 3565 return (mp);
3554 3566 }
3555 3567
3556 3568 /*
3557 3569 * This routine adjusts next-to-send sequence number variables, in the
3558 3570 * case where the reciever has shrunk it's window.
3559 3571 */
3560 3572 void
3561 3573 tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt)
3562 3574 {
3563 3575 mblk_t *xmit_tail;
3564 3576 int32_t offset;
3565 3577
3566 3578 tcp->tcp_snxt = snxt;
3567 3579
3568 3580 /* Get the mblk, and the offset in it, as per the shrunk window */
3569 3581 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset);
3570 3582 ASSERT(xmit_tail != NULL);
3571 3583 tcp->tcp_xmit_tail = xmit_tail;
3572 3584 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr -
3573 3585 xmit_tail->b_rptr - offset;
3574 3586 }
3575 3587
3576 3588 /*
3577 3589 * This handles the case when the receiver has shrunk its win. Per RFC 1122
3578 3590 * if the receiver shrinks the window, i.e. moves the right window to the
3579 3591 * left, the we should not send new data, but should retransmit normally the
3580 3592 * old unacked data between suna and suna + swnd. We might has sent data
3581 3593 * that is now outside the new window, pretend that we didn't send it.
3582 3594 */
3583 3595 static void
3584 3596 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count)
3585 3597 {
3586 3598 uint32_t snxt = tcp->tcp_snxt;
3587 3599
3588 3600 ASSERT(shrunk_count > 0);
3589 3601
3590 3602 if (!tcp->tcp_is_wnd_shrnk) {
3591 3603 tcp->tcp_snxt_shrunk = snxt;
3592 3604 tcp->tcp_is_wnd_shrnk = B_TRUE;
3593 3605 } else if (SEQ_GT(snxt, tcp->tcp_snxt_shrunk)) {
3594 3606 tcp->tcp_snxt_shrunk = snxt;
3595 3607 }
3596 3608
3597 3609 /* Pretend we didn't send the data outside the window */
3598 3610 snxt -= shrunk_count;
3599 3611
3600 3612 /* Reset all the values per the now shrunk window */
3601 3613 tcp_update_xmit_tail(tcp, snxt);
3602 3614 tcp->tcp_unsent += shrunk_count;
3603 3615
3604 3616 /*
3605 3617 * If the SACK option is set, delete the entire list of
3606 3618 * notsack'ed blocks.
3607 3619 */
3608 3620 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
3609 3621
3610 3622 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0)
3611 3623 /*
3612 3624 * Make sure the timer is running so that we will probe a zero
3613 3625 * window.
3614 3626 */
3615 3627 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
3616 3628 }
3617 3629
3618 3630 /*
3619 3631 * tcp_fill_header is called by tcp_send() to fill the outgoing TCP header
3620 3632 * with the template header, as well as other options such as time-stamp,
3621 3633 * ECN and/or SACK.
3622 3634 */
3623 3635 static void
3624 3636 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, int num_sack_blk)
3625 3637 {
3626 3638 tcpha_t *tcp_tmpl, *tcpha;
3627 3639 uint32_t *dst, *src;
3628 3640 int hdrlen;
3629 3641 conn_t *connp = tcp->tcp_connp;
3630 3642
3631 3643 ASSERT(OK_32PTR(rptr));
3632 3644
3633 3645 /* Template header */
3634 3646 tcp_tmpl = tcp->tcp_tcpha;
3635 3647
3636 3648 /* Header of outgoing packet */
3637 3649 tcpha = (tcpha_t *)(rptr + connp->conn_ixa->ixa_ip_hdr_length);
3638 3650
3639 3651 /* dst and src are opaque 32-bit fields, used for copying */
3640 3652 dst = (uint32_t *)rptr;
3641 3653 src = (uint32_t *)connp->conn_ht_iphc;
3642 3654 hdrlen = connp->conn_ht_iphc_len;
3643 3655
3644 3656 /* Fill time-stamp option if needed */
3645 3657 if (tcp->tcp_snd_ts_ok) {
3646 3658 U32_TO_BE32(LBOLT_FASTPATH,
3647 3659 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4);
3648 3660 U32_TO_BE32(tcp->tcp_ts_recent,
3649 3661 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8);
3650 3662 } else {
3651 3663 ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
3652 3664 }
3653 3665
3654 3666 /*
3655 3667 * Copy the template header; is this really more efficient than
3656 3668 * calling bcopy()? For simple IPv4/TCP, it may be the case,
3657 3669 * but perhaps not for other scenarios.
3658 3670 */
3659 3671 dst[0] = src[0];
3660 3672 dst[1] = src[1];
3661 3673 dst[2] = src[2];
3662 3674 dst[3] = src[3];
3663 3675 dst[4] = src[4];
3664 3676 dst[5] = src[5];
3665 3677 dst[6] = src[6];
3666 3678 dst[7] = src[7];
3667 3679 dst[8] = src[8];
3668 3680 dst[9] = src[9];
3669 3681 if (hdrlen -= 40) {
3670 3682 hdrlen >>= 2;
3671 3683 dst += 10;
3672 3684 src += 10;
3673 3685 do {
3674 3686 *dst++ = *src++;
3675 3687 } while (--hdrlen);
3676 3688 }
3677 3689
3678 3690 /*
3679 3691 * Set the ECN info in the TCP header if it is not a zero
3680 3692 * window probe. Zero window probe is only sent in
3681 3693 * tcp_wput_data() and tcp_timer().
3682 3694 */
3683 3695 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) {
3684 3696 TCP_SET_ECT(tcp, rptr);
3685 3697
3686 3698 if (tcp->tcp_ecn_echo_on)
3687 3699 tcpha->tha_flags |= TH_ECE;
3688 3700 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
3689 3701 tcpha->tha_flags |= TH_CWR;
3690 3702 tcp->tcp_ecn_cwr_sent = B_TRUE;
3691 3703 }
3692 3704 }
3693 3705
3694 3706 /* Fill in SACK options */
3695 3707 if (num_sack_blk > 0) {
3696 3708 uchar_t *wptr = rptr + connp->conn_ht_iphc_len;
3697 3709 sack_blk_t *tmp;
3698 3710 int32_t i;
3699 3711
3700 3712 wptr[0] = TCPOPT_NOP;
3701 3713 wptr[1] = TCPOPT_NOP;
3702 3714 wptr[2] = TCPOPT_SACK;
3703 3715 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
3704 3716 sizeof (sack_blk_t);
3705 3717 wptr += TCPOPT_REAL_SACK_LEN;
3706 3718
3707 3719 tmp = tcp->tcp_sack_list;
3708 3720 for (i = 0; i < num_sack_blk; i++) {
3709 3721 U32_TO_BE32(tmp[i].begin, wptr);
3710 3722 wptr += sizeof (tcp_seq);
3711 3723 U32_TO_BE32(tmp[i].end, wptr);
3712 3724 wptr += sizeof (tcp_seq);
3713 3725 }
3714 3726 tcpha->tha_offset_and_reserved +=
3715 3727 ((num_sack_blk * 2 + 1) << 4);
3716 3728 }
3717 3729 }
↓ open down ↓ |
241 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX