Commit | Line | Data |
---|---|---|
5e74df82 | 1 | /* tcp_output.c 4.28 82/01/17 */ |
76ee76df BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
5 | #include "../h/mbuf.h" | |
405c9168 | 6 | #include "../h/protosw.h" |
76ee76df | 7 | #include "../h/socket.h" |
d52566dd | 8 | #include "../h/socketvar.h" |
0974b45c BJ |
9 | #include "../net/in.h" |
10 | #include "../net/in_pcb.h" | |
11 | #include "../net/in_systm.h" | |
d52566dd | 12 | #include "../net/ip.h" |
eb44bfb2 | 13 | #include "../net/ip_var.h" |
d52566dd | 14 | #include "../net/tcp.h" |
0974b45c | 15 | #define TCPOUTFLAGS |
d52566dd | 16 | #include "../net/tcp_fsm.h" |
0974b45c BJ |
17 | #include "../net/tcp_seq.h" |
18 | #include "../net/tcp_timer.h" | |
19 | #include "../net/tcp_var.h" | |
20 | #include "../net/tcpip.h" | |
f1dd32da | 21 | #include "../net/tcp_debug.h" |
f1b2fa5b | 22 | #include "../errno.h" |
76ee76df | 23 | |
4aed14e3 | 24 | char *tcpstates[]; /* XXX */ |
ea727f86 | 25 | /* |
4aed14e3 | 26 | * Tcp output routine: figure out what should be sent and send it. |
ea727f86 | 27 | */ |
a6503abf | 28 | tcp_output(tp) |
53a5409e | 29 | register struct tcpcb *tp; |
ea727f86 | 30 | { |
53a5409e | 31 | register struct socket *so = tp->t_inpcb->inp_socket; |
a6503abf BJ |
32 | register int len; |
33 | struct mbuf *m0; | |
34 | int off, flags; | |
35 | register struct mbuf *m; | |
36 | register struct tcpiphdr *ti; | |
37 | int win; | |
76ee76df | 38 | |
a6503abf | 39 | COUNT(TCP_OUTPUT); |
76ee76df | 40 | |
a6503abf | 41 | /* |
0974b45c BJ |
42 | * Determine length of data that can be transmitted, |
43 | * and flags that will be used. | |
44 | * If there is some data or critical controls (SYN, RST) | |
45 | * to send, then transmit; otherwise, investigate further. | |
a6503abf BJ |
46 | */ |
47 | off = tp->snd_nxt - tp->snd_una; | |
405c9168 | 48 | len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; |
3232ef09 BJ |
49 | if (len < 0) |
50 | return; /* past FIN */ | |
0974b45c BJ |
51 | if (len > tp->t_maxseg) |
52 | len = tp->t_maxseg; | |
53 | flags = tcp_outflags[tp->t_state]; | |
275e05b7 | 54 | if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) |
405c9168 | 55 | flags &= ~TH_FIN; |
3232ef09 | 56 | if (len || (flags & (TH_SYN|TH_RST|TH_FIN))) |
a6503abf BJ |
57 | goto send; |
58 | ||
59 | /* | |
3232ef09 | 60 | * Send if we owe peer an ACK. |
a6503abf | 61 | */ |
0974b45c | 62 | if (tp->t_flags & TF_ACKNOW) |
a6503abf | 63 | goto send; |
76ee76df | 64 | |
a6503abf BJ |
65 | /* |
66 | * Calculate available window in i, and also amount | |
67 | * of window known to peer (as advertised window less | |
68 | * next expected input.) If this is 35% or more of the | |
69 | * maximum possible window, then want to send a segment to peer. | |
70 | */ | |
0974b45c BJ |
71 | win = sbspace(&so->so_rcv); |
72 | if (win > 0 && | |
73 | ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) | |
a6503abf BJ |
74 | goto send; |
75 | ||
76 | /* | |
77 | * No reason to send a segment, just return. | |
78 | */ | |
f1b2fa5b | 79 | return (0); |
a6503abf BJ |
80 | |
81 | send: | |
82 | /* | |
83 | * Grab a header mbuf, attaching a copy of data to | |
84 | * be transmitted, and initialize the header from | |
85 | * the template for sends on this connection. | |
86 | */ | |
76ee76df BJ |
87 | MGET(m, 0); |
88 | if (m == 0) | |
89 | return (0); | |
4aed14e3 | 90 | m->m_off = MMAXOFF - sizeof (struct tcpiphdr); |
53a5409e | 91 | m->m_len = sizeof (struct tcpiphdr); |
a6503abf BJ |
92 | if (len) { |
93 | m->m_next = m_copy(so->so_snd.sb_mb, off, len); | |
94 | if (m->m_next == 0) | |
95 | len = 0; | |
96 | } | |
97 | ti = mtod(m, struct tcpiphdr *); | |
98 | if (tp->t_template == 0) | |
99 | panic("tcp_output"); | |
f1b2fa5b | 100 | bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); |
a6503abf BJ |
101 | |
102 | /* | |
103 | * Fill in fields, remembering maximum advertised | |
104 | * window for use in delaying messages about window sizes. | |
105 | */ | |
4aed14e3 BJ |
106 | ti->ti_seq = tp->snd_nxt; |
107 | ti->ti_ack = tp->rcv_nxt; | |
108 | #if vax | |
109 | ti->ti_seq = htonl(ti->ti_seq); | |
110 | ti->ti_ack = htonl(ti->ti_ack); | |
111 | #endif | |
0974b45c | 112 | if (tp->t_tcpopt) { |
f1b2fa5b | 113 | m0 = m->m_next; |
0974b45c BJ |
114 | m->m_next = m_get(0); |
115 | if (m->m_next == 0) { | |
116 | (void) m_free(m); | |
f1b2fa5b | 117 | m_freem(m); |
0974b45c BJ |
118 | return (0); |
119 | } | |
120 | m->m_next->m_next = m0; | |
121 | m->m_off = MMINOFF; | |
122 | m->m_len = tp->t_tcpopt->m_len; | |
123 | bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t), | |
f1b2fa5b | 124 | (unsigned)tp->t_tcpopt->m_len); |
0974b45c BJ |
125 | ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2; |
126 | } | |
127 | ti->ti_flags = flags; | |
a6503abf BJ |
128 | win = sbspace(&so->so_rcv); |
129 | if (win > 0) | |
f1b2fa5b | 130 | ti->ti_win = htons((u_short)win); |
0974b45c | 131 | if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { |
5e74df82 BJ |
132 | ti->ti_urp = tp->snd_up - tp->snd_nxt; |
133 | #if vax | |
134 | ti->ti_urp = htons(ti->ti_urp); | |
135 | #endif | |
a6503abf BJ |
136 | ti->ti_flags |= TH_URG; |
137 | } else | |
138 | /* | |
139 | * If no urgent pointer to send, then we pull | |
140 | * the urgent pointer to the left edge of the send window | |
141 | * so that it doesn't drift into the send window on sequence | |
142 | * number wraparound. | |
143 | */ | |
0974b45c BJ |
144 | tp->snd_up = tp->snd_una; /* drag it along */ |
145 | /* PUSH */ | |
a6503abf BJ |
146 | |
147 | /* | |
148 | * Put TCP length in extended header, and then | |
149 | * checksum extended header and data. | |
150 | */ | |
151 | if (len) | |
152 | ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr))); | |
0974b45c BJ |
153 | ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len); |
154 | ||
155 | /* | |
156 | * Advance snd_nxt over sequence space of this segment | |
157 | */ | |
158 | if (flags & (TH_SYN|TH_FIN)) | |
4aed14e3 | 159 | tp->snd_nxt++; |
0974b45c BJ |
160 | tp->snd_nxt += len; |
161 | ||
162 | /* | |
405c9168 | 163 | * If this transmission closes the window, |
4aed14e3 BJ |
164 | * start persistance timer at 2 round trip times |
165 | * but at least TCPTV_PERSMIN ticks. | |
0974b45c | 166 | */ |
89413846 | 167 | if (TCPS_HAVERCVDSYN(tp->t_state) && |
275e05b7 | 168 | SEQ_GEQ(tp->snd_nxt, tp->snd_una+tp->snd_wnd) && |
4aed14e3 BJ |
169 | tp->t_timer[TCPT_PERSIST] == 0) |
170 | TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], | |
171 | 2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX); | |
405c9168 BJ |
172 | |
173 | /* | |
174 | * Time this transmission if not a retransmission and | |
175 | * not currently timing anything. | |
176 | */ | |
177 | if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { | |
178 | tp->t_rtt = 1; | |
179 | tp->t_rtseq = tp->snd_nxt - len; | |
180 | } | |
181 | ||
182 | /* | |
183 | * Set retransmit timer if not currently set. | |
4aed14e3 | 184 | * Initial value for retransmit timer to tcp_beta*tp->t_srtt. |
405c9168 BJ |
185 | * Initialize shift counter which is used for exponential |
186 | * backoff of retransmit time. | |
187 | */ | |
4aed14e3 BJ |
188 | if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) { |
189 | TCPT_RANGESET(tp->t_timer[TCPT_REXMT], | |
190 | tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); | |
275e05b7 | 191 | tp->t_rtt = 0; |
405c9168 | 192 | tp->t_rxtshift = 0; |
0974b45c | 193 | } |
a6503abf | 194 | |
f1dd32da BJ |
195 | /* |
196 | * Trace. | |
197 | */ | |
198 | if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) | |
199 | tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); | |
200 | ||
a6503abf BJ |
201 | /* |
202 | * Fill in IP length and desired time to live and | |
203 | * send to IP level. | |
204 | */ | |
205 | ((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr); | |
206 | ((struct ip *)ti)->ip_ttl = TCP_TTL; | |
5e74df82 | 207 | if (ip_output(m, tp->t_ipopt) == 0) |
0974b45c | 208 | return (0); |
a6503abf BJ |
209 | |
210 | /* | |
211 | * Data sent (as far as we can tell). | |
212 | * If this advertises a larger window than any other segment, | |
4aed14e3 | 213 | * then remember the size of the advertised window. |
0974b45c | 214 | * Drop send for purpose of ACK requirements. |
a6503abf | 215 | */ |
be43ac7f | 216 | if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) |
a6503abf | 217 | tp->rcv_adv = tp->rcv_nxt + win; |
0974b45c | 218 | tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); |
4aed14e3 BJ |
219 | if (SEQ_GT(tp->snd_nxt, tp->snd_max)) |
220 | tp->snd_max = tp->snd_nxt; | |
0974b45c | 221 | return (1); |
76ee76df | 222 | } |