02c82711ddc83d2b2ce846c2dbe357d61b9ccae5
[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/pg/pg.h>
20 #include <vnet/ppp/packet.h>
21 #include <pppoe/pppoe.h>
22
23 typedef struct {
24   u32 next_index;
25   u32 session_index;
26   u32 session_id;
27   u32 error;
28 } pppoe_rx_trace_t;
29
30 static u8 * format_pppoe_rx_trace (u8 * s, va_list * args)
31 {
32   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34   pppoe_rx_trace_t * t = va_arg (*args, pppoe_rx_trace_t *);
35
36   if (t->session_index != ~0)
37     {
38       s = format (s, "PPPoE decap from pppoe_session%d session_id %d next %d error %d",
39                   t->session_index, t->session_id, t->next_index, t->error);
40     }
41   else
42     {
43       s = format (s, "PPPoE decap error - session for session_id %d does not exist",
44                   t->session_id);
45     }
46   return s;
47 }
48
49 static uword
50 pppoe_input (vlib_main_t * vm,
51              vlib_node_runtime_t * node,
52              vlib_frame_t * from_frame)
53 {
54   u32 n_left_from, next_index, * from, * to_next;
55   pppoe_main_t * pem = &pppoe_main;
56   vnet_main_t * vnm = pem->vnet_main;
57   vnet_interface_main_t * im = &vnm->interface_main;
58   u32 pkts_decapsulated = 0;
59   u32 thread_index = vlib_get_thread_index();
60   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
61   pppoe_entry_key_t cached_key;
62   pppoe_entry_result_t cached_result;
63
64   from = vlib_frame_vector_args (from_frame);
65   n_left_from = from_frame->n_vectors;
66
67   /* Clear the one-entry cache in case session table was updated */
68   cached_key.raw = ~0;
69   cached_result.raw = ~0;       /* warning be gone */
70
71   next_index = node->cached_next_index;
72   stats_sw_if_index = node->runtime_data[0];
73   stats_n_packets = stats_n_bytes = 0;
74
75   while (n_left_from > 0)
76     {
77       u32 n_left_to_next;
78
79       vlib_get_next_frame (vm, node, next_index,
80                            to_next, n_left_to_next);
81       while (n_left_from >= 4 && n_left_to_next >= 2)
82         {
83           u32 bi0, bi1;
84           vlib_buffer_t * b0, * b1;
85           u32 next0, next1;
86           ethernet_header_t *h0, *h1;
87           pppoe_header_t * pppoe0, * pppoe1;
88           u16 ppp_proto0 = 0, ppp_proto1 = 0;
89           pppoe_session_t * t0, * t1;
90           u32 error0, error1;
91           u32 sw_if_index0, sw_if_index1, len0, len1;
92           pppoe_entry_key_t key0, key1;
93           pppoe_entry_result_t result0, result1;
94           u32 bucket0, bucket1;
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           /* leaves current_data pointing at the pppoe header */
125           pppoe0 = vlib_buffer_get_current (b0);
126           pppoe1 = vlib_buffer_get_current (b1);
127           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
128           ppp_proto1 = clib_net_to_host_u16(pppoe1->ppp_proto);
129
130           /* Manipulate packet 0 */
131           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
132              && (ppp_proto0 != PPP_PROTOCOL_ip6))
133             {
134               error0 = PPPOE_ERROR_CONTROL_PLANE;
135               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
136               goto trace0;
137             }
138
139           /* get client mac */
140           vlib_buffer_reset(b0);
141           h0 = vlib_buffer_get_current (b0);
142
143           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
144                           h0->src_address, pppoe0->session_id,
145                           &key0, &bucket0, &result0);
146           if (PREDICT_FALSE (result0.fields.session_index == ~0))
147             {
148               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
149               next0 = PPPOE_INPUT_NEXT_DROP;
150               goto trace0;
151             }
152
153           t0 = pool_elt_at_index (pem->sessions,
154                                   result0.fields.session_index);
155
156           /* Pop Eth and PPPPoE header */
157           vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0));
158
159           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
160                   PPPOE_INPUT_NEXT_IP4_INPUT
161                   : PPPOE_INPUT_NEXT_IP6_INPUT;
162
163           sw_if_index0 = t0->sw_if_index;
164           len0 = vlib_buffer_length_in_chain (vm, b0);
165
166           pkts_decapsulated ++;
167           stats_n_packets += 1;
168           stats_n_bytes += len0;
169
170           /* Batch stats increment on the same pppoe session so counter
171              is not incremented per packet */
172           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
173             {
174               stats_n_packets -= 1;
175               stats_n_bytes -= len0;
176               if (stats_n_packets)
177                 vlib_increment_combined_counter
178                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
179                    thread_index, stats_sw_if_index,
180                    stats_n_packets, stats_n_bytes);
181               stats_n_packets = 1;
182               stats_n_bytes = len0;
183               stats_sw_if_index = sw_if_index0;
184             }
185
186         trace0:
187           b0->error = error0 ? node->errors[error0] : 0;
188
189           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
190             {
191               pppoe_rx_trace_t *tr
192                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
193               tr->next_index = next0;
194               tr->error = error0;
195               tr->session_index = result0.fields.session_index;
196               tr->session_id = clib_net_to_host_u32(pppoe0->session_id);
197             }
198
199
200           /* Manipulate packet 1 */
201           if ((ppp_proto1 != PPP_PROTOCOL_ip4)
202              && (ppp_proto1 != PPP_PROTOCOL_ip6))
203             {
204               error1 = PPPOE_ERROR_CONTROL_PLANE;
205               next1 = PPPOE_INPUT_NEXT_CP_INPUT;
206               goto trace1;
207             }
208
209           /* get client mac */
210           vlib_buffer_reset(b1);
211           h1 = vlib_buffer_get_current (b1);
212
213           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
214                           h1->src_address, pppoe1->session_id,
215                           &key1, &bucket1, &result1);
216           if (PREDICT_FALSE (result1.fields.session_index == ~0))
217             {
218               error1 = PPPOE_ERROR_NO_SUCH_SESSION;
219               next1 = PPPOE_INPUT_NEXT_DROP;
220               goto trace1;
221             }
222
223           t1 = pool_elt_at_index (pem->sessions,
224                                   result1.fields.session_index);
225
226           /* Pop Eth and PPPPoE header */
227           vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*pppoe1));
228
229           next1 = (ppp_proto1==PPP_PROTOCOL_ip4)?
230                   PPPOE_INPUT_NEXT_IP4_INPUT
231                   : PPPOE_INPUT_NEXT_IP6_INPUT;
232
233           sw_if_index1 = t1->sw_if_index;
234           len1 = vlib_buffer_length_in_chain (vm, b1);
235
236           pkts_decapsulated ++;
237           stats_n_packets += 1;
238           stats_n_bytes += len1;
239
240           /* Batch stats increment on the same pppoe session so counter
241              is not incremented per packet */
242           if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
243             {
244               stats_n_packets -= 1;
245               stats_n_bytes -= len1;
246               if (stats_n_packets)
247                 vlib_increment_combined_counter
248                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
249                    thread_index, stats_sw_if_index,
250                    stats_n_packets, stats_n_bytes);
251               stats_n_packets = 1;
252               stats_n_bytes = len1;
253               stats_sw_if_index = sw_if_index1;
254             }
255
256         trace1:
257           b1->error = error1 ? node->errors[error1] : 0;
258
259           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
260             {
261               pppoe_rx_trace_t *tr
262                 = vlib_add_trace (vm, node, b1, sizeof (*tr));
263               tr->next_index = next1;
264               tr->error = error1;
265               tr->session_index = result1.fields.session_index;
266               tr->session_id = clib_net_to_host_u32(pppoe1->session_id);
267             }
268
269           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
270                                            to_next, n_left_to_next,
271                                            bi0, bi1, next0, next1);
272         }
273
274       while (n_left_from > 0 && n_left_to_next > 0)
275         {
276           u32 bi0;
277           vlib_buffer_t * b0;
278           u32 next0;
279           ethernet_header_t *h0;
280           pppoe_header_t * pppoe0;
281           u16 ppp_proto0 = 0;
282           pppoe_session_t * t0;
283           u32 error0;
284           u32 sw_if_index0, len0;
285           pppoe_entry_key_t key0;
286           pppoe_entry_result_t result0;
287           u32 bucket0;
288
289           bi0 = from[0];
290           to_next[0] = bi0;
291           from += 1;
292           to_next += 1;
293           n_left_from -= 1;
294           n_left_to_next -= 1;
295
296           b0 = vlib_get_buffer (vm, bi0);
297           error0 = 0;
298
299           /* leaves current_data pointing at the pppoe header */
300           pppoe0 = vlib_buffer_get_current (b0);
301           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
302
303           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
304              && (ppp_proto0 != PPP_PROTOCOL_ip6))
305             {
306               error0 = PPPOE_ERROR_CONTROL_PLANE;
307               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
308               goto trace00;
309             }
310
311           /* get client mac */
312           vlib_buffer_reset(b0);
313           h0 = vlib_buffer_get_current (b0);
314
315           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
316                           h0->src_address, pppoe0->session_id,
317                           &key0, &bucket0, &result0);
318           if (PREDICT_FALSE (result0.fields.session_index == ~0))
319             {
320               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
321               next0 = PPPOE_INPUT_NEXT_DROP;
322               goto trace00;
323             }
324
325           t0 = pool_elt_at_index (pem->sessions,
326                                   result0.fields.session_index);
327
328           /* Pop Eth and PPPPoE header */
329           vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0));
330
331           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
332                   PPPOE_INPUT_NEXT_IP4_INPUT
333                   : PPPOE_INPUT_NEXT_IP6_INPUT;
334
335           sw_if_index0 = t0->sw_if_index;
336           len0 = vlib_buffer_length_in_chain (vm, b0);
337
338           pkts_decapsulated ++;
339           stats_n_packets += 1;
340           stats_n_bytes += len0;
341
342           /* Batch stats increment on the same pppoe session so counter
343              is not incremented per packet */
344           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
345             {
346               stats_n_packets -= 1;
347               stats_n_bytes -= len0;
348               if (stats_n_packets)
349                 vlib_increment_combined_counter
350                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
351                    thread_index, stats_sw_if_index,
352                    stats_n_packets, stats_n_bytes);
353               stats_n_packets = 1;
354               stats_n_bytes = len0;
355               stats_sw_if_index = sw_if_index0;
356             }
357
358         trace00:
359           b0->error = error0 ? node->errors[error0] : 0;
360
361           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
362             {
363               pppoe_rx_trace_t *tr
364                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
365               tr->next_index = next0;
366               tr->error = error0;
367               tr->session_index = result0.fields.session_index;
368               tr->session_id = clib_net_to_host_u16(pppoe0->session_id);
369             }
370           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
371                                            to_next, n_left_to_next,
372                                            bi0, next0);
373         }
374
375       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
376     }
377   /* Do we still need this now that session tx stats is kept? */
378   vlib_node_increment_counter (vm, pppoe_input_node.index,
379                                PPPOE_ERROR_DECAPSULATED,
380                                pkts_decapsulated);
381
382   /* Increment any remaining batch stats */
383   if (stats_n_packets)
384     {
385       vlib_increment_combined_counter
386         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
387          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
388       node->runtime_data[0] = stats_sw_if_index;
389     }
390
391   return from_frame->n_vectors;
392 }
393
394 static char * pppoe_error_strings[] = {
395 #define pppoe_error(n,s) s,
396 #include <pppoe/pppoe_error.def>
397 #undef pppoe_error
398 #undef _
399 };
400
401 VLIB_REGISTER_NODE (pppoe_input_node) = {
402   .function = pppoe_input,
403   .name = "pppoe-input",
404   /* Takes a vector of packets. */
405   .vector_size = sizeof (u32),
406
407   .n_errors = PPPOE_N_ERROR,
408   .error_strings = pppoe_error_strings,
409
410   .n_next_nodes = PPPOE_INPUT_N_NEXT,
411   .next_nodes = {
412 #define _(s,n) [PPPOE_INPUT_NEXT_##s] = n,
413     foreach_pppoe_input_next
414 #undef _
415   },
416
417   .format_trace = format_pppoe_rx_trace,
418 };
419
420 VLIB_NODE_FUNCTION_MULTIARCH (pppoe_input_node, pppoe_input)
421
422