FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
dhcp_client.hpp
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
16 #ifndef __VOM_DHCP_CLIENT_H__
17 #define __VOM_DHCP_CLIENT_H__
18 
19 #include "vom/hw.hpp"
20 #include "vom/inspect.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/object_base.hpp"
23 #include "vom/om.hpp"
24 #include "vom/prefix.hpp"
25 #include "vom/singular_db.hpp"
26 
27 namespace VOM {
28 namespace dhcp_client_cmds {
29 class events_cmd;
30 };
31 /**
32  * A representation of DHCP client on an interface
33  */
34 class dhcp_client : public object_base
35 {
36 public:
37  /**
38  * typedef for the DHCP client key type
39  */
41 
42  struct state_t : enum_base<state_t>
43  {
44  const static state_t DISCOVER;
45  const static state_t REQUEST;
46  const static state_t BOUND;
47 
48  static const state_t& from_vpp(int i);
49 
50  private:
51  /**
52  * Private constructor taking the value and the string name
53  */
54  state_t(int v, const std::string& s);
55  };
56 
57  /**
58  * A DHCP lease data
59  */
60  struct lease_t
61  {
62  lease_t();
63  lease_t(const state_t& state,
64  std::shared_ptr<interface> itf,
67  const std::string& hostname,
68  const mac_address_t& mac);
69 
70  std::string to_string() const;
71 
72  const state_t& state;
73  std::shared_ptr<interface> itf;
78  };
79 
80  /**
81  * A class that listens to DHCP Events
82  */
84  {
85  public:
86  /**
87  * Constructor
88  */
90 
91  /**
92  * listener's virtual function invoked when a DHCP event is
93  * available to read
94  */
95  virtual void handle_dhcp_event(std::shared_ptr<lease_t> e) = 0;
96 
97  /**
98  * Return the HW::item associated with this command
99  */
101 
102  protected:
103  /**
104  * The HW::item associated with this command
105  */
107  };
108 
109  /**
110  * Construct a new object matching the desried state
111  */
112  dhcp_client(const interface& itf,
113  const std::string& hostname,
114  bool set_broadcast_flag = true,
116  event_listener* ev = nullptr);
117 
118  /**
119  * Construct a new object matching the desried state
120  */
121  dhcp_client(const interface& itf,
122  const std::string& hostname,
123  const l2_address_t& client_id,
124  bool set_broadcast_flag = true,
126  event_listener* ev = nullptr);
127 
128  /**
129  * Copy Constructor
130  */
131  dhcp_client(const dhcp_client& o);
132 
133  /**
134  * Destructor
135  */
136  ~dhcp_client();
137 
138  /**
139  * Comparison operator - for UT
140  */
141  bool operator==(const dhcp_client& d) const;
142 
143  /**
144  * Return the object's key
145  */
146  const key_t& key() const;
147 
148  /**
149  * Return the 'singular' of the DHCP client that matches this object
150  */
151  std::shared_ptr<dhcp_client> singular() const;
152 
153  /**
154  * convert to string format for debug purposes
155  */
156  std::string to_string() const;
157 
158  /**
159  * Dump all DHCP clients into the stream provided
160  */
161  static void dump(std::ostream& os);
162 
163  /**
164  * Find a DHCP client from its key
165  */
166  static std::shared_ptr<dhcp_client> find(const key_t& k);
167 
168  /**
169  * return the current lease data
170  */
171  const std::shared_ptr<lease_t> lease() const;
172 
173 private:
174  /**
175  * Class definition for listeners to OM events
176  */
178  {
179  public:
180  event_handler();
181  virtual ~event_handler() = default;
182 
183  /**
184  * Handle a populate event
185  */
186  void handle_populate(const client_db::key_t& key);
187 
188  /**
189  * Handle a replay event
190  */
191  void handle_replay();
192 
193  /**
194  * Show the object in the Singular DB
195  */
196  void show(std::ostream& os);
197 
198  /**
199  * Get the sortable Id of the listener
200  */
201  dependency_t order() const;
202  };
203 
204  /**
205  * event_handler to register with OM
206  */
207  static event_handler m_evh;
208 
209  /**
210  * Enquue commonds to the VPP command Q for the update
211  */
212  void update(const dhcp_client& obj);
213 
214  /**
215  * Find or add DHCP client to the OM
216  */
217  static std::shared_ptr<dhcp_client> find_or_add(const dhcp_client& temp);
218 
219  /*
220  * It's the OM class that calls singular()
221  */
222  friend class OM;
223 
224  /**
225  * It's the singular_db class that calls replay()
226  */
227  friend class singular_db<key_t, dhcp_client>;
228 
229  /**
230  * Sweep/reap the object if still stale
231  */
232  void sweep(void);
233 
234  /**
235  * replay the object to create it in hardware
236  */
237  void replay(void);
238 
239  void lease(std::shared_ptr<lease_t> l);
240 
241  /**
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
245  */
246  const std::shared_ptr<interface> m_itf;
247 
248  /**
249  * The hostname in the DHCP client
250  */
251  const std::string m_hostname;
252 
253  /**
254  * The option-61 client_id in the DHCP client
255  */
256  const l2_address_t m_client_id;
257 
258  /**
259  * Flag to control the setting the of DHCP discover's broadcast flag
260  */
261  const bool m_set_broadcast_flag;
262 
263  /**
264  * DSCP setting for generated IP packets
265  */
266  const ip_dscp_t m_dscp;
267 
268  /**
269  * HW configuration for the binding. The bool representing the
270  * do/don't bind.
271  */
272  HW::item<bool> m_binding;
273 
274  /**
275  * A pointer to an event listener for client events
276  */
277  event_listener* m_evl;
278 
279  /**
280  * Current lease state for this client
281  */
282  std::shared_ptr<lease_t> m_lease;
283 
284  std::shared_ptr<dhcp_client_cmds::events_cmd> m_event_cmd;
285 
286  void handle_dhcp_event(std::shared_ptr<lease_t> e);
287 
288  /**
289  * A map of all Dhcp clients keyed against the interface.
290  */
292 
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();
295 
296  class dhcp_client_listener : public event_listener
297  {
298  public:
299  /**
300  * listener's virtual function invoked when a DHCP event is
301  * available to read
302  */
303  void handle_dhcp_event(std::shared_ptr<lease_t> e);
304  };
305  static dhcp_client_listener m_listener;
306 };
307 };
308 
309 /*
310  * fd.io coding-style-patch-verification: OFF
311  *
312  * Local Variables:
313  * eval: (c-set-style "mozilla")
314  * End:
315  */
316 
317 #endif
VOM::dhcp_client::event_listener::m_status
HW::item< bool > m_status
The HW::item associated with this command.
Definition: dhcp_client.hpp:106
VOM::route::prefix_t
A prefix defintion.
Definition: prefix.hpp:131
VOM::HW::item< bool >
VOM::dhcp_client::event_listener::event_listener
event_listener()
Constructor.
Definition: dhcp_client.cpp:235
VOM::dhcp_client::state_t::REQUEST
const static state_t REQUEST
Definition: dhcp_client.hpp:45
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::dependency_t
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
om.hpp
VOM::dhcp_client::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: dhcp_client.cpp:140
VOM::OM
The interface to writing objects into VPP OM.
Definition: om.hpp:140
VOM::dhcp_client::key_t
interface::key_t key_t
typedef for the DHCP client key type
Definition: dhcp_client.hpp:40
VOM::dhcp_client::state_t::DISCOVER
const static state_t DISCOVER
Definition: dhcp_client.hpp:44
VOM::dhcp_client::find
static std::shared_ptr< dhcp_client > find(const key_t &k)
Find a DHCP client from its key.
Definition: dhcp_client.cpp:191
VOM::inspect::command_handler
inspect command handler Handler
Definition: inspect.hpp:54
VOM::dhcp_client::dhcp_client
dhcp_client(const interface &itf, const std::string &hostname, bool set_broadcast_flag=true, const ip_dscp_t &dscp=ip_dscp_t::DSCP_CS0, event_listener *ev=nullptr)
Construct a new object matching the desried state.
Definition: dhcp_client.cpp:48
VOM::dhcp_client::lease_t::router_address
boost::asio::ip::address router_address
Definition: dhcp_client.hpp:74
VOM::dhcp_client::~dhcp_client
~dhcp_client()
Destructor.
Definition: dhcp_client.cpp:93
VOM::dhcp_client::dump
static void dump(std::ostream &os)
Dump all DHCP clients into the stream provided.
Definition: dhcp_client.cpp:125
VOM::dhcp_client_cmds::events_cmd
A functor class represents our desire to recieve interface events.
Definition: dhcp_client_cmds.hpp:128
VOM::OM::listener
Class definition for listeners to OM events.
Definition: om.hpp:284
VOM::dhcp_client::lease_t::itf
std::shared_ptr< interface > itf
Definition: dhcp_client.hpp:73
VOM::dhcp_client::operator==
bool operator==(const dhcp_client &d) const
Comparison operator - for UT.
Definition: dhcp_client.cpp:102
VOM::dhcp_client::lease_t::mac
mac_address_t mac
Definition: dhcp_client.hpp:77
hostname
string hostname[64]
Definition: dhcp.api:159
VOM::dhcp_client::state_t
Definition: dhcp_client.hpp:42
VOM::dhcp_client::lease_t
A DHCP lease data.
Definition: dhcp_client.hpp:60
VOM::dhcp_client
A representation of DHCP client on an interface.
Definition: dhcp_client.hpp:34
VOM::dhcp_client::lease_t::to_string
std::string to_string() const
Definition: dhcp_client.cpp:224
VOM::dhcp_client::event_listener::handle_dhcp_event
virtual void handle_dhcp_event(std::shared_ptr< lease_t > e)=0
listener's virtual function invoked when a DHCP event is available to read
VOM::enum_base
A template base class for all enum types.
Definition: enum_base.hpp:30
VOM::dhcp_client::lease_t::lease_t
lease_t()
Definition: dhcp_client.cpp:202
VOM::interface
A representation of an interface in VPP.
Definition: interface.hpp:41
hw.hpp
VOM::dhcp_client::lease_t::host_prefix
route::prefix_t host_prefix
Definition: dhcp_client.hpp:75
VOM::client_db::key_t
const typedef std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
VOM::dhcp_client::lease
const std::shared_ptr< lease_t > lease() const
return the current lease data
Definition: dhcp_client.cpp:173
address
manual_print typedef address
Definition: ip_types.api:96
VOM::dhcp_client::state_t::from_vpp
static const state_t & from_vpp(int i)
Definition: dhcp_client.cpp:32
VOM::dhcp_client::event_listener
A class that listens to DHCP Events.
Definition: dhcp_client.hpp:83
VOM::l2_address_t
Type def of a L2 address as read from VPP.
Definition: types.hpp:339
VOM::dhcp_client::key
const key_t & key() const
Return the object's key.
Definition: dhcp_client.cpp:109
VOM::dhcp_client::lease_t::hostname
std::string hostname
Definition: dhcp_client.hpp:76
inspect.hpp
singular_db.hpp
prefix.hpp
VOM::dhcp_client::event_listener::status
HW::item< bool > & status()
Return the HW::item associated with this command.
Definition: dhcp_client.cpp:241
VOM::mac_address_t
Type def of a Ethernet address.
Definition: types.hpp:295
VOM::dhcp_client::state_t::BOUND
const static state_t BOUND
Definition: dhcp_client.hpp:46
VOM::ip_dscp_t
IP DSCP values.
Definition: prefix.hpp:81
interface.hpp
VOM::dhcp_client::singular
std::shared_ptr< dhcp_client > singular() const
Return the 'singular' of the DHCP client that matches this object.
Definition: dhcp_client.cpp:197
VOM::ip_dscp_t::DSCP_CS0
const static ip_dscp_t DSCP_CS0
Definition: prefix.hpp:86
VOM::singular_db
A Database to store the unique 'singular' instances of a single object type.
Definition: singular_db.hpp:33
i
int i
Definition: flowhash_template.h:376
set_broadcast_flag
bool set_broadcast_flag
Definition: dhcp.api:162
VOM::interface::key_t
std::string key_t
The key for interface's key.
Definition: interface.hpp:56
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
VOM::dhcp_client::lease_t::state
const state_t & state
Definition: dhcp_client.hpp:72
VOM::object_base
A base class for all object_base in the VPP object_base-Model.
Definition: object_base.hpp:29
object_base.hpp
dscp
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
string
const char *const string
Definition: cJSON.h:172