gbp: Add support for allow sclass/dclass 1
[vpp.git] / src / plugins / gbp / gbp_contract.h
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 __GBP_CONTRACT_H__
17 #define __GBP_CONTRACT_H__
18
19 #include <plugins/gbp/gbp_types.h>
20
21 #define foreach_gbp_policy_error                           \
22   _(ALLOW_NO_SCLASS,    "allow-no-sclass")                 \
23   _(ALLOW_INTRA,        "allow-intra-sclass")              \
24   _(ALLOW_A_BIT,        "allow-a-bit-set")                 \
25   _(ALLOW_SCLASS_1,     "allow-sclass-1")                  \
26   _(ALLOW_CONTRACT,     "allow-contract")                  \
27   _(DROP_CONTRACT,      "drop-contract")                   \
28   _(DROP_ETHER_TYPE,    "drop-ether-type")                 \
29   _(DROP_NO_CONTRACT,   "drop-no-contract")                \
30   _(DROP_NO_DCLASS,     "drop-no-dclass")
31
32 /**
33  * The key for an Contract
34  */
35 typedef struct gbp_contract_key_t_
36 {
37   union
38   {
39     struct
40     {
41       /**
42        * source and destination EPGs for which the ACL applies
43        */
44       sclass_t gck_src;
45       sclass_t gck_dst;
46     };
47     u32 as_u32;
48   };
49 } gbp_contract_key_t;
50
51 typedef struct gbp_next_hop_t_
52 {
53   fib_node_t gnh_node;
54   ip46_address_t gnh_ip;
55   mac_address_t gnh_mac;
56   index_t gnh_gu;
57   index_t gnh_bd;
58   index_t gnh_rd;
59   u32 gnh_ge;
60   u32 gnh_sibling;
61   index_t gnh_ai[FIB_PROTOCOL_IP_MAX];
62 } gbp_next_hop_t;
63
64 #define foreach_gbp_hash_mode   \
65   _(SRC_IP, "src-ip")           \
66   _(DST_IP, "dst-ip")           \
67   _(SYMMETRIC, "symmetric")
68
69 typedef enum gbp_hash_mode_t_
70 {
71 #define _(v,s) GBP_HASH_MODE_##v,
72   foreach_gbp_hash_mode
73 #undef _
74 } gbp_hash_mode_t;
75
76 #define foreach_gbp_rule_action   \
77   _(PERMIT,   "permit")           \
78   _(DENY,     "deny")             \
79   _(REDIRECT, "redirect")
80
81 typedef enum gbp_rule_action_t_
82 {
83 #define _(v,s) GBP_RULE_##v,
84   foreach_gbp_rule_action
85 #undef _
86 } gbp_rule_action_t;
87
88 #define foreach_gbp_policy_node   \
89   _(L2, "L2")                     \
90   _(IP4, "ip4")                   \
91   _(IP6, "ip6")
92
93 typedef enum gbp_policy_node_t_
94 {
95 #define _(v,s) GBP_POLICY_NODE_##v,
96   foreach_gbp_policy_node
97 #undef _
98 } gbp_policy_node_t;
99 #define GBP_POLICY_N_NODES (GBP_POLICY_NODE_IP6+1)
100
101 #define FOR_EACH_GBP_POLICY_NODE(pnode)         \
102   for (pnode = GBP_POLICY_NODE_L2; pnode < GBP_POLICY_N_NODES; pnode++)
103
104 typedef struct gbp_rule_t_
105 {
106   gbp_rule_action_t gu_action;
107   gbp_hash_mode_t gu_hash_mode;
108   index_t *gu_nhs;
109
110   /**
111    * DPO of the load-balance object used to redirect
112    */
113   dpo_id_t gu_dpo[GBP_POLICY_N_NODES][FIB_PROTOCOL_IP_MAX];
114 } gbp_rule_t;
115
116 /**
117  * A Group Based Policy Contract.
118  *  Determines the ACL that applies to traffic pass between two endpoint groups
119  */
120 typedef struct gbp_contract_t_
121 {
122   /**
123    * source and destination EPGs
124    */
125   gbp_contract_key_t gc_key;
126
127   u32 gc_acl_index;
128   u32 gc_lc_index;
129
130   /**
131    * The ACL to apply for packets from the source to the destination EPG
132    */
133   index_t *gc_rules;
134
135   /**
136    * An ethertype whitelist
137    */
138   u16 *gc_allowed_ethertypes;
139 } gbp_contract_t;
140
141 /**
142  * EPG src,dst pair to ACL mapping table, aka contract DB
143  */
144 typedef struct gbp_contract_db_t_
145 {
146   /**
147    * We can form a u64 key from the pair, so use a simple hash table
148    */
149   uword *gc_hash;
150 } gbp_contract_db_t;
151
152 extern int gbp_contract_update (sclass_t sclass,
153                                 sclass_t dclass,
154                                 u32 acl_index,
155                                 index_t * rules,
156                                 u16 * allowed_ethertypes, u32 * stats_index);
157 extern int gbp_contract_delete (sclass_t sclass, sclass_t dclass);
158
159 extern index_t gbp_rule_alloc (gbp_rule_action_t action,
160                                gbp_hash_mode_t hash_mode, index_t * nhs);
161 extern index_t gbp_next_hop_alloc (const ip46_address_t * ip,
162                                    index_t grd,
163                                    const mac_address_t * mac, index_t gbd);
164
165 typedef int (*gbp_contract_cb_t) (gbp_contract_t * gbpe, void *ctx);
166 extern void gbp_contract_walk (gbp_contract_cb_t bgpe, void *ctx);
167
168 extern u8 *format_gbp_contract (u8 * s, va_list * args);
169
170 /**
171  * DP functions and databases
172  */
173 extern gbp_contract_db_t gbp_contract_db;
174
175 always_inline index_t
176 gbp_contract_find (gbp_contract_key_t * key)
177 {
178   uword *p;
179
180   p = hash_get (gbp_contract_db.gc_hash, key->as_u32);
181
182   if (NULL != p)
183     return (p[0]);
184
185   return (INDEX_INVALID);
186 }
187
188 extern gbp_contract_t *gbp_contract_pool;
189
190 always_inline gbp_contract_t *
191 gbp_contract_get (index_t gci)
192 {
193   return (pool_elt_at_index (gbp_contract_pool, gci));
194 }
195
196 extern gbp_rule_t *gbp_rule_pool;
197
198 always_inline gbp_rule_t *
199 gbp_rule_get (index_t gui)
200 {
201   return (pool_elt_at_index (gbp_rule_pool, gui));
202 }
203
204 extern vlib_combined_counter_main_t gbp_contract_permit_counters;
205 extern vlib_combined_counter_main_t gbp_contract_drop_counters;
206
207 #endif
208
209 /*
210  * fd.io coding-style-patch-verification: ON
211  *
212  * Local Variables:
213  * eval: (c-set-style "gnu")
214  * End:
215  */