memfile_lease_mgr.h 15.9 KB
Newer Older
1
// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef MEMFILE_LEASE_MGR_H
#define MEMFILE_LEASE_MGR_H

18
#include <dhcp/hwaddr.h>
19
#include <dhcpsrv/key_from_key.h>
20 21
#include <dhcpsrv/lease_mgr.h>

22 23
#include <boost/multi_index/indexed_by.hpp>
#include <boost/multi_index/member.hpp>
24 25
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
26
#include <boost/multi_index/composite_key.hpp>
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
namespace isc {
namespace dhcp {

// This is a concrete implementation of a Lease database.
//
// It is for testing purposes only. It is NOT a production code.
//
// It does not do anything useful now, and is used for abstract LeaseMgr
// class testing. It may later evolve into more useful backend if the
// need arises. We can reuse code from memfile benchmark. See code in
// tests/tools/dhcp-ubench/memfile_bench.{cc|h}
class Memfile_LeaseMgr : public LeaseMgr {
public:

    /// @brief The sole lease manager constructor
    ///
    /// dbconfig is a generic way of passing parameters. Parameters
    /// are passed in the "name=value" format, separated by spaces.
    /// Values may be enclosed in double quotes, if needed.
    ///
48 49 50
    /// @param parameters A data structure relating keywords and values
    ///        concerned with the database.
    Memfile_LeaseMgr(const ParameterMap& parameters);
51 52 53 54 55 56

    /// @brief Destructor (closes file)
    virtual ~Memfile_LeaseMgr();

    /// @brief Adds an IPv4 lease.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
57
    /// @todo Not implemented yet
58
    /// @param lease lease to be added
Tomek Mrugalski's avatar
Tomek Mrugalski committed
59
    virtual bool addLease(const Lease4Ptr& lease);
60 61 62 63

    /// @brief Adds an IPv6 lease.
    ///
    /// @param lease lease to be added
Tomek Mrugalski's avatar
Tomek Mrugalski committed
64
    virtual bool addLease(const Lease6Ptr& lease);
65 66 67

    /// @brief Returns existing IPv4 lease for specified IPv4 address.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
68
    /// @todo Not implemented yet
69 70 71
    /// @param addr address of the searched lease
    ///
    /// @return a collection of leases
72
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
73 74 75

    /// @brief Returns existing IPv4 leases for specified hardware address.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
76 77
    /// @todo Not implemented yet
    ///
78 79 80 81 82 83 84 85
    /// Although in the usual case there will be only one lease, for mobile
    /// clients or clients with multiple static/fixed/reserved leases there
    /// can be more than one. Thus return type is a container, not a single
    /// pointer.
    ///
    /// @param hwaddr hardware address of the client
    ///
    /// @return lease collection
86
    virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
87 88 89 90

    /// @brief Returns existing IPv4 leases for specified hardware address
    ///        and a subnet
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
91 92
    /// @todo Not implemented yet
    ///
93 94 95 96 97 98 99 100 101 102 103 104
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
    /// @param hwaddr hardware address of the client
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
                                SubnetID subnet_id) const;

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
105 106
    /// @todo Not implemented yet
    ///
107 108 109
    /// @param clientid client identifier
    virtual Lease4Collection getLease4(const ClientId& clientid) const;

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /// @brief Returns IPv4 lease for specified client-id/hwaddr/subnet-id tuple
    ///
    /// There can be at most one lease for a given client-id/hwaddr tuple
    /// in a single pool, so this method with either return a single lease
    /// or NULL.
    ///
    /// @param clientid client identifier
    /// @param hwaddr hardware address of the client
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const ClientId& clientid,
                                const HWAddr& hwaddr,
                                SubnetID subnet_id) const;

125 126 127 128 129
    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
130 131
    /// @todo Not implemented yet
    ///
132 133 134 135 136 137 138 139 140 141 142 143
    /// @param clientid client identifier
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const ClientId& clientid,
                                SubnetID subnet_id) const;

    /// @brief Returns existing IPv6 lease for a given IPv6 address.
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
144
    virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const;
145 146 147

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
148 149
    /// @todo Not implemented yet
    ///
150 151 152 153
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
    /// @return collection of IPv6 leases
