1c0b746e5SOllivier Robert /* 29c2daa00SOllivier Robert * ntp_monitor - monitor ntpd statistics 3c0b746e5SOllivier Robert */ 4c0b746e5SOllivier Robert #ifdef HAVE_CONFIG_H 5c0b746e5SOllivier Robert # include <config.h> 6c0b746e5SOllivier Robert #endif 7c0b746e5SOllivier Robert 8c0b746e5SOllivier Robert #include "ntpd.h" 9c0b746e5SOllivier Robert #include "ntp_io.h" 10c0b746e5SOllivier Robert #include "ntp_if.h" 112b15cb3dSCy Schubert #include "ntp_lists.h" 12c0b746e5SOllivier Robert #include "ntp_stdlib.h" 13ea906c41SOllivier Robert #include <ntp_random.h> 14c0b746e5SOllivier Robert 15224ba2bdSOllivier Robert #include <stdio.h> 16224ba2bdSOllivier Robert #include <signal.h> 17224ba2bdSOllivier Robert #ifdef HAVE_SYS_IOCTL_H 18224ba2bdSOllivier Robert # include <sys/ioctl.h> 19224ba2bdSOllivier Robert #endif 20224ba2bdSOllivier Robert 21c0b746e5SOllivier Robert /* 222b15cb3dSCy Schubert * Record statistics based on source address, mode and version. The 232b15cb3dSCy Schubert * receive procedure calls us with the incoming rbufp before it does 242b15cb3dSCy Schubert * anything else. While at it, implement rate controls for inbound 252b15cb3dSCy Schubert * traffic. 26c0b746e5SOllivier Robert * 272b15cb3dSCy Schubert * Each entry is doubly linked into two lists, a hash table and a most- 282b15cb3dSCy Schubert * recently-used (MRU) list. When a packet arrives it is looked up in 299c2daa00SOllivier Robert * the hash table. If found, the statistics are updated and the entry 309c2daa00SOllivier Robert * relinked at the head of the MRU list. If not found, a new entry is 319c2daa00SOllivier Robert * allocated, initialized and linked into both the hash table and at the 329c2daa00SOllivier Robert * head of the MRU list. 33c0b746e5SOllivier Robert * 349c2daa00SOllivier Robert * Memory is usually allocated by grabbing a big chunk of new memory and 359c2daa00SOllivier Robert * cutting it up into littler pieces. The exception to this when we hit 369c2daa00SOllivier Robert * the memory limit. Then we free memory by grabbing entries off the 379c2daa00SOllivier Robert * tail for the MRU list, unlinking from the hash table, and 38c0b746e5SOllivier Robert * reinitializing. 39c0b746e5SOllivier Robert * 402b15cb3dSCy Schubert * INC_MONLIST is the default allocation granularity in entries. 412b15cb3dSCy Schubert * INIT_MONLIST is the default initial allocation in entries. 42c0b746e5SOllivier Robert */ 432b15cb3dSCy Schubert #ifdef MONMEMINC /* old name */ 442b15cb3dSCy Schubert # define INC_MONLIST MONMEMINC 452b15cb3dSCy Schubert #elif !defined(INC_MONLIST) 462b15cb3dSCy Schubert # define INC_MONLIST (4 * 1024 / sizeof(mon_entry)) 47c0b746e5SOllivier Robert #endif 482b15cb3dSCy Schubert #ifndef INIT_MONLIST 492b15cb3dSCy Schubert # define INIT_MONLIST (4 * 1024 / sizeof(mon_entry)) 502b15cb3dSCy Schubert #endif 512b15cb3dSCy Schubert #ifndef MRU_MAXDEPTH_DEF 522b15cb3dSCy Schubert # define MRU_MAXDEPTH_DEF (1024 * 1024 / sizeof(mon_entry)) 53c0b746e5SOllivier Robert #endif 54c0b746e5SOllivier Robert 55c0b746e5SOllivier Robert /* 56c0b746e5SOllivier Robert * Hashing stuff 57c0b746e5SOllivier Robert */ 582b15cb3dSCy Schubert u_char mon_hash_bits; 59c0b746e5SOllivier Robert 60c0b746e5SOllivier Robert /* 612b15cb3dSCy Schubert * Pointers to the hash table and the MRU list. Memory for the hash 622b15cb3dSCy Schubert * table is allocated only if monitoring is enabled. 63c0b746e5SOllivier Robert */ 642b15cb3dSCy Schubert mon_entry ** mon_hash; /* MRU hash table */ 652b15cb3dSCy Schubert mon_entry mon_mru_list; /* mru listhead */ 669c2daa00SOllivier Robert 67c0b746e5SOllivier Robert /* 682b15cb3dSCy Schubert * List of free structures structures, and counters of in-use and total 69c0b746e5SOllivier Robert * structures. The free structures are linked with the hash_next field. 70c0b746e5SOllivier Robert */ 712b15cb3dSCy Schubert static mon_entry *mon_free; /* free list or null if none */ 722b15cb3dSCy Schubert u_int mru_alloc; /* mru list + free list count */ 732b15cb3dSCy Schubert u_int mru_entries; /* mru list count */ 742b15cb3dSCy Schubert u_int mru_peakentries; /* highest mru_entries seen */ 752b15cb3dSCy Schubert u_int mru_initalloc = INIT_MONLIST;/* entries to preallocate */ 762b15cb3dSCy Schubert u_int mru_incalloc = INC_MONLIST;/* allocation batch factor */ 772b15cb3dSCy Schubert static u_int mon_mem_increments; /* times called malloc() */ 782b15cb3dSCy Schubert 792b15cb3dSCy Schubert /* 802b15cb3dSCy Schubert * Parameters of the RES_LIMITED restriction option. We define headway 812b15cb3dSCy Schubert * as the idle time between packets. A packet is discarded if the 822b15cb3dSCy Schubert * headway is less than the minimum, as well as if the average headway 832b15cb3dSCy Schubert * is less than eight times the increment. 842b15cb3dSCy Schubert */ 852b15cb3dSCy Schubert int ntp_minpkt = NTP_MINPKT; /* minimum (log 2 s) */ 862b15cb3dSCy Schubert u_char ntp_minpoll = NTP_MINPOLL; /* increment (log 2 s) */ 87c0b746e5SOllivier Robert 88c0b746e5SOllivier Robert /* 89c0b746e5SOllivier Robert * Initialization state. We may be monitoring, we may not. If 90c0b746e5SOllivier Robert * we aren't, we may not even have allocated any memory yet. 91c0b746e5SOllivier Robert */ 922b15cb3dSCy Schubert u_int mon_enabled; /* enable switch */ 932b15cb3dSCy Schubert u_int mru_mindepth = 600; /* preempt above this */ 942b15cb3dSCy Schubert int mru_maxage = 64; /* for entries older than */ 952b15cb3dSCy Schubert u_int mru_maxdepth = /* MRU count hard limit */ 962b15cb3dSCy Schubert MRU_MAXDEPTH_DEF; 972b15cb3dSCy Schubert int mon_age = 3000; /* preemption limit */ 982b15cb3dSCy Schubert 992b15cb3dSCy Schubert static void mon_getmoremem(void); 1002b15cb3dSCy Schubert static void remove_from_hash(mon_entry *); 1012b15cb3dSCy Schubert static inline void mon_free_entry(mon_entry *); 1022b15cb3dSCy Schubert static inline void mon_reclaim_entry(mon_entry *); 1032b15cb3dSCy Schubert 104c0b746e5SOllivier Robert 105c0b746e5SOllivier Robert /* 106c0b746e5SOllivier Robert * init_mon - initialize monitoring global data 107c0b746e5SOllivier Robert */ 108c0b746e5SOllivier Robert void 109c0b746e5SOllivier Robert init_mon(void) 110c0b746e5SOllivier Robert { 111c0b746e5SOllivier Robert /* 112c0b746e5SOllivier Robert * Don't do much of anything here. We don't allocate memory 1132b15cb3dSCy Schubert * until mon_start(). 114c0b746e5SOllivier Robert */ 115c0b746e5SOllivier Robert mon_enabled = MON_OFF; 1162b15cb3dSCy Schubert INIT_DLIST(mon_mru_list, mru); 1172b15cb3dSCy Schubert } 118c0b746e5SOllivier Robert 1192b15cb3dSCy Schubert 1202b15cb3dSCy Schubert /* 1212b15cb3dSCy Schubert * remove_from_hash - removes an entry from the address hash table and 1222b15cb3dSCy Schubert * decrements mru_entries. 1232b15cb3dSCy Schubert */ 1242b15cb3dSCy Schubert static void 1252b15cb3dSCy Schubert remove_from_hash( 1262b15cb3dSCy Schubert mon_entry *mon 1272b15cb3dSCy Schubert ) 1282b15cb3dSCy Schubert { 1292b15cb3dSCy Schubert u_int hash; 1302b15cb3dSCy Schubert mon_entry *punlinked; 1312b15cb3dSCy Schubert 1322b15cb3dSCy Schubert mru_entries--; 1332b15cb3dSCy Schubert hash = MON_HASH(&mon->rmtadr); 1342b15cb3dSCy Schubert UNLINK_SLIST(punlinked, mon_hash[hash], mon, hash_next, 1352b15cb3dSCy Schubert mon_entry); 136*9034852cSGleb Smirnoff ENSURE(punlinked == mon); 1372b15cb3dSCy Schubert } 1382b15cb3dSCy Schubert 1392b15cb3dSCy Schubert 1402b15cb3dSCy Schubert static inline void 1412b15cb3dSCy Schubert mon_free_entry( 1422b15cb3dSCy Schubert mon_entry *m 1432b15cb3dSCy Schubert ) 1442b15cb3dSCy Schubert { 1452b15cb3dSCy Schubert ZERO(*m); 1462b15cb3dSCy Schubert LINK_SLIST(mon_free, m, hash_next); 1472b15cb3dSCy Schubert } 1482b15cb3dSCy Schubert 1492b15cb3dSCy Schubert 1502b15cb3dSCy Schubert /* 1512b15cb3dSCy Schubert * mon_reclaim_entry - Remove an entry from the MRU list and from the 1522b15cb3dSCy Schubert * hash array, then zero-initialize it. Indirectly 1532b15cb3dSCy Schubert * decrements mru_entries. 1542b15cb3dSCy Schubert 1552b15cb3dSCy Schubert * The entry is prepared to be reused. Before return, in 1562b15cb3dSCy Schubert * remove_from_hash(), mru_entries is decremented. It is the caller's 1572b15cb3dSCy Schubert * responsibility to increment it again. 1582b15cb3dSCy Schubert */ 1592b15cb3dSCy Schubert static inline void 1602b15cb3dSCy Schubert mon_reclaim_entry( 1612b15cb3dSCy Schubert mon_entry *m 1622b15cb3dSCy Schubert ) 1632b15cb3dSCy Schubert { 1642b15cb3dSCy Schubert DEBUG_INSIST(NULL != m); 1652b15cb3dSCy Schubert 1662b15cb3dSCy Schubert UNLINK_DLIST(m, mru); 1672b15cb3dSCy Schubert remove_from_hash(m); 1682b15cb3dSCy Schubert ZERO(*m); 1692b15cb3dSCy Schubert } 1702b15cb3dSCy Schubert 1712b15cb3dSCy Schubert 1722b15cb3dSCy Schubert /* 1732b15cb3dSCy Schubert * mon_getmoremem - get more memory and put it on the free list 1742b15cb3dSCy Schubert */ 1752b15cb3dSCy Schubert static void 1762b15cb3dSCy Schubert mon_getmoremem(void) 1772b15cb3dSCy Schubert { 1782b15cb3dSCy Schubert mon_entry *chunk; 1792b15cb3dSCy Schubert u_int entries; 1802b15cb3dSCy Schubert 1812b15cb3dSCy Schubert entries = (0 == mon_mem_increments) 1822b15cb3dSCy Schubert ? mru_initalloc 1832b15cb3dSCy Schubert : mru_incalloc; 1842b15cb3dSCy Schubert 1852b15cb3dSCy Schubert if (entries) { 186276da39aSCy Schubert chunk = eallocarray(entries, sizeof(*chunk)); 1872b15cb3dSCy Schubert mru_alloc += entries; 1882b15cb3dSCy Schubert for (chunk += entries; entries; entries--) 1892b15cb3dSCy Schubert mon_free_entry(--chunk); 1902b15cb3dSCy Schubert 1912b15cb3dSCy Schubert mon_mem_increments++; 1922b15cb3dSCy Schubert } 193c0b746e5SOllivier Robert } 194c0b746e5SOllivier Robert 195c0b746e5SOllivier Robert 196c0b746e5SOllivier Robert /* 197c0b746e5SOllivier Robert * mon_start - start up the monitoring software 198c0b746e5SOllivier Robert */ 199c0b746e5SOllivier Robert void 200c0b746e5SOllivier Robert mon_start( 201c0b746e5SOllivier Robert int mode 202c0b746e5SOllivier Robert ) 203c0b746e5SOllivier Robert { 2042b15cb3dSCy Schubert size_t octets; 2052b15cb3dSCy Schubert u_int min_hash_slots; 206c0b746e5SOllivier Robert 2072b15cb3dSCy Schubert if (MON_OFF == mode) /* MON_OFF is 0 */ 2082b15cb3dSCy Schubert return; 2092b15cb3dSCy Schubert if (mon_enabled) { 210c0b746e5SOllivier Robert mon_enabled |= mode; 211c0b746e5SOllivier Robert return; 212c0b746e5SOllivier Robert } 2132b15cb3dSCy Schubert if (0 == mon_mem_increments) 214c0b746e5SOllivier Robert mon_getmoremem(); 2152b15cb3dSCy Schubert /* 2162b15cb3dSCy Schubert * Select the MRU hash table size to limit the average count 2172b15cb3dSCy Schubert * per bucket at capacity (mru_maxdepth) to 8, if possible 2182b15cb3dSCy Schubert * given our hash is limited to 16 bits. 2192b15cb3dSCy Schubert */ 2202b15cb3dSCy Schubert min_hash_slots = (mru_maxdepth / 8) + 1; 2212b15cb3dSCy Schubert mon_hash_bits = 0; 2222b15cb3dSCy Schubert while (min_hash_slots >>= 1) 2232b15cb3dSCy Schubert mon_hash_bits++; 2242b15cb3dSCy Schubert mon_hash_bits = max(4, mon_hash_bits); 2252b15cb3dSCy Schubert mon_hash_bits = min(16, mon_hash_bits); 2262b15cb3dSCy Schubert octets = sizeof(*mon_hash) * MON_HASH_SIZE; 2272b15cb3dSCy Schubert mon_hash = erealloc_zero(mon_hash, octets, 0); 228c0b746e5SOllivier Robert 229c0b746e5SOllivier Robert mon_enabled = mode; 230c0b746e5SOllivier Robert } 231c0b746e5SOllivier Robert 232c0b746e5SOllivier Robert 233c0b746e5SOllivier Robert /* 234c0b746e5SOllivier Robert * mon_stop - stop the monitoring software 235c0b746e5SOllivier Robert */ 236c0b746e5SOllivier Robert void 237c0b746e5SOllivier Robert mon_stop( 238c0b746e5SOllivier Robert int mode 239c0b746e5SOllivier Robert ) 240c0b746e5SOllivier Robert { 2412b15cb3dSCy Schubert mon_entry *mon; 242c0b746e5SOllivier Robert 2432b15cb3dSCy Schubert if (MON_OFF == mon_enabled) 244c0b746e5SOllivier Robert return; 245c0b746e5SOllivier Robert if ((mon_enabled & mode) == 0 || mode == MON_OFF) 246c0b746e5SOllivier Robert return; 247c0b746e5SOllivier Robert 248c0b746e5SOllivier Robert mon_enabled &= ~mode; 249c0b746e5SOllivier Robert if (mon_enabled != MON_OFF) 250c0b746e5SOllivier Robert return; 251c0b746e5SOllivier Robert 252c0b746e5SOllivier Robert /* 2532b15cb3dSCy Schubert * Move everything on the MRU list to the free list quickly, 2542b15cb3dSCy Schubert * without bothering to remove each from either the MRU list or 2552b15cb3dSCy Schubert * the hash table. 256c0b746e5SOllivier Robert */ 2572b15cb3dSCy Schubert ITER_DLIST_BEGIN(mon_mru_list, mon, mru, mon_entry) 2582b15cb3dSCy Schubert mon_free_entry(mon); 2592b15cb3dSCy Schubert ITER_DLIST_END() 2602b15cb3dSCy Schubert 2612b15cb3dSCy Schubert /* empty the MRU list and hash table. */ 2622b15cb3dSCy Schubert mru_entries = 0; 2632b15cb3dSCy Schubert INIT_DLIST(mon_mru_list, mru); 2642b15cb3dSCy Schubert zero_mem(mon_hash, sizeof(*mon_hash) * MON_HASH_SIZE); 265c0b746e5SOllivier Robert } 266c0b746e5SOllivier Robert 267c0b746e5SOllivier Robert 2682b15cb3dSCy Schubert /* 2692b15cb3dSCy Schubert * mon_clearinterface -- remove mru entries referring to a local address 2702b15cb3dSCy Schubert * which is going away. 2712b15cb3dSCy Schubert */ 272ea906c41SOllivier Robert void 2732b15cb3dSCy Schubert mon_clearinterface( 2742b15cb3dSCy Schubert endpt *lcladr 2752b15cb3dSCy Schubert ) 276ea906c41SOllivier Robert { 2772b15cb3dSCy Schubert mon_entry *mon; 278ea906c41SOllivier Robert 2792b15cb3dSCy Schubert /* iterate mon over mon_mru_list */ 2802b15cb3dSCy Schubert ITER_DLIST_BEGIN(mon_mru_list, mon, mru, mon_entry) 2812b15cb3dSCy Schubert if (mon->lcladr == lcladr) { 2822b15cb3dSCy Schubert /* remove from mru list */ 2832b15cb3dSCy Schubert UNLINK_DLIST(mon, mru); 2842b15cb3dSCy Schubert /* remove from hash list, adjust mru_entries */ 2852b15cb3dSCy Schubert remove_from_hash(mon); 2862b15cb3dSCy Schubert /* put on free list */ 2872b15cb3dSCy Schubert mon_free_entry(mon); 288ea906c41SOllivier Robert } 2892b15cb3dSCy Schubert ITER_DLIST_END() 290ea906c41SOllivier Robert } 2912b15cb3dSCy Schubert 292c0b746e5SOllivier Robert 293c0b746e5SOllivier Robert /* 294c0b746e5SOllivier Robert * ntp_monitor - record stats about this packet 295ea906c41SOllivier Robert * 2962b15cb3dSCy Schubert * Returns supplied restriction flags, with RES_LIMITED and RES_KOD 2972b15cb3dSCy Schubert * cleared unless the packet should not be responded to normally 2982b15cb3dSCy Schubert * (RES_LIMITED) and possibly should trigger a KoD response (RES_KOD). 2992b15cb3dSCy Schubert * The returned flags are saved in the MRU entry, so that it reflects 3002b15cb3dSCy Schubert * whether the last packet from that source triggered rate limiting, 3012b15cb3dSCy Schubert * and if so, possible KoD response. This implies you can not tell 3022b15cb3dSCy Schubert * whether a given address is eligible for rate limiting/KoD from the 3032b15cb3dSCy Schubert * monlist restrict bits, only whether or not the last packet triggered 3042b15cb3dSCy Schubert * such responses. ntpdc -c reslist lets you see whether RES_LIMITED 3052b15cb3dSCy Schubert * or RES_KOD is lit for a particular address before ntp_monitor()'s 3062b15cb3dSCy Schubert * typical dousing. 307c0b746e5SOllivier Robert */ 3082b15cb3dSCy Schubert u_short 309c0b746e5SOllivier Robert ntp_monitor( 3102b15cb3dSCy Schubert struct recvbuf *rbufp, 3112b15cb3dSCy Schubert u_short flags 312c0b746e5SOllivier Robert ) 313c0b746e5SOllivier Robert { 3142b15cb3dSCy Schubert l_fp interval_fp; 3152b15cb3dSCy Schubert struct pkt * pkt; 3162b15cb3dSCy Schubert mon_entry * mon; 3172b15cb3dSCy Schubert mon_entry * oldest; 3182b15cb3dSCy Schubert int oldest_age; 3192b15cb3dSCy Schubert u_int hash; 3202b15cb3dSCy Schubert u_short restrict_mask; 3212b15cb3dSCy Schubert u_char mode; 3222b15cb3dSCy Schubert u_char version; 3232b15cb3dSCy Schubert int interval; 3242b15cb3dSCy Schubert int head; /* headway increment */ 3252b15cb3dSCy Schubert int leak; /* new headway */ 3262b15cb3dSCy Schubert int limit; /* average threshold */ 327c0b746e5SOllivier Robert 328*9034852cSGleb Smirnoff REQUIRE(rbufp != NULL); 329*9034852cSGleb Smirnoff 330c0b746e5SOllivier Robert if (mon_enabled == MON_OFF) 3312b15cb3dSCy Schubert return ~(RES_LIMITED | RES_KOD) & flags; 332c0b746e5SOllivier Robert 333c0b746e5SOllivier Robert pkt = &rbufp->recv_pkt; 3342b15cb3dSCy Schubert hash = MON_HASH(&rbufp->recv_srcadr); 335c0b746e5SOllivier Robert mode = PKT_MODE(pkt->li_vn_mode); 3362b15cb3dSCy Schubert version = PKT_VERSION(pkt->li_vn_mode); 3372b15cb3dSCy Schubert mon = mon_hash[hash]; 338c0b746e5SOllivier Robert 339c0b746e5SOllivier Robert /* 3402b15cb3dSCy Schubert * We keep track of all traffic for a given IP in one entry, 3412b15cb3dSCy Schubert * otherwise cron'ed ntpdate or similar evades RES_LIMITED. 3429c2daa00SOllivier Robert */ 3432b15cb3dSCy Schubert 3442b15cb3dSCy Schubert for (; mon != NULL; mon = mon->hash_next) 3452b15cb3dSCy Schubert if (SOCK_EQ(&mon->rmtadr, &rbufp->recv_srcadr)) 3462b15cb3dSCy Schubert break; 3472b15cb3dSCy Schubert 3482b15cb3dSCy Schubert if (mon != NULL) { 3492b15cb3dSCy Schubert interval_fp = rbufp->recv_time; 3502b15cb3dSCy Schubert L_SUB(&interval_fp, &mon->last); 3512b15cb3dSCy Schubert /* add one-half second to round up */ 3522b15cb3dSCy Schubert L_ADDUF(&interval_fp, 0x80000000); 3532b15cb3dSCy Schubert interval = interval_fp.l_i; 3542b15cb3dSCy Schubert mon->last = rbufp->recv_time; 3552b15cb3dSCy Schubert NSRCPORT(&mon->rmtadr) = NSRCPORT(&rbufp->recv_srcadr); 3562b15cb3dSCy Schubert mon->count++; 3572b15cb3dSCy Schubert restrict_mask = flags; 3582b15cb3dSCy Schubert mon->vn_mode = VN_MODE(version, mode); 3592b15cb3dSCy Schubert 3602b15cb3dSCy Schubert /* Shuffle to the head of the MRU list. */ 3612b15cb3dSCy Schubert UNLINK_DLIST(mon, mru); 3622b15cb3dSCy Schubert LINK_DLIST(mon_mru_list, mon, mru); 3639c2daa00SOllivier Robert 3649c2daa00SOllivier Robert /* 3652b15cb3dSCy Schubert * At this point the most recent arrival is first in the 3662b15cb3dSCy Schubert * MRU list. Decrease the counter by the headway, but 3672b15cb3dSCy Schubert * not less than zero. 368c0b746e5SOllivier Robert */ 3692b15cb3dSCy Schubert mon->leak -= interval; 3702b15cb3dSCy Schubert mon->leak = max(0, mon->leak); 3712b15cb3dSCy Schubert head = 1 << ntp_minpoll; 3722b15cb3dSCy Schubert leak = mon->leak + head; 3732b15cb3dSCy Schubert limit = NTP_SHIFT * head; 3742b15cb3dSCy Schubert 3752b15cb3dSCy Schubert DPRINTF(2, ("MRU: interval %d headway %d limit %d\n", 3762b15cb3dSCy Schubert interval, leak, limit)); 3772b15cb3dSCy Schubert 3782b15cb3dSCy Schubert /* 3792b15cb3dSCy Schubert * If the minimum and average thresholds are not 3802b15cb3dSCy Schubert * exceeded, douse the RES_LIMITED and RES_KOD bits and 3812b15cb3dSCy Schubert * increase the counter by the headway increment. Note 3822b15cb3dSCy Schubert * that we give a 1-s grace for the minimum threshold 3832b15cb3dSCy Schubert * and a 2-s grace for the headway increment. If one or 3842b15cb3dSCy Schubert * both thresholds are exceeded and the old counter is 3852b15cb3dSCy Schubert * less than the average threshold, set the counter to 3862b15cb3dSCy Schubert * the average threshold plus the increment and leave 3872b15cb3dSCy Schubert * the RES_LIMITED and RES_KOD bits lit. Otherwise, 3882b15cb3dSCy Schubert * leave the counter alone and douse the RES_KOD bit. 3892b15cb3dSCy Schubert * This rate-limits the KoDs to no less than the average 3902b15cb3dSCy Schubert * headway. 3912b15cb3dSCy Schubert */ 3922b15cb3dSCy Schubert if (interval + 1 >= ntp_minpkt && leak < limit) { 3932b15cb3dSCy Schubert mon->leak = leak - 2; 3942b15cb3dSCy Schubert restrict_mask &= ~(RES_LIMITED | RES_KOD); 3952b15cb3dSCy Schubert } else if (mon->leak < limit) 3962b15cb3dSCy Schubert mon->leak = limit + head; 3972b15cb3dSCy Schubert else 3982b15cb3dSCy Schubert restrict_mask &= ~RES_KOD; 3992b15cb3dSCy Schubert 4002b15cb3dSCy Schubert mon->flags = restrict_mask; 4012b15cb3dSCy Schubert 4022b15cb3dSCy Schubert return mon->flags; 403c0b746e5SOllivier Robert } 404c0b746e5SOllivier Robert 405c0b746e5SOllivier Robert /* 406c0b746e5SOllivier Robert * If we got here, this is the first we've heard of this 407c0b746e5SOllivier Robert * guy. Get him some memory, either from the free list 408c0b746e5SOllivier Robert * or from the tail of the MRU list. 4092b15cb3dSCy Schubert * 4102b15cb3dSCy Schubert * The following ntp.conf "mru" knobs come into play determining 4112b15cb3dSCy Schubert * the depth (or count) of the MRU list: 4122b15cb3dSCy Schubert * - mru_mindepth ("mru mindepth") is a floor beneath which 4132b15cb3dSCy Schubert * entries are kept without regard to their age. The 4142b15cb3dSCy Schubert * default is 600 which matches the longtime implementation 4152b15cb3dSCy Schubert * limit on the total number of entries. 4162b15cb3dSCy Schubert * - mru_maxage ("mru maxage") is a ceiling on the age in 4172b15cb3dSCy Schubert * seconds of entries. Entries older than this are 4182b15cb3dSCy Schubert * reclaimed once mon_mindepth is exceeded. 64s default. 4192b15cb3dSCy Schubert * Note that entries older than this can easily survive 4202b15cb3dSCy Schubert * as they are reclaimed only as needed. 4212b15cb3dSCy Schubert * - mru_maxdepth ("mru maxdepth") is a hard limit on the 4222b15cb3dSCy Schubert * number of entries. 4232b15cb3dSCy Schubert * - "mru maxmem" sets mru_maxdepth to the number of entries 4242b15cb3dSCy Schubert * which fit in the given number of kilobytes. The default is 4252b15cb3dSCy Schubert * 1024, or 1 megabyte. 4262b15cb3dSCy Schubert * - mru_initalloc ("mru initalloc" sets the count of the 4272b15cb3dSCy Schubert * initial allocation of MRU entries. 4282b15cb3dSCy Schubert * - "mru initmem" sets mru_initalloc in units of kilobytes. 4292b15cb3dSCy Schubert * The default is 4. 4302b15cb3dSCy Schubert * - mru_incalloc ("mru incalloc" sets the number of entries to 4312b15cb3dSCy Schubert * allocate on-demand each time the free list is empty. 4322b15cb3dSCy Schubert * - "mru incmem" sets mru_incalloc in units of kilobytes. 4332b15cb3dSCy Schubert * The default is 4. 4342b15cb3dSCy Schubert * Whichever of "mru maxmem" or "mru maxdepth" occurs last in 4352b15cb3dSCy Schubert * ntp.conf controls. Similarly for "mru initalloc" and "mru 4362b15cb3dSCy Schubert * initmem", and for "mru incalloc" and "mru incmem". 437c0b746e5SOllivier Robert */ 4382b15cb3dSCy Schubert if (mru_entries < mru_mindepth) { 4392b15cb3dSCy Schubert if (NULL == mon_free) 4409c2daa00SOllivier Robert mon_getmoremem(); 4412b15cb3dSCy Schubert UNLINK_HEAD_SLIST(mon, mon_free, hash_next); 4422b15cb3dSCy Schubert } else { 4432b15cb3dSCy Schubert oldest = TAIL_DLIST(mon_mru_list, mru); 4442b15cb3dSCy Schubert oldest_age = 0; /* silence uninit warning */ 4452b15cb3dSCy Schubert if (oldest != NULL) { 4462b15cb3dSCy Schubert interval_fp = rbufp->recv_time; 4472b15cb3dSCy Schubert L_SUB(&interval_fp, &oldest->last); 4482b15cb3dSCy Schubert /* add one-half second to round up */ 4492b15cb3dSCy Schubert L_ADDUF(&interval_fp, 0x80000000); 4502b15cb3dSCy Schubert oldest_age = interval_fp.l_i; 4512b15cb3dSCy Schubert } 4522b15cb3dSCy Schubert /* note -1 is legal for mru_maxage (disables) */ 4532b15cb3dSCy Schubert if (oldest != NULL && mru_maxage < oldest_age) { 4542b15cb3dSCy Schubert mon_reclaim_entry(oldest); 4552b15cb3dSCy Schubert mon = oldest; 4562b15cb3dSCy Schubert } else if (mon_free != NULL || mru_alloc < 4572b15cb3dSCy Schubert mru_maxdepth) { 4582b15cb3dSCy Schubert if (NULL == mon_free) 4592b15cb3dSCy Schubert mon_getmoremem(); 4602b15cb3dSCy Schubert UNLINK_HEAD_SLIST(mon, mon_free, hash_next); 4612b15cb3dSCy Schubert /* Preempt from the MRU list if old enough. */ 4622b15cb3dSCy Schubert } else if (ntp_random() / (2. * FRAC) > 4632b15cb3dSCy Schubert (double)oldest_age / mon_age) { 4642b15cb3dSCy Schubert return ~(RES_LIMITED | RES_KOD) & flags; 4652b15cb3dSCy Schubert } else { 4662b15cb3dSCy Schubert mon_reclaim_entry(oldest); 4672b15cb3dSCy Schubert mon = oldest; 4682b15cb3dSCy Schubert } 469c0b746e5SOllivier Robert } 470c0b746e5SOllivier Robert 471*9034852cSGleb Smirnoff INSIST(mon != NULL); 472*9034852cSGleb Smirnoff 473c0b746e5SOllivier Robert /* 474c0b746e5SOllivier Robert * Got one, initialize it 475c0b746e5SOllivier Robert */ 4762b15cb3dSCy Schubert mru_entries++; 4772b15cb3dSCy Schubert mru_peakentries = max(mru_peakentries, mru_entries); 4782b15cb3dSCy Schubert mon->last = rbufp->recv_time; 4792b15cb3dSCy Schubert mon->first = mon->last; 4802b15cb3dSCy Schubert mon->count = 1; 4812b15cb3dSCy Schubert mon->flags = ~(RES_LIMITED | RES_KOD) & flags; 4822b15cb3dSCy Schubert mon->leak = 0; 4832b15cb3dSCy Schubert memcpy(&mon->rmtadr, &rbufp->recv_srcadr, sizeof(mon->rmtadr)); 4842b15cb3dSCy Schubert mon->vn_mode = VN_MODE(version, mode); 4852b15cb3dSCy Schubert mon->lcladr = rbufp->dstadr; 4862b15cb3dSCy Schubert mon->cast_flags = (u_char)(((rbufp->dstadr->flags & 4872b15cb3dSCy Schubert INT_MCASTOPEN) && rbufp->fd == mon->lcladr->fd) ? MDF_MCAST 4882b15cb3dSCy Schubert : rbufp->fd == mon->lcladr->bfd ? MDF_BCAST : MDF_UCAST); 489c0b746e5SOllivier Robert 490c0b746e5SOllivier Robert /* 4919c2daa00SOllivier Robert * Drop him into front of the hash table. Also put him on top of 4929c2daa00SOllivier Robert * the MRU list. 493c0b746e5SOllivier Robert */ 4942b15cb3dSCy Schubert LINK_SLIST(mon_hash[hash], mon, hash_next); 4952b15cb3dSCy Schubert LINK_DLIST(mon_mru_list, mon, mru); 4962b15cb3dSCy Schubert 4972b15cb3dSCy Schubert return mon->flags; 498c0b746e5SOllivier Robert } 499c0b746e5SOllivier Robert 500c0b746e5SOllivier Robert 501