]>
Commit | Line | Data |
---|---|---|
062e2281 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
e25c139f | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
062e2281 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 44 Peer Selection Algorithm */ |
10 | ||
582c2af2 FC |
11 | #include "squid.h" |
12 | #include "acl/FilledChecklist.h" | |
9865de72 | 13 | #include "base/AsyncCbdataCalls.h" |
6043e368 | 14 | #include "base/InstanceId.h" |
8b082ed9 | 15 | #include "base/TypeTraits.h" |
a011edee | 16 | #include "CachePeer.h" |
21c22f04 | 17 | #include "carp.h" |
582c2af2 | 18 | #include "client_side.h" |
4a3b98d7 | 19 | #include "dns/LookupDetails.h" |
a37fdd8a | 20 | #include "errorpage.h" |
a553a5a3 | 21 | #include "event.h" |
eb13c21e | 22 | #include "FwdState.h" |
af69c635 | 23 | #include "globals.h" |
bbaf2685 | 24 | #include "hier_code.h" |
924f73bc | 25 | #include "htcp.h" |
d3dddfb5 | 26 | #include "http/Stream.h" |
582c2af2 | 27 | #include "HttpRequest.h" |
9b5c4a9a | 28 | #include "icmp/net_db.h" |
582c2af2 | 29 | #include "ICP.h" |
c7a52e6d | 30 | #include "ip/tools.h" |
602d9612 | 31 | #include "ipcache.h" |
f0ba2534 | 32 | #include "neighbors.h" |
f795b373 | 33 | #include "peer_sourcehash.h" |
37236ba1 | 34 | #include "peer_userhash.h" |
582c2af2 | 35 | #include "PeerSelectState.h" |
4d5904f7 | 36 | #include "SquidConfig.h" |
582c2af2 FC |
37 | #include "SquidTime.h" |
38 | #include "Store.h" | |
9865de72 | 39 | #include "util.h" // for tvSubDsec() which should be in SquidTime.h |
062e2281 | 40 | |
6043e368 AR |
41 | /** |
42 | * A CachePeer which has been selected as a possible destination. | |
43 | * Listed as pointers here so as to prevent duplicates being added but will | |
44 | * be converted to a set of IP address path options before handing back out | |
45 | * to the caller. | |
46 | * | |
47 | * Certain connection flags and outgoing settings will also be looked up and | |
48 | * set based on the received request and CachePeer settings before handing back. | |
49 | */ | |
50 | class FwdServer | |
51 | { | |
52 | MEMPROXY_CLASS(FwdServer); | |
53 | ||
54 | public: | |
55 | FwdServer(CachePeer *p, hier_code c) : | |
56 | _peer(p), | |
57 | code(c), | |
58 | next(nullptr) | |
59 | {} | |
60 | ||
61 | CbcPointer<CachePeer> _peer; /* NULL --> origin server */ | |
62 | hier_code code; | |
63 | FwdServer *next; | |
64 | }; | |
65 | ||
26ac0430 | 66 | static struct { |
75e88d56 | 67 | int timeouts; |
2fadd50d | 68 | } PeerStats; |
062e2281 | 69 | |
26ac0430 AJ |
70 | static const char *DirectStr[] = { |
71 | "DIRECT_UNKNOWN", | |
72 | "DIRECT_NO", | |
73 | "DIRECT_MAYBE", | |
74 | "DIRECT_YES" | |
75 | }; | |
75e88d56 | 76 | |
e7b08eec AR |
77 | /// a helper class to report a selected destination (for debugging) |
78 | class PeerSelectionDumper | |
79 | { | |
80 | public: | |
cb835136 AR |
81 | PeerSelectionDumper(const PeerSelector * const aSelector, const CachePeer * const aPeer, const hier_code aCode): |
82 | selector(aSelector), peer(aPeer), code(aCode) {} | |
e7b08eec | 83 | |
cb835136 | 84 | const PeerSelector * const selector; ///< selection parameters |
e7b08eec AR |
85 | const CachePeer * const peer; ///< successful selection info |
86 | const hier_code code; ///< selection algorithm | |
87 | }; | |
88 | ||
cb835136 | 89 | CBDATA_CLASS_INIT(PeerSelector); |
aa839030 | 90 | |
e7b08eec AR |
91 | /// prints PeerSelectionDumper (for debugging) |
92 | static std::ostream & | |
93 | operator <<(std::ostream &os, const PeerSelectionDumper &fsd) | |
94 | { | |
95 | os << hier_code_str[fsd.code]; | |
96 | ||
97 | if (fsd.peer) | |
98 | os << '/' << fsd.peer->host; | |
cb835136 AR |
99 | else if (fsd.selector) // useful for DIRECT and gone PINNED destinations |
100 | os << '#' << fsd.selector->request->url.host(); | |
e7b08eec AR |
101 | |
102 | return os; | |
103 | } | |
104 | ||
9865de72 EB |
105 | /// An ICP ping timeout service. |
106 | /// Protects event.cc (which is designed to handle a few unrelated timeouts) | |
107 | /// from exposure to thousands of ping-related timeouts on busy proxies. | |
108 | class PeerSelectorPingMonitor | |
109 | { | |
110 | public: | |
111 | /// registers the given selector to be notified about the IPC ping timeout | |
112 | void monitor(PeerSelector *); | |
113 | ||
114 | /// removes a PeerSelector from the waiting list | |
115 | void forget(PeerSelector *); | |
116 | ||
117 | /// \returns a (nil) registration of a non-waiting peer selector | |
118 | WaitingPeerSelectorPosition npos() { return selectors.end(); } | |
119 | ||
120 | private: | |
121 | static void NoteWaitOver(void *monitor); | |
122 | ||
123 | void startWaiting(); | |
124 | void abortWaiting(); | |
125 | void noteWaitOver(); | |
126 | ||
127 | WaitingPeerSelectors selectors; ///< \see WaitingPeerSelectors | |
128 | }; | |
129 | ||
130 | /// monitors all PeerSelector ICP ping timeouts | |
131 | static PeerSelectorPingMonitor & | |
132 | PingMonitor() | |
133 | { | |
134 | static const auto Instance = new PeerSelectorPingMonitor(); | |
135 | return *Instance; | |
136 | } | |
137 | ||
138 | /* PeerSelectorPingMonitor */ | |
139 | ||
140 | /// PeerSelectorPingMonitor::noteWaitOver() wrapper | |
141 | void | |
142 | PeerSelectorPingMonitor::NoteWaitOver(void *raw) | |
143 | { | |
144 | assert(raw); | |
145 | static_cast<PeerSelectorPingMonitor*>(raw)->noteWaitOver(); | |
146 | } | |
147 | ||
148 | /// schedules a single event to represent all waiting selectors | |
149 | void | |
150 | PeerSelectorPingMonitor::startWaiting() | |
151 | { | |
152 | assert(!selectors.empty()); | |
153 | const auto interval = tvSubDsec(current_time, selectors.begin()->first); | |
154 | eventAdd("PeerSelectorPingMonitor::NoteWaitOver", &PeerSelectorPingMonitor::NoteWaitOver, this, interval, 0, false); | |
155 | } | |
156 | ||
157 | /// undoes an earlier startWaiting() call | |
158 | void | |
159 | PeerSelectorPingMonitor::abortWaiting() | |
160 | { | |
161 | // our event may be already in the AsyncCallQueue but that is OK: | |
162 | // such queued calls cannot accumulate, and we ignore any stale ones | |
163 | eventDelete(&PeerSelectorPingMonitor::NoteWaitOver, nullptr); | |
164 | } | |
165 | ||
166 | /// calls back all ready PeerSelectors and continues to wait for others | |
167 | void | |
168 | PeerSelectorPingMonitor::noteWaitOver() | |
169 | { | |
170 | while (!selectors.empty() && current_time >= selectors.begin()->first) { | |
171 | const auto selector = selectors.begin()->second; | |
172 | CallBack(selector->al, [selector,this] { | |
173 | selector->ping.monitorRegistration = npos(); | |
174 | AsyncCall::Pointer callback = asyncCall(44, 4, "PeerSelector::HandlePingTimeout", | |
b6388dfd | 175 | cbdataDialer(PeerSelector::HandlePingTimeout, selector)); |
9865de72 EB |
176 | ScheduleCallHere(callback); |
177 | }); | |
178 | selectors.erase(selectors.begin()); | |
179 | } | |
180 | ||
181 | if (!selectors.empty()) { | |
182 | // Since abortWaiting() is unreliable, we may have been awakened by a | |
183 | // stale event A after event B has been scheduled. Now we are going to | |
184 | // schedule event C. Prevent event accumulation by deleting B (if any). | |
185 | abortWaiting(); | |
186 | ||
187 | startWaiting(); | |
188 | } | |
189 | } | |
190 | ||
191 | void | |
192 | PeerSelectorPingMonitor::monitor(PeerSelector *selector) | |
193 | { | |
194 | assert(selector); | |
195 | ||
196 | const auto deadline = selector->ping.deadline(); | |
197 | const auto position = selectors.emplace(deadline, selector); | |
198 | selector->ping.monitorRegistration = position; | |
199 | ||
200 | if (position == selectors.begin()) { | |
201 | if (selectors.size() > 1) | |
202 | abortWaiting(); // remove the previously scheduled earlier event | |
203 | startWaiting(); | |
204 | } // else the already scheduled event is still the earliest one | |
205 | } | |
206 | ||
207 | void | |
208 | PeerSelectorPingMonitor::forget(PeerSelector *selector) | |
209 | { | |
210 | assert(selector); | |
211 | ||
212 | if (selector->ping.monitorRegistration == npos()) | |
213 | return; // already forgotten | |
214 | ||
215 | const auto wasFirst = selector->ping.monitorRegistration == selectors.begin(); | |
216 | selectors.erase(selector->ping.monitorRegistration); | |
217 | selector->ping.monitorRegistration = npos(); | |
218 | ||
219 | if (wasFirst) { | |
220 | // do not reschedule if there are still elements with the same deadline | |
221 | if (!selectors.empty() && selectors.begin()->first == selector->ping.deadline()) | |
222 | return; | |
223 | abortWaiting(); | |
224 | if (!selectors.empty()) | |
225 | startWaiting(); | |
226 | } // else do nothing since the old scheduled event is still the earliest one | |
227 | } | |
228 | ||
229 | /* PeerSelector */ | |
230 | ||
cb835136 | 231 | PeerSelector::~PeerSelector() |
348b2031 | 232 | { |
36339742 AJ |
233 | while (servers) { |
234 | FwdServer *next = servers->next; | |
3c670b50 | 235 | delete servers; |
36339742 AJ |
236 | servers = next; |
237 | } | |
238 | ||
9865de72 EB |
239 | cancelPingTimeoutMonitoring(); |
240 | ||
029c8349 AJ |
241 | if (entry) { |
242 | debugs(44, 3, entry->url()); | |
029c8349 | 243 | entry->ping_status = PING_DONE; |
5ae21d99 AJ |
244 | } |
245 | ||
029c8349 | 246 | if (acl_checklist) { |
cb835136 | 247 | debugs(44, DBG_IMPORTANT, "BUG: peer selector gone while waiting for a slow ACL"); |
029c8349 | 248 | delete acl_checklist; |
348b2031 | 249 | } |
62e76326 | 250 | |
029c8349 | 251 | HTTPMSGUNLOCK(request); |
62e76326 | 252 | |
029c8349 AJ |
253 | if (entry) { |
254 | assert(entry->ping_status != PING_WAITING); | |
1f90fd4a | 255 | entry->unlock("peerSelect"); |
029c8349 | 256 | entry = NULL; |
73a201f8 | 257 | } |
62e76326 | 258 | |
029c8349 | 259 | delete lastError; |
348b2031 | 260 | } |
062e2281 | 261 | |
9865de72 EB |
262 | void |
263 | PeerSelector::startPingWaiting() | |
264 | { | |
265 | assert(entry); | |
266 | assert(entry->ping_status != PING_WAITING); | |
267 | PingMonitor().monitor(this); | |
268 | entry->ping_status = PING_WAITING; | |
269 | } | |
270 | ||
271 | void | |
272 | PeerSelector::cancelPingTimeoutMonitoring() | |
273 | { | |
274 | PingMonitor().forget(this); | |
275 | } | |
276 | ||
2d72d4fd | 277 | static int |
cb365059 | 278 | peerSelectIcpPing(PeerSelector *ps, int direct, StoreEntry * entry) |
062e2281 | 279 | { |
cb365059 EB |
280 | assert(ps); |
281 | HttpRequest *request = ps->request; | |
282 | ||
7b665aeb | 283 | int n; |
db1cd23c | 284 | assert(entry); |
285 | assert(entry->ping_status == PING_NONE); | |
9bd6a36d | 286 | assert(direct != DIRECT_YES); |
cb835136 | 287 | debugs(44, 3, entry->url()); |
62e76326 | 288 | |
45e5102d | 289 | if (!request->flags.hierarchical && direct != DIRECT_NO) |
62e76326 | 290 | return 0; |
291 | ||
d46a87a8 | 292 | if (EBIT_TEST(entry->flags, KEY_PRIVATE) && !neighbors_do_private_keys) |
62e76326 | 293 | if (direct != DIRECT_NO) |
294 | return 0; | |
295 | ||
cb365059 | 296 | n = neighborsCount(ps); |
62e76326 | 297 | |
cb835136 | 298 | debugs(44, 3, "counted " << n << " neighbors"); |
62e76326 | 299 | |
7b665aeb | 300 | return n; |
062e2281 | 301 | } |
302 | ||
6043e368 AR |
303 | static void |
304 | peerSelect(PeerSelectionInitiator *initiator, | |
cfd66529 | 305 | HttpRequest * request, |
d4806c91 | 306 | AccessLogEntry::Pointer const &al, |
6043e368 | 307 | StoreEntry * entry) |
75e88d56 | 308 | { |
86b389fc | 309 | if (entry) |
7f06a3d8 | 310 | debugs(44, 3, *entry << ' ' << entry->url()); |
86b389fc | 311 | else |
7f06a3d8 | 312 | debugs(44, 3, request->method); |
62e76326 | 313 | |
cb835136 | 314 | const auto selector = new PeerSelector(initiator); |
62e76326 | 315 | |
cb835136 AR |
316 | selector->request = request; |
317 | HTTPMSGLOCK(selector->request); | |
318 | selector->al = al; | |
62e76326 | 319 | |
cb835136 | 320 | selector->entry = entry; |
62e76326 | 321 | |
6cfa8966 | 322 | #if USE_CACHE_DIGESTS |
62e76326 | 323 | |
39edba21 | 324 | request->hier.peer_select_start = current_time; |
62e76326 | 325 | |
39edba21 | 326 | #endif |
62e76326 | 327 | |
cb835136 AR |
328 | if (selector->entry) |
329 | selector->entry->lock("peerSelect"); | |
62e76326 | 330 | |
cb835136 | 331 | selector->selectMore(); |
75e88d56 | 332 | } |
333 | ||
6043e368 AR |
334 | void |
335 | PeerSelectionInitiator::startSelectingDestinations(HttpRequest *request, const AccessLogEntry::Pointer &ale, StoreEntry *entry) | |
336 | { | |
337 | subscribed = true; | |
338 | peerSelect(this, request, ale, entry); | |
339 | // and wait for noteDestination() and/or noteDestinationsEnd() calls | |
340 | } | |
341 | ||
cb835136 | 342 | void |
329c128c | 343 | PeerSelector::checkNeverDirectDone(const Acl::Answer answer) |
75e88d56 | 344 | { |
cb835136 AR |
345 | acl_checklist = nullptr; |
346 | debugs(44, 3, answer); | |
347 | never_direct = answer; | |
3985b3f8 | 348 | switch (answer) { |
ae026ec6 | 349 | case ACCESS_ALLOWED: |
f365d297 | 350 | /** if never_direct says YES, do that. */ |
cb835136 AR |
351 | direct = DIRECT_NO; |
352 | debugs(44, 3, "direct = " << DirectStr[direct] << " (never_direct allow)"); | |
ae026ec6 AJ |
353 | break; |
354 | case ACCESS_DENIED: // not relevant. | |
1f585949 | 355 | case ACCESS_DUNNO: // not relevant. |
ae026ec6 | 356 | break; |
1f585949 | 357 | case ACCESS_AUTH_REQUIRED: |
ae026ec6 | 358 | debugs(44, DBG_IMPORTANT, "WARNING: never_direct resulted in " << answer << ". Username ACLs are not reliable here."); |
1f585949 | 359 | break; |
ae026ec6 | 360 | } |
cb835136 | 361 | selectMore(); |
75e88d56 | 362 | } |
363 | ||
cb835136 | 364 | void |
329c128c | 365 | PeerSelector::CheckNeverDirectDone(Acl::Answer answer, void *data) |
75e88d56 | 366 | { |
cb835136 AR |
367 | static_cast<PeerSelector*>(data)->checkNeverDirectDone(answer); |
368 | } | |
369 | ||
370 | void | |
329c128c | 371 | PeerSelector::checkAlwaysDirectDone(const Acl::Answer answer) |
cb835136 AR |
372 | { |
373 | acl_checklist = nullptr; | |
374 | debugs(44, 3, answer); | |
375 | always_direct = answer; | |
3985b3f8 | 376 | switch (answer) { |
ae026ec6 AJ |
377 | case ACCESS_ALLOWED: |
378 | /** if always_direct says YES, do that. */ | |
cb835136 AR |
379 | direct = DIRECT_YES; |
380 | debugs(44, 3, "direct = " << DirectStr[direct] << " (always_direct allow)"); | |
ae026ec6 AJ |
381 | break; |
382 | case ACCESS_DENIED: // not relevant. | |
1f585949 | 383 | case ACCESS_DUNNO: // not relevant. |
ae026ec6 | 384 | break; |
1f585949 | 385 | case ACCESS_AUTH_REQUIRED: |
ae026ec6 | 386 | debugs(44, DBG_IMPORTANT, "WARNING: always_direct resulted in " << answer << ". Username ACLs are not reliable here."); |
1f585949 | 387 | break; |
ae026ec6 | 388 | } |
cb835136 AR |
389 | selectMore(); |
390 | } | |
391 | ||
392 | void | |
329c128c | 393 | PeerSelector::CheckAlwaysDirectDone(Acl::Answer answer, void *data) |
cb835136 AR |
394 | { |
395 | static_cast<PeerSelector*>(data)->checkAlwaysDirectDone(answer); | |
75e88d56 | 396 | } |
397 | ||
cb835136 | 398 | /// \returns true (after destroying "this") if the peer initiator is gone |
6043e368 | 399 | /// \returns false (without side effects) otherwise |
cb835136 AR |
400 | bool |
401 | PeerSelector::selectionAborted() | |
6043e368 | 402 | { |
cb835136 | 403 | if (interestedInitiator()) |
6043e368 AR |
404 | return false; |
405 | ||
406 | debugs(44, 3, "Aborting peer selection: Initiator gone or lost interest."); | |
cb835136 | 407 | delete this; |
6043e368 AR |
408 | return true; |
409 | } | |
410 | ||
cb835136 | 411 | /// A single DNS resolution loop iteration: Converts selected FwdServer to IPs. |
cfd66529 | 412 | void |
cb835136 | 413 | PeerSelector::resolveSelected() |
cfd66529 | 414 | { |
cb835136 | 415 | if (selectionAborted()) |
36339742 | 416 | return; |
6043e368 | 417 | |
cb835136 | 418 | FwdServer *fs = servers; |
36339742 | 419 | |
7177edfb AJ |
420 | // Bug 3243: CVE 2009-0801 |
421 | // Bypass of browser same-origin access control in intercepted communication | |
422 | // To resolve this we must use only the original client destination when going DIRECT | |
423 | // on intercepted traffic which failed Host verification | |
cb835136 | 424 | const HttpRequest *req = request; |
45e5102d | 425 | const bool isIntercepted = !req->flags.redirected && |
0d901ef4 | 426 | (req->flags.intercepted || req->flags.interceptTproxy); |
45e5102d | 427 | const bool useOriginalDst = Config.onoff.client_dst_passthru || !req->flags.hostVerified; |
7177edfb AJ |
428 | const bool choseDirect = fs && fs->code == HIER_DIRECT; |
429 | if (isIntercepted && useOriginalDst && choseDirect) { | |
d9371d7b AJ |
430 | // check the client is still around before using any of its details |
431 | if (req->clientConnectionManager.valid()) { | |
432 | // construct a "result" adding the ORIGINAL_DST to the set instead of DIRECT | |
433 | Comm::ConnectionPointer p = new Comm::Connection(); | |
434 | p->remote = req->clientConnectionManager->clientConnection->local; | |
6043e368 | 435 | fs->code = ORIGINAL_DST; // fs->code is DIRECT. This fixes the display. |
cb835136 | 436 | handlePath(p, *fs); |
d9371d7b | 437 | } |
7177edfb AJ |
438 | |
439 | // clear the used fs and continue | |
cb835136 | 440 | servers = fs->next; |
3c670b50 | 441 | delete fs; |
cb835136 | 442 | resolveSelected(); |
7177edfb AJ |
443 | return; |
444 | } | |
445 | ||
3dde9e52 CT |
446 | if (fs && fs->code == PINNED) { |
447 | // Nil path signals a PINNED destination selection. Our initiator should | |
448 | // borrow and use clientConnectionManager's pinned connection object | |
449 | // (regardless of that connection destination). | |
450 | handlePath(nullptr, *fs); | |
451 | servers = fs->next; | |
452 | delete fs; | |
453 | resolveSelected(); | |
454 | return; | |
455 | } | |
456 | ||
cfd66529 | 457 | // convert the list of FwdServer destinations into destinations IP addresses |
cb835136 | 458 | if (fs && wantsMoreDestinations()) { |
cfd66529 | 459 | // send the next one off for DNS lookup. |
cb835136 AR |
460 | const char *host = fs->_peer.valid() ? fs->_peer->host : request->url.host(); |
461 | debugs(44, 2, "Find IP destination for: " << url() << "' via " << host); | |
462 | Dns::nbgethostbyname(host, this); | |
cfd66529 AJ |
463 | return; |
464 | } | |
465 | ||
c2f4e9cc AJ |
466 | // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries. |
467 | // due to the allocation method of fs, we must deallocate each manually. | |
468 | // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries | |
6043e368 | 469 | if (fs) { |
cb835136 | 470 | assert(fs == servers); |
9bc68187 | 471 | while (fs) { |
cb835136 | 472 | servers = fs->next; |
3c670b50 | 473 | delete fs; |
cb835136 | 474 | fs = servers; |
c2f4e9cc AJ |
475 | } |
476 | } | |
477 | ||
cfd66529 | 478 | // done with DNS lookups. pass back to caller |
62e76326 | 479 | |
cb835136 AR |
480 | debugs(44, 2, id << " found all " << foundPaths << " destinations for " << url()); |
481 | debugs(44, 2, " always_direct = " << always_direct); | |
482 | debugs(44, 2, " never_direct = " << never_direct); | |
483 | debugs(44, 2, " timedout = " << ping.timedout); | |
5ae21d99 | 484 | |
cb835136 AR |
485 | ping.stop = current_time; |
486 | request->hier.ping = ping; // final result | |
3dfe8371 | 487 | |
cb835136 | 488 | if (lastError && foundPaths) { |
6043e368 AR |
489 | // nobody cares about errors if we found destinations despite them |
490 | debugs(44, 3, "forgetting the last error"); | |
cb835136 AR |
491 | delete lastError; |
492 | lastError = nullptr; | |
db1cd23c | 493 | } |
62e76326 | 494 | |
cb835136 AR |
495 | if (const auto initiator = interestedInitiator()) |
496 | initiator->noteDestinationsEnd(lastError); | |
497 | lastError = nullptr; // initiator owns the ErrorState object now | |
498 | delete this; | |
93775f90 | 499 | } |
500 | ||
fd9c47d1 | 501 | void |
cb835136 | 502 | PeerSelector::noteLookup(const Dns::LookupDetails &details) |
cfd66529 | 503 | { |
fd9c47d1 | 504 | /* ignore lookup delays that occurred after the initiator moved on */ |
cfd66529 | 505 | |
cb835136 | 506 | if (selectionAborted()) |
fd9c47d1 | 507 | return; |
cfd66529 | 508 | |
fd9c47d1 AR |
509 | if (!wantsMoreDestinations()) |
510 | return; | |
cfd66529 | 511 | |
fd9c47d1 AR |
512 | request->recordLookup(details); |
513 | } | |
5229395c | 514 | |
fd9c47d1 | 515 | void |
cb835136 | 516 | PeerSelector::noteIp(const Ip::Address &ip) |
fd9c47d1 | 517 | { |
cb835136 | 518 | if (selectionAborted()) |
fd9c47d1 | 519 | return; |
cfd66529 | 520 | |
fd9c47d1 AR |
521 | if (!wantsMoreDestinations()) |
522 | return; | |
27d1f0a0 | 523 | |
fd9c47d1 | 524 | const auto peer = servers->_peer.valid(); |
cfd66529 | 525 | |
fd9c47d1 AR |
526 | // for TPROXY spoofing, we must skip unusable addresses |
527 | if (request->flags.spoofClientIp && !(peer && peer->options.no_tproxy) ) { | |
528 | if (ip.isIPv4() != request->client_addr.isIPv4()) | |
529 | return; // cannot spoof the client address on this link | |
530 | } | |
c7a52e6d | 531 | |
fd9c47d1 AR |
532 | Comm::ConnectionPointer p = new Comm::Connection(); |
533 | p->remote = ip; | |
534 | p->remote.port(peer ? peer->http_port : request->url.port()); | |
535 | handlePath(p, *servers); | |
536 | } | |
c7a52e6d | 537 | |
fd9c47d1 | 538 | void |
cb835136 | 539 | PeerSelector::noteIps(const Dns::CachedIps *ia, const Dns::LookupDetails &details) |
fd9c47d1 | 540 | { |
cb835136 | 541 | if (selectionAborted()) |
fd9c47d1 | 542 | return; |
cfd66529 | 543 | |
fd9c47d1 AR |
544 | FwdServer *fs = servers; |
545 | if (!ia) { | |
546 | debugs(44, 3, "Unknown host: " << (fs->_peer.valid() ? fs->_peer->host : request->url.host())); | |
a37fdd8a | 547 | // discard any previous error. |
fd9c47d1 AR |
548 | delete lastError; |
549 | lastError = NULL; | |
a37fdd8a | 550 | if (fs->code == HIER_DIRECT) { |
7e6eabbc | 551 | lastError = new ErrorState(ERR_DNS_FAIL, Http::scServiceUnavailable, request, al); |
fd9c47d1 | 552 | lastError->dnsError = details.error; |
a37fdd8a | 553 | } |
cfd66529 | 554 | } |
fd9c47d1 | 555 | // else noteIp() calls have already processed all IPs in *ia |
cfd66529 | 556 | |
fd9c47d1 | 557 | servers = fs->next; |
3c670b50 | 558 | delete fs; |
cfd66529 | 559 | |
cb835136 AR |
560 | // continue resolving selected peers |
561 | resolveSelected(); | |
cfd66529 AJ |
562 | } |
563 | ||
cb835136 AR |
564 | int |
565 | PeerSelector::checkNetdbDirect() | |
b3264694 | 566 | { |
9b5c4a9a | 567 | #if USE_ICMP |
a3c6762c | 568 | CachePeer *p; |
b3264694 | 569 | int myrtt; |
570 | int myhops; | |
62e76326 | 571 | |
cb835136 | 572 | if (direct == DIRECT_NO) |
62e76326 | 573 | return 0; |
574 | ||
9b5c4a9a AJ |
575 | /* base lookup on RTT and Hops if ICMP NetDB is enabled. */ |
576 | ||
cb835136 | 577 | myrtt = netdbHostRtt(request->url.host()); |
5c51bffb AJ |
578 | debugs(44, 3, "MY RTT = " << myrtt << " msec"); |
579 | debugs(44, 3, "minimum_direct_rtt = " << Config.minDirectRtt << " msec"); | |
62e76326 | 580 | |
5f84d830 | 581 | if (myrtt && myrtt <= Config.minDirectRtt) |
62e76326 | 582 | return 1; |
583 | ||
cb835136 | 584 | myhops = netdbHostHops(request->url.host()); |
62e76326 | 585 | |
cb835136 AR |
586 | debugs(44, 3, "MY hops = " << myhops); |
587 | debugs(44, 3, "minimum_direct_hops = " << Config.minDirectHops); | |
62e76326 | 588 | |
b3264694 | 589 | if (myhops && myhops <= Config.minDirectHops) |
62e76326 | 590 | return 1; |
591 | ||
cb835136 | 592 | p = whichPeer(closest_parent_miss); |
62e76326 | 593 | |
5f84d830 | 594 | if (p == NULL) |
62e76326 | 595 | return 0; |
596 | ||
cb835136 | 597 | debugs(44, 3, "closest_parent_miss RTT = " << ping.p_rtt << " msec"); |
62e76326 | 598 | |
cb835136 | 599 | if (myrtt && myrtt <= ping.p_rtt) |
62e76326 | 600 | return 1; |
601 | ||
9b5c4a9a AJ |
602 | #endif /* USE_ICMP */ |
603 | ||
b3264694 | 604 | return 0; |
605 | } | |
606 | ||
cb835136 AR |
607 | void |
608 | PeerSelector::selectMore() | |
062e2281 | 609 | { |
cb835136 | 610 | if (selectionAborted()) |
36339742 | 611 | return; |
36339742 | 612 | |
5c51bffb | 613 | debugs(44, 3, request->method << ' ' << request->url.host()); |
62e76326 | 614 | |
45e5102d | 615 | /** If we don't know whether DIRECT is permitted ... */ |
cb835136 AR |
616 | if (direct == DIRECT_UNKNOWN) { |
617 | if (always_direct == ACCESS_DUNNO) { | |
618 | debugs(44, 3, "direct = " << DirectStr[direct] << " (always_direct to be checked)"); | |
45e5102d | 619 | /** check always_direct; */ |
d4806c91 | 620 | ACLFilledChecklist *ch = new ACLFilledChecklist(Config.accessList.AlwaysDirect, request, NULL); |
cb835136 AR |
621 | ch->al = al; |
622 | acl_checklist = ch; | |
cb365059 | 623 | acl_checklist->syncAle(request, nullptr); |
cb835136 | 624 | acl_checklist->nonBlockingCheck(CheckAlwaysDirectDone, this); |
62e76326 | 625 | return; |
cb835136 AR |
626 | } else if (never_direct == ACCESS_DUNNO) { |
627 | debugs(44, 3, "direct = " << DirectStr[direct] << " (never_direct to be checked)"); | |
45e5102d | 628 | /** check never_direct; */ |
d4806c91 | 629 | ACLFilledChecklist *ch = new ACLFilledChecklist(Config.accessList.NeverDirect, request, NULL); |
cb835136 AR |
630 | ch->al = al; |
631 | acl_checklist = ch; | |
cb365059 | 632 | acl_checklist->syncAle(request, nullptr); |
cb835136 | 633 | acl_checklist->nonBlockingCheck(CheckNeverDirectDone, this); |
62e76326 | 634 | return; |
450fe1cb | 635 | } else if (request->flags.noDirect) { |
45e5102d | 636 | /** if we are accelerating, direct is not an option. */ |
cb835136 AR |
637 | direct = DIRECT_NO; |
638 | debugs(44, 3, "direct = " << DirectStr[direct] << " (forced non-direct)"); | |
450fe1cb | 639 | } else if (request->flags.loopDetected) { |
45e5102d | 640 | /** if we are in a forwarding-loop, direct is not an option. */ |
cb835136 AR |
641 | direct = DIRECT_YES; |
642 | debugs(44, 3, "direct = " << DirectStr[direct] << " (forwarding loop detected)"); | |
643 | } else if (checkNetdbDirect()) { | |
644 | direct = DIRECT_YES; | |
645 | debugs(44, 3, "direct = " << DirectStr[direct] << " (checkNetdbDirect)"); | |
62e76326 | 646 | } else { |
cb835136 AR |
647 | direct = DIRECT_MAYBE; |
648 | debugs(44, 3, "direct = " << DirectStr[direct] << " (default)"); | |
62e76326 | 649 | } |
650 | ||
cb835136 | 651 | debugs(44, 3, "direct = " << DirectStr[direct]); |
db1cd23c | 652 | } |
62e76326 | 653 | |
d67acb4e | 654 | if (!entry || entry->ping_status == PING_NONE) |
cb835136 | 655 | selectPinned(); |
df503d6c | 656 | if (entry == NULL) { |
62e76326 | 657 | (void) 0; |
df503d6c | 658 | } else if (entry->ping_status == PING_NONE) { |
cb835136 | 659 | selectSomeNeighbor(); |
62e76326 | 660 | |
661 | if (entry->ping_status == PING_WAITING) | |
662 | return; | |
db1cd23c | 663 | } else if (entry->ping_status == PING_WAITING) { |
cb835136 | 664 | selectSomeNeighborReplies(); |
9865de72 | 665 | cancelPingTimeoutMonitoring(); |
62e76326 | 666 | entry->ping_status = PING_DONE; |
db1cd23c | 667 | } |
62e76326 | 668 | |
cb835136 | 669 | switch (direct) { |
62e76326 | 670 | |
168dfda9 | 671 | case DIRECT_YES: |
cb835136 | 672 | selectSomeDirect(); |
62e76326 | 673 | break; |
674 | ||
168dfda9 | 675 | case DIRECT_NO: |
cb835136 AR |
676 | selectSomeParent(); |
677 | selectAllParents(); | |
62e76326 | 678 | break; |
679 | ||
168dfda9 | 680 | default: |
62e76326 | 681 | |
682 | if (Config.onoff.prefer_direct) | |
cb835136 | 683 | selectSomeDirect(); |
62e76326 | 684 | |
45e5102d | 685 | if (request->flags.hierarchical || !Config.onoff.nonhierarchical_direct) { |
cb835136 AR |
686 | selectSomeParent(); |
687 | selectAllParents(); | |
8b9a89c9 | 688 | } |
62e76326 | 689 | |
690 | if (!Config.onoff.prefer_direct) | |
cb835136 | 691 | selectSomeDirect(); |
62e76326 | 692 | |
693 | break; | |
168dfda9 | 694 | } |
62e76326 | 695 | |
cb835136 AR |
696 | // end peer selection; start resolving selected peers |
697 | resolveSelected(); | |
db1cd23c | 698 | } |
699 | ||
cb365059 | 700 | bool peerAllowedToUse(const CachePeer *, PeerSelector*); |
cfd66529 | 701 | |
cb835136 AR |
702 | /// Selects a pinned connection if it exists, is valid, and is allowed. |
703 | void | |
704 | PeerSelector::selectPinned() | |
d67acb4e | 705 | { |
cb835136 | 706 | // TODO: Avoid all repeated calls. Relying on PING_DONE is not enough. |
d67acb4e AJ |
707 | if (!request->pinnedConnection()) |
708 | return; | |
3dde9e52 | 709 | |
daf80700 CT |
710 | const auto peer = request->pinnedConnection()->pinnedPeer(); |
711 | const auto usePinned = peer ? peerAllowedToUse(peer, this) : (direct != DIRECT_NO); | |
712 | // If the pinned connection is prohibited (for this request) then | |
cb835136 | 713 | // the initiator must decide whether it is OK to open a new one instead. |
daf80700 CT |
714 | request->pinnedConnection()->pinning.peerAccessDenied = !usePinned; |
715 | ||
716 | addSelection(peer, PINNED); | |
717 | if (entry) | |
718 | entry->ping_status = PING_DONE; // skip ICP | |
d67acb4e AJ |
719 | } |
720 | ||
27774cee | 721 | /** |
db1cd23c | 722 | * Selects a neighbor (parent or sibling) based on one of the |
723 | * following methods: | |
724 | * Cache Digests | |
725 | * CARP | |
9b5c4a9a | 726 | * ICMP Netdb RTT estimates |
db1cd23c | 727 | * ICP/HTCP queries |
728 | */ | |
cb835136 AR |
729 | void |
730 | PeerSelector::selectSomeNeighbor() | |
db1cd23c | 731 | { |
a3c6762c | 732 | CachePeer *p; |
db1cd23c | 733 | hier_code code = HIER_NONE; |
734 | assert(entry->ping_status == PING_NONE); | |
62e76326 | 735 | |
cb835136 | 736 | if (direct == DIRECT_YES) { |
62e76326 | 737 | entry->ping_status = PING_DONE; |
738 | return; | |
124511e5 | 739 | } |
62e76326 | 740 | |
6cfa8966 | 741 | #if USE_CACHE_DIGESTS |
cb365059 | 742 | if ((p = neighborsDigestSelect(this))) { |
5c51bffb | 743 | if (neighborType(p, request->url) == PEER_PARENT) |
62e76326 | 744 | code = CD_PARENT_HIT; |
745 | else | |
746 | code = CD_SIBLING_HIT; | |
db1cd23c | 747 | } else |
c127134a | 748 | #endif |
cb365059 | 749 | if ((p = netdbClosestParent(this))) { |
62e76326 | 750 | code = CLOSEST_PARENT; |
cb365059 | 751 | } else if (peerSelectIcpPing(this, direct, entry)) { |
cb835136 AR |
752 | debugs(44, 3, "Doing ICP pings"); |
753 | ping.start = current_time; | |
754 | ping.n_sent = neighborsUdpPing(request, | |
755 | entry, | |
756 | HandlePingReply, | |
757 | this, | |
758 | &ping.n_replies_expected, | |
759 | &ping.timeout); | |
9865de72 EB |
760 | // TODO: Refactor neighborsUdpPing() to guarantee positive timeouts. |
761 | if (ping.timeout < 0) | |
762 | ping.timeout = 0; | |
cb835136 AR |
763 | |
764 | if (ping.n_sent == 0) | |
fa84c01d | 765 | debugs(44, DBG_CRITICAL, "WARNING: neighborsUdpPing returned 0"); |
cb835136 AR |
766 | debugs(44, 3, ping.n_replies_expected << |
767 | " ICP replies expected, RTT " << ping.timeout << | |
26ac0430 | 768 | " msec"); |
62e76326 | 769 | |
cb835136 | 770 | if (ping.n_replies_expected > 0) { |
9865de72 | 771 | startPingWaiting(); |
62e76326 | 772 | return; |
773 | } | |
774 | } | |
775 | ||
db1cd23c | 776 | if (code != HIER_NONE) { |
62e76326 | 777 | assert(p); |
cb835136 | 778 | addSelection(p, code); |
db1cd23c | 779 | } |
62e76326 | 780 | |
db1cd23c | 781 | entry->ping_status = PING_DONE; |
782 | } | |
783 | ||
cb835136 AR |
784 | /// Selects a neighbor (parent or sibling) based on ICP/HTCP replies. |
785 | void | |
786 | PeerSelector::selectSomeNeighborReplies() | |
db1cd23c | 787 | { |
a3c6762c | 788 | CachePeer *p = NULL; |
db1cd23c | 789 | hier_code code = HIER_NONE; |
cb835136 AR |
790 | assert(entry->ping_status == PING_WAITING); |
791 | assert(direct != DIRECT_YES); | |
62e76326 | 792 | |
cb835136 | 793 | if (checkNetdbDirect()) { |
62e76326 | 794 | code = CLOSEST_DIRECT; |
cb835136 | 795 | addSelection(nullptr, code); |
62e76326 | 796 | return; |
db1cd23c | 797 | } |
62e76326 | 798 | |
cb835136 AR |
799 | if ((p = hit)) { |
800 | code = hit_type == PEER_PARENT ? PARENT_HIT : SIBLING_HIT; | |
26ac0430 | 801 | } else { |
cb835136 AR |
802 | if (!closest_parent_miss.isAnyAddr()) { |
803 | p = whichPeer(closest_parent_miss); | |
cc192b50 | 804 | code = CLOSEST_PARENT_MISS; |
cb835136 AR |
805 | } else if (!first_parent_miss.isAnyAddr()) { |
806 | p = whichPeer(first_parent_miss); | |
cc192b50 | 807 | code = FIRST_PARENT_MISS; |
808 | } | |
809 | } | |
db1cd23c | 810 | if (p && code != HIER_NONE) { |
cb835136 | 811 | addSelection(p, code); |
db1cd23c | 812 | } |
813 | } | |
814 | ||
cb835136 AR |
815 | /// Adds a "direct" entry if the request can be forwarded to the origin server. |
816 | void | |
817 | PeerSelector::selectSomeDirect() | |
db1cd23c | 818 | { |
cb835136 | 819 | if (direct == DIRECT_NO) |
62e76326 | 820 | return; |
821 | ||
db80e881 | 822 | /* WAIS is not implemented natively */ |
cb835136 | 823 | if (request->url.getScheme() == AnyP::PROTO_WAIS) |
26ac0430 | 824 | return; |
db80e881 | 825 | |
cb835136 | 826 | addSelection(nullptr, HIER_DIRECT); |
db1cd23c | 827 | } |
828 | ||
cb835136 AR |
829 | void |
830 | PeerSelector::selectSomeParent() | |
db1cd23c | 831 | { |
a3c6762c | 832 | CachePeer *p; |
db1cd23c | 833 | hier_code code = HIER_NONE; |
5c51bffb | 834 | debugs(44, 3, request->method << ' ' << request->url.host()); |
62e76326 | 835 | |
cb835136 | 836 | if (direct == DIRECT_YES) |
62e76326 | 837 | return; |
838 | ||
cb365059 | 839 | if ((p = peerSourceHashSelectParent(this))) { |
b4ab8666 | 840 | code = SOURCEHASH_PARENT; |
2f1431ea | 841 | #if USE_AUTH |
cb365059 | 842 | } else if ((p = peerUserHashSelectParent(this))) { |
f7e1d9ce | 843 | code = USERHASH_PARENT; |
2f1431ea | 844 | #endif |
cb365059 | 845 | } else if ((p = carpSelectParent(this))) { |
62e76326 | 846 | code = CARP; |
cb365059 | 847 | } else if ((p = getRoundRobinParent(this))) { |
62e76326 | 848 | code = ROUNDROBIN_PARENT; |
cb365059 | 849 | } else if ((p = getWeightedRoundRobinParent(this))) { |
62e76326 | 850 | code = ROUNDROBIN_PARENT; |
cb365059 | 851 | } else if ((p = getFirstUpParent(this))) { |
62e76326 | 852 | code = FIRSTUP_PARENT; |
cb365059 | 853 | } else if ((p = getDefaultParent(this))) { |
b4ab8666 | 854 | code = DEFAULT_PARENT; |
db1cd23c | 855 | } |
62e76326 | 856 | |
db1cd23c | 857 | if (code != HIER_NONE) { |
cb835136 | 858 | addSelection(p, code); |
062e2281 | 859 | } |
860 | } | |
861 | ||
cb835136 AR |
862 | /// Adds alive parents. Used as a last resort for never_direct. |
863 | void | |
864 | PeerSelector::selectAllParents() | |
168dfda9 | 865 | { |
a3c6762c | 866 | CachePeer *p; |
168dfda9 | 867 | /* Add all alive parents */ |
62e76326 | 868 | |
168dfda9 | 869 | for (p = Config.peers; p; p = p->next) { |
62e76326 | 870 | /* XXX: neighbors.c lacks a public interface for enumerating |
871 | * parents to a request so we have to dig some here.. | |
872 | */ | |
873 | ||
5c51bffb | 874 | if (neighborType(p, request->url) != PEER_PARENT) |
62e76326 | 875 | continue; |
876 | ||
cb365059 | 877 | if (!peerHTTPOkay(p, this)) |
62e76326 | 878 | continue; |
879 | ||
cb835136 | 880 | addSelection(p, ANY_OLD_PARENT); |
168dfda9 | 881 | } |
62e76326 | 882 | |
168dfda9 | 883 | /* XXX: should add dead parents here, but it is currently |
884 | * not possible to find out which parents are dead or which | |
885 | * simply are not configured to handle the request. | |
886 | */ | |
887 | /* Add default parent as a last resort */ | |
cb365059 | 888 | if ((p = getDefaultParent(this))) { |
cb835136 | 889 | addSelection(p, DEFAULT_PARENT); |
168dfda9 | 890 | } |
891 | } | |
892 | ||
cb835136 AR |
893 | void |
894 | PeerSelector::handlePingTimeout() | |
062e2281 | 895 | { |
cb835136 | 896 | debugs(44, 3, url()); |
62e76326 | 897 | |
9865de72 EB |
898 | // do nothing if ping reply came while handlePingTimeout() was queued |
899 | if (!entry || entry->ping_status != PING_WAITING) | |
900 | return; | |
901 | ||
902 | entry->ping_status = PING_DONE; | |
6043e368 | 903 | |
cb835136 | 904 | if (selectionAborted()) |
62e76326 | 905 | return; |
62e76326 | 906 | |
5db6bf73 | 907 | ++PeerStats.timeouts; |
cb835136 AR |
908 | ping.timedout = 1; |
909 | selectMore(); | |
910 | } | |
911 | ||
912 | void | |
9865de72 | 913 | PeerSelector::HandlePingTimeout(PeerSelector *selector) |
cb835136 | 914 | { |
9865de72 | 915 | selector->handlePingTimeout(); |
85034133 | 916 | } |
917 | ||
918 | void | |
919 | peerSelectInit(void) | |
920 | { | |
75e88d56 | 921 | memset(&PeerStats, '\0', sizeof(PeerStats)); |
062e2281 | 922 | } |
93775f90 | 923 | |
cb835136 AR |
924 | void |
925 | PeerSelector::handleIcpParentMiss(CachePeer *p, icp_common_t *header) | |
b3264694 | 926 | { |
927 | int rtt; | |
62e76326 | 928 | |
9b5c4a9a | 929 | #if USE_ICMP |
b3264694 | 930 | if (Config.onoff.query_icmp) { |
62e76326 | 931 | if (header->flags & ICP_FLAG_SRC_RTT) { |
932 | rtt = header->pad & 0xFFFF; | |
9b5c4a9a | 933 | int hops = (header->pad >> 16) & 0xFFFF; |
62e76326 | 934 | |
935 | if (rtt > 0 && rtt < 0xFFFF) | |
cb835136 | 936 | netdbUpdatePeer(request->url, p, rtt, hops); |
62e76326 | 937 | |
cb835136 AR |
938 | if (rtt && (ping.p_rtt == 0 || rtt < ping.p_rtt)) { |
939 | closest_parent_miss = p->in_addr; | |
940 | ping.p_rtt = rtt; | |
62e76326 | 941 | } |
942 | } | |
b3264694 | 943 | } |
8b082ed9 FC |
944 | #else |
945 | (void)header; | |
9b5c4a9a | 946 | #endif /* USE_ICMP */ |
62e76326 | 947 | |
18ec72b2 | 948 | /* if closest-only is set, then don't allow FIRST_PARENT_MISS */ |
cd196bc8 | 949 | if (p->options.closest_only) |
62e76326 | 950 | return; |
951 | ||
85223cd7 | 952 | /* set FIRST_MISS if there is no CLOSEST parent */ |
cb835136 | 953 | if (!closest_parent_miss.isAnyAddr()) |
62e76326 | 954 | return; |
955 | ||
cb835136 | 956 | rtt = (tvSubMsec(ping.start, current_time) - p->basetime) / p->weight; |
62e76326 | 957 | |
d1b63fc8 | 958 | if (rtt < 1) |
62e76326 | 959 | rtt = 1; |
960 | ||
cb835136 AR |
961 | if (first_parent_miss.isAnyAddr() || rtt < ping.w_rtt) { |
962 | first_parent_miss = p->in_addr; | |
963 | ping.w_rtt = rtt; | |
b3264694 | 964 | } |
965 | } | |
93775f90 | 966 | |
cb835136 AR |
967 | void |
968 | PeerSelector::handleIcpReply(CachePeer *p, const peer_t type, icp_common_t *header) | |
93775f90 | 969 | { |
e6ccf245 | 970 | icp_opcode op = header->getOpCode(); |
cb835136 | 971 | debugs(44, 3, icp_opcode_str[op] << ' ' << url()); |
69c95dd3 | 972 | #if USE_CACHE_DIGESTS && 0 |
26b164ac | 973 | /* do cd lookup to count false misses */ |
62e76326 | 974 | |
3ab66981 | 975 | if (p && request) |
62e76326 | 976 | peerNoteDigestLookup(request, p, |
cb365059 | 977 | peerDigestLookup(p, this)); |
62e76326 | 978 | |
26b164ac | 979 | #endif |
62e76326 | 980 | |
cb835136 | 981 | ++ping.n_recv; |
62e76326 | 982 | |
27cd7235 | 983 | if (op == ICP_MISS || op == ICP_DECHO) { |
62e76326 | 984 | if (type == PEER_PARENT) |
cb835136 | 985 | handleIcpParentMiss(p, header); |
a7c05555 | 986 | } else if (op == ICP_HIT) { |
cb835136 AR |
987 | hit = p; |
988 | hit_type = type; | |
989 | selectMore(); | |
62e76326 | 990 | return; |
db1cd23c | 991 | } |
62e76326 | 992 | |
cb835136 | 993 | if (ping.n_recv < ping.n_replies_expected) |
62e76326 | 994 | return; |
995 | ||
cb835136 | 996 | selectMore(); |
93775f90 | 997 | } |
86aebcda | 998 | |
999 | #if USE_HTCP | |
cb835136 AR |
1000 | void |
1001 | PeerSelector::handleHtcpReply(CachePeer *p, const peer_t type, HtcpReplyData *htcp) | |
86aebcda | 1002 | { |
cb835136 AR |
1003 | debugs(44, 3, (htcp->hit ? "HIT" : "MISS") << ' ' << url()); |
1004 | ++ping.n_recv; | |
62e76326 | 1005 | |
44e237d0 | 1006 | if (htcp->hit) { |
cb835136 AR |
1007 | hit = p; |
1008 | hit_type = type; | |
1009 | selectMore(); | |
62e76326 | 1010 | return; |
44e237d0 | 1011 | } |
62e76326 | 1012 | |
44e237d0 | 1013 | if (type == PEER_PARENT) |
cb835136 | 1014 | handleHtcpParentMiss(p, htcp); |
62e76326 | 1015 | |
cb835136 | 1016 | if (ping.n_recv < ping.n_replies_expected) |
62e76326 | 1017 | return; |
1018 | ||
cb835136 | 1019 | selectMore(); |
44e237d0 | 1020 | } |
1021 | ||
cb835136 AR |
1022 | void |
1023 | PeerSelector::handleHtcpParentMiss(CachePeer *p, HtcpReplyData *htcp) | |
44e237d0 | 1024 | { |
1025 | int rtt; | |
62e76326 | 1026 | |
9b5c4a9a | 1027 | #if USE_ICMP |
44e237d0 | 1028 | if (Config.onoff.query_icmp) { |
62e76326 | 1029 | if (htcp->cto.rtt > 0) { |
1030 | rtt = (int) htcp->cto.rtt * 1000; | |
9b5c4a9a | 1031 | int hops = (int) htcp->cto.hops * 1000; |
cb835136 | 1032 | netdbUpdatePeer(request->url, p, rtt, hops); |
62e76326 | 1033 | |
cb835136 AR |
1034 | if (rtt && (ping.p_rtt == 0 || rtt < ping.p_rtt)) { |
1035 | closest_parent_miss = p->in_addr; | |
1036 | ping.p_rtt = rtt; | |
62e76326 | 1037 | } |
1038 | } | |
44e237d0 | 1039 | } |
8b082ed9 FC |
1040 | #else |
1041 | (void)htcp; | |
9b5c4a9a | 1042 | #endif /* USE_ICMP */ |
62e76326 | 1043 | |
44e237d0 | 1044 | /* if closest-only is set, then don't allow FIRST_PARENT_MISS */ |
cd196bc8 | 1045 | if (p->options.closest_only) |
62e76326 | 1046 | return; |
1047 | ||
44e237d0 | 1048 | /* set FIRST_MISS if there is no CLOSEST parent */ |
cb835136 | 1049 | if (!closest_parent_miss.isAnyAddr()) |
62e76326 | 1050 | return; |
1051 | ||
cb835136 | 1052 | rtt = (tvSubMsec(ping.start, current_time) - p->basetime) / p->weight; |
62e76326 | 1053 | |
d1b63fc8 | 1054 | if (rtt < 1) |
62e76326 | 1055 | rtt = 1; |
1056 | ||
cb835136 AR |
1057 | if (first_parent_miss.isAnyAddr() || rtt < ping.w_rtt) { |
1058 | first_parent_miss = p->in_addr; | |
1059 | ping.w_rtt = rtt; | |
44e237d0 | 1060 | } |
86aebcda | 1061 | } |
62e76326 | 1062 | |
86aebcda | 1063 | #endif |
1064 | ||
cb835136 AR |
1065 | void |
1066 | PeerSelector::HandlePingReply(CachePeer * p, peer_t type, AnyP::ProtocolType proto, void *pingdata, void *data) | |
86aebcda | 1067 | { |
0c3d3f65 | 1068 | if (proto == AnyP::PROTO_ICP) |
cb835136 | 1069 | static_cast<PeerSelector*>(data)->handleIcpReply(p, type, static_cast<icp_common_t*>(pingdata)); |
62e76326 | 1070 | |
86aebcda | 1071 | #if USE_HTCP |
62e76326 | 1072 | |
0c3d3f65 | 1073 | else if (proto == AnyP::PROTO_HTCP) |
cb835136 | 1074 | static_cast<PeerSelector*>(data)->handleHtcpReply(p, type, static_cast<HtcpReplyData*>(pingdata)); |
62e76326 | 1075 | |
86aebcda | 1076 | #endif |
62e76326 | 1077 | |
86aebcda | 1078 | else |
cb835136 | 1079 | debugs(44, DBG_IMPORTANT, "ERROR: ignoring an ICP reply with unknown protocol " << proto); |
86aebcda | 1080 | } |
db1cd23c | 1081 | |
cb835136 AR |
1082 | void |
1083 | PeerSelector::addSelection(CachePeer *peer, const hier_code code) | |
db1cd23c | 1084 | { |
e7b08eec | 1085 | // Find the end of the servers list. Bail on a duplicate destination. |
cb835136 AR |
1086 | auto **serversTail = &servers; |
1087 | while (const auto server = *serversTail) { | |
e7b08eec AR |
1088 | // There can be at most one PINNED destination. |
1089 | // Non-PINNED destinations are uniquely identified by their CachePeer | |
1090 | // (even though a DIRECT destination might match a cache_peer address). | |
9b7992d9 EB |
1091 | // TODO: We may still add duplicates because the same peer could have |
1092 | // been removed from `servers` already (and given to the requestor). | |
e7b08eec | 1093 | const bool duplicate = (server->code == PINNED) ? |
cb835136 | 1094 | (code == PINNED) : (server->_peer == peer); |
e7b08eec | 1095 | if (duplicate) { |
cb835136 AR |
1096 | debugs(44, 3, "skipping " << PeerSelectionDumper(this, peer, code) << |
1097 | "; have " << PeerSelectionDumper(this, server->_peer.get(), server->code)); | |
e7b08eec AR |
1098 | return; |
1099 | } | |
cb835136 | 1100 | serversTail = &server->next; |
e7b08eec | 1101 | } |
62e76326 | 1102 | |
cb835136 AR |
1103 | debugs(44, 3, "adding " << PeerSelectionDumper(this, peer, code)); |
1104 | *serversTail = new FwdServer(peer, code); | |
db1cd23c | 1105 | } |
b24880fe | 1106 | |
cb835136 | 1107 | PeerSelector::PeerSelector(PeerSelectionInitiator *initiator): |
6043e368 | 1108 | request(nullptr), |
f53969cc SM |
1109 | entry (NULL), |
1110 | always_direct(Config.accessList.AlwaysDirect?ACCESS_DUNNO:ACCESS_DENIED), | |
1111 | never_direct(Config.accessList.NeverDirect?ACCESS_DUNNO:ACCESS_DENIED), | |
1112 | direct(DIRECT_UNKNOWN), | |
f53969cc SM |
1113 | lastError(NULL), |
1114 | servers (NULL), | |
1115 | first_parent_miss(), | |
1116 | closest_parent_miss(), | |
1117 | hit(NULL), | |
1118 | hit_type(PEER_NONE), | |
6043e368 AR |
1119 | acl_checklist (NULL), |
1120 | initiator_(initiator) | |
b24880fe | 1121 | { |
cc192b50 | 1122 | ; // no local defaults. |
b24880fe | 1123 | } |
1124 | ||
851feda6 | 1125 | const SBuf |
cb835136 | 1126 | PeerSelector::url() const |
769c64cc AJ |
1127 | { |
1128 | if (entry) | |
851feda6 | 1129 | return SBuf(entry->url()); |
769c64cc AJ |
1130 | |
1131 | if (request) | |
851feda6 | 1132 | return request->effectiveRequestUri(); |
769c64cc | 1133 | |
851feda6 AJ |
1134 | static const SBuf noUrl("[no URL]"); |
1135 | return noUrl; | |
769c64cc AJ |
1136 | } |
1137 | ||
6043e368 AR |
1138 | /// \returns valid/interested peer initiator or nil |
1139 | PeerSelectionInitiator * | |
cb835136 | 1140 | PeerSelector::interestedInitiator() |
6043e368 AR |
1141 | { |
1142 | const auto initiator = initiator_.valid(); | |
1143 | ||
1144 | if (!initiator) { | |
1145 | debugs(44, 3, id << " initiator gone"); | |
1146 | return nullptr; | |
1147 | } | |
1148 | ||
1149 | if (!initiator->subscribed) { | |
1150 | debugs(44, 3, id << " initiator lost interest"); | |
1151 | return nullptr; | |
1152 | } | |
1153 | ||
fd9c47d1 | 1154 | debugs(44, 7, id); |
6043e368 AR |
1155 | return initiator; |
1156 | } | |
1157 | ||
1158 | bool | |
cb835136 | 1159 | PeerSelector::wantsMoreDestinations() const { |
6043e368 | 1160 | const auto maxCount = Config.forward_max_tries; |
8b082ed9 | 1161 | return maxCount >= 0 && foundPaths < static_cast<size_t>(maxCount); |
6043e368 AR |
1162 | } |
1163 | ||
1164 | void | |
3dde9e52 | 1165 | PeerSelector::handlePath(const Comm::ConnectionPointer &path, FwdServer &fs) |
6043e368 AR |
1166 | { |
1167 | ++foundPaths; | |
1168 | ||
3dde9e52 CT |
1169 | if (path) { |
1170 | path->peerType = fs.code; | |
1171 | path->setPeer(fs._peer.get()); | |
6043e368 | 1172 | |
3dde9e52 CT |
1173 | // check for a configured outgoing address for this destination... |
1174 | getOutgoingAddress(request, path); | |
1175 | debugs(44, 2, id << " found " << path << ", destination #" << foundPaths << " for " << url()); | |
1176 | } else | |
1177 | debugs(44, 2, id << " found pinned, destination #" << foundPaths << " for " << url()); | |
6043e368 AR |
1178 | |
1179 | request->hier.ping = ping; // may be updated later | |
1180 | ||
6043e368 AR |
1181 | debugs(44, 2, " always_direct = " << always_direct); |
1182 | debugs(44, 2, " never_direct = " << never_direct); | |
1183 | debugs(44, 2, " timedout = " << ping.timedout); | |
1184 | ||
1185 | if (const auto initiator = interestedInitiator()) | |
1186 | initiator->noteDestination(path); | |
1187 | } | |
1188 | ||
cb835136 | 1189 | InstanceIdDefinitions(PeerSelector, "PeerSelector"); |
6043e368 | 1190 | |
b24880fe | 1191 | ping_data::ping_data() : |
f53969cc SM |
1192 | n_sent(0), |
1193 | n_recv(0), | |
1194 | n_replies_expected(0), | |
1195 | timeout(0), | |
1196 | timedout(0), | |
1197 | w_rtt(0), | |
9865de72 EB |
1198 | p_rtt(0), |
1199 | monitorRegistration(PingMonitor().npos()) | |
b24880fe | 1200 | { |
1201 | start.tv_sec = 0; | |
1202 | start.tv_usec = 0; | |
1203 | stop.tv_sec = 0; | |
1204 | stop.tv_usec = 0; | |
1205 | } | |
f53969cc | 1206 | |
9865de72 EB |
1207 | timeval |
1208 | ping_data::deadline() const | |
1209 | { | |
1210 | timeval timeInterval; | |
1211 | timeInterval.tv_sec = timeout / 1000; | |
1212 | timeInterval.tv_usec = (timeout % 1000) * 1000; | |
1213 | ||
1214 | timeval result; | |
1215 | tvAdd(result, start, timeInterval); | |
1216 | return result; | |
1217 | } | |
1218 |