xref: /csrg-svn/sys/netinet/tcp_output.c (revision 5163)
1*5163Swnj /*	tcp_output.c	4.21	81/12/02	*/
24677Swnj 
34677Swnj #include "../h/param.h"
44677Swnj #include "../h/systm.h"
54677Swnj #include "../h/mbuf.h"
6*5163Swnj #include "../h/protosw.h"
74677Swnj #include "../h/socket.h"
84804Swnj #include "../h/socketvar.h"
95088Swnj #include "../net/in.h"
105088Swnj #include "../net/in_pcb.h"
115088Swnj #include "../net/in_systm.h"
124804Swnj #include "../net/ip.h"
134900Swnj #include "../net/ip_var.h"
144804Swnj #include "../net/tcp.h"
155088Swnj #define	TCPOUTFLAGS
165088Swnj #include "../net/tcp_fsm.h"
175088Swnj #include "../net/tcp_seq.h"
185088Swnj #include "../net/tcp_timer.h"
194804Swnj #include "../net/tcp_var.h"
205088Swnj #include "../net/tcpip.h"
215110Swnj #include "../errno.h"
224677Swnj 
234678Swnj /*
245075Swnj  * Tcp output routine: figure out what should be sent
255075Swnj  * and, if nothing, send a null segment anyways if force is nonzero
265075Swnj  * (e.g. to be sure to send an ACK).
275075Swnj  *
285075Swnj  * This routine can be called only after SYNs have been exchanged.
294678Swnj  */
305075Swnj tcp_output(tp)
315075Swnj 	register struct tcpcb *tp;
324678Swnj {
335075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
345075Swnj 	register int len;
355075Swnj 	struct mbuf *m0;
365075Swnj 	int off, flags;
375075Swnj 	register struct mbuf *m;
385075Swnj 	register struct tcpiphdr *ti;
395075Swnj 	int win;
404678Swnj 
415075Swnj COUNT(TCP_OUTPUT);
424678Swnj 
435075Swnj 	/*
445088Swnj 	 * Determine length of data that can be transmitted,
455088Swnj 	 * and flags that will be used.
465088Swnj 	 * If there is some data or critical controls (SYN, RST)
475088Swnj 	 * to send, then transmit; otherwise, investigate further.
485075Swnj 	 */
495075Swnj 	off = tp->snd_nxt - tp->snd_una;
50*5163Swnj 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
515088Swnj 	if (len > tp->t_maxseg)
525088Swnj 		len = tp->t_maxseg;
535088Swnj 	flags = tcp_outflags[tp->t_state];
54*5163Swnj 	if (len < so->so_snd.sb_cc)
55*5163Swnj 		flags &= ~TH_FIN;
565088Swnj 	if (len || (flags & (TH_SYN|TH_RST)))
575075Swnj 		goto send;
584678Swnj 
595075Swnj 	/*
605088Swnj 	 * See if we owe peer an ACK or have a unacked FIN to send.
615075Swnj 	 */
625088Swnj 	if (tp->t_flags & TF_ACKNOW)
635075Swnj 		goto send;
645088Swnj 	if ((so->so_state & SS_CANTSENDMORE) &&
655088Swnj 	    TCPS_OURFINNOTACKED(tp->t_state))
665075Swnj 		goto send;
674678Swnj 
685075Swnj 	/*
695075Swnj 	 * Calculate available window in i, and also amount
705075Swnj 	 * of window known to peer (as advertised window less
715075Swnj 	 * next expected input.)  If this is 35% or more of the
725075Swnj 	 * maximum possible window, then want to send a segment to peer.
735075Swnj 	 */
745088Swnj 	win = sbspace(&so->so_rcv);
755088Swnj 	if (win > 0 &&
765088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
775075Swnj 		goto send;
784678Swnj 
795075Swnj 	/*
805075Swnj 	 * No reason to send a segment, just return.
815075Swnj 	 */
825110Swnj 	return (0);
834678Swnj 
845075Swnj send:
855075Swnj 	/*
865075Swnj 	 * Grab a header mbuf, attaching a copy of data to
875075Swnj 	 * be transmitted, and initialize the header from
885075Swnj 	 * the template for sends on this connection.
895075Swnj 	 */
904677Swnj 	MGET(m, 0);
914677Swnj 	if (m == 0)
924677Swnj 		return (0);
934885Swnj 	m->m_off = MMAXOFF - sizeof(struct tcpiphdr);
944885Swnj 	m->m_len = sizeof (struct tcpiphdr);
955075Swnj 	if (len) {
965075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
975075Swnj 		if (m->m_next == 0)
985075Swnj 			len = 0;
995075Swnj 	}
1005075Swnj 	ti = mtod(m, struct tcpiphdr *);
1015075Swnj 	if (tp->t_template == 0)
1025075Swnj 		panic("tcp_output");
1035110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
1045075Swnj 
1055075Swnj 	/*
1065075Swnj 	 * Fill in fields, remembering maximum advertised
1075075Swnj 	 * window for use in delaying messages about window sizes.
1085075Swnj 	 */
1095075Swnj 	ti->ti_seq = htonl(tp->snd_nxt);
1105088Swnj 	ti->ti_ack = htonl(tp->rcv_nxt);
1115088Swnj 	if (tp->t_tcpopt) {
1125110Swnj 		m0 = m->m_next;
1135088Swnj 		m->m_next = m_get(0);
1145088Swnj 		if (m->m_next == 0) {
1155088Swnj 			(void) m_free(m);
1165110Swnj 			m_freem(m);
1175088Swnj 			return (0);
1185088Swnj 		}
1195088Swnj 		m->m_next->m_next = m0;
1205088Swnj 		m->m_off = MMINOFF;
1215088Swnj 		m->m_len = tp->t_tcpopt->m_len;
1225088Swnj 		bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t),
1235110Swnj 		    (unsigned)tp->t_tcpopt->m_len);
1245088Swnj 		ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2;
1255088Swnj 	}
1265088Swnj 	ti->ti_flags = flags;
1275075Swnj 	win = sbspace(&so->so_rcv);
1285075Swnj 	if (win > 0)
1295110Swnj 		ti->ti_win = htons((u_short)win);
1305088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1315088Swnj 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1325075Swnj 		ti->ti_flags |= TH_URG;
1335075Swnj 	} else
1345075Swnj 		/*
1355075Swnj 		 * If no urgent pointer to send, then we pull
1365075Swnj 		 * the urgent pointer to the left edge of the send window
1375075Swnj 		 * so that it doesn't drift into the send window on sequence
1385075Swnj 		 * number wraparound.
1395075Swnj 		 */
1405088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
1415088Swnj 	/* PUSH */
1425075Swnj 
1435075Swnj 	/*
1445075Swnj 	 * Put TCP length in extended header, and then
1455075Swnj 	 * checksum extended header and data.
1465075Swnj 	 */
1475075Swnj 	if (len)
1485075Swnj 		ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr)));
1495088Swnj 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len);
1505075Swnj 
1515075Swnj 	/*
1525088Swnj 	 * Advance snd_nxt over sequence space of this segment
1535088Swnj 	 */
1545088Swnj 	if (flags & (TH_SYN|TH_FIN))
1555088Swnj 		len++;
1565088Swnj 	tp->snd_nxt += len;
1575088Swnj 
1585088Swnj 	/*
159*5163Swnj 	 * If this transmission closes the window,
160*5163Swnj 	 * start persistance timer at 2 round trip
161*5163Swnj 	 * times but at least TCPTV_PERSMIN ticks.
1625088Swnj 	 */
163*5163Swnj 	if (tp->snd_una + tp->snd_wnd >= tp->snd_nxt &&
164*5163Swnj 	    tp->t_timer[TCPT_PERSIST] == 0) {
165*5163Swnj 		tp->t_timer[TCPT_PERSIST] = 2 * tp->t_srtt;
166*5163Swnj 		if (tp->t_timer[TCPT_PERSIST] < TCPTV_PERSMIN)
167*5163Swnj 			tp->t_timer[TCPT_PERSIST] = TCPTV_PERSMIN;
168*5163Swnj 		if (tp->t_timer[TCPT_PERSIST] > TCPTV_MAX)
169*5163Swnj 			tp->t_timer[TCPT_PERSIST] = TCPTV_MAX;
1705088Swnj 	}
1715088Swnj 
1725088Swnj 	/*
173*5163Swnj 	 * Time this transmission if not a retransmission and
174*5163Swnj 	 * not currently timing anything.
175*5163Swnj 	 */
176*5163Swnj 	if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
177*5163Swnj 		tp->t_rtt = 1;
178*5163Swnj 		tp->t_rtseq = tp->snd_nxt - len;
179*5163Swnj 	}
180*5163Swnj 
181*5163Swnj 	/*
182*5163Swnj 	 * Set retransmit timer if not currently set.
183*5163Swnj 	 * Initial value for retransmit timer to tcp_beta*tp->t_srtt,
184*5163Swnj 	 * with a minimum of TCPTV_MIN and a max of TCPTV_MAX.
185*5163Swnj 	 * Initialize shift counter which is used for exponential
186*5163Swnj 	 * backoff of retransmit time.
187*5163Swnj 	 */
188*5163Swnj 	if (tp->t_timer[TCPT_REXMT] == 0) {
189*5163Swnj 		tp->t_timer[TCPT_REXMT] = tcp_beta * tp->t_srtt;
190*5163Swnj 		if (tp->t_timer[TCPT_REXMT] < TCPTV_MIN)
191*5163Swnj 			tp->t_timer[TCPT_REXMT] = TCPTV_MIN;
192*5163Swnj 		if (tp->t_timer[TCPT_REXMT] > TCPTV_MAX)
193*5163Swnj 			tp->t_timer[TCPT_REXMT] = TCPTV_MAX;
194*5163Swnj 		tp->t_rxtshift = 0;
195*5163Swnj 	}
196*5163Swnj 
197*5163Swnj 	/*
1985075Swnj 	 * Fill in IP length and desired time to live and
1995075Swnj 	 * send to IP level.
2005075Swnj 	 */
2015075Swnj 	((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr);
2025075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
2035088Swnj 	if (ip_output(m, tp->t_ipopt) == 0)
2045088Swnj 		return (0);
2055075Swnj 
2065075Swnj 	/*
2075075Swnj 	 * Data sent (as far as we can tell).
2085075Swnj 	 * If this advertises a larger window than any other segment,
2095075Swnj 	 * then record its sequence to be used in suppressing messages.
2105088Swnj 	 * Drop send for purpose of ACK requirements.
2115075Swnj 	 */
2125075Swnj 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
2135075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
2145088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
2155088Swnj 	tp->snd_max = tp->snd_nxt;
2165088Swnj 	return (1);
2174677Swnj }
218