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/api_types.hpp"
18 #include "vom/bond_group_binding.hpp"
19 #include "vom/bond_group_binding_cmds.hpp"
20 #include "vom/bond_interface_cmds.hpp"
21 #include "vom/interface_cmds.hpp"
22 #include "vom/interface_factory.hpp"
23 #include "vom/l3_binding_cmds.hpp"
24 #include "vom/logger.hpp"
25 #include "vom/prefix.hpp"
26 #include "vom/singular_db_funcs.hpp"
27 #include "vom/stat_reader.hpp"
28 #include "vom/tap_interface_cmds.hpp"
32 * A DB of all the interfaces, key on the name
34 singular_db<interface::key_t, interface> interface::m_db;
37 * A DB of all the interfaces, key on VPP's handle
39 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
41 interface::event_handler interface::m_evh;
44 * the event enable command.
46 std::shared_ptr<interface_cmds::events_cmd> interface::m_events_cmd;
49 * Construct a new object matching the desried state
51 interface::interface(const std::string& name,
52 interface::type_t itf_type,
53 interface::admin_state_t itf_state,
54 const std::string& tag)
55 : m_hdl(handle_t::INVALID)
59 , m_table_id(route::DEFAULT_TABLE)
60 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
61 , m_stats_type(stats_type_t::NORMAL)
64 , m_oper(oper_state_t::DOWN)
69 interface::interface(const std::string& name,
70 interface::type_t itf_type,
71 interface::admin_state_t itf_state,
72 const route_domain& rd,
73 const std::string& tag)
74 : m_hdl(handle_t::INVALID)
79 , m_table_id(m_rd->table_id())
80 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
81 , m_stats_type(stats_type_t::NORMAL)
84 , m_oper(oper_state_t::DOWN)
89 interface::interface(const interface& o)
95 , m_table_id(o.m_table_id)
96 , m_l2_address(o.m_l2_address)
97 , m_stats_type(o.m_stats_type)
99 , m_listener(o.m_listener)
106 interface::operator==(const interface& i) const
108 return ((key() == i.key()) &&
109 (m_l2_address.data() == i.m_l2_address.data()) &&
110 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
111 (m_oper == i.m_oper));
114 interface::event_listener::event_listener()
115 : m_status(rc_t::NOOP)
120 interface::event_listener::status()
125 interface::stat_listener::stat_listener()
126 : m_status(rc_t::NOOP)
131 interface::stat_listener::status()
137 * Return the interface type
139 const interface::type_t&
140 interface::type() const
146 interface::handle() const
148 return (singular()->handle_i());
152 interface::handle_i() const
154 return (m_hdl.data());
158 interface::l2_address() const
160 return (m_l2_address.data());
163 interface::const_iterator_t
169 interface::const_iterator_t
178 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
179 m_table_id.data() = route::DEFAULT_TABLE;
181 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
183 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
190 // If the interface is up, bring it down
191 if (m_state && interface::admin_state_t::UP == m_state.data()) {
192 m_state.data() = interface::admin_state_t::DOWN;
193 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
197 std::queue<cmd*> cmds;
198 HW::enqueue(mk_delete_cmd(cmds));
207 std::queue<cmd*> cmds;
208 HW::enqueue(mk_create_cmd(cmds));
211 if (m_state && interface::admin_state_t::UP == m_state.data()) {
212 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
216 enable_stats(m_listener, m_stats_type.data());
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) {
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::BVI == m_type) {
287 q.push(new interface_cmds::bvi_create_cmd(m_hdl, m_name));
288 q.push(new interface_cmds::set_tag(m_hdl, m_name));
290 } else if (type_t::AFPACKET == m_type) {
291 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
293 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
294 } else if (type_t::TAPV2 == m_type) {
296 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
297 } else if (type_t::VHOST == m_type) {
298 q.push(new interface_cmds::vhost_create_cmd(m_hdl, m_name, m_tag));
307 interface::mk_delete_cmd(std::queue<cmd*>& q)
309 if (type_t::LOOPBACK == m_type) {
310 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
311 } else if (type_t::BVI == m_type) {
312 q.push(new interface_cmds::bvi_delete_cmd(m_hdl));
313 } else if (type_t::AFPACKET == m_type) {
314 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
315 } else if (type_t::VHOST == m_type) {
316 q.push(new interface_cmds::vhost_delete_cmd(m_hdl, m_name));
323 interface::update(const interface& desired)
326 * the desired state is always that the interface should be created
328 if (rc_t::OK != m_hdl.rc()) {
329 std::queue<cmd*> cmds;
330 HW::enqueue(mk_create_cmd(cmds));
332 * interface create now, so we can barf early if it fails
338 * If the interface is not created do other commands should be issued
340 if (rc_t::OK != m_hdl.rc())
344 * change the interface state to that which is deisred
346 if (m_state.update(desired.m_state)) {
347 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
351 * change the interface state to that which is deisred
353 if (m_l2_address.update(desired.m_l2_address)) {
354 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
358 * If the interface is mapped into a route domain, set VPP's
361 if (m_rd != desired.m_rd) {
363 * changing route domains. need to remove all L3 bindings, swap the table
364 * then reapply the bindings.
366 auto it = l3_binding::cbegin();
368 while (it != l3_binding::cend()) {
369 if (it->second.lock()->itf().key() == key())
370 it->second.lock()->sweep();
374 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
376 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
378 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
381 it = l3_binding::cbegin();
382 while (it != l3_binding::cend()) {
383 if (it->second.lock()->itf().key() == key())
384 it->second.lock()->replay(); //(*it->second.lock());
387 } else if (!m_table_id && m_rd) {
389 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
391 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
396 interface::set(const admin_state_t& state)
402 interface::set(const l2_address_t& addr)
404 m_l2_address = { addr, rc_t::NOOP };
408 interface::set(const handle_t& hdl)
414 interface::set(const oper_state_t& state)
420 interface::set(const std::string& tag)
426 interface::set(const counter_t& count, const std::string& stat_type)
428 if ("rx" == stat_type)
429 m_stats.m_rx = count;
430 else if ("tx" == stat_type)
431 m_stats.m_tx = count;
432 else if ("drops" == stat_type)
433 m_stats.m_drop = count;
434 else if ("rx-unicast" == stat_type)
435 m_stats.m_rx_unicast = count;
436 else if ("tx-unicast" == stat_type)
437 m_stats.m_tx_unicast = count;
438 else if ("rx-multicast" == stat_type)
439 m_stats.m_rx_multicast = count;
440 else if ("tx-multicast" == stat_type)
441 m_stats.m_tx_multicast = count;
442 else if ("rx-broadcast" == stat_type)
443 m_stats.m_rx_broadcast = count;
444 else if ("tx-broadcast" == stat_type)
445 m_stats.m_rx_broadcast = count;
448 const interface::stats_t&
449 interface::get_stats(void) const
455 interface::publish_stats()
457 m_listener->handle_interface_stat(*this);
461 operator<<(std::ostream& os, const interface::stats_t& stats)
464 << "rx " << stats.m_rx << " rx-unicast " << stats.m_rx_unicast
465 << " rx-multicast " << stats.m_rx_multicast << " rx-broadcast "
466 << stats.m_rx_broadcast << " tx " << stats.m_tx << " tx-unicast "
467 << stats.m_tx_unicast << " tx-multicast " << stats.m_tx_multicast
468 << " tx-broadcast " << stats.m_tx_broadcast << " drops " << stats.m_drop
475 interface::enable_stats_i(interface::stat_listener* el, const stats_type_t& st)
478 if (stats_type_t::DETAILED == st) {
479 m_stats_type.set(rc_t::NOOP);
480 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
481 m_stats_type, handle_i(), true));
483 stat_reader::registers(*this);
489 interface::enable_stats(interface::stat_listener* el, const stats_type_t& st)
491 singular()->enable_stats_i(el, st);
495 interface::disable_stats_i()
497 if (m_listener != NULL) {
498 if (stats_type_t::DETAILED == m_stats_type) {
499 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
500 m_stats_type, handle_i(), false));
502 stat_reader::unregisters(*this);
508 interface::disable_stats()
510 singular()->disable_stats_i();
513 std::shared_ptr<interface>
514 interface::singular_i() const
516 return (m_db.find_or_add(key(), *this));
519 std::shared_ptr<interface>
520 interface::singular() const
525 std::shared_ptr<interface>
526 interface::find(const key_t& k)
528 return (m_db.find(k));
531 std::shared_ptr<interface>
532 interface::find(const handle_t& handle)
534 return (m_hdl_db[handle].lock());
538 interface::add(const key_t& key, const HW::item<handle_t>& item)
540 std::shared_ptr<interface> sp = find(key);
543 m_hdl_db[item.data()] = sp;
548 interface::remove(const HW::item<handle_t>& item)
550 m_hdl_db.erase(item.data());
554 interface::dump(std::ostream& os)
560 interface::enable_events(interface::event_listener& el)
562 m_events_cmd = std::make_shared<interface_cmds::events_cmd>(el);
563 HW::enqueue(m_events_cmd);
568 interface::disable_events()
570 m_events_cmd.reset();
574 interface::event_handler::handle_populate(const client_db::key_t& key)
577 * dump VPP vhost-user interfaces
579 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
580 std::make_shared<interface_cmds::vhost_dump_cmd>();
585 for (auto& vhost_itf_record : *vcmd) {
586 std::shared_ptr<interface> vitf =
587 interface_factory::new_vhost_user_interface(
588 vhost_itf_record.get_payload());
589 VOM_LOG(log_level_t::DEBUG) << " vhost-dump: " << vitf->to_string();
590 OM::commit(key, *vitf);
594 * dump VPP af-packet interfaces
596 std::shared_ptr<interface_cmds::af_packet_dump_cmd> afcmd =
597 std::make_shared<interface_cmds::af_packet_dump_cmd>();
602 for (auto& af_packet_itf_record : *afcmd) {
603 std::shared_ptr<interface> afitf =
604 interface_factory::new_af_packet_interface(
605 af_packet_itf_record.get_payload());
606 VOM_LOG(log_level_t::DEBUG) << " af_packet-dump: " << afitf->to_string();
607 OM::commit(key, *afitf);
611 * dump VPP tapv2 interfaces
613 std::shared_ptr<tap_interface_cmds::tapv2_dump_cmd> tapv2cmd =
614 std::make_shared<tap_interface_cmds::tapv2_dump_cmd>();
616 HW::enqueue(tapv2cmd);
619 for (auto& tapv2_record : *tapv2cmd) {
620 std::shared_ptr<tap_interface> tapv2itf =
621 interface_factory::new_tap_interface(tapv2_record.get_payload());
622 VOM_LOG(log_level_t::DEBUG) << "tapv2-dump: " << tapv2itf->to_string();
625 * Write each of the discovered interfaces into the OM,
626 * but disable the HW Command q whilst we do, so that no
627 * commands are sent to VPP
629 OM::commit(key, *tapv2itf);
633 * dump VPP interfaces
635 std::shared_ptr<interface_cmds::dump_cmd> cmd =
636 std::make_shared<interface_cmds::dump_cmd>();
641 for (auto& itf_record : *cmd) {
642 auto payload = itf_record.get_payload();
643 VOM_LOG(log_level_t::DEBUG) << "dump: [" << payload.sw_if_index
644 << " name:" << (char*)payload.interface_name
645 << " tag:" << (char*)payload.tag << "]";
647 std::shared_ptr<interface> itf = interface_factory::new_interface(payload);
649 if (itf && interface::type_t::LOCAL != itf->type()) {
650 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
652 * Write each of the discovered interfaces into the OM,
653 * but disable the HW Command q whilst we do, so that no
654 * commands are sent to VPP
656 OM::commit(key, *itf);
659 * Get the address configured on the interface
661 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
662 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
663 l3_binding_cmds::dump_v4_cmd(itf->handle()));
668 for (auto& l3_record : *dcmd) {
669 auto& payload = l3_record.get_payload();
670 const route::prefix_t pfx = from_api(payload.prefix);
672 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
674 l3_binding l3(*itf, pfx);
681 * dump VPP bond interfaces
683 std::shared_ptr<bond_interface_cmds::dump_cmd> bcmd =
684 std::make_shared<bond_interface_cmds::dump_cmd>();
689 for (auto& bond_itf_record : *bcmd) {
690 std::shared_ptr<bond_interface> bond_itf =
691 interface_factory::new_bond_interface(bond_itf_record.get_payload());
693 VOM_LOG(log_level_t::DEBUG) << " bond-dump:" << bond_itf->to_string();
696 * Write each of the discovered interfaces into the OM,
697 * but disable the HW Command q whilst we do, so that no
698 * commands are sent to VPP
700 OM::commit(key, *bond_itf);
702 std::shared_ptr<bond_group_binding_cmds::dump_cmd> scmd =
703 std::make_shared<bond_group_binding_cmds::dump_cmd>(
704 bond_group_binding_cmds::dump_cmd(bond_itf->handle()));
709 bond_group_binding::enslaved_itf_t enslaved_itfs;
711 for (auto& slave_itf_record : *scmd) {
712 bond_member slave_itf = interface_factory::new_bond_member_interface(
713 slave_itf_record.get_payload());
715 VOM_LOG(log_level_t::DEBUG) << " slave-dump:" << slave_itf.to_string();
718 * Write each of the discovered interfaces into the OM,
719 * but disable the HW Command q whilst we do, so that no
720 * commands are sent to VPP
722 // OM::commit(slave_itf->key(), *slave_itf);
723 enslaved_itfs.insert(slave_itf);
726 if (!enslaved_itfs.empty()) {
727 bond_group_binding bid(*bond_itf, enslaved_itfs);
729 * Write each of the discovered interfaces into the OM,
730 * but disable the HW Command q whilst we do, so that no
731 * commands are sent to VPP
733 OM::commit(key, bid);
738 interface::event_handler::event_handler()
740 OM::register_listener(this);
741 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
745 interface::event_handler::handle_replay()
751 interface::event_handler::order() const
753 return (dependency_t::INTERFACE);
757 interface::event_handler::show(std::ostream& os)
765 * fd.io coding-style-patch-verification: ON
768 * eval: (c-set-style "mozilla")