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