blob: c7b7e436450bbbca3ae487d24b813950c029ce53 [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
52interface::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)
Neale Ranns8ac4ce82017-11-17 05:08:55 -080057 : m_hdl(handle, rc_t::OK)
Neale Ranns812ed392017-10-16 04:20:13 -070058 , m_name(name)
59 , m_type(type)
Neale Ranns8ac4ce82017-11-17 05:08:55 -080060 , m_state(state, rc_t::OK)
Neale Ranns812ed392017-10-16 04:20:13 -070061 , m_table_id(route::DEFAULT_TABLE)
62 , m_l2_address(l2_address)
63 , m_oper(oper_state_t::DOWN)
64{
65}
66
67interface::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)
72 , m_name(name)
73 , m_type(itf_type)
74 , m_rd(rd.singular())
75 , m_state(itf_state)
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)
79{
80}
81
82interface::interface(const interface& o)
83 : m_hdl(o.m_hdl)
84 , m_name(o.m_name)
85 , m_type(o.m_type)
86 , m_rd(o.m_rd)
87 , m_state(o.m_state)
88 , m_table_id(o.m_table_id)
89 , m_l2_address(o.m_l2_address)
90 , m_oper(o.m_oper)
91{
92}
93
Neale Rannsfd920602017-11-23 12:15:00 -080094bool
95interface::operator==(const interface& i) const
96{
97 return ((key() == i.key()) &&
98 (m_l2_address.data() == i.m_l2_address.data()) &&
99 (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
100 (m_oper == i.m_oper));
101}
102
Neale Ranns812ed392017-10-16 04:20:13 -0700103interface::event_listener::event_listener()
104 : m_status(rc_t::NOOP)
105{
106}
107
108HW::item<bool>&
109interface::event_listener::status()
110{
111 return (m_status);
112}
113
114interface::stat_listener::stat_listener()
115 : m_status(rc_t::NOOP)
116{
117}
118
119HW::item<bool>&
120interface::stat_listener::status()
121{
122 return (m_status);
123}
124
125/**
126 * Return the interface type
127 */
128const interface::type_t&
129interface::type() const
130{
131 return (m_type);
132}
133
134const handle_t&
135interface::handle() const
136{
Neale Ranns263f9552017-11-15 02:52:13 -0800137 return (singular()->handle_i());
138}
139
140const handle_t&
141interface::handle_i() const
142{
Neale Ranns812ed392017-10-16 04:20:13 -0700143 return (m_hdl.data());
144}
145
146const l2_address_t&
147interface::l2_address() const
148{
149 return (m_l2_address.data());
150}
151
152interface::const_iterator_t
153interface::cbegin()
154{
155 return m_db.cbegin();
156}
157
158interface::const_iterator_t
159interface::cend()
160{
161 return m_db.cend();
162}
163
164void
165interface::sweep()
166{
Neale Ranns352ea0c2017-11-14 11:04:28 -0800167 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
Neale Ranns812ed392017-10-16 04:20:13 -0700168 m_table_id.data() = route::DEFAULT_TABLE;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700169 HW::enqueue(
170 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
171 HW::enqueue(
172 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700173 }
174
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100175 if (m_stats)
176 HW::dequeue(m_stats);
177
Neale Ranns812ed392017-10-16 04:20:13 -0700178 // If the interface is up, bring it down
179 if (m_state && interface::admin_state_t::UP == m_state.data()) {
180 m_state.data() = interface::admin_state_t::DOWN;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700181 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700182 }
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100183
Neale Ranns812ed392017-10-16 04:20:13 -0700184 if (m_hdl) {
185 std::queue<cmd*> cmds;
186 HW::enqueue(mk_delete_cmd(cmds));
187 }
188 HW::write();
189}
190
191void
192interface::replay()
193{
194 if (m_hdl) {
195 std::queue<cmd*> cmds;
196 HW::enqueue(mk_create_cmd(cmds));
197 }
198
199 if (m_state && interface::admin_state_t::UP == m_state.data()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700200 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700201 }
202
Neale Ranns352ea0c2017-11-14 11:04:28 -0800203 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700204 HW::enqueue(
205 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
206 HW::enqueue(
207 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700208 }
209}
210
211interface::~interface()
212{
213 sweep();
214 release();
215}
216
217void
218interface::release()
219{
220 // not in the DB anymore.
221 m_db.release(m_name, this);
222}
223
224std::string
225interface::to_string() const
226{
227 std::ostringstream s;
228 s << "interface:[" << m_name << " type:" << m_type.to_string()
Neale Rannsfd920602017-11-23 12:15:00 -0800229 << " hdl:" << m_hdl.to_string() << " l2-address:["
230 << m_l2_address.to_string() << "]";
Neale Ranns812ed392017-10-16 04:20:13 -0700231
232 if (m_rd) {
233 s << " rd:" << m_rd->to_string();
234 }
235
236 s << " admin-state:" << m_state.to_string()
237 << " oper-state:" << m_oper.to_string() << "]";
238
239 return (s.str());
240}
241
242const std::string&
243interface::name() const
244{
245 return (m_name);
246}
247
Neale Rannsfd920602017-11-23 12:15:00 -0800248const interface::key_t&
Neale Ranns812ed392017-10-16 04:20:13 -0700249interface::key() const
250{
251 return (name());
252}
253
254std::queue<cmd*>&
255interface::mk_create_cmd(std::queue<cmd*>& q)
256{
257 if (type_t::LOOPBACK == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700258 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700259 } else if (type_t::BVI == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700260 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
261 q.push(new interface_cmds::set_tag(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700262 } else if (type_t::AFPACKET == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700263 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700264 } else if (type_t::TAP == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700265 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700266 }
267
268 return (q);
269}
270
271std::queue<cmd*>&
272interface::mk_delete_cmd(std::queue<cmd*>& q)
273{
274 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700275 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700276 } else if (type_t::AFPACKET == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700277 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700278 } else if (type_t::TAP == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700279 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700280 }
281
282 return (q);
283}
284
285void
286interface::update(const interface& desired)
287{
288 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800289 * the desired state is always that the interface should be created
290 */
Neale Ranns812ed392017-10-16 04:20:13 -0700291 if (rc_t::OK != m_hdl.rc()) {
292 std::queue<cmd*> cmds;
293 HW::enqueue(mk_create_cmd(cmds));
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800294 /*
295 * interface create now, so we can barf early if it fails
296 */
297 HW::write();
Neale Ranns812ed392017-10-16 04:20:13 -0700298 }
299
300 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800301 * If the interface is not created do other commands should be issued
302 */
303 if (rc_t::OK != m_hdl.rc())
304 return;
305
306 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800307 * change the interface state to that which is deisred
308 */
Neale Ranns812ed392017-10-16 04:20:13 -0700309 if (m_state.update(desired.m_state)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700310 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700311 }
312
313 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800314 * change the interface state to that which is deisred
315 */
Neale Ranns812ed392017-10-16 04:20:13 -0700316 if (m_l2_address.update(desired.m_l2_address)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700317 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700318 }
319
320 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800321 * If the interface is mapped into a route domain, set VPP's
322 * table ID
323 */
324 if (m_rd != desired.m_rd) {
325 /*
326 * changing route domains. need to remove all L3 bindings, swap the table
327 * then reapply the bindings.
328 */
329 auto it = l3_binding::cbegin();
330
331 while (it != l3_binding::cend()) {
332 if (it->second.lock()->itf().key() == key())
333 it->second.lock()->sweep();
334 ++it;
335 }
336 m_rd = desired.m_rd;
337 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
338 HW::enqueue(
339 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
340 HW::enqueue(
341 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
342 HW::write();
343
344 it = l3_binding::cbegin();
345 while (it != l3_binding::cend()) {
346 if (it->second.lock()->itf().key() == key())
347 it->second.lock()->replay(); //(*it->second.lock());
348 ++it;
349 }
350 } else if (!m_table_id && m_rd) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700351 HW::enqueue(
352 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
353 HW::enqueue(
354 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700355 }
356}
357
358void
359interface::set(const l2_address_t& addr)
360{
361 assert(rc_t::UNSET == m_l2_address.rc());
362 m_l2_address.set(rc_t::NOOP);
363 m_l2_address.update(addr);
364}
365
366void
367interface::set(const oper_state_t& state)
368{
369 m_oper = state;
370}
371
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100372void
373interface::enable_stats_i(interface::stat_listener& el)
374{
375 m_stats.reset(new interface_cmds::stats_cmd(el, handle_i()));
376 HW::enqueue(m_stats);
377 HW::write();
378}
379
380void
381interface::enable_stats(interface::stat_listener& el)
382{
383 singular()->enable_stats_i(el);
384}
385
Neale Ranns812ed392017-10-16 04:20:13 -0700386std::shared_ptr<interface>
387interface::singular_i() const
388{
Neale Rannsfd920602017-11-23 12:15:00 -0800389 return (m_db.find_or_add(key(), *this));
Neale Ranns812ed392017-10-16 04:20:13 -0700390}
391
392std::shared_ptr<interface>
393interface::singular() const
394{
395 return singular_i();
396}
397
398std::shared_ptr<interface>
Neale Rannsfd920602017-11-23 12:15:00 -0800399interface::find(const key_t& k)
Neale Ranns812ed392017-10-16 04:20:13 -0700400{
Neale Rannsfd920602017-11-23 12:15:00 -0800401 return (m_db.find(k));
Neale Ranns812ed392017-10-16 04:20:13 -0700402}
403
404std::shared_ptr<interface>
405interface::find(const handle_t& handle)
406{
407 return (m_hdl_db[handle].lock());
408}
409
410void
Neale Rannsfd920602017-11-23 12:15:00 -0800411interface::add(const key_t& key, const HW::item<handle_t>& item)
Neale Ranns812ed392017-10-16 04:20:13 -0700412{
Neale Rannsfd920602017-11-23 12:15:00 -0800413 std::shared_ptr<interface> sp = find(key);
Neale Ranns812ed392017-10-16 04:20:13 -0700414
415 if (sp && item) {
416 m_hdl_db[item.data()] = sp;
417 }
418}
419
420void
421interface::remove(const HW::item<handle_t>& item)
422{
423 m_hdl_db.erase(item.data());
424}
425
426void
427interface::dump(std::ostream& os)
428{
429 m_db.dump(os);
430}
431
432void
433interface::event_handler::handle_populate(const client_db::key_t& key)
434{
435 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800436 * dump VPP current states
437 */
Neale Ranns1d781552017-11-27 04:52:35 -0800438 std::shared_ptr<interface_cmds::dump_cmd> cmd =
439 std::make_shared<interface_cmds::dump_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700440
441 HW::enqueue(cmd);
442 HW::write();
443
444 for (auto& itf_record : *cmd) {
445 std::unique_ptr<interface> itf =
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700446 interface_factory::new_interface(itf_record.get_payload());
Neale Ranns812ed392017-10-16 04:20:13 -0700447
448 if (itf && interface::type_t::LOCAL != itf->type()) {
449 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
450 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800451 * Write each of the discovered interfaces into the OM,
452 * but disable the HW Command q whilst we do, so that no
453 * commands are sent to VPP
454 */
Neale Ranns812ed392017-10-16 04:20:13 -0700455 OM::commit(key, *itf);
456
457 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800458 * Get the address configured on the interface
459 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700460 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
461 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
462 l3_binding_cmds::dump_v4_cmd(itf->handle()));
Neale Ranns812ed392017-10-16 04:20:13 -0700463
464 HW::enqueue(dcmd);
465 HW::write();
466
467 for (auto& l3_record : *dcmd) {
468 auto& payload = l3_record.get_payload();
469 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
470 payload.prefix_length);
471
472 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
473
474 l3_binding l3(*itf, pfx);
475 OM::commit(key, l3);
476 }
477 }
478 }
479}
480
481interface::event_handler::event_handler()
482{
483 OM::register_listener(this);
484 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
485}
486
487void
488interface::event_handler::handle_replay()
489{
490 m_db.replay();
491}
492
493dependency_t
494interface::event_handler::order() const
495{
496 return (dependency_t::INTERFACE);
497}
498
499void
500interface::event_handler::show(std::ostream& os)
501{
502 m_db.dump(os);
503}
Neale Rannsfd920602017-11-23 12:15:00 -0800504
505} // namespace VOM
506
Neale Ranns812ed392017-10-16 04:20:13 -0700507/*
508 * fd.io coding-style-patch-verification: ON
509 *
510 * Local Variables:
511 * eval: (c-set-style "mozilla")
512 * End:
513 */