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