memfile_lease_mgr.h 17.3 KB
Newer Older
1
// Copyright (C) 2012-2014 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 20
#include <dhcpsrv/lease_mgr.h>

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

27 28 29 30 31 32 33 34 35 36 37 38 39 40
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:

41 42 43 44 45 46 47 48 49 50 51
    /// @brief Specifies universe (V4, V6)
    ///
    /// This enumeration is used by various functions in Memfile Lease Manager,
    /// to identify the lease type referred to. In particular, it is used by
    /// functions operating on the lease files to distinguish between lease
    /// files for DHCPv4 and DHCPv6.
    enum Universe {
        V4,
        V6
    };

52 53 54 55 56 57
    /// @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.
    ///
58 59 60
    /// @param parameters A data structure relating keywords and values
    ///        concerned with the database.
    Memfile_LeaseMgr(const ParameterMap& parameters);
61 62 63 64 65 66 67

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

    /// @brief Adds an IPv4 lease.
    ///
    /// @param lease lease to be added
Tomek Mrugalski's avatar
Tomek Mrugalski committed
68
    virtual bool addLease(const Lease4Ptr& lease);
69 70 71 72

    /// @brief Adds an IPv6 lease.
    ///
    /// @param lease lease to be added
Tomek Mrugalski's avatar
Tomek Mrugalski committed
73
    virtual bool addLease(const Lease6Ptr& lease);
74 75 76

    /// @brief Returns existing IPv4 lease for specified IPv4 address.
    ///
77 78 79 80
    /// This function returns a copy of the lease. The modification in the
    /// return lease does not affect the instance held in the lease storage.
    ///
    /// @param addr An address of the searched lease.
81 82
    ///
    /// @return a collection of leases
83
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
84 85 86 87 88 89 90 91 92 93 94

    /// @brief Returns existing IPv4 leases for specified hardware address.
    ///
    /// 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
95
    virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
96

97
    /// @brief Returns existing IPv4 lease for specified hardware address
98 99
    ///        and a subnet
    ///
100 101
    /// This function returns a copy of the lease. The modification in the
    /// return lease does not affect the instance held in the lease storage.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
102
    ///
103 104 105 106 107 108 109 110 111 112 113 114
    /// 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
    ///
115 116
    /// @param client_id client identifier
    virtual Lease4Collection getLease4(const ClientId& client_id) const;
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    /// @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;

133 134
    /// @brief Returns existing IPv4 lease for specified client-id
    ///
135 136 137
    /// This function returns a copy of the lease. The modification in the
    /// return lease does not affect the instance held in the lease storage.
    ///
138 139 140 141 142 143 144 145 146 147 148 149
    /// 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 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.
    ///
150 151 152
    /// This function returns a copy of the lease. The modification in the
    /// return lease does not affect the instance held in the lease storage.
    ///
153
    /// @param type specifies lease type: (NA, TA or PD)
154
    /// @param addr An address of the searched lease.
155 156
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
157
    virtual Lease6Ptr getLease6(Lease::Type type,
158
                                const isc::asiolink::IOAddress& addr) const;
159 160 161

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
162 163
    /// @todo Not implemented yet
    ///
164
    /// @param type specifies lease type: (NA, TA or PD)
165 166 167 168
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
    /// @return collection of IPv6 leases
169
    virtual Lease6Collection getLeases6(Lease::Type type,
170
                                        const DUID& duid, uint32_t iaid) const;
171

172
    /// @brief Returns existing IPv6 lease for a given DUID/IA/subnet-id tuple
173
    ///
174 175
    /// This function returns a copy of the lease. The modification in the
    /// return lease does not affect the instance held in the lease storage.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
176
    ///
177
    /// @param type specifies lease type: (NA, TA or PD)
178 179 180 181
    /// @param duid client DUID
    /// @param iaid IA identifier
    /// @param subnet_id identifier of the subnet the lease must belong to
    ///
182
    /// @return lease collection (may be empty if no lease is found)
183
    virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
184
                                        uint32_t iaid, SubnetID subnet_id) const;
185 186 187

    /// @brief Updates IPv4 lease.
    ///
188 189
    /// @warning This function does not validate the pointer to the lease.
    /// It is caller's responsibility to pass the valid pointer.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
190
    ///
191 192 193
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
194
    virtual void updateLease4(const Lease4Ptr& lease4);
195

196
    /// @brief Updates IPv6 lease.
197
    ///
198 199
    /// @warning This function does not validate the pointer to the lease.
    /// It is caller's responsibility to pass the valid pointer.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
200
    ///
201
    /// @param lease6 The lease to be updated.
202 203
    ///
    /// If no such lease is present, an exception will be thrown.
204
    virtual void updateLease6(const Lease6Ptr& lease6);
205 206 207

    /// @brief Deletes a lease.
    ///
208 209
    /// @param addr Address of the lease to be deleted. (This can be IPv4 or
    ///        IPv6.)
210 211
    ///
    /// @return true if deletion was successful, false if no such lease exists
212
    virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
213

