xref: /csrg-svn/sys/netinet/tcp_output.c (revision 10895)
1*10895Ssam /*	tcp_output.c	4.51	83/02/10	*/
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"
9*10895Ssam #include "../h/errno.h"
10*10895Ssam 
11*10895Ssam #include "../net/route.h"
12*10895Ssam 
138402Swnj #include "../netinet/in.h"
148402Swnj #include "../netinet/in_pcb.h"
158402Swnj #include "../netinet/in_systm.h"
168402Swnj #include "../netinet/ip.h"
178402Swnj #include "../netinet/ip_var.h"
188402Swnj #include "../netinet/tcp.h"
195088Swnj #define	TCPOUTFLAGS
208402Swnj #include "../netinet/tcp_fsm.h"
218402Swnj #include "../netinet/tcp_seq.h"
228402Swnj #include "../netinet/tcp_timer.h"
238402Swnj #include "../netinet/tcp_var.h"
248402Swnj #include "../netinet/tcpip.h"
258402Swnj #include "../netinet/tcp_debug.h"
264677Swnj 
274678Swnj /*
288314Sroot  * 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 	 */
1449643Ssam 	MGET(m, M_DONTWAIT, MT_DATA);
14510143Ssam 	if (m == INADDR_ANY)
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 	ti->ti_seq = htonl(ti->ti_seq);
1665245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1675441Swnj 	/*
1685441Swnj 	 * Before ESTABLISHED, force sending of initial options
1695441Swnj 	 * unless TCP set to not do any options.
1705441Swnj 	 */
1715441Swnj 	if (tp->t_state < TCPS_ESTABLISHED) {
1725441Swnj 		if (tp->t_flags&TF_NOOPT)
1735441Swnj 			goto noopt;
1745441Swnj 		opt = tcp_initopt;
1755441Swnj 		optlen = sizeof (tcp_initopt);
1768314Sroot 		*(u_short *)(opt + 2) = MIN(so->so_rcv.sb_hiwat / 2, 1024);
1775441Swnj 		*(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2));
1785441Swnj 	} else {
1795441Swnj 		if (tp->t_tcpopt == 0)
1805441Swnj 			goto noopt;
1815441Swnj 		opt = mtod(tp->t_tcpopt, u_char *);
1825441Swnj 		optlen = tp->t_tcpopt->m_len;
1835441Swnj 	}
1848314Sroot 	if (opt) {
1855110Swnj 		m0 = m->m_next;
1869643Ssam 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
1875088Swnj 		if (m->m_next == 0) {
1885088Swnj 			(void) m_free(m);
1895441Swnj 			m_freem(m0);
1906505Ssam 			return (ENOBUFS);
1915088Swnj 		}
1925088Swnj 		m->m_next->m_next = m0;
1935441Swnj 		m0 = m->m_next;
1945441Swnj 		m0->m_len = optlen;
1956162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
1965441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
1975441Swnj 		while (m0->m_len & 0x3) {
1985441Swnj 			*opt++ = TCPOPT_EOL;
1995441Swnj 			m0->m_len++;
2005441Swnj 		}
2015441Swnj 		optlen = m0->m_len;
2025441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2035088Swnj 	}
2045441Swnj noopt:
2055088Swnj 	ti->ti_flags = flags;
2065075Swnj 	win = sbspace(&so->so_rcv);
2076279Swnj 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
2086279Swnj 		win = 0;
2095075Swnj 	if (win > 0)
2105110Swnj 		ti->ti_win = htons((u_short)win);
2115088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2125420Swnj 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
2135420Swnj 		ti->ti_urp = htons(ti->ti_urp);
2145075Swnj 		ti->ti_flags |= TH_URG;
2155075Swnj 	} else
2165075Swnj 		/*
2175075Swnj 		 * If no urgent pointer to send, then we pull
2185075Swnj 		 * the urgent pointer to the left edge of the send window
2195075Swnj 		 * so that it doesn't drift into the send window on sequence
2205075Swnj 		 * number wraparound.
2215075Swnj 		 */
2225088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
2237644Sroot 	/*
2247644Sroot 	 * If anything to send and we can send it all, set PUSH.
2257644Sroot 	 * (This will keep happy those implementations which only
22610143Ssam 	 * give data to the user when a buffer fills or a PUSH comes in.)
2277644Sroot 	 */
2287644Sroot 	if (len && off+len == so->so_snd.sb_cc)
2297644Sroot 		ti->ti_flags |= TH_PUSH;
2305075Swnj 
2315075Swnj 	/*
2325075Swnj 	 * Put TCP length in extended header, and then
2335075Swnj 	 * checksum extended header and data.
2345075Swnj 	 */
2355441Swnj 	if (len + optlen) {
2365441Swnj 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
2375441Swnj 		ti->ti_len = htons((u_short)ti->ti_len);
2385441Swnj 	}
2396162Ssam 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
2405075Swnj 
2415075Swnj 	/*
2427125Swnj 	 * In transmit state, time the transmission and arrange for
2437125Swnj 	 * the retransmit.  In persist state, reset persist time for
2447125Swnj 	 * next persist.
2455088Swnj 	 */
2467125Swnj 	if (tp->t_force == 0) {
2477125Swnj 		/*
2487146Swnj 		 * Advance snd_nxt over sequence space of this segment.
2497125Swnj 		 */
2507125Swnj 		if (flags & (TH_SYN|TH_FIN))
2517125Swnj 			tp->snd_nxt++;
2527125Swnj 		tp->snd_nxt += len;
2537149Swnj 		if (SEQ_GT(tp->snd_nxt, tp->snd_max))
2547149Swnj 			tp->snd_max = tp->snd_nxt;
2555088Swnj 
2567125Swnj 		/*
2577125Swnj 		 * Time this transmission if not a retransmission and
2587125Swnj 		 * not currently timing anything.
2597125Swnj 		 */
2607125Swnj 		if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
2617125Swnj 			tp->t_rtt = 1;
2627125Swnj 			tp->t_rtseq = tp->snd_nxt - len;
2637125Swnj 		}
2645088Swnj 
2657125Swnj 		/*
2667125Swnj 		 * Set retransmit timer if not currently set.
2677125Swnj 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
2687125Swnj 		 * Initialize shift counter which is used for exponential
2697125Swnj 		 * backoff of retransmit time.
2707125Swnj 		 */
2717125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
2727125Swnj 		    tp->snd_nxt != tp->snd_una) {
2737125Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
2747125Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
2757125Swnj 			tp->t_rtt = 0;
2767125Swnj 			tp->t_rxtshift = 0;
2777125Swnj 		}
2787125Swnj 		tp->t_timer[TCPT_PERSIST] = 0;
2797149Swnj 	} else {
2807149Swnj 		if (SEQ_GT(tp->snd_una+1, tp->snd_max))
2817149Swnj 			tp->snd_max = tp->snd_una+1;
2827146Swnj 	}
2835163Swnj 
2845163Swnj 	/*
2855268Sroot 	 * Trace.
2865268Sroot 	 */
2877146Swnj 	if (so->so_options & SO_DEBUG)
2885268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
2895268Sroot 
2905268Sroot 	/*
2915075Swnj 	 * Fill in IP length and desired time to live and
2925075Swnj 	 * send to IP level.
2935075Swnj 	 */
2945441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
2955075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
2967146Swnj 	if (error = ip_output(m, tp->t_ipopt, (so->so_options & SO_DONTROUTE) ?
2977146Swnj 	    &routetoif : &tp->t_inpcb->inp_route, 0))
2986505Ssam 		return (error);
2995075Swnj 
3005075Swnj 	/*
3015075Swnj 	 * Data sent (as far as we can tell).
3025075Swnj 	 * If this advertises a larger window than any other segment,
3035245Sroot 	 * then remember the size of the advertised window.
3045088Swnj 	 * Drop send for purpose of ACK requirements.
3055075Swnj 	 */
3065252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
3075075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
3085088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
3097125Swnj 	if (sendalot && tp->t_force == 0)
3107125Swnj 		goto again;
3116505Ssam 	return (0);
3124677Swnj }
3137125Swnj 
3147125Swnj tcp_setpersist(tp)
3157125Swnj 	register struct tcpcb *tp;
3167125Swnj {
3177125Swnj 
3187125Swnj 	if (tp->t_timer[TCPT_REXMT])
3197125Swnj 		panic("tcp_output REXMT");
3207125Swnj 	/*
3217125Swnj 	 * Start/restart persistance timer.
3227125Swnj 	 */
3237125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3247125Swnj 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
3257125Swnj 	    TCPTV_PERSMIN, TCPTV_MAX);
3267125Swnj 	tp->t_rxtshift++;
3277125Swnj 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
3287125Swnj 		tp->t_rxtshift = 0;
3297125Swnj }
330