OpenSDN source code
dhcp_proto.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef vnsw_agent_dhcp_proto_hpp
7 #define vnsw_agent_dhcp_proto_hpp
8 
9 #include "pkt/proto.h"
10 #include "services/dhcp_handler.h"
11 
12 #define DHCP_TRACE(obj, arg) \
13 do { \
14  std::ostringstream _str; \
15  _str << arg; \
16  Dhcp##obj::TraceMsg(DhcpTraceBuf, __FILE__, __LINE__, _str.str()); \
17 } while (false) \
18 
19 class Timer;
20 class Interface;
21 class DhcpLeaseDb;
22 typedef boost::asio::ip::udp boost_udp;
23 
24 class DhcpProto : public Proto {
25 public:
26  static const uint32_t kDhcpMaxPacketSize = 1024;
27  static const uint32_t kDhcpLeaseFileDeleteTimeout = 60 * 60 * 1000; // 60min
28 
29  enum DhcpMsgType {
31  };
32 
33  typedef std::map<Interface *, DhcpLeaseDb *> LeaseManagerMap;
34  typedef std::pair<Interface *, DhcpLeaseDb *> LeaseManagerPair;
35 
37  uint8_t *pkt;
38  uint32_t len;
39 
40  DhcpVhostMsg(uint8_t *dhcp, uint32_t length)
41  : InterTaskMsg(DHCP_VHOST_MSG), pkt(dhcp), len(length) {}
42  virtual ~DhcpVhostMsg() { if (pkt) delete [] pkt; }
43  };
44 
45  struct DhcpStats {
46  DhcpStats() { Reset(); }
47  void Reset() {
51  }
52 
53  uint32_t discover;
54  uint32_t request;
55  uint32_t inform;
56  uint32_t decline;
57  uint32_t release;
58  uint32_t other;
59  uint32_t offers;
60  uint32_t acks;
61  uint32_t nacks;
62  uint32_t relay_req;
63  uint32_t relay_resp;
64  uint32_t errors;
65  uint32_t unknown_msg_drop;
67  uint32_t incorrect_mac;
68  };
69 
70  void Shutdown();
71  DhcpProto(Agent *agent, boost::asio::io_context &io, bool run_with_vrouter);
72  virtual ~DhcpProto();
73  ProtoHandler *AllocProtoHandler(boost::shared_ptr<PktInfo> info,
74  boost::asio::io_context &io);
75  void SendDhcpIpc(uint8_t *dhcp, std::size_t len);
76 
77  bool dhcp_relay_mode() const { return dhcp_relay_mode_; }
78  void set_dhcp_relay_mode(bool mode) { dhcp_relay_mode_ = mode; }
79 
82  uint32_t ip_fabric_interface_index() const {
84  }
85  void set_ip_fabric_interface_index(uint32_t ind) {
87  }
88  uint32_t pkt_interface_index() const {
89  return pkt_interface_index_;
90  }
91  void set_pkt_interface_index(uint32_t ind) {
93  }
96  }
99  }
100  bool IsRunningWithVrouter() const { return run_with_vrouter_; }
101 
109  void IncrStatsAcks() { stats_.acks++; }
117  const DhcpStats &GetStats() const { return stats_; }
118  void ClearStats() { stats_.Reset(); }
119 
120  void CreateLeaseDb(VmInterface *vmi);
121  void DeleteLeaseDb(VmInterface *vmi);
122  DhcpLeaseDb *GetLeaseDb(Interface *intrface);
123  const LeaseManagerMap &lease_manager() const { return lease_manager_; }
124 
125 private:
126  void ItfNotify(DBEntryBase *entry);
127  void VnNotify(DBEntryBase *entry);
128  void AsyncRead();
129  void ReadHandler(const boost::system::error_code &error, std::size_t len);
130  std::string GetLeaseFileName(const VmInterface *vmi);
132  bool LeaseFileCleanupExpiry(uint32_t seqno);
133 
143 
144  boost::asio::ip::udp::socket dhcp_server_socket_;
145  boost::asio::ip::udp::endpoint remote_endpoint_;
147 
148  std::set<VmInterface *> gw_vmi_list_;
152 
154 };
155 
156 #endif // vnsw_agent_dhcp_proto_hpp
Definition: agent.h:361
int ListenerId
Definition: db_table.h:63
bool dhcp_relay_mode_
Definition: dhcp_proto.h:135
void IncrDhcpDisabledDrops()
Definition: dhcp_proto.h:115
const MacAddress & ip_fabric_interface_mac() const
Definition: dhcp_proto.h:94
void Shutdown()
Definition: dhcp_proto.cc:64
const LeaseManagerMap & lease_manager() const
Definition: dhcp_proto.h:123
static const uint32_t kDhcpMaxPacketSize
Definition: dhcp_proto.h:26
DISALLOW_COPY_AND_ASSIGN(DhcpProto)
void ItfNotify(DBEntryBase *entry)
Definition: dhcp_proto.cc:119
void IncrIncorrectMac()
Definition: dhcp_proto.h:116
void IncrStatsOffers()
Definition: dhcp_proto.h:108
void DeleteLeaseDb(VmInterface *vmi)
Definition: dhcp_proto.cc:207
ProtoHandler * AllocProtoHandler(boost::shared_ptr< PktInfo > info, boost::asio::io_context &io)
Definition: dhcp_proto.cc:114
void IncrStatsRelease()
Definition: dhcp_proto.h:106
void AsyncRead()
Definition: dhcp_proto.cc:83
Interface * ip_fabric_interface_
Definition: dhcp_proto.h:136
uint32_t pkt_interface_index_
Definition: dhcp_proto.h:138
uint32_t gateway_delete_seqno_
Definition: dhcp_proto.h:150
void ClearStats()
Definition: dhcp_proto.h:118
uint32_t ip_fabric_interface_index_
Definition: dhcp_proto.h:137
virtual ~DhcpProto()
Definition: dhcp_proto.cc:61
void IncrStatsOther()
Definition: dhcp_proto.h:107
bool dhcp_relay_mode() const
Definition: dhcp_proto.h:77
boost::asio::ip::udp::socket dhcp_server_socket_
Definition: dhcp_proto.h:144
LeaseManagerMap lease_manager_
Definition: dhcp_proto.h:149
const DhcpStats & GetStats() const
Definition: dhcp_proto.h:117
void IncrStatsRelayReqs()
Definition: dhcp_proto.h:111
Timer * lease_file_cleanup_timer_
Definition: dhcp_proto.h:151
void StartLeaseFileCleanupTimer()
Definition: dhcp_proto.cc:253
void IncrStatsDiscover()
Definition: dhcp_proto.h:102
DBTableBase::ListenerId vnid_
Definition: dhcp_proto.h:141
std::pair< Interface *, DhcpLeaseDb * > LeaseManagerPair
Definition: dhcp_proto.h:34
bool LeaseFileCleanupExpiry(uint32_t seqno)
Definition: dhcp_proto.cc:261
DBTableBase::ListenerId iid_
Definition: dhcp_proto.h:140
void IncrStatsAcks()
Definition: dhcp_proto.h:109
void VnNotify(DBEntryBase *entry)
Definition: dhcp_proto.cc:216
void IncrStatsRelayResps()
Definition: dhcp_proto.h:112
bool run_with_vrouter_
Definition: dhcp_proto.h:134
uint32_t pkt_interface_index() const
Definition: dhcp_proto.h:88
void set_dhcp_relay_mode(bool mode)
Definition: dhcp_proto.h:78
std::map< Interface *, DhcpLeaseDb * > LeaseManagerMap
Definition: dhcp_proto.h:33
void ReadHandler(const boost::system::error_code &error, std::size_t len)
Definition: dhcp_proto.cc:93
void set_ip_fabric_interface(Interface *itf)
Definition: dhcp_proto.h:81
void IncrUnknownMsgDrops()
Definition: dhcp_proto.h:114
void IncrStatsInform()
Definition: dhcp_proto.h:104
bool IsRunningWithVrouter() const
Definition: dhcp_proto.h:100
uint8_t * dhcp_server_read_buf_
Definition: dhcp_proto.h:146
void IncrStatsNacks()
Definition: dhcp_proto.h:110
@ DHCP_VHOST_MSG
Definition: dhcp_proto.h:30
void set_ip_fabric_interface_mac(const MacAddress &mac)
Definition: dhcp_proto.h:97
DhcpLeaseDb * GetLeaseDb(Interface *intrface)
Definition: dhcp_proto.cc:227
std::set< VmInterface * > gw_vmi_list_
Definition: dhcp_proto.h:148
void IncrStatsRequest()
Definition: dhcp_proto.h:103
boost::asio::ip::udp::endpoint remote_endpoint_
Definition: dhcp_proto.h:145
Interface * ip_fabric_interface() const
Definition: dhcp_proto.h:80
static const uint32_t kDhcpLeaseFileDeleteTimeout
Definition: dhcp_proto.h:27
void set_ip_fabric_interface_index(uint32_t ind)
Definition: dhcp_proto.h:85
void IncrStatsDecline()
Definition: dhcp_proto.h:105
DhcpProto(Agent *agent, boost::asio::io_context &io, bool run_with_vrouter)
Definition: dhcp_proto.cc:20
std::string GetLeaseFileName(const VmInterface *vmi)
Definition: dhcp_proto.cc:235
DhcpStats stats_
Definition: dhcp_proto.h:142
void IncrStatsErrors()
Definition: dhcp_proto.h:113
MacAddress ip_fabric_interface_mac_
Definition: dhcp_proto.h:139
void SendDhcpIpc(uint8_t *dhcp, std::size_t len)
Definition: dhcp_proto.cc:109
void set_pkt_interface_index(uint32_t ind)
Definition: dhcp_proto.h:91
uint32_t ip_fabric_interface_index() const
Definition: dhcp_proto.h:82
void CreateLeaseDb(VmInterface *vmi)
Definition: dhcp_proto.cc:159
Definition: timer.h:58
boost::asio::ip::udp boost_udp
Definition: dhcp_proto.h:21
Definition: io_utils.cc:12
uint32_t incorrect_mac
Definition: dhcp_proto.h:67
uint32_t unknown_msg_drop
Definition: dhcp_proto.h:65
uint32_t dhcp_disabled_drop
Definition: dhcp_proto.h:66
DhcpVhostMsg(uint8_t *dhcp, uint32_t length)
Definition: dhcp_proto.h:40