fcadfa69ded2190f52bef3702bc358ae6763abb4
[vpp.git] / extras / vom / vom / dhcp_client.cpp
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/singular_db_funcs.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&
31 dhcp_client::state_t::from_vpp(int n)
32 {
33   if (REQUEST == n)
34     return (REQUEST);
35   if (BOUND == n)
36     return (BOUND);
37
38   return (DISCOVER);
39 }
40
41 singular_db<interface::key_t, dhcp_client> dhcp_client::m_db;
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
47 dhcp_client::dhcp_client(const interface& itf,
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
61 dhcp_client::dhcp_client(const interface& itf,
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
76 dhcp_client::dhcp_client(const dhcp_client& o)
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
87 dhcp_client::~dhcp_client()
88 {
89   sweep();
90
91   // not in the DB anymore.
92   m_db.release(m_itf->key(), this);
93 }
94
95 bool
96 dhcp_client::operator==(const dhcp_client& l) const
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&
103 dhcp_client::key() const
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
134 dhcp_client::to_string() const
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>
166 dhcp_client::lease() const
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>
184 dhcp_client::find(const key_t& k)
185 {
186   return (m_db.find(k));
187 }
188
189 std::shared_ptr<dhcp_client>
190 dhcp_client::singular() const
191 {
192   return find_or_add(*this);
193 }
194
195 dhcp_client::lease_t::lease_t()
196   : state(state_t::DISCOVER)
197   , mac(mac_address_t::ZERO)
198 {
199 }
200
201 dhcp_client::lease_t::lease_t(const state_t& state,
202                               std::shared_ptr<interface> itf,
203                               const boost::asio::ip::address& router_address,
204                               const route::prefix_t& host_prefix,
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
217 dhcp_client::lease_t::to_string() const
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
228 dhcp_client::event_listener::event_listener()
229   : m_status(rc_t::NOOP)
230 {
231 }
232
233 HW::item<bool>&
234 dhcp_client::event_listener::status()
235 {
236   return (m_status);
237 }
238
239 dhcp_client::event_handler::event_handler()
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
287 dependency_t
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  */