blob: 0099bde42ba6a335bd3061b0bc4061e8e86ddf94 [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#ifndef __VOM_INTERFACE_H__
17#define __VOM_INTERFACE_H__
18
Neale Ranns812ed392017-10-16 04:20:13 -070019#include "vom/enum_base.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070020#include "vom/hw.hpp"
21#include "vom/inspect.hpp"
22#include "vom/object_base.hpp"
23#include "vom/om.hpp"
24#include "vom/prefix.hpp"
25#include "vom/route_domain.hpp"
26#include "vom/rpc_cmd.hpp"
27#include "vom/singular_db.hpp"
28
Neale Ranns812ed392017-10-16 04:20:13 -070029namespace VOM {
30/**
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070031 * Forward declaration of the stats and events command
32 */
33namespace interface_cmds {
Neale Rannsa2ee0292017-11-28 22:29:13 -080034class stats_enable_cmd;
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070035class events_cmd;
36};
37
38/**
Neale Ranns812ed392017-10-16 04:20:13 -070039 * A representation of an interface in VPP
40 */
41class interface : public object_base
42{
43public:
44 /**
45 * The key for interface's key
46 */
Neale Rannsfd920602017-11-23 12:15:00 -080047 typedef std::string key_t;
Neale Ranns812ed392017-10-16 04:20:13 -070048
49 /**
50 * The iterator type
51 */
52 typedef singular_db<const std::string, interface>::const_iterator
53 const_iterator_t;
54
55 /**
56 * An interface type
57 */
58 struct type_t : enum_base<type_t>
59 {
60 /**
61 * Unkown type
62 */
63 const static type_t UNKNOWN;
64 /**
65 * A brideged Virtual interface (aka SVI or IRB)
66 */
67 const static type_t BVI;
68 /**
69 * VXLAN interface
70 */
71 const static type_t VXLAN;
72 /**
73 * Ethernet interface type
74 */
75 const static type_t ETHERNET;
76 /**
77 * AF-Packet interface type
78 */
79 const static type_t AFPACKET;
80 /**
81 * loopback interface type
82 */
83 const static type_t LOOPBACK;
84 /**
85 * Local interface type (specific to VPP)
86 */
87 const static type_t LOCAL;
88 /**
89 * TAP interface type
90 */
91 const static type_t TAP;
92
93 /**
Neale Ranns4ef42262018-02-20 08:10:44 -080094 * vhost-user interface type
95 */
96 const static type_t VHOST;
97
98 /**
Mohsin Kazmied76ee22018-03-02 12:31:37 +010099 * bond interface type
100 */
101 const static type_t BOND;
102
103 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700104 * Convert VPP's name of the interface to a type
105 */
106 static type_t from_string(const std::string& str);
107
108 private:
109 /**
110 * Private constructor taking the value and the string name
111 */
112 type_t(int v, const std::string& s);
113 };
114
115 /**
116 * The admin state of the interface
117 */
118 struct admin_state_t : enum_base<admin_state_t>
119 {
120 /**
121 * Admin DOWN state
122 */
123 const static admin_state_t DOWN;
124 /**
125 * Admin UP state
126 */
127 const static admin_state_t UP;
128
129 /**
130 * Convert VPP's numerical value to enum type
131 */
132 static admin_state_t from_int(uint8_t val);
133
134 private:
135 /**
136 * Private constructor taking the value and the string name
137 */
138 admin_state_t(int v, const std::string& s);
139 };
140
141 /**
142 * The oper state of the interface
143 */
144 struct oper_state_t : enum_base<oper_state_t>
145 {
146 /**
147 * Operational DOWN state
148 */
149 const static oper_state_t DOWN;
150 /**
151 * Operational UP state
152 */
153 const static oper_state_t UP;
154
155 /**
156 * Convert VPP's numerical value to enum type
157 */
158 static oper_state_t from_int(uint8_t val);
159
160 private:
161 /**
162 * Private constructor taking the value and the string name
163 */
164 oper_state_t(int v, const std::string& s);
165 };
166
167 /**
168 * Construct a new object matching the desried state
169 */
Neale Ranns4ef42262018-02-20 08:10:44 -0800170 interface(const std::string& name,
171 type_t type,
172 admin_state_t state,
173 const std::string& tag = "");
Neale Ranns812ed392017-10-16 04:20:13 -0700174 /**
175 * Construct a new object matching the desried state mapped
176 * to a specific route_domain
177 */
178 interface(const std::string& name,
179 type_t type,
180 admin_state_t state,
Neale Ranns4ef42262018-02-20 08:10:44 -0800181 const route_domain& rd,
182 const std::string& tag = "");
Neale Ranns812ed392017-10-16 04:20:13 -0700183 /**
184 * Destructor
185 */
186 virtual ~interface();
187
188 /**
189 * Copy Constructor
190 */
191 interface(const interface& o);
192
193 static const_iterator_t cbegin();
194 static const_iterator_t cend();
195
196 /**
197 * Return the matching'singular' of the interface
198 */
199 std::shared_ptr<interface> singular() const;
200
201 /**
202 * convert to string format for debug purposes
203 */
204 virtual std::string to_string(void) const;
205
206 /**
207 * Return VPP's handle to this object
208 */
209 const handle_t& handle() const;
210
211 /**
212 * Return the interface type
213 */
214 const type_t& type() const;
215
216 /**
217 * Return the interface type
218 */
219 const std::string& name() const;
220
221 /**
222 * Return the interface type
223 */
Neale Rannsfd920602017-11-23 12:15:00 -0800224 const key_t& key() const;
Neale Ranns812ed392017-10-16 04:20:13 -0700225
226 /**
227 * Return the L2 Address
228 */
229 const l2_address_t& l2_address() const;
230
231 /**
Neale Ranns4ef42262018-02-20 08:10:44 -0800232 * Set the admin state of the interface
233 */
234 void set(const admin_state_t& state);
235
236 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700237 * Set the L2 Address
238 */
239 void set(const l2_address_t& addr);
240
241 /**
242 * Set the operational state of the interface, as reported by VPP
243 */
244 void set(const oper_state_t& state);
245
246 /**
Neale Ranns4ef42262018-02-20 08:10:44 -0800247 * Set the tag to the interface
248 */
249 void set(const std::string& tag);
250
251 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800252 * Comparison operator - only used for UT
253 */
254 virtual bool operator==(const interface& i) const;
255
256 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700257 * A base class for interface Create commands
258 */
259 template <typename MSG>
260 class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
261 {
262 public:
263 create_cmd(HW::item<handle_t>& item, const std::string& name)
264 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
265 , m_name(name)
266 {
267 }
268
269 /**
270 * Destructor
271 */
272 virtual ~create_cmd() = default;
273
274 /**
275 * Comparison operator - only used for UT
276 */
277 virtual bool operator==(const create_cmd& o) const
278 {
279 return (m_name == o.m_name);
280 }
281
282 /**
283 * Indicate the succeeded, when the HW Q is disabled.
284 */
285 void succeeded()
286 {
287 rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
288 interface::add(m_name, this->item());
289 }
290
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700291 /**
292 * add the created interface to the DB
293 */
294 void insert_interface() { interface::add(m_name, this->item()); }
295
Neale Ranns812ed392017-10-16 04:20:13 -0700296 virtual vapi_error_e operator()(MSG& reply)
297 {
298 int sw_if_index = reply.get_response().get_payload().sw_if_index;
299 int retval = reply.get_response().get_payload().retval;
300
301 VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
302
303 rc_t rc = rc_t::from_vpp_retval(retval);
304 handle_t handle = handle_t::INVALID;
305
306 if (rc_t::OK == rc) {
307 handle = sw_if_index;
308 }
309
310 HW::item<handle_t> res(handle, rc);
311
312 this->fulfill(res);
313
314 return (VAPI_OK);
315 }
316
317 protected:
318 /**
319 * The name of the interface to be created
320 */
321 const std::string& m_name;
322 };
323
324 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700325 * Base class for intterface Delete commands
326 */
327 template <typename MSG>
328 class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
329 {
330 public:
331 delete_cmd(HW::item<handle_t>& item, const std::string& name)
332 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
333 , m_name(name)
334 {
335 }
336
337 delete_cmd(HW::item<handle_t>& item)
338 : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
339 , m_name()
340 {
341 }
342
343 /**
344 * Destructor
345 */
346 virtual ~delete_cmd() = default;
347
348 /**
349 * Comparison operator - only used for UT
350 */
351 virtual bool operator==(const delete_cmd& o) const
352 {
353 return (this->m_hw_item == o.m_hw_item);
354 }
355
356 /**
357 * Indicate the succeeded, when the HW Q is disabled.
358 */
359 void succeeded() {}
360
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700361 /**
Mohsin Kazmi3a758b02018-02-27 14:05:15 +0100362 * remove the deleted interface from the DB
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700363 */
364 void remove_interface() { interface::remove(this->item()); }
365
Neale Ranns812ed392017-10-16 04:20:13 -0700366 protected:
367 /**
368 * The name of the interface to be created
369 */
370 const std::string m_name;
371 };
372
373 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700374 * A class that listens to interface Events
375 */
376 class event_listener
377 {
378 public:
379 /**
380 * Default Constructor
381 */
382 event_listener();
383
384 /**
385 * Virtual function called on the listener when the command has data
386 * ready to process
387 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700388 virtual void handle_interface_event(interface_cmds::events_cmd* cmd) = 0;
Neale Ranns812ed392017-10-16 04:20:13 -0700389
390 /**
391 * Return the HW::item representing the status
392 */
393 HW::item<bool>& status();
394
395 protected:
396 /**
397 * The status of the subscription
398 */
399 HW::item<bool> m_status;
400 };
401
402 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700403 * A class that listens to interface Stats
404 */
405 class stat_listener
406 {
407 public:
408 /**
409 * Default Constructor
410 */
411 stat_listener();
412
413 /**
414 * Virtual function called on the listener when the command has data
415 * ready to process
416 */
Neale Rannsa2ee0292017-11-28 22:29:13 -0800417 virtual void handle_interface_stat(
418 interface_cmds::stats_enable_cmd* cmd) = 0;
Neale Ranns812ed392017-10-16 04:20:13 -0700419
420 /**
421 * Return the HW::item representing the status
422 */
423 HW::item<bool>& status();
424
425 protected:
426 /**
427 * The status of the subscription
428 */
429 HW::item<bool> m_status;
430 };
431
432 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800433 * The the singular instance of the interface in the DB by handle
Neale Ranns812ed392017-10-16 04:20:13 -0700434 */
435 static std::shared_ptr<interface> find(const handle_t& h);
436
437 /**
Neale Rannsfd920602017-11-23 12:15:00 -0800438 * The the singular instance of the interface in the DB by key
Neale Ranns812ed392017-10-16 04:20:13 -0700439 */
Neale Rannsfd920602017-11-23 12:15:00 -0800440 static std::shared_ptr<interface> find(const key_t& k);
Neale Ranns812ed392017-10-16 04:20:13 -0700441
442 /**
443 * Dump all interfaces into the stream provided
444 */
445 static void dump(std::ostream& os);
446
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100447 /**
448 * Enable stats for this interface
449 */
450 void enable_stats(stat_listener& el);
451
Neale Ranns812ed392017-10-16 04:20:13 -0700452protected:
453 /**
Neale Rannsa2ee0292017-11-28 22:29:13 -0800454 * Set the handle of an interface object. Only called by the interface
455 * factory during the populate
Neale Ranns812ed392017-10-16 04:20:13 -0700456 */
Neale Rannsa2ee0292017-11-28 22:29:13 -0800457 void set(const handle_t& handle);
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700458 friend class interface_factory;
Neale Ranns812ed392017-10-16 04:20:13 -0700459
460 /**
461 * The SW interface handle VPP has asigned to the interface
462 */
463 HW::item<handle_t> m_hdl;
464
465 /**
466 * Return the matching 'singular' of the interface
467 */
468 virtual std::shared_ptr<interface> singular_i() const;
469
470 /**
471 * release/remove an interface form the singular store
472 */
473 void release();
474
475 /**
476 * Virtual functions to construct an interface create commands.
477 * Overridden in derived classes like the sub_interface
478 */
479 virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
480
481 /**
482 * Virtual functions to construct an interface delete commands.
483 * Overridden in derived classes like the sub_interface
484 */
485 virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
486
487 /**
488 * Sweep/reap the object if still stale
489 */
490 virtual void sweep(void);
491
492 /**
493 * A map of all interfaces key against the interface's name
494 */
Neale Rannsfd920602017-11-23 12:15:00 -0800495 static singular_db<key_t, interface> m_db;
Neale Ranns812ed392017-10-16 04:20:13 -0700496
497 /**
498 * Add an interface to the DB keyed on handle
499 */
Neale Rannsfd920602017-11-23 12:15:00 -0800500 static void add(const key_t& name, const HW::item<handle_t>& item);
Neale Ranns812ed392017-10-16 04:20:13 -0700501
502 /**
503 * remove an interface from the DB keyed on handle
504 */
505 static void remove(const HW::item<handle_t>& item);
506
507private:
508 /**
509 * Class definition for listeners to OM events
510 */
511 class event_handler : public OM::listener, public inspect::command_handler
512 {
513 public:
514 event_handler();
515 virtual ~event_handler() = default;
516
517 /**
518 * Handle a populate event
519 */
520 void handle_populate(const client_db::key_t& key);
521
522 /**
523 * Handle a replay event
524 */
525 void handle_replay();
526
527 /**
528 * Show the object in the Singular DB
529 */
530 void show(std::ostream& os);
531
532 /**
533 * Get the sortable Id of the listener
534 */
535 dependency_t order() const;
536 };
537
538 static event_handler m_evh;
539
540 /**
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100541 * enable the interface stats in the singular instance
542 */
543 void enable_stats_i(stat_listener& el);
544
545 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700546 * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
547 */
548 void update(const interface& obj);
549
550 /*
Neale Ranns263f9552017-11-15 02:52:13 -0800551 * return the interface's handle in the singular instance
552 */
553 const handle_t& handle_i() const;
554
555 /*
Neale Ranns812ed392017-10-16 04:20:13 -0700556 * It's the OM class that calls singular()
557 */
558 friend class OM;
559
560 /**
561 * It's the singular_db class that calls replay()
562 */
Neale Rannsfd920602017-11-23 12:15:00 -0800563 friend class singular_db<key_t, interface>;
Neale Ranns812ed392017-10-16 04:20:13 -0700564
565 /**
566 * The interfaces name
567 */
568 const std::string m_name;
569
570 /**
571 * The interface type. clearly this cannot be changed
572 * once the interface has been created.
573 */
574 const type_t m_type;
575
576 /**
577 * shared pointer to the routeDoamin the interface is in.
Neale Ranns352ea0c2017-11-14 11:04:28 -0800578 * NULL is not mapped - i.e. in the default table
Neale Ranns812ed392017-10-16 04:20:13 -0700579 */
Neale Ranns352ea0c2017-11-14 11:04:28 -0800580 std::shared_ptr<route_domain> m_rd;
Neale Ranns812ed392017-10-16 04:20:13 -0700581
582 /**
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100583 * shared pointer to the stats object for this interface.
584 */
Neale Rannsa2ee0292017-11-28 22:29:13 -0800585 std::shared_ptr<interface_cmds::stats_enable_cmd> m_stats;
Mohsin Kazmi5a4f96a2017-11-20 10:23:47 +0100586
587 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700588 * The state of the interface
589 */
590 HW::item<admin_state_t> m_state;
591
592 /**
593 * HW state of the VPP table mapping
594 */
595 HW::item<route::table_id_t> m_table_id;
596
597 /**
598 * HW state of the L2 address
599 */
600 HW::item<l2_address_t> m_l2_address;
601
602 /**
603 * Operational state of the interface
604 */
605 oper_state_t m_oper;
606
607 /**
Neale Ranns4ef42262018-02-20 08:10:44 -0800608 * tag of the interface
609 */
610 std::string m_tag;
611
612 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700613 * A map of all interfaces keyed against VPP's handle
614 */
615 static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
616
617 /**
618 * replay the object to create it in hardware
619 */
620 virtual void replay(void);
621
622 /**
623 * Create commands are firends so they can add interfaces to the
624 * handle store.
625 */
626 template <typename MSG>
627 friend class create_cmd;
628
629 /**
630 * Create commands are firends so they can remove interfaces from the
631 * handle store.
632 */
633 template <typename MSG>
634 friend class delete_cmd;
635};
636};
637/*
638 * fd.io coding-style-patch-verification: ON
639 *
640 * Local Variables:
641 * eval: (c-set-style "mozilla")
642 * End:
643 */
644#endif