FD.io VPP  v21.01.1
Vector Packet Processing
dhcp_client.cpp
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 #include "vom/dhcp_client.hpp"
17 #include "vom/dhcp_client_cmds.hpp"
18 #include "vom/route_api_types.hpp"
20 
21 namespace VOM {
22 const dhcp_client::state_t dhcp_client::state_t::DISCOVER(0, "discover");
23 const dhcp_client::state_t dhcp_client::state_t::REQUEST(1, "request");
24 const dhcp_client::state_t dhcp_client::state_t::BOUND(2, "bound");
25 
26 dhcp_client::state_t::state_t(int v, const std::string& s)
27  : enum_base<dhcp_client::state_t>(v, s)
28 {
29 }
30 
31 const dhcp_client::state_t&
33 {
34  if (REQUEST == n)
35  return (REQUEST);
36  if (BOUND == n)
37  return (BOUND);
38 
39  return (DISCOVER);
40 }
41 
43 std::weak_ptr<dhcp_client_cmds::events_cmd> dhcp_client::m_s_event_cmd;
44 dhcp_client::dhcp_client_listener dhcp_client::m_listener;
45 
46 dhcp_client::event_handler dhcp_client::m_evh;
47 
49  const std::string& hostname,
50  bool set_broadcast_flag,
51  const ip_dscp_t& dscp,
52  event_listener* ev)
53  : m_itf(itf.singular())
54  , m_hostname(hostname)
55  , m_client_id(l2_address_t::ZERO)
56  , m_set_broadcast_flag(set_broadcast_flag)
57  , m_dscp(dscp)
58  , m_binding(0)
59  , m_evl(ev)
60  , m_event_cmd(get_event_cmd())
61 {
62 }
63 
65  const std::string& hostname,
66  const l2_address_t& client_id,
67  bool set_broadcast_flag,
68  const ip_dscp_t& dscp,
69  event_listener* ev)
70  : m_itf(itf.singular())
71  , m_hostname(hostname)
72  , m_client_id(client_id)
73  , m_set_broadcast_flag(set_broadcast_flag)
74  , m_dscp(dscp)
75  , m_binding(0)
76  , m_evl(ev)
77  , m_event_cmd(get_event_cmd())
78 {
79 }
80 
82  : m_itf(o.m_itf)
83  , m_hostname(o.m_hostname)
84  , m_client_id(o.m_client_id)
85  , m_set_broadcast_flag(o.m_set_broadcast_flag)
86  , m_dscp(o.m_dscp)
87  , m_binding(0)
88  , m_evl(o.m_evl)
89  , m_event_cmd(o.m_event_cmd)
90 {
91 }
92 
94 {
95  sweep();
96 
97  // not in the DB anymore.
98  m_db.release(m_itf->key(), this);
99 }
100 
101 bool
103 {
104  return ((key() == l.key()) && (m_hostname == l.m_hostname) &&
105  (m_client_id == l.m_client_id && m_dscp == l.m_dscp));
106 }
107 
108 const dhcp_client::key_t&
110 {
111  return (m_itf->key());
112 }
113 
114 void
115 dhcp_client::sweep()
116 {
117  if (m_binding) {
118  HW::enqueue(
119  new dhcp_client_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
120  }
121  HW::write();
122 }
123 
124 void
125 dhcp_client::dump(std::ostream& os)
126 {
127  db_dump(m_db, os);
128 }
129 
130 void
131 dhcp_client::replay()
132 {
133  if (m_binding) {
135  m_binding, m_itf->handle(), m_hostname, m_client_id, false, m_dscp));
136  }
137 }
138 
141 {
142  std::ostringstream s;
143  s << "DHCP-client: " << m_itf->to_string() << " hostname:" << m_hostname
144  << " client_id:[" << m_client_id << "] "
145  << "dscp:" << m_dscp.to_string() << " " << m_binding.to_string();
146  if (m_lease)
147  s << " " << m_lease->to_string();
148  else
149  s << " no-lease";
150 
151  return (s.str());
152 }
153 
154 void
155 dhcp_client::update(const dhcp_client& desired)
156 {
157  /*
158  * the desired state is always that the interface should be created
159  */
160  if (!m_binding) {
162  m_binding, m_itf->handle(), m_hostname, m_client_id, false, m_dscp));
163  }
164 
165  if (desired.m_lease)
166  m_lease = desired.m_lease;
167  if (m_evl != desired.m_evl) {
168  m_evl = desired.m_evl;
169  }
170 }
171 
172 const std::shared_ptr<dhcp_client::lease_t>
174 {
175  return (m_lease);
176 }
177 
178 void
179 dhcp_client::lease(std::shared_ptr<dhcp_client::lease_t> lease)
180 {
181  m_lease = lease;
182 }
183 
184 std::shared_ptr<dhcp_client>
185 dhcp_client::find_or_add(const dhcp_client& temp)
186 {
187  return (m_db.find_or_add(temp.m_itf->key(), temp));
188 }
189 
190 std::shared_ptr<dhcp_client>
192 {
193  return (m_db.find(k));
194 }
195 
196 std::shared_ptr<dhcp_client>
198 {
199  return find_or_add(*this);
200 }
201 
203  : state(state_t::DISCOVER)
204  , mac(mac_address_t::ZERO)
205 {
206 }
207 
209  std::shared_ptr<interface> itf,
212  const std::string& hostname,
213  const mac_address_t& mac)
214  : state(state)
215  , itf(itf)
216  , router_address(router_address)
217  , host_prefix(host_prefix)
218  , hostname(hostname)
219  , mac(mac)
220 {
221 }
222 
225 {
226  std::stringstream ss;
227 
228  ss << "lease:[" << itf->to_string() << " state: " << state.to_string()
229  << " host: " << host_prefix.to_string() << " router: " << router_address
230  << " mac: " << mac.to_string() << "]";
231 
232  return (ss.str());
233 }
234 
236  : m_status(rc_t::NOOP)
237 {
238 }
239 
242 {
243  return (m_status);
244 }
245 
247 {
248  OM::register_listener(this);
249  inspect::register_handler({ "dhcp" }, "DHCP clients", this);
250 }
251 
252 void
253 dhcp_client::event_handler::handle_replay()
254 {
255  m_db.replay();
256 }
257 
258 void
259 dhcp_client::event_handler::handle_populate(const client_db::key_t& key)
260 {
261  std::shared_ptr<dhcp_client_cmds::dump_cmd> cmd =
262  std::make_shared<dhcp_client_cmds::dump_cmd>();
263 
264  HW::enqueue(cmd);
265  HW::write();
266 
267  for (auto& record : *cmd) {
268  auto& payload = record.get_payload();
269 
270  std::shared_ptr<interface> itf =
271  interface::find(payload.client.sw_if_index);
272 
273  if (!itf) {
274  VOM_LOG(log_level_t::ERROR) << "dhcp-client dump:"
275  << " itf:" << payload.client.sw_if_index;
276  continue;
277  }
278 
279  const dhcp_client::state_t& s =
280  dhcp_client::state_t::from_vpp(payload.lease.state);
281  route::prefix_t pfx(payload.lease.is_ipv6,
282  (uint8_t*)&payload.lease.host_address.un,
283  payload.lease.mask_width);
285  reinterpret_cast<const char*>(payload.lease.hostname);
286  l2_address_t l2(payload.client.id + 1);
287  dhcp_client dc(*itf, hostname, l2, payload.client.set_broadcast_flag,
288  from_api(payload.client.dscp));
289  dc.lease(std::make_shared<dhcp_client::lease_t>(
290  s, itf, from_bytes(0, (uint8_t*)&payload.lease.router_address.un), pfx,
291  hostname, mac_address_t(payload.lease.host_mac)));
292  OM::commit(key, dc);
293  }
294 }
295 
297 dhcp_client::event_handler::order() const
298 {
299  return (dependency_t::BINDING);
300 }
301 
302 void
303 dhcp_client::event_handler::show(std::ostream& os)
304 {
305  db_dump(m_db, os);
306 }
307 
308 std::shared_ptr<dhcp_client_cmds::events_cmd>
309 dhcp_client::get_event_cmd()
310 {
311  if (m_s_event_cmd.expired()) {
312  std::shared_ptr<dhcp_client_cmds::events_cmd> c =
313  std::make_shared<dhcp_client_cmds::events_cmd>(m_listener);
314 
315  m_s_event_cmd = c;
316 
317  HW::enqueue(c);
318  HW::write();
319 
320  return c;
321  }
322 
323  return (m_s_event_cmd.lock());
324 }
325 
326 void
327 dhcp_client::handle_dhcp_event(std::shared_ptr<lease_t> lease)
328 {
329  m_lease = lease;
330  if (m_evl)
331  m_evl->handle_dhcp_event(m_lease);
332 }
333 
334 void
335 dhcp_client::dhcp_client_listener::handle_dhcp_event(std::shared_ptr<lease_t> e)
336 {
337  /*
338  * Find the client the event references
339  */
340  std::shared_ptr<dhcp_client> client = find(e->itf->key());
341 
342  if (client) {
343  client->handle_dhcp_event(e);
344  }
345 }
346 
347 }; // namespace VOM
348 
349 /*
350  * fd.io coding-style-patch-verification: OFF
351  *
352  * Local Variables:
353  * eval: (c-set-style "mozilla")
354  * End:
355  */
string hostname[64]
Definition: dhcp.api:159
HW::item< bool > m_status
The HW::item associated with this command.
#define VOM_LOG(lvl)
Definition: logger.hpp:181
vl_api_mac_address_t mac
Definition: l2.api:502
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:214
const 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
static std::shared_ptr< interface > find(const handle_t &h)
The the singular instance of the interface in the DB by handle.
Definition: interface.cpp:538
const char *const string
Definition: cJSON.h:172
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static const state_t REQUEST
Definition: dhcp_client.hpp:45
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
A representation of DHCP client on an interface.
Definition: dhcp_client.hpp:34
A class that listens to DHCP Events.
Definition: dhcp_client.hpp:83
Type def of a L2 address as read from VPP.
Definition: types.hpp:339
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
const key_t & key() const
Return the object&#39;s key.
typedef dhcp_client
DHCP Client config data.
Definition: dhcp.api:157
route::prefix_t host_prefix
Definition: dhcp_client.hpp:75
std::shared_ptr< dhcp_client > singular() const
Return the &#39;singular&#39; of the DHCP client that matches this object.
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
std::string to_string() const
static const state_t BOUND
Definition: dhcp_client.hpp:46
static const state_t DISCOVER
Definition: dhcp_client.hpp:44
bool set_broadcast_flag
Definition: dhcp.api:162
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:33
boost::asio::ip::address router_address
Definition: dhcp_client.hpp:74
interface::key_t key_t
typedef for the DHCP client key type
Definition: dhcp_client.hpp:40
svmdb_client_t * c
IP DSCP values.
Definition: prefix.hpp:81
A representation of an interface in VPP.
Definition: interface.hpp:41
std::shared_ptr< interface > itf
Definition: dhcp_client.hpp:73
static const state_t & from_vpp(int i)
Definition: dhcp_client.cpp:32
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
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
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
void event_handler(void *tls_async)
Definition: tls_async.c:334
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
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
static void dump(std::ostream &os)
Dump all DHCP clients into the stream provided.
manual_print typedef address
Definition: ip_types.api:96
~dhcp_client()
Destructor.
Definition: dhcp_client.cpp:93
std::string to_string() const
convert to string format for debug purposes
boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:224
bool operator==(const dhcp_client &d) const
Comparison operator - for UT.
static const log_level_t ERROR
Definition: logger.hpp:29
Then L2/objects that bind to interfaces, BD, ACLS, etc.
std::string to_string() const
String conversion.
Definition: types.cpp:148
HW::item< bool > & status()
Return the HW::item associated with this command.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
A representation of a method call to VPP.
Definition: cmd.hpp:32
static std::shared_ptr< dhcp_client > find(const key_t &k)
Find a DHCP client from its key.
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
Type def of a Ethernet address.
Definition: types.hpp:295
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
A prefix defintion.
Definition: prefix.hpp:131
A cmd class that Unbinds Dhcp Config from an interface.
A command class that binds the DHCP config to the interface.
const std::shared_ptr< lease_t > lease() const
return the current lease data