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
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "dnsdist.hh"
#include "dnsdist-xsk.hh"
#ifdef HAVE_XSK
#include <sys/poll.h>
#include "dolog.hh"
#include "dnsdist-metrics.hh"
#include "dnsdist-proxy-protocol.hh"
#include "threadname.hh"
#include "xsk.hh"
namespace dnsdist::xsk
{
std::vector<std::shared_ptr<XskSocket>> g_xsk;
void XskResponderThread(std::shared_ptr<DownstreamState> dss, std::shared_ptr<XskWorker> xskInfo)
{
try {
setThreadName("dnsdist/XskResp");
auto localRespRuleActions = g_respruleactions.getLocal();
auto localCacheInsertedRespRuleActions = g_cacheInsertedRespRuleActions.getLocal();
auto pollfds = getPollFdsForWorker(*xskInfo);
while (!dss->isStopped()) {
poll(pollfds.data(), pollfds.size(), -1);
bool needNotify = false;
if ((pollfds[0].revents & POLLIN) != 0) {
needNotify = true;
xskInfo->cleanSocketNotification();
#if defined(__SANITIZE_THREAD__)
xskInfo->incomingPacketsQueue.lock()->consume_all([&](XskPacket& packet) {
#else
xskInfo->incomingPacketsQueue.consume_all([&](XskPacket& packet) {
#endif
if (packet.getDataLen() < sizeof(dnsheader)) {
xskInfo->markAsFree(packet);
return;
}
const dnsheader_aligned dnsHeader(packet.getPayloadData());
const auto queryId = dnsHeader->id;
auto ids = dss->getState(queryId);
if (ids) {
if (!ids->isXSK()) {
dss->restoreState(queryId, std::move(*ids));
ids = std::nullopt;
}
}
if (!ids) {
xskInfo->markAsFree(packet);
return;
}
auto response = packet.clonePacketBuffer();
if (response.size() > packet.getCapacity()) {
/* fallback to sending the packet via normal socket */
ids->xskPacketHeader.clear();
}
if (!processResponderPacket(dss, response, *localRespRuleActions, *localCacheInsertedRespRuleActions, std::move(*ids))) {
xskInfo->markAsFree(packet);
infolog("XSK packet pushed to queue because processResponderPacket failed");
return;
}
if (response.size() > packet.getCapacity()) {
/* fallback to sending the packet via normal socket */
sendUDPResponse(ids->cs->udpFD, response, ids->delayMsec, ids->hopLocal, ids->hopRemote);
infolog("XSK packet falling back because packet is too large");
xskInfo->markAsFree(packet);
return;
}
packet.setHeader(ids->xskPacketHeader);
if (!packet.setPayload(response)) {
infolog("Unable to set XSK payload !");
}
if (ids->delayMsec > 0) {
packet.addDelay(ids->delayMsec);
}
packet.updatePacket();
xskInfo->pushToSendQueue(packet);
});
}
if (needNotify) {
xskInfo->notifyXskSocket();
}
}
}
catch (const std::exception& e) {
errlog("XSK responder thread died because of exception: %s", e.what());
}
catch (const PDNSException& e) {
errlog("XSK responder thread died because of PowerDNS exception: %s", e.reason);
}
catch (...) {
errlog("XSK responder thread died because of an exception: %s", "unknown");
}
}
bool XskIsQueryAcceptable(const XskPacket& packet, ClientState& clientState, LocalHolders& holders, bool& expectProxyProtocol)
{
const auto& from = packet.getFromAddr();
expectProxyProtocol = expectProxyProtocolFrom(from);
if (!holders.acl->match(from) && !expectProxyProtocol) {
vinfolog("Query from %s dropped because of ACL", from.toStringWithPort());
++dnsdist::metrics::g_stats.aclDrops;
return false;
}
clientState.queries++;
++dnsdist::metrics::g_stats.queries;
return true;
}
void XskRouter(std::shared_ptr<XskSocket> xsk)
{
setThreadName("dnsdist/XskRouter");
uint32_t failed = 0;
// packets to be submitted for sending
vector<XskPacket> fillInTx;
const auto& fds = xsk->getDescriptors();
// list of workers that need to be notified
std::set<int> needNotify;
while (true) {
try {
auto ready = xsk->wait(-1);
// descriptor 0 gets incoming AF_XDP packets
if ((fds.at(0).revents & POLLIN) != 0) {
auto packets = xsk->recv(64, &failed);
dnsdist::metrics::g_stats.nonCompliantQueries += failed;
for (auto& packet : packets) {
const auto dest = packet.getToAddr();
auto worker = xsk->getWorkerByDestination(dest);
if (!worker) {
xsk->markAsFree(packet);
continue;
}
worker->pushToProcessingQueue(packet);
needNotify.insert(worker->workerWaker.getHandle());
}
for (auto socket : needNotify) {
uint64_t value = 1;
auto written = write(socket, &value, sizeof(value));
if (written != sizeof(value)) {
// oh, well, the worker is clearly overloaded
// but there is nothing we can do about it,
// and hopefully the queue will be processed eventually
}
}
needNotify.clear();
ready--;
}
for (size_t fdIndex = 1; fdIndex < fds.size() && ready > 0; fdIndex++) {
if ((fds.at(fdIndex).revents & POLLIN) != 0) {
ready--;
const auto& info = xsk->getWorkerByDescriptor(fds.at(fdIndex).fd);
#if defined(__SANITIZE_THREAD__)
info->outgoingPacketsQueue.lock()->consume_all([&](XskPacket& packet) {
#else
info->outgoingPacketsQueue.consume_all([&](XskPacket& packet) {
#endif
if ((packet.getFlags() & XskPacket::UPDATE) == 0) {
xsk->markAsFree(packet);
return;
}
if ((packet.getFlags() & XskPacket::DELAY) != 0) {
xsk->pushDelayed(packet);
return;
}
fillInTx.push_back(packet);
});
info->cleanWorkerNotification();
}
}
xsk->pickUpReadyPacket(fillInTx);
xsk->recycle(4096);
xsk->fillFq();
xsk->send(fillInTx);
}
catch (...) {
vinfolog("Exception in XSK router loop");
}
}
}
void XskClientThread(ClientState* clientState)
{
setThreadName("dnsdist/xskClient");
auto xskInfo = clientState->xskInfo;
LocalHolders holders;
for (;;) {
#if defined(__SANITIZE_THREAD__)
while (xskInfo->incomingPacketsQueue.lock()->read_available() == 0U) {
#else
while (xskInfo->incomingPacketsQueue.read_available() == 0U) {
#endif
xskInfo->waitForXskSocket();
}
#if defined(__SANITIZE_THREAD__)
xskInfo->incomingPacketsQueue.lock()->consume_all([&](XskPacket& packet) {
#else
xskInfo->incomingPacketsQueue.consume_all([&](XskPacket& packet) {
#endif
if (XskProcessQuery(*clientState, holders, packet)) {
packet.updatePacket();
xskInfo->pushToSendQueue(packet);
}
else {
xskInfo->markAsFree(packet);
}
});
xskInfo->notifyXskSocket();
}
}
static std::string getDestinationMap(bool isV6)
{
return !isV6 ? "/sys/fs/bpf/dnsdist/xsk-destinations-v4" : "/sys/fs/bpf/dnsdist/xsk-destinations-v6";
}
void addDestinationAddress(const ComboAddress& addr)
{
auto map = getDestinationMap(addr.isIPv6());
XskSocket::addDestinationAddress(map, addr);
}
void removeDestinationAddress(const ComboAddress& addr)
{
auto map = getDestinationMap(addr.isIPv6());
XskSocket::removeDestinationAddress(map, addr);
}
void clearDestinationAddresses()
{
auto map = getDestinationMap(false);
XskSocket::clearDestinationMap(map, false);
map = getDestinationMap(true);
XskSocket::clearDestinationMap(map, true);
}
}
#endif /* HAVE_XSK */
|