-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathofp_tcp_output.c
1560 lines (1450 loc) · 46.2 KB
/
ofp_tcp_output.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
* The Regents of the University of California.
* Copyright (c) 2015 Nokia Solutions and Networks
* Copyright (c) 2015 Enea Software AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ofp_tcp_output.c 8.4 (Berkeley) 5/24/95
*/
#include <string.h>
#include <stddef.h>
#include "ofpi_errno.h"
#include "ofpi_protosw.h"
#include "ofpi_sysctl.h"
#include "ofpi_socketvar.h"
#include "ofpi_in.h"
#include "ofpi_ip.h"
#ifdef INET6
#include "ofpi_ip6.h"
#include "ofpi_ip6_var.h"
#endif /* INET6 */
#define OFP_TCPOUTFLAGS
#include "ofpi_util.h"
#include "ofpi_pkt_processing.h"
#include "ofpi_systm.h"
#include "ofpi_timer.h"
#include "ofpi_tcp.h"
#include "ofpi_tcp_fsm.h"
#include "ofpi_tcp_seq.h"
#include "ofpi_tcp_timer.h"
#include "ofpi_tcp_var.h"
//#include "ofp_tcpip.h"
#ifdef TCPDEBUG
#include <netinet/tcp_debug.h>
#endif
//#include <machine/in_cksum.h>
extern int ofp_max_linkhdr;
VNET_DEFINE(int, ofp_path_mtu_discovery) = 1;
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, path_mtu_discovery, OFP_CTLFLAG_RW,
&ofp_path_mtu_discovery, 1,
"Enable Path MTU Discovery");
VNET_DEFINE(int, ofp_ss_fltsz) = 1;
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, slowstart_flightsize, OFP_CTLFLAG_RW,
&ofp_ss_fltsz, 1,
"Slow start flight size");
VNET_DEFINE(int, ofp_ss_fltsz_local) = 4;
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, local_slowstart_flightsize,
OFP_CTLFLAG_RW, &ofp_ss_fltsz_local, 1,
"Slow start flight size for local networks");
VNET_DEFINE(int, ofp_tcp_do_tso) = 1;
#define V_tcp_do_tso VNET(ofp_tcp_do_tso)
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, tso, OFP_CTLFLAG_RW,
&ofp_tcp_do_tso, 0,
"Enable TCP Segmentation Offload");
VNET_DEFINE(int, ofp_tcp_do_autosndbuf) = 1;
#define V_tcp_do_autosndbuf VNET(ofp_tcp_do_autosndbuf)
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, sendbuf_auto, OFP_CTLFLAG_RW,
&ofp_tcp_do_autosndbuf, 0,
"Enable automatic send buffer sizing");
VNET_DEFINE(int, ofp_tcp_autosndbuf_inc) = 8*1024;
#define V_tcp_autosndbuf_inc VNET(ofp_tcp_autosndbuf_inc)
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, sendbuf_inc, OFP_CTLFLAG_RW,
&ofp_tcp_autosndbuf_inc, 0,
"Incrementor step size of automatic send buffer");
VNET_DEFINE(int, ofp_tcp_autosndbuf_max) = 2*1024*1024;
#define V_tcp_autosndbuf_max VNET(ofp_tcp_autosndbuf_max)
OFP_SYSCTL_INT(_net_inet_tcp, OFP_OID_AUTO, sendbuf_max, OFP_CTLFLAG_RW,
&ofp_tcp_autosndbuf_max, 0,
"Max size of automatic send buffer");
/*
static inline void hhook_run_tcp_est_out(struct tcpcb *tp,
struct ofp_tcphdr *th, struct tcpopt *to,
long len, int tso);
*/
static inline void cc_after_idle(struct tcpcb *tp);
/*
* Wrapper for the TCP established ouput helper hook.
*/
#if 0
static void inline
hhook_run_tcp_est_out(struct tcpcb *tp, struct ofp_tcphdr *th,
struct tcpopt *to, long len, int tso)
{
struct tcp_hhook_data hhook_data;
if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) {
hhook_data.tp = tp;
hhook_data.th = th;
hhook_data.to = to;
hhook_data.len = len;
hhook_data.tso = tso;
hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data,
tp->osd);
}
}
#endif
/*
* CC wrapper hook functions
*/
static inline void
cc_after_idle(struct tcpcb *tp)
{
(void)tp;
#if 0
INP_WLOCK_ASSERT(tp->t_inpcb);
if (CC_ALGO(tp)->after_idle != NULL)
CC_ALGO(tp)->after_idle(tp->ccv);
#endif
}
/*
* Tcp output routine: figure out what should be sent and send it.
*/
int
ofp_tcp_output(struct tcpcb *tp)
{
struct socket *so = tp->t_inpcb->inp_socket;
long len, recwin, sendwin;
int off, flags, error = 0; /* Keep compiler happy */
odp_packet_t m;
struct ofp_ip *ip = NULL;
struct ipovly *ipov = NULL;
struct ofp_tcphdr *th;
uint8_t opt[OFP_TCP_MAXOLEN];
unsigned ipoptlen, optlen, hdrlen;
int idle, sendalot;
int sack_rxmit, sack_bytes_rxmt;
struct sackhole *p;
int tso;
struct tcpopt to;
(void)ipov;
#if 0
int maxburst = OFP_TCP_MAXBURST;
#endif
#ifdef INET6
struct ofp_ip6_hdr *ip6 = NULL;
int isipv6;
isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
#endif
INP_WLOCK_ASSERT(tp->t_inpcb);
SOCKBUF_LOCK(&so->so_snd);
/*
* Determine length of data that should be transmitted,
* and flags that will be used.
* If there is some data or critical controls (SYN, RST)
* to send, then transmit; otherwise, investigate further.
*/
idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
if (idle && (int)(ofp_timer_ticks(0) - tp->t_rcvtime) >= tp->t_rxtcur)
cc_after_idle(tp);
t_flags_and(tp->t_flags, ~TF_LASTIDLE);
if (idle) {/* OK */
if (tp->t_flags & TF_MORETOCOME) {
t_flags_or(tp->t_flags, TF_LASTIDLE);
idle = 0;
}
}
again:
/*
* If we've recently taken a timeout, snd_max will be greater than
* snd_nxt. There may be SACK information that allows us to avoid
* resending already delivered data. Adjust snd_nxt accordingly.
*/
if ((tp->t_flags & TF_SACK_PERMIT) &&
SEQ_LT(tp->snd_nxt, tp->snd_max))
ofp_tcp_sack_adjust(tp);
sendalot = 0;
tso = 0;
off = tp->snd_nxt - tp->snd_una;
#ifndef min
#define min(a, b) ((int64_t)a > (int64_t)b ? (int64_t)b : (int64_t)a)
#endif
sendwin = min(tp->snd_wnd, tp->snd_cwnd);
flags = tcp_outflags[tp->t_state];
/*
* Send any SACK-generated retransmissions. If we're explicitly trying
* to send out new data (when sendalot is 1), bypass this function.
* If we retransmit in fast recovery mode, decrement snd_cwnd, since
* we're replacing a (future) new transmission with a retransmission
* now, and we previously incremented snd_cwnd in ofp_tcp_input().
*/
/*
* Still in sack recovery , reset rxmit flag to zero.
*/
sack_rxmit = 0;
sack_bytes_rxmt = 0;
len = 0;
p = NULL;
if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) &&
(p = ofp_tcp_sack_output(tp, &sack_bytes_rxmt))) {
long cwin;
cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
if (cwin < 0)
cwin = 0;
/* Do not retransmit SACK segments beyond snd_recover */
if (SEQ_GT(p->end, tp->snd_recover)) {
/*
* (At least) part of sack hole extends beyond
* snd_recover. Check to see if we can rexmit data
* for this hole.
*/
if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
/*
* Can't rexmit any more data for this hole.
* That data will be rexmitted in the next
* sack recovery episode, when snd_recover
* moves past p->rxmit.
*/
p = NULL;
goto after_sack_rexmit;
} else {
/* Can rexmit part of the current hole */
len = ((long)ulmin(cwin,
tp->snd_recover - p->rxmit));
}
} else {
len = ((long)ulmin(cwin, p->end - p->rxmit));
}
off = p->rxmit - tp->snd_una;
KASSERT(off >= 0,("%s: sack block to the left of una : %d",
__func__, off));
if (len > 0) {
sack_rxmit = 1;
sendalot = 1;
TCPSTAT_INC(tcps_sack_rexmits);
TCPSTAT_ADD(tcps_sack_rexmit_bytes,
min(len, tp->t_maxseg));
}
}
after_sack_rexmit:
/*
* Get standard flags, and add SYN or FIN if requested by 'hidden'
* state flags.
*/
if (tp->t_flags & TF_NEEDFIN)
flags |= OFP_TH_FIN;
if (tp->t_flags & TF_NEEDSYN)
flags |= OFP_TH_SYN;
/*
* If in persist timeout with window of 0, send 1 byte.
* Otherwise, if window is small but nonzero
* and timer expired, we will send what we can
* and go to transmit state.
*/
if (tp->t_flags & TF_FORCEDATA) {
if (sendwin == 0) {
/*
* If we still have some data to send, then
* clear the FIN bit. Usually this would
* happen below when it realizes that we
* aren't sending all the data. However,
* if we have exactly 1 byte of unsent data,
* then it won't clear the FIN bit below,
* and if we are in persist state, we wind
* up sending the packet without recording
* that we sent the FIN bit.
*
* We can't just blindly clear the FIN bit,
* because if we don't have any more data
* to send then the probe will be the FIN
* itself.
*/
if (off < (int)so->so_snd.sb_cc)
flags &= ~OFP_TH_FIN;
sendwin = 1;
} else {
ofp_tcp_timer_activate(tp, TT_PERSIST, 0);
tp->t_rxtshift = 0;
}
}
/*
* If snd_nxt == snd_max and we have transmitted a FIN, the
* offset will be > 0 even if so_snd.sb_cc is 0, resulting in
* a negative length. This can also occur when TCP opens up
* its congestion window while receiving additional duplicate
* acks after fast-retransmit because TCP will reset snd_nxt
* to snd_max after the fast-retransmit.
*
* In the normal retransmit-FIN-only case, however, snd_nxt will
* be set to snd_una, the offset will be 0, and the length may
* wind up 0.
*
* If sack_rxmit is true we are retransmitting from the scoreboard
* in which case len is already set.
*/
if (sack_rxmit == 0) {/* OK */
if (sack_bytes_rxmt == 0) {/* OK */
len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
} else {
long cwin;
/*
* We are inside of a SACK recovery episode and are
* sending new data, having retransmitted all the
* data possible in the scoreboard.
*/
len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
- off);
/*
* Don't remove this (len > 0) check !
* We explicitly check for len > 0 here (although it
* isn't really necessary), to work around a gcc
* optimization issue - to force gcc to compute
* len above. Without this check, the computation
* of len is bungled by the optimizer.
*/
if (len > 0) {
cwin = tp->snd_cwnd -
(tp->snd_nxt - tp->sack_newdata) -
sack_bytes_rxmt;
if (cwin < 0)
cwin = 0;
len = lmin(len, cwin);
}
}
}
/*
* Lop off SYN bit if it has already been sent. However, if this
* is SYN-SENT state and if segment contains data and if we don't
* know that foreign host supports TAO, suppress sending segment.
*/
if ((flags & OFP_TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
if (tp->t_state != TCPS_SYN_RECEIVED)
flags &= ~OFP_TH_SYN;
off--, len++;
}
/*
* Be careful not to send data and/or FIN on SYN segments.
* This measure is needed to prevent interoperability problems
* with not fully conformant TCP implementations.
*/
if ((flags & OFP_TH_SYN) && (tp->t_flags & TF_NOOPT)) {
len = 0;
flags &= ~OFP_TH_FIN;
}
if (len < 0) {
/*
* If FIN has been sent but not acked,
* but we haven't been called to retransmit,
* len will be < 0. Otherwise, window shrank
* after we sent into it. If window shrank to 0,
* cancel pending retransmit, pull snd_nxt back
* to (closed) window, and set the persist timer
* if it isn't already going. If the window didn't
* close completely, just wait for an ACK.
*/
len = 0;
if (sendwin == 0) {
ofp_tcp_timer_activate(tp, TT_REXMT, 0);
tp->t_rxtshift = 0;
tp->snd_nxt = tp->snd_una;
if (!ofp_tcp_timer_active(tp, TT_PERSIST))
ofp_tcp_setpersist(tp);
}
}
/* len will be >= 0 after this point. */
KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
/*
* Automatic sizing of send socket buffer. Often the send buffer
* size is not optimally adjusted to the actual network conditions
* at hand (delay bandwidth product). Setting the buffer size too
* small limits throughput on links with high bandwidth and high
* delay (eg. trans-continental/oceanic links). Setting the
* buffer size too big consumes too much real kernel memory,
* especially with many connections on busy servers.
*
* The criteria to step up the send buffer one notch are:
* 1. receive window of remote host is larger than send buffer
* (with a fudge factor of 5/4th);
* 2. send buffer is filled to 7/8th with data (so we actually
* have data to make use of it);
* 3. send buffer fill has not hit maximal automatic size;
* 4. our send window (slow start and cogestion controlled) is
* larger than sent but unacknowledged data in send buffer.
*
* The remote host receive window scaling factor may limit the
* growing of the send buffer before it reaches its allowed
* maximum.
*
* It scales directly with slow start or congestion window
* and does at most one step per received ACK. This fast
* scaling has the drawback of growing the send buffer beyond
* what is strictly necessary to make full use of a given
* delay*bandwith product. However testing has shown this not
* to be much of an problem. At worst we are trading wasting
* of available bandwith (the non-use of it) for wasting some
* socket buffer memory.
*
* TODO: Shrink send buffer during idle periods together
* with congestion window. Requires another timer. Has to
* wait for upcoming tcp timer rewrite.
*/
#if 0 /* HJo: FIX */
if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
so->so_snd.sb_cc < V_tcp_autosndbuf_max &&
sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
if (!ofp_sbreserve_locked(&so->so_snd,
min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc,
V_tcp_autosndbuf_max), so, curthread))
so->so_snd.sb_flags &= ~SB_AUTOSIZE;
}
}
#endif
/*
* Decide if we can use TCP Segmentation Offloading (if supported by
* hardware).
*
* TSO may only be used if we are in a pure bulk sending state. The
* presence of TCP-MD5, SACK retransmits, SACK advertizements and
* IP options prevent using TSO. With TSO the TCP header is the same
* (except for the sequence number) for all generated packets. This
* makes it impossible to transmit any options which vary per generated
* segment or packet.
*/
if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg &&
((tp->t_flags & TF_SIGNATURE) == 0) &&
tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
tp->t_inpcb->inp_options == ODP_PACKET_INVALID &&
tp->t_inpcb->in6p_options == ODP_PACKET_INVALID)
tso = 1;
if (sack_rxmit) {
if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
flags &= ~OFP_TH_FIN;
} else {/* OK */
if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
flags &= ~OFP_TH_FIN;
}
recwin = sbspace(&so->so_rcv);
/*
* Sender silly window avoidance. We transmit under the following
* conditions when len is non-zero:
*
* - We have a full segment (or more with TSO)
* - This is the last buffer in a write()/send() and we are
* either idle or running NODELAY
* - we've timed out (e.g. persist timer)
* - we have more then 1/2 the maximum send window's worth of
* data (receiver may be limited the window size)
* - we need to retransmit
*/
if (len) {/* OK */
if (len >= tp->t_maxseg)
goto send;
/*
* NOTE! on localhost connections an 'ack' from the remote
* end may occur synchronously with the output and cause
* us to flush a buffer queued with moretocome. XXX
*
* note: the len + off check is almost certainly unnecessary.
*/
if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */
(idle || (tp->t_flags & TF_NODELAY)) &&
len + off >= so->so_snd.sb_cc &&
(tp->t_flags & TF_NOPUSH) == 0) {
goto send;
}
if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */
goto send;
if (len >= (int)(tp->max_sndwnd / 2) && tp->max_sndwnd > 0)
goto send;
if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */
goto send;
if (sack_rxmit)
goto send;
}
/* Don't send TCP window updates right after first received data */
if (!(tp->t_flags & TF_WND_UPDATES) &&
tp->rcv_nxt != tp->irs + 1) {
tp->t_flags |= TF_WND_UPDATES;
}
/*
* Sending of standalone window updates.
*
* Window updates are important when we close our window due to a
* full socket buffer and are opening it again after the application
* reads data from it. Once the window has opened again and the
* remote end starts to send again the ACK clock takes over and
* provides the most current window information.
*
* We must avoid the silly window syndrome whereas every read
* from the receive buffer, no matter how small, causes a window
* update to be sent. We also should avoid sending a flurry of
* window updates when the socket buffer had queued a lot of data
* and the application is doing small reads.
*
* Prevent a flurry of pointless window updates by only sending
* an update when we can increase the advertized window by more
* than 1/4th of the socket buffer capacity. When the buffer is
* getting full or is very small be more aggressive and send an
* update whenever we can increase by two mss sized segments.
* In all other situations the ACK's to new incoming data will
* carry further window increases.
*
* Don't send an independent window update if a delayed
* ACK is pending (it will get piggy-backed on it) or the
* remote side already has done a half-close and won't send
* more data. Skip this if the connection is in T/TCP
* half-open state.
*
* Don't send TCP window updates right after first received data
*/
if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
!(tp->t_flags & TF_DELACK) &&
(tp->t_flags & TF_WND_UPDATES) &&
!TCPS_HAVERCVDFIN(tp->t_state)) {
/*
* "adv" is the amount we could increase the window,
* taking into account that we are limited by
* OFP_TCP_MAXWIN << tp->rcv_scale.
*/
long adv;
int oldwin;
adv = min(recwin, (long)OFP_TCP_MAXWIN << tp->rcv_scale);
if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
oldwin = (tp->rcv_adv - tp->rcv_nxt);
adv -= oldwin;
} else
oldwin = 0;
/*
* If the new window size ends up being the same as or less
* than the old size when it is scaled, then don't force
* a window update.
*/
if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
goto dontupdate;
if (adv >= (long)(2 * tp->t_maxseg) &&
(adv >= (long)(so->so_rcv.sb_hiwat / 4) ||
recwin <= (so->so_rcv.sb_hiwat / 8) ||
so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg))
goto send;
if (2 * adv >= (long) so->so_rcv.sb_hiwat)
goto send;
}
dontupdate:
/*
* Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
* is also a catch-all for the retransmit timer timeout case.
*/
if (tp->t_flags & TF_ACKNOW)
goto send;
if ((flags & OFP_TH_RST) ||
((flags & OFP_TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
goto send;
if (SEQ_GT(tp->snd_up, tp->snd_una))
goto send;
/*
* If our state indicates that FIN should be sent
* and we have not yet done so, then we need to send.
*/
if ((flags & OFP_TH_FIN) &&
((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
goto send;
/*
* In SACK, it is possible for ofp_tcp_output to fail to send a segment
* after the retransmission timer has been turned off. Make sure
* that the retransmission timer is set.
*/
if ((tp->t_flags & TF_SACK_PERMIT) &&
SEQ_GT(tp->snd_max, tp->snd_una) &&
!ofp_tcp_timer_active(tp, TT_REXMT) &&
!ofp_tcp_timer_active(tp, TT_PERSIST)) {
ofp_tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
goto just_return;
}
/*
* TCP window updates are not reliable, rather a polling protocol
* using ``persist'' packets is used to insure receipt of window
* updates. The three ``states'' for the output side are:
* idle not doing retransmits or persists
* persisting to move a small or zero window
* (re)transmitting and thereby not persisting
*
* ofp_tcp_timer_active(tp, TT_PERSIST)
* is true when we are in persist state.
* (tp->t_flags & TF_FORCEDATA)
* is set when we are called to send a persist packet.
* ofp_tcp_timer_active(tp, TT_REXMT)
* is set when we are retransmitting
* The output side is idle when both timers are zero.
*
* If send window is too small, there is data to transmit, and no
* retransmit or persist is pending, then go to persist state.
* If nothing happens soon, send when timer expires:
* if window is nonzero, transmit what we can,
* otherwise force out a byte.
*/
if (so->so_snd.sb_cc && !ofp_tcp_timer_active(tp, TT_REXMT) &&
!ofp_tcp_timer_active(tp, TT_PERSIST)) {
tp->t_rxtshift = 0;
ofp_tcp_setpersist(tp);
}
/*
* No reason to send a segment, just return.
*/
just_return:
SOCKBUF_UNLOCK(&so->so_snd);
return (0);
send:
SOCKBUF_LOCK_ASSERT(&so->so_snd);
/*
* Before ESTABLISHED, force sending of initial options
* unless TCP set not to do any options.
* NOTE: we assume that the IP/TCP header plus TCP options
* always fit in a single mbuf, leaving room for a maximum
* link header, i.e.
* ofp_max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
*/
optlen = 0;
#ifdef INET6
if (isipv6)
hdrlen = sizeof (struct ofp_ip6_hdr) + sizeof (struct ofp_tcphdr);
else
#endif
hdrlen = sizeof (struct tcpiphdr);
/*
* Compute options for segment.
* We only have to care about SYN and established connection
* segments. Options for SYN-ACK segments are handled in TCP
* syncache.
*/
if ((tp->t_flags & TF_NOOPT) == 0) {/* OK */
to.to_flags = 0;
/* Maximum segment size. */
if (flags & OFP_TH_SYN) {
tp->snd_nxt = tp->iss;
to.to_mss = ofp_tcp_mssopt(&tp->t_inpcb->inp_inc);
to.to_flags |= TOF_MSS;
}
/* Window scaling. */
if ((flags & OFP_TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
to.to_wscale = tp->request_r_scale;
to.to_flags |= TOF_SCALE;
}
/* Timestamps. */
if ((tp->t_flags & TF_RCVD_TSTMP) ||
((flags & OFP_TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {/* OK */
to.to_tsval = tcp_ts_getticks() + tp->ts_offset;
to.to_tsecr = tp->ts_recent;
to.to_flags |= TOF_TS;
/* Set receive buffer autosizing timestamp. */
if (tp->rfbuf_ts == 0 &&
(so->so_rcv.sb_flags & SB_AUTOSIZE))
tp->rfbuf_ts = tcp_ts_getticks();
}
/* Selective ACK's. */
if (tp->t_flags & TF_SACK_PERMIT) {/* OK */
if (flags & OFP_TH_SYN)
to.to_flags |= TOF_SACKPERM;
else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
(tp->t_flags & TF_SACK_PERMIT) &&
tp->rcv_numsacks > 0) {
to.to_flags |= TOF_SACK;
to.to_nsacks = tp->rcv_numsacks;
to.to_sacks = (uint8_t *)tp->sackblks;
}
}
#ifdef TCP_SIGNATURE
/* TCP-MD5 (RFC2385). */
if (tp->t_flags & TF_SIGNATURE)
to.to_flags |= TOF_SIGNATURE;
#endif /* TCP_SIGNATURE */
/* Processing the options. */
hdrlen += optlen = ofp_tcp_addoptions(&to, opt);
}
#ifdef INET6
if (isipv6) /*Bogdan: no options*/
ipoptlen = 0;/*ip6_optlen(tp->t_inpcb); */
else
#endif
if (tp->t_inpcb->inp_options != ODP_PACKET_INVALID)
ipoptlen = odp_packet_len(tp->t_inpcb->inp_options) -
offsetof(struct ofp_ipoption, ipopt_list);
else
ipoptlen = 0;
#ifdef IPSEC
ipoptlen += ipsec_optlen;
#endif
/*
* Adjust data length if insertion of options will
* bump the packet length beyond the t_maxopd length.
* Clear the FIN bit because we cut off the tail of
* the segment.
*/
if (len + optlen + ipoptlen > tp->t_maxopd) {/* OK */
flags &= ~OFP_TH_FIN;
if (tso) {
KASSERT(ipoptlen == 0,
("%s: TSO can't do IP options", __func__));
/*
* Limit a burst to OFP_IP_MAXPACKET minus IP,
* TCP and options length to keep ip->ip_len
* from overflowing.
*/
if (len > OFP_IP_MAXPACKET - hdrlen) {
len = OFP_IP_MAXPACKET - hdrlen;
sendalot = 1;
}
/*
* Prevent the last segment from being
* fractional unless the send sockbuf can
* be emptied.
*/
if (sendalot && off + len < so->so_snd.sb_cc) {
len -= len % (tp->t_maxopd - optlen);
sendalot = 1;
}
/*
* Send the FIN in a separate segment
* after the bulk sending is done.
* We don't trust the TSO implementations
* to clear the FIN flag on all but the
* last segment.
*/
if (tp->t_flags & TF_NEEDFIN)
sendalot = 1;
} else {/* OK */
len = tp->t_maxopd - optlen - ipoptlen;
sendalot = 1;
}
} else
tso = 0;
KASSERT(len + hdrlen + ipoptlen <= OFP_IP_MAXPACKET,
("%s: len > OFP_IP_MAXPACKET", __func__));
/*#ifdef DIAGNOSTIC*/
#if 0
#ifdef INET6
if (ofp_max_linkhdr + hdrlen > MCLBYTES)
#else
if (ofp_max_linkhdr + hdrlen > MHLEN)
#endif
panic("tcphdr too big");
#endif
/*#endif*/
/*
* This KASSERT is here to catch edge cases at a well defined place.
* Before, those had triggered (random) panic conditions further down.
*/
KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
/*
* Grab a header mbuf, attaching a copy of data to
* be transmitted, and initialize the header from
* the template for sends on this connection.
*/
if (len) {/* OK */
if ((tp->t_flags & TF_FORCEDATA) && len == 1)
TCPSTAT_INC(tcps_sndprobe);
else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
tp->t_sndrexmitpack++;
TCPSTAT_INC(tcps_sndrexmitpack);
TCPSTAT_ADD(tcps_sndrexmitbyte, len);
} else {/* OK */
TCPSTAT_INC(tcps_sndpack);
TCPSTAT_ADD(tcps_sndbyte, len);
}
m = ofp_socket_packet_alloc(hdrlen + len);
if (m == ODP_PACKET_INVALID) {
SOCKBUF_UNLOCK(&so->so_snd);
error = OFP_ENOBUFS;
goto out;
}
#if 0
if (MHLEN < hdrlen + ofp_max_linkhdr) {
MCLGET(m, M_DONTWAIT);
if ((odp_packet_flags(m) & M_EXT) == 0) {
SOCKBUF_UNLOCK(&so->so_snd);
odp_packet_free(m);
error = OFP_ENOBUFS;
goto out;
}
}
#endif
odp_packet_l3_offset_set(m, 0);
odp_packet_l4_offset_set(m,
#ifdef INET6
isipv6? sizeof(struct ofp_ip6_hdr) + ipoptlen :
#endif /*INET6*/
sizeof(struct ofp_ip));
ofp_sockbuf_copy_out(&so->so_snd, off, len,
(char *)odp_packet_data(m) + hdrlen);
/*
odp_packet_t src = so->so_snd.sb_mb[so->so_snd.sb_get];
memcpy((uint8_t *)odp_packet_data(m) + hdrlen,
odp_packet_data(src), len);
*/
/*
* If we're sending everything we've got, set PUSH.
* (This will keep happy those implementations which only
* give data to the user when a buffer fills or
* a PUSH comes in.)
*/
if (off + len == so->so_snd.sb_cc)
flags |= OFP_TH_PUSH;
} else {
if (tp->t_flags & TF_ACKNOW)
TCPSTAT_INC(tcps_sndacks);
else if (flags & (OFP_TH_SYN|OFP_TH_FIN|OFP_TH_RST))
TCPSTAT_INC(tcps_sndctrl);
else if (SEQ_GT(tp->snd_up, tp->snd_una))
TCPSTAT_INC(tcps_sndurg);
else
TCPSTAT_INC(tcps_sndwinup);
m = ofp_socket_packet_alloc(hdrlen);
if (m == ODP_PACKET_INVALID) {
SOCKBUF_UNLOCK(&so->so_snd);
error = OFP_ENOBUFS;
goto out;
}
#if 0
if (isipv6 && (MHLEN < hdrlen + ofp_max_linkhdr) &&
MHLEN >= hdrlen) {
MH_ALIGN(m, hdrlen);
} else
#endif
odp_packet_l3_offset_set(m, 0);
odp_packet_l4_offset_set(m,
#ifdef INET6
isipv6? sizeof(struct ofp_ip6_hdr) + ipoptlen :
#endif /*INET6*/
sizeof(struct ofp_ip));
}
#ifdef MAC
mac_inpcb_create_mbuf(tp->t_inpcb, m);
#endif
#ifdef INET6
if (isipv6) {
ip6 = (struct ofp_ip6_hdr *)odp_packet_data(m);
th = (struct ofp_tcphdr *)odp_packet_l4_ptr(m, NULL);
ofp_tcpip_fillheaders(tp->t_inpcb, ip6, th);
} else
#endif /* INET6 */
{/* OK */
ip = (struct ofp_ip *)(odp_packet_data(m));
ipov = (struct ipovly *)ip;
th = (struct ofp_tcphdr *)(ip + 1);
ofp_tcpip_fillheaders(tp->t_inpcb, ip, th);
}
/*
* Fill in fields, remembering maximum advertised
* window for use in delaying messages about window sizes.
* If resending a FIN, be sure not to use a new sequence number.
*/
if ((flags & OFP_TH_FIN) && (tp->t_flags & TF_SENTFIN) &&
tp->snd_nxt == tp->snd_max)
tp->snd_nxt--;
/*
* If we are starting a connection, send ECN setup
* SYN packet. If we are on a retransmit, we may
* resend those bits a number of times as per
* RFC 3168.
*/
if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
if (tp->t_rxtshift >= 1) {
if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
flags |= OFP_TH_ECE|OFP_TH_CWR;
} else
flags |= OFP_TH_ECE|OFP_TH_CWR;
}
if (tp->t_state == TCPS_ESTABLISHED &&
(tp->t_flags & TF_ECN_PERMIT)) {
/*
* If the peer has ECN, mark data packets with
* ECN capable transmission (ECT).
* Ignore pure ack packets, retransmissions and window probes.
*/
if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
!((tp->t_flags & TF_FORCEDATA) && len == 1)) {
#ifdef INET6
if (isipv6)
ip6->ofp_ip6_flow |= odp_cpu_to_be_32(OFP_IPTOS_ECN_ECT0 << 20);
else
#endif
ip->ip_tos |= OFP_IPTOS_ECN_ECT0;
TCPSTAT_INC(tcps_ecn_ect0);
}
/*
* Reply with proper ECN notifications.
*/
if (tp->t_flags & TF_ECN_SND_CWR) {
flags |= OFP_TH_CWR;
t_flags_and(tp->t_flags, ~TF_ECN_SND_CWR);
}
if (tp->t_flags & TF_ECN_SND_ECE)
flags |= OFP_TH_ECE;
}
/*
* If we are doing retransmissions, then snd_nxt will
* not reflect the first unsent octet. For ACK only
* packets, we do not want the sequence number of the
* retransmitted packet, we want the sequence number
* of the next unsent octet. So, if there is no data
* (and no SYN or FIN), use snd_max instead of snd_nxt
* when filling in ti_seq. But if we are in persist
* state, snd_max might reflect one byte beyond the
* right edge of the window, so use snd_nxt in that
* case, since we know we aren't doing a retransmission.
* (retransmit and persist are mutually exclusive...)
*/
if (sack_rxmit == 0) {/* OK */