modules/ims_qos: added patch for flow-description bug when request originates from...
[sip-router] / src / core / tcp_stats.c
1 /* 
2  * Copyright (C) 2010 iptelorg GmbH
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /** Kamailio core :: TCP statistics.
18  * @file tcp_stats.c
19  * @ingroup:  core
20  * Module: \ref core
21  */
22
23 #ifdef USE_TCP
24 #include "tcp_stats.h"
25
26 #ifdef USE_TCP_STATS
27
28 #include "counters.h"
29 #include "tcp_info.h"
30
31 struct tcp_counters_h tcp_cnts_h;
32
33
34 enum tcp_info_req { TCP_INFO_NONE, TCP_INFO_CONN_NO, TCP_INFO_WR_QUEUE_SZ };
35
36 static counter_val_t tcp_info(counter_handle_t h, void* what);
37
38 /* tcp counters definitions */
39 counter_def_t tcp_cnt_defs[] =  {
40         {&tcp_cnts_h.established, "established", 0, 0, 0,
41                 "incremented each time a tcp connection is established."},
42         {&tcp_cnts_h.passive_open, "passive_open", 0, 0, 0,
43                 "total number of accepted connections (so far)."},
44         {&tcp_cnts_h.connect_success, "connect_success", 0, 0, 0,
45                 "total number of successfully active opened connections"
46                         " (successful connect()s)."},
47         {&tcp_cnts_h.connect_failed, "connect_failed", 0, 0, 0,
48                 "number of failed active connection attempts."},
49         {&tcp_cnts_h.local_reject, "local_reject", 0, 0, 0,
50                 "number of rejected incoming connections."},
51         {&tcp_cnts_h.con_timeout, "con_timeout", 0, 0, 0,
52                 "total number of connections that did timeout (idle for too long)."},
53         {&tcp_cnts_h.con_reset, "con_reset", 0, 0, 0,
54                 "total number of TCP_RSTs received on established connections."},
55         {&tcp_cnts_h.send_timeout, "send_timeout", 0, 0, 0,
56                 "number of send attempts that failed due to a timeout"
57                         "(note: works only in tcp async mode)."},
58         {&tcp_cnts_h.sendq_full, "sendq_full", 0, 0, 0,
59                 "number of send attempts that failed because of exceeded buffering"
60                         "capacity (send queue full, works only in tcp async mode)."},
61         {0, "current_opened_connections", 0,
62                 tcp_info, (void*)(long)TCP_INFO_CONN_NO,
63                 "number of currently opened connections."},
64         {0, "current_write_queue_size", 0,
65                 tcp_info, (void*)(long)TCP_INFO_WR_QUEUE_SZ,
66                 "current sum of all the connections write queue sizes."},
67         {0, 0, 0, 0, 0, 0 }
68 };
69
70
71
72 /** helper function for some stats (which are kept internally inside tcp).
73  */
74 static counter_val_t tcp_info(counter_handle_t h, void* what)
75 {
76         enum tcp_info_req w;
77         struct tcp_gen_info ti;
78
79         if (tcp_disable)
80                 return 0;
81         w = (int)(long)what;
82         tcp_get_info(&ti);
83         switch(w) {
84                 case TCP_INFO_CONN_NO:
85                         return ti.tcp_connections_no;
86                 case TCP_INFO_WR_QUEUE_SZ:
87                         return ti.tcp_write_queued;
88                 case TCP_INFO_NONE:
89                         break;
90         };
91         return 0;
92 }
93
94 /** initialize tcp statistics.
95  *  Must be called before forking.
96  * @return < 0 on errror, 0 on success.
97  */
98 int tcp_stats_init()
99 {
100 #define TCP_REG_COUNTER(name) \
101         if (counter_register(&tcp_cnts_h.name, "tcp", # name, 0, 0, 0, 0) < 0) \
102                 goto error;
103
104         if (counter_register_array("tcp", tcp_cnt_defs) < 0)
105                 goto error;
106         return 0;
107 error:
108         return -1;
109 }
110
111
112 void tcp_stats_destroy()
113 {
114         /* do nothing */
115 }
116
117
118 #endif /* USE_TCP_STATS */
119 #endif /* USE_TCP */
120 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */