summaryrefslogtreecommitdiffstats
path: root/src/vnet/tcp/tcp_newreno.c
blob: 8c7e77fc97442d6eb0ce8cb146f05cbc314beaf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/tcp/tcp.h>
#include <vnet/tcp/tcp_inlines.h>

typedef struct nwreno_cfg_
{
  u32 ssthresh;
} newreno_cfg_t;

static newreno_cfg_t newreno_cfg = {
  .ssthresh = 0x7FFFFFFFU,
};

static void
newreno_congestion (tcp_connection_t * tc)
{
  tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss);
  tc->cwnd = tc->ssthresh;
}

static void
newreno_loss (tcp_connection_t * tc)
{
  tc->cwnd = tcp_loss_wnd (tc);
}

static void
newreno_recovered (tcp_connection_t * tc)
{
  tc->cwnd = tc->ssthresh;
}

static void
newreno_rcv_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
{
  if (tcp_in_slowstart (tc))
    {
      tc->cwnd += clib_min (tc->snd_mss, rs->delivered);
    }
  else
    {
      /* tc->cwnd += clib_max ((tc->snd_mss * tc->snd_mss) / tc->cwnd, 1); */
      tcp_cwnd_accumulate (tc, tc->cwnd, rs->delivered);
    }
}

void
newreno_rcv_cong_ack (tcp_connection_t * tc, tcp_cc_ack_t ack_type,
		      tcp_rate_sample_t * rs)
{
  /* With sacks prr controls the data in flight post congestion */
  if (PREDICT_TRUE (tcp_opts_sack_permitted (tc)))
    return;

  if (ack_type == TCP_CC_DUPACK)
    {
      tc->cwnd += tc->snd_mss;
    }
  else if (ack_type == TCP_CC_PARTIALACK)
    {
      /* RFC 6582 Sec. 3.2
       * Deflate the congestion window by the amount of new data
       * acknowledged by the Cumulative Acknowledgment field.
       * If the partial ACK acknowledges at least one SMSS of new data,
       * then add back SMSS bytes to the congestion window. This
       * artificially inflates the congestion window in order to reflect
       * the additional segment that has left the network. This "partial
       * window deflation" attempts to ensure that, when fast recovery
       * eventually ends, approximately ssthresh amount of data will be
       * outstanding in the network. */
      tc->cwnd = (tc->cwnd > tc->bytes_acked + tc->snd_mss) ?
		   tc->cwnd - tc->bytes_acked :
		   tc->snd_mss;
      if (tc->bytes_acked > tc->snd_mss)
	tc->cwnd += tc->snd_mss;
    }
}

static void
newreno_conn_init (tcp_connection_t * tc)
{
  tc->ssthresh = newreno_cfg.ssthresh;
  tc->cwnd = tcp_initial_cwnd (tc);
}

static uword
newreno_unformat_config (unformat_input_t * input)
{
  u32 ssthresh = 0x7FFFFFFFU;

  if (!input)
    return 0;

  unformat_skip_white_space (input);

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "ssthresh %u", &ssthresh))
	newreno_cfg.ssthresh = ssthresh;
      else
	return 0;
    }
  return 1;
}

const static tcp_cc_algorithm_t tcp_newreno = {
  .name = "newreno",
  .unformat_cfg = newreno_unformat_config,
  .congestion = newreno_congestion,
  .loss = newreno_loss,
  .recovered = newreno_recovered,
  .rcv_ack = newreno_rcv_ack,
  .rcv_cong_ack = newreno_rcv_cong_ack,
  .init = newreno_conn_init
};

clib_error_t *
newreno_init (vlib_main_t * vm)
{
  clib_error_t *error = 0;

  tcp_cc_algo_register (TCP_CC_NEWRENO, &tcp_newreno);

  return error;
}

