2 *------------------------------------------------------------------
3 * Copyright (c) 2018 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
21 #include <vnet/interface.h>
22 #include <vnet/api_errno.h>
23 #include <vpp/app/version.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
30 /* define message IDs */
31 #include <gbp/gbp_msg_enum.h>
33 #define vl_typedefs /* define message structures */
34 #include <gbp/gbp_all_api_h.h>
37 #define vl_endianfun /* define message structures */
38 #include <gbp/gbp_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <gbp/gbp_all_api_h.h>
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <gbp/gbp_all_api_h.h>
52 #include <vlibapi/api_helper_macros.h>
54 #define foreach_gbp_api_msg \
55 _(GBP_ENDPOINT_ADD_DEL, gbp_endpoint_add_del) \
56 _(GBP_ENDPOINT_DUMP, gbp_endpoint_dump) \
57 _(GBP_SUBNET_ADD_DEL, gbp_subnet_add_del) \
58 _(GBP_SUBNET_DUMP, gbp_subnet_dump) \
59 _(GBP_ENDPOINT_GROUP_ADD_DEL, gbp_endpoint_group_add_del) \
60 _(GBP_ENDPOINT_GROUP_DUMP, gbp_endpoint_group_dump) \
61 _(GBP_RECIRC_ADD_DEL, gbp_recirc_add_del) \
62 _(GBP_RECIRC_DUMP, gbp_recirc_dump) \
63 _(GBP_CONTRACT_ADD_DEL, gbp_contract_add_del) \
64 _(GBP_CONTRACT_DUMP, gbp_contract_dump)
68 static u16 msg_id_base;
70 #define GBP_MSG_BASE msg_id_base
73 vl_api_gbp_endpoint_add_del_t_handler (vl_api_gbp_endpoint_add_del_t * mp)
75 vl_api_gbp_endpoint_add_del_reply_t *rmp;
76 ip46_address_t ip = { };
80 sw_if_index = ntohl (mp->endpoint.sw_if_index);
81 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
84 if (mp->endpoint.is_ip6)
86 clib_memcpy (&ip.ip6, mp->endpoint.address, sizeof (ip.ip6));
90 clib_memcpy (&ip.ip4, mp->endpoint.address, sizeof (ip.ip4));
96 gbp_endpoint_update (sw_if_index, &ip, ntohs (mp->endpoint.epg_id));
100 gbp_endpoint_delete (sw_if_index, &ip);
103 BAD_SW_IF_INDEX_LABEL;
105 REPLY_MACRO (VL_API_GBP_ENDPOINT_ADD_DEL_REPLY + GBP_MSG_BASE);
108 typedef struct gbp_walk_ctx_t_
110 vl_api_registration_t *reg;
115 gbp_endpoint_send_details (gbp_endpoint_t * gbpe, void *args)
117 vl_api_gbp_endpoint_details_t *mp;
121 mp = vl_msg_api_alloc (sizeof (*mp));
125 memset (mp, 0, sizeof (*mp));
126 mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_DETAILS + GBP_MSG_BASE);
127 mp->context = ctx->context;
129 mp->endpoint.sw_if_index = ntohl (gbpe->ge_key->gek_sw_if_index);
130 mp->endpoint.is_ip6 = !ip46_address_is_ip4 (&gbpe->ge_key->gek_ip);
131 if (mp->endpoint.is_ip6)
132 clib_memcpy (&mp->endpoint.address,
133 &gbpe->ge_key->gek_ip.ip6,
134 sizeof (gbpe->ge_key->gek_ip.ip6));
136 clib_memcpy (&mp->endpoint.address,
137 &gbpe->ge_key->gek_ip.ip4,
138 sizeof (gbpe->ge_key->gek_ip.ip4));
140 mp->endpoint.epg_id = ntohs (gbpe->ge_epg_id);
142 vl_api_send_msg (ctx->reg, (u8 *) mp);
148 vl_api_gbp_endpoint_dump_t_handler (vl_api_gbp_endpoint_dump_t * mp)
150 vl_api_registration_t *reg;
152 reg = vl_api_client_index_to_registration (mp->client_index);
156 gbp_walk_ctx_t ctx = {
158 .context = mp->context,
161 gbp_endpoint_walk (gbp_endpoint_send_details, &ctx);
165 vl_api_gbp_endpoint_group_add_del_t_handler
166 (vl_api_gbp_endpoint_group_add_del_t * mp)
168 vl_api_gbp_endpoint_group_add_del_reply_t *rmp;
169 u32 uplink_sw_if_index;
172 uplink_sw_if_index = ntohl (mp->epg.uplink_sw_if_index);
173 if (!vnet_sw_if_index_is_api_valid (uplink_sw_if_index))
174 goto bad_sw_if_index;
178 rv = gbp_endpoint_group_add (ntohs (mp->epg.epg_id),
179 ntohl (mp->epg.bd_id),
180 ntohl (mp->epg.ip4_table_id),
181 ntohl (mp->epg.ip6_table_id),
186 gbp_endpoint_group_delete (ntohs (mp->epg.epg_id));
189 BAD_SW_IF_INDEX_LABEL;
191 REPLY_MACRO (VL_API_GBP_ENDPOINT_GROUP_ADD_DEL_REPLY + GBP_MSG_BASE);
195 vl_api_gbp_subnet_add_del_t_handler (vl_api_gbp_subnet_add_del_t * mp)
197 vl_api_gbp_subnet_add_del_reply_t *rmp;
200 .fp_len = mp->subnet.address_length,
201 .fp_proto = (mp->subnet.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
204 if (mp->subnet.is_ip6)
205 clib_memcpy (&pfx.fp_addr.ip6, mp->subnet.address,
206 sizeof (pfx.fp_addr.ip6));
208 clib_memcpy (&pfx.fp_addr.ip4, mp->subnet.address,
209 sizeof (pfx.fp_addr.ip4));
211 rv = gbp_subnet_add_del (ntohl (mp->subnet.table_id),
213 ntohl (mp->subnet.sw_if_index),
214 ntohs (mp->subnet.epg_id),
215 mp->is_add, mp->subnet.is_internal);
217 REPLY_MACRO (VL_API_GBP_SUBNET_ADD_DEL_REPLY + GBP_MSG_BASE);
221 gbp_subnet_send_details (u32 table_id,
222 const fib_prefix_t * pfx,
224 epg_id_t epg, u8 is_internal, void *args)
226 vl_api_gbp_subnet_details_t *mp;
230 mp = vl_msg_api_alloc (sizeof (*mp));
234 memset (mp, 0, sizeof (*mp));
235 mp->_vl_msg_id = ntohs (VL_API_GBP_SUBNET_DETAILS + GBP_MSG_BASE);
236 mp->context = ctx->context;
238 mp->subnet.is_internal = is_internal;
239 mp->subnet.sw_if_index = ntohl (sw_if_index);
240 mp->subnet.epg_id = ntohs (epg);
241 mp->subnet.is_ip6 = (pfx->fp_proto == FIB_PROTOCOL_IP6);
242 mp->subnet.address_length = pfx->fp_len;
243 mp->subnet.table_id = ntohl (table_id);
244 if (mp->subnet.is_ip6)
245 clib_memcpy (&mp->subnet.address,
246 &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
248 clib_memcpy (&mp->subnet.address,
249 &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
252 vl_api_send_msg (ctx->reg, (u8 *) mp);
258 vl_api_gbp_subnet_dump_t_handler (vl_api_gbp_subnet_dump_t * mp)
260 vl_api_registration_t *reg;
262 reg = vl_api_client_index_to_registration (mp->client_index);
266 gbp_walk_ctx_t ctx = {
268 .context = mp->context,
271 gbp_subnet_walk (gbp_subnet_send_details, &ctx);
275 gbp_endpoint_group_send_details (gbp_endpoint_group_t * gepg, void *args)
277 vl_api_gbp_endpoint_group_details_t *mp;
281 mp = vl_msg_api_alloc (sizeof (*mp));
285 memset (mp, 0, sizeof (*mp));
286 mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_GROUP_DETAILS + GBP_MSG_BASE);
287 mp->context = ctx->context;
289 mp->epg.uplink_sw_if_index = ntohl (gepg->gepg_uplink_sw_if_index);
290 mp->epg.epg_id = ntohs (gepg->gepg_id);
291 mp->epg.bd_id = ntohl (gepg->gepg_bd);
292 mp->epg.ip4_table_id = ntohl (gepg->gepg_rd[FIB_PROTOCOL_IP4]);
293 mp->epg.ip6_table_id = ntohl (gepg->gepg_rd[FIB_PROTOCOL_IP6]);
295 vl_api_send_msg (ctx->reg, (u8 *) mp);
301 vl_api_gbp_endpoint_group_dump_t_handler (vl_api_gbp_endpoint_group_dump_t *
304 vl_api_registration_t *reg;
306 reg = vl_api_client_index_to_registration (mp->client_index);
310 gbp_walk_ctx_t ctx = {
312 .context = mp->context,
315 gbp_endpoint_group_walk (gbp_endpoint_group_send_details, &ctx);
319 vl_api_gbp_recirc_add_del_t_handler (vl_api_gbp_recirc_add_del_t * mp)
321 vl_api_gbp_recirc_add_del_reply_t *rmp;
325 sw_if_index = ntohl (mp->recirc.sw_if_index);
326 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
327 goto bad_sw_if_index;
330 gbp_recirc_add (sw_if_index,
331 ntohs (mp->recirc.epg_id), mp->recirc.is_ext);
333 gbp_recirc_delete (sw_if_index);
335 BAD_SW_IF_INDEX_LABEL;
337 REPLY_MACRO (VL_API_GBP_RECIRC_ADD_DEL_REPLY + GBP_MSG_BASE);
341 gbp_recirc_send_details (gbp_recirc_t * gr, void *args)
343 vl_api_gbp_recirc_details_t *mp;
347 mp = vl_msg_api_alloc (sizeof (*mp));
351 memset (mp, 0, sizeof (*mp));
352 mp->_vl_msg_id = ntohs (VL_API_GBP_RECIRC_DETAILS + GBP_MSG_BASE);
353 mp->context = ctx->context;
355 mp->recirc.epg_id = ntohs (gr->gr_epg);
356 mp->recirc.sw_if_index = ntohl (gr->gr_sw_if_index);
357 mp->recirc.is_ext = ntohl (gr->gr_is_ext);
359 vl_api_send_msg (ctx->reg, (u8 *) mp);
365 vl_api_gbp_recirc_dump_t_handler (vl_api_gbp_recirc_dump_t * mp)
367 vl_api_registration_t *reg;
369 reg = vl_api_client_index_to_registration (mp->client_index);
373 gbp_walk_ctx_t ctx = {
375 .context = mp->context,
378 gbp_recirc_walk (gbp_recirc_send_details, &ctx);
382 vl_api_gbp_contract_add_del_t_handler (vl_api_gbp_contract_add_del_t * mp)
384 vl_api_gbp_contract_add_del_reply_t *rmp;
388 gbp_contract_update (ntohs (mp->contract.src_epg),
389 ntohs (mp->contract.dst_epg),
390 ntohl (mp->contract.acl_index));
392 gbp_contract_delete (ntohs (mp->contract.src_epg),
393 ntohs (mp->contract.dst_epg));
395 REPLY_MACRO (VL_API_GBP_CONTRACT_ADD_DEL_REPLY + GBP_MSG_BASE);
399 gbp_contract_send_details (gbp_contract_t * gbpc, void *args)
401 vl_api_gbp_contract_details_t *mp;
405 mp = vl_msg_api_alloc (sizeof (*mp));
409 memset (mp, 0, sizeof (*mp));
410 mp->_vl_msg_id = ntohs (VL_API_GBP_CONTRACT_DETAILS + GBP_MSG_BASE);
411 mp->context = ctx->context;
413 mp->contract.src_epg = ntohs (gbpc->gc_key.gck_src);
414 mp->contract.dst_epg = ntohs (gbpc->gc_key.gck_dst);
415 mp->contract.acl_index = ntohl (gbpc->gc_value.gc_acl_index);
417 vl_api_send_msg (ctx->reg, (u8 *) mp);
423 vl_api_gbp_contract_dump_t_handler (vl_api_gbp_contract_dump_t * mp)
425 vl_api_registration_t *reg;
427 reg = vl_api_client_index_to_registration (mp->client_index);
431 gbp_walk_ctx_t ctx = {
433 .context = mp->context,
436 gbp_contract_walk (gbp_contract_send_details, &ctx);
441 * Add vpe's API message handlers to the table.
442 * vlib has alread mapped shared memory and
443 * added the client registration handlers.
444 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
446 #define vl_msg_name_crc_list
447 #include <gbp/gbp_all_api_h.h>
448 #undef vl_msg_name_crc_list
451 setup_message_id_table (api_main_t * am)
453 #define _(id,n,crc) \
454 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + GBP_MSG_BASE);
455 foreach_vl_msg_name_crc_gbp;
460 gbp_api_hookup (vlib_main_t * vm)
463 vl_msg_api_set_handlers(VL_API_##N + GBP_MSG_BASE, \
465 vl_api_##n##_t_handler, \
467 vl_api_##n##_t_endian, \
468 vl_api_##n##_t_print, \
469 sizeof(vl_api_##n##_t), 1);
474 static clib_error_t *
475 gbp_init (vlib_main_t * vm)
477 api_main_t *am = &api_main;
478 gbp_main_t *gbpm = &gbp_main;
479 u8 *name = format (0, "gbp_%08x%c", api_version, 0);
481 gbpm->gbp_acl_user_id = ~0;
483 /* Ask for a correctly-sized block of API message decode slots */
484 msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
485 VL_MSG_FIRST_AVAILABLE);
488 /* Add our API messages to the global name_crc hash table */
489 setup_message_id_table (am);
495 VLIB_API_INIT_FUNCTION (gbp_init);
498 VLIB_PLUGIN_REGISTER () = {
499 .version = VPP_BUILD_VER,
500 .description = "Group Based Policy",
506 * fd.io coding-style-patch-verification: ON
509 * eval: (c-set-style "gnu")