1d0c4f543bc7ce13a8c5824df85f0bdf4900f2d3
[vpp.git] / extras / vom / vom / interface_cmds.hpp
1 /*
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_CMDS_H__
17 #define __VOM_INTERFACE_CMDS_H__
18
19 #include <vapi/vapi.hpp>
20
21 #include "vom/dump_cmd.hpp"
22 #include "vom/event_cmd.hpp"
23 #include "vom/interface.hpp"
24 #include "vom/rpc_cmd.hpp"
25
26 #include <vapi/af_packet.api.vapi.hpp>
27 #include <vapi/interface.api.vapi.hpp>
28 #include <vapi/stats.api.vapi.hpp>
29 #include <vapi/tap.api.vapi.hpp>
30 #include <vapi/vhost_user.api.vapi.hpp>
31 #include <vapi/vpe.api.vapi.hpp>
32
33 namespace VOM {
34
35 namespace interface_cmds {
36 /**
37  * Factory method to construct a new interface from the VPP record
38  */
39 std::unique_ptr<interface> new_interface(
40   const vapi_payload_sw_interface_details& vd);
41
42 /**
43  * A command class to create Loopback interfaces in VPP
44  */
45 class loopback_create_cmd : public interface::create_cmd<vapi::Create_loopback>
46 {
47 public:
48   /**
49    * Constructor taking the HW::item to update
50    * and the name of the interface to create
51    */
52   loopback_create_cmd(HW::item<handle_t>& item, const std::string& name);
53   ~loopback_create_cmd() = default;
54
55   /**
56    * Issue the command to VPP/HW
57    */
58   rc_t issue(connection& con);
59
60   /**
61  * convert to string format for debug purposes
62  */
63   std::string to_string() const;
64 };
65
66 /**
67  * A command class to create af_packet interfaces in VPP
68  */
69 class af_packet_create_cmd
70   : public interface::create_cmd<vapi::Af_packet_create>
71 {
72 public:
73   /**
74    * Constructor taking the HW::item to update
75    * and the name of the interface to create
76    */
77   af_packet_create_cmd(HW::item<handle_t>& item, const std::string& name);
78   ~af_packet_create_cmd() = default;
79   /**
80    * Issue the command to VPP/HW
81    */
82   rc_t issue(connection& con);
83   /**
84    * convert to string format for debug purposes
85    */
86   std::string to_string() const;
87 };
88
89 /**
90  * A functor class that creates an interface
91  */
92 class vhost_create_cmd
93   : public interface::create_cmd<vapi::Create_vhost_user_if>
94 {
95 public:
96   vhost_create_cmd(HW::item<handle_t>& item,
97                    const std::string& name,
98                    const std::string& tag);
99
100   /**
101    * Issue the command to VPP/HW
102    */
103   rc_t issue(connection& con);
104   /**
105    * convert to string format for debug purposes
106    */
107   std::string to_string() const;
108
109 private:
110   const std::string m_tag;
111 };
112
113 /**
114  * A command class to delete loopback interfaces in VPP
115  */
116 class loopback_delete_cmd : public interface::delete_cmd<vapi::Delete_loopback>
117 {
118 public:
119   /**
120    * Constructor taking the HW::item to update
121    */
122   loopback_delete_cmd(HW::item<handle_t>& item);
123
124   /**
125    * Issue the command to VPP/HW
126    */
127   rc_t issue(connection& con);
128   /**
129    * convert to string format for debug purposes
130    */
131   std::string to_string() const;
132 };
133
134 /**
135  * A command class to delete af-packet interfaces in VPP
136  */
137 class af_packet_delete_cmd
138   : public interface::delete_cmd<vapi::Af_packet_delete>
139 {
140 public:
141   /**
142    * Constructor taking the HW::item to update
143    * and the name of the interface to delete
144    */
145   af_packet_delete_cmd(HW::item<handle_t>& item, const std::string& name);
146
147   /**
148    * Issue the command to VPP/HW
149    */
150   rc_t issue(connection& con);
151   /**
152    * convert to string format for debug purposes
153    */
154   std::string to_string() const;
155 };
156
157 /**
158  * A functor class that deletes a Vhost interface
159  */
160 class vhost_delete_cmd
161   : public interface::delete_cmd<vapi::Delete_vhost_user_if>
162 {
163 public:
164   vhost_delete_cmd(HW::item<handle_t>& item, const std::string& name);
165
166   /**
167    * Issue the command to VPP/HW
168    */
169   rc_t issue(connection& con);
170   /**
171    * convert to string format for debug purposes
172    */
173   std::string to_string() const;
174 };
175
176 /**
177  * A command class to set tag on interfaces
178  */
179 class set_tag
180   : public rpc_cmd<HW::item<handle_t>, rc_t, vapi::Sw_interface_tag_add_del>
181 {
182 public:
183   /**
184    * Constructor taking the HW::item to update
185    */
186   set_tag(HW::item<handle_t>& item, const std::string& name);
187
188   /**
189    * Issue the command to VPP/HW
190    */
191   rc_t issue(connection& con);
192
193   /**
194    * convert to string format for debug purposes
195    */
196   std::string to_string() const;
197
198   /**
199    * Comparison operator - only used for UT
200    */
201   bool operator==(const set_tag& i) const;
202
203 private:
204   /**
205    * The tag to add
206    */
207   const std::string m_name;
208 };
209
210 /**
211  * A cmd class that changes the admin state
212  */
213 class state_change_cmd : public rpc_cmd<HW::item<interface::admin_state_t>,
214                                         rc_t,
215                                         vapi::Sw_interface_set_flags>
216 {
217 public:
218   /**
219    * Constructor taking the HW::item to update
220    * and the name handle of the interface whose state is to change
221    */
222   state_change_cmd(HW::item<interface::admin_state_t>& s,
223                    const HW::item<handle_t>& h);
224
225   /**
226    * Issue the command to VPP/HW
227    */
228   rc_t issue(connection& con);
229   /**
230    * convert to string format for debug purposes
231    */
232   std::string to_string() const;
233
234   /**
235    * Comparison operator - only used for UT
236    */
237   bool operator==(const state_change_cmd& i) const;
238
239 private:
240   /**
241    * the handle of the interface to update
242    */
243   const HW::item<handle_t>& m_hdl;
244 };
245
246 /**
247  * A command class that binds an interface to an L3 table
248  */
249 class set_table_cmd : public rpc_cmd<HW::item<route::table_id_t>,
250                                      rc_t,
251                                      vapi::Sw_interface_set_table>
252 {
253 public:
254   /**
255    * Constructor taking the HW::item to update
256    * and the name handle of the interface whose table is to change
257    */
258   set_table_cmd(HW::item<route::table_id_t>& item,
259                 const l3_proto_t& proto,
260                 const HW::item<handle_t>& h);
261
262   /**
263    * Issue the command to VPP/HW
264    */
265   rc_t issue(connection& con);
266
267   /**
268    * convert to string format for debug purposes
269    */
270   std::string to_string() const;
271
272   /**
273    * Comparison operator - only used for UT
274    */
275   bool operator==(const set_table_cmd& i) const;
276
277 private:
278   /**
279    * the handle of the interface to update
280    */
281   const HW::item<handle_t>& m_hdl;
282
283   /**
284    * The L3 protocol of the table
285    */
286   l3_proto_t m_proto;
287 };
288
289 /**
290  * A command class that changes the MAC address on an interface
291  */
292 class set_mac_cmd : public rpc_cmd<HW::item<l2_address_t>,
293                                    rc_t,
294                                    vapi::Sw_interface_set_mac_address>
295 {
296 public:
297   /**
298    * Constructor taking the HW::item to update
299    * and the handle of the interface
300    */
301   set_mac_cmd(HW::item<l2_address_t>& item, const HW::item<handle_t>& h);
302
303   /**
304    * Issue the command to VPP/HW
305    */
306   rc_t issue(connection& con);
307
308   /**
309    * convert to string format for debug purposes
310    */
311   std::string to_string() const;
312
313   /**
314    * Comparison operator - only used for UT
315    */
316   bool operator==(const set_mac_cmd& i) const;
317
318 private:
319   /**
320    * the handle of the interface to update
321    */
322   const HW::item<handle_t>& m_hdl;
323 };
324
325 /**
326  * A command class that enables detailed stats collection on an interface
327  */
328 class collect_detail_stats_change_cmd
329   : public rpc_cmd<HW::item<interface::stats_type_t>,
330                    rc_t,
331                    vapi::Collect_detailed_interface_stats>
332 {
333 public:
334   /**
335    * Constructor taking the HW::item to update
336    * and the handle of the interface
337    */
338   collect_detail_stats_change_cmd(HW::item<interface::stats_type_t>& item,
339                                   const handle_t& h,
340                                   bool enable);
341
342   /**
343    * Issue the command to VPP/HW
344    */
345   rc_t issue(connection& con);
346
347   /**
348    * convert to string format for debug purposes
349    */
350   std::string to_string() const;
351
352   /**
353    * Comparison operator - only used for UT
354    */
355   bool operator==(const collect_detail_stats_change_cmd& i) const;
356
357 private:
358   /**
359    * the handle of the interface to update
360    */
361   const handle_t& m_hdl;
362
363   /**
364    * enable or disable the detailed stats collection
365    */
366   bool m_enable;
367 };
368
369 /**
370  * A command class represents our desire to recieve interface events
371  */
372 class events_cmd
373   : public event_cmd<vapi::Want_interface_events, vapi::Sw_interface_event>
374 {
375 public:
376   /**
377    * Constructor taking the listner to notify
378    */
379   events_cmd(interface::event_listener& el);
380
381   /**
382    * Issue the command to VPP/HW
383    */
384   rc_t issue(connection& con);
385
386   /**
387    * Retires the command - unsubscribe from the events.
388    */
389   void retire(connection& con);
390
391   /**
392    * convert to string format for debug purposes
393    */
394   std::string to_string() const;
395
396   /**
397    * Comparison operator - only used for UT
398    */
399   bool operator==(const events_cmd& i) const;
400
401   /**
402    * Called when it's time to poke the listeners
403    */
404   void notify();
405
406 private:
407   /**
408    * The listeners to notify when data/events arrive
409    */
410   interface::event_listener& m_listener;
411 };
412
413 /**
414  * A command class represents our desire to recieve interface stats
415  */
416 class stats_enable_cmd
417   : public event_cmd<vapi::Want_per_interface_combined_stats,
418                      vapi::Vnet_per_interface_combined_counters>
419 {
420 public:
421   /**
422    * Constructor taking the listner to notify
423    */
424   stats_enable_cmd(interface::stat_listener& el, const handle_t& handle);
425
426   /**
427    * Issue the command to VPP/HW
428    */
429   rc_t issue(connection& con);
430
431   /**
432    * Retires the command - unsubscribe from the stats.
433    */
434   void retire(connection& con);
435
436   /**
437    * convert to string format for debug purposes
438    */
439   std::string to_string() const;
440
441   /**
442    * (re)set status
443    */
444   void set(const rc_t& rc);
445
446   /**
447    * get listener
448    */
449   interface::stat_listener& listener() const;
450
451   /**
452    * Comparison operator - only used for UT
453    */
454   bool operator==(const stats_enable_cmd& i) const;
455
456   /**
457    * Called when it's time to poke the listeners
458    */
459   void notify();
460
461 private:
462   /**
463    * The listeners to notify when data/stats arrive
464    */
465   interface::stat_listener& m_listener;
466
467   /**
468    * The interface on which we are enabling states
469    */
470   const handle_t& m_swifindex;
471 };
472
473 /**
474  * A command class represents our desire to recieve interface stats
475  */
476 class stats_disable_cmd
477   : public rpc_cmd<HW::item<bool>,
478                    rc_t,
479                    vapi::Want_per_interface_combined_stats>
480 {
481 public:
482   /**
483    * Constructor taking the listner to notify
484    */
485   stats_disable_cmd(const handle_t& handle);
486
487   /**
488    * Issue the command to VPP/HW
489    */
490   rc_t issue(connection& con);
491
492   /**
493    * convert to string format for debug purposes
494    */
495   std::string to_string() const;
496
497   /**
498    * Comparison operator - only used for UT
499    */
500   bool operator==(const stats_disable_cmd& i) const;
501
502 private:
503   HW::item<bool> m_res;
504   /**
505    * The interface on which we are disabling states
506    */
507   handle_t m_swifindex;
508 };
509
510 /**
511  * A cmd class that Dumps all the Vpp interfaces
512  */
513 class dump_cmd : public VOM::dump_cmd<vapi::Sw_interface_dump>
514 {
515 public:
516   /**
517    * Default Constructor
518    */
519   dump_cmd();
520
521   /**
522    * Issue the command to VPP/HW
523    */
524   rc_t issue(connection& con);
525   /**
526    * convert to string format for debug purposes
527    */
528   std::string to_string() const;
529
530   /**
531    * Comparison operator - only used for UT
532    */
533   bool operator==(const dump_cmd& i) const;
534 };
535
536 /**
537  * A cmd class that Dumps all the Vpp Interfaces
538  */
539 class vhost_dump_cmd : public VOM::dump_cmd<vapi::Sw_interface_vhost_user_dump>
540 {
541 public:
542   /**
543    * Default Constructor
544    */
545   vhost_dump_cmd();
546
547   /**
548    * Issue the command to VPP/HW
549    */
550   rc_t issue(connection& con);
551   /**
552    * convert to string format for debug purposes
553    */
554   std::string to_string() const;
555
556   /**
557    * Comparison operator - only used for UT
558    */
559   bool operator==(const vhost_dump_cmd& i) const;
560 };
561
562 /**
563  * A cmd class that Dumps all the Vpp interfaces
564  */
565 class af_packet_dump_cmd : public VOM::dump_cmd<vapi::Af_packet_dump>
566 {
567 public:
568   /**
569    * Default Constructor
570    */
571   af_packet_dump_cmd() = default;
572
573   /**
574    * Issue the command to VPP/HW
575    */
576   rc_t issue(connection& con);
577   /**
578    * convert to string format for debug purposes
579    */
580   std::string to_string() const;
581
582   /**
583    * Comparison operator - only used for UT
584    */
585   bool operator==(const af_packet_dump_cmd& i) const;
586 };
587 };
588 };
589 /*
590  * fd.io coding-style-patch-verification: ON
591  *
592  * Local Variables:
593  * eval: (c-set-style "mozilla")
594  * End:
595  */
596 #endif