blob: fd0105e4305780ace43df5ad88611a21f71a3feb [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Calvinaba0fe42016-08-19 13:43:59 -040049typedef enum
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051#define _(s,n) PPP_INPUT_NEXT_##s,
52 foreach_ppp_input_next
53#undef _
Calvinaba0fe42016-08-19 13:43:59 -040054 PPP_INPUT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} ppp_input_next_t;
56
Calvinaba0fe42016-08-19 13:43:59 -040057typedef struct
58{
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 u8 packet_data[32];
60} ppp_input_trace_t;
61
Calvinaba0fe42016-08-19 13:43:59 -040062static u8 *
63format_ppp_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064{
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Calvinaba0fe42016-08-19 13:43:59 -040067 ppp_input_trace_t *t = va_arg (*va, ppp_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
69 s = format (s, "%U", format_ppp_header, t->packet_data);
70
71 return s;
72}
73
Calvinaba0fe42016-08-19 13:43:59 -040074typedef struct
75{
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 /* Sparse vector mapping ppp protocol in network byte order
77 to next index. */
Calvinaba0fe42016-08-19 13:43:59 -040078 u16 *next_by_protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Calvinaba0fe42016-08-19 13:43:59 -040080 u32 *sparse_index_by_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081} ppp_input_runtime_t;
82
83static uword
84ppp_input (vlib_main_t * vm,
Calvinaba0fe42016-08-19 13:43:59 -040085 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
Calvinaba0fe42016-08-19 13:43:59 -040087 ppp_input_runtime_t *rt = (void *) node->runtime_data;
88 u32 n_left_from, next_index, i_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
90 from = vlib_frame_vector_args (from_frame);
91 n_left_from = from_frame->n_vectors;
92
93 if (node->flags & VLIB_NODE_FLAG_TRACE)
94 vlib_trace_frame_buffers_only (vm, node,
95 from,
96 n_left_from,
97 sizeof (from[0]),
98 sizeof (ppp_input_trace_t));
99
100 next_index = node->cached_next_index;
101 i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
102
103 while (n_left_from > 0)
104 {
105 u32 n_left_to_next;
106
Calvinaba0fe42016-08-19 13:43:59 -0400107 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109 while (n_left_from >= 4 && n_left_to_next >= 2)
110 {
111 u32 bi0, bi1;
Calvinaba0fe42016-08-19 13:43:59 -0400112 vlib_buffer_t *b0, *b1;
113 ppp_header_t *h0, *h1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 u32 i0, i1, protocol0, protocol1, enqueue_code;
115
116 /* Prefetch next iteration. */
117 {
Calvinaba0fe42016-08-19 13:43:59 -0400118 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
120 p2 = vlib_get_buffer (vm, from[2]);
121 p3 = vlib_get_buffer (vm, from[3]);
122
123 vlib_prefetch_buffer_header (p2, LOAD);
124 vlib_prefetch_buffer_header (p3, LOAD);
125
126 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
127 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
128 }
129
130 bi0 = from[0];
131 bi1 = from[1];
132 to_next[0] = bi0;
133 to_next[1] = bi1;
134 from += 2;
135 to_next += 2;
136 n_left_to_next -= 2;
137 n_left_from -= 2;
138
139 b0 = vlib_get_buffer (vm, bi0);
140 b1 = vlib_get_buffer (vm, bi1);
141
Zhiyong Yang7107d7e2019-05-06 04:47:22 -0400142 h0 = vlib_buffer_get_current (b0);
143 h1 = vlib_buffer_get_current (b1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Zhiyong Yang7107d7e2019-05-06 04:47:22 -0400145 vlib_buffer_advance (b0, sizeof (ppp_header_t));
146 vlib_buffer_advance (b1, sizeof (ppp_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
148 /* Index sparse array with network byte order. */
149 protocol0 = h0->protocol;
150 protocol1 = h1->protocol;
Calvinaba0fe42016-08-19 13:43:59 -0400151 sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0,
152 &i1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Calvinaba0fe42016-08-19 13:43:59 -0400154 b0->error =
155 node->errors[i0 ==
156 SPARSE_VEC_INVALID_INDEX ? PPP_ERROR_UNKNOWN_PROTOCOL
157 : PPP_ERROR_NONE];
158 b1->error =
159 node->errors[i1 ==
160 SPARSE_VEC_INVALID_INDEX ? PPP_ERROR_UNKNOWN_PROTOCOL
161 : PPP_ERROR_NONE];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Calvinaba0fe42016-08-19 13:43:59 -0400163 enqueue_code = (i0 != i_next) + 2 * (i1 != i_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
165 if (PREDICT_FALSE (enqueue_code != 0))
166 {
167 switch (enqueue_code)
168 {
169 case 1:
170 /* A B A */
171 to_next[-2] = bi1;
172 to_next -= 1;
173 n_left_to_next += 1;
Calvinaba0fe42016-08-19 13:43:59 -0400174 vlib_set_next_frame_buffer (vm, node,
175 vec_elt (rt->next_by_protocol,
176 i0), bi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 break;
178
179 case 2:
180 /* A A B */
181 to_next -= 1;
182 n_left_to_next += 1;
Calvinaba0fe42016-08-19 13:43:59 -0400183 vlib_set_next_frame_buffer (vm, node,
184 vec_elt (rt->next_by_protocol,
185 i1), bi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 break;
187
188 case 3:
189 /* A B B or A B C */
190 to_next -= 2;
191 n_left_to_next += 2;
Calvinaba0fe42016-08-19 13:43:59 -0400192 vlib_set_next_frame_buffer (vm, node,
193 vec_elt (rt->next_by_protocol,
194 i0), bi0);
195 vlib_set_next_frame_buffer (vm, node,
196 vec_elt (rt->next_by_protocol,
197 i1), bi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 if (i0 == i1)
199 {
200 vlib_put_next_frame (vm, node, next_index,
201 n_left_to_next);
202 i_next = i1;
203 next_index = vec_elt (rt->next_by_protocol, i_next);
Calvinaba0fe42016-08-19 13:43:59 -0400204 vlib_get_next_frame (vm, node, next_index, to_next,
205 n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 }
207 }
208 }
209 }
Calvinaba0fe42016-08-19 13:43:59 -0400210
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 while (n_left_from > 0 && n_left_to_next > 0)
212 {
213 u32 bi0;
Calvinaba0fe42016-08-19 13:43:59 -0400214 vlib_buffer_t *b0;
215 ppp_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 u32 i0, protocol0;
217
218 bi0 = from[0];
219 to_next[0] = bi0;
220 from += 1;
221 to_next += 1;
222 n_left_from -= 1;
223 n_left_to_next -= 1;
224
225 b0 = vlib_get_buffer (vm, bi0);
226
Zhiyong Yang7107d7e2019-05-06 04:47:22 -0400227 h0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Zhiyong Yang7107d7e2019-05-06 04:47:22 -0400229 vlib_buffer_advance (b0, sizeof (ppp_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231 protocol0 = h0->protocol;
232 i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
233
Calvinaba0fe42016-08-19 13:43:59 -0400234 b0->error =
235 node->errors[i0 ==
236 SPARSE_VEC_INVALID_INDEX ? PPP_ERROR_UNKNOWN_PROTOCOL
237 : PPP_ERROR_NONE];
238
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 /* Sent packet to wrong next? */
240 if (PREDICT_FALSE (i0 != i_next))
241 {
242 /* Return old frame; remove incorrectly enqueued packet. */
243 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
244
245 /* Send to correct next. */
246 i_next = i0;
247 next_index = vec_elt (rt->next_by_protocol, i_next);
248 vlib_get_next_frame (vm, node, next_index,
249 to_next, n_left_to_next);
250 to_next[0] = bi0;
251 to_next += 1;
252 n_left_to_next -= 1;
253 }
254 }
255
256 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
257 }
258
259 return from_frame->n_vectors;
260}
261
Calvinaba0fe42016-08-19 13:43:59 -0400262static char *ppp_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263#define ppp_error(n,s) s,
264#include "error.def"
265#undef ppp_error
266};
267
Calvinaba0fe42016-08-19 13:43:59 -0400268/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269VLIB_REGISTER_NODE (ppp_input_node) = {
270 .function = ppp_input,
271 .name = "ppp-input",
272 /* Takes a vector of packets. */
273 .vector_size = sizeof (u32),
274
275 .runtime_data_bytes = sizeof (ppp_input_runtime_t),
276
277 .n_errors = PPP_N_ERROR,
278 .error_strings = ppp_error_strings,
279
280 .n_next_nodes = PPP_INPUT_N_NEXT,
281 .next_nodes = {
282#define _(s,n) [PPP_INPUT_NEXT_##s] = n,
283 foreach_ppp_input_next
284#undef _
285 },
286
287 .format_buffer = format_ppp_header_with_length,
288 .format_trace = format_ppp_input_trace,
289 .unformat_buffer = unformat_ppp_header,
290};
Calvinaba0fe42016-08-19 13:43:59 -0400291/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Calvinaba0fe42016-08-19 13:43:59 -0400293static clib_error_t *
Damjan Marione9f929b2017-03-16 11:32:09 +0100294ppp_input_runtime_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295{
Calvinaba0fe42016-08-19 13:43:59 -0400296 ppp_input_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 rt = vlib_node_get_runtime_data (vm, ppp_input_node.index);
299
300 rt->next_by_protocol = sparse_vec_new
Calvinaba0fe42016-08-19 13:43:59 -0400301 ( /* elt bytes */ sizeof (rt->next_by_protocol[0]),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 /* bits in index */ BITS (((ppp_header_t *) 0)->protocol));
303
304 vec_validate (rt->sparse_index_by_next_index, PPP_INPUT_NEXT_DROP);
305 vec_validate (rt->sparse_index_by_next_index, PPP_INPUT_NEXT_PUNT);
306 rt->sparse_index_by_next_index[PPP_INPUT_NEXT_DROP]
307 = SPARSE_VEC_INVALID_INDEX;
308 rt->sparse_index_by_next_index[PPP_INPUT_NEXT_PUNT]
309 = SPARSE_VEC_INVALID_INDEX;
310
311 return 0;
312}
313
Damjan Marione9f929b2017-03-16 11:32:09 +0100314static clib_error_t *
315ppp_input_init (vlib_main_t * vm)
316{
317
318 {
319 clib_error_t *error = vlib_call_init_function (vm, ppp_init);
320 if (error)
321 clib_error_report (error);
322 }
323
324 ppp_setup_node (vm, ppp_input_node.index);
325 ppp_input_runtime_init (vm);
326
327 return 0;
328}
329
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330VLIB_INIT_FUNCTION (ppp_input_init);
Damjan Marione9f929b2017-03-16 11:32:09 +0100331VLIB_WORKER_INIT_FUNCTION (ppp_input_runtime_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
333void
334ppp_register_input_protocol (vlib_main_t * vm,
Calvinaba0fe42016-08-19 13:43:59 -0400335 ppp_protocol_t protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336{
Calvinaba0fe42016-08-19 13:43:59 -0400337 ppp_main_t *em = &ppp_main;
338 ppp_protocol_info_t *pi;
339 ppp_input_runtime_t *rt;
340 u16 *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 u32 i;
342
343 {
Calvinaba0fe42016-08-19 13:43:59 -0400344 clib_error_t *error = vlib_call_init_function (vm, ppp_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 if (error)
346 clib_error_report (error);
347 }
348
349 pi = ppp_get_protocol_info (em, protocol);
350 pi->node_index = node_index;
Calvinaba0fe42016-08-19 13:43:59 -0400351 pi->next_index = vlib_node_add_next (vm, ppp_input_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
353 /* Setup ppp protocol -> next index sparse vector mapping. */
354 rt = vlib_node_get_runtime_data (vm, ppp_input_node.index);
Calvinaba0fe42016-08-19 13:43:59 -0400355 n =
356 sparse_vec_validate (rt->next_by_protocol,
357 clib_host_to_net_u16 (protocol));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 n[0] = pi->next_index;
359
360 /* Rebuild next index -> sparse index inverse mapping when sparse vector
361 is updated. */
362 vec_validate (rt->sparse_index_by_next_index, pi->next_index);
363 for (i = 1; i < vec_len (rt->next_by_protocol); i++)
364 rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
365}
Calvinaba0fe42016-08-19 13:43:59 -0400366
367/*
368 * fd.io coding-style-patch-verification: ON
369 *
370 * Local Variables:
371 * eval: (c-set-style "gnu")
372 * End:
373 */