2 * gbp.h : Group Based Policy
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <plugins/gbp/gbp.h>
19 #include <plugins/gbp/gbp_bridge_domain.h>
20 #include <plugins/gbp/gbp_route_domain.h>
21 #include <plugins/gbp/gbp_policy_dpo.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/dpo/drop_dpo.h>
27 * Single contract DB instance
29 gbp_contract_db_t gbp_contract_db;
31 gbp_contract_t *gbp_contract_pool;
33 vlib_log_class_t gc_logger;
35 fib_node_type_t gbp_next_hop_fib_type;
37 gbp_rule_t *gbp_rule_pool;
38 gbp_next_hop_t *gbp_next_hop_pool;
40 #define GBP_CONTRACT_DBG(...) \
41 vlib_log_notice (gc_logger, __VA_ARGS__);
44 gbp_rule_alloc (gbp_rule_action_t action,
45 gbp_hash_mode_t hash_mode, index_t * nhs)
49 pool_get_zero (gbp_rule_pool, gu);
52 gu->gu_action = action;
54 return (gu - gbp_rule_pool);
58 gbp_next_hop_alloc (const ip46_address_t * ip,
59 index_t grd, const mac_address_t * mac, index_t gbd)
61 fib_protocol_t fproto;
64 pool_get_zero (gbp_next_hop_pool, gnh);
66 fib_node_init (&gnh->gnh_node, gbp_next_hop_fib_type);
68 ip46_address_copy (&gnh->gnh_ip, ip);
69 mac_address_copy (&gnh->gnh_mac, mac);
74 FOR_EACH_FIB_IP_PROTOCOL (fproto) gnh->gnh_ai[fproto] = INDEX_INVALID;
76 return (gnh - gbp_next_hop_pool);
79 static inline gbp_next_hop_t *
80 gbp_next_hop_get (index_t gui)
82 return (pool_elt_at_index (gbp_next_hop_pool, gui));
86 gbp_contract_rules_free (index_t * rules)
90 vec_foreach (gui, rules)
92 gbp_policy_node_t pnode;
93 fib_protocol_t fproto;
97 gu = gbp_rule_get (*gui);
99 FOR_EACH_GBP_POLICY_NODE (pnode)
101 FOR_EACH_FIB_IP_PROTOCOL (fproto)
103 dpo_reset (&gu->gu_dpo[pnode][fproto]);
104 dpo_reset (&gu->gu_dpo[pnode][fproto]);
108 vec_foreach (gnhi, gu->gu_nhs)
110 fib_protocol_t fproto;
112 gnh = gbp_next_hop_get (*gnhi);
113 gbp_bridge_domain_unlock (gnh->gnh_bd);
114 gbp_route_domain_unlock (gnh->gnh_rd);
115 gbp_endpoint_child_remove (gnh->gnh_ge, gnh->gnh_sibling);
116 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_RR, gnh->gnh_ge);
118 FOR_EACH_FIB_IP_PROTOCOL (fproto)
120 adj_unlock (gnh->gnh_ai[fproto]);
128 format_gbp_next_hop (u8 * s, va_list * args)
130 index_t gnhi = va_arg (*args, index_t);
133 gnh = gbp_next_hop_get (gnhi);
135 s = format (s, "%U, %U, %U EP:%d",
136 format_mac_address_t, &gnh->gnh_mac,
137 format_gbp_bridge_domain, gnh->gnh_bd,
138 format_ip46_address, &gnh->gnh_ip, IP46_TYPE_ANY, gnh->gnh_ge);
144 format_gbp_rule_action (u8 * s, va_list * args)
146 gbp_rule_action_t action = va_arg (*args, gbp_rule_action_t);
150 #define _(v,a) case GBP_RULE_##v: return (format (s, "%s", a));
151 foreach_gbp_rule_action
155 return (format (s, "unknown"));
159 format_gbp_hash_mode (u8 * s, va_list * args)
161 gbp_hash_mode_t action = va_arg (*args, gbp_hash_mode_t);
165 #define _(v,a) case GBP_HASH_MODE_##v: return (format (s, "%s", a));
166 foreach_gbp_hash_mode
170 return (format (s, "unknown"));
174 format_gbp_policy_node (u8 * s, va_list * args)
176 gbp_policy_node_t action = va_arg (*args, gbp_policy_node_t);
180 #define _(v,a) case GBP_POLICY_NODE_##v: return (format (s, "%s", a));
181 foreach_gbp_policy_node
185 return (format (s, "unknown"));
189 format_gbp_rule (u8 * s, va_list * args)
191 index_t gui = va_arg (*args, index_t);
192 gbp_policy_node_t pnode;
193 fib_protocol_t fproto;
197 gu = gbp_rule_get (gui);
198 s = format (s, "%U", format_gbp_rule_action, gu->gu_action);
200 switch (gu->gu_action)
202 case GBP_RULE_PERMIT:
205 case GBP_RULE_REDIRECT:
206 s = format (s, ", %U", format_gbp_hash_mode, gu->gu_hash_mode);
210 vec_foreach (gnhi, gu->gu_nhs)
212 s = format (s, "\n [%U]", format_gbp_next_hop, *gnhi);
215 FOR_EACH_GBP_POLICY_NODE (pnode)
217 s = format (s, "\n policy-%U", format_gbp_policy_node, pnode);
219 FOR_EACH_FIB_IP_PROTOCOL (fproto)
221 if (dpo_id_is_valid (&gu->gu_dpo[pnode][fproto]))
224 format (s, "\n %U", format_dpo_id,
225 &gu->gu_dpo[pnode][fproto], 8);
234 gbp_contract_mk_adj (gbp_next_hop_t * gnh, fib_protocol_t fproto)
236 ethernet_header_t *eth;
241 old_ai = gnh->gnh_ai[fproto];
243 vec_validate (rewrite, sizeof (*eth) - 1);
244 eth = (ethernet_header_t *) rewrite;
246 GBP_CONTRACT_DBG ("...mk-adj: %U", format_gbp_next_hop,
247 gnh - gbp_next_hop_pool);
249 ge = gbp_endpoint_get (gnh->gnh_ge);
251 eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
252 ETHERNET_TYPE_IP4 : ETHERNET_TYPE_IP6));
253 mac_address_to_bytes (gbp_route_domain_get_local_mac (), eth->src_address);
254 mac_address_to_bytes (&gnh->gnh_mac, eth->dst_address);
256 gnh->gnh_ai[fproto] =
257 adj_nbr_add_or_lock_w_rewrite (fproto,
258 fib_proto_to_link (fproto),
259 &gnh->gnh_ip, ge->ge_fwd.gef_itf, rewrite);
265 gbp_contract_mk_lb (index_t gui, fib_protocol_t fproto)
267 load_balance_path_t *paths = NULL;
268 gbp_policy_node_t pnode;
274 u32 policy_nodes[] = {
275 [GBP_POLICY_NODE_L2] = gbp_policy_port_node.index,
276 [GBP_POLICY_NODE_IP4] = ip4_gbp_policy_dpo_node.index,
277 [GBP_POLICY_NODE_IP6] = ip6_gbp_policy_dpo_node.index,
280 GBP_CONTRACT_DBG ("..mk-lb: %U", format_gbp_rule, gui);
282 gu = gbp_rule_get (gui);
283 dproto = fib_proto_to_dpo (fproto);
285 if (GBP_RULE_REDIRECT != gu->gu_action)
288 vec_foreach_index (ii, gu->gu_nhs)
290 gnh = gbp_next_hop_get (gu->gu_nhs[ii]);
292 gbp_contract_mk_adj (gnh, FIB_PROTOCOL_IP4);
293 gbp_contract_mk_adj (gnh, FIB_PROTOCOL_IP6);
296 FOR_EACH_GBP_POLICY_NODE (pnode)
298 vec_validate (paths, vec_len (gu->gu_nhs) - 1);
300 vec_foreach_index (ii, gu->gu_nhs)
302 gnh = gbp_next_hop_get (gu->gu_nhs[ii]);
304 paths[ii].path_index = FIB_NODE_INDEX_INVALID;
305 paths[ii].path_weight = 1;
306 dpo_set (&paths[ii].path_dpo, DPO_ADJACENCY,
307 dproto, gnh->gnh_ai[fproto]);
310 // FIXME get algo and sticky bit from contract LB algo
311 if (!dpo_id_is_valid (&gu->gu_dpo[pnode][fproto]))
313 dpo_id_t dpo = DPO_INVALID;
315 dpo_set (&dpo, DPO_LOAD_BALANCE, dproto,
316 load_balance_create (vec_len (paths),
317 dproto, IP_FLOW_HASH_DEFAULT));
318 dpo_stack_from_node (policy_nodes[pnode],
319 &gu->gu_dpo[pnode][fproto], &dpo);
323 load_balance_multipath_update (&gu->gu_dpo[pnode][fproto],
324 paths, LOAD_BALANCE_FLAG_NONE);
330 gbp_contract_mk_one_lb (index_t gui)
332 gbp_contract_mk_lb (gui, FIB_PROTOCOL_IP4);
333 gbp_contract_mk_lb (gui, FIB_PROTOCOL_IP6);
337 gbp_contract_next_hop_resolve (index_t gui, index_t gnhi)
339 gbp_bridge_domain_t *gbd;
345 gnh = gbp_next_hop_get (gnhi);
346 gbd = gbp_bridge_domain_get (gnh->gnh_bd);
349 vec_add1 (ips, gnh->gnh_ip);
352 * source the endpoint this contract needs to forward via.
353 * give ofrwarding details via the spine proxy. if this EP is known
354 * to us, then since we source here with a low priority, the learned
355 * info will take precedenc.
357 rv = gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_RR,
358 gbd->gb_uu_fwd_sw_if_index,
361 gnh->gnh_bd, gnh->gnh_rd, EPG_INVALID,
362 GBP_ENDPOINT_FLAG_NONE, NULL, NULL,
367 gnh->gnh_sibling = gbp_endpoint_child_add (gnh->gnh_ge,
368 gbp_next_hop_fib_type, gnhi);
371 GBP_CONTRACT_DBG ("..resolve: %d: %d: %U", gui, gnhi, format_gbp_next_hop,
379 gbp_contract_rule_resolve (index_t gui)
384 gu = gbp_rule_get (gui);
386 GBP_CONTRACT_DBG ("..resolve: %U", format_gbp_rule, gui);
388 vec_foreach (gnhi, gu->gu_nhs)
390 gbp_contract_next_hop_resolve (gui, *gnhi);
395 gbp_contract_resolve (index_t * guis)
399 vec_foreach (gui, guis)
401 gbp_contract_rule_resolve (*gui);
406 gbp_contract_mk_lbs (index_t * guis)
410 vec_foreach (gui, guis)
412 gbp_contract_mk_one_lb (*gui);
417 gbp_contract_update (epg_id_t src_epg,
418 epg_id_t dst_epg, u32 acl_index, index_t * rules)
420 gbp_main_t *gm = &gbp_main;
426 gbp_contract_key_t key = {
431 if (~0 == gm->gbp_acl_user_id)
433 acl_plugin_exports_init (&gm->acl_plugin);
434 gm->gbp_acl_user_id =
435 gm->acl_plugin.register_user_module ("GBP ACL", "src-epg", "dst-epg");
438 p = hash_get (gbp_contract_db.gc_hash, key.as_u32);
442 gc = gbp_contract_get (gci);
443 gbp_contract_rules_free (gc->gc_rules);
444 gbp_main.acl_plugin.put_lookup_context_index (gc->gc_lc_index);
449 pool_get_zero (gbp_contract_pool, gc);
451 gci = gc - gbp_contract_pool;
452 hash_set (gbp_contract_db.gc_hash, key.as_u32, gci);
455 GBP_CONTRACT_DBG ("update: %U", format_gbp_contract, gci);
457 gc->gc_rules = rules;
458 gbp_contract_resolve (gc->gc_rules);
459 gbp_contract_mk_lbs (gc->gc_rules);
461 gc->gc_acl_index = acl_index;
463 gm->acl_plugin.get_lookup_context_index (gm->gbp_acl_user_id,
466 vec_add1 (acl_vec, gc->gc_acl_index);
467 gm->acl_plugin.set_acl_vec_for_context (gc->gc_lc_index, acl_vec);
474 gbp_contract_delete (epg_id_t src_epg, epg_id_t dst_epg)
476 gbp_contract_key_t key = {
483 p = hash_get (gbp_contract_db.gc_hash, key.as_u32);
486 gc = gbp_contract_get (p[0]);
488 gbp_contract_rules_free (gc->gc_rules);
489 gbp_main.acl_plugin.put_lookup_context_index (gc->gc_lc_index);
491 hash_unset (gbp_contract_db.gc_hash, key.as_u32);
492 pool_put (gbp_contract_pool, gc);
497 return (VNET_API_ERROR_NO_SUCH_ENTRY);
501 gbp_contract_walk (gbp_contract_cb_t cb, void *ctx)
506 pool_foreach(gc, gbp_contract_pool,
514 static clib_error_t *
515 gbp_contract_cli (vlib_main_t * vm,
516 unformat_input_t * input, vlib_cli_command_t * cmd)
518 epg_id_t src_epg_id = EPG_INVALID, dst_epg_id = EPG_INVALID;
522 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
524 if (unformat (input, "add"))
526 else if (unformat (input, "del"))
528 else if (unformat (input, "src-epg %d", &src_epg_id))
530 else if (unformat (input, "dst-epg %d", &dst_epg_id))
532 else if (unformat (input, "acl-index %d", &acl_index))
538 if (EPG_INVALID == src_epg_id)
539 return clib_error_return (0, "Source EPG-ID must be specified");
540 if (EPG_INVALID == dst_epg_id)
541 return clib_error_return (0, "Destination EPG-ID must be specified");
545 gbp_contract_update (src_epg_id, dst_epg_id, acl_index, NULL);
549 gbp_contract_delete (src_epg_id, dst_epg_id);
556 * Configure a GBP Contract
559 * @cliexstart{set gbp contract [del] src-epg <ID> dst-epg <ID> acl-index <ACL>}
563 VLIB_CLI_COMMAND (gbp_contract_cli_node, static) =
565 .path = "gbp contract",
567 "gbp contract [del] src-epg <ID> dst-epg <ID> acl-index <ACL>",
568 .function = gbp_contract_cli,
573 format_gbp_contract_key (u8 * s, va_list * args)
575 gbp_contract_key_t *gck = va_arg (*args, gbp_contract_key_t *);
577 s = format (s, "{%d,%d}", gck->gck_src, gck->gck_dst);
583 format_gbp_contract (u8 * s, va_list * args)
585 index_t gci = va_arg (*args, index_t);
589 gc = gbp_contract_get (gci);
591 s = format (s, "%U: acl-index:%d",
592 format_gbp_contract_key, &gc->gc_key, gc->gc_acl_index);
594 vec_foreach (gui, gc->gc_rules)
596 s = format (s, "\n %d: %U", *gui, format_gbp_rule, *gui);
602 static clib_error_t *
603 gbp_contract_show (vlib_main_t * vm,
604 unformat_input_t * input, vlib_cli_command_t * cmd)
608 vlib_cli_output (vm, "Contracts:");
611 pool_foreach_index (gci, gbp_contract_pool,
613 vlib_cli_output (vm, " [%d] %U", gci, format_gbp_contract, gci);
621 * Show Group Based Policy Contracts
624 * @cliexstart{show gbp contract}
628 VLIB_CLI_COMMAND (gbp_contract_show_node, static) = {
629 .path = "show gbp contract",
630 .short_help = "show gbp contract\n",
631 .function = gbp_contract_show,
636 gbp_next_hop_get_node (fib_node_index_t index)
640 gnh = gbp_next_hop_get (index);
642 return (&gnh->gnh_node);
646 gbp_next_hop_last_lock_gone (fib_node_t * node)
651 static gbp_next_hop_t *
652 gbp_next_hop_from_fib_node (fib_node_t * node)
654 ASSERT (gbp_next_hop_fib_type == node->fn_type);
655 return ((gbp_next_hop_t *) node);
658 static fib_node_back_walk_rc_t
659 gbp_next_hop_back_walk_notify (fib_node_t * node,
660 fib_node_back_walk_ctx_t * ctx)
664 gnh = gbp_next_hop_from_fib_node (node);
666 gbp_contract_mk_one_lb (gnh->gnh_gu);
668 return (FIB_NODE_BACK_WALK_CONTINUE);
672 * The FIB path's graph node virtual function table
674 static const fib_node_vft_t gbp_next_hop_vft = {
675 .fnv_get = gbp_next_hop_get_node,
676 .fnv_last_lock = gbp_next_hop_last_lock_gone,
677 .fnv_back_walk = gbp_next_hop_back_walk_notify,
678 // .fnv_mem_show = fib_path_memory_show,
681 static clib_error_t *
682 gbp_contract_init (vlib_main_t * vm)
684 gc_logger = vlib_log_register_class ("gbp", "con");
685 gbp_next_hop_fib_type = fib_node_register_new_type (&gbp_next_hop_vft);
690 VLIB_INIT_FUNCTION (gbp_contract_init);
693 * fd.io coding-style-patch-verification: ON
696 * eval: (c-set-style "gnu")