]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-lbpolicies.hh
Merge pull request #9073 from pieterlexis/runtime-dirs-virtual-hosting
[thirdparty/pdns.git] / pdns / dnsdist-lbpolicies.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23
24 struct dnsdist_ffi_servers_list_t;
25 struct dnsdist_ffi_server_t;
26 struct dnsdist_ffi_dnsquestion_t;
27
28 struct DownstreamState;
29
30 struct ServerPolicy
31 {
32 template <class T> using NumberedVector = std::vector<std::pair<unsigned int, T> >;
33 using NumberedServerVector = NumberedVector<shared_ptr<DownstreamState>>;
34 typedef std::function<shared_ptr<DownstreamState>(const NumberedServerVector& servers, const DNSQuestion*)> policyfunc_t;
35 typedef std::function<unsigned int(dnsdist_ffi_servers_list_t* servers, dnsdist_ffi_dnsquestion_t* dq)> ffipolicyfunc_t;
36
37 ServerPolicy(const std::string& name_, policyfunc_t policy_, bool isLua_): name(name_), policy(policy_), isLua(isLua_)
38 {
39 }
40 ServerPolicy(const std::string& name_, ffipolicyfunc_t policy_): name(name_), ffipolicy(policy_), isLua(true), isFFI(true)
41 {
42 }
43 ServerPolicy()
44 {
45 }
46
47 string name;
48 policyfunc_t policy;
49 ffipolicyfunc_t ffipolicy;
50 bool isLua{false};
51 bool isFFI{false};
52
53 std::string toString() const {
54 return string("ServerPolicy") + (isLua ? " (Lua)" : "") + " \"" + name + "\"";
55 }
56 };
57
58 struct ServerPool;
59
60 using pools_t=map<std::string,std::shared_ptr<ServerPool>>;
61 std::shared_ptr<ServerPool> getPool(const pools_t& pools, const std::string& poolName);
62 std::shared_ptr<ServerPool> createPoolIfNotExists(pools_t& pools, const string& poolName);
63 void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr<ServerPolicy> policy);
64 void addServerToPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server);
65 void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server);
66
67 ServerPolicy::NumberedServerVector getDownstreamCandidates(const map<std::string,std::shared_ptr<ServerPool>>& pools, const std::string& poolName);
68
69 std::shared_ptr<DownstreamState> firstAvailable(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
70
71 std::shared_ptr<DownstreamState> leastOutstanding(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
72 std::shared_ptr<DownstreamState> wrandom(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
73 std::shared_ptr<DownstreamState> whashed(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
74 std::shared_ptr<DownstreamState> whashedFromHash(const ServerPolicy::NumberedServerVector& servers, size_t hash);
75 std::shared_ptr<DownstreamState> chashed(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
76 std::shared_ptr<DownstreamState> chashedFromHash(const ServerPolicy::NumberedServerVector& servers, size_t hash);
77 std::shared_ptr<DownstreamState> roundrobin(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
78 std::shared_ptr<DownstreamState> getSelectedBackendFromPolicy(const ServerPolicy& policy, const ServerPolicy::NumberedServerVector& servers, DNSQuestion& dq);
79
80 extern double g_consistentHashBalancingFactor;
81 extern double g_weightedBalancingFactor;
82 extern uint32_t g_hashperturb;
83 extern bool g_roundrobinFailOnNoServer;