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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __GBP_CONTRACT_H__
17 #define __GBP_CONTRACT_H__
19 #include <plugins/gbp/gbp.h>
20 #include <plugins/gbp/gbp_types.h>
22 #define foreach_gbp_contract_error \
23 _(ALLOW_NO_SCLASS, "allow-no-sclass") \
24 _(ALLOW_INTRA, "allow-intra-sclass") \
25 _(ALLOW_A_BIT, "allow-a-bit-set") \
26 _(ALLOW_SCLASS_1, "allow-sclass-1") \
27 _(ALLOW_CONTRACT, "allow-contract") \
28 _(DROP_CONTRACT, "drop-contract") \
29 _(DROP_ETHER_TYPE, "drop-ether-type") \
30 _(DROP_NO_CONTRACT, "drop-no-contract") \
31 _(DROP_NO_DCLASS, "drop-no-dclass")
35 #define _(sym,str) GBP_CONTRACT_ERROR_##sym,
36 foreach_gbp_contract_error
39 #define GBP_CONTRACT_N_ERROR GBP_CONTRACT_N_ERROR
40 } gbp_contract_error_t;
42 extern char *gbp_contract_error_strings[GBP_CONTRACT_N_ERROR];
45 * The key for an Contract
47 typedef struct gbp_contract_key_t_
53 gbp_scope_t gck_scope;
55 * source and destination EPGs for which the ACL applies
64 typedef struct gbp_next_hop_t_
67 ip46_address_t gnh_ip;
68 mac_address_t gnh_mac;
74 index_t gnh_ai[FIB_PROTOCOL_IP_MAX];
77 #define foreach_gbp_hash_mode \
80 _(SYMMETRIC, "symmetric")
82 typedef enum gbp_hash_mode_t_
84 #define _(v,s) GBP_HASH_MODE_##v,
89 #define foreach_gbp_rule_action \
92 _(REDIRECT, "redirect")
94 typedef enum gbp_rule_action_t_
96 #define _(v,s) GBP_RULE_##v,
97 foreach_gbp_rule_action
101 #define foreach_gbp_policy_node \
106 typedef enum gbp_policy_node_t_
108 #define _(v,s) GBP_POLICY_NODE_##v,
109 foreach_gbp_policy_node
112 #define GBP_POLICY_N_NODES (GBP_POLICY_NODE_IP6+1)
114 #define FOR_EACH_GBP_POLICY_NODE(pnode) \
115 for (pnode = GBP_POLICY_NODE_L2; pnode < GBP_POLICY_N_NODES; pnode++)
117 typedef struct gbp_rule_t_
119 gbp_rule_action_t gu_action;
120 gbp_hash_mode_t gu_hash_mode;
124 * DPO of the load-balance object used to redirect
126 dpo_id_t gu_dpo[GBP_POLICY_N_NODES][FIB_PROTOCOL_IP_MAX];
130 * A Group Based Policy Contract.
131 * Determines the ACL that applies to traffic pass between two endpoint groups
133 typedef struct gbp_contract_t_
136 * source and destination EPGs
138 gbp_contract_key_t gc_key;
144 * The ACL to apply for packets from the source to the destination EPG
149 * An ethertype whitelist
151 u16 *gc_allowed_ethertypes;
155 * EPG src,dst pair to ACL mapping table, aka contract DB
157 typedef struct gbp_contract_db_t_
160 * We can form a u64 key from the pair, so use a simple hash table
165 extern int gbp_contract_update (gbp_scope_t scope,
170 u16 * allowed_ethertypes, u32 * stats_index);
171 extern int gbp_contract_delete (gbp_scope_t scope, sclass_t sclass,
174 extern index_t gbp_rule_alloc (gbp_rule_action_t action,
175 gbp_hash_mode_t hash_mode, index_t * nhs);
176 extern index_t gbp_next_hop_alloc (const ip46_address_t * ip,
178 const mac_address_t * mac, index_t gbd);
180 typedef int (*gbp_contract_cb_t) (gbp_contract_t * gbpe, void *ctx);
181 extern void gbp_contract_walk (gbp_contract_cb_t bgpe, void *ctx);
183 extern u8 *format_gbp_contract (u8 * s, va_list * args);
186 * DP functions and databases
188 extern gbp_contract_db_t gbp_contract_db;
190 always_inline index_t
191 gbp_contract_find (gbp_contract_key_t * key)
195 p = hash_get (gbp_contract_db.gc_hash, key->as_u64);
200 return (INDEX_INVALID);
203 extern gbp_contract_t *gbp_contract_pool;
205 always_inline gbp_contract_t *
206 gbp_contract_get (index_t gci)
208 return (pool_elt_at_index (gbp_contract_pool, gci));
211 extern gbp_rule_t *gbp_rule_pool;
213 always_inline gbp_rule_t *
214 gbp_rule_get (index_t gui)
216 return (pool_elt_at_index (gbp_rule_pool, gui));
219 extern vlib_combined_counter_main_t gbp_contract_permit_counters;
220 extern vlib_combined_counter_main_t gbp_contract_drop_counters;
224 GBP_CONTRACT_APPLY_L2,
225 GBP_CONTRACT_APPLY_IP4,
226 GBP_CONTRACT_APPLY_IP6,
227 } gbp_contract_apply_type_t;
229 static_always_inline gbp_rule_action_t
230 gbp_contract_apply (vlib_main_t * vm, gbp_main_t * gm,
231 gbp_contract_key_t * key, vlib_buffer_t * b,
232 gbp_rule_t ** rule, u32 * intra, u32 * sclass1,
233 gbp_contract_error_t * err,
234 gbp_contract_apply_type_t type)
236 fa_5tuple_opaque_t fa_5tuple;
237 const gbp_contract_t *contract;
238 index_t contract_index;
239 u32 acl_pos, acl_match, rule_match, trace_bitmap;
245 if (key->gck_src == key->gck_dst)
247 /* intra-epg allowed */
249 *err = GBP_CONTRACT_ERROR_ALLOW_INTRA;
250 return GBP_RULE_PERMIT;
253 if (1 == key->gck_src || 1 == key->gck_dst)
255 /* sclass 1 allowed */
257 *err = GBP_CONTRACT_ERROR_ALLOW_SCLASS_1;
258 return GBP_RULE_PERMIT;
261 /* look for contract */
262 contract_index = gbp_contract_find (key);
263 if (INDEX_INVALID == contract_index)
265 *err = GBP_CONTRACT_ERROR_DROP_NO_CONTRACT;
266 return GBP_RULE_DENY;
269 contract = gbp_contract_get (contract_index);
271 *err = GBP_CONTRACT_ERROR_DROP_CONTRACT;
275 case GBP_CONTRACT_APPLY_IP4:
278 case GBP_CONTRACT_APPLY_IP6:
281 case GBP_CONTRACT_APPLY_L2:
283 /* check ethertype */
285 ((u16 *) (vlib_buffer_get_current (b) +
286 vnet_buffer (b)->l2.l2_len))[-1];
288 if (~0 == vec_search (contract->gc_allowed_ethertypes, etype))
290 *err = GBP_CONTRACT_ERROR_DROP_ETHER_TYPE;
294 switch (clib_net_to_host_u16 (etype))
296 case ETHERNET_TYPE_IP4:
299 case ETHERNET_TYPE_IP6:
311 acl_plugin_fill_5tuple_inline (gm->acl_plugin.p_acl_main,
312 contract->gc_lc_index, b, ip6,
313 GBP_CONTRACT_APPLY_L2 != type /* input */ ,
314 GBP_CONTRACT_APPLY_L2 == type /* l2_path */ ,
316 acl_plugin_match_5tuple_inline (gm->acl_plugin.p_acl_main,
317 contract->gc_lc_index, &fa_5tuple, ip6,
318 &action, &acl_pos, &acl_match, &rule_match,
323 *rule = gbp_rule_get (contract->gc_rules[rule_match]);
324 switch ((*rule)->gu_action)
326 case GBP_RULE_PERMIT:
327 case GBP_RULE_REDIRECT:
328 *err = GBP_CONTRACT_ERROR_ALLOW_CONTRACT;
329 vlib_increment_combined_counter (&gbp_contract_permit_counters,
330 vm->thread_index, contract_index, 1,
331 vlib_buffer_length_in_chain (vm, b));
332 return (*rule)->gu_action;
338 vlib_increment_combined_counter (&gbp_contract_drop_counters,
339 vm->thread_index, contract_index, 1,
340 vlib_buffer_length_in_chain (vm, b));
341 return GBP_RULE_DENY;
344 #endif /* __GBP_CONTRACT_H__ */
346 * fd.io coding-style-patch-verification: ON
349 * eval: (c-set-style "gnu")