]> git.ipfire.org Git - thirdparty/squid.git/blob - src/delay_pools.cc
Various audit updates
[thirdparty/squid.git] / src / delay_pools.cc
1 /*
2 * DEBUG: section 77 Delay Pools
3 * AUTHOR: Robert Collins <robertc@squid-cache.org>
4 * Based upon original delay pools code by
5 * David Luyer <david@luyer.net>
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 *
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 */
36
37 /**
38 \defgroup DelayPoolsInternal Delay Pools Internal
39 \ingroup DelayPoolsAPI
40 */
41
42 #include "squid.h"
43
44 #if USE_DELAY_POOLS
45 #include "client_side_request.h"
46 #include "comm/Connection.h"
47 #include "CommonPool.h"
48 #include "CompositePoolNode.h"
49 #include "ConfigParser.h"
50 #include "DelayBucket.h"
51 #include "DelayId.h"
52 #include "DelayPool.h"
53 #include "DelayPools.h"
54 #include "DelaySpec.h"
55 #include "DelayTagged.h"
56 #include "DelayUser.h"
57 #include "DelayVector.h"
58 #include "event.h"
59 #include "ip/Address.h"
60 #include "MemObject.h"
61 #include "mgr/Registration.h"
62 #include "NullDelayId.h"
63 #include "SquidString.h"
64 #include "SquidTime.h"
65 #include "Store.h"
66 #include "StoreClient.h"
67
68 /// \ingroup DelayPoolsInternal
69 long DelayPools::MemoryUsed = 0;
70
71 /// \ingroup DelayPoolsInternal
72 class Aggregate : public CompositePoolNode
73 {
74
75 public:
76 typedef RefCount<Aggregate> Pointer;
77 void *operator new(size_t);
78 void operator delete (void *);
79 Aggregate();
80 ~Aggregate();
81 virtual DelaySpec *rate() {return &spec;}
82
83 virtual DelaySpec const *rate() const {return &spec;}
84
85 virtual void stats(StoreEntry * sentry);
86 virtual void dump(StoreEntry *entry) const;
87 virtual void update(int incr);
88 virtual void parse();
89
90 virtual DelayIdComposite::Pointer id(CompositeSelectionDetails &);
91
92 private:
93
94 /// \ingroup DelayPoolsInternal
95 class AggregateId:public DelayIdComposite
96 {
97
98 public:
99 void *operator new(size_t);
100 void operator delete (void *);
101 AggregateId (RefCount<Aggregate>);
102 virtual int bytesWanted (int min, int max) const;
103 virtual void bytesIn(int qty);
104 virtual void delayRead(DeferredRead const &);
105
106 private:
107 RefCount<Aggregate> theAggregate;
108 };
109
110 friend class AggregateId;
111
112 DelayBucket theBucket;
113 DelaySpec spec;
114 };
115
116 /// \ingroup DelayPoolsInternal
117 template <class Key, class Value>
118 class VectorMap
119 {
120
121 public:
122 VectorMap();
123 unsigned int size() const;
124 unsigned char findKeyIndex (Key const key) const;
125 bool indexUsed (unsigned char const index) const;
126 unsigned int insert (Key const key);
127
128 #define IND_MAP_SZ 256
129
130 Key key_map[IND_MAP_SZ];
131 Value values[IND_MAP_SZ];
132
133 private:
134 unsigned int nextMapPosition;
135 };
136
137 /// \ingroup DelayPoolsInternal
138 class VectorPool : public CompositePoolNode
139 {
140
141 public:
142 typedef RefCount<VectorPool> Pointer;
143 virtual void dump(StoreEntry *entry) const;
144 virtual void parse();
145 virtual void update(int incr);
146 virtual void stats(StoreEntry * sentry);
147
148 virtual DelayIdComposite::Pointer id(CompositeSelectionDetails &);
149 VectorMap<unsigned char, DelayBucket> buckets;
150 VectorPool();
151 ~VectorPool();
152
153 protected:
154 bool keyAllocated (unsigned char const key) const;
155 virtual DelaySpec *rate() {return &spec;}
156
157 virtual DelaySpec const *rate() const {return &spec;}
158
159 virtual char const *label() const = 0;
160
161 virtual unsigned int makeKey(Ip::Address &src_addr) const = 0;
162
163 DelaySpec spec;
164
165 /// \ingroup DelayPoolsInternal
166 class Id:public DelayIdComposite
167 {
168
169 public:
170 void *operator new(size_t);
171 void operator delete (void *);
172 Id (RefCount<VectorPool>, int);
173 virtual int bytesWanted (int min, int max) const;
174 virtual void bytesIn(int qty);
175
176 private:
177 RefCount<VectorPool> theVector;
178 int theIndex;
179 };
180 };
181
182 /// \ingroup DelayPoolsInternal
183 class IndividualPool : public VectorPool
184 {
185
186 public:
187 void *operator new(size_t);
188 void operator delete(void *);
189
190 protected:
191 virtual char const *label() const {return "Individual";}
192 virtual unsigned int makeKey(Ip::Address &src_addr) const;
193 };
194
195 /// \ingroup DelayPoolsInternal
196 class ClassCNetPool : public VectorPool
197 {
198
199 public:
200 void *operator new(size_t);
201 void operator delete (void *);
202
203 protected:
204 virtual char const *label() const {return "Network";}
205 virtual unsigned int makeKey (Ip::Address &src_addr) const;
206 };
207
208 /* don't use remote storage for these */
209 /// \ingroup DelayPoolsInternal
210 class ClassCBucket
211 {
212
213 public:
214 bool individualUsed (unsigned int index)const;
215 unsigned char findHostMapPosition (unsigned char const host) const;
216 bool individualAllocated (unsigned char host) const;
217 unsigned char hostPosition (DelaySpec &rate, unsigned char const host);
218 void initHostIndex (DelaySpec &rate, unsigned char index, unsigned char host);
219 void update (DelaySpec const &, int incr);
220 void stats(StoreEntry *)const;
221
222 DelayBucket net;
223 VectorMap<unsigned char, DelayBucket> individuals;
224 };
225
226 /// \ingroup DelayPoolsInternal
227 class ClassCHostPool : public CompositePoolNode
228 {
229
230 public:
231 typedef RefCount<ClassCHostPool> Pointer;
232 virtual void dump(StoreEntry *entry) const;
233 virtual void parse();
234 virtual void update(int incr);
235 virtual void stats(StoreEntry * sentry);
236
237 virtual DelayIdComposite::Pointer id(CompositeSelectionDetails &);
238 ClassCHostPool();
239 ~ClassCHostPool();
240
241 protected:
242 bool keyAllocated (unsigned char const key) const;
243 virtual DelaySpec *rate() {return &spec;}
244
245 virtual DelaySpec const *rate() const {return &spec;}
246
247 virtual char const *label() const {return "Individual";}
248
249 virtual unsigned int makeKey(Ip::Address &src_addr) const;
250
251 unsigned char makeHostKey(Ip::Address &src_addr) const;
252
253 DelaySpec spec;
254 VectorMap<unsigned char, ClassCBucket> buckets;
255
256 class Id;
257
258 friend class ClassCHostPool::Id;
259
260 /// \ingroup DelayPoolsInternal
261 class Id:public DelayIdComposite
262 {
263
264 public:
265 void *operator new(size_t);
266 void operator delete (void *);
267 Id (RefCount<ClassCHostPool>, unsigned char, unsigned char);
268 virtual int bytesWanted (int min, int max) const;
269 virtual void bytesIn(int qty);
270
271 private:
272 RefCount<ClassCHostPool> theClassCHost;
273 unsigned char theNet;
274 unsigned char theHost;
275 };
276 };
277
278 void
279 Aggregate::AggregateId::delayRead(DeferredRead const &aRead)
280 {
281 theAggregate->delayRead(aRead);
282 }
283
284 void *
285 CommonPool::operator new(size_t size)
286 {
287 DelayPools::MemoryUsed += sizeof (CommonPool);
288 return ::operator new (size);
289 }
290
291 void
292 CommonPool::operator delete(void *address)
293 {
294 DelayPools::MemoryUsed -= sizeof(CommonPool);
295 ::operator delete(address);
296 }
297
298 CommonPool *
299 CommonPool::Factory(unsigned char _class, CompositePoolNode::Pointer& compositeCopy)
300 {
301 CommonPool *result = new CommonPool;
302
303 switch (_class) {
304
305 case 0:
306 break;
307
308 case 1:
309 compositeCopy = new Aggregate;
310 result->typeLabel = "1";
311 break;
312
313 case 2:
314 result->typeLabel = "2";
315 {
316 DelayVector::Pointer temp = new DelayVector;
317 compositeCopy = temp.getRaw();
318 temp->push_back (new Aggregate);
319 temp->push_back(new IndividualPool);
320 }
321 break;
322
323 case 3:
324 result->typeLabel = "3";
325 {
326 DelayVector::Pointer temp = new DelayVector;
327 compositeCopy = temp.getRaw();
328 temp->push_back (new Aggregate);
329 temp->push_back (new ClassCNetPool);
330 temp->push_back (new ClassCHostPool);
331 }
332 break;
333
334 case 4:
335 result->typeLabel = "4";
336 {
337 DelayVector::Pointer temp = new DelayVector;
338 compositeCopy = temp.getRaw();
339 temp->push_back (new Aggregate);
340 temp->push_back (new ClassCNetPool);
341 temp->push_back (new ClassCHostPool);
342 #if USE_AUTH
343 temp->push_back (new DelayUser);
344 #endif
345 }
346 break;
347
348 case 5:
349 result->typeLabel = "5";
350 compositeCopy = new DelayTagged;
351 break;
352
353 default:
354 fatal ("unknown delay pool class");
355 return NULL;
356 };
357
358 return result;
359 }
360
361 CommonPool::CommonPool()
362 {}
363
364 void
365 ClassCBucket::update (DelaySpec const &rate, int incr)
366 {
367 /* If we aren't active, don't try to update us ! */
368 assert (rate.restore_bps != -1);
369
370 for (unsigned int j = 0; j < individuals.size(); ++j)
371 individuals.values[j].update (rate, incr);
372 }
373
374 void
375 ClassCBucket::stats(StoreEntry *sentry)const
376 {
377 for (unsigned int j = 0; j < individuals.size(); ++j) {
378 assert (individualUsed (j));
379 storeAppendPrintf(sentry, " %d:",individuals.key_map[j]);
380 individuals.values[j].stats (sentry);
381 }
382 }
383
384 unsigned char
385 ClassCBucket::findHostMapPosition (unsigned char const host) const
386 {
387 return individuals.findKeyIndex(host);
388 }
389
390 bool
391 ClassCBucket::individualUsed (unsigned int index)const
392 {
393 return individuals.indexUsed(index);
394 }
395
396 bool
397 ClassCBucket::individualAllocated (unsigned char host) const
398 {
399 return individualUsed(findHostMapPosition (host));
400 }
401
402 unsigned char
403 ClassCBucket::hostPosition (DelaySpec &rate, unsigned char const host)
404 {
405 if (individualAllocated (host))
406 return findHostMapPosition(host);
407
408 assert (!individualUsed (findHostMapPosition(host)));
409
410 unsigned char result = findHostMapPosition(host);
411
412 initHostIndex (rate, result, host);
413
414 return result;
415 }
416
417 void
418 ClassCBucket::initHostIndex (DelaySpec &rate, unsigned char index, unsigned char host)
419 {
420 assert (!individualUsed(index));
421
422 unsigned char const newIndex = individuals.insert (host);
423
424 /* give the bucket a default value */
425 individuals.values[newIndex].init (rate);
426 }
427
428 void *
429 CompositePoolNode::operator new(size_t size)
430 {
431 DelayPools::MemoryUsed += sizeof (CompositePoolNode);
432 return ::operator new (size);
433 }
434
435 void
436 CompositePoolNode::operator delete (void *address)
437 {
438 DelayPools::MemoryUsed -= sizeof (CompositePoolNode);
439 ::operator delete (address);
440 }
441
442 void *
443 Aggregate::operator new(size_t size)
444 {
445 DelayPools::MemoryUsed += sizeof (Aggregate);
446 return ::operator new (size);
447 }
448
449 void
450 Aggregate::operator delete (void *address)
451 {
452 DelayPools::MemoryUsed -= sizeof (Aggregate);
453 ::operator delete (address);
454 }
455
456 Aggregate::Aggregate()
457 {
458 theBucket.init (*rate());
459 DelayPools::registerForUpdates (this);
460 }
461
462 Aggregate::~Aggregate()
463 {
464 DelayPools::deregisterForUpdates (this);
465 }
466
467 void
468 Aggregate::stats(StoreEntry * sentry)
469 {
470 rate()->stats (sentry, "Aggregate");
471
472 if (rate()->restore_bps == -1)
473 return;
474
475 storeAppendPrintf(sentry, "\t\tCurrent: ");
476
477 theBucket.stats(sentry);
478
479 storeAppendPrintf(sentry, "\n\n");
480 }
481
482 void
483 Aggregate::dump(StoreEntry *entry) const
484 {
485 rate()->dump (entry);
486 }
487
488 void
489 Aggregate::update(int incr)
490 {
491 theBucket.update(*rate(), incr);
492 kickReads();
493 }
494
495 void
496 Aggregate::parse()
497 {
498 rate()->parse();
499 }
500
501 DelayIdComposite::Pointer
502 Aggregate::id(CompositeSelectionDetails &details)
503 {
504 if (rate()->restore_bps != -1)
505 return new AggregateId (this);
506 else
507 return new NullDelayId;
508 }
509
510 void *
511 Aggregate::AggregateId::operator new(size_t size)
512 {
513 DelayPools::MemoryUsed += sizeof (AggregateId);
514 return ::operator new (size);
515 }
516
517 void
518 Aggregate::AggregateId::operator delete (void *address)
519 {
520 DelayPools::MemoryUsed -= sizeof (AggregateId);
521 ::operator delete (address);
522 }
523
524 Aggregate::AggregateId::AggregateId(RefCount<Aggregate> anAggregate) : theAggregate(anAggregate)
525 {}
526
527 int
528 Aggregate::AggregateId::bytesWanted (int min, int max) const
529 {
530 return theAggregate->theBucket.bytesWanted(min, max);
531 }
532
533 void
534 Aggregate::AggregateId::bytesIn(int qty)
535 {
536 theAggregate->theBucket.bytesIn(qty);
537 theAggregate->kickReads();
538 }
539
540 DelayPool *DelayPools::delay_data = NULL;
541 time_t DelayPools::LastUpdate = 0;
542 unsigned short DelayPools::pools_ (0);
543
544 void
545 DelayPools::RegisterWithCacheManager(void)
546 {
547 Mgr::RegisterAction("delay", "Delay Pool Levels", Stats, 0, 1);
548 }
549
550 void
551 DelayPools::Init()
552 {
553 LastUpdate = getCurrentTime();
554 RegisterWithCacheManager();
555 }
556
557 void
558 DelayPools::InitDelayData()
559 {
560 if (!pools())
561 return;
562
563 DelayPools::delay_data = new DelayPool[pools()];
564
565 DelayPools::MemoryUsed += pools() * sizeof(DelayPool);
566
567 eventAdd("DelayPools::Update", DelayPools::Update, NULL, 1.0, 1);
568 }
569
570 void
571 DelayPools::FreeDelayData()
572 {
573 eventDelete(DelayPools::Update, NULL);
574 delete[] DelayPools::delay_data;
575 DelayPools::MemoryUsed -= pools() * sizeof(*DelayPools::delay_data);
576 pools_ = 0;
577 }
578
579 void
580 DelayPools::Update(void *unused)
581 {
582 if (!pools())
583 return;
584
585 eventAdd("DelayPools::Update", Update, NULL, 1.0, 1);
586
587 int incr = squid_curtime - LastUpdate;
588
589 if (incr < 1)
590 return;
591
592 LastUpdate = squid_curtime;
593
594 std::vector<Updateable *>::iterator pos = toUpdate.begin();
595
596 while (pos != toUpdate.end()) {
597 (*pos)->update(incr);
598 ++pos;
599 }
600 }
601
602 void
603 DelayPools::registerForUpdates(Updateable *anObject)
604 {
605 /* Assume no doubles */
606 toUpdate.push_back(anObject);
607 }
608
609 void
610 DelayPools::deregisterForUpdates (Updateable *anObject)
611 {
612 std::vector<Updateable *>::iterator pos = toUpdate.begin();
613
614 while (pos != toUpdate.end() && *pos != anObject) {
615 ++pos;
616 }
617
618 if (pos != toUpdate.end()) {
619 /* move all objects down one */
620 std::vector<Updateable *>::iterator temp = pos;
621 ++pos;
622
623 while (pos != toUpdate.end()) {
624 *temp = *pos;
625 ++temp;
626 ++pos;
627 }
628
629 toUpdate.pop_back();
630 }
631 }
632
633 std::vector<Updateable *> DelayPools::toUpdate;
634
635 void
636 DelayPools::Stats(StoreEntry * sentry)
637 {
638 unsigned short i;
639
640 storeAppendPrintf(sentry, "Delay pools configured: %d\n\n", DelayPools::pools());
641
642 for (i = 0; i < DelayPools::pools(); ++i) {
643 if (DelayPools::delay_data[i].theComposite().getRaw()) {
644 storeAppendPrintf(sentry, "Pool: %d\n\tClass: %s\n\n", i + 1, DelayPools::delay_data[i].pool->theClassTypeLabel());
645 DelayPools::delay_data[i].theComposite()->stats (sentry);
646 } else
647 storeAppendPrintf(sentry, "\tMisconfigured pool.\n\n");
648 }
649
650 storeAppendPrintf(sentry, "Memory Used: %d bytes\n", (int) DelayPools::MemoryUsed);
651 }
652
653 void
654 DelayPools::FreePools()
655 {
656 if (!DelayPools::pools())
657 return;
658
659 FreeDelayData();
660 }
661
662 unsigned short
663 DelayPools::pools()
664 {
665 return pools_;
666 }
667
668 void
669 DelayPools::pools(unsigned short newPools)
670 {
671 if (pools()) {
672 debugs(3, DBG_CRITICAL, "parse_delay_pool_count: multiple delay_pools lines, aborting all previous delay_pools config");
673 FreePools();
674 }
675
676 pools_ = newPools;
677
678 if (pools())
679 InitDelayData();
680 }
681
682 template <class Key, class Value>
683 VectorMap<Key,Value>::VectorMap() : nextMapPosition(0)
684 {}
685
686 template <class Key, class Value>
687 unsigned int
688 VectorMap<Key,Value>::size() const
689 {
690 return nextMapPosition;
691 }
692
693 template <class Key, class Value>
694 unsigned int
695 VectorMap<Key,Value>::insert (Key const key)
696 {
697 unsigned char index = findKeyIndex (key);
698 assert (!indexUsed(index));
699
700 key_map[index] = key;
701
702 ++nextMapPosition;
703
704 return index;
705 }
706
707 void *
708 IndividualPool::operator new(size_t size)
709 {
710 DelayPools::MemoryUsed += sizeof (IndividualPool);
711 return ::operator new (size);
712 }
713
714 void
715 IndividualPool::operator delete (void *address)
716 {
717 DelayPools::MemoryUsed -= sizeof (IndividualPool);
718 ::operator delete (address);
719 }
720
721 VectorPool::VectorPool()
722 {
723 DelayPools::registerForUpdates (this);
724 }
725
726 VectorPool::~VectorPool()
727 {
728 DelayPools::deregisterForUpdates (this);
729 }
730
731 void
732 VectorPool::stats(StoreEntry * sentry)
733 {
734 rate()->stats (sentry, label());
735
736 if (rate()->restore_bps == -1) {
737 storeAppendPrintf(sentry, "\n\n");
738 return;
739 }
740
741 storeAppendPrintf(sentry, "\t\tCurrent:");
742
743 for (unsigned int i = 0; i < buckets.size(); ++i) {
744 storeAppendPrintf(sentry, " %d:", buckets.key_map[i]);
745 buckets.values[i].stats(sentry);
746 }
747
748 if (!buckets.size())
749 storeAppendPrintf(sentry, " Not used yet.");
750
751 storeAppendPrintf(sentry, "\n\n");
752 }
753
754 void
755 VectorPool::dump(StoreEntry *entry) const
756 {
757 rate()->dump (entry);
758 }
759
760 void
761 VectorPool::update(int incr)
762 {
763 if (rate()->restore_bps == -1)
764 return;
765
766 for (unsigned int i = 0; i< buckets.size(); ++i)
767 buckets.values[i].update (*rate(), incr);
768 }
769
770 void
771 VectorPool::parse()
772 {
773 rate()->parse();
774 }
775
776 bool
777 VectorPool::keyAllocated (unsigned char const key) const
778 {
779 return buckets.indexUsed(buckets.findKeyIndex (key));
780 }
781
782 template <class Key, class Value>
783 bool
784 VectorMap<Key,Value>::indexUsed (unsigned char const index) const
785 {
786 return index < size();
787 }
788
789 /** returns the used position, or the position to allocate */
790 template <class Key, class Value>
791 unsigned char
792 VectorMap<Key,Value>::findKeyIndex (Key const key) const
793 {
794 for (unsigned int index = 0; index < size(); ++index) {
795 assert(indexUsed(index));
796
797 if (key_map[index] == key)
798 return index;
799 }
800
801 /* not in map */
802 return size();
803 }
804
805 DelayIdComposite::Pointer
806 VectorPool::id(CompositeSelectionDetails &details)
807 {
808 if (rate()->restore_bps == -1)
809 return new NullDelayId;
810
811 /* non-IPv4 are not able to provide IPv4-bitmask for this pool type key. */
812 if ( !details.src_addr.isIPv4() )
813 return new NullDelayId;
814
815 unsigned int key = makeKey(details.src_addr);
816
817 if (keyAllocated(key))
818 return new Id(this, buckets.findKeyIndex(key));
819
820 unsigned char const resultIndex = buckets.insert(key);
821
822 buckets.values[resultIndex].init(*rate());
823
824 return new Id(this, resultIndex);
825 }
826
827 void *
828 VectorPool::Id::operator new(size_t size)
829 {
830 DelayPools::MemoryUsed += sizeof (Id);
831 return ::operator new (size);
832 }
833
834 void
835 VectorPool::Id::operator delete(void *address)
836 {
837 DelayPools::MemoryUsed -= sizeof (Id);
838 ::operator delete (address);
839 }
840
841 VectorPool::Id::Id(VectorPool::Pointer aPool, int anIndex) : theVector (aPool), theIndex (anIndex)
842 {}
843
844 int
845 VectorPool::Id::bytesWanted (int min, int max) const
846 {
847 return theVector->buckets.values[theIndex].bytesWanted (min, max);
848 }
849
850 void
851 VectorPool::Id::bytesIn(int qty)
852 {
853 theVector->buckets.values[theIndex].bytesIn (qty);
854 }
855
856 unsigned int
857 IndividualPool::makeKey(Ip::Address &src_addr) const
858 {
859 /* IPv4 required for this pool */
860 if ( !src_addr.isIPv4() )
861 return 1;
862
863 struct in_addr host;
864 src_addr.getInAddr(host);
865 return (ntohl(host.s_addr) & 0xff);
866 }
867
868 void *
869 ClassCNetPool::operator new(size_t size)
870 {
871 DelayPools::MemoryUsed += sizeof (ClassCNetPool);
872 return ::operator new (size);
873 }
874
875 void
876 ClassCNetPool::operator delete (void *address)
877 {
878 DelayPools::MemoryUsed -= sizeof (ClassCNetPool);
879 ::operator delete (address);
880 }
881
882 unsigned int
883 ClassCNetPool::makeKey(Ip::Address &src_addr) const
884 {
885 /* IPv4 required for this pool */
886 if ( !src_addr.isIPv4() )
887 return 1;
888
889 struct in_addr net;
890 src_addr.getInAddr(net);
891 return ( (ntohl(net.s_addr) >> 8) & 0xff);
892 }
893
894 ClassCHostPool::ClassCHostPool()
895 {
896 DelayPools::registerForUpdates (this);
897 }
898
899 ClassCHostPool::~ClassCHostPool()
900 {
901 DelayPools::deregisterForUpdates (this);
902 }
903
904 void
905 ClassCHostPool::stats(StoreEntry * sentry)
906 {
907 rate()->stats (sentry, label());
908
909 if (rate()->restore_bps == -1) {
910 storeAppendPrintf(sentry, "\n\n");
911 return;
912 }
913
914 for (unsigned int index = 0; index < buckets.size(); ++index) {
915 storeAppendPrintf(sentry, "\t\tCurrent [Network %d]:", buckets.key_map[index]);
916 buckets.values[index].stats (sentry);
917 storeAppendPrintf(sentry, "\n");
918 }
919
920 if (!buckets.size())
921 storeAppendPrintf(sentry, "\t\tCurrent [All networks]: Not used yet.\n");
922
923 storeAppendPrintf(sentry, "\n\n");
924 }
925
926 void
927 ClassCHostPool::dump(StoreEntry *entry) const
928 {
929 rate()->dump (entry);
930 }
931
932 void
933 ClassCHostPool::update(int incr)
934 {
935 if (rate()->restore_bps == -1)
936 return;
937
938 for (unsigned int i = 0; i< buckets.size(); ++i)
939 buckets.values[i].update (*rate(), incr);
940 }
941
942 void
943 ClassCHostPool::parse()
944 {
945 rate()->parse();
946 }
947
948 bool
949 ClassCHostPool::keyAllocated (unsigned char const key) const
950 {
951 return buckets.indexUsed(buckets.findKeyIndex (key));
952 }
953
954 unsigned char
955 ClassCHostPool::makeHostKey(Ip::Address &src_addr) const
956 {
957 /* IPv4 required for this pool */
958 if ( !src_addr.isIPv4() )
959 return 1;
960
961 /* Temporary bypass for IPv4-only */
962 struct in_addr host;
963 src_addr.getInAddr(host);
964 return (ntohl(host.s_addr) & 0xff);
965 }
966
967 unsigned int
968 ClassCHostPool::makeKey(Ip::Address &src_addr) const
969 {
970 /* IPv4 required for this pool */
971 if ( !src_addr.isIPv4() )
972 return 1;
973
974 struct in_addr net;
975 src_addr.getInAddr(net);
976 return ( (ntohl(net.s_addr) >> 8) & 0xff);
977 }
978
979 DelayIdComposite::Pointer
980 ClassCHostPool::id(CompositeSelectionDetails &details)
981 {
982 if (rate()->restore_bps == -1)
983 return new NullDelayId;
984
985 /* non-IPv4 are not able to provide IPv4-bitmask for this pool type key. */
986 if ( !details.src_addr.isIPv4() )
987 return new NullDelayId;
988
989 unsigned int key = makeKey (details.src_addr);
990
991 unsigned char host = makeHostKey (details.src_addr);
992
993 unsigned char hostIndex;
994
995 unsigned char netIndex;
996
997 if (keyAllocated (key))
998 netIndex = buckets.findKeyIndex(key);
999 else
1000 netIndex = buckets.insert (key);
1001
1002 hostIndex = buckets.values[netIndex].hostPosition (*rate(), host);
1003
1004 return new Id (this, netIndex, hostIndex);
1005 }
1006
1007 void *
1008 ClassCHostPool::Id::operator new(size_t size)
1009 {
1010 DelayPools::MemoryUsed += sizeof (Id);
1011 return ::operator new (size);
1012 }
1013
1014 void
1015 ClassCHostPool::Id::operator delete (void *address)
1016 {
1017 DelayPools::MemoryUsed -= sizeof (Id);
1018 ::operator delete (address);
1019 }
1020
1021 ClassCHostPool::Id::Id (ClassCHostPool::Pointer aPool, unsigned char aNet, unsigned char aHost) : theClassCHost (aPool), theNet (aNet), theHost (aHost)
1022 {}
1023
1024 int
1025 ClassCHostPool::Id::bytesWanted (int min, int max) const
1026 {
1027 return theClassCHost->buckets.values[theNet].individuals.values[theHost].bytesWanted (min, max);
1028 }
1029
1030 void
1031 ClassCHostPool::Id::bytesIn(int qty)
1032 {
1033 theClassCHost->buckets.values[theNet].individuals.values[theHost].bytesIn (qty);
1034 }
1035
1036 #endif /* USE_DELAY_POOLS */