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/bond_group_binding.hpp"
18 #include "vom/bond_group_binding_cmds.hpp"
19 #include "vom/bond_interface_cmds.hpp"
20 #include "vom/interface_cmds.hpp"
21 #include "vom/interface_factory.hpp"
22 #include "vom/l3_binding_cmds.hpp"
23 #include "vom/logger.hpp"
24 #include "vom/prefix.hpp"
25 #include "vom/singular_db_funcs.hpp"
26 #include "vom/tap_interface_cmds.hpp"
30 * A DB of all the interfaces, key on the name
32 singular_db<interface::key_t, interface> interface::m_db;
35 * A DB of all the interfaces, key on VPP's handle
37 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
39 interface::event_handler interface::m_evh;
42 * Construct a new object matching the desried state
44 interface::interface(const std::string& name,
45 interface::type_t itf_type,
46 interface::admin_state_t itf_state,
47 const std::string& tag)
48 : m_hdl(handle_t::INVALID)
52 , m_table_id(route::DEFAULT_TABLE)
53 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
54 , m_stats_type(stats_type_t::NORMAL)
55 , m_oper(oper_state_t::DOWN)
60 interface::interface(const std::string& name,
61 interface::type_t itf_type,
62 interface::admin_state_t itf_state,
63 const route_domain& rd,
64 const std::string& tag)
65 : m_hdl(handle_t::INVALID)
70 , m_table_id(m_rd->table_id())
71 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
72 , m_stats_type(stats_type_t::NORMAL)
73 , m_oper(oper_state_t::DOWN)
78 interface::interface(const interface& o)
84 , m_table_id(o.m_table_id)
85 , m_l2_address(o.m_l2_address)
86 , m_stats_type(o.m_stats_type)
93 interface::operator==(const interface& i) const
95 return ((key() == i.key()) &&
96 (m_l2_address.data() == i.m_l2_address.data()) &&
97 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
98 (m_oper == i.m_oper));
101 interface::event_listener::event_listener()
102 : m_status(rc_t::NOOP)
107 interface::event_listener::status()
112 interface::stat_listener::stat_listener()
113 : m_status(rc_t::NOOP)
118 interface::stat_listener::status()
124 * Return the interface type
126 const interface::type_t&
127 interface::type() const
133 interface::handle() const
135 return (singular()->handle_i());
139 interface::handle_i() const
141 return (m_hdl.data());
145 interface::l2_address() const
147 return (m_l2_address.data());
150 interface::const_iterator_t
156 interface::const_iterator_t
165 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
166 m_table_id.data() = route::DEFAULT_TABLE;
168 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
170 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
174 if (stats_type_t::DETAILED == m_stats_type) {
175 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
176 m_stats_type, handle_i(), false));
178 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
182 // If the interface is up, bring it down
183 if (m_state && interface::admin_state_t::UP == m_state.data()) {
184 m_state.data() = interface::admin_state_t::DOWN;
185 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
189 std::queue<cmd*> cmds;
190 HW::enqueue(mk_delete_cmd(cmds));
199 std::queue<cmd*> cmds;
200 HW::enqueue(mk_create_cmd(cmds));
203 if (m_state && interface::admin_state_t::UP == m_state.data()) {
204 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
208 if (stats_type_t::DETAILED == m_stats_type) {
209 m_stats_type.set(rc_t::NOOP);
210 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
211 m_stats_type, handle_i(), true));
213 stat_listener& listener = m_stats->listener();
214 listener.status().set(rc_t::NOOP);
215 m_stats.reset(new interface_cmds::stats_enable_cmd(listener, handle_i()));
216 HW::enqueue(m_stats);
219 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
221 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
223 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
227 interface::~interface()
236 // not in the DB anymore.
237 m_db.release(m_name, this);
241 interface::to_string() const
243 std::ostringstream s;
244 s << "interface:[" << m_name << " type:" << m_type.to_string()
245 << " hdl:" << m_hdl.to_string() << " l2-address:["
246 << m_l2_address.to_string() << "]";
249 s << " rd:" << m_rd->to_string();
252 s << " admin-state:" << m_state.to_string()
253 << " oper-state:" << m_oper.to_string();
255 if (!m_tag.empty()) {
256 s << " tag:[" << m_tag << "]";
265 interface::name() const
270 const interface::key_t&
271 interface::key() const
277 interface::mk_create_cmd(std::queue<cmd*>& q)
279 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
280 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
281 q.push(new interface_cmds::set_tag(m_hdl, m_name));
283 * set the m_tag for pretty-print
286 } else if (type_t::AFPACKET == m_type) {
287 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
289 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
290 } else if (type_t::TAP == m_type || type_t::TAPV2 == m_type) {
292 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
293 } else if (type_t::VHOST == m_type) {
294 q.push(new interface_cmds::vhost_create_cmd(m_hdl, m_name, m_tag));
303 interface::mk_delete_cmd(std::queue<cmd*>& q)
305 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
306 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
307 } else if (type_t::AFPACKET == m_type) {
308 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
309 } else if (type_t::VHOST == m_type) {
310 q.push(new interface_cmds::vhost_delete_cmd(m_hdl, m_name));
317 interface::update(const interface& desired)
320 * the desired state is always that the interface should be created
322 if (rc_t::OK != m_hdl.rc()) {
323 std::queue<cmd*> cmds;
324 HW::enqueue(mk_create_cmd(cmds));
326 * interface create now, so we can barf early if it fails
332 * If the interface is not created do other commands should be issued
334 if (rc_t::OK != m_hdl.rc())
338 * change the interface state to that which is deisred
340 if (m_state.update(desired.m_state)) {
341 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
345 * change the interface state to that which is deisred
347 if (m_l2_address.update(desired.m_l2_address)) {
348 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
352 * If the interface is mapped into a route domain, set VPP's
355 if (m_rd != desired.m_rd) {
357 * changing route domains. need to remove all L3 bindings, swap the table
358 * then reapply the bindings.
360 auto it = l3_binding::cbegin();
362 while (it != l3_binding::cend()) {
363 if (it->second.lock()->itf().key() == key())
364 it->second.lock()->sweep();
368 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
370 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
372 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
375 it = l3_binding::cbegin();
376 while (it != l3_binding::cend()) {
377 if (it->second.lock()->itf().key() == key())
378 it->second.lock()->replay(); //(*it->second.lock());
381 } else if (!m_table_id && m_rd) {
383 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
385 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
390 interface::set(const admin_state_t& state)
396 interface::set(const l2_address_t& addr)
398 assert(rc_t::UNSET == m_l2_address.rc());
399 m_l2_address.set(rc_t::NOOP);
400 m_l2_address.update(addr);
404 interface::set(const handle_t& hdl)
410 interface::set(const oper_state_t& state)
416 interface::set(const std::string& tag)
422 interface::enable_stats_i(interface::stat_listener& el, const stats_type_t& st)
425 if (stats_type_t::DETAILED == st) {
427 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
428 m_stats_type, handle_i(), true));
430 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
431 HW::enqueue(m_stats);
437 interface::enable_stats(interface::stat_listener& el, const stats_type_t& st)
439 singular()->enable_stats_i(el, st);
442 std::shared_ptr<interface>
443 interface::singular_i() const
445 return (m_db.find_or_add(key(), *this));
448 std::shared_ptr<interface>
449 interface::singular() const
454 std::shared_ptr<interface>
455 interface::find(const key_t& k)
457 return (m_db.find(k));
460 std::shared_ptr<interface>
461 interface::find(const handle_t& handle)
463 return (m_hdl_db[handle].lock());
467 interface::add(const key_t& key, const HW::item<handle_t>& item)
469 std::shared_ptr<interface> sp = find(key);
472 m_hdl_db[item.data()] = sp;
477 interface::remove(const HW::item<handle_t>& item)
479 m_hdl_db.erase(item.data());
483 interface::dump(std::ostream& os)
489 interface::event_handler::handle_populate(const client_db::key_t& key)
492 * dump VPP vhost-user interfaces
494 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
495 std::make_shared<interface_cmds::vhost_dump_cmd>();
500 for (auto& vhost_itf_record : *vcmd) {
501 std::shared_ptr<interface> vitf =
502 interface_factory::new_vhost_user_interface(
503 vhost_itf_record.get_payload());
504 VOM_LOG(log_level_t::DEBUG) << " vhost-dump: " << vitf->to_string();
505 OM::commit(key, *vitf);
509 * dump VPP af-packet interfaces
511 std::shared_ptr<interface_cmds::af_packet_dump_cmd> afcmd =
512 std::make_shared<interface_cmds::af_packet_dump_cmd>();
517 for (auto& af_packet_itf_record : *afcmd) {
518 std::shared_ptr<interface> afitf =
519 interface_factory::new_af_packet_interface(
520 af_packet_itf_record.get_payload());
521 VOM_LOG(log_level_t::DEBUG) << " af_packet-dump: " << afitf->to_string();
522 OM::commit(key, *afitf);
526 * dump VPP tap interfaces
528 std::shared_ptr<tap_interface_cmds::tap_dump_cmd> tapcmd =
529 std::make_shared<tap_interface_cmds::tap_dump_cmd>();
534 for (auto& tap_record : *tapcmd) {
535 std::shared_ptr<tap_interface> tapitf =
536 interface_factory::new_tap_interface(tap_record.get_payload());
537 VOM_LOG(log_level_t::DEBUG) << "tap-dump: " << tapitf->to_string();
540 * Write each of the discovered interfaces into the OM,
541 * but disable the HW Command q whilst we do, so that no
542 * commands are sent to VPP
544 OM::commit(key, *tapitf);
548 * dump VPP tapv2 interfaces
550 std::shared_ptr<tap_interface_cmds::tapv2_dump_cmd> tapv2cmd =
551 std::make_shared<tap_interface_cmds::tapv2_dump_cmd>();
553 HW::enqueue(tapv2cmd);
556 for (auto& tapv2_record : *tapv2cmd) {
557 std::shared_ptr<tap_interface> tapv2itf =
558 interface_factory::new_tap_v2_interface(tapv2_record.get_payload());
559 VOM_LOG(log_level_t::DEBUG) << "tapv2-dump: " << tapv2itf->to_string();
562 * Write each of the discovered interfaces into the OM,
563 * but disable the HW Command q whilst we do, so that no
564 * commands are sent to VPP
566 OM::commit(key, *tapv2itf);
570 * dump VPP interfaces
572 std::shared_ptr<interface_cmds::dump_cmd> cmd =
573 std::make_shared<interface_cmds::dump_cmd>();
578 for (auto& itf_record : *cmd) {
579 auto payload = itf_record.get_payload();
580 VOM_LOG(log_level_t::DEBUG) << "dump: [" << payload.sw_if_index
581 << " name:" << (char*)payload.interface_name
582 << " tag:" << (char*)payload.tag << "]";
584 std::shared_ptr<interface> itf = interface_factory::new_interface(payload);
586 if (itf && interface::type_t::LOCAL != itf->type()) {
587 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
589 * Write each of the discovered interfaces into the OM,
590 * but disable the HW Command q whilst we do, so that no
591 * commands are sent to VPP
593 OM::commit(key, *itf);
596 * Get the address configured on the interface
598 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
599 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
600 l3_binding_cmds::dump_v4_cmd(itf->handle()));
605 for (auto& l3_record : *dcmd) {
606 auto& payload = l3_record.get_payload();
607 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
608 payload.prefix_length);
610 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
612 l3_binding l3(*itf, pfx);
619 * dump VPP bond interfaces
621 std::shared_ptr<bond_interface_cmds::dump_cmd> bcmd =
622 std::make_shared<bond_interface_cmds::dump_cmd>();
627 for (auto& bond_itf_record : *bcmd) {
628 std::shared_ptr<bond_interface> bond_itf =
629 interface_factory::new_bond_interface(bond_itf_record.get_payload());
631 VOM_LOG(log_level_t::DEBUG) << " bond-dump:" << bond_itf->to_string();
634 * Write each of the discovered interfaces into the OM,
635 * but disable the HW Command q whilst we do, so that no
636 * commands are sent to VPP
638 OM::commit(key, *bond_itf);
640 std::shared_ptr<bond_group_binding_cmds::dump_cmd> scmd =
641 std::make_shared<bond_group_binding_cmds::dump_cmd>(
642 bond_group_binding_cmds::dump_cmd(bond_itf->handle()));
647 bond_group_binding::enslaved_itf_t enslaved_itfs;
649 for (auto& slave_itf_record : *scmd) {
650 bond_member slave_itf = interface_factory::new_bond_member_interface(
651 slave_itf_record.get_payload());
653 VOM_LOG(log_level_t::DEBUG) << " slave-dump:" << slave_itf.to_string();
656 * Write each of the discovered interfaces into the OM,
657 * but disable the HW Command q whilst we do, so that no
658 * commands are sent to VPP
660 // OM::commit(slave_itf->key(), *slave_itf);
661 enslaved_itfs.insert(slave_itf);
664 if (!enslaved_itfs.empty()) {
665 bond_group_binding bid(*bond_itf, enslaved_itfs);
667 * Write each of the discovered interfaces into the OM,
668 * but disable the HW Command q whilst we do, so that no
669 * commands are sent to VPP
671 OM::commit(key, bid);
676 interface::event_handler::event_handler()
678 OM::register_listener(this);
679 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
683 interface::event_handler::handle_replay()
689 interface::event_handler::order() const
691 return (dependency_t::INTERFACE);
695 interface::event_handler::show(std::ostream& os)
703 * fd.io coding-style-patch-verification: ON
706 * eval: (c-set-style "mozilla")