FD.io VPP  v20.01-48-g3e0dafb74
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 
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 }
101 
102 igmp_group_t *
104  const igmp_key_t * gkey, igmp_filter_mode_t mode)
105 {
106  igmp_main_t *im = &igmp_main;
107  igmp_group_t *group;
108  u32 ii;
109 
110  IGMP_DBG ("new-group: %U", format_igmp_key, gkey);
111  pool_get (im->groups, group);
112  clib_memset (group, 0, sizeof (igmp_group_t));
113  group->key = clib_mem_alloc (sizeof (igmp_key_t));
114  clib_memcpy (group->key, gkey, sizeof (igmp_key_t));
115  group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE] =
116  hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
117  group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE] =
118  hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
119  group->router_filter_mode = mode;
120  group->config = igmp_config_index (config);
121  group->n_reports_sent = 0;
122 
123  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
124  group->timers[ii] = IGMP_TIMER_ID_INVALID;
125 
126  hash_set_mem (config->igmp_group_by_key, group->key, group - im->groups);
127 
128  /* If interface is in ROUTER mode and IGMP proxy is enabled
129  * add mfib path.
130  */
131  if (config->mode == IGMP_MODE_ROUTER)
132  {
133  igmp_proxy_device_mfib_path_add_del (group, /* add */ 1);
134  }
135 
136  return (group);
137 }
138 
139 /**
140  * the set of present sources minus the new set
141  */
142 ip46_address_t *
145  const ip46_address_t * saddrs)
146 {
147  const ip46_address_t *s1;
148  ip46_address_t *pmn;
149  igmp_src_t *src;
150  u32 found;
151 
152  pmn = NULL;
153 
154  /* *INDENT-OFF* */
155  if (0 == vec_len(saddrs))
156  {
157  FOR_EACH_SRC(src, group, mode,
158  ({
159  vec_add1(pmn, *src->key);
160  }));
161  }
162  else
163  {
164  FOR_EACH_SRC(src, group, mode,
165  ({
166  found = 0;
167  vec_foreach(s1, saddrs)
168  {
169  if (ip46_address_is_equal(s1, src->key))
170  {
171  found = 1;
172  break;
173  }
174  }
175 
176  if (!found)
177  vec_add1(pmn, *src->key);
178  }));
179  }
180  /* *INDENT-ON* */
181 
182  return (pmn);
183 }
184 
185 /**
186  * the set of new sources minus the present set
187  */
188 ip46_address_t *
191  const ip46_address_t * saddrs)
192 {
193  const ip46_address_t *s1;
194  ip46_address_t *npm;
195  igmp_src_t *src;
196  u32 found;
197 
198  npm = NULL;
199 
200  /* *INDENT-OFF* */
201  vec_foreach(s1, saddrs)
202  {
203  found = 0;
204  FOR_EACH_SRC(src, group, mode,
205  ({
206  if (ip46_address_is_equal(s1, src->key))
207  {
208  found = 1;
209  break;
210  }
211  }));
212 
213  if (!found)
214  vec_add1(npm, *s1);
215  }
216  /* *INDENT-ON* */
217 
218  return (npm);
219 }
220 
221 ip46_address_t *
224  const ip46_address_t * saddrs)
225 {
226  ip46_address_t *intersect;
227  const ip46_address_t *s1;
228  igmp_src_t *src;
229 
230  intersect = NULL;
231 
232  /* *INDENT-OFF* */
233  FOR_EACH_SRC(src, group, mode,
234  ({
235  vec_foreach(s1, saddrs)
236  {
237  if (s1->ip4.as_u32 == src->key->ip4.as_u32)
238  {
239  vec_add1(intersect, *s1);
240  break;
241  }
242  }
243  }));
244  /* *INDENT-ON* */
245 
246  return (intersect);
247 }
248 
249 u32
251 {
252  return (hash_elts (group->igmp_src_by_key[mode]));
253 }
254 
255 
256 igmp_src_t *
258 {
259  uword *p;
260  igmp_src_t *src = NULL;
261  if (!group)
262  return NULL;
263 
264  p = hash_get_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE], key);
265  if (p)
266  src = vec_elt_at_index (igmp_main.srcs, p[0]);
267 
268  return src;
269 }
270 
271 u32
273 {
274  return (g - igmp_main.groups);
275 }
276 
277 igmp_group_t *
279 {
280  return (pool_elt_at_index (igmp_main.groups, index));
281 }
282 
283 u8 *
284 format_igmp_group_timer_type (u8 * s, va_list * args)
285 {
287 
288  switch (type)
289  {
290 #define _(v,t) case IGMP_GROUP_TIMER_##v: return (format (s, "%s", t));
292 #undef _
293  }
294  return (s);
295 }
296 
297 u8 *
298 format_igmp_group (u8 * s, va_list * args)
299 {
300  igmp_group_t *group = va_arg (*args, igmp_group_t *);
301  u32 indent = va_arg (*args, u32);
302  igmp_src_t *src;
303  u32 ii;
304 
305  s = format (s, "%U%U",
306  format_white_space, indent, format_igmp_key, group->key);
307 
308  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
309  s = format (s, "\n%U %U:%U", format_white_space, indent,
311  format_igmp_timer_id, group->timers[ii]);
312 
313  /* *INDENT-OFF* */
314  FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
315  ({
316  s = format (s, "\n%U", format_igmp_src, src, indent+4);
317  }));
318  /* *INDENT-ON* */
319 
320  return (s);
321 }
322 
323 /*
324  * fd.io coding-style-patch-verification: ON
325  *
326  * Local Variables:
327  * eval: (c-set-style "gnu")
328  * End:
329  */
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
#define NULL
Definition: clib.h:58
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:523
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:60
#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:237
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:143
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_gre_tunnel_mode_t mode
Definition: gre.api:55
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:519
#define hash_unset_mem(h, key)
Definition: hash.h:291
vl_api_ip4_address_t saddrs[n_srcs]
Definition: igmp.api:71
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:257
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:287
collection of data related to IGMP
Definition: igmp.h:63
static u8 ip46_address_is_equal(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:93
igmp_group_t * igmp_group_alloc(igmp_config_t *config, const igmp_key_t *gkey, igmp_filter_mode_t mode)
Definition: igmp_group.c:103
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:298
u8 * format_igmp_group_timer_type(u8 *s, va_list *args)
Definition: igmp_group.c:284
enum igmp_filter_mode_t_ igmp_filter_mode_t
#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:278
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:222
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
typedef key
Definition: ipsec_types.api:83
igmp_group_t * groups
pool of groups
Definition: igmp.h:100
#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
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:189
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:250
void igmp_group_clear(igmp_group_t *group)
Definition: igmp_group.c:68
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:272
IGMP source The representation of a specified source address with in multicast group.
Definition: igmp_src.h:41