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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <vlib/vlib.h>
19 #include <vnet/ppp/packet.h>
20 #include <pppoe/pppoe.h>
22 vlib_node_registration_t pppoe_tap_dispatch_node;
24 #define foreach_pppoe_tap_next \
25 _(DROP, "error-drop") \
26 _(TUNTAP, "tuntap-tx" ) \
27 _(INTERFACE, "interface-output" ) \
31 #define _(s,n) PPPOE_TAP_NEXT_##s,
32 foreach_pppoe_tap_next
46 static u8 * format_pppoe_tap_trace (u8 * s, va_list * args)
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;
53 if (t->sw_if_index != pem->tap_if_index)
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);
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);
71 * Perform learning on one packet based on the mac table lookup result.
73 static_always_inline void
74 pppoe_learn_process (vlib_node_runtime_t * node,
78 pppoe_entry_key_t * key0,
79 pppoe_entry_key_t * cached_key,
81 pppoe_entry_result_t * result0)
83 /* Check mac table lookup result */
84 if (PREDICT_TRUE (result0->fields.sw_if_index == sw_if_index0))
87 * The entry was in the table, and the sw_if_index matched, the normal case
91 else if (result0->fields.sw_if_index == ~0)
93 /* The entry was not in table, so add it */
94 result0->fields.sw_if_index = sw_if_index0;
95 result0->fields.session_index = ~0;
96 cached_key->raw = ~0; /* invalidate the cache */
100 /* The entry was in the table, but with the wrong sw_if_index mapping (mac move) */
101 result0->fields.sw_if_index = sw_if_index0;
104 /* Update the entry */
105 BVT (clib_bihash_kv) kv;
107 kv.value = result0->raw;
108 BV (clib_bihash_add_del) (&pem->session_table, &kv, 1 /* is_add */ );
112 pppoe_tap_dispatch (vlib_main_t * vm,
113 vlib_node_runtime_t * node,
114 vlib_frame_t * from_frame)
116 u32 n_left_from, next_index, * from, * to_next;
117 pppoe_main_t * pem = &pppoe_main;
118 vnet_main_t * vnm = pem->vnet_main;
119 vnet_interface_main_t * im = &vnm->interface_main;
120 u32 pkts_decapsulated = 0;
121 u32 thread_index = vlib_get_thread_index();
122 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
123 pppoe_entry_key_t cached_key;
124 pppoe_entry_result_t cached_result;
126 from = vlib_frame_vector_args (from_frame);
127 n_left_from = from_frame->n_vectors;
129 /* Clear the one-entry cache in case session table was updated */
131 cached_result.raw = ~0; /* warning be gone */
133 next_index = node->cached_next_index;
134 stats_sw_if_index = node->runtime_data[0];
135 stats_n_packets = stats_n_bytes = 0;
137 while (n_left_from > 0)
141 vlib_get_next_frame (vm, node, next_index,
142 to_next, n_left_to_next);
144 while (n_left_from > 0 && n_left_to_next > 0)
148 ethernet_header_t *h0;
149 pppoe_header_t * pppoe0;
150 pppoe_entry_key_t key0;
151 pppoe_entry_result_t result0;
156 u32 rx_sw_if_index0=~0, tx_sw_if_index0=~0, len0;
157 vnet_hw_interface_t *hi;
158 vnet_sw_interface_t *si;
167 b0 = vlib_get_buffer (vm, bi0);
168 /* leaves current_data pointing at the pppoe header */
169 pppoe0 = vlib_buffer_get_current (b0);
170 rx_sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
172 if (PREDICT_FALSE (pppoe0->ver_type != PPPOE_VER_TYPE))
174 error0 = PPPOE_ERROR_BAD_VER_TYPE;
175 next0 = PPPOE_INPUT_NEXT_DROP;
179 vlib_buffer_reset(b0);
180 h0 = vlib_buffer_get_current (b0);
182 if(rx_sw_if_index0 == pem->tap_if_index)
184 pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
186 &key0, &bucket0, &result0);
187 tx_sw_if_index0 = result0.fields.sw_if_index;
189 if (PREDICT_FALSE (tx_sw_if_index0 == ~0))
191 error0 = PPPOE_ERROR_NO_SUCH_SESSION;
192 next0 = PPPOE_INPUT_NEXT_DROP;
196 next0 = PPPOE_TAP_NEXT_INTERFACE;
197 vnet_buffer(b0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
199 /* set src mac address */
200 si = vnet_get_sw_interface(vnm, tx_sw_if_index0);
201 hi = vnet_get_hw_interface (vnm, si->hw_if_index);
202 clib_memcpy (vlib_buffer_get_current (b0)+6, hi->hw_address, 6);
206 pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
207 h0->src_address, pppoe0->session_id,
208 &key0, &bucket0, &result0);
209 tx_sw_if_index0 = result0.fields.sw_if_index;
211 /* learn client session */
212 pppoe_learn_process (node, pem, b0, rx_sw_if_index0,
216 next0 = PPPOE_TAP_NEXT_TUNTAP;
217 vnet_buffer(b0)->sw_if_index[VLIB_TX] = pem->tap_if_index;
220 len0 = vlib_buffer_length_in_chain (vm, b0);
222 pkts_decapsulated ++;
223 stats_n_packets += 1;
224 stats_n_bytes += len0;
226 /* Batch stats increment on the same pppoe session so counter
227 is not incremented per packet */
228 if (PREDICT_FALSE (rx_sw_if_index0 != stats_sw_if_index))
230 stats_n_packets -= 1;
231 stats_n_bytes -= len0;
233 vlib_increment_combined_counter
234 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
235 thread_index, stats_sw_if_index,
236 stats_n_packets, stats_n_bytes);
238 stats_n_bytes = len0;
239 stats_sw_if_index = rx_sw_if_index0;
243 b0->error = error0 ? node->errors[error0] : 0;
245 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
247 pppoe_tap_trace_t *tr
248 = vlib_add_trace (vm, node, b0, sizeof (*tr));
249 tr->next_index = next0;
251 tr->sw_if_index = tx_sw_if_index0;
252 tr->tap_if_index = pem->tap_if_index;
253 tr->pppoe_code = pppoe0->code;
254 tr->ppp_proto = clib_net_to_host_u16(pppoe0->ppp_proto);
256 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
257 to_next, n_left_to_next,
261 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
263 /* Do we still need this now that session tx stats is kept? */
264 vlib_node_increment_counter (vm, pppoe_input_node.index,
265 PPPOE_ERROR_DECAPSULATED,
268 /* Increment any remaining batch stats */
271 vlib_increment_combined_counter
272 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
273 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
274 node->runtime_data[0] = stats_sw_if_index;
277 return from_frame->n_vectors;
280 VLIB_REGISTER_NODE (pppoe_tap_dispatch_node) = {
281 .function = pppoe_tap_dispatch,
282 .name = "pppoe-tap-dispatch",
283 /* Takes a vector of packets. */
284 .vector_size = sizeof (u32),
286 .n_next_nodes = PPPOE_TAP_N_NEXT,
288 #define _(s,n) [PPPOE_TAP_NEXT_##s] = n,
289 foreach_pppoe_tap_next
293 .format_trace = format_pppoe_tap_trace,
296 VLIB_NODE_FUNCTION_MULTIARCH (pppoe_tap_dispatch_node, pppoe_tap_dispatch)