acl-plugin: refactor the "show acl-plugin" CLI, use vlib_cli_output() vs. format()
[vpp.git] / src / plugins / pppoe / pppoe_tap_node.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or pemplied.
13  * See the License for the specific language governing permissions and
14  * lpemitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/ppp/packet.h>
20 #include <pppoe/pppoe.h>
21
22 vlib_node_registration_t pppoe_tap_dispatch_node;
23
24 #define foreach_pppoe_tap_next        \
25 _(DROP, "error-drop")                  \
26 _(TUNTAP, "tuntap-tx" )                \
27 _(INTERFACE, "interface-output" )      \
28
29 typedef enum
30 {
31 #define _(s,n) PPPOE_TAP_NEXT_##s,
32   foreach_pppoe_tap_next
33 #undef _
34     PPPOE_TAP_N_NEXT,
35 } pppoe_tap_next_t;
36
37 typedef struct {
38   u32 next_index;
39   u32 sw_if_index;
40   u32 tap_if_index;
41   u8 pppoe_code;
42   u16 ppp_proto;
43   u32 error;
44 } pppoe_tap_trace_t;
45
46 static u8 * format_pppoe_tap_trace (u8 * s, va_list * args)
47 {
48   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50   pppoe_tap_trace_t * t = va_arg (*args, pppoe_tap_trace_t *);
51   pppoe_main_t * pem = &pppoe_main;
52
53   if (t->sw_if_index != pem->tap_if_index)
54     {
55       s = format (s, "PPPoE dispatch from sw_if_index %d next %d error %d \n"
56                   "  pppoe_code 0x%x  ppp_proto 0x%x",
57                   t->sw_if_index, t->next_index, t->error,
58                   t->pppoe_code, t->ppp_proto);
59     }
60   else
61     {
62       s = format (s, "PPPoE dispatch from tap_if_index %d next %d error %d \n"
63                   "  pppoe_code 0x%x  ppp_proto 0x%x",
64                   t->tap_if_index, t->next_index, t->error,
65                   t->pppoe_code, t->ppp_proto);
66     }
67   return s;
68 }
69
70 static uword
71 pppoe_tap_dispatch (vlib_main_t * vm,
72                     vlib_node_runtime_t * node,
73                     vlib_frame_t * from_frame)
74 {
75   u32 n_left_from, next_index, * from, * to_next;
76   pppoe_main_t * pem = &pppoe_main;
77   vnet_main_t * vnm = pem->vnet_main;
78   vnet_interface_main_t * im = &vnm->interface_main;
79   u32 pkts_decapsulated = 0;
80   u32 thread_index = vlib_get_thread_index();
81   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
82   pppoe_entry_key_t cached_key;
83   pppoe_entry_result_t cached_result;
84
85   from = vlib_frame_vector_args (from_frame);
86   n_left_from = from_frame->n_vectors;
87
88   /* Clear the one-entry cache in case session table was updated */
89   cached_key.raw = ~0;
90   cached_result.raw = ~0;       /* warning be gone */
91
92   next_index = node->cached_next_index;
93   stats_sw_if_index = node->runtime_data[0];
94   stats_n_packets = stats_n_bytes = 0;
95
96   while (n_left_from > 0)
97     {
98       u32 n_left_to_next;
99
100       vlib_get_next_frame (vm, node, next_index,
101                            to_next, n_left_to_next);
102
103       while (n_left_from > 0 && n_left_to_next > 0)
104         {
105           u32 bi0;
106           vlib_buffer_t * b0;
107           ethernet_header_t *h0;
108           pppoe_header_t * pppoe0;
109           pppoe_entry_key_t key0;
110           pppoe_entry_result_t result0;
111
112           u32 bucket0;
113           u32 next0;
114           u32 error0 = 0;
115           u32 rx_sw_if_index0=~0, tx_sw_if_index0=~0, len0;
116           vnet_hw_interface_t *hi;
117           vnet_sw_interface_t *si;
118
119           bi0 = from[0];
120           to_next[0] = bi0;
121           from += 1;
122           to_next += 1;
123           n_left_from -= 1;
124           n_left_to_next -= 1;
125
126           b0 = vlib_get_buffer (vm, bi0);
127           /* leaves current_data pointing at the pppoe header */
128           pppoe0 = vlib_buffer_get_current (b0);
129           rx_sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
130
131           if (PREDICT_FALSE (pppoe0->ver_type != PPPOE_VER_TYPE))
132             {
133               error0 = PPPOE_ERROR_BAD_VER_TYPE;
134               next0 = PPPOE_INPUT_NEXT_DROP;
135               goto trace00;
136             }
137
138           vlib_buffer_reset(b0);
139           h0 = vlib_buffer_get_current (b0);
140
141           if(rx_sw_if_index0 == pem->tap_if_index)
142             {
143               pppoe_lookup_1 (&pem->link_table, &cached_key, &cached_result,
144                               h0->dst_address, 0,
145                               &key0, &bucket0, &result0);
146               tx_sw_if_index0 = result0.fields.sw_if_index;
147
148               if (PREDICT_FALSE (tx_sw_if_index0 == ~0))
149                 {
150                   error0 = PPPOE_ERROR_NO_SUCH_SESSION;
151                   next0 = PPPOE_INPUT_NEXT_DROP;
152                   goto trace00;
153                 }
154
155               next0 = PPPOE_TAP_NEXT_INTERFACE;
156               vnet_buffer(b0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
157
158               /* set src mac address */
159               si = vnet_get_sw_interface(vnm, tx_sw_if_index0);
160               hi = vnet_get_hw_interface (vnm, si->hw_if_index);
161               clib_memcpy (vlib_buffer_get_current (b0)+6, hi->hw_address, 6);
162             }
163           else
164             {
165               pppoe_lookup_1 (&pem->link_table, &cached_key, &cached_result,
166                               h0->src_address, 0,
167                               &key0, &bucket0, &result0);
168               tx_sw_if_index0 = result0.fields.sw_if_index;
169
170               /* learn client session */
171               pppoe_learn_process (&pem->link_table, rx_sw_if_index0,
172                                    &key0, &cached_key,
173                                    &bucket0, &result0);
174
175               next0 = PPPOE_TAP_NEXT_TUNTAP;
176               vnet_buffer(b0)->sw_if_index[VLIB_TX] = pem->tap_if_index;
177             }
178
179           len0 = vlib_buffer_length_in_chain (vm, b0);
180
181           pkts_decapsulated ++;
182           stats_n_packets += 1;
183           stats_n_bytes += len0;
184
185           /* Batch stats increment on the same pppoe session so counter
186              is not incremented per packet */
187           if (PREDICT_FALSE (rx_sw_if_index0 != stats_sw_if_index))
188             {
189               stats_n_packets -= 1;
190               stats_n_bytes -= len0;
191               if (stats_n_packets)
192                 vlib_increment_combined_counter
193                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
194                    thread_index, stats_sw_if_index,
195                    stats_n_packets, stats_n_bytes);
196               stats_n_packets = 1;
197               stats_n_bytes = len0;
198               stats_sw_if_index = rx_sw_if_index0;
199             }
200         
201         trace00:  
202           b0->error = error0 ? node->errors[error0] : 0;
203
204           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
205             {
206               pppoe_tap_trace_t *tr
207                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
208               tr->next_index = next0;
209               tr->error = error0;
210               tr->sw_if_index = tx_sw_if_index0;
211               tr->tap_if_index = pem->tap_if_index;
212               tr->pppoe_code = pppoe0->code;
213               tr->ppp_proto = clib_net_to_host_u16(pppoe0->ppp_proto);
214             }
215           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
216                                            to_next, n_left_to_next,
217                                            bi0, next0);
218         }
219
220       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
221     }
222   /* Do we still need this now that session tx stats is kept? */
223   vlib_node_increment_counter (vm, pppoe_input_node.index,
224                                PPPOE_ERROR_DECAPSULATED,
225                                pkts_decapsulated);
226
227   /* Increment any remaining batch stats */
228   if (stats_n_packets)
229     {
230       vlib_increment_combined_counter
231         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
232          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
233       node->runtime_data[0] = stats_sw_if_index;
234     }
235
236   return from_frame->n_vectors;
237 }
238
239 VLIB_REGISTER_NODE (pppoe_tap_dispatch_node) = {
240   .function = pppoe_tap_dispatch,
241   .name = "pppoe-tap-dispatch",
242   /* Takes a vector of packets. */
243   .vector_size = sizeof (u32),
244
245   .n_next_nodes = PPPOE_TAP_N_NEXT,
246   .next_nodes = {
247 #define _(s,n) [PPPOE_TAP_NEXT_##s] = n,
248     foreach_pppoe_tap_next
249 #undef _
250   },
251
252   .format_trace = format_pppoe_tap_trace,
253 };
254
255 VLIB_NODE_FUNCTION_MULTIARCH (pppoe_tap_dispatch_node, pppoe_tap_dispatch)
256