-
Notifications
You must be signed in to change notification settings - Fork 779
/
P_HostDBProcessor.h
559 lines (475 loc) · 15.9 KB
/
P_HostDBProcessor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/****************************************************************************
P_HostDBProcessor.h
****************************************************************************/
#ifndef _P_HostDBProcessor_h_
#define _P_HostDBProcessor_h_
#include "I_HostDBProcessor.h"
//
// Data
//
extern int hostdb_enable;
extern int hostdb_migrate_on_demand;
extern int hostdb_cluster;
extern int hostdb_cluster_round_robin;
extern int hostdb_lookup_timeout;
extern int hostdb_insert_timeout;
extern int hostdb_re_dns_on_reload;
// 0 = obey, 1 = ignore, 2 = min(X,ttl), 3 = max(X,ttl)
enum {
TTL_OBEY,
TTL_IGNORE,
TTL_MIN,
TTL_MAX,
};
extern int hostdb_ttl_mode;
extern int hostdb_srv_enabled;
// extern int hostdb_timestamp;
extern int hostdb_sync_frequency;
extern int hostdb_disable_reverse_lookup;
// Static configuration information
extern HostDBCache hostDB;
/** Host DB record mark.
The records in the host DB are de facto segregated by roughly the
DNS query type. We use an intermediate type to provide a little flexibility
although the type is presumed to be a single byte.
*/
enum HostDBMark {
HOSTDB_MARK_GENERIC, ///< Anything that's not one of the other types.
HOSTDB_MARK_IPV4, ///< IPv4 / T_A
HOSTDB_MARK_IPV6, ///< IPv6 / T_AAAA
HOSTDB_MARK_SRV, ///< Service / T_SRV
};
/** Convert a HostDB @a mark to a string.
@return A static string.
*/
extern char const *string_for(HostDBMark mark);
inline unsigned int
HOSTDB_CLIENT_IP_HASH(sockaddr const *lhs, sockaddr const *rhs)
{
unsigned int zret = ~static_cast<unsigned int>(0);
if (ats_ip_are_compatible(lhs, rhs)) {
if (ats_is_ip4(lhs)) {
in_addr_t ip1 = ats_ip4_addr_cast(lhs);
in_addr_t ip2 = ats_ip4_addr_cast(rhs);
zret = (ip1 >> 16) ^ ip1 ^ ip2 ^ (ip2 >> 16);
} else if (ats_is_ip6(lhs)) {
uint32_t const *ip1 = ats_ip_addr32_cast(lhs);
uint32_t const *ip2 = ats_ip_addr32_cast(rhs);
for (int i = 0; i < 4; ++i, ++ip1, ++ip2) {
zret ^= (*ip1 >> 16) ^ *ip1 ^ *ip2 ^ (*ip2 >> 16);
}
}
}
return zret & 0xFFFF;
}
//
// Constants
//
#define HOST_DB_HITS_BITS 3
#define HOST_DB_TAG_BITS 56
#define CONFIGURATION_HISTORY_PROBE_DEPTH 1
// Bump this any time hostdb format is changed
#define HOST_DB_CACHE_MAJOR_VERSION 3
#define HOST_DB_CACHE_MINOR_VERSION 0
// 2.2: IP family split 2.1 : IPv6
#define DEFAULT_HOST_DB_FILENAME "host.db"
#define DEFAULT_HOST_DB_SIZE (1 << 14)
// Timeout DNS every 24 hours by default if ttl_mode is enabled
#define HOST_DB_IP_TIMEOUT (24 * 60 * 60)
// DNS entries should be revalidated every 12 hours
#define HOST_DB_IP_STALE (12 * 60 * 60)
// DNS entries which failed lookup, should be revalidated every hour
#define HOST_DB_IP_FAIL_TIMEOUT (60 * 60)
//#define HOST_DB_MAX_INTERVAL (0x7FFFFFFF)
#define HOST_DB_MAX_TTL (0x1FFFFF) // 24 days
//
// Constants
//
// period to wait for a remote probe...
#define HOST_DB_CLUSTER_TIMEOUT HRTIME_MSECONDS(5000)
#define HOST_DB_RETRY_PERIOD HRTIME_MSECONDS(20)
#define HOST_DB_ITERATE_PERIOD HRTIME_MSECONDS(5)
//#define TEST(_x) _x
#define TEST(_x)
struct ClusterMachine;
struct HostEnt;
struct ClusterConfiguration;
// Stats
enum HostDB_Stats {
hostdb_total_lookups_stat,
hostdb_total_hits_stat, // D == total hits
hostdb_ttl_stat, // D average TTL
hostdb_ttl_expires_stat, // D == TTL Expires
hostdb_re_dns_on_reload_stat,
HostDB_Stat_Count
};
struct RecRawStatBlock;
extern RecRawStatBlock *hostdb_rsb;
// Stat Macros
#define HOSTDB_DEBUG_COUNT_DYN_STAT(_x, _y) RecIncrRawStatCount(hostdb_rsb, mutex->thread_holding, (int)_x, _y)
#define HOSTDB_INCREMENT_DYN_STAT(_x) RecIncrRawStatSum(hostdb_rsb, mutex->thread_holding, (int)_x, 1)
#define HOSTDB_DECREMENT_DYN_STAT(_x) RecIncrRawStatSum(hostdb_rsb, mutex->thread_holding, (int)_x, -1)
#define HOSTDB_SUM_DYN_STAT(_x, _r) RecIncrRawStatSum(hostdb_rsb, mutex->thread_holding, (int)_x, _r)
#define HOSTDB_READ_DYN_STAT(_x, _count, _sum) \
do { \
RecGetRawStatSum(hostdb_rsb, (int)_x, &_sum); \
RecGetRawStatCount(hostdb_rsb, (int)_x, &_count); \
} while (0)
#define HOSTDB_SET_DYN_COUNT(_x, _count) RecSetRawStatCount(hostdb_rsb, _x, _count);
#define HOSTDB_INCREMENT_THREAD_DYN_STAT(_s, _t) RecIncrRawStatSum(hostdb_rsb, _t, (int)_s, 1);
#define HOSTDB_DECREMENT_THREAD_DYN_STAT(_s, _t) RecIncrRawStatSum(hostdb_rsb, _t, (int)_s, -1);
struct CmpConstBuffferCaseInsensitive {
bool
operator()(ts::ConstBuffer a, ts::ConstBuffer b) const
{
return ptr_len_casecmp(a._ptr, a._size, b._ptr, b._size) < 0;
}
};
// Our own typedef for the host file mapping
typedef std::map<ts::ConstBuffer, IpAddr, CmpConstBuffferCaseInsensitive> HostsFileMap;
// A to hold a ref-counted map
struct RefCountedHostsFileMap : public RefCountObj {
HostsFileMap hosts_file_map;
ats_scoped_str HostFileText;
ink_time_t next_sync_time; // time of the next sync
};
//
// HostDBCache (Private)
//
struct HostDBCache {
int start(int flags = 0);
// Map to contain all of the host file overrides, initialize it to empty
Ptr<RefCountedHostsFileMap> hosts_file_ptr;
// TODO: make ATS call a close() method or something on shutdown (it does nothing of the sort today)
RefCountCache<HostDBInfo> *refcountcache;
// TODO configurable number of items in the cache
Queue<HostDBContinuation, Continuation::Link_link> *pending_dns;
Queue<HostDBContinuation, Continuation::Link_link> &pending_dns_for_hash(INK_MD5 &md5);
Queue<HostDBContinuation, Continuation::Link_link> *remoteHostDBQueue;
HostDBCache();
};
inline int
HostDBRoundRobin::index_of(sockaddr const *ip)
{
bool bad = (rrcount <= 0 || rrcount > HOST_DB_MAX_ROUND_ROBIN_INFO || good <= 0 || good > HOST_DB_MAX_ROUND_ROBIN_INFO);
if (bad) {
ink_assert(!"bad round robin size");
return -1;
}
for (int i = 0; i < good; i++) {
if (ats_ip_addr_eq(ip, info(i).ip())) {
return i;
}
}
return -1;
}
inline HostDBInfo *
HostDBRoundRobin::find_ip(sockaddr const *ip)
{
int idx = this->index_of(ip);
return idx < 0 ? NULL : &info(idx);
}
inline HostDBInfo *
HostDBRoundRobin::select_next(sockaddr const *ip)
{
HostDBInfo *zret = 0;
if (good > 1) {
int idx = this->index_of(ip);
if (idx >= 0) {
idx = (idx + 1) % good;
zret = &info(idx);
}
}
return zret;
}
inline HostDBInfo *
HostDBRoundRobin::find_target(const char *target)
{
bool bad = (rrcount <= 0 || rrcount > HOST_DB_MAX_ROUND_ROBIN_INFO || good <= 0 || good > HOST_DB_MAX_ROUND_ROBIN_INFO);
if (bad) {
ink_assert(!"bad round robin size");
return NULL;
}
uint32_t key = makeHostHash(target);
for (int i = 0; i < good; i++) {
if (info(i).data.srv.key == key && !strcmp(target, info(i).srvname(this)))
return &info(i);
}
return NULL;
}
inline HostDBInfo *
HostDBRoundRobin::select_best_http(sockaddr const *client_ip, ink_time_t now, int32_t fail_window)
{
bool bad = (rrcount <= 0 || rrcount > HOST_DB_MAX_ROUND_ROBIN_INFO || good <= 0 || good > HOST_DB_MAX_ROUND_ROBIN_INFO);
if (bad) {
ink_assert(!"bad round robin size");
return NULL;
}
int best_any = 0;
int best_up = -1;
// Basic round robin, increment current and mod with how many we have
if (HostDBProcessor::hostdb_strict_round_robin) {
Debug("hostdb", "Using strict round robin");
// Check that the host we selected is alive
for (int i = 0; i < good; i++) {
best_any = current++ % good;
if (info(best_any).is_alive(now, fail_window)) {
best_up = best_any;
break;
}
}
} else if (HostDBProcessor::hostdb_timed_round_robin > 0) {
Debug("hostdb", "Using timed round-robin for HTTP");
if ((now - timed_rr_ctime) > HostDBProcessor::hostdb_timed_round_robin) {
Debug("hostdb", "Timed interval expired.. rotating");
++current;
timed_rr_ctime = now;
}
for (int i = 0; i < good; i++) {
best_any = current++ % good;
if (info(best_any).is_alive(now, fail_window)) {
best_up = best_any;
break;
}
}
Debug("hostdb", "Using %d for best_up", best_up);
} else {
Debug("hostdb", "Using default round robin");
unsigned int best_hash_any = 0;
unsigned int best_hash_up = 0;
sockaddr const *ip;
for (int i = 0; i < good; i++) {
ip = info(i).ip();
unsigned int h = HOSTDB_CLIENT_IP_HASH(client_ip, ip);
if (best_hash_any <= h) {
best_any = i;
best_hash_any = h;
}
if (info(i).is_alive(now, fail_window)) {
if (best_hash_up <= h) {
best_up = i;
best_hash_up = h;
}
}
}
}
if (best_up != -1) {
ink_assert(best_up >= 0 && best_up < good);
return &info(best_up);
} else {
ink_assert(best_any >= 0 && best_any < good);
return &info(best_any);
}
}
inline HostDBInfo *
HostDBRoundRobin::select_best_srv(char *target, InkRand *rand, ink_time_t now, int32_t fail_window)
{
bool bad = (rrcount <= 0 || rrcount > HOST_DB_MAX_ROUND_ROBIN_INFO || good <= 0 || good > HOST_DB_MAX_ROUND_ROBIN_INFO);
if (bad) {
ink_assert(!"bad round robin size");
return NULL;
}
#ifdef DEBUG
for (int i = 1; i < good; ++i) {
ink_assert(info(i).data.srv.srv_priority >= info(i - 1).data.srv.srv_priority);
}
#endif
int i = 0, len = 0;
uint32_t weight = 0, p = INT32_MAX;
HostDBInfo *result = NULL;
HostDBInfo *infos[HOST_DB_MAX_ROUND_ROBIN_INFO];
do {
// if the real isn't alive-- exclude it from selection
if (!info(i).is_alive(now, fail_window)) {
continue;
}
if (info(i).data.srv.srv_priority <= p) {
p = info(i).data.srv.srv_priority;
weight += info(i).data.srv.srv_weight;
infos[len++] = &info(i);
} else
break;
} while (++i < good);
if (len == 0) { // all failed
result = &info(current++ % good);
} else if (weight == 0) { // srv weight is 0
result = &info(current++ % len);
} else {
uint32_t xx = rand->random() % weight;
for (i = 0; i < len && xx >= infos[i]->data.srv.srv_weight; ++i)
xx -= infos[i]->data.srv.srv_weight;
result = infos[i];
}
if (result) {
strcpy(target, result->srvname(this));
return result;
}
return NULL;
}
//
// Types
//
/** Container for an MD5 hash and its dependent data.
This handles both the host name and raw address cases.
*/
struct HostDBMD5 {
typedef HostDBMD5 self; ///< Self reference type.
INK_MD5 hash; ///< The hash value.
char const *host_name; ///< Host name.
int host_len; ///< Length of @a _host_name
IpAddr ip; ///< IP address.
in_port_t port; ///< IP port (host order).
/// DNS server. Not strictly part of the MD5 data but
/// it's both used by @c HostDBContinuation and provides access to
/// MD5 data. It's just handier to store it here for both uses.
DNSServer *dns_server;
SplitDNS *pSD; ///< Hold the container for @a dns_server.
HostDBMark db_mark; ///< Mark / type of record.
/// Default constructor.
HostDBMD5();
/// Destructor.
~HostDBMD5();
/// Recompute and update the MD5 hash.
void refresh();
/** Assign a hostname.
This updates the split DNS data as well.
*/
self &set_host(char const *name, int len);
};
//
// Handles a HostDB lookup request
//
struct HostDBContinuation;
typedef int (HostDBContinuation::*HostDBContHandler)(int, void *);
struct HostDBContinuation : public Continuation {
Action action;
HostDBMD5 md5;
// IpEndpoint ip;
unsigned int ttl;
// HostDBMark db_mark; ///< Target type.
/// Original IP address family style. Note this will disagree with
/// @a md5.db_mark when doing a retry on an alternate family. The retry
/// logic depends on it to avoid looping.
HostResStyle host_res_style; ///< Address family priority.
int dns_lookup_timeout;
// INK_MD5 md5;
Event *timeout;
ClusterMachine *from;
Continuation *from_cont;
HostDBApplicationInfo app;
int probe_depth;
size_t current_iterate_pos;
ClusterMachine *past_probes[CONFIGURATION_HISTORY_PROBE_DEPTH];
// char name[MAXDNAME];
// int namelen;
char md5_host_name_store[MAXDNAME + 1]; // used as backing store for @a md5
char srv_target_name[MAXDNAME];
// void *m_pDS;
Action *pending_action;
unsigned int missing : 1;
unsigned int force_dns : 1;
unsigned int round_robin : 1;
int probeEvent(int event, Event *e);
int iterateEvent(int event, Event *e);
int clusterEvent(int event, Event *e);
int clusterResponseEvent(int event, Event *e);
int dnsEvent(int event, HostEnt *e);
int dnsPendingEvent(int event, Event *e);
int backgroundEvent(int event, Event *e);
int retryEvent(int event, Event *e);
int removeEvent(int event, Event *e);
int setbyEvent(int event, Event *e);
/// Recompute the MD5 and update ancillary values.
void refresh_MD5();
void do_dns();
bool
is_byname()
{
return md5.db_mark == HOSTDB_MARK_IPV4 || md5.db_mark == HOSTDB_MARK_IPV6;
}
bool
is_srv()
{
return md5.db_mark == HOSTDB_MARK_SRV;
}
HostDBInfo *lookup_done(IpAddr const &ip, char const *aname, bool round_robin, unsigned int attl, SRVHosts *s = NULL,
HostDBInfo *r = NULL);
bool do_get_response(Event *e);
void do_put_response(ClusterMachine *m, HostDBInfo *r, Continuation *cont);
int failed_cluster_request(Event *e);
int key_partition();
void remove_trigger_pending_dns();
int set_check_pending_dns();
ClusterMachine *master_machine(ClusterConfiguration *cc);
HostDBInfo *insert(unsigned int attl);
/** Optional values for @c init.
*/
struct Options {
typedef Options self; ///< Self reference type.
int timeout; ///< Timeout value. Default 0
HostResStyle host_res_style; ///< IP address family fallback. Default @c HOST_RES_NONE
bool force_dns; ///< Force DNS lookup. Default @c false
Continuation *cont; ///< Continuation / action. Default @c NULL (none)
Options() : timeout(0), host_res_style(HOST_RES_NONE), force_dns(false), cont(0) {}
};
static const Options DEFAULT_OPTIONS; ///< Default defaults.
void init(HostDBMD5 const &md5, Options const &opt = DEFAULT_OPTIONS);
int make_get_message(char *buf, int len);
int make_put_message(HostDBInfo *r, Continuation *c, char *buf, int len);
HostDBContinuation()
: Continuation(NULL),
ttl(0),
host_res_style(DEFAULT_OPTIONS.host_res_style),
dns_lookup_timeout(DEFAULT_OPTIONS.timeout),
timeout(0),
from(0),
from_cont(0),
probe_depth(0),
current_iterate_pos(0),
missing(false),
force_dns(DEFAULT_OPTIONS.force_dns),
round_robin(false)
{
ink_zero(md5_host_name_store);
ink_zero(md5.hash);
SET_HANDLER((HostDBContHandler)&HostDBContinuation::probeEvent);
}
};
inline unsigned int
master_hash(INK_MD5 const &md5)
{
return static_cast<int>(md5[1] >> 32);
}
inline bool
is_dotted_form_hostname(const char *c)
{
return -1 != (int)ink_inet_addr(c);
}
inline Queue<HostDBContinuation> &
HostDBCache::pending_dns_for_hash(INK_MD5 &md5)
{
return pending_dns[this->refcountcache->partition_for_key(md5.fold())];
}
inline int
HostDBContinuation::key_partition()
{
return hostDB.refcountcache->partition_for_key(md5.hash.fold());
}
#endif /* _P_HostDBProcessor_h_ */