tcp_stats.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2009 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. /*! \file
  17. * \brief Kamailio core :: tcp_stats.h - tcp statistics macros
  18. * \ingroup core
  19. */
  20. #ifndef __tcp_stats_h
  21. #define __tcp_stats_h
  22. /* enable tcp stats by default */
  23. #ifndef NO_TCP_STATS
  24. #define USE_TCP_STATS
  25. #endif
  26. #ifndef USE_TCP_STATS
  27. #define INIT_TCP_STATS() 0 /* success */
  28. #define DESTROY_TCP_STATS()
  29. #define TCP_STATS_ESTABLISHED(state)
  30. #define TCP_STATS_CONNECT_FAILED()
  31. #define TCP_STATS_LOCAL_REJECT()
  32. #define TCP_STATS_CON_TIMEOUT()
  33. #define TCP_STATS_CON_RESET()
  34. #define TCP_STATS_SEND_TIMEOUT()
  35. #define TCP_STATS_SENDQ_FULL()
  36. #else /* USE_TCP_STATS */
  37. #include "counters.h"
  38. struct tcp_counters_h {
  39. counter_handle_t established;
  40. counter_handle_t passive_open;
  41. counter_handle_t connect_success;
  42. counter_handle_t connect_failed;
  43. counter_handle_t local_reject;
  44. counter_handle_t con_timeout;
  45. counter_handle_t con_reset;
  46. counter_handle_t send_timeout;
  47. counter_handle_t sendq_full;
  48. };
  49. extern struct tcp_counters_h tcp_cnts_h;
  50. int tcp_stats_init(void);
  51. void tcp_stats_destroy(void);
  52. #define INIT_TCP_STATS() tcp_stats_init()
  53. #define DESTROY_TCP_STATS() tcp_stats_destroy()
  54. /** called each time a new tcp connection is established.
  55. * @param state - S_CONN_ACCEPT if it was the result of an accept()
  56. * - S_CONN_CONNECT if it was the result of a connect()
  57. * Note: in general it will be called when the first packet was received or
  58. * sent on the new connection and not immediately after accept() or
  59. * connect()
  60. */
  61. #define TCP_STATS_ESTABLISHED(state) \
  62. do { \
  63. counter_inc(tcp_cnts_h.established); \
  64. if (state == S_CONN_ACCEPT) \
  65. counter_inc(tcp_cnts_h.passive_open); \
  66. else \
  67. counter_inc(tcp_cnts_h.connect_success); \
  68. }while(0)
  69. /** called each time a new outgoing connection fails. */
  70. #define TCP_STATS_CONNECT_FAILED() \
  71. counter_inc(tcp_cnts_h.connect_failed)
  72. /** called each time a new incoming connection is rejected.
  73. * (accept() denied due to maximum number of TCP connections being exceeded)
  74. */
  75. #define TCP_STATS_LOCAL_REJECT() \
  76. counter_inc(tcp_cnts_h.local_reject)
  77. /** called each time a connection lifetime expires.
  78. * (the connection is closed for being idle for too long)
  79. */
  80. #define TCP_STATS_CON_TIMEOUT() \
  81. counter_inc(tcp_cnts_h.con_timeout)
  82. /** called each time a TCP RST is received on an established connection. */
  83. #define TCP_STATS_CON_RESET() \
  84. counter_inc(tcp_cnts_h.con_reset)
  85. /** called each time a send operation fails due to a timeout.
  86. * FIXME: it works only in async mode (in sync. mode a send might timeout
  87. * but the stats won't be increased).
  88. */
  89. #define TCP_STATS_SEND_TIMEOUT() \
  90. counter_inc(tcp_cnts_h.send_timeout)
  91. /** called each time a send fails due to the buffering capacity being exceeded.
  92. * (used only in tcp async mode)
  93. */
  94. #define TCP_STATS_SENDQ_FULL() \
  95. counter_inc(tcp_cnts_h.sendq_full)
  96. #endif /* USE_TCP_STATS */
  97. #endif /*__tcp_stats_h*/
  98. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */