aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/igmp/igmp_api.c
blob: 164779c3e5ae0c52c6fba44a488409eb24e43e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <igmp/igmp.h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>

/* define message IDs */
#include <igmp/igmp_msg_enum.h>

/* define message structures */
#define vl_typedefs
#include <igmp/igmp_all_api_h.h>
#undef vl_typedefs

/* define generated endian-swappers */
#define vl_endianfun
#include <igmp/igmp_all_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include <igmp/igmp_all_api_h.h>
#undef vl_printfun

/* Get the API version number */
#define vl_api_version(n,v) static u32 api_version=(v);
#include <igmp/igmp_all_api_h.h>
#undef vl_api_version

#include <vlibapi/api_helper_macros.h>

#define foreach_igmp_plugin_api_msg                      \
_(IGMP_LISTEN, igmp_listen)                              \
_(IGMP_ENABLE_DISABLE, igmp_enable_disable)              \
_(IGMP_DUMP, igmp_dump)					 \
_(IGMP_CLEAR_INTERFACE, igmp_clear_interface)		 \
_(WANT_IGMP_EVENTS, want_igmp_events)			 \

static void
vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vnet_main_t *vnm = vnet_get_main ();
  igmp_main_t *im = &igmp_main;
  vl_api_igmp_listen_reply_t *rmp;
  int rv = 0;
  ip46_address_t saddr, gaddr;

  if (!vnet_sw_interface_is_api_valid (vnm, ntohl (mp->sw_if_index)))
    {
      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
      goto done;
    }

  if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->sw_if_index)) &&
       VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
    {
      rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
      goto done;
    }

  clib_memcpy (&saddr.ip4.as_u8, mp->saddr, sizeof (u8) * 4);
  clib_memcpy (&gaddr.ip4.as_u8, mp->gaddr, sizeof (u8) * 4);

  rv = igmp_listen (vm, mp->enable, ntohl (mp->sw_if_index), saddr, gaddr, 1);

done:;
  unix_shared_memory_queue_t *q =
    vl_api_client_index_to_input_queue (mp->client_index);
  if (!q)
    return;

  rmp = vl_msg_api_alloc (sizeof (*rmp));
  rmp->_vl_msg_id = htons ((VL_API_IGMP_LISTEN_REPLY) + im->msg_id_base);
  rmp->context = mp->context;
  rmp->retval = htonl (rv);

  vl_msg_api_send_shmem (q, (u8 *) & rmp);
}

static void
vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
{
  vl_api_igmp_enable_disable_reply_t *rmp;
  igmp_main_t *im = &igmp_main;
  int rv = 0;

  REPLY_MACRO (VL_API_IGMP_ENABLE_DISABLE_REPLY + im->msg_id_base);
}

static void
send_igmp_details (unix_shared_memory_queue_t * q, igmp_main_t * im,
		   igmp_config_t * config, igmp_group_t * group,
		   igmp_src_t * src, u32 context)
{
  vl_api_igmp_details_t *mp;

  mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));

  mp->_vl_msg_id = htons (VL_API_IGMP_DETAILS + im->msg_id_base);
  mp->context = context;
  mp->sw_if_index = htonl (config->sw_if_index);
  clib_memcpy (mp->saddr, &src->addr.ip4, sizeof (u8) * 4);
  clib_memcpy (mp->gaddr, &group->addr.ip4, sizeof (u8) * 4);

  vl_msg_api_send_shmem (q, (u8 *) & mp);
}

static void
vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
{
  igmp_main_t *im = &igmp_main;
  igmp_config_t *config;
  igmp_group_t *group;
  igmp_src_t *src;

  unix_shared_memory_queue_t *q =
    vl_api_client_index_to_input_queue (mp->client_index);
  if (!q)
    return;

  if (mp->dump_all)
    {
      /* *INDENT-OFF* */
      pool_foreach (config, im->configs, (
        {
	    pool_foreach (group, config->groups, (
	      {
		pool_foreach (src, group->srcs, (
		  {
		    send_igmp_details (q, im, config, group, src, mp->context);
		  }));
	      }));
        }));
      /* *INDENT-ON* */
      return;
    }
  config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
  if (config)
    {
      /* *INDENT-OFF* */
      pool_foreach (group, config->groups, (
	{
	  pool_foreach (src, group->srcs, (
	    {
	      send_igmp_details (q, im, config, group, src, mp->context);
	    }));
	}));
      /* *INDENT-ON* */
    }
}

static void
vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
{
  igmp_main_t *im = &igmp_main;
  igmp_config_t *config;
  vl_api_igmp_clear_interface_reply_t *rmp;
  int rv = 0;

  config = igmp_config_lookup (im, ntohl (mp->sw_if_index));
  if (config)
    igmp_clear_config (config);

  unix_shared_memory_queue_t *q =
    vl_api_client_index_to_input_queue (mp->client_index);
  if (!q)
    return;

  rmp = vl_msg_api_alloc (sizeof (*rmp));
  rmp->_vl_msg_id =
    htons ((VL_API_IGMP_CLEAR_INTERFACE_REPLY) + im->msg_id_base);
  rmp->context = mp->context;
  rmp->retval = htonl (rv);

  vl_msg_api_send_shmem (q, (u8 *) & rmp);
}

/** \brief igmp group lookup
    @param im - igmp main
    @param client_index - client index
*/
static vpe_client_registration_t *
igmp_api_client_lookup (igmp_main_t * im, u32 client_index)
{
  uword *p;
  vpe_client_registration_t *api_client = NULL;

  p = hash_get_mem (im->igmp_api_client_by_client_index, &client_index);
  if (p)
    api_client = vec_elt_at_index (im->api_clients, p[0]);

  return api_client;
}

