igmp: bugfix and minor improvements
[vpp.git] / src / plugins / igmp / igmp_api.c
1 /*
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17
18 #include <igmp/igmp.h>
19
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22
23 /* define message IDs */
24 #include <igmp/igmp_msg_enum.h>
25
26 /* define message structures */
27 #define vl_typedefs
28 #include <igmp/igmp_all_api_h.h>
29 #undef vl_typedefs
30
31 /* define generated endian-swappers */
32 #define vl_endianfun
33 #include <igmp/igmp_all_api_h.h>
34 #undef vl_endianfun
35
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
38 #define vl_printfun
39 #include <igmp/igmp_all_api_h.h>
40 #undef vl_printfun
41
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>
45 #undef vl_api_version
46
47 #include <vlibapi/api_helper_macros.h>
48
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)                    \
55
56 static void
57 vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
58 {
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;
63   int rv = 0;
64   ip46_address_t saddr, gaddr;
65
66   if (!vnet_sw_interface_is_api_valid (vnm, ntohl (mp->sw_if_index)))
67     {
68       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
69       goto done;
70     }
71
72   if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->sw_if_index)) &&
73        VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
74     {
75       rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
76       goto done;
77     }
78
79   clib_memcpy (&saddr.ip4.as_u8, mp->saddr, sizeof (u8) * 4);
80   clib_memcpy (&gaddr.ip4.as_u8, mp->gaddr, sizeof (u8) * 4);
81
82   rv =
83     igmp_listen (vm, mp->enable, ntohl (mp->sw_if_index), saddr, gaddr,
84                  IGMP_CONFIG_FLAG_CLI_API_CONFIGURED);
85
86 done:;
87   unix_shared_memory_queue_t *q =
88     vl_api_client_index_to_input_queue (mp->client_index);
89   if (!q)
90     return;
91
92   rmp = vl_msg_api_alloc (sizeof (*rmp));
93   rmp->_vl_msg_id = htons ((VL_API_IGMP_LISTEN_REPLY) + im->msg_id_base);
94   rmp->context = mp->context;
95   rmp->retval = htonl (rv);
96
97   vl_msg_api_send_shmem (q, (u8 *) & rmp);
98 }
99
100 static void
101 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
102 {
103   vl_api_igmp_enable_disable_reply_t *rmp;
104   igmp_main_t *im = &igmp_main;
105   int rv = 0;
106
107   REPLY_MACRO (VL_API_IGMP_ENABLE_DISABLE_REPLY + im->msg_id_base);
108 }
109
110 static void
111 send_igmp_details (unix_shared_memory_queue_t * q, igmp_main_t * im,
112                    igmp_config_t * config, igmp_group_t * group,
113                    igmp_src_t * src, u32 context)
114 {
115   vl_api_igmp_details_t *mp;
116
117   mp = vl_msg_api_alloc (sizeof (*mp));
118   memset (mp, 0, sizeof (*mp));
119
120   mp->_vl_msg_id = htons (VL_API_IGMP_DETAILS + im->msg_id_base);
121   mp->context = context;
122   mp->sw_if_index = htonl (config->sw_if_index);
123   clib_memcpy (mp->saddr, &src->addr.ip4, sizeof (u8) * 4);
124   clib_memcpy (mp->gaddr, &group->addr.ip4, sizeof (u8) * 4);
125
126   vl_msg_api_send_shmem (q, (u8 *) & mp);
127 }
128
129 static void
130 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
131 {
132   igmp_main_t *im = &igmp_main;
133   igmp_config_t *config;
134   igmp_group_t *group;
135   igmp_src_t *src;
136
137   unix_shared_memory_queue_t *q =
138     vl_api_client_index_to_input_queue (mp->client_index);
139   if (!q)
140     return;
141
142   if (mp->dump_all)
143     {
144       /* *INDENT-OFF* */
145       pool_foreach (config, im->configs, (
146         {
147             pool_foreach (group, config->groups, (
148               {
149                 pool_foreach (src, group->srcs, (
150                   {
151                     send_igmp_details (q, im, config, group, src, mp->context);
152                   }));
153               }));
154         }));
155       /* *INDENT-ON* */
156       return;
157     }
158   config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
159   if (config)
160     {
161       /* *INDENT-OFF* */
162       pool_foreach (group, config->groups, (
163         {
164           pool_foreach (src, group->srcs, (
165             {
166               send_igmp_details (q, im, config, group, src, mp->context);
167             }));
168         }));
169       /* *INDENT-ON* */
170     }
171 }
172
173 static void
174 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
175 {
176   igmp_main_t *im = &igmp_main;
177   igmp_config_t *config;
178   vl_api_igmp_clear_interface_reply_t *rmp;
179   int rv = 0;
180
181   config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
182   if (config)
183     igmp_clear_config (config);
184
185   unix_shared_memory_queue_t *q =
186     vl_api_client_index_to_input_queue (mp->client_index);
187   if (!q)
188     return;
189
190   rmp = vl_msg_api_alloc (sizeof (*rmp));
191   rmp->_vl_msg_id =
192     htons ((VL_API_IGMP_CLEAR_INTERFACE_REPLY) + im->msg_id_base);
193   rmp->context = mp->context;
194   rmp->retval = htonl (rv);
195
196   vl_msg_api_send_shmem (q, (u8 *) & rmp);
197 }
198
199 /** \brief igmp group lookup
200     @param im - igmp main
201     @param client_index - client index
202 */
203 static vpe_client_registration_t *
204 igmp_api_client_lookup (igmp_main_t * im, u32 client_index)
205 {
206   uword *p;
207   vpe_client_registration_t *api_client = NULL;
208
209   p = hash_get (im->igmp_api_client_by_client_index, client_index);
210   if (p)
211     api_client = vec_elt_at_index (im->api_clients, p[0]);
212
213   return api_client;
214 }
215
216 static void
217 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
218 {
219   igmp_main_t *im = &igmp_main;
220   vpe_client_registration_t *api_client;
221   vl_api_want_igmp_events_reply_t *rmp;
222   int rv = 0;
223
224   api_client = igmp_api_client_lookup (im, mp->client_index);
225   if (api_client)
226     {
227       if (mp->enable)
228         {
229           rv = VNET_API_ERROR_INVALID_REGISTRATION;
230           goto done;
231         }
232       hash_unset (im->igmp_api_client_by_client_index,
233                   api_client->client_index);
234       pool_put (im->api_clients, api_client);
235       goto done;
236     }
237   if (mp->enable)
238     {
239       pool_get (im->api_clients, api_client);
240       memset (api_client, 0, sizeof (vpe_client_registration_t));
241       api_client->client_index = mp->client_index;
242       api_client->client_pid = mp->pid;
243       hash_set (im->igmp_api_client_by_client_index,
244                 mp->client_index, api_client - im->api_clients);
245       goto done;
246     }
247   rv = VNET_API_ERROR_INVALID_REGISTRATION;
248
249 done:;
250   unix_shared_memory_queue_t *q =
251     vl_api_client_index_to_input_queue (mp->client_index);
252   if (!q)
253     return;
254
255   rmp = vl_msg_api_alloc (sizeof (*rmp));
256   rmp->_vl_msg_id = htons ((VL_API_WANT_IGMP_EVENTS_REPLY) + im->msg_id_base);
257   rmp->context = mp->context;
258   rmp->retval = htonl (rv);
259
260   vl_msg_api_send_shmem (q, (u8 *) & rmp);
261 }
262
263 static clib_error_t *
264 want_igmp_events_reaper (u32 client_index)
265 {
266   igmp_main_t *im = &igmp_main;
267   vpe_client_registration_t *api_client;
268   uword *p;
269
270   p = hash_get (im->igmp_api_client_by_client_index, client_index);
271
272   if (p)
273     {
274       api_client = pool_elt_at_index (im->api_clients, p[0]);
275       pool_put (im->api_clients, api_client);
276       hash_unset (im->igmp_api_client_by_client_index, client_index);
277     }
278   return (NULL);
279 }
280
281 VL_MSG_API_REAPER_FUNCTION (want_igmp_events_reaper);
282
283 void
284 send_igmp_event (unix_shared_memory_queue_t * q, u32 context,
285                  igmp_main_t * im, igmp_config_t * config,
286                  igmp_group_t * group, igmp_src_t * src)
287 {
288   vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
289   memset (mp, 0, sizeof (*mp));
290
291   mp->_vl_msg_id = ntohs ((VL_API_IGMP_EVENT) + im->msg_id_base);
292   mp->context = context;
293   mp->sw_if_index = htonl (config->sw_if_index);
294   clib_memcpy (&mp->saddr, &src->addr.ip4, sizeof (ip4_address_t));
295   clib_memcpy (&mp->gaddr, &group->addr.ip4, sizeof (ip4_address_t));
296   mp->is_join =
297     (group->type == IGMP_MEMBERSHIP_GROUP_mode_is_filter_include) ? 1 : 0;
298
299   vl_msg_api_send_shmem (q, (u8 *) & mp);
300 }
301
302 void
303 igmp_event (igmp_main_t * im, igmp_config_t * config, igmp_group_t * group,
304             igmp_src_t * src)
305 {
306   vpe_client_registration_t *api_client;
307   unix_shared_memory_queue_t *q;
308   /* *INDENT-OFF* */
309   pool_foreach (api_client, im->api_clients,
310     ({
311       q = vl_api_client_index_to_input_queue (api_client->client_index);
312       if (q)
313         send_igmp_event (q, 0, im, config, group, src);
314     }));
315   /* *INDENT-ON* */
316   if (group->type == IGMP_MEMBERSHIP_GROUP_block_old_sources)
317     {
318       igmp_clear_group (config, group);
319       if (pool_elts (config->groups) == 0)
320         {
321           hash_unset (im->igmp_config_by_sw_if_index, config->sw_if_index);
322           pool_put (im->configs, config);
323         }
324     }
325 }
326
327 #define vl_msg_name_crc_list
328 #include <igmp/igmp_all_api_h.h>
329 #undef vl_msg_name_crc_list
330
331 static void
332 setup_message_id_table (igmp_main_t * im, api_main_t * am)
333 {
334 #define _(id,n,crc) \
335   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + im->msg_id_base);
336   foreach_vl_msg_name_crc_igmp;
337 #undef _
338 }
339
340 /* Set up the API message handling tables */
341 static clib_error_t *
342 igmp_plugin_api_hookup (vlib_main_t * vm)
343 {
344   igmp_main_t *im = &igmp_main;
345   api_main_t *am = &api_main;
346   u8 *name;
347
348   /* Construct the API name */
349   name = format (0, "igmp_%08x%c", api_version, 0);
350
351   /* Ask for a correctly-sized block of API message decode slots */
352   im->msg_id_base = vl_msg_api_get_msg_ids
353     ((char *) name, VL_MSG_FIRST_AVAILABLE);
354
355 #define _(N,n)                                                  \
356     vl_msg_api_set_handlers((VL_API_##N + im->msg_id_base),     \
357                            #n,                                  \
358                            vl_api_##n##_t_handler,              \
359                            vl_noop_handler,                     \
360                            vl_api_##n##_t_endian,               \
361                            vl_api_##n##_t_print,                \
362                            sizeof(vl_api_##n##_t), 1);
363   foreach_igmp_plugin_api_msg;
364 #undef _
365
366   /*
367    * Set up the (msg_name, crc, message-id) table
368    */
369   setup_message_id_table (im, am);
370
371   vec_free (name);
372   return 0;
373 }
374
375 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
376
377 /*
378  * fd.io coding-style-patch-verification: ON
379  *
380  * Local Variables:
381  * eval: (c-set-style "gnu")
382  * End:
383  */