pppoe: make pppoe plugin work with dot1q subinterfaces
[vpp.git] / src / plugins / pppoe / pppoe_decap.c
1 /*
2  * decap.c: pppoe session decap packet processing
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/ppp/packet.h>
20 #include <pppoe/pppoe.h>
21
22 typedef struct {
23   u32 next_index;
24   u32 session_index;
25   u32 session_id;
26   u32 error;
27 } pppoe_rx_trace_t;
28
29 static u8 * format_pppoe_rx_trace (u8 * s, va_list * args)
30 {
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 *);
34
35   if (t->session_index != ~0)
36     {
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);
39     }
40   else
41     {
42       s = format (s, "PPPoE decap error - session for session_id %d does not exist",
43                   t->session_id);
44     }
45   return s;
46 }
47
48 VLIB_NODE_FN (pppoe_input_node) (vlib_main_t * vm,
49              vlib_node_runtime_t * node,
50              vlib_frame_t * from_frame)
51 {
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;
61
62   from = vlib_frame_vector_args (from_frame);
63   n_left_from = from_frame->n_vectors;
64
65   /* Clear the one-entry cache in case session table was updated */
66   cached_key.raw = ~0;
67   cached_result.raw = ~0;       /* warning be gone */
68
69   next_index = node->cached_next_index;
70   stats_sw_if_index = node->runtime_data[0];
71   stats_n_packets = stats_n_bytes = 0;
72
73   while (n_left_from > 0)
74     {
75       u32 n_left_to_next;
76
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)
80         {
81           u32 bi0, bi1;
82           vlib_buffer_t * b0, * b1;
83           u32 next0, next1;
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;
93           u32 bucket0, bucket1;
94           u16 type0, type1;
95
96           /* Prefetch next iteration. */
97           {
98             vlib_buffer_t * p2, * p3;
99
100             p2 = vlib_get_buffer (vm, from[2]);
101             p3 = vlib_get_buffer (vm, from[3]);
102
103             vlib_prefetch_buffer_header (p2, LOAD);
104             vlib_prefetch_buffer_header (p3, LOAD);
105
106             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
107             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
108           }
109
110           bi0 = from[0];
111           bi1 = from[1];
112           to_next[0] = bi0;
113           to_next[1] = bi1;
114           from += 2;
115           to_next += 2;
116           n_left_to_next -= 2;
117           n_left_from -= 2;
118
119           b0 = vlib_get_buffer (vm, bi0);
120           b1 = vlib_get_buffer (vm, bi1);
121           error0 = 0;
122           error1 = 0;
123
124                   /* get client mac */
125           vlib_buffer_reset(b0);
126           h0 = vlib_buffer_get_current (b0);
127
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                         next0 = PPPOE_INPUT_NEXT_DROP;
137                         goto trace0;
138                           }
139                   } else {
140                           pppoe0 = (pppoe_header_t*)(h0+1);
141                   }
142
143           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);          
144
145           /* Manipulate packet 0 */
146           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
147              && (ppp_proto0 != PPP_PROTOCOL_ip6))
148             {
149                   vlan0 == 0 ?
150                     vlib_buffer_advance(b0, sizeof(*h0))
151               :
152                     vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0));
153               error0 = PPPOE_ERROR_CONTROL_PLANE;
154               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
155               goto trace0;
156             }
157
158
159           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
160                           h0->src_address, pppoe0->session_id,
161                           &key0, &bucket0, &result0);
162           if (PREDICT_FALSE (result0.fields.session_index == ~0))
163             {
164               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
165               next0 = PPPOE_INPUT_NEXT_DROP;
166               goto trace0;
167             }
168
169           t0 = pool_elt_at_index (pem->sessions,
170                                   result0.fields.session_index);
171
172           /* Pop Eth and PPPoE header */
173           vlan0 == 0 ?
174                 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0))
175           :
176                 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0)+sizeof(*pppoe0));
177
178           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
179                   PPPOE_INPUT_NEXT_IP4_INPUT
180                   : PPPOE_INPUT_NEXT_IP6_INPUT;
181
182           sw_if_index0 = t0->sw_if_index;
183           len0 = vlib_buffer_length_in_chain (vm, b0);
184
185           pkts_decapsulated ++;
186           stats_n_packets += 1;
187           stats_n_bytes += len0;
188
189           /* Batch stats increment on the same pppoe session so counter
190              is not incremented per packet */
191           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
192             {
193               stats_n_packets -= 1;
194               stats_n_bytes -= len0;
195               if (stats_n_packets)
196                 vlib_increment_combined_counter
197                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
198                    thread_index, stats_sw_if_index,
199                    stats_n_packets, stats_n_bytes);
200               stats_n_packets = 1;
201               stats_n_bytes = len0;
202               stats_sw_if_index = sw_if_index0;
203             }
204
205         trace0:
206           b0->error = error0 ? node->errors[error0] : 0;
207
208           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
209             {
210               pppoe_rx_trace_t *tr
211                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
212               tr->next_index = next0;
213               tr->error = error0;
214               tr->session_index = result0.fields.session_index;
215               tr->session_id = clib_net_to_host_u32(pppoe0->session_id);
216             }
217
218           /* get client mac */
219           vlib_buffer_reset(b1);
220           h1 = vlib_buffer_get_current (b1);
221
222                   /* get pppoe header */
223                   type1 = clib_net_to_host_u16(h1->type);
224                   if(type1 == ETHERNET_TYPE_VLAN){
225                           vlan1 = (ethernet_vlan_header_t *)(h1+1);
226                           type1 = clib_net_to_host_u16(vlan1->type);
227                           pppoe1 = (pppoe_header_t*)(vlan1+1);
228                           if( type1 != ETHERNET_TYPE_PPPOE_DISCOVERY && type1 != ETHERNET_TYPE_PPPOE_SESSION ) {
229                                 error1 = PPPOE_ERROR_BAD_VER_TYPE;
230                         next1 = PPPOE_INPUT_NEXT_DROP;
231                         goto trace1;
232                           }
233                   } else {
234                           pppoe1 = (pppoe_header_t*)(h1+1);
235                   }
236
237                   ppp_proto1 = clib_net_to_host_u16(pppoe1->ppp_proto);
238
239           /* Manipulate packet 1 */
240           if ((ppp_proto1 != PPP_PROTOCOL_ip4)
241              && (ppp_proto1 != PPP_PROTOCOL_ip6))
242             {
243                   vlan1 == 0 ?
244                     vlib_buffer_advance(b1, sizeof(*h1))
245               :
246                     vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*vlan1));
247               error1 = PPPOE_ERROR_CONTROL_PLANE;
248               next1 = PPPOE_INPUT_NEXT_CP_INPUT;
249               goto trace1;
250             }
251
252           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
253                           h1->src_address, pppoe1->session_id,
254                           &key1, &bucket1, &result1);
255           if (PREDICT_FALSE (result1.fields.session_index == ~0))
256             {
257               error1 = PPPOE_ERROR_NO_SUCH_SESSION;
258               next1 = PPPOE_INPUT_NEXT_DROP;
259               goto trace1;
260             }
261
262           t1 = pool_elt_at_index (pem->sessions,
263                                   result1.fields.session_index);
264
265           /* Pop Eth and PPPoE header */
266           vlan1 == 0 ?
267                 vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*pppoe1))
268           :
269                 vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*vlan1)+sizeof(*pppoe1));
270
271           next1 = (ppp_proto1==PPP_PROTOCOL_ip4)?
272                   PPPOE_INPUT_NEXT_IP4_INPUT
273                   : PPPOE_INPUT_NEXT_IP6_INPUT;
274
275           sw_if_index1 = t1->sw_if_index;
276           len1 = vlib_buffer_length_in_chain (vm, b1);
277
278           pkts_decapsulated ++;
279           stats_n_packets += 1;
280           stats_n_bytes += len1;
281
282           /* Batch stats increment on the same pppoe session so counter
283              is not incremented per packet */
284           if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
285             {
286               stats_n_packets -= 1;
287               stats_n_bytes -= len1;
288               if (stats_n_packets)
289                 vlib_increment_combined_counter
290                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
291                    thread_index, stats_sw_if_index,
292                    stats_n_packets, stats_n_bytes);
293               stats_n_packets = 1;
294               stats_n_bytes = len1;
295               stats_sw_if_index = sw_if_index1;
296             }
297
298         trace1:
299           b1->error = error1 ? node->errors[error1] : 0;
300
301           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
302             {
303               pppoe_rx_trace_t *tr
304                 = vlib_add_trace (vm, node, b1, sizeof (*tr));
305               tr->next_index = next1;
306               tr->error = error1;
307               tr->session_index = result1.fields.session_index;
308               tr->session_id = clib_net_to_host_u32(pppoe1->session_id);
309             }
310
311           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
312                                            to_next, n_left_to_next,
313                                            bi0, bi1, next0, next1);
314         }
315
316       while (n_left_from > 0 && n_left_to_next > 0)
317         {
318           u32 bi0;
319           vlib_buffer_t * b0;
320           u32 next0;
321           ethernet_header_t *h0;
322           ethernet_vlan_header_t *vlan0 = 0;
323           pppoe_header_t * pppoe0;
324           u16 ppp_proto0 = 0;
325           pppoe_session_t * t0;
326           u32 error0;
327           u32 sw_if_index0, len0;
328           pppoe_entry_key_t key0;
329           pppoe_entry_result_t result0;
330           u32 bucket0;
331           u32 type0;
332
333           bi0 = from[0];
334           to_next[0] = bi0;
335           from += 1;
336           to_next += 1;
337           n_left_from -= 1;
338           n_left_to_next -= 1;
339
340           b0 = vlib_get_buffer (vm, bi0);
341           error0 = 0;
342
343           /* get client mac */
344           vlib_buffer_reset(b0);
345           h0 = vlib_buffer_get_current (b0);
346
347                   /* get pppoe header */
348                   type0 = clib_net_to_host_u16(h0->type);
349                   if(type0 == ETHERNET_TYPE_VLAN){
350                           vlan0 = (ethernet_vlan_header_t *)(h0+1);
351                           type0 = clib_net_to_host_u16(vlan0->type);
352                           pppoe0 = (pppoe_header_t*)(vlan0+1);
353                           if( type0 != ETHERNET_TYPE_PPPOE_DISCOVERY && type0 != ETHERNET_TYPE_PPPOE_SESSION ) {
354                                 error0 = PPPOE_ERROR_BAD_VER_TYPE;
355                         next0 = PPPOE_INPUT_NEXT_DROP;
356                         goto trace00;
357                           }
358                   } else {
359                           pppoe0 = (pppoe_header_t*)(h0+1);
360                   }
361
362           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);   
363
364           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
365              && (ppp_proto0 != PPP_PROTOCOL_ip6))
366             {
367                   vlan0 == 0 ?
368                     vlib_buffer_advance(b0, sizeof(*h0))
369               :
370                     vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0));
371               error0 = PPPOE_ERROR_CONTROL_PLANE;
372               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
373               goto trace00;
374             }
375
376           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
377                           h0->src_address, pppoe0->session_id,
378                           &key0, &bucket0, &result0);
379           if (PREDICT_FALSE (result0.fields.session_index == ~0))
380             {
381               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
382               next0 = PPPOE_INPUT_NEXT_DROP;
383               goto trace00;
384             }
385
386           t0 = pool_elt_at_index (pem->sessions,
387                                   result0.fields.session_index);
388
389           /* Pop Eth and PPPoE header */
390           vlan0 == 0 ?
391                 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0))
392           :
393                 vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*vlan0)+sizeof(*pppoe0));
394
395           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
396                   PPPOE_INPUT_NEXT_IP4_INPUT
397                   : PPPOE_INPUT_NEXT_IP6_INPUT;
398
399           sw_if_index0 = t0->sw_if_index;
400           len0 = vlib_buffer_length_in_chain (vm, b0);
401
402           pkts_decapsulated ++;
403           stats_n_packets += 1;
404           stats_n_bytes += len0;
405
406           /* Batch stats increment on the same pppoe session so counter
407              is not incremented per packet */
408           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
409             {
410               stats_n_packets -= 1;
411               stats_n_bytes -= len0;
412               if (stats_n_packets)
413                 vlib_increment_combined_counter
414                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
415                    thread_index, stats_sw_if_index,
416                    stats_n_packets, stats_n_bytes);
417               stats_n_packets = 1;
418               stats_n_bytes = len0;
419               stats_sw_if_index = sw_if_index0;
420             }
421
422         trace00:
423           b0->error = error0 ? node->errors[error0] : 0;
424
425           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
426             {
427               pppoe_rx_trace_t *tr
428                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
429               tr->next_index = next0;
430               tr->error = error0;
431               tr->session_index = result0.fields.session_index;
432               tr->session_id = clib_net_to_host_u16(pppoe0->session_id);
433             }
434           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
435                                            to_next, n_left_to_next,
436                                            bi0, next0);
437         }
438
439       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
440     }
441   /* Do we still need this now that session tx stats is kept? */
442   vlib_node_increment_counter (vm, pppoe_input_node.index,
443                                PPPOE_ERROR_DECAPSULATED,
444                                pkts_decapsulated);
445
446   /* Increment any remaining batch stats */
447   if (stats_n_packets)
448     {
449       vlib_increment_combined_counter
450         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
451          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
452       node->runtime_data[0] = stats_sw_if_index;
453     }
454
455   return from_frame->n_vectors;
456 }
457
458 #ifndef CLIB_MARCH_VARIANT
459 char * pppoe_error_strings[] = {
460 #define pppoe_error(n,s) s,
461 #include <pppoe/pppoe_error.def>
462 #undef pppoe_error
463 #undef _
464 };
465 #endif /* CLIB_MARCH_VARIANT */
466
467 VLIB_REGISTER_NODE (pppoe_input_node) = {
468   .name = "pppoe-input",
469   /* Takes a vector of packets. */
470   .vector_size = sizeof (u32),
471
472   .n_errors = PPPOE_N_ERROR,
473   .error_strings = pppoe_error_strings,
474
475   .n_next_nodes = PPPOE_INPUT_N_NEXT,
476   .next_nodes = {
477 #define _(s,n) [PPPOE_INPUT_NEXT_##s] = n,
478     foreach_pppoe_input_next
479 #undef _
480   },
481
482   .format_trace = format_pppoe_rx_trace,
483 };
484
485 /* *INDENT-OFF* */
486 VNET_FEATURE_INIT (pppoe_input_node, static) =
487 {
488   .arc_name = "device-input",
489   .node_name = "pppoe-input",
490   .runs_before = VNET_FEATURES ("ethernet-input"),
491 };
492 /* *INDENT-ON */