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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/dhcp_client.hpp"
17 #include "vom/dhcp_client_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
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");
25 dhcp_client::state_t::state_t(int v, const std::string& s)
26 : enum_base<dhcp_client::state_t>(v, s)
30 const dhcp_client::state_t&
31 dhcp_client::state_t::from_vpp(int n)
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;
45 dhcp_client::event_handler dhcp_client::m_evh;
47 dhcp_client::dhcp_client(const interface& itf,
48 const std::string& hostname,
49 bool set_broadcast_flag,
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)
57 , m_event_cmd(get_event_cmd())
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,
66 : m_itf(itf.singular())
67 , m_hostname(hostname)
68 , m_client_id(client_id)
69 , m_set_broadcast_flag(set_broadcast_flag)
72 , m_event_cmd(get_event_cmd())
76 dhcp_client::dhcp_client(const dhcp_client& o)
78 , m_hostname(o.m_hostname)
79 , m_client_id(o.m_client_id)
80 , m_set_broadcast_flag(o.m_set_broadcast_flag)
83 , m_event_cmd(o.m_event_cmd)
87 dhcp_client::~dhcp_client()
91 // not in the DB anymore.
92 m_db.release(m_itf->key(), this);
96 dhcp_client::operator==(const dhcp_client& l) const
98 return ((key() == l.key()) && (m_hostname == l.m_hostname) &&
99 (m_client_id == l.m_client_id));
102 const dhcp_client::key_t&
103 dhcp_client::key() const
105 return (m_itf->key());
113 new dhcp_client_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
119 dhcp_client::dump(std::ostream& os)
125 dhcp_client::replay()
128 HW::enqueue(new dhcp_client_cmds::bind_cmd(m_binding, m_itf->handle(),
129 m_hostname, m_client_id));
134 dhcp_client::to_string() const
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();
140 s << " " << m_lease->to_string();
148 dhcp_client::update(const dhcp_client& desired)
151 * the desired state is always that the interface should be created
154 HW::enqueue(new dhcp_client_cmds::bind_cmd(m_binding, m_itf->handle(),
155 m_hostname, m_client_id));
159 m_lease = desired.m_lease;
160 if (m_evl != desired.m_evl) {
161 m_evl = desired.m_evl;
165 const std::shared_ptr<dhcp_client::lease_t>
166 dhcp_client::lease() const
172 dhcp_client::lease(std::shared_ptr<dhcp_client::lease_t> lease)
177 std::shared_ptr<dhcp_client>
178 dhcp_client::find_or_add(const dhcp_client& temp)
180 return (m_db.find_or_add(temp.m_itf->key(), temp));
183 std::shared_ptr<dhcp_client>
184 dhcp_client::find(const key_t& k)
186 return (m_db.find(k));
189 std::shared_ptr<dhcp_client>
190 dhcp_client::singular() const
192 return find_or_add(*this);
195 dhcp_client::lease_t::lease_t()
196 : state(state_t::DISCOVER)
197 , mac(mac_address_t::ZERO)
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)
209 , router_address(router_address)
210 , host_prefix(host_prefix)
217 dhcp_client::lease_t::to_string() const
219 std::stringstream ss;
221 ss << "lease:[" << itf->to_string() << " state: " << state.to_string()
222 << " host: " << host_prefix.to_string() << " router: " << router_address
223 << " mac: " << mac.to_string() << "]";
228 dhcp_client::event_listener::event_listener()
229 : m_status(rc_t::NOOP)
234 dhcp_client::event_listener::status()
239 dhcp_client::event_handler::event_handler()
241 OM::register_listener(this);
242 inspect::register_handler({ "dhcp" }, "DHCP clients", this);
246 dhcp_client::event_handler::handle_replay()
252 dhcp_client::event_handler::handle_populate(const client_db::key_t& key)
254 std::shared_ptr<dhcp_client_cmds::dump_cmd> cmd =
255 std::make_shared<dhcp_client_cmds::dump_cmd>();
260 for (auto& record : *cmd) {
261 auto& payload = record.get_payload();
263 std::shared_ptr<interface> itf =
264 interface::find(payload.client.sw_if_index);
267 VOM_LOG(log_level_t::ERROR) << "dhcp-client dump:"
268 << " itf:" << payload.client.sw_if_index;
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)));
288 dhcp_client::event_handler::order() const
290 return (dependency_t::BINDING);
294 dhcp_client::event_handler::show(std::ostream& os)
299 std::shared_ptr<dhcp_client_cmds::events_cmd>
300 dhcp_client::get_event_cmd()
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);
314 return (m_s_event_cmd.lock());
318 dhcp_client::handle_dhcp_event(std::shared_ptr<lease_t> lease)
322 m_evl->handle_dhcp_event(m_lease);
326 dhcp_client::dhcp_client_listener::handle_dhcp_event(std::shared_ptr<lease_t> e)
329 * Find the client the event references
331 std::shared_ptr<dhcp_client> client = find(e->itf->key());
334 client->handle_dhcp_event(e);
341 * fd.io coding-style-patch-verification: ON
344 * eval: (c-set-style "mozilla")