VLIB_INIT_FUNCTION (newreno_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
pan class="n">a = ctx->a; b = ctx->b; c = ctx->c; b += ++c; mend = m2 = mm + ARRAY_LEN (ctx->memory) / 2; m = mm; while (m < mend) { rngstep32 (a << 13, a, b, mm, m, m2, r, x, y); rngstep32 (a >> 6, a, b, mm, m, m2, r, x, y); rngstep32 (a << 2, a, b, mm, m, m2, r, x, y); rngstep32 (a >> 16, a, b, mm, m, m2, r, x, y); } m2 = mm; while (m2 < mend) { rngstep32 (a << 13, a, b, mm, m, m2, r, x, y); rngstep32 (a >> 6, a, b, mm, m, m2, r, x, y); rngstep32 (a << 2, a, b, mm, m, m2, r, x, y); rngstep32 (a >> 16, a, b, mm, m, m2, r, x, y); } ctx->a = a; ctx->b = b; ctx->c = c; } /* Perform 2 isaac runs with different contexts simultaneously. */ void isaac2 (isaac_t * ctx, uword * results) { #define _(n) \ u32 a##n, b##n, c##n, x##n, y##n, * m##n, * mm##n, * m2##n, * r##n, * mend##n _(0); _(1); (void) mend1; /* "set but unused variable" error on mend1 with gcc 4.9 */ #undef _ #define _(n) \ do { \ mm##n = ctx[(n)].memory; \ r##n = results + (n) * ISAAC_SIZE; \ a##n = ctx[(n)].a; \ b##n = ctx[(n)].b; \ c##n = ctx[(n)].c; \ b##n += ++c##n; \ mend##n = m2##n = mm##n + ARRAY_LEN (ctx[(n)].memory) / 2; \ m##n = mm##n; \ } while (0) _(0); _(1); #undef _ while (m0 < mend0) { rngstep32 (a0 << 13, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 << 13, a1, b1, mm1, m1, m21, r1, x1, y1); rngstep32 (a0 >> 6, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 >> 6, a1, b1, mm1, m1, m21, r1, x1, y1); rngstep32 (a0 << 2, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 << 2, a1, b1, mm1, m1, m21, r1, x1, y1); rngstep32 (a0 >> 16, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 >> 16, a1, b1, mm1, m1, m21, r1, x1, y1); } m20 = mm0; m21 = mm1; while (m20 < mend0) { rngstep32 (a0 << 13, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 << 13, a1, b1, mm1, m1, m21, r1, x1, y1); rngstep32 (a0 >> 6, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 >> 6, a1, b1, mm1, m1, m21, r1, x1, y1); rngstep32 (a0 << 2, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 << 2, a1, b1, mm1, m1, m21, r1, x1, y1); rngstep32 (a0 >> 16, a0, b0, mm0, m0, m20, r0, x0, y0); rngstep32 (a1 >> 16, a1, b1, mm1, m1, m21, r1, x1, y1); } ctx[0].a = a0; ctx[0].b = b0; ctx[0].c = c0; ctx[1].a = a1; ctx[1].b = b1; ctx[1].c = c1; } #define mix32(a,b,c,d,e,f,g,h) \ { \ a^=b<<11; d+=a; b+=c; \ b^=c>>2; e+=b; c+=d; \ c^=d<<8; f+=c; d+=e; \ d^=e>>16; g+=d; e+=f; \ e^=f<<10; h+=e; f+=g; \ f^=g>>4; a+=f; g+=h; \ g^=h<<8; b+=g; h+=a; \ h^=a>>9; c+=h; a+=b; \ } void isaac_init (isaac_t * ctx, uword * seeds) { word i; u32 a, b, c, d, e, f, g, h, *m, *r; ctx->a = ctx->b = ctx->c = 0; m = ctx->memory; r = seeds; a = b = c = d = e = f = g = h = 0x9e3779b9; /* the golden ratio */ for (i = 0; i < 4; ++i) /* scramble it */ mix32 (a, b, c, d, e, f, g, h); /* initialize using the contents of r[] as the seed */ for (i = 0; i < ISAAC_SIZE; i += 8) { a += r[i]; b += r[i + 1]; c += r[i + 2]; d += r[i + 3]; e += r[i + 4]; f += r[i + 5]; g += r[i + 6]; h += r[i + 7]; mix32 (a, b, c, d, e, f, g, h); m[i] = a; m[i + 1] = b; m[i + 2] = c; m[i + 3] = d; m[i + 4] = e; m[i + 5] = f; m[i + 6] = g; m[i + 7] = h; } /* do a second pass to make all of the seed affect all of m */ for (i = 0; i < ISAAC_SIZE; i += 8) { a += m[i]; b += m[i + 1]; c += m[i + 2]; d += m[i + 3]; e += m[i + 4]; f += m[i + 5]; g += m[i + 6]; h += m[i + 7]; mix32 (a, b, c, d, e, f, g, h); m[i] = a; m[i + 1] = b; m[i + 2] = c; m[i + 3] = d; m[i + 4] = e; m[i + 5] = f; m[i + 6] = g; m[i + 7] = h; } } #endif /* uword_bits == 32 */ #if uword_bits == 64 #define ind64(mm,x) (*(u64 *)((u8 *)(mm) + ((x) & ((ISAAC_SIZE-1)<<3)))) #define rngstep64(mix,a,b,mm,m,m2,r,x,y) \ { \ x = *m; \ a = (mix) + *(m2++); \ *(m++) = y = ind64(mm,x) + a + b; \ *(r++) = b = ind64(mm,y>>ISAAC_LOG2_SIZE) + x; \ } void isaac (isaac_t * ctx, uword * results) { u64 a, b, c, x, y, *m, *mm, *m2, *r, *mend; mm = ctx->memory; r = results; a = ctx->a; b = ctx->b; c = ctx->c; b += ++c; mend = m2 = mm + ARRAY_LEN (ctx->memory) / 2; m = mm; while (m < mend) { rngstep64 (~(a ^ (a << 21)), a, b, mm, m, m2, r, x, y); rngstep64 (a ^ (a >> 5), a, b, mm, m, m2, r, x, y); rngstep64 (a ^ (a << 12), a, b, mm, m, m2, r, x, y); rngstep64 (a ^ (a >> 33), a, b, mm, m, m2, r, x, y); } m2 = mm; while (m2 < mend) { rngstep64 (~(a ^ (a << 21)), a, b, mm, m, m2, r, x, y); rngstep64 (a ^ (a >> 5), a, b, mm, m, m2, r, x, y); rngstep64 (a ^ (a << 12), a, b, mm, m, m2, r, x, y); rngstep64 (a ^ (a >> 33), a, b, mm, m, m2, r, x, y); } ctx->a = a; ctx->b = b; ctx->c = c; } /* Perform 2 isaac runs with different contexts simultaneously. */ void isaac2 (isaac_t * ctx, uword * results) { #define _(n) \ u64 a##n, b##n, c##n, x##n, y##n, * m##n, * mm##n, * m2##n, * r##n, * mend##n _(0); _(1); #undef _ #define _(n) \ do { \ mm##n = ctx[(n)].memory; \ r##n = results + (n) * ISAAC_SIZE; \ a##n = ctx[(n)].a; \ b##n = ctx[(n)].b; \ c##n = ctx[(n)].c; \ b##n += ++c##n; \ mend##n = m2##n = mm##n + ARRAY_LEN (ctx[(n)].memory) / 2; \ m##n = mm##n; \ } while (0) _(0); _(1); #undef _ (void) mend1; /* compiler warning */ while (m0 < mend0) { rngstep64 (~(a0 ^ (a0 << 21)), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (~(a1 ^ (a1 << 21)), a1, b1, mm1, m1, m21, r1, x1, y1); rngstep64 (a0 ^ (a0 >> 5), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (a1 ^ (a1 >> 5), a1, b1, mm1, m1, m21, r1, x1, y1); rngstep64 (a0 ^ (a0 << 12), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (a1 ^ (a1 << 12), a1, b1, mm1, m1, m21, r1, x1, y1); rngstep64 (a0 ^ (a0 >> 33), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (a1 ^ (a1 >> 33), a1, b1, mm1, m1, m21, r1, x1, y1); } m20 = mm0; m21 = mm1; while (m20 < mend0) { rngstep64 (~(a0 ^ (a0 << 21)), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (~(a1 ^ (a1 << 21)), a1, b1, mm1, m1, m21, r1, x1, y1); rngstep64 (a0 ^ (a0 >> 5), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (a1 ^ (a1 >> 5), a1, b1, mm1, m1, m21, r1, x1, y1); rngstep64 (a0 ^ (a0 << 12), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (a1 ^ (a1 << 12), a1, b1, mm1, m1, m21, r1, x1, y1); rngstep64 (a0 ^ (a0 >> 33), a0, b0, mm0, m0, m20, r0, x0, y0); rngstep64 (a1 ^ (a1 >> 33), a1, b1, mm1, m1, m21, r1, x1, y1); } ctx[0].a = a0; ctx[0].b = b0; ctx[0].c = c0; ctx[1].a = a1; ctx[1].b = b1; ctx[1].c = c1; } #define mix64(a,b,c,d,e,f,g,h) \ { \ a-=e; f^=h>>9; h+=a; \ b-=f; g^=a<<9; a+=b; \ c-=g; h^=b>>23; b+=c; \ d-=h; a^=c<<15; c+=d; \ e-=a; b^=d>>14; d+=e; \ f-=b; c^=e<<20; e+=f; \ g-=c; d^=f>>17; f+=g; \ h-=d; e^=g<<14; g+=h; \ } void isaac_init (isaac_t * ctx, uword * seeds) { word i; u64 a, b, c, d, e, f, g, h, *m, *r; ctx->a = ctx->b = ctx->c = 0; m = ctx->memory; r = seeds; a = b = c = d = e = f = g = h = 0x9e3779b97f4a7c13LL; /* the golden ratio */ for (i = 0; i < 4; ++i) /* scramble it */ mix64 (a, b, c, d, e, f, g, h); for (i = 0; i < ISAAC_SIZE; i += 8) /* fill in mm[] with messy stuff */ { a += r[i]; b += r[i + 1]; c += r[i + 2]; d += r[i + 3]; e += r[i + 4]; f += r[i + 5]; g += r[i + 6]; h += r[i + 7]; mix64 (a, b, c, d, e, f, g, h); m[i] = a; m[i + 1] = b; m[i + 2] = c; m[i + 3] = d; m[i + 4] = e; m[i + 5] = f; m[i + 6] = g; m[i + 7] = h; } /* do a second pass to make all of the seed affect all of mm */ for (i = 0; i < ISAAC_SIZE; i += 8) { a += m[i]; b += m[i + 1]; c += m[i + 2]; d += m[i + 3]; e += m[i + 4]; f += m[i + 5]; g += m[i + 6]; h += m[i + 7]; mix64 (a, b, c, d, e, f, g, h); m[i] = a; m[i + 1] = b; m[i + 2] = c; m[i + 3] = d; m[i + 4] = e; m[i + 5] = f; m[i + 6] = g; m[i + 7] = h; } } #endif /* uword_bits == 64 */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */