2 * ntp_monitor - monitor ntpd statistics
11 #include "ntp_stdlib.h"
12 #include <ntp_random.h>
16 #ifdef HAVE_SYS_IOCTL_H
17 # include <sys/ioctl.h>
21 * I'm still not sure I like what I've done here. It certainly consumes
22 * memory like it is going out of style, and also may not be as low
23 * overhead as I'd imagined.
25 * Anyway, we record statistics based on source address, mode and
26 * version (for now, anyway. Check the code). The receive procedure
27 * calls us with the incoming rbufp before it does anything else.
29 * Each entry is doubly linked into two lists, a hash table and a
30 * most-recently-used list. When a packet arrives it is looked up in
31 * the hash table. If found, the statistics are updated and the entry
32 * relinked at the head of the MRU list. If not found, a new entry is
33 * allocated, initialized and linked into both the hash table and at the
34 * head of the MRU list.
36 * Memory is usually allocated by grabbing a big chunk of new memory and
37 * cutting it up into littler pieces. The exception to this when we hit
38 * the memory limit. Then we free memory by grabbing entries off the
39 * tail for the MRU list, unlinking from the hash table, and
42 * trimmed back memory consumption ... jdg 8/94
45 * Limits on the number of structures allocated. This limit is picked
46 * with the illicit knowlege that we can only return somewhat less
47 * than 8K bytes in a mode 7 response packet, and that each structure
48 * will require about 20 bytes of space in the response.
50 * ... I don't believe the above is true anymore ... jdg
53 #define MAXMONMEM 600 /* we allocate up to 600 structures */
56 #define MONMEMINC 40 /* allocate them 40 at a time */
62 #define MON_HASH_SIZE 128
63 #define MON_HASH_MASK (MON_HASH_SIZE-1)
64 #define MON_HASH(addr) sock_hash(addr)
67 * Pointers to the hash table, the MRU list and the count table. Memory
68 * for the hash and count tables is only allocated if monitoring is
71 static struct mon_data *mon_hash[MON_HASH_SIZE]; /* list ptrs */
72 struct mon_data mon_mru_list;
75 * List of free structures structures, and counters of free and total
76 * structures. The free structures are linked with the hash_next field.
78 static struct mon_data *mon_free; /* free list or null if none */
79 static int mon_total_mem; /* total structures allocated */
80 static int mon_mem_increments; /* times called malloc() */
83 * Initialization state. We may be monitoring, we may not. If
84 * we aren't, we may not even have allocated any memory yet.
86 int mon_enabled; /* enable switch */
87 u_long mon_age = 3000; /* preemption limit */
88 static int mon_have_memory;
89 static void mon_getmoremem P((void));
90 static void remove_from_hash P((struct mon_data *));
93 * init_mon - initialize monitoring global data
99 * Don't do much of anything here. We don't allocate memory
100 * until someone explicitly starts us.
102 mon_enabled = MON_OFF;
106 mon_mem_increments = 0;
108 memset(&mon_hash[0], 0, sizeof mon_hash);
109 memset(&mon_mru_list, 0, sizeof mon_mru_list);
114 * mon_start - start up the monitoring software
122 if (mon_enabled != MON_OFF) {
129 if (!mon_have_memory) {
131 mon_mem_increments = 0;
137 mon_mru_list.mru_next = &mon_mru_list;
138 mon_mru_list.mru_prev = &mon_mru_list;
144 * mon_stop - stop the monitoring software
151 register struct mon_data *md, *md_next;
154 if (mon_enabled == MON_OFF)
156 if ((mon_enabled & mode) == 0 || mode == MON_OFF)
159 mon_enabled &= ~mode;
160 if (mon_enabled != MON_OFF)
164 * Put everything back on the free list
166 for (i = 0; i < MON_HASH_SIZE; i++) {
167 md = mon_hash[i]; /* get next list */
168 mon_hash[i] = NULL; /* zero the list head */
170 md_next = md->hash_next;
171 md->hash_next = mon_free;
177 mon_mru_list.mru_next = &mon_mru_list;
178 mon_mru_list.mru_prev = &mon_mru_list;
182 ntp_monclearinterface(struct interface *interface)
186 for (md = mon_mru_list.mru_next; md != &mon_mru_list;
188 if (md->interface == interface)
190 /* dequeue from mru list and put to free list */
191 md->mru_prev->mru_next = md->mru_next;
192 md->mru_next->mru_prev = md->mru_prev;
193 remove_from_hash(md);
194 md->hash_next = mon_free;
201 * ntp_monitor - record stats about this packet
203 * Returns 1 if the packet is at the head of the list, 0 otherwise.
207 struct recvbuf *rbufp
210 register struct pkt *pkt;
211 register struct mon_data *md;
212 struct sockaddr_storage addr;
216 if (mon_enabled == MON_OFF)
219 pkt = &rbufp->recv_pkt;
220 memset(&addr, 0, sizeof(addr));
221 memcpy(&addr, &(rbufp->recv_srcadr), sizeof(addr));
222 hash = MON_HASH(&addr);
223 mode = PKT_MODE(pkt->li_vn_mode);
228 * Match address only to conserve MRU size.
230 if (SOCKCMP(&md->rmtadr, &addr)) {
231 md->drop_count = current_time - md->lasttime;
232 md->lasttime = current_time;
234 md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
235 md->mode = (u_char) mode;
236 md->version = PKT_VERSION(pkt->li_vn_mode);
239 * Shuffle to the head of the MRU list.
241 md->mru_next->mru_prev = md->mru_prev;
242 md->mru_prev->mru_next = md->mru_next;
243 md->mru_next = mon_mru_list.mru_next;
244 md->mru_prev = &mon_mru_list;
245 mon_mru_list.mru_next->mru_prev = md;
246 mon_mru_list.mru_next = md;
253 * If we got here, this is the first we've heard of this
254 * guy. Get him some memory, either from the free list
255 * or from the tail of the MRU list.
257 if (mon_free == NULL && mon_total_mem >= MAXMONMEM) {
260 * Preempt from the MRU list if old enough.
262 md = mon_mru_list.mru_prev;
263 /* We get 31 bits from ntp_random() */
264 if (((u_long)ntp_random()) / FRAC >
265 (double)(current_time - md->lasttime) / mon_age)
268 md->mru_prev->mru_next = &mon_mru_list;
269 mon_mru_list.mru_prev = md->mru_prev;
270 remove_from_hash(md);
272 if (mon_free == NULL)
275 mon_free = md->hash_next;
279 * Got one, initialize it
281 md->avg_interval = 0;
282 md->lasttime = current_time;
285 memset(&md->rmtadr, 0, sizeof(md->rmtadr));
286 memcpy(&md->rmtadr, &addr, sizeof(addr));
287 md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
288 md->mode = (u_char) mode;
289 md->version = PKT_VERSION(pkt->li_vn_mode);
290 md->interface = rbufp->dstadr;
291 md->cast_flags = (u_char)(((rbufp->dstadr->flags & INT_MCASTOPEN) &&
292 rbufp->fd == md->interface->fd) ? MDF_MCAST: rbufp->fd ==
293 md->interface->bfd ? MDF_BCAST : MDF_UCAST);
296 * Drop him into front of the hash table. Also put him on top of
299 md->hash_next = mon_hash[hash];
301 md->mru_next = mon_mru_list.mru_next;
302 md->mru_prev = &mon_mru_list;
303 mon_mru_list.mru_next->mru_prev = md;
304 mon_mru_list.mru_next = md;
310 * mon_getmoremem - get more memory and put it on the free list
315 register struct mon_data *md;
317 struct mon_data *freedata; /* 'old' free list (null) */
319 md = (struct mon_data *)emalloc(MONMEMINC *
320 sizeof(struct mon_data));
323 for (i = 0; i < (MONMEMINC-1); i++) {
324 md->hash_next = (md + 1);
329 * md now points at the last. Link in the rest of the chain.
331 md->hash_next = freedata;
332 mon_total_mem += MONMEMINC;
333 mon_mem_increments++;
342 register struct mon_data *md_prev;
344 hash = MON_HASH(&md->rmtadr);
345 if (mon_hash[hash] == md) {
346 mon_hash[hash] = md->hash_next;
348 md_prev = mon_hash[hash];
349 while (md_prev->hash_next != md) {
350 md_prev = md_prev->hash_next;
351 if (md_prev == NULL) {
356 md_prev->hash_next = md->hash_next;