FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
igmp_timer.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 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_timer.h>
19 #include <igmp/igmp.h>
20 
21 /**
22  * Default timer values as per RFC
23  */
24 
26  [IGMP_TIMER_QUERY] = 60,
27  [IGMP_TIMER_SRC] = (3 * 60),
28  [IGMP_TIMER_LEAVE] = 60,
29  [IGMP_TIMER_REPORT_INTERVAL] = 1,
30 };
31 
32 #define IGMP_N_TIMERS (IGMP_TIMER_REPORT_INTERVAL+1)
33 
34 /**
35  * Timer
36  */
37 typedef struct igmp_timer_t_
38 {
39  /** Expiration timer */
41 
42  /** Call-back function to invoke on expiry */
44 
45  /** index of the object that scheduled the timer */
47 
48  /** Data registered by the client and passed back when the timer expires */
49  void *data;
50 } igmp_timer_t;
51 
52 enum
53 {
56 
57 /**
58  * pool of timers
59  */
61 
62 /**
63  * Vector of pending timers
64  */
66 
67 u32
69 {
70  ASSERT (t < IGMP_N_TIMERS);
71  return (igmp_default_timer_values[t]);
72 }
73 
74 void
76 {
77  ASSERT (t < IGMP_N_TIMERS);
79 }
80 
81 
82 static int
83 igmp_timer_compare (const void *_v1, const void *_v2)
84 {
85  const u32 *i1 = _v1, *i2 = _v2;
86  const igmp_timer_t *t1, *t2;
87  f64 dt;
88 
89  t1 = pool_elt_at_index (timer_pool, *i1);
90  t2 = pool_elt_at_index (timer_pool, *i2);
91 
92  dt = t2->exp_time - t1->exp_time;
93 
94  return (dt < 0 ? -1 : (dt > 0 ? +1 : 0));
95 }
96 
97 /** \brief igmp get next timer
98 
99  Get next timer.
100 */
101 u32
103 {
104  if (0 == vec_len (pending_timers))
105  return (IGMP_TIMER_ID_INVALID);
106 
107  return (pending_timers[vec_len (pending_timers) - 1]);
108 }
109 
110 void *
112 {
113  igmp_timer_t *timer;
114 
115  timer = pool_elt_at_index (timer_pool, tid);
116 
117  return (timer->data);
118 }
119 
120 void
122 {
123  igmp_timer_t *timer;
124 
125  timer = pool_elt_at_index (timer_pool, tid);
126 
127  timer->data = data;
128 }
129 
130 int
132 {
133  return (IGMP_TIMER_ID_INVALID == tid);
134 }
135 
136 /** \brief igmp timer process
137  @param vm - vlib main
138  @param rt - vlib runtime node
139  @param f - vlib frame
140 
141  Handle igmp timers.
142 */
143 static uword
145  vlib_frame_t * f)
146 {
147  uword *event_data = 0, event_type;
148  igmp_timer_id_t tid;
149  igmp_timer_t *timer;
150 
151  tid = IGMP_TIMER_ID_INVALID;
152 
153  while (1)
154  {
155  /* suspend util timer expires */
156  if (IGMP_TIMER_ID_INVALID != tid)
157  {
158  timer = pool_elt_at_index (timer_pool, tid);
160  (vm, timer->exp_time - vlib_time_now (vm));
161  }
162  else
164 
165  event_type = vlib_process_get_events (vm, &event_data);
166  vec_reset_length (event_data);
167 
168  if (event_type == IGMP_PROCESS_EVENT_UPDATE_TIMER)
169  goto next_timer;
170 
171  /* timer expired */
172  ASSERT (tid != IGMP_TIMER_ID_INVALID);
173 
174  timer = pool_elt_at_index (timer_pool, tid);
175  ASSERT (timer->func != NULL);
176  timer->func (timer->obj, timer->data);
177 
178  next_timer:
179  tid = igmp_get_next_timer ();
180  }
181  return 0;
182 }
183 
184 /* *INDENT-OFF* */
186 {
187  .function = igmp_timer_process,
188  .type = VLIB_NODE_TYPE_PROCESS,
189  .name = "igmp-timer-process",
190  .n_next_nodes = 0,
191 };
192 /* *INDENT-ON* */
193 
196 {
197  igmp_timer_t *timer;
198  vlib_main_t *vm;
199 
200  ASSERT (fn);
201 
202  vm = vlib_get_main ();
203  pool_get (timer_pool, timer);
204 
205  timer->exp_time = vlib_time_now (vm) + when;
206  timer->obj = obj;
207  timer->func = fn;
208  timer->data = data;
209 
211 
213 
216 
217  return (timer - timer_pool);
218 }
219 
220 void
222 {
223  if (IGMP_TIMER_ID_INVALID == *tid)
224  return;
226  pool_put_index (timer_pool, *tid);
227  *tid = IGMP_TIMER_ID_INVALID;
228 
232 }
233 
234 u8 *
235 format_igmp_timer_id (u8 * s, va_list * args)
236 {
237  igmp_timer_id_t tid = va_arg (*args, igmp_timer_id_t);
238  igmp_timer_t *timer;
239 
240  if (IGMP_TIMER_ID_INVALID == tid)
241  {
242  s = format (s, "not-running");
243  }
244  else
245  {
246  timer = pool_elt_at_index (timer_pool, tid);
247 
248  s =
249  format (s, "[expires-in:%f]",
250  timer->exp_time - vlib_time_now (vlib_get_main ()));
251  }
252 
253  return (s);
254 }
255 
256 /*
257  * fd.io coding-style-patch-verification: ON
258  *
259  * Local Variables:
260  * eval: (c-set-style "gnu")
261  * End:
262  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
igmp_timer_get_data
void * igmp_timer_get_data(igmp_timer_id_t tid)
Definition: igmp_timer.c:111
igmp_timer_process
static uword igmp_timer_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
igmp timer process
Definition: igmp_timer.c:144
igmp_get_next_timer
u32 igmp_get_next_timer(void)
igmp get next timer
Definition: igmp_timer.c:102
vlib_process_wait_for_event
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:660
f
vlib_frame_t * f
Definition: interface_output.c:1080
igmp_timer_is_running
int igmp_timer_is_running(igmp_timer_id_t tid)
Definition: igmp_timer.c:131
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
igmp_timer_retire
void igmp_timer_retire(igmp_timer_id_t *tid)
Definition: igmp_timer.c:221
igmp_timer.h
IGMP_TIMER_ID_INVALID
#define IGMP_TIMER_ID_INVALID
Definition: igmp_timer.h:28
igmp.h
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
igmp_process_event_t
enum @682 igmp_process_event_t
igmp_timer_function_t
void(* igmp_timer_function_t)(u32 obj, void *data)
A call-back function invoked when a timer expires;.
Definition: igmp_timer.h:35
format_igmp_timer_id
u8 * format_igmp_timer_id(u8 *s, va_list *args)
Definition: igmp_timer.c:235
vlib_frame_t
Definition: node.h:372
vlib_process_signal_event
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1019
pool_put_index
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:337
igmp_timer_compare
static int igmp_timer_compare(const void *_v1, const void *_v2)
Definition: igmp_timer.c:83
igmp_timer_type_set
void igmp_timer_type_set(igmp_timer_type_t t, u32 v)
Definition: igmp_timer.c:75
igmp_timer_type_get
u32 igmp_timer_type_get(igmp_timer_type_t t)
Definition: igmp_timer.c:68
igmp_timer_process_node
vlib_node_registration_t igmp_timer_process_node
(constructor) VLIB_REGISTER_NODE (igmp_timer_process_node)
Definition: igmp_timer.c:185
vlib_process_get_events
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type,...
Definition: node_funcs.h:583
IGMP_N_TIMERS
#define IGMP_N_TIMERS
Definition: igmp_timer.c:32
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
pending_timers
static u32 * pending_timers
Vector of pending timers.
Definition: igmp_timer.c:65
igmp_timer_type_t
enum igmp_timer_type_t_ igmp_timer_type_t
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
IGMP_PROCESS_EVENT_UPDATE_TIMER
@ IGMP_PROCESS_EVENT_UPDATE_TIMER
Definition: igmp_timer.c:54
uword
u64 uword
Definition: types.h:112
f64
double f64
Definition: types.h:142
igmp_timer_set_data
void igmp_timer_set_data(igmp_timer_id_t tid, void *data)
Definition: igmp_timer.c:121
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vec_search
#define vec_search(v, E)
Search a vector for the index of the entry that matches.
Definition: vec.h:1054
igmp_timer_t_::obj
u32 obj
index of the object that scheduled the timer
Definition: igmp_timer.c:46
data
u8 data[128]
Definition: ipsec_types.api:92
igmp_timer_t_
Timer.
Definition: igmp_timer.c:37
vlib_process_wait_for_event_or_clock
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:755
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
VLIB_NODE_TYPE_PROCESS
@ VLIB_NODE_TYPE_PROCESS
Definition: node.h:84
igmp_timer_id_t
u32 igmp_timer_id_t
The id of a running timer.
Definition: igmp_timer.h:26
vec_sort_with_function
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1097
igmp_default_timer_values
static igmp_timer_type_t igmp_default_timer_values[]
Default timer values as per RFC.
Definition: igmp_timer.c:25
vlib_main_t
Definition: main.h:102
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:399
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
vlib_node_runtime_t
Definition: node.h:454
igmp_timer_t
struct igmp_timer_t_ igmp_timer_t
Timer.
igmp_timer_t_::data
void * data
Data registered by the client and passed back when the timer expires.
Definition: igmp_timer.c:49
timer_pool
static igmp_timer_t * timer_pool
pool of timers
Definition: igmp_timer.c:60
vec_del1
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:896
igmp_timer_schedule
igmp_timer_id_t igmp_timer_schedule(f64 when, u32 obj, igmp_timer_function_t fn, void *data)
Schedule a timer to expire in 'when' seconds.
Definition: igmp_timer.c:195
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
igmp_timer_t_::func
igmp_timer_function_t func
Call-back function to invoke on expiry.
Definition: igmp_timer.c:43
igmp_timer_t_::exp_time
f64 exp_time
Expiration timer.
Definition: igmp_timer.c:40