xref: /csrg-svn/sys/netinet/tcp_output.c (revision 8314)
1*8314Sroot /*	tcp_output.c	4.45	82/10/05	*/
24677Swnj 
34677Swnj #include "../h/param.h"
44677Swnj #include "../h/systm.h"
54677Swnj #include "../h/mbuf.h"
65163Swnj #include "../h/protosw.h"
74677Swnj #include "../h/socket.h"
84804Swnj #include "../h/socketvar.h"
95088Swnj #include "../net/in.h"
106352Ssam #include "../net/route.h"
115088Swnj #include "../net/in_pcb.h"
125088Swnj #include "../net/in_systm.h"
134804Swnj #include "../net/ip.h"
144900Swnj #include "../net/ip_var.h"
154804Swnj #include "../net/tcp.h"
165088Swnj #define	TCPOUTFLAGS
175088Swnj #include "../net/tcp_fsm.h"
185088Swnj #include "../net/tcp_seq.h"
195088Swnj #include "../net/tcp_timer.h"
204804Swnj #include "../net/tcp_var.h"
215088Swnj #include "../net/tcpip.h"
225268Sroot #include "../net/tcp_debug.h"
236505Ssam #include <errno.h>
244677Swnj 
255245Sroot char *tcpstates[]; /* XXX */
265441Swnj 
274678Swnj /*
28*8314Sroot  * Initial options.
295441Swnj  */
305441Swnj u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
315441Swnj 
325441Swnj /*
335245Sroot  * Tcp output routine: figure out what should be sent and send it.
344678Swnj  */
355075Swnj tcp_output(tp)
365075Swnj 	register struct tcpcb *tp;
374678Swnj {
385075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
395075Swnj 	register int len;
405075Swnj 	struct mbuf *m0;
416505Ssam 	int off, flags, win, error;
425075Swnj 	register struct mbuf *m;
435075Swnj 	register struct tcpiphdr *ti;
445441Swnj 	u_char *opt;
455441Swnj 	unsigned optlen = 0;
467125Swnj 	int sendalot;
474678Swnj 
484678Swnj 
495075Swnj 	/*
506279Swnj 	 * Determine length of data that should be transmitted,
515088Swnj 	 * and flags that will be used.
525088Swnj 	 * If there is some data or critical controls (SYN, RST)
535088Swnj 	 * to send, then transmit; otherwise, investigate further.
545075Swnj 	 */
557125Swnj again:
567125Swnj 	sendalot = 0;
575075Swnj 	off = tp->snd_nxt - tp->snd_una;
585163Swnj 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
595285Sroot 	if (len < 0)
606505Ssam 		return (0);	/* ??? */	/* past FIN */
617125Swnj 	if (len > tp->t_maxseg) {
625088Swnj 		len = tp->t_maxseg;
637125Swnj 		sendalot = 1;
647125Swnj 	}
656279Swnj 
665088Swnj 	flags = tcp_outflags[tp->t_state];
675299Sroot 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
685163Swnj 		flags &= ~TH_FIN;
696279Swnj 	if (flags & (TH_SYN|TH_RST|TH_FIN))
705075Swnj 		goto send;
716279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
726279Swnj 		goto send;
734678Swnj 
745075Swnj 	/*
756279Swnj 	 * Sender silly window avoidance.  If can send all data,
766279Swnj 	 * a maximum segment, at least 1/4 of window do it,
776279Swnj 	 * or are forced, do it; otherwise don't bother.
786279Swnj 	 */
796279Swnj 	if (len) {
806279Swnj 		if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc)
816279Swnj 			goto send;
826279Swnj 		if (len * 4 >= tp->snd_wnd)		/* a lot */
836279Swnj 			goto send;
846279Swnj 		if (tp->t_force)
856279Swnj 			goto send;
866279Swnj 	}
876279Swnj 
886279Swnj 	/*
895285Sroot 	 * Send if we owe peer an ACK.
905075Swnj 	 */
915441Swnj 	if (tp->t_flags&TF_ACKNOW)
925075Swnj 		goto send;
934678Swnj 
945441Swnj 
955441Swnj 	/*
965075Swnj 	 * Calculate available window in i, and also amount
975075Swnj 	 * of window known to peer (as advertised window less
985075Swnj 	 * next expected input.)  If this is 35% or more of the
995075Swnj 	 * maximum possible window, then want to send a segment to peer.
1005075Swnj 	 */
1015088Swnj 	win = sbspace(&so->so_rcv);
1025088Swnj 	if (win > 0 &&
1035088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
1045075Swnj 		goto send;
1054678Swnj 
1065075Swnj 	/*
1077125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1087125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1097125Swnj 	 * updates.  The three ``states'' for the output side are:
1107125Swnj 	 *	idle			not doing retransmits or persists
1117125Swnj 	 *	persisting		to move a zero window
1127125Swnj 	 *	(re)transmitting	and thereby not persisting
1137125Swnj 	 *
1147125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1157125Swnj 	 *	is set when we are in persist state.
1167125Swnj 	 * tp->t_force
1177125Swnj 	 *	is set when we are called to send a persist packet.
1187125Swnj 	 * tp->t_timer[TCPT_REXMT]
1197125Swnj 	 *	is set when we are retransmitting
1207125Swnj 	 * The output side is idle when both timers are zero.
1217125Swnj 	 *
1227125Swnj 	 * If send window is closed, there is data to transmit, and no
1237125Swnj 	 * retransmit or persist is pending, then go to persist state,
1247125Swnj 	 * arranging to force out a byte to get more current window information
1257125Swnj 	 * if nothing happens soon.
1267125Swnj 	 */
1277125Swnj 	if (tp->snd_wnd == 0 && so->so_snd.sb_cc &&
1287125Swnj 	    tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) {
1297125Swnj 		tp->t_rxtshift = 0;
1307125Swnj 		tcp_setpersist(tp);
1317125Swnj 	}
1327125Swnj 
1337125Swnj 	/*
1345075Swnj 	 * No reason to send a segment, just return.
1355075Swnj 	 */
1365110Swnj 	return (0);
1374678Swnj 
1385075Swnj send:
1395075Swnj 	/*
1405075Swnj 	 * Grab a header mbuf, attaching a copy of data to
1415075Swnj 	 * be transmitted, and initialize the header from
1425075Swnj 	 * the template for sends on this connection.
1435075Swnj 	 */
1444677Swnj 	MGET(m, 0);
1454677Swnj 	if (m == 0)
1466505Ssam 		return (ENOBUFS);
1475245Sroot 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
1484885Swnj 	m->m_len = sizeof (struct tcpiphdr);
1495075Swnj 	if (len) {
1505075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
1515075Swnj 		if (m->m_next == 0)
1525075Swnj 			len = 0;
1535075Swnj 	}
1545075Swnj 	ti = mtod(m, struct tcpiphdr *);
1555075Swnj 	if (tp->t_template == 0)
1565075Swnj 		panic("tcp_output");
1575110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
1585075Swnj 
1595075Swnj 	/*
1605075Swnj 	 * Fill in fields, remembering maximum advertised
1615075Swnj 	 * window for use in delaying messages about window sizes.
1625075Swnj 	 */
1635245Sroot 	ti->ti_seq = tp->snd_nxt;
1645245Sroot 	ti->ti_ack = tp->rcv_nxt;
1655245Sroot #if vax
1665245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
1675245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1685245Sroot #endif
1695441Swnj 	/*
1705441Swnj 	 * Before ESTABLISHED, force sending of initial options
1715441Swnj 	 * unless TCP set to not do any options.
1725441Swnj 	 */
1735441Swnj 	if (tp->t_state < TCPS_ESTABLISHED) {
1745441Swnj 		if (tp->t_flags&TF_NOOPT)
1755441Swnj 			goto noopt;
1765441Swnj 		opt = tcp_initopt;
1775441Swnj 		optlen = sizeof (tcp_initopt);
178*8314Sroot 		*(u_short *)(opt + 2) = MIN(so->so_rcv.sb_hiwat / 2, 1024);
1795441Swnj #if vax
1805441Swnj 		*(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2));
1815441Swnj #endif
1825441Swnj 	} else {
1835441Swnj 		if (tp->t_tcpopt == 0)
1845441Swnj 			goto noopt;
1855441Swnj 		opt = mtod(tp->t_tcpopt, u_char *);
1865441Swnj 		optlen = tp->t_tcpopt->m_len;
1875441Swnj 	}
188*8314Sroot 	if (opt) {
1895110Swnj 		m0 = m->m_next;
1905584Sroot 		m->m_next = m_get(M_DONTWAIT);
1915088Swnj 		if (m->m_next == 0) {
1925088Swnj 			(void) m_free(m);
1935441Swnj 			m_freem(m0);
1946505Ssam 			return (ENOBUFS);
1955088Swnj 		}
1965088Swnj 		m->m_next->m_next = m0;
1975441Swnj 		m0 = m->m_next;
1985441Swnj 		m0->m_len = optlen;
1996162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
2005441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
2015441Swnj 		while (m0->m_len & 0x3) {
2025441Swnj 			*opt++ = TCPOPT_EOL;
2035441Swnj 			m0->m_len++;
2045441Swnj 		}
2055441Swnj 		optlen = m0->m_len;
2065441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2075088Swnj 	}
2085441Swnj noopt:
2095088Swnj 	ti->ti_flags = flags;
2105075Swnj 	win = sbspace(&so->so_rcv);
2116279Swnj 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
2126279Swnj 		win = 0;
2135075Swnj 	if (win > 0)
2146279Swnj #if vax
2155110Swnj 		ti->ti_win = htons((u_short)win);
2166279Swnj #else
2176279Swnj 		ti->ti_win = win;
2186279Swnj #endif
2195088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2205420Swnj 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
2215420Swnj #if vax
2225420Swnj 		ti->ti_urp = htons(ti->ti_urp);
2235420Swnj #endif
2245075Swnj 		ti->ti_flags |= TH_URG;
2255075Swnj 	} else
2265075Swnj 		/*
2275075Swnj 		 * If no urgent pointer to send, then we pull
2285075Swnj 		 * the urgent pointer to the left edge of the send window
2295075Swnj 		 * so that it doesn't drift into the send window on sequence
2305075Swnj 		 * number wraparound.
2315075Swnj 		 */
2325088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
2337644Sroot 	/*
2347644Sroot 	 * If anything to send and we can send it all, set PUSH.
2357644Sroot 	 * (This will keep happy those implementations which only
2367644Sroot 	 * give data to the user when a buffer fills or a PUSH comes in.
2377644Sroot 	 */
2387644Sroot 	if (len && off+len == so->so_snd.sb_cc)
2397644Sroot 		ti->ti_flags |= TH_PUSH;
2405075Swnj 
2415075Swnj 	/*
2425075Swnj 	 * Put TCP length in extended header, and then
2435075Swnj 	 * checksum extended header and data.
2445075Swnj 	 */
2455441Swnj 	if (len + optlen) {
2465441Swnj 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
2475441Swnj #if vax
2485441Swnj 		ti->ti_len = htons((u_short)ti->ti_len);
2495441Swnj #endif
2505441Swnj 	}
2516162Ssam 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
2525075Swnj 
2535075Swnj 	/*
2547125Swnj 	 * In transmit state, time the transmission and arrange for
2557125Swnj 	 * the retransmit.  In persist state, reset persist time for
2567125Swnj 	 * next persist.
2575088Swnj 	 */
2587125Swnj 	if (tp->t_force == 0) {
2597125Swnj 		/*
2607146Swnj 		 * Advance snd_nxt over sequence space of this segment.
2617125Swnj 		 */
2627125Swnj 		if (flags & (TH_SYN|TH_FIN))
2637125Swnj 			tp->snd_nxt++;
2647125Swnj 		tp->snd_nxt += len;
2657149Swnj 		if (SEQ_GT(tp->snd_nxt, tp->snd_max))
2667149Swnj 			tp->snd_max = tp->snd_nxt;
2675088Swnj 
2687125Swnj 		/*
2697125Swnj 		 * Time this transmission if not a retransmission and
2707125Swnj 		 * not currently timing anything.
2717125Swnj 		 */
2727125Swnj 		if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
2737125Swnj 			tp->t_rtt = 1;
2747125Swnj 			tp->t_rtseq = tp->snd_nxt - len;
2757125Swnj 		}
2765088Swnj 
2777125Swnj 		/*
2787125Swnj 		 * Set retransmit timer if not currently set.
2797125Swnj 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
2807125Swnj 		 * Initialize shift counter which is used for exponential
2817125Swnj 		 * backoff of retransmit time.
2827125Swnj 		 */
2837125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
2847125Swnj 		    tp->snd_nxt != tp->snd_una) {
2857125Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
2867125Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
2877125Swnj 			tp->t_rtt = 0;
2887125Swnj 			tp->t_rxtshift = 0;
2897125Swnj 		}
2907125Swnj 		tp->t_timer[TCPT_PERSIST] = 0;
2917149Swnj 	} else {
2927149Swnj 		if (SEQ_GT(tp->snd_una+1, tp->snd_max))
2937149Swnj 			tp->snd_max = tp->snd_una+1;
2947146Swnj 	}
2955163Swnj 
2965163Swnj 	/*
2975268Sroot 	 * Trace.
2985268Sroot 	 */
2997146Swnj 	if (so->so_options & SO_DEBUG)
3005268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
3015268Sroot 
3025268Sroot 	/*
3035075Swnj 	 * Fill in IP length and desired time to live and
3045075Swnj 	 * send to IP level.
3055075Swnj 	 */
3065441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
3075075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
3087146Swnj 	if (error = ip_output(m, tp->t_ipopt, (so->so_options & SO_DONTROUTE) ?
3097146Swnj 	    &routetoif : &tp->t_inpcb->inp_route, 0))
3106505Ssam 		return (error);
3115075Swnj 
3125075Swnj 	/*
3135075Swnj 	 * Data sent (as far as we can tell).
3145075Swnj 	 * If this advertises a larger window than any other segment,
3155245Sroot 	 * then remember the size of the advertised window.
3165088Swnj 	 * Drop send for purpose of ACK requirements.
3175075Swnj 	 */
3185252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
3195075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
3205088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
3217125Swnj 	if (sendalot && tp->t_force == 0)
3227125Swnj 		goto again;
3236505Ssam 	return (0);
3244677Swnj }
3257125Swnj 
3267125Swnj tcp_setpersist(tp)
3277125Swnj 	register struct tcpcb *tp;
3287125Swnj {
3297125Swnj 
3307125Swnj 	if (tp->t_timer[TCPT_REXMT])
3317125Swnj 		panic("tcp_output REXMT");
3327125Swnj 	/*
3337125Swnj 	 * Start/restart persistance timer.
3347125Swnj 	 */
3357125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3367125Swnj 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
3377125Swnj 	    TCPTV_PERSMIN, TCPTV_MAX);
3387125Swnj 	tp->t_rxtshift++;
3397125Swnj 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
3407125Swnj 		tp->t_rxtshift = 0;
3417125Swnj }
342