2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef __VOM_DHCP_CLIENT_H__
17 #define __VOM_DHCP_CLIENT_H__
20 #include "vom/inspect.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/object_base.hpp"
24 #include "vom/prefix.hpp"
25 #include "vom/singular_db.hpp"
28 namespace dhcp_client_cmds {
32 * A representation of DHCP client on an interface
34 class dhcp_client : public object_base
38 * typedef for the DHCP client key type
40 typedef interface::key_t key_t;
42 struct state_t : enum_base<state_t>
44 const static state_t DISCOVER;
45 const static state_t REQUEST;
46 const static state_t BOUND;
48 static const state_t& from_vpp(int i);
52 * Private constructor taking the value and the string name
54 state_t(int v, const std::string& s);
63 lease_t(const state_t& state,
64 std::shared_ptr<interface> itf,
65 const boost::asio::ip::address& router_address,
66 const route::prefix_t& host_prefix,
67 const std::string& hostname,
68 const mac_address_t& mac);
70 std::string to_string() const;
73 std::shared_ptr<interface> itf;
74 boost::asio::ip::address router_address;
75 route::prefix_t host_prefix;
81 * A class that listens to DHCP Events
92 * listener's virtual function invoked when a DHCP event is
95 virtual void handle_dhcp_event(std::shared_ptr<lease_t> e) = 0;
98 * Return the HW::item associated with this command
100 HW::item<bool>& status();
104 * The HW::item associated with this command
106 HW::item<bool> m_status;
110 * Construct a new object matching the desried state
112 dhcp_client(const interface& itf,
113 const std::string& hostname,
114 bool set_broadcast_flag = true,
115 const ip_dscp_t& dscp = ip_dscp_t::DSCP_CS0,
116 event_listener* ev = nullptr);
119 * Construct a new object matching the desried state
121 dhcp_client(const interface& itf,
122 const std::string& hostname,
123 const l2_address_t& client_id,
124 bool set_broadcast_flag = true,
125 const ip_dscp_t& dscp = ip_dscp_t::DSCP_CS0,
126 event_listener* ev = nullptr);
131 dhcp_client(const dhcp_client& o);
139 * Comparison operator - for UT
141 bool operator==(const dhcp_client& d) const;
144 * Return the object's key
146 const key_t& key() const;
149 * Return the 'singular' of the DHCP client that matches this object
151 std::shared_ptr<dhcp_client> singular() const;
154 * convert to string format for debug purposes
156 std::string to_string() const;
159 * Dump all DHCP clients into the stream provided
161 static void dump(std::ostream& os);
164 * Find a DHCP client from its key
166 static std::shared_ptr<dhcp_client> find(const key_t& k);
169 * return the current lease data
171 const std::shared_ptr<lease_t> lease() const;
175 * Class definition for listeners to OM events
177 class event_handler : public OM::listener, public inspect::command_handler
181 virtual ~event_handler() = default;
184 * Handle a populate event
186 void handle_populate(const client_db::key_t& key);
189 * Handle a replay event
191 void handle_replay();
194 * Show the object in the Singular DB
196 void show(std::ostream& os);
199 * Get the sortable Id of the listener
201 dependency_t order() const;
205 * event_handler to register with OM
207 static event_handler m_evh;
210 * Enquue commonds to the VPP command Q for the update
212 void update(const dhcp_client& obj);
215 * Find or add DHCP client to the OM
217 static std::shared_ptr<dhcp_client> find_or_add(const dhcp_client& temp);
220 * It's the OM class that calls singular()
225 * It's the singular_db class that calls replay()
227 friend class singular_db<key_t, dhcp_client>;
230 * Sweep/reap the object if still stale
235 * replay the object to create it in hardware
239 void lease(std::shared_ptr<lease_t> l);
242 * A reference counting pointer to the interface on which DHCP client
243 * resides. By holding the reference here, we can guarantee that
244 * this object will outlive the interface
246 const std::shared_ptr<interface> m_itf;
249 * The hostname in the DHCP client
251 const std::string m_hostname;
254 * The option-61 client_id in the DHCP client
256 const l2_address_t m_client_id;
259 * Flag to control the setting the of DHCP discover's broadcast flag
261 const bool m_set_broadcast_flag;
264 * DSCP setting for generated IP packets
266 const ip_dscp_t m_dscp;
269 * HW configuration for the binding. The bool representing the
272 HW::item<bool> m_binding;
275 * A pointer to an event listener for client events
277 event_listener* m_evl;
280 * Current lease state for this client
282 std::shared_ptr<lease_t> m_lease;
284 std::shared_ptr<dhcp_client_cmds::events_cmd> m_event_cmd;
286 void handle_dhcp_event(std::shared_ptr<lease_t> e);
289 * A map of all Dhcp clients keyed against the interface.
291 static singular_db<key_t, dhcp_client> m_db;
293 static std::weak_ptr<dhcp_client_cmds::events_cmd> m_s_event_cmd;
294 static std::shared_ptr<dhcp_client_cmds::events_cmd> get_event_cmd();
296 class dhcp_client_listener : public event_listener
300 * listener's virtual function invoked when a DHCP event is
303 void handle_dhcp_event(std::shared_ptr<lease_t> e);
305 static dhcp_client_listener m_listener;
310 * fd.io coding-style-patch-verification: ON
313 * eval: (c-set-style "mozilla")