vcl: allow more rx events on peek
[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.api_enum.h>
27 #include <igmp/igmp.api_types.h>
28 #include <vnet/format_fns.h>
29
30 #include <vlibapi/api_helper_macros.h>
31
32 #define IGMP_MSG_ID(_id) (_id + igmp_main.msg_id_base)
33
34 static void
35 vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
36 {
37   vlib_main_t *vm = vlib_get_main ();
38   vnet_main_t *vnm = vnet_get_main ();
39   vl_api_igmp_listen_reply_t *rmp;
40   int ii, rv = 0;
41   ip46_address_t gaddr, *saddrs = NULL;
42
43   VALIDATE_SW_IF_INDEX (&mp->group);
44
45   if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->group.sw_if_index)) &&
46        VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
47     {
48       // FIXME - don't we clear this state on interface down ...
49       rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
50       goto done;
51     }
52
53   clib_memset (&gaddr, 0, sizeof (gaddr));
54   clib_memcpy (&gaddr.ip4, &mp->group.gaddr, sizeof (ip4_address_t));
55
56   vec_validate (saddrs, mp->group.n_srcs - 1);
57
58   vec_foreach_index (ii, saddrs)
59   {
60     clib_memcpy (&saddrs[ii].ip4,
61                  &mp->group.saddrs[ii], sizeof (ip4_address_t));
62   }
63
64   rv = igmp_listen (vm,
65                     (mp->group.filter ?
66                      IGMP_FILTER_MODE_INCLUDE :
67                      IGMP_FILTER_MODE_EXCLUDE),
68                     ntohl (mp->group.sw_if_index), saddrs, &gaddr);
69
70   vec_free (saddrs);
71
72   BAD_SW_IF_INDEX_LABEL;
73 done:;
74   REPLY_MACRO (VL_API_IGMP_LISTEN_REPLY);
75 }
76
77 static void
78 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
79 {
80   vl_api_igmp_enable_disable_reply_t *rmp;
81   int rv = 0;
82
83   VALIDATE_SW_IF_INDEX (mp);
84
85   rv = igmp_enable_disable (ntohl (mp->sw_if_index),
86                             mp->enable,
87                             (mp->mode ? IGMP_MODE_HOST : IGMP_MODE_ROUTER));
88
89   BAD_SW_IF_INDEX_LABEL;
90
91   REPLY_MACRO (VL_API_IGMP_ENABLE_DISABLE_REPLY);
92 }
93
94 static void
95 vl_api_igmp_proxy_device_add_del_t_handler (vl_api_igmp_proxy_device_add_del_t
96                                             * mp)
97 {
98   vl_api_igmp_proxy_device_add_del_reply_t *rmp;
99   int rv = 0;
100
101   VALIDATE_SW_IF_INDEX (mp);
102
103   rv =
104     igmp_proxy_device_add_del (ntohl (mp->vrf_id), ntohl (mp->sw_if_index),
105                                mp->add);
106
107   BAD_SW_IF_INDEX_LABEL;
108
109   REPLY_MACRO (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_REPLY);
110 }
111
112 static void
113   vl_api_igmp_proxy_device_add_del_interface_t_handler
114   (vl_api_igmp_proxy_device_add_del_interface_t * mp)
115 {
116   vl_api_igmp_proxy_device_add_del_interface_reply_t *rmp;
117   int rv = 0;
118
119   VALIDATE_SW_IF_INDEX (mp);
120
121   rv =
122     igmp_proxy_device_add_del_interface (ntohl (mp->vrf_id),
123                                          ntohl (mp->sw_if_index), mp->add);
124
125   BAD_SW_IF_INDEX_LABEL;
126
127   REPLY_MACRO (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_INTERFACE_REPLY);
128 }
129
130 static void
131 send_igmp_details (vl_api_registration_t * rp, igmp_main_t * im,
132                    igmp_config_t * config, igmp_group_t * group,
133                    igmp_src_t * src, u32 context)
134 {
135   vl_api_igmp_details_t *mp;
136
137   mp = vl_msg_api_alloc (sizeof (*mp));
138   clib_memset (mp, 0, sizeof (*mp));
139
140   mp->_vl_msg_id = htons (IGMP_MSG_ID (VL_API_IGMP_DETAILS));
141   mp->context = context;
142   mp->sw_if_index = htonl (config->sw_if_index);
143   clib_memcpy (&mp->saddr, &src->key->ip4, sizeof (src->key->ip4));
144   clib_memcpy (&mp->gaddr, &group->key->ip4, sizeof (group->key->ip4));
145
146   vl_api_send_msg (rp, (u8 *) mp);
147 }
148
149 static void
150 igmp_config_dump (igmp_main_t * im,
151                   vl_api_registration_t * rp,
152                   u32 context, igmp_config_t * config)
153 {
154   igmp_group_t *group;
155   igmp_src_t *src;
156
157   FOR_EACH_GROUP (group, config,
158     ({
159       FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
160         ({
161           send_igmp_details (rp, im, config, group, src, context);
162         }));
163     }));
164 }
165
166 static void
167 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
168 {
169   igmp_main_t *im = &igmp_main;
170   igmp_config_t *config;
171   u32 sw_if_index;
172   vl_api_registration_t *rp;
173
174   rp = vl_api_client_index_to_registration (mp->client_index);
175   if (rp == 0)
176     return;
177
178   sw_if_index = ntohl (mp->sw_if_index);
179   if (~0 == sw_if_index)
180     {
181       pool_foreach (config, im->configs)
182          {
183           igmp_config_dump(im, rp, mp->context, config);
184         }
185     }
186   else
187     {
188       config = igmp_config_lookup (sw_if_index);
189       if (config)
190         {
191           igmp_config_dump (im, rp, mp->context, config);
192         }
193     }
194 }
195
196 static void
197 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
198 {
199   vl_api_igmp_clear_interface_reply_t *rmp;
200   igmp_config_t *config;
201   int rv = 0;
202
203   config = igmp_config_lookup (ntohl (mp->sw_if_index));
204   if (config)
205     igmp_clear_config (config);
206
207   REPLY_MACRO (VL_API_IGMP_CLEAR_INTERFACE_REPLY);
208 }
209
210 static vl_api_group_prefix_type_t
211 igmp_group_type_int_to_api (igmp_group_prefix_type_t t)
212 {
213   switch (t)
214     {
215     case IGMP_GROUP_PREFIX_TYPE_ASM:
216       return (htonl (ASM));
217     case IGMP_GROUP_PREFIX_TYPE_SSM:
218       return (htonl (SSM));
219     }
220
221   return (SSM);
222 }
223
224 static igmp_group_prefix_type_t
225 igmp_group_type_api_to_int (vl_api_group_prefix_type_t t)
226 {
227   switch (htonl (t))
228     {
229     case ASM:
230       return (IGMP_GROUP_PREFIX_TYPE_ASM);
231     case SSM:
232       return (IGMP_GROUP_PREFIX_TYPE_SSM);
233     }
234
235   return (IGMP_GROUP_PREFIX_TYPE_SSM);
236 }
237
238 static void
239 vl_api_igmp_group_prefix_set_t_handler (vl_api_igmp_group_prefix_set_t * mp)
240 {
241   vl_api_igmp_group_prefix_set_reply_t *rmp;
242   fib_prefix_t pfx;
243   int rv = 0;
244
245   ip_prefix_decode (&mp->gp.prefix, &pfx);
246   igmp_group_prefix_set (&pfx, igmp_group_type_api_to_int (mp->gp.type));
247
248   REPLY_MACRO (VL_API_IGMP_GROUP_PREFIX_SET_REPLY);
249 }
250
251 typedef struct igmp_ssm_range_walk_ctx_t_
252 {
253   vl_api_registration_t *rp;
254   u32 context;
255 } igmp_ssm_range_walk_ctx_t;
256
257 static walk_rc_t
258 igmp_ssm_range_walk_dump (const fib_prefix_t * pfx,
259                           igmp_group_prefix_type_t type, void *args)
260 {
261   igmp_ssm_range_walk_ctx_t *ctx = args;
262   vl_api_igmp_group_prefix_details_t *mp;
263
264   mp = vl_msg_api_alloc (sizeof (*mp));
265   clib_memset (mp, 0, sizeof (*mp));
266
267   mp->_vl_msg_id = htons (IGMP_MSG_ID (VL_API_IGMP_DETAILS));
268   mp->context = ctx->context;
269   mp->gp.type = igmp_group_type_int_to_api (type);
270   ip_prefix_encode (pfx, &mp->gp.prefix);
271
272   vl_api_send_msg (ctx->rp, (u8 *) mp);
273
274   return (WALK_CONTINUE);
275 }
276
277 static void
278 vl_api_igmp_group_prefix_dump_t_handler (vl_api_igmp_dump_t * mp)
279 {
280   vl_api_registration_t *rp;
281
282   rp = vl_api_client_index_to_registration (mp->client_index);
283   if (rp == 0)
284     return;
285
286   igmp_ssm_range_walk_ctx_t ctx = {
287     .rp = rp,
288     .context = mp->context,
289   };
290
291   igmp_ssm_range_walk (igmp_ssm_range_walk_dump, &ctx);
292 }
293
294 static vpe_client_registration_t *
295 igmp_api_client_lookup (igmp_main_t * im, u32 client_index)
296 {
297   uword *p;
298   vpe_client_registration_t *api_client = NULL;
299
300   p = hash_get (im->igmp_api_client_by_client_index, client_index);
301   if (p)
302     api_client = vec_elt_at_index (im->api_clients, p[0]);
303
304   return api_client;
305 }
306
307 static void
308 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
309 {
310   igmp_main_t *im = &igmp_main;
311   vpe_client_registration_t *api_client;
312   vl_api_want_igmp_events_reply_t *rmp;
313   int rv = 0;
314
315   api_client = igmp_api_client_lookup (im, mp->client_index);
316   if (api_client)
317     {
318       if (mp->enable)
319         {
320           rv = VNET_API_ERROR_INVALID_REGISTRATION;
321           goto done;
322         }
323       hash_unset (im->igmp_api_client_by_client_index,
324                   api_client->client_index);
325       pool_put (im->api_clients, api_client);
326       goto done;
327     }
328   if (mp->enable)
329     {
330       pool_get (im->api_clients, api_client);
331       clib_memset (api_client, 0, sizeof (vpe_client_registration_t));
332       api_client->client_index = mp->client_index;
333       api_client->client_pid = mp->pid;
334       hash_set (im->igmp_api_client_by_client_index,
335                 mp->client_index, api_client - im->api_clients);
336       goto done;
337     }
338   rv = VNET_API_ERROR_INVALID_REGISTRATION;
339
340 done:
341   REPLY_MACRO (VL_API_WANT_IGMP_EVENTS_REPLY);
342 }
343
344 static clib_error_t *
345 want_igmp_events_reaper (u32 client_index)
346 {
347   igmp_main_t *im = &igmp_main;
348   vpe_client_registration_t *api_client;
349   uword *p;
350
351   p = hash_get (im->igmp_api_client_by_client_index, client_index);
352
353   if (p)
354     {
355       api_client = pool_elt_at_index (im->api_clients, p[0]);
356       pool_put (im->api_clients, api_client);
357       hash_unset (im->igmp_api_client_by_client_index, client_index);
358     }
359   return (NULL);
360 }
361
362 VL_MSG_API_REAPER_FUNCTION (want_igmp_events_reaper);
363
364 void
365 send_igmp_event (vl_api_registration_t * rp,
366                  igmp_filter_mode_t filter,
367                  u32 sw_if_index,
368                  const ip46_address_t * saddr, const ip46_address_t * gaddr)
369 {
370   vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
371   clib_memset (mp, 0, sizeof (*mp));
372
373   mp->_vl_msg_id = ntohs ((VL_API_IGMP_EVENT) + igmp_main.msg_id_base);
374   mp->sw_if_index = htonl (sw_if_index);
375   mp->filter = htonl (filter);
376   clib_memcpy (&mp->saddr, &saddr->ip4, sizeof (ip4_address_t));
377   clib_memcpy (&mp->gaddr, &gaddr->ip4, sizeof (ip4_address_t));
378
379   vl_api_send_msg (rp, (u8 *) mp);
380 }
381
382 void
383 igmp_event (igmp_filter_mode_t filter,
384             u32 sw_if_index,
385             const ip46_address_t * saddr, const ip46_address_t * gaddr)
386 {
387   vpe_client_registration_t *api_client;
388   vl_api_registration_t *rp;
389   igmp_main_t *im;
390
391   im = &igmp_main;
392
393   IGMP_DBG ("event: (%U, %U) %U %U",
394             format_ip46_address, saddr, IP46_TYPE_ANY,
395             format_ip46_address, saddr, IP46_TYPE_ANY,
396             format_vnet_sw_if_index_name,
397             vnet_get_main (), sw_if_index, format_igmp_filter_mode, filter);
398
399
400   pool_foreach (api_client, im->api_clients)
401      {
402       rp = vl_api_client_index_to_registration (api_client->client_index);
403       if (rp)
404         send_igmp_event (rp, filter, sw_if_index, saddr, gaddr);
405     }
406 }
407
408 /* Set up the API message handling tables */
409 #include <igmp/igmp.api.c>
410 static clib_error_t *
411 igmp_plugin_api_hookup (vlib_main_t * vm)
412 {
413   igmp_main_t *im = &igmp_main;
414
415   /* Ask for a correctly-sized block of API message decode slots */
416   im->msg_id_base = setup_message_id_table ();
417
418   return 0;
419 }
420
421 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
422
423 /*
424  * fd.io coding-style-patch-verification: ON
425  *
426  * Local Variables:
427  * eval: (c-set-style "gnu")
428  * End:
429  */