dhcp_client.cpp revision 103ba6ba
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"
19#include "vom/singular_db_funcs.hpp"
20
21namespace VOM {
22const dhcp_client::state_t dhcp_client::state_t::DISCOVER(0, "discover");
23const dhcp_client::state_t dhcp_client::state_t::REQUEST(1, "request");
24const dhcp_client::state_t dhcp_client::state_t::BOUND(2, "bound");
25
26dhcp_client::state_t::state_t(int v, const std::string& s)
27  : enum_base<dhcp_client::state_t>(v, s)
28{
29}
30
31const dhcp_client::state_t&
32dhcp_client::state_t::from_vpp(int n)
33{
34  if (REQUEST == n)
35    return (REQUEST);
36  if (BOUND == n)
37    return (BOUND);
38
39  return (DISCOVER);
40}
41
42singular_db<interface::key_t, dhcp_client> dhcp_client::m_db;
43std::weak_ptr<dhcp_client_cmds::events_cmd> dhcp_client::m_s_event_cmd;
44dhcp_client::dhcp_client_listener dhcp_client::m_listener;
45
46dhcp_client::event_handler dhcp_client::m_evh;
47
48dhcp_client::dhcp_client(const interface& itf,
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
64dhcp_client::dhcp_client(const interface& itf,
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
81dhcp_client::dhcp_client(const dhcp_client& o)
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
93dhcp_client::~dhcp_client()
94{
95  sweep();
96
97  // not in the DB anymore.
98  m_db.release(m_itf->key(), this);
99}
100
101bool
102dhcp_client::operator==(const dhcp_client& l) const
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
108const dhcp_client::key_t&
109dhcp_client::key() const
110{
111  return (m_itf->key());
112}
113
114void
115dhcp_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
124void
125dhcp_client::dump(std::ostream& os)
126{
127  db_dump(m_db, os);
128}
129
130void
131dhcp_client::replay()
132{
133  if (m_binding) {
134    HW::enqueue(new dhcp_client_cmds::bind_cmd(
135      m_binding, m_itf->handle(), m_hostname, m_client_id, false, m_dscp));
136  }
137}
138
139std::string
140dhcp_client::to_string() const
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
154void
155dhcp_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) {
161    HW::enqueue(new dhcp_client_cmds::bind_cmd(
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
172const std::shared_ptr<dhcp_client::lease_t>
173dhcp_client::lease() const
174{
175  return (m_lease);
176}
177
178void
179dhcp_client::lease(std::shared_ptr<dhcp_client::lease_t> lease)
180{
181  m_lease = lease;
182}
183
184std::shared_ptr<dhcp_client>
185dhcp_client::find_or_add(const dhcp_client& temp)
186{
187  return (m_db.find_or_add(temp.m_itf->key(), temp));
188}
189
190std::shared_ptr<dhcp_client>
191dhcp_client::find(const key_t& k)
192{
193  return (m_db.find(k));
194}
195
196std::shared_ptr<dhcp_client>
197dhcp_client::singular() const
198{
199  return find_or_add(*this);
200}
201
202dhcp_client::lease_t::lease_t()
203  : state(state_t::DISCOVER)
204  , mac(mac_address_t::ZERO)
205{
206}
207
208dhcp_client::lease_t::lease_t(const state_t& state,
209                              std::shared_ptr<interface> itf,
210                              const boost::asio::ip::address& router_address,
211                              const route::prefix_t& host_prefix,
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
223std::string
224dhcp_client::lease_t::to_string() const
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
235dhcp_client::event_listener::event_listener()
236  : m_status(rc_t::NOOP)
237{
238}
239
240HW::item<bool>&
241dhcp_client::event_listener::status()
242{
243  return (m_status);
244}
245
246dhcp_client::event_handler::event_handler()
247{
248  OM::register_listener(this);
249  inspect::register_handler({ "dhcp" }, "DHCP clients", this);
250}
251
252void
253dhcp_client::event_handler::handle_replay()
254{
255  m_db.replay();
256}
257
258void
259dhcp_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);
284    std::string hostname =
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
296dependency_t
297dhcp_client::event_handler::order() const
298{
299  return (dependency_t::BINDING);
300}
301
302void
303dhcp_client::event_handler::show(std::ostream& os)
304{
305  db_dump(m_db, os);
306}
307
308std::shared_ptr<dhcp_client_cmds::events_cmd>
309dhcp_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
326void
327dhcp_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
334void
335dhcp_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: ON
351 *
352 * Local Variables:
353 * eval: (c-set-style "mozilla")
354 * End:
355 */
356