Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * TCP Veno congestion control
0004  *
0005  * This is based on the congestion detection/avoidance scheme described in
0006  *    C. P. Fu, S. C. Liew.
0007  *    "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks."
0008  *    IEEE Journal on Selected Areas in Communication,
0009  *    Feb. 2003.
0010  *  See https://www.ie.cuhk.edu.hk/fileadmin/staff_upload/soung/Journal/J3.pdf
0011  */
0012 
0013 #include <linux/mm.h>
0014 #include <linux/module.h>
0015 #include <linux/skbuff.h>
0016 #include <linux/inet_diag.h>
0017 
0018 #include <net/tcp.h>
0019 
0020 /* Default values of the Veno variables, in fixed-point representation
0021  * with V_PARAM_SHIFT bits to the right of the binary point.
0022  */
0023 #define V_PARAM_SHIFT 1
0024 static const int beta = 3 << V_PARAM_SHIFT;
0025 
0026 /* Veno variables */
0027 struct veno {
0028     u8 doing_veno_now;  /* if true, do veno for this rtt */
0029     u16 cntrtt;     /* # of rtts measured within last rtt */
0030     u32 minrtt;     /* min of rtts measured within last rtt (in usec) */
0031     u32 basertt;        /* the min of all Veno rtt measurements seen (in usec) */
0032     u32 inc;        /* decide whether to increase cwnd */
0033     u32 diff;       /* calculate the diff rate */
0034 };
0035 
0036 /* There are several situations when we must "re-start" Veno:
0037  *
0038  *  o when a connection is established
0039  *  o after an RTO
0040  *  o after fast recovery
0041  *  o when we send a packet and there is no outstanding
0042  *    unacknowledged data (restarting an idle connection)
0043  *
0044  */
0045 static inline void veno_enable(struct sock *sk)
0046 {
0047     struct veno *veno = inet_csk_ca(sk);
0048 
0049     /* turn on Veno */
0050     veno->doing_veno_now = 1;
0051 
0052     veno->minrtt = 0x7fffffff;
0053 }
0054 
0055 static inline void veno_disable(struct sock *sk)
0056 {
0057     struct veno *veno = inet_csk_ca(sk);
0058 
0059     /* turn off Veno */
0060     veno->doing_veno_now = 0;
0061 }
0062 
0063 static void tcp_veno_init(struct sock *sk)
0064 {
0065     struct veno *veno = inet_csk_ca(sk);
0066 
0067     veno->basertt = 0x7fffffff;
0068     veno->inc = 1;
0069     veno_enable(sk);
0070 }
0071 
0072 /* Do rtt sampling needed for Veno. */
0073 static void tcp_veno_pkts_acked(struct sock *sk,
0074                 const struct ack_sample *sample)
0075 {
0076     struct veno *veno = inet_csk_ca(sk);
0077     u32 vrtt;
0078 
0079     if (sample->rtt_us < 0)
0080         return;
0081 
0082     /* Never allow zero rtt or baseRTT */
0083     vrtt = sample->rtt_us + 1;
0084 
0085     /* Filter to find propagation delay: */
0086     if (vrtt < veno->basertt)
0087         veno->basertt = vrtt;
0088 
0089     /* Find the min rtt during the last rtt to find
0090      * the current prop. delay + queuing delay:
0091      */
0092     veno->minrtt = min(veno->minrtt, vrtt);
0093     veno->cntrtt++;
0094 }
0095 
0096 static void tcp_veno_state(struct sock *sk, u8 ca_state)
0097 {
0098     if (ca_state == TCP_CA_Open)
0099         veno_enable(sk);
0100     else
0101         veno_disable(sk);
0102 }
0103 
0104 /*
0105  * If the connection is idle and we are restarting,
0106  * then we don't want to do any Veno calculations
0107  * until we get fresh rtt samples.  So when we
0108  * restart, we reset our Veno state to a clean
0109  * state. After we get acks for this flight of
0110  * packets, _then_ we can make Veno calculations
0111  * again.
0112  */
0113 static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
0114 {
0115     if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
0116         tcp_veno_init(sk);
0117 }
0118 
0119 static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
0120 {
0121     struct tcp_sock *tp = tcp_sk(sk);
0122     struct veno *veno = inet_csk_ca(sk);
0123 
0124     if (!veno->doing_veno_now) {
0125         tcp_reno_cong_avoid(sk, ack, acked);
0126         return;
0127     }
0128 
0129     /* limited by applications */
0130     if (!tcp_is_cwnd_limited(sk))
0131         return;
0132 
0133     /* We do the Veno calculations only if we got enough rtt samples */
0134     if (veno->cntrtt <= 2) {
0135         /* We don't have enough rtt samples to do the Veno
0136          * calculation, so we'll behave like Reno.
0137          */
0138         tcp_reno_cong_avoid(sk, ack, acked);
0139     } else {
0140         u64 target_cwnd;
0141         u32 rtt;
0142 
0143         /* We have enough rtt samples, so, using the Veno
0144          * algorithm, we determine the state of the network.
0145          */
0146 
0147         rtt = veno->minrtt;
0148 
0149         target_cwnd = (u64)tcp_snd_cwnd(tp) * veno->basertt;
0150         target_cwnd <<= V_PARAM_SHIFT;
0151         do_div(target_cwnd, rtt);
0152 
0153         veno->diff = (tcp_snd_cwnd(tp) << V_PARAM_SHIFT) - target_cwnd;
0154 
0155         if (tcp_in_slow_start(tp)) {
0156             /* Slow start. */
0157             acked = tcp_slow_start(tp, acked);
0158             if (!acked)
0159                 goto done;
0160         }
0161 
0162         /* Congestion avoidance. */
0163         if (veno->diff < beta) {
0164             /* In the "non-congestive state", increase cwnd
0165              * every rtt.
0166              */
0167             tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked);
0168         } else {
0169             /* In the "congestive state", increase cwnd
0170              * every other rtt.
0171              */
0172             if (tp->snd_cwnd_cnt >= tcp_snd_cwnd(tp)) {
0173                 if (veno->inc &&
0174                     tcp_snd_cwnd(tp) < tp->snd_cwnd_clamp) {
0175                     tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
0176                     veno->inc = 0;
0177                 } else
0178                     veno->inc = 1;
0179                 tp->snd_cwnd_cnt = 0;
0180             } else
0181                 tp->snd_cwnd_cnt += acked;
0182         }
0183 done:
0184         if (tcp_snd_cwnd(tp) < 2)
0185             tcp_snd_cwnd_set(tp, 2);
0186         else if (tcp_snd_cwnd(tp) > tp->snd_cwnd_clamp)
0187             tcp_snd_cwnd_set(tp, tp->snd_cwnd_clamp);
0188     }
0189     /* Wipe the slate clean for the next rtt. */
0190     /* veno->cntrtt = 0; */
0191     veno->minrtt = 0x7fffffff;
0192 }
0193 
0194 /* Veno MD phase */
0195 static u32 tcp_veno_ssthresh(struct sock *sk)
0196 {
0197     const struct tcp_sock *tp = tcp_sk(sk);
0198     struct veno *veno = inet_csk_ca(sk);
0199 
0200     if (veno->diff < beta)
0201         /* in "non-congestive state", cut cwnd by 1/5 */
0202         return max(tcp_snd_cwnd(tp) * 4 / 5, 2U);
0203     else
0204         /* in "congestive state", cut cwnd by 1/2 */
0205         return max(tcp_snd_cwnd(tp) >> 1U, 2U);
0206 }
0207 
0208 static struct tcp_congestion_ops tcp_veno __read_mostly = {
0209     .init       = tcp_veno_init,
0210     .ssthresh   = tcp_veno_ssthresh,
0211     .undo_cwnd  = tcp_reno_undo_cwnd,
0212     .cong_avoid = tcp_veno_cong_avoid,
0213     .pkts_acked = tcp_veno_pkts_acked,
0214     .set_state  = tcp_veno_state,
0215     .cwnd_event = tcp_veno_cwnd_event,
0216 
0217     .owner      = THIS_MODULE,
0218     .name       = "veno",
0219 };
0220 
0221 static int __init tcp_veno_register(void)
0222 {
0223     BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
0224     tcp_register_congestion_control(&tcp_veno);
0225     return 0;
0226 }
0227 
0228 static void __exit tcp_veno_unregister(void)
0229 {
0230     tcp_unregister_congestion_control(&tcp_veno);
0231 }
0232 
0233 module_init(tcp_veno_register);
0234 module_exit(tcp_veno_unregister);
0235 
0236 MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
0237 MODULE_LICENSE("GPL");
0238 MODULE_DESCRIPTION("TCP Veno");