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<const std::string, 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 : m_hdl(handle_t::INVALID)
46 , m_table_id(route::DEFAULT_TABLE)
47 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
48 , m_oper(oper_state_t::DOWN)
52 interface::interface(const handle_t& handle,
53 const l2_address_t& l2_address,
54 const std::string& name,
55 interface::type_t type,
56 interface::admin_state_t state)
61 , m_table_id(route::DEFAULT_TABLE)
62 , m_l2_address(l2_address)
63 , m_oper(oper_state_t::DOWN)
67 interface::interface(const std::string& name,
68 interface::type_t itf_type,
69 interface::admin_state_t itf_state,
70 const route_domain& rd)
71 : m_hdl(handle_t::INVALID)
76 , m_table_id(m_rd->table_id())
77 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
78 , m_oper(oper_state_t::DOWN)
82 interface::interface(const interface& o)
88 , m_table_id(o.m_table_id)
89 , m_l2_address(o.m_l2_address)
94 interface::event_listener::event_listener()
95 : m_status(rc_t::NOOP)
100 interface::event_listener::status()
105 interface::stat_listener::stat_listener()
106 : m_status(rc_t::NOOP)
111 interface::stat_listener::status()
117 * Return the interface type
119 const interface::type_t&
120 interface::type() const
126 interface::handle() const
128 return (m_hdl.data());
132 interface::l2_address() const
134 return (m_l2_address.data());
137 interface::const_iterator_t
140 return m_db.cbegin();
143 interface::const_iterator_t
152 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
153 m_table_id.data() = route::DEFAULT_TABLE;
155 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
157 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
160 // If the interface is up, bring it down
161 if (m_state && interface::admin_state_t::UP == m_state.data()) {
162 m_state.data() = interface::admin_state_t::DOWN;
163 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
166 std::queue<cmd*> cmds;
167 HW::enqueue(mk_delete_cmd(cmds));
176 std::queue<cmd*> cmds;
177 HW::enqueue(mk_create_cmd(cmds));
180 if (m_state && interface::admin_state_t::UP == m_state.data()) {
181 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
184 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
186 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
188 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
192 interface::~interface()
201 // not in the DB anymore.
202 m_db.release(m_name, this);
206 interface::to_string() const
208 std::ostringstream s;
209 s << "interface:[" << m_name << " type:" << m_type.to_string()
210 << " hdl:" << m_hdl.to_string()
211 << " l2-address:" << m_l2_address.to_string();
214 s << " rd:" << m_rd->to_string();
217 s << " admin-state:" << m_state.to_string()
218 << " oper-state:" << m_oper.to_string() << "]";
224 interface::name() const
229 const interface::key_type&
230 interface::key() const
236 interface::mk_create_cmd(std::queue<cmd*>& q)
238 if (type_t::LOOPBACK == m_type) {
239 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
240 } else if (type_t::BVI == m_type) {
241 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
242 q.push(new interface_cmds::set_tag(m_hdl, m_name));
243 } else if (type_t::AFPACKET == m_type) {
244 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
245 } else if (type_t::TAP == m_type) {
246 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
253 interface::mk_delete_cmd(std::queue<cmd*>& q)
255 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
256 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
257 } else if (type_t::AFPACKET == m_type) {
258 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
259 } else if (type_t::TAP == m_type) {
260 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
267 interface::update(const interface& desired)
270 * the desired state is always that the interface should be created
272 if (rc_t::OK != m_hdl.rc()) {
273 std::queue<cmd*> cmds;
274 HW::enqueue(mk_create_cmd(cmds));
278 * change the interface state to that which is deisred
280 if (m_state.update(desired.m_state)) {
281 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
285 * change the interface state to that which is deisred
287 if (m_l2_address.update(desired.m_l2_address)) {
288 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
292 * If the interface is mapped into a route domain, set VPP's
295 if (m_rd != desired.m_rd) {
297 * changing route domains. need to remove all L3 bindings, swap the table
298 * then reapply the bindings.
300 auto it = l3_binding::cbegin();
302 while (it != l3_binding::cend()) {
303 if (it->second.lock()->itf().key() == key())
304 it->second.lock()->sweep();
308 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
310 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
312 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
315 it = l3_binding::cbegin();
316 while (it != l3_binding::cend()) {
317 if (it->second.lock()->itf().key() == key())
318 it->second.lock()->replay(); //(*it->second.lock());
321 } else if (!m_table_id && m_rd) {
323 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
325 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
330 interface::set(const l2_address_t& addr)
332 assert(rc_t::UNSET == m_l2_address.rc());
333 m_l2_address.set(rc_t::NOOP);
334 m_l2_address.update(addr);
338 interface::set(const oper_state_t& state)
343 std::shared_ptr<interface>
344 interface::singular_i() const
346 return (m_db.find_or_add(name(), *this));
349 std::shared_ptr<interface>
350 interface::singular() const
355 std::shared_ptr<interface>
356 interface::find(const std::string& name)
358 return (m_db.find(name));
361 std::shared_ptr<interface>
362 interface::find(const handle_t& handle)
364 return (m_hdl_db[handle].lock());
368 interface::add(const std::string& name, const HW::item<handle_t>& item)
370 std::shared_ptr<interface> sp = find(name);
373 m_hdl_db[item.data()] = sp;
378 interface::remove(const HW::item<handle_t>& item)
380 m_hdl_db.erase(item.data());
384 interface::dump(std::ostream& os)
390 interface::event_handler::handle_populate(const client_db::key_t& key)
393 * dump VPP current states
395 std::shared_ptr<interface_cmds::dump_cmd> cmd(new interface_cmds::dump_cmd());
400 for (auto& itf_record : *cmd) {
401 std::unique_ptr<interface> itf =
402 interface_factory::new_interface(itf_record.get_payload());
404 if (itf && interface::type_t::LOCAL != itf->type()) {
405 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
407 * Write each of the discovered interfaces into the OM,
408 * but disable the HW Command q whilst we do, so that no
409 * commands are sent to VPP
411 OM::commit(key, *itf);
414 * Get the address configured on the interface
416 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
417 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
418 l3_binding_cmds::dump_v4_cmd(itf->handle()));
423 for (auto& l3_record : *dcmd) {
424 auto& payload = l3_record.get_payload();
425 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
426 payload.prefix_length);
428 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
430 l3_binding l3(*itf, pfx);
437 interface::event_handler::event_handler()
439 OM::register_listener(this);
440 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
444 interface::event_handler::handle_replay()
450 interface::event_handler::order() const
452 return (dependency_t::INTERFACE);
456 interface::event_handler::show(std::ostream& os)
462 * fd.io coding-style-patch-verification: ON
465 * eval: (c-set-style "mozilla")