blob: 39ca074920fe9024ee5afa493bc61972be168f53 [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 */
27singular_db<const std::string, interface> interface::m_db;
28
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
94interface::event_listener::event_listener()
95 : m_status(rc_t::NOOP)
96{
97}
98
99HW::item<bool>&
100interface::event_listener::status()
101{
102 return (m_status);
103}
104
105interface::stat_listener::stat_listener()
106 : m_status(rc_t::NOOP)
107{
108}
109
110HW::item<bool>&
111interface::stat_listener::status()
112{
113 return (m_status);
114}
115
116/**
117 * Return the interface type
118 */
119const interface::type_t&
120interface::type() const
121{
122 return (m_type);
123}
124
125const handle_t&
126interface::handle() const
127{
Neale Ranns263f9552017-11-15 02:52:13 -0800128 return (singular()->handle_i());
129}
130
131const handle_t&
132interface::handle_i() const
133{
Neale Ranns812ed392017-10-16 04:20:13 -0700134 return (m_hdl.data());
135}
136
137const l2_address_t&
138interface::l2_address() const
139{
140 return (m_l2_address.data());
141}
142
143interface::const_iterator_t
144interface::cbegin()
145{
146 return m_db.cbegin();
147}
148
149interface::const_iterator_t
150interface::cend()
151{
152 return m_db.cend();
153}
154
155void
156interface::sweep()
157{
Neale Ranns352ea0c2017-11-14 11:04:28 -0800158 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
Neale Ranns812ed392017-10-16 04:20:13 -0700159 m_table_id.data() = route::DEFAULT_TABLE;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700160 HW::enqueue(
161 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
162 HW::enqueue(
163 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700164 }
165
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100166 if (m_stats)
167 HW::dequeue(m_stats);
168
Neale Ranns812ed392017-10-16 04:20:13 -0700169 // If the interface is up, bring it down
170 if (m_state && interface::admin_state_t::UP == m_state.data()) {
171 m_state.data() = interface::admin_state_t::DOWN;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700172 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700173 }
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100174
Neale Ranns812ed392017-10-16 04:20:13 -0700175 if (m_hdl) {
176 std::queue<cmd*> cmds;
177 HW::enqueue(mk_delete_cmd(cmds));
178 }
179 HW::write();
180}
181
182void
183interface::replay()
184{
185 if (m_hdl) {
186 std::queue<cmd*> cmds;
187 HW::enqueue(mk_create_cmd(cmds));
188 }
189
190 if (m_state && interface::admin_state_t::UP == m_state.data()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700191 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700192 }
193
Neale Ranns352ea0c2017-11-14 11:04:28 -0800194 if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700195 HW::enqueue(
196 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
197 HW::enqueue(
198 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700199 }
200}
201
202interface::~interface()
203{
204 sweep();
205 release();
206}
207
208void
209interface::release()
210{
211 // not in the DB anymore.
212 m_db.release(m_name, this);
213}
214
215std::string
216interface::to_string() const
217{
218 std::ostringstream s;
219 s << "interface:[" << m_name << " type:" << m_type.to_string()
220 << " hdl:" << m_hdl.to_string()
221 << " l2-address:" << m_l2_address.to_string();
222
223 if (m_rd) {
224 s << " rd:" << m_rd->to_string();
225 }
226
227 s << " admin-state:" << m_state.to_string()
228 << " oper-state:" << m_oper.to_string() << "]";
229
230 return (s.str());
231}
232
233const std::string&
234interface::name() const
235{
236 return (m_name);
237}
238
239const interface::key_type&
240interface::key() const
241{
242 return (name());
243}
244
245std::queue<cmd*>&
246interface::mk_create_cmd(std::queue<cmd*>& q)
247{
248 if (type_t::LOOPBACK == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700249 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700250 } else if (type_t::BVI == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700251 q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
252 q.push(new interface_cmds::set_tag(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700253 } else if (type_t::AFPACKET == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700254 q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700255 } else if (type_t::TAP == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700256 q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700257 }
258
259 return (q);
260}
261
262std::queue<cmd*>&
263interface::mk_delete_cmd(std::queue<cmd*>& q)
264{
265 if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700266 q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700267 } else if (type_t::AFPACKET == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700268 q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
Neale Ranns812ed392017-10-16 04:20:13 -0700269 } else if (type_t::TAP == m_type) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700270 q.push(new interface_cmds::tap_delete_cmd(m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700271 }
272
273 return (q);
274}
275
276void
277interface::update(const interface& desired)
278{
279 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800280 * the desired state is always that the interface should be created
281 */
Neale Ranns812ed392017-10-16 04:20:13 -0700282 if (rc_t::OK != m_hdl.rc()) {
283 std::queue<cmd*> cmds;
284 HW::enqueue(mk_create_cmd(cmds));
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800285 /*
286 * interface create now, so we can barf early if it fails
287 */
288 HW::write();
Neale Ranns812ed392017-10-16 04:20:13 -0700289 }
290
291 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800292 * If the interface is not created do other commands should be issued
293 */
294 if (rc_t::OK != m_hdl.rc())
295 return;
296
297 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800298 * change the interface state to that which is deisred
299 */
Neale Ranns812ed392017-10-16 04:20:13 -0700300 if (m_state.update(desired.m_state)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700301 HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700302 }
303
304 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800305 * change the interface state to that which is deisred
306 */
Neale Ranns812ed392017-10-16 04:20:13 -0700307 if (m_l2_address.update(desired.m_l2_address)) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700308 HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700309 }
310
311 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800312 * If the interface is mapped into a route domain, set VPP's
313 * table ID
314 */
315 if (m_rd != desired.m_rd) {
316 /*
317 * changing route domains. need to remove all L3 bindings, swap the table
318 * then reapply the bindings.
319 */
320 auto it = l3_binding::cbegin();
321
322 while (it != l3_binding::cend()) {
323 if (it->second.lock()->itf().key() == key())
324 it->second.lock()->sweep();
325 ++it;
326 }
327 m_rd = desired.m_rd;
328 m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
329 HW::enqueue(
330 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
331 HW::enqueue(
332 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
333 HW::write();
334
335 it = l3_binding::cbegin();
336 while (it != l3_binding::cend()) {
337 if (it->second.lock()->itf().key() == key())
338 it->second.lock()->replay(); //(*it->second.lock());
339 ++it;
340 }
341 } else if (!m_table_id && m_rd) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700342 HW::enqueue(
343 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
344 HW::enqueue(
345 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
Neale Ranns812ed392017-10-16 04:20:13 -0700346 }
347}
348
349void
350interface::set(const l2_address_t& addr)
351{
352 assert(rc_t::UNSET == m_l2_address.rc());
353 m_l2_address.set(rc_t::NOOP);
354 m_l2_address.update(addr);
355}
356
357void
358interface::set(const oper_state_t& state)
359{
360 m_oper = state;
361}
362
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100363void
364interface::enable_stats_i(interface::stat_listener& el)
365{
366 m_stats.reset(new interface_cmds::stats_cmd(el, handle_i()));
367 HW::enqueue(m_stats);
368 HW::write();
369}
370
371void
372interface::enable_stats(interface::stat_listener& el)
373{
374 singular()->enable_stats_i(el);
375}
376
Neale Ranns812ed392017-10-16 04:20:13 -0700377std::shared_ptr<interface>
378interface::singular_i() const
379{
380 return (m_db.find_or_add(name(), *this));
381}
382
383std::shared_ptr<interface>
384interface::singular() const
385{
386 return singular_i();
387}
388
389std::shared_ptr<interface>
390interface::find(const std::string& name)
391{
392 return (m_db.find(name));
393}
394
395std::shared_ptr<interface>
396interface::find(const handle_t& handle)
397{
398 return (m_hdl_db[handle].lock());
399}
400
401void
402interface::add(const std::string& name, const HW::item<handle_t>& item)
403{
404 std::shared_ptr<interface> sp = find(name);
405
406 if (sp && item) {
407 m_hdl_db[item.data()] = sp;
408 }
409}
410
411void
412interface::remove(const HW::item<handle_t>& item)
413{
414 m_hdl_db.erase(item.data());
415}
416
417void
418interface::dump(std::ostream& os)
419{
420 m_db.dump(os);
421}
422
423void
424interface::event_handler::handle_populate(const client_db::key_t& key)
425{
426 /*
427 * dump VPP current states
428 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700429 std::shared_ptr<interface_cmds::dump_cmd> cmd(new interface_cmds::dump_cmd());
Neale Ranns812ed392017-10-16 04:20:13 -0700430
431 HW::enqueue(cmd);
432 HW::write();
433
434 for (auto& itf_record : *cmd) {
435 std::unique_ptr<interface> itf =
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700436 interface_factory::new_interface(itf_record.get_payload());
Neale Ranns812ed392017-10-16 04:20:13 -0700437
438 if (itf && interface::type_t::LOCAL != itf->type()) {
439 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
440 /*
441 * Write each of the discovered interfaces into the OM,
442 * but disable the HW Command q whilst we do, so that no
443 * commands are sent to VPP
444 */
445 OM::commit(key, *itf);
446
447 /**
448 * Get the address configured on the interface
449 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700450 std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
451 std::make_shared<l3_binding_cmds::dump_v4_cmd>(
452 l3_binding_cmds::dump_v4_cmd(itf->handle()));
Neale Ranns812ed392017-10-16 04:20:13 -0700453
454 HW::enqueue(dcmd);
455 HW::write();
456
457 for (auto& l3_record : *dcmd) {
458 auto& payload = l3_record.get_payload();
459 const route::prefix_t pfx(payload.is_ipv6, payload.ip,
460 payload.prefix_length);
461
462 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
463
464 l3_binding l3(*itf, pfx);
465 OM::commit(key, l3);
466 }
467 }
468 }
469}
470
471interface::event_handler::event_handler()
472{
473 OM::register_listener(this);
474 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
475}
476
477void
478interface::event_handler::handle_replay()
479{
480 m_db.replay();
481}
482
483dependency_t
484interface::event_handler::order() const
485{
486 return (dependency_t::INTERFACE);
487}
488
489void
490interface::event_handler::show(std::ostream& os)
491{
492 m_db.dump(os);
493}
494}
495/*
496 * fd.io coding-style-patch-verification: ON
497 *
498 * Local Variables:
499 * eval: (c-set-style "mozilla")
500 * End:
501 */