igmp: data structure refactoring
[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 = igmp_listen (vm, mp->enable, ntohl (mp->sw_if_index), saddr, gaddr, 1);
83
84 done:;
85   unix_shared_memory_queue_t *q =
86     vl_api_client_index_to_input_queue (mp->client_index);
87   if (!q)
88     return;
89
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);
94
95   vl_msg_api_send_shmem (q, (u8 *) & rmp);
96 }
97
98 static void
99 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
100 {
101   vl_api_igmp_enable_disable_reply_t *rmp;
102   igmp_main_t *im = &igmp_main;
103   int rv = 0;
104
105   REPLY_MACRO (VL_API_IGMP_ENABLE_DISABLE_REPLY + im->msg_id_base);
106 }
107
108 static void
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)
112 {
113   vl_api_igmp_details_t *mp;
114
115   mp = vl_msg_api_alloc (sizeof (*mp));
116   memset (mp, 0, sizeof (*mp));
117
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);
123
124   vl_msg_api_send_shmem (q, (u8 *) & mp);
125 }
126
127 static void
128 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
129 {
130   igmp_main_t *im = &igmp_main;
131   igmp_config_t *config;
132   igmp_group_t *group;
133   igmp_src_t *src;
134
135   unix_shared_memory_queue_t *q =
136     vl_api_client_index_to_input_queue (mp->client_index);
137   if (!q)
138     return;
139
140   if (mp->dump_all)
141     {
142       /* *INDENT-OFF* */
143       pool_foreach (config, im->configs, (
144         {
145             pool_foreach (group, config->groups, (
146               {
147                 pool_foreach (src, group->srcs, (
148                   {
149                     send_igmp_details (q, im, config, group, src, mp->context);
150                   }));
151               }));
152         }));
153       /* *INDENT-ON* */
154       return;
155     }
156   config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
157   if (config)
158     {
159       /* *INDENT-OFF* */
160       pool_foreach (group, config->groups, (
161         {
162           pool_foreach (src, group->srcs, (
163             {
164               send_igmp_details (q, im, config, group, src, mp->context);
165             }));
166         }));
167       /* *INDENT-ON* */
168     }
169 }
170
171 static void
172 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
173 {
174   igmp_main_t *im = &igmp_main;
175   igmp_config_t *config;
176   vl_api_igmp_clear_interface_reply_t *rmp;
177   int rv = 0;
178
179   config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
180   if (config)
181     igmp_clear_config (config);
182
183   unix_shared_memory_queue_t *q =
184     vl_api_client_index_to_input_queue (mp->client_index);
185   if (!q)
186     return;
187
188   rmp = vl_msg_api_alloc (sizeof (*rmp));
189   rmp->_vl_msg_id =
190     htons ((VL_API_IGMP_CLEAR_INTERFACE_REPLY) + im->msg_id_base);
191   rmp->context = mp->context;
192   rmp->retval = htonl (rv);
193
194   vl_msg_api_send_shmem (q, (u8 *) & rmp);
195 }
196
197 static void
198 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
199 {
200   igmp_main_t *im = &igmp_main;
201   igmp_api_client_t *api_client;
202   vl_api_want_igmp_events_reply_t *rmp;
203   int rv = 0;
204
205   api_client = igmp_api_client_lookup (im, mp->client_index);
206   if (api_client)
207     {
208       if (mp->enable)
209         {
210           rv = VNET_API_ERROR_INVALID_REGISTRATION;
211           goto done;
212         }
213       hash_unset_mem (im->igmp_api_client_by_client_index,
214                       &api_client->client_index);
215       pool_put (im->api_clients, api_client);
216       goto done;
217     }
218   if (mp->enable)
219     {
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);
226       goto done;
227     }
228   rv = VNET_API_ERROR_INVALID_REGISTRATION;
229
230 done:;
231   unix_shared_memory_queue_t *q =
232     vl_api_client_index_to_input_queue (mp->client_index);
233   if (!q)
234     return;
235
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);
240
241   vl_msg_api_send_shmem (q, (u8 *) & rmp);
242 }
243
244 void
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)
248 {
249   vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
250   memset (mp, 0, sizeof (*mp));
251
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));
257   mp->is_join =
258     (group->type == IGMP_MEMBERSHIP_GROUP_mode_is_filter_include) ? 1 : 0;
259
260   vl_msg_api_send_shmem (q, (u8 *) & mp);
261 }
262
263 void
264 igmp_event (igmp_main_t * im, igmp_config_t * config, igmp_group_t * group,
265             igmp_src_t * src)
266 {
267   igmp_api_client_t *api_client;
268   unix_shared_memory_queue_t *q;
269   /* *INDENT-OFF* */
270   pool_foreach (api_client, im->api_clients,
271     ({
272       q = vl_api_client_index_to_input_queue (api_client->client_index);
273       if (q)
274         send_igmp_event (q, 0, im, config, group, src);
275     }));
276   /* *INDENT-ON* */
277   if (group->type == IGMP_MEMBERSHIP_GROUP_block_old_sources)
278     {
279       igmp_clear_group (config, group);
280       if (pool_elts (config->groups) == 0)
281         {
282           hash_unset_mem (im->igmp_config_by_sw_if_index,
283                           &config->sw_if_index);
284           pool_put (im->configs, config);
285         }
286     }
287 }
288
289 #define vl_msg_name_crc_list
290 #include <igmp/igmp_all_api_h.h>
291 #undef vl_msg_name_crc_list
292
293 static void
294 setup_message_id_table (igmp_main_t * im, api_main_t * am)
295 {
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;
299 #undef _
300 }
301
302 /* Set up the API message handling tables */
303 static clib_error_t *
304 igmp_plugin_api_hookup (vlib_main_t * vm)
305 {
306   igmp_main_t *im = &igmp_main;
307   api_main_t *am = &api_main;
308   u8 *name;
309
310   /* Construct the API name */
311   name = format (0, "igmp_%08x%c", api_version, 0);
312
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);
316
317 #define _(N,n)                                                  \
318     vl_msg_api_set_handlers((VL_API_##N + im->msg_id_base),     \
319                            #n,                                  \
320                            vl_api_##n##_t_handler,              \
321                            vl_noop_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;
326 #undef _
327
328   /*
329    * Set up the (msg_name, crc, message-id) table
330    */
331   setup_message_id_table (im, am);
332
333   vec_free (name);
334   return 0;
335 }
336
337 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
338
339 /*
340  * fd.io coding-style-patch-verification: ON
341  *
342  * Local Variables:
343  * eval: (c-set-style "gnu")
344  * End:
345  */