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

📄 ccid3.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  net/dccp/ccids/ccid3.c * *  Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand. *  Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz> * *  An implementation of the DCCP protocol * *  This code has been developed by the University of Waikato WAND *  research group. For further information please see http://www.wand.net.nz/ * *  This code also uses code from Lulea University, rereleased as GPL by its *  authors: *  Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon * *  Changes to meet Linux coding standards, to make it meet latest ccid3 draft *  and to make it work as a loadable module in the DCCP stack written by *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>. * *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br> * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "../ccid.h"#include "../dccp.h"#include "lib/packet_history.h"#include "lib/loss_interval.h"#include "lib/tfrc.h"#include "ccid3.h"#include <asm/unaligned.h>#ifdef CONFIG_IP_DCCP_CCID3_DEBUGstatic int ccid3_debug;#define ccid3_pr_debug(format, a...)	DCCP_PR_DEBUG(ccid3_debug, format, ##a)#else#define ccid3_pr_debug(format, a...)#endifstatic struct dccp_tx_hist *ccid3_tx_hist;static struct dccp_rx_hist *ccid3_rx_hist;/* *	Transmitter Half-Connection Routines */#ifdef CONFIG_IP_DCCP_CCID3_DEBUGstatic const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state){	static char *ccid3_state_names[] = {	[TFRC_SSTATE_NO_SENT]  = "NO_SENT",	[TFRC_SSTATE_NO_FBACK] = "NO_FBACK",	[TFRC_SSTATE_FBACK]    = "FBACK",	[TFRC_SSTATE_TERM]     = "TERM",	};	return ccid3_state_names[state];}#endifstatic void ccid3_hc_tx_set_state(struct sock *sk,				  enum ccid3_hc_tx_states state){	struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);	enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;	ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",		       dccp_role(sk), sk, ccid3_tx_state_name(oldstate),		       ccid3_tx_state_name(state));	WARN_ON(state == oldstate);	hctx->ccid3hctx_state = state;}/* * Compute the initial sending rate X_init according to RFC 3390: *	w_init   =    min(4 * MSS, max(2 * MSS, 4380 bytes)) *	X_init   =    w_init / RTT * For consistency with other parts of the code, X_init is scaled by 2^6. */static inline u64 rfc3390_initial_rate(struct sock *sk){	const struct dccp_sock *dp = dccp_sk(sk);	const __u32 w_init = min(4 * dp->dccps_mss_cache,				 max(2 * dp->dccps_mss_cache, 4380U));	return scaled_div(w_init << 6, ccid3_hc_tx_sk(sk)->ccid3hctx_rtt);}/* * Recalculate t_ipi and delta (should be called whenever X changes) */static inline void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hctx){	/* Calculate new t_ipi = s / X_inst (X_inst is in 64 * bytes/second) */	hctx->ccid3hctx_t_ipi = scaled_div32(((u64)hctx->ccid3hctx_s) << 6,					     hctx->ccid3hctx_x);	/* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */	hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,					   TFRC_OPSYS_HALF_TIME_GRAN);	ccid3_pr_debug("t_ipi=%u, delta=%u, s=%u, X=%u\n",		       hctx->ccid3hctx_t_ipi, hctx->ccid3hctx_delta,		       hctx->ccid3hctx_s, (unsigned)(hctx->ccid3hctx_x >> 6));}/** * ccid3_hc_tx_update_x  -  Update allowed sending rate X * @stamp: most recent time if available - can be left NULL. * This function tracks draft rfc3448bis, check there for latest details. * * Note: X and X_recv are both stored in units of 64 * bytes/second, to support *       fine-grained resolution of sending rates. This requires scaling by 2^6 *       throughout the code. Only X_calc is unscaled (in bytes/second). * */static void ccid3_hc_tx_update_x(struct sock *sk, ktime_t *stamp){	struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);	__u64 min_rate = 2 * hctx->ccid3hctx_x_recv;	const  __u64 old_x = hctx->ccid3hctx_x;	ktime_t now = stamp? *stamp : ktime_get_real();	/*	 * Handle IDLE periods: do not reduce below RFC3390 initial sending rate	 * when idling [RFC 4342, 5.1]. See also draft-ietf-dccp-rfc3448bis.	 * For consistency with X and X_recv, min_rate is also scaled by 2^6.	 */	if (unlikely(hctx->ccid3hctx_idle)) {		min_rate = rfc3390_initial_rate(sk);		min_rate = max(min_rate, 2 * hctx->ccid3hctx_x_recv);	}	if (hctx->ccid3hctx_p > 0) {		hctx->ccid3hctx_x = min(((__u64)hctx->ccid3hctx_x_calc) << 6,					min_rate);		hctx->ccid3hctx_x = max(hctx->ccid3hctx_x,					(((__u64)hctx->ccid3hctx_s) << 6) /								TFRC_T_MBI);	} else if (ktime_us_delta(now, hctx->ccid3hctx_t_ld)				- (s64)hctx->ccid3hctx_rtt >= 0) {		hctx->ccid3hctx_x =			max(min(2 * hctx->ccid3hctx_x, min_rate),			    scaled_div(((__u64)hctx->ccid3hctx_s) << 6,				       hctx->ccid3hctx_rtt));		hctx->ccid3hctx_t_ld = now;	}	if (hctx->ccid3hctx_x != old_x) {		ccid3_pr_debug("X_prev=%u, X_now=%u, X_calc=%u, "			       "X_recv=%u\n", (unsigned)(old_x >> 6),			       (unsigned)(hctx->ccid3hctx_x >> 6),			       hctx->ccid3hctx_x_calc,			       (unsigned)(hctx->ccid3hctx_x_recv >> 6));		ccid3_update_send_interval(hctx);	}}/* *	Track the mean packet size `s' (cf. RFC 4342, 5.3 and  RFC 3448, 4.1) *	@len: DCCP packet payload size in bytes */static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hctx, int len){	const u16 old_s = hctx->ccid3hctx_s;	hctx->ccid3hctx_s = old_s == 0 ? len : (9 * old_s + len) / 10;	if (hctx->ccid3hctx_s != old_s)		ccid3_update_send_interval(hctx);}/* *	Update Window Counter using the algorithm from [RFC 4342, 8.1]. *	The algorithm is not applicable if RTT < 4 microseconds. */static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hctx,						ktime_t now){	u32 quarter_rtts;	if (unlikely(hctx->ccid3hctx_rtt < 4))	/* avoid divide-by-zero */		return;	quarter_rtts = ktime_us_delta(now, hctx->ccid3hctx_t_last_win_count);	quarter_rtts /= hctx->ccid3hctx_rtt / 4;	if (quarter_rtts > 0) {		hctx->ccid3hctx_t_last_win_count = now;		hctx->ccid3hctx_last_win_count	+= min_t(u32, quarter_rtts, 5);		hctx->ccid3hctx_last_win_count	&= 0xF;		/* mod 16 */	}}static void ccid3_hc_tx_no_feedback_timer(unsigned long data){	struct sock *sk = (struct sock *)data;	struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);	unsigned long t_nfb = USEC_PER_SEC / 5;	bh_lock_sock(sk);	if (sock_owned_by_user(sk)) {		/* Try again later. */		/* XXX: set some sensible MIB */		goto restart_timer;	}	ccid3_pr_debug("%s(%p, state=%s) - entry \n", dccp_role(sk), sk,		       ccid3_tx_state_name(hctx->ccid3hctx_state));	hctx->ccid3hctx_idle = 1;	switch (hctx->ccid3hctx_state) {	case TFRC_SSTATE_NO_FBACK:		/* RFC 3448, 4.4: Halve send rate directly */		hctx->ccid3hctx_x = max(hctx->ccid3hctx_x / 2,					(((__u64)hctx->ccid3hctx_s) << 6) /								    TFRC_T_MBI);		ccid3_pr_debug("%s(%p, state=%s), updated tx rate to %u "			       "bytes/s\n", dccp_role(sk), sk,			       ccid3_tx_state_name(hctx->ccid3hctx_state),			       (unsigned)(hctx->ccid3hctx_x >> 6));		/* The value of R is still undefined and so we can not recompute		 * the timeout value. Keep initial value as per [RFC 4342, 5]. */		t_nfb = TFRC_INITIAL_TIMEOUT;		ccid3_update_send_interval(hctx);		break;	case TFRC_SSTATE_FBACK:		/*		 *  Modify the cached value of X_recv [RFC 3448, 4.4]		 *		 *  If (p == 0 || X_calc > 2 * X_recv)		 *    X_recv = max(X_recv / 2, s / (2 * t_mbi));		 *  Else		 *    X_recv = X_calc / 4;		 *		 *  Note that X_recv is scaled by 2^6 while X_calc is not		 */		BUG_ON(hctx->ccid3hctx_p && !hctx->ccid3hctx_x_calc);		if (hctx->ccid3hctx_p == 0 ||		    (hctx->ccid3hctx_x_calc > (hctx->ccid3hctx_x_recv >> 5))) {			hctx->ccid3hctx_x_recv =				max(hctx->ccid3hctx_x_recv / 2,				    (((__u64)hctx->ccid3hctx_s) << 6) /							      (2 * TFRC_T_MBI));		} else {			hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc;			hctx->ccid3hctx_x_recv <<= 4;		}		/* Now recalculate X [RFC 3448, 4.3, step (4)] */		ccid3_hc_tx_update_x(sk, NULL);		/*		 * Schedule no feedback timer to expire in		 * max(t_RTO, 2 * s/X)  =  max(t_RTO, 2 * t_ipi)		 * See comments in packet_recv() regarding the value of t_RTO.		 */		t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);		break;	case TFRC_SSTATE_NO_SENT:		DCCP_BUG("%s(%p) - Illegal state NO_SENT", dccp_role(sk), sk);		/* fall through */	case TFRC_SSTATE_TERM:		goto out;	}restart_timer:	sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,			   jiffies + usecs_to_jiffies(t_nfb));out:	bh_unlock_sock(sk);	sock_put(sk);}/* * returns *   > 0: delay (in msecs) that should pass before actually sending *   = 0: can send immediately *   < 0: error condition; do not send packet */static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb){	struct dccp_sock *dp = dccp_sk(sk);	struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);	ktime_t now = ktime_get_real();	s64 delay;	/*	 * This function is called only for Data and DataAck packets. Sending	 * zero-sized Data(Ack)s is theoretically possible, but for congestion	 * control this case is pathological - ignore it.	 */	if (unlikely(skb->len == 0))		return -EBADMSG;	switch (hctx->ccid3hctx_state) {	case TFRC_SSTATE_NO_SENT:		sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,			       (jiffies +				usecs_to_jiffies(TFRC_INITIAL_TIMEOUT)));		hctx->ccid3hctx_last_win_count	 = 0;		hctx->ccid3hctx_t_last_win_count = now;		/* Set t_0 for initial packet */		hctx->ccid3hctx_t_nom = now;		hctx->ccid3hctx_s = skb->len;		/*		 * Use initial RTT sample when available: recommended by erratum		 * to RFC 4342. This implements the initialisation procedure of		 * draft rfc3448bis, section 4.2. Remember, X is scaled by 2^6.		 */		if (dp->dccps_syn_rtt) {			ccid3_pr_debug("SYN RTT = %uus\n", dp->dccps_syn_rtt);			hctx->ccid3hctx_rtt  = dp->dccps_syn_rtt;			hctx->ccid3hctx_x    = rfc3390_initial_rate(sk);			hctx->ccid3hctx_t_ld = now;		} else {			/* Sender does not have RTT sample: X = MSS/second */			hctx->ccid3hctx_x = dp->dccps_mss_cache;			hctx->ccid3hctx_x <<= 6;		}		ccid3_update_send_interval(hctx);		ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);		break;	case TFRC_SSTATE_NO_FBACK:	case TFRC_SSTATE_FBACK:		delay = ktime_us_delta(hctx->ccid3hctx_t_nom, now);		ccid3_pr_debug("delay=%ld\n", (long)delay);		/*		 *	Scheduling of packet transmissions [RFC 3448, 4.6]		 *		 * if (t_now > t_nom - delta)		 *       // send the packet now		 * else		 *       // send the packet in (t_nom - t_now) milliseconds.		 */		if (delay - (s64)hctx->ccid3hctx_delta >= 1000)			return (u32)delay / 1000L;		ccid3_hc_tx_update_win_count(hctx, now);		break;	case TFRC_SSTATE_TERM:		DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);		return -EINVAL;	}

⌨️ 快捷键说明

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