FD.io VPP  v16.06
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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>
19 
20 #if DPDK==1
22 
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
25 
27 
28 typedef struct {
29  u32 next_index;
30 } li_hit_trace_t;
31 
32 /* packet trace format function */
33 static u8 * format_li_hit_trace (u8 * s, va_list * args)
34 {
35  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37  li_hit_trace_t * t = va_arg (*args, li_hit_trace_t *);
38 
39  s = format (s, "LI_HIT: next index %d", t->next_index);
40 
41  return s;
42 }
43 
45 
46 #define foreach_li_hit_error \
47 _(HITS, "LI packets processed") \
48 _(NO_COLLECTOR, "No collector configured")
49 
50 typedef enum {
51 #define _(sym,str) LI_HIT_ERROR_##sym,
52  foreach_li_hit_error
53 #undef _
54  LI_HIT_N_ERROR,
55 } li_hit_error_t;
56 
57 static char * li_hit_error_strings[] = {
58 #define _(sym,string) string,
59  foreach_li_hit_error
60 #undef _
61 };
62 
63 typedef enum {
64  LI_HIT_NEXT_ETHERNET,
65  LI_HIT_N_NEXT,
66 } li_hit_next_t;
67 
68 static uword
70  vlib_node_runtime_t * node,
71  vlib_frame_t * frame)
72 {
73  u32 n_left_from, * from, * to_next;
74  li_hit_next_t next_index;
75  vlib_frame_t * int_frame = 0;
76  u32 * to_int_next = 0;
77  li_main_t * lm = &li_main;
78 
79  from = vlib_frame_vector_args (frame);
80  n_left_from = frame->n_vectors;
81  next_index = node->cached_next_index;
82 
83  if (PREDICT_FALSE (vec_len (lm->collectors) == 0))
84  {
85  vlib_node_increment_counter (vm, li_hit_node.index,
86  LI_HIT_ERROR_NO_COLLECTOR,
87  n_left_from);
88  }
89  else
90  {
91  /* The intercept frame... */
92  int_frame = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
93  to_int_next = vlib_frame_vector_args (int_frame);
94  }
95 
96  while (n_left_from > 0)
97  {
98  u32 n_left_to_next;
99 
100  vlib_get_next_frame (vm, node, next_index,
101  to_next, n_left_to_next);
102 
103 #if 0
104  while (n_left_from >= 4 && n_left_to_next >= 2)
105  {
106  u32 next0 = LI_HIT_NEXT_INTERFACE_OUTPUT;
107  u32 next1 = LI_HIT_NEXT_INTERFACE_OUTPUT;
108  u32 sw_if_index0, sw_if_index1;
109  u8 tmp0[6], tmp1[6];
110  ethernet_header_t *en0, *en1;
111  u32 bi0, bi1;
112  vlib_buffer_t * b0, * b1;
113 
114  /* Prefetch next iteration. */
115  {
116  vlib_buffer_t * p2, * p3;
117 
118  p2 = vlib_get_buffer (vm, from[2]);
119  p3 = vlib_get_buffer (vm, from[3]);
120 
121  vlib_prefetch_buffer_header (p2, LOAD);
122  vlib_prefetch_buffer_header (p3, LOAD);
123 
126  }
127 
128  /* speculatively enqueue b0 and b1 to the current next frame */
129  to_next[0] = bi0 = from[0];
130  to_next[1] = bi1 = from[1];
131  from += 2;
132  to_next += 2;
133  n_left_from -= 2;
134  n_left_to_next -= 2;
135 
136  b0 = vlib_get_buffer (vm, bi0);
137  b1 = vlib_get_buffer (vm, bi1);
138 
139  /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
140  ASSERT (b0->current_data == 0);
141  ASSERT (b1->current_data == 0);
142 
143  en0 = vlib_buffer_get_current (b0);
144  en1 = vlib_buffer_get_current (b1);
145 
146  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
147  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
148 
149  /* Send pkt back out the RX interface */
150  vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
151  vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
152 
153  /* $$$$$ End of processing 2 x packets $$$$$ */
154 
156  {
157  if (b0->flags & VLIB_BUFFER_IS_TRACED)
158  {
159  li_hit_trace_t *t =
160  vlib_add_trace (vm, node, b0, sizeof (*t));
161  t->sw_if_index = sw_if_index0;
162  t->next_index = next0;
163  }
164  if (b1->flags & VLIB_BUFFER_IS_TRACED)
165  {
166  li_hit_trace_t *t =
167  vlib_add_trace (vm, node, b1, sizeof (*t));
168  t->sw_if_index = sw_if_index1;
169  t->next_index = next1;
170  }
171  }
172 
173  /* verify speculative enqueues, maybe switch current next frame */
174  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
175  to_next, n_left_to_next,
176  bi0, bi1, next0, next1);
177  }
178 #endif /* $$$ dual-loop off */
179 
180  while (n_left_from > 0 && n_left_to_next > 0)
181  {
182  u32 bi0;
183  vlib_buffer_t * b0;
184  vlib_buffer_t * c0;
185  ip4_udp_header_t * iu0;
186  ip4_header_t * ip0;
187  udp_header_t * udp0;
188  u32 next0 = LI_HIT_NEXT_ETHERNET;
189 
190  /* speculatively enqueue b0 to the current next frame */
191  bi0 = from[0];
192  to_next[0] = bi0;
193  from += 1;
194  to_next += 1;
195  n_left_from -= 1;
196  n_left_to_next -= 1;
197 
198  b0 = vlib_get_buffer (vm, bi0);
199  if (PREDICT_TRUE(to_int_next != 0))
200  {
201  /* Make an intercept copy */
202  c0 = vlib_dpdk_clone_buffer (vm, b0);
203 
204  vlib_buffer_advance(c0, -sizeof(*iu0));
205 
206  iu0 = vlib_buffer_get_current(c0);
207  ip0 = &iu0->ip4;
208 
209  ip0->ip_version_and_header_length = 0x45;
210  ip0->ttl = 254;
211  ip0->protocol = IP_PROTOCOL_UDP;
212 
213  ip0->src_address.as_u32 = lm->src_addrs[0].as_u32;
214  ip0->dst_address.as_u32 = lm->collectors[0].as_u32;
215  ip0->length = vlib_buffer_length_in_chain (vm, c0);
216  ip0->checksum = ip4_header_checksum (ip0);
217 
218  udp0 = &iu0->udp;
219  udp0->src_port = udp0->dst_port =
220  clib_host_to_net_u16(lm->ports[0]);
221  udp0->checksum = 0;
222  udp0->length =
223  clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm , b0));
224 
225  to_int_next [0] = vlib_get_buffer_index (vm, c0);
226  to_int_next++;
227  }
228 
230  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
231  {
232  li_hit_trace_t *t =
233  vlib_add_trace (vm, node, b0, sizeof (*t));
234  t->next_index = next0;
235  }
236 
237  /* verify speculative enqueue, maybe switch current next frame */
238  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
239  to_next, n_left_to_next,
240  bi0, next0);
241  }
242 
243  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
244  }
245 
246  if (int_frame)
247  {
248  int_frame->n_vectors = frame->n_vectors;
249  vlib_put_frame_to_node (vm, ip4_lookup_node.index, int_frame);
250  }
251 
252  vlib_node_increment_counter (vm, li_hit_node.index,
253  LI_HIT_ERROR_HITS, frame->n_vectors);
254  return frame->n_vectors;
255 }
256 
257 VLIB_REGISTER_NODE (li_hit_node) = {
258  .function = li_hit_node_fn,
259  .name = "li-hit",
260  .vector_size = sizeof (u32),
261  .format_trace = format_li_hit_trace,
263 
264  .n_errors = ARRAY_LEN(li_hit_error_strings),
265  .error_strings = li_hit_error_strings,
266 
267  .n_next_nodes = LI_HIT_N_NEXT,
268 
269  /* edit / add dispositions here */
270  .next_nodes = {
271  [LI_HIT_NEXT_ETHERNET] = "ethernet-input-not-l2",
272  },
273 };
274 
275 #else
276 #include <vlib/vlib.h>
277 
278 static uword
280  vlib_node_runtime_t * node,
281  vlib_frame_t * frame)
282 {
283  clib_warning ("LI not implemented (no DPDK)");
284  return 0;
285 }
286 
287 VLIB_REGISTER_NODE (li_hit_node) = {
288  .vector_size = sizeof (u32),
289  .function = li_hit_node_fn,
290  .name = "li-hit",
291 };
292 
293 static clib_error_t *
295 {
296  return 0;
297 }
298 
300 
301 
302 #endif /* DPDK */
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
#define CLIB_UNUSED(x)
Definition: clib.h:79
ip4_address_t src_address
Definition: ip4_packet.h:138
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define PREDICT_TRUE(x)
Definition: clib.h:98
struct _vlib_node_registration vlib_node_registration_t
vlib_node_registration_t li_hit_node
(constructor) VLIB_REGISTER_NODE (li_hit_node)
Definition: node.c:287
static clib_error_t * li_init(vlib_main_t *vm)
Definition: node.c:294
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:1360
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:77
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
ip4_address_t dst_address
Definition: ip4_packet.h:138
always_inline uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
#define clib_warning(format, args...)
Definition: error.h:59
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
#define PREDICT_FALSE(x)
Definition: clib.h:97
always_inline void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:970
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Definition: buffer_node.h:43
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
always_inline u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:194
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
u16 cached_next_index
Definition: node.h:422
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u64 uword
Definition: types.h:112
Definition: defs.h:46
ip4_address_t * src_addrs
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
unsigned char u8
Definition: types.h:56
always_inline 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
li_main_t li_main
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
static vlib_buffer_t * vlib_dpdk_clone_buffer(vlib_main_t *vm, vlib_buffer_t *b)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
static uword li_hit_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:279
u8 data[0]
Packet data.
Definition: buffer.h:150
u8 ip_version_and_header_length
Definition: ip4_packet.h:108
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:184
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
always_inline u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:82
Definition: defs.h:45
ip4_address_t * collectors