xref: /csrg-svn/sys/netinet/tcp_output.c (revision 17063)
1*17063Sbloom /*	tcp_output.c	6.3	84/08/29	*/
24677Swnj 
3*17063Sbloom #include "param.h"
4*17063Sbloom #include "systm.h"
5*17063Sbloom #include "mbuf.h"
6*17063Sbloom #include "protosw.h"
7*17063Sbloom #include "socket.h"
8*17063Sbloom #include "socketvar.h"
9*17063Sbloom #include "errno.h"
1010895Ssam 
1110895Ssam #include "../net/route.h"
1210895Ssam 
13*17063Sbloom #include "in.h"
14*17063Sbloom #include "in_pcb.h"
15*17063Sbloom #include "in_systm.h"
16*17063Sbloom #include "ip.h"
17*17063Sbloom #include "ip_var.h"
18*17063Sbloom #include "tcp.h"
195088Swnj #define	TCPOUTFLAGS
20*17063Sbloom #include "tcp_fsm.h"
21*17063Sbloom #include "tcp_seq.h"
22*17063Sbloom #include "tcp_timer.h"
23*17063Sbloom #include "tcp_var.h"
24*17063Sbloom #include "tcpip.h"
25*17063Sbloom #include "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 
485075Swnj 	/*
496279Swnj 	 * Determine length of data that should be transmitted,
505088Swnj 	 * and flags that will be used.
515088Swnj 	 * If there is some data or critical controls (SYN, RST)
525088Swnj 	 * to send, then transmit; otherwise, investigate further.
535075Swnj 	 */
547125Swnj again:
557125Swnj 	sendalot = 0;
565075Swnj 	off = tp->snd_nxt - tp->snd_una;
575163Swnj 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
585285Sroot 	if (len < 0)
596505Ssam 		return (0);	/* ??? */	/* past FIN */
607125Swnj 	if (len > tp->t_maxseg) {
615088Swnj 		len = tp->t_maxseg;
627125Swnj 		sendalot = 1;
637125Swnj 	}
646279Swnj 
655088Swnj 	flags = tcp_outflags[tp->t_state];
665299Sroot 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
675163Swnj 		flags &= ~TH_FIN;
686279Swnj 	if (flags & (TH_SYN|TH_RST|TH_FIN))
695075Swnj 		goto send;
706279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
716279Swnj 		goto send;
724678Swnj 
735075Swnj 	/*
746279Swnj 	 * Sender silly window avoidance.  If can send all data,
756279Swnj 	 * a maximum segment, at least 1/4 of window do it,
766279Swnj 	 * or are forced, do it; otherwise don't bother.
776279Swnj 	 */
786279Swnj 	if (len) {
796279Swnj 		if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc)
806279Swnj 			goto send;
816279Swnj 		if (len * 4 >= tp->snd_wnd)		/* a lot */
826279Swnj 			goto send;
836279Swnj 		if (tp->t_force)
846279Swnj 			goto send;
856279Swnj 	}
866279Swnj 
876279Swnj 	/*
885285Sroot 	 * Send if we owe peer an ACK.
895075Swnj 	 */
905441Swnj 	if (tp->t_flags&TF_ACKNOW)
915075Swnj 		goto send;
924678Swnj 
935441Swnj 
945441Swnj 	/*
955075Swnj 	 * Calculate available window in i, and also amount
965075Swnj 	 * of window known to peer (as advertised window less
975075Swnj 	 * next expected input.)  If this is 35% or more of the
985075Swnj 	 * maximum possible window, then want to send a segment to peer.
995075Swnj 	 */
1005088Swnj 	win = sbspace(&so->so_rcv);
1015088Swnj 	if (win > 0 &&
1025088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
1035075Swnj 		goto send;
1044678Swnj 
1055075Swnj 	/*
1067125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1077125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1087125Swnj 	 * updates.  The three ``states'' for the output side are:
1097125Swnj 	 *	idle			not doing retransmits or persists
1107125Swnj 	 *	persisting		to move a zero window
1117125Swnj 	 *	(re)transmitting	and thereby not persisting
1127125Swnj 	 *
1137125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1147125Swnj 	 *	is set when we are in persist state.
1157125Swnj 	 * tp->t_force
1167125Swnj 	 *	is set when we are called to send a persist packet.
1177125Swnj 	 * tp->t_timer[TCPT_REXMT]
1187125Swnj 	 *	is set when we are retransmitting
1197125Swnj 	 * The output side is idle when both timers are zero.
1207125Swnj 	 *
1217125Swnj 	 * If send window is closed, there is data to transmit, and no
1227125Swnj 	 * retransmit or persist is pending, then go to persist state,
1237125Swnj 	 * arranging to force out a byte to get more current window information
1247125Swnj 	 * if nothing happens soon.
1257125Swnj 	 */
1267125Swnj 	if (tp->snd_wnd == 0 && so->so_snd.sb_cc &&
1277125Swnj 	    tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) {
1287125Swnj 		tp->t_rxtshift = 0;
1297125Swnj 		tcp_setpersist(tp);
1307125Swnj 	}
1317125Swnj 
1327125Swnj 	/*
1335075Swnj 	 * No reason to send a segment, just return.
1345075Swnj 	 */
1355110Swnj 	return (0);
1364678Swnj 
1375075Swnj send:
1385075Swnj 	/*
1395075Swnj 	 * Grab a header mbuf, attaching a copy of data to
1405075Swnj 	 * be transmitted, and initialize the header from
1415075Swnj 	 * the template for sends on this connection.
1425075Swnj 	 */
14311720Ssam 	MGET(m, M_DONTWAIT, MT_HEADER);
14411720Ssam 	if (m == NULL)
1456505Ssam 		return (ENOBUFS);
1465245Sroot 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
1474885Swnj 	m->m_len = sizeof (struct tcpiphdr);
1485075Swnj 	if (len) {
1495075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
1505075Swnj 		if (m->m_next == 0)
1515075Swnj 			len = 0;
1525075Swnj 	}
1535075Swnj 	ti = mtod(m, struct tcpiphdr *);
1545075Swnj 	if (tp->t_template == 0)
1555075Swnj 		panic("tcp_output");
1565110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
1575075Swnj 
1585075Swnj 	/*
1595075Swnj 	 * Fill in fields, remembering maximum advertised
1605075Swnj 	 * window for use in delaying messages about window sizes.
1615075Swnj 	 */
1625245Sroot 	ti->ti_seq = tp->snd_nxt;
1635245Sroot 	ti->ti_ack = tp->rcv_nxt;
1645245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
1655245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1665441Swnj 	/*
1675441Swnj 	 * Before ESTABLISHED, force sending of initial options
1685441Swnj 	 * unless TCP set to not do any options.
1695441Swnj 	 */
1705441Swnj 	if (tp->t_state < TCPS_ESTABLISHED) {
1715441Swnj 		if (tp->t_flags&TF_NOOPT)
1725441Swnj 			goto noopt;
1735441Swnj 		opt = tcp_initopt;
1745441Swnj 		optlen = sizeof (tcp_initopt);
1758314Sroot 		*(u_short *)(opt + 2) = MIN(so->so_rcv.sb_hiwat / 2, 1024);
1765441Swnj 		*(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2));
1775441Swnj 	} else {
1785441Swnj 		if (tp->t_tcpopt == 0)
1795441Swnj 			goto noopt;
1805441Swnj 		opt = mtod(tp->t_tcpopt, u_char *);
1815441Swnj 		optlen = tp->t_tcpopt->m_len;
1825441Swnj 	}
1838314Sroot 	if (opt) {
1845110Swnj 		m0 = m->m_next;
1859643Ssam 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
1865088Swnj 		if (m->m_next == 0) {
1875088Swnj 			(void) m_free(m);
1885441Swnj 			m_freem(m0);
1896505Ssam 			return (ENOBUFS);
1905088Swnj 		}
1915088Swnj 		m->m_next->m_next = m0;
1925441Swnj 		m0 = m->m_next;
1935441Swnj 		m0->m_len = optlen;
1946162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
1955441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
1965441Swnj 		while (m0->m_len & 0x3) {
1975441Swnj 			*opt++ = TCPOPT_EOL;
1985441Swnj 			m0->m_len++;
1995441Swnj 		}
2005441Swnj 		optlen = m0->m_len;
2015441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2025088Swnj 	}
2035441Swnj noopt:
2045088Swnj 	ti->ti_flags = flags;
2055075Swnj 	win = sbspace(&so->so_rcv);
2066279Swnj 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
2076279Swnj 		win = 0;
2085075Swnj 	if (win > 0)
2095110Swnj 		ti->ti_win = htons((u_short)win);
2105088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2115420Swnj 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
2125420Swnj 		ti->ti_urp = htons(ti->ti_urp);
2135075Swnj 		ti->ti_flags |= TH_URG;
2145075Swnj 	} else
2155075Swnj 		/*
2165075Swnj 		 * If no urgent pointer to send, then we pull
2175075Swnj 		 * the urgent pointer to the left edge of the send window
2185075Swnj 		 * so that it doesn't drift into the send window on sequence
2195075Swnj 		 * number wraparound.
2205075Swnj 		 */
2215088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
2227644Sroot 	/*
2237644Sroot 	 * If anything to send and we can send it all, set PUSH.
2247644Sroot 	 * (This will keep happy those implementations which only
22510143Ssam 	 * give data to the user when a buffer fills or a PUSH comes in.)
2267644Sroot 	 */
2277644Sroot 	if (len && off+len == so->so_snd.sb_cc)
2287644Sroot 		ti->ti_flags |= TH_PUSH;
2295075Swnj 
2305075Swnj 	/*
2315075Swnj 	 * Put TCP length in extended header, and then
2325075Swnj 	 * checksum extended header and data.
2335075Swnj 	 */
2345441Swnj 	if (len + optlen) {
2355441Swnj 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
2365441Swnj 		ti->ti_len = htons((u_short)ti->ti_len);
2375441Swnj 	}
2386162Ssam 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
2395075Swnj 
2405075Swnj 	/*
2417125Swnj 	 * In transmit state, time the transmission and arrange for
2427125Swnj 	 * the retransmit.  In persist state, reset persist time for
2437125Swnj 	 * next persist.
2445088Swnj 	 */
2457125Swnj 	if (tp->t_force == 0) {
2467125Swnj 		/*
2477146Swnj 		 * Advance snd_nxt over sequence space of this segment.
2487125Swnj 		 */
2497125Swnj 		if (flags & (TH_SYN|TH_FIN))
2507125Swnj 			tp->snd_nxt++;
2517125Swnj 		tp->snd_nxt += len;
25215385Ssam 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
2537149Swnj 			tp->snd_max = tp->snd_nxt;
25415385Ssam 			/*
25515385Ssam 			 * Time this transmission if not a retransmission and
25615385Ssam 			 * not currently timing anything.
25715385Ssam 			 */
25815385Ssam 			if (tp->t_rtt == 0) {
25915385Ssam 				tp->t_rtt = 1;
26015385Ssam 				tp->t_rtseq = tp->snd_nxt - len;
26115385Ssam 			}
2627125Swnj 		}
2635088Swnj 
2647125Swnj 		/*
2657125Swnj 		 * Set retransmit timer if not currently set.
2667125Swnj 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
2677125Swnj 		 * Initialize shift counter which is used for exponential
2687125Swnj 		 * backoff of retransmit time.
2697125Swnj 		 */
2707125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
2717125Swnj 		    tp->snd_nxt != tp->snd_una) {
2727125Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
2737125Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
2747125Swnj 			tp->t_rtt = 0;
2757125Swnj 			tp->t_rxtshift = 0;
2767125Swnj 		}
2777125Swnj 		tp->t_timer[TCPT_PERSIST] = 0;
2787149Swnj 	} else {
2797149Swnj 		if (SEQ_GT(tp->snd_una+1, tp->snd_max))
2807149Swnj 			tp->snd_max = tp->snd_una+1;
2817146Swnj 	}
2825163Swnj 
2835163Swnj 	/*
2845268Sroot 	 * Trace.
2855268Sroot 	 */
2867146Swnj 	if (so->so_options & SO_DEBUG)
2875268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
2885268Sroot 
2895268Sroot 	/*
2905075Swnj 	 * Fill in IP length and desired time to live and
2915075Swnj 	 * send to IP level.
2925075Swnj 	 */
2935441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
2945075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
29512418Ssam 	if (so->so_options & SO_DONTROUTE)
29612765Ssam 		error =
29712765Ssam 		   ip_output(m, tp->t_ipopt, (struct route *)0, IP_ROUTETOIF);
29812418Ssam 	else
29912418Ssam 		error = ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0);
30012418Ssam 	if (error)
3016505Ssam 		return (error);
3025075Swnj 
3035075Swnj 	/*
3045075Swnj 	 * Data sent (as far as we can tell).
3055075Swnj 	 * If this advertises a larger window than any other segment,
3065245Sroot 	 * then remember the size of the advertised window.
3075088Swnj 	 * Drop send for purpose of ACK requirements.
3085075Swnj 	 */
3095252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
3105075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
3115088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
3127125Swnj 	if (sendalot && tp->t_force == 0)
3137125Swnj 		goto again;
3146505Ssam 	return (0);
3154677Swnj }
3167125Swnj 
3177125Swnj tcp_setpersist(tp)
3187125Swnj 	register struct tcpcb *tp;
3197125Swnj {
3207125Swnj 
3217125Swnj 	if (tp->t_timer[TCPT_REXMT])
3227125Swnj 		panic("tcp_output REXMT");
3237125Swnj 	/*
3247125Swnj 	 * Start/restart persistance timer.
3257125Swnj 	 */
3267125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3277125Swnj 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
3287125Swnj 	    TCPTV_PERSMIN, TCPTV_MAX);
3297125Swnj 	tp->t_rxtshift++;
3307125Swnj 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
3317125Swnj 		tp->t_rxtshift = 0;
3327125Swnj }
333