vom: Add support for redirect contracts in gbp
[vpp.git] / extras / vom / vom / gbp_rule.hpp
1 /*
2  * Copyright (c) 2018 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_GBP_RULE_H__
17 #define __VOM_GBP_RULE_H__
18
19 #include <set>
20
21 #include "vom/types.hpp"
22 #include <vapi/gbp.api.vapi.h>
23 namespace VOM {
24 class gbp_rule
25 {
26 public:
27   /**
28    * Representation of next hop
29    */
30   struct next_hop_t
31   {
32     /**
33      * Constructor for next_hop_t
34      */
35     next_hop_t(const boost::asio::ip::address& ip,
36                const mac_address_t& mac,
37                uint32_t bd_id,
38                uint32_t rd_id);
39
40     /**
41      * default destructor
42      */
43     ~next_hop_t() = default;
44
45     /**
46      * convert to string
47      */
48     std::string to_string() const;
49
50     /**
51      * less-than operator
52      */
53     bool operator<(const next_hop_t& nh) const;
54
55     /**
56      * comparison operator (for testing)
57      */
58     bool operator==(const next_hop_t& nh) const;
59
60     /**
61      * get the IP address
62      */
63     const boost::asio::ip::address& getIp(void) const;
64
65     /**
66      * get the mac address
67      */
68     const mac_address_t& getMac(void) const;
69
70     /**
71      * get the bridge domain Id
72      */
73     const uint32_t getBdId(void) const;
74
75     /**
76      * get the route domain Id
77      */
78     const uint32_t getRdId(void) const;
79
80   private:
81     /**
82      * IP address for next hop
83      */
84     const boost::asio::ip::address m_ip;
85
86     /**
87      * mac address for interface lookup
88      */
89     const mac_address_t m_mac;
90
91     /**
92      * bridge domain in which redirected endpoints exist
93      */
94     const uint32_t m_bd_id;
95
96     /**
97      * route domain in which redirected endpoints exist
98      */
99     const uint32_t m_rd_id;
100   };
101
102   /**
103    * hash mode enum
104    */
105   struct hash_mode_t : public enum_base<hash_mode_t>
106   {
107     /**
108      * Flow Hash is calculated based on SRC IP
109      * in case of load balancing
110      */
111     const static hash_mode_t SRC_IP;
112
113     /**
114      * Flow hash is calculated based on DST IP
115      */
116     const static hash_mode_t DST_IP;
117
118     /**
119      * Flow hash is calculated based on SRC IP,
120      * DST IP and Protocol. SRC IP and DST IP
121      * addresses are sorted before hash such that
122      * a same hash is generated in both directions.
123      */
124     const static hash_mode_t SYMMETRIC;
125
126     /**
127      * create the hash mode from int value
128      */
129     static const hash_mode_t& from_int(vapi_enum_gbp_hash_mode i);
130
131   private:
132     hash_mode_t(int v, const std::string s);
133   };
134
135   /**
136    * unordered set of next hops
137    */
138   typedef std::set<next_hop_t> next_hops_t;
139
140   /**
141    * Representation of set of next hops and
142    * associated hash mode profile
143    */
144   struct next_hop_set_t
145   {
146     /**
147      * Constructor for next_hop_set_t
148      */
149     next_hop_set_t(const hash_mode_t& hm, next_hops_t& nhs);
150
151     /**
152      * Destructor for next_hop_set_t
153      */
154     ~next_hop_set_t() = default;
155
156     /**
157      * convert to string
158      */
159     std::string to_string() const;
160
161     /**
162      * Comparison operator
163      */
164     bool operator==(const next_hop_set_t& nhs) const;
165
166     /**
167      * get the hash mode
168      */
169     const hash_mode_t& getHashMode(void) const;
170
171     /**
172      * get the set of next hops
173      */
174     const next_hops_t& getNextHops(void) const;
175
176   private:
177     /**
178      * hash mode for this rule
179      */
180     const hash_mode_t m_hm;
181
182     /**
183      * set of next hops
184      */
185     const next_hops_t m_nhs;
186   };
187
188   /**
189    * ACL rule action enum
190    */
191   struct action_t : public enum_base<action_t>
192   {
193     /**
194      * Permit action
195      */
196     const static action_t PERMIT;
197
198     /**
199      * Deny action
200      */
201     const static action_t DENY;
202
203     /**
204      * Redirect action
205      */
206     const static action_t REDIRECT;
207
208     /**
209      * create the action from int value
210      */
211     static const action_t& from_int(vapi_enum_gbp_rule_action i);
212
213   private:
214     action_t(int v, const std::string s);
215   };
216
217   /**
218    * Construct a new object matching the desried state
219    */
220   gbp_rule(uint32_t priority, const next_hop_set_t& nhs, const action_t& a);
221
222   /**
223    * Copy Constructor
224    */
225   gbp_rule(const gbp_rule& o) = default;
226
227   /**
228    * Destructor
229    */
230   ~gbp_rule() = default;
231
232   /**
233    * convert to string format for debug purposes
234    */
235   std::string to_string() const;
236
237   /**
238    * less-than operator
239    */
240   bool operator<(const gbp_rule& rule) const;
241
242   /**
243    * comparison operator (for testing)
244    */
245   bool operator==(const gbp_rule& rule) const;
246
247   /**
248    * Getters
249    */
250   uint32_t priority() const;
251   const next_hop_set_t& nhs() const;
252   const action_t& action() const;
253
254 private:
255   /**
256    * Priority. Used to sort the rules in a list in the order
257    * in which they are applied
258    */
259   uint32_t m_priority;
260
261   /**
262    * set of next hops along with hash mode profile
263    */
264   const next_hop_set_t m_nhs;
265
266   /**
267    * Action on match
268    */
269   const action_t m_action;
270 };
271 };
272
273 /*
274  * fd.io coding-style-patch-verification: ON
275  *
276  * Local Variables:
277  * eval: (c-set-style "mozilla")
278  * End:
279  */
280
281 #endif