Introduce a mac_address_t on the API and in VPP
[vpp.git] / extras / vom / 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    * virtual interfaces - those that depend on some real interface
57    */
58   VIRTUAL_INTERFACE,
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   /**
87    * Destructor
88    */
89   ~rc_t() = default;
90
91   /**
92    * The value un-set
93    */
94   const static rc_t UNSET;
95
96   /**
97    * The HW write/update action was/has not been attempted
98    */
99   const static rc_t NOOP;
100
101   /**
102    * The HW write was successfull
103    */
104   const static rc_t OK;
105
106   /**
107    * HW write reported invalid input
108    */
109   const static rc_t INVALID;
110
111   /**
112    * HW write timedout - VPP did not respond within a timely manner
113    */
114   const static rc_t TIMEOUT;
115
116   /**
117    * Get the rc_t from the VPP API value
118    */
119   static const rc_t& from_vpp_retval(int32_t rv);
120
121 private:
122   /**
123    * Constructor
124    */
125   rc_t(int v, const std::string s);
126 };
127
128 /**
129  * Feature Directions
130  */
131 struct direction_t : public enum_base<direction_t>
132 {
133   /**
134    * Constructor
135    */
136   direction_t(int v, const std::string s);
137
138   /**
139    * Destructor
140    */
141   ~direction_t() = default;
142
143   /**
144    * Permit Direction
145    */
146   const static direction_t INPUT;
147
148   /**
149    * Deny Direction
150    */
151   const static direction_t OUTPUT;
152 };
153
154 /**
155  * Output ostream for direction_t
156  */
157 std::ostream& operator<<(std::ostream& os, const direction_t& dir);
158
159 /**
160  * Feature Ethertype
161  */
162 struct ethertype_t : public enum_base<ethertype_t>
163 {
164   /**
165    * Constructor
166    */
167   ethertype_t(int v, const std::string s);
168
169   /**
170    * Destructor
171    */
172   ~ethertype_t() = default;
173
174   /**
175    * Ethertype Arp
176    */
177   const static ethertype_t ARP;
178
179   /**
180    * Ethertype FCoE
181    */
182   const static ethertype_t FCOE;
183
184   /**
185    * Ethertype IPv4
186    */
187   const static ethertype_t IPV4;
188
189   /**
190    * Ethertype Ipv6
191    */
192   const static ethertype_t IPV6;
193
194   /**
195    * Ethertype MAC Security
196    */
197   const static ethertype_t MAC_SECURITY;
198
199   /**
200    * Ethertype MPLS unicast
201    */
202   const static ethertype_t MPLS_UNICAST;
203
204   /**
205    * Ethertype TRILL
206    */
207   const static ethertype_t TRILL;
208
209   /**
210    * Ethertype Unspecified
211    */
212   const static ethertype_t UNSPECIFIED;
213
214   /**
215    * Get the ethertype from the numeric value
216    */
217   static const ethertype_t& from_numeric_val(uint16_t numeric);
218 };
219
220 /**
221  * Output ostream for ethertype_t
222  */
223 std::ostream& operator<<(std::ostream& os, const ethertype_t& eth);
224
225 /**
226  * A type declaration of an interface handle in VPP
227  */
228 struct handle_t
229 {
230   /**
231    * Constructor
232    */
233   handle_t(int value);
234
235   /**
236    * Constructor
237    */
238   handle_t();
239
240   /**
241    * convert to string format for debug purposes
242    */
243   std::string to_string() const;
244
245   /**
246    * Comparison operator
247    */
248   bool operator==(const handle_t& other) const;
249
250   /**
251    * Comparison operator
252    */
253   bool operator!=(const handle_t& other) const;
254
255   /**
256    * less than operator
257    */
258   bool operator<(const handle_t& other) const;
259
260   /**
261    * A value of an interface handle_t that means the itf does not exist
262    */
263   const static handle_t INVALID;
264
265   /**
266    * get the value of the handle
267    */
268   uint32_t value() const;
269
270   /**
271    * reset the value of the handle to ~0
272    */
273   void reset();
274
275 private:
276   /**
277    * VPP's handle value
278    */
279   uint32_t m_value;
280 };
281
282 /**
283  * ostream print of a handle_t
284  */
285 std::ostream& operator<<(std::ostream& os, const handle_t& h);
286
287 /**
288  * Type def of a Ethernet address
289  */
290 struct mac_address_t
291 {
292   mac_address_t(const uint8_t bytes[6]);
293   mac_address_t(const std::string& str);
294   mac_address_t(std::initializer_list<uint8_t> bytes);
295   /**
296    * Convert to byte array
297    */
298   void to_bytes(uint8_t* array, uint8_t len) const;
299
300   /**
301    * An all 1's MAC address
302    */
303   const static mac_address_t ONE;
304
305   /**
306    * An all 0's MAC address
307    */
308   const static mac_address_t ZERO;
309
310   /**
311    * Comparison operator
312    */
313   bool operator==(const mac_address_t& m) const;
314
315   /**
316    * less than operator
317    */
318   bool operator<(const mac_address_t& m) const;
319
320   /**
321    * String conversion
322    */
323   std::string to_string() const;
324
325   /**
326    * Underlying bytes array
327    */
328   std::array<uint8_t, 6> bytes;
329 };
330
331 /**
332  * Type def of a L2 address as read from VPP
333  */
334 struct l2_address_t
335 {
336   l2_address_t(const uint8_t bytes[8], uint8_t n_bytes);
337   l2_address_t(std::initializer_list<uint8_t> bytes);
338   l2_address_t(const mac_address_t& mac);
339
340   /**
341    * Convert to byte array
342    */
343   void to_bytes(uint8_t* array, uint8_t len) const;
344
345   /**
346    * An all 1's L2 address
347    */
348   const static l2_address_t ONE;
349
350   /**
351    * An all 0's L2 address
352    */
353   const static l2_address_t ZERO;
354
355   /**
356    * Comparison operator
357    */
358   bool operator==(const l2_address_t& m) const;
359
360   /**
361    * Comparison operator
362    */
363   bool operator!=(const l2_address_t& m) const;
364
365   /**
366    * String conversion
367    */
368   std::string to_string() const;
369
370   /**
371    * MAC address conversion
372    */
373   mac_address_t to_mac() const;
374
375   /**
376    * Underlying bytes array - filled from least to most significant
377    */
378   std::vector<uint8_t> bytes;
379 };
380
381 /**
382  * Ostream operator for a MAC address
383  */
384 std::ostream& operator<<(std::ostream& os, const mac_address_t& mac);
385
386 /**
387  * Ostream operator for a MAC address
388  */
389 std::ostream& operator<<(std::ostream& os, const l2_address_t& l2);
390 };
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "mozilla")
397  * End:
398  */
399
400 #endif