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