Line data Source code
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 { 30 : DHCP_VHOST_MSG, 31 : }; 32 : 33 : typedef std::map<Interface *, DhcpLeaseDb *> LeaseManagerMap; 34 : typedef std::pair<Interface *, DhcpLeaseDb *> LeaseManagerPair; 35 : 36 : struct DhcpVhostMsg : InterTaskMsg { 37 : uint8_t *pkt; 38 : uint32_t len; 39 : 40 0 : DhcpVhostMsg(uint8_t *dhcp, uint32_t length) 41 0 : : InterTaskMsg(DHCP_VHOST_MSG), pkt(dhcp), len(length) {} 42 0 : virtual ~DhcpVhostMsg() { if (pkt) delete [] pkt; } 43 : }; 44 : 45 : struct DhcpStats { 46 41 : DhcpStats() { Reset(); } 47 134 : void Reset() { 48 134 : discover = request = inform = decline = release = other = 49 134 : offers = acks = nacks = errors = relay_req = relay_resp = 50 134 : unknown_msg_drop = dhcp_disabled_drop = incorrect_mac = 0; 51 134 : } 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; 66 : uint32_t dhcp_disabled_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 0 : bool dhcp_relay_mode() const { return dhcp_relay_mode_; } 78 2 : void set_dhcp_relay_mode(bool mode) { dhcp_relay_mode_ = mode; } 79 : 80 : Interface *ip_fabric_interface() const { return ip_fabric_interface_; } 81 4 : void set_ip_fabric_interface(Interface *itf) { ip_fabric_interface_ = itf; } 82 0 : uint32_t ip_fabric_interface_index() const { 83 0 : return ip_fabric_interface_index_; 84 : } 85 4 : void set_ip_fabric_interface_index(uint32_t ind) { 86 4 : ip_fabric_interface_index_ = ind; 87 4 : } 88 0 : uint32_t pkt_interface_index() const { 89 0 : return pkt_interface_index_; 90 : } 91 4 : void set_pkt_interface_index(uint32_t ind) { 92 4 : pkt_interface_index_ = ind; 93 4 : } 94 0 : const MacAddress &ip_fabric_interface_mac() const { 95 0 : return ip_fabric_interface_mac_; 96 : } 97 2 : void set_ip_fabric_interface_mac(const MacAddress &mac) { 98 2 : ip_fabric_interface_mac_ = mac; 99 2 : } 100 : bool IsRunningWithVrouter() const { return run_with_vrouter_; } 101 : 102 0 : void IncrStatsDiscover() { stats_.discover++; } 103 0 : void IncrStatsRequest() { stats_.request++; } 104 0 : void IncrStatsInform() { stats_.inform++; } 105 0 : void IncrStatsDecline() { stats_.decline++; } 106 0 : void IncrStatsRelease() { stats_.release++; } 107 0 : void IncrStatsOther() { stats_.other++; } 108 0 : void IncrStatsOffers() { stats_.offers++; } 109 0 : void IncrStatsAcks() { stats_.acks++; } 110 0 : void IncrStatsNacks() { stats_.nacks++; } 111 0 : void IncrStatsRelayReqs() { stats_.relay_req++; } 112 0 : void IncrStatsRelayResps() { stats_.relay_resp++; } 113 0 : void IncrStatsErrors() { stats_.errors++; } 114 0 : void IncrUnknownMsgDrops() { stats_.unknown_msg_drop++; } 115 0 : void IncrDhcpDisabledDrops() { stats_.dhcp_disabled_drop++; } 116 0 : void IncrIncorrectMac() { stats_.incorrect_mac++; } 117 78 : const DhcpStats &GetStats() const { return stats_; } 118 93 : void ClearStats() { stats_.Reset(); } 119 : 120 : void CreateLeaseDb(VmInterface *vmi); 121 : void DeleteLeaseDb(VmInterface *vmi); 122 : DhcpLeaseDb *GetLeaseDb(Interface *intrface); 123 0 : 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); 131 : void StartLeaseFileCleanupTimer(); 132 : bool LeaseFileCleanupExpiry(uint32_t seqno); 133 : 134 : bool run_with_vrouter_; 135 : bool dhcp_relay_mode_; 136 : Interface *ip_fabric_interface_; 137 : uint32_t ip_fabric_interface_index_; 138 : uint32_t pkt_interface_index_; 139 : MacAddress ip_fabric_interface_mac_; 140 : DBTableBase::ListenerId iid_; 141 : DBTableBase::ListenerId vnid_; 142 : DhcpStats stats_; 143 : 144 : boost::asio::ip::udp::socket dhcp_server_socket_; 145 : boost::asio::ip::udp::endpoint remote_endpoint_; 146 : uint8_t *dhcp_server_read_buf_; 147 : 148 : std::set<VmInterface *> gw_vmi_list_; 149 : LeaseManagerMap lease_manager_; 150 : uint32_t gateway_delete_seqno_; 151 : Timer *lease_file_cleanup_timer_; 152 : 153 : DISALLOW_COPY_AND_ASSIGN(DhcpProto); 154 : }; 155 : 156 : #endif // vnsw_agent_dhcp_proto_hpp