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/stat_reader.hpp"
27 #include "vom/tap_interface_cmds.hpp"
31 * A DB of all the interfaces, key on the name
33 singular_db<interface::key_t, interface> interface::m_db;
36 * A DB of all the interfaces, key on VPP's handle
38 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
40 interface::event_handler interface::m_evh;
43 * the event enable command.
45 std::shared_ptr<interface_cmds::events_cmd> interface::m_events_cmd;
48 * Construct a new object matching the desried state
50 interface::interface(const std::string& name,
51 interface::type_t itf_type,
52 interface::admin_state_t itf_state,
53 const std::string& tag)
54 : m_hdl(handle_t::INVALID)
58 , m_table_id(route::DEFAULT_TABLE)
59 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
60 , m_stats_type(stats_type_t::NORMAL)
63 , m_oper(oper_state_t::DOWN)
68 interface::interface(const std::string& name,
69 interface::type_t itf_type,
70 interface::admin_state_t itf_state,
71 const route_domain& rd,
72 const std::string& tag)
73 : m_hdl(handle_t::INVALID)
78 , m_table_id(m_rd->table_id())
79 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
80 , m_stats_type(stats_type_t::NORMAL)
83 , m_oper(oper_state_t::DOWN)
88 interface::interface(const interface& o)
94 , m_table_id(o.m_table_id)
95 , m_l2_address(o.m_l2_address)
96 , m_stats_type(o.m_stats_type)
98 , m_listener(o.m_listener)
105 interface::operator==(const interface& i) const
107 return ((key() == i.key()) &&
108 (m_l2_address.data() == i.m_l2_address.data()) &&
109 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
110 (m_oper == i.m_oper));
113 interface::event_listener::event_listener()
114 : m_status(rc_t::NOOP)
119 interface::event_listener::status()
124 interface::stat_listener::stat_listener()
125 : m_status(rc_t::NOOP)
130 interface::stat_listener::status()
136 * Return the interface type
138 const interface::type_t&
139 interface::type() const
145 interface::handle() const
147 return (singular()->handle_i());
151 interface::handle_i() const
153 return (m_hdl.data());
157 interface::l2_address() const
159 return (m_l2_address.data());
162 interface::const_iterator_t
168 interface::const_iterator_t
177 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
178 m_table_id.data() = route::DEFAULT_TABLE;
180 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
182 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
189 // If the interface is up, bring it down
190 if (m_state && interface::admin_state_t::UP == m_state.data()) {
191 m_state.data() = interface::admin_state_t::DOWN;
192 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
196 std::queue<cmd*> cmds;
197 HW::enqueue(mk_delete_cmd(cmds));
206 std::queue<cmd*> cmds;
207 HW::enqueue(mk_create_cmd(cmds));
210 if (m_state && interface::admin_state_t::UP == m_state.data()) {
211 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
215 enable_stats(m_listener, m_stats_type.data());
218 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
220 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
222 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
226 interface::~interface()
235 // not in the DB anymore.
236 m_db.release(m_name, this);
240 interface::to_string() const
242 std::ostringstream s;
243 s << "interface:[" << m_name << " type:" << m_type.to_string()
244 << " hdl:" << m_hdl.to_string() << " l2-address:["
245 << m_l2_address.to_string() << "]";
248 s << " rd:" << m_rd->to_string();
251 s << " admin-state:" << m_state.to_string()
252 << " oper-state:" << m_oper.to_string();
254 if (!m_tag.empty()) {
255 s << " tag:[" << m_tag << "]";
264 interface::name() const
269 const interface::key_t&
270 interface::key() const
276 interface::mk_create_cmd(std::queue<cmd*>& q)
278 if (type_t::LOOPBACK == m_type) {
279 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
280 q.push(new interface_cmds::set_tag(m_hdl, m_name));
282 * set the m_tag for pretty-print
285 } else if (type_t::BVI == m_type) {
286 q.push(new interface_cmds::bvi_create_cmd(m_hdl, m_name));
287 q.push(new interface_cmds::set_tag(m_hdl, m_name));
289 } else if (type_t::AFPACKET == m_type) {
290 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
292 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
293 } else if (type_t::TAPV2 == m_type) {
295 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
296 } else if (type_t::VHOST == m_type) {
297 q.push(new interface_cmds::vhost_create_cmd(m_hdl, m_name, m_tag));
306 interface::mk_delete_cmd(std::queue<cmd*>& q)
308 if (type_t::LOOPBACK == m_type) {
309 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
310 } else if (type_t::BVI == m_type) {
311 q.push(new interface_cmds::bvi_delete_cmd(m_hdl));
312 } else if (type_t::AFPACKET == m_type) {
313 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
314 } else if (type_t::VHOST == m_type) {
315 q.push(new interface_cmds::vhost_delete_cmd(m_hdl, m_name));
322 interface::update(const interface& desired)
325 * the desired state is always that the interface should be created
327 if (rc_t::OK != m_hdl.rc()) {
328 std::queue<cmd*> cmds;
329 HW::enqueue(mk_create_cmd(cmds));
331 * interface create now, so we can barf early if it fails
337 * If the interface is not created do other commands should be issued
339 if (rc_t::OK != m_hdl.rc())
343 * change the interface state to that which is deisred
345 if (m_state.update(desired.m_state)) {
346 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
350 * change the interface state to that which is deisred
352 if (m_l2_address.update(desired.m_l2_address)) {
353 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
357 * If the interface is mapped into a route domain, set VPP's
360 if (m_rd != desired.m_rd) {
362 * changing route domains. need to remove all L3 bindings, swap the table
363 * then reapply the bindings.
365 auto it = l3_binding::cbegin();
367 while (it != l3_binding::cend()) {
368 if (it->second.lock()->itf().key() == key())
369 it->second.lock()->sweep();
373 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
375 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
377 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
380 it = l3_binding::cbegin();
381 while (it != l3_binding::cend()) {
382 if (it->second.lock()->itf().key() == key())
383 it->second.lock()->replay(); //(*it->second.lock());
386 } else if (!m_table_id && m_rd) {
388 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
390 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
395 interface::set(const admin_state_t& state)
401 interface::set(const l2_address_t& addr)
403 m_l2_address = { addr, rc_t::NOOP };
407 interface::set(const handle_t& hdl)
413 interface::set(const oper_state_t& state)
419 interface::set(const std::string& tag)
425 interface::set(const counter_t& count, const std::string& stat_type)
427 if ("rx" == stat_type)
428 m_stats.m_rx = count;
429 else if ("tx" == stat_type)
430 m_stats.m_tx = count;
431 else if ("drops" == stat_type)
432 m_stats.m_drop = count;
433 else if ("rx-unicast" == stat_type)
434 m_stats.m_rx_unicast = count;
435 else if ("tx-unicast" == stat_type)
436 m_stats.m_tx_unicast = count;
437 else if ("rx-multicast" == stat_type)
438 m_stats.m_rx_multicast = count;
439 else if ("tx-multicast" == stat_type)
440 m_stats.m_tx_multicast = count;
441 else if ("rx-broadcast" == stat_type)
442 m_stats.m_rx_broadcast = count;
443 else if ("tx-broadcast" == stat_type)
444 m_stats.m_rx_broadcast = count;
447 const interface::stats_t&
448 interface::get_stats(void) const
454 interface::publish_stats()
456 m_listener->handle_interface_stat(*this);
460 operator<<(std::ostream& os, const interface::stats_t& stats)
463 << "rx " << stats.m_rx << " rx-unicast " << stats.m_rx_unicast
464 << " rx-multicast " << stats.m_rx_multicast << " rx-broadcast "
465 << stats.m_rx_broadcast << " tx " << stats.m_tx << " tx-unicast "
466 << stats.m_tx_unicast << " tx-multicast " << stats.m_tx_multicast
467 << " tx-broadcast " << stats.m_tx_broadcast << " drops " << stats.m_drop
474 interface::enable_stats_i(interface::stat_listener* el, const stats_type_t& st)
477 if (stats_type_t::DETAILED == st) {
478 m_stats_type.set(rc_t::NOOP);
479 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
480 m_stats_type, handle_i(), true));
482 stat_reader::registers(*this);
488 interface::enable_stats(interface::stat_listener* el, const stats_type_t& st)
490 singular()->enable_stats_i(el, st);
494 interface::disable_stats_i()
496 if (m_listener != NULL) {
497 if (stats_type_t::DETAILED == m_stats_type) {
498 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
499 m_stats_type, handle_i(), false));
501 stat_reader::unregisters(*this);
507 interface::disable_stats()
509 singular()->disable_stats_i();
512 std::shared_ptr<interface>
513 interface::singular_i() const
515 return (m_db.find_or_add(key(), *this));
518 std::shared_ptr<interface>
519 interface::singular() const
524 std::shared_ptr<interface>
525 interface::find(const key_t& k)
527 return (m_db.find(k));
530 std::shared_ptr<interface>
531 interface::find(const handle_t& handle)
533 return (m_hdl_db[handle].lock());
537 interface::add(const key_t& key, const HW::item<handle_t>& item)
539 std::shared_ptr<interface> sp = find(key);
542 m_hdl_db[item.data()] = sp;
547 interface::remove(const HW::item<handle_t>& item)
549 m_hdl_db.erase(item.data());
553 interface::dump(std::ostream& os)
559 interface::enable_events(interface::event_listener& el)
561 m_events_cmd = std::make_shared<interface_cmds::events_cmd>(el);
562 HW::enqueue(m_events_cmd);
567 interface::disable_events()
569 m_events_cmd.reset();
573 interface::event_handler::handle_populate(const client_db::key_t& key)
576 * dump VPP vhost-user interfaces
578 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
579 std::make_shared<interface_cmds::vhost_dump_cmd>();
584 for (auto& vhost_itf_record : *vcmd) {
585 std::shared_ptr<interface> vitf =
586 interface_factory::new_vhost_user_interface(
587 vhost_itf_record.get_payload());
588 VOM_LOG(log_level_t::DEBUG) << " vhost-dump: " << vitf->to_string();
589 OM::commit(key, *vitf);
593 * dump VPP af-packet interfaces
595 std::shared_ptr<interface_cmds::af_packet_dump_cmd> afcmd =
596 std::make_shared<interface_cmds::af_packet_dump_cmd>();
601 for (auto& af_packet_itf_record : *afcmd) {
602 std::shared_ptr<interface> afitf =
603 interface_factory::new_af_packet_interface(
604 af_packet_itf_record.get_payload());
605 VOM_LOG(log_level_t::DEBUG) << " af_packet-dump: " << afitf->to_string();
606 OM::commit(key, *afitf);
610 * dump VPP tapv2 interfaces
612 std::shared_ptr<tap_interface_cmds::tapv2_dump_cmd> tapv2cmd =
613 std::make_shared<tap_interface_cmds::tapv2_dump_cmd>();
615 HW::enqueue(tapv2cmd);
618 for (auto& tapv2_record : *tapv2cmd) {
619 std::shared_ptr<tap_interface> tapv2itf =
620 interface_factory::new_tap_interface(tapv2_record.get_payload());
621 VOM_LOG(log_level_t::DEBUG) << "tapv2-dump: " << tapv2itf->to_string();
624 * Write each of the discovered interfaces into the OM,
625 * but disable the HW Command q whilst we do, so that no
626 * commands are sent to VPP
628 OM::commit(key, *tapv2itf);
632 * dump VPP interfaces
634 std::shared_ptr<interface_cmds::dump_cmd> cmd =
635 std::make_shared<interface_cmds::dump_cmd>();
640 for (auto& itf_record : *cmd) {
641 auto payload = itf_record.get_payload();
642 VOM_LOG(log_level_t::DEBUG) << "dump: [" << payload.sw_if_index
643 << " name:" << (char*)payload.interface_name
644 << " tag:" << (char*)payload.tag << "]";
646 std::shared_ptr<interface> itf = interface_factory::new_interface(payload);
648 if (itf && interface::type_t::LOCAL != itf->type()) {
649 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
651 * Write each of the discovered interfaces into the OM,
652 * but disable the HW Command q whilst we do, so that no
653 * commands are sent to VPP
655 OM::commit(key, *itf);
658 * Get the address configured on the interface
660 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
661 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
662 l3_binding_cmds::dump_v4_cmd(itf->handle()));
667 for (auto& l3_record : *dcmd) {
668 auto& payload = l3_record.get_payload();
669 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
670 payload.prefix_length);
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")