1 | /* |
2 | * Copyright (c) 2021 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 "tcp_includes.h" |
30 | |
31 | #include <sys/domain.h> |
32 | #include <sys/sdt.h> |
33 | |
34 | #define TCP_CCDBG_NOUNIT 0xffffffff |
35 | static kern_ctl_ref tcp_ccdbg_ctlref = NULL; |
36 | volatile uint32_t tcp_ccdbg_unit = TCP_CCDBG_NOUNIT; |
37 | |
38 | /* Allow only one socket to connect at any time for debugging */ |
39 | static errno_t |
40 | tcp_ccdbg_control_connect(kern_ctl_ref kctl, struct sockaddr_ctl *sac, |
41 | void **uinfo) |
42 | { |
43 | #pragma unused(kctl) |
44 | #pragma unused(uinfo) |
45 | |
46 | UInt32 old_value = TCP_CCDBG_NOUNIT; |
47 | UInt32 new_value = sac->sc_unit; |
48 | |
49 | if (tcp_ccdbg_unit != old_value) { |
50 | return EALREADY; |
51 | } |
52 | |
53 | if (OSCompareAndSwap(old_value, new_value, &tcp_ccdbg_unit)) { |
54 | return 0; |
55 | } else { |
56 | return EALREADY; |
57 | } |
58 | } |
59 | |
60 | static errno_t |
61 | tcp_ccdbg_control_disconnect(kern_ctl_ref kctl, u_int32_t unit, void *uinfo) |
62 | { |
63 | #pragma unused(kctl, unit, uinfo) |
64 | |
65 | if (unit == tcp_ccdbg_unit) { |
66 | UInt32 old_value = tcp_ccdbg_unit; |
67 | UInt32 new_value = TCP_CCDBG_NOUNIT; |
68 | if (tcp_ccdbg_unit == new_value) { |
69 | return 0; |
70 | } |
71 | |
72 | if (!OSCompareAndSwap(old_value, new_value, |
73 | &tcp_ccdbg_unit)) { |
74 | log(LOG_DEBUG, |
75 | "failed to disconnect tcp_cc debug control" ); |
76 | } |
77 | } |
78 | return 0; |
79 | } |
80 | |
81 | inline void |
82 | tcp_ccdbg_trace(struct tcpcb *tp, struct tcphdr *th, int32_t event) |
83 | { |
84 | #if !CONFIG_DTRACE |
85 | #pragma unused(th) |
86 | #endif /* !CONFIG_DTRACE */ |
87 | struct inpcb *inp = tp->t_inpcb; |
88 | |
89 | if (tcp_cc_debug && tcp_ccdbg_unit > 0) { |
90 | struct tcp_cc_debug_state dbg_state; |
91 | struct timespec tv; |
92 | |
93 | bzero(s: &dbg_state, n: sizeof(dbg_state)); |
94 | |
95 | nanotime(ts: &tv); |
96 | /* Take time in seconds */ |
97 | dbg_state.ccd_tsns = (tv.tv_sec * 1000000000) + tv.tv_nsec; |
98 | inet_ntop(SOCK_DOM(inp->inp_socket), |
99 | ((SOCK_DOM(inp->inp_socket) == PF_INET) ? |
100 | (void *)&inp->inp_laddr.s_addr : |
101 | (void *)&inp->in6p_laddr), dbg_state.ccd_srcaddr, |
102 | sizeof(dbg_state.ccd_srcaddr)); |
103 | dbg_state.ccd_srcport = ntohs(inp->inp_lport); |
104 | inet_ntop(SOCK_DOM(inp->inp_socket), |
105 | ((SOCK_DOM(inp->inp_socket) == PF_INET) ? |
106 | (void *)&inp->inp_faddr.s_addr : |
107 | (void *)&inp->in6p_faddr), dbg_state.ccd_destaddr, |
108 | sizeof(dbg_state.ccd_destaddr)); |
109 | dbg_state.ccd_destport = ntohs(inp->inp_fport); |
110 | |
111 | dbg_state.ccd_snd_cwnd = tp->snd_cwnd; |
112 | dbg_state.ccd_snd_wnd = tp->snd_wnd; |
113 | dbg_state.ccd_snd_ssthresh = tp->snd_ssthresh; |
114 | dbg_state.ccd_pipeack = tp->t_pipeack; |
115 | dbg_state.ccd_rttcur = tp->t_rttcur; |
116 | dbg_state.ccd_rxtcur = tp->t_rxtcur; |
117 | dbg_state.ccd_srtt = tp->t_srtt >> TCP_RTT_SHIFT; |
118 | dbg_state.ccd_event = event; |
119 | dbg_state.ccd_sndcc = inp->inp_socket->so_snd.sb_cc; |
120 | dbg_state.ccd_sndhiwat = inp->inp_socket->so_snd.sb_hiwat; |
121 | dbg_state.ccd_bytes_acked = tp->t_bytes_acked; |
122 | dbg_state.ccd_cc_index = tp->tcp_cc_index; |
123 | switch (tp->tcp_cc_index) { |
124 | case TCP_CC_ALGO_CUBIC_INDEX: |
125 | dbg_state.u.cubic_state.ccd_last_max = |
126 | tp->t_ccstate->cub_last_max; |
127 | dbg_state.u.cubic_state.ccd_tcp_win = |
128 | tp->t_ccstate->cub_tcp_win; |
129 | dbg_state.u.cubic_state.ccd_avg_lastmax = |
130 | tp->t_ccstate->cub_avg_lastmax; |
131 | dbg_state.u.cubic_state.ccd_mean_deviation = |
132 | tp->t_ccstate->cub_mean_dev; |
133 | break; |
134 | case TCP_CC_ALGO_BACKGROUND_INDEX: |
135 | dbg_state.u.ledbat_state.led_base_rtt = |
136 | get_base_rtt(tp); |
137 | break; |
138 | default: |
139 | break; |
140 | } |
141 | |
142 | ctl_enqueuedata(kctlref: tcp_ccdbg_ctlref, unit: tcp_ccdbg_unit, |
143 | data: &dbg_state, len: sizeof(dbg_state), flags: 0); |
144 | } |
145 | DTRACE_TCP5(cc, void, NULL, struct inpcb *, inp, |
146 | struct tcpcb *, tp, struct tcphdr *, th, int32_t, event); |
147 | } |
148 | |
149 | void |
150 | tcp_ccdbg_control_register(void) |
151 | { |
152 | struct kern_ctl_reg ccdbg_control; |
153 | errno_t err; |
154 | |
155 | bzero(s: &ccdbg_control, n: sizeof(ccdbg_control)); |
156 | strlcpy(dst: ccdbg_control.ctl_name, TCP_CC_CONTROL_NAME, |
157 | n: 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 | ccdbg_control.ctl_sendsize = 32 * 1024; |
163 | |
164 | err = ctl_register(userkctl: &ccdbg_control, kctlref: &tcp_ccdbg_ctlref); |
165 | if (err != 0) { |
166 | log(LOG_ERR, "failed to register tcp_cc debug control" ); |
167 | } |
168 | } |
169 | |