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