2 * Copyright (C) 1996-2016 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
),
40 theOptions(NULL
), theOptionsFetcher(0), theLastUpdate(0),
43 connOverloadReported(false),
45 isSuspended(0), notifying(false),
46 updateScheduled(false),
47 wasAnnouncedUp(true), // do not announce an "up" service at startup
51 theIdleConns
= new IdleConnList("ICAP Service", NULL
);
54 Adaptation::Icap::ServiceRep::~ServiceRep()
57 Must(!theOptionsFetcher
);
62 Adaptation::Icap::ServiceRep::finalize()
64 Adaptation::Service::finalize();
66 // use /etc/services or default port if needed
67 const bool have_port
= cfg().port
>= 0;
70 if (cfg().protocol
.caseCmp("icaps") == 0)
71 serv
= getservbyname("icaps", "tcp");
73 serv
= getservbyname("icap", "tcp");
76 writeableCfg().port
= htons(serv
->s_port
);
78 writeableCfg().port
= cfg().protocol
.caseCmp("icaps") == 0 ? DEFAULT_ICAPS_PORT
: DEFAULT_ICAP_PORT
;
82 if (cfg().protocol
.caseCmp("icaps") == 0)
83 writeableCfg().secure
.encryptTransport
= true;
85 if (cfg().secure
.encryptTransport
) {
86 debugs(3, DBG_IMPORTANT
, "Initializing service " << cfg().resource
<< " SSL context");
87 sslContext
= writeableCfg().secure
.createClientContext(true);
90 if (!cfg().connectionEncryption
.configured())
91 writeableCfg().connectionEncryption
.defaultTo(cfg().secure
.encryptTransport
);
93 theSessionFailures
.configure(TheConfig
.oldest_service_failure
> 0 ?
94 TheConfig
.oldest_service_failure
: -1);
97 void Adaptation::Icap::ServiceRep::noteFailure()
99 const int failures
= theSessionFailures
.count(1);
100 debugs(93,4, HERE
<< " failure " << failures
<< " out of " <<
101 TheConfig
.service_failure_limit
<< " allowed in " <<
102 TheConfig
.oldest_service_failure
<< "sec " << status());
107 if (TheConfig
.service_failure_limit
>= 0 &&
108 failures
> TheConfig
.service_failure_limit
)
109 suspend("too many failures");
111 // TODO: Should bypass setting affect how much Squid tries to talk to
112 // the ICAP service that is currently unusable and is likely to remain
113 // so for some time? The current code says "no". Perhaps the answer
114 // should be configurable.
117 // returns a persistent or brand new connection; negative int on failures
118 Comm::ConnectionPointer
119 Adaptation::Icap::ServiceRep::getConnection(bool retriableXact
, bool &reused
)
121 Comm::ConnectionPointer connection
;
123 /* 2011-06-17: rousskov:
124 * There are two things that happen at the same time in pop(). Both are important.
125 * 1) Ensure that we can use a pconn for this transaction.
126 * 2) Ensure that the number of idle pconns does not grow without bounds.
128 * Both happen in the beginning of the transaction. Both are dictated by real-world problems.
129 * retriable means you can repeat the request if you suspect the first try failed due to a pconn race.
130 * HTTP and ICAP rules prohibit the use of pconns for non-retriable requests.
132 * If there are zero idle connections, (2) is irrelevant. (2) is only relevant when there are many
133 * idle connections and we should not open more connections without closing some idle ones,
134 * or instead of just opening a new connection and leaving idle connections as is.
135 * In other words, (2) tells us to close one FD for each new one we open due to retriable.
138 connection
= theIdleConns
->pop();
140 theIdleConns
->closeN(1);
142 reused
= Comm::IsConnOpen(connection
);
144 debugs(93,3, HERE
<< "got connection: " << connection
);
148 // pools connection if it is reusable or closes it
149 void Adaptation::Icap::ServiceRep::putConnection(const Comm::ConnectionPointer
&conn
, bool isReusable
, bool sendReset
, const char *comment
)
151 Must(Comm::IsConnOpen(conn
));
152 // do not pool an idle connection if we owe connections
153 if (isReusable
&& excessConnections() == 0) {
154 debugs(93, 3, HERE
<< "pushing pconn" << comment
);
155 commUnsetConnTimeout(conn
);
156 theIdleConns
->push(conn
);
158 debugs(93, 3, HERE
<< (sendReset
? "RST" : "FIN") << "-closing " <<
160 // comm_close called from Connection::close will clear timeout
161 // TODO: add "bool sendReset = false" to Connection::close()?
163 comm_reset_close(conn
);
168 Must(theBusyConns
> 0);
170 // a connection slot released. Check if there are waiters....
174 // a wrapper to avoid exposing theIdleConns
175 void Adaptation::Icap::ServiceRep::noteConnectionUse(const Comm::ConnectionPointer
&conn
)
177 Must(Comm::IsConnOpen(conn
));
178 fd_table
[conn
->fd
].noteUse(); // pconn re-use, albeit not via PconnPool API
181 void Adaptation::Icap::ServiceRep::noteConnectionFailed(const char *comment
)
183 debugs(93, 3, HERE
<< "Connection failed: " << comment
);
187 void Adaptation::Icap::ServiceRep::setMaxConnections()
189 if (cfg().maxConn
>= 0)
190 theMaxConnections
= cfg().maxConn
;
191 else if (theOptions
&& theOptions
->max_connections
>= 0)
192 theMaxConnections
= theOptions
->max_connections
;
194 theMaxConnections
= -1;
198 if (::Config
.workers
> 1 )
199 theMaxConnections
/= ::Config
.workers
;
202 int Adaptation::Icap::ServiceRep::availableConnections() const
204 if (theMaxConnections
< 0)
207 // we are available if we can open or reuse connections
208 // in other words, if we will not create debt
209 int available
= max(0, theMaxConnections
- theBusyConns
);
211 if (!available
&& !connOverloadReported
) {
212 debugs(93, DBG_IMPORTANT
, "WARNING: ICAP Max-Connections limit " <<
213 "exceeded for service " << cfg().uri
<< ". Open connections now: " <<
214 theBusyConns
+ theIdleConns
->count() << ", including " <<
215 theIdleConns
->count() << " idle persistent connections.");
216 connOverloadReported
= true;
219 if (cfg().onOverload
== srvForce
)
225 // The number of connections which excess the Max-Connections limit
226 int Adaptation::Icap::ServiceRep::excessConnections() const
228 if (theMaxConnections
< 0)
231 // Waiters affect the number of needed connections but a needed
232 // connection may still be excessive from Max-Connections p.o.v.
233 // so we should not account for waiting transaction needs here.
234 const int debt
= theBusyConns
+ theIdleConns
->count() - theMaxConnections
;
241 void Adaptation::Icap::ServiceRep::noteGoneWaiter()
245 // in case the notified transaction did not take the connection slot
249 // called when a connection slot may become available
250 void Adaptation::Icap::ServiceRep::busyCheckpoint()
252 if (theNotificationWaiters
.empty()) // nobody is waiting for a slot
256 int available
= availableConnections();
259 // It is possible to have waiters when no limit on connections exist in
260 // case of reconfigure or because new Options received.
261 // In this case, notify all waiting transactions.
262 freed
= theNotificationWaiters
.size();
264 // avoid notifying more waiters than there will be available slots
265 const int notifiedWaiters
= theAllWaiters
- theNotificationWaiters
.size();
266 freed
= available
- notifiedWaiters
;
269 debugs(93,7, HERE
<< "Available connections: " << available
<<
270 " freed slots: " << freed
<<
271 " waiting in queue: " << theNotificationWaiters
.size());
273 while (freed
> 0 && !theNotificationWaiters
.empty()) {
274 Client i
= theNotificationWaiters
.front();
275 theNotificationWaiters
.pop_front();
276 ScheduleCallHere(i
.callback
);
282 void Adaptation::Icap::ServiceRep::suspend(const char *reason
)
285 debugs(93,4, HERE
<< "keeping suspended, also for " << reason
);
287 isSuspended
= reason
;
288 debugs(93, DBG_IMPORTANT
, "suspending ICAP service for " << reason
);
289 scheduleUpdate(squid_curtime
+ TheConfig
.service_revival_delay
);
290 announceStatusChange("suspended", true);
294 bool Adaptation::Icap::ServiceRep::probed() const
296 return theLastUpdate
!= 0;
299 bool Adaptation::Icap::ServiceRep::hasOptions() const
301 return theOptions
&& theOptions
->valid() && theOptions
->fresh();
304 bool Adaptation::Icap::ServiceRep::up() const
306 return !isSuspended
&& hasOptions();
309 bool Adaptation::Icap::ServiceRep::availableForNew() const
312 int available
= availableConnections();
316 return (available
- theAllWaiters
> 0);
319 bool Adaptation::Icap::ServiceRep::availableForOld() const
323 int available
= availableConnections();
324 return (available
!= 0); // it is -1 (no limit) or has available slots
327 bool Adaptation::Icap::ServiceRep::wantsUrl(const SBuf
&urlPath
) const
330 return theOptions
->transferKind(urlPath
) != Adaptation::Icap::Options::xferIgnore
;
333 bool Adaptation::Icap::ServiceRep::wantsPreview(const SBuf
&urlPath
, size_t &wantedSize
) const
337 if (theOptions
->preview
< 0)
340 if (theOptions
->transferKind(urlPath
) != Adaptation::Icap::Options::xferPreview
)
343 wantedSize
= theOptions
->preview
;
348 bool Adaptation::Icap::ServiceRep::allows204() const
351 return true; // in the future, we may have ACLs to prevent 204s
354 bool Adaptation::Icap::ServiceRep::allows206() const
357 if (theOptions
->allow206
)
358 return true; // in the future, we may have ACLs to prevent 206s
363 void ServiceRep_noteTimeToUpdate(void *data
)
365 Adaptation::Icap::ServiceRep
*service
= static_cast<Adaptation::Icap::ServiceRep
*>(data
);
367 service
->noteTimeToUpdate();
370 void Adaptation::Icap::ServiceRep::noteTimeToUpdate()
373 updateScheduled
= false;
375 if (detached() || theOptionsFetcher
.set()) {
376 debugs(93,5, HERE
<< "ignores options update " << status());
380 debugs(93,5, HERE
<< "performs a regular options update " << status());
381 startGettingOptions();
386 void Adaptation::Icap::ServiceRep_noteTimeToNotify(void *data
)
388 Adaptation::Icap::ServiceRep
*service
= static_cast<Adaptation::Icap::ServiceRep
*>(data
);
390 service
->noteTimeToNotify();
394 void Adaptation::Icap::ServiceRep::noteTimeToNotify()
398 debugs(93,7, HERE
<< "notifies " << theClients
.size() << " clients " <<
401 // note: we must notify even if we are invalidated
405 while (!theClients
.empty()) {
406 Client i
= theClients
.back();
407 theClients
.pop_back();
408 ScheduleCallHere(i
.callback
);
415 void Adaptation::Icap::ServiceRep::callWhenAvailable(AsyncCall::Pointer
&cb
, bool priority
)
417 debugs(93,8, "ICAPServiceRep::callWhenAvailable");
420 Must(!theIdleConns
->count()); // or we should not be waiting
423 i
.service
= Pointer(this);
426 theNotificationWaiters
.push_front(i
);
428 theNotificationWaiters
.push_back(i
);
433 void Adaptation::Icap::ServiceRep::callWhenReady(AsyncCall::Pointer
&cb
)
437 debugs(93,5, HERE
<< "Adaptation::Icap::Service is asked to call " << *cb
<<
438 " when ready " << status());
440 Must(!broken()); // we do not wait for a broken service
443 i
.service
= Pointer(this); // TODO: is this really needed?
445 theClients
.push_back(i
);
447 if (theOptionsFetcher
.set() || notifying
)
448 return; // do nothing, we will be picked up in noteTimeToNotify()
450 if (needNewOptions())
451 startGettingOptions();
453 scheduleNotification();
456 void Adaptation::Icap::ServiceRep::scheduleNotification()
458 debugs(93,7, HERE
<< "will notify " << theClients
.size() << " clients");
459 CallJobHere(93, 5, this, Adaptation::Icap::ServiceRep
, noteTimeToNotify
);
462 bool Adaptation::Icap::ServiceRep::needNewOptions() const
464 return !detached() && !up();
467 void Adaptation::Icap::ServiceRep::changeOptions(Adaptation::Icap::Options
*newOptions
)
469 debugs(93,8, HERE
<< "changes options from " << theOptions
<< " to " <<
470 newOptions
<< ' ' << status());
473 theOptions
= newOptions
;
474 theSessionFailures
.clear();
476 theLastUpdate
= squid_curtime
;
479 announceStatusChange("down after an options fetch failure", true);
482 void Adaptation::Icap::ServiceRep::checkOptions()
484 if (theOptions
== NULL
)
487 if (!theOptions
->valid()) {
488 debugs(93, DBG_IMPORTANT
, "WARNING: Squid got an invalid ICAP OPTIONS response " <<
489 "from service " << cfg().uri
<< "; error: " << theOptions
->error
);
494 * Issue a warning if the ICAP server returned methods in the
495 * options response that don't match the method from squid.conf.
498 if (!theOptions
->methods
.empty()) {
499 bool method_found
= false;
501 std::vector
<ICAP::Method
>::iterator iter
= theOptions
->methods
.begin();
503 while (iter
!= theOptions
->methods
.end()) {
505 if (*iter
== cfg().method
) {
510 method_list
.append(ICAP::methodStr(*iter
));
511 method_list
.append(" ", 1);
516 debugs(93, DBG_IMPORTANT
, "WARNING: Squid is configured to use ICAP method " <<
518 " for service " << cfg().uri
<<
519 " but OPTIONS response declares the methods are " << method_list
);
524 * Check the ICAP server's date header for clock skew
526 const int skew
= (int)(theOptions
->timestamp() - squid_curtime
);
527 if (abs(skew
) > theOptions
->ttl()) {
528 // TODO: If skew is negative, the option will be considered down
529 // because of stale options. We should probably change this.
530 debugs(93, DBG_IMPORTANT
, "ICAP service's clock is skewed by " << skew
<<
531 " seconds: " << cfg().uri
);
535 void Adaptation::Icap::ServiceRep::announceStatusChange(const char *downPhrase
, bool important
) const
537 if (wasAnnouncedUp
== up()) // no significant changes to announce
540 const char *what
= cfg().bypass
? "optional" : "essential";
541 const char *state
= wasAnnouncedUp
? downPhrase
: "up";
542 const int level
= important
? 1 :2;
543 debugs(93,level
, what
<< " ICAP service is " << state
<< ": " <<
544 cfg().uri
<< ' ' << status());
546 wasAnnouncedUp
= !wasAnnouncedUp
;
549 // we are receiving ICAP OPTIONS response headers here or NULL on failures
550 void Adaptation::Icap::ServiceRep::noteAdaptationAnswer(const Answer
&answer
)
552 Must(initiated(theOptionsFetcher
));
553 clearAdaptation(theOptionsFetcher
);
555 if (answer
.kind
== Answer::akError
) {
556 debugs(93,3, HERE
<< "failed to fetch options " << status());
561 Must(answer
.kind
== Answer::akForward
); // no akBlock for OPTIONS requests
562 const HttpMsg
*msg
= answer
.message
.getRaw();
565 debugs(93,5, HERE
<< "is interpreting new options " << status());
567 Adaptation::Icap::Options
*newOptions
= NULL
;
568 if (const HttpReply
*r
= dynamic_cast<const HttpReply
*>(msg
)) {
569 newOptions
= new Adaptation::Icap::Options
;
570 newOptions
->configure(r
);
572 debugs(93, DBG_IMPORTANT
, "ICAP service got wrong options message " << status());
575 handleNewOptions(newOptions
);
578 // we (a) must keep trying to get OPTIONS and (b) are RefCounted so we
579 // must keep our job alive (XXX: until nobody needs us)
580 void Adaptation::Icap::ServiceRep::callException(const std::exception
&e
)
582 clearAdaptation(theOptionsFetcher
);
583 debugs(93,2, "ICAP probably failed to fetch options (" << e
.what() <<
588 void Adaptation::Icap::ServiceRep::handleNewOptions(Adaptation::Icap::Options
*newOptions
)
590 // new options may be NULL
591 changeOptions(newOptions
);
593 debugs(93,3, HERE
<< "got new options and is now " << status());
595 scheduleUpdate(optionsFetchTime());
597 // XXX: this whole feature bases on the false assumption a service only has one IP
599 const int excess
= excessConnections();
600 // if we owe connections and have idle pconns, close the latter
601 if (excess
&& theIdleConns
->count() > 0) {
602 const int n
= min(excess
, theIdleConns
->count());
603 debugs(93,5, HERE
<< "closing " << n
<< " pconns to relief debt");
604 theIdleConns
->closeN(n
);
607 scheduleNotification();
610 void Adaptation::Icap::ServiceRep::startGettingOptions()
612 Must(!theOptionsFetcher
);
613 debugs(93,6, HERE
<< "will get new options " << status());
615 // XXX: "this" here is "self"; works until refcounting API changes
616 theOptionsFetcher
= initiateAdaptation(
617 new Adaptation::Icap::OptXactLauncher(this));
618 // TODO: timeout in case Adaptation::Icap::OptXact never calls us back?
619 // Such a timeout should probably be a generic AsyncStart feature.
622 void Adaptation::Icap::ServiceRep::scheduleUpdate(time_t when
)
624 if (updateScheduled
) {
625 debugs(93,7, HERE
<< "reschedules update");
626 // XXX: check whether the event is there because AR saw
627 // an unreproducible eventDelete assertion on 2007/06/18
628 if (eventFind(&ServiceRep_noteTimeToUpdate
, this))
629 eventDelete(&ServiceRep_noteTimeToUpdate
, this);
631 debugs(93, DBG_IMPORTANT
, "XXX: ICAP service lost an update event.");
632 updateScheduled
= false;
635 debugs(93,7, HERE
<< "raw OPTIONS fetch at " << when
<< " or in " <<
636 (when
- squid_curtime
) << " sec");
637 debugs(93,9, HERE
<< "last fetched at " << theLastUpdate
<< " or " <<
638 (squid_curtime
- theLastUpdate
) << " sec ago");
640 /* adjust update time to prevent too-frequent updates */
642 if (when
< squid_curtime
)
643 when
= squid_curtime
;
645 // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig
646 const int minUpdateGap
= 30; // seconds
647 if (when
< theLastUpdate
+ minUpdateGap
)
648 when
= theLastUpdate
+ minUpdateGap
;
650 const int delay
= when
- squid_curtime
;
651 debugs(93,5, HERE
<< "will fetch OPTIONS in " << delay
<< " sec");
653 eventAdd("Adaptation::Icap::ServiceRep::noteTimeToUpdate",
654 &ServiceRep_noteTimeToUpdate
, this, delay
, 0, true);
655 updateScheduled
= true;
658 // returns absolute time when OPTIONS should be fetched
660 Adaptation::Icap::ServiceRep::optionsFetchTime() const
662 if (theOptions
&& theOptions
->valid()) {
663 const time_t expire
= theOptions
->expire();
664 debugs(93,7, HERE
<< "options expire on " << expire
<< " >= " << squid_curtime
);
666 // conservative estimate of how long the OPTIONS transaction will take
667 // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig
668 const int expectedWait
= 20; // seconds
670 // Unknown or invalid (too small) expiration times should not happen.
671 // Adaptation::Icap::Options should use the default TTL, and ICAP servers should not
672 // send invalid TTLs, but bugs and attacks happen.
673 if (expire
< expectedWait
)
674 return squid_curtime
;
676 return expire
- expectedWait
; // before the current options expire
679 // use revival delay as "expiration" time for a service w/o valid options
680 return squid_curtime
+ TheConfig
.service_revival_delay
;
683 Adaptation::Initiate
*
684 Adaptation::Icap::ServiceRep::makeXactLauncher(HttpMsg
*virgin
,
685 HttpRequest
*cause
, AccessLogEntry::Pointer
&alp
)
687 return new Adaptation::Icap::ModXactLauncher(virgin
, cause
, alp
, this);
690 // returns a temporary string depicting service status, for debugging
691 const char *Adaptation::Icap::ServiceRep::status() const
701 buf
.append("down", 4);
703 buf
.append(",susp", 5);
706 buf
.append(",!opt", 5);
707 else if (!theOptions
->valid())
708 buf
.append(",!valid", 7);
709 else if (!theOptions
->fresh())
710 buf
.append(",stale", 6);
714 buf
.append(",detached", 9);
716 if (theOptionsFetcher
.set())
717 buf
.append(",fetch", 6);
720 buf
.append(",notif", 6);
722 if (const int failures
= theSessionFailures
.remembered())
723 buf
.appendf(",fail%d", failures
);
728 return buf
.content();
731 void Adaptation::Icap::ServiceRep::detach()
733 debugs(93,3, HERE
<< "detaching ICAP service: " << cfg().uri
<<
738 bool Adaptation::Icap::ServiceRep::detached() const
743 Adaptation::Icap::ConnWaiterDialer::ConnWaiterDialer(const CbcPointer
<Adaptation::Icap::ModXact
> &xact
,
744 Adaptation::Icap::ConnWaiterDialer::Parent::Method aHandler
):
745 Parent(xact
, aHandler
)
747 theService
= &xact
->service();
748 theService
->noteNewWaiter();
751 Adaptation::Icap::ConnWaiterDialer::ConnWaiterDialer(const Adaptation::Icap::ConnWaiterDialer
&aConnWaiter
): Parent(aConnWaiter
)
753 theService
= aConnWaiter
.theService
;
754 theService
->noteNewWaiter();
757 Adaptation::Icap::ConnWaiterDialer::~ConnWaiterDialer()
759 theService
->noteGoneWaiter();