FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
oddbuf_periodic.c
Go to the documentation of this file.
1 /*
2  * oddbuf_periodic.c - skeleton plug-in periodic function
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vppinfra/error.h>
20 #include <oddbuf/oddbuf.h>
21 
22 static void
23 handle_event1 (oddbuf_main_t * pm, f64 now, uword event_data)
24 {
25  clib_warning ("received ODDBUF_EVENT1");
26 }
27 
28 static void
29 handle_event2 (oddbuf_main_t * pm, f64 now, uword event_data)
30 {
31  clib_warning ("received ODDBUF_EVENT2");
32 }
33 
34 static void
36 {
37  clib_warning ("Periodic timeouts now %s",
38  event_data ? "enabled" : "disabled");
39  pm->periodic_timer_enabled = event_data;
40 }
41 
42 static void
44 {
45  clib_warning ("timeout at %.2f", now);
46 }
47 
48 static uword
51 {
53  f64 now;
54  f64 timeout = 10.0;
55  uword *event_data = 0;
56  uword event_type;
57  int i;
58 
59  while (1)
60  {
61  if (pm->periodic_timer_enabled)
63  else
65 
66  now = vlib_time_now (vm);
67 
68  event_type = vlib_process_get_events (vm, (uword **) & event_data);
69 
70  switch (event_type)
71  {
72  /* Handle ODDBUF_EVENT1 */
73  case ODDBUF_EVENT1:
74  for (i = 0; i < vec_len (event_data); i++)
75  handle_event1 (pm, now, event_data[i]);
76  break;
77 
78  /* Handle ODDBUF_EVENT2 */
79  case ODDBUF_EVENT2:
80  for (i = 0; i < vec_len (event_data); i++)
81  handle_event2 (pm, now, event_data[i]);
82  break;
83  /* Handle the periodic timer on/off event */
84  case ODDBUF_EVENT_PERIODIC_ENABLE_DISABLE:
85  for (i = 0; i < vec_len (event_data); i++)
86  handle_periodic_enable_disable (pm, now, event_data[i]);
87  break;
88 
89  /* Handle periodic timeouts */
90  case ~0:
91  handle_timeout (pm, now);
92  break;
93  }
94  vec_reset_length (event_data);
95  }
96  return 0; /* or not */
97 }
98 
99 void
101 {
102  /* Already created the process node? */
103  if (omp->periodic_node_index > 0)
104  return;
105 
106  /* No, create it now and make a note of the node index */
107  omp->periodic_node_index = vlib_process_create (omp->vlib_main, "oddbuf-periodic-process", oddbuf_periodic_process, 16 /* log2_n_stack_bytes */
108  );
109 }
110 
111 /*
112  * fd.io coding-style-patch-verification: ON
113  *
114  * Local Variables:
115  * eval: (c-set-style "gnu")
116  * End:
117  */
static void handle_timeout(oddbuf_main_t *pm, f64 now)
vlib_main_t * vlib_main
Definition: oddbuf.h:43
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:673
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:593
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
int i
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
static void handle_event1(oddbuf_main_t *pm, f64 now, uword event_data)
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:516
static void handle_event2(oddbuf_main_t *pm, f64 now, uword event_data)
u32 vlib_process_create(vlib_main_t *vm, char *name, vlib_node_function_t *f, u32 log2_n_stack_bytes)
Create a vlib process.
Definition: node.c:743
u8 periodic_timer_enabled
Definition: oddbuf.h:35
void oddbuf_create_periodic_process(oddbuf_main_t *omp)
static void handle_periodic_enable_disable(oddbuf_main_t *pm, f64 now, uword event_data)
vlib_main_t * vm
Definition: buffer.c:323
#define clib_warning(format, args...)
Definition: error.h:59
static uword oddbuf_periodic_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
oddbuf_main_t oddbuf_main
Definition: oddbuf.c:54