2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 93 ICAP (RFC 3507) Client */
12 #include "adaptation/Answer.h"
13 #include "adaptation/icap/Config.h"
14 #include "adaptation/icap/ModXact.h"
15 #include "adaptation/icap/Options.h"
16 #include "adaptation/icap/OptXact.h"
17 #include "adaptation/icap/ServiceRep.h"
18 #include "base/TextException.h"
19 #include "comm/Connection.h"
20 #include "ConfigParser.h"
24 #include "HttpReply.h"
26 #include "SquidConfig.h"
27 #include "SquidTime.h"
29 #define DEFAULT_ICAP_PORT 1344
30 #define DEFAULT_ICAPS_PORT 11344
32 CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap
, ServiceRep
);
34 Adaptation::Icap::ServiceRep::ServiceRep(const ServiceConfigPointer
&svcCfg
):
35 AsyncJob("Adaptation::Icap::ServiceRep"), Adaptation::Service(svcCfg
),
36 theOptions(NULL
), theOptionsFetcher(0), theLastUpdate(0),
39 connOverloadReported(false),
41 isSuspended(0), notifying(false),
42 updateScheduled(false),
43 wasAnnouncedUp(true), // do not announce an "up" service at startup
47 theIdleConns
= new IdleConnList("ICAP Service", NULL
);
50 Adaptation::Icap::ServiceRep::~ServiceRep()
54 Must(!theOptionsFetcher
);
60 Adaptation::Icap::ServiceRep::finalize()
62 Adaptation::Service::finalize();
64 // use /etc/services or default port if needed
65 const bool have_port
= cfg().port
>= 0;
68 if (cfg().protocol
.caseCmp("icaps") == 0)
69 serv
= getservbyname("icaps", "tcp");
71 serv
= getservbyname("icap", "tcp");
74 writeableCfg().port
= htons(serv
->s_port
);
76 writeableCfg().port
= cfg().protocol
.caseCmp("icaps") == 0 ? DEFAULT_ICAPS_PORT
: DEFAULT_ICAP_PORT
;
80 if (cfg().protocol
.caseCmp("icaps") == 0)
81 writeableCfg().secure
.encryptTransport
= true;
83 if (cfg().secure
.encryptTransport
) {
84 debugs(3, DBG_IMPORTANT
, "Initializing service " << cfg().resource
<< " SSL context");
85 sslContext
= writeableCfg().secure
.createClientContext(true);
88 if (!cfg().connectionEncryption
.configured())
89 writeableCfg().connectionEncryption
.defaultTo(cfg().secure
.encryptTransport
);
91 theSessionFailures
.configure(TheConfig
.oldest_service_failure
> 0 ?
92 TheConfig
.oldest_service_failure
: -1);
95 void Adaptation::Icap::ServiceRep::noteFailure()
97 const int failures
= theSessionFailures
.count(1);
98 debugs(93,4, HERE
<< " failure " << failures
<< " out of " <<
99 TheConfig
.service_failure_limit
<< " allowed in " <<
100 TheConfig
.oldest_service_failure
<< "sec " << status());
105 if (TheConfig
.service_failure_limit
>= 0 &&
106 failures
> TheConfig
.service_failure_limit
)
107 suspend("too many failures");
109 // TODO: Should bypass setting affect how much Squid tries to talk to
110 // the ICAP service that is currently unusable and is likely to remain
111 // so for some time? The current code says "no". Perhaps the answer
112 // should be configurable.
115 // returns a persistent or brand new connection; negative int on failures
116 Comm::ConnectionPointer
117 Adaptation::Icap::ServiceRep::getConnection(bool retriableXact
, bool &reused
)
119 Comm::ConnectionPointer connection
;
121 /* 2011-06-17: rousskov:
122 * There are two things that happen at the same time in pop(). Both are important.
123 * 1) Ensure that we can use a pconn for this transaction.
124 * 2) Ensure that the number of idle pconns does not grow without bounds.
126 * Both happen in the beginning of the transaction. Both are dictated by real-world problems.
127 * retriable means you can repeat the request if you suspect the first try failed due to a pconn race.
128 * HTTP and ICAP rules prohibit the use of pconns for non-retriable requests.
130 * If there are zero idle connections, (2) is irrelevant. (2) is only relevant when there are many
131 * idle connections and we should not open more connections without closing some idle ones,
132 * or instead of just opening a new connection and leaving idle connections as is.
133 * In other words, (2) tells us to close one FD for each new one we open due to retriable.
136 connection
= theIdleConns
->pop();
138 theIdleConns
->closeN(1);
140 reused
= Comm::IsConnOpen(connection
);
142 debugs(93,3, HERE
<< "got connection: " << connection
);
146 // pools connection if it is reusable or closes it
147 void Adaptation::Icap::ServiceRep::putConnection(const Comm::ConnectionPointer
&conn
, bool isReusable
, bool sendReset
, const char *comment
)
149 Must(Comm::IsConnOpen(conn
));
150 // do not pool an idle connection if we owe connections
151 if (isReusable
&& excessConnections() == 0) {
152 debugs(93, 3, HERE
<< "pushing pconn" << comment
);
153 commUnsetConnTimeout(conn
);
154 theIdleConns
->push(conn
);
156 debugs(93, 3, HERE
<< (sendReset
? "RST" : "FIN") << "-closing " <<
158 // comm_close called from Connection::close will clear timeout
159 // TODO: add "bool sendReset = false" to Connection::close()?
161 comm_reset_close(conn
);
166 Must(theBusyConns
> 0);
168 // a connection slot released. Check if there are waiters....
172 // a wrapper to avoid exposing theIdleConns
173 void Adaptation::Icap::ServiceRep::noteConnectionUse(const Comm::ConnectionPointer
&conn
)
175 Must(Comm::IsConnOpen(conn
));
176 fd_table
[conn
->fd
].noteUse(); // pconn re-use, albeit not via PconnPool API
179 void Adaptation::Icap::ServiceRep::noteConnectionFailed(const char *comment
)
181 debugs(93, 3, HERE
<< "Connection failed: " << comment
);
185 void Adaptation::Icap::ServiceRep::setMaxConnections()
187 if (cfg().maxConn
>= 0)
188 theMaxConnections
= cfg().maxConn
;
189 else if (theOptions
&& theOptions
->max_connections
>= 0)
190 theMaxConnections
= theOptions
->max_connections
;
192 theMaxConnections
= -1;
196 if (::Config
.workers
> 1 )
197 theMaxConnections
/= ::Config
.workers
;
200 int Adaptation::Icap::ServiceRep::availableConnections() const
202 if (theMaxConnections
< 0)
205 // we are available if we can open or reuse connections
206 // in other words, if we will not create debt
207 int available
= max(0, theMaxConnections
- theBusyConns
);
209 if (!available
&& !connOverloadReported
) {
210 debugs(93, DBG_IMPORTANT
, "WARNING: ICAP Max-Connections limit " <<
211 "exceeded for service " << cfg().uri
<< ". Open connections now: " <<
212 theBusyConns
+ theIdleConns
->count() << ", including " <<
213 theIdleConns
->count() << " idle persistent connections.");
214 connOverloadReported
= true;
217 if (cfg().onOverload
== srvForce
)
223 // The number of connections which excess the Max-Connections limit
224 int Adaptation::Icap::ServiceRep::excessConnections() const
226 if (theMaxConnections
< 0)
229 // Waiters affect the number of needed connections but a needed
230 // connection may still be excessive from Max-Connections p.o.v.
231 // so we should not account for waiting transaction needs here.
232 const int debt
= theBusyConns
+ theIdleConns
->count() - theMaxConnections
;
239 void Adaptation::Icap::ServiceRep::noteGoneWaiter()
243 // in case the notified transaction did not take the connection slot
247 // called when a connection slot may become available
248 void Adaptation::Icap::ServiceRep::busyCheckpoint()
250 if (theNotificationWaiters
.empty()) // nobody is waiting for a slot
254 int available
= availableConnections();
257 // It is possible to have waiters when no limit on connections exist in
258 // case of reconfigure or because new Options received.
259 // In this case, notify all waiting transactions.
260 freed
= theNotificationWaiters
.size();
262 // avoid notifying more waiters than there will be available slots
263 const int notifiedWaiters
= theAllWaiters
- theNotificationWaiters
.size();
264 freed
= available
- notifiedWaiters
;
267 debugs(93,7, HERE
<< "Available connections: " << available
<<
268 " freed slots: " << freed
<<
269 " waiting in queue: " << theNotificationWaiters
.size());
271 while (freed
> 0 && !theNotificationWaiters
.empty()) {
272 Client i
= theNotificationWaiters
.front();
273 theNotificationWaiters
.pop_front();
274 ScheduleCallHere(i
.callback
);
280 void Adaptation::Icap::ServiceRep::suspend(const char *reason
)
283 debugs(93,4, HERE
<< "keeping suspended, also for " << reason
);
285 isSuspended
= reason
;
286 debugs(93, DBG_IMPORTANT
, "suspending ICAP service for " << reason
);
287 scheduleUpdate(squid_curtime
+ TheConfig
.service_revival_delay
);
288 announceStatusChange("suspended", true);
292 bool Adaptation::Icap::ServiceRep::probed() const
294 return theLastUpdate
!= 0;
297 bool Adaptation::Icap::ServiceRep::hasOptions() const
299 return theOptions
&& theOptions
->valid() && theOptions
->fresh();
302 bool Adaptation::Icap::ServiceRep::up() const
304 return !isSuspended
&& hasOptions();
307 bool Adaptation::Icap::ServiceRep::availableForNew() const
310 int available
= availableConnections();
314 return (available
- theAllWaiters
> 0);
317 bool Adaptation::Icap::ServiceRep::availableForOld() const
321 int available
= availableConnections();
322 return (available
!= 0); // it is -1 (no limit) or has available slots
325 bool Adaptation::Icap::ServiceRep::wantsUrl(const SBuf
&urlPath
) const
328 return theOptions
->transferKind(urlPath
) != Adaptation::Icap::Options::xferIgnore
;
331 bool Adaptation::Icap::ServiceRep::wantsPreview(const SBuf
&urlPath
, size_t &wantedSize
) const
335 if (theOptions
->preview
< 0)
338 if (theOptions
->transferKind(urlPath
) != Adaptation::Icap::Options::xferPreview
)
341 wantedSize
= theOptions
->preview
;
346 bool Adaptation::Icap::ServiceRep::allows204() const
349 return true; // in the future, we may have ACLs to prevent 204s
352 bool Adaptation::Icap::ServiceRep::allows206() const
355 if (theOptions
->allow206
)
356 return true; // in the future, we may have ACLs to prevent 206s
361 void ServiceRep_noteTimeToUpdate(void *data
)
363 Adaptation::Icap::ServiceRep
*service
= static_cast<Adaptation::Icap::ServiceRep
*>(data
);
365 service
->noteTimeToUpdate();
368 void Adaptation::Icap::ServiceRep::noteTimeToUpdate()
371 updateScheduled
= false;
373 if (detached() || theOptionsFetcher
.set()) {
374 debugs(93,5, HERE
<< "ignores options update " << status());
378 debugs(93,5, HERE
<< "performs a regular options update " << status());
379 startGettingOptions();
384 void Adaptation::Icap::ServiceRep_noteTimeToNotify(void *data
)
386 Adaptation::Icap::ServiceRep
*service
= static_cast<Adaptation::Icap::ServiceRep
*>(data
);
388 service
->noteTimeToNotify();
392 void Adaptation::Icap::ServiceRep::noteTimeToNotify()
396 debugs(93,7, HERE
<< "notifies " << theClients
.size() << " clients " <<
399 // note: we must notify even if we are invalidated
403 while (!theClients
.empty()) {
404 Client i
= theClients
.back();
405 theClients
.pop_back();
406 ScheduleCallHere(i
.callback
);
413 void Adaptation::Icap::ServiceRep::callWhenAvailable(AsyncCall::Pointer
&cb
, bool priority
)
415 debugs(93,8, "ICAPServiceRep::callWhenAvailable");
418 Must(!theIdleConns
->count()); // or we should not be waiting
421 i
.service
= Pointer(this);
424 theNotificationWaiters
.push_front(i
);
426 theNotificationWaiters
.push_back(i
);
431 void Adaptation::Icap::ServiceRep::callWhenReady(AsyncCall::Pointer
&cb
)
435 debugs(93,5, HERE
<< "Adaptation::Icap::Service is asked to call " << *cb
<<
436 " when ready " << status());
438 Must(!broken()); // we do not wait for a broken service
441 i
.service
= Pointer(this); // TODO: is this really needed?
443 theClients
.push_back(i
);
445 if (theOptionsFetcher
.set() || notifying
)
446 return; // do nothing, we will be picked up in noteTimeToNotify()
448 if (needNewOptions())
449 startGettingOptions();
451 scheduleNotification();
454 void Adaptation::Icap::ServiceRep::scheduleNotification()
456 debugs(93,7, HERE
<< "will notify " << theClients
.size() << " clients");
457 CallJobHere(93, 5, this, Adaptation::Icap::ServiceRep
, noteTimeToNotify
);
460 bool Adaptation::Icap::ServiceRep::needNewOptions() const
462 return !detached() && !up();
465 void Adaptation::Icap::ServiceRep::changeOptions(Adaptation::Icap::Options
*newOptions
)
467 debugs(93,8, HERE
<< "changes options from " << theOptions
<< " to " <<
468 newOptions
<< ' ' << status());
471 theOptions
= newOptions
;
472 theSessionFailures
.clear();
474 theLastUpdate
= squid_curtime
;
477 announceStatusChange("down after an options fetch failure", true);
480 void Adaptation::Icap::ServiceRep::checkOptions()
482 if (theOptions
== NULL
)
485 if (!theOptions
->valid()) {
486 debugs(93, DBG_IMPORTANT
, "WARNING: Squid got an invalid ICAP OPTIONS response " <<
487 "from service " << cfg().uri
<< "; error: " << theOptions
->error
);
492 * Issue a warning if the ICAP server returned methods in the
493 * options response that don't match the method from squid.conf.
496 if (!theOptions
->methods
.empty()) {
497 bool method_found
= false;
499 std::vector
<ICAP::Method
>::iterator iter
= theOptions
->methods
.begin();
501 while (iter
!= theOptions
->methods
.end()) {
503 if (*iter
== cfg().method
) {
508 method_list
.append(ICAP::methodStr(*iter
));
509 method_list
.append(" ", 1);
514 debugs(93, DBG_IMPORTANT
, "WARNING: Squid is configured to use ICAP method " <<
516 " for service " << cfg().uri
<<
517 " but OPTIONS response declares the methods are " << method_list
);
522 * Check the ICAP server's date header for clock skew
524 const int skew
= (int)(theOptions
->timestamp() - squid_curtime
);
525 if (abs(skew
) > theOptions
->ttl()) {
526 // TODO: If skew is negative, the option will be considered down
527 // because of stale options. We should probably change this.
528 debugs(93, DBG_IMPORTANT
, "ICAP service's clock is skewed by " << skew
<<
529 " seconds: " << cfg().uri
);
533 void Adaptation::Icap::ServiceRep::announceStatusChange(const char *downPhrase
, bool important
) const
535 if (wasAnnouncedUp
== up()) // no significant changes to announce
538 const char *what
= cfg().bypass
? "optional" : "essential";
539 const char *state
= wasAnnouncedUp
? downPhrase
: "up";
540 const int level
= important
? 1 :2;
541 debugs(93,level
, what
<< " ICAP service is " << state
<< ": " <<
542 cfg().uri
<< ' ' << status());
544 wasAnnouncedUp
= !wasAnnouncedUp
;
547 // we are receiving ICAP OPTIONS response headers here or NULL on failures
548 void Adaptation::Icap::ServiceRep::noteAdaptationAnswer(const Answer
&answer
)
550 Must(initiated(theOptionsFetcher
));
551 clearAdaptation(theOptionsFetcher
);
553 if (answer
.kind
== Answer::akError
) {
554 debugs(93,3, HERE
<< "failed to fetch options " << status());
559 Must(answer
.kind
== Answer::akForward
); // no akBlock for OPTIONS requests
560 const Http::Message
*msg
= answer
.message
.getRaw();
563 debugs(93,5, HERE
<< "is interpreting new options " << status());
565 Adaptation::Icap::Options
*newOptions
= NULL
;
566 if (const HttpReply
*r
= dynamic_cast<const HttpReply
*>(msg
)) {
567 newOptions
= new Adaptation::Icap::Options
;
568 newOptions
->configure(r
);
570 debugs(93, DBG_IMPORTANT
, "ICAP service got wrong options message " << status());
573 handleNewOptions(newOptions
);
576 // we (a) must keep trying to get OPTIONS and (b) are RefCounted so we
577 // must keep our job alive (XXX: until nobody needs us)
578 void Adaptation::Icap::ServiceRep::callException(const std::exception
&e
)
580 clearAdaptation(theOptionsFetcher
);
581 debugs(93,2, "ICAP probably failed to fetch options (" << e
.what() <<
586 void Adaptation::Icap::ServiceRep::handleNewOptions(Adaptation::Icap::Options
*newOptions
)
588 // new options may be NULL
589 changeOptions(newOptions
);
591 debugs(93,3, HERE
<< "got new options and is now " << status());
593 scheduleUpdate(optionsFetchTime());
595 // XXX: this whole feature bases on the false assumption a service only has one IP
597 const int excess
= excessConnections();
598 // if we owe connections and have idle pconns, close the latter
599 if (excess
&& theIdleConns
->count() > 0) {
600 const int n
= min(excess
, theIdleConns
->count());
601 debugs(93,5, HERE
<< "closing " << n
<< " pconns to relief debt");
602 theIdleConns
->closeN(n
);
605 scheduleNotification();
608 void Adaptation::Icap::ServiceRep::startGettingOptions()
610 Must(!theOptionsFetcher
);
611 debugs(93,6, HERE
<< "will get new options " << status());
613 // XXX: "this" here is "self"; works until refcounting API changes
614 theOptionsFetcher
= initiateAdaptation(
615 new Adaptation::Icap::OptXactLauncher(this));
616 // TODO: timeout in case Adaptation::Icap::OptXact never calls us back?
617 // Such a timeout should probably be a generic AsyncStart feature.
620 void Adaptation::Icap::ServiceRep::scheduleUpdate(time_t when
)
622 if (updateScheduled
) {
623 debugs(93,7, HERE
<< "reschedules update");
624 // XXX: check whether the event is there because AR saw
625 // an unreproducible eventDelete assertion on 2007/06/18
626 if (eventFind(&ServiceRep_noteTimeToUpdate
, this))
627 eventDelete(&ServiceRep_noteTimeToUpdate
, this);
629 debugs(93, DBG_IMPORTANT
, "XXX: ICAP service lost an update event.");
630 updateScheduled
= false;
633 debugs(93,7, HERE
<< "raw OPTIONS fetch at " << when
<< " or in " <<
634 (when
- squid_curtime
) << " sec");
635 debugs(93,9, HERE
<< "last fetched at " << theLastUpdate
<< " or " <<
636 (squid_curtime
- theLastUpdate
) << " sec ago");
638 /* adjust update time to prevent too-frequent updates */
640 if (when
< squid_curtime
)
641 when
= squid_curtime
;
643 // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig
644 const int minUpdateGap
= 30; // seconds
645 if (when
< theLastUpdate
+ minUpdateGap
)
646 when
= theLastUpdate
+ minUpdateGap
;
648 const int delay
= when
- squid_curtime
;
649 debugs(93,5, HERE
<< "will fetch OPTIONS in " << delay
<< " sec");
651 eventAdd("Adaptation::Icap::ServiceRep::noteTimeToUpdate",
652 &ServiceRep_noteTimeToUpdate
, this, delay
, 0, true);
653 updateScheduled
= true;
656 // returns absolute time when OPTIONS should be fetched
658 Adaptation::Icap::ServiceRep::optionsFetchTime() const
660 if (theOptions
&& theOptions
->valid()) {
661 const time_t expire
= theOptions
->expire();
662 debugs(93,7, HERE
<< "options expire on " << expire
<< " >= " << squid_curtime
);
664 // conservative estimate of how long the OPTIONS transaction will take
665 // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig
666 const int expectedWait
= 20; // seconds
668 // Unknown or invalid (too small) expiration times should not happen.
669 // Adaptation::Icap::Options should use the default TTL, and ICAP servers should not
670 // send invalid TTLs, but bugs and attacks happen.
671 if (expire
< expectedWait
)
672 return squid_curtime
;
674 return expire
- expectedWait
; // before the current options expire
677 // use revival delay as "expiration" time for a service w/o valid options
678 return squid_curtime
+ TheConfig
.service_revival_delay
;
681 Adaptation::Initiate
*
682 Adaptation::Icap::ServiceRep::makeXactLauncher(Http::Message
*virgin
,
683 HttpRequest
*cause
, AccessLogEntry::Pointer
&alp
)
685 return new Adaptation::Icap::ModXactLauncher(virgin
, cause
, alp
, this);
688 // returns a temporary string depicting service status, for debugging
689 const char *Adaptation::Icap::ServiceRep::status() const
699 buf
.append("down", 4);
701 buf
.append(",susp", 5);
704 buf
.append(",!opt", 5);
705 else if (!theOptions
->valid())
706 buf
.append(",!valid", 7);
707 else if (!theOptions
->fresh())
708 buf
.append(",stale", 6);
712 buf
.append(",detached", 9);
714 if (theOptionsFetcher
.set())
715 buf
.append(",fetch", 6);
718 buf
.append(",notif", 6);
720 if (const int failures
= theSessionFailures
.remembered())
721 buf
.appendf(",fail%d", failures
);
726 return buf
.content();
729 void Adaptation::Icap::ServiceRep::detach()
731 debugs(93,3, HERE
<< "detaching ICAP service: " << cfg().uri
<<
736 bool Adaptation::Icap::ServiceRep::detached() const
741 Adaptation::Icap::ConnWaiterDialer::ConnWaiterDialer(const CbcPointer
<Adaptation::Icap::ModXact
> &xact
,
742 Adaptation::Icap::ConnWaiterDialer::Parent::Method aHandler
):
743 Parent(xact
, aHandler
)
745 theService
= &xact
->service();
746 theService
->noteNewWaiter();
749 Adaptation::Icap::ConnWaiterDialer::ConnWaiterDialer(const Adaptation::Icap::ConnWaiterDialer
&aConnWaiter
): Parent(aConnWaiter
)
751 theService
= aConnWaiter
.theService
;
752 theService
->noteNewWaiter();
755 Adaptation::Icap::ConnWaiterDialer::~ConnWaiterDialer()
757 theService
->noteGoneWaiter();