LCOV - code coverage report
Current view: top level - vnsw/agent/services - dhcp_lease_db.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 6 7 85.7 %
Date: 2026-11-09 02:10:49 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef vnsw_agent_dhcp_lease_h__
       7             : #define vnsw_agent_dhcp_lease_h__
       8             : 
       9             : #include <fstream>
      10             : #include <boost/dynamic_bitset.hpp>
      11             : 
      12             : class Timer;
      13             : namespace pugi {
      14             : class xml_node;
      15             : }
      16             : 
      17             : // DHCP lease management is implemented here for a given subnet - used by
      18             : // Gateway interfaces. Hosts on the gateway interface get addresses allocated
      19             : // from the subnet assigned to the Gateway interface. Addresses other than
      20             : // reserved addresses can be leased or released.
      21             : // Two bitmasks are maintained, each bit representing an IP of the subnet
      22             : //     1. lease_bitmap - bit set implies IP is available and can be leased
      23             : //     2. released_lease_bitmap - bit set implies IP was leased earlier but
      24             : //        can be given again. This is maintained so that we can give the same
      25             : //        lease, if possible.
      26             : // When a new lease request comes, first available address from lease_bitmap
      27             : // is allocated. When lease_bitmap is exhausted, a released address from
      28             : // released_lease_bitmap is allocated.
      29             : //
      30             : // Lease records are persisted in a file. Records are appended to the file,
      31             : // with the last record being the latest for a client. The lease file is
      32             : // compacted after a certain number of lease updates.
      33             : 
      34             : class DhcpLeaseDb {
      35             : public:
      36             :     static const uint32_t kDhcpLeaseTimer = 300000;        // milli seconds
      37             : 
      38             :     struct DhcpLease {
      39             :         MacAddress mac_;
      40             :         mutable Ip4Address ip_;
      41             :         mutable uint64_t lease_expiry_time_;
      42             :         mutable bool released_;
      43             : 
      44          89 :         DhcpLease(const MacAddress &m, const Ip4Address &i,
      45          89 :                   uint64_t t, bool r) :
      46          89 :             mac_(m), ip_(i), lease_expiry_time_(t), released_(r) {}
      47             : 
      48         201 :         bool operator <(const DhcpLease &rhs) const {
      49         201 :             return mac_ < rhs.mac_;
      50             :         }
      51             :     };
      52             : 
      53             :     DhcpLeaseDb(const Ip4Address &subnet, uint8_t plen,
      54             :                 const std::vector<Ip4Address> &reserve_addresses,
      55             :                 const std::string &lease_filename, boost::asio::io_context &io);
      56             :     virtual ~DhcpLeaseDb();
      57             : 
      58             :     // update subnet details
      59             :     void Update(const Ip4Address &subnet, uint8_t plen,
      60             :                 const std::vector<Ip4Address> &reserve_addresses);
      61             : 
      62             :     // allocate an address for the lease time (specified in seconds)
      63             :     bool Allocate(const MacAddress &mac, Ip4Address *address, uint64_t lease);
      64             :     bool Release(const MacAddress &mac);
      65             : 
      66             :     Ip4Address subnet() const { return subnet_; }
      67           0 :     uint8_t plen() const { return plen_; }
      68           9 :     const std::set<DhcpLease> &leases() const { return leases_; }
      69             :     void ClearLeases();
      70             :     void set_lease_timeout(uint32_t timeout);
      71             : 
      72             : private:
      73             :     friend class DhcpTest;
      74             :     typedef boost::dynamic_bitset<> Bitmap;
      75             : 
      76             :     bool LeaseTimerExpiry();
      77             :     void UpdateLease(const MacAddress &mac, const Ip4Address &ip,
      78             :                      uint64_t expiry, bool released);
      79             :     void ReserveAddresses(const std::vector<Ip4Address> &addresses,
      80             :                           bool subnet_change);
      81             :     void IndexToAddress(size_t index, Ip4Address *address) const;
      82             :     size_t AddressToIndex(const Ip4Address &address) const;
      83             :     bool IsReservedAddress(const Ip4Address &address) const;
      84             :     void UpdateLeaseFileName(const std::string &name);
      85             :     void CreateLeaseFile();
      86             :     void PersistLeaseRecord(const MacAddress &mac, const Ip4Address &ip,
      87             :                             const uint64_t &expiry, bool released);
      88             :     void PersistLeaseRecords(const std::vector<DhcpLease> &leases);
      89             :     void WriteLeaseRecord(std::ofstream &lease_ofstream,
      90             :                           const MacAddress &mac, const Ip4Address &ip,
      91             :                           const uint64_t &expiry, bool released);
      92             :     void LoadLeaseFile();
      93             :     void ReadLeaseFile(std::string &leases);
      94             :     void ParseLeaseFile(const std::string &leases);
      95             :     void ParseLease(const pugi::xml_node &lease);
      96             : 
      97             :     Ip4Address subnet_;
      98             :     uint8_t    plen_;
      99             :     Bitmap     lease_bitmap_;
     100             :     Bitmap     released_lease_bitmap_; // bitmap indicating released addresses
     101             :     std::vector<Ip4Address> reserve_addresses_;
     102             :     std::set<DhcpLease> leases_;
     103             : 
     104             :     uint32_t max_lease_update_count_;
     105             :     uint32_t lease_update_count_;
     106             :     uint32_t lease_timeout_;
     107             :     Timer *timer_;
     108             :     std::string lease_filename_;
     109             : 
     110             :     DISALLOW_COPY_AND_ASSIGN(DhcpLeaseDb);
     111             : };
     112             : 
     113             : #endif // vnsw_agent_dhcp_lease_h__

Generated by: LCOV version 1.14