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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
|
#ifndef BOOST_TEST_DYN_LINK
#define BOOST_TEST_DYN_LINK
#endif
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>
#include "dnsdist.hh"
#include "dnsdist-lua.hh"
#include "dnsdist-lua-ffi.hh"
#include "dolog.hh"
uint16_t g_maxOutstanding{std::numeric_limits<uint16_t>::max()};
#include "ext/luawrapper/include/LuaContext.hpp"
LockGuarded<LuaContext> g_lua{LuaContext()};
bool g_snmpEnabled{false};
bool g_snmpTrapsEnabled{false};
DNSDistSNMPAgent* g_snmpAgent{nullptr};
#if BENCH_POLICIES
bool g_verbose{true};
#include "dnsdist-rings.hh"
Rings g_rings;
GlobalStateHolder<NetmaskTree<DynBlock>> g_dynblockNMG;
GlobalStateHolder<SuffixMatchTree<DynBlock>> g_dynblockSMT;
#endif /* BENCH_POLICIES */
GlobalStateHolder<pools_t> g_pools;
std::vector<std::unique_ptr<ClientState>> g_frontends;
/* add stub implementations, we don't want to include the corresponding object files
and their dependencies */
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
bool TLSFrontend::setupTLS()
{
return true;
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
std::string DNSQuestion::getTrailingData() const
{
return "";
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
bool DNSQuestion::setTrailingData(const std::string& tail)
{
return false;
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
bool DNSDistSNMPAgent::sendDNSTrap(const DNSQuestion& dnsQuestion, const std::string& reason)
{
return false;
}
void setLuaNoSideEffect()
{
}
DNSAction::Action SpoofAction::operator()(DNSQuestion* dnsQuestion, std::string* ruleresult) const
{
return DNSAction::Action::None;
}
bool setupDoTProtocolNegotiation(std::shared_ptr<TLSCtx>& tlsCtx)
{
(void)tlsCtx;
return true;
}
// NOLINTNEXTLINE(performance-unnecessary-value-param): this is a stub, the real one is not that simple and the performance does not matter
void responderThread(std::shared_ptr<DownstreamState> dss)
{
}
string g_outputBuffer;
std::atomic<bool> g_configurationDone{false};
static DNSQuestion getDQ(const DNSName* providedName = nullptr)
{
static const DNSName qname("powerdns.com.");
static PacketBuffer packet(sizeof(dnsheader));
static InternalQueryState ids;
ids.origDest = ComboAddress("127.0.0.1:53");
ids.origRemote = ComboAddress("192.0.2.1:42");
ids.qname = providedName != nullptr ? *providedName : qname;
ids.qtype = QType::A;
ids.qclass = QClass::IN;
ids.protocol = dnsdist::Protocol::DoUDP;
ids.queryRealTime.start();
DNSQuestion dnsQuestion(ids, packet);
return dnsQuestion;
}
static void benchPolicy(const ServerPolicy& pol)
{
#if BENCH_POLICIES
bool existingVerboseValue = g_verbose;
g_verbose = false;
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
ServerPolicy::NumberedServerVector servers;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
servers.at(idx - 1).second->setUp();
/* we need to have a weight of at least 1000 to get an optimal repartition with the consistent hashing algo */
servers.at(idx - 1).second->setWeight(1000);
/* make sure that the hashes have been computed */
servers.at(idx - 1).second->hash();
}
StopWatch sw;
sw.start();
for (size_t idx = 0; idx < 1000; idx++) {
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
}
}
cerr << pol.name << " took " << std::to_string(sw.udiff()) << " us for " << names.size() << endl;
g_verbose = existingVerboseValue;
#endif /* BENCH_POLICIES */
}
static void resetLuaContext()
{
/* we need to reset this before cleaning the Lua state because the server policy might holds
a reference to a Lua function (Lua policies) */
g_policy.setState(ServerPolicy("leastOutstanding", leastOutstanding, false));
*(g_lua.lock()) = LuaContext();
}
BOOST_AUTO_TEST_SUITE(dnsdistlbpolicies)
BOOST_AUTO_TEST_CASE(test_firstAvailable)
{
auto dnsQuestion = getDQ();
ServerPolicy pol{"firstAvailable", firstAvailable, false};
ServerPolicy::NumberedServerVector servers;
servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")));
/* servers start as 'down' */
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server == nullptr);
/* mark the server as 'up' */
servers.at(0).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server != nullptr);
/* add a second server, we should still get the first one */
servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")));
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(0).second);
/* mark the first server as 'down', second as 'up' */
servers.at(0).second->setDown();
servers.at(1).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(1).second);
benchPolicy(pol);
}
BOOST_AUTO_TEST_CASE(test_firstAvailableWithOrderAndQPS)
{
auto dnsQuestion = getDQ();
size_t qpsLimit = 10;
ServerPolicy pol{"firstAvailable", firstAvailable, false};
ServerPolicy::NumberedServerVector servers;
servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")));
servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")));
/* Second server has a higher order, so most queries should be routed to the first (remember that
we need to keep them ordered!).
However the first server has a QPS limit at 10 qps, so any query above that should be routed
to the second server. */
servers.at(0).second->d_config.order = 1;
servers.at(1).second->d_config.order = 2;
servers.at(0).second->qps = QPSLimiter(qpsLimit, qpsLimit);
/* mark the servers as 'up' */
servers.at(0).second->setUp();
servers.at(1).second->setUp();
/* the first queries under the QPS limit should be
sent to the first server */
for (size_t idx = 0; idx < qpsLimit; idx++) {
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(0).second);
server->incQueriesCount();
}
/* then to the second server */
for (size_t idx = 0; idx < 100; idx++) {
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(1).second);
server->incQueriesCount();
}
}
BOOST_AUTO_TEST_CASE(test_roundRobin)
{
auto dnsQuestion = getDQ();
ServerPolicy pol{"roundrobin", roundrobin, false};
ServerPolicy::NumberedServerVector servers;
/* selecting a server on an empty server list */
g_roundrobinFailOnNoServer = false;
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server == nullptr);
servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")));
/* servers start as 'down' but the RR policy returns a server unless g_roundrobinFailOnNoServer is set */
g_roundrobinFailOnNoServer = true;
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server == nullptr);
g_roundrobinFailOnNoServer = false;
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server != nullptr);
/* mark the server as 'up' */
servers.at(0).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server != nullptr);
/* add a second server, we should get the first one then the second one */
servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")));
servers.at(1).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(0).second);
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(1).second);
/* mark the first server as 'down', second as 'up' */
servers.at(0).second->setDown();
servers.at(1).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(1).second);
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
/* mark all servers 'up' */
for (auto& serv : servers) {
serv.second->setUp();
serversMap[serv.second] = 0;
}
for (size_t idx = 0; idx < 1000; idx++) {
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_EQUAL(entry.second, 1000 / servers.size());
total += entry.second;
}
BOOST_CHECK_EQUAL(total, 1000U);
benchPolicy(pol);
}
BOOST_AUTO_TEST_CASE(test_leastOutstanding)
{
auto dnsQuestion = getDQ();
ServerPolicy pol{"leastOutstanding", leastOutstanding, false};
ServerPolicy::NumberedServerVector servers;
servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")));
/* servers start as 'down' */
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server == nullptr);
/* mark the server as 'up' */
servers.at(0).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_CHECK(server != nullptr);
/* add a second server, we should still get the first one */
servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")));
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(0).second);
/* mark the first server as 'down', second as 'up' */
servers.at(0).second->setDown();
servers.at(1).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(1).second);
/* mark both servers as 'up', increase the outstanding count of the first one */
servers.at(0).second->setUp();
servers.at(0).second->outstanding = 42;
servers.at(1).second->setUp();
server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server != nullptr);
BOOST_CHECK(server == servers.at(1).second);
benchPolicy(pol);
}
BOOST_AUTO_TEST_CASE(test_wrandom)
{
auto dnsQuestion = getDQ();
ServerPolicy pol{"wrandom", wrandom, false};
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
}
benchPolicy(pol);
for (size_t idx = 0; idx < 1000; idx++) {
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (1000 / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (1000 / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, 1000U);
/* reset */
for (auto& entry : serversMap) {
entry.second = 0;
BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1);
}
/* reset */
for (auto& entry : serversMap) {
entry.second = 0;
BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1);
}
/* change the weight of the last server to 100, default is 1 */
servers.at(servers.size() - 1).second->d_config.d_weight = 100;
for (size_t idx = 0; idx < 1000; idx++) {
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
total = 0;
uint64_t totalW = 0;
for (const auto& entry : serversMap) {
total += entry.second;
totalW += entry.first->d_config.d_weight;
}
BOOST_CHECK_EQUAL(total, 1000U);
auto last = servers.at(servers.size() - 1).second;
const auto got = serversMap[last];
float expected = static_cast<float>(1000 * 1.0 * last->d_config.d_weight) / static_cast<float>(totalW);
BOOST_CHECK_GT(got, expected / 2);
BOOST_CHECK_LT(got, expected * 2);
}
BOOST_AUTO_TEST_CASE(test_whashed)
{
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
ServerPolicy pol{"whashed", whashed, false};
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
}
benchPolicy(pol);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
/* reset */
for (auto& entry : serversMap) {
entry.second = 0;
BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1);
}
/* request 1000 times the same name, we should go to the same server every time */
{
auto dnsQuestion = getDQ(&names.at(0));
auto server = pol.getSelectedBackend(servers, dnsQuestion);
for (size_t idx = 0; idx < 1000; idx++) {
BOOST_CHECK(pol.getSelectedBackend(servers, dnsQuestion) == server);
}
}
/* reset */
for (auto& entry : serversMap) {
entry.second = 0;
BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1);
}
/* change the weight of the last server to 100, default is 1 */
servers.at(servers.size() - 1).second->setWeight(100);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
total = 0;
uint64_t totalW = 0;
for (const auto& entry : serversMap) {
total += entry.second;
totalW += entry.first->d_config.d_weight;
}
BOOST_CHECK_EQUAL(total, names.size());
auto last = servers.at(servers.size() - 1).second;
const auto got = serversMap[last];
float expected = static_cast<float>(static_cast<double>(names.size()) * 1.0 * last->d_config.d_weight) / static_cast<float>(totalW);
BOOST_CHECK_GT(got, expected / 2);
BOOST_CHECK_LT(got, expected * 2);
}
BOOST_AUTO_TEST_CASE(test_chashed)
{
bool existingVerboseValue = g_verbose;
g_verbose = false;
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
ServerPolicy pol{"chashed", chashed, false};
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
/* we need to have a weight of at least 1000 to get an optimal repartition with the consistent hashing algo */
servers.at(idx - 1).second->setWeight(1000);
/* make sure that the hashes have been computed */
servers.at(idx - 1).second->hash();
}
benchPolicy(pol);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
/* reset */
for (auto& entry : serversMap) {
entry.second = 0;
BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1000);
}
/* request 1000 times the same name, we should go to the same server every time */
{
auto dnsQuestion = getDQ(&names.at(0));
auto server = pol.getSelectedBackend(servers, dnsQuestion);
for (size_t idx = 0; idx < 1000; idx++) {
BOOST_CHECK(pol.getSelectedBackend(servers, dnsQuestion) == server);
}
}
/* reset */
for (auto& entry : serversMap) {
entry.second = 0;
BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1000);
}
/* change the weight of the last server to 100000, others stay at 1000 */
servers.at(servers.size() - 1).second->setWeight(100000);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
total = 0;
uint64_t totalW = 0;
for (const auto& entry : serversMap) {
total += entry.second;
totalW += entry.first->d_config.d_weight;
}
BOOST_CHECK_EQUAL(total, names.size());
auto last = servers.at(servers.size() - 1).second;
const auto got = serversMap[last];
float expected = static_cast<float>(static_cast<double>(names.size()) * 1.0 * last->d_config.d_weight) / static_cast<float>(totalW);
BOOST_CHECK_GT(got, expected / 2);
BOOST_CHECK_LT(got, expected * 2);
g_verbose = existingVerboseValue;
}
BOOST_AUTO_TEST_CASE(test_lua)
{
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
static const std::string policySetupStr = R"foo(
local counter = 0
function luaroundrobin(servers, dq)
counter = counter + 1
return servers[1 + (counter % #servers)]
end
setServerPolicyLua("luaroundrobin", luaroundrobin)
)foo";
resetLuaContext();
g_lua.lock()->writeFunction("setServerPolicyLua", [](const string& name, const ServerPolicy::policyfunc_t& policy) {
g_policy.setState(ServerPolicy{name, policy, true});
});
g_lua.lock()->executeCode(policySetupStr);
{
ServerPolicy pol = g_policy.getCopy();
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
}
BOOST_REQUIRE_EQUAL(servers.size(), 10U);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
benchPolicy(pol);
}
resetLuaContext();
}
#ifdef LUAJIT_VERSION
BOOST_AUTO_TEST_CASE(test_lua_ffi_rr)
{
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
static const std::string policySetupStr = R"foo(
local ffi = require("ffi")
local C = ffi.C
local counter = 0
function ffilb(servers_list, dq)
local serversCount = tonumber(C.dnsdist_ffi_servers_list_get_count(servers_list))
counter = counter + 1
return counter % serversCount
end
setServerPolicyLuaFFI("FFI round-robin", ffilb)
)foo";
resetLuaContext();
g_lua.lock()->executeCode(getLuaFFIWrappers());
g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) {
g_policy.setState(ServerPolicy(name, policy));
});
g_lua.lock()->executeCode(policySetupStr);
{
ServerPolicy pol = g_policy.getCopy();
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
}
BOOST_REQUIRE_EQUAL(servers.size(), 10U);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
benchPolicy(pol);
}
resetLuaContext();
}
BOOST_AUTO_TEST_CASE(test_lua_ffi_no_server_available)
{
DNSName dnsName("powerdns.com.");
static const std::string policySetupStr = R"foo(
local ffi = require("ffi")
local C = ffi.C
local counter = 0
function ffipolicy(servers_list, dq)
local serversCount = tonumber(C.dnsdist_ffi_servers_list_get_count(servers_list))
-- return clearly out of bounds value to indicate that no server can be used
return serversCount + 100
end
setServerPolicyLuaFFI("FFI policy", ffipolicy)
)foo";
resetLuaContext();
g_lua.lock()->executeCode(getLuaFFIWrappers());
g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& policyName, ServerPolicy::ffipolicyfunc_t policy) {
g_policy.setState(ServerPolicy(policyName, std::move(policy)));
});
g_lua.lock()->executeCode(policySetupStr);
{
ServerPolicy pol = g_policy.getCopy();
ServerPolicy::NumberedServerVector servers;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
servers.at(idx - 1).second->setUp();
}
BOOST_REQUIRE_EQUAL(servers.size(), 10U);
auto dnsQuestion = getDQ(&dnsName);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(server == nullptr);
}
resetLuaContext();
}
BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed)
{
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
static const std::string policySetupStr = R"foo(
local ffi = require("ffi")
local C = ffi.C
function ffilb(servers_list, dq)
local serversCount = tonumber(C.dnsdist_ffi_servers_list_get_count(servers_list))
local hash = tonumber(C.dnsdist_ffi_dnsquestion_get_qname_hash(dq, 0))
return hash % serversCount
end
setServerPolicyLuaFFI("FFI hashed", ffilb)
)foo";
resetLuaContext();
g_lua.lock()->executeCode(getLuaFFIWrappers());
g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) {
g_policy.setState(ServerPolicy(name, policy));
});
g_lua.lock()->executeCode(policySetupStr);
{
ServerPolicy pol = g_policy.getCopy();
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
}
BOOST_REQUIRE_EQUAL(servers.size(), 10U);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
benchPolicy(pol);
}
resetLuaContext();
}
BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed)
{
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
static const std::string policySetupStr = R"foo(
local ffi = require("ffi")
local C = ffi.C
function ffilb(servers_list, dq)
return tonumber(C.dnsdist_ffi_servers_list_whashed(servers_list, dq, C.dnsdist_ffi_dnsquestion_get_qname_hash(dq, 0)))
end
setServerPolicyLuaFFI("FFI whashed", ffilb)
)foo";
resetLuaContext();
g_lua.lock()->executeCode(getLuaFFIWrappers());
g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) {
g_policy.setState(ServerPolicy(name, policy));
});
g_lua.lock()->executeCode(policySetupStr);
{
ServerPolicy pol = g_policy.getCopy();
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
}
BOOST_REQUIRE_EQUAL(servers.size(), 10U);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
benchPolicy(pol);
}
resetLuaContext();
}
BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed)
{
bool existingVerboseValue = g_verbose;
g_verbose = false;
std::vector<DNSName> names;
names.reserve(1000);
for (size_t idx = 0; idx < 1000; idx++) {
names.emplace_back("powerdns-" + std::to_string(idx) + ".com.");
}
static const std::string policySetupStr = R"foo(
local ffi = require("ffi")
local C = ffi.C
function ffilb(servers_list, dq)
return tonumber(C.dnsdist_ffi_servers_list_chashed(servers_list, dq, C.dnsdist_ffi_dnsquestion_get_qname_hash(dq, 0)))
end
setServerPolicyLuaFFI("FFI chashed", ffilb)
)foo";
resetLuaContext();
g_lua.lock()->executeCode(getLuaFFIWrappers());
g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) {
g_policy.setState(ServerPolicy(name, policy));
});
g_lua.lock()->executeCode(policySetupStr);
{
ServerPolicy pol = g_policy.getCopy();
ServerPolicy::NumberedServerVector servers;
std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap;
for (size_t idx = 1; idx <= 10; idx++) {
servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")));
serversMap[servers.at(idx - 1).second] = 0;
servers.at(idx - 1).second->setUp();
/* we need to have a weight of at least 1000 to get an optimal repartition with the consistent hashing algo */
servers.at(idx - 1).second->setWeight(1000);
/* make sure that the hashes have been computed */
servers.at(idx - 1).second->hash();
}
BOOST_REQUIRE_EQUAL(servers.size(), 10U);
for (const auto& name : names) {
auto dnsQuestion = getDQ(&name);
auto server = pol.getSelectedBackend(servers, dnsQuestion);
BOOST_REQUIRE(serversMap.count(server) == 1);
++serversMap[server];
}
uint64_t total = 0;
for (const auto& entry : serversMap) {
BOOST_CHECK_GT(entry.second, 0U);
BOOST_CHECK_GT(entry.second, (names.size() / servers.size() / 2));
BOOST_CHECK_LT(entry.second, (names.size() / servers.size() * 2));
total += entry.second;
}
BOOST_CHECK_EQUAL(total, names.size());
benchPolicy(pol);
}
g_verbose = existingVerboseValue;
resetLuaContext();
}
#endif /* LUAJIT_VERSION */
BOOST_AUTO_TEST_SUITE_END()
|