statistics.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. IPVersionType req_ip_version_; // IP version
  48. TransportProtocolType 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() :
  65. req_ip_version_(IP_VERSION_UNSPEC),
  66. req_transport_protocol_(TRANSPORT_UNSPEC)
  67. {}
  68. /// \brief Return opcode of the request.
  69. /// \return opcode of the request
  70. /// \throw isc::InvalidOperation Opcode is not set
  71. const isc::dns::Opcode& getRequestOpCode() const {
  72. if (req_opcode_) {
  73. return (req_opcode_.get());
  74. } else {
  75. isc_throw(isc::InvalidOperation, "Opcode is not set");
  76. }
  77. }
  78. /// \brief Set opcode of the request.
  79. /// \param opcode Opcode of the request
  80. /// \throw None
  81. void setRequestOpCode(const isc::dns::Opcode& opcode) {
  82. req_opcode_ = opcode;
  83. }
  84. /// \brief Return IP version carrying the request.
  85. /// \return IP version carrying the request
  86. /// \throw None
  87. IPVersionType getRequestIPVersion() const {
  88. return (req_ip_version_);
  89. }
  90. /// \brief Set IP version carrying the request.
  91. /// \param ip_version IP version carrying the request
  92. /// \throw isc::InvalidParameter ip_version is invalid
  93. void setRequestIPVersion(const IPVersionType ip_version) {
  94. if (ip_version == IP_VERSION_UNSPEC) {
  95. isc_throw(isc::InvalidParameter, "Invalid IP version");
  96. }
  97. req_ip_version_ = ip_version;
  98. }
  99. /// \brief Return transport protocol carrying the request.
  100. /// \return Transport protocol carrying the request
  101. /// \throw None
  102. TransportProtocolType getRequestTransportProtocol() const {
  103. return (req_transport_protocol_);
  104. }
  105. /// \brief Set transport protocol carrying the request.
  106. /// \param transport_protocol Transport protocol carrying the request
  107. /// \throw isc::InvalidParameter transport_protocol is invalid
  108. void setRequestTransportProtocol(
  109. const TransportProtocolType transport_protocol)
  110. {
  111. if (transport_protocol == TRANSPORT_UNSPEC) {
  112. isc_throw(isc::InvalidParameter, "Invalid transport protocol");
  113. }
  114. req_transport_protocol_ = transport_protocol;
  115. }
  116. /// \brief Return whether EDNS version of the request is 0 or not.
  117. /// \return true if EDNS version of the request is 0
  118. /// \throw None
  119. bool getRequestEDNS0() const {
  120. return (bit_attributes_[REQ_WITH_EDNS_0]);
  121. }
  122. /// \brief Set whether EDNS version of the request is 0 or not.
  123. /// \param with_edns_0 true if EDNS version of the request is 0
  124. /// \throw None
  125. void setRequestEDNS0(const bool with_edns_0) {
  126. bit_attributes_[REQ_WITH_EDNS_0] = with_edns_0;
  127. }
  128. /// \brief Return DNSSEC OK (DO) bit of the request.
  129. /// \return true if DNSSEC OK (DO) bit of the request is set
  130. /// \throw None
  131. bool getRequestDO() const {
  132. return (bit_attributes_[REQ_WITH_DNSSEC_OK]);
  133. }
  134. /// \brief Set DNSSEC OK (DO) bit of the request.
  135. /// \param with_dnssec_ok true if DNSSEC OK (DO) bit of the request is set
  136. /// \throw None
  137. void setRequestDO(const bool with_dnssec_ok) {
  138. bit_attributes_[REQ_WITH_DNSSEC_OK] = with_dnssec_ok;
  139. }
  140. /// \brief Return whether the request is TSIG signed or not.
  141. /// \return true if the request is TSIG signed
  142. /// \throw None
  143. bool getRequestSigTSIG() const {
  144. return (bit_attributes_[REQ_TSIG_SIGNED]);
  145. }
  146. /// \brief Return whether the signature of the request is bad or not.
  147. /// \return true if the signature of the request is bad
  148. /// \throw None
  149. bool getRequestSigBadSig() const {
  150. return (bit_attributes_[REQ_BADSIG]);
  151. }
  152. /// \brief Set TSIG attributes of the request.
  153. /// \param signed_tsig true if the request is signed with TSIG
  154. /// \param badsig true if the signature of the request is bad
  155. /// \throw None
  156. void setRequestTSIG(const bool signed_tsig, const bool badsig) {
  157. assert(!(!signed_tsig && badsig));
  158. bit_attributes_[REQ_TSIG_SIGNED] = signed_tsig;
  159. bit_attributes_[REQ_BADSIG] = badsig;
  160. }
  161. /// \brief Return TC (truncated) bit of the response.
  162. /// \return true if the response is truncated
  163. /// \throw None
  164. bool getResponseTruncated() const {
  165. return (bit_attributes_[RES_IS_TRUNCATED]);
  166. }
  167. /// \brief Set TC (truncated) bit of the response.
  168. /// \param is_truncated true if the response is truncated
  169. /// \throw None
  170. void setResponseTruncated(const bool is_truncated) {
  171. bit_attributes_[RES_IS_TRUNCATED] = is_truncated;
  172. }
  173. /// \brief Return whether the response is TSIG signed or not.
  174. /// \return true if the response is signed with TSIG
  175. /// \throw None
  176. bool getResponseTSIG() const {
  177. return (bit_attributes_[RES_TSIG_SIGNED]);
  178. }
  179. /// \brief Set whether the response is TSIG signed or not.
  180. /// \param signed_tsig true if the response is signed with TSIG
  181. /// \throw None
  182. void setResponseTSIG(const bool signed_tsig) {
  183. bit_attributes_[RES_TSIG_SIGNED] = signed_tsig;
  184. }
  185. };
  186. /// \brief Set of DNS message counters.
  187. ///
  188. /// \c Counters is a set of DNS message counters class. It holds DNS message
  189. /// counters and provides an interface to increment the counter of specified
  190. /// type (e.g. UDP message, TCP message).
  191. ///
  192. /// This class is designed to be a part of \c AuthSrv.
  193. /// Call \c inc() to increment a counter for the message.
  194. /// Call \c get() to get a set of DNS message counters.
  195. ///
  196. /// We may eventually want to change the structure to hold values that are
  197. /// not counters (such as concurrent TCP connections), or seperate generic
  198. /// part to src/lib to share with the other modules.
  199. ///
  200. /// This class is constructed on startup of the server, so
  201. /// construction overhead of this approach should be acceptable.
  202. ///
  203. /// \todo Consider overhead of \c Counters::inc()
  204. class Counters : boost::noncopyable {
  205. private:
  206. // counter for DNS message attributes
  207. isc::statistics::Counter server_msg_counter_;
  208. void incRequest(const MessageAttributes& msgattrs);
  209. void incResponse(const MessageAttributes& msgattrs,
  210. const isc::dns::Message& response);
  211. public:
  212. /// \brief A type of statistics item tree in isc::data::MapElement.
  213. /// \verbatim
  214. /// {
  215. /// zone_name => {
  216. /// item_name => item_value,
  217. /// item_name => item_value, ...
  218. /// },
  219. /// ...
  220. /// }
  221. /// item_name is a string seperated by '.'.
  222. /// item_value is an integer.
  223. /// \endverbatim
  224. ///
  225. typedef isc::data::ConstElementPtr ConstItemTreePtr;
  226. /// \brief The constructor.
  227. ///
  228. /// This constructor is mostly exception free. But it may still throw
  229. /// a standard exception if memory allocation fails inside the method.
  230. ///
  231. Counters();
  232. /// \brief Increment counters according to the parameters.
  233. ///
  234. /// This method is mostly exception free. But it may still throw a
  235. /// standard exception if memory allocation fails inside the method.
  236. ///
  237. /// \param msgattrs DNS message attributes.
  238. /// \param response DNS response message.
  239. /// \param done DNS response was sent to the client.
  240. ///
  241. void inc(const MessageAttributes& msgattrs,
  242. const isc::dns::Message& response, const bool done);
  243. /// \brief Get statistics counters.
  244. ///
  245. /// This method is mostly exception free. But it may still throw a
  246. /// standard exception if memory allocation fails inside the method.
  247. ///
  248. /// \return statistics data
  249. ///
  250. /// \throw std::bad_alloc Internal resource allocation fails
  251. ///
  252. ConstItemTreePtr get() const;
  253. };
  254. } // namespace statistics
  255. } // namespace auth
  256. } // namespace isc
  257. #endif // __STATISTICS_H
  258. // Local Variables:
  259. // mode: c++
  260. // End: