/
ICP.h
1352 lines (1214 loc) · 32.1 KB
/
ICP.h
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.h
****************************************************************************/
#ifndef _ICP_H_
#define _ICP_H_
#include "P_Net.h"
#include "P_Cache.h"
#define ET_ICP ET_CALL
#include "URL.h"
#include "ICPevents.h"
#include "ICPProcessor.h"
#include "ts/DynArray.h"
//*********************************************************************
// ICP Configurables
//*********************************************************************
#define ICP_DEBUG 1
//*********************************************************************
// ICP.h -- Internet Cache Protocol (ICP) related data structures.
//
// Message protocol definitions as defined by RFC 2186
// "Internet Cache Protocol (ICP), version 2".
//*********************************************************************
typedef struct ICPMsgHeader {
uint8_t opcode;
uint8_t version;
uint16_t msglen;
uint32_t requestno;
uint32_t optionflags;
uint32_t optiondata;
uint32_t shostid;
} ICPMsgHdr_t;
//-----------------------
// opcode definitions
//-----------------------
typedef enum {
ICP_OP_INVALID, // 00
ICP_OP_QUERY, // 01
ICP_OP_HIT, // 02
ICP_OP_MISS, // 03
ICP_OP_ERR, // 04
//
ICP_OP_UNUSED5, // 05 unused
ICP_OP_UNUSED6, // 06 unused
ICP_OP_UNUSED7, // 07 unused
ICP_OP_UNUSED8, // 08 unused
ICP_OP_UNUSED9, // 09 unused
//
ICP_OP_SECHO, // 10
ICP_OP_DECHO, // 11
//
ICP_OP_UNUSED12, // 12 unused
ICP_OP_UNUSED13, // 13 unused
ICP_OP_UNUSED14, // 14 unused
ICP_OP_UNUSED15, // 15 unused
ICP_OP_UNUSED16, // 16 unused
ICP_OP_UNUSED17, // 17 unused
ICP_OP_UNUSED18, // 18 unused
ICP_OP_UNUSED19, // 19 unused
ICP_OP_UNUSED20, // 20 unused
//
ICP_OP_MISS_NOFETCH, // 21
ICP_OP_DENIED, // 22
ICP_OP_HIT_OBJ, // 23
ICP_OP_END_OF_OPS // 24 mark end of opcodes
} ICPopcode_t;
#define ICP_OP_LAST (ICP_OP_END_OF_OPS - 1)
//-----------------------
// version definitions
//-----------------------
#define ICP_VERSION_1 1
#define ICP_VERSION_2 2
#define ICP_VERSION_3 3
#define ICP_VERSION ICP_VERSION_2
//--------------------------
// optionflags definitions
//--------------------------
#define ICP_FLAG_HIT_OBJ 0x80000000ul
#define ICP_FLAG_SRC_RTT 0x40000000ul
//-----------------
// ICP Constants
//-----------------
#define MAX_ICP_MSGSIZE (16 * 1024)
#define MAX_ICP_MSG_PAYLOAD_SIZE (MAX_ICP_MSGSIZE - sizeof(ICPmsgHdr_t))
#define MAX_ICP_QUERY_PAYLOAD_SIZE (MAX_ICP_MSG_PAYLOAD_SIZE - sizeof(uint32_t))
#define MAX_DEFINED_PEERS 64
#define MSG_IOVECS 16
//------------
// ICP Data
//------------
typedef struct ICPData {
char *URL; // null terminated
} ICPData_t;
//-------------
// ICP Query
//-------------
typedef struct ICPQuery {
uint32_t rhostid;
char *URL; // null terminated (outgoing)
} ICPQuery_t;
//------------
// ICP Hit
//------------
typedef struct ICPHit {
char *URL; // null terminated
} ICPHit_t;
//------------
// ICP Miss
//------------
typedef struct ICPMiss {
char *URL; // null terminated
} ICPMiss_t;
//------------------
// ICP Hit Object
//------------------
typedef struct ICPHitObj {
char *URL; // null terminated
char *p_objsize; // byte aligned uint16_t immediately follows URL null
uint16_t objsize; // decoded object size
char *data; // object data
} ICPHitObj_t;
//------------------------
// ICP message descriptor
//------------------------
typedef struct ICPMsg {
ICPMsgHdr_t h;
union {
ICPData_t data;
ICPQuery_t query;
ICPHit_t hit;
ICPMiss_t miss;
ICPHitObj_t hitobj;
} un;
} ICPMsg_t;
//******************************************************************
// ICP implementation specific data structures.
//******************************************************************
class BitMap;
class ICPProcessor;
class ICPPeriodicCont;
class ICPHandlerCont;
class ICPPeerReadCont;
class ICPRequestCont;
typedef enum {
PEER_NONE = 0,
PEER_PARENT = 1,
PEER_SIBLING = 2,
PEER_LOCAL = 3,
PEER_MULTICAST = 4,
} PeerType_t;
#if !defined(USE_CAS_FOR_ATOMICLOCK)
class AtomicLock
{
public:
AtomicLock();
~AtomicLock();
int Lock();
int HaveLock();
void Unlock();
private:
Ptr<ProxyMutex> _mutex;
};
#else // USE_CAS_FOR_ATOMICLOCK
class AtomicLock
{
public:
AtomicLock();
~AtomicLock();
int Lock();
int HaveLock();
void Unlock();
private:
enum {
UNLOCKED = 0,
LOCKED = 1,
};
int32_t _lock_word;
};
#endif // USE_CAS_FOR_ATOMICLOCK
//-----------------------------------------------------------------
// Class ICPConfigData -- deal with global ICP configuration data
//-----------------------------------------------------------------
class ICPConfigData
{
friend class ICPConfiguration;
public:
ICPConfigData()
: _icp_enabled(0), _icp_port(0), _icp_interface(0), _multicast_enabled(0), _icp_query_timeout(0), _cache_lookup_local(0),
_stale_lookup(0), _reply_to_unknown_peer(0), _default_reply_port(0), _cache_generation(-1)
{
}
~ICPConfigData() {} // Note: _icp_interface freed prior to delete
inline int operator==(ICPConfigData &);
inline int
ICPconfigured()
{
return _icp_enabled;
}
inline int
ICPport()
{
return _icp_port;
}
inline char *
ICPinterface()
{
return _icp_interface;
}
inline int
ICPmulticastConfigured()
{
return _multicast_enabled;
}
inline int
ICPqueryTimeout()
{
return _icp_query_timeout;
}
inline int
ICPLocalCacheLookup()
{
return _cache_lookup_local;
}
inline int
ICPStaleLookup()
{
return _stale_lookup;
}
inline int
ICPReplyToUnknownPeer()
{
return _reply_to_unknown_peer;
}
inline int
ICPDefaultReplyPort()
{
return _default_reply_port;
}
inline cache_generation_t
ICPCacheGeneration() const
{
return _cache_generation;
}
private:
//---------------------------------------------------------
// ICP Configuration data derived from "records.config"
//---------------------------------------------------------
int _icp_enabled; // see ICP_MODE_XXX defines
int _icp_port;
char *_icp_interface;
int _multicast_enabled;
int _icp_query_timeout;
int _cache_lookup_local;
int _stale_lookup;
int _reply_to_unknown_peer;
int _default_reply_port;
int64_t _cache_generation;
};
//----------------------------------------------------------------
// Class PeerConfigData -- deal with peer ICP configuration data
//----------------------------------------------------------------
class PeerConfigData
{
friend class ICPConfiguration;
friend class ICPProcessor;
public:
PeerConfigData();
PeerConfigData(int ctype, IpAddr const &ip_addr, int proxy_port, int icp_port)
: _ctype(ctype), _ip_addr(ip_addr), _proxy_port(proxy_port), _icp_port(icp_port), _mc_member(0), _mc_ttl(0),
_my_ip_addr(ip_addr)
{
_hostname[0] = 0;
}
~PeerConfigData() {}
bool operator==(PeerConfigData &);
inline const char *
GetHostname()
{
return _hostname;
}
inline int
GetCType()
{
return _ctype;
}
inline IpAddr const &
GetIPAddr()
{
return _my_ip_addr;
}
inline int
GetProxyPort()
{
return _proxy_port;
}
inline int
GetICPPort()
{
return _icp_port;
}
inline int
MultiCastMember()
{
return _mc_member;
}
inline IpAddr const &
GetMultiCastIPAddr()
{
return _mc_ip_addr;
}
inline int
GetMultiCastTTL()
{
return _mc_ttl;
}
// Static member functions
static PeerType_t CTypeToPeerType_t(int);
static int GetHostIPByName(char *, IpAddr &);
enum {
HOSTNAME_SIZE = 256,
};
enum {
CTYPE_NONE = 0,
CTYPE_PARENT = 1,
CTYPE_SIBLING = 2,
CTYPE_LOCAL = 3,
};
private:
//---------------------------------------------------------
// Peer Configuration data derived from "icp.config"
//---------------------------------------------------------
char _hostname[HOSTNAME_SIZE];
int _ctype;
IpAddr _ip_addr;
int _proxy_port;
int _icp_port;
//-------------------
// MultiCast data
//-------------------
int _mc_member;
IpAddr _mc_ip_addr;
int _mc_ttl;
//----------------------------------------------
// Computed data not subject to "==" test
//----------------------------------------------
IpAddr _my_ip_addr;
};
//---------------------------------------------------------------
// Class ICPConfigUpdateCont -- Continuation which retries
// icp_config_change_callback(). Continuation started
// due to manager config callout or failure to acquire lock.
//---------------------------------------------------------------
class ICPConfigUpdateCont : public Continuation
{
public:
ICPConfigUpdateCont(void *data, void *value);
~ICPConfigUpdateCont() {}
int RetryICPconfigUpdate(int, Event *);
enum {
RETRY_INTERVAL = 10,
};
private:
void *_data;
void *_value;
};
//------------------------------------------------------------------
// Class ICPConfiguration -- Overall management of ICP Config data
//------------------------------------------------------------------
class ICPConfiguration
{
public:
ICPConfiguration();
~ICPConfiguration();
int GlobalConfigChange();
void UpdateGlobalConfig();
int PeerConfigChange();
void UpdatePeerConfig();
inline ICPConfigData *
globalConfig()
{
return _icp_cdata;
}
inline PeerConfigData *
indexToPeerConfigData(int index)
{
ink_assert(index <= MAX_DEFINED_PEERS);
return _peer_cdata[index];
}
// TS configuration management callout for "icp.config".
static int mgr_icp_config_change_callback(const char *, RecDataT, RecData, void *);
// ICP configuration callout for ET_ICP
static void *icp_config_change_callback(void *, void *, int startup = 0);
inline int
Lock()
{
return _l.Lock();
}
inline void
Unlock()
{
_l.Unlock();
return;
}
inline int
HaveLock()
{
return _l.HaveLock();
}
inline int
ICPConfigCallouts()
{
return _icp_config_callouts;
}
private:
// Class data declarations
AtomicLock _l;
int _icp_config_callouts;
// All ICP operation is based on "icp_data" and "peer_cdata".
// The "icp_data_current" and "peer_cdata_current" reflect the
// current state of the configuration. "icp_data_current" is
// updated via configuration callouts. "peer_cdata_current"
// is updated by the periodic ICP processor event (ICPPeriodicCont),
// when configuration management signals us with a callout on "icp.config".
// We merge current to working only after disabling ICP operation and
// waiting for pending requests to complete.
//
ICPConfigData *_icp_cdata;
ICPConfigData *_icp_cdata_current;
PeerConfigData *_peer_cdata[MAX_DEFINED_PEERS + 1];
PeerConfigData *_peer_cdata_current[MAX_DEFINED_PEERS + 1];
};
//------------------------------------------------------------------------
// Class Peer -- Internal structure representing ICP peers derived from
// configuration data (abstract base class).
//------------------------------------------------------------------------
// Peer state
#define PEER_UP (1 << 0)
#define PEER_MULTICAST_COUNT_EVENT (1 << 1) // Member probe event active
#define PEER_DYNAMIC (1 << 2) // Dynamically added, not in config
struct CacheVConnection;
class Peer : public RefCountObj
{
public:
Peer(PeerType_t, ICPProcessor *, bool dynamic_peer = false);
virtual ~Peer() {}
void LogRecvMsg(ICPMsg_t *, int);
// Pure virtual functions
virtual sockaddr *GetIP() = 0;
virtual Action *SendMsg_re(Continuation *, void *, struct msghdr *, struct sockaddr const *to) = 0;
virtual Action *RecvFrom_re(Continuation *, void *, IOBufferBlock *, int, struct sockaddr *, socklen_t *) = 0;
virtual int GetRecvFD() = 0;
virtual int GetSendFD() = 0;
virtual int ExpectedReplies(BitMap *) = 0;
virtual int ValidSender(sockaddr *) = 0;
virtual void LogSendMsg(ICPMsg_t *, sockaddr const *) = 0;
virtual int IsOnline() = 0;
virtual Connection *GetSendChan() = 0;
virtual Connection *GetRecvChan() = 0;
virtual int ExtToIntRecvSockAddr(sockaddr const *, sockaddr *) = 0;
enum {
OFFLINE_THRESHOLD = 20,
};
inline PeerType_t
GetType()
{
return _type;
}
inline int
GetPeerID()
{
return _id;
}
inline void
SetPeerID(int newid)
{
_id = newid;
}
inline void
SetNext(Peer *p)
{
_next = p;
}
inline Peer *
GetNext()
{
return _next;
}
inline bool
shouldStartRead()
{
return !notFirstRead;
}
inline void
startingRead()
{
notFirstRead = 1;
}
inline void
cancelRead()
{
notFirstRead = 0;
}
inline bool
readActive()
{
return (readAction != NULL);
}
inline bool
isUp()
{
return (_state & PEER_UP);
}
// these shouldn't be public
// this is for delayed I/O
Ptr<IOBufferBlock> buf;
IpEndpoint fromaddr;
socklen_t fromaddrlen;
int notFirstRead; // priming the reads
Action *readAction; // outstanding read
Action *writeAction; // outstanding write
protected:
PeerType_t _type;
int _id; // handle for this peer
Peer *_next;
ICPProcessor *_ICPpr;
//--------------
// State data
//--------------
int _state;
//-------------------
// Peer Statistics
//-------------------
struct PeerStats {
ink_hrtime last_send;
ink_hrtime last_receive;
int sent[ICP_OP_LAST + 1];
int recv[ICP_OP_LAST + 1];
int total_sent;
int total_received;
int dropped_replies; // arrived after timeout
} _stats;
};
//------------------------------------------------
// Class ParentSiblingPeer (derived from Peer)
//------------------------------------------------
class ParentSiblingPeer : public Peer
{
public:
ParentSiblingPeer(PeerType_t, PeerConfigData *, ICPProcessor *, bool dynamic_peer = false);
~ParentSiblingPeer()
{
if (_pconfig && (_state & PEER_DYNAMIC))
delete _pconfig;
}
int GetProxyPort();
int GetICPPort();
virtual sockaddr *GetIP();
virtual Action *SendMsg_re(Continuation *, void *, struct msghdr *, struct sockaddr const *to);
virtual Action *RecvFrom_re(Continuation *, void *, IOBufferBlock *, int, struct sockaddr *, socklen_t *);
virtual int GetRecvFD();
virtual int GetSendFD();
virtual int ExpectedReplies(BitMap *);
virtual int ValidSender(struct sockaddr *);
virtual void LogSendMsg(ICPMsg_t *, sockaddr const *);
virtual int ExtToIntRecvSockAddr(sockaddr const *in, sockaddr *out);
inline virtual int
IsOnline()
{
return 1;
}
inline virtual Connection *
GetSendChan()
{
return &_chan;
}
inline virtual Connection *
GetRecvChan()
{
return &_chan;
}
inline PeerConfigData *
GetConfig()
{
return _pconfig;
}
inline Connection *
GetChan()
{
return &_chan;
}
private:
// Class data declarations
PeerConfigData *_pconfig; // associated config data
IpEndpoint _ip; ///< Cache for GetIP().
Connection _chan;
};
//------------------------------------------------
// Class MultiCastPeer (derived from Peer)
//------------------------------------------------
class MultiCastPeer : public Peer
{
public:
MultiCastPeer(IpAddr const &, uint16_t, int, ICPProcessor *);
~MultiCastPeer() {}
int GetTTL();
int AddMultiCastChild(Peer *P);
/** Find the multicast child peer with IP address @a ip on @a port.
If @a port is 0 the port is not checked.
*/
Peer *FindMultiCastChild(IpAddr const &ip, ///< IP address.
uint16_t port = 0 ///< Port (host order).
);
virtual sockaddr *GetIP();
virtual Action *SendMsg_re(Continuation *, void *, struct msghdr *, struct sockaddr const *to);
virtual Action *RecvFrom_re(Continuation *, void *, IOBufferBlock *, int, struct sockaddr *, socklen_t *);
virtual int GetRecvFD();
virtual int GetSendFD();
virtual int ExpectedReplies(BitMap *);
virtual int ValidSender(struct sockaddr *);
virtual void LogSendMsg(ICPMsg_t *, sockaddr const *);
virtual int IsOnline();
inline virtual Connection *
GetRecvChan()
{
return &_recv_chan;
}
inline virtual Connection *
GetSendChan()
{
return &_send_chan;
}
inline virtual int
ExtToIntRecvSockAddr(sockaddr const *in, sockaddr *out)
{
Peer *P = FindMultiCastChild(IpAddr(in));
if (P) {
ats_ip_copy(out, in);
ats_ip_port_cast(out) = ats_ip_port_cast(P->GetIP());
return 1;
} else {
return 0;
}
}
private:
// Class data declarations
Connection _send_chan;
Connection _recv_chan;
//---------------------------
// Multicast specific data
//---------------------------
IpEndpoint _mc_ip;
int _mc_ttl;
struct multicast_data {
double avg_members; // running avg of multicast responders
int defined_members; // as specified in icp.config
int n_count_events; // responder count events
int count_event_reqno; // reqno associated with count event
int expected_replies; // current expected responders on multicast
} _mc;
};
//----------------------------------------------------
// Class BitMap -- Generic bit map management class
//----------------------------------------------------
class BitMap
{
public:
BitMap(int);
~BitMap();
void SetBit(int);
void ClearBit(int);
int IsBitSet(int);
private:
enum {
STATIC_BITMAP_BYTE_SIZE = 16,
BITS_PER_BYTE = 8,
};
char _static_bitmap[STATIC_BITMAP_BYTE_SIZE];
char *_bitmap;
int _bitmap_size;
int _bitmap_byte_size;
};
//----------------------------------------
// ICPProcessor -- ICP External interface
//----------------------------------------
class ICPProcessor
{
friend class ICPHandlerCont; // Incoming msg periodic handler
friend class ICPPeerReadCont; // Incoming ICP request handler
friend class ICPRequestCont; // Outgoing ICP request handler
public:
ICPProcessor();
~ICPProcessor();
// Exported interfaces for other subsystems
void start();
Action *ICPQuery(Continuation *, URL *);
// Exported interfaces to other ICP classes
typedef enum {
RC_RECONFIG,
RC_ENABLE_ICP,
RC_DONE,
} ReconfigState_t;
ReconfigState_t ReconfigureStateMachine(ReconfigState_t, int, int);
Peer *FindPeer(IpAddr const &ip, uint16_t port = 0);
Peer *
FindPeer(IpEndpoint const &ip)
{
return this->FindPeer(IpAddr(&ip), ats_ip_port_host_order(&ip));
}
Peer *
FindPeer(sockaddr const *ip)
{
return this->FindPeer(IpAddr(ip), ats_ip_port_host_order(ip));
}
inline Peer *
GetLocalPeer()
{
return _LocalPeer;
}
inline Peer *
IdToPeer(int id)
{
return _PeerList[id];
}
inline ICPConfiguration *
GetConfig()
{
return _ICPConfig;
}
inline int
GetFreePeers()
{
return PEER_LIST_SIZE - (_nPeerList + 1);
}
inline int
GetFreeSendPeers()
{
return SEND_PEER_LIST_SIZE - (_nSendPeerList + 1);
}
inline int
GetFreeRecvPeers()
{
return RECV_PEER_LIST_SIZE - (_nRecvPeerList + 1);
}
private:
inline int
Lock()
{
return _l->Lock();
}
inline void
Unlock()
{
_l->Unlock();
return;
}
inline int
HaveLock()
{
return _l->HaveLock();
}
int BuildPeerList();
void FreePeerList();
int SetupListenSockets();
void ShutdownListenSockets();
int Reconfigure(int, int);
void InitICPStatCallbacks();
inline void
DisableICPQueries()
{
_AllowIcpQueries = 0;
}
inline void
EnableICPQueries()
{
_AllowIcpQueries = 1;
}
inline int
AllowICPQueries()
{
return _AllowIcpQueries;
}
inline int
PendingQuery()
{
return _PendingIcpQueries;
}
inline void
IncPendingQuery()
{
_PendingIcpQueries++;
}
inline void
DecPendingQuery()
{
_PendingIcpQueries--;
}
Peer *GenericFindListPeer(IpAddr const &, uint16_t, int, Ptr<Peer> *);
Peer *FindSendListPeer(IpAddr const &, uint16_t);
Peer *FindRecvListPeer(IpAddr const &, uint16_t);
int AddPeer(Peer *);
int AddPeerToSendList(Peer *);
int AddPeerToRecvList(Peer *);
int AddPeerToParentList(Peer *);
inline int
GetSendPeers()
{
return _nSendPeerList + 1;
}
inline Peer *
GetNthSendPeer(int n, int bias)
{
return _SendPeerList[(bias + n) % (_nSendPeerList + 1)];
}
inline int
GetRecvPeers()
{
return _nRecvPeerList + 1;
}
inline Peer *
GetNthRecvPeer(int n, int bias)
{
return _RecvPeerList[(bias + n) % (_nRecvPeerList + 1)];
}
inline int
GetStartingSendPeerBias()
{
return ++_curSendPeer;
}
inline int
GetStartingRecvPeerBias()
{
return ++_curRecvPeer;
}
inline int
GetParentPeers()
{
return _nParentPeerList + 1;
}
inline Peer *
GetNthParentPeer(int n, int bias)
{
return _ParentPeerList[(bias + n) % (_nParentPeerList + 1)];
}
inline int
GetStartingParentPeerBias()
{
return ++_curParentPeer;
}
inline void
SetLastRecvPeerBias(int b)
{
_last_recv_peer_bias = b;
}
inline int
GetLastRecvPeerBias()
{
return _last_recv_peer_bias;
}
void CancelPendingReads();
void DumpICPConfig();
private:
// Class data declarations
AtomicLock *_l;
int _Initialized;
int _AllowIcpQueries;
int _PendingIcpQueries;
ICPConfiguration *_ICPConfig;
ICPPeriodicCont *_ICPPeriodic;
ICPHandlerCont *_ICPHandler;
ICPHandlerCont *_mcastCB_handler;
Event *_PeriodicEvent;
Event *_ICPHandlerEvent;
enum {
PEER_LIST_SIZE = 2 * MAX_DEFINED_PEERS,
SEND_PEER_LIST_SIZE = 2 * MAX_DEFINED_PEERS,
RECV_PEER_LIST_SIZE = 2 * MAX_DEFINED_PEERS,
PARENT_PEER_LIST_SIZE = 2 * MAX_DEFINED_PEERS,
PEER_ID_POLL_INDEX_SIZE = 2 * MAX_DEFINED_PEERS
};
// All Peer elements
int _nPeerList; // valid PeerList[] entries - 1
Ptr<Peer> _PeerList[PEER_LIST_SIZE];
Ptr<Peer> _LocalPeer;
// Peers which are targets of ICP queries
int _curSendPeer; // index bias for SendPeerList[]
int _nSendPeerList; // valid SendPeerList[] entries - 1
Ptr<Peer> _SendPeerList[SEND_PEER_LIST_SIZE];
// List of Peers whom we issue reads from
int _curRecvPeer; // index bias for RecvPeerList[]
int _nRecvPeerList; // valid RecvPeerList[] entries - 1
Ptr<Peer> _RecvPeerList[RECV_PEER_LIST_SIZE];