VOM: Additions to allow uses to UT applications that use VOM
[vpp.git] / src / vpp-api / vom / types.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_TYPES_H__
17 #define __VOM_TYPES_H__
18
19 #include <array>
20 #include <vector>
21
22 #include <boost/asio/ip/address.hpp>
23
24 #include "vom/enum_base.hpp"
25
26 /**
27  * Convenince wrapper macro for error handling in VAPI sends
28  */
29 #define VAPI_CALL(_stmt)                                                       \
30   {                                                                            \
31     vapi_error_e _rv;                                                          \
32     do {                                                                       \
33       _rv = (_stmt);                                                           \
34     } while (VAPI_OK != _rv);                                                  \
35   }
36
37 namespace VOM {
38 /**
39  * There needs to be a strict order in which object types are read from VPP
40  *  (at boot time) and replayed to VPP (if VPP restarts). That ordering is
41  * defined in this enum types
42  */
43 enum class dependency_t
44 {
45   /**
46    * Global Configuration has no dependency
47    */
48   GLOBAL = 0,
49
50   /**
51    * interfaces are the root of the dependency graph
52    */
53   INTERFACE,
54
55   /**
56    * Tunnel or virtual interfaces next
57    */
58   TUNNEL,
59
60   /**
61    * Tables in which entries are added, e.g bridge/route-domains
62    */
63   TABLE,
64
65   /**
66    * ACLs
67    */
68   ACL,
69
70   /**
71    * Then L2/objects that bind to interfaces, BD, ACLS, etc
72    */
73   BINDING,
74
75   /**
76    * Entries in Tables
77    */
78   ENTRY,
79 };
80
81 /**
82  * Error codes that VPP will return during a HW write
83  */
84 struct rc_t : public enum_base<rc_t>
85 {
86   rc_t(const rc_t& rc) = default;
87
88   /**
89    * Destructor
90    */
91   ~rc_t();
92
93   /**
94    * The value un-set
95    */
96   const static rc_t UNSET;
97
98   /**
99    * The HW write/update action was/has not been attempted
100    */
101   const static rc_t NOOP;
102
103   /**
104    * The HW write was successfull
105    */
106   const static rc_t OK;
107
108   /**
109    * HW write is in progress. Also used for the 'want' events
110    * that never complete
111    */
112   const static rc_t INPROGRESS;
113
114   /**
115    * HW write reported invalid input
116    */
117   const static rc_t INVALID;
118
119   /**
120    * HW write timedout - VPP did not respond within a timely manner
121    */
122   const static rc_t TIMEOUT;
123
124   /**
125    * Get the rc_t from the VPP API value
126    */
127   static const rc_t& from_vpp_retval(int32_t rv);
128
129 private:
130   /**
131    * Constructor
132    */
133   rc_t(int v, const std::string s);
134 };
135
136 /**
137  * Feature Directions
138  */
139 struct direction_t : public enum_base<direction_t>
140 {
141   /**
142    * Constructor
143    */
144   direction_t(int v, const std::string s);
145
146   /**
147    * Destructor
148    */
149   ~direction_t() = default;
150
151   /**
152    * Permit Direction
153    */
154   const static direction_t INPUT;
155
156   /**
157    * Deny Direction
158    */
159   const static direction_t OUTPUT;
160 };
161
162 /**
163  * A type declaration of an interface handle in VPP
164  */
165 struct handle_t
166 {
167   /**
168    * Constructor
169    */
170   handle_t(int value);
171
172   /**
173    * Constructor
174    */
175   handle_t();
176
177   /**
178    * convert to string format for debug purposes
179    */
180   std::string to_string() const;
181
182   /**
183    * Comparison operator
184    */
185   bool operator==(const handle_t& other) const;
186
187   /**
188    * Comparison operator
189    */
190   bool operator!=(const handle_t& other) const;
191
192   /**
193    * less than operator
194    */
195   bool operator<(const handle_t& other) const;
196
197   /**
198    * A value of an interface handle_t that means the itf does not exist
199    */
200   const static handle_t INVALID;
201
202   /**
203    * get the value of the handle
204    */
205   uint32_t value() const;
206
207 private:
208   /**
209    * VPP's handle value
210    */
211   uint32_t m_value;
212 };
213
214 /**
215  * ostream print of a handle_t
216  */
217 std::ostream& operator<<(std::ostream& os, const handle_t& h);
218
219 /**
220  * Type def of a Ethernet address
221  */
222 struct mac_address_t
223 {
224   mac_address_t(uint8_t bytes[6]);
225   mac_address_t(const std::string& str);
226   mac_address_t(std::initializer_list<uint8_t> bytes);
227   /**
228    * Convert to byte array
229    */
230   void to_bytes(uint8_t* array, uint8_t len) const;
231
232   /**
233    * An all 1's MAC address
234    */
235   const static mac_address_t ONE;
236
237   /**
238    * An all 0's MAC address
239    */
240   const static mac_address_t ZERO;
241
242   /**
243    * Comparison operator
244    */
245   bool operator==(const mac_address_t& m) const;
246
247   /**
248    * less than operator
249    */
250   bool operator<(const mac_address_t& m) const;
251
252   /**
253    * String conversion
254    */
255   std::string to_string() const;
256
257   /**
258    * Underlying bytes array
259    */
260   std::array<uint8_t, 6> bytes;
261 };
262
263 /**
264  * Type def of a L2 address as read from VPP
265  */
266 struct l2_address_t
267 {
268   l2_address_t(const uint8_t bytes[8], uint8_t n_bytes);
269   l2_address_t(std::initializer_list<uint8_t> bytes);
270   l2_address_t(const mac_address_t& mac);
271
272   /**
273    * Convert to byte array
274    */
275   void to_bytes(uint8_t* array, uint8_t len) const;
276
277   /**
278    * An all 1's L2 address
279    */
280   const static l2_address_t ONE;
281
282   /**
283    * An all 0's L2 address
284    */
285   const static l2_address_t ZERO;
286
287   /**
288    * Comparison operator
289    */
290   bool operator==(const l2_address_t& m) const;
291
292   /**
293    * Comparison operator
294    */
295   bool operator!=(const l2_address_t& m) const;
296
297   /**
298    * String conversion
299    */
300   std::string to_string() const;
301
302   /**
303    * MAC address conversion
304    */
305   mac_address_t to_mac() const;
306
307   /**
308    * Underlying bytes array - filled from least to most significant
309    */
310   std::vector<uint8_t> bytes;
311 };
312
313 /**
314  * Ostream operator for a MAC address
315  */
316 std::ostream& operator<<(std::ostream& os, const mac_address_t& mac);
317
318 /**
319  * Ostream operator for a MAC address
320  */
321 std::ostream& operator<<(std::ostream& os, const l2_address_t& l2);
322 };
323
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "mozilla")
329  * End:
330  */
331
332 #endif