blob: 9fa96af2d973843b3191d1c35387fa9a931be564 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include "vom/interface.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/interface_cmds.hpp"
18#include "vom/interface_factory.hpp"
19#include "vom/l3_binding_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070020#include "vom/logger.hpp"
21#include "vom/prefix.hpp"
22
23namespace VOM {
24/**
25 * A DB of all the interfaces, key on the name
26 */
Neale Rannsfd920602017-11-23 12:15:00 -080027singular_db<interface::key_t, interface> interface::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -070028
29/**
30 * A DB of all the interfaces, key on VPP's handle
31 */
32std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
33
34interface::event_handler interface::m_evh;
35
36/**
37 * Construct a new object matching the desried state
38 */
39interface::interface(const std::string& name,
40 interface::type_t itf_type,
41 interface::admin_state_t itf_state)
42 : m_hdl(handle_t::INVALID)
43 , m_name(name)
44 , m_type(itf_type)
45 , m_state(itf_state)
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)
49{
50}
51
Neale Ranns812ed392017-10-16 04:20:13 -070052interface::interface(const std::string& name,
53 interface::type_t itf_type,
54 interface::admin_state_t itf_state,
55 const route_domain& rd)
56 : m_hdl(handle_t::INVALID)
57 , m_name(name)
58 , m_type(itf_type)
59 , m_rd(rd.singular())
60 , m_state(itf_state)
61 , m_table_id(m_rd->table_id())
62 , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
63 , m_oper(oper_state_t::DOWN)
64{
65}
66
67interface::interface(const interface& o)
68 : m_hdl(o.m_hdl)
69 , m_name(o.m_name)
70 , m_type(o.m_type)
71 , m_rd(o.m_rd)
72 , m_state(o.m_state)
73 , m_table_id(o.m_table_id)
74 , m_l2_address(o.m_l2_address)
75 , m_oper(o.m_oper)
76{
77}
78
Neale Rannsfd920602017-11-23 12:15:00 -080079bool
80interface::operator==(const interface& i) const
81{
82 return ((key() == i.key()) &&
83 (m_l2_address.data() == i.m_l2_address.data()) &&
84 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
85 (m_oper == i.m_oper));
86}
87
Neale Ranns812ed392017-10-16 04:20:13 -070088interface::event_listener::event_listener()
89 : m_status(rc_t::NOOP)
90{
91}
92
93HW::item<bool>&
94interface::event_listener::status()
95{
96 return (m_status);
97}
98
99interface::stat_listener::stat_listener()
100 : m_status(rc_t::NOOP)
101{
102}
103
104HW::item<bool>&
105interface::stat_listener::status()
106{
107 return (m_status);
108}
109
110/**
111 * Return the interface type
112 */
113const interface::type_t&
114interface::type() const
115{
116 return (m_type);
117}
118
119const handle_t&
120interface::handle() const
121{
Neale Ranns263f9552017-11-15 02:52:13 -0800122 return (singular()->handle_i());
123}
124
125const handle_t&
126interface::handle_i() const
127{
Neale Ranns812ed392017-10-16 04:20:13 -0700128 return (m_hdl.data());
129}
130
131const l2_address_t&
132interface::l2_address() const
133{
134 return (m_l2_address.data());
135}
136
137interface::const_iterator_t
138interface::cbegin()
139{
140 return m_db.cbegin();
141}
142
143interface::const_iterator_t
144interface::cend()
145{
146 return m_db.cend();
147}
148
149void
150interface::sweep()
151{
Neale Ranns352ea0c2017-11-14 11:04:28 -0800152 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
Neale Ranns812ed392017-10-16 04:20:13 -0700153 m_table_id.data() = route::DEFAULT_TABLE;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700154 HW::enqueue(
155 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
156 HW::enqueue(
157 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700158 }
159
Neale Rannsa2ee0292017-11-28 22:29:13 -0800160 if (m_stats) {
161 HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
162 m_stats.reset();
163 }
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100164
Neale Ranns812ed392017-10-16 04:20:13 -0700165 // If the interface is up, bring it down
166 if (m_state && interface::admin_state_t::UP == m_state.data()) {
167 m_state.data() = interface::admin_state_t::DOWN;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700168 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700169 }
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100170
Neale Ranns812ed392017-10-16 04:20:13 -0700171 if (m_hdl) {
172 std::queue<cmd*> cmds;
173 HW::enqueue(mk_delete_cmd(cmds));
174 }
175 HW::write();
176}
177
178void
179interface::replay()
180{
181 if (m_hdl) {
182 std::queue<cmd*> cmds;
183 HW::enqueue(mk_create_cmd(cmds));
184 }
185
186 if (m_state && interface::admin_state_t::UP == m_state.data()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700187 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700188 }
189
Neale Ranns352ea0c2017-11-14 11:04:28 -0800190 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700191 HW::enqueue(
192 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
193 HW::enqueue(
194 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700195 }
196}
197
198interface::~interface()
199{
200 sweep();
201 release();
202}
203
204void
205interface::release()
206{
207 // not in the DB anymore.
208 m_db.release(m_name, this);
209}
210
211std::string
212interface::to_string() const
213{
214 std::ostringstream s;
215 s << "interface:[" << m_name << " type:" << m_type.to_string()
Neale Rannsfd920602017-11-23 12:15:00 -0800216 << " hdl:" << m_hdl.to_string() << " l2-address:["
217 << m_l2_address.to_string() << "]";
Neale Ranns812ed392017-10-16 04:20:13 -0700218
219 if (m_rd) {
220 s << " rd:" << m_rd->to_string();
221 }
222
223 s << " admin-state:" << m_state.to_string()
224 << " oper-state:" << m_oper.to_string() << "]";
225
226 return (s.str());
227}
228
229const std::string&
230interface::name() const
231{
232 return (m_name);
233}
234
Neale Rannsfd920602017-11-23 12:15:00 -0800235const interface::key_t&
Neale Ranns812ed392017-10-16 04:20:13 -0700236interface::key() const
237{
238 return (name());
239}
240
241std::queue<cmd*>&
242interface::mk_create_cmd(std::queue<cmd*>& q)
243{
244 if (type_t::LOOPBACK == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700245 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700246 } else if (type_t::BVI == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700247 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
248 q.push(new interface_cmds::set_tag(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700249 } else if (type_t::AFPACKET == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700250 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700251 } else if (type_t::TAP == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700252 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
Neale Ranns088f0e22017-12-01 00:19:43 -0800253 } else {
254 m_hdl.set(rc_t::OK);
Neale Ranns812ed392017-10-16 04:20:13 -0700255 }
256
257 return (q);
258}
259
260std::queue<cmd*>&
261interface::mk_delete_cmd(std::queue<cmd*>& q)
262{
263 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700264 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700265 } else if (type_t::AFPACKET == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700266 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700267 } else if (type_t::TAP == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700268 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700269 }
270
271 return (q);
272}
273
274void
275interface::update(const interface& desired)
276{
277 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800278 * the desired state is always that the interface should be created
279 */
Neale Ranns812ed392017-10-16 04:20:13 -0700280 if (rc_t::OK != m_hdl.rc()) {
281 std::queue<cmd*> cmds;
282 HW::enqueue(mk_create_cmd(cmds));
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800283 /*
284 * interface create now, so we can barf early if it fails
285 */
286 HW::write();
Neale Ranns812ed392017-10-16 04:20:13 -0700287 }
288
289 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800290 * If the interface is not created do other commands should be issued
291 */
292 if (rc_t::OK != m_hdl.rc())
293 return;
294
295 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800296 * change the interface state to that which is deisred
297 */
Neale Ranns812ed392017-10-16 04:20:13 -0700298 if (m_state.update(desired.m_state)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700299 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700300 }
301
302 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800303 * change the interface state to that which is deisred
304 */
Neale Ranns812ed392017-10-16 04:20:13 -0700305 if (m_l2_address.update(desired.m_l2_address)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700306 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700307 }
308
309 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800310 * If the interface is mapped into a route domain, set VPP's
311 * table ID
312 */
313 if (m_rd != desired.m_rd) {
314 /*
315 * changing route domains. need to remove all L3 bindings, swap the table
316 * then reapply the bindings.
317 */
318 auto it = l3_binding::cbegin();
319
320 while (it != l3_binding::cend()) {
321 if (it->second.lock()->itf().key() == key())
322 it->second.lock()->sweep();
323 ++it;
324 }
325 m_rd = desired.m_rd;
326 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
327 HW::enqueue(
328 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
329 HW::enqueue(
330 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
331 HW::write();
332
333 it = l3_binding::cbegin();
334 while (it != l3_binding::cend()) {
335 if (it->second.lock()->itf().key() == key())
336 it->second.lock()->replay(); //(*it->second.lock());
337 ++it;
338 }
339 } else if (!m_table_id && m_rd) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700340 HW::enqueue(
341 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
342 HW::enqueue(
343 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700344 }
345}
346
347void
348interface::set(const l2_address_t& addr)
349{
350 assert(rc_t::UNSET == m_l2_address.rc());
351 m_l2_address.set(rc_t::NOOP);
352 m_l2_address.update(addr);
353}
354
355void
Neale Rannsa2ee0292017-11-28 22:29:13 -0800356interface::set(const handle_t& hdl)
357{
358 m_hdl = hdl;
359}
360
361void
Neale Ranns812ed392017-10-16 04:20:13 -0700362interface::set(const oper_state_t& state)
363{
364 m_oper = state;
365}
366
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100367void
368interface::enable_stats_i(interface::stat_listener& el)
369{
Neale Ranns088f0e22017-12-01 00:19:43 -0800370 if (!m_stats) {
371 m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
372 HW::enqueue(m_stats);
373 HW::write();
374 }
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100375}
376
377void
378interface::enable_stats(interface::stat_listener& el)
379{
380 singular()->enable_stats_i(el);
381}
382
Neale Ranns812ed392017-10-16 04:20:13 -0700383std::shared_ptr<interface>
384interface::singular_i() const
385{
Neale Rannsfd920602017-11-23 12:15:00 -0800386 return (m_db.find_or_add(key(), *this));
Neale Ranns812ed392017-10-16 04:20:13 -0700387}
388
389std::shared_ptr<interface>
390interface::singular() const
391{
392 return singular_i();
393}
394
395std::shared_ptr<interface>
Neale Rannsfd920602017-11-23 12:15:00 -0800396interface::find(const key_t& k)
Neale Ranns812ed392017-10-16 04:20:13 -0700397{
Neale Rannsfd920602017-11-23 12:15:00 -0800398 return (m_db.find(k));
Neale Ranns812ed392017-10-16 04:20:13 -0700399}
400
401std::shared_ptr<interface>
402interface::find(const handle_t& handle)
403{
404 return (m_hdl_db[handle].lock());
405}
406
407void
Neale Rannsfd920602017-11-23 12:15:00 -0800408interface::add(const key_t& key, const HW::item<handle_t>& item)
Neale Ranns812ed392017-10-16 04:20:13 -0700409{
Neale Rannsfd920602017-11-23 12:15:00 -0800410 std::shared_ptr<interface> sp = find(key);
Neale Ranns812ed392017-10-16 04:20:13 -0700411
412 if (sp && item) {
413 m_hdl_db[item.data()] = sp;
414 }
415}
416
417void
418interface::remove(const HW::item<handle_t>& item)
419{
420 m_hdl_db.erase(item.data());
421}
422
423void
424interface::dump(std::ostream& os)
425{
426 m_db.dump(os);
427}
428
429void
430interface::event_handler::handle_populate(const client_db::key_t& key)
431{
432 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800433 * dump VPP current states
434 */
Neale Ranns1d781552017-11-27 04:52:35 -0800435 std::shared_ptr<interface_cmds::dump_cmd> cmd =
436 std::make_shared<interface_cmds::dump_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700437
438 HW::enqueue(cmd);
439 HW::write();
440
441 for (auto& itf_record : *cmd) {
Neale Rannsa2ee0292017-11-28 22:29:13 -0800442 std::shared_ptr<interface> itf =
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700443 interface_factory::new_interface(itf_record.get_payload());
Neale Ranns812ed392017-10-16 04:20:13 -0700444
445 if (itf && interface::type_t::LOCAL != itf->type()) {
446 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
447 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800448 * Write each of the discovered interfaces into the OM,
449 * but disable the HW Command q whilst we do, so that no
450 * commands are sent to VPP
451 */
Neale Ranns812ed392017-10-16 04:20:13 -0700452 OM::commit(key, *itf);
453
454 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800455 * Get the address configured on the interface
456 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700457 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
458 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
459 l3_binding_cmds::dump_v4_cmd(itf->handle()));
Neale Ranns812ed392017-10-16 04:20:13 -0700460
461 HW::enqueue(dcmd);
462 HW::write();
463
464 for (auto& l3_record : *dcmd) {
465 auto& payload = l3_record.get_payload();
466 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
467 payload.prefix_length);
468
469 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
470
471 l3_binding l3(*itf, pfx);
472 OM::commit(key, l3);
473 }
474 }
475 }
476}
477
478interface::event_handler::event_handler()
479{
480 OM::register_listener(this);
481 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
482}
483
484void
485interface::event_handler::handle_replay()
486{
487 m_db.replay();
488}
489
490dependency_t
491interface::event_handler::order() const
492{
493 return (dependency_t::INTERFACE);
494}
495
496void
497interface::event_handler::show(std::ostream& os)
498{
499 m_db.dump(os);
500}
Neale Rannsfd920602017-11-23 12:15:00 -0800501
502} // namespace VOM
503
Neale Ranns812ed392017-10-16 04:20:13 -0700504/*
505 * fd.io coding-style-patch-verification: ON
506 *
507 * Local Variables:
508 * eval: (c-set-style "mozilla")
509 * End:
510 */