static void
vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
{
  igmp_main_t *im = &igmp_main;
  vpe_client_registration_t *api_client;
  vl_api_want_igmp_events_reply_t *rmp;
  int rv = 0;

  api_client = igmp_api_client_lookup (im, mp->client_index);
  if (api_client)
    {
      if (mp->enable)
	{
	  rv = VNET_API_ERROR_INVALID_REGISTRATION;
	  goto done;
	}
      hash_unset_mem (im->igmp_api_client_by_client_index,
		      &api_client->client_index);
      pool_put (im->api_clients, api_client);
      goto done;
    }
  if (mp->enable)
    {
      pool_get (im->api_clients, api_client);
      memset (api_client, 0, sizeof (vpe_client_registration_t));
      api_client->client_index = mp->client_index;
      api_client->client_pid = mp->pid;
      hash_set_mem (im->igmp_api_client_by_client_index,
		    &mp->client_index, api_client - im->api_clients);
      goto done;
    }
  rv = VNET_API_ERROR_INVALID_REGISTRATION;

done:;
  unix_shared_memory_queue_t *q =
    vl_api_client_index_to_input_queue (mp->client_index);
  if (!q)
    return;

  rmp = vl_msg_api_alloc (sizeof (*rmp));
  rmp->_vl_msg_id = htons ((VL_API_WANT_IGMP_EVENTS_REPLY) + im->msg_id_base);
  rmp->context = mp->context;
  rmp->retval = htonl (rv);

  vl_msg_api_send_shmem (q, (u8 *) & rmp);
}

static clib_error_t *
want_igmp_events_reaper (u32 client_index)
{
  igmp_main_t *im = &igmp_main;
  vpe_client_registration_t *api_client;
  uword *p;

  p = hash_get_mem (im->igmp_api_client_by_client_index, &client_index);

  if (p)
    {
      api_client = pool_elt_at_index (im->api_clients, p[0]);
      pool_put (im->api_clients, api_client);
      hash_unset_mem (im->igmp_api_client_by_client_index, &client_index);
    }
  return (NULL);
}

VL_MSG_API_REAPER_FUNCTION (want_igmp_events_reaper);

void
send_igmp_event (unix_shared_memory_queue_t * q, u32 context,
		 igmp_main_t * im, igmp_config_t * config,
		 igmp_group_t * group, igmp_src_t * src)
{
  vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));

  mp->_vl_msg_id = ntohs ((VL_API_IGMP_EVENT) + im->msg_id_base);
  mp->context = context;
  mp->sw_if_index = htonl (config->sw_if_index);
  clib_memcpy (&mp->saddr, &src->addr.ip4, sizeof (ip4_address_t));
  clib_memcpy (&mp->gaddr, &group->addr.ip4, sizeof (ip4_address_t));
  mp->is_join =
    (group->type == IGMP_MEMBERSHIP_GROUP_mode_is_filter_include) ? 1 : 0;

  vl_msg_api_send_shmem (q, (u8 *) & mp);
}

void
igmp_event (igmp_main_t * im, igmp_config_t * config, igmp_group_t * group,
	    igmp_src_t * src)
{
  vpe_client_registration_t *api_client;
  unix_shared_memory_queue_t *q;
  /* *INDENT-OFF* */
  pool_foreach (api_client, im->api_clients,
    ({
      q = vl_api_client_index_to_input_queue (api_client->client_index);
      if (q)
	send_igmp_event (q, 0, im, config, group, src);
    }));
  /* *INDENT-ON* */
  if (group->type == IGMP_MEMBERSHIP_GROUP_block_old_sources)
    {
      igmp_clear_group (config, group);
      if (pool_elts (config->groups) == 0)
	{
	  hash_unset_mem (im->igmp_config_by_sw_if_index,
			  &config->sw_if_index);
	  pool_put (im->configs, config);
	}
    }
}

#define vl_msg_name_crc_list
#include <igmp/igmp_all_api_h.h>
#undef vl_msg_name_crc_list

static void
setup_message_id_table (igmp_main_t * im, api_main_t * am)
{
#define _(id,n,crc) \
  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + im->msg_id_base);
  foreach_vl_msg_name_crc_igmp;
#undef _
}

/* Set up the API message handling tables */
static clib_error_t *
igmp_plugin_api_hookup (vlib_main_t * vm)
{
  igmp_main_t *im = &igmp_main;
  api_main_t *am = &api_main;
  u8 *name;

  /* Construct the API name */
  name = format (0, "igmp_%08x%c", api_version, 0);

  /* Ask for a correctly-sized block of API message decode slots */
  im->msg_id_base = vl_msg_api_get_msg_ids
    ((char *) name, VL_MSG_FIRST_AVAILABLE);

#define _(N,n)                                                  \
    vl_msg_api_set_handlers((VL_API_##N + im->msg_id_base),     \
                           #n,                                  \
                           vl_api_##n##_t_handler,              \
                           vl_noop_handler,                     \
                           vl_api_##n##_t_endian,               \
                           vl_api_##n##_t_print,                \
                           sizeof(vl_api_##n##_t), 1);
  foreach_igmp_plugin_api_msg;
#undef _

  /*
   * Set up the (msg_name, crc, message-id) table
   */
  setup_message_id_table (im, am);

  vec_free (name);
  return 0;
}

VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */