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  * ppp_node.c: ppp packet processing
17  *
18  * Copyright (c) 2010 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <vlib/vlib.h>
41 #include <vnet/pg/pg.h>
42 #include <vnet/ppp/ppp.h>
43 #include <vppinfra/sparse_vec.h>
44 
45 #define foreach_ppp_input_next \
46  _ (PUNT, "error-punt") \
47  _ (DROP, "error-drop")
48 
49 typedef enum {
50 #define _(s,n) PPP_INPUT_NEXT_##s,
52 #undef _
55 
56 typedef struct {
57  u8 packet_data[32];
59 
60 static u8 * format_ppp_input_trace (u8 * s, va_list * va)
61 {
62  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
63  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
64  ppp_input_trace_t * t = va_arg (*va, ppp_input_trace_t *);
65 
66  s = format (s, "%U", format_ppp_header, t->packet_data);
67 
68  return s;
69 }
70 
71 typedef struct {
72  /* Sparse vector mapping ppp protocol in network byte order
73  to next index. */
75 
78 
79 static uword
81  vlib_node_runtime_t * node,
82  vlib_frame_t * from_frame)
83 {
84  ppp_input_runtime_t * rt = (void *) node->runtime_data;
85  u32 n_left_from, next_index, i_next, * from, * to_next;
86 
87  from = vlib_frame_vector_args (from_frame);
88  n_left_from = from_frame->n_vectors;
89 
90  if (node->flags & VLIB_NODE_FLAG_TRACE)
92  from,
93  n_left_from,
94  sizeof (from[0]),
95  sizeof (ppp_input_trace_t));
96 
97  next_index = node->cached_next_index;
98  i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
99 
100  while (n_left_from > 0)
101  {
102  u32 n_left_to_next;
103 
104  vlib_get_next_frame (vm, node, next_index,
105  to_next, n_left_to_next);
106 
107  while (n_left_from >= 4 && n_left_to_next >= 2)
108  {
109  u32 bi0, bi1;
110  vlib_buffer_t * b0, * b1;
111  ppp_header_t * h0, * h1;
112  u32 i0, i1, protocol0, protocol1, enqueue_code;
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 
124  CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
125  CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
126  }
127 
128  bi0 = from[0];
129  bi1 = from[1];
130  to_next[0] = bi0;
131  to_next[1] = bi1;
132  from += 2;
133  to_next += 2;
134  n_left_to_next -= 2;
135  n_left_from -= 2;
136 
137  b0 = vlib_get_buffer (vm, bi0);
138  b1 = vlib_get_buffer (vm, bi1);
139 
140  h0 = (void *) (b0->data + b0->current_data);
141  h1 = (void *) (b1->data + b1->current_data);
142 
143  b0->current_data += sizeof (h0[0]);
144  b1->current_data += sizeof (h1[0]);
145 
146  b0->current_length -= sizeof (h0[0]);
147  b1->current_length -= sizeof (h1[0]);
148 
149  /* Index sparse array with network byte order. */
150  protocol0 = h0->protocol;
151  protocol1 = h1->protocol;
152  sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0, &i1);
153 
154  b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? PPP_ERROR_UNKNOWN_PROTOCOL : PPP_ERROR_NONE];
155  b1->error = node->errors[i1 == SPARSE_VEC_INVALID_INDEX ? PPP_ERROR_UNKNOWN_PROTOCOL : PPP_ERROR_NONE];
156 
157  enqueue_code = (i0 != i_next) + 2*(i1 != i_next);
158 
159  if (PREDICT_FALSE (enqueue_code != 0))
160  {
161  switch (enqueue_code)
162  {
163  case 1:
164  /* A B A */
165  to_next[-2] = bi1;
166  to_next -= 1;
167  n_left_to_next += 1;
168  vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i0), bi0);
169  break;
170 
171  case 2:
172  /* A A B */
173  to_next -= 1;
174  n_left_to_next += 1;
175  vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i1), bi1);
176  break;
177 
178  case 3:
179  /* A B B or A B C */
180  to_next -= 2;
181  n_left_to_next += 2;
182  vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i0), bi0);
183  vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i1), bi1);
184  if (i0 == i1)
185  {
186  vlib_put_next_frame (vm, node, next_index,
187  n_left_to_next);
188  i_next = i1;
189  next_index = vec_elt (rt->next_by_protocol, i_next);
190  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
191  }
192  }
193  }
194  }
195 
196  while (n_left_from > 0 && n_left_to_next > 0)
197  {
198  u32 bi0;
199  vlib_buffer_t * b0;
200  ppp_header_t * h0;
201  u32 i0, protocol0;
202 
203  bi0 = from[0];
204  to_next[0] = bi0;
205  from += 1;
206  to_next += 1;
207  n_left_from -= 1;
208  n_left_to_next -= 1;
209 
210  b0 = vlib_get_buffer (vm, bi0);
211 
212  h0 = (void *) (b0->data + b0->current_data);
213 
214  b0->current_data += sizeof (h0[0]);
215  b0->current_length -= sizeof (h0[0]);
216 
217  protocol0 = h0->protocol;
218  i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
219 
220  b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? PPP_ERROR_UNKNOWN_PROTOCOL : PPP_ERROR_NONE];
221 
222  /* Sent packet to wrong next? */
223  if (PREDICT_FALSE (i0 != i_next))
224  {
225  /* Return old frame; remove incorrectly enqueued packet. */
226  vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
227 
228  /* Send to correct next. */
229  i_next = i0;
230  next_index = vec_elt (rt->next_by_protocol, i_next);
231  vlib_get_next_frame (vm, node, next_index,
232  to_next, n_left_to_next);
233  to_next[0] = bi0;
234  to_next += 1;
235  n_left_to_next -= 1;
236  }
237  }
238 
239  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
240  }
241 
242  return from_frame->n_vectors;
243 }
244 
245 static char * ppp_error_strings[] = {
246 #define ppp_error(n,s) s,
247 #include "error.def"
248 #undef ppp_error
249 };
250 
252  .function = ppp_input,
253  .name = "ppp-input",
254  /* Takes a vector of packets. */
255  .vector_size = sizeof (u32),
256 
257  .runtime_data_bytes = sizeof (ppp_input_runtime_t),
258 
259  .n_errors = PPP_N_ERROR,
260  .error_strings = ppp_error_strings,
261 
262  .n_next_nodes = PPP_INPUT_N_NEXT,
263  .next_nodes = {
264 #define _(s,n) [PPP_INPUT_NEXT_##s] = n,
266 #undef _
267  },
268 
269  .format_buffer = format_ppp_header_with_length,
270  .format_trace = format_ppp_input_trace,
271  .unformat_buffer = unformat_ppp_header,
272 };
273 
275 {
276  ppp_input_runtime_t * rt;
277 
278  {
280  if (error)
281  clib_error_report (error);
282  }
283 
284  ppp_setup_node (vm, ppp_input_node.index);
285 
287 
289  (/* elt bytes */ sizeof (rt->next_by_protocol[0]),
290  /* bits in index */ BITS (((ppp_header_t *) 0)->protocol));
291 
292  vec_validate (rt->sparse_index_by_next_index, PPP_INPUT_NEXT_DROP);
293  vec_validate (rt->sparse_index_by_next_index, PPP_INPUT_NEXT_PUNT);
294  rt->sparse_index_by_next_index[PPP_INPUT_NEXT_DROP]
296  rt->sparse_index_by_next_index[PPP_INPUT_NEXT_PUNT]
298 
299  return 0;
300 }
301 
303 
304 void
306  ppp_protocol_t protocol,
307  u32 node_index)
308 {
309  ppp_main_t * em = &ppp_main;
310  ppp_protocol_info_t * pi;
311  ppp_input_runtime_t * rt;
312  u16 * n;
313  u32 i;
314 
315  {
317  if (error)
318  clib_error_report (error);
319  }
320 
321  pi = ppp_get_protocol_info (em, protocol);
322  pi->node_index = node_index;
323  pi->next_index = vlib_node_add_next (vm,
324  ppp_input_node.index,
325  node_index);
326 
327  /* Setup ppp protocol -> next index sparse vector mapping. */
329  n = sparse_vec_validate (rt->next_by_protocol, clib_host_to_net_u16 (protocol));
330  n[0] = pi->next_index;
331 
332  /* Rebuild next index -> sparse index inverse mapping when sparse vector
333  is updated. */
335  for (i = 1; i < vec_len (rt->next_by_protocol); i++)
337 }
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
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
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
#define CLIB_UNUSED(x)
Definition: clib.h:79
unformat_function_t unformat_ppp_header
Definition: ppp.h:108
u16 protocol
Definition: packet.h:186
always_inline ppp_protocol_info_t * ppp_get_protocol_info(ppp_main_t *em, ppp_protocol_t protocol)
Definition: ppp.h:80
vlib_node_registration_t ppp_input_node
(constructor) VLIB_REGISTER_NODE (ppp_input_node)
Definition: node.c:251
#define foreach_ppp_input_next
Definition: node.c:45
#define clib_error_report(e)
Definition: error.h:126
u32 next_index
Definition: ppp.h:67
vlib_error_t * errors
Definition: node.h:378
ppp_protocol_t
Definition: packet.h:150
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
#define sparse_vec_validate(v, i)
Definition: sparse_vec.h:213
format_function_t format_ppp_header
Definition: ppp.h:99
#define vlib_call_init_function(vm, x)
Definition: init.h:159
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
void ppp_register_input_protocol(vlib_main_t *vm, ppp_protocol_t protocol, u32 node_index)
Definition: node.c:305
u8 packet_data[32]
Definition: node.c:57
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
always_inline void * sparse_vec_new(uword elt_bytes, uword sparse_index_bits)
Definition: sparse_vec.h:68
ppp_main_t ppp_main
Definition: ppp.c:44
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
u32 node_index
Definition: ppp.h:64
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
always_inline void ppp_setup_node(vlib_main_t *vm, u32 node_index)
Definition: ppp.h:112
Definition: ppp.h:70
ppp_input_next_t
Definition: node.c:49
format_function_t format_ppp_header_with_length
Definition: ppp.h:100
u16 cached_next_index
Definition: node.h:422
always_inline void sparse_vec_index2(void *v, u32 si0, u32 si1, u32 *i0_return, u32 *i1_return)
Definition: sparse_vec.h:158
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
u16 * next_by_protocol
Definition: node.c:74
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
u64 uword
Definition: types.h:112
#define vec_elt(v, i)
Get vector value at index i.
static char * ppp_error_strings[]
Definition: node.c:245
void vlib_trace_frame_buffers_only(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, uword n_buffers, uword next_buffer_stride, uword n_buffer_data_bytes_in_trace)
Definition: trace.c:45
unsigned short u16
Definition: types.h:57
always_inline void * vlib_node_get_runtime_data(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:76
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
always_inline void vlib_set_next_frame_buffer(vlib_main_t *vm, vlib_node_runtime_t *node, u32 next_index, u32 buffer_index)
Definition: node_funcs.h:292
static clib_error_t * ppp_init(vlib_main_t *vm)
Definition: ppp.c:226
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
static uword ppp_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:80
static clib_error_t * ppp_input_init(vlib_main_t *vm)
Definition: node.c:274
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
static u8 * format_ppp_input_trace(u8 *s, va_list *va)
Definition: node.c:60
#define BITS(x)
Definition: clib.h:58
uword runtime_data[(128-1 *sizeof(vlib_node_function_t *)-1 *sizeof(vlib_error_t *)-11 *sizeof(u32)-5 *sizeof(u16))/sizeof(uword)]
Definition: node.h:432
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
u32 * sparse_index_by_next_index
Definition: node.c:76
#define SPARSE_VEC_INVALID_INDEX
Definition: sparse_vec.h:65
always_inline uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:150