FD.io VPP  v18.07.1-13-g909ba93
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  IGMP_DBG ("clear-group: %U %U",
78  format_igmp_key, group->key,
80  vnet_get_main (), config->sw_if_index);
81 
83 
84  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
85  {
86  igmp_timer_retire (&group->timers[ii]);
87  }
88 
89  hash_unset_mem (config->igmp_group_by_key, group->key);
90  clib_mem_free (group->key);
91  pool_put (igmp_main.groups, group);
92 }
93 
96  const igmp_key_t * gkey, igmp_filter_mode_t mode)
97 {
98  igmp_main_t *im = &igmp_main;
99  igmp_group_t *group;
100  u32 ii;
101 
102  IGMP_DBG ("new-group: %U", format_igmp_key, gkey);
103  pool_get (im->groups, group);
104  memset (group, 0, sizeof (igmp_group_t));
105  group->key = clib_mem_alloc (sizeof (igmp_key_t));
106  clib_memcpy (group->key, gkey, sizeof (igmp_key_t));
107  group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE] =
108  hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
109  group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE] =
110  hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
111  group->router_filter_mode = mode;
112  group->config = igmp_config_index (config);
113  group->n_reports_sent = 0;
114 
115  for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
116  group->timers[ii] = IGMP_TIMER_ID_INVALID;
117 
118  hash_set_mem (config->igmp_group_by_key, group->key, group - im->groups);
119  return (group);
120 }
121 
122 /**
123  * the set of present sources minus the new set
124  */
125 ip46_address_t *
127  igmp_filter_mode_t mode,
128  const ip46_address_t * saddrs)
129 {
130  const ip46_address_t *s1;
131  ip46_address_t *pmn;
132  igmp_src_t *src;
133  u32 found;
134 
135  pmn = NULL;
136 
137  /* *INDENT-OFF* */
138  if (0 == vec_len(saddrs))
139  {
140  FOR_EACH_SRC(src, group, mode,
141  ({
142  vec_add1(pmn, *src->key);
143  }));
144  }
145  else
146  {
147  FOR_EACH_SRC(src, group, mode,
148  ({
149  found = 0;
150  vec_foreach(s1, saddrs)
151  {
152  if (ip46_address_is_equal(s1, src->key))
153  {
154  found = 1;
155  break;
156  }
157  }
158 
159  if (!found)
160  vec_add1(pmn, *src->key);
161  }));
162  }
163  /* *INDENT-ON* */
164 
165  return (pmn);
166 }
167 
168 /**
169  * the set of new sources minus the present set
170  */
171 ip46_address_t *
173  igmp_filter_mode_t mode,
174  const ip46_address_t * saddrs)
175 {
176  const ip46_address_t *s1;
177  ip46_address_t *npm;
178  igmp_src_t *src;
179  u32 found;
180 
181  npm = NULL;
182 
183  /* *INDENT-OFF* */
184  vec_foreach(s1, saddrs)
185  {
186  found = 0;
187  FOR_EACH_SRC(src, group, mode,
188  ({
189  if (ip46_address_is_equal(s1, src->key))
190  {
191  found = 1;
192  break;
193  }
194  }));
195 
196  if (!found)
197  vec_add1(npm, *s1);
198  }
199  /* *INDENT-ON* */
200 
201  return (npm);
202 }
203 
204 ip46_address_t *
206  igmp_filter_mode_t mode,
207  const ip46_address_t * saddrs)
208 {
209  ip46_address_t *intersect;
210  const ip46_address_t *s1;
211  igmp_src_t *src;
212 
213  intersect = NULL;
214 
215  /* *INDENT-OFF* */
216  FOR_EACH_SRC(src, group, mode,
217  ({
218  vec_foreach(s1, saddrs)
219  {
220  if (s1->ip4.as_u32 == src->key->ip4.as_u32)
221  {
222  vec_add1(intersect, *s1);
223  break;
224  }
225  }
226  }));
227  /* *INDENT-ON* */
228 
229  return (intersect);
230 }
231 
232 u32
234 {
235  return (hash_elts (group->igmp_src_by_key[mode]));
236 }
237 
238 
239 igmp_src_t *
241 {
242  uword *p;
243  igmp_src_t *src = NULL;
244  if (!group)
245  return NULL;
246 
247  p = hash_get_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE], key);
248  if (p)
249  src = vec_elt_at_index (igmp_main.srcs, p[0]);
250 
251  return src;
252 }
253 
254 u32
256 {
257  return (g - igmp_main.groups);
258 }
259 
260 igmp_group_t *
262 {
263  return (pool_elt_at_index (igmp_main.groups, index));
264 }
265 
266 /*
267  * fd.io coding-style-patch-verification: ON
268  *
269  * Local Variables:
270  * eval: (c-set-style "gnu")
271  * End:
272  */
u8 * format_igmp_key(u8 *s, va_list *args)
Definition: igmp_format.c:188
void igmp_timer_retire(igmp_timer_id_t *tid)
Definition: igmp_timer.c:222
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
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:55
#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
#define hash_set_mem(h, key, value)
Definition: hash.h:275
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:228
format_function_t format_vnet_sw_if_index_name
void igmp_src_free(igmp_src_t *src)
Definition: igmp_src.c:23
#define IGMP_DBG(...)
Definition: igmp.h:37
void igmp_src_refresh(igmp_src_t *src)
Definition: igmp_src.c:115
u32 n_reports_sent
The number of times the last report has been sent.
Definition: igmp_group.h:81
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:126
u32 igmp_config_index(const igmp_config_t *c)
Get the pool index for a config.
Definition: igmp_config.c:64
igmp_src_t * igmp_src_alloc(u32 group_index, const igmp_key_t *skey, igmp_mode_t mode)
Definition: igmp_src.c:71
#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:68
#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:464
#define hash_unset_mem(h, key)
Definition: hash.h:291
u32 config
The pool index of the config object this group is in.
Definition: igmp_group.h:76
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:240
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:274
collection of data related to IGMP
Definition: igmp.h:62
igmp_group_t * igmp_group_alloc(igmp_config_t *config, const igmp_key_t *gkey, igmp_filter_mode_t mode)
Definition: igmp_group.c:95
u32 timers[IGMP_GROUP_N_TIMERS]
A vector of running timers for the group.
Definition: igmp_group.h:66
#define clib_memcpy(a, b, c)
Definition: string.h:75
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:49
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:261
u32 igmp_src_index(igmp_src_t *src)
Definition: igmp_src.c:139
static void clib_mem_free(void *p)
Definition: mem.h:179
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:112
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:205
igmp_filter_mode_t router_filter_mode
The current filter mode of the group (see 6.2.1)
Definition: igmp_group.h:71
IGMP interface configuration.
Definition: igmp_config.h:43
igmp_group_t * groups
pool of groups
Definition: igmp.h:99
#define ip46_address_is_equal(a1, a2)
Definition: ip6_packet.h:87
#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:89
igmp_key_t * key
The group&#39;s key within the per-interface config.
Definition: igmp_group.h:58
IGMP group A multicast group address for which reception has been requested.
Definition: igmp_group.h:55
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:172
u32 igmp_group_n_srcs(const igmp_group_t *group, igmp_filter_mode_t mode)
Definition: igmp_group.c:233
void igmp_group_clear(igmp_group_t *group)
Definition: igmp_group.c:68
igmp_src_t * srcs
pool of sources
Definition: igmp.h:103
uword * igmp_src_by_key[IGMP_N_FILTER_MODES]
Source list per-filter mode.
Definition: igmp_group.h:86
u32 igmp_group_index(const igmp_group_t *g)
Definition: igmp_group.c:255
IGMP source The representation of a specified source address with in multicast group.
Definition: igmp_src.h:41