/contrib/ntp/ntpd/ntp_monitor.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 358 lines · 218 code · 38 blank · 102 comment · 41 complexity · 0df9be83c4b2c1b855ef6aa0a2b1cf3a MD5 · raw file

  1. /*
  2. * ntp_monitor - monitor ntpd statistics
  3. */
  4. #ifdef HAVE_CONFIG_H
  5. # include <config.h>
  6. #endif
  7. #include "ntpd.h"
  8. #include "ntp_io.h"
  9. #include "ntp_if.h"
  10. #include "ntp_stdlib.h"
  11. #include <ntp_random.h>
  12. #include <stdio.h>
  13. #include <signal.h>
  14. #ifdef HAVE_SYS_IOCTL_H
  15. # include <sys/ioctl.h>
  16. #endif
  17. /*
  18. * I'm still not sure I like what I've done here. It certainly consumes
  19. * memory like it is going out of style, and also may not be as low
  20. * overhead as I'd imagined.
  21. *
  22. * Anyway, we record statistics based on source address, mode and
  23. * version (for now, anyway. Check the code). The receive procedure
  24. * calls us with the incoming rbufp before it does anything else.
  25. *
  26. * Each entry is doubly linked into two lists, a hash table and a
  27. * most-recently-used list. When a packet arrives it is looked up in
  28. * the hash table. If found, the statistics are updated and the entry
  29. * relinked at the head of the MRU list. If not found, a new entry is
  30. * allocated, initialized and linked into both the hash table and at the
  31. * head of the MRU list.
  32. *
  33. * Memory is usually allocated by grabbing a big chunk of new memory and
  34. * cutting it up into littler pieces. The exception to this when we hit
  35. * the memory limit. Then we free memory by grabbing entries off the
  36. * tail for the MRU list, unlinking from the hash table, and
  37. * reinitializing.
  38. *
  39. * trimmed back memory consumption ... jdg 8/94
  40. */
  41. /*
  42. * Limits on the number of structures allocated. This limit is picked
  43. * with the illicit knowlege that we can only return somewhat less
  44. * than 8K bytes in a mode 7 response packet, and that each structure
  45. * will require about 20 bytes of space in the response.
  46. *
  47. * ... I don't believe the above is true anymore ... jdg
  48. */
  49. #ifndef MAXMONMEM
  50. #define MAXMONMEM 600 /* we allocate up to 600 structures */
  51. #endif
  52. #ifndef MONMEMINC
  53. #define MONMEMINC 40 /* allocate them 40 at a time */
  54. #endif
  55. /*
  56. * Hashing stuff
  57. */
  58. #define MON_HASH_SIZE 128
  59. #define MON_HASH_MASK (MON_HASH_SIZE-1)
  60. #define MON_HASH(addr) sock_hash(addr)
  61. /*
  62. * Pointers to the hash table, the MRU list and the count table. Memory
  63. * for the hash and count tables is only allocated if monitoring is
  64. * turned on.
  65. */
  66. static struct mon_data *mon_hash[MON_HASH_SIZE]; /* list ptrs */
  67. struct mon_data mon_mru_list;
  68. /*
  69. * List of free structures structures, and counters of free and total
  70. * structures. The free structures are linked with the hash_next field.
  71. */
  72. static struct mon_data *mon_free; /* free list or null if none */
  73. static int mon_total_mem; /* total structures allocated */
  74. static int mon_mem_increments; /* times called malloc() */
  75. /*
  76. * Initialization state. We may be monitoring, we may not. If
  77. * we aren't, we may not even have allocated any memory yet.
  78. */
  79. int mon_enabled; /* enable switch */
  80. u_long mon_age = 3000; /* preemption limit */
  81. static int mon_have_memory;
  82. static void mon_getmoremem P((void));
  83. static void remove_from_hash P((struct mon_data *));
  84. /*
  85. * init_mon - initialize monitoring global data
  86. */
  87. void
  88. init_mon(void)
  89. {
  90. /*
  91. * Don't do much of anything here. We don't allocate memory
  92. * until someone explicitly starts us.
  93. */
  94. mon_enabled = MON_OFF;
  95. mon_have_memory = 0;
  96. mon_total_mem = 0;
  97. mon_mem_increments = 0;
  98. mon_free = NULL;
  99. memset(&mon_hash[0], 0, sizeof mon_hash);
  100. memset(&mon_mru_list, 0, sizeof mon_mru_list);
  101. }
  102. /*
  103. * mon_start - start up the monitoring software
  104. */
  105. void
  106. mon_start(
  107. int mode
  108. )
  109. {
  110. if (mon_enabled != MON_OFF) {
  111. mon_enabled |= mode;
  112. return;
  113. }
  114. if (mode == MON_OFF)
  115. return;
  116. if (!mon_have_memory) {
  117. mon_total_mem = 0;
  118. mon_mem_increments = 0;
  119. mon_free = NULL;
  120. mon_getmoremem();
  121. mon_have_memory = 1;
  122. }
  123. mon_mru_list.mru_next = &mon_mru_list;
  124. mon_mru_list.mru_prev = &mon_mru_list;
  125. mon_enabled = mode;
  126. }
  127. /*
  128. * mon_stop - stop the monitoring software
  129. */
  130. void
  131. mon_stop(
  132. int mode
  133. )
  134. {
  135. register struct mon_data *md, *md_next;
  136. register int i;
  137. if (mon_enabled == MON_OFF)
  138. return;
  139. if ((mon_enabled & mode) == 0 || mode == MON_OFF)
  140. return;
  141. mon_enabled &= ~mode;
  142. if (mon_enabled != MON_OFF)
  143. return;
  144. /*
  145. * Put everything back on the free list
  146. */
  147. for (i = 0; i < MON_HASH_SIZE; i++) {
  148. md = mon_hash[i]; /* get next list */
  149. mon_hash[i] = NULL; /* zero the list head */
  150. while (md != NULL) {
  151. md_next = md->hash_next;
  152. md->hash_next = mon_free;
  153. mon_free = md;
  154. md = md_next;
  155. }
  156. }
  157. mon_mru_list.mru_next = &mon_mru_list;
  158. mon_mru_list.mru_prev = &mon_mru_list;
  159. }
  160. void
  161. ntp_monclearinterface(struct interface *interface)
  162. {
  163. struct mon_data *md;
  164. for (md = mon_mru_list.mru_next; md != &mon_mru_list;
  165. md = md->mru_next) {
  166. if (md->interface == interface)
  167. {
  168. /* dequeue from mru list and put to free list */
  169. md->mru_prev->mru_next = md->mru_next;
  170. md->mru_next->mru_prev = md->mru_prev;
  171. remove_from_hash(md);
  172. md->hash_next = mon_free;
  173. mon_free = md;
  174. }
  175. }
  176. }
  177. /*
  178. * ntp_monitor - record stats about this packet
  179. *
  180. * Returns 1 if the packet is at the head of the list, 0 otherwise.
  181. */
  182. int
  183. ntp_monitor(
  184. struct recvbuf *rbufp
  185. )
  186. {
  187. register struct pkt *pkt;
  188. register struct mon_data *md;
  189. struct sockaddr_storage addr;
  190. register int hash;
  191. register int mode;
  192. if (mon_enabled == MON_OFF)
  193. return 0;
  194. pkt = &rbufp->recv_pkt;
  195. memset(&addr, 0, sizeof(addr));
  196. memcpy(&addr, &(rbufp->recv_srcadr), sizeof(addr));
  197. hash = MON_HASH(&addr);
  198. mode = PKT_MODE(pkt->li_vn_mode);
  199. md = mon_hash[hash];
  200. while (md != NULL) {
  201. /*
  202. * Match address only to conserve MRU size.
  203. */
  204. if (SOCKCMP(&md->rmtadr, &addr)) {
  205. md->drop_count = current_time - md->lasttime;
  206. md->lasttime = current_time;
  207. md->count++;
  208. md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
  209. md->mode = (u_char) mode;
  210. md->version = PKT_VERSION(pkt->li_vn_mode);
  211. /*
  212. * Shuffle to the head of the MRU list.
  213. */
  214. md->mru_next->mru_prev = md->mru_prev;
  215. md->mru_prev->mru_next = md->mru_next;
  216. md->mru_next = mon_mru_list.mru_next;
  217. md->mru_prev = &mon_mru_list;
  218. mon_mru_list.mru_next->mru_prev = md;
  219. mon_mru_list.mru_next = md;
  220. return 1;
  221. }
  222. md = md->hash_next;
  223. }
  224. /*
  225. * If we got here, this is the first we've heard of this
  226. * guy. Get him some memory, either from the free list
  227. * or from the tail of the MRU list.
  228. */
  229. if (mon_free == NULL && mon_total_mem >= MAXMONMEM) {
  230. /*
  231. * Preempt from the MRU list if old enough.
  232. */
  233. md = mon_mru_list.mru_prev;
  234. /* We get 31 bits from ntp_random() */
  235. if (((u_long)ntp_random()) / FRAC >
  236. (double)(current_time - md->lasttime) / mon_age)
  237. return 0;
  238. md->mru_prev->mru_next = &mon_mru_list;
  239. mon_mru_list.mru_prev = md->mru_prev;
  240. remove_from_hash(md);
  241. } else {
  242. if (mon_free == NULL)
  243. mon_getmoremem();
  244. md = mon_free;
  245. mon_free = md->hash_next;
  246. }
  247. /*
  248. * Got one, initialize it
  249. */
  250. md->avg_interval = 0;
  251. md->lasttime = current_time;
  252. md->count = 1;
  253. md->drop_count = 0;
  254. memset(&md->rmtadr, 0, sizeof(md->rmtadr));
  255. memcpy(&md->rmtadr, &addr, sizeof(addr));
  256. md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
  257. md->mode = (u_char) mode;
  258. md->version = PKT_VERSION(pkt->li_vn_mode);
  259. md->interface = rbufp->dstadr;
  260. md->cast_flags = (u_char)(((rbufp->dstadr->flags & INT_MCASTOPEN) &&
  261. rbufp->fd == md->interface->fd) ? MDF_MCAST: rbufp->fd ==
  262. md->interface->bfd ? MDF_BCAST : MDF_UCAST);
  263. /*
  264. * Drop him into front of the hash table. Also put him on top of
  265. * the MRU list.
  266. */
  267. md->hash_next = mon_hash[hash];
  268. mon_hash[hash] = md;
  269. md->mru_next = mon_mru_list.mru_next;
  270. md->mru_prev = &mon_mru_list;
  271. mon_mru_list.mru_next->mru_prev = md;
  272. mon_mru_list.mru_next = md;
  273. return 1;
  274. }
  275. /*
  276. * mon_getmoremem - get more memory and put it on the free list
  277. */
  278. static void
  279. mon_getmoremem(void)
  280. {
  281. register struct mon_data *md;
  282. register int i;
  283. struct mon_data *freedata; /* 'old' free list (null) */
  284. md = (struct mon_data *)emalloc(MONMEMINC *
  285. sizeof(struct mon_data));
  286. freedata = mon_free;
  287. mon_free = md;
  288. for (i = 0; i < (MONMEMINC-1); i++) {
  289. md->hash_next = (md + 1);
  290. md++;
  291. }
  292. /*
  293. * md now points at the last. Link in the rest of the chain.
  294. */
  295. md->hash_next = freedata;
  296. mon_total_mem += MONMEMINC;
  297. mon_mem_increments++;
  298. }
  299. static void
  300. remove_from_hash(
  301. struct mon_data *md
  302. )
  303. {
  304. register int hash;
  305. register struct mon_data *md_prev;
  306. hash = MON_HASH(&md->rmtadr);
  307. if (mon_hash[hash] == md) {
  308. mon_hash[hash] = md->hash_next;
  309. } else {
  310. md_prev = mon_hash[hash];
  311. while (md_prev->hash_next != md) {
  312. md_prev = md_prev->hash_next;
  313. if (md_prev == NULL) {
  314. /* logic error */
  315. return;
  316. }
  317. }
  318. md_prev->hash_next = md->hash_next;
  319. }
  320. }