FD.io VPP  v21.01.1
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
21 
22 #define foreach_ipfix_collector_error \
23 _(PROCESSED, "Number of IP-Fix packets processed") \
24 _(NO_LISTENER, "Number of IP-Fix packets with no listener")
25 
26 typedef enum
27 {
28 #define _(sym,str) IPFIX_COLLECTOR_ERROR_##sym,
30 #undef _
33 
35 #define _(sym,string) string,
37 #undef _
38 };
39 
40 typedef enum
41 {
45 
46 typedef struct
47 {
52 
54 
55 /* packet trace format function */
56 static u8 *
57 format_ipfix_collector_trace (u8 * s, va_list * args)
58 {
59  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61  ipfix_collector_trace_t *t = va_arg (*args, ipfix_collector_trace_t *);
62 
63  s = format (s,
64  "IPFIX_COLLECTOR: set_id %u, next_node %u", t->set_id,
65  t->next_node);
66  return s;
67 }
68 
69 /**
70  * @brief Node to receive IP-Fix packets.
71  * @node ipfix-collector
72  *
73  * This function receives IP-FIX packets and forwards them to other graph nodes
74  * based on SetID field in IP-FIX.
75  *
76  * @param vm vlib_main_t corresponding to the current thread.
77  * @param node vlib_node_runtime_t data for this node.
78  * @param frame vlib_frame_t whose contents should be dispatched.
79  *
80  * @par Graph mechanics: buffer, next index usage
81  *
82  * <em>Uses:</em>
83  * - <code>vlib_buffer_get_current(p0)</code>
84  * - Parses IP-Fix packet to extract SetId which will be used to decide
85  * next node where packets should be enqueued.
86  *
87  * <em>Next Index:</em>
88  * - Dispatches the packet to other VPP graph nodes based on their registartion
89  * for the IP-Fix SetId using API ipfix_collector_reg_setid().
90  */
91 uword
94  vlib_frame_t * from_frame)
95 {
96  u32 n_left_from, next_index, *from, *to_next;
97  word n_no_listener = 0;
98  word n_listener = 0;
99 
100  from = vlib_frame_vector_args (from_frame);
101  n_left_from = from_frame->n_vectors;
102 
103  next_index = node->cached_next_index;
104 
105  while (n_left_from > 0)
106  {
107  u32 n_left_to_next;
108 
109  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
110 
111  while (n_left_from >= 4 && n_left_to_next >= 2)
112  {
113  u32 bi0, bi1;
114  vlib_buffer_t *b0, *b1;
115  u32 next0, next1;
116  ipfix_message_header_t *ipfix0, *ipfix1;
117  ipfix_set_header_t *set0, *set1;
118  u16 set_id0, set_id1;
119  ipfix_client *client0, *client1;
120 
121  /* Prefetch next iteration. */
122  {
123  vlib_buffer_t *p2, *p3;
124 
125  p2 = vlib_get_buffer (vm, from[2]);
126  p3 = vlib_get_buffer (vm, from[3]);
127 
128  vlib_prefetch_buffer_header (p2, LOAD);
129  vlib_prefetch_buffer_header (p3, LOAD);
130 
131  CLIB_PREFETCH (p2->data,
132  (sizeof (ipfix_message_header_t) +
133  sizeof (ipfix_set_header_t)), LOAD);
134  CLIB_PREFETCH (p3->data,
135  (sizeof (ipfix_message_header_t) +
136  sizeof (ipfix_set_header_t)), LOAD);
137  }
138 
139  bi0 = from[0];
140  bi1 = from[1];
141  to_next[0] = bi0;
142  to_next[1] = bi1;
143  from += 2;
144  to_next += 2;
145  n_left_to_next -= 2;
146  n_left_from -= 2;
147 
148  b0 = vlib_get_buffer (vm, bi0);
149  b1 = vlib_get_buffer (vm, bi1);
150 
151  ipfix0 = vlib_buffer_get_current (b0);
152  ipfix1 = vlib_buffer_get_current (b1);
153 
154  set0 = (ipfix_set_header_t *) (ipfix0 + 1);
155  set1 = (ipfix_set_header_t *) (ipfix1 + 1);
156 
157  set_id0 = (u16) (clib_net_to_host_u32 (set0->set_id_length) >> 16);
158  set_id1 = (u16) (clib_net_to_host_u32 (set1->set_id_length) >> 16);
159 
160  client0 = ipfix_collector_get_client (set_id0);
161  client1 = ipfix_collector_get_client (set_id1);
162 
163  if (PREDICT_TRUE (NULL != client0))
164  {
165  next0 = client0->client_next_node;
166  n_listener++;
167  }
168  else
169  {
171  n_no_listener++;
172  }
173 
174  if (PREDICT_TRUE (NULL != client1))
175  {
176  next1 = client1->client_next_node;
177  n_listener++;
178  }
179  else
180  {
182  n_no_listener++;
183  }
184 
186  (sizeof (ipfix_message_header_t)
187  + sizeof (ipfix_set_header_t)));
189  (sizeof (ipfix_message_header_t)
190  + sizeof (ipfix_set_header_t)));
191 
192  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
193  {
194  ipfix_collector_trace_t *tr = vlib_add_trace (vm, node,
195  b0, sizeof (*tr));
196  tr->next_node = (client0 ? client0->client_node : 0xFFFFFFFF);
197  tr->set_id = set_id0;
198  }
199  if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
200  {
201  ipfix_collector_trace_t *tr = vlib_add_trace (vm, node,
202  b1, sizeof (*tr));
203  tr->next_node = (client1 ? client1->client_node : 0xFFFFFFFF);
204  tr->set_id = set_id1;
205  }
206 
207  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
208  to_next, n_left_to_next,
209  bi0, bi1, next0, next1);
210  }
211 
212  while (n_left_from > 0 && n_left_to_next > 0)
213  {
214  u32 bi0;
215  vlib_buffer_t *b0;
216  u32 next0;
217  ipfix_message_header_t *ipfix0;
218  ipfix_set_header_t *set0;
219  u16 set_id0;
220  ipfix_client *client0;
221 
222  bi0 = from[0];
223  to_next[0] = bi0;
224  from += 1;
225  to_next += 1;
226  n_left_from -= 1;
227  n_left_to_next -= 1;
228 
229  b0 = vlib_get_buffer (vm, bi0);
230  ipfix0 = vlib_buffer_get_current (b0);
231 
232  set0 = (ipfix_set_header_t *) (ipfix0 + 1);
233 
234  set_id0 = (u16) (clib_net_to_host_u32 (set0->set_id_length) >> 16);
235 
236  client0 = ipfix_collector_get_client (set_id0);
237 
238  if (PREDICT_TRUE (NULL != client0))
239  {
240  next0 = client0->client_next_node;
241  n_listener++;
242  }
243  else
244  {
246  n_no_listener++;
247  }
248 
250  (sizeof (ipfix_message_header_t)
251  + sizeof (ipfix_set_header_t)));
252  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
253  {
254  ipfix_collector_trace_t *tr = vlib_add_trace (vm, node,
255  b0, sizeof (*tr));
256  tr->next_node = (client0 ? client0->client_node : 0xFFFFFFFF);
257  tr->set_id = set_id0;
258  }
259 
260  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
261  to_next, n_left_to_next,
262  bi0, next0);
263  }
264 
265  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
266  }
267  vlib_error_count (vm, node->node_index,
268  IPFIX_COLLECTOR_ERROR_NO_LISTENER, n_no_listener);
269  vlib_error_count (vm, node->node_index,
270  IPFIX_COLLECTOR_ERROR_PROCESSED, n_listener);
271  return from_frame->n_vectors;
272 }
273 
274 /* *INDENT-OFF* */
276  .function = ipfix_collector_node_fn,
277  .name = "ipfix-collector",
278  .vector_size = sizeof (u32),
279  .format_trace = format_ipfix_collector_trace,
281 
283  .error_strings = ipfix_collector_error_strings,
284 
285  .n_next_nodes = IPFIX_COLLECTOR_N_NEXT,
286 
287  /* edit / add dispositions here */
288  .next_nodes = {
289  [IPFIX_COLLECTOR_NEXT_DROP] = "error-drop",
290  },
291 };
292 /* *INDENT-ON* */
293 
294 /*
295  * fd.io coding-style-patch-verification: ON
296  *
297  * Local Variables:
298  * eval: (c-set-style "gnu")
299  * End:
300  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define CLIB_UNUSED(x)
Definition: clib.h:87
IP-FIX collector internal client structure to store SetID to client node ID.
#define PREDICT_TRUE(x)
Definition: clib.h:122
u32 client_next_node
ipfix-collector next index where packets have to be redirected.
#define foreach_ipfix_collector_error
Definition: node.c:22
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
static ipfix_client * ipfix_collector_get_client(u16 set_id)
vlib_main_t * vm
Definition: in2out_ed.c:1580
vlib_node_registration_t ipfix_collector_node
(constructor) VLIB_REGISTER_NODE (ipfix_collector_node)
Definition: node.c:53
unsigned char u8
Definition: types.h:56
i64 word
Definition: types.h:111
description fragment has unexpected format
Definition: map.api:433
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:207
u32 client_node
Node index where packets have to be redirected.
unsigned int u32
Definition: types.h:88
ipfix_collector_next_t
Definition: node.c:40
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static u8 * format_ipfix_collector_trace(u8 *s, va_list *args)
Definition: node.c:57
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:233
#define PREDICT_FALSE(x)
Definition: clib.h:121
u32 node_index
Node index.
Definition: node.h:488
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:391
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
u16 n_vectors
Definition: node.h:397
uword ipfix_collector_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Node to receive IP-Fix packets.
Definition: node.c:92
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
u8 data[]
Packet data.
Definition: buffer.h:181
#define ARRAY_LEN(x)
Definition: clib.h:67
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:483
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:511
flowperpkt_error_t
Definition: node.c:26
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:252
struct _vlib_node_registration vlib_node_registration_t
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:634
static char * ipfix_collector_error_strings[]
Definition: node.c:34
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85