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