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/interface_cmds.hpp"
18 #include "vom/interface_factory.hpp"
19 #include "vom/l3_binding_cmds.hpp"
20 #include "vom/logger.hpp"
21 #include "vom/prefix.hpp"
25 * A DB of all the interfaces, key on the name
27 singular_db<interface::key_t, interface> interface::m_db;
30 * A DB of all the interfaces, key on VPP's handle
32 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
34 interface::event_handler interface::m_evh;
37 * Construct a new object matching the desried state
39 interface::interface(const std::string& name,
40 interface::type_t itf_type,
41 interface::admin_state_t itf_state,
42 const std::string& tag)
43 : m_hdl(handle_t::INVALID)
47 , m_table_id(route::DEFAULT_TABLE)
48 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
49 , m_oper(oper_state_t::DOWN)
54 interface::interface(const std::string& name,
55 interface::type_t itf_type,
56 interface::admin_state_t itf_state,
57 const route_domain& rd,
58 const std::string& tag)
59 : m_hdl(handle_t::INVALID)
64 , m_table_id(m_rd->table_id())
65 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
66 , m_oper(oper_state_t::DOWN)
71 interface::interface(const interface& o)
77 , m_table_id(o.m_table_id)
78 , m_l2_address(o.m_l2_address)
85 interface::operator==(const interface& i) const
87 return ((key() == i.key()) &&
88 (m_l2_address.data() == i.m_l2_address.data()) &&
89 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
90 (m_oper == i.m_oper));
93 interface::event_listener::event_listener()
94 : m_status(rc_t::NOOP)
99 interface::event_listener::status()
104 interface::stat_listener::stat_listener()
105 : m_status(rc_t::NOOP)
110 interface::stat_listener::status()
116 * Return the interface type
118 const interface::type_t&
119 interface::type() const
125 interface::handle() const
127 return (singular()->handle_i());
131 interface::handle_i() const
133 return (m_hdl.data());
137 interface::l2_address() const
139 return (m_l2_address.data());
142 interface::const_iterator_t
145 return m_db.cbegin();
148 interface::const_iterator_t
157 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
158 m_table_id.data() = route::DEFAULT_TABLE;
160 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
162 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
166 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
170 // If the interface is up, bring it down
171 if (m_state && interface::admin_state_t::UP == m_state.data()) {
172 m_state.data() = interface::admin_state_t::DOWN;
173 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
177 std::queue<cmd*> cmds;
178 HW::enqueue(mk_delete_cmd(cmds));
187 std::queue<cmd*> cmds;
188 HW::enqueue(mk_create_cmd(cmds));
191 if (m_state && interface::admin_state_t::UP == m_state.data()) {
192 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
195 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
197 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
199 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
203 interface::~interface()
212 // not in the DB anymore.
213 m_db.release(m_name, this);
217 interface::to_string() const
219 std::ostringstream s;
220 s << "interface:[" << m_name << " type:" << m_type.to_string()
221 << " hdl:" << m_hdl.to_string() << " l2-address:["
222 << m_l2_address.to_string() << "]";
225 s << " rd:" << m_rd->to_string();
228 s << " admin-state:" << m_state.to_string()
229 << " oper-state:" << m_oper.to_string();
231 if (!m_tag.empty()) {
232 s << " tag:[" << m_tag << "]";
241 interface::name() const
246 const interface::key_t&
247 interface::key() const
253 interface::mk_create_cmd(std::queue<cmd*>& q)
255 if (type_t::LOOPBACK == m_type) {
256 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
257 } else if (type_t::BVI == m_type) {
258 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
259 q.push(new interface_cmds::set_tag(m_hdl, m_name));
261 * set the m_tag for pretty-print
264 } else if (type_t::AFPACKET == m_type) {
265 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
267 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
268 } else if (type_t::TAP == m_type) {
269 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
271 q.push(new interface_cmds::set_tag(m_hdl, m_tag));
272 } else if (type_t::VHOST == m_type) {
273 q.push(new interface_cmds::vhost_create_cmd(m_hdl, m_name, m_tag));
282 interface::mk_delete_cmd(std::queue<cmd*>& q)
284 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
285 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
286 } else if (type_t::AFPACKET == m_type) {
287 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
288 } else if (type_t::TAP == m_type) {
289 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
290 } else if (type_t::VHOST == m_type) {
291 q.push(new interface_cmds::vhost_delete_cmd(m_hdl, m_name));
298 interface::update(const interface& desired)
301 * the desired state is always that the interface should be created
303 if (rc_t::OK != m_hdl.rc()) {
304 std::queue<cmd*> cmds;
305 HW::enqueue(mk_create_cmd(cmds));
307 * interface create now, so we can barf early if it fails
313 * If the interface is not created do other commands should be issued
315 if (rc_t::OK != m_hdl.rc())
319 * change the interface state to that which is deisred
321 if (m_state.update(desired.m_state)) {
322 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
326 * change the interface state to that which is deisred
328 if (m_l2_address.update(desired.m_l2_address)) {
329 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
333 * If the interface is mapped into a route domain, set VPP's
336 if (m_rd != desired.m_rd) {
338 * changing route domains. need to remove all L3 bindings, swap the table
339 * then reapply the bindings.
341 auto it = l3_binding::cbegin();
343 while (it != l3_binding::cend()) {
344 if (it->second.lock()->itf().key() == key())
345 it->second.lock()->sweep();
349 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
351 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
353 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
356 it = l3_binding::cbegin();
357 while (it != l3_binding::cend()) {
358 if (it->second.lock()->itf().key() == key())
359 it->second.lock()->replay(); //(*it->second.lock());
362 } else if (!m_table_id && m_rd) {
364 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
366 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
371 interface::set(const admin_state_t& state)
377 interface::set(const l2_address_t& addr)
379 assert(rc_t::UNSET == m_l2_address.rc());
380 m_l2_address.set(rc_t::NOOP);
381 m_l2_address.update(addr);
385 interface::set(const handle_t& hdl)
391 interface::set(const oper_state_t& state)
397 interface::set(const std::string& tag)
403 interface::enable_stats_i(interface::stat_listener& el)
406 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
407 HW::enqueue(m_stats);
413 interface::enable_stats(interface::stat_listener& el)
415 singular()->enable_stats_i(el);
418 std::shared_ptr<interface>
419 interface::singular_i() const
421 return (m_db.find_or_add(key(), *this));
424 std::shared_ptr<interface>
425 interface::singular() const
430 std::shared_ptr<interface>
431 interface::find(const key_t& k)
433 return (m_db.find(k));
436 std::shared_ptr<interface>
437 interface::find(const handle_t& handle)
439 return (m_hdl_db[handle].lock());
443 interface::add(const key_t& key, const HW::item<handle_t>& item)
445 std::shared_ptr<interface> sp = find(key);
448 m_hdl_db[item.data()] = sp;
453 interface::remove(const HW::item<handle_t>& item)
455 m_hdl_db.erase(item.data());
459 interface::dump(std::ostream& os)
465 interface::event_handler::handle_populate(const client_db::key_t& key)
467 std::shared_ptr<interface_cmds::vhost_dump_cmd> vcmd =
468 std::make_shared<interface_cmds::vhost_dump_cmd>();
473 for (auto& vhost_itf_record : *vcmd) {
474 std::shared_ptr<interface> vitf =
475 interface_factory::new_vhost_user_interface(
476 vhost_itf_record.get_payload());
477 VOM_LOG(log_level_t::DEBUG) << "dump: " << vitf->to_string();
478 OM::commit(key, *vitf);
482 * dump VPP current states
484 std::shared_ptr<interface_cmds::dump_cmd> cmd =
485 std::make_shared<interface_cmds::dump_cmd>();
490 for (auto& itf_record : *cmd) {
491 std::shared_ptr<interface> itf =
492 interface_factory::new_interface(itf_record.get_payload());
494 if (itf && interface::type_t::LOCAL != itf->type()) {
495 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
497 * Write each of the discovered interfaces into the OM,
498 * but disable the HW Command q whilst we do, so that no
499 * commands are sent to VPP
501 OM::commit(key, *itf);
504 * Get the address configured on the interface
506 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
507 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
508 l3_binding_cmds::dump_v4_cmd(itf->handle()));
513 for (auto& l3_record : *dcmd) {
514 auto& payload = l3_record.get_payload();
515 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
516 payload.prefix_length);
518 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
520 l3_binding l3(*itf, pfx);
527 interface::event_handler::event_handler()
529 OM::register_listener(this);
530 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
534 interface::event_handler::handle_replay()
540 interface::event_handler::order() const
542 return (dependency_t::INTERFACE);
546 interface::event_handler::show(std::ostream& os)
554 * fd.io coding-style-patch-verification: ON
557 * eval: (c-set-style "mozilla")