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>
23 /* define message IDs */
24 #include <igmp/igmp_msg_enum.h>
26 /* define message structures */
28 #include <igmp/igmp_all_api_h.h>
31 /* define generated endian-swappers */
33 #include <igmp/igmp_all_api_h.h>
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #include <igmp/igmp_all_api_h.h>
42 /* Get the API version number */
43 #define vl_api_version(n,v) static u32 api_version=(v);
44 #include <igmp/igmp_all_api_h.h>
47 #include <vlibapi/api_helper_macros.h>
49 #define foreach_igmp_plugin_api_msg \
50 _(IGMP_LISTEN, igmp_listen) \
51 _(IGMP_ENABLE_DISABLE, igmp_enable_disable) \
52 _(IGMP_DUMP, igmp_dump) \
53 _(IGMP_CLEAR_INTERFACE, igmp_clear_interface) \
54 _(WANT_IGMP_EVENTS, want_igmp_events) \
57 vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
59 vlib_main_t *vm = vlib_get_main ();
60 vnet_main_t *vnm = vnet_get_main ();
61 igmp_main_t *im = &igmp_main;
62 vl_api_igmp_listen_reply_t *rmp;
64 ip46_address_t saddr, gaddr;
66 if (!vnet_sw_interface_is_api_valid (vnm, ntohl (mp->sw_if_index)))
68 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
72 if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->sw_if_index)) &&
73 VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
75 rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
79 clib_memcpy (&saddr.ip4.as_u8, mp->saddr, sizeof (u8) * 4);
80 clib_memcpy (&gaddr.ip4.as_u8, mp->gaddr, sizeof (u8) * 4);
82 rv = igmp_listen (vm, mp->enable, ntohl (mp->sw_if_index), saddr, gaddr, 1);
85 unix_shared_memory_queue_t *q =
86 vl_api_client_index_to_input_queue (mp->client_index);
90 rmp = vl_msg_api_alloc (sizeof (*rmp));
91 rmp->_vl_msg_id = htons ((VL_API_IGMP_LISTEN_REPLY) + im->msg_id_base);
92 rmp->context = mp->context;
93 rmp->retval = htonl (rv);
95 vl_msg_api_send_shmem (q, (u8 *) & rmp);
99 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
101 vl_api_igmp_enable_disable_reply_t *rmp;
102 igmp_main_t *im = &igmp_main;
105 REPLY_MACRO (VL_API_IGMP_ENABLE_DISABLE_REPLY + im->msg_id_base);
109 send_igmp_details (unix_shared_memory_queue_t * q, igmp_main_t * im,
110 igmp_config_t * config, igmp_group_t * group,
111 igmp_src_t * src, u32 context)
113 vl_api_igmp_details_t *mp;
115 mp = vl_msg_api_alloc (sizeof (*mp));
116 memset (mp, 0, sizeof (*mp));
118 mp->_vl_msg_id = htons (VL_API_IGMP_DETAILS + im->msg_id_base);
119 mp->context = context;
120 mp->sw_if_index = htonl (config->sw_if_index);
121 clib_memcpy (mp->saddr, &src->addr.ip4, sizeof (u8) * 4);
122 clib_memcpy (mp->gaddr, &group->addr.ip4, sizeof (u8) * 4);
124 vl_msg_api_send_shmem (q, (u8 *) & mp);
128 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
130 igmp_main_t *im = &igmp_main;
131 igmp_config_t *config;
135 unix_shared_memory_queue_t *q =
136 vl_api_client_index_to_input_queue (mp->client_index);
143 pool_foreach (config, im->configs, (
145 pool_foreach (group, config->groups, (
147 pool_foreach (src, group->srcs, (
149 send_igmp_details (q, im, config, group, src, mp->context);
156 config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
160 pool_foreach (group, config->groups, (
162 pool_foreach (src, group->srcs, (
164 send_igmp_details (q, im, config, group, src, mp->context);
172 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
174 igmp_main_t *im = &igmp_main;
175 igmp_config_t *config;
176 vl_api_igmp_clear_interface_reply_t *rmp;
179 config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
181 igmp_clear_config (config);
183 unix_shared_memory_queue_t *q =
184 vl_api_client_index_to_input_queue (mp->client_index);
188 rmp = vl_msg_api_alloc (sizeof (*rmp));
190 htons ((VL_API_IGMP_CLEAR_INTERFACE_REPLY) + im->msg_id_base);
191 rmp->context = mp->context;
192 rmp->retval = htonl (rv);
194 vl_msg_api_send_shmem (q, (u8 *) & rmp);
198 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
200 igmp_main_t *im = &igmp_main;
201 igmp_api_client_t *api_client;
202 vl_api_want_igmp_events_reply_t *rmp;
205 api_client = igmp_api_client_lookup (im, mp->client_index);
210 rv = VNET_API_ERROR_INVALID_REGISTRATION;
213 hash_unset_mem (im->igmp_api_client_by_client_index,
214 &api_client->client_index);
215 pool_put (im->api_clients, api_client);
220 pool_get (im->api_clients, api_client);
221 memset (api_client, 0, sizeof (igmp_api_client_t));
222 api_client->client_index = mp->client_index;
223 api_client->pid = mp->pid;
224 hash_set_mem (im->igmp_api_client_by_client_index,
225 &mp->client_index, api_client - im->api_clients);
228 rv = VNET_API_ERROR_INVALID_REGISTRATION;
231 unix_shared_memory_queue_t *q =
232 vl_api_client_index_to_input_queue (mp->client_index);
236 rmp = vl_msg_api_alloc (sizeof (*rmp));
237 rmp->_vl_msg_id = htons ((VL_API_WANT_IGMP_EVENTS_REPLY) + im->msg_id_base);
238 rmp->context = mp->context;
239 rmp->retval = htonl (rv);
241 vl_msg_api_send_shmem (q, (u8 *) & rmp);
245 send_igmp_event (unix_shared_memory_queue_t * q, u32 context,
246 igmp_main_t * im, igmp_config_t * config,
247 igmp_group_t * group, igmp_src_t * src)
249 vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
250 memset (mp, 0, sizeof (*mp));
252 mp->_vl_msg_id = ntohs ((VL_API_IGMP_EVENT) + im->msg_id_base);
253 mp->context = context;
254 mp->sw_if_index = htonl (config->sw_if_index);
255 clib_memcpy (&mp->saddr, &src->addr.ip4, sizeof (ip4_address_t));
256 clib_memcpy (&mp->gaddr, &group->addr.ip4, sizeof (ip4_address_t));
258 (group->type == IGMP_MEMBERSHIP_GROUP_mode_is_filter_include) ? 1 : 0;
260 vl_msg_api_send_shmem (q, (u8 *) & mp);
264 igmp_event (igmp_main_t * im, igmp_config_t * config, igmp_group_t * group,
267 igmp_api_client_t *api_client;
268 unix_shared_memory_queue_t *q;
270 pool_foreach (api_client, im->api_clients,
272 q = vl_api_client_index_to_input_queue (api_client->client_index);
274 send_igmp_event (q, 0, im, config, group, src);
277 if (group->type == IGMP_MEMBERSHIP_GROUP_block_old_sources)
279 igmp_clear_group (config, group);
280 if (pool_elts (config->groups) == 0)
282 hash_unset_mem (im->igmp_config_by_sw_if_index,
283 &config->sw_if_index);
284 pool_put (im->configs, config);
289 #define vl_msg_name_crc_list
290 #include <igmp/igmp_all_api_h.h>
291 #undef vl_msg_name_crc_list
294 setup_message_id_table (igmp_main_t * im, api_main_t * am)
296 #define _(id,n,crc) \
297 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + im->msg_id_base);
298 foreach_vl_msg_name_crc_igmp;
302 /* Set up the API message handling tables */
303 static clib_error_t *
304 igmp_plugin_api_hookup (vlib_main_t * vm)
306 igmp_main_t *im = &igmp_main;
307 api_main_t *am = &api_main;
310 /* Construct the API name */
311 name = format (0, "igmp_%08x%c", api_version, 0);
313 /* Ask for a correctly-sized block of API message decode slots */
314 im->msg_id_base = vl_msg_api_get_msg_ids
315 ((char *) name, VL_MSG_FIRST_AVAILABLE);
318 vl_msg_api_set_handlers((VL_API_##N + im->msg_id_base), \
320 vl_api_##n##_t_handler, \
322 vl_api_##n##_t_endian, \
323 vl_api_##n##_t_print, \
324 sizeof(vl_api_##n##_t), 1);
325 foreach_igmp_plugin_api_msg;
329 * Set up the (msg_name, crc, message-id) table
331 setup_message_id_table (im, am);
337 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
340 * fd.io coding-style-patch-verification: ON
343 * eval: (c-set-style "gnu")