misc: Purge unused pg includes
[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           pppoe_header_t * pppoe0, * pppoe1;
86           u16 ppp_proto0 = 0, ppp_proto1 = 0;
87           pppoe_session_t * t0, * t1;
88           u32 error0, error1;
89           u32 sw_if_index0, sw_if_index1, len0, len1;
90           pppoe_entry_key_t key0, key1;
91           pppoe_entry_result_t result0, result1;
92           u32 bucket0, bucket1;
93
94           /* Prefetch next iteration. */
95           {
96             vlib_buffer_t * p2, * p3;
97
98             p2 = vlib_get_buffer (vm, from[2]);
99             p3 = vlib_get_buffer (vm, from[3]);
100
101             vlib_prefetch_buffer_header (p2, LOAD);
102             vlib_prefetch_buffer_header (p3, LOAD);
103
104             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
105             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
106           }
107
108           bi0 = from[0];
109           bi1 = from[1];
110           to_next[0] = bi0;
111           to_next[1] = bi1;
112           from += 2;
113           to_next += 2;
114           n_left_to_next -= 2;
115           n_left_from -= 2;
116
117           b0 = vlib_get_buffer (vm, bi0);
118           b1 = vlib_get_buffer (vm, bi1);
119           error0 = 0;
120           error1 = 0;
121
122           /* leaves current_data pointing at the pppoe header */
123           pppoe0 = vlib_buffer_get_current (b0);
124           pppoe1 = vlib_buffer_get_current (b1);
125           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
126           ppp_proto1 = clib_net_to_host_u16(pppoe1->ppp_proto);
127
128           /* Manipulate packet 0 */
129           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
130              && (ppp_proto0 != PPP_PROTOCOL_ip6))
131             {
132               error0 = PPPOE_ERROR_CONTROL_PLANE;
133               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
134               goto trace0;
135             }
136
137           /* get client mac */
138           vlib_buffer_reset(b0);
139           h0 = vlib_buffer_get_current (b0);
140
141           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
142                           h0->src_address, pppoe0->session_id,
143                           &key0, &bucket0, &result0);
144           if (PREDICT_FALSE (result0.fields.session_index == ~0))
145             {
146               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
147               next0 = PPPOE_INPUT_NEXT_DROP;
148               goto trace0;
149             }
150
151           t0 = pool_elt_at_index (pem->sessions,
152                                   result0.fields.session_index);
153
154           /* Pop Eth and PPPoE header */
155           vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0));
156
157           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
158                   PPPOE_INPUT_NEXT_IP4_INPUT
159                   : PPPOE_INPUT_NEXT_IP6_INPUT;
160
161           sw_if_index0 = t0->sw_if_index;
162           len0 = vlib_buffer_length_in_chain (vm, b0);
163
164           pkts_decapsulated ++;
165           stats_n_packets += 1;
166           stats_n_bytes += len0;
167
168           /* Batch stats increment on the same pppoe session so counter
169              is not incremented per packet */
170           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
171             {
172               stats_n_packets -= 1;
173               stats_n_bytes -= len0;
174               if (stats_n_packets)
175                 vlib_increment_combined_counter
176                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
177                    thread_index, stats_sw_if_index,
178                    stats_n_packets, stats_n_bytes);
179               stats_n_packets = 1;
180               stats_n_bytes = len0;
181               stats_sw_if_index = sw_if_index0;
182             }
183
184         trace0:
185           b0->error = error0 ? node->errors[error0] : 0;
186
187           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
188             {
189               pppoe_rx_trace_t *tr
190                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
191               tr->next_index = next0;
192               tr->error = error0;
193               tr->session_index = result0.fields.session_index;
194               tr->session_id = clib_net_to_host_u32(pppoe0->session_id);
195             }
196
197
198           /* Manipulate packet 1 */
199           if ((ppp_proto1 != PPP_PROTOCOL_ip4)
200              && (ppp_proto1 != PPP_PROTOCOL_ip6))
201             {
202               error1 = PPPOE_ERROR_CONTROL_PLANE;
203               next1 = PPPOE_INPUT_NEXT_CP_INPUT;
204               goto trace1;
205             }
206
207           /* get client mac */
208           vlib_buffer_reset(b1);
209           h1 = vlib_buffer_get_current (b1);
210
211           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
212                           h1->src_address, pppoe1->session_id,
213                           &key1, &bucket1, &result1);
214           if (PREDICT_FALSE (result1.fields.session_index == ~0))
215             {
216               error1 = PPPOE_ERROR_NO_SUCH_SESSION;
217               next1 = PPPOE_INPUT_NEXT_DROP;
218               goto trace1;
219             }
220
221           t1 = pool_elt_at_index (pem->sessions,
222                                   result1.fields.session_index);
223
224           /* Pop Eth and PPPoE header */
225           vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*pppoe1));
226
227           next1 = (ppp_proto1==PPP_PROTOCOL_ip4)?
228                   PPPOE_INPUT_NEXT_IP4_INPUT
229                   : PPPOE_INPUT_NEXT_IP6_INPUT;
230
231           sw_if_index1 = t1->sw_if_index;
232           len1 = vlib_buffer_length_in_chain (vm, b1);
233
234           pkts_decapsulated ++;
235           stats_n_packets += 1;
236           stats_n_bytes += len1;
237
238           /* Batch stats increment on the same pppoe session so counter
239              is not incremented per packet */
240           if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
241             {
242               stats_n_packets -= 1;
243               stats_n_bytes -= len1;
244               if (stats_n_packets)
245                 vlib_increment_combined_counter
246                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
247                    thread_index, stats_sw_if_index,
248                    stats_n_packets, stats_n_bytes);
249               stats_n_packets = 1;
250               stats_n_bytes = len1;
251               stats_sw_if_index = sw_if_index1;
252             }
253
254         trace1:
255           b1->error = error1 ? node->errors[error1] : 0;
256
257           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
258             {
259               pppoe_rx_trace_t *tr
260                 = vlib_add_trace (vm, node, b1, sizeof (*tr));
261               tr->next_index = next1;
262               tr->error = error1;
263               tr->session_index = result1.fields.session_index;
264               tr->session_id = clib_net_to_host_u32(pppoe1->session_id);
265             }
266
267           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
268                                            to_next, n_left_to_next,
269                                            bi0, bi1, next0, next1);
270         }
271
272       while (n_left_from > 0 && n_left_to_next > 0)
273         {
274           u32 bi0;
275           vlib_buffer_t * b0;
276           u32 next0;
277           ethernet_header_t *h0;
278           pppoe_header_t * pppoe0;
279           u16 ppp_proto0 = 0;
280           pppoe_session_t * t0;
281           u32 error0;
282           u32 sw_if_index0, len0;
283           pppoe_entry_key_t key0;
284           pppoe_entry_result_t result0;
285           u32 bucket0;
286
287           bi0 = from[0];
288           to_next[0] = bi0;
289           from += 1;
290           to_next += 1;
291           n_left_from -= 1;
292           n_left_to_next -= 1;
293
294           b0 = vlib_get_buffer (vm, bi0);
295           error0 = 0;
296
297           /* leaves current_data pointing at the pppoe header */
298           pppoe0 = vlib_buffer_get_current (b0);
299           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
300
301           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
302              && (ppp_proto0 != PPP_PROTOCOL_ip6))
303             {
304               error0 = PPPOE_ERROR_CONTROL_PLANE;
305               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
306               goto trace00;
307             }
308
309           /* get client mac */
310           vlib_buffer_reset(b0);
311           h0 = vlib_buffer_get_current (b0);
312
313           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
314                           h0->src_address, pppoe0->session_id,
315                           &key0, &bucket0, &result0);
316           if (PREDICT_FALSE (result0.fields.session_index == ~0))
317             {
318               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
319               next0 = PPPOE_INPUT_NEXT_DROP;
320               goto trace00;
321             }
322
323           t0 = pool_elt_at_index (pem->sessions,
324                                   result0.fields.session_index);
325
326           /* Pop Eth and PPPoE header */
327           vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0));
328
329           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
330                   PPPOE_INPUT_NEXT_IP4_INPUT
331                   : PPPOE_INPUT_NEXT_IP6_INPUT;
332
333           sw_if_index0 = t0->sw_if_index;
334           len0 = vlib_buffer_length_in_chain (vm, b0);
335
336           pkts_decapsulated ++;
337           stats_n_packets += 1;
338           stats_n_bytes += len0;
339
340           /* Batch stats increment on the same pppoe session so counter
341              is not incremented per packet */
342           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
343             {
344               stats_n_packets -= 1;
345               stats_n_bytes -= len0;
346               if (stats_n_packets)
347                 vlib_increment_combined_counter
348                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
349                    thread_index, stats_sw_if_index,
350                    stats_n_packets, stats_n_bytes);
351               stats_n_packets = 1;
352               stats_n_bytes = len0;
353               stats_sw_if_index = sw_if_index0;
354             }
355
356         trace00:
357           b0->error = error0 ? node->errors[error0] : 0;
358
359           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
360             {
361               pppoe_rx_trace_t *tr
362                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
363               tr->next_index = next0;
364               tr->error = error0;
365               tr->session_index = result0.fields.session_index;
366               tr->session_id = clib_net_to_host_u16(pppoe0->session_id);
367             }
368           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
369                                            to_next, n_left_to_next,
370                                            bi0, next0);
371         }
372
373       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
374     }
375   /* Do we still need this now that session tx stats is kept? */
376   vlib_node_increment_counter (vm, pppoe_input_node.index,
377                                PPPOE_ERROR_DECAPSULATED,
378                                pkts_decapsulated);
379
380   /* Increment any remaining batch stats */
381   if (stats_n_packets)
382     {
383       vlib_increment_combined_counter
384         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
385          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
386       node->runtime_data[0] = stats_sw_if_index;
387     }
388
389   return from_frame->n_vectors;
390 }
391
392 #ifndef CLIB_MARCH_VARIANT
393 char * pppoe_error_strings[] = {
394 #define pppoe_error(n,s) s,
395 #include <pppoe/pppoe_error.def>
396 #undef pppoe_error
397 #undef _
398 };
399 #endif /* CLIB_MARCH_VARIANT */
400
401 VLIB_REGISTER_NODE (pppoe_input_node) = {
402   .name = "pppoe-input",
403   /* Takes a vector of packets. */
404   .vector_size = sizeof (u32),
405
406   .n_errors = PPPOE_N_ERROR,
407   .error_strings = pppoe_error_strings,
408
409   .n_next_nodes = PPPOE_INPUT_N_NEXT,
410   .next_nodes = {
411 #define _(s,n) [PPPOE_INPUT_NEXT_##s] = n,
412     foreach_pppoe_input_next
413 #undef _
414   },
415
416   .format_trace = format_pppoe_rx_trace,
417 };
418
419