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/route_api_types.hpp"
19 #include "vom/singular_db_funcs.hpp"
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");
26 dhcp_client::state_t::state_t(int v, const std::string& s)
27 : enum_base<dhcp_client::state_t>(v, s)
31 const dhcp_client::state_t&
32 dhcp_client::state_t::from_vpp(int n)
42 singular_db<interface::key_t, dhcp_client> dhcp_client::m_db;
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;
46 dhcp_client::event_handler dhcp_client::m_evh;
48 dhcp_client::dhcp_client(const interface& itf,
49 const std::string& hostname,
50 bool set_broadcast_flag,
51 const ip_dscp_t& dscp,
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)
60 , m_event_cmd(get_event_cmd())
64 dhcp_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,
70 : m_itf(itf.singular())
71 , m_hostname(hostname)
72 , m_client_id(client_id)
73 , m_set_broadcast_flag(set_broadcast_flag)
77 , m_event_cmd(get_event_cmd())
81 dhcp_client::dhcp_client(const dhcp_client& o)
83 , m_hostname(o.m_hostname)
84 , m_client_id(o.m_client_id)
85 , m_set_broadcast_flag(o.m_set_broadcast_flag)
89 , m_event_cmd(o.m_event_cmd)
93 dhcp_client::~dhcp_client()
97 // not in the DB anymore.
98 m_db.release(m_itf->key(), this);
102 dhcp_client::operator==(const dhcp_client& l) const
104 return ((key() == l.key()) && (m_hostname == l.m_hostname) &&
105 (m_client_id == l.m_client_id && m_dscp == l.m_dscp));
108 const dhcp_client::key_t&
109 dhcp_client::key() const
111 return (m_itf->key());
119 new dhcp_client_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
125 dhcp_client::dump(std::ostream& os)
131 dhcp_client::replay()
134 HW::enqueue(new dhcp_client_cmds::bind_cmd(
135 m_binding, m_itf->handle(), m_hostname, m_client_id, false, m_dscp));
140 dhcp_client::to_string() const
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();
147 s << " " << m_lease->to_string();
155 dhcp_client::update(const dhcp_client& desired)
158 * the desired state is always that the interface should be created
161 HW::enqueue(new dhcp_client_cmds::bind_cmd(
162 m_binding, m_itf->handle(), m_hostname, m_client_id, false, m_dscp));
166 m_lease = desired.m_lease;
167 if (m_evl != desired.m_evl) {
168 m_evl = desired.m_evl;
172 const std::shared_ptr<dhcp_client::lease_t>
173 dhcp_client::lease() const
179 dhcp_client::lease(std::shared_ptr<dhcp_client::lease_t> lease)
184 std::shared_ptr<dhcp_client>
185 dhcp_client::find_or_add(const dhcp_client& temp)
187 return (m_db.find_or_add(temp.m_itf->key(), temp));
190 std::shared_ptr<dhcp_client>
191 dhcp_client::find(const key_t& k)
193 return (m_db.find(k));
196 std::shared_ptr<dhcp_client>
197 dhcp_client::singular() const
199 return find_or_add(*this);
202 dhcp_client::lease_t::lease_t()
203 : state(state_t::DISCOVER)
204 , mac(mac_address_t::ZERO)
208 dhcp_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)
216 , router_address(router_address)
217 , host_prefix(host_prefix)
224 dhcp_client::lease_t::to_string() const
226 std::stringstream ss;
228 ss << "lease:[" << itf->to_string() << " state: " << state.to_string()
229 << " host: " << host_prefix.to_string() << " router: " << router_address
230 << " mac: " << mac.to_string() << "]";
235 dhcp_client::event_listener::event_listener()
236 : m_status(rc_t::NOOP)
241 dhcp_client::event_listener::status()
246 dhcp_client::event_handler::event_handler()
248 OM::register_listener(this);
249 inspect::register_handler({ "dhcp" }, "DHCP clients", this);
253 dhcp_client::event_handler::handle_replay()
259 dhcp_client::event_handler::handle_populate(const client_db::key_t& key)
261 std::shared_ptr<dhcp_client_cmds::dump_cmd> cmd =
262 std::make_shared<dhcp_client_cmds::dump_cmd>();
267 for (auto& record : *cmd) {
268 auto& payload = record.get_payload();
270 std::shared_ptr<interface> itf =
271 interface::find(payload.client.sw_if_index);
274 VOM_LOG(log_level_t::ERROR) << "dhcp-client dump:"
275 << " itf:" << payload.client.sw_if_index;
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)));
297 dhcp_client::event_handler::order() const
299 return (dependency_t::BINDING);
303 dhcp_client::event_handler::show(std::ostream& os)
308 std::shared_ptr<dhcp_client_cmds::events_cmd>
309 dhcp_client::get_event_cmd()
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);
323 return (m_s_event_cmd.lock());
327 dhcp_client::handle_dhcp_event(std::shared_ptr<lease_t> lease)
331 m_evl->handle_dhcp_event(m_lease);
335 dhcp_client::dhcp_client_listener::handle_dhcp_event(std::shared_ptr<lease_t> e)
338 * Find the client the event references
340 std::shared_ptr<dhcp_client> client = find(e->itf->key());
343 client->handle_dhcp_event(e);
350 * fd.io coding-style-patch-verification: ON
353 * eval: (c-set-style "mozilla")