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