1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /************************************************************************
25 
26    Net.cc
27 
28 
29 ************************************************************************/
30 
31 #include "P_Net.h"
32 #include <utility>
33 
34 RecRawStatBlock *net_rsb = nullptr;
35 
36 // All in milli-seconds
37 int net_config_poll_timeout = -1; // This will get set via either command line or records.config.
38 int net_event_period        = 10;
39 int net_accept_period       = 10;
40 int net_retry_delay         = 10;
41 int net_throttle_delay      = 50; /* milliseconds */
42 
43 // For the in/out congestion control: ToDo: this probably would be better as ports: specifications
44 std::string_view net_ccp_in;
45 std::string_view net_ccp_out;
46 
47 static inline void
configure_net()48 configure_net()
49 {
50   REC_RegisterConfigUpdateFunc("proxy.config.net.connections_throttle", change_net_connections_throttle, nullptr);
51   REC_ReadConfigInteger(fds_throttle, "proxy.config.net.connections_throttle");
52 
53   REC_EstablishStaticConfigInt32(net_retry_delay, "proxy.config.net.retry_delay");
54   REC_EstablishStaticConfigInt32(net_throttle_delay, "proxy.config.net.throttle_delay");
55 
56   // These are not reloadable
57   REC_ReadConfigInteger(net_event_period, "proxy.config.net.event_period");
58   REC_ReadConfigInteger(net_accept_period, "proxy.config.net.accept_period");
59 
60   // This is kinda fugly, but better than it was before (on every connection in and out)
61   // Note that these would need to be ats_free()'d if we ever want to clean that up, but
62   // we have no good way of dealing with that on such globals I think?
63   RecString ccp;
64 
65   REC_ReadConfigStringAlloc(ccp, "proxy.config.net.tcp_congestion_control_in");
66   if (ccp && *ccp != '\0') {
67     net_ccp_in = ccp;
68   } else {
69     ats_free(ccp);
70   }
71 
72   REC_ReadConfigStringAlloc(ccp, "proxy.config.net.tcp_congestion_control_out");
73   if (ccp && *ccp != '\0') {
74     net_ccp_out = ccp;
75   } else {
76     ats_free(ccp);
77   }
78 }
79 
80 static inline void
register_net_stats()81 register_net_stats()
82 {
83   const std::pair<const char *, Net_Stats> persistent[] = {
84     {"proxy.process.net.calls_to_read", net_calls_to_read_stat},
85     {"proxy.process.net.calls_to_read_nodata", net_calls_to_read_nodata_stat},
86     {"proxy.process.net.calls_to_readfromnet", net_calls_to_readfromnet_stat},
87     {"proxy.process.net.calls_to_readfromnet_afterpoll", net_calls_to_readfromnet_afterpoll_stat},
88     {"proxy.process.net.calls_to_write", net_calls_to_write_stat},
89     {"proxy.process.net.calls_to_write_nodata", net_calls_to_write_nodata_stat},
90     {"proxy.process.net.calls_to_writetonet", net_calls_to_writetonet_stat},
91     {"proxy.process.net.calls_to_writetonet_afterpoll", net_calls_to_writetonet_afterpoll_stat},
92     {"proxy.process.net.inactivity_cop_lock_acquire_failure", inactivity_cop_lock_acquire_failure_stat},
93     {"proxy.process.net.net_handler_run", net_handler_run_stat},
94     {"proxy.process.net.read_bytes", net_read_bytes_stat},
95     {"proxy.process.net.write_bytes", net_write_bytes_stat},
96     {"proxy.process.net.fastopen_out.attempts", net_fastopen_attempts_stat},
97     {"proxy.process.net.fastopen_out.successes", net_fastopen_successes_stat},
98     {"proxy.process.socks.connections_successful", socks_connections_successful_stat},
99     {"proxy.process.socks.connections_unsuccessful", socks_connections_unsuccessful_stat},
100   };
101 
102   const std::pair<const char *, Net_Stats> non_persistent[] = {
103     {"proxy.process.net.accepts_currently_open", net_accepts_currently_open_stat},
104     {"proxy.process.net.connections_currently_open", net_connections_currently_open_stat},
105     {"proxy.process.net.default_inactivity_timeout_applied", default_inactivity_timeout_applied_stat},
106     {"proxy.process.net.default_inactivity_timeout_count", default_inactivity_timeout_count_stat},
107     {"proxy.process.net.dynamic_keep_alive_timeout_in_count", keep_alive_queue_timeout_count_stat},
108     {"proxy.process.net.dynamic_keep_alive_timeout_in_total", keep_alive_queue_timeout_total_stat},
109     {"proxy.process.socks.connections_currently_open", socks_connections_currently_open_stat},
110   };
111 
112   for (auto &p : persistent) {
113     RecRegisterRawStat(net_rsb, RECT_PROCESS, p.first, RECD_INT, RECP_PERSISTENT, p.second, RecRawStatSyncSum);
114   }
115 
116   for (auto &p : non_persistent) {
117     RecRegisterRawStat(net_rsb, RECT_PROCESS, p.first, RECD_INT, RECP_NON_PERSISTENT, p.second, RecRawStatSyncSum);
118   }
119 
120   NET_CLEAR_DYN_STAT(net_handler_run_stat);
121   NET_CLEAR_DYN_STAT(net_connections_currently_open_stat);
122   NET_CLEAR_DYN_STAT(net_accepts_currently_open_stat);
123   NET_CLEAR_DYN_STAT(net_calls_to_readfromnet_stat);
124   NET_CLEAR_DYN_STAT(net_calls_to_readfromnet_afterpoll_stat);
125   NET_CLEAR_DYN_STAT(net_calls_to_read_stat);
126   NET_CLEAR_DYN_STAT(net_calls_to_read_nodata_stat);
127   NET_CLEAR_DYN_STAT(net_calls_to_writetonet_stat);
128   NET_CLEAR_DYN_STAT(net_calls_to_writetonet_afterpoll_stat);
129   NET_CLEAR_DYN_STAT(net_calls_to_write_stat);
130   NET_CLEAR_DYN_STAT(net_calls_to_write_nodata_stat);
131   NET_CLEAR_DYN_STAT(socks_connections_currently_open_stat);
132   NET_CLEAR_DYN_STAT(keep_alive_queue_timeout_total_stat);
133   NET_CLEAR_DYN_STAT(keep_alive_queue_timeout_count_stat);
134   NET_CLEAR_DYN_STAT(default_inactivity_timeout_count_stat);
135   NET_CLEAR_DYN_STAT(default_inactivity_timeout_applied_stat);
136 
137   RecRegisterRawStat(net_rsb, RECT_PROCESS, "proxy.process.tcp.total_accepts", RECD_INT, RECP_NON_PERSISTENT,
138                      static_cast<int>(net_tcp_accept_stat), RecRawStatSyncSum);
139   NET_CLEAR_DYN_STAT(net_tcp_accept_stat);
140 
141   RecRegisterRawStat(net_rsb, RECT_PROCESS, "proxy.process.net.connections_throttled_in", RECD_INT, RECP_PERSISTENT,
142                      (int)net_connections_throttled_in_stat, RecRawStatSyncSum);
143   RecRegisterRawStat(net_rsb, RECT_PROCESS, "proxy.process.net.connections_throttled_out", RECD_INT, RECP_PERSISTENT,
144                      (int)net_connections_throttled_out_stat, RecRawStatSyncSum);
145   RecRegisterRawStat(net_rsb, RECT_PROCESS, "proxy.process.net.max.requests_throttled_in", RECD_INT, RECP_PERSISTENT,
146                      (int)net_requests_max_throttled_in_stat, RecRawStatSyncSum);
147 }
148 
149 void
ink_net_init(ts::ModuleVersion version)150 ink_net_init(ts::ModuleVersion version)
151 {
152   static int init_called = 0;
153 
154   ink_release_assert(version.check(NET_SYSTEM_MODULE_INTERNAL_VERSION));
155 
156   if (!init_called) {
157     // do one time stuff
158     // create a stat block for NetStats
159     net_rsb = RecAllocateRawStatBlock(static_cast<int>(Net_Stat_Count));
160     configure_net();
161     register_net_stats();
162   }
163 
164   init_called = 1;
165 }
166