GBP V2
[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    * bond group binding is after interfaces but before
57    * anything else
58    */
59   BOND_BINDING,
60
61   /**
62    * Tunnel or virtual interfaces next
63    */
64   TUNNEL,
65
66   /**
67    * Tables in which entries are added, e.g bridge/route-domains
68    */
69   TABLE,
70
71   /**
72    * ACLs
73    */
74   ACL,
75
76   /**
77    * Then L2/objects that bind to interfaces, BD, ACLS, etc
78    */
79   BINDING,
80
81   /**
82    * Entries in Tables
83    */
84   ENTRY,
85 };
86
87 /**
88  * Error codes that VPP will return during a HW write
89  */
90 struct rc_t : public enum_base<rc_t>
91 {
92   rc_t(const rc_t& rc) = default;
93
94   /**
95    * Destructor
96    */
97   ~rc_t() = default;
98
99   /**
100    * The value un-set
101    */
102   const static rc_t UNSET;
103
104   /**
105    * The HW write/update action was/has not been attempted
106    */
107   const static rc_t NOOP;
108
109   /**
110    * The HW write was successfull
111    */
112   const static rc_t OK;
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  * Output ostream for direction_t
164  */
165 std::ostream& operator<<(std::ostream& os, const direction_t& dir);
166
167 /**
168  * Feature Ethertype
169  */
170 struct ethertype_t : public enum_base<ethertype_t>
171 {
172   /**
173    * Constructor
174    */
175   ethertype_t(int v, const std::string s);
176
177   /**
178    * Destructor
179    */
180   ~ethertype_t() = default;
181
182   /**
183    * Ethertype Arp
184    */
185   const static ethertype_t ARP;
186
187   /**
188    * Ethertype FCoE
189    */
190   const static ethertype_t FCOE;
191
192   /**
193    * Ethertype IPv4
194    */
195   const static ethertype_t IPV4;
196
197   /**
198    * Ethertype Ipv6
199    */
200   const static ethertype_t IPV6;
201
202   /**
203    * Ethertype MAC Security
204    */
205   const static ethertype_t MAC_SECURITY;
206
207   /**
208    * Ethertype MPLS unicast
209    */
210   const static ethertype_t MPLS_UNICAST;
211
212   /**
213    * Ethertype TRILL
214    */
215   const static ethertype_t TRILL;
216
217   /**
218    * Ethertype Unspecified
219    */
220   const static ethertype_t UNSPECIFIED;
221
222   /**
223    * Get the ethertype from the numeric value
224    */
225   static const ethertype_t& from_numeric_val(uint16_t numeric);
226 };
227
228 /**
229  * Output ostream for ethertype_t
230  */
231 std::ostream& operator<<(std::ostream& os, const ethertype_t& eth);
232
233 /**
234  * A type declaration of an interface handle in VPP
235  */
236 struct handle_t
237 {
238   /**
239    * Constructor
240    */
241   handle_t(int value);
242
243   /**
244    * Constructor
245    */
246   handle_t();
247
248   /**
249    * convert to string format for debug purposes
250    */
251   std::string to_string() const;
252
253   /**
254    * Comparison operator
255    */
256   bool operator==(const handle_t& other) const;
257
258   /**
259    * Comparison operator
260    */
261   bool operator!=(const handle_t& other) const;
262
263   /**
264    * less than operator
265    */
266   bool operator<(const handle_t& other) const;
267
268   /**
269    * A value of an interface handle_t that means the itf does not exist
270    */
271   const static handle_t INVALID;
272
273   /**
274    * get the value of the handle
275    */
276   uint32_t value() const;
277
278   /**
279    * reset the value of the handle to ~0
280    */
281   void reset();
282
283 private:
284   /**
285    * VPP's handle value
286    */
287   uint32_t m_value;
288 };
289
290 /**
291  * ostream print of a handle_t
292  */
293 std::ostream& operator<<(std::ostream& os, const handle_t& h);
294
295 /**
296  * Type def of a Ethernet address
297  */
298 struct mac_address_t
299 {
300   mac_address_t(uint8_t bytes[6]);
301   mac_address_t(const std::string& str);
302   mac_address_t(std::initializer_list<uint8_t> bytes);
303   /**
304    * Convert to byte array
305    */
306   void to_bytes(uint8_t* array, uint8_t len) const;
307
308   /**
309    * An all 1's MAC address
310    */
311   const static mac_address_t ONE;
312
313   /**
314    * An all 0's MAC address
315    */
316   const static mac_address_t ZERO;
317
318   /**
319    * Comparison operator
320    */
321   bool operator==(const mac_address_t& m) const;
322
323   /**
324    * less than operator
325    */
326   bool operator<(const mac_address_t& m) const;
327
328   /**
329    * String conversion
330    */
331   std::string to_string() const;
332
333   /**
334    * Underlying bytes array
335    */
336   std::array<uint8_t, 6> bytes;
337 };
338
339 /**
340  * Type def of a L2 address as read from VPP
341  */
342 struct l2_address_t
343 {
344   l2_address_t(const uint8_t bytes[8], uint8_t n_bytes);
345   l2_address_t(std::initializer_list<uint8_t> bytes);
346   l2_address_t(const mac_address_t& mac);
347
348   /**
349    * Convert to byte array
350    */
351   void to_bytes(uint8_t* array, uint8_t len) const;
352
353   /**
354    * An all 1's L2 address
355    */
356   const static l2_address_t ONE;
357
358   /**
359    * An all 0's L2 address
360    */
361   const static l2_address_t ZERO;
362
363   /**
364    * Comparison operator
365    */
366   bool operator==(const l2_address_t& m) const;
367
368   /**
369    * Comparison operator
370    */
371   bool operator!=(const l2_address_t& m) const;
372
373   /**
374    * String conversion
375    */
376   std::string to_string() const;
377
378   /**
379    * MAC address conversion
380    */
381   mac_address_t to_mac() const;
382
383   /**
384    * Underlying bytes array - filled from least to most significant
385    */
386   std::vector<uint8_t> bytes;
387 };
388
389 /**
390  * Ostream operator for a MAC address
391  */
392 std::ostream& operator<<(std::ostream& os, const mac_address_t& mac);
393
394 /**
395  * Ostream operator for a MAC address
396  */
397 std::ostream& operator<<(std::ostream& os, const l2_address_t& l2);
398 };
399
400 /*
401  * fd.io coding-style-patch-verification: ON
402  *
403  * Local Variables:
404  * eval: (c-set-style "mozilla")
405  * End:
406  */
407
408 #endif