IGMP: proxy device
[vpp.git] / src / plugins / igmp / igmp_src.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_src.h>
19 #include <igmp/igmp_group.h>
20 #include <igmp/igmp.h>
21
22 void
23 igmp_src_free (igmp_src_t * src)
24 {
25   igmp_main_t *im = &igmp_main;
26
27   IGMP_DBG ("free-src: (%U)", format_igmp_key, src->key);
28
29   igmp_timer_retire (&src->timers[IGMP_SRC_TIMER_EXP]);
30
31   clib_mem_free (src->key);
32   pool_put (im->srcs, src);
33 }
34
35 static void
36 igmp_src_exp (u32 obj, void *dat)
37 {
38   igmp_group_t *group;
39   igmp_src_t *src;
40
41   src = pool_elt_at_index (igmp_main.srcs, obj);
42   group = igmp_group_get (src->group);
43
44   IGMP_DBG ("src-exp: %U", format_igmp_key, src->key);
45
46   igmp_timer_retire (&src->timers[IGMP_SRC_TIMER_EXP]);
47
48   if (IGMP_MODE_ROUTER == src->mode)
49     {
50       igmp_config_t *config;
51       igmp_group_t *group;
52
53       /*
54        * inform interest parties
55        */
56       group = igmp_group_get (src->group);
57       config = igmp_config_get (group->config);
58
59       igmp_event (IGMP_FILTER_MODE_EXCLUDE,
60                   config->sw_if_index, src->key, group->key);
61
62       igmp_proxy_device_block_src (config, group, src);
63     }
64
65   igmp_group_src_remove (group, src);
66   igmp_src_free (src);
67
68   if (0 == igmp_group_n_srcs (group, IGMP_FILTER_MODE_INCLUDE))
69     igmp_group_clear (group);
70 }
71
72 igmp_src_t *
73 igmp_src_alloc (u32 group_index, const igmp_key_t * skey, igmp_mode_t mode)
74 {
75   igmp_main_t *im = &igmp_main;
76   igmp_src_t *src;
77
78   IGMP_DBG ("new-src: (%U)", format_igmp_key, skey);
79
80   pool_get (im->srcs, src);
81   memset (src, 0, sizeof (igmp_src_t));
82   src->mode = mode;
83   src->key = clib_mem_alloc (sizeof (*skey));
84   src->group = group_index;
85   clib_memcpy (src->key, skey, sizeof (*skey));
86
87   if (IGMP_MODE_ROUTER == mode)
88     {
89       igmp_config_t *config;
90       igmp_group_t *group;
91       /*
92        * start a timer that determines whether the source is still
93        * active on the link
94        */
95       src->timers[IGMP_SRC_TIMER_EXP] =
96         igmp_timer_schedule (igmp_timer_type_get (IGMP_TIMER_SRC),
97                              src - im->srcs, igmp_src_exp, NULL);
98
99       /*
100        * inform interest parties
101        */
102       group = igmp_group_get (src->group);
103       config = igmp_config_get (group->config);
104
105       igmp_event (IGMP_FILTER_MODE_INCLUDE,
106                   config->sw_if_index, src->key, group->key);
107     }
108   else
109     {
110       src->timers[IGMP_SRC_TIMER_EXP] = IGMP_TIMER_ID_INVALID;
111     }
112
113   return (src);
114 }
115
116 void
117 igmp_src_refresh (igmp_src_t * src)
118 {
119   IGMP_DBG ("refresh-src: (%U)", format_igmp_key, src->key);
120
121   igmp_timer_retire (&src->timers[IGMP_SRC_TIMER_EXP]);
122
123   src->timers[IGMP_SRC_TIMER_EXP] =
124     igmp_timer_schedule (igmp_timer_type_get (IGMP_TIMER_SRC),
125                          igmp_src_index (src), igmp_src_exp, NULL);
126 }
127
128 void
129 igmp_src_blocked (igmp_src_t * src)
130 {
131   IGMP_DBG ("block-src: (%U)", format_igmp_key, src->key);
132
133   igmp_timer_retire (&src->timers[IGMP_SRC_TIMER_EXP]);
134
135   src->timers[IGMP_SRC_TIMER_EXP] =
136     igmp_timer_schedule (igmp_timer_type_get (IGMP_TIMER_LEAVE),
137                          igmp_src_index (src), igmp_src_exp, NULL);
138 }
139
140 u32
141 igmp_src_index (igmp_src_t * src)
142 {
143   return (src - igmp_main.srcs);
144 }
145
146
147 /*
148  * fd.io coding-style-patch-verification: ON
149  *
150  * Local Variables:
151  * eval: (c-set-style "gnu")
152  * End:
153  */