2 *------------------------------------------------------------------
3 * Copyright (c) 2017 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 <igmp/igmp.h>
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <igmp/igmp_ssm_range.h>
25 /* define message IDs */
26 #include <igmp/igmp_msg_enum.h>
28 /* define message structures */
30 #include <igmp/igmp_all_api_h.h>
33 /* define generated endian-swappers */
35 #include <igmp/igmp_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <igmp/igmp_all_api_h.h>
44 /* Get the API version number */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <igmp/igmp_all_api_h.h>
49 #include <vlibapi/api_helper_macros.h>
51 #define IGMP_MSG_ID(_id) (_id + igmp_main.msg_id_base)
53 #define foreach_igmp_plugin_api_msg \
54 _(IGMP_LISTEN, igmp_listen) \
55 _(IGMP_ENABLE_DISABLE, igmp_enable_disable) \
56 _(IGMP_PROXY_DEVICE_ADD_DEL, igmp_proxy_device_add_del) \
57 _(IGMP_PROXY_DEVICE_ADD_DEL_INTERFACE, igmp_proxy_device_add_del_interface) \
58 _(IGMP_DUMP, igmp_dump) \
59 _(IGMP_CLEAR_INTERFACE, igmp_clear_interface) \
60 _(IGMP_CLEAR_INTERFACE, igmp_clear_interface) \
61 _(IGMP_GROUP_PREFIX_SET, igmp_group_prefix_set) \
62 _(IGMP_GROUP_PREFIX_DUMP, igmp_group_prefix_dump) \
63 _(WANT_IGMP_EVENTS, want_igmp_events) \
66 vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
68 vlib_main_t *vm = vlib_get_main ();
69 vnet_main_t *vnm = vnet_get_main ();
70 vl_api_igmp_listen_reply_t *rmp;
72 ip46_address_t gaddr, *saddrs = NULL;
74 VALIDATE_SW_IF_INDEX (&mp->group);
76 if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->group.sw_if_index)) &&
77 VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
79 // FIXME - don't we clear this state on interface down ...
80 rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
84 clib_memset (&gaddr, 0, sizeof (gaddr));
85 clib_memcpy (&gaddr.ip4, &mp->group.gaddr, sizeof (ip4_address_t));
87 vec_validate (saddrs, mp->group.n_srcs - 1);
89 vec_foreach_index (ii, saddrs)
91 clib_memcpy (&saddrs[ii].ip4,
92 &mp->group.saddrs[ii], sizeof (ip4_address_t));
97 IGMP_FILTER_MODE_INCLUDE :
98 IGMP_FILTER_MODE_EXCLUDE),
99 ntohl (mp->group.sw_if_index), saddrs, &gaddr);
103 BAD_SW_IF_INDEX_LABEL;
105 REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_LISTEN_REPLY));
109 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
111 vl_api_igmp_enable_disable_reply_t *rmp;
114 VALIDATE_SW_IF_INDEX (mp);
116 rv = igmp_enable_disable (ntohl (mp->sw_if_index),
118 (mp->mode ? IGMP_MODE_HOST : IGMP_MODE_ROUTER));
120 BAD_SW_IF_INDEX_LABEL;
122 REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_ENABLE_DISABLE_REPLY));
126 vl_api_igmp_proxy_device_add_del_t_handler (vl_api_igmp_proxy_device_add_del_t
129 vl_api_igmp_proxy_device_add_del_reply_t *rmp;
132 VALIDATE_SW_IF_INDEX (mp);
135 igmp_proxy_device_add_del (ntohl (mp->vrf_id), ntohl (mp->sw_if_index),
138 BAD_SW_IF_INDEX_LABEL;
140 REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_REPLY));
144 vl_api_igmp_proxy_device_add_del_interface_t_handler
145 (vl_api_igmp_proxy_device_add_del_interface_t * mp)
147 vl_api_igmp_proxy_device_add_del_interface_reply_t *rmp;
150 VALIDATE_SW_IF_INDEX (mp);
153 igmp_proxy_device_add_del_interface (ntohl (mp->vrf_id),
154 ntohl (mp->sw_if_index), mp->add);
156 BAD_SW_IF_INDEX_LABEL;
158 REPLY_MACRO (IGMP_MSG_ID
159 (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_INTERFACE_REPLY));
163 send_igmp_details (unix_shared_memory_queue_t * q, igmp_main_t * im,
164 igmp_config_t * config, igmp_group_t * group,
165 igmp_src_t * src, u32 context)
167 vl_api_igmp_details_t *mp;
169 mp = vl_msg_api_alloc (sizeof (*mp));
170 clib_memset (mp, 0, sizeof (*mp));
172 mp->_vl_msg_id = htons (IGMP_MSG_ID (VL_API_IGMP_DETAILS));
173 mp->context = context;
174 mp->sw_if_index = htonl (config->sw_if_index);
175 clib_memcpy (&mp->saddr, &src->key->ip4, sizeof (src->key->ip4));
176 clib_memcpy (&mp->gaddr, &group->key->ip4, sizeof (group->key->ip4));
178 vl_msg_api_send_shmem (q, (u8 *) & mp);
182 igmp_config_dump (igmp_main_t * im,
183 unix_shared_memory_queue_t * q,
184 u32 context, igmp_config_t * config)
190 FOR_EACH_GROUP (group, config,
192 FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
194 send_igmp_details (q, im, config, group, src, context);
201 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
203 unix_shared_memory_queue_t *q;
204 igmp_main_t *im = &igmp_main;
205 igmp_config_t *config;
208 q = vl_api_client_index_to_input_queue (mp->client_index);
212 sw_if_index = ntohl (mp->sw_if_index);
213 if (~0 == sw_if_index)
216 pool_foreach (config, im->configs,
218 igmp_config_dump(im, q, mp->context, config);
224 config = igmp_config_lookup (sw_if_index);
227 igmp_config_dump (im, q, mp->context, config);
233 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
235 vl_api_igmp_clear_interface_reply_t *rmp;
236 igmp_config_t *config;
239 config = igmp_config_lookup (ntohl (mp->sw_if_index));
241 igmp_clear_config (config);
243 REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_CLEAR_INTERFACE_REPLY));
246 static vl_api_group_prefix_type_t
247 igmp_group_type_int_to_api (igmp_group_prefix_type_t t)
251 case IGMP_GROUP_PREFIX_TYPE_ASM:
252 return (htonl (ASM));
253 case IGMP_GROUP_PREFIX_TYPE_SSM:
254 return (htonl (SSM));
260 static igmp_group_prefix_type_t
261 igmp_group_type_api_to_int (vl_api_group_prefix_type_t t)
266 return (IGMP_GROUP_PREFIX_TYPE_ASM);
268 return (IGMP_GROUP_PREFIX_TYPE_SSM);
271 return (IGMP_GROUP_PREFIX_TYPE_SSM);
275 vl_api_igmp_group_prefix_set_t_handler (vl_api_igmp_group_prefix_set_t * mp)
277 vl_api_igmp_group_prefix_set_reply_t *rmp;
281 ip_prefix_decode (&mp->gp.prefix, &pfx);
282 igmp_group_prefix_set (&pfx, igmp_group_type_api_to_int (mp->gp.type));
284 REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_GROUP_PREFIX_SET_REPLY));
287 typedef struct igmp_ssm_range_walk_ctx_t_
289 unix_shared_memory_queue_t *q;
291 } igmp_ssm_range_walk_ctx_t;
294 igmp_ssm_range_walk_dump (const fib_prefix_t * pfx,
295 igmp_group_prefix_type_t type, void *args)
297 igmp_ssm_range_walk_ctx_t *ctx = args;
298 vl_api_igmp_group_prefix_details_t *mp;
300 mp = vl_msg_api_alloc (sizeof (*mp));
301 clib_memset (mp, 0, sizeof (*mp));
303 mp->_vl_msg_id = htons (IGMP_MSG_ID (VL_API_IGMP_DETAILS));
304 mp->context = ctx->context;
305 mp->gp.type = igmp_group_type_int_to_api (type);
306 ip_prefix_encode (pfx, &mp->gp.prefix);
308 vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
310 return (WALK_CONTINUE);
314 vl_api_igmp_group_prefix_dump_t_handler (vl_api_igmp_dump_t * mp)
316 unix_shared_memory_queue_t *q;
318 q = vl_api_client_index_to_input_queue (mp->client_index);
322 igmp_ssm_range_walk_ctx_t ctx = {
324 .context = mp->context,
327 igmp_ssm_range_walk (igmp_ssm_range_walk_dump, &ctx);
330 static vpe_client_registration_t *
331 igmp_api_client_lookup (igmp_main_t * im, u32 client_index)
334 vpe_client_registration_t *api_client = NULL;
336 p = hash_get (im->igmp_api_client_by_client_index, client_index);
338 api_client = vec_elt_at_index (im->api_clients, p[0]);
344 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
346 igmp_main_t *im = &igmp_main;
347 vpe_client_registration_t *api_client;
348 vl_api_want_igmp_events_reply_t *rmp;
351 api_client = igmp_api_client_lookup (im, mp->client_index);
356 rv = VNET_API_ERROR_INVALID_REGISTRATION;
359 hash_unset (im->igmp_api_client_by_client_index,
360 api_client->client_index);
361 pool_put (im->api_clients, api_client);
366 pool_get (im->api_clients, api_client);
367 clib_memset (api_client, 0, sizeof (vpe_client_registration_t));
368 api_client->client_index = mp->client_index;
369 api_client->client_pid = mp->pid;
370 hash_set (im->igmp_api_client_by_client_index,
371 mp->client_index, api_client - im->api_clients);
374 rv = VNET_API_ERROR_INVALID_REGISTRATION;
377 REPLY_MACRO (VL_API_WANT_IGMP_EVENTS_REPLY + im->msg_id_base);
380 static clib_error_t *
381 want_igmp_events_reaper (u32 client_index)
383 igmp_main_t *im = &igmp_main;
384 vpe_client_registration_t *api_client;
387 p = hash_get (im->igmp_api_client_by_client_index, client_index);
391 api_client = pool_elt_at_index (im->api_clients, p[0]);
392 pool_put (im->api_clients, api_client);
393 hash_unset (im->igmp_api_client_by_client_index, client_index);
398 VL_MSG_API_REAPER_FUNCTION (want_igmp_events_reaper);
401 send_igmp_event (unix_shared_memory_queue_t * q,
403 igmp_filter_mode_t filter,
405 const ip46_address_t * saddr, const ip46_address_t * gaddr)
407 vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
408 clib_memset (mp, 0, sizeof (*mp));
410 mp->_vl_msg_id = ntohs ((VL_API_IGMP_EVENT) + igmp_main.msg_id_base);
411 mp->context = context;
412 mp->sw_if_index = htonl (sw_if_index);
413 mp->filter = htonl (filter);
414 clib_memcpy (&mp->saddr, &saddr->ip4, sizeof (ip4_address_t));
415 clib_memcpy (&mp->gaddr, &gaddr->ip4, sizeof (ip4_address_t));
417 vl_msg_api_send_shmem (q, (u8 *) & mp);
421 igmp_event (igmp_filter_mode_t filter,
423 const ip46_address_t * saddr, const ip46_address_t * gaddr)
425 vpe_client_registration_t *api_client;
426 unix_shared_memory_queue_t *q;
431 IGMP_DBG ("event: (%U, %U) %U %U",
432 format_ip46_address, saddr, IP46_TYPE_ANY,
433 format_ip46_address, saddr, IP46_TYPE_ANY,
434 format_vnet_sw_if_index_name,
435 vnet_get_main (), sw_if_index, format_igmp_filter_mode, filter);
439 pool_foreach (api_client, im->api_clients,
441 q = vl_api_client_index_to_input_queue (api_client->client_index);
443 send_igmp_event (q, 0, filter, sw_if_index, saddr, gaddr);
448 #define vl_msg_name_crc_list
449 #include <igmp/igmp_all_api_h.h>
450 #undef vl_msg_name_crc_list
453 setup_message_id_table (igmp_main_t * im, api_main_t * am)
455 #define _(id,n,crc) \
456 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + im->msg_id_base);
457 foreach_vl_msg_name_crc_igmp;
461 /* Set up the API message handling tables */
462 static clib_error_t *
463 igmp_plugin_api_hookup (vlib_main_t * vm)
465 igmp_main_t *im = &igmp_main;
466 api_main_t *am = &api_main;
469 /* Construct the API name */
470 name = format (0, "igmp_%08x%c", api_version, 0);
472 /* Ask for a correctly-sized block of API message decode slots */
473 im->msg_id_base = vl_msg_api_get_msg_ids
474 ((char *) name, VL_MSG_FIRST_AVAILABLE);
477 vl_msg_api_set_handlers((VL_API_##N + im->msg_id_base), \
479 vl_api_##n##_t_handler, \
481 vl_api_##n##_t_endian, \
482 vl_api_##n##_t_print, \
483 sizeof(vl_api_##n##_t), 1);
484 foreach_igmp_plugin_api_msg;
488 * Set up the (msg_name, crc, message-id) table
490 setup_message_id_table (im, am);
496 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
499 * fd.io coding-style-patch-verification: ON
502 * eval: (c-set-style "gnu")