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"
28 * A DB of all the interfaces, key on the name
30 singular_db<interface::key_t, interface> interface::m_db;
33 * A DB of all the interfaces, key on VPP's handle
35 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
37 interface::event_handler interface::m_evh;
40 * Construct a new object matching the desried state
42 interface::interface(const std::string& name,
43 interface::type_t itf_type,
44 interface::admin_state_t itf_state,
45 const std::string& tag)
46 : m_hdl(handle_t::INVALID)
50 , m_table_id(route::DEFAULT_TABLE)
51 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
52 , m_stats_type(stats_type_t::NORMAL)
53 , m_oper(oper_state_t::DOWN)
58 interface::interface(const std::string& name,
59 interface::type_t itf_type,
60 interface::admin_state_t itf_state,
61 const route_domain& rd,
62 const std::string& tag)
63 : m_hdl(handle_t::INVALID)
68 , m_table_id(m_rd->table_id())
69 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
70 , m_stats_type(stats_type_t::NORMAL)
71 , m_oper(oper_state_t::DOWN)
76 interface::interface(const interface& o)
82 , m_table_id(o.m_table_id)
83 , m_l2_address(o.m_l2_address)
84 , m_stats_type(o.m_stats_type)
91 interface::operator==(const interface& i) const
93 return ((key() == i.key()) &&
94 (m_l2_address.data() == i.m_l2_address.data()) &&
95 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
96 (m_oper == i.m_oper));
99 interface::event_listener::event_listener()
100 : m_status(rc_t::NOOP)
105 interface::event_listener::status()
110 interface::stat_listener::stat_listener()
111 : m_status(rc_t::NOOP)
116 interface::stat_listener::status()
122 * Return the interface type
124 const interface::type_t&
125 interface::type() const
131 interface::handle() const
133 return (singular()->handle_i());
137 interface::handle_i() const
139 return (m_hdl.data());
143 interface::l2_address() const
145 return (m_l2_address.data());
148 interface::const_iterator_t
151 return m_db.cbegin();
154 interface::const_iterator_t
163 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
164 m_table_id.data() = route::DEFAULT_TABLE;
166 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
168 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
172 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
176 // If the interface is up, bring it down
177 if (m_state && interface::admin_state_t::UP == m_state.data()) {
178 m_state.data() = interface::admin_state_t::DOWN;
179 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
183 std::queue<cmd*> cmds;
184 HW::enqueue(mk_delete_cmd(cmds));
193 std::queue<cmd*> cmds;
194 HW::enqueue(mk_create_cmd(cmds));
197 if (m_state && interface::admin_state_t::UP == m_state.data()) {
198 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
201 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
203 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
205 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
209 interface::~interface()
218 // not in the DB anymore.
219 m_db.release(m_name, this);
223 interface::to_string() const
225 std::ostringstream s;
226 s << "interface:[" << m_name << " type:" << m_type.to_string()
227 << " hdl:" << m_hdl.to_string() << " l2-address:["
228 << m_l2_address.to_string() << "]";
231 s << " rd:" << m_rd->to_string();
234 s << " admin-state:" << m_state.to_string()
235 << " oper-state:" << m_oper.to_string();
237 if (!m_tag.empty()) {
238 s << " tag:[" << m_tag << "]";
247 interface::name() const
252 const interface::key_t&
253 interface::key() const
259 interface::mk_create_cmd(std::queue<cmd*>& q)
261 if (type_t::LOOPBACK == m_type) {
262 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
263 } else if (type_t::BVI == m_type) {
264 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
265 q.push(new interface_cmds::set_tag(m_hdl, m_name));
267 * set the m_tag for pretty-print
270 } else if (type_t::AFPACKET == m_type) {
271 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
273 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
274 } else if (type_t::TAP == m_type) {
275 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
277 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
278 } else if (type_t::VHOST == m_type) {
279 q.push(new interface_cmds::vhost_create_cmd(m_hdl, m_name, m_tag));
288 interface::mk_delete_cmd(std::queue<cmd*>& q)
290 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
291 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
292 } else if (type_t::AFPACKET == m_type) {
293 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
294 } else if (type_t::TAP == m_type) {
295 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
296 } else if (type_t::VHOST == m_type) {
297 q.push(new interface_cmds::vhost_delete_cmd(m_hdl, m_name));
304 interface::update(const interface& desired)
307 * the desired state is always that the interface should be created
309 if (rc_t::OK != m_hdl.rc()) {
310 std::queue<cmd*> cmds;
311 HW::enqueue(mk_create_cmd(cmds));
313 * interface create now, so we can barf early if it fails
319 * If the interface is not created do other commands should be issued
321 if (rc_t::OK != m_hdl.rc())
325 * change the interface state to that which is deisred
327 if (m_state.update(desired.m_state)) {
328 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
332 * change the interface state to that which is deisred
334 if (m_l2_address.update(desired.m_l2_address)) {
335 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
339 * If the interface is mapped into a route domain, set VPP's
342 if (m_rd != desired.m_rd) {
344 * changing route domains. need to remove all L3 bindings, swap the table
345 * then reapply the bindings.
347 auto it = l3_binding::cbegin();
349 while (it != l3_binding::cend()) {
350 if (it->second.lock()->itf().key() == key())
351 it->second.lock()->sweep();
355 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
357 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
359 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
362 it = l3_binding::cbegin();
363 while (it != l3_binding::cend()) {
364 if (it->second.lock()->itf().key() == key())
365 it->second.lock()->replay(); //(*it->second.lock());
368 } else if (!m_table_id && m_rd) {
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));
377 interface::set(const admin_state_t& state)
383 interface::set(const l2_address_t& addr)
385 assert(rc_t::UNSET == m_l2_address.rc());
386 m_l2_address.set(rc_t::NOOP);
387 m_l2_address.update(addr);
391 interface::set(const handle_t& hdl)
397 interface::set(const oper_state_t& state)
403 interface::set(const std::string& tag)
409 interface::enable_stats_i(interface::stat_listener& el, const stats_type_t& st)
412 if (stats_type_t::DETAILED == st) {
414 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
415 m_stats_type, handle_i(), true));
417 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
418 HW::enqueue(m_stats);
424 interface::enable_stats(interface::stat_listener& el, const stats_type_t& st)
426 singular()->enable_stats_i(el, st);
429 std::shared_ptr<interface>
430 interface::singular_i() const
432 return (m_db.find_or_add(key(), *this));
435 std::shared_ptr<interface>
436 interface::singular() const
441 std::shared_ptr<interface>
442 interface::find(const key_t& k)
444 return (m_db.find(k));
447 std::shared_ptr<interface>
448 interface::find(const handle_t& handle)
450 return (m_hdl_db[handle].lock());
454 interface::add(const key_t& key, const HW::item<handle_t>& item)
456 std::shared_ptr<interface> sp = find(key);
459 m_hdl_db[item.data()] = sp;
464 interface::remove(const HW::item<handle_t>& item)
466 m_hdl_db.erase(item.data());
470 interface::dump(std::ostream& os)
476 interface::event_handler::handle_populate(const client_db::key_t& key)
479 * dump VPP current states
481 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
482 std::make_shared<interface_cmds::vhost_dump_cmd>();
487 for (auto& vhost_itf_record : *vcmd) {
488 std::shared_ptr<interface> vitf =
489 interface_factory::new_vhost_user_interface(
490 vhost_itf_record.get_payload());
491 VOM_LOG(log_level_t::DEBUG) << " vhost-dump: " << vitf->to_string();
492 OM::commit(key, *vitf);
495 std::shared_ptr<interface_cmds::dump_cmd> cmd =
496 std::make_shared<interface_cmds::dump_cmd>();
501 for (auto& itf_record : *cmd) {
502 std::shared_ptr<interface> itf =
503 interface_factory::new_interface(itf_record.get_payload());
505 if (itf && interface::type_t::LOCAL != itf->type()) {
506 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
508 * Write each of the discovered interfaces into the OM,
509 * but disable the HW Command q whilst we do, so that no
510 * commands are sent to VPP
512 OM::commit(key, *itf);
515 * Get the address configured on the interface
517 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
518 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
519 l3_binding_cmds::dump_v4_cmd(itf->handle()));
524 for (auto& l3_record : *dcmd) {
525 auto& payload = l3_record.get_payload();
526 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
527 payload.prefix_length);
529 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
531 l3_binding l3(*itf, pfx);
537 std::shared_ptr<bond_interface_cmds::dump_cmd> bcmd =
538 std::make_shared<bond_interface_cmds::dump_cmd>();
543 for (auto& bond_itf_record : *bcmd) {
544 std::shared_ptr<bond_interface> bond_itf =
545 interface_factory::new_bond_interface(bond_itf_record.get_payload());
547 VOM_LOG(log_level_t::DEBUG) << " bond-dump:" << bond_itf->to_string();
550 * Write each of the discovered interfaces into the OM,
551 * but disable the HW Command q whilst we do, so that no
552 * commands are sent to VPP
554 OM::commit(key, *bond_itf);
556 std::shared_ptr<bond_group_binding_cmds::dump_cmd> scmd =
557 std::make_shared<bond_group_binding_cmds::dump_cmd>(
558 bond_group_binding_cmds::dump_cmd(bond_itf->handle()));
563 bond_group_binding::enslaved_itf_t enslaved_itfs;
565 for (auto& slave_itf_record : *scmd) {
566 bond_member slave_itf = interface_factory::new_bond_member_interface(
567 slave_itf_record.get_payload());
569 VOM_LOG(log_level_t::DEBUG) << " slave-dump:" << slave_itf.to_string();
572 * Write each of the discovered interfaces into the OM,
573 * but disable the HW Command q whilst we do, so that no
574 * commands are sent to VPP
576 // OM::commit(slave_itf->key(), *slave_itf);
577 enslaved_itfs.insert(slave_itf);
580 if (!enslaved_itfs.empty()) {
581 bond_group_binding bid(*bond_itf, enslaved_itfs);
583 * Write each of the discovered interfaces into the OM,
584 * but disable the HW Command q whilst we do, so that no
585 * commands are sent to VPP
587 OM::commit(key, bid);
592 interface::event_handler::event_handler()
594 OM::register_listener(this);
595 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
599 interface::event_handler::handle_replay()
605 interface::event_handler::order() const
607 return (dependency_t::INTERFACE);
611 interface::event_handler::show(std::ostream& os)
619 * fd.io coding-style-patch-verification: ON
622 * eval: (c-set-style "mozilla")