2 *------------------------------------------------------------------
3 * gbp_api.c - layer 2 emulation api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vnet/plugin/plugin.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vpp/app/version.h>
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
32 /* define message IDs */
33 #include <gbp/gbp_msg_enum.h>
35 #define vl_typedefs /* define message structures */
36 #include <gbp/gbp_all_api_h.h>
39 #define vl_endianfun /* define message structures */
40 #include <gbp/gbp_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <gbp/gbp_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <acl/acl_all_api_h.h>
54 #include <vlibapi/api_helper_macros.h>
56 #define foreach_gbp_api_msg \
57 _(GBP_ENDPOINT_ADD_DEL, gbp_endpoint_add_del) \
58 _(GBP_ENDPOINT_DUMP, gbp_endpoint_dump) \
59 _(GBP_CONTRACT_ADD_DEL, gbp_contract_add_del) \
60 _(GBP_CONTRACT_DUMP, gbp_contract_dump)
65 typedef struct gbp_main_t_
70 static gbp_main_t gbp_main;
72 #define GBP_MSG_BASE gbp_main.msg_id_base
75 vl_api_gbp_endpoint_add_del_t_handler (vl_api_gbp_endpoint_add_del_t * mp)
77 vl_api_gbp_endpoint_add_del_reply_t *rmp;
78 ip46_address_t ip = { };
82 sw_if_index = ntohl (mp->endpoint.sw_if_index);
83 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
86 if (mp->endpoint.is_ip6)
88 clib_memcpy (&ip.ip6, mp->endpoint.address, sizeof (ip.ip6));
92 clib_memcpy (&ip.ip4, mp->endpoint.address, sizeof (ip.ip4));
97 gbp_endpoint_update (sw_if_index, &ip, ntohl (mp->endpoint.epg_id));
101 gbp_endpoint_delete (sw_if_index, &ip);
104 BAD_SW_IF_INDEX_LABEL;
106 REPLY_MACRO (VL_API_GBP_ENDPOINT_ADD_DEL_REPLY + GBP_MSG_BASE);
109 typedef struct gbp_walk_ctx_t_
111 vl_api_registration_t *reg;
116 gbp_endpoint_send_details (gbp_endpoint_t * gbpe, void *args)
118 vl_api_gbp_endpoint_details_t *mp;
122 mp = vl_msg_api_alloc (sizeof (*mp));
126 memset (mp, 0, sizeof (*mp));
127 mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_DETAILS + GBP_MSG_BASE);
128 mp->context = ctx->context;
130 mp->endpoint.sw_if_index = ntohl (gbpe->ge_key->gek_sw_if_index);
131 mp->endpoint.is_ip6 = !ip46_address_is_ip4 (&gbpe->ge_key->gek_ip);
132 if (mp->endpoint.is_ip6)
133 clib_memcpy (&mp->endpoint.address,
134 &gbpe->ge_key->gek_ip.ip6,
135 sizeof (gbpe->ge_key->gek_ip.ip6));
137 clib_memcpy (&mp->endpoint.address,
138 &gbpe->ge_key->gek_ip.ip4,
139 sizeof (gbpe->ge_key->gek_ip.ip4));
141 mp->endpoint.epg_id = ntohl (gbpe->ge_epg_id);
143 vl_api_send_msg (ctx->reg, (u8 *) mp);
149 vl_api_gbp_endpoint_dump_t_handler (vl_api_gbp_endpoint_dump_t * mp)
151 vl_api_registration_t *reg;
153 reg = vl_api_client_index_to_registration (mp->client_index);
157 gbp_walk_ctx_t ctx = {
159 .context = mp->context,
162 gbp_endpoint_walk (gbp_endpoint_send_details, &ctx);
166 vl_api_gbp_contract_add_del_t_handler (vl_api_gbp_contract_add_del_t * mp)
168 vl_api_gbp_contract_add_del_reply_t *rmp;
172 gbp_contract_update (ntohl (mp->contract.src_epg),
173 ntohl (mp->contract.dst_epg),
174 ntohl (mp->contract.acl_index));
176 gbp_contract_delete (ntohl (mp->contract.src_epg),
177 ntohl (mp->contract.dst_epg));
179 REPLY_MACRO (VL_API_GBP_CONTRACT_ADD_DEL_REPLY + GBP_MSG_BASE);
183 gbp_contract_send_details (gbp_contract_t * gbpc, void *args)
185 vl_api_gbp_contract_details_t *mp;
189 mp = vl_msg_api_alloc (sizeof (*mp));
193 memset (mp, 0, sizeof (*mp));
194 mp->_vl_msg_id = ntohs (VL_API_GBP_CONTRACT_DETAILS + GBP_MSG_BASE);
195 mp->context = ctx->context;
197 mp->contract.src_epg = ntohl (gbpc->gc_key.gck_src);
198 mp->contract.dst_epg = ntohl (gbpc->gc_key.gck_dst);
199 mp->contract.acl_index = ntohl (gbpc->gc_acl_index);
201 vl_api_send_msg (ctx->reg, (u8 *) mp);
207 vl_api_gbp_contract_dump_t_handler (vl_api_gbp_contract_dump_t * mp)
209 vl_api_registration_t *reg;
211 reg = vl_api_client_index_to_registration (mp->client_index);
215 gbp_walk_ctx_t ctx = {
217 .context = mp->context,
220 gbp_contract_walk (gbp_contract_send_details, &ctx);
225 * Add vpe's API message handlers to the table.
226 * vlib has alread mapped shared memory and
227 * added the client registration handlers.
228 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
230 #define vl_msg_name_crc_list
231 #include <gbp/gbp_all_api_h.h>
232 #undef vl_msg_name_crc_list
235 setup_message_id_table (api_main_t * am)
237 #define _(id,n,crc) \
238 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + GBP_MSG_BASE);
239 foreach_vl_msg_name_crc_gbp;
244 gbp_api_hookup (vlib_main_t * vm)
247 vl_msg_api_set_handlers(VL_API_##N + GBP_MSG_BASE, \
249 vl_api_##n##_t_handler, \
251 vl_api_##n##_t_endian, \
252 vl_api_##n##_t_print, \
253 sizeof(vl_api_##n##_t), 1);
258 static clib_error_t *
259 gbp_init (vlib_main_t * vm)
261 api_main_t *am = &api_main;
262 gbp_main_t *gbpm = &gbp_main;
263 u8 *name = format (0, "gbp_%08x%c", api_version, 0);
265 /* Ask for a correctly-sized block of API message decode slots */
266 gbpm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
267 VL_MSG_FIRST_AVAILABLE);
271 /* Add our API messages to the global name_crc hash table */
272 setup_message_id_table (am);
278 VLIB_API_INIT_FUNCTION (gbp_init);
281 VLIB_PLUGIN_REGISTER () = {
282 .version = VPP_BUILD_VER,
283 .description = "Group Based Policy",
289 * fd.io coding-style-patch-verification: ON
292 * eval: (c-set-style "gnu")