Kea 2.7.7
memfile_lease_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
11#include <dhcpsrv/cfgmgr.h>
13#include <dhcpsrv/dhcpsrv_log.h>
16#include <dhcpsrv/timer_mgr.h>
18#include <stats/stats_mgr.h>
20#include <util/pid_file.h>
21
22#include <boost/foreach.hpp>
23#include <cstdio>
24#include <cstring>
25#include <errno.h>
26#include <iostream>
27#include <limits>
28#include <sstream>
29
30namespace {
31
39const char* KEA_LFC_EXECUTABLE_ENV_NAME = "KEA_LFC_EXECUTABLE";
40
41} // namespace
42
43using namespace isc::asiolink;
44using namespace isc::data;
45using namespace isc::db;
46using namespace isc::util;
47using namespace isc::stats;
48
49namespace isc {
50namespace dhcp {
51
66class LFCSetup {
67public:
68
77
81 ~LFCSetup();
82
94 void setup(const uint32_t lfc_interval,
95 const boost::shared_ptr<CSVLeaseFile4>& lease_file4,
96 const boost::shared_ptr<CSVLeaseFile6>& lease_file6,
97 bool run_once_now = false);
98
100 void execute();
101
105 bool isRunning() const;
106
108 int getExitStatus() const;
109
110private:
111
114 boost::scoped_ptr<ProcessSpawn> process_;
115
118
120 pid_t pid_;
121
126 TimerMgrPtr timer_mgr_;
127};
128
130 : process_(), callback_(callback), pid_(0),
131 timer_mgr_(TimerMgr::instance()) {
132}
133
135 try {
136 // Remove the timer. This will throw an exception if the timer does not
137 // exist. There are several possible reasons for this:
138 // a) It hasn't been registered (although if the LFC Setup instance
139 // exists it means that the timer must have been registered or that
140 // such registration has been attempted).
141 // b) The registration may fail if the duplicate timer exists or if the
142 // TimerMgr's worker thread is running but if this happens it is a
143 // programming error.
144 // c) The program is shutting down and the timer has been removed by
145 // another component.
146 timer_mgr_->unregisterTimer("memfile-lfc");
147
148 } catch (const std::exception& ex) {
149 // We don't want exceptions being thrown from the destructor so we just
150 // log a message here. The message is logged at debug severity as
151 // we don't want an error message output during shutdown.
154 }
155}
156
157void
158LFCSetup::setup(const uint32_t lfc_interval,
159 const boost::shared_ptr<CSVLeaseFile4>& lease_file4,
160 const boost::shared_ptr<CSVLeaseFile6>& lease_file6,
161 bool run_once_now) {
162
163 // If to nothing to do, punt
164 if (lfc_interval == 0 && !run_once_now) {
165 return;
166 }
167
168 // Start preparing the command line for kea-lfc.
169 std::string executable;
170 char* c_executable = getenv(KEA_LFC_EXECUTABLE_ENV_NAME);
171 if (!c_executable) {
172 executable = KEA_LFC_EXECUTABLE;
173 } else {
174 executable = c_executable;
175 }
176
177 // Gather the base file name.
178 std::string lease_file = lease_file4 ? lease_file4->getFilename() :
179 lease_file6->getFilename();
180
181 // Create the other names by appending suffixes to the base name.
182 ProcessArgs args;
183 // Universe: v4 or v6.
184 args.push_back(lease_file4 ? "-4" : "-6");
185
186 // Previous file.
187 args.push_back("-x");
188 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
190 // Input file.
191 args.push_back("-i");
192 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
194 // Output file.
195 args.push_back("-o");
196 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
198 // Finish file.
199 args.push_back("-f");
200 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
202 // PID file.
203 args.push_back("-p");
204 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
206
207 // The configuration file is currently unused.
208 args.push_back("-c");
209 args.push_back("ignored-path");
210
211 // Create the process (do not start it yet).
212 process_.reset(new ProcessSpawn(ProcessSpawn::ASYNC, executable, args,
213 ProcessEnvVars(), true));
214
215 // If we've been told to run it once now, invoke the callback directly.
216 if (run_once_now) {
217 callback_();
218 }
219
220 // If it's supposed to run periodically, setup that now.
221 if (lfc_interval > 0) {
222 // Set the timer to call callback function periodically.
224
225 // Multiple the lfc_interval value by 1000 as this value specifies
226 // a timeout in seconds, whereas the setup() method expects the
227 // timeout in milliseconds.
228 timer_mgr_->registerTimer("memfile-lfc", callback_, lfc_interval * 1000,
230 timer_mgr_->setup("memfile-lfc");
231 }
232}
233
234void
236 try {
238 .arg(process_->getCommandLine());
239 pid_ = process_->spawn();
240
241 } catch (const ProcessSpawnError&) {
243 }
244}
245
246bool
248 return (process_ && process_->isRunning(pid_));
249}
250
251int
253 if (!process_) {
254 isc_throw(InvalidOperation, "unable to obtain LFC process exit code: "
255 " the process is null");
256 }
257 return (process_->getExitStatus(pid_));
258}
259
260
267public:
273 : LeaseStatsQuery(select_mode), rows_(0), next_pos_(rows_.end()) {
274 };
275
280 : LeaseStatsQuery(subnet_id), rows_(0), next_pos_(rows_.end()) {
281 };
282
287 MemfileLeaseStatsQuery(const SubnetID& first_subnet_id, const SubnetID& last_subnet_id)
288 : LeaseStatsQuery(first_subnet_id, last_subnet_id), rows_(0), next_pos_(rows_.end()) {
289 };
290
293
304 virtual bool getNextRow(LeaseStatsRow& row) {
305 if (next_pos_ == rows_.end()) {
306 return (false);
307 }
308
309 row = *next_pos_;
310 ++next_pos_;
311 return (true);
312 }
313
315 int getRowCount() const {
316 return (rows_.size());
317 }
318
319protected:
321 std::vector<LeaseStatsRow> rows_;
322
324 std::vector<LeaseStatsRow>::iterator next_pos_;
325};
326
337public:
344 const SelectMode& select_mode = ALL_SUBNETS)
345 : MemfileLeaseStatsQuery(select_mode), storage4_(storage4) {
346 };
347
352 MemfileLeaseStatsQuery4(Lease4Storage& storage4, const SubnetID& subnet_id)
353 : MemfileLeaseStatsQuery(subnet_id), storage4_(storage4) {
354 };
355
361 MemfileLeaseStatsQuery4(Lease4Storage& storage4, const SubnetID& first_subnet_id,
362 const SubnetID& last_subnet_id)
363 : MemfileLeaseStatsQuery(first_subnet_id, last_subnet_id), storage4_(storage4) {
364 };
365
368
383 void start() {
384 switch (getSelectMode()) {
385 case ALL_SUBNETS:
386 case SINGLE_SUBNET:
387 case SUBNET_RANGE:
388 startSubnets();
389 break;
390
391 case ALL_SUBNET_POOLS:
392 startSubnetPools();
393 break;
394 }
395 }
396
397private:
412 void startSubnets() {
414 = storage4_.get<SubnetIdIndexTag>();
415
416 // Set lower and upper bounds based on select mode
417 Lease4StorageSubnetIdIndex::const_iterator lower;
418 Lease4StorageSubnetIdIndex::const_iterator upper;
419
420 switch (getSelectMode()) {
421 case ALL_SUBNETS:
422 lower = idx.begin();
423 upper = idx.end();
424 break;
425
426 case SINGLE_SUBNET:
427 lower = idx.lower_bound(getFirstSubnetID());
428 upper = idx.upper_bound(getFirstSubnetID());
429 break;
430
431 case SUBNET_RANGE:
432 lower = idx.lower_bound(getFirstSubnetID());
433 upper = idx.upper_bound(getLastSubnetID());
434 break;
435
436 default:
437 return;
438 }
439
440 // Return an empty set if there are no rows.
441 if (lower == upper) {
442 return;
443 }
444
445 // Iterate over the leases in order by subnet, accumulating per
446 // subnet counts for each state of interest. As we finish each
447 // subnet, add the appropriate rows to our result set.
448 SubnetID cur_id = 0;
449 int64_t assigned = 0;
450 int64_t declined = 0;
451 for (Lease4StorageSubnetIdIndex::const_iterator lease = lower;
452 lease != upper; ++lease) {
453 // If we've hit the next subnet, add rows for the current subnet
454 // and wipe the accumulators
455 if ((*lease)->subnet_id_ != cur_id) {
456 if (cur_id > 0) {
457 if (assigned > 0) {
458 rows_.push_back(LeaseStatsRow(cur_id,
460 assigned));
461 assigned = 0;
462 }
463
464 if (declined > 0) {
465 rows_.push_back(LeaseStatsRow(cur_id,
467 declined));
468 declined = 0;
469 }
470 }
471
472 // Update current subnet id
473 cur_id = (*lease)->subnet_id_;
474 }
475
476 // Bump the appropriate accumulator
477 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
478 ++assigned;
479 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
480 ++declined;
481 }
482 }
483
484 // Make the rows for last subnet
485 if (assigned > 0) {
486 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DEFAULT,
487 assigned));
488 }
489
490 if (declined > 0) {
491 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DECLINED,
492 declined));
493 }
494
495 // Reset the next row position back to the beginning of the rows.
496 next_pos_ = rows_.begin();
497 }
498
513 void startSubnetPools() {
515 = storage4_.get<SubnetIdPoolIdIndexTag>();
516
517 // Set lower and upper bounds based on select mode
518 Lease4StorageSubnetIdPoolIdIndex::const_iterator lower;
519 Lease4StorageSubnetIdPoolIdIndex::const_iterator upper;
520 switch (getSelectMode()) {
521 case ALL_SUBNET_POOLS:
522 lower = idx.begin();
523 upper = idx.end();
524 break;
525
526 default:
527 return;
528 }
529
530 // Return an empty set if there are no rows.
531 if (lower == upper) {
532 return;
533 }
534
535 // Iterate over the leases in order by subnet and pool, accumulating per
536 // subnet and pool counts for each state of interest. As we finish each
537 // subnet or pool, add the appropriate rows to our result set.
538 SubnetID cur_id = 0;
539 uint32_t cur_pool_id = 0;
540 int64_t assigned = 0;
541 int64_t declined = 0;
542 for (Lease4StorageSubnetIdPoolIdIndex::const_iterator lease = lower;
543 lease != upper; ++lease) {
544 // If we've hit the next pool, add rows for the current subnet and
545 // pool and wipe the accumulators
546 if ((*lease)->pool_id_ != cur_pool_id) {
547 if (assigned > 0) {
548 rows_.push_back(LeaseStatsRow(cur_id,
550 assigned, cur_pool_id));
551 assigned = 0;
552 }
553
554 if (declined > 0) {
555 rows_.push_back(LeaseStatsRow(cur_id,
557 declined, cur_pool_id));
558 declined = 0;
559 }
560
561 // Update current pool id
562 cur_pool_id = (*lease)->pool_id_;
563 }
564
565 // If we've hit the next subnet, add rows for the current subnet
566 // and wipe the accumulators
567 if ((*lease)->subnet_id_ != cur_id) {
568 if (cur_id > 0) {
569 if (assigned > 0) {
570 rows_.push_back(LeaseStatsRow(cur_id,
572 assigned, cur_pool_id));
573 assigned = 0;
574 }
575
576 if (declined > 0) {
577 rows_.push_back(LeaseStatsRow(cur_id,
579 declined, cur_pool_id));
580 declined = 0;
581 }
582 }
583
584 // Update current subnet id
585 cur_id = (*lease)->subnet_id_;
586
587 // Reset pool id
588 cur_pool_id = 0;
589 }
590
591 // Bump the appropriate accumulator
592 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
593 ++assigned;
594 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
595 ++declined;
596 }
597 }
598
599 // Make the rows for last subnet
600 if (assigned > 0) {
601 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DEFAULT,
602 assigned, cur_pool_id));
603 }
604
605 if (declined > 0) {
606 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DECLINED,
607 declined, cur_pool_id));
608 }
609
610 // Reset the next row position back to the beginning of the rows.
611 next_pos_ = rows_.begin();
612 }
613
615 Lease4Storage& storage4_;
616};
617
618
629public:
636 const SelectMode& select_mode = ALL_SUBNETS)
637 : MemfileLeaseStatsQuery(select_mode), storage6_(storage6) {
638 };
639
644 MemfileLeaseStatsQuery6(Lease6Storage& storage6, const SubnetID& subnet_id)
645 : MemfileLeaseStatsQuery(subnet_id), storage6_(storage6) {
646 };
647
653 MemfileLeaseStatsQuery6(Lease6Storage& storage6, const SubnetID& first_subnet_id,
654 const SubnetID& last_subnet_id)
655 : MemfileLeaseStatsQuery(first_subnet_id, last_subnet_id), storage6_(storage6) {
656 };
657
660
676 void start() {
677 switch (getSelectMode()) {
678 case ALL_SUBNETS:
679 case SINGLE_SUBNET:
680 case SUBNET_RANGE:
681 startSubnets();
682 break;
683
684 case ALL_SUBNET_POOLS:
685 startSubnetPools();
686 break;
687 }
688 }
689
690private:
706 virtual void startSubnets() {
708 = storage6_.get<SubnetIdIndexTag>();
709
710 // Set lower and upper bounds based on select mode
711 Lease6StorageSubnetIdIndex::const_iterator lower;
712 Lease6StorageSubnetIdIndex::const_iterator upper;
713 switch (getSelectMode()) {
714 case ALL_SUBNETS:
715 lower = idx.begin();
716 upper = idx.end();
717 break;
718
719 case SINGLE_SUBNET:
720 lower = idx.lower_bound(getFirstSubnetID());
721 upper = idx.upper_bound(getFirstSubnetID());
722 break;
723
724 case SUBNET_RANGE:
725 lower = idx.lower_bound(getFirstSubnetID());
726 upper = idx.upper_bound(getLastSubnetID());
727 break;
728
729 default:
730 return;
731 }
732
733 // Return an empty set if there are no rows.
734 if (lower == upper) {
735 return;
736 }
737
738 // Iterate over the leases in order by subnet, accumulating per
739 // subnet counts for each state of interest. As we finish each
740 // subnet, add the appropriate rows to our result set.
741 SubnetID cur_id = 0;
742 int64_t assigned = 0;
743 int64_t declined = 0;
744 int64_t assigned_pds = 0;
745 int64_t registered = 0;
746 for (Lease6StorageSubnetIdIndex::const_iterator lease = lower;
747 lease != upper; ++lease) {
748 // If we've hit the next subnet, add rows for the current subnet
749 // and wipe the accumulators
750 if ((*lease)->subnet_id_ != cur_id) {
751 if (cur_id > 0) {
752 if (assigned > 0) {
753 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
755 assigned));
756 assigned = 0;
757 }
758
759 if (declined > 0) {
760 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
762 declined));
763 declined = 0;
764 }
765
766 if (assigned_pds > 0) {
767 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
769 assigned_pds));
770 assigned_pds = 0;
771 }
772
773 if (registered > 0) {
774 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
776 registered));
777 registered = 0;
778 }
779 }
780
781 // Update current subnet id
782 cur_id = (*lease)->subnet_id_;
783 }
784
785 // Bump the appropriate accumulator
786 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
787 switch((*lease)->type_) {
788 case Lease::TYPE_NA:
789 ++assigned;
790 break;
791 case Lease::TYPE_PD:
792 ++assigned_pds;
793 break;
794 default:
795 break;
796 }
797 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
798 // In theory only NAs can be declined
799 if (((*lease)->type_) == Lease::TYPE_NA) {
800 ++declined;
801 }
802 } else if ((*lease)->state_ == Lease::STATE_REGISTERED) {
803 // In theory only NAs can be registered
804 if (((*lease)->type_) == Lease::TYPE_NA) {
805 ++registered;
806 }
807 }
808 }
809
810 // Make the rows for last subnet, unless there were no rows
811 if (assigned > 0) {
812 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
813 Lease::STATE_DEFAULT, assigned));
814 }
815
816 if (declined > 0) {
817 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
818 Lease::STATE_DECLINED, declined));
819 }
820
821 if (assigned_pds > 0) {
822 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
823 Lease::STATE_DEFAULT, assigned_pds));
824 }
825
826 if (registered > 0) {
827 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
828 Lease::STATE_REGISTERED, registered));
829 }
830
831 // Set the next row position to the beginning of the rows.
832 next_pos_ = rows_.begin();
833 }
834
849 virtual void startSubnetPools() {
851 = storage6_.get<SubnetIdPoolIdIndexTag>();
852
853 // Set lower and upper bounds based on select mode
854 Lease6StorageSubnetIdPoolIdIndex::const_iterator lower;
855 Lease6StorageSubnetIdPoolIdIndex::const_iterator upper;
856 switch (getSelectMode()) {
857 case ALL_SUBNET_POOLS:
858 lower = idx.begin();
859 upper = idx.end();
860 break;
861
862 default:
863 return;
864 }
865
866 // Return an empty set if there are no rows.
867 if (lower == upper) {
868 return;
869 }
870
871 // Iterate over the leases in order by subnet, accumulating per
872 // subnet counts for each state of interest. As we finish each
873 // subnet, add the appropriate rows to our result set.
874 SubnetID cur_id = 0;
875 uint32_t cur_pool_id = 0;
876 int64_t assigned = 0;
877 int64_t declined = 0;
878 int64_t assigned_pds = 0;
879 for (Lease6StorageSubnetIdPoolIdIndex::const_iterator lease = lower;
880 lease != upper; ++lease) {
881 // If we've hit the next pool, add rows for the current subnet and
882 // pool and wipe the accumulators
883 if ((*lease)->pool_id_ != cur_pool_id) {
884 if (assigned > 0) {
885 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
887 assigned, cur_pool_id));
888 assigned = 0;
889 }
890
891 if (declined > 0) {
892 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
894 declined, cur_pool_id));
895 declined = 0;
896 }
897
898 if (assigned_pds > 0) {
899 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
901 assigned_pds, cur_pool_id));
902 assigned_pds = 0;
903 }
904
905 // Update current pool id
906 cur_pool_id = (*lease)->pool_id_;
907 }
908
909 // If we've hit the next subnet, add rows for the current subnet
910 // and wipe the accumulators
911 if ((*lease)->subnet_id_ != cur_id) {
912 if (cur_id > 0) {
913 if (assigned > 0) {
914 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
916 assigned, cur_pool_id));
917 assigned = 0;
918 }
919
920 if (declined > 0) {
921 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
923 declined, cur_pool_id));
924 declined = 0;
925 }
926
927 if (assigned_pds > 0) {
928 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
930 assigned_pds, cur_pool_id));
931 assigned_pds = 0;
932 }
933 }
934
935 // Update current subnet id
936 cur_id = (*lease)->subnet_id_;
937
938 // Reset pool id
939 cur_pool_id = 0;
940 }
941
942 // Bump the appropriate accumulator
943 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
944 switch((*lease)->type_) {
945 case Lease::TYPE_NA:
946 ++assigned;
947 break;
948 case Lease::TYPE_PD:
949 ++assigned_pds;
950 break;
951 default:
952 break;
953 }
954 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
955 // In theory only NAs can be declined
956 if (((*lease)->type_) == Lease::TYPE_NA) {
957 ++declined;
958 }
959 }
960 }
961
962 // Make the rows for last subnet, unless there were no rows
963 if (assigned > 0) {
964 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
965 Lease::STATE_DEFAULT, assigned,
966 cur_pool_id));
967 }
968
969 if (declined > 0) {
970 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
971 Lease::STATE_DECLINED, declined,
972 cur_pool_id));
973 }
974
975 if (assigned_pds > 0) {
976 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
977 Lease::STATE_DEFAULT, assigned_pds,
978 cur_pool_id));
979 }
980
981 // Set the next row position to the beginning of the rows.
982 next_pos_ = rows_.begin();
983 }
984
986 Lease6Storage& storage6_;
987};
988
989// Explicit definition of class static constants. Values are given in the
990// declaration so they're not needed here.
995
997 : TrackingLeaseMgr(), lfc_setup_(), conn_(parameters), mutex_(new std::mutex) {
998 bool conversion_needed = false;
999
1000 // Check if the extended info tables are enabled.
1001 setExtendedInfoTablesEnabled(parameters);
1002
1003 // Check the universe and use v4 file or v6 file.
1004 std::string universe = conn_.getParameter("universe");
1005 if (universe == "4") {
1006 std::string file4 = initLeaseFilePath(V4);
1007 if (!file4.empty()) {
1008 conversion_needed = loadLeasesFromFiles<Lease4,
1009 CSVLeaseFile4>(file4,
1011 storage4_);
1012 static_cast<void>(extractExtendedInfo4(false, false));
1013 }
1014 } else {
1015 std::string file6 = initLeaseFilePath(V6);
1016 if (!file6.empty()) {
1017 conversion_needed = loadLeasesFromFiles<Lease6,
1018 CSVLeaseFile6>(file6,
1020 storage6_);
1022 }
1023 }
1024
1025 // If lease persistence have been disabled for both v4 and v6,
1026 // issue a warning. It is ok not to write leases to disk when
1027 // doing testing, but it should not be done in normal server
1028 // operation.
1029 if (!persistLeases(V4) && !persistLeases(V6)) {
1031 } else {
1032 if (conversion_needed) {
1033 auto const& version(getVersion());
1035 .arg(version.first).arg(version.second);
1036 }
1037 lfcSetup(conversion_needed);
1038 }
1039}
1040
1042 if (lease_file4_) {
1043 lease_file4_->close();
1044 lease_file4_.reset();
1045 }
1046 if (lease_file6_) {
1047 lease_file6_->close();
1048 lease_file6_.reset();
1049 }
1050}
1051
1052std::string
1054 std::stringstream tmp;
1055 tmp << "Memfile backend ";
1056 if (u == V4) {
1057 tmp << MAJOR_VERSION_V4 << "." << MINOR_VERSION_V4;
1058 } else if (u == V6) {
1059 tmp << MAJOR_VERSION_V6 << "." << MINOR_VERSION_V6;
1060 }
1061 return tmp.str();
1062}
1063
1064std::string
1066 uint16_t family = CfgMgr::instance().getFamily();
1067 if (family == AF_INET6) {
1069 } else {
1071 }
1072}
1073
1074bool
1075Memfile_LeaseMgr::addLeaseInternal(const Lease4Ptr& lease) {
1076 if (getLease4Internal(lease->addr_)) {
1077 // there is a lease with specified address already
1078 return (false);
1079 }
1080
1081 // Try to write a lease to disk first. If this fails, the lease will
1082 // not be inserted to the memory and the disk and in-memory data will
1083 // remain consistent.
1084 if (persistLeases(V4)) {
1085 lease_file4_->append(*lease);
1086 }
1087
1088 storage4_.insert(lease);
1089
1090 // Update lease current expiration time (allows update between the creation
1091 // of the Lease up to the point of insertion in the database).
1092 lease->updateCurrentExpirationTime();
1093
1094 // Increment class lease counters.
1095 class_lease_counter_.addLease(lease);
1096
1097 // Run installed callbacks.
1098 if (hasCallbacks()) {
1099 trackAddLease(lease);
1100 }
1101
1102 return (true);
1103}
1104
1105bool
1108 DHCPSRV_MEMFILE_ADD_ADDR4).arg(lease->addr_.toText());
1109
1110 if (MultiThreadingMgr::instance().getMode()) {
1111 std::lock_guard<std::mutex> lock(*mutex_);
1112 return (addLeaseInternal(lease));
1113 } else {
1114 return (addLeaseInternal(lease));
1115 }
1116}
1117
1118bool
1119Memfile_LeaseMgr::addLeaseInternal(const Lease6Ptr& lease) {
1120 if (getLease6Internal(lease->type_, lease->addr_)) {
1121 // there is a lease with specified address already
1122 return (false);
1123 }
1124
1125 // Try to write a lease to disk first. If this fails, the lease will
1126 // not be inserted to the memory and the disk and in-memory data will
1127 // remain consistent.
1128 if (persistLeases(V6)) {
1129 lease_file6_->append(*lease);
1130 }
1131
1132 lease->extended_info_action_ = Lease6::ACTION_IGNORE;
1133 storage6_.insert(lease);
1134
1135 // Update lease current expiration time (allows update between the creation
1136 // of the Lease up to the point of insertion in the database).
1137 lease->updateCurrentExpirationTime();
1138
1139 // Increment class lease counters.
1140 class_lease_counter_.addLease(lease);
1141
1143 static_cast<void>(addExtendedInfo6(lease));
1144 }
1145
1146 // Run installed callbacks.
1147 if (hasCallbacks()) {
1148 trackAddLease(lease);
1149 }
1150
1151 return (true);
1152}
1153
1154bool
1157 DHCPSRV_MEMFILE_ADD_ADDR6).arg(lease->addr_.toText());
1158
1159 if (MultiThreadingMgr::instance().getMode()) {
1160 std::lock_guard<std::mutex> lock(*mutex_);
1161 return (addLeaseInternal(lease));
1162 } else {
1163 return (addLeaseInternal(lease));
1164 }
1165}
1166
1168Memfile_LeaseMgr::getLease4Internal(const isc::asiolink::IOAddress& addr) const {
1169 const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
1170 Lease4StorageAddressIndex::iterator l = idx.find(addr);
1171 if (l == idx.end()) {
1172 return (Lease4Ptr());
1173 } else {
1174 return (Lease4Ptr(new Lease4(**l)));
1175 }
1176}
1177
1181 DHCPSRV_MEMFILE_GET_ADDR4).arg(addr.toText());
1182
1183 if (MultiThreadingMgr::instance().getMode()) {
1184 std::lock_guard<std::mutex> lock(*mutex_);
1185 return (getLease4Internal(addr));
1186 } else {
1187 return (getLease4Internal(addr));
1188 }
1189}
1190
1191void
1192Memfile_LeaseMgr::getLease4Internal(const HWAddr& hwaddr,
1193 Lease4Collection& collection) const {
1194 // Using composite index by 'hw address' and 'subnet id'. It is
1195 // ok to use it for searching by the 'hw address' only.
1197 storage4_.get<HWAddressSubnetIdIndexTag>();
1198 std::pair<Lease4StorageHWAddressSubnetIdIndex::const_iterator,
1199 Lease4StorageHWAddressSubnetIdIndex::const_iterator> l
1200 = idx.equal_range(boost::make_tuple(hwaddr.hwaddr_));
1201
1202 BOOST_FOREACH(auto const& lease, l) {
1203 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1204 }
1205}
1206
1210 DHCPSRV_MEMFILE_GET_HWADDR).arg(hwaddr.toText());
1211
1212 Lease4Collection collection;
1213 if (MultiThreadingMgr::instance().getMode()) {
1214 std::lock_guard<std::mutex> lock(*mutex_);
1215 getLease4Internal(hwaddr, collection);
1216 } else {
1217 getLease4Internal(hwaddr, collection);
1218 }
1219
1220 return (collection);
1221}
1222
1224Memfile_LeaseMgr::getLease4Internal(const HWAddr& hwaddr,
1225 SubnetID subnet_id) const {
1226 // Get the index by HW Address and Subnet Identifier.
1228 storage4_.get<HWAddressSubnetIdIndexTag>();
1229 // Try to find the lease using HWAddr and subnet id.
1230 Lease4StorageHWAddressSubnetIdIndex::const_iterator lease =
1231 idx.find(boost::make_tuple(hwaddr.hwaddr_, subnet_id));
1232 // Lease was not found. Return empty pointer to the caller.
1233 if (lease == idx.end()) {
1234 return (Lease4Ptr());
1235 }
1236
1237 // Lease was found. Return it to the caller.
1238 return (Lease4Ptr(new Lease4(**lease)));
1239}
1240
1243 SubnetID subnet_id) const {
1245 DHCPSRV_MEMFILE_GET_SUBID_HWADDR).arg(subnet_id)
1246 .arg(hwaddr.toText());
1247
1248 if (MultiThreadingMgr::instance().getMode()) {
1249 std::lock_guard<std::mutex> lock(*mutex_);
1250 return (getLease4Internal(hwaddr, subnet_id));
1251 } else {
1252 return (getLease4Internal(hwaddr, subnet_id));
1253 }
1254}
1255
1256void
1257Memfile_LeaseMgr::getLease4Internal(const ClientId& client_id,
1258 Lease4Collection& collection) const {
1259 // Using composite index by 'client id' and 'subnet id'. It is ok
1260 // to use it to search by 'client id' only.
1262 storage4_.get<ClientIdSubnetIdIndexTag>();
1263 std::pair<Lease4StorageClientIdSubnetIdIndex::const_iterator,
1264 Lease4StorageClientIdSubnetIdIndex::const_iterator> l
1265 = idx.equal_range(boost::make_tuple(client_id.getClientId()));
1266
1267 BOOST_FOREACH(auto const& lease, l) {
1268 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1269 }
1270}
1271
1273Memfile_LeaseMgr::getLease4(const ClientId& client_id) const {
1275 DHCPSRV_MEMFILE_GET_CLIENTID).arg(client_id.toText());
1276
1277 Lease4Collection collection;
1278 if (MultiThreadingMgr::instance().getMode()) {
1279 std::lock_guard<std::mutex> lock(*mutex_);
1280 getLease4Internal(client_id, collection);
1281 } else {
1282 getLease4Internal(client_id, collection);
1283 }
1284
1285 return (collection);
1286}
1287
1289Memfile_LeaseMgr::getLease4Internal(const ClientId& client_id,
1290 SubnetID subnet_id) const {
1291 // Get the index by client and subnet id.
1293 storage4_.get<ClientIdSubnetIdIndexTag>();
1294 // Try to get the lease using client id and subnet id.
1295 Lease4StorageClientIdSubnetIdIndex::const_iterator lease =
1296 idx.find(boost::make_tuple(client_id.getClientId(), subnet_id));
1297 // Lease was not found. Return empty pointer to the caller.
1298 if (lease == idx.end()) {
1299 return (Lease4Ptr());
1300 }
1301 // Lease was found. Return it to the caller.
1302 return (Lease4Ptr(new Lease4(**lease)));
1303}
1304
1307 SubnetID subnet_id) const {
1310 .arg(client_id.toText());
1311
1312 if (MultiThreadingMgr::instance().getMode()) {
1313 std::lock_guard<std::mutex> lock(*mutex_);
1314 return (getLease4Internal(client_id, subnet_id));
1315 } else {
1316 return (getLease4Internal(client_id, subnet_id));
1317 }
1318}
1319
1320void
1321Memfile_LeaseMgr::getLeases4Internal(SubnetID subnet_id,
1322 Lease4Collection& collection) const {
1323 const Lease4StorageSubnetIdIndex& idx = storage4_.get<SubnetIdIndexTag>();
1324 std::pair<Lease4StorageSubnetIdIndex::const_iterator,
1325 Lease4StorageSubnetIdIndex::const_iterator> l =
1326 idx.equal_range(subnet_id);
1327
1328 BOOST_FOREACH(auto const& lease, l) {
1329 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1330 }
1331}
1332
1336 .arg(subnet_id);
1337
1338 Lease4Collection collection;
1339 if (MultiThreadingMgr::instance().getMode()) {
1340 std::lock_guard<std::mutex> lock(*mutex_);
1341 getLeases4Internal(subnet_id, collection);
1342 } else {
1343 getLeases4Internal(subnet_id, collection);
1344 }
1345
1346 return (collection);
1347}
1348
1349void
1350Memfile_LeaseMgr::getLeases4Internal(const std::string& hostname,
1351 Lease4Collection& collection) const {
1352 const Lease4StorageHostnameIndex& idx = storage4_.get<HostnameIndexTag>();
1353 std::pair<Lease4StorageHostnameIndex::const_iterator,
1354 Lease4StorageHostnameIndex::const_iterator> l =
1355 idx.equal_range(hostname);
1356
1357 BOOST_FOREACH(auto const& lease, l) {
1358 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1359 }
1360}
1361
1363Memfile_LeaseMgr::getLeases4(const std::string& hostname) const {
1365 .arg(hostname);
1366
1367 Lease4Collection collection;
1368 if (MultiThreadingMgr::instance().getMode()) {
1369 std::lock_guard<std::mutex> lock(*mutex_);
1370 getLeases4Internal(hostname, collection);
1371 } else {
1372 getLeases4Internal(hostname, collection);
1373 }
1374
1375 return (collection);
1376}
1377
1378void
1379Memfile_LeaseMgr::getLeases4Internal(Lease4Collection& collection) const {
1380 for (auto const& lease : storage4_) {
1381 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1382 }
1383}
1384
1388
1389 Lease4Collection collection;
1390 if (MultiThreadingMgr::instance().getMode()) {
1391 std::lock_guard<std::mutex> lock(*mutex_);
1392 getLeases4Internal(collection);
1393 } else {
1394 getLeases4Internal(collection);
1395 }
1396
1397 return (collection);
1398}
1399
1400void
1401Memfile_LeaseMgr::getLeases4Internal(const asiolink::IOAddress& lower_bound_address,
1402 const LeasePageSize& page_size,
1403 Lease4Collection& collection) const {
1404 const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
1405 Lease4StorageAddressIndex::const_iterator lb = idx.lower_bound(lower_bound_address);
1406
1407 // Exclude the lower bound address specified by the caller.
1408 if ((lb != idx.end()) && ((*lb)->addr_ == lower_bound_address)) {
1409 ++lb;
1410 }
1411
1412 // Return all other leases being within the page size.
1413 for (auto lease = lb;
1414 (lease != idx.end()) && (std::distance(lb, lease) < page_size.page_size_);
1415 ++lease) {
1416 collection.push_back(Lease4Ptr(new Lease4(**lease)));
1417 }
1418}
1419
1422 const LeasePageSize& page_size) const {
1423 // Expecting IPv4 address.
1424 if (!lower_bound_address.isV4()) {
1425 isc_throw(InvalidAddressFamily, "expected IPv4 address while "
1426 "retrieving leases from the lease database, got "
1427 << lower_bound_address);
1428 }
1429
1431 .arg(page_size.page_size_)
1432 .arg(lower_bound_address.toText());
1433
1434 Lease4Collection collection;
1435 if (MultiThreadingMgr::instance().getMode()) {
1436 std::lock_guard<std::mutex> lock(*mutex_);
1437 getLeases4Internal(lower_bound_address, page_size, collection);
1438 } else {
1439 getLeases4Internal(lower_bound_address, page_size, collection);
1440 }
1441
1442 return (collection);
1443}
1444
1446Memfile_LeaseMgr::getLease6Internal(Lease::Type type,
1447 const isc::asiolink::IOAddress& addr) const {
1448 Lease6Storage::iterator l = storage6_.find(addr);
1449 if (l == storage6_.end() || !(*l) || ((*l)->type_ != type)) {
1450 return (Lease6Ptr());
1451 } else {
1452 return (Lease6Ptr(new Lease6(**l)));
1453 }
1454}
1455
1457Memfile_LeaseMgr::getAnyLease6Internal(const isc::asiolink::IOAddress& addr) const {
1458 Lease6Storage::iterator l = storage6_.find(addr);
1459 if (l == storage6_.end() || !(*l)) {
1460 return (Lease6Ptr());
1461 } else {
1462 return (Lease6Ptr(new Lease6(**l)));
1463 }
1464}
1465
1468 const isc::asiolink::IOAddress& addr) const {
1471 .arg(addr.toText())
1472 .arg(Lease::typeToText(type));
1473
1474 if (MultiThreadingMgr::instance().getMode()) {
1475 std::lock_guard<std::mutex> lock(*mutex_);
1476 return (getLease6Internal(type, addr));
1477 } else {
1478 return (getLease6Internal(type, addr));
1479 }
1480}
1481
1482void
1483Memfile_LeaseMgr::getLeases6Internal(Lease::Type type,
1484 const DUID& duid,
1485 uint32_t iaid,
1486 Lease6Collection& collection) const {
1487 // Get the index by DUID, IAID, lease type.
1488 const Lease6StorageDuidIaidTypeIndex& idx = storage6_.get<DuidIaidTypeIndexTag>();
1489 // Try to get the lease using the DUID, IAID and lease type.
1490 std::pair<Lease6StorageDuidIaidTypeIndex::const_iterator,
1491 Lease6StorageDuidIaidTypeIndex::const_iterator> l =
1492 idx.equal_range(boost::make_tuple(duid.getDuid(), iaid, type));
1493
1494 for (Lease6StorageDuidIaidTypeIndex::const_iterator lease =
1495 l.first; lease != l.second; ++lease) {
1496 collection.push_back(Lease6Ptr(new Lease6(**lease)));
1497 }
1498}
1499
1502 const DUID& duid,
1503 uint32_t iaid) const {
1506 .arg(iaid)
1507 .arg(duid.toText())
1508 .arg(Lease::typeToText(type));
1509
1510 Lease6Collection collection;
1511 if (MultiThreadingMgr::instance().getMode()) {
1512 std::lock_guard<std::mutex> lock(*mutex_);
1513 getLeases6Internal(type, duid, iaid, collection);
1514 } else {
1515 getLeases6Internal(type, duid, iaid, collection);
1516 }
1517
1518 return (collection);
1519}
1520
1521void
1522Memfile_LeaseMgr::getLeases6Internal(Lease::Type type,
1523 const DUID& duid,
1524 uint32_t iaid,
1525 SubnetID subnet_id,
1526 Lease6Collection& collection) const {
1527 // Get the index by DUID, IAID, lease type.
1528 const Lease6StorageDuidIaidTypeIndex& idx = storage6_.get<DuidIaidTypeIndexTag>();
1529 // Try to get the lease using the DUID, IAID and lease type.
1530 std::pair<Lease6StorageDuidIaidTypeIndex::const_iterator,
1531 Lease6StorageDuidIaidTypeIndex::const_iterator> l =
1532 idx.equal_range(boost::make_tuple(duid.getDuid(), iaid, type));
1533
1534 for (Lease6StorageDuidIaidTypeIndex::const_iterator lease =
1535 l.first; lease != l.second; ++lease) {
1536 // Filter out the leases which subnet id doesn't match.
1537 if ((*lease)->subnet_id_ == subnet_id) {
1538 collection.push_back(Lease6Ptr(new Lease6(**lease)));
1539 }
1540 }
1541}
1542
1545 const DUID& duid,
1546 uint32_t iaid,
1547 SubnetID subnet_id) const {
1550 .arg(iaid)
1551 .arg(subnet_id)
1552 .arg(duid.toText())
1553 .arg(Lease::typeToText(type));
1554
1555 Lease6Collection collection;
1556 if (MultiThreadingMgr::instance().getMode()) {
1557 std::lock_guard<std::mutex> lock(*mutex_);
1558 getLeases6Internal(type, duid, iaid, subnet_id, collection);
1559 } else {
1560 getLeases6Internal(type, duid, iaid, subnet_id, collection);
1561 }
1562
1563 return (collection);
1564}
1565
1566void
1567Memfile_LeaseMgr::getLeases6Internal(SubnetID subnet_id,
1568 Lease6Collection& collection) const {
1569 const Lease6StorageSubnetIdIndex& idx = storage6_.get<SubnetIdIndexTag>();
1570 std::pair<Lease6StorageSubnetIdIndex::const_iterator,
1571 Lease6StorageSubnetIdIndex::const_iterator> l =
1572 idx.equal_range(subnet_id);
1573
1574 BOOST_FOREACH(auto const& lease, l) {
1575 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1576 }
1577}
1578
1582 .arg(subnet_id);
1583
1584 Lease6Collection collection;
1585 if (MultiThreadingMgr::instance().getMode()) {
1586 std::lock_guard<std::mutex> lock(*mutex_);
1587 getLeases6Internal(subnet_id, collection);
1588 } else {
1589 getLeases6Internal(subnet_id, collection);
1590 }
1591
1592 return (collection);
1593}
1594
1595void
1596Memfile_LeaseMgr::getLeases6Internal(const std::string& hostname,
1597 Lease6Collection& collection) const {
1598 const Lease6StorageHostnameIndex& idx = storage6_.get<HostnameIndexTag>();
1599 std::pair<Lease6StorageHostnameIndex::const_iterator,
1600 Lease6StorageHostnameIndex::const_iterator> l =
1601 idx.equal_range(hostname);
1602
1603 BOOST_FOREACH(auto const& lease, l) {
1604 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1605 }
1606}
1607
1609Memfile_LeaseMgr::getLeases6(const std::string& hostname) const {
1611 .arg(hostname);
1612
1613 Lease6Collection collection;
1614 if (MultiThreadingMgr::instance().getMode()) {
1615 std::lock_guard<std::mutex> lock(*mutex_);
1616 getLeases6Internal(hostname, collection);
1617 } else {
1618 getLeases6Internal(hostname, collection);
1619 }
1620
1621 return (collection);
1622}
1623
1624void
1625Memfile_LeaseMgr::getLeases6Internal(Lease6Collection& collection) const {
1626 for (auto const& lease : storage6_) {
1627 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1628 }
1629}
1630
1634
1635 Lease6Collection collection;
1636 if (MultiThreadingMgr::instance().getMode()) {
1637 std::lock_guard<std::mutex> lock(*mutex_);
1638 getLeases6Internal(collection);
1639 } else {
1640 getLeases6Internal(collection);
1641 }
1642
1643 return (collection);
1644}
1645
1646void
1647Memfile_LeaseMgr::getLeases6Internal(const DUID& duid,
1648 Lease6Collection& collection) const {
1649 const Lease6StorageDuidIndex& idx = storage6_.get<DuidIndexTag>();
1650 std::pair<Lease6StorageDuidIndex::const_iterator,
1651 Lease6StorageDuidIndex::const_iterator> l =
1652 idx.equal_range(duid.getDuid());
1653
1654 BOOST_FOREACH(auto const& lease, l) {
1655 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1656 }
1657}
1658
1662 .arg(duid.toText());
1663
1664 Lease6Collection collection;
1665 if (MultiThreadingMgr::instance().getMode()) {
1666 std::lock_guard<std::mutex> lock(*mutex_);
1667 getLeases6Internal(duid, collection);
1668 } else {
1669 getLeases6Internal(duid, collection);
1670 }
1671
1672 return (collection);
1673}
1674
1675void
1676Memfile_LeaseMgr::getLeases6Internal(const asiolink::IOAddress& lower_bound_address,
1677 const LeasePageSize& page_size,
1678 Lease6Collection& collection) const {
1679 const Lease6StorageAddressIndex& idx = storage6_.get<AddressIndexTag>();
1680 Lease6StorageAddressIndex::const_iterator lb = idx.lower_bound(lower_bound_address);
1681
1682 // Exclude the lower bound address specified by the caller.
1683 if ((lb != idx.end()) && ((*lb)->addr_ == lower_bound_address)) {
1684 ++lb;
1685 }
1686
1687 // Return all other leases being within the page size.
1688 for (auto lease = lb;
1689 (lease != idx.end()) && (std::distance(lb, lease) < page_size.page_size_);
1690 ++lease) {
1691 collection.push_back(Lease6Ptr(new Lease6(**lease)));
1692 }
1693}
1694
1697 const LeasePageSize& page_size) const {
1698 // Expecting IPv6 address.
1699 if (!lower_bound_address.isV6()) {
1700 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
1701 "retrieving leases from the lease database, got "
1702 << lower_bound_address);
1703 }
1704
1706 .arg(page_size.page_size_)
1707 .arg(lower_bound_address.toText());
1708
1709 Lease6Collection collection;
1710 if (MultiThreadingMgr::instance().getMode()) {
1711 std::lock_guard<std::mutex> lock(*mutex_);
1712 getLeases6Internal(lower_bound_address, page_size, collection);
1713 } else {
1714 getLeases6Internal(lower_bound_address, page_size, collection);
1715 }
1716
1717 return (collection);
1718}
1719
1721Memfile_LeaseMgr::getLeases6Internal(SubnetID subnet_id,
1722 const IOAddress& lower_bound_address,
1723 const LeasePageSize& page_size) const {
1724 Lease6Collection collection;
1725 const Lease6StorageSubnetIdIndex& idx = storage6_.get<SubnetIdIndexTag>();
1726 Lease6StorageSubnetIdIndex::const_iterator lb =
1727 idx.lower_bound(boost::make_tuple(subnet_id, lower_bound_address));
1728
1729 // Exclude the lower bound address specified by the caller.
1730 if ((lb != idx.end()) && ((*lb)->addr_ == lower_bound_address)) {
1731 ++lb;
1732 }
1733
1734 // Return all leases being within the page size.
1735 for (auto it = lb; it != idx.end(); ++it) {
1736 if ((*it)->subnet_id_ != subnet_id) {
1737 // Gone after the subnet id index.
1738 break;
1739 }
1740 collection.push_back(Lease6Ptr(new Lease6(**it)));
1741 if (collection.size() >= page_size.page_size_) {
1742 break;
1743 }
1744 }
1745 return (collection);
1746}
1747
1750 const IOAddress& lower_bound_address,
1751 const LeasePageSize& page_size) const {
1754 .arg(page_size.page_size_)
1755 .arg(lower_bound_address.toText())
1756 .arg(subnet_id);
1757
1758 // Expecting IPv6 valid address.
1759 if (!lower_bound_address.isV6()) {
1760 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
1761 "retrieving leases from the lease database, got "
1762 << lower_bound_address);
1763 }
1764
1765 if (MultiThreadingMgr::instance().getMode()) {
1766 std::lock_guard<std::mutex> lock(*mutex_);
1767 return (getLeases6Internal(subnet_id,
1768 lower_bound_address,
1769 page_size));
1770 } else {
1771 return (getLeases6Internal(subnet_id,
1772 lower_bound_address,
1773 page_size));
1774 }
1775}
1776
1777void
1778Memfile_LeaseMgr::getExpiredLeases4Internal(Lease4Collection& expired_leases,
1779 const size_t max_leases) const {
1780 // Obtain the index which segragates leases by state and time.
1781 const Lease4StorageExpirationIndex& index = storage4_.get<ExpirationIndexTag>();
1782
1783 // Retrieve leases which are not reclaimed and which haven't expired. The
1784 // 'less-than' operator will be used for both components of the index. So,
1785 // for the 'state' 'false' is less than 'true'. Also the leases with
1786 // expiration time lower than current time will be returned.
1787 Lease4StorageExpirationIndex::const_iterator ub =
1788 index.upper_bound(boost::make_tuple(false, time(0)));
1789
1790 // Copy only the number of leases indicated by the max_leases parameter.
1791 for (Lease4StorageExpirationIndex::const_iterator lease = index.begin();
1792 (lease != ub) && ((max_leases == 0) || (std::distance(index.begin(), lease) <
1793 max_leases));
1794 ++lease) {
1795 expired_leases.push_back(Lease4Ptr(new Lease4(**lease)));
1796 }
1797}
1798
1799void
1801 const size_t max_leases) const {
1803 .arg(max_leases);
1804
1805 if (MultiThreadingMgr::instance().getMode()) {
1806 std::lock_guard<std::mutex> lock(*mutex_);
1807 getExpiredLeases4Internal(expired_leases, max_leases);
1808 } else {
1809 getExpiredLeases4Internal(expired_leases, max_leases);
1810 }
1811}
1812
1813void
1814Memfile_LeaseMgr::getExpiredLeases6Internal(Lease6Collection& expired_leases,
1815 const size_t max_leases) const {
1816 // Obtain the index which segragates leases by state and time.
1817 const Lease6StorageExpirationIndex& index = storage6_.get<ExpirationIndexTag>();
1818
1819 // Retrieve leases which are not reclaimed and which haven't expired. The
1820 // 'less-than' operator will be used for both components of the index. So,
1821 // for the 'state' 'false' is less than 'true'. Also the leases with
1822 // expiration time lower than current time will be returned.
1823 Lease6StorageExpirationIndex::const_iterator ub =
1824 index.upper_bound(boost::make_tuple(false, time(0)));
1825
1826 // Copy only the number of leases indicated by the max_leases parameter.
1827 for (Lease6StorageExpirationIndex::const_iterator lease = index.begin();
1828 (lease != ub) && ((max_leases == 0) || (std::distance(index.begin(), lease) <
1829 max_leases));
1830 ++lease) {
1831 expired_leases.push_back(Lease6Ptr(new Lease6(**lease)));
1832 }
1833}
1834
1835void
1837 const size_t max_leases) const {
1839 .arg(max_leases);
1840
1841 if (MultiThreadingMgr::instance().getMode()) {
1842 std::lock_guard<std::mutex> lock(*mutex_);
1843 getExpiredLeases6Internal(expired_leases, max_leases);
1844 } else {
1845 getExpiredLeases6Internal(expired_leases, max_leases);
1846 }
1847}
1848
1849void
1850Memfile_LeaseMgr::updateLease4Internal(const Lease4Ptr& lease) {
1851 // Obtain 'by address' index.
1852 Lease4StorageAddressIndex& index = storage4_.get<AddressIndexTag>();
1853
1854 bool persist = persistLeases(V4);
1855
1856 // Lease must exist if it is to be updated.
1857 Lease4StorageAddressIndex::const_iterator lease_it = index.find(lease->addr_);
1858 if (lease_it == index.end()) {
1859 isc_throw(NoSuchLease, "failed to update the lease with address "
1860 << lease->addr_ << " - no such lease");
1861 } else if ((!persist) && (((*lease_it)->cltt_ != lease->current_cltt_) ||
1862 ((*lease_it)->valid_lft_ != lease->current_valid_lft_))) {
1863 // For test purpose only: check that the lease has not changed in
1864 // the database.
1865 isc_throw(NoSuchLease, "unable to update lease for address " <<
1866 lease->addr_.toText() << " either because the lease does not exist, "
1867 "it has been deleted or it has changed in the database.");
1868 }
1869
1870 // Try to write a lease to disk first. If this fails, the lease will
1871 // not be inserted to the memory and the disk and in-memory data will
1872 // remain consistent.
1873 if (persist) {
1874 lease_file4_->append(*lease);
1875 }
1876
1877 // Update lease current expiration time.
1878 lease->updateCurrentExpirationTime();
1879
1880 // Save a copy of the old lease as lease_it will point to the new
1881 // one after the replacement.
1882 Lease4Ptr old_lease = *lease_it;
1883
1884 // Use replace() to re-index leases.
1885 index.replace(lease_it, Lease4Ptr(new Lease4(*lease)));
1886
1887 // Adjust class lease counters.
1888 class_lease_counter_.updateLease(lease, old_lease);
1889
1890 // Run installed callbacks.
1891 if (hasCallbacks()) {
1892 trackUpdateLease(lease);
1893 }
1894}
1895
1896void
1899 DHCPSRV_MEMFILE_UPDATE_ADDR4).arg(lease->addr_.toText());
1900
1901 if (MultiThreadingMgr::instance().getMode()) {
1902 std::lock_guard<std::mutex> lock(*mutex_);
1903 updateLease4Internal(lease);
1904 } else {
1905 updateLease4Internal(lease);
1906 }
1907}
1908
1909void
1910Memfile_LeaseMgr::updateLease6Internal(const Lease6Ptr& lease) {
1911 // Obtain 'by address' index.
1912 Lease6StorageAddressIndex& index = storage6_.get<AddressIndexTag>();
1913
1914 bool persist = persistLeases(V6);
1915
1916 // Get the recorded action and reset it.
1917 Lease6::ExtendedInfoAction recorded_action = lease->extended_info_action_;
1918 lease->extended_info_action_ = Lease6::ACTION_IGNORE;
1919
1920 // Lease must exist if it is to be updated.
1921 Lease6StorageAddressIndex::const_iterator lease_it = index.find(lease->addr_);
1922 if (lease_it == index.end()) {
1923 isc_throw(NoSuchLease, "failed to update the lease with address "
1924 << lease->addr_ << " - no such lease");
1925 } else if ((!persist) && (((*lease_it)->cltt_ != lease->current_cltt_) ||
1926 ((*lease_it)->valid_lft_ != lease->current_valid_lft_))) {
1927 // For test purpose only: check that the lease has not changed in
1928 // the database.
1929 isc_throw(NoSuchLease, "unable to update lease for address " <<
1930 lease->addr_.toText() << " either because the lease does not exist, "
1931 "it has been deleted or it has changed in the database.");
1932 }
1933
1934 // Try to write a lease to disk first. If this fails, the lease will
1935 // not be inserted to the memory and the disk and in-memory data will
1936 // remain consistent.
1937 if (persist) {
1938 lease_file6_->append(*lease);
1939 }
1940
1941 // Update lease current expiration time.
1942 lease->updateCurrentExpirationTime();
1943
1944 // Save a copy of the old lease as lease_it will point to the new
1945 // one after the replacement.
1946 Lease6Ptr old_lease = *lease_it;
1947
1948 // Use replace() to re-index leases.
1949 index.replace(lease_it, Lease6Ptr(new Lease6(*lease)));
1950
1951 // Adjust class lease counters.
1952 class_lease_counter_.updateLease(lease, old_lease);
1953
1954 // Update extended info tables.
1956 switch (recorded_action) {
1958 break;
1959
1961 deleteExtendedInfo6(lease->addr_);
1962 break;
1963
1965 deleteExtendedInfo6(lease->addr_);
1966 static_cast<void>(addExtendedInfo6(lease));
1967 break;
1968 }
1969 }
1970
1971 // Run installed callbacks.
1972 if (hasCallbacks()) {
1973 trackUpdateLease(lease);
1974 }
1975}
1976
1977void
1980 DHCPSRV_MEMFILE_UPDATE_ADDR6).arg(lease->addr_.toText());
1981
1982 if (MultiThreadingMgr::instance().getMode()) {
1983 std::lock_guard<std::mutex> lock(*mutex_);
1984 updateLease6Internal(lease);
1985 } else {
1986 updateLease6Internal(lease);
1987 }
1988}
1989
1990bool
1991Memfile_LeaseMgr::deleteLeaseInternal(const Lease4Ptr& lease) {
1992 const isc::asiolink::IOAddress& addr = lease->addr_;
1993 Lease4Storage::iterator l = storage4_.find(addr);
1994 if (l == storage4_.end()) {
1995 // No such lease
1996 return (false);
1997 } else {
1998 if (persistLeases(V4)) {
1999 // Copy the lease. The valid lifetime needs to be modified and
2000 // we don't modify the original lease.
2001 Lease4 lease_copy = **l;
2002 // Setting valid lifetime to 0 means that lease is being
2003 // removed.
2004 lease_copy.valid_lft_ = 0;
2005 lease_file4_->append(lease_copy);
2006 } else {
2007 // For test purpose only: check that the lease has not changed in
2008 // the database.
2009 if (((*l)->cltt_ != lease->current_cltt_) ||
2010 ((*l)->valid_lft_ != lease->current_valid_lft_)) {
2011 return false;
2012 }
2013 }
2014
2015 storage4_.erase(l);
2016
2017 // Decrement class lease counters.
2018 class_lease_counter_.removeLease(lease);
2019
2020 // Run installed callbacks.
2021 if (hasCallbacks()) {
2022 trackDeleteLease(lease);
2023 }
2024
2025 return (true);
2026 }
2027}
2028
2029bool
2032 DHCPSRV_MEMFILE_DELETE_ADDR4).arg(lease->addr_.toText());
2033
2034 if (MultiThreadingMgr::instance().getMode()) {
2035 std::lock_guard<std::mutex> lock(*mutex_);
2036 return (deleteLeaseInternal(lease));
2037 } else {
2038 return (deleteLeaseInternal(lease));
2039 }
2040}
2041
2042bool
2043Memfile_LeaseMgr::deleteLeaseInternal(const Lease6Ptr& lease) {
2044 lease->extended_info_action_ = Lease6::ACTION_IGNORE;
2045
2046 const isc::asiolink::IOAddress& addr = lease->addr_;
2047 Lease6Storage::iterator l = storage6_.find(addr);
2048 if (l == storage6_.end()) {
2049 // No such lease
2050 return (false);
2051 } else {
2052 if (persistLeases(V6)) {
2053 // Copy the lease. The lifetimes need to be modified and we
2054 // don't modify the original lease.
2055 Lease6 lease_copy = **l;
2056 // Setting lifetimes to 0 means that lease is being removed.
2057 lease_copy.valid_lft_ = 0;
2058 lease_copy.preferred_lft_ = 0;
2059 lease_file6_->append(lease_copy);
2060 } else {
2061 // For test purpose only: check that the lease has not changed in
2062 // the database.
2063 if (((*l)->cltt_ != lease->current_cltt_) ||
2064 ((*l)->valid_lft_ != lease->current_valid_lft_)) {
2065 return false;
2066 }
2067 }
2068
2069 storage6_.erase(l);
2070
2071 // Decrement class lease counters.
2072 class_lease_counter_.removeLease(lease);
2073
2074 // Delete references from extended info tables.
2076 deleteExtendedInfo6(lease->addr_);
2077 }
2078
2079 // Run installed callbacks.
2080 if (hasCallbacks()) {
2081 trackDeleteLease(lease);
2082 }
2083
2084 return (true);
2085 }
2086}
2087
2088bool
2091 DHCPSRV_MEMFILE_DELETE_ADDR6).arg(lease->addr_.toText());
2092
2093 if (MultiThreadingMgr::instance().getMode()) {
2094 std::lock_guard<std::mutex> lock(*mutex_);
2095 return (deleteLeaseInternal(lease));
2096 } else {
2097 return (deleteLeaseInternal(lease));
2098 }
2099}
2100
2101uint64_t
2105 .arg(secs);
2106
2107 if (MultiThreadingMgr::instance().getMode()) {
2108 std::lock_guard<std::mutex> lock(*mutex_);
2109 return (deleteExpiredReclaimedLeases<
2111 >(secs, V4, storage4_, lease_file4_));
2112 } else {
2113 return (deleteExpiredReclaimedLeases<
2115 >(secs, V4, storage4_, lease_file4_));
2116 }
2117}
2118
2119uint64_t
2123 .arg(secs);
2124
2125 if (MultiThreadingMgr::instance().getMode()) {
2126 std::lock_guard<std::mutex> lock(*mutex_);
2127 return (deleteExpiredReclaimedLeases<
2129 >(secs, V6, storage6_, lease_file6_));
2130 } else {
2131 return (deleteExpiredReclaimedLeases<
2133 >(secs, V6, storage6_, lease_file6_));
2134 }
2135}
2136
2137template<typename IndexType, typename LeaseType, typename StorageType,
2138 typename LeaseFileType>
2139uint64_t
2140Memfile_LeaseMgr::deleteExpiredReclaimedLeases(const uint32_t secs,
2141 const Universe& universe,
2142 StorageType& storage,
2143 LeaseFileType& lease_file) {
2144 // Obtain the index which segragates leases by state and time.
2145 IndexType& index = storage.template get<ExpirationIndexTag>();
2146
2147 // This returns the first element which is greater than the specified
2148 // tuple (true, time(0) - secs). However, the range between the
2149 // beginning of the index and returned element also includes all the
2150 // elements for which the first value is false (lease state is NOT
2151 // reclaimed), because false < true. All elements between the
2152 // beginning of the index and the element returned, for which the
2153 // first value is true, represent the reclaimed leases which should
2154 // be deleted, because their expiration time + secs has occurred earlier
2155 // than current time.
2156 typename IndexType::const_iterator upper_limit =
2157 index.upper_bound(boost::make_tuple(true, time(0) - secs));
2158
2159 // Now, we have to exclude all elements of the index which represent
2160 // leases in the state other than reclaimed - with the first value
2161 // in the index equal to false. Note that elements in the index are
2162 // ordered from the lower to the higher ones. So, all elements with
2163 // the first value of false are placed before the elements with the
2164 // value of true. Hence, we have to find the first element which
2165 // contains value of true. The time value is the lowest possible.
2166 typename IndexType::const_iterator lower_limit =
2167 index.upper_bound(boost::make_tuple(true, std::numeric_limits<int64_t>::min()));
2168
2169 // If there are some elements in this range, delete them.
2170 uint64_t num_leases = static_cast<uint64_t>(std::distance(lower_limit, upper_limit));
2171 if (num_leases > 0) {
2172
2175 .arg(num_leases);
2176
2177 // If lease persistence is enabled, we also have to mark leases
2178 // as deleted in the lease file. We do this by setting the
2179 // lifetime to 0.
2180 if (persistLeases(universe)) {
2181 for (typename IndexType::const_iterator lease = lower_limit;
2182 lease != upper_limit; ++lease) {
2183 // Copy lease to not affect the lease in the container.
2184 LeaseType lease_copy(**lease);
2185 // Set the valid lifetime to 0 to indicate the removal
2186 // of the lease.
2187 lease_copy.valid_lft_ = 0;
2188 lease_file->append(lease_copy);
2189 }
2190 }
2191
2192 // Erase leases from memory.
2193 index.erase(lower_limit, upper_limit);
2194
2195 }
2196 // Return number of leases deleted.
2197 return (num_leases);
2198}
2199
2200std::string
2202 return (std::string("In memory database with leases stored in a CSV file."));
2203}
2204
2205std::pair<uint32_t, uint32_t>
2206Memfile_LeaseMgr::getVersion(const std::string& /* timer_name */) const {
2207 std::string const& universe(conn_.getParameter("universe"));
2208 if (universe == "4") {
2209 return std::make_pair(MAJOR_VERSION_V4, MINOR_VERSION_V4);
2210 } else if (universe == "6") {
2211 return std::make_pair(MAJOR_VERSION_V6, MINOR_VERSION_V6);
2212 }
2213 isc_throw(BadValue, "cannot determine version for universe " << universe);
2214}
2215
2216void
2220
2221void
2226
2227std::string
2228Memfile_LeaseMgr::appendSuffix(const std::string& file_name,
2229 const LFCFileType& file_type) {
2230 std::string name(file_name);
2231 switch (file_type) {
2232 case FILE_INPUT:
2233 name += ".1";
2234 break;
2235 case FILE_PREVIOUS:
2236 name += ".2";
2237 break;
2238 case FILE_OUTPUT:
2239 name += ".output";
2240 break;
2241 case FILE_FINISH:
2242 name += ".completed";
2243 break;
2244 case FILE_PID:
2245 name += ".pid";
2246 break;
2247 default:
2248 // Do not append any suffix for the FILE_CURRENT.
2249 ;
2250 }
2251
2252 return (name);
2253}
2254
2255std::string
2257 std::ostringstream s;
2258 s << CfgMgr::instance().getDataDir() << "/kea-leases";
2259 s << (u == V4 ? "4" : "6");
2260 s << ".csv";
2261 return (s.str());
2262}
2263
2264std::string
2266 if (u == V4) {
2267 return (lease_file4_ ? lease_file4_->getFilename() : "");
2268 }
2269
2270 return (lease_file6_ ? lease_file6_->getFilename() : "");
2271}
2272
2273bool
2275 // Currently, if the lease file IO is not created, it means that writes to
2276 // disk have been explicitly disabled by the administrator. At some point,
2277 // there may be a dedicated ON/OFF flag implemented to control this.
2278 if (u == V4 && lease_file4_) {
2279 return (true);
2280 }
2281
2282 return (u == V6 && lease_file6_);
2283}
2284
2285std::string
2286Memfile_LeaseMgr::initLeaseFilePath(Universe u) {
2287 std::string persist_val;
2288 try {
2289 persist_val = conn_.getParameter("persist");
2290 } catch (const Exception&) {
2291 // If parameter persist hasn't been specified, we use a default value
2292 // 'yes'.
2293 persist_val = "true";
2294 }
2295 // If persist_val is 'false' we will not store leases to disk, so let's
2296 // return empty file name.
2297 if (persist_val == "false") {
2298 return ("");
2299
2300 } else if (persist_val != "true") {
2301 isc_throw(isc::BadValue, "invalid value 'persist="
2302 << persist_val << "'");
2303 }
2304
2305 std::string lease_file;
2306 try {
2307 lease_file = conn_.getParameter("name");
2308 } catch (const Exception&) {
2309 lease_file = getDefaultLeaseFilePath(u);
2310 }
2311 return (lease_file);
2312}
2313
2314template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
2315bool
2316Memfile_LeaseMgr::loadLeasesFromFiles(const std::string& filename,
2317 boost::shared_ptr<LeaseFileType>& lease_file,
2318 StorageType& storage) {
2319 // Check if the instance of the LFC is running right now. If it is
2320 // running, we refuse to load leases as the LFC may be writing to the
2321 // lease files right now. When the user retries server configuration
2322 // it should go through.
2325 PIDFile pid_file(appendSuffix(filename, FILE_PID));
2326 if (pid_file.check()) {
2327 isc_throw(DbOpenError, "unable to load leases from files while the "
2328 "lease file cleanup is in progress");
2329 }
2330
2331 storage.clear();
2332
2333 std::string max_row_errors_str = "0";
2334 try {
2335 max_row_errors_str = conn_.getParameter("max-row-errors");
2336 } catch (const std::exception&) {
2337 // Ignore and default to 0.
2338 }
2339
2340 int64_t max_row_errors64;
2341 try {
2342 max_row_errors64 = boost::lexical_cast<int64_t>(max_row_errors_str);
2343 } catch (const boost::bad_lexical_cast&) {
2344 isc_throw(isc::BadValue, "invalid value of the max-row-errors "
2345 << max_row_errors_str << " specified");
2346 }
2347 if ((max_row_errors64 < 0) ||
2348 (max_row_errors64 > std::numeric_limits<uint32_t>::max())) {
2349 isc_throw(isc::BadValue, "invalid value of the max-row-errors "
2350 << max_row_errors_str << " specified");
2351 }
2352 uint32_t max_row_errors = static_cast<uint32_t>(max_row_errors64);
2353
2354 // Load the leasefile.completed, if exists.
2355 bool conversion_needed = false;
2356 lease_file.reset(new LeaseFileType(std::string(filename + ".completed")));
2357 if (lease_file->exists()) {
2358 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2359 max_row_errors);
2360 conversion_needed = conversion_needed || lease_file->needsConversion();
2361 } else {
2362 // If the leasefile.completed doesn't exist, let's load the leases
2363 // from leasefile.2 and leasefile.1, if they exist.
2364 lease_file.reset(new LeaseFileType(appendSuffix(filename, FILE_PREVIOUS)));
2365 if (lease_file->exists()) {
2366 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2367 max_row_errors);
2368 conversion_needed = conversion_needed || lease_file->needsConversion();
2369 }
2370
2371 lease_file.reset(new LeaseFileType(appendSuffix(filename, FILE_INPUT)));
2372 if (lease_file->exists()) {
2373 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2374 max_row_errors);
2375 conversion_needed = conversion_needed || lease_file->needsConversion();
2376 }
2377 }
2378
2379 // Always load leases from the primary lease file. If the lease file
2380 // doesn't exist it will be created by the LeaseFileLoader. Note
2381 // that the false value passed as the last parameter to load
2382 // function causes the function to leave the file open after
2383 // it is parsed. This file will be used by the backend to record
2384 // future lease updates.
2385 lease_file.reset(new LeaseFileType(filename));
2386 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2387 max_row_errors, false);
2388 conversion_needed = conversion_needed || lease_file->needsConversion();
2389
2390 return (conversion_needed);
2391}
2392
2393
2394bool
2396 return (lfc_setup_->isRunning());
2397}
2398
2399int
2401 return (lfc_setup_->getExitStatus());
2402}
2403
2404void
2407
2408 // Check if we're in the v4 or v6 space and use the appropriate file.
2409 if (lease_file4_) {
2411 lfcExecute(lease_file4_);
2412 } else if (lease_file6_) {
2414 lfcExecute(lease_file6_);
2415 }
2416}
2417
2418void
2419Memfile_LeaseMgr::lfcSetup(bool conversion_needed) {
2420 std::string lfc_interval_str = "3600";
2421 try {
2422 lfc_interval_str = conn_.getParameter("lfc-interval");
2423 } catch (const std::exception&) {
2424 // Ignore and default to 3600.
2425 }
2426
2427 uint32_t lfc_interval = 0;
2428 try {
2429 lfc_interval = boost::lexical_cast<uint32_t>(lfc_interval_str);
2430 } catch (const boost::bad_lexical_cast&) {
2431 isc_throw(isc::BadValue, "invalid value of the lfc-interval "
2432 << lfc_interval_str << " specified");
2433 }
2434
2435 if (lfc_interval > 0 || conversion_needed) {
2436 lfc_setup_.reset(new LFCSetup(std::bind(&Memfile_LeaseMgr::lfcCallback, this)));
2437 lfc_setup_->setup(lfc_interval, lease_file4_, lease_file6_, conversion_needed);
2438 }
2439}
2440
2441template<typename LeaseFileType>
2442void
2443Memfile_LeaseMgr::lfcExecute(boost::shared_ptr<LeaseFileType>& lease_file) {
2444 bool do_lfc = true;
2445
2446 // Check the status of the LFC instance.
2447 // If the finish file exists or the copy of the lease file exists it
2448 // is an indication that another LFC instance may be in progress or
2449 // may be stalled. In that case we don't want to rotate the current
2450 // lease file to avoid overriding the contents of the existing file.
2451 CSVFile lease_file_finish(appendSuffix(lease_file->getFilename(), FILE_FINISH));
2452 CSVFile lease_file_copy(appendSuffix(lease_file->getFilename(), FILE_INPUT));
2453 if (!lease_file_finish.exists() && !lease_file_copy.exists()) {
2454 // Close the current file so as we can move it to the copy file.
2455 lease_file->close();
2456 // Move the current file to the copy file. Remember the result
2457 // because we don't want to run LFC if the rename failed.
2458 do_lfc = (rename(lease_file->getFilename().c_str(),
2459 lease_file_copy.getFilename().c_str()) == 0);
2460
2461 if (!do_lfc) {
2463 .arg(lease_file->getFilename())
2464 .arg(lease_file_copy.getFilename())
2465 .arg(strerror(errno));
2466 }
2467
2468 // Regardless if we successfully moved the current file or not,
2469 // we need to re-open the current file for the server to write
2470 // new lease updates. If the file has been successfully moved,
2471 // this will result in creation of the new file. Otherwise,
2472 // an existing file will be opened.
2473 try {
2474 lease_file->open(true);
2475
2476 } catch (const CSVFileError& ex) {
2477 // If we're unable to open the lease file this is a serious
2478 // error because the server will not be able to persist
2479 // leases.
2487 .arg(lease_file->getFilename())
2488 .arg(ex.what());
2489 // Reset the pointer to the file so as the backend doesn't
2490 // try to write leases to disk.
2491 lease_file.reset();
2492 do_lfc = false;
2493 }
2494 }
2495 // Once the files have been rotated, or untouched if another LFC had
2496 // not finished, a new process is started.
2497 if (do_lfc) {
2498 lfc_setup_->execute();
2499 }
2500}
2501
2504 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery4(storage4_));
2505 if (MultiThreadingMgr::instance().getMode()) {
2506 std::lock_guard<std::mutex> lock(*mutex_);
2507 query->start();
2508 } else {
2509 query->start();
2510 }
2511
2512 return(query);
2513}
2514
2518 if (MultiThreadingMgr::instance().getMode()) {
2519 std::lock_guard<std::mutex> lock(*mutex_);
2520 query->start();
2521 } else {
2522 query->start();
2523 }
2524
2525 return(query);
2526}
2527
2530 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery4(storage4_, subnet_id));
2531 if (MultiThreadingMgr::instance().getMode()) {
2532 std::lock_guard<std::mutex> lock(*mutex_);
2533 query->start();
2534 } else {
2535 query->start();
2536 }
2537
2538 return(query);
2539}
2540
2543 const SubnetID& last_subnet_id) {
2544 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery4(storage4_, first_subnet_id,
2545 last_subnet_id));
2546 if (MultiThreadingMgr::instance().getMode()) {
2547 std::lock_guard<std::mutex> lock(*mutex_);
2548 query->start();
2549 } else {
2550 query->start();
2551 }
2552
2553 return(query);
2554}
2555
2558 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery6(storage6_));
2559 if (MultiThreadingMgr::instance().getMode()) {
2560 std::lock_guard<std::mutex> lock(*mutex_);
2561 query->start();
2562 } else {
2563 query->start();
2564 }
2565
2566 return(query);
2567}
2568
2572 if (MultiThreadingMgr::instance().getMode()) {
2573 std::lock_guard<std::mutex> lock(*mutex_);
2574 query->start();
2575 } else {
2576 query->start();
2577 }
2578
2579 return(query);
2580}
2581
2584 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery6(storage6_, subnet_id));
2585 if (MultiThreadingMgr::instance().getMode()) {
2586 std::lock_guard<std::mutex> lock(*mutex_);
2587 query->start();
2588 } else {
2589 query->start();
2590 }
2591
2592 return(query);
2593}
2594
2597 const SubnetID& last_subnet_id) {
2598 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery6(storage6_, first_subnet_id,
2599 last_subnet_id));
2600 if (MultiThreadingMgr::instance().getMode()) {
2601 std::lock_guard<std::mutex> lock(*mutex_);
2602 query->start();
2603 } else {
2604 query->start();
2605 }
2606
2607 return(query);
2608}
2609
2610size_t
2613 .arg(subnet_id);
2614
2615 // Get the index by DUID, IAID, lease type.
2616 const Lease4StorageSubnetIdIndex& idx = storage4_.get<SubnetIdIndexTag>();
2617
2618 // Try to get the lease using the DUID, IAID and lease type.
2619 std::pair<Lease4StorageSubnetIdIndex::const_iterator,
2620 Lease4StorageSubnetIdIndex::const_iterator> r =
2621 idx.equal_range(subnet_id);
2622
2623 // Let's collect all leases.
2624 Lease4Collection leases;
2625 BOOST_FOREACH(auto const& lease, r) {
2626 leases.push_back(lease);
2627 }
2628
2629 size_t num = leases.size();
2630 for (auto const& l : leases) {
2631 deleteLease(l);
2632 }
2634 .arg(subnet_id).arg(num);
2635
2636 return (num);
2637}
2638
2639size_t
2642 .arg(subnet_id);
2643
2644 // Get the index by DUID, IAID, lease type.
2645 const Lease6StorageSubnetIdIndex& idx = storage6_.get<SubnetIdIndexTag>();
2646
2647 // Try to get the lease using the DUID, IAID and lease type.
2648 std::pair<Lease6StorageSubnetIdIndex::const_iterator,
2649 Lease6StorageSubnetIdIndex::const_iterator> r =
2650 idx.equal_range(subnet_id);
2651
2652 // Let's collect all leases.
2653 Lease6Collection leases;
2654 BOOST_FOREACH(auto const& lease, r) {
2655 leases.push_back(lease);
2656 }
2657
2658 size_t num = leases.size();
2659 for (auto const& l : leases) {
2660 deleteLease(l);
2661 }
2663 .arg(subnet_id).arg(num);
2664
2665 return (num);
2666}
2667
2668void
2670 class_lease_counter_.clear();
2671 for (auto const& lease : storage4_) {
2672 // Bump the appropriate accumulator
2673 if (lease->state_ == Lease::STATE_DEFAULT) {
2674 class_lease_counter_.addLease(lease);
2675 }
2676 }
2677}
2678
2679void
2681 class_lease_counter_.clear();
2682 for (auto const& lease : storage6_) {
2683 // Bump the appropriate accumulator
2684 if (lease->state_ == Lease::STATE_DEFAULT) {
2685 class_lease_counter_.addLease(lease);
2686 }
2687 }
2688}
2689
2690size_t
2692 const Lease::Type& ltype /* = Lease::TYPE_V4*/) const {
2693 if (MultiThreadingMgr::instance().getMode()) {
2694 std::lock_guard<std::mutex> lock(*mutex_);
2695 return(class_lease_counter_.getClassCount(client_class, ltype));
2696 } else {
2697 return(class_lease_counter_.getClassCount(client_class, ltype));
2698 }
2699}
2700
2701void
2703 return(class_lease_counter_.clear());
2704}
2705
2706std::string
2708 if (!user_context) {
2709 return ("");
2710 }
2711
2712 ConstElementPtr limits = user_context->find("ISC/limits");
2713 if (!limits) {
2714 return ("");
2715 }
2716
2717 // Iterate of the 'client-classes' list in 'limits'. For each class that specifies
2718 // an "address-limit", check its value against the class's lease count.
2719 ConstElementPtr classes = limits->get("client-classes");
2720 if (classes) {
2721 for (int i = 0; i < classes->size(); ++i) {
2722 ConstElementPtr class_elem = classes->get(i);
2723 // Get class name.
2724 ConstElementPtr name_elem = class_elem->get("name");
2725 if (!name_elem) {
2726 isc_throw(BadValue, "checkLimits4 - client-class.name is missing: "
2727 << prettyPrint(limits));
2728 }
2729
2730 std::string name = name_elem->stringValue();
2731
2732 // Now look for an address-limit
2733 size_t limit;
2734 if (!getLeaseLimit(class_elem, Lease::TYPE_V4, limit)) {
2735 // No limit, go to the next class.
2736 continue;
2737 }
2738
2739 // If the limit is > 0 look up the class lease count. Limit of 0 always
2740 // denies the lease.
2741 size_t lease_count = 0;
2742 if (limit) {
2743 lease_count = getClassLeaseCount(name);
2744 }
2745
2746 // If we're over the limit, return the error, no need to evaluate any others.
2747 if (lease_count >= limit) {
2748 std::ostringstream ss;
2749 ss << "address limit " << limit << " for client class \""
2750 << name << "\", current lease count " << lease_count;
2751 return (ss.str());
2752 }
2753 }
2754 }
2755
2756 // If there were class limits we passed them, now look for a subnet limit.
2757 ConstElementPtr subnet_elem = limits->get("subnet");
2758 if (subnet_elem) {
2759 // Get the subnet id.
2760 ConstElementPtr id_elem = subnet_elem->get("id");
2761 if (!id_elem) {
2762 isc_throw(BadValue, "checkLimits4 - subnet.id is missing: "
2763 << prettyPrint(limits));
2764 }
2765
2766 SubnetID subnet_id = id_elem->intValue();
2767
2768 // Now look for an address-limit.
2769 size_t limit;
2770 if (getLeaseLimit(subnet_elem, Lease::TYPE_V4, limit)) {
2771 // If the limit is > 0 look up the subnet lease count. Limit of 0 always
2772 // denies the lease.
2773 int64_t lease_count = 0;
2774 if (limit) {
2775 lease_count = getSubnetStat(subnet_id, "assigned-addresses");
2776 }
2777
2778 // If we're over the limit, return the error.
2779 if (lease_count >= limit) {
2780 std::ostringstream ss;
2781 ss << "address limit " << limit << " for subnet ID " << subnet_id
2782 << ", current lease count " << lease_count;
2783 return (ss.str());
2784 }
2785 }
2786 }
2787
2788 // No limits exceeded!
2789 return ("");
2790}
2791
2792std::string
2794 if (!user_context) {
2795 return ("");
2796 }
2797
2798 ConstElementPtr limits = user_context->find("ISC/limits");
2799 if (!limits) {
2800 return ("");
2801 }
2802
2803 // Iterate over the 'client-classes' list in 'limits'. For each class that specifies
2804 // limit (either "address-limit" or "prefix-limit", check its value against the appropriate
2805 // class lease count.
2806 ConstElementPtr classes = limits->get("client-classes");
2807 if (classes) {
2808 for (int i = 0; i < classes->size(); ++i) {
2809 ConstElementPtr class_elem = classes->get(i);
2810 // Get class name.
2811 ConstElementPtr name_elem = class_elem->get("name");
2812 if (!name_elem) {
2813 isc_throw(BadValue, "checkLimits6 - client-class.name is missing: "
2814 << prettyPrint(limits));
2815 }
2816
2817 std::string name = name_elem->stringValue();
2818
2819 // Now look for either address-limit or a prefix=limit.
2820 size_t limit = 0;
2822 if (!getLeaseLimit(class_elem, ltype, limit)) {
2823 ltype = Lease::TYPE_PD;
2824 if (!getLeaseLimit(class_elem, ltype, limit)) {
2825 // No limits for this class, skip to the next.
2826 continue;
2827 }
2828 }
2829
2830 // If the limit is > 0 look up the class lease count. Limit of 0 always
2831 // denies the lease.
2832 size_t lease_count = 0;
2833 if (limit) {
2834 lease_count = getClassLeaseCount(name, ltype);
2835 }
2836
2837 // If we're over the limit, return the error, no need to evaluate any others.
2838 if (lease_count >= limit) {
2839 std::ostringstream ss;
2840 ss << (ltype == Lease::TYPE_NA ? "address" : "prefix")
2841 << " limit " << limit << " for client class \""
2842 << name << "\", current lease count " << lease_count;
2843 return (ss.str());
2844 }
2845 }
2846 }
2847
2848 // If there were class limits we passed them, now look for a subnet limit.
2849 ConstElementPtr subnet_elem = limits->get("subnet");
2850 if (subnet_elem) {
2851 // Get the subnet id.
2852 ConstElementPtr id_elem = subnet_elem->get("id");
2853 if (!id_elem) {
2854 isc_throw(BadValue, "checkLimits6 - subnet.id is missing: "
2855 << prettyPrint(limits));
2856 }
2857
2858 SubnetID subnet_id = id_elem->intValue();
2859
2860 // Now look for either address-limit or a prefix=limit.
2861 size_t limit = 0;
2863 if (!getLeaseLimit(subnet_elem, ltype, limit)) {
2864 ltype = Lease::TYPE_PD;
2865 if (!getLeaseLimit(subnet_elem, ltype, limit)) {
2866 // No limits for the subnet so none exceeded!
2867 return ("");
2868 }
2869 }
2870
2871 // If the limit is > 0 look up the class lease count. Limit of 0 always
2872 // denies the lease.
2873 int64_t lease_count = 0;
2874 if (limit) {
2875 lease_count = getSubnetStat(subnet_id, (ltype == Lease::TYPE_NA ?
2876 "assigned-nas" : "assigned-pds"));
2877 }
2878
2879 // If we're over the limit, return the error.
2880 if (lease_count >= limit) {
2881 std::ostringstream ss;
2882 ss << (ltype == Lease::TYPE_NA ? "address" : "prefix")
2883 << " limit " << limit << " for subnet ID " << subnet_id
2884 << ", current lease count " << lease_count;
2885 return (ss.str());
2886 }
2887 }
2888
2889 // No limits exceeded!
2890 return ("");
2891}
2892
2893bool
2895 return true;
2896}
2897
2898int64_t
2899Memfile_LeaseMgr::getSubnetStat(const SubnetID& subnet_id, const std::string& stat_label) const {
2902 std::string stat_name = StatsMgr::generateName("subnet", subnet_id, stat_label);
2903 ConstElementPtr stat = StatsMgr::instance().get(stat_name);
2904 ConstElementPtr samples = stat->get(stat_name);
2905 if (samples && samples->size()) {
2906 auto sample = samples->get(0);
2907 if (sample->size()) {
2908 auto count_elem = sample->get(0);
2909 return (count_elem->intValue());
2910 }
2911 }
2912
2913 return (0);
2914}
2915
2916bool
2917Memfile_LeaseMgr::getLeaseLimit(ConstElementPtr parent, Lease::Type ltype, size_t& limit) const {
2918 ConstElementPtr limit_elem = parent->get(ltype == Lease::TYPE_PD ?
2919 "prefix-limit" : "address-limit");
2920 if (limit_elem) {
2921 limit = limit_elem->intValue();
2922 return (true);
2923 }
2924
2925 return (false);
2926}
2927
2928namespace {
2929
2930std::string
2931idToText(const OptionBuffer& id) {
2932 std::stringstream tmp;
2933 tmp << std::hex;
2934 bool delim = false;
2935 for (auto const& it : id) {
2936 if (delim) {
2937 tmp << ":";
2938 }
2939 tmp << std::setw(2) << std::setfill('0')
2940 << static_cast<unsigned int>(it);
2941 delim = true;
2942 }
2943 return (tmp.str());
2944}
2945
2946} // anonymous namespace
2947
2950 const IOAddress& lower_bound_address,
2951 const LeasePageSize& page_size,
2952 const time_t& qry_start_time /* = 0 */,
2953 const time_t& qry_end_time /* = 0 */) {
2956 .arg(page_size.page_size_)
2957 .arg(lower_bound_address.toText())
2958 .arg(idToText(relay_id))
2959 .arg(qry_start_time)
2960 .arg(qry_end_time);
2961
2962 // Expecting IPv4 address.
2963 if (!lower_bound_address.isV4()) {
2964 isc_throw(InvalidAddressFamily, "expected IPv4 address while "
2965 "retrieving leases from the lease database, got "
2966 << lower_bound_address);
2967 }
2968
2969 // Catch 2038 bug with 32 bit time_t.
2970 if ((qry_start_time < 0) || (qry_end_time < 0)) {
2971 isc_throw(BadValue, "negative time value");
2972 }
2973
2974 // Start time must be before end time.
2975 if ((qry_start_time > 0) && (qry_end_time > 0) &&
2976 (qry_start_time > qry_end_time)) {
2977 isc_throw(BadValue, "start time must be before end time");
2978 }
2979
2980 if (MultiThreadingMgr::instance().getMode()) {
2981 std::lock_guard<std::mutex> lock(*mutex_);
2982 return (getLeases4ByRelayIdInternal(relay_id,
2983 lower_bound_address,
2984 page_size,
2985 qry_start_time,
2986 qry_end_time));
2987 } else {
2988 return (getLeases4ByRelayIdInternal(relay_id,
2989 lower_bound_address,
2990 page_size,
2991 qry_start_time,
2992 qry_end_time));
2993 }
2994}
2995
2997Memfile_LeaseMgr::getLeases4ByRelayIdInternal(const OptionBuffer& relay_id,
2998 const IOAddress& lower_bound_address,
2999 const LeasePageSize& page_size,
3000 const time_t& qry_start_time,
3001 const time_t& qry_end_time) {
3002 Lease4Collection collection;
3003 const Lease4StorageRelayIdIndex& idx = storage4_.get<RelayIdIndexTag>();
3004 Lease4StorageRelayIdIndex::const_iterator lb =
3005 idx.lower_bound(boost::make_tuple(relay_id, lower_bound_address));
3006 // Return all convenient leases being within the page size.
3007 IOAddress last_addr = lower_bound_address;
3008 for (; lb != idx.end(); ++lb) {
3009 if ((*lb)->addr_ == last_addr) {
3010 // Already seen: skip it.
3011 continue;
3012 }
3013 if ((*lb)->relay_id_ != relay_id) {
3014 // Gone after the relay id index.
3015 break;
3016 }
3017 last_addr = (*lb)->addr_;
3018 if ((qry_start_time > 0) && ((*lb)->cltt_ < qry_start_time)) {
3019 // Too old.
3020 continue;
3021 }
3022 if ((qry_end_time > 0) && ((*lb)->cltt_ > qry_end_time)) {
3023 // Too young.
3024 continue;
3025 }
3026 collection.push_back(Lease4Ptr(new Lease4(**lb)));
3027 if (collection.size() >= page_size.page_size_) {
3028 break;
3029 }
3030 }
3031 return (collection);
3032}
3033
3036 const IOAddress& lower_bound_address,
3037 const LeasePageSize& page_size,
3038 const time_t& qry_start_time /* = 0 */,
3039 const time_t& qry_end_time /* = 0 */) {
3042 .arg(page_size.page_size_)
3043 .arg(lower_bound_address.toText())
3044 .arg(idToText(remote_id))
3045 .arg(qry_start_time)
3046 .arg(qry_end_time);
3047
3048 // Expecting IPv4 address.
3049 if (!lower_bound_address.isV4()) {
3050 isc_throw(InvalidAddressFamily, "expected IPv4 address while "
3051 "retrieving leases from the lease database, got "
3052 << lower_bound_address);
3053 }
3054
3055 // Catch 2038 bug with 32 bit time_t.
3056 if ((qry_start_time < 0) || (qry_end_time < 0)) {
3057 isc_throw(BadValue, "negative time value");
3058 }
3059
3060 // Start time must be before end time.
3061 if ((qry_start_time > 0) && (qry_end_time > 0) &&
3062 (qry_start_time > qry_end_time)) {
3063 isc_throw(BadValue, "start time must be before end time");
3064 }
3065
3066 if (MultiThreadingMgr::instance().getMode()) {
3067 std::lock_guard<std::mutex> lock(*mutex_);
3068 return (getLeases4ByRemoteIdInternal(remote_id,
3069 lower_bound_address,
3070 page_size,
3071 qry_start_time,
3072 qry_end_time));
3073 } else {
3074 return (getLeases4ByRemoteIdInternal(remote_id,
3075 lower_bound_address,
3076 page_size,
3077 qry_start_time,
3078 qry_end_time));
3079 }
3080}
3081
3083Memfile_LeaseMgr::getLeases4ByRemoteIdInternal(const OptionBuffer& remote_id,
3084 const IOAddress& lower_bound_address,
3085 const LeasePageSize& page_size,
3086 const time_t& qry_start_time,
3087 const time_t& qry_end_time) {
3088 Lease4Collection collection;
3089 std::map<IOAddress, Lease4Ptr> sorted;
3090 const Lease4StorageRemoteIdIndex& idx = storage4_.get<RemoteIdIndexTag>();
3091 Lease4StorageRemoteIdRange er = idx.equal_range(remote_id);
3092 // Store all convenient leases being within the page size.
3093 BOOST_FOREACH(auto const& it, er) {
3094 const IOAddress& addr = it->addr_;
3095 if (addr <= lower_bound_address) {
3096 // Not greater than lower_bound_address.
3097 continue;
3098 }
3099 if ((qry_start_time > 0) && (it->cltt_ < qry_start_time)) {
3100 // Too old.
3101 continue;
3102 }
3103 if ((qry_end_time > 0) && (it->cltt_ > qry_end_time)) {
3104 // Too young.
3105 continue;
3106 }
3107 sorted[addr] = it;
3108 }
3109
3110 // Return all leases being within the page size.
3111 for (auto const& it : sorted) {
3112 collection.push_back(Lease4Ptr(new Lease4(*it.second)));
3113 if (collection.size() >= page_size.page_size_) {
3114 break;
3115 }
3116 }
3117 return (collection);
3118}
3119
3120void
3122 if (MultiThreadingMgr::instance().getMode()) {
3123 std::lock_guard<std::mutex> lock(*mutex_);
3124 relay_id6_.clear();
3125 remote_id6_.clear();
3126 } else {
3127 relay_id6_.clear();
3128 remote_id6_.clear();
3129 }
3130}
3131
3132size_t
3134 return (relay_id6_.size());
3135}
3136
3137size_t
3139 return (remote_id6_.size());
3140}
3141
3144 const IOAddress& lower_bound_address,
3145 const LeasePageSize& page_size) {
3148 .arg(page_size.page_size_)
3149 .arg(lower_bound_address.toText())
3150 .arg(relay_id.toText());
3151
3152 // Expecting IPv6 valid address.
3153 if (!lower_bound_address.isV6()) {
3154 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
3155 "retrieving leases from the lease database, got "
3156 << lower_bound_address);
3157 }
3158
3159 if (MultiThreadingMgr::instance().getMode()) {
3160 std::lock_guard<std::mutex> lock(*mutex_);
3161 return (getLeases6ByRelayIdInternal(relay_id,
3162 lower_bound_address,
3163 page_size));
3164 } else {
3165 return (getLeases6ByRelayIdInternal(relay_id,
3166 lower_bound_address,
3167 page_size));
3168 }
3169}
3170
3172Memfile_LeaseMgr::getLeases6ByRelayIdInternal(const DUID& relay_id,
3173 const IOAddress& lower_bound_address,
3174 const LeasePageSize& page_size) {
3175 const std::vector<uint8_t>& relay_id_data = relay_id.getDuid();
3176 Lease6Collection collection;
3177 const RelayIdIndex& idx = relay_id6_.get<RelayIdIndexTag>();
3178 RelayIdIndex::const_iterator lb =
3179 idx.lower_bound(boost::make_tuple(relay_id_data, lower_bound_address));
3180
3181 // Return all leases being within the page size.
3182 IOAddress last_addr = lower_bound_address;
3183 for (; lb != idx.end(); ++lb) {
3184 if ((*lb)->lease_addr_ == last_addr) {
3185 // Already seen: skip it.
3186 continue;
3187 }
3188 if ((*lb)->id_ != relay_id_data) {
3189 // Gone after the relay id index.
3190 break;
3191 }
3192 last_addr = (*lb)->lease_addr_;
3193 Lease6Ptr lease = getAnyLease6Internal(last_addr);
3194 if (lease) {
3195 collection.push_back(lease);
3196 if (collection.size() >= page_size.page_size_) {
3197 break;
3198 }
3199 }
3200 }
3201 return (collection);
3202}
3203
3206 const IOAddress& lower_bound_address,
3207 const LeasePageSize& page_size) {
3210 .arg(page_size.page_size_)
3211 .arg(lower_bound_address.toText())
3212 .arg(idToText(remote_id));
3213
3214 // Expecting IPv6 valid address.
3215 if (!lower_bound_address.isV6()) {
3216 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
3217 "retrieving leases from the lease database, got "
3218 << lower_bound_address);
3219 }
3220
3221 if (MultiThreadingMgr::instance().getMode()) {
3222 std::lock_guard<std::mutex> lock(*mutex_);
3223 return (getLeases6ByRemoteIdInternal(remote_id,
3224 lower_bound_address,
3225 page_size));
3226 } else {
3227 return (getLeases6ByRemoteIdInternal(remote_id,
3228 lower_bound_address,
3229 page_size));
3230 }
3231}
3232
3234Memfile_LeaseMgr::getLeases6ByRemoteIdInternal(const OptionBuffer& remote_id,
3235 const IOAddress& lower_bound_address,
3236 const LeasePageSize& page_size) {
3237 Lease6Collection collection;
3238 std::set<IOAddress> sorted;
3239 const RemoteIdIndex& idx = remote_id6_.get<RemoteIdIndexTag>();
3240 RemoteIdIndexRange er = idx.equal_range(remote_id);
3241 // Store all addresses greater than lower_bound_address.
3242 BOOST_FOREACH(auto const& it, er) {
3243 const IOAddress& addr = it->lease_addr_;
3244 if (addr <= lower_bound_address) {
3245 continue;
3246 }
3247 static_cast<void>(sorted.insert(addr));
3248 }
3249
3250 // Return all leases being within the page size.
3251 for (const IOAddress& addr : sorted) {
3252 Lease6Ptr lease = getAnyLease6Internal(addr);
3253 if (lease) {
3254 collection.push_back(lease);
3255 if (collection.size() >= page_size.page_size_) {
3256 break;
3257 }
3258 }
3259 }
3260 return (collection);
3261}
3262
3263size_t
3264Memfile_LeaseMgr::extractExtendedInfo4(bool update, bool current) {
3266 if (current) {
3267 cfg = CfgMgr::instance().getCurrentCfg()->getConsistency();
3268 } else {
3269 cfg = CfgMgr::instance().getStagingCfg()->getConsistency();
3270 }
3271 if (!cfg) {
3272 isc_throw(Unexpected, "the " << (current ? "current" : "staging")
3273 << " consistency configuration is null");
3274 }
3275 auto check = cfg->getExtendedInfoSanityCheck();
3276
3280 .arg(update ? " updating in file" : "");
3281
3282 size_t leases = 0;
3283 size_t modified = 0;
3284 size_t updated = 0;
3285 size_t processed = 0;
3286 auto& index = storage4_.get<AddressIndexTag>();
3287 auto lease_it = index.begin();
3288 auto next_it = index.end();
3289
3290 for (; lease_it != index.end(); lease_it = next_it) {
3291 next_it = std::next(lease_it);
3292 Lease4Ptr lease = *lease_it;
3293 ++leases;
3294 try {
3295 if (upgradeLease4ExtendedInfo(lease, check)) {
3296 ++modified;
3297 if (update && persistLeases(V4)) {
3298 lease_file4_->append(*lease);
3299 ++updated;
3300 }
3301 }
3302 // Work on a copy as the multi-index requires fields used
3303 // as indexes to be read-only.
3304 Lease4Ptr copy(new Lease4(*lease));
3306 if (!copy->relay_id_.empty() || !copy->remote_id_.empty()) {
3307 index.replace(lease_it, copy);
3308 ++processed;
3309 }
3310 } catch (const std::exception& ex) {
3313 .arg(lease->addr_.toText())
3314 .arg(ex.what());
3315 }
3316 }
3317
3319 .arg(leases)
3320 .arg(modified)
3321 .arg(updated)
3322 .arg(processed);
3323
3324 return (updated);
3325}
3326
3327size_t
3329 return (0);
3330}
3331
3332void
3334 CfgConsistencyPtr cfg = CfgMgr::instance().getStagingCfg()->getConsistency();
3335 if (!cfg) {
3336 isc_throw(Unexpected, "the staging consistency configuration is null");
3337 }
3338 auto check = cfg->getExtendedInfoSanityCheck();
3339 bool enabled = getExtendedInfoTablesEnabled();
3340
3344 .arg(enabled ? "enabled" : "disabled");
3345
3346 // Clear tables when enabled.
3347 if (enabled) {
3348 relay_id6_.clear();
3349 remote_id6_.clear();
3350 }
3351
3352 size_t leases = 0;
3353 size_t modified = 0;
3354 size_t processed = 0;
3355
3356 for (auto const& lease : storage6_) {
3357 ++leases;
3358 try {
3359 if (upgradeLease6ExtendedInfo(lease, check)) {
3360 ++modified;
3361 }
3362 if (enabled && addExtendedInfo6(lease)) {
3363 ++processed;
3364 }
3365 } catch (const std::exception& ex) {
3368 .arg(lease->addr_.toText())
3369 .arg(ex.what());
3370 }
3371 }
3372
3374 .arg(leases)
3375 .arg(modified)
3376 .arg(processed);
3377}
3378
3379size_t
3381 return (0);
3382}
3383
3384void
3386 LeaseAddressRelayIdIndex& relay_id_idx =
3388 static_cast<void>(relay_id_idx.erase(addr));
3389 LeaseAddressRemoteIdIndex& remote_id_idx =
3391 static_cast<void>(remote_id_idx.erase(addr));
3392}
3393
3394void
3396 const std::vector<uint8_t>& relay_id) {
3397 Lease6ExtendedInfoPtr ex_info;
3398 ex_info.reset(new Lease6ExtendedInfo(lease_addr, relay_id));
3399 relay_id6_.insert(ex_info);
3400}
3401
3402void
3404 const std::vector<uint8_t>& remote_id) {
3405 Lease6ExtendedInfoPtr ex_info;
3406 ex_info.reset(new Lease6ExtendedInfo(lease_addr, remote_id));
3407 remote_id6_.insert(ex_info);
3408}
3409
3410void
3411Memfile_LeaseMgr::writeLeases4(const std::string& filename) {
3412 if (MultiThreadingMgr::instance().getMode()) {
3413 std::lock_guard<std::mutex> lock(*mutex_);
3414 writeLeases4Internal(filename);
3415 } else {
3416 writeLeases4Internal(filename);
3417 }
3418}
3419
3420void
3421Memfile_LeaseMgr::writeLeases4Internal(const std::string& filename) {
3422 bool overwrite = (lease_file4_ && lease_file4_->getFilename() == filename);
3423 try {
3424 if (overwrite) {
3425 lease_file4_->close();
3426 }
3427 std::ostringstream old;
3428 old << filename << ".bak" << getpid();
3429 ::rename(filename.c_str(), old.str().c_str());
3430 CSVLeaseFile4 backup(filename);
3431 backup.open();
3432 for (auto const& lease : storage4_) {
3433 backup.append(*lease);
3434 }
3435 backup.close();
3436 if (overwrite) {
3437 lease_file4_->open(true);
3438 }
3439 } catch (const std::exception&) {
3440 if (overwrite) {
3441 lease_file4_->open(true);
3442 }
3443 throw;
3444 }
3445}
3446
3447void
3448Memfile_LeaseMgr::writeLeases6(const std::string& filename) {
3449 if (MultiThreadingMgr::instance().getMode()) {
3450 std::lock_guard<std::mutex> lock(*mutex_);
3451 writeLeases6Internal(filename);
3452 } else {
3453 writeLeases6Internal(filename);
3454 }
3455}
3456
3457void
3458Memfile_LeaseMgr::writeLeases6Internal(const std::string& filename) {
3459 bool overwrite = (lease_file6_ && lease_file6_->getFilename() == filename);
3460 try {
3461 if (overwrite) {
3462 lease_file6_->close();
3463 }
3464 std::ostringstream old;
3465 old << filename << ".bak" << getpid();
3466 ::rename(filename.c_str(), old.str().c_str());
3467 CSVLeaseFile6 backup(filename);
3468 backup.open();
3469 for (auto const& lease : storage6_) {
3470 backup.append(*lease);
3471 }
3472 backup.close();
3473 if (overwrite) {
3474 lease_file6_->open(true);
3475 }
3476 } catch (const std::exception&) {
3477 if (overwrite) {
3478 lease_file6_->open(true);
3479 }
3480 throw;
3481 }
3482}
3483
3490
3491} // namespace dhcp
3492} // namespace isc
int version()
returns Kea hooks version.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a function is called in a prohibited way.
A generic exception that is thrown when an unexpected error condition occurs.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown on failure to open database.
Invalid address family used as input to Lease Manager.
Provides methods to access CSV file with DHCPv4 leases.
Provides methods to access CSV file with DHCPv6 leases.
static std::string sanityCheckToText(LeaseSanity check_type)
Converts lease sanity check value to printable text.
util::Optional< std::string > getDataDir() const
returns path do the data directory
Definition cfgmgr.cc:34
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:235
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:28
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition cfgmgr.cc:120
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition cfgmgr.cc:115
void addLease(LeasePtr lease)
Increment the counts for all of a lease's classes by one.
void clear()
Remove all entries.
void removeLease(LeasePtr lease)
Decrement the counts for all of a lease's classes by one.
void updateLease(LeasePtr new_lease, LeasePtr old_lease)
Adjust class lease counts given a new and old version of a lease.
size_t getClassCount(const ClientClass &client_class, const Lease::Type &ltype=Lease::TYPE_V4) const
Fetches the lease count for the given class and lease type.
Holds Client identifier or client IPv4 address.
Definition duid.h:222
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
Definition duid.cc:69
Holds DUID (DHCPv6 Unique Identifier)
Definition duid.h:142
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition duid.cc:33
std::string toText() const
Returns textual representation of the identifier (e.g.
Definition duid.h:88
Represents a configuration for Lease File Cleanup.
void execute()
Spawns a new process.
int getExitStatus() const
Returns exit code of the last completed cleanup.
bool isRunning() const
Checks if the lease file cleanup is in progress.
LFCSetup(asiolink::IntervalTimer::Callback callback)
Constructor.
void setup(const uint32_t lfc_interval, const boost::shared_ptr< CSVLeaseFile4 > &lease_file4, const boost::shared_ptr< CSVLeaseFile6 > &lease_file6, bool run_once_now=false)
Sets the new configuration for the Lease File Cleanup.
Lease6 extended informations for Bulk Lease Query.
static void load(LeaseFileType &lease_file, StorageType &storage, const uint32_t max_errors=0, const bool close_file_on_exit=true)
Load leases from the lease file into the specified storage.
void setExtendedInfoTablesEnabled(const bool enabled)
Modifies the setting whether the lease6 extended info tables are enabled.
Definition lease_mgr.h:1020
static bool upgradeLease6ExtendedInfo(const Lease6Ptr &lease, CfgConsistency::ExtendedInfoSanity check=CfgConsistency::EXTENDED_INFO_CHECK_FIX)
Upgrade a V6 lease user context to the new extended info entry.
Definition lease_mgr.cc:776
bool getExtendedInfoTablesEnabled() const
Returns the setting indicating if lease6 extended info tables are enabled.
Definition lease_mgr.h:1012
static void extractLease4ExtendedInfo(const Lease4Ptr &lease, bool ignore_errors=true)
Extract relay and remote identifiers from the extended info.
static bool upgradeLease4ExtendedInfo(const Lease4Ptr &lease, CfgConsistency::ExtendedInfoSanity check=CfgConsistency::EXTENDED_INFO_CHECK_FIX)
The following queries are used to fulfill Bulk Lease Query queries.
Definition lease_mgr.cc:555
virtual bool addExtendedInfo6(const Lease6Ptr &lease)
Extract extended info from a lease6 and add it into tables.
Wraps value holding size of the page with leases.
Definition lease_mgr.h:46
const size_t page_size_
Holds page size.
Definition lease_mgr.h:56
Base class for fulfilling a statistical lease data query.
Definition lease_mgr.h:149
SubnetID getLastSubnetID() const
Returns the value of last subnet ID specified (or zero)
Definition lease_mgr.h:209
SubnetID getFirstSubnetID() const
Returns the value of first subnet ID specified (or zero)
Definition lease_mgr.h:204
SelectMode getSelectMode() const
Returns the selection criteria mode The value returned is based upon the constructor variant used and...
Definition lease_mgr.h:216
SelectMode
Defines the types of selection criteria supported.
Definition lease_mgr.h:152
Memfile derivation of the IPv4 statistical lease data query.
MemfileLeaseStatsQuery4(Lease4Storage &storage4, const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Constructor for a subnet range query.
MemfileLeaseStatsQuery4(Lease4Storage &storage4, const SelectMode &select_mode=ALL_SUBNETS)
Constructor for an all subnets query.
virtual ~MemfileLeaseStatsQuery4()
Destructor.
void start()
Creates the IPv4 lease statistical data result set.
MemfileLeaseStatsQuery4(Lease4Storage &storage4, const SubnetID &subnet_id)
Constructor for a single subnet query.
Memfile derivation of the IPv6 statistical lease data query.
void start()
Creates the IPv6 lease statistical data result set.
virtual ~MemfileLeaseStatsQuery6()
Destructor.
MemfileLeaseStatsQuery6(Lease6Storage &storage6, const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Constructor for a subnet range query.
MemfileLeaseStatsQuery6(Lease6Storage &storage6, const SelectMode &select_mode=ALL_SUBNETS)
Constructor.
MemfileLeaseStatsQuery6(Lease6Storage &storage6, const SubnetID &subnet_id)
Constructor for a single subnet query.
Base Memfile derivation of the statistical lease data query.
std::vector< LeaseStatsRow >::iterator next_pos_
An iterator for accessing the next row within the result set.
MemfileLeaseStatsQuery(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Constructor for subnet range query.
virtual bool getNextRow(LeaseStatsRow &row)
Fetches the next row in the result set.
std::vector< LeaseStatsRow > rows_
A vector containing the "result set".
MemfileLeaseStatsQuery(const SelectMode &select_mode=ALL_SUBNETS)
Constructor for all subnets query.
virtual ~MemfileLeaseStatsQuery()
Destructor.
int getRowCount() const
Returns the number of rows in the result set.
MemfileLeaseStatsQuery(const SubnetID &subnet_id)
Constructor for single subnet query.
Lease6ExtendedInfoRemoteIdTable remote_id6_
stores IPv6 by-remote-id cross-reference table
virtual void rollback() override
Rollback Transactions.
size_t extractExtendedInfo4(bool update, bool current)
Extract extended info for v4 leases.
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id) override
Creates and runs the IPv6 lease stats query for a single subnet.
virtual void clearClassLeaseCounts() override
Clears the class-lease count map.
virtual size_t byRelayId6size() const override
Return the by-relay-id table size.
virtual Lease4Collection getLeases4ByRelayId(const OptionBuffer &relay_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size, const time_t &qry_start_time=0, const time_t &qry_end_time=0) override
The following queries are used to fulfill Bulk Lease Query queries.
Memfile_LeaseMgr(const db::DatabaseConnection::ParameterMap &parameters)
The sole lease manager constructor.
bool isLFCRunning() const
Checks if the process performing lease file cleanup is running.
virtual void writeLeases4(const std::string &filename) override
Write V4 leases to a file.
virtual void updateLease4(const Lease4Ptr &lease4) override
Updates IPv4 lease.
virtual size_t wipeLeases4(const SubnetID &subnet_id) override
Removes specified IPv4 leases.
virtual void deleteExtendedInfo6(const isc::asiolink::IOAddress &addr) override
Delete lease6 extended info from tables.
virtual bool isJsonSupported() const override
Checks if JSON support is enabled in the database.
Universe
Specifies universe (V4, V6)
static TrackingLeaseMgrPtr factory(const isc::db::DatabaseConnection::ParameterMap &parameters)
Factory class method.
LFCFileType
Types of the lease files used by the Lease File Cleanup.
@ FILE_PREVIOUS
Previous Lease File.
virtual bool deleteLease(const Lease4Ptr &lease) override
Deletes an IPv4 lease.
virtual void commit() override
Commit Transactions.
virtual size_t wipeLeases6(const SubnetID &subnet_id) override
Removed specified IPv6 leases.
virtual std::pair< uint32_t, uint32_t > getVersion(const std::string &timer_name=std::string()) const override
Returns backend version.
int getLFCExitStatus() const
Returns the status code returned by the last executed LFC process.
virtual LeaseStatsQueryPtr startPoolLeaseStatsQuery6() override
Creates and runs the IPv6 lease stats query for all subnets and pools.
bool persistLeases(Universe u) const
Specifies whether or not leases are written to disk.
virtual void addRemoteId6(const isc::asiolink::IOAddress &lease_addr, const std::vector< uint8_t > &remote_id) override
Add lease6 extended info into by-remote-id table.
static std::string getDBVersion()
Return extended version info.
virtual Lease4Collection getLeases4() const override
Returns all IPv4 leases.
virtual size_t getClassLeaseCount(const ClientClass &client_class, const Lease::Type &ltype=Lease::TYPE_V4) const override
Returns the class lease count for a given class and lease type.
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const override
Returns a collection of expired DHCPv6 leases.
void buildExtendedInfoTables6()
Extended information / Bulk Lease Query shared interface.
virtual bool addLease(const Lease4Ptr &lease) override
Adds an IPv4 lease.
virtual size_t byRemoteId6size() const override
Return the by-remote-id table size.
virtual void writeLeases6(const std::string &filename) override
Write V6 leases to a file.
Lease6ExtendedInfoRelayIdTable relay_id6_
stores IPv6 by-relay-id cross-reference table
virtual void recountClassLeases6() override
Recount the leases per class for V6 leases.
virtual Lease4Collection getLeases4ByRemoteId(const OptionBuffer &remote_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size, const time_t &qry_start_time=0, const time_t &qry_end_time=0) override
Returns existing IPv4 leases with a given remote-id.
virtual void wipeExtendedInfoTables6() override
Wipe extended info table (v6).
boost::shared_ptr< CSVLeaseFile4 > lease_file4_
Holds the pointer to the DHCPv4 lease file IO.
std::string getLeaseFilePath(Universe u) const
Returns an absolute path to the lease file.
virtual Lease6Collection getLeases6ByRemoteId(const OptionBuffer &remote_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size) override
Returns existing IPv6 leases with a given remote-id.
virtual Lease6Collection getLeases6() const override
Returns all IPv6 leases.
virtual void lfcCallback()
A callback function triggering Lease File Cleanup (LFC).
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id) override
Creates and runs the IPv4 lease stats query for a single subnet.
virtual Lease6Collection getLeases6ByRelayId(const DUID &relay_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size) override
Returns existing IPv6 leases with a given relay-id.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id) override
Creates and runs the IPv6 lease stats query for a single subnet.
virtual std::string getDescription() const override
Returns description of the backend.
static std::string appendSuffix(const std::string &file_name, const LFCFileType &file_type)
Appends appropriate suffix to the file name.
virtual void addRelayId6(const isc::asiolink::IOAddress &lease_addr, const std::vector< uint8_t > &relay_id) override
Add lease6 extended info into by-relay-id table.
virtual size_t upgradeExtendedInfo6(const LeasePageSize &page_size) override
Upgrade extended info (v6).
virtual LeaseStatsQueryPtr startLeaseStatsQuery4() override
Creates and runs the IPv4 lease stats query.
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const override
Returns existing IPv6 lease for a given IPv6 address.
virtual void recountClassLeases4() override
Recount the leases per class for V4 leases.
virtual void updateLease6(const Lease6Ptr &lease6) override
Updates IPv6 lease.
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id) override
Creates and runs the IPv4 lease stats query for a single subnet.
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) override
Deletes all expired-reclaimed DHCPv4 leases.
static std::string getDBVersionInternal(Universe const &u)
Local version of getDBVersion() class method.
virtual LeaseStatsQueryPtr startPoolLeaseStatsQuery4() override
Creates and runs the IPv4 lease stats query for all subnets and pools.
virtual std::string checkLimits6(isc::data::ConstElementPtr const &user_context) const override
Checks if the IPv6 lease limits set in the given user context are exceeded.
virtual LeaseStatsQueryPtr startLeaseStatsQuery6() override
Creates and runs the IPv6 lease stats query.
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const override
Returns existing IPv4 lease for specified IPv4 address.
virtual ~Memfile_LeaseMgr()
Destructor (closes file)
virtual std::string checkLimits4(isc::data::ConstElementPtr const &user_context) const override
Checks if the IPv4 lease limits set in the given user context are exceeded.
boost::shared_ptr< CSVLeaseFile6 > lease_file6_
Holds the pointer to the DHCPv6 lease file IO.
std::string getDefaultLeaseFilePath(Universe u) const
Returns default path to the lease file.
virtual size_t upgradeExtendedInfo4(const LeasePageSize &page_size) override
Upgrade extended info (v4).
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) override
Deletes all expired-reclaimed DHCPv6 leases.
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const override
Returns a collection of expired DHCPv4 leases.
Attempt to update lease that was not there.
Manages a pool of asynchronous interval timers.
Definition timer_mgr.h:62
Introduces callbacks into the LeaseMgr.
void trackAddLease(const LeasePtr &lease)
Invokes the callbacks when a new lease is added.
void trackUpdateLease(const LeasePtr &lease)
Invokes the callbacks when a lease is updated.
void trackDeleteLease(const LeasePtr &lease)
Invokes the callbacks when a lease is deleted.
bool hasCallbacks() const
Checks if any callbacks have been registered.
static StatsMgr & instance()
Statistics Manager accessor method.
static std::string generateName(const std::string &context, Type index, const std::string &stat_name)
Generates statistic name in a given context.
Exception thrown when an error occurs during CSV file processing.
Definition csv_file.h:22
Provides input/output access to CSV files.
Definition csv_file.h:358
RAII class creating a critical section.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
Class to help with processing PID files.
Definition pid_file.h:40
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
isc::data::ConstElementPtr get(const std::string &name) const
Returns a single statistic as a JSON structure.
static const int MINOR_VERSION_V4
the minor version of the v4 memfile backend
static const int MINOR_VERSION_V6
the minor version of the v6 memfile backend
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition data.cc:1420
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition data.cc:1547
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_START
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_MEMFILE_GET_SUBID_CLIENTID
const isc::log::MessageID DHCPSRV_MEMFILE_EXTRACT_EXTENDED_INFO4
std::string ClientClass
Defines a single class name.
Definition classify.h:43
boost::multi_index_container< Lease4Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< AddressIndexTag >, boost::multi_index::member< Lease, isc::asiolink::IOAddress, &Lease::addr_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< HWAddressSubnetIdIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::const_mem_fun< Lease, const std::vector< uint8_t > &, &Lease::getHWAddrVector >, boost::multi_index::member< Lease, SubnetID, &Lease::subnet_id_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ClientIdSubnetIdIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::const_mem_fun< Lease4, const std::vector< uint8_t > &, &Lease4::getClientIdVector >, boost::multi_index::member< Lease, uint32_t, &Lease::subnet_id_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ExpirationIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::const_mem_fun< Lease, bool, &Lease::stateExpiredReclaimed >, boost::multi_index::const_mem_fun< Lease, int64_t, &Lease::getExpirationTime > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetIdIndexTag >, boost::multi_index::member< Lease, isc::dhcp::SubnetID, &Lease::subnet_id_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< HostnameIndexTag >, boost::multi_index::member< Lease, std::string, &Lease::hostname_ > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< RemoteIdIndexTag >, boost::multi_index::member< Lease4, std::vector< uint8_t >, &Lease4::remote_id_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< RelayIdIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::member< Lease4, std::vector< uint8_t >, &Lease4::relay_id_ >, boost::multi_index::member< Lease, isc::asiolink::IOAddress, &Lease::addr_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetIdPoolIdIndexTag >, boost::multi_index::composite_key< Lease4, boost::multi_index::member< Lease, SubnetID, &Lease::subnet_id_ >, boost::multi_index::member< Lease, uint32_t, &Lease::pool_id_ > > > > > Lease4Storage
A multi index container holding DHCPv4 leases.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_SUBID_HWADDR
const isc::log::MessageID DHCPSRV_MEMFILE_WIPE_LEASES6
const isc::log::MessageID DHCPSRV_MEMFILE_GET4
const isc::log::MessageID DHCPSRV_MEMFILE_BUILD_EXTENDED_INFO_TABLES6_ERROR
const isc::log::MessageID DHCPSRV_MEMFILE_GET_REMOTEID6
const isc::log::MessageID DHCPSRV_MEMFILE_GET_SUBID_PAGE6
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_UNREGISTER_TIMER_FAILED
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_SPAWN_FAIL
const isc::log::MessageID DHCPSRV_MEMFILE_DB
Lease6Storage::index< ExpirationIndexTag >::type Lease6StorageExpirationIndex
DHCPv6 lease storage index by expiration time.
const isc::log::MessageID DHCPSRV_MEMFILE_CONVERTING_LEASE_FILES
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_SETUP
const isc::log::MessageID DHCPSRV_MEMFILE_WIPE_LEASES6_FINISHED
Lease6Storage::index< DuidIndexTag >::type Lease6StorageDuidIndex
DHCPv6 lease storage index by DUID.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:528
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition lease.h:693
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition lease_mgr.h:233
const isc::log::MessageID DHCPSRV_MEMFILE_WIPE_LEASES4
Lease6Storage::index< SubnetIdPoolIdIndexTag >::type Lease6StorageSubnetIdPoolIdIndex
DHCPv6 lease storage index subnet-id and pool-id.
Lease6Storage::index< DuidIaidTypeIndexTag >::type Lease6StorageDuidIaidTypeIndex
DHCPv6 lease storage index by DUID, IAID, lease type.
boost::shared_ptr< TimerMgr > TimerMgrPtr
Type definition of the shared pointer to TimerMgr.
Definition timer_mgr.h:27
const isc::log::MessageID DHCPSRV_MEMFILE_GET_RELAYID4
const isc::log::MessageID DHCPSRV_MEMFILE_DELETE_EXPIRED_RECLAIMED_START
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_LEASE_FILE_RENAME_FAIL
const isc::log::MessageID DHCPSRV_MEMFILE_UPDATE_ADDR4
const isc::log::MessageID DHCPSRV_MEMFILE_GET_SUBID6
Lease4Storage::index< ExpirationIndexTag >::type Lease4StorageExpirationIndex
DHCPv4 lease storage index by expiration time.
Lease6Storage::index< AddressIndexTag >::type Lease6StorageAddressIndex
DHCPv6 lease storage index by address.
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
Definition dhcpsrv_log.h:38
const isc::log::MessageID DHCPSRV_MEMFILE_GET_ADDR4
const isc::log::MessageID DHCPSRV_MEMFILE_ROLLBACK
const isc::log::MessageID DHCPSRV_MEMFILE_UPDATE_ADDR6
Lease4Storage::index< HostnameIndexTag >::type Lease4StorageHostnameIndex
DHCPv4 lease storage index by hostname.
std::pair< Lease4StorageRemoteIdIndex::const_iterator, Lease4StorageRemoteIdIndex::const_iterator > Lease4StorageRemoteIdRange
DHCPv4 lease storage range by remote-id.
Lease6Storage::index< HostnameIndexTag >::type Lease6StorageHostnameIndex
DHCPv6 lease storage index by hostname.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_EXPIRED4
const isc::log::MessageID DHCPSRV_MEMFILE_GET_EXPIRED6
const isc::log::MessageID DHCPSRV_MEMFILE_GET_HOSTNAME4
boost::multi_index_container< Lease6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< AddressIndexTag >, boost::multi_index::member< Lease, isc::asiolink::IOAddress, &Lease::addr_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< DuidIaidTypeIndexTag >, boost::multi_index::composite_key< Lease6, boost::multi_index::const_mem_fun< Lease6, const std::vector< uint8_t > &, &Lease6::getDuidVector >, boost::multi_index::member< Lease6, uint32_t, &Lease6::iaid_ >, boost::multi_index::member< Lease6, Lease::Type, &Lease6::type_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ExpirationIndexTag >, boost::multi_index::composite_key< Lease6, boost::multi_index::const_mem_fun< Lease, bool, &Lease::stateExpiredReclaimed >, boost::multi_index::const_mem_fun< Lease, int64_t, &Lease::getExpirationTime > > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetIdIndexTag >, boost::multi_index::composite_key< Lease6, boost::multi_index::member< Lease, isc::dhcp::SubnetID, &Lease::subnet_id_ >, boost::multi_index::member< Lease, isc::asiolink::IOAddress, &Lease::addr_ > > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< DuidIndexTag >, boost::multi_index::const_mem_fun< Lease6, const std::vector< uint8_t > &, &Lease6::getDuidVector > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< HostnameIndexTag >, boost::multi_index::member< Lease, std::string, &Lease::hostname_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetIdPoolIdIndexTag >, boost::multi_index::composite_key< Lease6, boost::multi_index::member< Lease, SubnetID, &Lease::subnet_id_ >, boost::multi_index::member< Lease, uint32_t, &Lease::pool_id_ > > > > > Lease6Storage
A multi index container holding DHCPv6 leases.
Lease4Storage::index< ClientIdSubnetIdIndexTag >::type Lease4StorageClientIdSubnetIdIndex
DHCPv4 lease storage index by client-id and subnet-id.
Lease4Storage::index< HWAddressSubnetIdIndexTag >::type Lease4StorageHWAddressSubnetIdIndex
DHCPv4 lease storage index by HW address and subnet-id.
const isc::log::MessageID DHCPSRV_MEMFILE_EXTRACT_EXTENDED_INFO4_ERROR
const isc::log::MessageID DHCPSRV_MEMFILE_ADD_ADDR6
Lease6ExtendedInfoRelayIdTable::index< LeaseAddressIndexTag >::type LeaseAddressRelayIdIndex
Lease6 extended information by lease address index of by relay id table.
Lease6ExtendedInfoRelayIdTable::index< RelayIdIndexTag >::type RelayIdIndex
Lease6 extended information by relay id index.
Lease4Storage::index< RemoteIdIndexTag >::type Lease4StorageRemoteIdIndex
DHCPv4 lease storage index by remote-id.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
const isc::log::MessageID DHCPSRV_MEMFILE_BEGIN_EXTRACT_EXTENDED_INFO4
const isc::log::MessageID DHCPSRV_MEMFILE_WIPE_LEASES4_FINISHED
const isc::log::MessageID DHCPSRV_MEMFILE_GET_SUBID4
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_LEASE_FILE_REOPEN_FAIL
const isc::log::MessageID DHCPSRV_MEMFILE_COMMIT
const isc::log::MessageID DHCPSRV_MEMFILE_DELETE_EXPIRED_RECLAIMED4
boost::shared_ptr< CfgConsistency > CfgConsistencyPtr
Type used to for pointing to CfgConsistency structure.
const isc::log::MessageID DHCPSRV_MEMFILE_BEGIN_BUILD_EXTENDED_INFO_TABLES6
const isc::log::MessageID DHCPSRV_MEMFILE_GET_HOSTNAME6
const isc::log::MessageID DHCPSRV_MEMFILE_DELETE_EXPIRED_RECLAIMED6
const isc::log::MessageID DHCPSRV_MEMFILE_LFC_EXECUTE
std::pair< RemoteIdIndex::const_iterator, RemoteIdIndex::const_iterator > RemoteIdIndexRange
Lease6 extended information by remote id range.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_RELAYID6
const isc::log::MessageID DHCPSRV_MEMFILE_NO_STORAGE
const isc::log::MessageID DHCPSRV_MEMFILE_BUILD_EXTENDED_INFO_TABLES6
std::unique_ptr< TrackingLeaseMgr > TrackingLeaseMgrPtr
TrackingLeaseMgr pointer.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_PAGE4
const isc::log::MessageID DHCPSRV_MEMFILE_GET_HWADDR
const isc::log::MessageID DHCPSRV_MEMFILE_GET6_DUID
Lease4Storage::index< SubnetIdIndexTag >::type Lease4StorageSubnetIdIndex
DHCPv4 lease storage index subnet-id.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
const isc::log::MessageID DHCPSRV_MEMFILE_DELETE_ADDR4
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition lease.h:520
const isc::log::MessageID DHCPSRV_MEMFILE_GET_ADDR6
const isc::log::MessageID DHCPSRV_MEMFILE_ADD_ADDR4
const isc::log::MessageID DHCPSRV_MEMFILE_GET_CLIENTID
Lease4Storage::index< AddressIndexTag >::type Lease4StorageAddressIndex
DHCPv4 lease storage index by address.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_IAID_DUID
const isc::log::MessageID DHCPSRV_MEMFILE_GET6
const isc::log::MessageID DHCPSRV_MEMFILE_GET_REMOTEID4
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
Lease6ExtendedInfoRemoteIdTable::index< LeaseAddressIndexTag >::type LeaseAddressRemoteIdIndex
Lease6 extended information by lease address index of by remote id table.
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Definition dhcpsrv_log.h:26
Lease6Storage::index< SubnetIdIndexTag >::type Lease6StorageSubnetIdIndex
DHCPv6 lease storage index by subnet-id.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID
boost::shared_ptr< Lease6ExtendedInfo > Lease6ExtendedInfoPtr
Pointer to a Lease6ExtendedInfo object.
const isc::log::MessageID DHCPSRV_MEMFILE_DELETE_ADDR6
Lease6ExtendedInfoRemoteIdTable::index< RemoteIdIndexTag >::type RemoteIdIndex
Lease6 extended information by remote id index.
Lease4Storage::index< SubnetIdPoolIdIndexTag >::type Lease4StorageSubnetIdPoolIdIndex
DHCPv4 lease storage index subnet-id and pool-id.
Lease4Storage::index< RelayIdIndexTag >::type Lease4StorageRelayIdIndex
DHCPv4 lease storage index by relay-id.
const isc::log::MessageID DHCPSRV_MEMFILE_GET_PAGE6
Defines the logger used by the top-level component of kea-lfc.
Tag for indexes by address.
Tag for indexes by client-id, subnet-id tuple.
Tag for indexes by DUID, IAID, lease type tuple.
Tag for index using DUID.
Tag for indexes by expiration time.
Hardware type that represents information from DHCPv4 packet.
Definition hwaddr.h:20
std::vector< uint8_t > hwaddr_
Definition hwaddr.h:98
std::string toText(bool include_htype=true) const
Returns textual representation of a hardware address (e.g.
Definition hwaddr.cc:51
Tag for indexes by HW address, subnet-id tuple.
Tag for index using hostname.
Structure that holds a lease for IPv4 address.
Definition lease.h:323
Structure that holds a lease for IPv6 address and/or prefix.
Definition lease.h:536
ExtendedInfoAction
Action on extended info tables.
Definition lease.h:573
@ ACTION_UPDATE
update extended info tables.
Definition lease.h:576
@ ACTION_DELETE
delete reference to the lease
Definition lease.h:575
@ ACTION_IGNORE
ignore extended info,
Definition lease.h:574
Tag for indexes by lease address.
Contains a single row of lease statistical data.
Definition lease_mgr.h:64
static const uint32_t STATE_DEFAULT
A lease in the default state.
Definition lease.h:69
static const uint32_t STATE_DECLINED
Declined lease.
Definition lease.h:72
Type
Type of lease or pool.
Definition lease.h:46
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition lease.h:49
@ TYPE_V4
IPv4 lease.
Definition lease.h:50
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47
static const uint32_t STATE_REGISTERED
Registered self-generated lease.
Definition lease.h:81
static std::string typeToText(Type type)
returns text representation of a lease type
Definition lease.cc:56
Tag for index using relay-id.
Tag for index using remote-id.
Tag for indexes by subnet-id (and address for v6).