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/interface.hpp"
17 #include "vom/interface_cmds.hpp"
18 #include "vom/interface_factory.hpp"
19 #include "vom/l3_binding_cmds.hpp"
20 #include "vom/logger.hpp"
21 #include "vom/prefix.hpp"
25 * A DB of all the interfaces, key on the name
27 singular_db<interface::key_t, interface> interface::m_db;
30 * A DB of all the interfaces, key on VPP's handle
32 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
34 interface::event_handler interface::m_evh;
37 * Construct a new object matching the desried state
39 interface::interface(const std::string& name,
40 interface::type_t itf_type,
41 interface::admin_state_t itf_state)
42 : m_hdl(handle_t::INVALID)
46 , m_table_id(route::DEFAULT_TABLE)
47 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
48 , m_oper(oper_state_t::DOWN)
52 interface::interface(const std::string& name,
53 interface::type_t itf_type,
54 interface::admin_state_t itf_state,
55 const route_domain& rd)
56 : m_hdl(handle_t::INVALID)
61 , m_table_id(m_rd->table_id())
62 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
63 , m_oper(oper_state_t::DOWN)
67 interface::interface(const interface& o)
73 , m_table_id(o.m_table_id)
74 , m_l2_address(o.m_l2_address)
80 interface::operator==(const interface& i) const
82 return ((key() == i.key()) &&
83 (m_l2_address.data() == i.m_l2_address.data()) &&
84 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
85 (m_oper == i.m_oper));
88 interface::event_listener::event_listener()
89 : m_status(rc_t::NOOP)
94 interface::event_listener::status()
99 interface::stat_listener::stat_listener()
100 : m_status(rc_t::NOOP)
105 interface::stat_listener::status()
111 * Return the interface type
113 const interface::type_t&
114 interface::type() const
120 interface::handle() const
122 return (singular()->handle_i());
126 interface::handle_i() const
128 return (m_hdl.data());
132 interface::l2_address() const
134 return (m_l2_address.data());
137 interface::const_iterator_t
140 return m_db.cbegin();
143 interface::const_iterator_t
152 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
153 m_table_id.data() = route::DEFAULT_TABLE;
155 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
157 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
161 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
165 // If the interface is up, bring it down
166 if (m_state && interface::admin_state_t::UP == m_state.data()) {
167 m_state.data() = interface::admin_state_t::DOWN;
168 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
172 std::queue<cmd*> cmds;
173 HW::enqueue(mk_delete_cmd(cmds));
182 std::queue<cmd*> cmds;
183 HW::enqueue(mk_create_cmd(cmds));
186 if (m_state && interface::admin_state_t::UP == m_state.data()) {
187 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
190 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
192 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
194 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
198 interface::~interface()
207 // not in the DB anymore.
208 m_db.release(m_name, this);
212 interface::to_string() const
214 std::ostringstream s;
215 s << "interface:[" << m_name << " type:" << m_type.to_string()
216 << " hdl:" << m_hdl.to_string() << " l2-address:["
217 << m_l2_address.to_string() << "]";
220 s << " rd:" << m_rd->to_string();
223 s << " admin-state:" << m_state.to_string()
224 << " oper-state:" << m_oper.to_string() << "]";
230 interface::name() const
235 const interface::key_t&
236 interface::key() const
242 interface::mk_create_cmd(std::queue<cmd*>& q)
244 if (type_t::LOOPBACK == m_type) {
245 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
246 } else if (type_t::BVI == m_type) {
247 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
248 q.push(new interface_cmds::set_tag(m_hdl, m_name));
249 } else if (type_t::AFPACKET == m_type) {
250 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
251 } else if (type_t::TAP == m_type) {
252 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
261 interface::mk_delete_cmd(std::queue<cmd*>& q)
263 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
264 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
265 } else if (type_t::AFPACKET == m_type) {
266 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
267 } else if (type_t::TAP == m_type) {
268 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
275 interface::update(const interface& desired)
278 * the desired state is always that the interface should be created
280 if (rc_t::OK != m_hdl.rc()) {
281 std::queue<cmd*> cmds;
282 HW::enqueue(mk_create_cmd(cmds));
284 * interface create now, so we can barf early if it fails
290 * If the interface is not created do other commands should be issued
292 if (rc_t::OK != m_hdl.rc())
296 * change the interface state to that which is deisred
298 if (m_state.update(desired.m_state)) {
299 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
303 * change the interface state to that which is deisred
305 if (m_l2_address.update(desired.m_l2_address)) {
306 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
310 * If the interface is mapped into a route domain, set VPP's
313 if (m_rd != desired.m_rd) {
315 * changing route domains. need to remove all L3 bindings, swap the table
316 * then reapply the bindings.
318 auto it = l3_binding::cbegin();
320 while (it != l3_binding::cend()) {
321 if (it->second.lock()->itf().key() == key())
322 it->second.lock()->sweep();
326 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
328 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
330 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
333 it = l3_binding::cbegin();
334 while (it != l3_binding::cend()) {
335 if (it->second.lock()->itf().key() == key())
336 it->second.lock()->replay(); //(*it->second.lock());
339 } else if (!m_table_id && m_rd) {
341 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
343 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
348 interface::set(const l2_address_t& addr)
350 assert(rc_t::UNSET == m_l2_address.rc());
351 m_l2_address.set(rc_t::NOOP);
352 m_l2_address.update(addr);
356 interface::set(const handle_t& hdl)
362 interface::set(const oper_state_t& state)
368 interface::enable_stats_i(interface::stat_listener& el)
371 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
372 HW::enqueue(m_stats);
378 interface::enable_stats(interface::stat_listener& el)
380 singular()->enable_stats_i(el);
383 std::shared_ptr<interface>
384 interface::singular_i() const
386 return (m_db.find_or_add(key(), *this));
389 std::shared_ptr<interface>
390 interface::singular() const
395 std::shared_ptr<interface>
396 interface::find(const key_t& k)
398 return (m_db.find(k));
401 std::shared_ptr<interface>
402 interface::find(const handle_t& handle)
404 return (m_hdl_db[handle].lock());
408 interface::add(const key_t& key, const HW::item<handle_t>& item)
410 std::shared_ptr<interface> sp = find(key);
413 m_hdl_db[item.data()] = sp;
418 interface::remove(const HW::item<handle_t>& item)
420 m_hdl_db.erase(item.data());
424 interface::dump(std::ostream& os)
430 interface::event_handler::handle_populate(const client_db::key_t& key)
433 * dump VPP current states
435 std::shared_ptr<interface_cmds::dump_cmd> cmd =
436 std::make_shared<interface_cmds::dump_cmd>();
441 for (auto& itf_record : *cmd) {
442 std::shared_ptr<interface> itf =
443 interface_factory::new_interface(itf_record.get_payload());
445 if (itf && interface::type_t::LOCAL != itf->type()) {
446 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
448 * Write each of the discovered interfaces into the OM,
449 * but disable the HW Command q whilst we do, so that no
450 * commands are sent to VPP
452 OM::commit(key, *itf);
455 * Get the address configured on the interface
457 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
458 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
459 l3_binding_cmds::dump_v4_cmd(itf->handle()));
464 for (auto& l3_record : *dcmd) {
465 auto& payload = l3_record.get_payload();
466 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
467 payload.prefix_length);
469 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
471 l3_binding l3(*itf, pfx);
478 interface::event_handler::event_handler()
480 OM::register_listener(this);
481 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
485 interface::event_handler::handle_replay()
491 interface::event_handler::order() const
493 return (dependency_t::INTERFACE);
497 interface::event_handler::show(std::ostream& os)
505 * fd.io coding-style-patch-verification: ON
508 * eval: (c-set-style "mozilla")