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"
29 * A DB of all the interfaces, key on the name
31 singular_db<interface::key_t, interface> interface::m_db;
34 * A DB of all the interfaces, key on VPP's handle
36 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
38 interface::event_handler interface::m_evh;
41 * Construct a new object matching the desried state
43 interface::interface(const std::string& name,
44 interface::type_t itf_type,
45 interface::admin_state_t itf_state,
46 const std::string& tag)
47 : m_hdl(handle_t::INVALID)
51 , m_table_id(route::DEFAULT_TABLE)
52 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
53 , m_stats_type(stats_type_t::NORMAL)
54 , m_oper(oper_state_t::DOWN)
59 interface::interface(const std::string& name,
60 interface::type_t itf_type,
61 interface::admin_state_t itf_state,
62 const route_domain& rd,
63 const std::string& tag)
64 : m_hdl(handle_t::INVALID)
69 , m_table_id(m_rd->table_id())
70 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
71 , m_stats_type(stats_type_t::NORMAL)
72 , m_oper(oper_state_t::DOWN)
77 interface::interface(const interface& o)
83 , m_table_id(o.m_table_id)
84 , m_l2_address(o.m_l2_address)
85 , m_stats_type(o.m_stats_type)
92 interface::operator==(const interface& i) const
94 return ((key() == i.key()) &&
95 (m_l2_address.data() == i.m_l2_address.data()) &&
96 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
97 (m_oper == i.m_oper));
100 interface::event_listener::event_listener()
101 : m_status(rc_t::NOOP)
106 interface::event_listener::status()
111 interface::stat_listener::stat_listener()
112 : m_status(rc_t::NOOP)
117 interface::stat_listener::status()
123 * Return the interface type
125 const interface::type_t&
126 interface::type() const
132 interface::handle() const
134 return (singular()->handle_i());
138 interface::handle_i() const
140 return (m_hdl.data());
144 interface::l2_address() const
146 return (m_l2_address.data());
149 interface::const_iterator_t
155 interface::const_iterator_t
164 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
165 m_table_id.data() = route::DEFAULT_TABLE;
167 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
169 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
173 if (stats_type_t::DETAILED == m_stats_type) {
174 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
175 m_stats_type, handle_i(), false));
177 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
181 // If the interface is up, bring it down
182 if (m_state && interface::admin_state_t::UP == m_state.data()) {
183 m_state.data() = interface::admin_state_t::DOWN;
184 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
188 std::queue<cmd*> cmds;
189 HW::enqueue(mk_delete_cmd(cmds));
198 std::queue<cmd*> cmds;
199 HW::enqueue(mk_create_cmd(cmds));
202 if (m_state && interface::admin_state_t::UP == m_state.data()) {
203 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
207 if (stats_type_t::DETAILED == m_stats_type) {
208 m_stats_type.set(rc_t::NOOP);
209 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
210 m_stats_type, handle_i(), true));
212 stat_listener& listener = m_stats->listener();
213 listener.status().set(rc_t::NOOP);
214 m_stats.reset(new interface_cmds::stats_enable_cmd(listener, handle_i()));
215 HW::enqueue(m_stats);
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) || (type_t::BVI == 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::AFPACKET == m_type) {
286 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
288 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
289 } else if (type_t::TAP == m_type) {
290 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
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::TAP == m_type) {
310 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
311 } else if (type_t::VHOST == m_type) {
312 q.push(new interface_cmds::vhost_delete_cmd(m_hdl, m_name));
319 interface::update(const interface& desired)
322 * the desired state is always that the interface should be created
324 if (rc_t::OK != m_hdl.rc()) {
325 std::queue<cmd*> cmds;
326 HW::enqueue(mk_create_cmd(cmds));
328 * interface create now, so we can barf early if it fails
334 * If the interface is not created do other commands should be issued
336 if (rc_t::OK != m_hdl.rc())
340 * change the interface state to that which is deisred
342 if (m_state.update(desired.m_state)) {
343 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
347 * change the interface state to that which is deisred
349 if (m_l2_address.update(desired.m_l2_address)) {
350 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
354 * If the interface is mapped into a route domain, set VPP's
357 if (m_rd != desired.m_rd) {
359 * changing route domains. need to remove all L3 bindings, swap the table
360 * then reapply the bindings.
362 auto it = l3_binding::cbegin();
364 while (it != l3_binding::cend()) {
365 if (it->second.lock()->itf().key() == key())
366 it->second.lock()->sweep();
370 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
372 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
374 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
377 it = l3_binding::cbegin();
378 while (it != l3_binding::cend()) {
379 if (it->second.lock()->itf().key() == key())
380 it->second.lock()->replay(); //(*it->second.lock());
383 } else if (!m_table_id && m_rd) {
385 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
387 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
392 interface::set(const admin_state_t& state)
398 interface::set(const l2_address_t& addr)
400 assert(rc_t::UNSET == m_l2_address.rc());
401 m_l2_address.set(rc_t::NOOP);
402 m_l2_address.update(addr);
406 interface::set(const handle_t& hdl)
412 interface::set(const oper_state_t& state)
418 interface::set(const std::string& tag)
424 interface::enable_stats_i(interface::stat_listener& el, const stats_type_t& st)
427 if (stats_type_t::DETAILED == st) {
429 HW::enqueue(new interface_cmds::collect_detail_stats_change_cmd(
430 m_stats_type, handle_i(), true));
432 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
433 HW::enqueue(m_stats);
439 interface::enable_stats(interface::stat_listener& el, const stats_type_t& st)
441 singular()->enable_stats_i(el, st);
444 std::shared_ptr<interface>
445 interface::singular_i() const
447 return (m_db.find_or_add(key(), *this));
450 std::shared_ptr<interface>
451 interface::singular() const
456 std::shared_ptr<interface>
457 interface::find(const key_t& k)
459 return (m_db.find(k));
462 std::shared_ptr<interface>
463 interface::find(const handle_t& handle)
465 return (m_hdl_db[handle].lock());
469 interface::add(const key_t& key, const HW::item<handle_t>& item)
471 std::shared_ptr<interface> sp = find(key);
474 m_hdl_db[item.data()] = sp;
479 interface::remove(const HW::item<handle_t>& item)
481 m_hdl_db.erase(item.data());
485 interface::dump(std::ostream& os)
491 interface::event_handler::handle_populate(const client_db::key_t& key)
494 * dump VPP current states
496 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
497 std::make_shared<interface_cmds::vhost_dump_cmd>();
502 for (auto& vhost_itf_record : *vcmd) {
503 std::shared_ptr<interface> vitf =
504 interface_factory::new_vhost_user_interface(
505 vhost_itf_record.get_payload());
506 VOM_LOG(log_level_t::DEBUG) << " vhost-dump: " << vitf->to_string();
507 OM::commit(key, *vitf);
510 std::shared_ptr<interface_cmds::dump_cmd> cmd =
511 std::make_shared<interface_cmds::dump_cmd>();
516 for (auto& itf_record : *cmd) {
517 auto payload = itf_record.get_payload();
518 VOM_LOG(log_level_t::DEBUG) << "dump: [" << payload.sw_if_index
519 << " name:" << (char*)payload.interface_name
520 << " tag:" << (char*)payload.tag << "]";
522 std::shared_ptr<interface> itf = interface_factory::new_interface(payload);
524 if (itf && interface::type_t::LOCAL != itf->type()) {
525 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
527 * Write each of the discovered interfaces into the OM,
528 * but disable the HW Command q whilst we do, so that no
529 * commands are sent to VPP
531 OM::commit(key, *itf);
534 * Get the address configured on the interface
536 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
537 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
538 l3_binding_cmds::dump_v4_cmd(itf->handle()));
543 for (auto& l3_record : *dcmd) {
544 auto& payload = l3_record.get_payload();
545 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
546 payload.prefix_length);
548 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
550 l3_binding l3(*itf, pfx);
556 std::shared_ptr<bond_interface_cmds::dump_cmd> bcmd =
557 std::make_shared<bond_interface_cmds::dump_cmd>();
562 for (auto& bond_itf_record : *bcmd) {
563 std::shared_ptr<bond_interface> bond_itf =
564 interface_factory::new_bond_interface(bond_itf_record.get_payload());
566 VOM_LOG(log_level_t::DEBUG) << " bond-dump:" << bond_itf->to_string();
569 * Write each of the discovered interfaces into the OM,
570 * but disable the HW Command q whilst we do, so that no
571 * commands are sent to VPP
573 OM::commit(key, *bond_itf);
575 std::shared_ptr<bond_group_binding_cmds::dump_cmd> scmd =
576 std::make_shared<bond_group_binding_cmds::dump_cmd>(
577 bond_group_binding_cmds::dump_cmd(bond_itf->handle()));
582 bond_group_binding::enslaved_itf_t enslaved_itfs;
584 for (auto& slave_itf_record : *scmd) {
585 bond_member slave_itf = interface_factory::new_bond_member_interface(
586 slave_itf_record.get_payload());
588 VOM_LOG(log_level_t::DEBUG) << " slave-dump:" << slave_itf.to_string();
591 * Write each of the discovered interfaces into the OM,
592 * but disable the HW Command q whilst we do, so that no
593 * commands are sent to VPP
595 // OM::commit(slave_itf->key(), *slave_itf);
596 enslaved_itfs.insert(slave_itf);
599 if (!enslaved_itfs.empty()) {
600 bond_group_binding bid(*bond_itf, enslaved_itfs);
602 * Write each of the discovered interfaces into the OM,
603 * but disable the HW Command q whilst we do, so that no
604 * commands are sent to VPP
606 OM::commit(key, bid);
611 interface::event_handler::event_handler()
613 OM::register_listener(this);
614 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
618 interface::event_handler::handle_replay()
624 interface::event_handler::order() const
626 return (dependency_t::INTERFACE);
630 interface::event_handler::show(std::ostream& os)
638 * fd.io coding-style-patch-verification: ON
641 * eval: (c-set-style "mozilla")