154
    virtual Lease6Collection getLease6(const DUID& duid, uint32_t iaid) const;
155 156 157

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
158 159
    /// @todo Not implemented yet
    ///
160 161 162 163 164
    /// @param duid client DUID
    /// @param iaid IA identifier
    /// @param subnet_id identifier of the subnet the lease must belong to
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
165
    virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const;
166 167 168

    /// @brief Updates IPv4 lease.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
169 170
    /// @todo Not implemented yet
    ///
171 172 173
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
174
    virtual void updateLease4(const Lease4Ptr& lease4);
175 176 177

    /// @brief Updates IPv4 lease.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
178 179
    /// @todo Not implemented yet
    ///
180
    /// @param lease6 The lease to be updated.
181 182
    ///
    /// If no such lease is present, an exception will be thrown.
183
    virtual void updateLease6(const Lease6Ptr& lease6);
184 185 186

    /// @brief Deletes a lease.
    ///
187 188
    /// @param addr Address of the lease to be deleted. (This can be IPv4 or
    ///        IPv6.)
189 190
    ///
    /// @return true if deletion was successful, false if no such lease exists
191
    virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
192

193 194 195 196 197 198 199 200 201
    /// @brief Return backend type
    ///
    /// Returns the type of the backend.
    ///
    /// @return Type of the backend.
    virtual std::string getType() const {
        return (std::string("memfile"));
    }

202 203
    /// @brief Returns backend name.
    ///
204
    /// For now, memfile can only store data in memory.
205 206 207
    ///
    /// @return Name of the backend.
    virtual std::string getName() const {
208
        return ("memory");
209
    }
210 211 212 213

    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
214 215 216
    ///
    /// @return Description of the backend.
    virtual std::string getDescription() const;
217 218

    /// @brief Returns backend version.
219 220 221
    ///
    /// @return Version number as a pair of unsigned integers.  "first" is the
    ///         major version number, "second" the minor number.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    virtual std::pair<uint32_t, uint32_t> getVersion() const {
        return (std::make_pair(1, 0));
    }

    /// @brief Commit Transactions
    ///
    /// Commits all pending database operations.  On databases that don't
    /// support transactions, this is a no-op.
    virtual void commit();

    /// @brief Rollback Transactions
    ///
    /// Rolls back all pending database operations.  On databases that don't
    /// support transactions, this is a no-op.
    virtual void rollback();
237 238 239

protected:

240 241 242 243 244 245 246 247 248
    // This is a multi-index container, which holds elements that can
    // be accessed using different search indexes.
    typedef boost::multi_index_container<
        // It holds pointers to Lease6 objects.
        Lease6Ptr,
        boost::multi_index::indexed_by<
            // Specification of the first index starts here.
            // This index sorts leases by IPv6 addresses represented as
            // IOAddress objects.
249 250
            boost::multi_index::ordered_unique<
                boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
251 252 253 254 255 256 257 258 259 260
            >,

            // Specification of the second index starts here.
            boost::multi_index::ordered_unique<
                // This is a composite index that will be used to search for
                // the lease using three attributes: DUID, IAID, Subnet Id.
                boost::multi_index::composite_key<
                    Lease6,
                    // The DUID value can't be directly accessed from the Lease6
                    // object because it is wrapped with the DUID object (actually
261 262 263 264
                    // pointer to this object). Therefore we need to use
                    // KeyFromKeyExtractor class to extract the DUID value from
                    // this cascaded structure.
                    KeyFromKeyExtractor<
265 266 267 268 269 270 271 272 273 274 275 276 277
                        // The value of the DUID is accessed by the getDuid() method
                        // from the DUID object.
                        boost::multi_index::const_mem_fun<DUID, std::vector<uint8_t>,
                                                          &DUID::getDuid>,
                        // The DUID object is stored in the duid_ member of the
                        // Lease6 object.
                        boost::multi_index::member<Lease6, DuidPtr, &Lease6::duid_>
                    >,
                    // The two other ingredients of this index are IAID and
                    // subnet id.
                    boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
                    boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
                >
