OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dhcp_lease_db.h
Go to the documentation of this file.
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 {
39  mutable Ip4Address ip_;
40  mutable uint64_t lease_expiry_time_;
41  mutable bool released_;
42 
43  DhcpLease(const MacAddress &m, const Ip4Address &i,
44  uint64_t t, bool r) :
45  mac_(m), ip_(i), lease_expiry_time_(t), released_(r) {}
46 
47  bool operator <(const DhcpLease &rhs) const {
48  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  uint8_t plen() const { return plen_; }
67  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 
97  uint8_t plen_;
99  Bitmap released_lease_bitmap_; // bitmap indicating released addresses
100  std::vector<Ip4Address> reserve_addresses_;
101  std::set<DhcpLease> leases_;
102 
105  uint32_t lease_timeout_;
107  std::string lease_filename_;
108 
110 };
111 
112 #endif // vnsw_agent_dhcp_lease_h__
void PersistLeaseRecords(const std::vector< DhcpLease > &leases)
void UpdateLeaseFileName(const std::string &name)
void CreateLeaseFile()
virtual ~DhcpLeaseDb()
const std::set< DhcpLease > & leases() const
Definition: dhcp_lease_db.h:67
uint32_t lease_timeout_
boost::dynamic_bitset Bitmap
Definition: dhcp_lease_db.h:73
Bitmap released_lease_bitmap_
Definition: dhcp_lease_db.h:99
static const uint32_t kDhcpLeaseTimer
Definition: dhcp_lease_db.h:35
void IndexToAddress(size_t index, Ip4Address *address) const
void ParseLeaseFile(const std::string &leases)
uint8_t plen() const
Definition: dhcp_lease_db.h:66
void Update(const Ip4Address &subnet, uint8_t plen, const std::vector< Ip4Address > &reserve_addresses)
void ParseLease(const pugi::xml_node &lease)
std::set< DhcpLease > leases_
DISALLOW_COPY_AND_ASSIGN(DhcpLeaseDb)
void ReserveAddresses(const std::vector< Ip4Address > &addresses, bool subnet_change)
void LoadLeaseFile()
void WriteLeaseRecord(std::ofstream &lease_ofstream, const MacAddress &mac, const Ip4Address &ip, const uint64_t &expiry, bool released)
bool LeaseTimerExpiry()
void set_lease_timeout(uint32_t timeout)
friend class DhcpTest
Definition: dhcp_lease_db.h:72
void PersistLeaseRecord(const MacAddress &mac, const Ip4Address &ip, const uint64_t &expiry, bool released)
std::string lease_filename_
bool Allocate(const MacAddress &mac, Ip4Address *address, uint64_t lease)
size_t AddressToIndex(const Ip4Address &address) const
DhcpLease(const MacAddress &m, const Ip4Address &i, uint64_t t, bool r)
Definition: dhcp_lease_db.h:43
Timer * timer_
void UpdateLease(const MacAddress &mac, const Ip4Address &ip, uint64_t expiry, bool released)
Ip4Address subnet_
Definition: dhcp_lease_db.h:96
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
Bitmap lease_bitmap_
Definition: dhcp_lease_db.h:98
uint32_t lease_update_count_
bool IsReservedAddress(const Ip4Address &address) const
void ClearLeases()
std::vector< Ip4Address > reserve_addresses_
Ip4Address subnet() const
Definition: dhcp_lease_db.h:65
bool Release(const MacAddress &mac)
bool operator<(const DhcpLease &rhs) const
Definition: dhcp_lease_db.h:47
void ReadLeaseFile(std::string &leases)
DhcpLeaseDb(const Ip4Address &subnet, uint8_t plen, const std::vector< Ip4Address > &reserve_addresses, const std::string &lease_filename, boost::asio::io_context &io)
Definition: timer.h:54
uint8_t plen_
Definition: dhcp_lease_db.h:97
uint32_t max_lease_update_count_