214 215 216 217 218 219 220 221 222
    /// @brief Return backend type
    ///
    /// Returns the type of the backend.
    ///
    /// @return Type of the backend.
    virtual std::string getType() const {
        return (std::string("memfile"));
    }

223 224
    /// @brief Returns backend name.
    ///
225
    /// For now, memfile can only store data in memory.
226 227 228
    ///
    /// @return Name of the backend.
    virtual std::string getName() const {
229
        return ("memory");
230
    }
231 232 233 234

    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
235 236 237
    ///
    /// @return Description of the backend.
    virtual std::string getDescription() const;
238 239

    /// @brief Returns backend version.
240 241 242
    ///
    /// @return Version number as a pair of unsigned integers.  "first" is the
    ///         major version number, "second" the minor number.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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();
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    /// @brief Returns default path to the lease file.
    ///
    /// @param u Universe (V4 or V6).
    std::string getDefaultLeaseFilePath(Universe u) const;

    /// @brief Returns an absolute path to the lease file.
    ///
    /// @param u Universe (V4 or V6).
    std::string getLeaseFilePath(Universe u) const {
        return (u == V4 ? lease_file4_ : lease_file6_);
    }

    /// @brief Specifies whether or not leases are written to disk.
    ///
    /// It is possible that leases for DHCPv4 are written to disk whereas leases
    /// for DHCPv6 are not; or vice versa. The argument of the method specifies
    /// the type of lease in that respect.
    ///
    /// @param u Universe (V4 or V6).
    ///
    /// @return true if leases are written to lease file; if false is
    /// returned, leases will be held in memory and will be lost upon
    /// server shut down.
    bool persistLeases(Universe u) const;

284 285
protected:

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    /// @brief Initialize the location of the lease file.
    ///
    /// This method uses the parameters passed as a map to the constructor to
    /// initialize the location of the lease file. If the lease file is not
    /// specified, the method will use the default location for the universe
    /// (v4 or v6) selected. If the location is specified in the map as empty
    /// it will set the empty location, which implies that leases belonging to
    /// the specified universe will not be written to disk.
    ///
    /// @param u Universe (v4 or v6)
    /// @param parameters Map holding parameters of the Lease Manager, passed to
    /// the constructor.
    ///
    /// @return The location of the lease file that should be assigned to the
    /// lease_file4_ or lease_file6_, depending on the universe specified as an
    /// argument to this function.
    std::string initLeaseFilePath(Universe u);

304 305 306 307 308 309 310 311 312
    // 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.
313 314
            boost::multi_index::ordered_unique<
                boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
315 316 317 318 319 320 321 322
            >,

            // 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,
323 324 325 326
                    // The DUID can be retrieved from the Lease6 object using
                    // a getDuidVector const function.
                    boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
                                                      &Lease6::getDuidVector>,
327 328 329 330 331
                    // 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_>
                >
332 333
            >
        >
334 335 336 337 338 339
     > 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.
340
        Lease4Ptr,
341 342 343 344 345 346 347 348
        // 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.
349
                boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
350
            >,
351 352

            // Specification of the second index starts here.
353
            boost::multi_index::ordered_unique<
354 355
                // This is a composite index that combines two attributes of the
                // Lease4 object: hardware address and subnet id.
356 357
                boost::multi_index::composite_key<
                    Lease4,
358 359
                    // The hardware address is held in the hwaddr_ member of the
                    // Lease4 object.
360 361
                    boost::multi_index::member<Lease4, std::vector<uint8_t>,
                                               &Lease4::hwaddr_>,
362 363 364 365
                    // 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.
366 367 368
                    boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
                >
            >,
369 370

            // Specification of the third index starts here.
371
            boost::multi_index::ordered_unique<
372 373
                // This is a composite index that uses two values to search for a
                // lease: client id and subnet id.
374 375
                boost::multi_index::composite_key<
                    Lease4,
376 377
                    // The client id can be retrieved from the Lease4 object by
                    // calling getClientIdVector const function.
378
                    boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
379
                                                      &Lease4::getClientIdVector>,
380
                    // The subnet id is accessed through the subnet_id_ member.
381 382
                    boost::multi_index::member<Lease, uint32_t, &Lease::subnet_id_>
                >
383 384 385 386 387 388 389 390
            >,

            // 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,
391 392
                    // The client id can be retrieved from the Lease4 object by
                    // calling getClientIdVector const function.
393
                    boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
394
                                                      &Lease4::getClientIdVector>,
395 396 397 398 399 400 401
                    // 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_>
                >
402 403
            >
        >
404
    > Lease4Storage; // Specify the type name for this container.
405 406 407 408 409

    /// @brief stores IPv4 leases
    Lease4Storage storage4_;

    /// @brief stores IPv6 leases
410
    Lease6Storage storage6_;
411 412 413 414 415 416

    /// @brief Holds the absolute path to the lease file for DHCPv4.
    std::string lease_file4_;

    /// @brief Holds the absolute path to the lease file for DHCPv6.
    std::string lease_file6_;
417 418 419 420 421
};

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

422
#endif // MEMFILE_LEASE_MGR