278 279
            >
        >
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
     > Lease6Storage; // Specify the type name of this container.

    // This is a multi-index container, which holds elements that can
    // be accessed using different search indexes.
    typedef boost::multi_index_container<
        // It holds pointers to Lease4 objects.
        Lease4Ptr, 
        // Specification of search indexes starts here.
        boost::multi_index::indexed_by<
            // Specification of the first index starts here.
            // This index sorts leases by IPv4 addresses represented as
            // IOAddress objects.
            boost::multi_index::ordered_unique<
                // The IPv4 address are held in addr_ members that belong to
                // Lease class.
295
                boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
296
            >,
297 298

            // Specification of the second index starts here.
299
            boost::multi_index::ordered_unique<
300 301
                // This is a composite index that combines two attributes of the
                // Lease4 object: hardware address and subnet id.
302 303
                boost::multi_index::composite_key<
                    Lease4,
304 305
                    // The hardware address is held in the hwaddr_ member of the
                    // Lease4 object.
306 307
                    boost::multi_index::member<Lease4, std::vector<uint8_t>,
                                               &Lease4::hwaddr_>,
308 309 310 311
                    // The subnet id is held in the subnet_id_ member of Lease4
                    // class. Note that the subnet_id_ is defined in the base
                    // class (Lease) so we have to point to this class rather
                    // than derived class: Lease4.
312 313 314
                    boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
                >
            >,
315 316

            // Specification of the third index starts here.
317
            boost::multi_index::ordered_unique<
318 319
                // This is a composite index that uses two values to search for a
                // lease: client id and subnet id.
320 321
                boost::multi_index::composite_key<
                    Lease4,
322 323
                    // The client id value is not directly accessible through the
                    // Lease4 object as it is wrapped with the ClientIdPtr object.
324 325 326 327
                    // Therefore we use the KeyFromKeyExtractor class to access
                    // client id through this cascaded structure. The client id
                    // is used as an index value.
                    KeyFromKeyExtractor<
328 329
                        // Specify that the vector holding client id value can be obtained
                        // from the ClientId object.
330 331
                        boost::multi_index::const_mem_fun<ClientId, std::vector<uint8_t>,
                                                          &ClientId::getClientId>,
332 333
                        // Specify that the ClientId object (actually pointer to it) can
                        // be accessed by the client_id_ member of Lease4 class.
334 335
                        boost::multi_index::member<Lease4, ClientIdPtr, &Lease4::client_id_>
                    >,
336
                    // The subnet id is accessed through the subnet_id_ member.
337 338
                    boost::multi_index::member<Lease, uint32_t, &Lease::subnet_id_>
                >
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
            >,

            // Specification of the fourth index starts here.
            boost::multi_index::ordered_unique<
                // This is a composite index that uses two values to search for a
                // lease: client id and subnet id.
                boost::multi_index::composite_key<
                    Lease4,
                    // The client id value is not directly accessible through the
                    // Lease4 object as it is wrapped with the ClientIdPtr object.
                    // Therefore we use the KeyFromKeyExtractor class to access
                    // client id through this cascaded structure. The client id
                    // is used as an index value.
                    KeyFromKeyExtractor<
                        // Specify that the vector holding client id value can be obtained
                        // from the ClientId object.
                        boost::multi_index::const_mem_fun<ClientId, std::vector<uint8_t>,
                                                          &ClientId::getClientId>,
                        // Specify that the ClientId object (actually pointer to it) can
                        // be accessed by the client_id_ member of Lease4 class.
                        boost::multi_index::member<Lease4, ClientIdPtr, &Lease4::client_id_>
                    >,
                    // The hardware address is held in the hwaddr_ member of the
                    // Lease4 object.
                    boost::multi_index::member<Lease4, std::vector<uint8_t>,
                                               &Lease4::hwaddr_>,
                    // The subnet id is accessed through the subnet_id_ member.
                    boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
                >
368 369
            >
        >
370
    > Lease4Storage; // Specify the type name for this container.
371 372 373 374 375

    /// @brief stores IPv4 leases
    Lease4Storage storage4_;

    /// @brief stores IPv6 leases
376 377 378 379 380 381
    Lease6Storage storage6_;
};

}; // end of isc::dhcp namespace
}; // end of isc namespace

382
#endif // MEMFILE_LEASE_MGR