Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Sally Floyd's High Speed TCP (RFC 3649) congestion control
0004  *
0005  * See https://www.icir.org/floyd/hstcp.html
0006  *
0007  * John Heffner <jheffner@psc.edu>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <net/tcp.h>
0012 
0013 /* From AIMD tables from RFC 3649 appendix B,
0014  * with fixed-point MD scaled <<8.
0015  */
0016 static const struct hstcp_aimd_val {
0017     unsigned int cwnd;
0018     unsigned int md;
0019 } hstcp_aimd_vals[] = {
0020     {     38,  128, /*  0.50 */ },
0021     {    118,  112, /*  0.44 */ },
0022     {    221,  104, /*  0.41 */ },
0023     {    347,   98, /*  0.38 */ },
0024     {    495,   93, /*  0.37 */ },
0025     {    663,   89, /*  0.35 */ },
0026     {    851,   86, /*  0.34 */ },
0027     {   1058,   83, /*  0.33 */ },
0028     {   1284,   81, /*  0.32 */ },
0029     {   1529,   78, /*  0.31 */ },
0030     {   1793,   76, /*  0.30 */ },
0031     {   2076,   74, /*  0.29 */ },
0032     {   2378,   72, /*  0.28 */ },
0033     {   2699,   71, /*  0.28 */ },
0034     {   3039,   69, /*  0.27 */ },
0035     {   3399,   68, /*  0.27 */ },
0036     {   3778,   66, /*  0.26 */ },
0037     {   4177,   65, /*  0.26 */ },
0038     {   4596,   64, /*  0.25 */ },
0039     {   5036,   62, /*  0.25 */ },
0040     {   5497,   61, /*  0.24 */ },
0041     {   5979,   60, /*  0.24 */ },
0042     {   6483,   59, /*  0.23 */ },
0043     {   7009,   58, /*  0.23 */ },
0044     {   7558,   57, /*  0.22 */ },
0045     {   8130,   56, /*  0.22 */ },
0046     {   8726,   55, /*  0.22 */ },
0047     {   9346,   54, /*  0.21 */ },
0048     {   9991,   53, /*  0.21 */ },
0049     {  10661,   52, /*  0.21 */ },
0050     {  11358,   52, /*  0.20 */ },
0051     {  12082,   51, /*  0.20 */ },
0052     {  12834,   50, /*  0.20 */ },
0053     {  13614,   49, /*  0.19 */ },
0054     {  14424,   48, /*  0.19 */ },
0055     {  15265,   48, /*  0.19 */ },
0056     {  16137,   47, /*  0.19 */ },
0057     {  17042,   46, /*  0.18 */ },
0058     {  17981,   45, /*  0.18 */ },
0059     {  18955,   45, /*  0.18 */ },
0060     {  19965,   44, /*  0.17 */ },
0061     {  21013,   43, /*  0.17 */ },
0062     {  22101,   43, /*  0.17 */ },
0063     {  23230,   42, /*  0.17 */ },
0064     {  24402,   41, /*  0.16 */ },
0065     {  25618,   41, /*  0.16 */ },
0066     {  26881,   40, /*  0.16 */ },
0067     {  28193,   39, /*  0.16 */ },
0068     {  29557,   39, /*  0.15 */ },
0069     {  30975,   38, /*  0.15 */ },
0070     {  32450,   38, /*  0.15 */ },
0071     {  33986,   37, /*  0.15 */ },
0072     {  35586,   36, /*  0.14 */ },
0073     {  37253,   36, /*  0.14 */ },
0074     {  38992,   35, /*  0.14 */ },
0075     {  40808,   35, /*  0.14 */ },
0076     {  42707,   34, /*  0.13 */ },
0077     {  44694,   33, /*  0.13 */ },
0078     {  46776,   33, /*  0.13 */ },
0079     {  48961,   32, /*  0.13 */ },
0080     {  51258,   32, /*  0.13 */ },
0081     {  53677,   31, /*  0.12 */ },
0082     {  56230,   30, /*  0.12 */ },
0083     {  58932,   30, /*  0.12 */ },
0084     {  61799,   29, /*  0.12 */ },
0085     {  64851,   28, /*  0.11 */ },
0086     {  68113,   28, /*  0.11 */ },
0087     {  71617,   27, /*  0.11 */ },
0088     {  75401,   26, /*  0.10 */ },
0089     {  79517,   26, /*  0.10 */ },
0090     {  84035,   25, /*  0.10 */ },
0091     {  89053,   24, /*  0.10 */ },
0092 };
0093 
0094 #define HSTCP_AIMD_MAX  ARRAY_SIZE(hstcp_aimd_vals)
0095 
0096 struct hstcp {
0097     u32 ai;
0098 };
0099 
0100 static void hstcp_init(struct sock *sk)
0101 {
0102     struct tcp_sock *tp = tcp_sk(sk);
0103     struct hstcp *ca = inet_csk_ca(sk);
0104 
0105     ca->ai = 0;
0106 
0107     /* Ensure the MD arithmetic works.  This is somewhat pedantic,
0108      * since I don't think we will see a cwnd this large. :) */
0109     tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128);
0110 }
0111 
0112 static void hstcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
0113 {
0114     struct tcp_sock *tp = tcp_sk(sk);
0115     struct hstcp *ca = inet_csk_ca(sk);
0116 
0117     if (!tcp_is_cwnd_limited(sk))
0118         return;
0119 
0120     if (tcp_in_slow_start(tp))
0121         tcp_slow_start(tp, acked);
0122     else {
0123         /* Update AIMD parameters.
0124          *
0125          * We want to guarantee that:
0126          *     hstcp_aimd_vals[ca->ai-1].cwnd <
0127          *     snd_cwnd <=
0128          *     hstcp_aimd_vals[ca->ai].cwnd
0129          */
0130         if (tcp_snd_cwnd(tp) > hstcp_aimd_vals[ca->ai].cwnd) {
0131             while (tcp_snd_cwnd(tp) > hstcp_aimd_vals[ca->ai].cwnd &&
0132                    ca->ai < HSTCP_AIMD_MAX - 1)
0133                 ca->ai++;
0134         } else if (ca->ai && tcp_snd_cwnd(tp) <= hstcp_aimd_vals[ca->ai-1].cwnd) {
0135             while (ca->ai && tcp_snd_cwnd(tp) <= hstcp_aimd_vals[ca->ai-1].cwnd)
0136                 ca->ai--;
0137         }
0138 
0139         /* Do additive increase */
0140         if (tcp_snd_cwnd(tp) < tp->snd_cwnd_clamp) {
0141             /* cwnd = cwnd + a(w) / cwnd */
0142             tp->snd_cwnd_cnt += ca->ai + 1;
0143             if (tp->snd_cwnd_cnt >= tcp_snd_cwnd(tp)) {
0144                 tp->snd_cwnd_cnt -= tcp_snd_cwnd(tp);
0145                 tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
0146             }
0147         }
0148     }
0149 }
0150 
0151 static u32 hstcp_ssthresh(struct sock *sk)
0152 {
0153     const struct tcp_sock *tp = tcp_sk(sk);
0154     struct hstcp *ca = inet_csk_ca(sk);
0155 
0156     /* Do multiplicative decrease */
0157     return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * hstcp_aimd_vals[ca->ai].md) >> 8), 2U);
0158 }
0159 
0160 static struct tcp_congestion_ops tcp_highspeed __read_mostly = {
0161     .init       = hstcp_init,
0162     .ssthresh   = hstcp_ssthresh,
0163     .undo_cwnd  = tcp_reno_undo_cwnd,
0164     .cong_avoid = hstcp_cong_avoid,
0165 
0166     .owner      = THIS_MODULE,
0167     .name       = "highspeed"
0168 };
0169 
0170 static int __init hstcp_register(void)
0171 {
0172     BUILD_BUG_ON(sizeof(struct hstcp) > ICSK_CA_PRIV_SIZE);
0173     return tcp_register_congestion_control(&tcp_highspeed);
0174 }
0175 
0176 static void __exit hstcp_unregister(void)
0177 {
0178     tcp_unregister_congestion_control(&tcp_highspeed);
0179 }
0180 
0181 module_init(hstcp_register);
0182 module_exit(hstcp_unregister);
0183 
0184 MODULE_AUTHOR("John Heffner");
0185 MODULE_LICENSE("GPL");
0186 MODULE_DESCRIPTION("High Speed TCP");