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/cmd.hpp"
18 #include "vom/l3_binding.hpp"
19 #include "vom/logger.hpp"
20 #include "vom/prefix.hpp"
24 * A DB of all the interfaces, key on the name
26 singular_db<const std::string, interface> interface::m_db;
29 * A DB of all the interfaces, key on VPP's handle
31 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
33 interface::event_handler interface::m_evh;
36 * Construct a new object matching the desried state
38 interface::interface(const std::string& name,
39 interface::type_t itf_type,
40 interface::admin_state_t itf_state)
41 : m_hdl(handle_t::INVALID)
45 , m_table_id(route::DEFAULT_TABLE)
46 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
47 , m_oper(oper_state_t::DOWN)
51 interface::interface(const handle_t& handle,
52 const l2_address_t& l2_address,
53 const std::string& name,
54 interface::type_t type,
55 interface::admin_state_t state)
60 , m_table_id(route::DEFAULT_TABLE)
61 , m_l2_address(l2_address)
62 , m_oper(oper_state_t::DOWN)
66 interface::interface(const std::string& name,
67 interface::type_t itf_type,
68 interface::admin_state_t itf_state,
69 const route_domain& rd)
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_oper(oper_state_t::DOWN)
81 interface::interface(const interface& o)
87 , m_table_id(o.m_table_id)
88 , m_l2_address(o.m_l2_address)
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 (m_hdl.data());
131 interface::l2_address() const
133 return (m_l2_address.data());
136 interface::const_iterator_t
139 return m_db.cbegin();
142 interface::const_iterator_t
152 m_table_id.data() = route::DEFAULT_TABLE;
153 HW::enqueue(new set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
154 HW::enqueue(new set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
157 // If the interface is up, bring it down
158 if (m_state && interface::admin_state_t::UP == m_state.data()) {
159 m_state.data() = interface::admin_state_t::DOWN;
160 HW::enqueue(new state_change_cmd(m_state, m_hdl));
163 std::queue<cmd*> cmds;
164 HW::enqueue(mk_delete_cmd(cmds));
173 std::queue<cmd*> cmds;
174 HW::enqueue(mk_create_cmd(cmds));
177 if (m_state && interface::admin_state_t::UP == m_state.data()) {
178 HW::enqueue(new state_change_cmd(m_state, m_hdl));
182 HW::enqueue(new set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
183 HW::enqueue(new set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
187 interface::~interface()
196 // not in the DB anymore.
197 m_db.release(m_name, this);
201 interface::to_string() const
203 std::ostringstream s;
204 s << "interface:[" << m_name << " type:" << m_type.to_string()
205 << " hdl:" << m_hdl.to_string()
206 << " l2-address:" << m_l2_address.to_string();
209 s << " rd:" << m_rd->to_string();
212 s << " admin-state:" << m_state.to_string()
213 << " oper-state:" << m_oper.to_string() << "]";
219 interface::name() const
224 const interface::key_type&
225 interface::key() const
231 interface::mk_create_cmd(std::queue<cmd*>& q)
233 if (type_t::LOOPBACK == m_type) {
234 q.push(new loopback_create_cmd(m_hdl, m_name));
235 } else if (type_t::BVI == m_type) {
236 q.push(new loopback_create_cmd(m_hdl, m_name));
237 q.push(new set_tag(m_hdl, m_name));
238 } else if (type_t::AFPACKET == m_type) {
239 q.push(new af_packet_create_cmd(m_hdl, m_name));
240 } else if (type_t::TAP == m_type) {
241 q.push(new tap_create_cmd(m_hdl, m_name));
248 interface::mk_delete_cmd(std::queue<cmd*>& q)
250 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
251 q.push(new loopback_delete_cmd(m_hdl));
252 } else if (type_t::AFPACKET == m_type) {
253 q.push(new af_packet_delete_cmd(m_hdl, m_name));
254 } else if (type_t::TAP == m_type) {
255 q.push(new tap_delete_cmd(m_hdl));
262 interface::update(const interface& desired)
265 * the desired state is always that the interface should be created
267 if (rc_t::OK != m_hdl.rc()) {
268 std::queue<cmd*> cmds;
269 HW::enqueue(mk_create_cmd(cmds));
273 * change the interface state to that which is deisred
275 if (m_state.update(desired.m_state)) {
276 HW::enqueue(new state_change_cmd(m_state, m_hdl));
280 * change the interface state to that which is deisred
282 if (m_l2_address.update(desired.m_l2_address)) {
283 HW::enqueue(new set_mac_cmd(m_l2_address, m_hdl));
287 * If the interface is mapped into a route domain, set VPP's
290 if (!m_table_id && m_rd) {
291 HW::enqueue(new set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
292 HW::enqueue(new set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
297 interface::set(const l2_address_t& addr)
299 assert(rc_t::UNSET == m_l2_address.rc());
300 m_l2_address.set(rc_t::NOOP);
301 m_l2_address.update(addr);
305 interface::set(const oper_state_t& state)
310 std::shared_ptr<interface>
311 interface::singular_i() const
313 return (m_db.find_or_add(name(), *this));
316 std::shared_ptr<interface>
317 interface::singular() const
322 std::shared_ptr<interface>
323 interface::find(const std::string& name)
325 return (m_db.find(name));
328 std::shared_ptr<interface>
329 interface::find(const handle_t& handle)
331 return (m_hdl_db[handle].lock());
335 interface::add(const std::string& name, const HW::item<handle_t>& item)
337 std::shared_ptr<interface> sp = find(name);
340 m_hdl_db[item.data()] = sp;
345 interface::remove(const HW::item<handle_t>& item)
347 m_hdl_db.erase(item.data());
351 interface::dump(std::ostream& os)
357 interface::event_handler::handle_populate(const client_db::key_t& key)
360 * dump VPP current states
362 std::shared_ptr<interface::dump_cmd> cmd(new interface::dump_cmd());
367 for (auto& itf_record : *cmd) {
368 std::unique_ptr<interface> itf =
369 interface::new_interface(itf_record.get_payload());
371 if (itf && interface::type_t::LOCAL != itf->type()) {
372 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
374 * Write each of the discovered interfaces into the OM,
375 * but disable the HW Command q whilst we do, so that no
376 * commands are sent to VPP
378 OM::commit(key, *itf);
381 * Get the address configured on the interface
383 std::shared_ptr<l3_binding::dump_v4_cmd> dcmd =
384 std::make_shared<l3_binding::dump_v4_cmd>(
385 l3_binding::dump_v4_cmd(itf->handle()));
390 for (auto& l3_record : *dcmd) {
391 auto& payload = l3_record.get_payload();
392 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
393 payload.prefix_length);
395 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
397 l3_binding l3(*itf, pfx);
404 interface::event_handler::event_handler()
406 OM::register_listener(this);
407 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
411 interface::event_handler::handle_replay()
417 interface::event_handler::order() const
419 return (dependency_t::INTERFACE);
423 interface::event_handler::show(std::ostream& os)
429 * fd.io coding-style-patch-verification: ON
432 * eval: (c-set-style "mozilla")