FD.io VPP  v19.08.2-294-g37e99c22d
Vector Packet Processing
igmp_group.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 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_group.h>
19 #include <igmp/igmp.h>
20 
21 void
23 {
24  igmp_src_t *src;
25 
26  /* *INDENT-OFF* */
27  FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
28  ({
29  igmp_src_free(src);
30  }));
31  /* *INDENT-ON* */
32 
33  hash_free (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE]);
34  hash_free (group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE]);
35 }
36 
37 void
39 {
40  hash_unset_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE], src->key);
41  hash_unset_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE], src->key);
42 }
43 
44 igmp_src_t *
46  const igmp_key_t * skey, igmp_mode_t mode)
47 {
48  igmp_src_t *src;
49 
50  src = igmp_src_lookup (group, skey);
51 
52  if (NULL == src)
53  {
54  src = igmp_src_alloc (igmp_group_index (group), skey, mode);
55 
56  hash_set_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE],
57  src->key, igmp_src_index (src));
58  }
59  else
60  {
61  igmp_src_refresh (src);
62  }
63 
64  return (src);
65 }
66 
67 void
69 {
70  igmp_config_t *config;
71  u32 ii;
72 
73  ASSERT (*group);
74 
75  config = igmp_config_get ((*group)->config);
76 
77  /* If interface is in ROUTER mode and IGMP proxy is enabled
78  * remove mfib path.
79  */
80  if (config->mode == IGMP_MODE_ROUTER)
81  {
82  igmp_proxy_device_mfib_path_add_del (*group, /* add */ 0);
83  }
84 
85  IGMP_DBG ("clear-group: %U %U",
86  format_igmp_key, (*group)->key,
88  vnet_get_main (), config->sw_if_index);
89 
90  igmp_group_free_all_srcs (*group);
91 
92  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
93  {
94  igmp_timer_retire (&(*group)->timers[ii]);
95  }
96 
97  hash_unset_mem (config->igmp_group_by_key, (*group)->key);
98  clib_mem_free ((*group)->key);
99  pool_put (igmp_main.groups, *group);
100  *group = 0;
101 }
102 
103 igmp_group_t *
105  const igmp_key_t * gkey, igmp_filter_mode_t mode)
106 {
107  igmp_main_t *im = &igmp_main;
108  igmp_group_t *group;
109  u32 ii;
110 
111  IGMP_DBG ("new-group: %U", format_igmp_key, gkey);
112  pool_get (im->groups, group);
113  clib_memset (group, 0, sizeof (igmp_group_t));
114  group->key = clib_mem_alloc (sizeof (igmp_key_t));
115  clib_memcpy (group->key, gkey, sizeof (igmp_key_t));
116  group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE] =
117  hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
118  group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE] =
119  hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
120  group->router_filter_mode = mode;
121  group->config = igmp_config_index (config);
122  group->n_reports_sent = 0;
123 
124  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
125  group->timers[ii] = IGMP_TIMER_ID_INVALID;
126 
127  hash_set_mem (config->igmp_group_by_key, group->key, group - im->groups);
128 
129  /* If interface is in ROUTER mode and IGMP proxy is enabled
130  * add mfib path.
131  */
132  if (config->mode == IGMP_MODE_ROUTER)
133  {
134  igmp_proxy_device_mfib_path_add_del (group, /* add */ 1);
135  }
136 
137  return (group);
138 }
139 
140 /**
141  * the set of present sources minus the new set
142  */
143 ip46_address_t *
146  const ip46_address_t * saddrs)
147 {
148  const ip46_address_t *s1;
149  ip46_address_t *pmn;
150  igmp_src_t *src;
151  u32 found;
152 
153  pmn = NULL;
154 
155  /* *INDENT-OFF* */
156  if (0 == vec_len(saddrs))
157  {
158  FOR_EACH_SRC(src, group, mode,
159  ({
160  vec_add1(pmn, *src->key);
161  }));
162  }
163  else
164  {
165  FOR_EACH_SRC(src, group, mode,
166  ({
167  found = 0;
168  vec_foreach(s1, saddrs)
169  {
170  if (ip46_address_is_equal(s1, src->key))
171  {
172  found = 1;
173  break;
174  }
175  }
176 
177  if (!found)
178  vec_add1(pmn, *src->key);
179  }));
180  }
181  /* *INDENT-ON* */
182 
183  return (pmn);
184 }
185 
186 /**
187  * the set of new sources minus the present set
188  */
189 ip46_address_t *
192  const ip46_address_t * saddrs)
193 {
194  const ip46_address_t *s1;
195  ip46_address_t *npm;
196  igmp_src_t *src;
197  u32 found;
198 
199  npm = NULL;
200 
201  /* *INDENT-OFF* */
202  vec_foreach(s1, saddrs)
203  {
204  found = 0;
205  FOR_EACH_SRC(src, group, mode,
206  ({
207  if (ip46_address_is_equal(s1, src->key))
208  {
209  found = 1;
210  break;
211  }
212  }));
213 
214  if (!found)
215  vec_add1(npm, *s1);
216  }
217  /* *INDENT-ON* */
218 
219  return (npm);
220 }
221 
222 ip46_address_t *
225  const ip46_address_t * saddrs)
226 {
227  ip46_address_t *intersect;
228  const ip46_address_t *s1;
229  igmp_src_t *src;
230 
231  intersect = NULL;
232 
233  /* *INDENT-OFF* */
234  FOR_EACH_SRC(src, group, mode,
235  ({
236  vec_foreach(s1, saddrs)
237  {
238  if (s1->ip4.as_u32 == src->key->ip4.as_u32)
239  {
240  vec_add1(intersect, *s1);
241  break;
242  }
243  }
244  }));
245  /* *INDENT-ON* */
246 
247  return (intersect);
248 }
249 
250 u32
252 {
253  return (hash_elts (group->igmp_src_by_key[mode]));
254 }
255 
256 
257 igmp_src_t *
259 {
260  uword *p;
261  igmp_src_t *src = NULL;
262  if (!group)
263  return NULL;
264 
265  p = hash_get_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE], key);
266  if (p)
267  src = vec_elt_at_index (igmp_main.srcs, p[0]);
268 
269  return src;
270 }
271 
272 u32
274 {
275  return (g - igmp_main.groups);
276 }
277 
278 igmp_group_t *
280 {
281  return (pool_elt_at_index (igmp_main.groups, index));
282 }
283 
284 u8 *
285 format_igmp_group_timer_type (u8 * s, va_list * args)
286 {
288 
289  switch (type)
290  {
291 #define _(v,t) case IGMP_GROUP_TIMER_##v: return (format (s, "%s", t));
293 #undef _
294  }
295  return (s);
296 }
297 
298 u8 *
299 format_igmp_group (u8 * s, va_list * args)
300 {
301  igmp_group_t *group = va_arg (*args, igmp_group_t *);
302  u32 indent = va_arg (*args, u32);
303  igmp_src_t *src;
304  u32 ii;
305 
306  s = format (s, "%U%U",
307  format_white_space, indent, format_igmp_key, group->key);
308 
309  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
310  s = format (s, "\n%U %U:%U", format_white_space, indent,
312  format_igmp_timer_id, group->timers[ii]);
313 
314  /* *INDENT-OFF* */
315  FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
316  ({
317  s = format (s, "\n%U", format_igmp_src, src, indent+4);
318  }));
319  /* *INDENT-ON* */
320 
321  return (s);
322 }
323 
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "gnu")
329  * End:
330  */
u8 * format_igmp_key(u8 *s, va_list *args)
Definition: igmp_format.c:203
void igmp_proxy_device_mfib_path_add_del(igmp_group_t *group, u8 add)
Definition: igmp_proxy.c:27
void igmp_timer_retire(igmp_timer_id_t *tid)
Definition: igmp_timer.c:222
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
igmp_main_t igmp_main
Definition: igmp.c:36
void igmp_group_free_all_srcs(igmp_group_t *group)
Definition: igmp_group.c:22
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
igmp_src_t * igmp_group_src_update(igmp_group_t *group, const igmp_key_t *skey, igmp_mode_t mode)
Definition: igmp_group.c:45
vl_api_address_t src
Definition: gre.api:51
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
enum igmp_group_timer_type_t_ igmp_group_timer_type_t
Types of timers maintained for each group.
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
void igmp_src_free(igmp_src_t *src)
Definition: igmp_src.c:23
#define IGMP_DBG(...)
Definition: igmp.h:38
void igmp_src_refresh(igmp_src_t *src)
Definition: igmp_src.c:117
#define clib_memcpy(d, s, n)
Definition: string.h:180
u32 n_reports_sent
The number of times the last report has been sent.
Definition: igmp_group.h:82
ip46_address_t * igmp_group_present_minus_new(igmp_group_t *group, igmp_filter_mode_t mode, const ip46_address_t *saddrs)
the set of present sources minus the new set
Definition: igmp_group.c:144
u32 igmp_config_index(const igmp_config_t *c)
Get the pool index for a config.
Definition: igmp_config.c:64
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
igmp_src_t * igmp_src_alloc(u32 group_index, const igmp_key_t *skey, igmp_mode_t mode)
Definition: igmp_src.c:73
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
igmp_config_t * igmp_config_get(u32 index)
Get the config from the pool index.
Definition: igmp_config.c:70
unsigned int u32
Definition: types.h:88
uword * igmp_group_by_key
Database of groups joined on the link.
Definition: igmp_config.h:72
vl_api_fib_path_type_t type
Definition: fib_types.api:123
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:661
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
#define hash_unset_mem(h, key)
Definition: hash.h:291
u8 * format_igmp_src(u8 *s, va_list *args)
Definition: igmp_src.c:147
u32 config
The pool index of the config object this group is in.
Definition: igmp_group.h:77
void igmp_group_src_remove(igmp_group_t *group, igmp_src_t *src)
Definition: igmp_group.c:38
#define hash_free(h)
Definition: hash.h:310
igmp_src_t * igmp_src_lookup(igmp_group_t *group, const igmp_key_t *key)
igmp group lookup
Definition: igmp_group.c:258
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
void igmp_group_clear(igmp_group_t **group)
Definition: igmp_group.c:68
collection of data related to IGMP
Definition: igmp.h:63
igmp_group_t * igmp_group_alloc(igmp_config_t *config, const igmp_key_t *gkey, igmp_filter_mode_t mode)
Definition: igmp_group.c:104
u32 timers[IGMP_GROUP_N_TIMERS]
A vector of running timers for the group.
Definition: igmp_group.h:67
u8 * format_igmp_group(u8 *s, va_list *args)
Definition: igmp_group.c:299
u8 * format_igmp_group_timer_type(u8 *s, va_list *args)
Definition: igmp_group.c:285
enum igmp_filter_mode_t_ igmp_filter_mode_t
vl_api_vxlan_gbp_api_tunnel_mode_t mode
Definition: vxlan_gbp.api:44
#define IGMP_TIMER_ID_INVALID
Definition: igmp_timer.h:28
#define IGMP_GROUP_N_TIMERS
Definition: igmp_group.h:48
static uword hash_elts(void *v)
Definition: hash.h:118
#define ASSERT(truth)
igmp_group_t * igmp_group_get(u32 index)
Definition: igmp_group.c:279
u32 igmp_src_index(igmp_src_t *src)
Definition: igmp_src.c:141
static void clib_mem_free(void *p)
Definition: mem.h:226
ip46_address_t igmp_key_t
IGMP Key Used to index groups within an interface config and sources within a list.
Definition: igmp_types.h:49
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
ip46_address_t * igmp_group_new_intersect_present(igmp_group_t *group, igmp_filter_mode_t mode, const ip46_address_t *saddrs)
Definition: igmp_group.c:223
igmp_filter_mode_t router_filter_mode
The current filter mode of the group (see 6.2.1)
Definition: igmp_group.h:72
IGMP interface configuration.
Definition: igmp_config.h:47
u8 * format_igmp_timer_id(u8 *s, va_list *args)
Definition: igmp_timer.c:236
igmp_group_t * groups
pool of groups
Definition: igmp.h:100
#define ip46_address_is_equal(a1, a2)
Definition: ip6_packet.h:94
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define FOR_EACH_SRC(_src, _group, _filter, _body)
Definition: igmp_group.h:90
igmp_key_t * key
The group&#39;s key within the per-interface config.
Definition: igmp_group.h:59
IGMP group A multicast group address for which reception has been requested.
Definition: igmp_group.h:56
u64 uword
Definition: types.h:112
typedef key
Definition: ipsec.api:247
igmp_key_t * key
The source&#39;s key.
Definition: igmp_src.h:46
#define hash_get_mem(h, key)
Definition: hash.h:269
enum igmp_mode_t_ igmp_mode_t
#define vec_foreach(var, vec)
Vector iterator.
ip46_address_t * igmp_group_new_minus_present(igmp_group_t *group, igmp_filter_mode_t mode, const ip46_address_t *saddrs)
the set of new sources minus the present set
Definition: igmp_group.c:190
igmp_mode_t mode
Definition: igmp_config.h:62
u32 igmp_group_n_srcs(const igmp_group_t *group, igmp_filter_mode_t mode)
Definition: igmp_group.c:251
igmp_src_t * srcs
pool of sources
Definition: igmp.h:104
uword * igmp_src_by_key[IGMP_N_FILTER_MODES]
Source list per-filter mode.
Definition: igmp_group.h:87
u32 igmp_group_index(const igmp_group_t *g)
Definition: igmp_group.c:273
IGMP source The representation of a specified source address with in multicast group.
Definition: igmp_src.h:41