data.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* $Id: data.h,v 1.14 2002/08/31 16:09:55 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * A host carrying a 'mon' is considered a 'source' of information. A single
  33. * data 'stream' of information has a particular type: <cpu|mem|if|io>. A
  34. * source can provide multiple 'streams' simultaneously. A source spools
  35. * information towards a 'mux'. A 'stream' that has been converted to network
  36. * representation is called a 'packedstream'.
  37. */
  38. #ifndef _MON_LIB_DATA_H
  39. #define _MON_LIB_DATA_H
  40. #include <sys/queue.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include <limits.h>
  45. #include "lex.h"
  46. /* Polynominal to use for CRC generation */
  47. #define MON_CRCPOLY 0x04c11db7
  48. #if BYTE_ORDER == BIG_ENDIAN
  49. #define htonq(n) (n)
  50. #define ntohq(n) (n)
  51. #else
  52. static inline u_int64_t
  53. htonq (u_int64_t v)
  54. {
  55. return (u_int64_t)htonl(v) << 32 | htonl(v >> 32);
  56. }
  57. static inline u_int64_t
  58. ntohq (u_int64_t v)
  59. {
  60. return (u_int64_t)ntohl(v) << 32 | ntohl(v >> 32);
  61. }
  62. #endif
  63. /* Mon packet version
  64. * version 1:
  65. * mon_version:timestamp:length:crc:n*packedstream
  66. *
  67. * Note that the data portion is limited. The current (31/03/2002) largest
  68. * streamtype (if) needs 42 bytes without arguments. My _POSIX2_LINE_MAX is 2k,
  69. * so that works out to about 38 packedstreams in a single mon packet.
  70. */
  71. #define MON_PACKET_VER 1
  72. struct monpacket {
  73. struct {
  74. u_int64_t timestamp;
  75. u_int32_t crc;
  76. u_int16_t length;
  77. u_int8_t mon_version;
  78. u_int8_t reserved;
  79. } header;
  80. char data[_POSIX2_LINE_MAX];
  81. };
  82. /* The difference between a stream and a packed stream:
  83. * - A stream ties stream information to a file.
  84. * - A packed stream is the measured data itself
  85. *
  86. * A stream is meta data describing properties, a packed stream is the data itself.
  87. */
  88. struct stream {
  89. int type;
  90. char *args;
  91. char *file;
  92. SLIST_ENTRY(stream) streams;
  93. };
  94. SLIST_HEAD(streamlist, stream);
  95. struct source {
  96. char *name;
  97. u_int32_t ip;
  98. u_int16_t port;
  99. struct streamlist sl;
  100. SLIST_ENTRY(source) sources;
  101. };
  102. SLIST_HEAD(sourcelist, source);
  103. struct mux {
  104. char *name;
  105. int offset;
  106. int clientsocket;
  107. int monsocket;
  108. struct monpacket packet;
  109. struct sockaddr_in sockaddr;
  110. struct streamlist sl;
  111. u_int32_t senderr;
  112. u_int32_t ip;
  113. u_int16_t port;
  114. SLIST_ENTRY(mux) muxes;
  115. };
  116. SLIST_HEAD(muxlist, mux);
  117. /* ps2str types */
  118. #define PS2STR_PRETTY 0
  119. #define PS2STR_RRD 1
  120. /* Stream types */
  121. #define MT_IO 0
  122. #define MT_CPU 1
  123. #define MT_MEM 2
  124. #define MT_IF 3
  125. #define MT_PF 4
  126. #define MT_EOT 5
  127. /* NOTE: struct packetstream
  128. *
  129. * Unpacking of incoming packets is done via a packedstream structure. This
  130. * structure defines the maximum amount of data that can be contained in a
  131. * single network representation of a stream. It is used internally for sizing
  132. * only. Although the union members are here, they could also read u_int64_t[4]
  133. * with io, for instance.
  134. */
  135. #define MON_PS_ARGLEN 16
  136. struct packedstream {
  137. int type;
  138. int padding;
  139. char args[MON_PS_ARGLEN];
  140. union {
  141. struct {
  142. u_int64_t mtotal_transfers;
  143. u_int64_t mtotal_seeks;
  144. u_int64_t mtotal_bytes;
  145. } ps_io;
  146. struct {
  147. u_int16_t muser;
  148. u_int16_t mnice;
  149. u_int16_t msystem;
  150. u_int16_t minterrupt;
  151. u_int16_t midle;
  152. } ps_cpu;
  153. struct {
  154. u_int32_t mreal_active;
  155. u_int32_t mreal_total;
  156. u_int32_t mfree;
  157. u_int32_t mswap_used;
  158. u_int32_t mswap_total;
  159. } ps_mem;
  160. struct {
  161. u_int32_t mipackets;
  162. u_int32_t mopackets;
  163. u_int32_t mibytes;
  164. u_int32_t mobytes;
  165. u_int32_t mimcasts;
  166. u_int32_t momcasts;
  167. u_int32_t mierrors;
  168. u_int32_t moerrors;
  169. u_int32_t mcolls;
  170. u_int32_t mdrops;
  171. } ps_if;
  172. struct {
  173. u_int64_t bytes_v4_in;
  174. u_int64_t bytes_v4_out;
  175. u_int64_t bytes_v6_in;
  176. u_int64_t bytes_v6_out;
  177. u_int64_t packets_v4_in_pass;
  178. u_int64_t packets_v4_in_drop;
  179. u_int64_t packets_v4_out_pass;
  180. u_int64_t packets_v4_out_drop;
  181. u_int64_t packets_v6_in_pass;
  182. u_int64_t packets_v6_in_drop;
  183. u_int64_t packets_v6_out_pass;
  184. u_int64_t packets_v6_out_drop;
  185. u_int64_t states_entries;
  186. u_int64_t states_searches;
  187. u_int64_t states_inserts;
  188. u_int64_t states_removals;
  189. u_int64_t counters_match;
  190. u_int64_t counters_badoffset;
  191. u_int64_t counters_fragment;
  192. u_int64_t counters_short;
  193. u_int64_t counters_normalize;
  194. u_int64_t counters_memory;
  195. } ps_pf;
  196. } data;
  197. };
  198. /* prototypes */
  199. __BEGIN_DECLS
  200. const char *type2str(const int);
  201. const int token2type(const int);
  202. int calculate_churnbuffer(struct sourcelist *);
  203. int ps2strn(struct packedstream *, char *, int, int);
  204. int snpack(char *, int, char*, int, ...);
  205. int strlentype(int);
  206. int sunpack(char *, struct packedstream *);
  207. struct mux *add_mux(struct muxlist *, char *);
  208. struct mux *find_mux(struct muxlist *, char *);
  209. struct mux * rename_mux(struct muxlist *, struct mux *, char *);
  210. struct source *add_source(struct sourcelist *, char *);
  211. struct source *find_source(struct sourcelist *, char *);
  212. struct source *find_source_ip(struct sourcelist *, u_int32_t);
  213. struct stream *add_mux_stream(struct mux *, int, char *);
  214. struct stream *add_source_stream(struct source *, int, char *);
  215. struct stream *find_mux_stream(struct mux *, int, char *);
  216. struct stream *find_source_stream(struct source *, int, char *);
  217. u_int32_t crc32(const void*, unsigned int);
  218. void free_muxlist(struct muxlist *);
  219. void free_sourcelist(struct sourcelist *);
  220. void free_streamlist(struct streamlist *);
  221. void init_crc32();
  222. __END_DECLS
  223. #endif /*_MON_LIB_DATA_H*/