tcp_stats.h 3.6 KB

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