⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_output.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
			TCP_SKB_CB(skb)->when = tcp_time_stamp;			if (!tcp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL)))				update_send_head(sk, tp, skb);			else				tcp_check_probe_timer(sk, tp);		}	} else {		/* Socket is locked, keep trying until memory is available. */		for (;;) {			skb = alloc_skb(MAX_TCP_HEADER, GFP_KERNEL);			if (skb)				break;			current->policy |= SCHED_YIELD;			schedule();		}		/* Reserve space for headers and prepare control bits. */		skb_reserve(skb, MAX_TCP_HEADER);		skb->csum = 0;		TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);		TCP_SKB_CB(skb)->sacked = 0;		/* FIN eats a sequence byte, write_seq advanced by tcp_send_skb(). */		TCP_SKB_CB(skb)->seq = tp->write_seq;		TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;		tcp_send_skb(sk, skb, 0, mss_now);		__tcp_push_pending_frames(sk, tp, mss_now, 1);	}}/* We get here when a process closes a file descriptor (either due to * an explicit close() or as a byproduct of exit()'ing) and there * was unread data in the receive queue.  This behavior is recommended * by draft-ietf-tcpimpl-prob-03.txt section 3.10.  -DaveM */void tcp_send_active_reset(struct sock *sk, int priority){	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);	struct sk_buff *skb;	/* NOTE: No TCP options attached and we never retransmit this. */	skb = alloc_skb(MAX_TCP_HEADER, priority);	if (!skb) {		NET_INC_STATS(TCPAbortFailed);		return;	}	/* Reserve space for headers and prepare control bits. */	skb_reserve(skb, MAX_TCP_HEADER);	skb->csum = 0;	TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_RST);	TCP_SKB_CB(skb)->sacked = 0;	/* Send it off. */	TCP_SKB_CB(skb)->seq = tcp_acceptable_seq(sk, tp);	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;	TCP_SKB_CB(skb)->when = tcp_time_stamp;	if (tcp_transmit_skb(sk, skb))		NET_INC_STATS(TCPAbortFailed);}/* WARNING: This routine must only be called when we have already sent * a SYN packet that crossed the incoming SYN that caused this routine * to get called. If this assumption fails then the initial rcv_wnd * and rcv_wscale values will not be correct. */int tcp_send_synack(struct sock *sk){	struct sk_buff* skb;	skb = skb_peek(&sk->write_queue);	if (skb == NULL || !(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_SYN)) {		printk(KERN_DEBUG "tcp_send_synack: wrong queue state\n");		return -EFAULT;	}	if (!(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_ACK)) {		if (skb_cloned(skb)) {			struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);			if (nskb == NULL)				return -ENOMEM;			__skb_unlink(skb, &sk->write_queue);			__skb_queue_head(&sk->write_queue, nskb);			tcp_free_skb(sk, skb);			tcp_charge_skb(sk, nskb);			skb = nskb;		}		TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;		TCP_ECN_send_synack(&sk->tp_pinfo.af_tcp, skb);	}	TCP_SKB_CB(skb)->when = tcp_time_stamp;	return tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));}/* * Prepare a SYN-ACK. */struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,				 struct open_request *req){	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);	struct tcphdr *th;	int tcp_header_size;	struct sk_buff *skb;	skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);	if (skb == NULL)		return NULL;	/* Reserve space for headers. */	skb_reserve(skb, MAX_TCP_HEADER);	skb->dst = dst_clone(dst);	tcp_header_size = (sizeof(struct tcphdr) + TCPOLEN_MSS +			   (req->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0) +			   (req->wscale_ok ? TCPOLEN_WSCALE_ALIGNED : 0) +			   /* SACK_PERM is in the place of NOP NOP of TS */			   ((req->sack_ok && !req->tstamp_ok) ? TCPOLEN_SACKPERM_ALIGNED : 0));	skb->h.th = th = (struct tcphdr *) skb_push(skb, tcp_header_size);	memset(th, 0, sizeof(struct tcphdr));	th->syn = 1;	th->ack = 1;	TCP_ECN_make_synack(req, th);	th->source = sk->sport;	th->dest = req->rmt_port;	TCP_SKB_CB(skb)->seq = req->snt_isn;	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;	th->seq = htonl(TCP_SKB_CB(skb)->seq);	th->ack_seq = htonl(req->rcv_isn + 1);	if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */		__u8 rcv_wscale; 		/* Set this up on the first call only */		req->window_clamp = tp->window_clamp ? : dst->window;		/* tcp_full_space because it is guaranteed to be the first packet */		tcp_select_initial_window(tcp_full_space(sk), 			dst->advmss - (req->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),			&req->rcv_wnd,			&req->window_clamp,			req->wscale_ok,			&rcv_wscale);		req->rcv_wscale = rcv_wscale; 	}	/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */	th->window = htons(req->rcv_wnd);	TCP_SKB_CB(skb)->when = tcp_time_stamp;	tcp_syn_build_options((__u32 *)(th + 1), dst->advmss, req->tstamp_ok,			      req->sack_ok, req->wscale_ok, req->rcv_wscale,			      TCP_SKB_CB(skb)->when,			      req->ts_recent);	skb->csum = 0;	th->doff = (tcp_header_size >> 2);	TCP_INC_STATS(TcpOutSegs);	return skb;}int tcp_connect(struct sock *sk, struct sk_buff *buff){	struct dst_entry *dst = __sk_dst_get(sk);	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);	/* Reserve space for headers. */	skb_reserve(buff, MAX_TCP_HEADER);	/* We'll fix this up when we get a response from the other end.	 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.	 */	tp->tcp_header_len = sizeof(struct tcphdr) +		(sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);	/* If user gave his TCP_MAXSEG, record it to clamp */	if (tp->user_mss)		tp->mss_clamp = tp->user_mss;	tp->max_window = 0;	tcp_sync_mss(sk, dst->pmtu);	if (!tp->window_clamp)		tp->window_clamp = dst->window;	tp->advmss = dst->advmss;	tcp_initialize_rcv_mss(sk);	tcp_select_initial_window(tcp_full_space(sk),				  tp->advmss - (tp->ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),				  &tp->rcv_wnd,				  &tp->window_clamp,				  sysctl_tcp_window_scaling,				  &tp->rcv_wscale);	tp->rcv_ssthresh = tp->rcv_wnd;	/* Socket identity change complete, no longer	 * in TCP_CLOSE, so enter ourselves into the	 * hash tables.	 */	tcp_set_state(sk,TCP_SYN_SENT);	if (tp->af_specific->hash_connecting(sk))		goto err_out;	sk->err = 0;	sk->done = 0;	tp->snd_wnd = 0;	tcp_init_wl(tp, tp->write_seq, 0);	tp->snd_una = tp->write_seq;	tp->snd_sml = tp->write_seq;	tp->rcv_nxt = 0;	tp->rcv_wup = 0;	tp->copied_seq = 0;	tp->rto = TCP_TIMEOUT_INIT;	tp->retransmits = 0;	tcp_clear_retrans(tp);	TCP_SKB_CB(buff)->flags = TCPCB_FLAG_SYN;	TCP_ECN_send_syn(tp, buff);	TCP_SKB_CB(buff)->sacked = 0;	buff->csum = 0;	TCP_SKB_CB(buff)->seq = tp->write_seq++;	TCP_SKB_CB(buff)->end_seq = tp->write_seq;	tp->snd_nxt = tp->write_seq;	tp->pushed_seq = tp->write_seq;	/* Send it off. */	TCP_SKB_CB(buff)->when = tcp_time_stamp;	tp->retrans_stamp = TCP_SKB_CB(buff)->when;	__skb_queue_tail(&sk->write_queue, buff);	tcp_charge_skb(sk, buff);	tp->packets_out++;	tcp_transmit_skb(sk, skb_clone(buff, GFP_KERNEL));	TCP_INC_STATS(TcpActiveOpens);	/* Timer for repeating the SYN until an answer. */	tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);	return 0;err_out:	tcp_set_state(sk,TCP_CLOSE);	kfree_skb(buff);	return -EADDRNOTAVAIL;}/* Send out a delayed ack, the caller does the policy checking * to see if we should even be here.  See tcp_input.c:tcp_ack_snd_check() * for details. */void tcp_send_delayed_ack(struct sock *sk){	struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;	int ato = tp->ack.ato;	unsigned long timeout;	if (ato > TCP_DELACK_MIN) {		int max_ato = HZ/2;		if (tp->ack.pingpong || (tp->ack.pending&TCP_ACK_PUSHED))			max_ato = TCP_DELACK_MAX;		/* Slow path, intersegment interval is "high". */		/* If some rtt estimate is known, use it to bound delayed ack.		 * Do not use tp->rto here, use results of rtt measurements		 * directly.		 */		if (tp->srtt) {			int rtt = max(tp->srtt>>3, TCP_DELACK_MIN);			if (rtt < max_ato)				max_ato = rtt;		}		ato = min(ato, max_ato);	}	/* Stay within the limit we were given */	timeout = jiffies + ato;	/* Use new timeout only if there wasn't a older one earlier. */	if (tp->ack.pending&TCP_ACK_TIMER) {		/* If delack timer was blocked or is about to expire,		 * send ACK now.		 */		if (tp->ack.blocked || time_before_eq(tp->ack.timeout, jiffies+(ato>>2))) {			tcp_send_ack(sk);			return;		}		if (!time_before(timeout, tp->ack.timeout))			timeout = tp->ack.timeout;	}	tp->ack.pending |= TCP_ACK_SCHED|TCP_ACK_TIMER;	tp->ack.timeout = timeout;	if (!mod_timer(&tp->delack_timer, timeout))		sock_hold(sk);#ifdef TCP_FORMAL_WINDOW	/* Explanation. Header prediction path does not handle	 * case of zero window. If we send ACK immediately, pred_flags	 * are reset when sending ACK. If rcv_nxt is advanced and	 * ack is not sent, than delayed ack is scheduled.	 * Hence, it is the best place to check for zero window.	 */	if (tp->pred_flags) {		if (tcp_receive_window(tp) == 0)			tp->pred_flags = 0;	} else {		if (skb_queue_len(&tp->out_of_order_queue) == 0 &&		    !tp->urg_data)			tcp_fast_path_on(tp);	}#endif}/* This routine sends an ack and also updates the window. */void tcp_send_ack(struct sock *sk){	/* If we have been reset, we may not send again. */	if(sk->state != TCP_CLOSE) {		struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);		struct sk_buff *buff;		/* We are not putting this on the write queue, so		 * tcp_transmit_skb() will set the ownership to this		 * sock.		 */		buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);		if (buff == NULL) {			tcp_schedule_ack(tp);			tp->ack.ato = TCP_ATO_MIN;			tcp_reset_xmit_timer(sk, TCP_TIME_DACK, TCP_DELACK_MAX);			return;		}		/* Reserve space for headers and prepare control bits. */		skb_reserve(buff, MAX_TCP_HEADER);		buff->csum = 0;		TCP_SKB_CB(buff)->flags = TCPCB_FLAG_ACK;		TCP_SKB_CB(buff)->sacked = 0;		/* Send it off, this clears delayed acks for us. */		TCP_SKB_CB(buff)->seq = TCP_SKB_CB(buff)->end_seq = tcp_acceptable_seq(sk, tp);		TCP_SKB_CB(buff)->when = tcp_time_stamp;		tcp_transmit_skb(sk, buff);	}}/* This routine sends a packet with an out of date sequence * number. It assumes the other end will try to ack it. * * Question: what should we make while urgent mode? * 4.4BSD forces sending single byte of data. We cannot send * out of window data, because we have SND.NXT==SND.MAX... * * Current solution: to send TWO zero-length segments in urgent mode: * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is * out-of-date with SND.UNA-1 to probe window. */static int tcp_xmit_probe_skb(struct sock *sk, int urgent){	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);	struct sk_buff *skb;	/* We don't queue it, tcp_transmit_skb() sets ownership. */	skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);	if (skb == NULL) 		return -1;	/* Reserve space for headers and set control bits. */	skb_reserve(skb, MAX_TCP_HEADER);	skb->csum = 0;	TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;	TCP_SKB_CB(skb)->sacked = urgent;	/* Use a previous sequence.  This should cause the other	 * end to send an ack.  Don't queue or clone SKB, just	 * send it.	 */	TCP_SKB_CB(skb)->seq = urgent ? tp->snd_una : tp->snd_una - 1;	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;	TCP_SKB_CB(skb)->when = tcp_time_stamp;	return tcp_transmit_skb(sk, skb);}int tcp_write_wakeup(struct sock *sk){	if (sk->state != TCP_CLOSE) {		struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);		struct sk_buff *skb;		if ((skb = tp->send_head) != NULL &&		    before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)) {			int err;			int mss = tcp_current_mss(sk);			int seg_size = tp->snd_una+tp->snd_wnd-TCP_SKB_CB(skb)->seq;			if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))				tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;			/* We are probing the opening of a window			 * but the window size is != 0			 * must have been a result SWS avoidance ( sender )			 */			if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||			    skb->len > mss) {				seg_size = min(seg_size, mss);				TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;				if (tcp_fragment(sk, skb, seg_size))					return -1;			}			TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;			TCP_SKB_CB(skb)->when = tcp_time_stamp;			err = tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));			if (!err) {				update_send_head(sk, tp, skb);			}			return err;		} else {			if (tp->urg_mode &&			    between(tp->snd_up, tp->snd_una+1, tp->snd_una+0xFFFF))				tcp_xmit_probe_skb(sk, TCPCB_URG);			return tcp_xmit_probe_skb(sk, 0);		}	}	return -1;}/* A window probe timeout has occurred.  If window is not closed send * a partial packet else a zero probe. */void tcp_send_probe0(struct sock *sk){	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);	int err;	err = tcp_write_wakeup(sk);	if (tp->packets_out || !tp->send_head) {		/* Cancel probe timer, if it is not required. */		tp->probes_out = 0;		tp->backoff = 0;		return;	}	if (err <= 0) {		tp->backoff++;		tp->probes_out++;		tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0, 				      min(tp->rto << tp->backoff, TCP_RTO_MAX));	} else {		/* If packet was not sent due to local congestion,		 * do not backoff and do not remember probes_out.		 * Let local senders to fight for local resources.		 *		 * Use accumulated backoff yet.		 */		if (!tp->probes_out)			tp->probes_out=1;		tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0, 				      min(tp->rto << tp->backoff, TCP_RESOURCE_PROBE_INTERVAL));	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -