blob: 83da275f61fa89b8c7f1b8789884d5da5f0a5e6d [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"
17#include "vom/cmd.hpp"
18#include "vom/l3_binding.hpp"
19#include "vom/logger.hpp"
20#include "vom/prefix.hpp"
21
22namespace VOM {
23/**
24 * A DB of all the interfaces, key on the name
25 */
26singular_db<const std::string, interface> interface::m_db;
27
28/**
29 * A DB of all the interfaces, key on VPP's handle
30 */
31std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
32
33interface::event_handler interface::m_evh;
34
35/**
36 * Construct a new object matching the desried state
37 */
38interface::interface(const std::string& name,
39 interface::type_t itf_type,
40 interface::admin_state_t itf_state)
41 : m_hdl(handle_t::INVALID)
42 , m_name(name)
43 , m_type(itf_type)
44 , m_state(itf_state)
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)
48{
49}
50
51interface::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)
56 : m_hdl(handle)
57 , m_name(name)
58 , m_type(type)
59 , m_state(state)
60 , m_table_id(route::DEFAULT_TABLE)
61 , m_l2_address(l2_address)
62 , m_oper(oper_state_t::DOWN)
63{
64}
65
66interface::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)
71 , m_name(name)
72 , m_type(itf_type)
73 , m_rd(rd.singular())
74 , m_state(itf_state)
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)
78{
79}
80
81interface::interface(const interface& o)
82 : m_hdl(o.m_hdl)
83 , m_name(o.m_name)
84 , m_type(o.m_type)
85 , m_rd(o.m_rd)
86 , m_state(o.m_state)
87 , m_table_id(o.m_table_id)
88 , m_l2_address(o.m_l2_address)
89 , m_oper(o.m_oper)
90{
91}
92
93interface::event_listener::event_listener()
94 : m_status(rc_t::NOOP)
95{
96}
97
98HW::item<bool>&
99interface::event_listener::status()
100{
101 return (m_status);
102}
103
104interface::stat_listener::stat_listener()
105 : m_status(rc_t::NOOP)
106{
107}
108
109HW::item<bool>&
110interface::stat_listener::status()
111{
112 return (m_status);
113}
114
115/**
116 * Return the interface type
117 */
118const interface::type_t&
119interface::type() const
120{
121 return (m_type);
122}
123
124const handle_t&
125interface::handle() const
126{
127 return (m_hdl.data());
128}
129
130const l2_address_t&
131interface::l2_address() const
132{
133 return (m_l2_address.data());
134}
135
136interface::const_iterator_t
137interface::cbegin()
138{
139 return m_db.cbegin();
140}
141
142interface::const_iterator_t
143interface::cend()
144{
145 return m_db.cend();
146}
147
148void
149interface::sweep()
150{
151 if (m_table_id) {
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));
155 }
156
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));
161 }
162 if (m_hdl) {
163 std::queue<cmd*> cmds;
164 HW::enqueue(mk_delete_cmd(cmds));
165 }
166 HW::write();
167}
168
169void
170interface::replay()
171{
172 if (m_hdl) {
173 std::queue<cmd*> cmds;
174 HW::enqueue(mk_create_cmd(cmds));
175 }
176
177 if (m_state && interface::admin_state_t::UP == m_state.data()) {
178 HW::enqueue(new state_change_cmd(m_state, m_hdl));
179 }
180
181 if (m_table_id) {
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));
184 }
185}
186
187interface::~interface()
188{
189 sweep();
190 release();
191}
192
193void
194interface::release()
195{
196 // not in the DB anymore.
197 m_db.release(m_name, this);
198}
199
200std::string
201interface::to_string() const
202{
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();
207
208 if (m_rd) {
209 s << " rd:" << m_rd->to_string();
210 }
211
212 s << " admin-state:" << m_state.to_string()
213 << " oper-state:" << m_oper.to_string() << "]";
214
215 return (s.str());
216}
217
218const std::string&
219interface::name() const
220{
221 return (m_name);
222}
223
224const interface::key_type&
225interface::key() const
226{
227 return (name());
228}
229
230std::queue<cmd*>&
231interface::mk_create_cmd(std::queue<cmd*>& q)
232{
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));
242 }
243
244 return (q);
245}
246
247std::queue<cmd*>&
248interface::mk_delete_cmd(std::queue<cmd*>& q)
249{
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));
256 }
257
258 return (q);
259}
260
261void
262interface::update(const interface& desired)
263{
264 /*
265 * the desired state is always that the interface should be created
266 */
267 if (rc_t::OK != m_hdl.rc()) {
268 std::queue<cmd*> cmds;
269 HW::enqueue(mk_create_cmd(cmds));
270 }
271
272 /*
273 * change the interface state to that which is deisred
274 */
275 if (m_state.update(desired.m_state)) {
276 HW::enqueue(new state_change_cmd(m_state, m_hdl));
277 }
278
279 /*
280 * change the interface state to that which is deisred
281 */
282 if (m_l2_address.update(desired.m_l2_address)) {
283 HW::enqueue(new set_mac_cmd(m_l2_address, m_hdl));
284 }
285
286 /*
287 * If the interface is mapped into a route domain, set VPP's
288 * table ID
289 */
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));
293 }
294}
295
296void
297interface::set(const l2_address_t& addr)
298{
299 assert(rc_t::UNSET == m_l2_address.rc());
300 m_l2_address.set(rc_t::NOOP);
301 m_l2_address.update(addr);
302}
303
304void
305interface::set(const oper_state_t& state)
306{
307 m_oper = state;
308}
309
310std::shared_ptr<interface>
311interface::singular_i() const
312{
313 return (m_db.find_or_add(name(), *this));
314}
315
316std::shared_ptr<interface>
317interface::singular() const
318{
319 return singular_i();
320}
321
322std::shared_ptr<interface>
323interface::find(const std::string& name)
324{
325 return (m_db.find(name));
326}
327
328std::shared_ptr<interface>
329interface::find(const handle_t& handle)
330{
331 return (m_hdl_db[handle].lock());
332}
333
334void
335interface::add(const std::string& name, const HW::item<handle_t>& item)
336{
337 std::shared_ptr<interface> sp = find(name);
338
339 if (sp && item) {
340 m_hdl_db[item.data()] = sp;
341 }
342}
343
344void
345interface::remove(const HW::item<handle_t>& item)
346{
347 m_hdl_db.erase(item.data());
348}
349
350void
351interface::dump(std::ostream& os)
352{
353 m_db.dump(os);
354}
355
356void
357interface::event_handler::handle_populate(const client_db::key_t& key)
358{
359 /*
360 * dump VPP current states
361 */
362 std::shared_ptr<interface::dump_cmd> cmd(new interface::dump_cmd());
363
364 HW::enqueue(cmd);
365 HW::write();
366
367 for (auto& itf_record : *cmd) {
368 std::unique_ptr<interface> itf =
369 interface::new_interface(itf_record.get_payload());
370
371 if (itf && interface::type_t::LOCAL != itf->type()) {
372 VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
373 /*
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
377 */
378 OM::commit(key, *itf);
379
380 /**
381 * Get the address configured on the interface
382 */
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()));
386
387 HW::enqueue(dcmd);
388 HW::write();
389
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);
394
395 VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
396
397 l3_binding l3(*itf, pfx);
398 OM::commit(key, l3);
399 }
400 }
401 }
402}
403
404interface::event_handler::event_handler()
405{
406 OM::register_listener(this);
407 inspect::register_handler({ "interface", "intf" }, "interfaces", this);
408}
409
410void
411interface::event_handler::handle_replay()
412{
413 m_db.replay();
414}
415
416dependency_t
417interface::event_handler::order() const
418{
419 return (dependency_t::INTERFACE);
420}
421
422void
423interface::event_handler::show(std::ostream& os)
424{
425 m_db.dump(os);
426}
427}
428/*
429 * fd.io coding-style-patch-verification: ON
430 *
431 * Local Variables:
432 * eval: (c-set-style "mozilla")
433 * End:
434 */