Kea 3.1.0
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#include <util/filesystem.h>
22
23#include <boost/foreach.hpp>
24#include <cstdio>
25#include <cstring>
26#include <errno.h>
27#include <iostream>
28#include <limits>
29#include <sstream>
30
31namespace {
32
40const char* KEA_LFC_EXECUTABLE_ENV_NAME = "KEA_LFC_EXECUTABLE";
41
42} // namespace
43
44using namespace isc::asiolink;
45using namespace isc::data;
46using namespace isc::db;
47using namespace isc::util;
48using namespace isc::stats;
49using namespace isc::util::file;
50
51namespace isc {
52namespace dhcp {
53
68class LFCSetup {
69public:
70
79
83 ~LFCSetup();
84
96 void setup(const uint32_t lfc_interval,
97 const boost::shared_ptr<CSVLeaseFile4>& lease_file4,
98 const boost::shared_ptr<CSVLeaseFile6>& lease_file6,
99 bool run_once_now = false);
100
102 void execute();
103
107 bool isRunning() const;
108
110 int getExitStatus() const;
111
112private:
113
116 boost::scoped_ptr<ProcessSpawn> process_;
117
120
122 pid_t pid_;
123
128 TimerMgrPtr timer_mgr_;
129};
130
132 : process_(), callback_(callback), pid_(0),
133 timer_mgr_(TimerMgr::instance()) {
134}
135
137 try {
138 // Remove the timer. This will throw an exception if the timer does not
139 // exist. There are several possible reasons for this:
140 // a) It hasn't been registered (although if the LFC Setup instance
141 // exists it means that the timer must have been registered or that
142 // such registration has been attempted).
143 // b) The registration may fail if the duplicate timer exists or if the
144 // TimerMgr's worker thread is running but if this happens it is a
145 // programming error.
146 // c) The program is shutting down and the timer has been removed by
147 // another component.
148 timer_mgr_->unregisterTimer("memfile-lfc");
149
150 } catch (const std::exception& ex) {
151 // We don't want exceptions being thrown from the destructor so we just
152 // log a message here. The message is logged at debug severity as
153 // we don't want an error message output during shutdown.
156 }
157}
158
159void
160LFCSetup::setup(const uint32_t lfc_interval,
161 const boost::shared_ptr<CSVLeaseFile4>& lease_file4,
162 const boost::shared_ptr<CSVLeaseFile6>& lease_file6,
163 bool run_once_now) {
164
165 // If to nothing to do, punt
166 if (lfc_interval == 0 && !run_once_now) {
167 return;
168 }
169
170 // Start preparing the command line for kea-lfc.
171 std::string executable;
172 char* c_executable = getenv(KEA_LFC_EXECUTABLE_ENV_NAME);
173 if (!c_executable) {
174 executable = KEA_LFC_EXECUTABLE;
175 } else {
176 executable = c_executable;
177 }
178
179 // Gather the base file name.
180 std::string lease_file = lease_file4 ? lease_file4->getFilename() :
181 lease_file6->getFilename();
182
183 // Create the other names by appending suffixes to the base name.
184 ProcessArgs args;
185 // Universe: v4 or v6.
186 args.push_back(lease_file4 ? "-4" : "-6");
187
188 // Previous file.
189 args.push_back("-x");
190 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
192 // Input file.
193 args.push_back("-i");
194 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
196 // Output file.
197 args.push_back("-o");
198 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
200 // Finish file.
201 args.push_back("-f");
202 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
204 // PID file.
205 args.push_back("-p");
206 args.push_back(Memfile_LeaseMgr::appendSuffix(lease_file,
208
209 // The configuration file is currently unused.
210 args.push_back("-c");
211 args.push_back("ignored-path");
212
213 // Create the process (do not start it yet).
214 process_.reset(new ProcessSpawn(ProcessSpawn::ASYNC, executable, args,
215 ProcessEnvVars(), true));
216
217 // If we've been told to run it once now, invoke the callback directly.
218 if (run_once_now) {
219 callback_();
220 }
221
222 // If it's supposed to run periodically, setup that now.
223 if (lfc_interval > 0) {
224 // Set the timer to call callback function periodically.
226
227 // Multiple the lfc_interval value by 1000 as this value specifies
228 // a timeout in seconds, whereas the setup() method expects the
229 // timeout in milliseconds.
230 timer_mgr_->registerTimer("memfile-lfc", callback_, lfc_interval * 1000,
232 timer_mgr_->setup("memfile-lfc");
233 }
234}
235
236void
238 try {
240 .arg(process_->getCommandLine());
241 pid_ = process_->spawn();
242
243 } catch (const ProcessSpawnError&) {
245 }
246}
247
248bool
250 return (process_ && process_->isRunning(pid_));
251}
252
253int
255 if (!process_) {
256 isc_throw(InvalidOperation, "unable to obtain LFC process exit code: "
257 " the process is null");
258 }
259 return (process_->getExitStatus(pid_));
260}
261
262
269public:
275 : LeaseStatsQuery(select_mode), rows_(0), next_pos_(rows_.end()) {
276 };
277
282 : LeaseStatsQuery(subnet_id), rows_(0), next_pos_(rows_.end()) {
283 };
284
289 MemfileLeaseStatsQuery(const SubnetID& first_subnet_id, const SubnetID& last_subnet_id)
290 : LeaseStatsQuery(first_subnet_id, last_subnet_id), rows_(0), next_pos_(rows_.end()) {
291 };
292
295
306 virtual bool getNextRow(LeaseStatsRow& row) {
307 if (next_pos_ == rows_.end()) {
308 return (false);
309 }
310
311 row = *next_pos_;
312 ++next_pos_;
313 return (true);
314 }
315
317 int getRowCount() const {
318 return (rows_.size());
319 }
320
321protected:
323 std::vector<LeaseStatsRow> rows_;
324
326 std::vector<LeaseStatsRow>::iterator next_pos_;
327};
328
339public:
346 const SelectMode& select_mode = ALL_SUBNETS)
347 : MemfileLeaseStatsQuery(select_mode), storage4_(storage4) {
348 };
349
354 MemfileLeaseStatsQuery4(Lease4Storage& storage4, const SubnetID& subnet_id)
355 : MemfileLeaseStatsQuery(subnet_id), storage4_(storage4) {
356 };
357
363 MemfileLeaseStatsQuery4(Lease4Storage& storage4, const SubnetID& first_subnet_id,
364 const SubnetID& last_subnet_id)
365 : MemfileLeaseStatsQuery(first_subnet_id, last_subnet_id), storage4_(storage4) {
366 };
367
370
385 void start() {
386 switch (getSelectMode()) {
387 case ALL_SUBNETS:
388 case SINGLE_SUBNET:
389 case SUBNET_RANGE:
390 startSubnets();
391 break;
392
393 case ALL_SUBNET_POOLS:
394 startSubnetPools();
395 break;
396 }
397 }
398
399private:
414 void startSubnets() {
416 = storage4_.get<SubnetIdIndexTag>();
417
418 // Set lower and upper bounds based on select mode
419 Lease4StorageSubnetIdIndex::const_iterator lower;
420 Lease4StorageSubnetIdIndex::const_iterator upper;
421
422 switch (getSelectMode()) {
423 case ALL_SUBNETS:
424 lower = idx.begin();
425 upper = idx.end();
426 break;
427
428 case SINGLE_SUBNET:
429 lower = idx.lower_bound(getFirstSubnetID());
430 upper = idx.upper_bound(getFirstSubnetID());
431 break;
432
433 case SUBNET_RANGE:
434 lower = idx.lower_bound(getFirstSubnetID());
435 upper = idx.upper_bound(getLastSubnetID());
436 break;
437
438 default:
439 return;
440 }
441
442 // Return an empty set if there are no rows.
443 if (lower == upper) {
444 return;
445 }
446
447 // Iterate over the leases in order by subnet, accumulating per
448 // subnet counts for each state of interest. As we finish each
449 // subnet, add the appropriate rows to our result set.
450 SubnetID cur_id = 0;
451 int64_t assigned = 0;
452 int64_t declined = 0;
453 for (Lease4StorageSubnetIdIndex::const_iterator lease = lower;
454 lease != upper; ++lease) {
455 // If we've hit the next subnet, add rows for the current subnet
456 // and wipe the accumulators
457 if ((*lease)->subnet_id_ != cur_id) {
458 if (cur_id > 0) {
459 if (assigned > 0) {
460 rows_.push_back(LeaseStatsRow(cur_id,
462 assigned));
463 assigned = 0;
464 }
465
466 if (declined > 0) {
467 rows_.push_back(LeaseStatsRow(cur_id,
469 declined));
470 declined = 0;
471 }
472 }
473
474 // Update current subnet id
475 cur_id = (*lease)->subnet_id_;
476 }
477
478 // Bump the appropriate accumulator
479 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
480 ++assigned;
481 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
482 ++declined;
483 }
484 }
485
486 // Make the rows for last subnet
487 if (assigned > 0) {
488 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DEFAULT,
489 assigned));
490 }
491
492 if (declined > 0) {
493 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DECLINED,
494 declined));
495 }
496
497 // Reset the next row position back to the beginning of the rows.
498 next_pos_ = rows_.begin();
499 }
500
515 void startSubnetPools() {
517 = storage4_.get<SubnetIdPoolIdIndexTag>();
518
519 // Set lower and upper bounds based on select mode
520 Lease4StorageSubnetIdPoolIdIndex::const_iterator lower;
521 Lease4StorageSubnetIdPoolIdIndex::const_iterator upper;
522 switch (getSelectMode()) {
523 case ALL_SUBNET_POOLS:
524 lower = idx.begin();
525 upper = idx.end();
526 break;
527
528 default:
529 return;
530 }
531
532 // Return an empty set if there are no rows.
533 if (lower == upper) {
534 return;
535 }
536
537 // Iterate over the leases in order by subnet and pool, accumulating per
538 // subnet and pool counts for each state of interest. As we finish each
539 // subnet or pool, add the appropriate rows to our result set.
540 SubnetID cur_id = 0;
541 uint32_t cur_pool_id = 0;
542 int64_t assigned = 0;
543 int64_t declined = 0;
544 for (Lease4StorageSubnetIdPoolIdIndex::const_iterator lease = lower;
545 lease != upper; ++lease) {
546 // If we've hit the next pool, add rows for the current subnet and
547 // pool and wipe the accumulators
548 if ((*lease)->pool_id_ != cur_pool_id) {
549 if (assigned > 0) {
550 rows_.push_back(LeaseStatsRow(cur_id,
552 assigned, cur_pool_id));
553 assigned = 0;
554 }
555
556 if (declined > 0) {
557 rows_.push_back(LeaseStatsRow(cur_id,
559 declined, cur_pool_id));
560 declined = 0;
561 }
562
563 // Update current pool id
564 cur_pool_id = (*lease)->pool_id_;
565 }
566
567 // If we've hit the next subnet, add rows for the current subnet
568 // and wipe the accumulators
569 if ((*lease)->subnet_id_ != cur_id) {
570 if (cur_id > 0) {
571 if (assigned > 0) {
572 rows_.push_back(LeaseStatsRow(cur_id,
574 assigned, cur_pool_id));
575 assigned = 0;
576 }
577
578 if (declined > 0) {
579 rows_.push_back(LeaseStatsRow(cur_id,
581 declined, cur_pool_id));
582 declined = 0;
583 }
584 }
585
586 // Update current subnet id
587 cur_id = (*lease)->subnet_id_;
588
589 // Reset pool id
590 cur_pool_id = 0;
591 }
592
593 // Bump the appropriate accumulator
594 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
595 ++assigned;
596 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
597 ++declined;
598 }
599 }
600
601 // Make the rows for last subnet
602 if (assigned > 0) {
603 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DEFAULT,
604 assigned, cur_pool_id));
605 }
606
607 if (declined > 0) {
608 rows_.push_back(LeaseStatsRow(cur_id, Lease::STATE_DECLINED,
609 declined, cur_pool_id));
610 }
611
612 // Reset the next row position back to the beginning of the rows.
613 next_pos_ = rows_.begin();
614 }
615
617 Lease4Storage& storage4_;
618};
619
620
631public:
638 const SelectMode& select_mode = ALL_SUBNETS)
639 : MemfileLeaseStatsQuery(select_mode), storage6_(storage6) {
640 };
641
646 MemfileLeaseStatsQuery6(Lease6Storage& storage6, const SubnetID& subnet_id)
647 : MemfileLeaseStatsQuery(subnet_id), storage6_(storage6) {
648 };
649
655 MemfileLeaseStatsQuery6(Lease6Storage& storage6, const SubnetID& first_subnet_id,
656 const SubnetID& last_subnet_id)
657 : MemfileLeaseStatsQuery(first_subnet_id, last_subnet_id), storage6_(storage6) {
658 };
659
662
678 void start() {
679 switch (getSelectMode()) {
680 case ALL_SUBNETS:
681 case SINGLE_SUBNET:
682 case SUBNET_RANGE:
683 startSubnets();
684 break;
685
686 case ALL_SUBNET_POOLS:
687 startSubnetPools();
688 break;
689 }
690 }
691
692private:
708 virtual void startSubnets() {
710 = storage6_.get<SubnetIdIndexTag>();
711
712 // Set lower and upper bounds based on select mode
713 Lease6StorageSubnetIdIndex::const_iterator lower;
714 Lease6StorageSubnetIdIndex::const_iterator upper;
715 switch (getSelectMode()) {
716 case ALL_SUBNETS:
717 lower = idx.begin();
718 upper = idx.end();
719 break;
720
721 case SINGLE_SUBNET:
722 lower = idx.lower_bound(getFirstSubnetID());
723 upper = idx.upper_bound(getFirstSubnetID());
724 break;
725
726 case SUBNET_RANGE:
727 lower = idx.lower_bound(getFirstSubnetID());
728 upper = idx.upper_bound(getLastSubnetID());
729 break;
730
731 default:
732 return;
733 }
734
735 // Return an empty set if there are no rows.
736 if (lower == upper) {
737 return;
738 }
739
740 // Iterate over the leases in order by subnet, accumulating per
741 // subnet counts for each state of interest. As we finish each
742 // subnet, add the appropriate rows to our result set.
743 SubnetID cur_id = 0;
744 int64_t assigned = 0;
745 int64_t declined = 0;
746 int64_t assigned_pds = 0;
747 int64_t registered = 0;
748 for (Lease6StorageSubnetIdIndex::const_iterator lease = lower;
749 lease != upper; ++lease) {
750 // If we've hit the next subnet, add rows for the current subnet
751 // and wipe the accumulators
752 if ((*lease)->subnet_id_ != cur_id) {
753 if (cur_id > 0) {
754 if (assigned > 0) {
755 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
757 assigned));
758 assigned = 0;
759 }
760
761 if (declined > 0) {
762 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
764 declined));
765 declined = 0;
766 }
767
768 if (assigned_pds > 0) {
769 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
771 assigned_pds));
772 assigned_pds = 0;
773 }
774
775 if (registered > 0) {
776 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
778 registered));
779 registered = 0;
780 }
781 }
782
783 // Update current subnet id
784 cur_id = (*lease)->subnet_id_;
785 }
786
787 // Bump the appropriate accumulator
788 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
789 switch((*lease)->type_) {
790 case Lease::TYPE_NA:
791 ++assigned;
792 break;
793 case Lease::TYPE_PD:
794 ++assigned_pds;
795 break;
796 default:
797 break;
798 }
799 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
800 // In theory only NAs can be declined
801 if (((*lease)->type_) == Lease::TYPE_NA) {
802 ++declined;
803 }
804 } else if ((*lease)->state_ == Lease::STATE_REGISTERED) {
805 // In theory only NAs can be registered
806 if (((*lease)->type_) == Lease::TYPE_NA) {
807 ++registered;
808 }
809 }
810 }
811
812 // Make the rows for last subnet, unless there were no rows
813 if (assigned > 0) {
814 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
815 Lease::STATE_DEFAULT, assigned));
816 }
817
818 if (declined > 0) {
819 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
820 Lease::STATE_DECLINED, declined));
821 }
822
823 if (assigned_pds > 0) {
824 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
825 Lease::STATE_DEFAULT, assigned_pds));
826 }
827
828 if (registered > 0) {
829 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
830 Lease::STATE_REGISTERED, registered));
831 }
832
833 // Set the next row position to the beginning of the rows.
834 next_pos_ = rows_.begin();
835 }
836
851 virtual void startSubnetPools() {
853 = storage6_.get<SubnetIdPoolIdIndexTag>();
854
855 // Set lower and upper bounds based on select mode
856 Lease6StorageSubnetIdPoolIdIndex::const_iterator lower;
857 Lease6StorageSubnetIdPoolIdIndex::const_iterator upper;
858 switch (getSelectMode()) {
859 case ALL_SUBNET_POOLS:
860 lower = idx.begin();
861 upper = idx.end();
862 break;
863
864 default:
865 return;
866 }
867
868 // Return an empty set if there are no rows.
869 if (lower == upper) {
870 return;
871 }
872
873 // Iterate over the leases in order by subnet, accumulating per
874 // subnet counts for each state of interest. As we finish each
875 // subnet, add the appropriate rows to our result set.
876 SubnetID cur_id = 0;
877 uint32_t cur_pool_id = 0;
878 int64_t assigned = 0;
879 int64_t declined = 0;
880 int64_t assigned_pds = 0;
881 for (Lease6StorageSubnetIdPoolIdIndex::const_iterator lease = lower;
882 lease != upper; ++lease) {
883 // If we've hit the next pool, add rows for the current subnet and
884 // pool and wipe the accumulators
885 if ((*lease)->pool_id_ != cur_pool_id) {
886 if (assigned > 0) {
887 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
889 assigned, cur_pool_id));
890 assigned = 0;
891 }
892
893 if (declined > 0) {
894 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
896 declined, cur_pool_id));
897 declined = 0;
898 }
899
900 if (assigned_pds > 0) {
901 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
903 assigned_pds, cur_pool_id));
904 assigned_pds = 0;
905 }
906
907 // Update current pool id
908 cur_pool_id = (*lease)->pool_id_;
909 }
910
911 // If we've hit the next subnet, add rows for the current subnet
912 // and wipe the accumulators
913 if ((*lease)->subnet_id_ != cur_id) {
914 if (cur_id > 0) {
915 if (assigned > 0) {
916 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
918 assigned, cur_pool_id));
919 assigned = 0;
920 }
921
922 if (declined > 0) {
923 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
925 declined, cur_pool_id));
926 declined = 0;
927 }
928
929 if (assigned_pds > 0) {
930 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
932 assigned_pds, cur_pool_id));
933 assigned_pds = 0;
934 }
935 }
936
937 // Update current subnet id
938 cur_id = (*lease)->subnet_id_;
939
940 // Reset pool id
941 cur_pool_id = 0;
942 }
943
944 // Bump the appropriate accumulator
945 if ((*lease)->state_ == Lease::STATE_DEFAULT) {
946 switch((*lease)->type_) {
947 case Lease::TYPE_NA:
948 ++assigned;
949 break;
950 case Lease::TYPE_PD:
951 ++assigned_pds;
952 break;
953 default:
954 break;
955 }
956 } else if ((*lease)->state_ == Lease::STATE_DECLINED) {
957 // In theory only NAs can be declined
958 if (((*lease)->type_) == Lease::TYPE_NA) {
959 ++declined;
960 }
961 }
962 }
963
964 // Make the rows for last subnet, unless there were no rows
965 if (assigned > 0) {
966 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
967 Lease::STATE_DEFAULT, assigned,
968 cur_pool_id));
969 }
970
971 if (declined > 0) {
972 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_NA,
973 Lease::STATE_DECLINED, declined,
974 cur_pool_id));
975 }
976
977 if (assigned_pds > 0) {
978 rows_.push_back(LeaseStatsRow(cur_id, Lease::TYPE_PD,
979 Lease::STATE_DEFAULT, assigned_pds,
980 cur_pool_id));
981 }
982
983 // Set the next row position to the beginning of the rows.
984 next_pos_ = rows_.begin();
985 }
986
988 Lease6Storage& storage6_;
989};
990
991// Explicit definition of class static constants. Values are given in the
992// declaration so they're not needed here.
997
999 : TrackingLeaseMgr(), lfc_setup_(), conn_(parameters), mutex_(new std::mutex) {
1000 bool conversion_needed = false;
1001
1002 // Check if the extended info tables are enabled.
1003 setExtendedInfoTablesEnabled(parameters);
1004
1005 // Check the universe and use v4 file or v6 file.
1006 std::string universe = conn_.getParameter("universe");
1007 if (universe == "4") {
1008 std::string file4 = initLeaseFilePath(V4);
1009 if (!file4.empty()) {
1010 conversion_needed = loadLeasesFromFiles<Lease4,
1011 CSVLeaseFile4>(file4,
1013 storage4_);
1014 static_cast<void>(extractExtendedInfo4(false, false));
1015 }
1016 } else {
1017 std::string file6 = initLeaseFilePath(V6);
1018 if (!file6.empty()) {
1019 conversion_needed = loadLeasesFromFiles<Lease6,
1020 CSVLeaseFile6>(file6,
1022 storage6_);
1024 }
1025 }
1026
1027 // If lease persistence have been disabled for both v4 and v6,
1028 // issue a warning. It is ok not to write leases to disk when
1029 // doing testing, but it should not be done in normal server
1030 // operation.
1031 if (!persistLeases(V4) && !persistLeases(V6)) {
1033 } else {
1034 if (conversion_needed) {
1035 auto const& version(getVersion());
1037 .arg(version.first).arg(version.second);
1038 }
1039 lfcSetup(conversion_needed);
1040 }
1041}
1042
1044 if (lease_file4_) {
1045 lease_file4_->close();
1046 lease_file4_.reset();
1047 }
1048 if (lease_file6_) {
1049 lease_file6_->close();
1050 lease_file6_.reset();
1051 }
1052}
1053
1054std::string
1056 std::stringstream tmp;
1057 tmp << "Memfile backend ";
1058 if (u == V4) {
1059 tmp << MAJOR_VERSION_V4 << "." << MINOR_VERSION_V4;
1060 } else if (u == V6) {
1061 tmp << MAJOR_VERSION_V6 << "." << MINOR_VERSION_V6;
1062 }
1063 return tmp.str();
1064}
1065
1066std::string
1068 uint16_t family = CfgMgr::instance().getFamily();
1069 if (family == AF_INET6) {
1071 } else {
1073 }
1074}
1075
1076bool
1077Memfile_LeaseMgr::addLeaseInternal(const Lease4Ptr& lease) {
1078 if (getLease4Internal(lease->addr_)) {
1079 // there is a lease with specified address already
1080 return (false);
1081 }
1082
1083 // Try to write a lease to disk first. If this fails, the lease will
1084 // not be inserted to the memory and the disk and in-memory data will
1085 // remain consistent.
1086 if (persistLeases(V4)) {
1087 lease_file4_->append(*lease);
1088 }
1089
1090 storage4_.insert(lease);
1091
1092 // Update lease current expiration time (allows update between the creation
1093 // of the Lease up to the point of insertion in the database).
1094 lease->updateCurrentExpirationTime();
1095
1096 // Increment class lease counters.
1097 class_lease_counter_.addLease(lease);
1098
1099 // Run installed callbacks.
1100 if (hasCallbacks()) {
1101 trackAddLease(lease);
1102 }
1103
1104 return (true);
1105}
1106
1107bool
1110 DHCPSRV_MEMFILE_ADD_ADDR4).arg(lease->addr_.toText());
1111
1112 if (MultiThreadingMgr::instance().getMode()) {
1113 std::lock_guard<std::mutex> lock(*mutex_);
1114 return (addLeaseInternal(lease));
1115 } else {
1116 return (addLeaseInternal(lease));
1117 }
1118}
1119
1120bool
1121Memfile_LeaseMgr::addLeaseInternal(const Lease6Ptr& lease) {
1122 if (getLease6Internal(lease->type_, lease->addr_)) {
1123 // there is a lease with specified address already
1124 return (false);
1125 }
1126
1127 // Try to write a lease to disk first. If this fails, the lease will
1128 // not be inserted to the memory and the disk and in-memory data will
1129 // remain consistent.
1130 if (persistLeases(V6)) {
1131 lease_file6_->append(*lease);
1132 }
1133
1134 lease->extended_info_action_ = Lease6::ACTION_IGNORE;
1135 storage6_.insert(lease);
1136
1137 // Update lease current expiration time (allows update between the creation
1138 // of the Lease up to the point of insertion in the database).
1139 lease->updateCurrentExpirationTime();
1140
1141 // Increment class lease counters.
1142 class_lease_counter_.addLease(lease);
1143
1145 static_cast<void>(addExtendedInfo6(lease));
1146 }
1147
1148 // Run installed callbacks.
1149 if (hasCallbacks()) {
1150 trackAddLease(lease);
1151 }
1152
1153 return (true);
1154}
1155
1156bool
1159 DHCPSRV_MEMFILE_ADD_ADDR6).arg(lease->addr_.toText());
1160
1161 if (MultiThreadingMgr::instance().getMode()) {
1162 std::lock_guard<std::mutex> lock(*mutex_);
1163 return (addLeaseInternal(lease));
1164 } else {
1165 return (addLeaseInternal(lease));
1166 }
1167}
1168
1170Memfile_LeaseMgr::getLease4Internal(const isc::asiolink::IOAddress& addr) const {
1171 const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
1172 Lease4StorageAddressIndex::iterator l = idx.find(addr);
1173 if (l == idx.end()) {
1174 return (Lease4Ptr());
1175 } else {
1176 return (Lease4Ptr(new Lease4(**l)));
1177 }
1178}
1179
1183 DHCPSRV_MEMFILE_GET_ADDR4).arg(addr.toText());
1184
1185 if (MultiThreadingMgr::instance().getMode()) {
1186 std::lock_guard<std::mutex> lock(*mutex_);
1187 return (getLease4Internal(addr));
1188 } else {
1189 return (getLease4Internal(addr));
1190 }
1191}
1192
1193void
1194Memfile_LeaseMgr::getLease4Internal(const HWAddr& hwaddr,
1195 Lease4Collection& collection) const {
1196 // Using composite index by 'hw address' and 'subnet id'. It is
1197 // ok to use it for searching by the 'hw address' only.
1199 storage4_.get<HWAddressSubnetIdIndexTag>();
1200 std::pair<Lease4StorageHWAddressSubnetIdIndex::const_iterator,
1201 Lease4StorageHWAddressSubnetIdIndex::const_iterator> l
1202 = idx.equal_range(boost::make_tuple(hwaddr.hwaddr_));
1203
1204 BOOST_FOREACH(auto const& lease, l) {
1205 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1206 }
1207}
1208
1212 DHCPSRV_MEMFILE_GET_HWADDR).arg(hwaddr.toText());
1213
1214 Lease4Collection collection;
1215 if (MultiThreadingMgr::instance().getMode()) {
1216 std::lock_guard<std::mutex> lock(*mutex_);
1217 getLease4Internal(hwaddr, collection);
1218 } else {
1219 getLease4Internal(hwaddr, collection);
1220 }
1221
1222 return (collection);
1223}
1224
1226Memfile_LeaseMgr::getLease4Internal(const HWAddr& hwaddr,
1227 SubnetID subnet_id) const {
1228 // Get the index by HW Address and Subnet Identifier.
1230 storage4_.get<HWAddressSubnetIdIndexTag>();
1231 // Try to find the lease using HWAddr and subnet id.
1232 Lease4StorageHWAddressSubnetIdIndex::const_iterator lease =
1233 idx.find(boost::make_tuple(hwaddr.hwaddr_, subnet_id));
1234 // Lease was not found. Return empty pointer to the caller.
1235 if (lease == idx.end()) {
1236 return (Lease4Ptr());
1237 }
1238
1239 // Lease was found. Return it to the caller.
1240 return (Lease4Ptr(new Lease4(**lease)));
1241}
1242
1245 SubnetID subnet_id) const {
1247 DHCPSRV_MEMFILE_GET_SUBID_HWADDR).arg(subnet_id)
1248 .arg(hwaddr.toText());
1249
1250 if (MultiThreadingMgr::instance().getMode()) {
1251 std::lock_guard<std::mutex> lock(*mutex_);
1252 return (getLease4Internal(hwaddr, subnet_id));
1253 } else {
1254 return (getLease4Internal(hwaddr, subnet_id));
1255 }
1256}
1257
1258void
1259Memfile_LeaseMgr::getLease4Internal(const ClientId& client_id,
1260 Lease4Collection& collection) const {
1261 // Using composite index by 'client id' and 'subnet id'. It is ok
1262 // to use it to search by 'client id' only.
1264 storage4_.get<ClientIdSubnetIdIndexTag>();
1265 std::pair<Lease4StorageClientIdSubnetIdIndex::const_iterator,
1266 Lease4StorageClientIdSubnetIdIndex::const_iterator> l
1267 = idx.equal_range(boost::make_tuple(client_id.getClientId()));
1268
1269 BOOST_FOREACH(auto const& lease, l) {
1270 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1271 }
1272}
1273
1275Memfile_LeaseMgr::getLease4(const ClientId& client_id) const {
1277 DHCPSRV_MEMFILE_GET_CLIENTID).arg(client_id.toText());
1278
1279 Lease4Collection collection;
1280 if (MultiThreadingMgr::instance().getMode()) {
1281 std::lock_guard<std::mutex> lock(*mutex_);
1282 getLease4Internal(client_id, collection);
1283 } else {
1284 getLease4Internal(client_id, collection);
1285 }
1286
1287 return (collection);
1288}
1289
1291Memfile_LeaseMgr::getLease4Internal(const ClientId& client_id,
1292 SubnetID subnet_id) const {
1293 // Get the index by client and subnet id.
1295 storage4_.get<ClientIdSubnetIdIndexTag>();
1296 // Try to get the lease using client id and subnet id.
1297 Lease4StorageClientIdSubnetIdIndex::const_iterator lease =
1298 idx.find(boost::make_tuple(client_id.getClientId(), subnet_id));
1299 // Lease was not found. Return empty pointer to the caller.
1300 if (lease == idx.end()) {
1301 return (Lease4Ptr());
1302 }
1303 // Lease was found. Return it to the caller.
1304 return (Lease4Ptr(new Lease4(**lease)));
1305}
1306
1309 SubnetID subnet_id) const {
1312 .arg(client_id.toText());
1313
1314 if (MultiThreadingMgr::instance().getMode()) {
1315 std::lock_guard<std::mutex> lock(*mutex_);
1316 return (getLease4Internal(client_id, subnet_id));
1317 } else {
1318 return (getLease4Internal(client_id, subnet_id));
1319 }
1320}
1321
1322void
1323Memfile_LeaseMgr::getLeases4Internal(SubnetID subnet_id,
1324 Lease4Collection& collection) const {
1325 const Lease4StorageSubnetIdIndex& idx = storage4_.get<SubnetIdIndexTag>();
1326 std::pair<Lease4StorageSubnetIdIndex::const_iterator,
1327 Lease4StorageSubnetIdIndex::const_iterator> l =
1328 idx.equal_range(subnet_id);
1329
1330 BOOST_FOREACH(auto const& lease, l) {
1331 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1332 }
1333}
1334
1338 .arg(subnet_id);
1339
1340 Lease4Collection collection;
1341 if (MultiThreadingMgr::instance().getMode()) {
1342 std::lock_guard<std::mutex> lock(*mutex_);
1343 getLeases4Internal(subnet_id, collection);
1344 } else {
1345 getLeases4Internal(subnet_id, collection);
1346 }
1347
1348 return (collection);
1349}
1350
1351void
1352Memfile_LeaseMgr::getLeases4Internal(const std::string& hostname,
1353 Lease4Collection& collection) const {
1354 const Lease4StorageHostnameIndex& idx = storage4_.get<HostnameIndexTag>();
1355 std::pair<Lease4StorageHostnameIndex::const_iterator,
1356 Lease4StorageHostnameIndex::const_iterator> l =
1357 idx.equal_range(hostname);
1358
1359 BOOST_FOREACH(auto const& lease, l) {
1360 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1361 }
1362}
1363
1365Memfile_LeaseMgr::getLeases4(const std::string& hostname) const {
1367 .arg(hostname);
1368
1369 Lease4Collection collection;
1370 if (MultiThreadingMgr::instance().getMode()) {
1371 std::lock_guard<std::mutex> lock(*mutex_);
1372 getLeases4Internal(hostname, collection);
1373 } else {
1374 getLeases4Internal(hostname, collection);
1375 }
1376
1377 return (collection);
1378}
1379
1380void
1381Memfile_LeaseMgr::getLeases4Internal(Lease4Collection& collection) const {
1382 for (auto const& lease : storage4_) {
1383 collection.push_back(Lease4Ptr(new Lease4(*lease)));
1384 }
1385}
1386
1390
1391 Lease4Collection collection;
1392 if (MultiThreadingMgr::instance().getMode()) {
1393 std::lock_guard<std::mutex> lock(*mutex_);
1394 getLeases4Internal(collection);
1395 } else {
1396 getLeases4Internal(collection);
1397 }
1398
1399 return (collection);
1400}
1401
1402void
1403Memfile_LeaseMgr::getLeases4Internal(const asiolink::IOAddress& lower_bound_address,
1404 const LeasePageSize& page_size,
1405 Lease4Collection& collection) const {
1406 const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
1407 Lease4StorageAddressIndex::const_iterator lb = idx.lower_bound(lower_bound_address);
1408
1409 // Exclude the lower bound address specified by the caller.
1410 if ((lb != idx.end()) && ((*lb)->addr_ == lower_bound_address)) {
1411 ++lb;
1412 }
1413
1414 // Return all other leases being within the page size.
1415 for (auto lease = lb;
1416 (lease != idx.end()) &&
1417 (static_cast<size_t>(std::distance(lb, lease)) < page_size.page_size_);
1418 ++lease) {
1419 collection.push_back(Lease4Ptr(new Lease4(**lease)));
1420 }
1421}
1422
1425 const LeasePageSize& page_size) const {
1426 // Expecting IPv4 address.
1427 if (!lower_bound_address.isV4()) {
1428 isc_throw(InvalidAddressFamily, "expected IPv4 address while "
1429 "retrieving leases from the lease database, got "
1430 << lower_bound_address);
1431 }
1432
1434 .arg(page_size.page_size_)
1435 .arg(lower_bound_address.toText());
1436
1437 Lease4Collection collection;
1438 if (MultiThreadingMgr::instance().getMode()) {
1439 std::lock_guard<std::mutex> lock(*mutex_);
1440 getLeases4Internal(lower_bound_address, page_size, collection);
1441 } else {
1442 getLeases4Internal(lower_bound_address, page_size, collection);
1443 }
1444
1445 return (collection);
1446}
1447
1449Memfile_LeaseMgr::getLease6Internal(Lease::Type type,
1450 const isc::asiolink::IOAddress& addr) const {
1451 Lease6Storage::iterator l = storage6_.find(addr);
1452 if (l == storage6_.end() || !(*l) || ((*l)->type_ != type)) {
1453 return (Lease6Ptr());
1454 } else {
1455 return (Lease6Ptr(new Lease6(**l)));
1456 }
1457}
1458
1460Memfile_LeaseMgr::getAnyLease6Internal(const isc::asiolink::IOAddress& addr) const {
1461 Lease6Storage::iterator l = storage6_.find(addr);
1462 if (l == storage6_.end() || !(*l)) {
1463 return (Lease6Ptr());
1464 } else {
1465 return (Lease6Ptr(new Lease6(**l)));
1466 }
1467}
1468
1471 const isc::asiolink::IOAddress& addr) const {
1474 .arg(addr.toText())
1475 .arg(Lease::typeToText(type));
1476
1477 if (MultiThreadingMgr::instance().getMode()) {
1478 std::lock_guard<std::mutex> lock(*mutex_);
1479 return (getLease6Internal(type, addr));
1480 } else {
1481 return (getLease6Internal(type, addr));
1482 }
1483}
1484
1485void
1486Memfile_LeaseMgr::getLeases6Internal(Lease::Type type,
1487 const DUID& duid,
1488 uint32_t iaid,
1489 Lease6Collection& collection) const {
1490 // Get the index by DUID, IAID, lease type.
1491 const Lease6StorageDuidIaidTypeIndex& idx = storage6_.get<DuidIaidTypeIndexTag>();
1492 // Try to get the lease using the DUID, IAID and lease type.
1493 std::pair<Lease6StorageDuidIaidTypeIndex::const_iterator,
1494 Lease6StorageDuidIaidTypeIndex::const_iterator> l =
1495 idx.equal_range(boost::make_tuple(duid.getDuid(), iaid, type));
1496
1497 for (Lease6StorageDuidIaidTypeIndex::const_iterator lease =
1498 l.first; lease != l.second; ++lease) {
1499 collection.push_back(Lease6Ptr(new Lease6(**lease)));
1500 }
1501}
1502
1505 const DUID& duid,
1506 uint32_t iaid) const {
1509 .arg(iaid)
1510 .arg(duid.toText())
1511 .arg(Lease::typeToText(type));
1512
1513 Lease6Collection collection;
1514 if (MultiThreadingMgr::instance().getMode()) {
1515 std::lock_guard<std::mutex> lock(*mutex_);
1516 getLeases6Internal(type, duid, iaid, collection);
1517 } else {
1518 getLeases6Internal(type, duid, iaid, collection);
1519 }
1520
1521 return (collection);
1522}
1523
1524void
1525Memfile_LeaseMgr::getLeases6Internal(Lease::Type type,
1526 const DUID& duid,
1527 uint32_t iaid,
1528 SubnetID subnet_id,
1529 Lease6Collection& collection) const {
1530 // Get the index by DUID, IAID, lease type.
1531 const Lease6StorageDuidIaidTypeIndex& idx = storage6_.get<DuidIaidTypeIndexTag>();
1532 // Try to get the lease using the DUID, IAID and lease type.
1533 std::pair<Lease6StorageDuidIaidTypeIndex::const_iterator,
1534 Lease6StorageDuidIaidTypeIndex::const_iterator> l =
1535 idx.equal_range(boost::make_tuple(duid.getDuid(), iaid, type));
1536
1537 for (Lease6StorageDuidIaidTypeIndex::const_iterator lease =
1538 l.first; lease != l.second; ++lease) {
1539 // Filter out the leases which subnet id doesn't match.
1540 if ((*lease)->subnet_id_ == subnet_id) {
1541 collection.push_back(Lease6Ptr(new Lease6(**lease)));
1542 }
1543 }
1544}
1545
1548 const DUID& duid,
1549 uint32_t iaid,
1550 SubnetID subnet_id) const {
1553 .arg(iaid)
1554 .arg(subnet_id)
1555 .arg(duid.toText())
1556 .arg(Lease::typeToText(type));
1557
1558 Lease6Collection collection;
1559 if (MultiThreadingMgr::instance().getMode()) {
1560 std::lock_guard<std::mutex> lock(*mutex_);
1561 getLeases6Internal(type, duid, iaid, subnet_id, collection);
1562 } else {
1563 getLeases6Internal(type, duid, iaid, subnet_id, collection);
1564 }
1565
1566 return (collection);
1567}
1568
1569void
1570Memfile_LeaseMgr::getLeases6Internal(SubnetID subnet_id,
1571 Lease6Collection& collection) const {
1572 const Lease6StorageSubnetIdIndex& idx = storage6_.get<SubnetIdIndexTag>();
1573 std::pair<Lease6StorageSubnetIdIndex::const_iterator,
1574 Lease6StorageSubnetIdIndex::const_iterator> l =
1575 idx.equal_range(subnet_id);
1576
1577 BOOST_FOREACH(auto const& lease, l) {
1578 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1579 }
1580}
1581
1585 .arg(subnet_id);
1586
1587 Lease6Collection collection;
1588 if (MultiThreadingMgr::instance().getMode()) {
1589 std::lock_guard<std::mutex> lock(*mutex_);
1590 getLeases6Internal(subnet_id, collection);
1591 } else {
1592 getLeases6Internal(subnet_id, collection);
1593 }
1594
1595 return (collection);
1596}
1597
1598void
1599Memfile_LeaseMgr::getLeases6Internal(const std::string& hostname,
1600 Lease6Collection& collection) const {
1601 const Lease6StorageHostnameIndex& idx = storage6_.get<HostnameIndexTag>();
1602 std::pair<Lease6StorageHostnameIndex::const_iterator,
1603 Lease6StorageHostnameIndex::const_iterator> l =
1604 idx.equal_range(hostname);
1605
1606 BOOST_FOREACH(auto const& lease, l) {
1607 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1608 }
1609}
1610
1612Memfile_LeaseMgr::getLeases6(const std::string& hostname) const {
1614 .arg(hostname);
1615
1616 Lease6Collection collection;
1617 if (MultiThreadingMgr::instance().getMode()) {
1618 std::lock_guard<std::mutex> lock(*mutex_);
1619 getLeases6Internal(hostname, collection);
1620 } else {
1621 getLeases6Internal(hostname, collection);
1622 }
1623
1624 return (collection);
1625}
1626
1627void
1628Memfile_LeaseMgr::getLeases6Internal(Lease6Collection& collection) const {
1629 for (auto const& lease : storage6_) {
1630 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1631 }
1632}
1633
1637
1638 Lease6Collection collection;
1639 if (MultiThreadingMgr::instance().getMode()) {
1640 std::lock_guard<std::mutex> lock(*mutex_);
1641 getLeases6Internal(collection);
1642 } else {
1643 getLeases6Internal(collection);
1644 }
1645
1646 return (collection);
1647}
1648
1649void
1650Memfile_LeaseMgr::getLeases6Internal(const DUID& duid,
1651 Lease6Collection& collection) const {
1652 const Lease6StorageDuidIndex& idx = storage6_.get<DuidIndexTag>();
1653 std::pair<Lease6StorageDuidIndex::const_iterator,
1654 Lease6StorageDuidIndex::const_iterator> l =
1655 idx.equal_range(duid.getDuid());
1656
1657 BOOST_FOREACH(auto const& lease, l) {
1658 collection.push_back(Lease6Ptr(new Lease6(*lease)));
1659 }
1660}
1661
1665 .arg(duid.toText());
1666
1667 Lease6Collection collection;
1668 if (MultiThreadingMgr::instance().getMode()) {
1669 std::lock_guard<std::mutex> lock(*mutex_);
1670 getLeases6Internal(duid, collection);
1671 } else {
1672 getLeases6Internal(duid, collection);
1673 }
1674
1675 return (collection);
1676}
1677
1678void
1679Memfile_LeaseMgr::getLeases6Internal(const asiolink::IOAddress& lower_bound_address,
1680 const LeasePageSize& page_size,
1681 Lease6Collection& collection) const {
1682 const Lease6StorageAddressIndex& idx = storage6_.get<AddressIndexTag>();
1683 Lease6StorageAddressIndex::const_iterator lb = idx.lower_bound(lower_bound_address);
1684
1685 // Exclude the lower bound address specified by the caller.
1686 if ((lb != idx.end()) && ((*lb)->addr_ == lower_bound_address)) {
1687 ++lb;
1688 }
1689
1690 // Return all other leases being within the page size.
1691 for (auto lease = lb;
1692 (lease != idx.end()) &&
1693 (static_cast<size_t>(std::distance(lb, lease)) < page_size.page_size_);
1694 ++lease) {
1695 collection.push_back(Lease6Ptr(new Lease6(**lease)));
1696 }
1697}
1698
1701 const LeasePageSize& page_size) const {
1702 // Expecting IPv6 address.
1703 if (!lower_bound_address.isV6()) {
1704 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
1705 "retrieving leases from the lease database, got "
1706 << lower_bound_address);
1707 }
1708
1710 .arg(page_size.page_size_)
1711 .arg(lower_bound_address.toText());
1712
1713 Lease6Collection collection;
1714 if (MultiThreadingMgr::instance().getMode()) {
1715 std::lock_guard<std::mutex> lock(*mutex_);
1716 getLeases6Internal(lower_bound_address, page_size, collection);
1717 } else {
1718 getLeases6Internal(lower_bound_address, page_size, collection);
1719 }
1720
1721 return (collection);
1722}
1723
1725Memfile_LeaseMgr::getLeases6Internal(SubnetID subnet_id,
1726 const IOAddress& lower_bound_address,
1727 const LeasePageSize& page_size) const {
1728 Lease6Collection collection;
1729 const Lease6StorageSubnetIdIndex& idx = storage6_.get<SubnetIdIndexTag>();
1730 Lease6StorageSubnetIdIndex::const_iterator lb =
1731 idx.lower_bound(boost::make_tuple(subnet_id, lower_bound_address));
1732
1733 // Exclude the lower bound address specified by the caller.
1734 if ((lb != idx.end()) && ((*lb)->addr_ == lower_bound_address)) {
1735 ++lb;
1736 }
1737
1738 // Return all leases being within the page size.
1739 for (auto it = lb; it != idx.end(); ++it) {
1740 if ((*it)->subnet_id_ != subnet_id) {
1741 // Gone after the subnet id index.
1742 break;
1743 }
1744 collection.push_back(Lease6Ptr(new Lease6(**it)));
1745 if (collection.size() >= page_size.page_size_) {
1746 break;
1747 }
1748 }
1749 return (collection);
1750}
1751
1754 const IOAddress& lower_bound_address,
1755 const LeasePageSize& page_size) const {
1758 .arg(page_size.page_size_)
1759 .arg(lower_bound_address.toText())
1760 .arg(subnet_id);
1761
1762 // Expecting IPv6 valid address.
1763 if (!lower_bound_address.isV6()) {
1764 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
1765 "retrieving leases from the lease database, got "
1766 << lower_bound_address);
1767 }
1768
1769 if (MultiThreadingMgr::instance().getMode()) {
1770 std::lock_guard<std::mutex> lock(*mutex_);
1771 return (getLeases6Internal(subnet_id,
1772 lower_bound_address,
1773 page_size));
1774 } else {
1775 return (getLeases6Internal(subnet_id,
1776 lower_bound_address,
1777 page_size));
1778 }
1779}
1780
1781void
1782Memfile_LeaseMgr::getExpiredLeases4Internal(Lease4Collection& expired_leases,
1783 const size_t max_leases) const {
1784 // Obtain the index which segragates leases by state and time.
1785 const Lease4StorageExpirationIndex& index = storage4_.get<ExpirationIndexTag>();
1786
1787 // Retrieve leases which are not reclaimed and which haven't expired. The
1788 // 'less-than' operator will be used for both components of the index. So,
1789 // for the 'state' 'false' is less than 'true'. Also the leases with
1790 // expiration time lower than current time will be returned.
1791 Lease4StorageExpirationIndex::const_iterator ub =
1792 index.upper_bound(boost::make_tuple(false, time(0)));
1793
1794 // Copy only the number of leases indicated by the max_leases parameter.
1795 for (Lease4StorageExpirationIndex::const_iterator lease = index.begin();
1796 (lease != ub) &&
1797 ((max_leases == 0) ||
1798 (static_cast<size_t>(std::distance(index.begin(), lease)) < max_leases));
1799 ++lease) {
1800 expired_leases.push_back(Lease4Ptr(new Lease4(**lease)));
1801 }
1802}
1803
1804void
1806 const size_t max_leases) const {
1808 .arg(max_leases);
1809
1810 if (MultiThreadingMgr::instance().getMode()) {
1811 std::lock_guard<std::mutex> lock(*mutex_);
1812 getExpiredLeases4Internal(expired_leases, max_leases);
1813 } else {
1814 getExpiredLeases4Internal(expired_leases, max_leases);
1815 }
1816}
1817
1818void
1819Memfile_LeaseMgr::getExpiredLeases6Internal(Lease6Collection& expired_leases,
1820 const size_t max_leases) const {
1821 // Obtain the index which segragates leases by state and time.
1822 const Lease6StorageExpirationIndex& index = storage6_.get<ExpirationIndexTag>();
1823
1824 // Retrieve leases which are not reclaimed and which haven't expired. The
1825 // 'less-than' operator will be used for both components of the index. So,
1826 // for the 'state' 'false' is less than 'true'. Also the leases with
1827 // expiration time lower than current time will be returned.
1828 Lease6StorageExpirationIndex::const_iterator ub =
1829 index.upper_bound(boost::make_tuple(false, time(0)));
1830
1831 // Copy only the number of leases indicated by the max_leases parameter.
1832 for (Lease6StorageExpirationIndex::const_iterator lease = index.begin();
1833 (lease != ub) &&
1834 ((max_leases == 0) ||
1835 (static_cast<size_t>(std::distance(index.begin(), lease)) < max_leases));
1836 ++lease) {
1837 expired_leases.push_back(Lease6Ptr(new Lease6(**lease)));
1838 }
1839}
1840
1841void
1843 const size_t max_leases) const {
1845 .arg(max_leases);
1846
1847 if (MultiThreadingMgr::instance().getMode()) {
1848 std::lock_guard<std::mutex> lock(*mutex_);
1849 getExpiredLeases6Internal(expired_leases, max_leases);
1850 } else {
1851 getExpiredLeases6Internal(expired_leases, max_leases);
1852 }
1853}
1854
1855void
1856Memfile_LeaseMgr::updateLease4Internal(const Lease4Ptr& lease) {
1857 // Obtain 'by address' index.
1858 Lease4StorageAddressIndex& index = storage4_.get<AddressIndexTag>();
1859
1860 bool persist = persistLeases(V4);
1861
1862 // Lease must exist if it is to be updated.
1863 Lease4StorageAddressIndex::const_iterator lease_it = index.find(lease->addr_);
1864 if (lease_it == index.end()) {
1865 isc_throw(NoSuchLease, "failed to update the lease with address "
1866 << lease->addr_ << " - no such lease");
1867 } else if ((!persist) && (((*lease_it)->cltt_ != lease->current_cltt_) ||
1868 ((*lease_it)->valid_lft_ != lease->current_valid_lft_))) {
1869 // For test purpose only: check that the lease has not changed in
1870 // the database.
1871 isc_throw(NoSuchLease, "unable to update lease for address " <<
1872 lease->addr_.toText() << " either because the lease does not exist, "
1873 "it has been deleted or it has changed in the database.");
1874 }
1875
1876 // Try to write a lease to disk first. If this fails, the lease will
1877 // not be inserted to the memory and the disk and in-memory data will
1878 // remain consistent.
1879 if (persist) {
1880 lease_file4_->append(*lease);
1881 }
1882
1883 // Update lease current expiration time.
1884 lease->updateCurrentExpirationTime();
1885
1886 // Save a copy of the old lease as lease_it will point to the new
1887 // one after the replacement.
1888 Lease4Ptr old_lease = *lease_it;
1889
1890 // Use replace() to re-index leases.
1891 index.replace(lease_it, Lease4Ptr(new Lease4(*lease)));
1892
1893 // Adjust class lease counters.
1894 class_lease_counter_.updateLease(lease, old_lease);
1895
1896 // Run installed callbacks.
1897 if (hasCallbacks()) {
1898 trackUpdateLease(lease);
1899 }
1900}
1901
1902void
1905 DHCPSRV_MEMFILE_UPDATE_ADDR4).arg(lease->addr_.toText());
1906
1907 if (MultiThreadingMgr::instance().getMode()) {
1908 std::lock_guard<std::mutex> lock(*mutex_);
1909 updateLease4Internal(lease);
1910 } else {
1911 updateLease4Internal(lease);
1912 }
1913}
1914
1915void
1916Memfile_LeaseMgr::updateLease6Internal(const Lease6Ptr& lease) {
1917 // Obtain 'by address' index.
1918 Lease6StorageAddressIndex& index = storage6_.get<AddressIndexTag>();
1919
1920 bool persist = persistLeases(V6);
1921
1922 // Get the recorded action and reset it.
1923 Lease6::ExtendedInfoAction recorded_action = lease->extended_info_action_;
1924 lease->extended_info_action_ = Lease6::ACTION_IGNORE;
1925
1926 // Lease must exist if it is to be updated.
1927 Lease6StorageAddressIndex::const_iterator lease_it = index.find(lease->addr_);
1928 if (lease_it == index.end()) {
1929 isc_throw(NoSuchLease, "failed to update the lease with address "
1930 << lease->addr_ << " - no such lease");
1931 } else if ((!persist) && (((*lease_it)->cltt_ != lease->current_cltt_) ||
1932 ((*lease_it)->valid_lft_ != lease->current_valid_lft_))) {
1933 // For test purpose only: check that the lease has not changed in
1934 // the database.
1935 isc_throw(NoSuchLease, "unable to update lease for address " <<
1936 lease->addr_.toText() << " either because the lease does not exist, "
1937 "it has been deleted or it has changed in the database.");
1938 }
1939
1940 // Try to write a lease to disk first. If this fails, the lease will
1941 // not be inserted to the memory and the disk and in-memory data will
1942 // remain consistent.
1943 if (persist) {
1944 lease_file6_->append(*lease);
1945 }
1946
1947 // Update lease current expiration time.
1948 lease->updateCurrentExpirationTime();
1949
1950 // Save a copy of the old lease as lease_it will point to the new
1951 // one after the replacement.
1952 Lease6Ptr old_lease = *lease_it;
1953
1954 // Use replace() to re-index leases.
1955 index.replace(lease_it, Lease6Ptr(new Lease6(*lease)));
1956
1957 // Adjust class lease counters.
1958 class_lease_counter_.updateLease(lease, old_lease);
1959
1960 // Update extended info tables.
1962 switch (recorded_action) {
1964 break;
1965
1967 deleteExtendedInfo6(lease->addr_);
1968 break;
1969
1971 deleteExtendedInfo6(lease->addr_);
1972 static_cast<void>(addExtendedInfo6(lease));
1973 break;
1974 }
1975 }
1976
1977 // Run installed callbacks.
1978 if (hasCallbacks()) {
1979 trackUpdateLease(lease);
1980 }
1981}
1982
1983void
1986 DHCPSRV_MEMFILE_UPDATE_ADDR6).arg(lease->addr_.toText());
1987
1988 if (MultiThreadingMgr::instance().getMode()) {
1989 std::lock_guard<std::mutex> lock(*mutex_);
1990 updateLease6Internal(lease);
1991 } else {
1992 updateLease6Internal(lease);
1993 }
1994}
1995
1996bool
1997Memfile_LeaseMgr::deleteLeaseInternal(const Lease4Ptr& lease) {
1998 const isc::asiolink::IOAddress& addr = lease->addr_;
1999 Lease4Storage::iterator l = storage4_.find(addr);
2000 if (l == storage4_.end()) {
2001 // No such lease
2002 return (false);
2003 } else {
2004 if (persistLeases(V4)) {
2005 // Copy the lease. The valid lifetime needs to be modified and
2006 // we don't modify the original lease.
2007 Lease4 lease_copy = **l;
2008 // Setting valid lifetime to 0 means that lease is being
2009 // removed.
2010 lease_copy.valid_lft_ = 0;
2011 lease_file4_->append(lease_copy);
2012 } else {
2013 // For test purpose only: check that the lease has not changed in
2014 // the database.
2015 if (((*l)->cltt_ != lease->current_cltt_) ||
2016 ((*l)->valid_lft_ != lease->current_valid_lft_)) {
2017 return false;
2018 }
2019 }
2020
2021 storage4_.erase(l);
2022
2023 // Decrement class lease counters.
2024 class_lease_counter_.removeLease(lease);
2025
2026 // Run installed callbacks.
2027 if (hasCallbacks()) {
2028 trackDeleteLease(lease);
2029 }
2030
2031 return (true);
2032 }
2033}
2034
2035bool
2038 DHCPSRV_MEMFILE_DELETE_ADDR4).arg(lease->addr_.toText());
2039
2040 if (MultiThreadingMgr::instance().getMode()) {
2041 std::lock_guard<std::mutex> lock(*mutex_);
2042 return (deleteLeaseInternal(lease));
2043 } else {
2044 return (deleteLeaseInternal(lease));
2045 }
2046}
2047
2048bool
2049Memfile_LeaseMgr::deleteLeaseInternal(const Lease6Ptr& lease) {
2050 lease->extended_info_action_ = Lease6::ACTION_IGNORE;
2051
2052 const isc::asiolink::IOAddress& addr = lease->addr_;
2053 Lease6Storage::iterator l = storage6_.find(addr);
2054 if (l == storage6_.end()) {
2055 // No such lease
2056 return (false);
2057 } else {
2058 if (persistLeases(V6)) {
2059 // Copy the lease. The lifetimes need to be modified and we
2060 // don't modify the original lease.
2061 Lease6 lease_copy = **l;
2062 // Setting lifetimes to 0 means that lease is being removed.
2063 lease_copy.valid_lft_ = 0;
2064 lease_copy.preferred_lft_ = 0;
2065 lease_file6_->append(lease_copy);
2066 } else {
2067 // For test purpose only: check that the lease has not changed in
2068 // the database.
2069 if (((*l)->cltt_ != lease->current_cltt_) ||
2070 ((*l)->valid_lft_ != lease->current_valid_lft_)) {
2071 return false;
2072 }
2073 }
2074
2075 storage6_.erase(l);
2076
2077 // Decrement class lease counters.
2078 class_lease_counter_.removeLease(lease);
2079
2080 // Delete references from extended info tables.
2082 deleteExtendedInfo6(lease->addr_);
2083 }
2084
2085 // Run installed callbacks.
2086 if (hasCallbacks()) {
2087 trackDeleteLease(lease);
2088 }
2089
2090 return (true);
2091 }
2092}
2093
2094bool
2097 DHCPSRV_MEMFILE_DELETE_ADDR6).arg(lease->addr_.toText());
2098
2099 if (MultiThreadingMgr::instance().getMode()) {
2100 std::lock_guard<std::mutex> lock(*mutex_);
2101 return (deleteLeaseInternal(lease));
2102 } else {
2103 return (deleteLeaseInternal(lease));
2104 }
2105}
2106
2107uint64_t
2111 .arg(secs);
2112
2113 if (MultiThreadingMgr::instance().getMode()) {
2114 std::lock_guard<std::mutex> lock(*mutex_);
2115 return (deleteExpiredReclaimedLeases<
2117 >(secs, V4, storage4_, lease_file4_));
2118 } else {
2119 return (deleteExpiredReclaimedLeases<
2121 >(secs, V4, storage4_, lease_file4_));
2122 }
2123}
2124
2125uint64_t
2129 .arg(secs);
2130
2131 if (MultiThreadingMgr::instance().getMode()) {
2132 std::lock_guard<std::mutex> lock(*mutex_);
2133 return (deleteExpiredReclaimedLeases<
2135 >(secs, V6, storage6_, lease_file6_));
2136 } else {
2137 return (deleteExpiredReclaimedLeases<
2139 >(secs, V6, storage6_, lease_file6_));
2140 }
2141}
2142
2143template<typename IndexType, typename LeaseType, typename StorageType,
2144 typename LeaseFileType>
2145uint64_t
2146Memfile_LeaseMgr::deleteExpiredReclaimedLeases(const uint32_t secs,
2147 const Universe& universe,
2148 StorageType& storage,
2149 LeaseFileType& lease_file) {
2150 // Obtain the index which segragates leases by state and time.
2151 IndexType& index = storage.template get<ExpirationIndexTag>();
2152
2153 // This returns the first element which is greater than the specified
2154 // tuple (true, time(0) - secs). However, the range between the
2155 // beginning of the index and returned element also includes all the
2156 // elements for which the first value is false (lease state is NOT
2157 // reclaimed), because false < true. All elements between the
2158 // beginning of the index and the element returned, for which the
2159 // first value is true, represent the reclaimed leases which should
2160 // be deleted, because their expiration time + secs has occurred earlier
2161 // than current time.
2162 typename IndexType::const_iterator upper_limit =
2163 index.upper_bound(boost::make_tuple(true, time(0) - secs));
2164
2165 // Now, we have to exclude all elements of the index which represent
2166 // leases in the state other than reclaimed - with the first value
2167 // in the index equal to false. Note that elements in the index are
2168 // ordered from the lower to the higher ones. So, all elements with
2169 // the first value of false are placed before the elements with the
2170 // value of true. Hence, we have to find the first element which
2171 // contains value of true. The time value is the lowest possible.
2172 typename IndexType::const_iterator lower_limit =
2173 index.upper_bound(boost::make_tuple(true, std::numeric_limits<int64_t>::min()));
2174
2175 // If there are some elements in this range, delete them.
2176 uint64_t num_leases = static_cast<uint64_t>(std::distance(lower_limit, upper_limit));
2177 if (num_leases > 0) {
2178
2181 .arg(num_leases);
2182
2183 // If lease persistence is enabled, we also have to mark leases
2184 // as deleted in the lease file. We do this by setting the
2185 // lifetime to 0.
2186 if (persistLeases(universe)) {
2187 for (typename IndexType::const_iterator lease = lower_limit;
2188 lease != upper_limit; ++lease) {
2189 // Copy lease to not affect the lease in the container.
2190 LeaseType lease_copy(**lease);
2191 // Set the valid lifetime to 0 to indicate the removal
2192 // of the lease.
2193 lease_copy.valid_lft_ = 0;
2194 lease_file->append(lease_copy);
2195 }
2196 }
2197
2198 // Erase leases from memory.
2199 index.erase(lower_limit, upper_limit);
2200
2201 }
2202 // Return number of leases deleted.
2203 return (num_leases);
2204}
2205
2206std::string
2208 return (std::string("In memory database with leases stored in a CSV file."));
2209}
2210
2211std::pair<uint32_t, uint32_t>
2212Memfile_LeaseMgr::getVersion(const std::string& /* timer_name */) const {
2213 std::string const& universe(conn_.getParameter("universe"));
2214 if (universe == "4") {
2215 return std::make_pair(MAJOR_VERSION_V4, MINOR_VERSION_V4);
2216 } else if (universe == "6") {
2217 return std::make_pair(MAJOR_VERSION_V6, MINOR_VERSION_V6);
2218 }
2219 isc_throw(BadValue, "cannot determine version for universe " << universe);
2220}
2221
2222void
2226
2227void
2232
2233std::string
2234Memfile_LeaseMgr::appendSuffix(const std::string& file_name,
2235 const LFCFileType& file_type) {
2236 std::string name(file_name);
2237 switch (file_type) {
2238 case FILE_INPUT:
2239 name += ".1";
2240 break;
2241 case FILE_PREVIOUS:
2242 name += ".2";
2243 break;
2244 case FILE_OUTPUT:
2245 name += ".output";
2246 break;
2247 case FILE_FINISH:
2248 name += ".completed";
2249 break;
2250 case FILE_PID:
2251 name += ".pid";
2252 break;
2253 default:
2254 // Do not append any suffix for the FILE_CURRENT.
2255 ;
2256 }
2257
2258 return (name);
2259}
2260
2261std::string
2263 std::string filename /* = "" */) const {
2264 std::ostringstream s;;
2266 if (filename.empty()) {
2267 s << "/kea-leases";
2268 s << (u == V4 ? "4" : "6");
2269 s << ".csv";
2270 } else {
2271 s << "/" << filename;
2272 }
2273
2274 return (s.str());
2275}
2276
2277std::string
2279 if (u == V4) {
2280 return (lease_file4_ ? lease_file4_->getFilename() : "");
2281 }
2282
2283 return (lease_file6_ ? lease_file6_->getFilename() : "");
2284}
2285
2286bool
2288 // Currently, if the lease file IO is not created, it means that writes to
2289 // disk have been explicitly disabled by the administrator. At some point,
2290 // there may be a dedicated ON/OFF flag implemented to control this.
2291 if (u == V4 && lease_file4_) {
2292 return (true);
2293 }
2294
2295 return (u == V6 && lease_file6_);
2296}
2297
2298std::string
2299Memfile_LeaseMgr::initLeaseFilePath(Universe u) {
2300 std::string persist_val;
2301 try {
2302 persist_val = conn_.getParameter("persist");
2303 } catch (const Exception&) {
2304 // If parameter persist hasn't been specified, we use a default value
2305 // 'yes'.
2306 persist_val = "true";
2307 }
2308 // If persist_val is 'false' we will not store leases to disk, so let's
2309 // return empty file name.
2310 if (persist_val == "false") {
2311 return ("");
2312
2313 } else if (persist_val != "true") {
2314 isc_throw(isc::BadValue, "invalid value 'persist="
2315 << persist_val << "'");
2316 }
2317
2318 std::string lease_file;
2319 try {
2320 lease_file = conn_.getParameter("name");
2321 } catch (const Exception&) {
2322 // Not specified, use the default.
2323 return (getDefaultLeaseFilePath(u));
2324 }
2325
2326 try {
2327 lease_file = CfgMgr::instance().validatePath(lease_file);
2328 } catch (const SecurityWarn& ex) {
2330 .arg(ex.what());
2331 }
2332
2333 return (lease_file);
2334}
2335
2336template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
2337bool
2338Memfile_LeaseMgr::loadLeasesFromFiles(const std::string& filename,
2339 boost::shared_ptr<LeaseFileType>& lease_file,
2340 StorageType& storage) {
2341 // Check if the instance of the LFC is running right now. If it is
2342 // running, we refuse to load leases as the LFC may be writing to the
2343 // lease files right now. When the user retries server configuration
2344 // it should go through.
2347 PIDFile pid_file(appendSuffix(filename, FILE_PID));
2348 if (pid_file.check()) {
2349 isc_throw(DbOpenError, "unable to load leases from files while the "
2350 "lease file cleanup is in progress");
2351 }
2352
2353 storage.clear();
2354
2355 std::string max_row_errors_str = "0";
2356 try {
2357 max_row_errors_str = conn_.getParameter("max-row-errors");
2358 } catch (const std::exception&) {
2359 // Ignore and default to 0.
2360 }
2361
2362 int64_t max_row_errors64;
2363 try {
2364 max_row_errors64 = boost::lexical_cast<int64_t>(max_row_errors_str);
2365 } catch (const boost::bad_lexical_cast&) {
2366 isc_throw(isc::BadValue, "invalid value of the max-row-errors "
2367 << max_row_errors_str << " specified");
2368 }
2369 if ((max_row_errors64 < 0) ||
2370 (max_row_errors64 > std::numeric_limits<uint32_t>::max())) {
2371 isc_throw(isc::BadValue, "invalid value of the max-row-errors "
2372 << max_row_errors_str << " specified");
2373 }
2374 uint32_t max_row_errors = static_cast<uint32_t>(max_row_errors64);
2375
2376 // Load the leasefile.completed, if exists.
2377 bool conversion_needed = false;
2378 lease_file.reset(new LeaseFileType(std::string(filename + ".completed")));
2379 if (lease_file->exists()) {
2380 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2381 max_row_errors);
2382 conversion_needed = conversion_needed || lease_file->needsConversion();
2383 } else {
2384 // If the leasefile.completed doesn't exist, let's load the leases
2385 // from leasefile.2 and leasefile.1, if they exist.
2386 lease_file.reset(new LeaseFileType(appendSuffix(filename, FILE_PREVIOUS)));
2387 if (lease_file->exists()) {
2388 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2389 max_row_errors);
2390 conversion_needed = conversion_needed || lease_file->needsConversion();
2391 }
2392
2393 lease_file.reset(new LeaseFileType(appendSuffix(filename, FILE_INPUT)));
2394 if (lease_file->exists()) {
2395 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2396 max_row_errors);
2397 conversion_needed = conversion_needed || lease_file->needsConversion();
2398 }
2399 }
2400
2401 // Always load leases from the primary lease file. If the lease file
2402 // doesn't exist it will be created by the LeaseFileLoader. Note
2403 // that the false value passed as the last parameter to load
2404 // function causes the function to leave the file open after
2405 // it is parsed. This file will be used by the backend to record
2406 // future lease updates.
2407 lease_file.reset(new LeaseFileType(filename));
2408 LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
2409 max_row_errors, false);
2410 conversion_needed = conversion_needed || lease_file->needsConversion();
2411
2412 return (conversion_needed);
2413}
2414
2415
2416bool
2418 return (lfc_setup_->isRunning());
2419}
2420
2421int
2423 return (lfc_setup_->getExitStatus());
2424}
2425
2426void
2429
2430 // Check if we're in the v4 or v6 space and use the appropriate file.
2431 if (lease_file4_) {
2433 lfcExecute(lease_file4_);
2434 } else if (lease_file6_) {
2436 lfcExecute(lease_file6_);
2437 }
2438}
2439
2440void
2441Memfile_LeaseMgr::lfcSetup(bool conversion_needed) {
2442 std::string lfc_interval_str = "3600";
2443 try {
2444 lfc_interval_str = conn_.getParameter("lfc-interval");
2445 } catch (const std::exception&) {
2446 // Ignore and default to 3600.
2447 }
2448
2449 uint32_t lfc_interval = 0;
2450 try {
2451 lfc_interval = boost::lexical_cast<uint32_t>(lfc_interval_str);
2452 } catch (const boost::bad_lexical_cast&) {
2453 isc_throw(isc::BadValue, "invalid value of the lfc-interval "
2454 << lfc_interval_str << " specified");
2455 }
2456
2457 if (lfc_interval > 0 || conversion_needed) {
2458 lfc_setup_.reset(new LFCSetup(std::bind(&Memfile_LeaseMgr::lfcCallback, this)));
2459 lfc_setup_->setup(lfc_interval, lease_file4_, lease_file6_, conversion_needed);
2460 }
2461}
2462
2463template<typename LeaseFileType>
2464void
2465Memfile_LeaseMgr::lfcExecute(boost::shared_ptr<LeaseFileType>& lease_file) {
2466 bool do_lfc = true;
2467
2468 // Check the status of the LFC instance.
2469 // If the finish file exists or the copy of the lease file exists it
2470 // is an indication that another LFC instance may be in progress or
2471 // may be stalled. In that case we don't want to rotate the current
2472 // lease file to avoid overriding the contents of the existing file.
2473 CSVFile lease_file_finish(appendSuffix(lease_file->getFilename(), FILE_FINISH));
2474 CSVFile lease_file_copy(appendSuffix(lease_file->getFilename(), FILE_INPUT));
2475 if (!lease_file_finish.exists() && !lease_file_copy.exists()) {
2476 // Close the current file so as we can move it to the copy file.
2477 lease_file->close();
2478 // Move the current file to the copy file. Remember the result
2479 // because we don't want to run LFC if the rename failed.
2480 do_lfc = (rename(lease_file->getFilename().c_str(),
2481 lease_file_copy.getFilename().c_str()) == 0);
2482
2483 if (!do_lfc) {
2485 .arg(lease_file->getFilename())
2486 .arg(lease_file_copy.getFilename())
2487 .arg(strerror(errno));
2488 }
2489
2490 // Regardless if we successfully moved the current file or not,
2491 // we need to re-open the current file for the server to write
2492 // new lease updates. If the file has been successfully moved,
2493 // this will result in creation of the new file. Otherwise,
2494 // an existing file will be opened.
2495 try {
2496 lease_file->open(true);
2497
2498 } catch (const CSVFileError& ex) {
2499 // If we're unable to open the lease file this is a serious
2500 // error because the server will not be able to persist
2501 // leases.
2509 .arg(lease_file->getFilename())
2510 .arg(ex.what());
2511 // Reset the pointer to the file so as the backend doesn't
2512 // try to write leases to disk.
2513 lease_file.reset();
2514 do_lfc = false;
2515 }
2516 }
2517 // Once the files have been rotated, or untouched if another LFC had
2518 // not finished, a new process is started.
2519 if (do_lfc) {
2520 lfc_setup_->execute();
2521 }
2522}
2523
2526 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery4(storage4_));
2527 if (MultiThreadingMgr::instance().getMode()) {
2528 std::lock_guard<std::mutex> lock(*mutex_);
2529 query->start();
2530 } else {
2531 query->start();
2532 }
2533
2534 return(query);
2535}
2536
2540 if (MultiThreadingMgr::instance().getMode()) {
2541 std::lock_guard<std::mutex> lock(*mutex_);
2542 query->start();
2543 } else {
2544 query->start();
2545 }
2546
2547 return(query);
2548}
2549
2552 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery4(storage4_, subnet_id));
2553 if (MultiThreadingMgr::instance().getMode()) {
2554 std::lock_guard<std::mutex> lock(*mutex_);
2555 query->start();
2556 } else {
2557 query->start();
2558 }
2559
2560 return(query);
2561}
2562
2565 const SubnetID& last_subnet_id) {
2566 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery4(storage4_, first_subnet_id,
2567 last_subnet_id));
2568 if (MultiThreadingMgr::instance().getMode()) {
2569 std::lock_guard<std::mutex> lock(*mutex_);
2570 query->start();
2571 } else {
2572 query->start();
2573 }
2574
2575 return(query);
2576}
2577
2580 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery6(storage6_));
2581 if (MultiThreadingMgr::instance().getMode()) {
2582 std::lock_guard<std::mutex> lock(*mutex_);
2583 query->start();
2584 } else {
2585 query->start();
2586 }
2587
2588 return(query);
2589}
2590
2594 if (MultiThreadingMgr::instance().getMode()) {
2595 std::lock_guard<std::mutex> lock(*mutex_);
2596 query->start();
2597 } else {
2598 query->start();
2599 }
2600
2601 return(query);
2602}
2603
2606 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery6(storage6_, subnet_id));
2607 if (MultiThreadingMgr::instance().getMode()) {
2608 std::lock_guard<std::mutex> lock(*mutex_);
2609 query->start();
2610 } else {
2611 query->start();
2612 }
2613
2614 return(query);
2615}
2616
2619 const SubnetID& last_subnet_id) {
2620 LeaseStatsQueryPtr query(new MemfileLeaseStatsQuery6(storage6_, first_subnet_id,
2621 last_subnet_id));
2622 if (MultiThreadingMgr::instance().getMode()) {
2623 std::lock_guard<std::mutex> lock(*mutex_);
2624 query->start();
2625 } else {
2626 query->start();
2627 }
2628
2629 return(query);
2630}
2631
2632size_t
2635 .arg(subnet_id);
2636
2637 // Get the index by DUID, IAID, lease type.
2638 const Lease4StorageSubnetIdIndex& idx = storage4_.get<SubnetIdIndexTag>();
2639
2640 // Try to get the lease using the DUID, IAID and lease type.
2641 std::pair<Lease4StorageSubnetIdIndex::const_iterator,
2642 Lease4StorageSubnetIdIndex::const_iterator> r =
2643 idx.equal_range(subnet_id);
2644
2645 // Let's collect all leases.
2646 Lease4Collection leases;
2647 BOOST_FOREACH(auto const& lease, r) {
2648 leases.push_back(lease);
2649 }
2650
2651 size_t num = leases.size();
2652 for (auto const& l : leases) {
2653 deleteLease(l);
2654 }
2656 .arg(subnet_id).arg(num);
2657
2658 return (num);
2659}
2660
2661size_t
2664 .arg(subnet_id);
2665
2666 // Get the index by DUID, IAID, lease type.
2667 const Lease6StorageSubnetIdIndex& idx = storage6_.get<SubnetIdIndexTag>();
2668
2669 // Try to get the lease using the DUID, IAID and lease type.
2670 std::pair<Lease6StorageSubnetIdIndex::const_iterator,
2671 Lease6StorageSubnetIdIndex::const_iterator> r =
2672 idx.equal_range(subnet_id);
2673
2674 // Let's collect all leases.
2675 Lease6Collection leases;
2676 BOOST_FOREACH(auto const& lease, r) {
2677 leases.push_back(lease);
2678 }
2679
2680 size_t num = leases.size();
2681 for (auto const& l : leases) {
2682 deleteLease(l);
2683 }
2685 .arg(subnet_id).arg(num);
2686
2687 return (num);
2688}
2689
2690void
2692 class_lease_counter_.clear();
2693 for (auto const& lease : storage4_) {
2694 // Bump the appropriate accumulator
2695 if (lease->state_ == Lease::STATE_DEFAULT) {
2696 class_lease_counter_.addLease(lease);
2697 }
2698 }
2699}
2700
2701void
2703 class_lease_counter_.clear();
2704 for (auto const& lease : storage6_) {
2705 // Bump the appropriate accumulator
2706 if (lease->state_ == Lease::STATE_DEFAULT) {
2707 class_lease_counter_.addLease(lease);
2708 }
2709 }
2710}
2711
2712size_t
2714 const Lease::Type& ltype /* = Lease::TYPE_V4*/) const {
2715 if (MultiThreadingMgr::instance().getMode()) {
2716 std::lock_guard<std::mutex> lock(*mutex_);
2717 return(class_lease_counter_.getClassCount(client_class, ltype));
2718 } else {
2719 return(class_lease_counter_.getClassCount(client_class, ltype));
2720 }
2721}
2722
2723void
2725 return(class_lease_counter_.clear());
2726}
2727
2728std::string
2730 if (!user_context) {
2731 return ("");
2732 }
2733
2734 ConstElementPtr limits = user_context->find("ISC/limits");
2735 if (!limits) {
2736 return ("");
2737 }
2738
2739 // Iterate of the 'client-classes' list in 'limits'. For each class that specifies
2740 // an "address-limit", check its value against the class's lease count.
2741 ConstElementPtr classes = limits->get("client-classes");
2742 if (classes) {
2743 for (unsigned i = 0; i < classes->size(); ++i) {
2744 ConstElementPtr class_elem = classes->get(i);
2745 // Get class name.
2746 ConstElementPtr name_elem = class_elem->get("name");
2747 if (!name_elem) {
2748 isc_throw(BadValue, "checkLimits4 - client-class.name is missing: "
2749 << prettyPrint(limits));
2750 }
2751
2752 std::string name = name_elem->stringValue();
2753
2754 // Now look for an address-limit
2755 size_t limit;
2756 if (!getLeaseLimit(class_elem, Lease::TYPE_V4, limit)) {
2757 // No limit, go to the next class.
2758 continue;
2759 }
2760
2761 // If the limit is > 0 look up the class lease count. Limit of 0 always
2762 // denies the lease.
2763 size_t lease_count = 0;
2764 if (limit) {
2765 lease_count = getClassLeaseCount(name);
2766 }
2767
2768 // If we're over the limit, return the error, no need to evaluate any others.
2769 if (lease_count >= limit) {
2770 std::ostringstream ss;
2771 ss << "address limit " << limit << " for client class \""
2772 << name << "\", current lease count " << lease_count;
2773 return (ss.str());
2774 }
2775 }
2776 }
2777
2778 // If there were class limits we passed them, now look for a subnet limit.
2779 ConstElementPtr subnet_elem = limits->get("subnet");
2780 if (subnet_elem) {
2781 // Get the subnet id.
2782 ConstElementPtr id_elem = subnet_elem->get("id");
2783 if (!id_elem) {
2784 isc_throw(BadValue, "checkLimits4 - subnet.id is missing: "
2785 << prettyPrint(limits));
2786 }
2787
2788 SubnetID subnet_id = id_elem->intValue();
2789
2790 // Now look for an address-limit.
2791 size_t limit;
2792 if (getLeaseLimit(subnet_elem, Lease::TYPE_V4, limit)) {
2793 // If the limit is > 0 look up the subnet lease count. Limit of 0 always
2794 // denies the lease.
2795 int64_t lease_count = 0;
2796 if (limit) {
2797 lease_count = getSubnetStat(subnet_id, "assigned-addresses");
2798 }
2799
2800 // If we're over the limit, return the error.
2801 if (static_cast<uint64_t>(lease_count) >= limit) {
2802 std::ostringstream ss;
2803 ss << "address limit " << limit << " for subnet ID " << subnet_id
2804 << ", current lease count " << lease_count;
2805 return (ss.str());
2806 }
2807 }
2808 }
2809
2810 // No limits exceeded!
2811 return ("");
2812}
2813
2814std::string
2816 if (!user_context) {
2817 return ("");
2818 }
2819
2820 ConstElementPtr limits = user_context->find("ISC/limits");
2821 if (!limits) {
2822 return ("");
2823 }
2824
2825 // Iterate over the 'client-classes' list in 'limits'. For each class that specifies
2826 // limit (either "address-limit" or "prefix-limit", check its value against the appropriate
2827 // class lease count.
2828 ConstElementPtr classes = limits->get("client-classes");
2829 if (classes) {
2830 for (unsigned i = 0; i < classes->size(); ++i) {
2831 ConstElementPtr class_elem = classes->get(i);
2832 // Get class name.
2833 ConstElementPtr name_elem = class_elem->get("name");
2834 if (!name_elem) {
2835 isc_throw(BadValue, "checkLimits6 - client-class.name is missing: "
2836 << prettyPrint(limits));
2837 }
2838
2839 std::string name = name_elem->stringValue();
2840
2841 // Now look for either address-limit or a prefix=limit.
2842 size_t limit = 0;
2844 if (!getLeaseLimit(class_elem, ltype, limit)) {
2845 ltype = Lease::TYPE_PD;
2846 if (!getLeaseLimit(class_elem, ltype, limit)) {
2847 // No limits for this class, skip to the next.
2848 continue;
2849 }
2850 }
2851
2852 // If the limit is > 0 look up the class lease count. Limit of 0 always
2853 // denies the lease.
2854 size_t lease_count = 0;
2855 if (limit) {
2856 lease_count = getClassLeaseCount(name, ltype);
2857 }
2858
2859 // If we're over the limit, return the error, no need to evaluate any others.
2860 if (lease_count >= limit) {
2861 std::ostringstream ss;
2862 ss << (ltype == Lease::TYPE_NA ? "address" : "prefix")
2863 << " limit " << limit << " for client class \""
2864 << name << "\", current lease count " << lease_count;
2865 return (ss.str());
2866 }
2867 }
2868 }
2869
2870 // If there were class limits we passed them, now look for a subnet limit.
2871 ConstElementPtr subnet_elem = limits->get("subnet");
2872 if (subnet_elem) {
2873 // Get the subnet id.
2874 ConstElementPtr id_elem = subnet_elem->get("id");
2875 if (!id_elem) {
2876 isc_throw(BadValue, "checkLimits6 - subnet.id is missing: "
2877 << prettyPrint(limits));
2878 }
2879
2880 SubnetID subnet_id = id_elem->intValue();
2881
2882 // Now look for either address-limit or a prefix=limit.
2883 size_t limit = 0;
2885 if (!getLeaseLimit(subnet_elem, ltype, limit)) {
2886 ltype = Lease::TYPE_PD;
2887 if (!getLeaseLimit(subnet_elem, ltype, limit)) {
2888 // No limits for the subnet so none exceeded!
2889 return ("");
2890 }
2891 }
2892
2893 // If the limit is > 0 look up the class lease count. Limit of 0 always
2894 // denies the lease.
2895 int64_t lease_count = 0;
2896 if (limit) {
2897 lease_count = getSubnetStat(subnet_id, (ltype == Lease::TYPE_NA ?
2898 "assigned-nas" : "assigned-pds"));
2899 }
2900
2901 // If we're over the limit, return the error.
2902 if (static_cast<uint64_t>(lease_count) >= limit) {
2903 std::ostringstream ss;
2904 ss << (ltype == Lease::TYPE_NA ? "address" : "prefix")
2905 << " limit " << limit << " for subnet ID " << subnet_id
2906 << ", current lease count " << lease_count;
2907 return (ss.str());
2908 }
2909 }
2910
2911 // No limits exceeded!
2912 return ("");
2913}
2914
2915bool
2917 return true;
2918}
2919
2920int64_t
2921Memfile_LeaseMgr::getSubnetStat(const SubnetID& subnet_id, const std::string& stat_label) const {
2924 std::string stat_name = StatsMgr::generateName("subnet", subnet_id, stat_label);
2925 ConstElementPtr stat = StatsMgr::instance().get(stat_name);
2926 ConstElementPtr samples = stat->get(stat_name);
2927 if (samples && samples->size()) {
2928 auto sample = samples->get(0);
2929 if (sample->size()) {
2930 auto count_elem = sample->get(0);
2931 return (count_elem->intValue());
2932 }
2933 }
2934
2935 return (0);
2936}
2937
2938bool
2939Memfile_LeaseMgr::getLeaseLimit(ConstElementPtr parent, Lease::Type ltype, size_t& limit) const {
2940 ConstElementPtr limit_elem = parent->get(ltype == Lease::TYPE_PD ?
2941 "prefix-limit" : "address-limit");
2942 if (limit_elem) {
2943 limit = limit_elem->intValue();
2944 return (true);
2945 }
2946
2947 return (false);
2948}
2949
2950namespace {
2951
2952std::string
2953idToText(const OptionBuffer& id) {
2954 std::stringstream tmp;
2955 tmp << std::hex;
2956 bool delim = false;
2957 for (auto const& it : id) {
2958 if (delim) {
2959 tmp << ":";
2960 }
2961 tmp << std::setw(2) << std::setfill('0')
2962 << static_cast<unsigned int>(it);
2963 delim = true;
2964 }
2965 return (tmp.str());
2966}
2967
2968} // anonymous namespace
2969
2972 const IOAddress& lower_bound_address,
2973 const LeasePageSize& page_size,
2974 const time_t& qry_start_time /* = 0 */,
2975 const time_t& qry_end_time /* = 0 */) {
2978 .arg(page_size.page_size_)
2979 .arg(lower_bound_address.toText())
2980 .arg(idToText(relay_id))
2981 .arg(qry_start_time)
2982 .arg(qry_end_time);
2983
2984 // Expecting IPv4 address.
2985 if (!lower_bound_address.isV4()) {
2986 isc_throw(InvalidAddressFamily, "expected IPv4 address while "
2987 "retrieving leases from the lease database, got "
2988 << lower_bound_address);
2989 }
2990
2991 // Catch 2038 bug with 32 bit time_t.
2992 if ((qry_start_time < 0) || (qry_end_time < 0)) {
2993 isc_throw(BadValue, "negative time value");
2994 }
2995
2996 // Start time must be before end time.
2997 if ((qry_start_time > 0) && (qry_end_time > 0) &&
2998 (qry_start_time > qry_end_time)) {
2999 isc_throw(BadValue, "start time must be before end time");
3000 }
3001
3002 if (MultiThreadingMgr::instance().getMode()) {
3003 std::lock_guard<std::mutex> lock(*mutex_);
3004 return (getLeases4ByRelayIdInternal(relay_id,
3005 lower_bound_address,
3006 page_size,
3007 qry_start_time,
3008 qry_end_time));
3009 } else {
3010 return (getLeases4ByRelayIdInternal(relay_id,
3011 lower_bound_address,
3012 page_size,
3013 qry_start_time,
3014 qry_end_time));
3015 }
3016}
3017
3019Memfile_LeaseMgr::getLeases4ByRelayIdInternal(const OptionBuffer& relay_id,
3020 const IOAddress& lower_bound_address,
3021 const LeasePageSize& page_size,
3022 const time_t& qry_start_time,
3023 const time_t& qry_end_time) {
3024 Lease4Collection collection;
3025 const Lease4StorageRelayIdIndex& idx = storage4_.get<RelayIdIndexTag>();
3026 Lease4StorageRelayIdIndex::const_iterator lb =
3027 idx.lower_bound(boost::make_tuple(relay_id, lower_bound_address));
3028 // Return all convenient leases being within the page size.
3029 IOAddress last_addr = lower_bound_address;
3030 for (; lb != idx.end(); ++lb) {
3031 if ((*lb)->addr_ == last_addr) {
3032 // Already seen: skip it.
3033 continue;
3034 }
3035 if ((*lb)->relay_id_ != relay_id) {
3036 // Gone after the relay id index.
3037 break;
3038 }
3039 last_addr = (*lb)->addr_;
3040 if ((qry_start_time > 0) && ((*lb)->cltt_ < qry_start_time)) {
3041 // Too old.
3042 continue;
3043 }
3044 if ((qry_end_time > 0) && ((*lb)->cltt_ > qry_end_time)) {
3045 // Too young.
3046 continue;
3047 }
3048 collection.push_back(Lease4Ptr(new Lease4(**lb)));
3049 if (collection.size() >= page_size.page_size_) {
3050 break;
3051 }
3052 }
3053 return (collection);
3054}
3055
3058 const IOAddress& lower_bound_address,
3059 const LeasePageSize& page_size,
3060 const time_t& qry_start_time /* = 0 */,
3061 const time_t& qry_end_time /* = 0 */) {
3064 .arg(page_size.page_size_)
3065 .arg(lower_bound_address.toText())
3066 .arg(idToText(remote_id))
3067 .arg(qry_start_time)
3068 .arg(qry_end_time);
3069
3070 // Expecting IPv4 address.
3071 if (!lower_bound_address.isV4()) {
3072 isc_throw(InvalidAddressFamily, "expected IPv4 address while "
3073 "retrieving leases from the lease database, got "
3074 << lower_bound_address);
3075 }
3076
3077 // Catch 2038 bug with 32 bit time_t.
3078 if ((qry_start_time < 0) || (qry_end_time < 0)) {
3079 isc_throw(BadValue, "negative time value");
3080 }
3081
3082 // Start time must be before end time.
3083 if ((qry_start_time > 0) && (qry_end_time > 0) &&
3084 (qry_start_time > qry_end_time)) {
3085 isc_throw(BadValue, "start time must be before end time");
3086 }
3087
3088 if (MultiThreadingMgr::instance().getMode()) {
3089 std::lock_guard<std::mutex> lock(*mutex_);
3090 return (getLeases4ByRemoteIdInternal(remote_id,
3091 lower_bound_address,
3092 page_size,
3093 qry_start_time,
3094 qry_end_time));
3095 } else {
3096 return (getLeases4ByRemoteIdInternal(remote_id,
3097 lower_bound_address,
3098 page_size,
3099 qry_start_time,
3100 qry_end_time));
3101 }
3102}
3103
3105Memfile_LeaseMgr::getLeases4ByRemoteIdInternal(const OptionBuffer& remote_id,
3106 const IOAddress& lower_bound_address,
3107 const LeasePageSize& page_size,
3108 const time_t& qry_start_time,
3109 const time_t& qry_end_time) {
3110 Lease4Collection collection;
3111 std::map<IOAddress, Lease4Ptr> sorted;
3112 const Lease4StorageRemoteIdIndex& idx = storage4_.get<RemoteIdIndexTag>();
3113 Lease4StorageRemoteIdRange er = idx.equal_range(remote_id);
3114 // Store all convenient leases being within the page size.
3115 BOOST_FOREACH(auto const& it, er) {
3116 const IOAddress& addr = it->addr_;
3117 if (addr <= lower_bound_address) {
3118 // Not greater than lower_bound_address.
3119 continue;
3120 }
3121 if ((qry_start_time > 0) && (it->cltt_ < qry_start_time)) {
3122 // Too old.
3123 continue;
3124 }
3125 if ((qry_end_time > 0) && (it->cltt_ > qry_end_time)) {
3126 // Too young.
3127 continue;
3128 }
3129 sorted[addr] = it;
3130 }
3131
3132 // Return all leases being within the page size.
3133 for (auto const& it : sorted) {
3134 collection.push_back(Lease4Ptr(new Lease4(*it.second)));
3135 if (collection.size() >= page_size.page_size_) {
3136 break;
3137 }
3138 }
3139 return (collection);
3140}
3141
3142void
3144 if (MultiThreadingMgr::instance().getMode()) {
3145 std::lock_guard<std::mutex> lock(*mutex_);
3146 relay_id6_.clear();
3147 remote_id6_.clear();
3148 } else {
3149 relay_id6_.clear();
3150 remote_id6_.clear();
3151 }
3152}
3153
3154size_t
3156 return (relay_id6_.size());
3157}
3158
3159size_t
3161 return (remote_id6_.size());
3162}
3163
3166 const IOAddress& lower_bound_address,
3167 const LeasePageSize& page_size) {
3170 .arg(page_size.page_size_)
3171 .arg(lower_bound_address.toText())
3172 .arg(relay_id.toText());
3173
3174 // Expecting IPv6 valid address.
3175 if (!lower_bound_address.isV6()) {
3176 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
3177 "retrieving leases from the lease database, got "
3178 << lower_bound_address);
3179 }
3180
3181 if (MultiThreadingMgr::instance().getMode()) {
3182 std::lock_guard<std::mutex> lock(*mutex_);
3183 return (getLeases6ByRelayIdInternal(relay_id,
3184 lower_bound_address,
3185 page_size));
3186 } else {
3187 return (getLeases6ByRelayIdInternal(relay_id,
3188 lower_bound_address,
3189 page_size));
3190 }
3191}
3192
3194Memfile_LeaseMgr::getLeases6ByRelayIdInternal(const DUID& relay_id,
3195 const IOAddress& lower_bound_address,
3196 const LeasePageSize& page_size) {
3197 const std::vector<uint8_t>& relay_id_data = relay_id.getDuid();
3198 Lease6Collection collection;
3199 const RelayIdIndex& idx = relay_id6_.get<RelayIdIndexTag>();
3200 RelayIdIndex::const_iterator lb =
3201 idx.lower_bound(boost::make_tuple(relay_id_data, lower_bound_address));
3202
3203 // Return all leases being within the page size.
3204 IOAddress last_addr = lower_bound_address;
3205 for (; lb != idx.end(); ++lb) {
3206 if ((*lb)->lease_addr_ == last_addr) {
3207 // Already seen: skip it.
3208 continue;
3209 }
3210 if ((*lb)->id_ != relay_id_data) {
3211 // Gone after the relay id index.
3212 break;
3213 }
3214 last_addr = (*lb)->lease_addr_;
3215 Lease6Ptr lease = getAnyLease6Internal(last_addr);
3216 if (lease) {
3217 collection.push_back(lease);
3218 if (collection.size() >= page_size.page_size_) {
3219 break;
3220 }
3221 }
3222 }
3223 return (collection);
3224}
3225
3228 const IOAddress& lower_bound_address,
3229 const LeasePageSize& page_size) {
3232 .arg(page_size.page_size_)
3233 .arg(lower_bound_address.toText())
3234 .arg(idToText(remote_id));
3235
3236 // Expecting IPv6 valid address.
3237 if (!lower_bound_address.isV6()) {
3238 isc_throw(InvalidAddressFamily, "expected IPv6 address while "
3239 "retrieving leases from the lease database, got "
3240 << lower_bound_address);
3241 }
3242
3243 if (MultiThreadingMgr::instance().getMode()) {
3244 std::lock_guard<std::mutex> lock(*mutex_);
3245 return (getLeases6ByRemoteIdInternal(remote_id,
3246 lower_bound_address,
3247 page_size));
3248 } else {
3249 return (getLeases6ByRemoteIdInternal(remote_id,
3250 lower_bound_address,
3251 page_size));
3252 }
3253}
3254
3256Memfile_LeaseMgr::getLeases6ByRemoteIdInternal(const OptionBuffer& remote_id,
3257 const IOAddress& lower_bound_address,
3258 const LeasePageSize& page_size) {
3259 Lease6Collection collection;
3260 std::set<IOAddress> sorted;
3261 const RemoteIdIndex& idx = remote_id6_.get<RemoteIdIndexTag>();
3262 RemoteIdIndexRange er = idx.equal_range(remote_id);
3263 // Store all addresses greater than lower_bound_address.
3264 BOOST_FOREACH(auto const& it, er) {
3265 const IOAddress& addr = it->lease_addr_;
3266 if (addr <= lower_bound_address) {
3267 continue;
3268 }
3269 static_cast<void>(sorted.insert(addr));
3270 }
3271
3272 // Return all leases being within the page size.
3273 for (const IOAddress& addr : sorted) {
3274 Lease6Ptr lease = getAnyLease6Internal(addr);
3275 if (lease) {
3276 collection.push_back(lease);
3277 if (collection.size() >= page_size.page_size_) {
3278 break;
3279 }
3280 }
3281 }
3282 return (collection);
3283}
3284
3285size_t
3286Memfile_LeaseMgr::extractExtendedInfo4(bool update, bool current) {
3288 if (current) {
3289 cfg = CfgMgr::instance().getCurrentCfg()->getConsistency();
3290 } else {
3291 cfg = CfgMgr::instance().getStagingCfg()->getConsistency();
3292 }
3293 if (!cfg) {
3294 isc_throw(Unexpected, "the " << (current ? "current" : "staging")
3295 << " consistency configuration is null");
3296 }
3297 auto check = cfg->getExtendedInfoSanityCheck();
3298
3302 .arg(update ? " updating in file" : "");
3303
3304 size_t leases = 0;
3305 size_t modified = 0;
3306 size_t updated = 0;
3307 size_t processed = 0;
3308 auto& index = storage4_.get<AddressIndexTag>();
3309 auto lease_it = index.begin();
3310 auto next_it = index.end();
3311
3312 for (; lease_it != index.end(); lease_it = next_it) {
3313 next_it = std::next(lease_it);
3314 Lease4Ptr lease = *lease_it;
3315 ++leases;
3316 try {
3317 if (upgradeLease4ExtendedInfo(lease, check)) {
3318 ++modified;
3319 if (update && persistLeases(V4)) {
3320 lease_file4_->append(*lease);
3321 ++updated;
3322 }
3323 }
3324 // Work on a copy as the multi-index requires fields used
3325 // as indexes to be read-only.
3326 Lease4Ptr copy(new Lease4(*lease));
3328 if (!copy->relay_id_.empty() || !copy->remote_id_.empty()) {
3329 index.replace(lease_it, copy);
3330 ++processed;
3331 }
3332 } catch (const std::exception& ex) {
3335 .arg(lease->addr_.toText())
3336 .arg(ex.what());
3337 }
3338 }
3339
3341 .arg(leases)
3342 .arg(modified)
3343 .arg(updated)
3344 .arg(processed);
3345
3346 return (updated);
3347}
3348
3349size_t
3351 return (0);
3352}
3353
3354void
3356 CfgConsistencyPtr cfg = CfgMgr::instance().getStagingCfg()->getConsistency();
3357 if (!cfg) {
3358 isc_throw(Unexpected, "the staging consistency configuration is null");
3359 }
3360 auto check = cfg->getExtendedInfoSanityCheck();
3361 bool enabled = getExtendedInfoTablesEnabled();
3362
3366 .arg(enabled ? "enabled" : "disabled");
3367
3368 // Clear tables when enabled.
3369 if (enabled) {
3370 relay_id6_.clear();
3371 remote_id6_.clear();
3372 }
3373
3374 size_t leases = 0;
3375 size_t modified = 0;
3376 size_t processed = 0;
3377
3378 for (auto const& lease : storage6_) {
3379 ++leases;
3380 try {
3381 if (upgradeLease6ExtendedInfo(lease, check)) {
3382 ++modified;
3383 }
3384 if (enabled && addExtendedInfo6(lease)) {
3385 ++processed;
3386 }
3387 } catch (const std::exception& ex) {
3390 .arg(lease->addr_.toText())
3391 .arg(ex.what());
3392 }
3393 }
3394
3396 .arg(leases)
3397 .arg(modified)
3398 .arg(processed);
3399}
3400
3401size_t
3403 return (0);
3404}
3405
3406void
3408 LeaseAddressRelayIdIndex& relay_id_idx =
3410 static_cast<void>(relay_id_idx.erase(addr));
3411 LeaseAddressRemoteIdIndex& remote_id_idx =
3413 static_cast<void>(remote_id_idx.erase(addr));
3414}
3415
3416void
3418 const std::vector<uint8_t>& relay_id) {
3419 Lease6ExtendedInfoPtr ex_info;
3420 ex_info.reset(new Lease6ExtendedInfo(lease_addr, relay_id));
3421 relay_id6_.insert(ex_info);
3422}
3423
3424void
3426 const std::vector<uint8_t>& remote_id) {
3427 Lease6ExtendedInfoPtr ex_info;
3428 ex_info.reset(new Lease6ExtendedInfo(lease_addr, remote_id));
3429 remote_id6_.insert(ex_info);
3430}
3431
3432void
3433Memfile_LeaseMgr::writeLeases4(const std::string& filename) {
3434 if (MultiThreadingMgr::instance().getMode()) {
3435 std::lock_guard<std::mutex> lock(*mutex_);
3436 writeLeases4Internal(filename);
3437 } else {
3438 writeLeases4Internal(filename);
3439 }
3440}
3441
3442void
3443Memfile_LeaseMgr::writeLeases4Internal(const std::string& filename) {
3444 bool overwrite = (lease_file4_ && lease_file4_->getFilename() == filename);
3445 try {
3446 if (overwrite) {
3447 lease_file4_->close();
3448 }
3449 std::ostringstream old;
3450 old << filename << ".bak" << getpid();
3451 ::rename(filename.c_str(), old.str().c_str());
3452 CSVLeaseFile4 backup(filename);
3453 backup.open();
3454 for (auto const& lease : storage4_) {
3455 backup.append(*lease);
3456 }
3457 backup.close();
3458 if (overwrite) {
3459 lease_file4_->open(true);
3460 }
3461 } catch (const std::exception&) {
3462 if (overwrite) {
3463 lease_file4_->open(true);
3464 }
3465 throw;
3466 }
3467}
3468
3469void
3470Memfile_LeaseMgr::writeLeases6(const std::string& filename) {
3471 if (MultiThreadingMgr::instance().getMode()) {
3472 std::lock_guard<std::mutex> lock(*mutex_);
3473 writeLeases6Internal(filename);
3474 } else {
3475 writeLeases6Internal(filename);
3476 }
3477}
3478
3479void
3480Memfile_LeaseMgr::writeLeases6Internal(const std::string& filename) {
3481 bool overwrite = (lease_file6_ && lease_file6_->getFilename() == filename);
3482 try {
3483 if (overwrite) {
3484 lease_file6_->close();
3485 }
3486 std::ostringstream old;
3487 old << filename << ".bak" << getpid();
3488 ::rename(filename.c_str(), old.str().c_str());
3489 CSVLeaseFile6 backup(filename);
3490 backup.open();
3491 for (auto const& lease : storage6_) {
3492 backup.append(*lease);
3493 }
3494 backup.close();
3495 if (overwrite) {
3496 lease_file6_->open(true);
3497 }
3498 } catch (const std::exception&) {
3499 if (overwrite) {
3500 lease_file6_->open(true);
3501 }
3502 throw;
3503 }
3504}
3505
3508 try {
3511 return (TrackingLeaseMgrPtr(new Memfile_LeaseMgr(parameters)));
3512 } catch (const std::exception& ex) {
3514 .arg(ex.what());
3515 throw;
3516 }
3517}
3518
3519} // namespace dhcp
3520} // namespace isc
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.
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.
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:246
std::string validatePath(const std::string data_path) const
Validates a file path against the supported directory for DHDP data.
Definition cfgmgr.cc:40
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition cfgmgr.cc:121
std::string getDataDir(bool reset=false, const std::string explicit_path="")
Fetches the supported DHCP data directory.
Definition cfgmgr.cc:35
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition cfgmgr.cc:116
void addLease(LeasePtr lease)
Increment the counts for all of a lease's classes by one.
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
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
LeaseStatsQuery(const SelectMode &select_mode=ALL_SUBNETS)
Constructor to query statistics for all subnets.
Definition lease_mgr.cc:220
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.
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.
std::string getDefaultLeaseFilePath(Universe u, const std::string filename="") const
Returns default path to the lease file.
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.
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
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.
RAII class creating a critical section.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
int version()
returns Kea hooks version.
#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
int get(CalloutHandle &handle)
The gss-tsig-get command.
#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:1549
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:44
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_PATH_SECURITY_WARNING
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
const isc::log::MessageID DHCPSRV_MEMFILE_FAILED_TO_OPEN
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).