/
ICP.cc
2521 lines (2243 loc) · 82.8 KB
/
ICP.cc
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/****************************************************************************
ICP.cc
****************************************************************************/
#include "libts.h"
#include "Main.h"
#include "P_EventSystem.h"
#include "P_Cache.h"
#include "P_Net.h"
#include "MgmtUtils.h"
#include "P_RecProcess.h"
#include "ICP.h"
#include "ICPProcessor.h"
#include "ICPlog.h"
#include "logging/Log.h"
#include "logging/LogAccessICP.h"
#include "BaseManager.h"
#include "HdrUtils.h"
extern CacheLookupHttpConfig global_cache_lookup_config;
HTTPHdr gclient_request;
//****************************************************************************
// File Overview:
// ==============
// ICP files
// ICP.h -- All ICP class definitions.
// ICPlog.h -- ICP log object for logging system
// ICP.cc -- Incoming/outgoing ICP request and ICP configuration
// data base management.
// ICPConfig.cc -- ICP interface to Traffic Server configuration
// management, member functions for ICPlog (object
// passed to logging system) along with
// miscellaneous support routines.
// ICPevents.h -- Event definitions specific to ICP.
// ICPProcessor.h -- ICP external interface for other subsystems.
// External subsystems only need to include this
// header to use ICP.
// ICPProcessor.cc -- ICP external interface implementation.
// ICPStats.cc -- ICP statistic callback registration.
//
//
// Class Overview:
// ===============
// ICPConfigData -- Manages global ICP data from the TS configuration
// manager.
// PeerConfigData -- Manages ICP peer data from the TS configuration
// manager.
// ICPConfigUpdateCont -- Used by
// ICPConfiguration::icp_config_change_callback()
// to retry callout after a delay in cases where
// we cannot acquire the configuration lock.
// ICPConfiguration -- Overall manager of ICP configuration from TS
// configuration. Acts as interface and uses
// ICPConfigData and PeerConfigData to implement
// actions. Also fields/processes TS configuration
// callouts for "icp.config" changes. ICP classes only
// see ICPConfiguration when dealing with TS
// configuration info.
//
// Peer (base class) -- abstract base class
// ParentSiblingPeer : Peer -- ICP object describing parent/sibling
// peer which is initialized from the
// TS configuration data.
// MultiCastPeer : Peer -- ICP object describing MultiCast peer.
// Object is initialized from the TS
// configuration data.
//
// BitMap -- Generic bit map management class
//
// ICPProcessor -- Central class which starts all periodic events
// and maintains ICP configuration database. Delegates
// incoming data processing to ICPHandlerCont and
// outgoing data processing to ICPRequestCont. Implements
// reconfiguration actions and query requests from the
// external interface.
//
// ICPRequestCont -- Implements the state machine which processes
// locally generated ICP queries. Generates message
// queries and processes query responses. Responses
// received via callout from ICPPeerReadCont.
//
// PeriodicCont (base class) -- abstract base class
// ICPPeriodicCont : PeriodicCont -- Periodic which looks for ICP
// configuration changes sent by the Traffic Server
// configuration manager, and initiates ICP reconfiguration
// in the event we have a valid configuration change via
// ICPProcessor::ReconfigureStateMachine().
//
// ICPHandlerCont : PeriodicCont -- Periodic which monitors incoming
// ICP sockets and starts processing of the incoming ICP data.
//
// ICPPeerReadCont -- Implements the incoming data state machine.
// Processes remote ICP query requests and passes query
// responses to ICPRequestCont via a callout.
// ICPlog -- Logging object which encapsulates ICP query info required
// by the new logging subsystem to produce squid access log
// data for ICP queries.
//
//****************************************************************************
//
// ICP is integrated into HTTP miss processing as follows.
//
// if (HTTP Traffic Server Miss) {
// if (proxy.config.icp.enabled) {
// Status = QueryICP(URL, &target_ip);
// if (Status == ICP_HIT)
// Issue Http Request to (target_ip, proxy_port);
// }
// if (proxy.config.http.parent_proxy_routing_enable) {
// Issue Http Request to (proxy.config.http.parent_proxy_hostname,
// proxy.config.http.parent_proxy_port)
// }
// else
// Issue Http Request to Origin Server
// }
//
//****************************************************************************
// VC++ 5.0 is rather picky
typedef int (ICPPeerReadCont::*ICPPeerReadContHandler) (int, void *);
typedef int (ICPPeriodicCont::*ICPPeriodicContHandler) (int, void *);
typedef int (ICPHandlerCont::*ICPHandlerContHandler) (int, void *);
typedef int (ICPRequestCont::*ICPRequestContHandler) (int, void *);
// Plugin freshness function
PluginFreshnessCalcFunc pluginFreshnessCalcFunc = (PluginFreshnessCalcFunc) NULL;
//---------------------------------------
// Class ICPHandlerCont member functions
// Deal with incoming ICP data
//---------------------------------------
// Static data declarations
//Allocator *ICPHandlerCont::IncomingICPDataBuf;
int64_t ICPHandlerCont::ICPDataBuf_IOBuffer_sizeindex;
static ClassAllocator <ICPPeerReadCont::PeerReadData>PeerReadDataAllocator("PeerReadDataAllocator");
static ClassAllocator<ICPPeerReadCont> ICPPeerReadContAllocator("ICPPeerReadContAllocator");
static Action *default_action = NULL;
ICPHandlerCont::ICPHandlerCont(ICPProcessor * icpP)
: PeriodicCont(icpP)
{
}
// do nothing continuation handler
int
ICPHandlerCont::TossEvent(int /* event ATS_UNUSED */, Event * /* e ATS_UNUSED */)
{
return EVENT_DONE;
}
int
ICPHandlerCont::PeriodicEvent(int event, Event * /* e ATS_UNUSED */)
{
int n_peer, valid_peers;
Peer *P;
// Periodic handler which initiates incoming message processing
// on the defined peers.
valid_peers = _ICPpr->GetRecvPeers();
// get peer info from the completionEvent token.
switch (event) {
case EVENT_POLL:
case EVENT_INTERVAL:
{
// start read I/Os on peers which don't have outstanding I/Os
for (n_peer = 0; n_peer < valid_peers; ++n_peer) {
P = _ICPpr->GetNthRecvPeer(n_peer, _ICPpr->GetLastRecvPeerBias());
if (!P || (P && !P->IsOnline()))
continue;
if (P->shouldStartRead()) {
P->startingRead();
///////////////////////////////////////////
// Setup state machine
///////////////////////////////////////////
ICPPeerReadCont *s = ICPPeerReadContAllocator.alloc();
int local_lookup = _ICPpr->GetConfig()->globalConfig()->ICPLocalCacheLookup();
s->init(_ICPpr, P, local_lookup);
RECORD_ICP_STATE_CHANGE(s, event, ICPPeerReadCont::READ_ACTIVE);
///////////////////////////////////////////
// Start processing
///////////////////////////////////////////
s->handleEvent(EVENT_INTERVAL, (Event *) 0);
}
}
break;
}
default:
{
ink_release_assert(!"unexpected event");
break;
}
} // End of switch
return EVENT_CONT;
}
//***************************************************************************
// Nested Class PeerReadData member functions
// Used by ICPPeerReadCont to encapsulate the data required by
// PeerReadStateMachine
//***************************************************************************
ICPPeerReadCont::PeerReadData::PeerReadData()
{
init();
}
void
ICPPeerReadCont::PeerReadData::init()
{
_start_time = 0;
_mycont = 0;
_peer = 0;
_next_state = READ_ACTIVE;
_cache_lookup_local = 0;
_buf = 0;
_rICPmsg = 0;
_rICPmsg_len = 0;
_cachelookupURL.clear();
_queryResult = 0;
_ICPReqCont = 0;
_bytesReceived = 0;
#ifdef DEBUG_ICP
_nhistory = 0;
#endif
memset((void *) &_sender, 0, sizeof(_sender));
}
ICPPeerReadCont::PeerReadData::~PeerReadData()
{
reset(1);
}
void
ICPPeerReadCont::PeerReadData::reset(int full_reset)
{
if (full_reset) {
_peer = 0;
_buf = 0;
}
if (_rICPmsg) {
_rICPmsg = 0;
_rICPmsg_len = 0;
}
if (_cachelookupURL.valid()) {
_cachelookupURL.destroy();
}
}
//***************************************************************************
//------------------------------------------------------------------------
// ICPPeerReadCont -- ICP incoming message processing state machine
//------------------------------------------------------------------------
ICPPeerReadCont::ICPPeerReadCont():Continuation(0), _object_vc(NULL), _object_read(NULL),
_cache_req_hdr_heap_handle(NULL), _cache_resp_hdr_heap_handle(NULL), _ICPpr(NULL), _state(NULL),
_start_time(0), _recursion_depth(0)
{
}
void
ICPPeerReadCont::init(ICPProcessor * ICPpr, Peer * p, int lookup_local)
{
PeerReadData *s = PeerReadDataAllocator.alloc();
s->init();
s->_start_time = ink_get_hrtime();
s->_peer = p;
s->_next_state = READ_ACTIVE;
s->_cache_lookup_local = lookup_local;
SET_HANDLER((ICPPeerReadContHandler) & ICPPeerReadCont::ICPPeerReadEvent);
_ICPpr = ICPpr;
_state = s;
_recursion_depth = -1;
_object_vc = NULL;
_object_read = NULL;
_cache_req_hdr_heap_handle = NULL;
_cache_resp_hdr_heap_handle = NULL;
mutex = new_ProxyMutex();
}
ICPPeerReadCont::~ICPPeerReadCont()
{
reset(1); // Full reset
}
void
ICPPeerReadCont::reset(int full_reset)
{
mutex = 0;
if (this->_state) {
this->_state->reset(full_reset);
PeerReadDataAllocator.free(this->_state);
}
if (_cache_req_hdr_heap_handle) {
ats_free(_cache_req_hdr_heap_handle);
_cache_req_hdr_heap_handle = NULL;
}
if (_cache_resp_hdr_heap_handle) {
ats_free(_cache_resp_hdr_heap_handle);
_cache_resp_hdr_heap_handle = NULL;
}
}
int
ICPPeerReadCont::ICPPeerReadEvent(int event, Event * e)
{
switch (event) {
case EVENT_INTERVAL:
case EVENT_IMMEDIATE:
{
break;
}
case NET_EVENT_DATAGRAM_WRITE_COMPLETE:
case NET_EVENT_DATAGRAM_READ_COMPLETE:
case NET_EVENT_DATAGRAM_READ_ERROR:
case NET_EVENT_DATAGRAM_WRITE_ERROR:
{
ink_assert((event != NET_EVENT_DATAGRAM_READ_COMPLETE)
|| (_state->_next_state == READ_DATA_DONE));
ink_assert((event != NET_EVENT_DATAGRAM_WRITE_COMPLETE)
|| (_state->_next_state == WRITE_DONE));
ink_release_assert(this == (ICPPeerReadCont *)
completionUtil::getHandle(e));
break;
}
case CACHE_EVENT_LOOKUP_FAILED:
case CACHE_EVENT_LOOKUP:
{
ink_assert(_state->_next_state == AWAITING_CACHE_LOOKUP_RESPONSE);
break;
}
default:
{
ink_release_assert(!"unexpected event");
}
} // End of switch
// Front end to PeerReadStateMachine(), invoked by Event subsystem.
if (PeerReadStateMachine(_state, e) == EVENT_CONT) {
eventProcessor.schedule_in(this, RETRY_INTERVAL, ET_ICP);
return EVENT_DONE;
} else if (_state->_next_state == READ_PROCESSING_COMPLETE) {
_state->_peer->cancelRead();
this->reset(1); // Full reset
ICPPeerReadContAllocator.free(this);
return EVENT_DONE;
} else {
return EVENT_DONE;
}
}
int
ICPPeerReadCont::StaleCheck(int event, Event * /* e ATS_UNUSED */)
{
ip_port_text_buffer ipb;
ink_release_assert(mutex->thread_holding == this_ethread());
Debug("icp-stale", "Stale check res=%d for id=%d, [%s] from [%s]",
event, _state->_rICPmsg->h.requestno,
_state->_rICPmsg->un.query.URL, ats_ip_nptop(&_state->_sender, ipb, sizeof(ipb)));
switch (event) {
case ICP_STALE_OBJECT:
{
_state->_queryResult = CACHE_EVENT_LOOKUP_FAILED;
break;
}
case ICP_FRESH_OBJECT:
{
_state->_queryResult = CACHE_EVENT_LOOKUP;
break;
}
default:
{
Debug("icp-stale", "ICPPeerReadCont::StaleCheck: Invalid Event %d\n", event);
_state->_queryResult = CACHE_EVENT_LOOKUP_FAILED;
break;
}
}
_object_vc->do_io(VIO::CLOSE);
_object_vc = 0;
SET_HANDLER((ICPPeerReadContHandler) & ICPPeerReadCont::ICPPeerReadEvent);
return handleEvent(_state->_queryResult, 0);
}
int
ICPPeerReadCont::ICPPeerQueryEvent(int event, Event * e)
{
ip_port_text_buffer ipb;
Debug("icp", "Remote Query lookup res=%d for id=%d, [%s] from [%s]",
event, _state->_rICPmsg->h.requestno,
_state->_rICPmsg->un.query.URL, ats_ip_nptop(&_state->_sender, ipb, sizeof(ipb)));
if (pluginFreshnessCalcFunc) {
switch (event) {
case CACHE_EVENT_OPEN_READ:
{
_object_vc = (CacheVConnection *) e;
SET_HANDLER((ICPPeerReadContHandler) & ICPPeerReadCont::StaleCheck);
_object_vc->get_http_info(&_object_read);
(*pluginFreshnessCalcFunc) ((void *) this);
return EVENT_DONE;
}
case CACHE_EVENT_OPEN_READ_FAILED:
{
event = CACHE_EVENT_LOOKUP_FAILED;
break;
}
default:
break;
}
}
// Process result
_state->_queryResult = event;
SET_HANDLER((ICPPeerReadContHandler) & ICPPeerReadCont::ICPPeerReadEvent);
return handleEvent(event, e);
}
int
ICPPeerReadCont::ICPPeerQueryCont(int /* event ATS_UNUSED */, Event * /* e ATS_UNUSED */)
{
ip_port_text_buffer ipb;
Action *a;
// Perform lookup()/open_read() on behalf of PeerReadStateMachine()
((char *) _state->_rICPmsg)[MAX_ICP_MSGSIZE - 1] = 0; // null terminate
_state->_cachelookupURL.create(NULL);
const char *qurl = (const char *) _state->_rICPmsg->un.query.URL;
_state->_cachelookupURL.parse(qurl, strlen(qurl));
Debug("icp", "Remote Query for id=%d, [%s] from [%s]",
_state->_rICPmsg->h.requestno,
_state->_rICPmsg->un.query.URL,
ats_ip_nptop(&_state->_sender, ipb, sizeof(ipb))
);
SET_HANDLER((ICPPeerReadContHandler) & ICPPeerReadCont::ICPPeerQueryEvent);
if (_state->_rICPmsg->un.query.URL && *_state->_rICPmsg->un.query.URL) {
_state->_queryResult = ~CACHE_EVENT_LOOKUP_FAILED;
_start_time = ink_get_hrtime();
if (pluginFreshnessCalcFunc && _ICPpr->GetConfig()->globalConfig()->ICPStaleLookup()) {
//////////////////////////////////////////////////////////////
// Note: _cache_lookup_local is ignored in this case, since
// cache clustering is not used with stale lookup.
//////////////////////////////////////////////////////////////
a = cacheProcessor.open_read(this, &_state->_cachelookupURL, false,
&gclient_request, &global_cache_lookup_config, (time_t) 0);
} else {
a = cacheProcessor.lookup(this, &_state->_cachelookupURL, false, _state->_cache_lookup_local);
}
if (!a) {
a = ACTION_IO_ERROR;
}
if (a == ACTION_RESULT_DONE) {
return EVENT_DONE; // callback complete
} else if (a == ACTION_IO_ERROR) {
handleEvent(CACHE_EVENT_LOOKUP_FAILED, 0);
return EVENT_DONE; // callback complete
} else {
return EVENT_CONT; // callback pending
}
} else {
// Null URL, return failed lookup
handleEvent(CACHE_EVENT_LOOKUP_FAILED, 0);
return EVENT_DONE; // callback done
}
}
struct AutoReference
{
AutoReference(int *cnt)
{
_cnt = cnt;
(*_cnt)++;
}
~AutoReference()
{
(*_cnt)--;
}
int *_cnt;
};
int
ICPPeerReadCont::PeerReadStateMachine(PeerReadData * s, Event * e)
{
AutoReference l(&_recursion_depth);
ip_port_text_buffer ipb; // scratch buffer for diagnostic messages.
//-----------------------------------------------------------
// State machine to process ICP data received on UDP socket
//-----------------------------------------------------------
MUTEX_TRY_LOCK(lock, this->mutex, this_ethread());
if (!lock) {
// we didn't get the lock, so we don't need to unlock it
// coverity[missing_unlock]
return EVENT_CONT; // try again later
}
while (1) { // loop forever
switch (s->_next_state) {
case READ_ACTIVE:
{
ink_release_assert(_recursion_depth == 0);
if (!_ICPpr->Lock())
return EVENT_CONT; // unable to get lock, try again later
bool valid_peer = (_ICPpr->IdToPeer(s->_peer->GetPeerID()) == s->_peer);
if (valid_peer && _ICPpr->AllowICPQueries()
&& _ICPpr->GetConfig()->globalConfig()->ICPconfigured()) {
// Note pending incoming ICP request or response
_ICPpr->IncPendingQuery();
_ICPpr->Unlock();
s->_next_state = READ_DATA;
RECORD_ICP_STATE_CHANGE(s, 0, READ_DATA);
break; // move to next_state
} else {
_ICPpr->Unlock();
// ICP NOT enabled, do nothing
s->_next_state = READ_PROCESSING_COMPLETE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_PROCESSING_COMPLETE);
return EVENT_DONE;
}
}
#if !defined(__GNUC__)
_end_case_read_active: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case READ_DATA:
{
ink_release_assert(_recursion_depth == 0);
// Assumption of one outstanding read per peer...
// Setup read from FD
ink_assert(s->_peer->buf == NULL);
Ptr<IOBufferBlock> buf = s->_peer->buf = new_IOBufferBlock();
buf->alloc(ICPHandlerCont::ICPDataBuf_IOBuffer_sizeindex);
s->_peer->fromaddrlen = sizeof(s->_peer->fromaddr);
buf->fill(sizeof(ICPMsg_t)); // reserve space for decoding
char *be = buf->buf_end() - 1;
be[0] = 0; // null terminate buffer
s->_next_state = READ_DATA_DONE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_DATA_DONE);
ink_assert(s->_peer->readAction == NULL);
Action *a = s->_peer->RecvFrom_re(this, this, buf,
buf->write_avail() - 1,
&s->_peer->fromaddr.sa,
&s->_peer->fromaddrlen);
if (!a) {
a = ACTION_IO_ERROR;
}
if (a == ACTION_RESULT_DONE) {
// we will have been called back already and our state updated
// appropriately.
// move to next state
ink_assert(s->_next_state == PROCESS_READ_DATA);
break;
} else if (a == ACTION_IO_ERROR) {
// actually, this *could* be taken care of by the main handler, but
// error processing makes more sense at this point. Therefore,
// the main handler ignores the errors.
//
// No data, terminate read loop.
//
ICP_INCREMENT_DYN_STAT(no_data_read_stat);
s->_peer->buf = NULL; // release reference
s->_next_state = READ_NOT_ACTIVE_EXIT;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE_EXIT);
// move to next state
break;
} else {
s->_peer->readAction = a;
return EVENT_DONE;
}
}
#if !defined(__GNUC__)
_end_case_read_data: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case READ_DATA_DONE:
{
// Convert ICP message from network to host format
if (s->_peer->readAction != NULL) {
ink_assert(s->_peer->readAction == e);
s->_peer->readAction = NULL;
}
s->_bytesReceived = completionUtil::getBytesTransferred(e);
if (s->_bytesReceived >= 0) {
s->_next_state = PROCESS_READ_DATA;
RECORD_ICP_STATE_CHANGE(s, 0, PROCESS_READ_DATA);
} else {
ICP_INCREMENT_DYN_STAT(no_data_read_stat);
s->_peer->buf = NULL; // release reference
s->_next_state = READ_NOT_ACTIVE_EXIT;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE_EXIT);
}
if (_recursion_depth > 0) {
return EVENT_DONE;
} else {
break;
}
}
#if !defined(__GNUC__)
_end_case_read_data_done: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case PROCESS_READ_DATA:
case ADD_PEER:
{
ink_release_assert(_recursion_depth == 0);
Ptr<IOBufferBlock> bufblock = s->_peer->buf;
char *buf = bufblock->start();
if (s->_next_state == PROCESS_READ_DATA) {
ICPRequestCont::NetToHostICPMsg((ICPMsg_t *)
(buf + sizeof(ICPMsg_t)), (ICPMsg_t *) buf);
// adjust buffer pointers to point to decoded message.
bufblock->reset();
bufblock->fill(s->_bytesReceived);
// Validate message length for sanity
if (s->_bytesReceived < ((ICPMsg_t *) buf)->h.msglen) {
//
// Short read, terminate
//
ICP_INCREMENT_DYN_STAT(short_read_stat);
s->_peer->buf = NULL;
s->_next_state = READ_NOT_ACTIVE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
break; // move to next_state
}
}
// Validate receiver and convert the received sockaddr
// to internal sockaddr format.
IpEndpoint from;
if (!s->_peer->ExtToIntRecvSockAddr(&s->_peer->fromaddr.sa, &from.sa)) {
int status;
ICPConfigData *cfg = _ICPpr->GetConfig()->globalConfig();
ICPMsg_t *ICPmsg = (ICPMsg_t *) buf;
if ((cfg->ICPconfigured() == ICP_MODE_RECEIVE_ONLY) &&
cfg->ICPReplyToUnknownPeer() &&
((ICPmsg->h.version == ICP_VERSION_2) ||
(ICPmsg->h.version == ICP_VERSION_3)) && (ICPmsg->h.opcode == ICP_OP_QUERY)) {
//
// Add the unknown Peer to our database to
// allow us to resolve the lookup request.
//
if (!_ICPpr->GetConfig()->Lock()) {
s->_next_state = ADD_PEER;
RECORD_ICP_STATE_CHANGE(s, 0, ADD_PEER);
return EVENT_CONT;
}
if (!_ICPpr->GetFreePeers() || !_ICPpr->GetFreeSendPeers()) {
Warning("ICP Peer limit exceeded");
REC_SignalWarning(REC_SIGNAL_CONFIG_ERROR, "ICP Peer limit exceeded");
_ICPpr->GetConfig()->Unlock();
goto invalid_message;
}
int icp_reply_port = cfg->ICPDefaultReplyPort();
if (!icp_reply_port) {
icp_reply_port = ntohs(ats_ip_port_cast(&s->_peer->fromaddr));
}
PeerConfigData *Pcfg = NEW(new PeerConfigData(
PeerConfigData::CTYPE_SIBLING,
IpAddr(s->_peer->fromaddr),
0,
icp_reply_port
));
ParentSiblingPeer *P = NEW(new ParentSiblingPeer(PEER_SIBLING, Pcfg, _ICPpr, true));
status = _ICPpr->AddPeer(P);
ink_release_assert(status);
status = _ICPpr->AddPeerToSendList(P);
ink_release_assert(status);
P->GetChan()->setRemote(P->GetIP());
// coverity[uninit_use_in_call]
Note("ICP Peer added ip=%s", ats_ip_nptop(P->GetIP(), ipb, sizeof(ipb)));
from = s->_peer->fromaddr;
} else {
invalid_message:
//
// Sender does not exist in ICP configuration, terminate
//
ICP_INCREMENT_DYN_STAT(invalid_sender_stat);
Debug("icp", "Received msg from invalid sender [%s]",
ats_ip_nptop(&s->_peer->fromaddr, ipb, sizeof(ipb)));
s->_peer->buf = NULL;
s->_next_state = READ_NOT_ACTIVE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
break; // move to next_state
}
}
// we hand off the decoded buffer from the Peer to the PeerReadData
s->_sender = from;
s->_rICPmsg_len = s->_bytesReceived;
ink_assert(s->_buf == NULL);
s->_buf = s->_peer->buf;
s->_rICPmsg = (ICPMsg_t *) s->_buf->start();
s->_peer->buf = NULL;
//
// Handle only ICP_VERSION_2/3 messages. Reject all others.
//
if ((s->_rICPmsg->h.version != ICP_VERSION_2)
&& (s->_rICPmsg->h.version != ICP_VERSION_3)) {
ICP_INCREMENT_DYN_STAT(read_not_v2_icp_stat);
Debug("icp", "Received (v=%d) !v2 && !v3 msg from sender [%s]",
(uint32_t) s->_rICPmsg->h.version, ats_ip_nptop(&from, ipb, sizeof(ipb)));
s->_rICPmsg = NULL;
s->_buf = NULL;
s->_next_state = READ_NOT_ACTIVE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
break; // move to next_state
}
//
// If this is a query message, redirect to
// the query specific handlers.
//
if (s->_rICPmsg->h.opcode == ICP_OP_QUERY) {
ICP_INCREMENT_DYN_STAT(icp_remote_query_requests_stat);
ink_assert(!s->_mycont);
s->_next_state = AWAITING_CACHE_LOOKUP_RESPONSE;
RECORD_ICP_STATE_CHANGE(s, 0, AWAITING_CACHE_LOOKUP_RESPONSE);
if (ICPPeerQueryCont(0, (Event *) 0) == EVENT_DONE) {
break; // Callback complete
} else {
return EVENT_DONE; // Callback pending
}
} else {
// We have a response message for an ICP query.
Debug("icp", "Response for Id=%d, from [%s]",
s->_rICPmsg->h.requestno, ats_ip_nptop(&s->_sender, ipb, sizeof(ipb)));
ICP_INCREMENT_DYN_STAT(icp_remote_responses_stat);
s->_next_state = GET_ICP_REQUEST;
RECORD_ICP_STATE_CHANGE(s, 0, GET_ICP_REQUEST);
break; // move to next_state
}
}
#if !defined(__GNUC__)
_end_case_process_data_read: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case AWAITING_CACHE_LOOKUP_RESPONSE:
{
int status = 0;
void *data = s->_rICPmsg->un.query.URL;
int datalen = strlen((const char *) data) + 1;
if (s->_queryResult == CACHE_EVENT_LOOKUP) {
// Use the received ICP data buffer for the response message
Debug("icp", "Sending ICP_OP_HIT for id=%d, [%.*s] to [%s]",
s->_rICPmsg->h.requestno, datalen, (const char *)data, ats_ip_nptop(&s->_sender, ipb, sizeof(ipb)));
ICP_INCREMENT_DYN_STAT(icp_cache_lookup_success_stat);
status = ICPRequestCont::BuildICPMsg(ICP_OP_HIT,
s->_rICPmsg->h.requestno, 0 /* optflags */ , 0 /* optdata */ ,
0 /* shostid */ ,
data, datalen, &s->_mhdr, s->_iov, s->_rICPmsg);
} else if (s->_queryResult == CACHE_EVENT_LOOKUP_FAILED) {
// Use the received ICP data buffer for response message
Debug("icp", "Sending ICP_OP_MISS for id=%d, [%.*s] to [%s]",
s->_rICPmsg->h.requestno, datalen, (const char *)data, ats_ip_nptop(&s->_sender, ipb, sizeof(ipb)));
ICP_INCREMENT_DYN_STAT(icp_cache_lookup_fail_stat);
status = ICPRequestCont::BuildICPMsg(ICP_OP_MISS,
s->_rICPmsg->h.requestno, 0 /* optflags */ , 0 /* optdata */ ,
0 /* shostid */ ,
data, datalen, &s->_mhdr, s->_iov, s->_rICPmsg);
} else {
Warning("Bad cache lookup event: %d", s->_queryResult);
ink_release_assert(!"Invalid cache lookup event");
}
ink_assert(status == 0);
// Make system log entry for ICP query
ICPlog logentry(s);
LogAccessICP accessor(&logentry);
Log::access(&accessor);
s->_next_state = SEND_REPLY;
RECORD_ICP_STATE_CHANGE(s, 0, SEND_REPLY);
if (_recursion_depth > 0) {
return EVENT_DONE;
} else {
break;
}
}
#if !defined(__GNUC__)
_end_case_awaiting_cache_lookup_response: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case SEND_REPLY:
{
ink_release_assert(_recursion_depth == 0);
//
// Send the query response back to the sender
//
s->_next_state = WRITE_DONE;
RECORD_ICP_STATE_CHANGE(s, 0, WRITE_DONE);
ink_assert(s->_peer->writeAction == NULL);
Action *a = s->_peer->SendMsg_re(this, this,
&s->_mhdr, &s->_sender.sa);
if (!a) {
a = ACTION_IO_ERROR;
}
if (a == ACTION_RESULT_DONE) {
// we have been called back already and our state updated
// appropriately
break;
} else if (a == ACTION_IO_ERROR) {
// Partial write.
ICP_INCREMENT_DYN_STAT(query_response_partial_write_stat);
// coverity[uninit_use_in_call]
Debug("icp_warn", "ICP response send, sent=%d res=%d, ip=%s",
ntohs(s->_rICPmsg->h.msglen), -1, ats_ip_ntop(&s->_sender, ipb, sizeof(ipb)));
s->_next_state = READ_NOT_ACTIVE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
break;
} else {
s->_peer->writeAction = a;
return EVENT_DONE;
}
}
#if !defined(__GNUC__)
_end_case_send_reply: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case WRITE_DONE:
{
s->_peer->writeAction = NULL;
int len = completionUtil::getBytesTransferred(e);
if (len == (int)ntohs(s->_rICPmsg->h.msglen)) {
ICP_INCREMENT_DYN_STAT(query_response_write_stat);
s->_peer->LogSendMsg(s->_rICPmsg, &s->_sender.sa); // log query reply
} else {
// Partial write.
ICP_INCREMENT_DYN_STAT(query_response_partial_write_stat);
// coverity[uninit_use_in_call]
Debug("icp_warn", "ICP response send, sent=%d res=%d, ip=%s",
ntohs(s->_rICPmsg->h.msglen), len, ats_ip_ntop(&s->_sender, ipb, sizeof(ipb)));
}
// Processing complete, perform completion actions
s->_next_state = READ_NOT_ACTIVE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
Debug("icp", "state->READ_NOT_ACTIVE");
if (_recursion_depth > 0) {
return EVENT_DONE;
} else {
break; // move to next_state
}
}
#if !defined(__GNUC__)
_end_case_write_done: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case GET_ICP_REQUEST:
{
ink_release_assert(_recursion_depth == 0);
ink_assert(s->_rICPmsg && s->_rICPmsg_len); // Sanity check
// Get ICP request associated with response message
s->_ICPReqCont = ICPRequestCont::FindICPRequest(s->_rICPmsg->h.requestno);
if (s->_ICPReqCont) {
s->_next_state = GET_ICP_REQUEST_MUTEX;
RECORD_ICP_STATE_CHANGE(s, 0, GET_ICP_REQUEST_MUTEX);
break; // move to next_state
}
//
// No ICP request for response message, log as "response
// for non-existent ICP request" and terminate processing
//
Debug("icp", "No ICP Request for Id=%d", s->_rICPmsg->h.requestno);
ICP_INCREMENT_DYN_STAT(no_icp_request_for_response_stat);
Peer *p = _ICPpr->FindPeer(s->_sender);
p->LogRecvMsg(s->_rICPmsg, 0);
s->_next_state = READ_NOT_ACTIVE;
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
break; // move to next_state
}
#if !defined(__GNUC__)
_end_case_get_icp_request: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen
case GET_ICP_REQUEST_MUTEX:
{
ink_release_assert(_recursion_depth == 0);
ink_assert(s->_ICPReqCont);
Ptr<ProxyMutex> ICPReqContMutex(s->_ICPReqCont->mutex);
EThread *ethread = this_ethread();
ink_hrtime request_start_time;
if (!MUTEX_TAKE_TRY_LOCK(ICPReqContMutex, ethread)) {
ICP_INCREMENT_DYN_STAT(icp_response_request_nolock_stat);
//
// Unable to get ICP request mutex, delay and move back
// to the GET_ICP_REQUEST state. We need to do this
// since the ICP request may be deallocated by the active
// continuation.
//
s->_ICPReqCont = (ICPRequestCont *) 0;
s->_next_state = GET_ICP_REQUEST;
RECORD_ICP_STATE_CHANGE(s, 0, GET_ICP_REQUEST);
return EVENT_CONT;
}
// Log as "response for ICP request"
Peer *p = _ICPpr->FindPeer(s->_sender);
p->LogRecvMsg(s->_rICPmsg, 1);
// Process the ICP response for the given ICP request
ICPRequestCont::ICPRequestEventArgs_t args;
args.rICPmsg = s->_rICPmsg;
args.rICPmsg_len = s->_rICPmsg_len;
args.peer = p;
if (!s->_ICPReqCont->GetActionPtr()->cancelled) {
request_start_time = s->_ICPReqCont->GetRequestStartTime();
Debug("icp", "Passing Reply for ICP Id=%d", s->_rICPmsg->h.requestno);
s->_ICPReqCont->handleEvent((int) ICP_RESPONSE_MESSAGE, (void *) &args);
} else {
request_start_time = 0;
delete s->_ICPReqCont;
Debug("icp", "User cancelled ICP request Id=%d", s->_rICPmsg->h.requestno);
}
// Note: s->_ICPReqCont is deallocated at this point.
s->_ICPReqCont = 0;
MUTEX_UNTAKE_LOCK(ICPReqContMutex, ethread);
if (request_start_time) {
ICP_SUM_DYN_STAT(total_icp_response_time_stat, (ink_get_hrtime() - request_start_time));
}
RECORD_ICP_STATE_CHANGE(s, 0, READ_NOT_ACTIVE);
s->_next_state = READ_NOT_ACTIVE;
break; // move to next_state
}
#if !defined(__GNUC__)
_end_case_get_icp_request_mutex: // fix DEC warnings
#endif
ink_release_assert(0); // Should never happen