/src/opensips/net/net_tcp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2014-2015 OpenSIPS Project |
3 | | * Copyright (C) 2001-2003 FhG Fokus |
4 | | * |
5 | | * This file is part of opensips, a free SIP server. |
6 | | * |
7 | | * opensips is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * opensips is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | | * |
21 | | * |
22 | | * history: |
23 | | * --------- |
24 | | * 2015-01-xx created (razvanc) |
25 | | */ |
26 | | |
27 | | #include <sys/types.h> |
28 | | #include <sys/socket.h> |
29 | | #include <netinet/in.h> |
30 | | #include <netinet/in_systm.h> |
31 | | #include <netinet/ip.h> |
32 | | #include <netinet/tcp.h> |
33 | | #include <sys/uio.h> /* writev*/ |
34 | | #include <netdb.h> |
35 | | #include <stdlib.h> /*exit() */ |
36 | | #include <time.h> /*time() */ |
37 | | #include <unistd.h> |
38 | | #include <errno.h> |
39 | | #include <string.h> |
40 | | #include <pthread.h> |
41 | | #include <stdint.h> |
42 | | |
43 | | #include "../mem/mem.h" |
44 | | #include "../mem/shm_mem.h" |
45 | | #include "../globals.h" |
46 | | #include "../locking.h" |
47 | | #include "../socket_info.h" |
48 | | #include "../ut.h" |
49 | | #include "../pt.h" |
50 | | #include "../pt_load.h" |
51 | | #include "../daemonize.h" |
52 | | #include "../status_report.h" |
53 | | #include "../reactor.h" |
54 | | #include "../timer.h" |
55 | | #include "../ipc.h" |
56 | | #include "../receive.h" |
57 | | #include "../lib/cond.h" |
58 | | #include "../cfg_reload.h" |
59 | | |
60 | | #include "tcp_passfd.h" |
61 | | #include "net_tcp_proc.h" |
62 | | #include "net_tcp_report.h" |
63 | | #include "net_tcp.h" |
64 | | #include "tcp_common.h" |
65 | | #include "tcp_conn.h" |
66 | | #include "tcp_conn_profile.h" |
67 | | #include "trans.h" |
68 | | #include "net_tcp_dbg.h" |
69 | | |
70 | | struct struct_hist_list *con_hist; |
71 | | |
72 | | enum tcp_worker_state { STATE_INACTIVE=0, STATE_ACTIVE, STATE_DRAINING}; |
73 | | |
74 | | static int tcpconn_prepare_write(struct tcp_connection *tcpconn); |
75 | | |
76 | | /* definition of a TCP worker - the array of these TCP workers is |
77 | | * mainly intended to be used by the TCP main, to keep track of the |
78 | | * workers, about their load and so. Nevertheless, since the addition |
79 | | * of the process auto-scaling, other processes may need access to this |
80 | | * data, thus it's relocation in SHM (versus initial PKG). For example, |
81 | | * the attendant process is the one forking new TCP workers (scaling up), |
82 | | * so it must be able to set the ENABLE state for the TCP worker (and being |
83 | | * (seen by the TCP main proc). Similar, when a TCP worker shuts down, it has |
84 | | * to mark itself as DISABLED and the TCP main must see that. |
85 | | * Again, 99% this array is intended for TCP Main ops, it is not lock |
86 | | * protected, so be very careful with any ops from other procs. |
87 | | */ |
88 | | struct tcp_worker { |
89 | | pid_t pid; |
90 | | int pt_idx; /*!< Index in the main Process Table */ |
91 | | enum tcp_worker_state state; |
92 | | }; |
93 | | |
94 | | /* definition of a TCP partition */ |
95 | | struct tcp_partition { |
96 | | /*! \brief connection hash table (after ip&port), includes also aliases */ |
97 | | struct tcp_conn_alias** tcpconn_aliases_hash; |
98 | | /*! \brief connection hash table (after connection id) */ |
99 | | struct tcp_connection** tcpconn_id_hash; |
100 | | gen_lock_t* tcpconn_lock; |
101 | | }; |
102 | | |
103 | | |
104 | | /* array of TCP workers - to be used only by TCP MAIN */ |
105 | | struct tcp_worker *tcp_workers=0; |
106 | | static int tcp_dispatch_sock[2] = { -1, -1 }; |
107 | | |
108 | | /* unique for each connection, used for |
109 | | * quickly finding the corresponding connection for a reply */ |
110 | | static unsigned int* connection_id=0; |
111 | | static int *tcp_main_proc_no = 0; |
112 | | |
113 | | /* connections are created both by TCP main (on accept) and by any other |
114 | | * process (on connect), so the counter above must be incremented under lock - |
115 | | * otherwise concurrent creations may end up sharing the same connection ID */ |
116 | | static gen_lock_t* connection_id_lock=0; |
117 | | |
118 | | /* array of TCP partitions */ |
119 | | static struct tcp_partition tcp_parts[TCP_PARTITION_SIZE]; |
120 | | |
121 | | /*!< current number of open connections */ |
122 | | static unsigned int *tcp_connections_no = 0; |
123 | | static gen_lock_t *tcp_connections_lock = 0; |
124 | | |
125 | | /*!< by default don't accept aliases */ |
126 | | int tcp_accept_aliases=0; |
127 | | int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT; |
128 | | int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME; |
129 | | int tcp_socket_backlog=DEFAULT_TCP_SOCKET_BACKLOG; |
130 | | /*!< by default choose the best method */ |
131 | | enum poll_types tcp_poll_method=0; |
132 | | int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS; |
133 | | /* the configured/starting number of TCP workers */ |
134 | | int tcp_workers_no = UDP_WORKERS_NO; |
135 | | /* the maximum numbers of TCP workers */ |
136 | | int tcp_workers_max_no; |
137 | | /* the name of the auto-scaling profile (optional) */ |
138 | | char* tcp_auto_scaling_profile = NULL; |
139 | | /* Max number of seconds that we expect a full SIP message |
140 | | * to arrive in. Anything above will close the connection. */ |
141 | | int tcp_max_msg_time = TCP_CHILD_MAX_MSG_TIME; |
142 | | #ifdef HAVE_SO_KEEPALIVE |
143 | | int tcp_keepalive = 1; |
144 | | #else |
145 | | int tcp_keepalive = 0; |
146 | | #endif |
147 | | int tcp_keepcount = 0; |
148 | | int tcp_keepidle = 0; |
149 | | int tcp_keepinterval = 0; |
150 | | |
151 | | /*!< should we allow opening a new TCP conn when sending data |
152 | | * over UAC branches? - branch flag to be set in the SIP messages */ |
153 | | int tcp_no_new_conn_bflag = 0; |
154 | | /*!< should we allow opening a new TCP conn when sending data |
155 | | * back to UAS (replies)? - msg flag to be set in the SIP messages */ |
156 | | int tcp_no_new_conn_rplflag = 0; |
157 | | /*!< should a new TCP conn be open if needed? - variable used to used for |
158 | | * signalizing between SIP layer (branch flag) and TCP layer (tcp_send func)*/ |
159 | | int tcp_no_new_conn = 0; |
160 | | int tcp_threads = 0; |
161 | | |
162 | | /* if the TCP net layer is on or off (if no TCP based protos are loaded) */ |
163 | | static int tcp_disabled = 1; |
164 | | |
165 | | /* is the process TCP MAIN ? */ |
166 | | int is_tcp_main = 0; |
167 | | |
168 | | /* the ID of the TCP conn used for the last send operation in the |
169 | | * current process - attention, this is a really ugly HACK here */ |
170 | | unsigned int last_outgoing_tcp_id = 0; |
171 | | |
172 | | static struct scaling_profile *s_profile = NULL; |
173 | | |
174 | | /****************************** helper functions *****************************/ |
175 | | extern void handle_sigs(void); |
176 | | |
177 | | static inline int init_sock_keepalive(int s, const struct tcp_conn_profile *prof) |
178 | 0 | { |
179 | 0 | int ka; |
180 | 0 | #if defined(HAVE_TCP_KEEPINTVL) || defined(HAVE_TCP_KEEPIDLE) || defined(HAVE_TCP_KEEPCNT) |
181 | 0 | int optval; |
182 | 0 | #endif |
183 | |
|
184 | 0 | if (prof->keepinterval || prof->keepidle || prof->keepcount) |
185 | 0 | ka = 1; /* force on */ |
186 | 0 | else |
187 | 0 | ka = prof->keepalive; |
188 | |
|
189 | 0 | #ifdef HAVE_SO_KEEPALIVE |
190 | 0 | if (setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,&ka,sizeof(ka))<0){ |
191 | 0 | LM_WARN("setsockopt failed to enable SO_KEEPALIVE: %s\n", |
192 | 0 | strerror(errno)); |
193 | 0 | return -1; |
194 | 0 | } |
195 | 0 | LM_DBG("TCP keepalive enabled on socket %d\n",s); |
196 | 0 | #endif |
197 | 0 | #ifdef HAVE_TCP_KEEPINTVL |
198 | 0 | if ((optval = prof->keepinterval)) { |
199 | 0 | if (setsockopt(s,IPPROTO_TCP,TCP_KEEPINTVL,&optval,sizeof(optval))<0){ |
200 | 0 | LM_WARN("setsockopt failed to set keepalive probes interval: %s\n", |
201 | 0 | strerror(errno)); |
202 | 0 | } |
203 | 0 | } |
204 | 0 | #endif |
205 | 0 | #ifdef HAVE_TCP_KEEPIDLE |
206 | 0 | if ((optval = prof->keepidle)) { |
207 | 0 | if (setsockopt(s,IPPROTO_TCP,TCP_KEEPIDLE,&optval,sizeof(optval))<0){ |
208 | 0 | LM_WARN("setsockopt failed to set keepalive idle interval: %s\n", |
209 | 0 | strerror(errno)); |
210 | 0 | } |
211 | 0 | } |
212 | 0 | #endif |
213 | 0 | #ifdef HAVE_TCP_KEEPCNT |
214 | 0 | if ((optval = prof->keepcount)) { |
215 | 0 | if (setsockopt(s,IPPROTO_TCP,TCP_KEEPCNT,&optval,sizeof(optval))<0){ |
216 | 0 | LM_WARN("setsockopt failed to set maximum keepalive count: %s\n", |
217 | 0 | strerror(errno)); |
218 | 0 | } |
219 | 0 | } |
220 | 0 | #endif |
221 | 0 | return 0; |
222 | 0 | } |
223 | | |
224 | | static inline void set_sock_reuseport(int s) |
225 | 0 | { |
226 | 0 | int yes = 1; |
227 | |
|
228 | 0 | if (setsockopt(s,SOL_SOCKET,SO_REUSEPORT,&yes,sizeof(yes))<0){ |
229 | 0 | LM_WARN("setsockopt failed to set SO_REUSEPORT: %s\n", |
230 | 0 | strerror(errno)); |
231 | 0 | } |
232 | 0 | if (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes))<0){ |
233 | 0 | LM_WARN("setsockopt failed to set SO_REUSEADDR: %s\n", |
234 | 0 | strerror(errno)); |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | /*! \brief Set all socket/fd options: disable nagle, tos lowdelay, |
239 | | * non-blocking |
240 | | * \return -1 on error */ |
241 | | int tcp_init_sock_opt(int s, const struct tcp_conn_profile *prof, enum si_flags socketflags, int sock_tos) |
242 | 0 | { |
243 | 0 | int flags; |
244 | 0 | int optval; |
245 | |
|
246 | 0 | #ifdef DISABLE_NAGLE |
247 | 0 | flags=1; |
248 | 0 | if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)) < 0){ |
249 | 0 | LM_WARN("could not disable Nagle: %s\n", strerror(errno)); |
250 | 0 | } |
251 | 0 | #endif |
252 | | /* tos*/ |
253 | 0 | optval = (sock_tos > 0) ? sock_tos : tos; |
254 | 0 | if (optval > 0) { |
255 | 0 | if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){ |
256 | 0 | LM_WARN("setsockopt tos: %s\n", strerror(errno)); |
257 | | /* continue since this is not critical */ |
258 | 0 | } |
259 | 0 | } |
260 | |
|
261 | 0 | if (probe_max_sock_buff(s,1,MAX_SEND_BUFFER_SIZE,BUFFER_INCREMENT)) { |
262 | 0 | LM_WARN("setsockopt tcp snd buff: %s\n", strerror(errno)); |
263 | | /* continue since this is not critical */ |
264 | 0 | } |
265 | |
|
266 | 0 | init_sock_keepalive(s, prof); |
267 | 0 | if (socketflags & SI_REUSEPORT) |
268 | 0 | set_sock_reuseport(s); |
269 | | |
270 | | /* non-blocking */ |
271 | 0 | flags=fcntl(s, F_GETFL); |
272 | 0 | if (flags==-1){ |
273 | 0 | LM_ERR("fcntl failed: (%d) %s\n", errno, strerror(errno)); |
274 | 0 | goto error; |
275 | 0 | } |
276 | 0 | if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){ |
277 | 0 | LM_ERR("set non-blocking failed: (%d) %s\n", errno, strerror(errno)); |
278 | 0 | goto error; |
279 | 0 | } |
280 | 0 | return 0; |
281 | 0 | error: |
282 | 0 | return -1; |
283 | 0 | } |
284 | | |
285 | | struct tcp_ipc_payload { |
286 | | struct receive_info rcv; |
287 | | struct tcp_connection *conn; |
288 | | int msg_len; |
289 | | int data_len; |
290 | | char msg_buf[0]; |
291 | | }; |
292 | | |
293 | | int tcp_dispatch_msg(char *msg, int len, |
294 | | struct receive_info *rcv, const void *data, int data_len) |
295 | 0 | { |
296 | 0 | struct tcp_ipc_payload *payload; |
297 | 0 | struct tcp_connection *conn = NULL; |
298 | 0 | unsigned int alloc_len; |
299 | 0 | int n; |
300 | 0 | uintptr_t payload_ptr; |
301 | |
|
302 | 0 | if (len < 0) { |
303 | 0 | LM_BUG("negative TCP message length: %d\n", len); |
304 | 0 | return -1; |
305 | 0 | } |
306 | 0 | if (data_len < 0) { |
307 | 0 | LM_BUG("negative TCP dispatch data length: %d\n", data_len); |
308 | 0 | return -1; |
309 | 0 | } |
310 | 0 | if (data_len && !data) { |
311 | 0 | LM_BUG("missing TCP dispatch data buffer for %d bytes\n", data_len); |
312 | 0 | return -1; |
313 | 0 | } |
314 | | |
315 | 0 | alloc_len = sizeof(*payload) + len + 1 + data_len; |
316 | 0 | payload = shm_malloc(alloc_len); |
317 | 0 | if (!payload) { |
318 | 0 | LM_ERR("oom while allocating TCP IPC payload (%u bytes)\n", alloc_len); |
319 | 0 | return -1; |
320 | 0 | } |
321 | | |
322 | 0 | memcpy(&payload->rcv, rcv, sizeof(payload->rcv)); |
323 | 0 | payload->conn = NULL; |
324 | 0 | payload->msg_len = len; |
325 | 0 | payload->data_len = data_len; |
326 | 0 | memcpy(payload->msg_buf, msg, len); |
327 | 0 | payload->msg_buf[len] = '\0'; |
328 | 0 | if (data_len) |
329 | 0 | memcpy(payload->msg_buf + len + 1, data, data_len); |
330 | |
|
331 | 0 | if (rcv->proto_reserved1 && |
332 | 0 | tcp_conn_get(rcv->proto_reserved1, NULL, 0, PROTO_NONE, |
333 | 0 | NULL, &conn, NULL) > 0) { |
334 | 0 | payload->conn = conn; |
335 | 0 | } |
336 | |
|
337 | 0 | payload_ptr = (uintptr_t)payload; |
338 | 0 | n = send(tcp_dispatch_sock[1], &payload_ptr, sizeof(payload_ptr), 0); |
339 | 0 | if (n != (int)sizeof(payload_ptr)) { |
340 | 0 | LM_ERR("failed to dispatch TCP message to worker socket: %s\n", |
341 | 0 | (n < 0) ? strerror(errno) : "short write"); |
342 | 0 | if (payload->conn) |
343 | 0 | tcpconn_put(payload->conn); |
344 | 0 | shm_free(payload); |
345 | 0 | return -1; |
346 | 0 | } |
347 | | |
348 | 0 | return 0; |
349 | 0 | } |
350 | | |
351 | | enum tcp_job_op { |
352 | | TCP_READ_JOB = 1, |
353 | | TCP_WRITE_JOB = 2, |
354 | | TCP_RUN_JOB = 3, |
355 | | }; |
356 | | |
357 | | struct tcp_job { |
358 | | struct tcp_connection *conn; |
359 | | int op; |
360 | | tcp_thread_job_f run; |
361 | | void *data; |
362 | | long resp; |
363 | | int ret; |
364 | | struct tcp_job *next; |
365 | | }; |
366 | | |
367 | | static struct tcp_pool { |
368 | | pthread_t *threads; |
369 | | int threads_no; |
370 | | int stop; |
371 | | struct tcp_job *task_head; |
372 | | struct tcp_job *task_tail; |
373 | | |
374 | | pthread_mutex_t done_lock; |
375 | | struct tcp_job *done_head; |
376 | | struct tcp_job *done_tail; |
377 | | |
378 | | int notify_pipe[2]; |
379 | | } tcp_pool = { |
380 | | .threads = NULL, |
381 | | .threads_no = 0, |
382 | | .stop = 0, |
383 | | .task_head = NULL, |
384 | | .task_tail = NULL, |
385 | | .done_lock = PTHREAD_MUTEX_INITIALIZER, |
386 | | .done_head = NULL, |
387 | | .done_tail = NULL, |
388 | | .notify_pipe = {-1, -1}, |
389 | | }; |
390 | | |
391 | | struct tcp_shared_write_queue { |
392 | | gen_cond_t cond; |
393 | | struct tcp_connection *head; |
394 | | struct tcp_connection *tail; |
395 | | }; |
396 | | |
397 | | static struct tcp_shared_write_queue *tcp_write_queue = NULL; |
398 | | |
399 | | static inline int tcp_threads_active(void) |
400 | 0 | { |
401 | 0 | return tcp_pool.threads_no > 0; |
402 | 0 | } |
403 | | |
404 | | int tcp_write_in_main(void) |
405 | 0 | { |
406 | | /* This is a process-independent policy: TCP writes are handled by the |
407 | | * dedicated TCP main process, regardless of the caller process. */ |
408 | 0 | return !tcp_disabled; |
409 | 0 | } |
410 | | |
411 | | |
412 | | |
413 | | /********************** TCP conn management functions ************************/ |
414 | | |
415 | | /* initializes an already defined TCP listener */ |
416 | | int tcp_init_listener(struct socket_info *si) |
417 | 0 | { |
418 | 0 | union sockaddr_union* addr = &si->su; |
419 | |
|
420 | 0 | if (init_su(addr, &si->address, si->port_no)<0){ |
421 | 0 | LM_ERR("could no init sockaddr_union\n"); |
422 | 0 | return -1; |
423 | 0 | } |
424 | | |
425 | 0 | return 0; |
426 | 0 | } |
427 | | |
428 | | /* binding an defined TCP listener */ |
429 | | int tcp_bind_listener(struct socket_info *si) |
430 | 0 | { |
431 | 0 | union sockaddr_union* addr; |
432 | 0 | int optval; |
433 | 0 | #ifdef DISABLE_NAGLE |
434 | 0 | int flag; |
435 | 0 | #endif |
436 | |
|
437 | 0 | addr = &si->su; |
438 | 0 | si->socket = socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0); |
439 | 0 | if (si->socket==-1){ |
440 | 0 | LM_ERR("socket failed with [%s]\n", strerror(errno)); |
441 | 0 | goto error; |
442 | 0 | } |
443 | 0 | #ifdef DISABLE_NAGLE |
444 | 0 | flag=1; |
445 | 0 | if (setsockopt(si->socket, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) < 0){ |
446 | 0 | LM_ERR("could not disable Nagle: %s\n",strerror(errno)); |
447 | 0 | } |
448 | 0 | #endif |
449 | |
|
450 | 0 | #if !defined(TCP_DONT_REUSEADDR) |
451 | | /* Stevens, "Network Programming", Section 7.5, "Generic Socket |
452 | | * Options": "...server started,..a child continues..on existing |
453 | | * connection..listening server is restarted...call to bind fails |
454 | | * ... ALL TCP servers should specify the SO_REUSEADDRE option |
455 | | * to allow the server to be restarted in this situation |
456 | | */ |
457 | 0 | optval=1; |
458 | 0 | if (setsockopt(si->socket, SOL_SOCKET, SO_REUSEADDR, |
459 | 0 | (void*)&optval, sizeof(optval))==-1) { |
460 | 0 | LM_ERR("setsockopt failed with [%s]\n", strerror(errno)); |
461 | 0 | goto error; |
462 | 0 | } |
463 | 0 | #endif |
464 | | /* tos */ |
465 | 0 | optval = (si->tos > 0) ? si->tos : tos; |
466 | 0 | if (optval > 0) { |
467 | 0 | if (setsockopt(si->socket, IPPROTO_IP, IP_TOS, (void*)&optval, |
468 | 0 | sizeof(optval)) ==-1){ |
469 | 0 | LM_WARN("setsockopt tos: %s\n", strerror(errno)); |
470 | | /* continue since this is not critical */ |
471 | 0 | } |
472 | 0 | } |
473 | |
|
474 | 0 | if (probe_max_sock_buff(si->socket,1,MAX_SEND_BUFFER_SIZE, |
475 | 0 | BUFFER_INCREMENT)) { |
476 | 0 | LM_WARN("setsockopt tcp snd buff: %s\n",strerror(errno)); |
477 | | /* continue since this is not critical */ |
478 | 0 | } |
479 | |
|
480 | 0 | init_sock_keepalive(si->socket, &tcp_con_df_profile); |
481 | 0 | if (si->flags & SI_REUSEPORT) |
482 | 0 | set_sock_reuseport(si->socket); |
483 | 0 | if (bind(si->socket, &addr->s, sockaddru_len(*addr))==-1){ |
484 | 0 | LM_ERR("bind(%x, %p, %d) on %s:%d : %s\n", |
485 | 0 | si->socket, &addr->s, |
486 | 0 | (unsigned)sockaddru_len(*addr), |
487 | 0 | si->address_str.s, |
488 | 0 | si->port_no, |
489 | 0 | strerror(errno)); |
490 | 0 | goto error; |
491 | 0 | } |
492 | 0 | if (listen(si->socket, tcp_socket_backlog)==-1){ |
493 | 0 | LM_ERR("listen(%x, %p, %d) on %s: %s\n", |
494 | 0 | si->socket, &addr->s, |
495 | 0 | (unsigned)sockaddru_len(*addr), |
496 | 0 | si->address_str.s, |
497 | 0 | strerror(errno)); |
498 | 0 | goto error; |
499 | 0 | } |
500 | | |
501 | 0 | return 0; |
502 | 0 | error: |
503 | 0 | if (si->socket!=-1){ |
504 | 0 | close(si->socket); |
505 | 0 | si->socket=-1; |
506 | 0 | } |
507 | 0 | return -1; |
508 | 0 | } |
509 | | |
510 | | |
511 | | /*! \brief finds a connection, if id=0 return NULL |
512 | | * \note WARNING: unprotected (locks) use tcpconn_get unless you really |
513 | | * know what you are doing */ |
514 | | static struct tcp_connection* _tcpconn_find(unsigned int id) |
515 | 0 | { |
516 | 0 | struct tcp_connection *c; |
517 | 0 | unsigned hash; |
518 | |
|
519 | 0 | if (id){ |
520 | 0 | hash=tcp_id_hash(id); |
521 | 0 | for (c=TCP_PART(id).tcpconn_id_hash[hash]; c; c=c->id_next){ |
522 | | #ifdef EXTRA_DEBUG |
523 | | LM_DBG("c=%p, c->id=%u, port=%d\n",c, c->id, c->rcv.src_port); |
524 | | print_ip("ip=", &c->rcv.src_ip, "\n"); |
525 | | #endif |
526 | 0 | if ((id==c->id) && c->state!=S_CONN_BAD && |
527 | 0 | !(c->flags & F_CONN_FORCE_CLOSED)) |
528 | 0 | return c; |
529 | 0 | } |
530 | 0 | } |
531 | 0 | return 0; |
532 | 0 | } |
533 | | |
534 | | |
535 | | /* returns the correlation ID of a TCP connection */ |
536 | | int tcp_get_correlation_id( unsigned int id, unsigned long long *cid) |
537 | 0 | { |
538 | 0 | struct tcp_connection* c; |
539 | |
|
540 | 0 | TCPCONN_LOCK(id); |
541 | 0 | if ( (c=_tcpconn_find(id))!=NULL ) { |
542 | 0 | *cid = c->cid; |
543 | 0 | TCPCONN_UNLOCK(id); |
544 | 0 | return 0; |
545 | 0 | } |
546 | 0 | *cid = 0; |
547 | 0 | TCPCONN_UNLOCK(id); |
548 | 0 | return -1; |
549 | 0 | } |
550 | | |
551 | | /* returns the correlation ID of a TCP connection */ |
552 | | int tcp_get_rcv( unsigned int id, struct receive_info *ri) |
553 | 0 | { |
554 | 0 | struct tcp_connection* c; |
555 | |
|
556 | 0 | TCPCONN_LOCK(id); |
557 | 0 | if ( (c=_tcpconn_find(id))!=NULL ) { |
558 | 0 | memcpy(ri, &c->rcv, sizeof *ri); |
559 | 0 | TCPCONN_UNLOCK(id); |
560 | 0 | return 0; |
561 | 0 | } |
562 | 0 | TCPCONN_UNLOCK(id); |
563 | 0 | return -1; |
564 | 0 | } |
565 | | |
566 | | int tcp_get_main_proc_no(void) |
567 | 0 | { |
568 | 0 | return tcp_main_proc_no ? *tcp_main_proc_no : -1; |
569 | 0 | } |
570 | | |
571 | | |
572 | | /*! \brief _tcpconn_find with locks and acquire a shared connection reference */ |
573 | | int tcp_conn_get(unsigned int id, struct ip_addr* ip, int port, |
574 | | enum sip_protos proto, void *proto_extra_id, |
575 | | struct tcp_connection** conn, const struct socket_info* send_sock) |
576 | 0 | { |
577 | 0 | struct tcp_connection* c; |
578 | 0 | struct tcp_conn_alias* a; |
579 | 0 | unsigned hash; |
580 | 0 | unsigned int part; |
581 | |
|
582 | 0 | if (id) { |
583 | 0 | part = id; |
584 | 0 | TCPCONN_LOCK(part); |
585 | 0 | if ( (c=_tcpconn_find(part))!=NULL ) |
586 | 0 | goto found; |
587 | 0 | TCPCONN_UNLOCK(part); |
588 | 0 | } |
589 | | |
590 | | /* continue search based on IP address + port + transport */ |
591 | | #ifdef EXTRA_DEBUG |
592 | | LM_DBG("%d port %u\n",id, port); |
593 | | if (ip) print_ip("tcpconn_find: ip ", ip, "\n"); |
594 | | #endif |
595 | 0 | if (ip){ |
596 | 0 | hash=tcp_addr_hash(ip, port); |
597 | 0 | for( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) { |
598 | 0 | TCPCONN_LOCK(part); |
599 | 0 | for (a=TCP_PART(part).tcpconn_aliases_hash[hash]; a; a=a->next) { |
600 | | #ifdef EXTRA_DEBUG |
601 | | LM_DBG("a=%p, c=%p, c->id=%u, alias port= %d port=%d\n", |
602 | | a, a->parent, a->parent->id, a->port, |
603 | | a->parent->rcv.src_port); |
604 | | print_ip("ip=",&a->parent->rcv.src_ip,"\n"); |
605 | | if (send_sock && a->parent->rcv.bind_address) { |
606 | | print_ip("requested send_sock ip=", &send_sock->address,"\n"); |
607 | | print_ip("found send_sock ip=", &a->parent->rcv.bind_address->address,"\n"); |
608 | | } |
609 | | #endif |
610 | 0 | c = a->parent; |
611 | 0 | if (c->state != S_CONN_BAD && |
612 | 0 | !(c->flags & F_CONN_FORCE_CLOSED) && |
613 | 0 | ((c->flags & F_CONN_INIT) || |
614 | 0 | (c->state == S_CONN_CONNECTING && c->fd == -1)) && |
615 | 0 | (send_sock==NULL || send_sock == a->parent->rcv.bind_address) && |
616 | 0 | port == a->port && |
617 | 0 | proto == c->type && |
618 | 0 | ip_addr_cmp(ip, &c->rcv.src_ip) && |
619 | 0 | (proto_extra_id == NULL || |
620 | 0 | ((c->flags & F_CONN_INIT) && |
621 | 0 | (protos[proto].net.stream.conn.match == NULL || |
622 | 0 | protos[proto].net.stream.conn.match(c, proto_extra_id)))) ) |
623 | 0 | goto found; |
624 | 0 | } |
625 | 0 | TCPCONN_UNLOCK(part); |
626 | 0 | } |
627 | 0 | } |
628 | | |
629 | | /* not found */ |
630 | 0 | *conn = NULL; |
631 | 0 | return 0; |
632 | | |
633 | 0 | found: |
634 | 0 | c->refcnt++; |
635 | 0 | TCPCONN_UNLOCK(part); |
636 | 0 | sh_log(c->hist, TCP_REF, "tcp_conn_get, (%d)", c->refcnt); |
637 | |
|
638 | 0 | LM_DBG("con found in state %d\n",c->state); |
639 | |
|
640 | 0 | *conn = c; |
641 | 0 | return 1; |
642 | 0 | } |
643 | | |
644 | | |
645 | | /* used to tune the tcp_connection attributes - not to be used inside the |
646 | | network layer, but onlu from the above layer (otherwise we may end up |
647 | | in strange deadlocks!) */ |
648 | | int tcp_conn_fcntl(struct receive_info *rcv, int attr, void *value) |
649 | 0 | { |
650 | 0 | struct tcp_connection *con; |
651 | |
|
652 | 0 | switch (attr) { |
653 | 0 | case DST_FCNTL_SET_LIFETIME: |
654 | | /* set connection timeout */ |
655 | 0 | TCPCONN_LOCK(rcv->proto_reserved1); |
656 | 0 | con =_tcpconn_find(rcv->proto_reserved1); |
657 | 0 | if (!con) { |
658 | 0 | LM_ERR("Strange, tcp conn not found (id=%u)\n", |
659 | 0 | rcv->proto_reserved1); |
660 | 0 | } else { |
661 | 0 | tcp_conn_set_lifetime( con, (int)(long)(value)); |
662 | 0 | } |
663 | 0 | TCPCONN_UNLOCK(rcv->proto_reserved1); |
664 | 0 | return 0; |
665 | 0 | default: |
666 | 0 | LM_ERR("unsupported operation %d on conn\n",attr); |
667 | 0 | return -1; |
668 | 0 | } |
669 | 0 | return -1; |
670 | 0 | } |
671 | | |
672 | | |
673 | | static struct tcp_connection* tcpconn_add(struct tcp_connection *c) |
674 | 0 | { |
675 | 0 | unsigned hash; |
676 | |
|
677 | 0 | if (c){ |
678 | 0 | TCPCONN_LOCK(c->id); |
679 | | /* add it at the beginning of the list*/ |
680 | 0 | hash=tcp_id_hash(c->id); |
681 | 0 | c->id_hash=hash; |
682 | 0 | tcpconn_listadd(TCP_PART(c->id).tcpconn_id_hash[hash], c, id_next, |
683 | 0 | id_prev); |
684 | |
|
685 | 0 | hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port); |
686 | | /* set the first alias */ |
687 | 0 | c->con_aliases[0].port=c->rcv.src_port; |
688 | 0 | c->con_aliases[0].hash=hash; |
689 | 0 | c->con_aliases[0].parent=c; |
690 | 0 | tcpconn_listadd(TCP_PART(c->id).tcpconn_aliases_hash[hash], |
691 | 0 | &c->con_aliases[0], next, prev); |
692 | 0 | c->aliases++; |
693 | 0 | c->flags |= F_CONN_HASHED; |
694 | 0 | TCPCONN_UNLOCK(c->id); |
695 | 0 | LM_DBG("hashes: %d, %d\n", hash, c->id_hash); |
696 | 0 | return c; |
697 | 0 | }else{ |
698 | 0 | LM_CRIT("null connection pointer\n"); |
699 | 0 | return 0; |
700 | 0 | } |
701 | 0 | } |
702 | | |
703 | | static str e_tcp_src_ip = str_init("src_ip"); |
704 | | static str e_tcp_src_port = str_init("src_port"); |
705 | | static str e_tcp_dst_ip = str_init("dst_ip"); |
706 | | static str e_tcp_dst_port = str_init("dst_port"); |
707 | | static str e_tcp_c_proto = str_init("proto"); |
708 | | |
709 | | static void tcp_disconnect_event_raise(struct tcp_connection* c) |
710 | 0 | { |
711 | 0 | evi_params_p list = 0; |
712 | 0 | str src_ip,dst_ip, proto; |
713 | 0 | int src_port,dst_port; |
714 | 0 | char src_ip_buf[IP_ADDR_MAX_STR_SIZE],dst_ip_buf[IP_ADDR_MAX_STR_SIZE]; |
715 | | |
716 | | // event has to be triggered - check for subscribers |
717 | 0 | if (!evi_probe_event(EVI_TCP_DISCONNECT)) { |
718 | 0 | goto end; |
719 | 0 | } |
720 | | |
721 | 0 | if (!(list = evi_get_params())) |
722 | 0 | goto end; |
723 | | |
724 | 0 | src_ip.s = ip_addr2a( &c->rcv.src_ip ); |
725 | 0 | memcpy(src_ip_buf,src_ip.s,IP_ADDR_MAX_STR_SIZE); |
726 | 0 | src_ip.s = src_ip_buf; |
727 | 0 | src_ip.len = strlen(src_ip.s); |
728 | |
|
729 | 0 | if (evi_param_add_str(list, &e_tcp_src_ip, &src_ip)) { |
730 | 0 | LM_ERR("unable to add parameter\n"); |
731 | 0 | goto end; |
732 | 0 | } |
733 | | |
734 | 0 | src_port = c->rcv.src_port; |
735 | |
|
736 | 0 | if (evi_param_add_int(list, &e_tcp_src_port, &src_port)) { |
737 | 0 | LM_ERR("unable to add parameter\n"); |
738 | 0 | goto end; |
739 | 0 | } |
740 | | |
741 | 0 | dst_ip.s = ip_addr2a( &c->rcv.dst_ip ); |
742 | 0 | memcpy(dst_ip_buf,dst_ip.s,IP_ADDR_MAX_STR_SIZE); |
743 | 0 | dst_ip.s = dst_ip_buf; |
744 | 0 | dst_ip.len = strlen(dst_ip.s); |
745 | |
|
746 | 0 | if (evi_param_add_str(list, &e_tcp_dst_ip, &dst_ip)) { |
747 | 0 | LM_ERR("unable to add parameter\n"); |
748 | 0 | goto end; |
749 | 0 | } |
750 | | |
751 | 0 | dst_port = c->rcv.dst_port; |
752 | |
|
753 | 0 | if (evi_param_add_int(list, &e_tcp_dst_port, &dst_port)) { |
754 | 0 | LM_ERR("unable to add parameter\n"); |
755 | 0 | goto end; |
756 | 0 | } |
757 | | |
758 | 0 | proto.s = protos[c->rcv.proto].name; |
759 | 0 | proto.len = strlen(proto.s); |
760 | |
|
761 | 0 | if (evi_param_add_str(list, &e_tcp_c_proto, &proto)) { |
762 | 0 | LM_ERR("unable to add parameter\n"); |
763 | 0 | goto end; |
764 | 0 | } |
765 | | |
766 | 0 | if (is_tcp_main) { |
767 | 0 | if (evi_dispatch_event(EVI_TCP_DISCONNECT, list)) { |
768 | 0 | LM_ERR("unable to dispatch tcp disconnect event\n"); |
769 | 0 | } |
770 | 0 | } else { |
771 | 0 | if (evi_raise_event(EVI_TCP_DISCONNECT, list)) { |
772 | 0 | LM_ERR("unable to send tcp disconnect event\n"); |
773 | 0 | } |
774 | 0 | } |
775 | 0 | list = 0; |
776 | |
|
777 | 0 | end: |
778 | 0 | if (list) |
779 | 0 | evi_free_params(list); |
780 | 0 | } |
781 | | |
782 | | /* convenience macro to aid in shm_free() debugging */ |
783 | | #define _tcpconn_rm(c, ne) \ |
784 | 0 | do {\ |
785 | 0 | __tcpconn_rm(c, ne);\ |
786 | 0 | shm_free(c);\ |
787 | 0 | } while (0) |
788 | | |
789 | | struct tcp_req *tcp_conn_get_req(struct tcp_connection *c) |
790 | 0 | { |
791 | 0 | if (!c) |
792 | 0 | return NULL; |
793 | | |
794 | 0 | if (c->con_req) |
795 | 0 | return c->con_req; |
796 | | |
797 | 0 | c->con_req = thread_malloc(sizeof(*c->con_req)); |
798 | 0 | if (!c->con_req) { |
799 | 0 | LM_ERR("failed to allocate TCP request buffer for connection %u\n", |
800 | 0 | c->id); |
801 | 0 | return NULL; |
802 | 0 | } |
803 | 0 | memset(c->con_req, 0, sizeof(*c->con_req)); |
804 | |
|
805 | 0 | return c->con_req; |
806 | 0 | } |
807 | | |
808 | | void tcp_conn_destroy_req(struct tcp_connection *c) |
809 | 0 | { |
810 | 0 | if (!c || !c->con_req) |
811 | 0 | return; |
812 | | |
813 | 0 | thread_free(c->con_req); |
814 | 0 | c->con_req = NULL; |
815 | 0 | } |
816 | | |
817 | | /*! \brief unsafe tcpconn_rm version (nolocks) */ |
818 | | static void __tcpconn_rm(struct tcp_connection* c, int no_event) |
819 | 0 | { |
820 | 0 | int r; |
821 | |
|
822 | 0 | if (c->flags & F_CONN_HASHED) { |
823 | 0 | tcpconn_listrm(TCP_PART(c->id).tcpconn_id_hash[c->id_hash], c, |
824 | 0 | id_next, id_prev); |
825 | | /* remove all the aliases */ |
826 | 0 | for (r=0; r<c->aliases; r++) |
827 | 0 | tcpconn_listrm(TCP_PART(c->id).tcpconn_aliases_hash[ |
828 | 0 | c->con_aliases[r].hash], &c->con_aliases[r], next, prev); |
829 | 0 | c->flags &= ~F_CONN_HASHED; |
830 | 0 | } |
831 | 0 | lock_destroy(&c->write_lock); |
832 | |
|
833 | 0 | if (c->async) { |
834 | 0 | for (r = 0; r<c->async->pending; r++) |
835 | 0 | shm_free(c->async->chunks[r]); |
836 | 0 | shm_free(c->async); |
837 | 0 | c->async = NULL; |
838 | 0 | } |
839 | |
|
840 | 0 | lock_get(tcp_connections_lock); |
841 | 0 | (*tcp_connections_no)--; |
842 | 0 | lock_release(tcp_connections_lock); |
843 | | |
844 | | /* Only TCP main has valid process-private connection state. */ |
845 | 0 | if (is_tcp_main) { |
846 | 0 | if (c->proto_req) |
847 | 0 | thread_free(c->proto_req); |
848 | 0 | c->proto_req = NULL; |
849 | 0 | tcp_conn_destroy_req(c); |
850 | |
|
851 | 0 | if (protos[c->type].net.stream.conn.clean) |
852 | 0 | protos[c->type].net.stream.conn.clean(c); |
853 | 0 | } |
854 | |
|
855 | 0 | if (!no_event) tcp_disconnect_event_raise(c); |
856 | |
|
857 | | #ifdef DBG_TCPCON |
858 | | sh_log(c->hist, TCP_DESTROY, "type=%d", c->type); |
859 | | sh_unref(c->hist); |
860 | | c->hist = NULL; |
861 | | #endif |
862 | | |
863 | | /* shm_free(c); -- freed by _tcpconn_rm() */ |
864 | 0 | } |
865 | | |
866 | | /*! \brief add port as an alias for the "id" connection |
867 | | * \return 0 on success,-1 on failure */ |
868 | | int tcpconn_add_alias(struct sip_msg *msg, unsigned int id, int port, int proto) |
869 | 0 | { |
870 | 0 | struct tcp_connection* c; |
871 | 0 | unsigned hash; |
872 | 0 | struct tcp_conn_alias* a; |
873 | |
|
874 | 0 | a=0; |
875 | | /* fix the port */ |
876 | 0 | port=port ? port : protos[proto].default_port ; |
877 | 0 | TCPCONN_LOCK(id); |
878 | | /* check if alias already exists */ |
879 | 0 | c=_tcpconn_find(id); |
880 | 0 | if (c) { |
881 | 0 | if (msg && !(c->profile.alias_mode == TCP_ALIAS_ALWAYS |
882 | 0 | || (c->profile.alias_mode == TCP_ALIAS_RFC_5923 |
883 | 0 | && msg->via1->alias))) { |
884 | 0 | LM_DBG("refusing to add alias (alias_mode: %u, via 'alias': %u)\n", |
885 | 0 | c->profile.alias_mode, !!msg->via1->alias); |
886 | 0 | TCPCONN_UNLOCK(id); |
887 | 0 | return 0; |
888 | 0 | } |
889 | | |
890 | 0 | hash=tcp_addr_hash(&c->rcv.src_ip, port); |
891 | | /* search the aliases for an already existing one */ |
892 | 0 | for (a=TCP_PART(id).tcpconn_aliases_hash[hash]; a; a=a->next) { |
893 | 0 | if (a->parent->state != S_CONN_BAD && |
894 | 0 | port == a->port && |
895 | 0 | proto == a->parent->type && |
896 | 0 | ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) { |
897 | | /* found */ |
898 | 0 | if (a->parent!=c) goto error_sec; |
899 | 0 | else goto ok; |
900 | 0 | } |
901 | 0 | } |
902 | 0 | if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases; |
903 | 0 | c->con_aliases[c->aliases].parent=c; |
904 | 0 | c->con_aliases[c->aliases].port=port; |
905 | 0 | c->con_aliases[c->aliases].hash=hash; |
906 | 0 | tcpconn_listadd(TCP_PART(id).tcpconn_aliases_hash[hash], |
907 | 0 | &c->con_aliases[c->aliases], next, prev); |
908 | 0 | c->aliases++; |
909 | 0 | }else goto error_not_found; |
910 | 0 | ok: |
911 | 0 | TCPCONN_UNLOCK(id); |
912 | | #ifdef EXTRA_DEBUG |
913 | | if (a) LM_DBG("alias already present\n"); |
914 | | else LM_DBG("alias port %d for hash %d, id %u\n", port, hash, id); |
915 | | #endif |
916 | 0 | return 0; |
917 | 0 | error_aliases: |
918 | 0 | TCPCONN_UNLOCK(id); |
919 | 0 | LM_ERR("too many aliases for connection %p (%u)\n", c, id); |
920 | 0 | return -1; |
921 | 0 | error_not_found: |
922 | 0 | TCPCONN_UNLOCK(id); |
923 | 0 | LM_ERR("no connection found for id %u\n",id); |
924 | 0 | return -1; |
925 | 0 | error_sec: |
926 | 0 | LM_WARN("possible port hijack attempt\n"); |
927 | 0 | LM_WARN("alias already present and points to another connection " |
928 | 0 | "(%d : %d and %u : %d)\n", a->parent->id, port, id, port); |
929 | 0 | TCPCONN_UNLOCK(id); |
930 | 0 | return -1; |
931 | 0 | } |
932 | | |
933 | | |
934 | | static void tcpconn_put_rpc(int pid, void *param) |
935 | 0 | { |
936 | 0 | tcpconn_put(param); |
937 | 0 | } |
938 | | |
939 | | |
940 | | void tcpconn_put(struct tcp_connection* c) |
941 | 0 | { |
942 | 0 | int destroy = 0; |
943 | 0 | int release_in_main = 0; |
944 | 0 | int tcp_main_proc; |
945 | |
|
946 | 0 | TCPCONN_LOCK(c->id); |
947 | 0 | if ((c->flags & F_CONN_HASHED) == 0) { |
948 | 0 | if (!is_tcp_main && c->refcnt == 1) { |
949 | | /* Keep the last reference until TCP main accepts ownership. */ |
950 | 0 | release_in_main = 1; |
951 | 0 | } else { |
952 | 0 | c->refcnt--; |
953 | 0 | if (c->refcnt == 0) |
954 | 0 | destroy = 1; |
955 | 0 | } |
956 | 0 | } else { |
957 | | /* Hashed connections are destroyed by TCP main lifetime handling. */ |
958 | 0 | c->refcnt--; |
959 | 0 | } |
960 | 0 | TCPCONN_UNLOCK(c->id); |
961 | |
|
962 | 0 | if (release_in_main) { |
963 | 0 | tcp_main_proc = tcp_get_main_proc_no(); |
964 | 0 | if (tcp_main_proc < 0 || |
965 | 0 | ipc_send_rpc(tcp_main_proc, tcpconn_put_rpc, c) < 0) |
966 | 0 | LM_ERR("failed to release connection %p (%u) in TCP main; " |
967 | 0 | "leaving its final reference intact\n", c, c->id); |
968 | 0 | } else if (destroy) { |
969 | 0 | _tcpconn_rm(c, 1); |
970 | 0 | } |
971 | 0 | } |
972 | | |
973 | | |
974 | | static inline void tcpconn_ref(struct tcp_connection* c) |
975 | 0 | { |
976 | 0 | TCPCONN_LOCK(c->id); |
977 | 0 | c->refcnt++; |
978 | 0 | TCPCONN_UNLOCK(c->id); |
979 | 0 | } |
980 | | |
981 | | |
982 | | static inline unsigned int tcpconn_next_id(void) |
983 | 0 | { |
984 | 0 | unsigned int id; |
985 | |
|
986 | 0 | lock_get(connection_id_lock); |
987 | 0 | id = (*connection_id)++; |
988 | 0 | lock_release(connection_id_lock); |
989 | |
|
990 | 0 | return id; |
991 | 0 | } |
992 | | |
993 | | |
994 | | static struct tcp_connection* tcpconn_new(int sock, const union sockaddr_union* su, |
995 | | const struct socket_info* si, const struct tcp_conn_profile *prof, |
996 | | int state, int flags) |
997 | 0 | { |
998 | 0 | struct tcp_connection *c; |
999 | 0 | union sockaddr_union local_su; |
1000 | 0 | unsigned int su_size; |
1001 | 0 | int counted = 0; |
1002 | |
|
1003 | 0 | lock_get(tcp_connections_lock); |
1004 | 0 | if (*tcp_connections_no >= (unsigned int)tcp_max_connections) { |
1005 | 0 | lock_release(tcp_connections_lock); |
1006 | 0 | LM_ERR("maximum number of connections exceeded: %u/%d\n", |
1007 | 0 | *tcp_connections_no, tcp_max_connections); |
1008 | 0 | return 0; |
1009 | 0 | } |
1010 | 0 | (*tcp_connections_no)++; |
1011 | 0 | lock_release(tcp_connections_lock); |
1012 | 0 | counted = 1; |
1013 | |
|
1014 | 0 | c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection)); |
1015 | 0 | if (c==0){ |
1016 | 0 | LM_ERR("shared memory allocation failure\n"); |
1017 | 0 | goto error_count; |
1018 | 0 | } |
1019 | 0 | memset(c, 0, sizeof(struct tcp_connection)); /* zero init */ |
1020 | 0 | c->fd=sock; |
1021 | 0 | if (lock_init(&c->write_lock)==0){ |
1022 | 0 | LM_ERR("init lock failed\n"); |
1023 | 0 | goto error0; |
1024 | 0 | } |
1025 | | |
1026 | 0 | c->rcv.src_su=*su; |
1027 | |
|
1028 | 0 | c->refcnt=0; |
1029 | 0 | su2ip_addr(&c->rcv.src_ip, su); |
1030 | 0 | c->rcv.src_port=su_getport(su); |
1031 | 0 | c->rcv.bind_address = si; |
1032 | 0 | c->rcv.dst_ip = si->address; |
1033 | 0 | if (sock >= 0) { |
1034 | 0 | su_size = sockaddru_len(*su); |
1035 | 0 | if (getsockname(sock, (struct sockaddr *)&local_su, &su_size)<0) { |
1036 | 0 | LM_ERR("failed to get info on received interface/IP %d/%s\n", |
1037 | 0 | errno, strerror(errno)); |
1038 | 0 | goto error; |
1039 | 0 | } |
1040 | 0 | c->rcv.dst_port = su_getport(&local_su); |
1041 | 0 | } else { |
1042 | 0 | c->rcv.dst_port = (si->flags & SI_REUSEPORT) ? su_getport(&si->su) : 0; |
1043 | 0 | } |
1044 | 0 | print_ip("tcpconn_new: new tcp connection to: ", &c->rcv.src_ip, "\n"); |
1045 | 0 | LM_DBG("on port %d, proto %d\n", c->rcv.src_port, si->proto); |
1046 | 0 | c->id=tcpconn_next_id(); |
1047 | 0 | c->cid = (unsigned long long)c->id |
1048 | 0 | | ( (unsigned long long)(startup_time&0xFFFFFF) << 32 ) |
1049 | 0 | | ( (unsigned long long)(rand()&0xFF) << 56 ); |
1050 | |
|
1051 | 0 | c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/ |
1052 | 0 | c->rcv.proto_reserved2=0; |
1053 | 0 | c->state=state; |
1054 | 0 | c->extra_data=0; |
1055 | 0 | c->type = si->proto; |
1056 | 0 | c->rcv.proto = si->proto; |
1057 | | /* start with the default conn lifetime */ |
1058 | 0 | c->lifetime = get_ticks() + prof->con_lifetime; |
1059 | 0 | c->timeout = c->lifetime; |
1060 | 0 | c->profile = *prof; |
1061 | 0 | c->flags|=F_CONN_REMOVED|flags; |
1062 | | #ifdef DBG_TCPCON |
1063 | | c->hist = sh_push(c, con_hist); |
1064 | | #endif |
1065 | |
|
1066 | 0 | if (protos[si->proto].net.stream.async_chunks) { |
1067 | 0 | c->async = shm_malloc(sizeof(struct tcp_async_data) + |
1068 | 0 | protos[si->proto].net.stream.async_chunks * |
1069 | 0 | sizeof(struct tcp_async_chunk)); |
1070 | 0 | if (c->async) { |
1071 | 0 | c->async->allocated = protos[si->proto].net.stream.async_chunks; |
1072 | 0 | c->async->oldest = 0; |
1073 | 0 | c->async->pending = 0; |
1074 | 0 | } else { |
1075 | 0 | LM_ERR("could not allocate async data for con!\n"); |
1076 | 0 | goto error; |
1077 | 0 | } |
1078 | 0 | } |
1079 | 0 | if (sock >= 0) { |
1080 | 0 | if (protos[si->proto].net.stream.conn.init && |
1081 | 0 | protos[si->proto].net.stream.conn.init(c) < 0) { |
1082 | 0 | LM_ERR("failed to do proto %d specific init for conn %p\n", |
1083 | 0 | c->type, c); |
1084 | 0 | goto error; |
1085 | 0 | } |
1086 | 0 | c->flags |= F_CONN_INIT; |
1087 | 0 | } |
1088 | 0 | return c; |
1089 | | |
1090 | 0 | error: |
1091 | 0 | lock_destroy(&c->write_lock); |
1092 | 0 | error0: |
1093 | 0 | shm_free(c); |
1094 | 0 | error_count: |
1095 | 0 | if (counted) { |
1096 | 0 | lock_get(tcp_connections_lock); |
1097 | 0 | (*tcp_connections_no)--; |
1098 | 0 | lock_release(tcp_connections_lock); |
1099 | 0 | } |
1100 | 0 | return 0; |
1101 | 0 | } |
1102 | | |
1103 | | |
1104 | | /* creates a new tcp connection structure |
1105 | | * for an outgoing connection request; local private state is initialized later |
1106 | | * a +1 ref is set for the new conn ! |
1107 | | * IMPORTANT - the function assumes you want to create a new TCP conn as |
1108 | | * a result of a connect operation - the conn will be set as connect !! |
1109 | | * Accepted connection are triggered internally only */ |
1110 | | struct tcp_connection* tcp_conn_create(const union sockaddr_union* su, |
1111 | | const struct socket_info* si, struct tcp_conn_profile *prof, |
1112 | | int state) |
1113 | 0 | { |
1114 | 0 | struct tcp_connection *c; |
1115 | 0 | struct tcp_conn_profile default_prof; |
1116 | |
|
1117 | 0 | if (!prof) { |
1118 | 0 | tcp_con_get_profile(su, &si->su, si->proto, &default_prof); |
1119 | 0 | prof = &default_prof; |
1120 | 0 | } |
1121 | | |
1122 | | /* create the connection structure */ |
1123 | 0 | c = tcpconn_new(-1, su, si, prof, state, 0); |
1124 | 0 | if (c==NULL) { |
1125 | 0 | LM_ERR("tcpconn_new failed\n"); |
1126 | 0 | return NULL; |
1127 | 0 | } |
1128 | | |
1129 | 0 | c->refcnt++; /* safe to do it w/o locking, it's not yet |
1130 | | available to the rest of the world */ |
1131 | 0 | sh_log(c->hist, TCP_REF, "connect, (%d)", c->refcnt); |
1132 | 0 | return c; |
1133 | 0 | } |
1134 | | |
1135 | | |
1136 | | static inline void tcpconn_destroy(struct tcp_connection* tcpconn) |
1137 | 0 | { |
1138 | 0 | int fd; |
1139 | 0 | int unsigned id = tcpconn->id; |
1140 | 0 | int hashed; |
1141 | |
|
1142 | 0 | TCPCONN_LOCK(id); /*avoid races w/ tcp_send*/ |
1143 | 0 | tcpconn->refcnt--; |
1144 | 0 | if (tcpconn->refcnt==0){ |
1145 | 0 | LM_DBG("destroying connection %p, flags %04x\n", |
1146 | 0 | tcpconn, tcpconn->flags); |
1147 | 0 | fd=tcpconn->fd; |
1148 | | /* no reporting here - the tcpconn_destroy() function is called |
1149 | | * from the TCP_MAIN reactor when handling connectioned received |
1150 | | * from a worker; and we generate the CLOSE reports from WORKERs */ |
1151 | 0 | hashed = (tcpconn->flags & F_CONN_HASHED); |
1152 | 0 | _tcpconn_rm(tcpconn, hashed ? 0 : 1); |
1153 | 0 | if (fd >= 0) |
1154 | 0 | close(fd); |
1155 | 0 | }else{ |
1156 | | /* force timeout */ |
1157 | 0 | tcpconn->lifetime=0; |
1158 | 0 | tcpconn->timeout=0; |
1159 | 0 | tcpconn->state=S_CONN_BAD; |
1160 | 0 | sh_log(tcpconn->hist, TCP_DEL_DELAY, "tcpconn_destroy delayed, (%d)", |
1161 | 0 | tcpconn->refcnt); |
1162 | 0 | LM_DBG("delaying (%p, flags %04x) ref = %d ...\n", |
1163 | 0 | tcpconn, tcpconn->flags, tcpconn->refcnt); |
1164 | |
|
1165 | 0 | } |
1166 | 0 | TCPCONN_UNLOCK(id); |
1167 | 0 | } |
1168 | | |
1169 | | static void tcpconn_destroy_rpc(int _, void *param) |
1170 | 0 | { |
1171 | 0 | tcpconn_destroy(param); |
1172 | 0 | } |
1173 | | |
1174 | | /* wrapper to the internally used function */ |
1175 | | void tcp_conn_destroy(struct tcp_connection* tcpconn) |
1176 | 0 | { |
1177 | 0 | int tcp_main_proc; |
1178 | |
|
1179 | 0 | tcp_trigger_report(tcpconn, TCP_REPORT_CLOSE, |
1180 | 0 | "Closed by Proto layer"); |
1181 | 0 | sh_log(tcpconn->hist, TCP_UNREF, "tcp_conn_destroy, (%d)", tcpconn->refcnt); |
1182 | |
|
1183 | 0 | if (!is_tcp_main) { |
1184 | 0 | tcp_main_proc = tcp_get_main_proc_no(); |
1185 | 0 | if (tcp_main_proc < 0 || |
1186 | 0 | ipc_send_rpc(tcp_main_proc, tcpconn_destroy_rpc, tcpconn) < 0) |
1187 | 0 | LM_ERR("failed to destroy connection %p (%u) in TCP main; " |
1188 | 0 | "leaving its reference intact\n", tcpconn, tcpconn->id); |
1189 | 0 | return; |
1190 | 0 | } |
1191 | | |
1192 | 0 | tcpconn_destroy(tcpconn); |
1193 | 0 | } |
1194 | | |
1195 | | static inline int tcp_set_nonblock(int fd) |
1196 | 0 | { |
1197 | 0 | int flags; |
1198 | |
|
1199 | 0 | flags = fcntl(fd, F_GETFL); |
1200 | 0 | if (flags == -1) { |
1201 | 0 | LM_ERR("fcntl(F_GETFL) failed for %d: %s\n", fd, strerror(errno)); |
1202 | 0 | return -1; |
1203 | 0 | } |
1204 | | |
1205 | 0 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { |
1206 | 0 | LM_ERR("fcntl(F_SETFL) failed for %d: %s\n", fd, strerror(errno)); |
1207 | 0 | return -1; |
1208 | 0 | } |
1209 | | |
1210 | 0 | return 0; |
1211 | 0 | } |
1212 | | |
1213 | | static void tcp_push_done_job(struct tcp_job *job) |
1214 | 0 | { |
1215 | 0 | pthread_mutex_lock(&tcp_pool.done_lock); |
1216 | 0 | if (tcp_pool.done_tail) |
1217 | 0 | tcp_pool.done_tail->next = job; |
1218 | 0 | else |
1219 | 0 | tcp_pool.done_head = job; |
1220 | 0 | tcp_pool.done_tail = job; |
1221 | 0 | pthread_mutex_unlock(&tcp_pool.done_lock); |
1222 | 0 | } |
1223 | | |
1224 | | static struct tcp_job *tcp_pop_done_job(void) |
1225 | 0 | { |
1226 | 0 | struct tcp_job *job; |
1227 | |
|
1228 | 0 | pthread_mutex_lock(&tcp_pool.done_lock); |
1229 | 0 | job = tcp_pool.done_head; |
1230 | 0 | if (job) { |
1231 | 0 | tcp_pool.done_head = job->next; |
1232 | 0 | if (tcp_pool.done_head == NULL) |
1233 | 0 | tcp_pool.done_tail = NULL; |
1234 | 0 | } |
1235 | 0 | pthread_mutex_unlock(&tcp_pool.done_lock); |
1236 | |
|
1237 | 0 | return job; |
1238 | 0 | } |
1239 | | |
1240 | | static inline struct tcp_connection *tcp_pop_shared_write_conn_locked(void) |
1241 | 0 | { |
1242 | 0 | struct tcp_connection *conn; |
1243 | |
|
1244 | 0 | conn = tcp_write_queue->head; |
1245 | 0 | if (conn) { |
1246 | 0 | tcp_write_queue->head = conn->wq_next; |
1247 | 0 | if (tcp_write_queue->head == NULL) |
1248 | 0 | tcp_write_queue->tail = NULL; |
1249 | 0 | conn->wq_next = NULL; |
1250 | 0 | } |
1251 | |
|
1252 | 0 | return conn; |
1253 | 0 | } |
1254 | | |
1255 | | static void *tcp_thread_routine(void *arg) |
1256 | 0 | { |
1257 | 0 | struct tcp_job *job; |
1258 | 0 | struct tcp_connection *conn; |
1259 | 0 | char wake = 'x'; |
1260 | 0 | int rc; |
1261 | |
|
1262 | 0 | (void)arg; |
1263 | | |
1264 | | /* Reactor operations stay in TCP main; IO threads only run read/write |
1265 | | * callbacks and notify completion back to the main thread. */ |
1266 | 0 | while (1) { |
1267 | 0 | cond_lock(&tcp_write_queue->cond); |
1268 | 0 | while (!tcp_pool.stop && tcp_pool.task_head == NULL && |
1269 | 0 | tcp_write_queue->head == NULL) |
1270 | 0 | cond_wait(&tcp_write_queue->cond); |
1271 | |
|
1272 | 0 | if (tcp_pool.stop && tcp_pool.task_head == NULL && |
1273 | 0 | tcp_write_queue->head == NULL) { |
1274 | 0 | cond_unlock(&tcp_write_queue->cond); |
1275 | 0 | break; |
1276 | 0 | } |
1277 | | |
1278 | 0 | job = tcp_pool.task_head; |
1279 | 0 | if (job) { |
1280 | 0 | tcp_pool.task_head = job->next; |
1281 | 0 | if (tcp_pool.task_head == NULL) |
1282 | 0 | tcp_pool.task_tail = NULL; |
1283 | 0 | } else if ((conn = tcp_pop_shared_write_conn_locked()) != NULL) { |
1284 | 0 | job = thread_malloc(sizeof(*job)); |
1285 | 0 | if (!job) { |
1286 | 0 | LM_ERR("oom while building shared TCP write job\n"); |
1287 | 0 | conn->flags &= ~F_CONN_WRITE_QUEUED; |
1288 | 0 | cond_unlock(&tcp_write_queue->cond); |
1289 | 0 | tcpconn_put(conn); |
1290 | 0 | continue; |
1291 | 0 | } |
1292 | 0 | job->conn = conn; |
1293 | 0 | job->op = TCP_WRITE_JOB; |
1294 | 0 | job->run = NULL; |
1295 | 0 | job->data = NULL; |
1296 | 0 | job->resp = 0; |
1297 | 0 | job->ret = 0; |
1298 | 0 | job->next = NULL; |
1299 | 0 | } |
1300 | 0 | cond_unlock(&tcp_write_queue->cond); |
1301 | |
|
1302 | 0 | conn = job->conn; |
1303 | 0 | if (job->op == TCP_READ_JOB) { |
1304 | 0 | if (conn->msg_attempts && get_ticks() > conn->timeout) { |
1305 | 0 | job->ret = -1; |
1306 | 0 | job->resp = -2; |
1307 | 0 | } else if (protos[conn->type].net.stream.read) { |
1308 | 0 | job->resp = protos[conn->type].net.stream.read(conn, &job->ret); |
1309 | 0 | } else { |
1310 | 0 | LM_ERR("missing stream.read callback for proto %d\n", conn->type); |
1311 | 0 | job->ret = -1; |
1312 | 0 | job->resp = -1; |
1313 | 0 | } |
1314 | 0 | } else if (job->op == TCP_WRITE_JOB) { |
1315 | 0 | if (protos[conn->type].net.stream.write) { |
1316 | 0 | if (tcpconn_prepare_write(conn) < 0) { |
1317 | 0 | job->ret = -1; |
1318 | 0 | job->resp = -1; |
1319 | 0 | goto done_job; |
1320 | 0 | } |
1321 | 0 | lock_get(&conn->write_lock); |
1322 | 0 | job->resp = protos[conn->type].net.stream.write(conn, conn->fd); |
1323 | 0 | lock_release(&conn->write_lock); |
1324 | 0 | job->ret = (job->resp < 0) ? -1 : 0; |
1325 | 0 | } else { |
1326 | 0 | LM_ERR("missing stream.write callback for proto %d\n", conn->type); |
1327 | 0 | job->ret = -1; |
1328 | 0 | job->resp = -1; |
1329 | 0 | } |
1330 | 0 | } else if (job->op == TCP_RUN_JOB) { |
1331 | 0 | job->ret = job->run ? job->run(job->data) : -1; |
1332 | 0 | thread_free(job); |
1333 | 0 | continue; |
1334 | 0 | } else { |
1335 | 0 | LM_ERR("unknown TCP job op %d\n", job->op); |
1336 | 0 | job->ret = -1; |
1337 | 0 | job->resp = -1; |
1338 | 0 | } |
1339 | | |
1340 | 0 | done_job: |
1341 | 0 | job->next = NULL; |
1342 | 0 | tcp_push_done_job(job); |
1343 | |
|
1344 | 0 | rc = write(tcp_pool.notify_pipe[1], &wake, 1); |
1345 | 0 | if (rc < 0 && errno != EAGAIN && errno != EWOULDBLOCK) |
1346 | 0 | LM_ERR("failed to notify TCP IO completion: %s\n", strerror(errno)); |
1347 | 0 | } |
1348 | | |
1349 | 0 | return NULL; |
1350 | 0 | } |
1351 | | |
1352 | | static int tcp_pool_init(void) |
1353 | 0 | { |
1354 | 0 | int i; |
1355 | 0 | int started = 0; |
1356 | 0 | int threads_no; |
1357 | 0 | long cpu_no; |
1358 | |
|
1359 | 0 | if (tcp_threads > 0) |
1360 | 0 | threads_no = tcp_threads; |
1361 | 0 | else { |
1362 | 0 | cpu_no = sysconf(_SC_NPROCESSORS_ONLN); |
1363 | 0 | if (cpu_no > 0) |
1364 | 0 | threads_no = (int)cpu_no; |
1365 | 0 | else if (tcp_workers_no > 0) |
1366 | 0 | threads_no = tcp_workers_no; |
1367 | 0 | else |
1368 | 0 | threads_no = 1; |
1369 | 0 | } |
1370 | |
|
1371 | 0 | if (pipe(tcp_pool.notify_pipe) < 0) { |
1372 | 0 | LM_ERR("failed to create TCP IO notification pipe: %s\n", strerror(errno)); |
1373 | 0 | goto error; |
1374 | 0 | } |
1375 | | |
1376 | 0 | if (tcp_set_nonblock(tcp_pool.notify_pipe[0]) < 0 || |
1377 | 0 | tcp_set_nonblock(tcp_pool.notify_pipe[1]) < 0) |
1378 | 0 | goto error; |
1379 | | |
1380 | 0 | if (reactor_add_reader(tcp_pool.notify_pipe[0], |
1381 | 0 | F_TCP_NOTIFY, RCT_PRIO_PROC, NULL) < 0) { |
1382 | 0 | LM_ERR("failed to add TCP IO notify pipe to reactor\n"); |
1383 | 0 | goto error; |
1384 | 0 | } |
1385 | | |
1386 | 0 | tcp_pool.threads = pkg_malloc(sizeof(*tcp_pool.threads) * threads_no); |
1387 | 0 | if (!tcp_pool.threads) { |
1388 | 0 | LM_ERR("oom while allocating TCP IO threads array\n"); |
1389 | 0 | goto error; |
1390 | 0 | } |
1391 | | |
1392 | 0 | tcp_pool.stop = 0; |
1393 | 0 | tcp_pool.threads_no = threads_no; |
1394 | |
|
1395 | 0 | for (i = 0; i < threads_no; i++) { |
1396 | 0 | if (pthread_create(&tcp_pool.threads[i], NULL, |
1397 | 0 | tcp_thread_routine, NULL) != 0) { |
1398 | 0 | LM_ERR("failed to start TCP IO thread %d/%d\n", i + 1, threads_no); |
1399 | 0 | goto error; |
1400 | 0 | } |
1401 | 0 | started++; |
1402 | 0 | } |
1403 | | |
1404 | 0 | LM_NOTICE("TCP single IO mode started with %d threads\n", threads_no); |
1405 | 0 | return 0; |
1406 | | |
1407 | 0 | error: |
1408 | 0 | cond_lock(&tcp_write_queue->cond); |
1409 | 0 | tcp_pool.stop = 1; |
1410 | 0 | cond_broadcast(&tcp_write_queue->cond); |
1411 | 0 | cond_unlock(&tcp_write_queue->cond); |
1412 | |
|
1413 | 0 | if (tcp_pool.threads) { |
1414 | 0 | for (i = 0; i < started; i++) |
1415 | 0 | pthread_join(tcp_pool.threads[i], NULL); |
1416 | 0 | pkg_free(tcp_pool.threads); |
1417 | 0 | tcp_pool.threads = NULL; |
1418 | 0 | } |
1419 | 0 | tcp_pool.threads_no = 0; |
1420 | |
|
1421 | 0 | if (tcp_pool.notify_pipe[0] >= 0) { |
1422 | 0 | reactor_del_reader(tcp_pool.notify_pipe[0], -1, 0); |
1423 | 0 | close(tcp_pool.notify_pipe[0]); |
1424 | 0 | tcp_pool.notify_pipe[0] = -1; |
1425 | 0 | } |
1426 | 0 | if (tcp_pool.notify_pipe[1] >= 0) { |
1427 | 0 | close(tcp_pool.notify_pipe[1]); |
1428 | 0 | tcp_pool.notify_pipe[1] = -1; |
1429 | 0 | } |
1430 | |
|
1431 | 0 | return -1; |
1432 | 0 | } |
1433 | | |
1434 | | static void tcp_pool_destroy(void) |
1435 | 0 | { |
1436 | 0 | int i; |
1437 | 0 | struct tcp_job *job; |
1438 | 0 | struct tcp_job *next; |
1439 | 0 | struct tcp_connection *conn; |
1440 | |
|
1441 | 0 | if (!tcp_threads_active() && tcp_pool.notify_pipe[0] < 0) |
1442 | 0 | return; |
1443 | | |
1444 | 0 | cond_lock(&tcp_write_queue->cond); |
1445 | 0 | tcp_pool.stop = 1; |
1446 | 0 | cond_broadcast(&tcp_write_queue->cond); |
1447 | 0 | cond_unlock(&tcp_write_queue->cond); |
1448 | |
|
1449 | 0 | for (i = 0; i < tcp_pool.threads_no; i++) |
1450 | 0 | pthread_join(tcp_pool.threads[i], NULL); |
1451 | |
|
1452 | 0 | if (tcp_pool.threads) { |
1453 | 0 | pkg_free(tcp_pool.threads); |
1454 | 0 | tcp_pool.threads = NULL; |
1455 | 0 | } |
1456 | 0 | tcp_pool.threads_no = 0; |
1457 | |
|
1458 | 0 | if (tcp_pool.notify_pipe[0] >= 0) { |
1459 | 0 | reactor_del_reader(tcp_pool.notify_pipe[0], -1, 0); |
1460 | 0 | close(tcp_pool.notify_pipe[0]); |
1461 | 0 | tcp_pool.notify_pipe[0] = -1; |
1462 | 0 | } |
1463 | 0 | if (tcp_pool.notify_pipe[1] >= 0) { |
1464 | 0 | close(tcp_pool.notify_pipe[1]); |
1465 | 0 | tcp_pool.notify_pipe[1] = -1; |
1466 | 0 | } |
1467 | |
|
1468 | 0 | cond_lock(&tcp_write_queue->cond); |
1469 | 0 | for (job = tcp_pool.task_head; job; job = next) { |
1470 | 0 | next = job->next; |
1471 | 0 | if (job->conn) |
1472 | 0 | tcpconn_put(job->conn); |
1473 | 0 | thread_free(job); |
1474 | 0 | } |
1475 | 0 | tcp_pool.task_head = tcp_pool.task_tail = NULL; |
1476 | 0 | while ((conn = tcp_pop_shared_write_conn_locked()) != NULL) { |
1477 | 0 | conn->flags &= ~F_CONN_WRITE_QUEUED; |
1478 | 0 | tcpconn_put(conn); |
1479 | 0 | } |
1480 | 0 | cond_unlock(&tcp_write_queue->cond); |
1481 | |
|
1482 | 0 | pthread_mutex_lock(&tcp_pool.done_lock); |
1483 | 0 | for (job = tcp_pool.done_head; job; job = next) { |
1484 | 0 | next = job->next; |
1485 | 0 | if (job->conn) |
1486 | 0 | tcpconn_put(job->conn); |
1487 | 0 | thread_free(job); |
1488 | 0 | } |
1489 | 0 | tcp_pool.done_head = tcp_pool.done_tail = NULL; |
1490 | 0 | pthread_mutex_unlock(&tcp_pool.done_lock); |
1491 | 0 | } |
1492 | | |
1493 | | static int tcp_queue_job(struct tcp_connection *tcpconn, int op) |
1494 | 0 | { |
1495 | 0 | struct tcp_job *job; |
1496 | |
|
1497 | 0 | if (!tcp_threads_active()) |
1498 | 0 | return -1; |
1499 | | |
1500 | 0 | job = thread_malloc(sizeof(*job)); |
1501 | 0 | if (!job) { |
1502 | 0 | LM_ERR("oom while queuing TCP IO job\n"); |
1503 | 0 | return -1; |
1504 | 0 | } |
1505 | | |
1506 | 0 | job->conn = tcpconn; |
1507 | 0 | job->op = op; |
1508 | 0 | job->run = NULL; |
1509 | 0 | job->data = NULL; |
1510 | 0 | job->resp = 0; |
1511 | 0 | job->ret = 0; |
1512 | 0 | job->next = NULL; |
1513 | |
|
1514 | 0 | cond_lock(&tcp_write_queue->cond); |
1515 | 0 | if (tcp_pool.task_tail) |
1516 | 0 | tcp_pool.task_tail->next = job; |
1517 | 0 | else |
1518 | 0 | tcp_pool.task_head = job; |
1519 | 0 | tcp_pool.task_tail = job; |
1520 | 0 | cond_signal(&tcp_write_queue->cond); |
1521 | 0 | cond_unlock(&tcp_write_queue->cond); |
1522 | |
|
1523 | 0 | return 0; |
1524 | 0 | } |
1525 | | |
1526 | | int tcp_async_write_job(struct tcp_connection *tcpconn) |
1527 | 0 | { |
1528 | 0 | if (!tcp_write_queue) |
1529 | 0 | return -1; |
1530 | 0 | if ((tcpconn->flags & F_CONN_HASHED) == 0) |
1531 | 0 | tcpconn_add(tcpconn); |
1532 | |
|
1533 | 0 | cond_lock(&tcp_write_queue->cond); |
1534 | 0 | if (tcpconn->flags & F_CONN_WRITE_QUEUED) { |
1535 | 0 | cond_unlock(&tcp_write_queue->cond); |
1536 | | /* a write job is already queued for this connection and it owns |
1537 | | * exactly one reference, which it releases on completion; our |
1538 | | * callers treat success as a transfer of their own reference, so |
1539 | | * it has to be released here, otherwise it leaks and the |
1540 | | * connection can never be destroyed */ |
1541 | 0 | tcpconn_put(tcpconn); |
1542 | 0 | return 0; |
1543 | 0 | } |
1544 | | |
1545 | 0 | tcpconn->flags |= F_CONN_WRITE_QUEUED; |
1546 | 0 | tcpconn->wq_next = NULL; |
1547 | 0 | if (tcp_write_queue->tail) |
1548 | 0 | tcp_write_queue->tail->wq_next = tcpconn; |
1549 | 0 | else |
1550 | 0 | tcp_write_queue->head = tcpconn; |
1551 | 0 | tcp_write_queue->tail = tcpconn; |
1552 | 0 | cond_signal(&tcp_write_queue->cond); |
1553 | 0 | cond_unlock(&tcp_write_queue->cond); |
1554 | 0 | return 0; |
1555 | 0 | } |
1556 | | |
1557 | | int tcp_run_task(tcp_thread_job_f run, void *data) |
1558 | 0 | { |
1559 | 0 | struct tcp_job *job; |
1560 | |
|
1561 | 0 | if (!run) |
1562 | 0 | return -1; |
1563 | | |
1564 | 0 | if (!tcp_threads_active()) { |
1565 | 0 | run(data); |
1566 | 0 | return 0; |
1567 | 0 | } |
1568 | | |
1569 | 0 | job = thread_malloc(sizeof(*job)); |
1570 | 0 | if (!job) { |
1571 | 0 | LM_ERR("oom while queuing TCP run job\n"); |
1572 | 0 | return -1; |
1573 | 0 | } |
1574 | | |
1575 | 0 | job->conn = NULL; |
1576 | 0 | job->op = TCP_RUN_JOB; |
1577 | 0 | job->run = run; |
1578 | 0 | job->data = data; |
1579 | 0 | job->resp = 0; |
1580 | 0 | job->ret = -1; |
1581 | 0 | job->next = NULL; |
1582 | |
|
1583 | 0 | cond_lock(&tcp_write_queue->cond); |
1584 | 0 | if (tcp_pool.task_tail) |
1585 | 0 | tcp_pool.task_tail->next = job; |
1586 | 0 | else |
1587 | 0 | tcp_pool.task_head = job; |
1588 | 0 | tcp_pool.task_tail = job; |
1589 | 0 | cond_signal(&tcp_write_queue->cond); |
1590 | 0 | cond_unlock(&tcp_write_queue->cond); |
1591 | |
|
1592 | 0 | return 0; |
1593 | 0 | } |
1594 | | |
1595 | | static inline int tcp_queue_write_job(struct tcp_connection *tcpconn) |
1596 | 0 | { |
1597 | 0 | if (!(tcpconn->flags & F_CONN_REMOVED_READ) && tcpconn->fd != -1) { |
1598 | 0 | if (reactor_del_reader(tcpconn->fd, -1, 0) == -1) |
1599 | 0 | return -1; |
1600 | 0 | tcpconn->flags |= F_CONN_REMOVED_READ; |
1601 | 0 | } |
1602 | | |
1603 | 0 | if (tcp_async_write_job(tcpconn) < 0) |
1604 | 0 | return -1; |
1605 | | |
1606 | 0 | return 0; |
1607 | 0 | } |
1608 | | |
1609 | | static inline void tcp_fail_conn(struct tcp_connection *tcpconn, |
1610 | | const char *reason, int report) |
1611 | 0 | { |
1612 | 0 | if ((tcpconn->flags & F_CONN_REMOVED) != F_CONN_REMOVED && |
1613 | 0 | tcpconn->fd != -1) { |
1614 | 0 | reactor_del_all(tcpconn->fd, -1, IO_FD_CLOSING); |
1615 | 0 | tcpconn->flags |= F_CONN_REMOVED; |
1616 | 0 | } |
1617 | |
|
1618 | 0 | if (report) |
1619 | 0 | tcp_trigger_report(tcpconn, TCP_REPORT_CLOSE, (void *)reason); |
1620 | |
|
1621 | 0 | tcpconn_destroy(tcpconn); |
1622 | 0 | } |
1623 | | |
1624 | | static inline void tcp_complete_read(struct tcp_job *job) |
1625 | 0 | { |
1626 | 0 | struct tcp_connection *tcpconn; |
1627 | |
|
1628 | 0 | tcpconn = job->conn; |
1629 | |
|
1630 | 0 | if (job->resp == -2) { |
1631 | 0 | tcp_fail_conn(tcpconn, "Timeout waiting for a complete message", 1); |
1632 | 0 | return; |
1633 | 0 | } |
1634 | | |
1635 | 0 | if (job->resp < 0 || tcpconn->state == S_CONN_BAD) { |
1636 | 0 | tcp_fail_conn(tcpconn, "Read error", 1); |
1637 | 0 | return; |
1638 | 0 | } |
1639 | | |
1640 | 0 | if (tcpconn->state == S_CONN_EOF) { |
1641 | 0 | tcp_fail_conn(tcpconn, "EOF received", 1); |
1642 | 0 | return; |
1643 | 0 | } |
1644 | | |
1645 | 0 | if (tcpconn->flags & F_CONN_REMOVED_READ) { |
1646 | 0 | if (reactor_add_reader(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, tcpconn) < 0) { |
1647 | 0 | LM_ERR("failed to re-add TCP conn %p for read events\n", tcpconn); |
1648 | 0 | tcp_fail_conn(tcpconn, "Failed to re-arm read", 0); |
1649 | 0 | return; |
1650 | 0 | } |
1651 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_READ; |
1652 | 0 | } |
1653 | | |
1654 | 0 | tcpconn_put(tcpconn); |
1655 | 0 | } |
1656 | | |
1657 | | static inline void tcp_complete_write(struct tcp_job *job) |
1658 | 0 | { |
1659 | 0 | struct tcp_connection *tcpconn; |
1660 | 0 | int pending_chunks; |
1661 | |
|
1662 | 0 | tcpconn = job->conn; |
1663 | |
|
1664 | 0 | if (job->resp < 0 || tcpconn->state == S_CONN_BAD) { |
1665 | 0 | tcp_fail_conn(tcpconn, "Write error", 1); |
1666 | 0 | return; |
1667 | 0 | } |
1668 | | |
1669 | 0 | lock_get(&tcpconn->write_lock); |
1670 | 0 | pending_chunks = (tcpconn->async && tcpconn->async->pending); |
1671 | 0 | lock_release(&tcpconn->write_lock); |
1672 | |
|
1673 | 0 | if ((tcpconn->flags & F_CONN_REMOVED_READ) && tcpconn->fd != -1) { |
1674 | 0 | if (reactor_add_reader(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, |
1675 | 0 | tcpconn) < 0) { |
1676 | 0 | LM_ERR("failed to add TCP conn %p for read events\n", tcpconn); |
1677 | 0 | tcp_fail_conn(tcpconn, "Failed to arm read", 0); |
1678 | 0 | return; |
1679 | 0 | } |
1680 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_READ; |
1681 | 0 | } |
1682 | | |
1683 | 0 | if (job->resp == 1) { |
1684 | 0 | if (reactor_add_writer(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, tcpconn) < 0) { |
1685 | 0 | LM_ERR("failed to re-add TCP conn %p for write events\n", tcpconn); |
1686 | 0 | tcp_fail_conn(tcpconn, "Failed to re-arm write", 0); |
1687 | 0 | return; |
1688 | 0 | } |
1689 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_WRITE; |
1690 | 0 | tcpconn->flags &= ~F_CONN_WRITE_QUEUED; |
1691 | 0 | tcpconn_put(tcpconn); |
1692 | 0 | return; |
1693 | 0 | } |
1694 | | |
1695 | 0 | if (pending_chunks) { |
1696 | 0 | tcpconn->flags &= ~F_CONN_WRITE_QUEUED; |
1697 | 0 | if (tcp_async_write_job(tcpconn) < 0) { |
1698 | 0 | LM_ERR("failed queuing follow-up TCP write job\n"); |
1699 | 0 | tcpconn->flags &= ~F_CONN_WRITE_QUEUED; |
1700 | 0 | if (reactor_add_writer(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, tcpconn) < 0) { |
1701 | 0 | tcp_fail_conn(tcpconn, "Failed queueing follow-up write", 0); |
1702 | 0 | return; |
1703 | 0 | } |
1704 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_WRITE; |
1705 | 0 | tcpconn_put(tcpconn); |
1706 | 0 | } |
1707 | 0 | return; |
1708 | 0 | } |
1709 | | |
1710 | 0 | tcpconn->flags &= ~F_CONN_WRITE_QUEUED; |
1711 | 0 | tcpconn_put(tcpconn); |
1712 | 0 | } |
1713 | | |
1714 | | static inline int handle_tcp_notify(int fd) |
1715 | 0 | { |
1716 | 0 | char buf[64]; |
1717 | 0 | int n; |
1718 | 0 | struct tcp_job *job; |
1719 | |
|
1720 | 0 | while ((n = read(fd, buf, sizeof(buf))) > 0) |
1721 | 0 | ; |
1722 | 0 | if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) |
1723 | 0 | LM_ERR("failed to read TCP IO notify fd: %s\n", strerror(errno)); |
1724 | |
|
1725 | 0 | while ((job = tcp_pop_done_job()) != NULL) { |
1726 | 0 | if (job->op == TCP_READ_JOB) |
1727 | 0 | tcp_complete_read(job); |
1728 | 0 | else |
1729 | 0 | tcp_complete_write(job); |
1730 | 0 | thread_free(job); |
1731 | 0 | } |
1732 | |
|
1733 | 0 | return 0; |
1734 | 0 | } |
1735 | | |
1736 | | |
1737 | | /************************ TCP MAIN process functions ************************/ |
1738 | | |
1739 | | /*! \brief |
1740 | | * handles a new connection, called internally by tcp_main_loop/handle_io. |
1741 | | * \param si - pointer to one of the tcp socket_info structures on which |
1742 | | * an io event was detected (connection attempt) |
1743 | | * \return handle_* return convention: -1 on error, 0 on EAGAIN (no more |
1744 | | * io events queued), >0 on success. success/error refer only to |
1745 | | * the accept. |
1746 | | */ |
1747 | | static inline int handle_new_connect(const struct socket_info* si) |
1748 | 0 | { |
1749 | 0 | union sockaddr_union su; |
1750 | 0 | struct tcp_connection* tcpconn; |
1751 | 0 | struct tcp_conn_profile prof; |
1752 | 0 | socklen_t su_len = sizeof(su); |
1753 | 0 | int new_sock; |
1754 | 0 | unsigned int id; |
1755 | | |
1756 | | /* coverity[overrun-buffer-arg: FALSE] - union has 28 bytes, CID #200070 */ |
1757 | 0 | new_sock = accept(si->socket, &(su.s), &su_len); |
1758 | 0 | if (new_sock == -1) { |
1759 | 0 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
1760 | 0 | return 0; |
1761 | 0 | LM_ERR("failed to accept connection(%d): %s\n", errno, strerror(errno)); |
1762 | 0 | return -1; |
1763 | 0 | } |
1764 | | |
1765 | 0 | tcp_con_get_profile(&su, &si->su, si->proto, &prof); |
1766 | 0 | if (tcp_init_sock_opt(new_sock, &prof, si->flags, si->tos) < 0) { |
1767 | 0 | LM_ERR("tcp_init_sock_opt failed\n"); |
1768 | 0 | close(new_sock); |
1769 | 0 | return 1; /* success, because the accept was successful */ |
1770 | 0 | } |
1771 | | |
1772 | | /* add socket to list */ |
1773 | 0 | tcpconn = tcpconn_new(new_sock, &su, si, &prof, S_CONN_OK, |
1774 | 0 | F_CONN_ACCEPTED); |
1775 | 0 | if (tcpconn) { |
1776 | | /* Safe: the connection is not yet visible outside TCP main. */ |
1777 | 0 | tcpconn->refcnt++; |
1778 | 0 | sh_log(tcpconn->hist, TCP_REF, "accept, (%d)", tcpconn->refcnt); |
1779 | 0 | tcpconn_add(tcpconn); |
1780 | 0 | LM_DBG("new connection: %p %d flags: %04x\n", |
1781 | 0 | tcpconn, tcpconn->fd, tcpconn->flags); |
1782 | 0 | if (reactor_add_reader(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, |
1783 | 0 | tcpconn) < 0) { |
1784 | 0 | LM_ERR("failed to add accepted TCP conn to reactor\n"); |
1785 | 0 | id = tcpconn->id; |
1786 | 0 | TCPCONN_LOCK(id); |
1787 | 0 | tcpconn->refcnt--; |
1788 | 0 | if (tcpconn->refcnt == 0) { |
1789 | 0 | _tcpconn_rm(tcpconn, 1); |
1790 | 0 | close(new_sock); |
1791 | 0 | } else { |
1792 | 0 | tcpconn->lifetime = 0; |
1793 | 0 | tcpconn->timeout = 0; |
1794 | 0 | } |
1795 | 0 | TCPCONN_UNLOCK(id); |
1796 | 0 | } else { |
1797 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_READ; |
1798 | 0 | tcpconn_put(tcpconn); |
1799 | 0 | } |
1800 | 0 | } else { |
1801 | 0 | LM_ERR("tcpconn_new failed, closing socket\n"); |
1802 | 0 | close(new_sock); |
1803 | 0 | } |
1804 | 0 | return 1; /* accept() was successful */ |
1805 | 0 | } |
1806 | | |
1807 | | |
1808 | | /*! \brief |
1809 | | * handles an io event on one of the watched tcp connections |
1810 | | * |
1811 | | * \param tcpconn - pointer to the tcp_connection for which we have an io ev. |
1812 | | * \param fd_i - index in the fd_array table (needed for delete) |
1813 | | * \return handle_* return convention, but on success it always returns 0 |
1814 | | */ |
1815 | | inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i, |
1816 | | int event_type) |
1817 | 0 | { |
1818 | 0 | int err; |
1819 | 0 | unsigned int err_len; |
1820 | |
|
1821 | 0 | if (event_type == IO_WATCH_READ) { |
1822 | 0 | LM_DBG("data available on %p %d\n", tcpconn, tcpconn->fd); |
1823 | 0 | if (reactor_del_reader(tcpconn->fd, fd_i, 0) == -1) |
1824 | 0 | return -1; |
1825 | 0 | tcpconn->flags |= F_CONN_REMOVED_READ; |
1826 | 0 | tcpconn_ref(tcpconn); /* refcnt ++ */ |
1827 | 0 | sh_log(tcpconn->hist, TCP_REF, "tcp-main read queued, (%d)", |
1828 | 0 | tcpconn->refcnt); |
1829 | 0 | if (tcp_queue_job(tcpconn, TCP_READ_JOB) < 0) { |
1830 | 0 | LM_ERR("failed queuing TCP read job\n"); |
1831 | 0 | if (reactor_add_reader(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, |
1832 | 0 | tcpconn) < 0) { |
1833 | 0 | tcp_fail_conn(tcpconn, "Failed queueing read", 0); |
1834 | 0 | return 0; |
1835 | 0 | } |
1836 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_READ; |
1837 | 0 | tcpconn_put(tcpconn); |
1838 | 0 | } |
1839 | 0 | return 0; |
1840 | 0 | } else { |
1841 | 0 | LM_DBG("connection %p fd %d is now writable\n", tcpconn, tcpconn->fd); |
1842 | | /* we received a write event */ |
1843 | 0 | if (tcpconn->state == S_CONN_CONNECTING) { |
1844 | | /* we're coming from an async connect & write |
1845 | | * let's see if we connected successfully */ |
1846 | 0 | err_len = sizeof(err); |
1847 | 0 | if (getsockopt(tcpconn->fd, SOL_SOCKET, SO_ERROR, &err, &err_len) < 0 || |
1848 | 0 | err != 0) { |
1849 | 0 | LM_DBG("Failed connection attempt\n"); |
1850 | 0 | tcpconn_ref(tcpconn); |
1851 | 0 | sh_log(tcpconn->hist, TCP_REF, "tcpconn connect, (%d)", tcpconn->refcnt); |
1852 | 0 | reactor_del_all(tcpconn->fd, fd_i, IO_FD_CLOSING); |
1853 | 0 | tcpconn->flags|=F_CONN_REMOVED; |
1854 | 0 | tcp_trigger_report(tcpconn, TCP_REPORT_CLOSE, |
1855 | 0 | "Async connect failed"); |
1856 | 0 | sh_log(tcpconn->hist, TCP_UNREF, "tcpconn connect, (%d)", tcpconn->refcnt); |
1857 | 0 | tcpconn_destroy(tcpconn); |
1858 | 0 | return 0; |
1859 | 0 | } |
1860 | | |
1861 | | /* we successfully connected - further treat this case as if we |
1862 | | * were coming from an async write */ |
1863 | 0 | tcpconn->state = S_CONN_OK; |
1864 | 0 | LM_DBG("Successfully completed previous async connect\n"); |
1865 | | |
1866 | | /* now that we completed the async connection, we also need to |
1867 | | * listen for READ events, otherwise these will get lost */ |
1868 | 0 | if (tcpconn->flags & F_CONN_REMOVED_READ) { |
1869 | 0 | if (reactor_add_reader(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, |
1870 | 0 | tcpconn) < 0) { |
1871 | 0 | LM_ERR("failed to re-arm TCP conn %p for read events\n", |
1872 | 0 | tcpconn); |
1873 | 0 | tcp_fail_conn(tcpconn, "Failed to re-arm read", 0); |
1874 | 0 | return 0; |
1875 | 0 | } |
1876 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_READ; |
1877 | 0 | } |
1878 | | |
1879 | 0 | goto async_write; |
1880 | 0 | } else { |
1881 | 0 | async_write: |
1882 | | /* no more write events for now */ |
1883 | 0 | if (reactor_del_writer(tcpconn->fd, fd_i, 0) == -1) |
1884 | 0 | return -1; |
1885 | 0 | tcpconn->flags |= F_CONN_REMOVED_WRITE; |
1886 | 0 | tcpconn_ref(tcpconn); /* refcnt ++ */ |
1887 | 0 | sh_log(tcpconn->hist, TCP_REF, "tcpconn write, (%d)", |
1888 | 0 | tcpconn->refcnt); |
1889 | 0 | if (tcp_queue_write_job(tcpconn) < 0) { |
1890 | 0 | LM_ERR("failed queuing TCP write job\n"); |
1891 | 0 | if (reactor_add_writer(tcpconn->fd, F_TCPCONN, RCT_PRIO_NET, |
1892 | 0 | tcpconn) < 0) { |
1893 | 0 | tcp_fail_conn(tcpconn, "Failed queueing write", 0); |
1894 | 0 | return 0; |
1895 | 0 | } |
1896 | 0 | tcpconn->flags &= ~F_CONN_REMOVED_WRITE; |
1897 | 0 | tcpconn_put(tcpconn); |
1898 | 0 | } |
1899 | 0 | return 0; |
1900 | 0 | } |
1901 | 0 | } |
1902 | 0 | } |
1903 | | |
1904 | | |
1905 | | /*! \brief generic handle io routine, it will call the appropiate |
1906 | | * handle_xxx() based on the fd_map type |
1907 | | * |
1908 | | * \param fm - pointer to a fd hash entry |
1909 | | * \param idx - index in the fd_array (or -1 if not known) |
1910 | | * \return -1 on error |
1911 | | * 0 on EAGAIN or when by some other way it is known that no more |
1912 | | * io events are queued on the fd (the receive buffer is empty). |
1913 | | * Usefull to detect when there are no more io events queued for |
1914 | | * sigio_rt, epoll_et, kqueue. |
1915 | | * >0 on successful read from the fd (when there might be more io |
1916 | | * queued -- the receive buffer might still be non-empty) |
1917 | | */ |
1918 | | inline static int handle_io(struct fd_map* fm, int idx,int event_type) |
1919 | 0 | { |
1920 | 0 | int ret = 0; |
1921 | |
|
1922 | 0 | pt_become_active(); |
1923 | | /* for now we do not do any profiling here as all ops here are |
1924 | | only internals related to TCP passing beetween processing, no real |
1925 | | processing |
1926 | | */ |
1927 | 0 | switch(fm->type){ |
1928 | 0 | case F_TCP_LISTENER: |
1929 | 0 | ret = handle_new_connect((const struct socket_info*)fm->data); |
1930 | 0 | break; |
1931 | 0 | case F_TCPCONN: |
1932 | 0 | ret = handle_tcpconn_ev((struct tcp_connection*)fm->data, idx, |
1933 | 0 | event_type); |
1934 | 0 | break; |
1935 | 0 | case F_TCP_NOTIFY: |
1936 | 0 | ret = handle_tcp_notify(fm->fd); |
1937 | 0 | break; |
1938 | 0 | case F_IPC: |
1939 | 0 | ipc_handle_job(fm->fd); |
1940 | 0 | break; |
1941 | 0 | case F_NONE: |
1942 | 0 | LM_CRIT("empty fd map\n"); |
1943 | 0 | goto error; |
1944 | 0 | default: |
1945 | 0 | LM_CRIT("unknown fd type %d\n", fm->type); |
1946 | 0 | goto error; |
1947 | 0 | } |
1948 | 0 | pt_become_idle(); |
1949 | 0 | return ret; |
1950 | 0 | error: |
1951 | 0 | pt_become_idle(); |
1952 | 0 | return -1; |
1953 | 0 | } |
1954 | | |
1955 | | |
1956 | | /* |
1957 | | * iterates through all TCP connections and closes expired ones |
1958 | | * Note: runs once per second at most |
1959 | | */ |
1960 | | #define tcpconn_lifetime(last_sec) \ |
1961 | | do { \ |
1962 | | int now; \ |
1963 | | now = get_ticks(); \ |
1964 | | if (last_sec != now) { \ |
1965 | | last_sec = now; \ |
1966 | | __tcpconn_lifetime(0); \ |
1967 | | } \ |
1968 | | } while (0) |
1969 | | |
1970 | | |
1971 | | /*! \brief very inefficient for now - FIXME |
1972 | | * keep in sync with tcpconn_destroy, the "delete" part should be |
1973 | | * the same except for io_watch_del.. |
1974 | | * \todo FIXME (very inefficient for now) |
1975 | | */ |
1976 | | static inline void __tcpconn_lifetime(int shutdown) |
1977 | 0 | { |
1978 | 0 | struct tcp_connection *c, *next; |
1979 | 0 | unsigned int ticks,part; |
1980 | 0 | unsigned h; |
1981 | 0 | int fd; |
1982 | 0 | void *reason; |
1983 | |
|
1984 | 0 | if (have_ticks()) |
1985 | 0 | ticks=get_ticks(); |
1986 | 0 | else |
1987 | 0 | ticks=0; |
1988 | |
|
1989 | 0 | for( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) { |
1990 | 0 | if (!shutdown) TCPCONN_LOCK(part); /* fixme: we can lock only on delete IMO */ |
1991 | 0 | for(h=0; h<TCP_ID_HASH_SIZE; h++){ |
1992 | 0 | c=TCP_PART(part).tcpconn_id_hash[h]; |
1993 | 0 | while(c){ |
1994 | 0 | next=c->id_next; |
1995 | 0 | if (shutdown || ((c->refcnt == 0) && |
1996 | 0 | ((ticks > c->lifetime) || |
1997 | 0 | (c->msg_attempts && ticks > c->timeout)))) { |
1998 | 0 | if (!shutdown) |
1999 | 0 | LM_DBG("timeout for hash=%d - %p" |
2000 | 0 | " (%d > %d)\n", h, c, ticks, c->lifetime); |
2001 | 0 | fd=c->fd; |
2002 | | /* report the closing of the connection . Note that |
2003 | | * there are connectioned that use an foced expire to 0 |
2004 | | * as a way to be deleted - we are not interested in */ |
2005 | | /* Also, do not trigger reporting when shutdown |
2006 | | * is done */ |
2007 | 0 | if (c->lifetime>0 && !shutdown) { |
2008 | 0 | reason = (c->msg_attempts && ticks > c->timeout) ? |
2009 | 0 | "Timeout waiting for a complete message" : |
2010 | 0 | "Timeout on no traffic"; |
2011 | 0 | tcp_trigger_report(c, TCP_REPORT_CLOSE, reason); |
2012 | 0 | } |
2013 | 0 | if ((!shutdown)&&(fd>0)&&(c->refcnt==0)) { |
2014 | | /* if any of read or write are set, we need to remove |
2015 | | * the fd from the reactor */ |
2016 | 0 | if ((c->flags & F_CONN_REMOVED) != F_CONN_REMOVED){ |
2017 | 0 | reactor_del_all( fd, -1, IO_FD_CLOSING); |
2018 | 0 | c->flags|=F_CONN_REMOVED; |
2019 | 0 | } |
2020 | 0 | close(fd); |
2021 | 0 | c->fd = -1; |
2022 | 0 | } |
2023 | 0 | _tcpconn_rm(c, shutdown?1:0); |
2024 | 0 | } |
2025 | 0 | c=next; |
2026 | 0 | } |
2027 | 0 | } |
2028 | 0 | if (!shutdown) TCPCONN_UNLOCK(part); |
2029 | 0 | } |
2030 | 0 | } |
2031 | | |
2032 | | |
2033 | | static void tcp_main_server(void) |
2034 | 0 | { |
2035 | 0 | static unsigned int last_sec = 0; |
2036 | 0 | struct socket_info_full* sif; |
2037 | 0 | struct sr_module *m; |
2038 | 0 | int n; |
2039 | | |
2040 | | /* instruct tls_mgm to initialize all TLS domains */ |
2041 | 0 | for (m=modules; m; m = m->next) { |
2042 | 0 | if (strcmp(m->exports->name, "tls_mgm") == 0) |
2043 | 0 | if (init_child(PROC_TCP_MAIN) < 0) { |
2044 | 0 | LM_ERR("error in init_child for PROC_TCP_MAIN\n"); |
2045 | 0 | goto error; |
2046 | 0 | } |
2047 | 0 | } |
2048 | | |
2049 | | /* we run in a separate, dedicated process, with its own reactor |
2050 | | * (reactors are per process) */ |
2051 | 0 | if (init_worker_reactor("TCP_main", RCT_PRIO_MAX)<0) |
2052 | 0 | goto error; |
2053 | | |
2054 | | /* now start watching all the fds */ |
2055 | | |
2056 | | /* add all the sockets we listens on for connections */ |
2057 | 0 | for (n = PROTO_FIRST; n < PROTO_LAST; n++) |
2058 | 0 | if (is_tcp_based_proto(n)) |
2059 | 0 | for (sif = protos[n].listeners; sif; sif = sif->next) { |
2060 | 0 | struct socket_info* si = &sif->socket_info; |
2061 | 0 | if (protos[n].tran.bind_listener && |
2062 | 0 | protos[n].tran.bind_listener(si) < 0) { |
2063 | 0 | LM_ERR("failed to bind listener [%.*s], proto %s\n", |
2064 | 0 | si->name.len, si->name.s, protos[n].name); |
2065 | 0 | goto error; |
2066 | 0 | } |
2067 | 0 | if (si->socket != -1 && |
2068 | 0 | reactor_add_reader(si->socket, F_TCP_LISTENER, |
2069 | 0 | RCT_PRIO_NET, si) < 0) { |
2070 | 0 | LM_ERR("failed to add listen socket to reactor\n"); |
2071 | 0 | goto error; |
2072 | 0 | } |
2073 | 0 | } |
2074 | | /* init: start watching for the IPC jobs */ |
2075 | 0 | if (reactor_add_reader(IPC_FD_READ_SELF, F_IPC, RCT_PRIO_ASYNC, NULL)<0){ |
2076 | 0 | LM_CRIT("failed to add IPC pipe to reactor\n"); |
2077 | 0 | goto error; |
2078 | 0 | } |
2079 | | |
2080 | 0 | if (tcp_pool_init() < 0) |
2081 | 0 | goto error; |
2082 | | |
2083 | 0 | is_tcp_main = 1; |
2084 | | |
2085 | | /* main loop (requires "handle_io()" implementation) */ |
2086 | 0 | reactor_main_loop( TCP_MAIN_SELECT_TIMEOUT, error, |
2087 | 0 | tcpconn_lifetime(last_sec) ); |
2088 | |
|
2089 | 0 | error: |
2090 | 0 | tcp_pool_destroy(); |
2091 | 0 | destroy_worker_reactor(); |
2092 | 0 | LM_CRIT("exiting..."); |
2093 | 0 | exit(-1); |
2094 | 0 | } |
2095 | | |
2096 | | |
2097 | | |
2098 | | /**************************** Control functions ******************************/ |
2099 | | |
2100 | | /* initializes the TCP network level in terms of data structures */ |
2101 | | int tcp_init(void) |
2102 | 0 | { |
2103 | 0 | unsigned int i; |
2104 | | |
2105 | | /* first we do auto-detection to see if there are any TCP based |
2106 | | * protocols loaded */ |
2107 | 0 | for ( i=PROTO_FIRST ; i<PROTO_LAST ; i++ ) |
2108 | 0 | if (is_tcp_based_proto(i) && proto_has_listeners(i)) { |
2109 | 0 | tcp_disabled=0; |
2110 | 0 | break; |
2111 | 0 | } |
2112 | |
|
2113 | 0 | tcp_init_con_profiles(); |
2114 | |
|
2115 | 0 | if (tcp_disabled) |
2116 | 0 | return 0; |
2117 | | |
2118 | | #ifdef DBG_TCPCON |
2119 | | con_hist = shl_init("TCP con", 10000, 0); |
2120 | | if (!con_hist) { |
2121 | | LM_ERR("oom con hist\n"); |
2122 | | goto error; |
2123 | | } |
2124 | | #endif |
2125 | | |
2126 | 0 | if (tcp_auto_scaling_profile) { |
2127 | 0 | s_profile = get_scaling_profile(tcp_auto_scaling_profile); |
2128 | 0 | if (s_profile==NULL) { |
2129 | 0 | LM_WARN("TCP scaling profile <%s> not defined " |
2130 | 0 | "-> ignoring it...\n", tcp_auto_scaling_profile); |
2131 | 0 | } else { |
2132 | 0 | auto_scaling_enabled = 1; |
2133 | 0 | } |
2134 | 0 | } |
2135 | |
|
2136 | 0 | tcp_workers_max_no = (s_profile && (tcp_workers_no<s_profile->max_procs)) ? |
2137 | 0 | s_profile->max_procs : tcp_workers_no ; |
2138 | | |
2139 | | /* init tcp workers array */ |
2140 | 0 | tcp_workers = (struct tcp_worker*)shm_malloc |
2141 | 0 | ( tcp_workers_max_no*sizeof(struct tcp_worker) ); |
2142 | 0 | if (tcp_workers==0) { |
2143 | 0 | LM_CRIT("could not alloc tcp_workers array in shm memory\n"); |
2144 | 0 | goto error; |
2145 | 0 | } |
2146 | 0 | memset( tcp_workers, 0, tcp_workers_max_no*sizeof(struct tcp_worker)); |
2147 | | /* init globals */ |
2148 | 0 | connection_id=(unsigned int*)shm_malloc(sizeof(unsigned int)); |
2149 | 0 | if (connection_id==0){ |
2150 | 0 | LM_CRIT("could not alloc globals in shm memory\n"); |
2151 | 0 | goto error; |
2152 | 0 | } |
2153 | | // The rand() function returns a pseudo-random integer in the range 0 to |
2154 | | // RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]). |
2155 | 0 | *connection_id=(unsigned int)rand(); |
2156 | 0 | connection_id_lock=lock_alloc(); |
2157 | 0 | if (connection_id_lock==0){ |
2158 | 0 | LM_CRIT("could not alloc connection ID lock\n"); |
2159 | 0 | goto error; |
2160 | 0 | } |
2161 | 0 | if (lock_init(connection_id_lock)==0){ |
2162 | 0 | LM_CRIT("could not init connection ID lock\n"); |
2163 | 0 | lock_dealloc((void*)connection_id_lock); |
2164 | 0 | connection_id_lock=0; |
2165 | 0 | goto error; |
2166 | 0 | } |
2167 | 0 | tcp_connections_no = (unsigned int *)shm_malloc(sizeof(*tcp_connections_no)); |
2168 | 0 | if (tcp_connections_no == 0) { |
2169 | 0 | LM_CRIT("could not alloc tcp connection counter in shm memory\n"); |
2170 | 0 | goto error; |
2171 | 0 | } |
2172 | 0 | *tcp_connections_no = 0; |
2173 | 0 | tcp_main_proc_no = (int *)shm_malloc(sizeof(*tcp_main_proc_no)); |
2174 | 0 | if (tcp_main_proc_no == 0) { |
2175 | 0 | LM_CRIT("could not alloc tcp main proc slot in shm memory\n"); |
2176 | 0 | goto error; |
2177 | 0 | } |
2178 | 0 | *tcp_main_proc_no = -1; |
2179 | 0 | tcp_write_queue = shm_malloc(sizeof(*tcp_write_queue)); |
2180 | 0 | if (tcp_write_queue == 0) { |
2181 | 0 | LM_CRIT("could not alloc tcp shared write queue in shm memory\n"); |
2182 | 0 | goto error; |
2183 | 0 | } |
2184 | 0 | memset(tcp_write_queue, 0, sizeof(*tcp_write_queue)); |
2185 | 0 | if (cond_init(&tcp_write_queue->cond) != 0) { |
2186 | 0 | LM_CRIT("could not init tcp shared write queue cond\n"); |
2187 | 0 | shm_free(tcp_write_queue); |
2188 | 0 | tcp_write_queue = 0; |
2189 | 0 | goto error; |
2190 | 0 | } |
2191 | 0 | tcp_connections_lock = lock_alloc(); |
2192 | 0 | if (tcp_connections_lock == 0) { |
2193 | 0 | LM_CRIT("could not alloc tcp connection counter lock\n"); |
2194 | 0 | goto error; |
2195 | 0 | } |
2196 | 0 | if (lock_init(tcp_connections_lock) == 0) { |
2197 | 0 | LM_CRIT("could not init tcp connection counter lock\n"); |
2198 | 0 | lock_dealloc((void *)tcp_connections_lock); |
2199 | 0 | tcp_connections_lock = 0; |
2200 | 0 | goto error; |
2201 | 0 | } |
2202 | 0 | memset( &tcp_parts, 0, TCP_PARTITION_SIZE*sizeof(struct tcp_partition)); |
2203 | | /* init partitions */ |
2204 | 0 | for( i=0 ; i<TCP_PARTITION_SIZE ; i++ ) { |
2205 | | /* init lock */ |
2206 | 0 | tcp_parts[i].tcpconn_lock=lock_alloc(); |
2207 | 0 | if (tcp_parts[i].tcpconn_lock==0){ |
2208 | 0 | LM_CRIT("could not alloc lock\n"); |
2209 | 0 | goto error; |
2210 | 0 | } |
2211 | 0 | if (lock_init(tcp_parts[i].tcpconn_lock)==0){ |
2212 | 0 | LM_CRIT("could not init lock\n"); |
2213 | 0 | lock_dealloc((void*)tcp_parts[i].tcpconn_lock); |
2214 | 0 | tcp_parts[i].tcpconn_lock=0; |
2215 | 0 | goto error; |
2216 | 0 | } |
2217 | | /* alloc hashtables*/ |
2218 | 0 | tcp_parts[i].tcpconn_aliases_hash=(struct tcp_conn_alias**) |
2219 | 0 | shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*)); |
2220 | 0 | if (tcp_parts[i].tcpconn_aliases_hash==0){ |
2221 | 0 | LM_CRIT("could not alloc address hashtable in shm memory\n"); |
2222 | 0 | goto error; |
2223 | 0 | } |
2224 | 0 | tcp_parts[i].tcpconn_id_hash=(struct tcp_connection**) |
2225 | 0 | shm_malloc(TCP_ID_HASH_SIZE*sizeof(struct tcp_connection*)); |
2226 | 0 | if (tcp_parts[i].tcpconn_id_hash==0){ |
2227 | 0 | LM_CRIT("could not alloc id hashtable in shm memory\n"); |
2228 | 0 | goto error; |
2229 | 0 | } |
2230 | | /* init hashtables*/ |
2231 | 0 | memset((void*)tcp_parts[i].tcpconn_aliases_hash, 0, |
2232 | 0 | TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*)); |
2233 | 0 | memset((void*)tcp_parts[i].tcpconn_id_hash, 0, |
2234 | 0 | TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*)); |
2235 | 0 | } |
2236 | | |
2237 | 0 | return 0; |
2238 | 0 | error: |
2239 | | /* clean-up */ |
2240 | 0 | tcp_destroy(); |
2241 | 0 | return -1; |
2242 | 0 | } |
2243 | | |
2244 | | |
2245 | | /* destroys the TCP data */ |
2246 | | void tcp_destroy(void) |
2247 | 0 | { |
2248 | 0 | int part; |
2249 | |
|
2250 | 0 | if (tcp_parts[0].tcpconn_id_hash) |
2251 | | /* force close/expire for all active tcpconns*/ |
2252 | 0 | __tcpconn_lifetime(1); |
2253 | |
|
2254 | 0 | if (connection_id){ |
2255 | 0 | shm_free(connection_id); |
2256 | 0 | connection_id=0; |
2257 | 0 | } |
2258 | |
|
2259 | 0 | if (tcp_connections_no) { |
2260 | 0 | shm_free(tcp_connections_no); |
2261 | 0 | tcp_connections_no = 0; |
2262 | 0 | } |
2263 | |
|
2264 | 0 | if (tcp_main_proc_no) { |
2265 | 0 | shm_free(tcp_main_proc_no); |
2266 | 0 | tcp_main_proc_no = 0; |
2267 | 0 | } |
2268 | |
|
2269 | 0 | if (tcp_dispatch_sock[0] >= 0) { |
2270 | 0 | close(tcp_dispatch_sock[0]); |
2271 | 0 | tcp_dispatch_sock[0] = -1; |
2272 | 0 | } |
2273 | 0 | if (tcp_dispatch_sock[1] >= 0) { |
2274 | 0 | close(tcp_dispatch_sock[1]); |
2275 | 0 | tcp_dispatch_sock[1] = -1; |
2276 | 0 | } |
2277 | |
|
2278 | 0 | if (tcp_write_queue) { |
2279 | | /* Skip cond teardown during attendant shutdown. */ |
2280 | | /* cond_destroy(&tcp_write_queue->cond); */ |
2281 | 0 | shm_free(tcp_write_queue); |
2282 | 0 | tcp_write_queue = 0; |
2283 | 0 | } |
2284 | |
|
2285 | 0 | if (tcp_connections_lock) { |
2286 | 0 | lock_destroy(tcp_connections_lock); |
2287 | 0 | lock_dealloc((void *)tcp_connections_lock); |
2288 | 0 | tcp_connections_lock = 0; |
2289 | 0 | } |
2290 | |
|
2291 | 0 | if (connection_id_lock){ |
2292 | 0 | lock_destroy(connection_id_lock); |
2293 | 0 | lock_dealloc((void*)connection_id_lock); |
2294 | 0 | connection_id_lock=0; |
2295 | 0 | } |
2296 | |
|
2297 | 0 | for ( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) { |
2298 | 0 | if (tcp_parts[part].tcpconn_id_hash){ |
2299 | 0 | shm_free(tcp_parts[part].tcpconn_id_hash); |
2300 | 0 | tcp_parts[part].tcpconn_id_hash=0; |
2301 | 0 | } |
2302 | 0 | if (tcp_parts[part].tcpconn_aliases_hash){ |
2303 | 0 | shm_free(tcp_parts[part].tcpconn_aliases_hash); |
2304 | 0 | tcp_parts[part].tcpconn_aliases_hash=0; |
2305 | 0 | } |
2306 | 0 | if (tcp_parts[part].tcpconn_lock){ |
2307 | 0 | lock_destroy(tcp_parts[part].tcpconn_lock); |
2308 | 0 | lock_dealloc((void*)tcp_parts[part].tcpconn_lock); |
2309 | 0 | tcp_parts[part].tcpconn_lock=0; |
2310 | 0 | } |
2311 | 0 | } |
2312 | 0 | } |
2313 | | |
2314 | | |
2315 | | static int _get_own_tcp_worker_id(void) |
2316 | 0 | { |
2317 | 0 | pid_t pid; |
2318 | 0 | int i; |
2319 | |
|
2320 | 0 | pid = getpid(); |
2321 | 0 | for( i=0 ; i<tcp_workers_max_no ; i++) |
2322 | 0 | if(tcp_workers[i].pid==pid) |
2323 | 0 | return i; |
2324 | | |
2325 | 0 | return -1; |
2326 | 0 | } |
2327 | | |
2328 | | |
2329 | | void tcp_reset_worker_slot(void) |
2330 | 0 | { |
2331 | 0 | int i; |
2332 | |
|
2333 | 0 | if ((i=_get_own_tcp_worker_id())>=0) { |
2334 | 0 | tcp_workers[i].state=STATE_INACTIVE; |
2335 | 0 | tcp_workers[i].pid=0; |
2336 | 0 | tcp_workers[i].pt_idx=0; |
2337 | 0 | } |
2338 | 0 | } |
2339 | | |
2340 | | |
2341 | | static int fork_dynamic_tcp_process(void *foo) |
2342 | 0 | { |
2343 | 0 | int p_id; |
2344 | 0 | int r; |
2345 | 0 | const struct internal_fork_params ifp_sr_tcp = { |
2346 | 0 | .proc_desc = "SIP receiver TCP", |
2347 | 0 | .flags = OSS_PROC_DYNAMIC|OSS_PROC_NEEDS_SCRIPT, |
2348 | 0 | .type = TYPE_TCP, |
2349 | 0 | }; |
2350 | | |
2351 | | /* search for a free slot in the TCP workers table */ |
2352 | 0 | for (r = 0; r < tcp_workers_max_no; r++) |
2353 | 0 | if (tcp_workers[r].state == STATE_INACTIVE) |
2354 | 0 | break; |
2355 | |
|
2356 | 0 | if (r == tcp_workers_max_no) { |
2357 | 0 | LM_BUG("trying to fork one more TCP worker but no free slot in " |
2358 | 0 | "the TCP table (size=%d)\n", tcp_workers_max_no); |
2359 | 0 | return -1; |
2360 | 0 | } |
2361 | | |
2362 | 0 | if ((p_id = internal_fork(&ifp_sr_tcp)) < 0) { |
2363 | 0 | LM_ERR("cannot fork dynamic TCP worker process\n"); |
2364 | 0 | return -1; |
2365 | 0 | } else if (p_id == 0) { |
2366 | | /* new TCP worker process */ |
2367 | 0 | if (tcp_dispatch_sock[1] >= 0) |
2368 | 0 | close(tcp_dispatch_sock[1]); |
2369 | 0 | tcp_dispatch_sock[1] = -1; |
2370 | |
|
2371 | 0 | set_proc_attrs("TCP receiver"); |
2372 | 0 | tcp_workers[r].pid = getpid(); |
2373 | |
|
2374 | 0 | if (tcp_worker_proc_reactor_init(tcp_dispatch_sock[0]) < 0 || |
2375 | 0 | init_child(20000) || |
2376 | 0 | self_update_routing_script() < 0) |
2377 | 0 | goto error; |
2378 | | |
2379 | 0 | report_conditional_status(1, 0); |
2380 | 0 | clean_read_pipeend(); |
2381 | |
|
2382 | 0 | tcp_worker_proc_loop(); |
2383 | 0 | destroy_worker_reactor(); |
2384 | |
|
2385 | 0 | error: |
2386 | 0 | report_failure_status(); |
2387 | 0 | LM_ERR("initializing new TCP worker failed, exiting with error\n"); |
2388 | 0 | pt[process_no].flags |= OSS_PROC_SELFEXIT; |
2389 | 0 | exit(-1); |
2390 | 0 | } else { |
2391 | | /* parent/attendant */ |
2392 | 0 | tcp_workers[r].state = STATE_ACTIVE; |
2393 | 0 | tcp_workers[r].pt_idx = p_id; |
2394 | 0 | return p_id; |
2395 | 0 | } |
2396 | | |
2397 | 0 | return 0; |
2398 | 0 | } |
2399 | | |
2400 | | |
2401 | | static void tcp_process_graceful_terminate(int sender, void *param) |
2402 | 0 | { |
2403 | 0 | int i; |
2404 | | |
2405 | | /* accept this only from the attendant process */ |
2406 | 0 | if (sender != 0) { |
2407 | 0 | LM_BUG("graceful terminate received from a non-main process\n"); |
2408 | 0 | return; |
2409 | 0 | } |
2410 | 0 | LM_NOTICE("process %d received RPC to terminate from Main\n", process_no); |
2411 | | |
2412 | | /* reserve this slot until tcp_terminate_worker() detaches the process |
2413 | | * from the shared dispatch queue and completes its pending async work */ |
2414 | 0 | if ((i = _get_own_tcp_worker_id()) >= 0) |
2415 | 0 | tcp_workers[i].state = STATE_DRAINING; |
2416 | |
|
2417 | 0 | tcp_terminate_worker(); |
2418 | 0 | } |
2419 | | |
2420 | | |
2421 | | /* counts the number of TCP processes to start with; this number may |
2422 | | * change during runtime due auto-scaling */ |
2423 | | int tcp_count_processes(unsigned int *extra) |
2424 | 0 | { |
2425 | 0 | if (extra) *extra = 0; |
2426 | |
|
2427 | 0 | if (tcp_disabled) |
2428 | 0 | return 0; |
2429 | | |
2430 | 0 | if (s_profile && extra && s_profile->max_procs > tcp_workers_no) |
2431 | 0 | *extra = s_profile->max_procs - tcp_workers_no; |
2432 | |
|
2433 | 0 | return 1 /* tcp main / IO process */ + tcp_workers_no /* dispatch workers */; |
2434 | 0 | } |
2435 | | |
2436 | | |
2437 | | int tcp_start_processes(int *chd_rank, int *startup_done) |
2438 | 0 | { |
2439 | 0 | int r, p_id, flags; |
2440 | 0 | const struct internal_fork_params ifp_sr_tcp = { |
2441 | 0 | .proc_desc = "SIP receiver TCP", |
2442 | 0 | .flags = OSS_PROC_NEEDS_SCRIPT, |
2443 | 0 | .type = TYPE_TCP, |
2444 | 0 | }; |
2445 | |
|
2446 | 0 | if (tcp_disabled) |
2447 | 0 | return 0; |
2448 | | |
2449 | | /* create the shared dispatch socket from TCP main to TCP workers */ |
2450 | 0 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, tcp_dispatch_sock) < 0) { |
2451 | 0 | LM_ERR("socketpair failed for TCP worker dispatch: %s\n", |
2452 | 0 | strerror(errno)); |
2453 | 0 | goto error; |
2454 | 0 | } |
2455 | 0 | flags = fcntl(tcp_dispatch_sock[0], F_GETFL); |
2456 | 0 | if (flags == -1) { |
2457 | 0 | LM_ERR("fcntl failed for TCP worker dispatch socket: %s\n", |
2458 | 0 | strerror(errno)); |
2459 | 0 | goto error; |
2460 | 0 | } |
2461 | 0 | if (fcntl(tcp_dispatch_sock[0], F_SETFL, flags | O_NONBLOCK) == -1) { |
2462 | 0 | LM_ERR("failed to set non-blocking on TCP worker dispatch socket: %s\n", |
2463 | 0 | strerror(errno)); |
2464 | 0 | goto error; |
2465 | 0 | } |
2466 | 0 | flags = fcntl(tcp_dispatch_sock[1], F_GETFL); |
2467 | 0 | if (flags == -1) { |
2468 | 0 | LM_ERR("fcntl failed for TCP worker dispatch socket: %s\n", |
2469 | 0 | strerror(errno)); |
2470 | 0 | goto error; |
2471 | 0 | } |
2472 | 0 | if (fcntl(tcp_dispatch_sock[1], F_SETFL, flags | O_NONBLOCK) == -1) { |
2473 | 0 | LM_ERR("failed to set non-blocking on TCP worker dispatch socket: %s\n", |
2474 | 0 | strerror(errno)); |
2475 | 0 | goto error; |
2476 | 0 | } |
2477 | | |
2478 | 0 | if (auto_scaling_enabled && s_profile && |
2479 | 0 | create_process_group(TYPE_TCP, NULL, s_profile, |
2480 | 0 | fork_dynamic_tcp_process, tcp_process_graceful_terminate) != 0) |
2481 | 0 | LM_ERR("failed to create TCP auto-scaling process group; " |
2482 | 0 | "automatic scaling will not be possible\n"); |
2483 | |
|
2484 | 0 | for (r = 0; r < tcp_workers_no; r++) { |
2485 | 0 | (*chd_rank)++; |
2486 | 0 | p_id = internal_fork(&ifp_sr_tcp); |
2487 | 0 | if (p_id < 0) { |
2488 | 0 | LM_ERR("cannot fork TCP worker process %d\n", r); |
2489 | 0 | goto error; |
2490 | 0 | } else if (p_id > 0) { |
2491 | | /* parent */ |
2492 | 0 | tcp_workers[r].state = STATE_ACTIVE; |
2493 | 0 | tcp_workers[r].pt_idx = p_id; |
2494 | 0 | continue; |
2495 | 0 | } |
2496 | | |
2497 | | /* child */ |
2498 | 0 | if (tcp_dispatch_sock[1] >= 0) |
2499 | 0 | close(tcp_dispatch_sock[1]); |
2500 | 0 | tcp_dispatch_sock[1] = -1; |
2501 | |
|
2502 | 0 | set_proc_attrs("TCP receiver"); |
2503 | 0 | tcp_workers[r].pid = getpid(); |
2504 | |
|
2505 | 0 | if (tcp_worker_proc_reactor_init(tcp_dispatch_sock[0]) < 0 || |
2506 | 0 | init_child(*chd_rank) < 0) { |
2507 | 0 | LM_ERR("init_child failed for TCP worker %d\n", r); |
2508 | 0 | report_failure_status(); |
2509 | 0 | if (startup_done) |
2510 | 0 | *startup_done = -1; |
2511 | 0 | exit(-1); |
2512 | 0 | } |
2513 | | |
2514 | | /* first TCP worker runs startup_route if not already run */ |
2515 | 0 | if (startup_done && *startup_done == 0 && r == 0) { |
2516 | 0 | LM_DBG("running startup route for first TCP worker\n"); |
2517 | 0 | if (run_startup_route() < 0) { |
2518 | 0 | LM_ERR("startup route processing failed in TCP worker\n"); |
2519 | 0 | report_failure_status(); |
2520 | 0 | *startup_done = -1; |
2521 | 0 | exit(-1); |
2522 | 0 | } |
2523 | 0 | *startup_done = 1; |
2524 | 0 | } |
2525 | | |
2526 | 0 | report_conditional_status((!no_daemon_mode), 0); |
2527 | 0 | tcp_worker_proc_loop(); |
2528 | 0 | } |
2529 | | |
2530 | | /* Keep the worker endpoint in the attendant when auto-scaling is enabled, |
2531 | | * so dynamically forked TCP workers can inherit the shared queue. */ |
2532 | 0 | if (!s_profile && tcp_dispatch_sock[0] >= 0) { |
2533 | 0 | close(tcp_dispatch_sock[0]); |
2534 | 0 | tcp_dispatch_sock[0] = -1; |
2535 | 0 | } |
2536 | |
|
2537 | 0 | if (startup_done && tcp_workers_no > 0) |
2538 | 0 | while (!(*startup_done)) { |
2539 | 0 | usleep(5); |
2540 | 0 | handle_sigs(); |
2541 | 0 | } |
2542 | |
|
2543 | 0 | return 0; |
2544 | 0 | error: |
2545 | 0 | if (tcp_dispatch_sock[0] >= 0) { |
2546 | 0 | close(tcp_dispatch_sock[0]); |
2547 | 0 | tcp_dispatch_sock[0] = -1; |
2548 | 0 | } |
2549 | 0 | if (tcp_dispatch_sock[1] >= 0) { |
2550 | 0 | close(tcp_dispatch_sock[1]); |
2551 | 0 | tcp_dispatch_sock[1] = -1; |
2552 | 0 | } |
2553 | 0 | return -1; |
2554 | 0 | } |
2555 | | |
2556 | | static int tcpconn_update_local_port(struct tcp_connection *tcpconn) |
2557 | 0 | { |
2558 | 0 | union sockaddr_union local_su; |
2559 | 0 | socklen_t su_size; |
2560 | |
|
2561 | 0 | su_size = sockaddru_len(tcpconn->rcv.src_su); |
2562 | 0 | if (getsockname(tcpconn->fd, (struct sockaddr *)&local_su, &su_size) < 0) { |
2563 | 0 | LM_ERR("failed to get local socket info on conn %u: %s\n", |
2564 | 0 | tcpconn->id, strerror(errno)); |
2565 | 0 | return -1; |
2566 | 0 | } |
2567 | | |
2568 | 0 | tcpconn->rcv.dst_port = su_getport(&local_su); |
2569 | 0 | return 0; |
2570 | 0 | } |
2571 | | |
2572 | | static int tcpconn_prepare_write(struct tcp_connection *tcpconn) |
2573 | 0 | { |
2574 | 0 | int fd; |
2575 | 0 | int connected = 0; |
2576 | |
|
2577 | 0 | if ((tcpconn->flags & F_CONN_INIT) == 0) { |
2578 | 0 | if (protos[tcpconn->type].net.stream.conn.init && |
2579 | 0 | protos[tcpconn->type].net.stream.conn.init(tcpconn) < 0) { |
2580 | 0 | LM_ERR("failed to init proto %d conn %p in TCP main\n", |
2581 | 0 | tcpconn->type, tcpconn); |
2582 | 0 | return -1; |
2583 | 0 | } |
2584 | 0 | tcpconn->flags |= F_CONN_INIT; |
2585 | 0 | } |
2586 | | |
2587 | 0 | if (tcpconn->fd < 0) { |
2588 | 0 | if (!tcpconn->rcv.bind_address) { |
2589 | 0 | LM_ERR("missing bind_address for outbound conn %u\n", tcpconn->id); |
2590 | 0 | return -1; |
2591 | 0 | } |
2592 | | |
2593 | 0 | fd = tcp_sync_connect_fd(&tcpconn->rcv.bind_address->su, |
2594 | 0 | &tcpconn->rcv.src_su, tcpconn->type, &tcpconn->profile, |
2595 | 0 | tcpconn->rcv.bind_address->flags, |
2596 | 0 | tcpconn->rcv.bind_address->tos); |
2597 | 0 | if (fd < 0) |
2598 | 0 | return -1; |
2599 | | |
2600 | 0 | tcpconn->fd = fd; |
2601 | 0 | if (tcpconn_update_local_port(tcpconn) < 0) { |
2602 | 0 | close(fd); |
2603 | 0 | tcpconn->fd = -1; |
2604 | 0 | return -1; |
2605 | 0 | } |
2606 | | |
2607 | 0 | tcpconn->state = S_CONN_OK; |
2608 | 0 | connected = 1; |
2609 | 0 | } |
2610 | | |
2611 | 0 | if (connected && protos[tcpconn->type].net.stream.conn.connect) { |
2612 | 0 | if (protos[tcpconn->type].net.stream.conn.connect(tcpconn) < 0) { |
2613 | 0 | LM_ERR("failed to finish proto %d connect on conn %u\n", |
2614 | 0 | tcpconn->type, tcpconn->id); |
2615 | 0 | return -1; |
2616 | 0 | } |
2617 | 0 | } |
2618 | | |
2619 | 0 | return 0; |
2620 | 0 | } |
2621 | | |
2622 | | |
2623 | | int tcp_start_listener(void) |
2624 | 0 | { |
2625 | 0 | int p_id; |
2626 | 0 | const struct internal_fork_params ifp_tcp_main = { |
2627 | 0 | .proc_desc = "TCP main", |
2628 | 0 | .flags = 0, |
2629 | 0 | .type = TYPE_NONE, |
2630 | 0 | }; |
2631 | |
|
2632 | 0 | if (tcp_disabled) |
2633 | 0 | return 0; |
2634 | | |
2635 | | /* start the TCP manager process */ |
2636 | 0 | if ( (p_id=internal_fork(&ifp_tcp_main))<0 ) { |
2637 | 0 | LM_CRIT("cannot fork tcp main process\n"); |
2638 | 0 | goto error; |
2639 | 0 | }else if (p_id==0){ |
2640 | | /* child */ |
2641 | 0 | report_conditional_status( (!no_daemon_mode), 0); |
2642 | |
|
2643 | 0 | tcp_main_server(); |
2644 | 0 | exit(-1); |
2645 | 0 | } |
2646 | 0 | *tcp_main_proc_no = p_id; |
2647 | |
|
2648 | 0 | return 0; |
2649 | 0 | error: |
2650 | 0 | return -1; |
2651 | 0 | } |
2652 | | |
2653 | | int tcp_has_async_write(void) |
2654 | 0 | { |
2655 | 0 | return reactor_has_async(); |
2656 | 0 | } |
2657 | | |
2658 | | static int tcp_close_conn_run(void *data) |
2659 | 0 | { |
2660 | 0 | struct tcp_connection *conn = data; |
2661 | |
|
2662 | 0 | if (!conn) |
2663 | 0 | return -1; |
2664 | | |
2665 | 0 | tcp_conn_destroy(conn); |
2666 | 0 | return 0; |
2667 | 0 | } |
2668 | | |
2669 | | static void tcp_close_conn_rpc(int pid, void *param) |
2670 | 0 | { |
2671 | 0 | tcp_close_conn_run(param); |
2672 | 0 | } |
2673 | | |
2674 | | int tcp_close_connection(str *ipport) |
2675 | 0 | { |
2676 | 0 | struct tcp_connection *conn = NULL; |
2677 | 0 | struct ip_addr *ip; |
2678 | 0 | str host; |
2679 | 0 | unsigned int id; |
2680 | 0 | int port, proto, rc, tcp_main_proc; |
2681 | 0 | int start_proto, end_proto; |
2682 | 0 | unsigned int p; |
2683 | 0 | char *sep; |
2684 | |
|
2685 | 0 | if (tcp_disabled) |
2686 | 0 | return 0; |
2687 | | |
2688 | 0 | sep = q_memchr(ipport->s, ':', ipport->len); |
2689 | 0 | if (!sep) { |
2690 | 0 | if (str2int(ipport, &id) < 0) { |
2691 | 0 | LM_ERR("failed to parse tcp connection [%.*s]\n", |
2692 | 0 | ipport->len, ipport->s); |
2693 | 0 | return -1; |
2694 | 0 | } |
2695 | | |
2696 | 0 | switch (tcp_conn_get(id, NULL, 0, PROTO_NONE, NULL, &conn, NULL)) { |
2697 | 0 | case 1: |
2698 | 0 | goto found; |
2699 | 0 | case -1: |
2700 | 0 | return -1; |
2701 | 0 | default: |
2702 | 0 | return 0; |
2703 | 0 | } |
2704 | 0 | } |
2705 | | |
2706 | 0 | if (parse_phostport(ipport->s, ipport->len, &host.s, &host.len, |
2707 | 0 | &port, &proto) != 0 || port <= 0) { |
2708 | 0 | LM_ERR("failed to parse tcp connection [%.*s]\n", |
2709 | 0 | ipport->len, ipport->s); |
2710 | 0 | return -1; |
2711 | 0 | } |
2712 | | |
2713 | 0 | ip = str2ip(&host); |
2714 | 0 | if (!ip) |
2715 | 0 | ip = str2ip6(&host); |
2716 | 0 | if (!ip) { |
2717 | 0 | LM_ERR("invalid IP in tcp connection [%.*s]\n", |
2718 | 0 | ipport->len, ipport->s); |
2719 | 0 | return -1; |
2720 | 0 | } |
2721 | | |
2722 | 0 | if (proto != PROTO_NONE) { |
2723 | 0 | if (!is_tcp_based_proto(proto)) { |
2724 | 0 | LM_ERR("protocol %d is not TCP based for [%.*s]\n", |
2725 | 0 | proto, ipport->len, ipport->s); |
2726 | 0 | return -1; |
2727 | 0 | } |
2728 | 0 | start_proto = proto; |
2729 | 0 | end_proto = proto + 1; |
2730 | 0 | } else { |
2731 | 0 | start_proto = 0; |
2732 | 0 | end_proto = PROTO_LAST; |
2733 | 0 | } |
2734 | | |
2735 | 0 | for (p = start_proto; p < (unsigned int)end_proto; p++) { |
2736 | 0 | if (!is_tcp_based_proto(p)) |
2737 | 0 | continue; |
2738 | | |
2739 | 0 | switch (tcp_conn_get(0, ip, port, p, NULL, &conn, NULL)) { |
2740 | 0 | case 1: |
2741 | 0 | goto found; |
2742 | 0 | case -1: |
2743 | 0 | return -1; |
2744 | 0 | } |
2745 | 0 | } |
2746 | | |
2747 | 0 | return 0; |
2748 | | |
2749 | 0 | found: |
2750 | 0 | TCPCONN_LOCK(conn->id); |
2751 | 0 | conn->flags |= F_CONN_FORCE_CLOSED; |
2752 | 0 | TCPCONN_UNLOCK(conn->id); |
2753 | |
|
2754 | 0 | tcp_main_proc = tcp_get_main_proc_no(); |
2755 | 0 | if (tcp_main_proc < 0) |
2756 | 0 | rc = -1; |
2757 | 0 | else if (process_no == tcp_main_proc) |
2758 | 0 | rc = tcp_close_conn_run(conn); |
2759 | 0 | else |
2760 | 0 | rc = ipc_send_rpc(tcp_main_proc, tcp_close_conn_rpc, conn); |
2761 | |
|
2762 | 0 | if (rc < 0) { |
2763 | 0 | TCPCONN_LOCK(conn->id); |
2764 | 0 | conn->flags &= ~F_CONN_FORCE_CLOSED; |
2765 | 0 | TCPCONN_UNLOCK(conn->id); |
2766 | 0 | tcp_conn_release(conn, 0); |
2767 | 0 | return -1; |
2768 | 0 | } |
2769 | | |
2770 | 0 | return 1; |
2771 | 0 | } |
2772 | | |
2773 | | |
2774 | | /***************************** MI functions **********************************/ |
2775 | | |
2776 | | mi_response_t *mi_tcp_list_conns(const mi_params_t *params, |
2777 | | struct mi_handler *async_hdl) |
2778 | 0 | { |
2779 | 0 | mi_response_t *resp; |
2780 | 0 | mi_item_t *resp_obj; |
2781 | 0 | mi_item_t *conns_arr, *conn_item; |
2782 | 0 | struct tcp_connection *conn; |
2783 | 0 | time_t _ts; |
2784 | 0 | char date_buf[MI_DATE_BUF_LEN]; |
2785 | 0 | int date_buf_len; |
2786 | 0 | unsigned int i,j,part; |
2787 | 0 | char proto_buf[PROTO_NAME_MAX_SIZE]; |
2788 | 0 | char *proto_s; |
2789 | 0 | int proto_len; |
2790 | 0 | int proto_filter = PROTO_NONE; |
2791 | 0 | int filter_by_proto = 0; |
2792 | 0 | str proto_name; |
2793 | 0 | struct tm ltime; |
2794 | 0 | char *p; |
2795 | |
|
2796 | 0 | if (tcp_disabled) |
2797 | 0 | return init_mi_result_null(); |
2798 | | |
2799 | 0 | switch (try_get_mi_string_param(params, "proto", &proto_s, &proto_len)) { |
2800 | 0 | case 0: |
2801 | 0 | proto_name.s = proto_s; |
2802 | 0 | proto_name.len = proto_len; |
2803 | |
|
2804 | 0 | if (proto_len == 3 && str_strcasecmp(&proto_name, _str("any")) == 0) |
2805 | 0 | ; |
2806 | 0 | else if (parse_proto((unsigned char *)proto_s, proto_len, |
2807 | 0 | (int *)&proto_filter) < 0) |
2808 | 0 | return init_mi_error(400, MI_SSTR("Bad protocol")); |
2809 | 0 | else |
2810 | 0 | filter_by_proto = 1; |
2811 | 0 | break; |
2812 | 0 | case -1: |
2813 | 0 | break; |
2814 | 0 | default: |
2815 | 0 | return init_mi_param_error(); |
2816 | 0 | } |
2817 | | |
2818 | 0 | resp = init_mi_result_object(&resp_obj); |
2819 | 0 | if (!resp) |
2820 | 0 | return 0; |
2821 | | |
2822 | 0 | conns_arr = add_mi_array(resp_obj, MI_SSTR("Connections")); |
2823 | 0 | if (!conns_arr) { |
2824 | 0 | free_mi_response(resp); |
2825 | 0 | return 0; |
2826 | 0 | } |
2827 | | |
2828 | 0 | for( part=0 ; part<TCP_PARTITION_SIZE ; part++) { |
2829 | 0 | TCPCONN_LOCK(part); |
2830 | 0 | for( i=0; i<TCP_ID_HASH_SIZE ; i++ ) { |
2831 | 0 | for(conn=TCP_PART(part).tcpconn_id_hash[i];conn;conn=conn->id_next){ |
2832 | 0 | if (filter_by_proto && conn->type != proto_filter) |
2833 | 0 | continue; |
2834 | | |
2835 | | /* add one object fo each conn */ |
2836 | 0 | conn_item = add_mi_object(conns_arr, 0, 0); |
2837 | 0 | if (!conn_item) |
2838 | 0 | goto error; |
2839 | | |
2840 | | /* add ID */ |
2841 | 0 | if (add_mi_number(conn_item, MI_SSTR("ID"), conn->id) < 0) |
2842 | 0 | goto error; |
2843 | | |
2844 | | /* add type/proto */ |
2845 | 0 | p = proto2str(conn->type, proto_buf); |
2846 | 0 | if (add_mi_string(conn_item, MI_SSTR("Type"), proto_buf, |
2847 | 0 | (int)(long)(p-proto_buf)) < 0) |
2848 | 0 | goto error; |
2849 | | |
2850 | | /* add state */ |
2851 | 0 | if (add_mi_number(conn_item, MI_SSTR("State"), conn->state) < 0) |
2852 | 0 | goto error; |
2853 | | |
2854 | | /* add Remote IP:Port */ |
2855 | 0 | if (add_mi_string_fmt(conn_item, MI_SSTR("Remote"), "%s:%d", |
2856 | 0 | ip_addr2a(&conn->rcv.src_ip), conn->rcv.src_port) < 0) |
2857 | 0 | goto error; |
2858 | | |
2859 | | /* add Local IP:Port */ |
2860 | 0 | if (add_mi_string_fmt(conn_item, MI_SSTR("Local"), "%s:%d", |
2861 | 0 | ip_addr2a(&conn->rcv.dst_ip), conn->rcv.dst_port) < 0) |
2862 | 0 | goto error; |
2863 | | |
2864 | 0 | if (protos[conn->type].net.stream.conn.dump && |
2865 | 0 | protos[conn->type].net.stream.conn.dump(conn, |
2866 | 0 | conn_item) < 0) |
2867 | 0 | goto error; |
2868 | | |
2869 | | /* add lifetime */ |
2870 | 0 | _ts = (time_t)conn->lifetime + startup_time; |
2871 | 0 | localtime_r(&_ts, <ime); |
2872 | 0 | date_buf_len = strftime(date_buf, MI_DATE_BUF_LEN - 1, |
2873 | 0 | "%Y-%m-%d %H:%M:%S", <ime); |
2874 | 0 | if (date_buf_len != 0) { |
2875 | 0 | if (add_mi_string(conn_item, MI_SSTR("Lifetime"), |
2876 | 0 | date_buf, date_buf_len) < 0) |
2877 | 0 | goto error; |
2878 | 0 | } else { |
2879 | 0 | if (add_mi_number(conn_item, MI_SSTR("Lifetime"), _ts) < 0) |
2880 | 0 | goto error; |
2881 | 0 | } |
2882 | | |
2883 | | /* add the port-aliases */ |
2884 | 0 | for( j=0 ; j<conn->aliases ; j++ ) |
2885 | | /* add one node for each conn */ |
2886 | 0 | add_mi_number( conn_item, MI_SSTR("Alias port"), |
2887 | 0 | conn->con_aliases[j].port ); |
2888 | |
|
2889 | 0 | } |
2890 | 0 | } |
2891 | | |
2892 | 0 | TCPCONN_UNLOCK(part); |
2893 | 0 | } |
2894 | | |
2895 | 0 | return resp; |
2896 | | |
2897 | 0 | error: |
2898 | 0 | TCPCONN_UNLOCK(part); |
2899 | 0 | LM_ERR("failed to add MI item\n"); |
2900 | 0 | free_mi_response(resp); |
2901 | 0 | return 0; |
2902 | 0 | } |
2903 | | |
2904 | | |
2905 | | mi_response_t *mi_tcp_close_conn(const mi_params_t *params, |
2906 | | struct mi_handler *_) |
2907 | 0 | { |
2908 | 0 | str ipport; |
2909 | 0 | int rc; |
2910 | |
|
2911 | 0 | if (get_mi_string_param(params, "ipport", &ipport.s, &ipport.len) < 0) |
2912 | 0 | return init_mi_param_error(); |
2913 | | |
2914 | 0 | rc = tcp_close_connection(&ipport); |
2915 | 0 | if (rc < 0) |
2916 | 0 | return init_mi_error(400, MI_SSTR("Bad tcp connection")); |
2917 | 0 | if (rc == 0) |
2918 | 0 | return init_mi_result_null(); |
2919 | | |
2920 | 0 | return init_mi_result_ok(); |
2921 | 0 | } |