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 * the event enable command.
44 std::shared_ptr<interface_cmds::events_cmd> interface::m_events_cmd;
47 * Construct a new object matching the desried state
49 interface::interface(const std::string& name,
50 interface::type_t itf_type,
51 interface::admin_state_t itf_state,
52 const std::string& tag)
53 : m_hdl(handle_t::INVALID)
57 , m_table_id(route::DEFAULT_TABLE)
58 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
59 , m_stats_type(stats_type_t::NORMAL)
60 , m_oper(oper_state_t::DOWN)
65 interface::interface(const std::string& name,
66 interface::type_t itf_type,
67 interface::admin_state_t itf_state,
68 const route_domain& rd,
69 const std::string& tag)
70 : m_hdl(handle_t::INVALID)
75 , m_table_id(m_rd->table_id())
76 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
77 , m_stats_type(stats_type_t::NORMAL)
78 , m_oper(oper_state_t::DOWN)
83 interface::interface(const interface& o)
89 , m_table_id(o.m_table_id)
90 , m_l2_address(o.m_l2_address)
91 , m_stats_type(o.m_stats_type)
98 interface::operator==(const interface& i) const
100 return ((key() == i.key()) &&
101 (m_l2_address.data() == i.m_l2_address.data()) &&
102 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
103 (m_oper == i.m_oper));
106 interface::event_listener::event_listener()
107 : m_status(rc_t::NOOP)
112 interface::event_listener::status()
117 interface::stat_listener::stat_listener()
118 : m_status(rc_t::NOOP)
123 interface::stat_listener::status()
129 * Return the interface type
131 const interface::type_t&
132 interface::type() const
138 interface::handle() const
140 return (singular()->handle_i());
144 interface::handle_i() const
146 return (m_hdl.data());
150 interface::l2_address() const
152 return (m_l2_address.data());
155 interface::const_iterator_t
161 interface::const_iterator_t
170 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
171 m_table_id.data() = route::DEFAULT_TABLE;
173 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
175 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
179 if (stats_type_t::DETAILED == m_stats_type) {
180 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
181 m_stats_type, handle_i(), false));
183 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
187 // If the interface is up, bring it down
188 if (m_state && interface::admin_state_t::UP == m_state.data()) {
189 m_state.data() = interface::admin_state_t::DOWN;
190 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
194 std::queue<cmd*> cmds;
195 HW::enqueue(mk_delete_cmd(cmds));
204 std::queue<cmd*> cmds;
205 HW::enqueue(mk_create_cmd(cmds));
208 if (m_state && interface::admin_state_t::UP == m_state.data()) {
209 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
213 if (stats_type_t::DETAILED == m_stats_type) {
214 m_stats_type.set(rc_t::NOOP);
215 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
216 m_stats_type, handle_i(), true));
218 stat_listener& listener = m_stats->listener();
219 listener.status().set(rc_t::NOOP);
220 m_stats.reset(new interface_cmds::stats_enable_cmd(listener, handle_i()));
221 HW::enqueue(m_stats);
224 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
226 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
228 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
232 interface::~interface()
241 // not in the DB anymore.
242 m_db.release(m_name, this);
246 interface::to_string() const
248 std::ostringstream s;
249 s << "interface:[" << m_name << " type:" << m_type.to_string()
250 << " hdl:" << m_hdl.to_string() << " l2-address:["
251 << m_l2_address.to_string() << "]";
254 s << " rd:" << m_rd->to_string();
257 s << " admin-state:" << m_state.to_string()
258 << " oper-state:" << m_oper.to_string();
260 if (!m_tag.empty()) {
261 s << " tag:[" << m_tag << "]";
270 interface::name() const
275 const interface::key_t&
276 interface::key() const
282 interface::mk_create_cmd(std::queue<cmd*>& q)
284 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
285 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
286 q.push(new interface_cmds::set_tag(m_hdl, m_name));
288 * set the m_tag for pretty-print
291 } else if (type_t::AFPACKET == m_type) {
292 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
294 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
295 } else if (type_t::TAPV2 == m_type) {
297 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
298 } else if (type_t::VHOST == m_type) {
299 q.push(new interface_cmds::vhost_create_cmd(m_hdl, m_name, m_tag));
308 interface::mk_delete_cmd(std::queue<cmd*>& q)
310 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
311 q.push(new interface_cmds::loopback_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 assert(rc_t::UNSET == m_l2_address.rc());
404 m_l2_address.set(rc_t::NOOP);
405 m_l2_address.update(addr);
409 interface::set(const handle_t& hdl)
415 interface::set(const oper_state_t& state)
421 interface::set(const std::string& tag)
427 interface::enable_stats_i(interface::stat_listener& el, const stats_type_t& st)
430 if (stats_type_t::DETAILED == st) {
432 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
433 m_stats_type, handle_i(), true));
435 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
436 HW::enqueue(m_stats);
442 interface::enable_stats(interface::stat_listener& el, const stats_type_t& st)
444 singular()->enable_stats_i(el, st);
447 std::shared_ptr<interface>
448 interface::singular_i() const
450 return (m_db.find_or_add(key(), *this));
453 std::shared_ptr<interface>
454 interface::singular() const
459 std::shared_ptr<interface>
460 interface::find(const key_t& k)
462 return (m_db.find(k));
465 std::shared_ptr<interface>
466 interface::find(const handle_t& handle)
468 return (m_hdl_db[handle].lock());
472 interface::add(const key_t& key, const HW::item<handle_t>& item)
474 std::shared_ptr<interface> sp = find(key);
477 m_hdl_db[item.data()] = sp;
482 interface::remove(const HW::item<handle_t>& item)
484 m_hdl_db.erase(item.data());
488 interface::dump(std::ostream& os)
494 interface::enable_events(interface::event_listener& el)
496 m_events_cmd = std::make_shared<interface_cmds::events_cmd>(el);
497 HW::enqueue(m_events_cmd);
502 interface::disable_events()
504 m_events_cmd.reset();
508 interface::event_handler::handle_populate(const client_db::key_t& key)
511 * dump VPP vhost-user interfaces
513 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
514 std::make_shared<interface_cmds::vhost_dump_cmd>();
519 for (auto& vhost_itf_record : *vcmd) {
520 std::shared_ptr<interface> vitf =
521 interface_factory::new_vhost_user_interface(
522 vhost_itf_record.get_payload());
523 VOM_LOG(log_level_t::DEBUG) << " vhost-dump: " << vitf->to_string();
524 OM::commit(key, *vitf);
528 * dump VPP af-packet interfaces
530 std::shared_ptr<interface_cmds::af_packet_dump_cmd> afcmd =
531 std::make_shared<interface_cmds::af_packet_dump_cmd>();
536 for (auto& af_packet_itf_record : *afcmd) {
537 std::shared_ptr<interface> afitf =
538 interface_factory::new_af_packet_interface(
539 af_packet_itf_record.get_payload());
540 VOM_LOG(log_level_t::DEBUG) << " af_packet-dump: " << afitf->to_string();
541 OM::commit(key, *afitf);
545 * dump VPP tapv2 interfaces
547 std::shared_ptr<tap_interface_cmds::tapv2_dump_cmd> tapv2cmd =
548 std::make_shared<tap_interface_cmds::tapv2_dump_cmd>();
550 HW::enqueue(tapv2cmd);
553 for (auto& tapv2_record : *tapv2cmd) {
554 std::shared_ptr<tap_interface> tapv2itf =
555 interface_factory::new_tap_interface(tapv2_record.get_payload());
556 VOM_LOG(log_level_t::DEBUG) << "tapv2-dump: " << tapv2itf->to_string();
559 * Write each of the discovered interfaces into the OM,
560 * but disable the HW Command q whilst we do, so that no
561 * commands are sent to VPP
563 OM::commit(key, *tapv2itf);
567 * dump VPP interfaces
569 std::shared_ptr<interface_cmds::dump_cmd> cmd =
570 std::make_shared<interface_cmds::dump_cmd>();
575 for (auto& itf_record : *cmd) {
576 auto payload = itf_record.get_payload();
577 VOM_LOG(log_level_t::DEBUG) << "dump: [" << payload.sw_if_index
578 << " name:" << (char*)payload.interface_name
579 << " tag:" << (char*)payload.tag << "]";
581 std::shared_ptr<interface> itf = interface_factory::new_interface(payload);
583 if (itf && interface::type_t::LOCAL != itf->type()) {
584 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
586 * Write each of the discovered interfaces into the OM,
587 * but disable the HW Command q whilst we do, so that no
588 * commands are sent to VPP
590 OM::commit(key, *itf);
593 * Get the address configured on the interface
595 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
596 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
597 l3_binding_cmds::dump_v4_cmd(itf->handle()));
602 for (auto& l3_record : *dcmd) {
603 auto& payload = l3_record.get_payload();
604 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
605 payload.prefix_length);
607 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
609 l3_binding l3(*itf, pfx);
616 * dump VPP bond interfaces
618 std::shared_ptr<bond_interface_cmds::dump_cmd> bcmd =
619 std::make_shared<bond_interface_cmds::dump_cmd>();
624 for (auto& bond_itf_record : *bcmd) {
625 std::shared_ptr<bond_interface> bond_itf =
626 interface_factory::new_bond_interface(bond_itf_record.get_payload());
628 VOM_LOG(log_level_t::DEBUG) << " bond-dump:" << bond_itf->to_string();
631 * Write each of the discovered interfaces into the OM,
632 * but disable the HW Command q whilst we do, so that no
633 * commands are sent to VPP
635 OM::commit(key, *bond_itf);
637 std::shared_ptr<bond_group_binding_cmds::dump_cmd> scmd =
638 std::make_shared<bond_group_binding_cmds::dump_cmd>(
639 bond_group_binding_cmds::dump_cmd(bond_itf->handle()));
644 bond_group_binding::enslaved_itf_t enslaved_itfs;
646 for (auto& slave_itf_record : *scmd) {
647 bond_member slave_itf = interface_factory::new_bond_member_interface(
648 slave_itf_record.get_payload());
650 VOM_LOG(log_level_t::DEBUG) << " slave-dump:" << slave_itf.to_string();
653 * Write each of the discovered interfaces into the OM,
654 * but disable the HW Command q whilst we do, so that no
655 * commands are sent to VPP
657 // OM::commit(slave_itf->key(), *slave_itf);
658 enslaved_itfs.insert(slave_itf);
661 if (!enslaved_itfs.empty()) {
662 bond_group_binding bid(*bond_itf, enslaved_itfs);
664 * Write each of the discovered interfaces into the OM,
665 * but disable the HW Command q whilst we do, so that no
666 * commands are sent to VPP
668 OM::commit(key, bid);
673 interface::event_handler::event_handler()
675 OM::register_listener(this);
676 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
680 interface::event_handler::handle_replay()
686 interface::event_handler::order() const
688 return (dependency_t::INTERFACE);
692 interface::event_handler::show(std::ostream& os)
700 * fd.io coding-style-patch-verification: ON
703 * eval: (c-set-style "mozilla")