API: Change ip4_address and ip6_address to use type alias.
[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 #include <vnet/ip/ip_types_api.h>
23 #include <igmp/igmp_ssm_range.h>
24
25 /* define message IDs */
26 #include <igmp/igmp_msg_enum.h>
27
28 /* define message structures */
29 #define vl_typedefs
30 #include <igmp/igmp_all_api_h.h>
31 #undef vl_typedefs
32
33 /* define generated endian-swappers */
34 #define vl_endianfun
35 #include <igmp/igmp_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #define vl_printfun
41 #include <igmp/igmp_all_api_h.h>
42 #undef vl_printfun
43
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>
47 #undef vl_api_version
48
49 #include <vlibapi/api_helper_macros.h>
50
51 #define IGMP_MSG_ID(_id) (_id + igmp_main.msg_id_base)
52
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)                                          \
64
65 static void
66 vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
67 {
68   vlib_main_t *vm = vlib_get_main ();
69   vnet_main_t *vnm = vnet_get_main ();
70   vl_api_igmp_listen_reply_t *rmp;
71   int ii, rv = 0;
72   ip46_address_t gaddr, *saddrs = NULL;
73
74   VALIDATE_SW_IF_INDEX (&mp->group);
75
76   if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->group.sw_if_index)) &&
77        VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
78     {
79       // FIXME - don't we clear this state on interface down ...
80       rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
81       goto done;
82     }
83
84   clib_memset (&gaddr, 0, sizeof (gaddr));
85   clib_memcpy (&gaddr.ip4, &mp->group.gaddr, sizeof (ip4_address_t));
86
87   vec_validate (saddrs, mp->group.n_srcs - 1);
88
89   vec_foreach_index (ii, saddrs)
90   {
91     clib_memcpy (&saddrs[ii].ip4,
92                  &mp->group.saddrs[ii], sizeof (ip4_address_t));
93   }
94
95   rv = igmp_listen (vm,
96                     (mp->group.filter ?
97                      IGMP_FILTER_MODE_INCLUDE :
98                      IGMP_FILTER_MODE_EXCLUDE),
99                     ntohl (mp->group.sw_if_index), saddrs, &gaddr);
100
101   vec_free (saddrs);
102
103   BAD_SW_IF_INDEX_LABEL;
104 done:;
105   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_LISTEN_REPLY));
106 }
107
108 static void
109 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
110 {
111   vl_api_igmp_enable_disable_reply_t *rmp;
112   int rv = 0;
113
114   VALIDATE_SW_IF_INDEX (mp);
115
116   rv = igmp_enable_disable (ntohl (mp->sw_if_index),
117                             mp->enable,
118                             (mp->mode ? IGMP_MODE_HOST : IGMP_MODE_ROUTER));
119
120   BAD_SW_IF_INDEX_LABEL;
121
122   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_ENABLE_DISABLE_REPLY));
123 }
124
125 static void
126 vl_api_igmp_proxy_device_add_del_t_handler (vl_api_igmp_proxy_device_add_del_t
127                                             * mp)
128 {
129   vl_api_igmp_proxy_device_add_del_reply_t *rmp;
130   int rv = 0;
131
132   VALIDATE_SW_IF_INDEX (mp);
133
134   rv =
135     igmp_proxy_device_add_del (ntohl (mp->vrf_id), ntohl (mp->sw_if_index),
136                                mp->add);
137
138   BAD_SW_IF_INDEX_LABEL;
139
140   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_REPLY));
141 }
142
143 static void
144   vl_api_igmp_proxy_device_add_del_interface_t_handler
145   (vl_api_igmp_proxy_device_add_del_interface_t * mp)
146 {
147   vl_api_igmp_proxy_device_add_del_interface_reply_t *rmp;
148   int rv = 0;
149
150   VALIDATE_SW_IF_INDEX (mp);
151
152   rv =
153     igmp_proxy_device_add_del_interface (ntohl (mp->vrf_id),
154                                          ntohl (mp->sw_if_index), mp->add);
155
156   BAD_SW_IF_INDEX_LABEL;
157
158   REPLY_MACRO (IGMP_MSG_ID
159                (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_INTERFACE_REPLY));
160 }
161
162 static void
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)
166 {
167   vl_api_igmp_details_t *mp;
168
169   mp = vl_msg_api_alloc (sizeof (*mp));
170   clib_memset (mp, 0, sizeof (*mp));
171
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));
177
178   vl_msg_api_send_shmem (q, (u8 *) & mp);
179 }
180
181 static void
182 igmp_config_dump (igmp_main_t * im,
183                   unix_shared_memory_queue_t * q,
184                   u32 context, igmp_config_t * config)
185 {
186   igmp_group_t *group;
187   igmp_src_t *src;
188
189   /* *INDENT-OFF* */
190   FOR_EACH_GROUP (group, config,
191     ({
192       FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
193         ({
194           send_igmp_details (q, im, config, group, src, context);
195         }));
196     }));
197   /* *INDENT-ON* */
198 }
199
200 static void
201 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
202 {
203   unix_shared_memory_queue_t *q;
204   igmp_main_t *im = &igmp_main;
205   igmp_config_t *config;
206   u32 sw_if_index;
207
208   q = vl_api_client_index_to_input_queue (mp->client_index);
209   if (!q)
210     return;
211
212   sw_if_index = ntohl (mp->sw_if_index);
213   if (~0 == sw_if_index)
214     {
215       /* *INDENT-OFF* */
216       pool_foreach (config, im->configs,
217         ({
218           igmp_config_dump(im, q, mp->context, config);
219         }));
220       /* *INDENT-ON* */
221     }
222   else
223     {
224       config = igmp_config_lookup (sw_if_index);
225       if (config)
226         {
227           igmp_config_dump (im, q, mp->context, config);
228         }
229     }
230 }
231
232 static void
233 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
234 {
235   vl_api_igmp_clear_interface_reply_t *rmp;
236   igmp_config_t *config;
237   int rv = 0;
238
239   config = igmp_config_lookup (ntohl (mp->sw_if_index));
240   if (config)
241     igmp_clear_config (config);
242
243   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_CLEAR_INTERFACE_REPLY));
244 }
245
246 static vl_api_group_prefix_type_t
247 igmp_group_type_int_to_api (igmp_group_prefix_type_t t)
248 {
249   switch (t)
250     {
251     case IGMP_GROUP_PREFIX_TYPE_ASM:
252       return (htonl (ASM));
253     case IGMP_GROUP_PREFIX_TYPE_SSM:
254       return (htonl (SSM));
255     }
256
257   return (SSM);
258 }
259
260 static igmp_group_prefix_type_t
261 igmp_group_type_api_to_int (vl_api_group_prefix_type_t t)
262 {
263   switch (htonl (t))
264     {
265     case ASM:
266       return (IGMP_GROUP_PREFIX_TYPE_ASM);
267     case SSM:
268       return (IGMP_GROUP_PREFIX_TYPE_SSM);
269     }
270
271   return (IGMP_GROUP_PREFIX_TYPE_SSM);
272 }
273
274 static void
275 vl_api_igmp_group_prefix_set_t_handler (vl_api_igmp_group_prefix_set_t * mp)
276 {
277   vl_api_igmp_group_prefix_set_reply_t *rmp;
278   fib_prefix_t pfx;
279   int rv = 0;
280
281   ip_prefix_decode (&mp->gp.prefix, &pfx);
282   igmp_group_prefix_set (&pfx, igmp_group_type_api_to_int (mp->gp.type));
283
284   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_GROUP_PREFIX_SET_REPLY));
285 }
286
287 typedef struct igmp_ssm_range_walk_ctx_t_
288 {
289   unix_shared_memory_queue_t *q;
290   u32 context;
291 } igmp_ssm_range_walk_ctx_t;
292
293 static walk_rc_t
294 igmp_ssm_range_walk_dump (const fib_prefix_t * pfx,
295                           igmp_group_prefix_type_t type, void *args)
296 {
297   igmp_ssm_range_walk_ctx_t *ctx = args;
298   vl_api_igmp_group_prefix_details_t *mp;
299
300   mp = vl_msg_api_alloc (sizeof (*mp));
301   clib_memset (mp, 0, sizeof (*mp));
302
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);
307
308   vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
309
310   return (WALK_CONTINUE);
311 }
312
313 static void
314 vl_api_igmp_group_prefix_dump_t_handler (vl_api_igmp_dump_t * mp)
315 {
316   unix_shared_memory_queue_t *q;
317
318   q = vl_api_client_index_to_input_queue (mp->client_index);
319   if (!q)
320     return;
321
322   igmp_ssm_range_walk_ctx_t ctx = {
323     .q = q,
324     .context = mp->context,
325   };
326
327   igmp_ssm_range_walk (igmp_ssm_range_walk_dump, &ctx);
328 }
329
330 static vpe_client_registration_t *
331 igmp_api_client_lookup (igmp_main_t * im, u32 client_index)
332 {
333   uword *p;
334   vpe_client_registration_t *api_client = NULL;
335
336   p = hash_get (im->igmp_api_client_by_client_index, client_index);
337   if (p)
338     api_client = vec_elt_at_index (im->api_clients, p[0]);
339
340   return api_client;
341 }
342
343 static void
344 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
345 {
346   igmp_main_t *im = &igmp_main;
347   vpe_client_registration_t *api_client;
348   vl_api_want_igmp_events_reply_t *rmp;
349   int rv = 0;
350
351   api_client = igmp_api_client_lookup (im, mp->client_index);
352   if (api_client)
353     {
354       if (mp->enable)
355         {
356           rv = VNET_API_ERROR_INVALID_REGISTRATION;
357           goto done;
358         }
359       hash_unset (im->igmp_api_client_by_client_index,
360                   api_client->client_index);
361       pool_put (im->api_clients, api_client);
362       goto done;
363     }
364   if (mp->enable)
365     {
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);
372       goto done;
373     }
374   rv = VNET_API_ERROR_INVALID_REGISTRATION;
375
376 done:;
377   REPLY_MACRO (VL_API_WANT_IGMP_EVENTS_REPLY + im->msg_id_base);
378 }
379
380 static clib_error_t *
381 want_igmp_events_reaper (u32 client_index)
382 {
383   igmp_main_t *im = &igmp_main;
384   vpe_client_registration_t *api_client;
385   uword *p;
386
387   p = hash_get (im->igmp_api_client_by_client_index, client_index);
388
389   if (p)
390     {
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);
394     }
395   return (NULL);
396 }
397
398 VL_MSG_API_REAPER_FUNCTION (want_igmp_events_reaper);
399
400 void
401 send_igmp_event (unix_shared_memory_queue_t * q,
402                  u32 context,
403                  igmp_filter_mode_t filter,
404                  u32 sw_if_index,
405                  const ip46_address_t * saddr, const ip46_address_t * gaddr)
406 {
407   vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
408   clib_memset (mp, 0, sizeof (*mp));
409
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));
416
417   vl_msg_api_send_shmem (q, (u8 *) & mp);
418 }
419
420 void
421 igmp_event (igmp_filter_mode_t filter,
422             u32 sw_if_index,
423             const ip46_address_t * saddr, const ip46_address_t * gaddr)
424 {
425   vpe_client_registration_t *api_client;
426   unix_shared_memory_queue_t *q;
427   igmp_main_t *im;
428
429   im = &igmp_main;
430
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);
436
437
438   /* *INDENT-OFF* */
439   pool_foreach (api_client, im->api_clients,
440     ({
441       q = vl_api_client_index_to_input_queue (api_client->client_index);
442       if (q)
443         send_igmp_event (q, 0, filter, sw_if_index, saddr, gaddr);
444     }));
445   /* *INDENT-ON* */
446 }
447
448 #define vl_msg_name_crc_list
449 #include <igmp/igmp_all_api_h.h>
450 #undef vl_msg_name_crc_list
451
452 static void
453 setup_message_id_table (igmp_main_t * im, api_main_t * am)
454 {
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;
458 #undef _
459 }
460
461 /* Set up the API message handling tables */
462 static clib_error_t *
463 igmp_plugin_api_hookup (vlib_main_t * vm)
464 {
465   igmp_main_t *im = &igmp_main;
466   api_main_t *am = &api_main;
467   u8 *name;
468
469   /* Construct the API name */
470   name = format (0, "igmp_%08x%c", api_version, 0);
471
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);
475
476 #define _(N,n)                                                  \
477     vl_msg_api_set_handlers((VL_API_##N + im->msg_id_base),     \
478                            #n,                                  \
479                            vl_api_##n##_t_handler,              \
480                            vl_noop_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;
485 #undef _
486
487   /*
488    * Set up the (msg_name, crc, message-id) table
489    */
490   setup_message_id_table (im, am);
491
492   vec_free (name);
493   return 0;
494 }
495
496 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
497
498 /*
499  * fd.io coding-style-patch-verification: ON
500  *
501  * Local Variables:
502  * eval: (c-set-style "gnu")
503  * End:
504  */