be3d997cbebda2b650567f997b8771aa1c1c9adc
[vpp.git] / src / plugins / igmp / igmp_group.c
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
22 igmp_group_free_all_srcs (igmp_group_t * group)
23 {
24   igmp_src_t *src;
25
26   FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
27     ({
28       igmp_src_free(src);
29     }));
30
31   hash_free (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE]);
32   hash_free (group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE]);
33 }
34
35 void
36 igmp_group_src_remove (igmp_group_t * group, igmp_src_t * src)
37 {
38   hash_unset_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE], src->key);
39   hash_unset_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE], src->key);
40 }
41
42 igmp_src_t *
43 igmp_group_src_update (igmp_group_t * group,
44                        const igmp_key_t * skey, igmp_mode_t mode)
45 {
46   igmp_src_t *src;
47
48   src = igmp_src_lookup (group, skey);
49
50   if (NULL == src)
51     {
52       src = igmp_src_alloc (igmp_group_index (group), skey, mode);
53
54       hash_set_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE],
55                     src->key, igmp_src_index (src));
56     }
57   else
58     {
59       igmp_src_refresh (src);
60     }
61
62   return (src);
63 }
64
65 void
66 igmp_group_clear (igmp_group_t ** group)
67 {
68   igmp_config_t *config;
69   u32 ii;
70
71   ASSERT (*group);
72
73   config = igmp_config_get ((*group)->config);
74
75   /* If interface is in ROUTER mode and IGMP proxy is enabled
76    * remove mfib path.
77    */
78   if (config->mode == IGMP_MODE_ROUTER)
79     {
80       igmp_proxy_device_mfib_path_add_del (*group, /* add */ 0);
81     }
82
83   IGMP_DBG ("clear-group: %U %U",
84             format_igmp_key, (*group)->key,
85             format_vnet_sw_if_index_name,
86             vnet_get_main (), config->sw_if_index);
87
88   igmp_group_free_all_srcs (*group);
89
90   for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
91     {
92       igmp_timer_retire (&(*group)->timers[ii]);
93     }
94
95   hash_unset_mem (config->igmp_group_by_key, (*group)->key);
96   clib_mem_free ((*group)->key);
97   pool_put (igmp_main.groups, *group);
98   *group = 0;
99 }
100
101 igmp_group_t *
102 igmp_group_alloc (igmp_config_t * config,
103                   const igmp_key_t * gkey, igmp_filter_mode_t mode)
104 {
105   igmp_main_t *im = &igmp_main;
106   igmp_group_t *group;
107   u32 ii;
108
109   IGMP_DBG ("new-group: %U", format_igmp_key, gkey);
110   pool_get (im->groups, group);
111   clib_memset (group, 0, sizeof (igmp_group_t));
112   group->key = clib_mem_alloc (sizeof (igmp_key_t));
113   clib_memcpy (group->key, gkey, sizeof (igmp_key_t));
114   group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE] =
115     hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
116   group->igmp_src_by_key[IGMP_FILTER_MODE_EXCLUDE] =
117     hash_create_mem (0, sizeof (igmp_key_t), sizeof (uword));
118   group->router_filter_mode = mode;
119   group->config = igmp_config_index (config);
120   group->n_reports_sent = 0;
121
122   for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
123     group->timers[ii] = IGMP_TIMER_ID_INVALID;
124
125   hash_set_mem (config->igmp_group_by_key, group->key, group - im->groups);
126
127   /* If interface is in ROUTER mode and IGMP proxy is enabled
128    * add mfib path.
129    */
130   if (config->mode == IGMP_MODE_ROUTER)
131     {
132       igmp_proxy_device_mfib_path_add_del (group, /* add */ 1);
133     }
134
135   return (group);
136 }
137
138 /**
139  * the set of present sources minus the new set
140  */
141 ip46_address_t *
142 igmp_group_present_minus_new (igmp_group_t * group,
143                               igmp_filter_mode_t mode,
144                               const ip46_address_t * saddrs)
145 {
146   const ip46_address_t *s1;
147   ip46_address_t *pmn;
148   igmp_src_t *src;
149   u32 found;
150
151   pmn = NULL;
152
153   if (0 == vec_len(saddrs))
154     {
155       FOR_EACH_SRC(src, group, mode,
156         ({
157           vec_add1(pmn, *src->key);
158         }));
159     }
160   else
161     {
162       FOR_EACH_SRC(src, group, mode,
163         ({
164           found = 0;
165           vec_foreach(s1, saddrs)
166             {
167               if (ip46_address_is_equal(s1, src->key))
168                 {
169                   found = 1;
170                   break;
171                 }
172             }
173
174           if (!found)
175             vec_add1(pmn, *src->key);
176         }));
177     }
178
179   return (pmn);
180 }
181
182 /**
183  * the set of new sources minus the present set
184  */
185 ip46_address_t *
186 igmp_group_new_minus_present (igmp_group_t * group,
187                               igmp_filter_mode_t mode,
188                               const ip46_address_t * saddrs)
189 {
190   const ip46_address_t *s1;
191   ip46_address_t *npm;
192   igmp_src_t *src;
193   u32 found;
194
195   npm = NULL;
196
197   vec_foreach(s1, saddrs)
198     {
199       found = 0;
200       FOR_EACH_SRC(src, group, mode,
201         ({
202           if (ip46_address_is_equal(s1, src->key))
203             {
204               found = 1;
205               break;
206             }
207         }));
208
209       if (!found)
210         vec_add1(npm, *s1);
211     }
212
213   return (npm);
214 }
215
216 ip46_address_t *
217 igmp_group_new_intersect_present (igmp_group_t * group,
218                                   igmp_filter_mode_t mode,
219                                   const ip46_address_t * saddrs)
220 {
221   ip46_address_t *intersect;
222   const ip46_address_t *s1;
223   igmp_src_t *src;
224
225   intersect = NULL;
226
227   FOR_EACH_SRC(src, group, mode,
228     ({
229       vec_foreach(s1, saddrs)
230         {
231           if (s1->ip4.as_u32 == src->key->ip4.as_u32)
232             {
233               vec_add1(intersect, *s1);
234               break;
235             }
236         }
237     }));
238
239   return (intersect);
240 }
241
242 u32
243 igmp_group_n_srcs (const igmp_group_t * group, igmp_filter_mode_t mode)
244 {
245   return (hash_elts (group->igmp_src_by_key[mode]));
246 }
247
248
249 igmp_src_t *
250 igmp_src_lookup (igmp_group_t * group, const igmp_key_t * key)
251 {
252   uword *p;
253   igmp_src_t *src = NULL;
254   if (!group)
255     return NULL;
256
257   p = hash_get_mem (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE], key);
258   if (p)
259     src = vec_elt_at_index (igmp_main.srcs, p[0]);
260
261   return src;
262 }
263
264 u32
265 igmp_group_index (const igmp_group_t * g)
266 {
267   return (g - igmp_main.groups);
268 }
269
270 igmp_group_t *
271 igmp_group_get (u32 index)
272 {
273   return (pool_elt_at_index (igmp_main.groups, index));
274 }
275
276 u8 *
277 format_igmp_group_timer_type (u8 * s, va_list * args)
278 {
279   igmp_group_timer_type_t type = va_arg (*args, igmp_group_timer_type_t);
280
281   switch (type)
282     {
283 #define _(v,t) case IGMP_GROUP_TIMER_##v: return (format (s, "%s", t));
284       foreach_igmp_group_timer
285 #undef _
286     }
287   return (s);
288 }
289
290 u8 *
291 format_igmp_group (u8 * s, va_list * args)
292 {
293   igmp_group_t *group = va_arg (*args, igmp_group_t *);
294   u32 indent = va_arg (*args, u32);
295   igmp_src_t *src;
296   u32 ii;
297
298   s = format (s, "%U%U",
299               format_white_space, indent, format_igmp_key, group->key);
300
301   for (ii = 0; ii < IGMP_GROUP_N_TIMERS; ii++)
302     s = format (s, "\n%U  %U:%U", format_white_space, indent,
303                 format_igmp_group_timer_type, ii,
304                 format_igmp_timer_id, group->timers[ii]);
305
306   FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
307   ({
308     s = format (s, "\n%U", format_igmp_src, src, indent+4);
309   }));
310
311   return (s);
312 }
313
314 /*
315  * fd.io coding-style-patch-verification: ON
316  *
317  * Local Variables:
318  * eval: (c-set-style "gnu")
319  * End:
320  */