1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __VOM_INTERFACE_H__
#define __VOM_INTERFACE_H__
#include "vom/dump_cmd.hpp"
#include "vom/enum_base.hpp"
#include "vom/event_cmd.hpp"
#include "vom/hw.hpp"
#include "vom/inspect.hpp"
#include "vom/object_base.hpp"
#include "vom/om.hpp"
#include "vom/prefix.hpp"
#include "vom/route_domain.hpp"
#include "vom/rpc_cmd.hpp"
#include "vom/singular_db.hpp"
#include <vapi/af_packet.api.vapi.hpp>
#include <vapi/interface.api.vapi.hpp>
#include <vapi/stats.api.vapi.hpp>
#include <vapi/tap.api.vapi.hpp>
#include <vapi/vapi.hpp>
#include <vapi/vpe.api.vapi.hpp>
namespace VOM {
/**
* A representation of an interface in VPP
*/
class interface : public object_base
{
public:
/**
* The key for interface's key
*/
typedef std::string key_type;
/**
* The iterator type
*/
typedef singular_db<const std::string, interface>::const_iterator
const_iterator_t;
/**
* An interface type
*/
struct type_t : enum_base<type_t>
{
/**
* Unkown type
*/
const static type_t UNKNOWN;
/**
* A brideged Virtual interface (aka SVI or IRB)
*/
const static type_t BVI;
/**
* VXLAN interface
*/
const static type_t VXLAN;
/**
* Ethernet interface type
*/
const static type_t ETHERNET;
/**
* AF-Packet interface type
*/
const static type_t AFPACKET;
/**
* loopback interface type
*/
const static type_t LOOPBACK;
/**
* Local interface type (specific to VPP)
*/
const static type_t LOCAL;
/**
* TAP interface type
*/
const static type_t TAP;
/**
* Convert VPP's name of the interface to a type
*/
static type_t from_string(const std::string& str);
private:
/**
* Private constructor taking the value and the string name
*/
type_t(int v, const std::string& s);
};
/**
* The admin state of the interface
*/
struct admin_state_t : enum_base<admin_state_t>
{
/**
* Admin DOWN state
*/
const static admin_state_t DOWN;
/**
* Admin UP state
*/
const static admin_state_t UP;
/**
* Convert VPP's numerical value to enum type
*/
static admin_state_t from_int(uint8_t val);
private:
/**
* Private constructor taking the value and the string name
*/
admin_state_t(int v, const std::string& s);
};
/**
* The oper state of the interface
*/
struct oper_state_t : enum_base<oper_state_t>
{
/**
* Operational DOWN state
*/
const static oper_state_t DOWN;
/**
* Operational UP state
*/
const static oper_state_t UP;
/**
* Convert VPP's numerical value to enum type
*/
static oper_state_t from_int(uint8_t val);
private:
/**
* Private constructor taking the value and the string name
*/
oper_state_t(int v, const std::string& s);
};
/**
* Construct a new object matching the desried state
*/
interface(const std::string& name, type_t type, admin_state_t state);
/**
* Construct a new object matching the desried state mapped
* to a specific route_domain
*/
interface(const std::string& name,
type_t type,
admin_state_t state,
const route_domain& rd);
/**
* Destructor
*/
virtual ~interface();
/**
* Copy Constructor
*/
interface(const interface& o);
static const_iterator_t cbegin();
static const_iterator_t cend();
/**
* Return the matching'singular' of the interface
*/
std::shared_ptr<interface> singular() const;
/**
* convert to string format for debug purposes
*/
virtual std::string to_string(void) const;
/**
* Return VPP's handle to this object
*/
const handle_t& handle() const;
/**
* Return the interface type
*/
const type_t& type() const;
/**
* Return the interface type
*/
const std::string& name() const;
/**
* Return the interface type
*/
const key_type& key() const;
/**
* Return the L2 Address
*/
const l2_address_t& l2_address() const;
/**
* Set the L2 Address
*/
void set(const l2_address_t& addr);
/**
* Set the operational state of the interface, as reported by VPP
*/
void set(const oper_state_t& state);
/**
* A base class for interface Create commands
*/
template <typename MSG>
class create_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
{
public:
create_cmd(HW::item<handle_t>& item, const std::string& name)
: rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
, m_name(name)
{
}
/**
* Destructor
*/
virtual ~create_cmd() = default;
/**
* Comparison operator - only used for UT
*/
virtual bool operator==(const create_cmd& o) const
{
return (m_name == o.m_name);
}
/**
* Indicate the succeeded, when the HW Q is disabled.
*/
void succeeded()
{
rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>::succeeded();
interface::add(m_name, this->item());
}
virtual vapi_error_e operator()(MSG& reply)
{
int sw_if_index = reply.get_response().get_payload().sw_if_index;
int retval = reply.get_response().get_payload().retval;
VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
rc_t rc = rc_t::from_vpp_retval(retval);
handle_t handle = handle_t::INVALID;
if (rc_t::OK == rc) {
handle = sw_if_index;
}
HW::item<handle_t> res(handle, rc);
this->fulfill(res);
return (VAPI_OK);
}
protected:
/**
* The name of the interface to be created
*/
const std::string& m_name;
};
/**
* A command class to create Loopback interfaces in VPP
*/
class loopback_create_cmd : public create_cmd<vapi::Create_loopback>
{
public:
/**
* Constructor taking the HW::item to update
* and the name of the interface to create
*/
loopback_create_cmd(HW::item<handle_t>& item, const std::string& name);
~loopback_create_cmd() = default;
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
};
/**
* A command class to create af_packet interfaces in VPP
*/
class af_packet_create_cmd : public create_cmd<vapi::Af_packet_create>
{
public:
/**
* Constructor taking the HW::item to update
* and the name of the interface to create
*/
af_packet_create_cmd(HW::item<handle_t>& item, const std::string& name);
~af_packet_create_cmd() = default;
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
};
/**
* A command class to create TAP interfaces in VPP
*/
class tap_create_cmd : public create_cmd<vapi::Tap_connect>
{
public:
/**
* Constructor taking the HW::item to update
* and the name of the interface to create
*/
tap_create_cmd(HW::item<handle_t>& item, const std::string& name);
~tap_create_cmd() = default;
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
};
/**
* Base class for intterface Delete commands
*/
template <typename MSG>
class delete_cmd : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>
{
public:
delete_cmd(HW::item<handle_t>& item, const std::string& name)
: rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
, m_name(name)
{
}
delete_cmd(HW::item<handle_t>& item)
: rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, MSG>(item)
, m_name()
{
}
/**
* Destructor
*/
virtual ~delete_cmd() = default;
/**
* Comparison operator - only used for UT
*/
virtual bool operator==(const delete_cmd& o) const
{
return (this->m_hw_item == o.m_hw_item);
}
/**
* Indicate the succeeded, when the HW Q is disabled.
*/
void succeeded() {}
protected:
/**
* The name of the interface to be created
*/
const std::string m_name;
};
/**
* A command class to delete loopback interfaces in VPP
*/
class loopback_delete_cmd : public delete_cmd<vapi::Delete_loopback>
{
public:
/**
* Constructor taking the HW::item to update
*/
loopback_delete_cmd(HW::item<handle_t>& item);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
};
/**
* A command class to delete af-packet interfaces in VPP
*/
class af_packet_delete_cmd : public delete_cmd<vapi::Af_packet_delete>
{
public:
/**
* Constructor taking the HW::item to update
* and the name of the interface to delete
*/
af_packet_delete_cmd(HW::item<handle_t>& item, const std::string& name);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
};
/**
* A command class to delete TAP interfaces in VPP
*/
class tap_delete_cmd : public delete_cmd<vapi::Tap_delete>
{
public:
/**
* Constructor taking the HW::item to update
*/
tap_delete_cmd(HW::item<handle_t>& item);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
};
/**
* A command class to delete TAP interfaces in VPP
*/
class set_tag
: public rpc_cmd<HW::item<handle_t>, rc_t, vapi::Sw_interface_tag_add_del>
{
public:
/**
* Constructor taking the HW::item to update
*/
set_tag(HW::item<handle_t>& item, const std::string& name);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const set_tag& i) const;
private:
/**
* The tag to add
*/
const std::string m_name;
};
/**
* A cmd class that changes the admin state
*/
class state_change_cmd : public rpc_cmd<HW::item<admin_state_t>,
rc_t,
vapi::Sw_interface_set_flags>
{
public:
/**
* Constructor taking the HW::item to update
* and the name handle of the interface whose state is to change
*/
state_change_cmd(HW::item<admin_state_t>& s, const HW::item<handle_t>& h);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const state_change_cmd& i) const;
private:
/**
* the handle of the interface to update
*/
const HW::item<handle_t>& m_hdl;
};
/**
* A command class that binds an interface to an L3 table
*/
class set_table_cmd : public rpc_cmd<HW::item<route::table_id_t>,
rc_t,
vapi::Sw_interface_set_table>
{
public:
/**
* Constructor taking the HW::item to update
* and the name handle of the interface whose table is to change
*/
set_table_cmd(HW::item<route::table_id_t>& item,
const l3_proto_t& proto,
const HW::item<handle_t>& h);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const set_table_cmd& i) const;
private:
/**
* the handle of the interface to update
*/
const HW::item<handle_t>& m_hdl;
/**
* The L3 protocol of the table
*/
l3_proto_t m_proto;
};
/**
* A command class that binds an interface to an L3 table
*/
class set_mac_cmd : public rpc_cmd<HW::item<l2_address_t>,
rc_t,
vapi::Sw_interface_set_mac_address>
{
public:
/**
* Constructor taking the HW::item to update
* and the handle of the interface
*/
set_mac_cmd(HW::item<l2_address_t>& item, const HW::item<handle_t>& h);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const set_mac_cmd& i) const;
private:
/**
* the handle of the interface to update
*/
const HW::item<handle_t>& m_hdl;
};
/**
* Forward declaration of the Event command
*/
class events_cmd;
/**
* A class that listens to interface Events
*/
class event_listener
{
public:
/**
* Default Constructor
*/
event_listener();
/**
* Virtual function called on the listener when the command has data
* ready to process
*/
virtual void handle_interface_event(events_cmd* cmd) = 0;
/**
* Return the HW::item representing the status
*/
HW::item<bool>& status();
protected:
/**
* The status of the subscription
*/
HW::item<bool> m_status;
};
/**
* A command class represents our desire to recieve interface events
*/
class events_cmd
: public event_cmd<vapi::Want_interface_events, vapi::Sw_interface_event>
{
public:
/**
* Constructor taking the listner to notify
*/
events_cmd(event_listener& el);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* Retires the command - unsubscribe from the events.
*/
void retire(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const events_cmd& i) const;
/**
* Called when it's time to poke the listeners
*/
void notify();
private:
/**
* The listeners to notify when data/events arrive
*/
event_listener& m_listener;
};
/**
* Forward declaration of the stat command
*/
class stats_cmd;
/**
* A class that listens to interface Stats
*/
class stat_listener
{
public:
/**
* Default Constructor
*/
stat_listener();
/**
* Virtual function called on the listener when the command has data
* ready to process
*/
virtual void handle_interface_stat(stats_cmd* cmd) = 0;
/**
* Return the HW::item representing the status
*/
HW::item<bool>& status();
protected:
/**
* The status of the subscription
*/
HW::item<bool> m_status;
};
/**
* A command class represents our desire to recieve interface stats
*/
class stats_cmd : public event_cmd<vapi::Want_per_interface_combined_stats,
vapi::Vnet_per_interface_combined_counters>
{
public:
/**
* Constructor taking the listner to notify
*/
stats_cmd(stat_listener& el, const std::vector<handle_t>& interfaces);
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* Retires the command - unsubscribe from the stats.
*/
void retire(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const stats_cmd& i) const;
/**
* Called when it's time to poke the listeners
*/
void notify();
private:
/**
* The listeners to notify when data/stats arrive
*/
stat_listener& m_listener;
std::vector<handle_t> m_swifindex;
};
/**
* A cmd class that Dumps all the Vpp interfaces
*/
class dump_cmd : public VOM::dump_cmd<vapi::Sw_interface_dump>
{
public:
/**
* Default Constructor
*/
dump_cmd();
/**
* Issue the command to VPP/HW
*/
rc_t issue(connection& con);
/**
* convert to string format for debug purposes
*/
std::string to_string() const;
/**
* Comparison operator - only used for UT
*/
bool operator==(const dump_cmd& i) const;
};
/**
* The the singular instance of the interface in the object_base-Model
*/
static std::shared_ptr<interface> find(const interface& temp);
/**
* The the singular instance of the interface in the object_base-Model
* by handle
*/
static std::shared_ptr<interface> find(const handle_t& h);
/**
* The the singular instance of the interface in the object_base-Model
* by name
*/
static std::shared_ptr<interface> find(const std::string& s);
/**
* Dump all interfaces into the stream provided
*/
static void dump(std::ostream& os);
/**
* Factory method to construct a new interface from the VPP record
*/
static std::unique_ptr<interface> new_interface(
const vapi_payload_sw_interface_details& vd);
protected:
/**
* Construct an interface object with a handle and a HW address
*/
interface(const handle_t& handle,
const l2_address_t& l2_address,
const std::string& name,
type_t type,
admin_state_t state);
/**
* The SW interface handle VPP has asigned to the interface
*/
HW::item<handle_t> m_hdl;
/**
* Return the matching 'singular' of the interface
*/
virtual std::shared_ptr<interface> singular_i() const;
/**
* release/remove an interface form the singular store
*/
void release();
/**
* Virtual functions to construct an interface create commands.
* Overridden in derived classes like the sub_interface
*/
virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
/**
* Virtual functions to construct an interface delete commands.
* Overridden in derived classes like the sub_interface
*/
virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
/**
* Sweep/reap the object if still stale
*/
virtual void sweep(void);
/**
* A map of all interfaces key against the interface's name
*/
static singular_db<const std::string, interface> m_db;
/**
* Add an interface to the DB keyed on handle
*/
static void add(const std::string& name, const HW::item<handle_t>& item);
/**
* remove an interface from the DB keyed on handle
*/
static void remove(const HW::item<handle_t>& item);
private:
/**
* Class definition for listeners to OM events
*/
class event_handler : public OM::listener, public inspect::command_handler
{
public:
event_handler();
virtual ~event_handler() = default;
/**
* Handle a populate event
*/
void handle_populate(const client_db::key_t& key);
/**
* Handle a replay event
*/
void handle_replay();
/**
* Show the object in the Singular DB
*/
void show(std::ostream& os);
/**
* Get the sortable Id of the listener
*/
dependency_t order() const;
};
static event_handler m_evh;
/**
* Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
*/
void update(const interface& obj);
/*
* It's the OM class that calls singular()
*/
friend class OM;
/**
* It's the singular_db class that calls replay()
*/
friend class singular_db<const std::string, interface>;
/**
* The interfaces name
*/
const std::string m_name;
/**
* The interface type. clearly this cannot be changed
* once the interface has been created.
*/
const type_t m_type;
/**
* shared pointer to the routeDoamin the interface is in.
* NULL is not mapped - i.e. in eht default table
*/
const std::shared_ptr<route_domain> m_rd;
/**
* The state of the interface
*/
HW::item<admin_state_t> m_state;
/**
* HW state of the VPP table mapping
*/
HW::item<route::table_id_t> m_table_id;
/**
* HW state of the L2 address
*/
HW::item<l2_address_t> m_l2_address;
/**
* Operational state of the interface
*/
oper_state_t m_oper;
/**
* A map of all interfaces keyed against VPP's handle
*/
static std::map<handle_t, std::weak_ptr<interface>> m_hdl_db;
/**
* replay the object to create it in hardware
*/
virtual void replay(void);
/**
* Create commands are firends so they can add interfaces to the
* handle store.
*/
template <typename MSG>
friend class create_cmd;
/**
* Create commands are firends so they can remove interfaces from the
* handle store.
*/
template <typename MSG>
friend class delete_cmd;
};
};
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "mozilla")
* End:
*/
#endif
|