2 * ntp_monitor - monitor ntpd statistics
11 #include "ntp_stdlib.h"
15 #ifdef HAVE_SYS_IOCTL_H
16 # include <sys/ioctl.h>
20 * I'm still not sure I like what I've done here. It certainly consumes
21 * memory like it is going out of style, and also may not be as low
22 * overhead as I'd imagined.
24 * Anyway, we record statistics based on source address, mode and
25 * version (for now, anyway. Check the code). The receive procedure
26 * calls us with the incoming rbufp before it does anything else.
28 * Each entry is doubly linked into two lists, a hash table and a
29 * most-recently-used list. When a packet arrives it is looked up in
30 * the hash table. If found, the statistics are updated and the entry
31 * relinked at the head of the MRU list. If not found, a new entry is
32 * allocated, initialized and linked into both the hash table and at the
33 * head of the MRU list.
35 * Memory is usually allocated by grabbing a big chunk of new memory and
36 * cutting it up into littler pieces. The exception to this when we hit
37 * the memory limit. Then we free memory by grabbing entries off the
38 * tail for the MRU list, unlinking from the hash table, and
41 * trimmed back memory consumption ... jdg 8/94
44 * Limits on the number of structures allocated. This limit is picked
45 * with the illicit knowlege that we can only return somewhat less
46 * than 8K bytes in a mode 7 response packet, and that each structure
47 * will require about 20 bytes of space in the response.
49 * ... I don't believe the above is true anymore ... jdg
52 #define MAXMONMEM 600 /* we allocate up to 600 structures */
55 #define MONMEMINC 40 /* allocate them 40 at a time */
61 #define MON_HASH_SIZE 128
62 #define MON_HASH_MASK (MON_HASH_SIZE-1)
63 #define MON_HASH(addr) sock_hash(addr)
66 * Pointers to the hash table, the MRU list and the count table. Memory
67 * for the hash and count tables is only allocated if monitoring is
70 static struct mon_data *mon_hash[MON_HASH_SIZE]; /* list ptrs */
71 struct mon_data mon_mru_list;
74 * List of free structures structures, and counters of free and total
75 * structures. The free structures are linked with the hash_next field.
77 static struct mon_data *mon_free; /* free list or null if none */
78 static int mon_total_mem; /* total structures allocated */
79 static int mon_mem_increments; /* times called malloc() */
82 * Initialization state. We may be monitoring, we may not. If
83 * we aren't, we may not even have allocated any memory yet.
85 int mon_enabled; /* enable switch */
86 u_long mon_age = 3000; /* preemption limit */
87 static int mon_have_memory;
88 static void mon_getmoremem P((void));
89 static void remove_from_hash P((struct mon_data *));
92 * init_mon - initialize monitoring global data
98 * Don't do much of anything here. We don't allocate memory
99 * until someone explicitly starts us.
101 mon_enabled = MON_OFF;
105 mon_mem_increments = 0;
107 memset(&mon_hash[0], 0, sizeof mon_hash);
108 memset(&mon_mru_list, 0, sizeof mon_mru_list);
113 * mon_start - start up the monitoring software
121 if (mon_enabled != MON_OFF) {
128 if (!mon_have_memory) {
130 mon_mem_increments = 0;
136 mon_mru_list.mru_next = &mon_mru_list;
137 mon_mru_list.mru_prev = &mon_mru_list;
143 * mon_stop - stop the monitoring software
150 register struct mon_data *md, *md_next;
153 if (mon_enabled == MON_OFF)
155 if ((mon_enabled & mode) == 0 || mode == MON_OFF)
158 mon_enabled &= ~mode;
159 if (mon_enabled != MON_OFF)
163 * Put everything back on the free list
165 for (i = 0; i < MON_HASH_SIZE; i++) {
166 md = mon_hash[i]; /* get next list */
167 mon_hash[i] = NULL; /* zero the list head */
169 md_next = md->hash_next;
170 md->hash_next = mon_free;
176 mon_mru_list.mru_next = &mon_mru_list;
177 mon_mru_list.mru_prev = &mon_mru_list;
182 * ntp_monitor - record stats about this packet
186 struct recvbuf *rbufp
189 register struct pkt *pkt;
190 register struct mon_data *md;
191 struct sockaddr_storage addr;
195 if (mon_enabled == MON_OFF)
198 pkt = &rbufp->recv_pkt;
199 memset(&addr, 0, sizeof(addr));
200 memcpy(&addr, &(rbufp->recv_srcadr), sizeof(addr));
201 hash = MON_HASH(&addr);
202 mode = PKT_MODE(pkt->li_vn_mode);
207 * Match address only to conserve MRU size.
209 if (SOCKCMP(&md->rmtadr, &addr)) {
210 md->drop_count = current_time - md->lasttime;
211 md->lasttime = current_time;
213 md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
214 md->mode = (u_char) mode;
215 md->version = PKT_VERSION(pkt->li_vn_mode);
218 * Shuffle to the head of the MRU list.
220 md->mru_next->mru_prev = md->mru_prev;
221 md->mru_prev->mru_next = md->mru_next;
222 md->mru_next = mon_mru_list.mru_next;
223 md->mru_prev = &mon_mru_list;
224 mon_mru_list.mru_next->mru_prev = md;
225 mon_mru_list.mru_next = md;
232 * If we got here, this is the first we've heard of this
233 * guy. Get him some memory, either from the free list
234 * or from the tail of the MRU list.
236 if (mon_free == NULL && mon_total_mem >= MAXMONMEM) {
239 * Preempt from the MRU list if old enough.
241 md = mon_mru_list.mru_prev;
242 if (((u_long)RANDOM & 0xffffffff) / FRAC >
243 (double)(current_time - md->lasttime) / mon_age)
246 md->mru_prev->mru_next = &mon_mru_list;
247 mon_mru_list.mru_prev = md->mru_prev;
248 remove_from_hash(md);
250 if (mon_free == NULL)
253 mon_free = md->hash_next;
257 * Got one, initialize it
259 md->avg_interval = 0;
260 md->lasttime = current_time;
263 memset(&md->rmtadr, 0, sizeof(md->rmtadr));
264 memcpy(&md->rmtadr, &addr, sizeof(addr));
265 md->rmtport = NSRCPORT(&rbufp->recv_srcadr);
266 md->mode = (u_char) mode;
267 md->version = PKT_VERSION(pkt->li_vn_mode);
268 md->interface = rbufp->dstadr;
269 md->cast_flags = (u_char)(((rbufp->dstadr->flags & INT_MULTICAST) &&
270 rbufp->fd == md->interface->fd) ? MDF_MCAST: rbufp->fd ==
271 md->interface->bfd ? MDF_BCAST : MDF_UCAST);
274 * Drop him into front of the hash table. Also put him on top of
277 md->hash_next = mon_hash[hash];
279 md->mru_next = mon_mru_list.mru_next;
280 md->mru_prev = &mon_mru_list;
281 mon_mru_list.mru_next->mru_prev = md;
282 mon_mru_list.mru_next = md;
287 * mon_getmoremem - get more memory and put it on the free list
292 register struct mon_data *md;
294 struct mon_data *freedata; /* 'old' free list (null) */
296 md = (struct mon_data *)emalloc(MONMEMINC *
297 sizeof(struct mon_data));
300 for (i = 0; i < (MONMEMINC-1); i++) {
301 md->hash_next = (md + 1);
306 * md now points at the last. Link in the rest of the chain.
308 md->hash_next = freedata;
309 mon_total_mem += MONMEMINC;
310 mon_mem_increments++;
319 register struct mon_data *md_prev;
321 hash = MON_HASH(&md->rmtadr);
322 if (mon_hash[hash] == md) {
323 mon_hash[hash] = md->hash_next;
325 md_prev = mon_hash[hash];
326 while (md_prev->hash_next != md) {
327 md_prev = md_prev->hash_next;
328 if (md_prev == NULL) {
333 md_prev->hash_next = md->hash_next;