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