VOM: logging, populate and stats fixes
[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 reported invalid input
110    */
111   const static rc_t INVALID;
112
113   /**
114    * HW write timedout - VPP did not respond within a timely manner
115    */
116   const static rc_t TIMEOUT;
117
118   /**
119    * Get the rc_t from the VPP API value
120    */
121   static const rc_t& from_vpp_retval(int32_t rv);
122
123 private:
124   /**
125    * Constructor
126    */
127   rc_t(int v, const std::string s);
128 };
129
130 /**
131  * Feature Directions
132  */
133 struct direction_t : public enum_base<direction_t>
134 {
135   /**
136    * Constructor
137    */
138   direction_t(int v, const std::string s);
139
140   /**
141    * Destructor
142    */
143   ~direction_t() = default;
144
145   /**
146    * Permit Direction
147    */
148   const static direction_t INPUT;
149
150   /**
151    * Deny Direction
152    */
153   const static direction_t OUTPUT;
154 };
155
156 /**
157  * A type declaration of an interface handle in VPP
158  */
159 struct handle_t
160 {
161   /**
162    * Constructor
163    */
164   handle_t(int value);
165
166   /**
167    * Constructor
168    */
169   handle_t();
170
171   /**
172    * convert to string format for debug purposes
173    */
174   std::string to_string() const;
175
176   /**
177    * Comparison operator
178    */
179   bool operator==(const handle_t& other) const;
180
181   /**
182    * Comparison operator
183    */
184   bool operator!=(const handle_t& other) const;
185
186   /**
187    * less than operator
188    */
189   bool operator<(const handle_t& other) const;
190
191   /**
192    * A value of an interface handle_t that means the itf does not exist
193    */
194   const static handle_t INVALID;
195
196   /**
197    * get the value of the handle
198    */
199   uint32_t value() const;
200
201 private:
202   /**
203    * VPP's handle value
204    */
205   uint32_t m_value;
206 };
207
208 /**
209  * ostream print of a handle_t
210  */
211 std::ostream& operator<<(std::ostream& os, const handle_t& h);
212
213 /**
214  * Type def of a Ethernet address
215  */
216 struct mac_address_t
217 {
218   mac_address_t(uint8_t bytes[6]);
219   mac_address_t(const std::string& str);
220   mac_address_t(std::initializer_list<uint8_t> bytes);
221   /**
222    * Convert to byte array
223    */
224   void to_bytes(uint8_t* array, uint8_t len) const;
225
226   /**
227    * An all 1's MAC address
228    */
229   const static mac_address_t ONE;
230
231   /**
232    * An all 0's MAC address
233    */
234   const static mac_address_t ZERO;
235
236   /**
237    * Comparison operator
238    */
239   bool operator==(const mac_address_t& m) const;
240
241   /**
242    * less than operator
243    */
244   bool operator<(const mac_address_t& m) const;
245
246   /**
247    * String conversion
248    */
249   std::string to_string() const;
250
251   /**
252    * Underlying bytes array
253    */
254   std::array<uint8_t, 6> bytes;
255 };
256
257 /**
258  * Type def of a L2 address as read from VPP
259  */
260 struct l2_address_t
261 {
262   l2_address_t(const uint8_t bytes[8], uint8_t n_bytes);
263   l2_address_t(std::initializer_list<uint8_t> bytes);
264   l2_address_t(const mac_address_t& mac);
265
266   /**
267    * Convert to byte array
268    */
269   void to_bytes(uint8_t* array, uint8_t len) const;
270
271   /**
272    * An all 1's L2 address
273    */
274   const static l2_address_t ONE;
275
276   /**
277    * An all 0's L2 address
278    */
279   const static l2_address_t ZERO;
280
281   /**
282    * Comparison operator
283    */
284   bool operator==(const l2_address_t& m) const;
285
286   /**
287    * Comparison operator
288    */
289   bool operator!=(const l2_address_t& m) const;
290
291   /**
292    * String conversion
293    */
294   std::string to_string() const;
295
296   /**
297    * MAC address conversion
298    */
299   mac_address_t to_mac() const;
300
301   /**
302    * Underlying bytes array - filled from least to most significant
303    */
304   std::vector<uint8_t> bytes;
305 };
306
307 /**
308  * Ostream operator for a MAC address
309  */
310 std::ostream& operator<<(std::ostream& os, const mac_address_t& mac);
311
312 /**
313  * Ostream operator for a MAC address
314  */
315 std::ostream& operator<<(std::ostream& os, const l2_address_t& l2);
316 };
317
318 /*
319  * fd.io coding-style-patch-verification: ON
320  *
321  * Local Variables:
322  * eval: (c-set-style "mozilla")
323  * End:
324  */
325
326 #endif