FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
ioam_export_thread.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * ioam_export_thread.c
17  */
18 #include <vnet/api_errno.h>
19 #include <vppinfra/pool.h>
20 #include "ioam_export.h"
21 
23 #define EXPORT_TIMEOUT (20.0)
24 #define THREAD_PERIOD (30.0)
25 
26 static uword
29 {
31  f64 now;
32  f64 timeout = 30.0;
33  uword event_type;
34  uword *event_data = 0;
35  int i;
36  ioam_export_buffer_t *eb = 0, *new_eb = 0;
37  u32 *vec_buffer_indices = 0;
38  u32 *vec_buffer_to_be_sent = 0;
39  u32 *thread_index = 0;
40  u32 new_pool_index = 0;
41 
43  /* Wait for Godot... */
45  event_type = vlib_process_get_events (vm, &event_data);
46  if (event_type != 1)
47  clib_warning ("bogus kickoff event received, %d", event_type);
48  vec_reset_length (event_data);
49 
50  while (1)
51  {
53  event_type = vlib_process_get_events (vm, &event_data);
54  switch (event_type)
55  {
56  case 2: /* Stop and Wait for kickoff again */
57  timeout = 1e9;
58  break;
59  case 1: /* kickoff : Check for unsent buffers */
60  timeout = THREAD_PERIOD;
61  break;
62  case ~0: /* timeout */
63  break;
64  }
65  vec_reset_length (event_data);
66  now = vlib_time_now (vm);
67  /*
68  * Create buffers for threads that are not active enough
69  * to send out the export records
70  */
71  for (i = 0; i < vec_len (em->buffer_per_thread); i++)
72  {
73  /* If the worker thread is processing export records ignore further checks */
74  if (*em->lockp[i] == 1)
75  continue;
77  if (eb->records_in_this_buffer > 0 && now > (eb->touched_at + EXPORT_TIMEOUT))
78  {
79  pool_get_aligned (em->buffer_pool, new_eb,
81  memset (new_eb, 0, sizeof (*new_eb));
82  if (ioam_export_init_buffer (vm, new_eb) == 1)
83  {
84  new_pool_index = new_eb - em->buffer_pool;
85  vec_add (vec_buffer_indices, &new_pool_index, 1);
86  vec_add (vec_buffer_to_be_sent, &em->buffer_per_thread[i],
87  1);
88  vec_add (thread_index, &i, 1);
89  }
90  else
91  {
92  pool_put (em->buffer_pool, new_eb);
93  /*Give up */
94  goto CLEANUP;
95  }
96  }
97  }
98  if (vec_len (thread_index) != 0)
99  {
100  /*
101  * Now swap the buffers out
102  */
103  for (i = 0; i < vec_len (thread_index); i++)
104  {
105  while (__sync_lock_test_and_set (em->lockp[thread_index[i]], 1))
106  ;
107  em->buffer_per_thread[thread_index[i]] =
108  vec_pop (vec_buffer_indices);
109  *em->lockp[thread_index[i]] = 0;
110  }
111 
112  /* Send the buffers */
113  for (i = 0; i < vec_len (vec_buffer_to_be_sent); i++)
114  {
115  eb =
116  pool_elt_at_index (em->buffer_pool, vec_buffer_to_be_sent[i]);
117  ioam_export_send_buffer (vm, eb);
118  pool_put (em->buffer_pool, eb);
119  }
120  }
121 
122  CLEANUP:
123  /* Free any leftover/unused buffers and everything that was allocated */
124  for (i = 0; i < vec_len (vec_buffer_indices); i++)
125  {
126  new_eb = pool_elt_at_index (em->buffer_pool, vec_buffer_indices[i]);
127  vlib_buffer_free (vm, &new_eb->buffer_index, 1);
128  pool_put (em->buffer_pool, new_eb);
129  }
130  vec_free (vec_buffer_indices);
131  vec_free (vec_buffer_to_be_sent);
132  vec_free (thread_index);
133  }
134  return 0; /* not so much */
135 }
136 
138 {
139  .function = ioam_export_process,
140  .type = VLIB_NODE_TYPE_PROCESS,
141  .name = "ioam-export-process",
142 };
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
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:684
#define THREAD_PERIOD
Fixed length block allocator.
u32 export_process_node_index
Definition: ioam_export.h:72
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
struct _vlib_node_registration vlib_node_registration_t
static int ioam_export_send_buffer(vlib_main_t *vm, ioam_export_buffer_t *eb)
Definition: ioam_export.h:266
#define vec_pop(V)
Returns last element of a vector and decrements its length.
Definition: vec.h:576
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:559
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:527
ioam_export_buffer_t * buffer_pool
Definition: ioam_export.h:55
#define clib_warning(format, args...)
Definition: error.h:59
static uword ioam_export_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
volatile u32 ** lockp
Definition: ioam_export.h:59
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
static vlib_node_registration_t ioam_export_process_node
(constructor) VLIB_REGISTER_NODE (ioam_export_process_node)
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static int ioam_export_init_buffer(vlib_main_t *vm, ioam_export_buffer_t *eb)
Definition: ioam_export.h:105
unsigned int u32
Definition: types.h:88
void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: dpdk_buffer.c:766
#define EXPORT_TIMEOUT
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
ioam_export_main_t ioam_export_main
Definition: ioam_export.h:75