statistics.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __STATISTICS_H
  15. #define __STATISTICS_H 1
  16. #include <cc/data.h>
  17. #include <dns/message.h>
  18. #include <dns/opcode.h>
  19. #include <statistics/counter.h>
  20. #include <boost/noncopyable.hpp>
  21. #include <boost/optional.hpp>
  22. #include <bitset>
  23. #include <stdint.h>
  24. namespace isc {
  25. namespace auth {
  26. namespace statistics {
  27. /// \brief DNS Message attributes for statistics.
  28. ///
  29. /// This class holds some attributes related to a DNS message
  30. /// for statistics data collection.
  31. class MessageAttributes {
  32. public:
  33. /// \brief IP version of DNS message.
  34. enum IPVersionType {
  35. IP_VERSION_UNSPEC, // (initial value; internal use only)
  36. IP_VERSION_IPV4, ///< IPv4 message
  37. IP_VERSION_IPV6 ///< IPv6 message
  38. };
  39. /// \brief Transport protocol of DNS message.
  40. enum TransportProtocolType {
  41. TRANSPORT_UNSPEC, // (initial value; internal use only)
  42. TRANSPORT_UDP, ///< UDP message
  43. TRANSPORT_TCP ///< TCP message
  44. };
  45. private:
  46. // request attributes
  47. int req_ip_version_; // IP version
  48. int req_transport_protocol_; // Transport layer protocol
  49. boost::optional<isc::dns::Opcode> req_opcode_; // OpCode
  50. enum BitAttributes {
  51. REQ_WITH_EDNS_0, // request with EDNS ver.0
  52. REQ_WITH_DNSSEC_OK, // DNSSEC OK (DO) bit is set in request
  53. REQ_TSIG_SIGNED, // request is signed with valid TSIG
  54. REQ_BADSIG, // request is signed but bad signature
  55. RES_IS_TRUNCATED, // response is truncated
  56. RES_TSIG_SIGNED, // response is signed with TSIG
  57. BIT_ATTRIBUTES_TYPES
  58. };
  59. std::bitset<BIT_ATTRIBUTES_TYPES> bit_attributes_;
  60. public:
  61. /// \brief The constructor.
  62. ///
  63. /// \throw None
  64. MessageAttributes() : req_ip_version_(0), req_transport_protocol_(0)
  65. {}
  66. /// \brief Return opcode of the request.
  67. /// \return opcode of the request wrapped with boost::optional
  68. /// \throw isc::InvalidOperation Opcode is not set
  69. const boost::optional<const isc::dns::Opcode&> getRequestOpCode() const {
  70. if (req_opcode_) {
  71. return (req_opcode_.get());
  72. } else {
  73. return boost::none;
  74. }
  75. }
  76. /// \brief Set opcode of the request.
  77. /// \param opcode Opcode of the request
  78. /// \throw None
  79. void setRequestOpCode(const isc::dns::Opcode& opcode) {
  80. req_opcode_ = opcode;
  81. }
  82. /// \brief Get IP version carrying a request.
  83. /// \return IP version carrying a request (AF_INET or AF_INET6)
  84. /// \throw None
  85. int getRequestIPVersion() const {
  86. return (req_ip_version_);
  87. }
  88. /// \brief Set IP version carrying a request.
  89. /// \param ip_version AF_INET or AF_INET6
  90. /// \throw None
  91. void setRequestIPVersion(const int ip_version) {
  92. req_ip_version_ = ip_version;
  93. }
  94. /// \brief Get transport protocol carrying a request.
  95. /// \return Transport protocol carrying a request
  96. /// (IPPROTO_UDP or IPPROTO_TCP)
  97. /// \throw None
  98. int getRequestTransportProtocol() const {
  99. return (req_transport_protocol_);
  100. }
  101. /// \brief Set transport protocol carrying a request.
  102. /// \param transport_protocol IPPROTO_UDP or IPPROTO_TCP
  103. /// \throw None
  104. void setRequestTransportProtocol(const int transport_protocol) {
  105. req_transport_protocol_ = transport_protocol;
  106. }
  107. /// \brief Return whether EDNS version of the request is 0 or not.
  108. /// \return true if EDNS version of the request is 0
  109. /// \throw None
  110. bool getRequestEDNS0() const {
  111. return (bit_attributes_[REQ_WITH_EDNS_0]);
  112. }
  113. /// \brief Set whether EDNS version of the request is 0 or not.
  114. /// \param with_edns_0 true if EDNS version of the request is 0
  115. /// \throw None
  116. void setRequestEDNS0(const bool with_edns_0) {
  117. bit_attributes_[REQ_WITH_EDNS_0] = with_edns_0;
  118. }
  119. /// \brief Return DNSSEC OK (DO) bit of the request.
  120. /// \return true if DNSSEC OK (DO) bit of the request is set
  121. /// \throw None
  122. bool getRequestDO() const {
  123. return (bit_attributes_[REQ_WITH_DNSSEC_OK]);
  124. }
  125. /// \brief Set DNSSEC OK (DO) bit of the request.
  126. /// \param with_dnssec_ok true if DNSSEC OK (DO) bit of the request is set
  127. /// \throw None
  128. void setRequestDO(const bool with_dnssec_ok) {
  129. bit_attributes_[REQ_WITH_DNSSEC_OK] = with_dnssec_ok;
  130. }
  131. /// \brief Return whether the request is TSIG signed or not.
  132. /// \return true if the request is TSIG signed
  133. /// \throw None
  134. bool getRequestSigTSIG() const {
  135. return (bit_attributes_[REQ_TSIG_SIGNED]);
  136. }
  137. /// \brief Return whether the signature of the request is bad or not.
  138. /// \return true if the signature of the request is bad
  139. /// \throw None
  140. bool getRequestSigBadSig() const {
  141. return (bit_attributes_[REQ_BADSIG]);
  142. }
  143. /// \brief Set TSIG attributes of the request.
  144. /// \param signed_tsig true if the request is signed with TSIG
  145. /// \param badsig true if the signature of the request is bad
  146. /// \throw None
  147. void setRequestTSIG(const bool signed_tsig, const bool badsig) {
  148. assert(!(!signed_tsig && badsig));
  149. bit_attributes_[REQ_TSIG_SIGNED] = signed_tsig;
  150. bit_attributes_[REQ_BADSIG] = badsig;
  151. }
  152. /// \brief Return TC (truncated) bit of the response.
  153. /// \return true if the response is truncated
  154. /// \throw None
  155. bool getResponseTruncated() const {
  156. return (bit_attributes_[RES_IS_TRUNCATED]);
  157. }
  158. /// \brief Set TC (truncated) bit of the response.
  159. /// \param is_truncated true if the response is truncated
  160. /// \throw None
  161. void setResponseTruncated(const bool is_truncated) {
  162. bit_attributes_[RES_IS_TRUNCATED] = is_truncated;
  163. }
  164. /// \brief Return whether the response is TSIG signed or not.
  165. /// \return true if the response is signed with TSIG
  166. /// \throw None
  167. bool getResponseTSIG() const {
  168. return (bit_attributes_[RES_TSIG_SIGNED]);
  169. }
  170. /// \brief Set whether the response is TSIG signed or not.
  171. /// \param signed_tsig true if the response is signed with TSIG
  172. /// \throw None
  173. void setResponseTSIG(const bool signed_tsig) {
  174. bit_attributes_[RES_TSIG_SIGNED] = signed_tsig;
  175. }
  176. };
  177. /// \brief Set of DNS message counters.
  178. ///
  179. /// \c Counters is a set of DNS message counters class. It holds DNS message
  180. /// counters and provides an interface to increment the counter of specified
  181. /// type (e.g. UDP message, TCP message).
  182. ///
  183. /// This class is designed to be a part of \c AuthSrv.
  184. /// Call \c inc() to increment a counter for the message.
  185. /// Call \c get() to get a set of DNS message counters.
  186. ///
  187. /// We may eventually want to change the structure to hold values that are
  188. /// not counters (such as concurrent TCP connections), or seperate generic
  189. /// part to src/lib to share with the other modules.
  190. ///
  191. /// This class is constructed on startup of the server, so
  192. /// construction overhead of this approach should be acceptable.
  193. ///
  194. /// \todo Consider overhead of \c Counters::inc()
  195. class Counters : boost::noncopyable {
  196. private:
  197. // counter for DNS message attributes
  198. isc::statistics::Counter server_msg_counter_;
  199. void incRequest(const MessageAttributes& msgattrs);
  200. void incResponse(const MessageAttributes& msgattrs,
  201. const isc::dns::Message& response);
  202. public:
  203. /// \brief A type of statistics item tree in isc::data::MapElement.
  204. /// \verbatim
  205. /// {
  206. /// zone_name => {
  207. /// item_name => item_value,
  208. /// item_name => item_value, ...
  209. /// },
  210. /// ...
  211. /// }
  212. /// item_name is a string seperated by '.'.
  213. /// item_value is an integer.
  214. /// \endverbatim
  215. ///
  216. typedef isc::data::ConstElementPtr ConstItemTreePtr;
  217. /// \brief The constructor.
  218. ///
  219. /// This constructor is mostly exception free. But it may still throw
  220. /// a standard exception if memory allocation fails inside the method.
  221. ///
  222. Counters();
  223. /// \brief Increment counters according to the parameters.
  224. ///
  225. /// \param msgattrs DNS message attributes.
  226. /// \param response DNS response message.
  227. /// \param done DNS response was sent to the client.
  228. ///
  229. /// \throw None
  230. ///
  231. void inc(const MessageAttributes& msgattrs,
  232. const isc::dns::Message& response, const bool done);
  233. /// \brief Get statistics counters.
  234. ///
  235. /// This method is mostly exception free. But it may still throw a
  236. /// standard exception if memory allocation fails inside the method.
  237. ///
  238. /// \return statistics data
  239. ///
  240. /// \throw std::bad_alloc Internal resource allocation fails
  241. ///
  242. ConstItemTreePtr get() const;
  243. };
  244. } // namespace statistics
  245. } // namespace auth
  246. } // namespace isc
  247. #endif // __STATISTICS_H
  248. // Local Variables:
  249. // mode: c++
  250. // End: