xref: /csrg-svn/sys/netinet/tcp_output.c (revision 5268)
1 /*	tcp_output.c	4.24	81/12/20	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/protosw.h"
7 #include "../h/socket.h"
8 #include "../h/socketvar.h"
9 #include "../net/in.h"
10 #include "../net/in_pcb.h"
11 #include "../net/in_systm.h"
12 #include "../net/ip.h"
13 #include "../net/ip_var.h"
14 #include "../net/tcp.h"
15 #define	TCPOUTFLAGS
16 #include "../net/tcp_fsm.h"
17 #include "../net/tcp_seq.h"
18 #include "../net/tcp_timer.h"
19 #include "../net/tcp_var.h"
20 #include "../net/tcpip.h"
21 #include "../net/tcp_debug.h"
22 #include "../errno.h"
23 
24 char *tcpstates[]; /* XXX */
25 /*
26  * Tcp output routine: figure out what should be sent and send it.
27  */
28 tcp_output(tp)
29 	register struct tcpcb *tp;
30 {
31 	register struct socket *so = tp->t_inpcb->inp_socket;
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;
38 
39 COUNT(TCP_OUTPUT);
40 
41 	/*
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.
46 	 */
47 	off = tp->snd_nxt - tp->snd_una;
48 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
49 	if (len > tp->t_maxseg)
50 		len = tp->t_maxseg;
51 	if (len < 0)
52 		len = 0;		/* FIN can cause -1 */
53 	flags = tcp_outflags[tp->t_state];
54 	if (len < so->so_snd.sb_cc)
55 		flags &= ~TH_FIN;
56 	if (len || (flags & (TH_SYN|TH_RST)))
57 		goto send;
58 
59 	/*
60 	 * See if we owe peer an ACK or have a unacked FIN to send.
61 	 */
62 	if (tp->t_flags & TF_ACKNOW)
63 		goto send;
64 	if ((so->so_state & SS_CANTSENDMORE) &&
65 	    TCPS_OURFINNOTACKED(tp->t_state))
66 		goto send;
67 
68 	/*
69 	 * Calculate available window in i, and also amount
70 	 * of window known to peer (as advertised window less
71 	 * next expected input.)  If this is 35% or more of the
72 	 * maximum possible window, then want to send a segment to peer.
73 	 */
74 	win = sbspace(&so->so_rcv);
75 	if (win > 0 &&
76 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
77 		goto send;
78 
79 	/*
80 	 * No reason to send a segment, just return.
81 	 */
82 	return (0);
83 
84 send:
85 	/*
86 	 * Grab a header mbuf, attaching a copy of data to
87 	 * be transmitted, and initialize the header from
88 	 * the template for sends on this connection.
89 	 */
90 	MGET(m, 0);
91 	if (m == 0)
92 		return (0);
93 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
94 	m->m_len = sizeof (struct tcpiphdr);
95 	if (len) {
96 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
97 		if (m->m_next == 0)
98 			len = 0;
99 	}
100 	ti = mtod(m, struct tcpiphdr *);
101 	if (tp->t_template == 0)
102 		panic("tcp_output");
103 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
104 
105 	/*
106 	 * Fill in fields, remembering maximum advertised
107 	 * window for use in delaying messages about window sizes.
108 	 */
109 	ti->ti_seq = tp->snd_nxt;
110 	ti->ti_ack = tp->rcv_nxt;
111 #if vax
112 	ti->ti_seq = htonl(ti->ti_seq);
113 	ti->ti_ack = htonl(ti->ti_ack);
114 #endif
115 	if (tp->t_tcpopt) {
116 		m0 = m->m_next;
117 		m->m_next = m_get(0);
118 		if (m->m_next == 0) {
119 			(void) m_free(m);
120 			m_freem(m);
121 			return (0);
122 		}
123 		m->m_next->m_next = m0;
124 		m->m_off = MMINOFF;
125 		m->m_len = tp->t_tcpopt->m_len;
126 		bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t),
127 		    (unsigned)tp->t_tcpopt->m_len);
128 		ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2;
129 	}
130 	ti->ti_flags = flags;
131 	win = sbspace(&so->so_rcv);
132 	if (win > 0)
133 		ti->ti_win = htons((u_short)win);
134 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
135 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
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 		 */
144 		tp->snd_up = tp->snd_una;		/* drag it along */
145 	/* PUSH */
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)));
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))
159 		tp->snd_nxt++;
160 	tp->snd_nxt += len;
161 
162 	/*
163 	 * If this transmission closes the window,
164 	 * start persistance timer at 2 round trip times
165 	 * but at least TCPTV_PERSMIN ticks.
166 	 */
167 	if (SEQ_GT(tp->snd_nxt, tp->snd_una+tp->snd_wnd) &&
168 	    tp->t_timer[TCPT_PERSIST] == 0)
169 		TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
170 		    2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX);
171 
172 	/*
173 	 * Time this transmission if not a retransmission and
174 	 * not currently timing anything.
175 	 */
176 	if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
177 		tp->t_rtt = 1;
178 		tp->t_rtseq = tp->snd_nxt - len;
179 	}
180 
181 	/*
182 	 * Set retransmit timer if not currently set.
183 	 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
184 	 * Initialize shift counter which is used for exponential
185 	 * backoff of retransmit time.
186 	 */
187 	if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) {
188 		TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
189 		    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
190 		tp->t_rxtshift = 0;
191 	}
192 
193 	/*
194 	 * Trace.
195 	 */
196 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
197 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
198 
199 	/*
200 	 * Fill in IP length and desired time to live and
201 	 * send to IP level.
202 	 */
203 	((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr);
204 	((struct ip *)ti)->ip_ttl = TCP_TTL;
205 	if (ip_output(m, tp->t_ipopt) == 0)
206 		return (0);
207 
208 	/*
209 	 * Data sent (as far as we can tell).
210 	 * If this advertises a larger window than any other segment,
211 	 * then remember the size of the advertised window.
212 	 * Drop send for purpose of ACK requirements.
213 	 */
214 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
215 		tp->rcv_adv = tp->rcv_nxt + win;
216 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
217 	if (SEQ_GT(tp->snd_nxt, tp->snd_max))
218 		tp->snd_max = tp->snd_nxt;
219 	return (1);
220 }
221