2 * decap.c: pppoe session decap packet processing
4 * Copyright (c) 2017 Intel and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/ppp/packet.h>
20 #include <pppoe/pppoe.h>
29 static u8 * format_pppoe_rx_trace (u8 * s, va_list * args)
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 pppoe_rx_trace_t * t = va_arg (*args, pppoe_rx_trace_t *);
35 if (t->session_index != ~0)
37 s = format (s, "PPPoE decap from pppoe_session%d session_id %d next %d error %d",
38 t->session_index, t->session_id, t->next_index, t->error);
42 s = format (s, "PPPoE decap error - session for session_id %d does not exist",
48 VLIB_NODE_FN (pppoe_input_node) (vlib_main_t * vm,
49 vlib_node_runtime_t * node,
50 vlib_frame_t * from_frame)
52 u32 n_left_from, next_index, * from, * to_next;
53 pppoe_main_t * pem = &pppoe_main;
54 vnet_main_t * vnm = pem->vnet_main;
55 vnet_interface_main_t * im = &vnm->interface_main;
56 u32 pkts_decapsulated = 0;
57 u32 thread_index = vlib_get_thread_index();
58 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
59 pppoe_entry_key_t cached_key;
60 pppoe_entry_result_t cached_result;
62 from = vlib_frame_vector_args (from_frame);
63 n_left_from = from_frame->n_vectors;
65 /* Clear the one-entry cache in case session table was updated */
67 cached_result.raw = ~0; /* warning be gone */
69 next_index = node->cached_next_index;
70 stats_sw_if_index = node->runtime_data[0];
71 stats_n_packets = stats_n_bytes = 0;
73 while (n_left_from > 0)
77 vlib_get_next_frame (vm, node, next_index,
78 to_next, n_left_to_next);
79 while (n_left_from >= 4 && n_left_to_next >= 2)
82 vlib_buffer_t * b0, * b1;
84 ethernet_header_t *h0, *h1;
85 ethernet_vlan_header_t *vlan0 = 0, *vlan1 = 0;
86 pppoe_header_t * pppoe0, * pppoe1;
87 u16 ppp_proto0 = 0, ppp_proto1 = 0;
88 pppoe_session_t * t0, * t1;
89 u32 error0 = 0, error1 = 0;
90 u32 sw_if_index0, sw_if_index1, len0, len1;
91 pppoe_entry_key_t key0, key1;
92 pppoe_entry_result_t result0, result1;
96 /* Prefetch next iteration. */
98 vlib_buffer_t * p2, * p3;
100 p2 = vlib_get_buffer (vm, from[2]);
101 p3 = vlib_get_buffer (vm, from[3]);
103 vlib_prefetch_buffer_header (p2, LOAD);
104 vlib_prefetch_buffer_header (p3, LOAD);
106 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
107 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
119 b0 = vlib_get_buffer (vm, bi0);
120 b1 = vlib_get_buffer (vm, bi1);
125 vlib_buffer_reset(b0);
126 h0 = vlib_buffer_get_current (b0);
128 /* get pppoe header */
129 type0 = clib_net_to_host_u16(h0->type);
130 if(type0 == ETHERNET_TYPE_VLAN){
131 vlan0 = (ethernet_vlan_header_t *)(h0+1);
132 type0 = clib_net_to_host_u16(vlan0->type);
133 pppoe0 = (pppoe_header_t*)(vlan0+1);
134 if( type0 != ETHERNET_TYPE_PPPOE_DISCOVERY && type0 != ETHERNET_TYPE_PPPOE_SESSION ) {
135 error0 = PPPOE_ERROR_BAD_VER_TYPE;
136 result0.fields.session_index =
137 ~0; // avoid tracing random data
138 next0 = PPPOE_INPUT_NEXT_DROP;
142 pppoe0 = (pppoe_header_t*)(h0+1);
145 ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
147 /* Manipulate packet 0 */
148 if ((ppp_proto0 != PPP_PROTOCOL_ip4)
149 && (ppp_proto0 != PPP_PROTOCOL_ip6))
152 vlib_buffer_advance(b0, sizeof(*h0))
154 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0));
155 error0 = PPPOE_ERROR_CONTROL_PLANE;
156 next0 = PPPOE_INPUT_NEXT_CP_INPUT;
161 pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
162 h0->src_address, pppoe0->session_id,
163 &key0, &bucket0, &result0);
164 if (PREDICT_FALSE (result0.fields.session_index == ~0))
166 error0 = PPPOE_ERROR_NO_SUCH_SESSION;
167 next0 = PPPOE_INPUT_NEXT_DROP;
171 t0 = pool_elt_at_index (pem->sessions,
172 result0.fields.session_index);
174 /* Pop Eth and PPPoE header */
176 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0))
178 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0)+sizeof(*pppoe0));
180 next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
181 PPPOE_INPUT_NEXT_IP4_INPUT
182 : PPPOE_INPUT_NEXT_IP6_INPUT;
184 sw_if_index0 = t0->sw_if_index;
185 len0 = vlib_buffer_length_in_chain (vm, b0);
186 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
188 pkts_decapsulated ++;
189 stats_n_packets += 1;
190 stats_n_bytes += len0;
192 /* Batch stats increment on the same pppoe session so counter
193 is not incremented per packet */
194 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
196 stats_n_packets -= 1;
197 stats_n_bytes -= len0;
199 vlib_increment_combined_counter
200 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
201 thread_index, stats_sw_if_index,
202 stats_n_packets, stats_n_bytes);
204 stats_n_bytes = len0;
205 stats_sw_if_index = sw_if_index0;
209 b0->error = error0 ? node->errors[error0] : 0;
211 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
214 = vlib_add_trace (vm, node, b0, sizeof (*tr));
215 tr->next_index = next0;
217 tr->session_index = result0.fields.session_index;
218 tr->session_id = clib_net_to_host_u32(pppoe0->session_id);
222 vlib_buffer_reset(b1);
223 h1 = vlib_buffer_get_current (b1);
225 /* get pppoe header */
226 type1 = clib_net_to_host_u16(h1->type);
227 if(type1 == ETHERNET_TYPE_VLAN){
228 vlan1 = (ethernet_vlan_header_t *)(h1+1);
229 type1 = clib_net_to_host_u16(vlan1->type);
230 pppoe1 = (pppoe_header_t*)(vlan1+1);
231 if( type1 != ETHERNET_TYPE_PPPOE_DISCOVERY && type1 != ETHERNET_TYPE_PPPOE_SESSION ) {
232 error1 = PPPOE_ERROR_BAD_VER_TYPE;
233 result1.fields.session_index =
234 ~0; // avoid tracing random data
235 next1 = PPPOE_INPUT_NEXT_DROP;
239 pppoe1 = (pppoe_header_t*)(h1+1);
242 ppp_proto1 = clib_net_to_host_u16(pppoe1->ppp_proto);
244 /* Manipulate packet 1 */
245 if ((ppp_proto1 != PPP_PROTOCOL_ip4)
246 && (ppp_proto1 != PPP_PROTOCOL_ip6))
249 vlib_buffer_advance(b1, sizeof(*h1))
251 vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*vlan1));
252 error1 = PPPOE_ERROR_CONTROL_PLANE;
253 next1 = PPPOE_INPUT_NEXT_CP_INPUT;
257 pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
258 h1->src_address, pppoe1->session_id,
259 &key1, &bucket1, &result1);
260 if (PREDICT_FALSE (result1.fields.session_index == ~0))
262 error1 = PPPOE_ERROR_NO_SUCH_SESSION;
263 next1 = PPPOE_INPUT_NEXT_DROP;
267 t1 = pool_elt_at_index (pem->sessions,
268 result1.fields.session_index);
270 /* Pop Eth and PPPoE header */
272 vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*pppoe1))
274 vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*vlan1)+sizeof(*pppoe1));
276 next1 = (ppp_proto1==PPP_PROTOCOL_ip4)?
277 PPPOE_INPUT_NEXT_IP4_INPUT
278 : PPPOE_INPUT_NEXT_IP6_INPUT;
280 sw_if_index1 = t1->sw_if_index;
281 len1 = vlib_buffer_length_in_chain (vm, b1);
282 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
284 pkts_decapsulated ++;
285 stats_n_packets += 1;
286 stats_n_bytes += len1;
288 /* Batch stats increment on the same pppoe session so counter
289 is not incremented per packet */
290 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
292 stats_n_packets -= 1;
293 stats_n_bytes -= len1;
295 vlib_increment_combined_counter
296 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
297 thread_index, stats_sw_if_index,
298 stats_n_packets, stats_n_bytes);
300 stats_n_bytes = len1;
301 stats_sw_if_index = sw_if_index1;
305 b1->error = error1 ? node->errors[error1] : 0;
307 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
310 = vlib_add_trace (vm, node, b1, sizeof (*tr));
311 tr->next_index = next1;
313 tr->session_index = result1.fields.session_index;
314 tr->session_id = clib_net_to_host_u32(pppoe1->session_id);
317 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
318 to_next, n_left_to_next,
319 bi0, bi1, next0, next1);
322 while (n_left_from > 0 && n_left_to_next > 0)
327 ethernet_header_t *h0;
328 ethernet_vlan_header_t *vlan0 = 0;
329 pppoe_header_t * pppoe0;
331 pppoe_session_t * t0;
333 u32 sw_if_index0, len0;
334 pppoe_entry_key_t key0;
335 pppoe_entry_result_t result0;
346 b0 = vlib_get_buffer (vm, bi0);
350 vlib_buffer_reset(b0);
351 h0 = vlib_buffer_get_current (b0);
353 /* get pppoe header */
354 type0 = clib_net_to_host_u16(h0->type);
355 if(type0 == ETHERNET_TYPE_VLAN){
356 vlan0 = (ethernet_vlan_header_t *)(h0+1);
357 type0 = clib_net_to_host_u16(vlan0->type);
358 pppoe0 = (pppoe_header_t*)(vlan0+1);
359 if( type0 != ETHERNET_TYPE_PPPOE_DISCOVERY && type0 != ETHERNET_TYPE_PPPOE_SESSION ) {
360 error0 = PPPOE_ERROR_BAD_VER_TYPE;
361 result0.fields.session_index =
362 ~0; // avoid tracing random data
363 next0 = PPPOE_INPUT_NEXT_DROP;
367 pppoe0 = (pppoe_header_t*)(h0+1);
370 ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
372 if ((ppp_proto0 != PPP_PROTOCOL_ip4)
373 && (ppp_proto0 != PPP_PROTOCOL_ip6))
376 vlib_buffer_advance(b0, sizeof(*h0))
378 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0));
379 error0 = PPPOE_ERROR_CONTROL_PLANE;
380 next0 = PPPOE_INPUT_NEXT_CP_INPUT;
384 pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
385 h0->src_address, pppoe0->session_id,
386 &key0, &bucket0, &result0);
387 if (PREDICT_FALSE (result0.fields.session_index == ~0))
389 error0 = PPPOE_ERROR_NO_SUCH_SESSION;
390 next0 = PPPOE_INPUT_NEXT_DROP;
394 t0 = pool_elt_at_index (pem->sessions,
395 result0.fields.session_index);
397 /* Pop Eth and PPPoE header */
399 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0))
401 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0)+sizeof(*pppoe0));
403 next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
404 PPPOE_INPUT_NEXT_IP4_INPUT
405 : PPPOE_INPUT_NEXT_IP6_INPUT;
407 sw_if_index0 = t0->sw_if_index;
408 len0 = vlib_buffer_length_in_chain (vm, b0);
409 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
411 pkts_decapsulated ++;
412 stats_n_packets += 1;
413 stats_n_bytes += len0;
415 /* Batch stats increment on the same pppoe session so counter
416 is not incremented per packet */
417 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
419 stats_n_packets -= 1;
420 stats_n_bytes -= len0;
422 vlib_increment_combined_counter
423 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
424 thread_index, stats_sw_if_index,
425 stats_n_packets, stats_n_bytes);
427 stats_n_bytes = len0;
428 stats_sw_if_index = sw_if_index0;
432 b0->error = error0 ? node->errors[error0] : 0;
434 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
437 = vlib_add_trace (vm, node, b0, sizeof (*tr));
438 tr->next_index = next0;
440 tr->session_index = result0.fields.session_index;
441 tr->session_id = clib_net_to_host_u16(pppoe0->session_id);
443 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
444 to_next, n_left_to_next,
448 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
450 /* Do we still need this now that session tx stats is kept? */
451 vlib_node_increment_counter (vm, pppoe_input_node.index,
452 PPPOE_ERROR_DECAPSULATED,
455 /* Increment any remaining batch stats */
458 vlib_increment_combined_counter
459 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
460 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
461 node->runtime_data[0] = stats_sw_if_index;
464 return from_frame->n_vectors;
467 #ifndef CLIB_MARCH_VARIANT
468 char * pppoe_error_strings[] = {
469 #define pppoe_error(n,s) s,
470 #include <pppoe/pppoe_error.def>
474 #endif /* CLIB_MARCH_VARIANT */
476 VLIB_REGISTER_NODE (pppoe_input_node) = {
477 .name = "pppoe-input",
478 /* Takes a vector of packets. */
479 .vector_size = sizeof (u32),
481 .n_errors = PPPOE_N_ERROR,
482 .error_strings = pppoe_error_strings,
484 .n_next_nodes = PPPOE_INPUT_N_NEXT,
486 #define _(s,n) [PPPOE_INPUT_NEXT_##s] = n,
487 foreach_pppoe_input_next
491 .format_trace = format_pppoe_rx_trace,
495 VNET_FEATURE_INIT (pppoe_input_node, static) =
497 .arc_name = "device-input",
498 .node_name = "pppoe-input",
499 .runs_before = VNET_FEATURES ("ethernet-input"),