]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_cc.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_cc.c
CommitLineData
fe8ab488
A
1/*
2 * Copyright (c) 2013-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/syslog.h>
33#include <sys/protosw.h>
34#include <sys/socketvar.h>
35#include <sys/kern_control.h>
36#include <sys/domain.h>
37
38#include <netinet/in.h>
39#include <netinet/tcp.h>
40#include <netinet/tcp_var.h>
41#include <netinet/tcp_cc.h>
42#include <mach/sdt.h>
43#include <libkern/OSAtomic.h>
44
45struct tcp_cc_debug_state {
46 u_int64_t ccd_tsns;
47 char ccd_srcaddr[INET6_ADDRSTRLEN];
48 uint16_t ccd_srcport;
49 char ccd_destaddr[INET6_ADDRSTRLEN];
50 uint16_t ccd_destport;
51 uint32_t ccd_snd_cwnd;
52 uint32_t ccd_snd_wnd;
53 uint32_t ccd_snd_ssthresh;
3e170ce0 54 uint32_t ccd_pipeack;
fe8ab488
A
55 uint32_t ccd_rttcur;
56 uint32_t ccd_rxtcur;
57 uint32_t ccd_srtt;
58 uint32_t ccd_event;
59 uint32_t ccd_sndcc;
60 uint32_t ccd_sndhiwat;
61 uint32_t ccd_bytes_acked;
ecc0ceb4
A
62 u_int8_t ccd_cc_index;
63 u_int8_t ccd_unused_1__;
64 u_int16_t ccd_unused_2__;
fe8ab488
A
65 union {
66 struct {
67 uint32_t ccd_last_max;
68 uint32_t ccd_tcp_win;
69 uint32_t ccd_target_win;
70 uint32_t ccd_avg_lastmax;
71 uint32_t ccd_mean_deviation;
72 } cubic_state;
ecc0ceb4
A
73 struct {
74 u_int32_t led_base_rtt;
75 } ledbat_state;
fe8ab488
A
76 } u;
77};
78
79int tcp_cc_debug = 0;
80SYSCTL_INT(_net_inet_tcp, OID_AUTO, cc_debug, CTLFLAG_RW | CTLFLAG_LOCKED,
81 &tcp_cc_debug, 0, "Enable debug data collection");
82
83extern struct tcp_cc_algo tcp_cc_newreno;
84SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno_sockets,
85 CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_cc_newreno.num_sockets,
86 0, "Number of sockets using newreno");
87
88extern struct tcp_cc_algo tcp_cc_ledbat;
89SYSCTL_INT(_net_inet_tcp, OID_AUTO, background_sockets,
90 CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_cc_ledbat.num_sockets,
91 0, "Number of sockets using background transport");
92
93extern struct tcp_cc_algo tcp_cc_cubic;
94SYSCTL_INT(_net_inet_tcp, OID_AUTO, cubic_sockets,
95 CTLFLAG_RD | CTLFLAG_LOCKED,&tcp_cc_cubic.num_sockets,
96 0, "Number of sockets using cubic");
97
98int tcp_use_newreno = 0;
99SYSCTL_INT(_net_inet_tcp, OID_AUTO, use_newreno,
100 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_use_newreno, 0,
101 "Use TCP NewReno by default");
102
3e170ce0
A
103static int tcp_check_cwnd_nonvalidated = 1;
104#if (DEBUG || DEVELOPMENT)
105SYSCTL_INT(_net_inet_tcp, OID_AUTO, cwnd_nonvalidated,
106 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_check_cwnd_nonvalidated, 0,
107 "Check if congestion window is non-validated");
108#endif /* (DEBUG || DEVELOPMENT) */
109
fe8ab488
A
110 #define SET_SNDSB_IDEAL_SIZE(sndsb, size) \
111 sndsb->sb_idealsize = min(max(tcp_sendspace, tp->snd_ssthresh), \
112 tcp_autosndbuf_max);
113
114/* Array containing pointers to currently implemented TCP CC algorithms */
115struct tcp_cc_algo* tcp_cc_algo_list[TCP_CC_ALGO_COUNT];
116struct zone *tcp_cc_zone;
117
118/* Information for colelcting TCP debug information using control socket */
119#define TCP_CCDEBUG_CONTROL_NAME "com.apple.network.tcp_ccdebug"
120#define TCP_CCDBG_NOUNIT 0xffffffff
121static kern_ctl_ref tcp_ccdbg_ctlref = NULL;
122volatile UInt32 tcp_ccdbg_unit = TCP_CCDBG_NOUNIT;
123
124void tcp_cc_init(void);
125static void tcp_cc_control_register(void);
126static errno_t tcp_ccdbg_control_connect(kern_ctl_ref kctl,
127 struct sockaddr_ctl *sac, void **uinfo);
128static errno_t tcp_ccdbg_control_disconnect(kern_ctl_ref kctl,
129 u_int32_t unit, void *uinfo);
130static struct tcp_cc_algo tcp_cc_algo_none;
131/*
132 * Initialize TCP congestion control algorithms.
133 */
134
135void
136tcp_cc_init(void)
137{
138 bzero(&tcp_cc_algo_list, sizeof(tcp_cc_algo_list));
139 bzero(&tcp_cc_algo_none, sizeof(tcp_cc_algo_none));
140
141 tcp_cc_algo_list[TCP_CC_ALGO_NONE] = &tcp_cc_algo_none;
142 tcp_cc_algo_list[TCP_CC_ALGO_NEWRENO_INDEX] = &tcp_cc_newreno;
143 tcp_cc_algo_list[TCP_CC_ALGO_BACKGROUND_INDEX] = &tcp_cc_ledbat;
144 tcp_cc_algo_list[TCP_CC_ALGO_CUBIC_INDEX] = &tcp_cc_cubic;
145
146 tcp_cc_control_register();
147}
148
149static void
150tcp_cc_control_register(void)
151{
152 struct kern_ctl_reg ccdbg_control;
153 errno_t err;
154
155 bzero(&ccdbg_control, sizeof(ccdbg_control));
156 strlcpy(ccdbg_control.ctl_name, TCP_CCDEBUG_CONTROL_NAME,
157 sizeof(ccdbg_control.ctl_name));
158 ccdbg_control.ctl_connect = tcp_ccdbg_control_connect;
159 ccdbg_control.ctl_disconnect = tcp_ccdbg_control_disconnect;
160 ccdbg_control.ctl_flags |= CTL_FLAG_PRIVILEGED;
161 ccdbg_control.ctl_flags |= CTL_FLAG_REG_SOCK_STREAM;
162
163 err = ctl_register(&ccdbg_control, &tcp_ccdbg_ctlref);
164 if (err != 0) {
165 log(LOG_ERR, "failed to register tcp_cc debug control");
166 }
167}
168
169/* Allow only one socket to connect at any time for debugging */
170static errno_t
171tcp_ccdbg_control_connect(kern_ctl_ref kctl, struct sockaddr_ctl *sac,
172 void **uinfo)
173{
174#pragma unused(kctl)
175#pragma unused(uinfo)
176
177 UInt32 old_value = TCP_CCDBG_NOUNIT;
178 UInt32 new_value = sac->sc_unit;
179
180 if (tcp_ccdbg_unit != old_value)
181 return (EALREADY);
182
183 if (OSCompareAndSwap(old_value, new_value, &tcp_ccdbg_unit))
184 return (0);
185 else
186 return (EALREADY);
187}
188
189static errno_t
190tcp_ccdbg_control_disconnect(kern_ctl_ref kctl, u_int32_t unit, void *uinfo)
191{
192#pragma unused(kctl, unit, uinfo)
193
194 if (unit == tcp_ccdbg_unit) {
195 UInt32 old_value = tcp_ccdbg_unit;
196 UInt32 new_value = TCP_CCDBG_NOUNIT;
197 if (tcp_ccdbg_unit == new_value)
198 return (0);
199
200 if (!OSCompareAndSwap(old_value, new_value,
201 &tcp_ccdbg_unit))
202 log(LOG_DEBUG,
203 "failed to disconnect tcp_cc debug control");
204 }
205 return (0);
206}
207
208inline void
209tcp_ccdbg_trace(struct tcpcb *tp, struct tcphdr *th, int32_t event)
210{
211#if !CONFIG_DTRACE
212#pragma unused(th)
213#endif /* !CONFIG_DTRACE */
214 struct inpcb *inp = tp->t_inpcb;
215
216 if (tcp_cc_debug && tcp_ccdbg_unit > 0) {
217 struct tcp_cc_debug_state dbg_state;
218 struct timespec tv;
219
220 bzero(&dbg_state, sizeof(dbg_state));
221
222 nanotime(&tv);
223 /* Take time in seconds */
224 dbg_state.ccd_tsns = (tv.tv_sec * 1000000000) + tv.tv_nsec;
225 inet_ntop(SOCK_DOM(inp->inp_socket),
226 ((SOCK_DOM(inp->inp_socket) == PF_INET) ?
227 (void *)&inp->inp_laddr.s_addr :
228 (void *)&inp->in6p_laddr), dbg_state.ccd_srcaddr,
229 sizeof(dbg_state.ccd_srcaddr));
230 dbg_state.ccd_srcport = ntohs(inp->inp_lport);
231 inet_ntop(SOCK_DOM(inp->inp_socket),
232 ((SOCK_DOM(inp->inp_socket) == PF_INET) ?
233 (void *)&inp->inp_faddr.s_addr :
234 (void *)&inp->in6p_faddr), dbg_state.ccd_destaddr,
235 sizeof(dbg_state.ccd_destaddr));
236 dbg_state.ccd_destport = ntohs(inp->inp_fport);
237
238 dbg_state.ccd_snd_cwnd = tp->snd_cwnd;
239 dbg_state.ccd_snd_wnd = tp->snd_wnd;
240 dbg_state.ccd_snd_ssthresh = tp->snd_ssthresh;
3e170ce0 241 dbg_state.ccd_pipeack = tp->t_pipeack;
fe8ab488
A
242 dbg_state.ccd_rttcur = tp->t_rttcur;
243 dbg_state.ccd_rxtcur = tp->t_rxtcur;
244 dbg_state.ccd_srtt = tp->t_srtt >> TCP_RTT_SHIFT;
245 dbg_state.ccd_event = event;
246 dbg_state.ccd_sndcc = inp->inp_socket->so_snd.sb_cc;
247 dbg_state.ccd_sndhiwat = inp->inp_socket->so_snd.sb_hiwat;
248 dbg_state.ccd_bytes_acked = tp->t_bytes_acked;
ecc0ceb4 249 dbg_state.ccd_cc_index = tp->tcp_cc_index;
fe8ab488
A
250 switch (tp->tcp_cc_index) {
251 case TCP_CC_ALGO_CUBIC_INDEX:
252 dbg_state.u.cubic_state.ccd_last_max =
253 tp->t_ccstate->cub_last_max;
254 dbg_state.u.cubic_state.ccd_tcp_win =
255 tp->t_ccstate->cub_tcp_win;
256 dbg_state.u.cubic_state.ccd_target_win =
257 tp->t_ccstate->cub_target_win;
258 dbg_state.u.cubic_state.ccd_avg_lastmax =
259 tp->t_ccstate->cub_avg_lastmax;
260 dbg_state.u.cubic_state.ccd_mean_deviation =
261 tp->t_ccstate->cub_mean_dev;
262 break;
ecc0ceb4
A
263 case TCP_CC_ALGO_BACKGROUND_INDEX:
264 dbg_state.u.ledbat_state.led_base_rtt =
265 get_base_rtt(tp);
266 break;
fe8ab488
A
267 default:
268 break;
269 }
270
271 ctl_enqueuedata(tcp_ccdbg_ctlref, tcp_ccdbg_unit,
272 &dbg_state, sizeof(dbg_state), 0);
273 }
274 DTRACE_TCP5(cc, void, NULL, struct inpcb *, inp,
275 struct tcpcb *, tp, struct tcphdr *, th, int32_t, event);
276}
277
278void tcp_cc_resize_sndbuf(struct tcpcb *tp)
279{
280 struct sockbuf *sb;
281 /*
282 * If the send socket buffer size is bigger than ssthresh,
283 * it is time to trim it because we do not want to hold
284 * too many mbufs in the socket buffer
285 */
286 sb = &tp->t_inpcb->inp_socket->so_snd;
287 if (sb->sb_hiwat > tp->snd_ssthresh &&
288 (sb->sb_flags & SB_AUTOSIZE)) {
289 if (sb->sb_idealsize > tp->snd_ssthresh) {
290 SET_SNDSB_IDEAL_SIZE(sb, tp->snd_ssthresh);
291 }
292 sb->sb_flags |= SB_TRIM;
293 }
294}
295
296void tcp_bad_rexmt_fix_sndbuf(struct tcpcb *tp)
297{
298 struct sockbuf *sb;
299 sb = &tp->t_inpcb->inp_socket->so_snd;
300 if ((sb->sb_flags & (SB_TRIM|SB_AUTOSIZE)) == (SB_TRIM|SB_AUTOSIZE)) {
301 /*
302 * If there was a retransmission that was not necessary
303 * then the size of socket buffer can be restored to
304 * what it was before
305 */
306 SET_SNDSB_IDEAL_SIZE(sb, tp->snd_ssthresh);
307 if (sb->sb_hiwat <= sb->sb_idealsize) {
308 sbreserve(sb, sb->sb_idealsize);
309 sb->sb_flags &= ~SB_TRIM;
310 }
311 }
312}
313
314/*
315 * Calculate initial cwnd according to RFC3390.
316 *
317 * Keep the old ss_fltsz sysctl for ABI compabitility issues.
318 * but it will be overriden if tcp_do_rfc3390 sysctl when it is set.
319 */
320void
321tcp_cc_cwnd_init_or_reset(struct tcpcb *tp)
322{
323 if (tp->t_flags & TF_LOCAL) {
324 tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local;
325 } else {
326 /* initial congestion window according to RFC 3390 */
327 if (tcp_do_rfc3390)
328 tp->snd_cwnd = min(4 * tp->t_maxseg,
329 max(2 * tp->t_maxseg, TCP_CC_CWND_INIT_BYTES));
330 else
331 tp->snd_cwnd = tp->t_maxseg * ss_fltsz;
332 }
333}
334
335/*
336 * Indicate whether this ack should be delayed.
337 * Here is the explanation for different settings of tcp_delack_enabled:
338 * - when set to 1, the bhavior is same as when set to 2. We kept this
339 * for binary compatibility.
340 * - when set to 2, will "ack every other packet"
341 * - if our last ack wasn't a 0-sized window.
342 * - if the peer hasn't sent us a TH_PUSH data packet (radar 3649245).
343 * If TH_PUSH is set, take this as a clue that we need to ACK
344 * with no delay. This helps higher level protocols who
345 * won't send us more data even if the window is open
346 * because their last "segment" hasn't been ACKed
347 * - when set to 3, will do "streaming detection"
348 * - if we receive more than "maxseg_unacked" full packets
349 * in the last 100ms
350 * - if the connection is not in slow-start or idle or
351 * loss/recovery states
352 * - if those criteria aren't met, it will ack every other packet.
353 */
354int
355tcp_cc_delay_ack(struct tcpcb *tp, struct tcphdr *th)
356{
fe8ab488
A
357 switch (tcp_delack_enabled) {
358 case 1:
359 case 2:
360 if ((tp->t_flags & TF_RXWIN0SENT) == 0 &&
361 (th->th_flags & TH_PUSH) == 0 &&
362 (tp->t_unacksegs == 1))
363 return(1);
364 break;
365 case 3:
366 if ((tp->t_flags & TF_RXWIN0SENT) == 0 &&
367 (th->th_flags & TH_PUSH) == 0 &&
368 ((tp->t_unacksegs == 1) ||
369 ((tp->t_flags & TF_STRETCHACK) != 0 &&
370 tp->t_unacksegs < (maxseg_unacked))))
371 return(1);
372 break;
373 }
374 return(0);
375}
376
377void
378tcp_cc_allocate_state(struct tcpcb *tp)
379{
380 if (tp->tcp_cc_index == TCP_CC_ALGO_CUBIC_INDEX &&
381 tp->t_ccstate == NULL) {
382 tp->t_ccstate = (struct tcp_ccstate *)zalloc(tcp_cc_zone);
383
384 /*
385 * If we could not allocate memory for congestion control
386 * state, revert to using TCP NewReno as it does not
387 * require any state
388 */
389 if (tp->t_ccstate == NULL)
390 tp->tcp_cc_index = TCP_CC_ALGO_NEWRENO_INDEX;
391 else
392 bzero(tp->t_ccstate, sizeof(*tp->t_ccstate));
393 }
394}
395
396/*
397 * If stretch ack was disabled automatically on long standing connections,
398 * re-evaluate the situation after 15 minutes to enable it.
399 */
400#define TCP_STRETCHACK_DISABLE_WIN (15 * 60 * TCP_RETRANSHZ)
401void
402tcp_cc_after_idle_stretchack(struct tcpcb *tp)
403{
404 int32_t tdiff;
405
406 if (!(tp->t_flagsext & TF_DISABLE_STRETCHACK))
407 return;
408
409 tdiff = timer_diff(tcp_now, 0, tp->rcv_nostrack_ts, 0);
410 if (tdiff < 0)
411 tdiff = -tdiff;
412
413 if (tdiff > TCP_STRETCHACK_DISABLE_WIN) {
414 tp->t_flagsext &= ~TF_DISABLE_STRETCHACK;
415 tp->t_stretchack_delayed = 0;
416
417 tcp_reset_stretch_ack(tp);
418 }
419}
3e170ce0
A
420
421/*
422 * Detect if the congestion window is non-vlidated according to
423 * draft-ietf-tcpm-newcwv-07
424 */
425
426inline uint32_t
427tcp_cc_is_cwnd_nonvalidated(struct tcpcb *tp)
428{
429 if (tp->t_pipeack == 0 || tcp_check_cwnd_nonvalidated == 0) {
430 tp->t_flagsext &= ~TF_CWND_NONVALIDATED;
431 return (0);
432 }
433 if (tp->t_pipeack >= (tp->snd_cwnd) >> 1)
434 tp->t_flagsext &= ~TF_CWND_NONVALIDATED;
435 else
436 tp->t_flagsext |= TF_CWND_NONVALIDATED;
437 return (tp->t_flagsext & TF_CWND_NONVALIDATED);
438}
439
440/*
441 * Adjust congestion window in response to congestion in non-validated
442 * phase.
443 */
444inline void
445tcp_cc_adjust_nonvalidated_cwnd(struct tcpcb *tp)
446{
447 tp->t_pipeack = tcp_get_max_pipeack(tp);
448 tcp_clear_pipeack_state(tp);
449 tp->snd_cwnd = (max(tp->t_pipeack, tp->t_lossflightsize) >> 1);
450 tp->snd_cwnd = max(tp->snd_cwnd, TCP_CC_CWND_INIT_BYTES);
451 tp->snd_cwnd += tp->t_maxseg * tcprexmtthresh;
452 tp->t_flagsext &= ~TF_CWND_NONVALIDATED;
453}
454
455/*
456 * Return maximum of all the pipeack samples. Since the number of samples
457 * TCP_PIPEACK_SAMPLE_COUNT is 3 at this time, it will be simpler to do
458 * a comparision. We should change ths if the number of samples increases.
459 */
460inline u_int32_t
461tcp_get_max_pipeack(struct tcpcb *tp)
462{
463 u_int32_t max_pipeack = 0;
464 max_pipeack = (tp->t_pipeack_sample[0] > tp->t_pipeack_sample[1]) ?
465 tp->t_pipeack_sample[0] : tp->t_pipeack_sample[1];
466 max_pipeack = (tp->t_pipeack_sample[2] > max_pipeack) ?
467 tp->t_pipeack_sample[2] : max_pipeack;
468
469 return (max_pipeack);
470}
471
472inline void
473tcp_clear_pipeack_state(struct tcpcb *tp)
474{
475 bzero(tp->t_pipeack_sample, sizeof(tp->t_pipeack_sample));
476 tp->t_pipeack_ind = 0;
477 tp->t_lossflightsize = 0;
478}