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 #define foreach_pppoe_cp_next \
23 _(DROP, "error-drop") \
24 _(INTERFACE, "interface-output" ) \
28 #define _(s,n) PPPOE_CP_NEXT_##s,
43 static u8 * format_pppoe_cp_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 pppoe_cp_trace_t * t = va_arg (*args, pppoe_cp_trace_t *);
48 pppoe_main_t * pem = &pppoe_main;
50 if (t->sw_if_index != pem->cp_if_index)
52 s = format (s, "PPPoE dispatch from sw_if_index %d next %d error %d \n"
53 " pppoe_code 0x%x ppp_proto 0x%x",
54 t->sw_if_index, t->next_index, t->error,
55 t->pppoe_code, t->ppp_proto);
59 s = format (s, "PPPoE dispatch from cp_if_index %d next %d error %d \n"
60 " pppoe_code 0x%x ppp_proto 0x%x",
61 t->cp_if_index, t->next_index, t->error,
62 t->pppoe_code, t->ppp_proto);
67 VLIB_NODE_FN (pppoe_cp_dispatch_node) (vlib_main_t * vm,
68 vlib_node_runtime_t * node,
69 vlib_frame_t * from_frame)
71 u32 n_left_from, next_index, * from, * to_next;
72 pppoe_main_t * pem = &pppoe_main;
73 vnet_main_t * vnm = pem->vnet_main;
74 vnet_interface_main_t * im = &vnm->interface_main;
75 u32 pkts_decapsulated = 0;
76 u32 thread_index = vlib_get_thread_index();
77 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
78 pppoe_entry_key_t cached_key;
79 pppoe_entry_result_t cached_result;
81 from = vlib_frame_vector_args (from_frame);
82 n_left_from = from_frame->n_vectors;
84 /* Clear the one-entry cache in case session table was updated */
86 cached_result.raw = ~0; /* warning be gone */
88 next_index = node->cached_next_index;
89 stats_sw_if_index = node->runtime_data[0];
90 stats_n_packets = stats_n_bytes = 0;
92 while (n_left_from > 0)
96 vlib_get_next_frame (vm, node, next_index,
97 to_next, n_left_to_next);
99 while (n_left_from > 0 && n_left_to_next > 0)
103 ethernet_header_t *h0;
104 pppoe_header_t * pppoe0;
105 pppoe_entry_key_t key0;
106 pppoe_entry_result_t result0;
111 u32 rx_sw_if_index0=~0, tx_sw_if_index0=~0, len0;
112 vnet_hw_interface_t *hi;
113 vnet_sw_interface_t *si;
122 b0 = vlib_get_buffer (vm, bi0);
123 /* leaves current_data pointing at the pppoe header */
124 pppoe0 = vlib_buffer_get_current (b0);
125 rx_sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
127 if (PREDICT_FALSE (pppoe0->ver_type != PPPOE_VER_TYPE))
129 error0 = PPPOE_ERROR_BAD_VER_TYPE;
130 next0 = PPPOE_INPUT_NEXT_DROP;
134 vlib_buffer_reset(b0);
135 h0 = vlib_buffer_get_current (b0);
137 if(rx_sw_if_index0 == pem->cp_if_index)
139 pppoe_lookup_1 (&pem->link_table, &cached_key, &cached_result,
141 &key0, &bucket0, &result0);
142 tx_sw_if_index0 = result0.fields.sw_if_index;
144 if (PREDICT_FALSE (tx_sw_if_index0 == ~0))
146 error0 = PPPOE_ERROR_NO_SUCH_SESSION;
147 next0 = PPPOE_INPUT_NEXT_DROP;
151 next0 = PPPOE_CP_NEXT_INTERFACE;
152 vnet_buffer(b0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
154 /* set src mac address */
155 si = vnet_get_sw_interface(vnm, tx_sw_if_index0);
156 hi = vnet_get_hw_interface (vnm, si->hw_if_index);
157 clib_memcpy_fast (vlib_buffer_get_current (b0)+6, hi->hw_address, 6);
161 pppoe_lookup_1 (&pem->link_table, &cached_key, &cached_result,
163 &key0, &bucket0, &result0);
164 tx_sw_if_index0 = result0.fields.sw_if_index;
166 /* learn client session */
167 pppoe_learn_process (&pem->link_table, rx_sw_if_index0,
171 next0 = PPPOE_CP_NEXT_INTERFACE;
172 vnet_buffer(b0)->sw_if_index[VLIB_TX] = pem->cp_if_index;
175 len0 = vlib_buffer_length_in_chain (vm, b0);
177 pkts_decapsulated ++;
178 stats_n_packets += 1;
179 stats_n_bytes += len0;
181 /* Batch stats increment on the same pppoe session so counter
182 is not incremented per packet */
183 if (PREDICT_FALSE (rx_sw_if_index0 != stats_sw_if_index))
185 stats_n_packets -= 1;
186 stats_n_bytes -= len0;
188 vlib_increment_combined_counter
189 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
190 thread_index, stats_sw_if_index,
191 stats_n_packets, stats_n_bytes);
193 stats_n_bytes = len0;
194 stats_sw_if_index = rx_sw_if_index0;
198 b0->error = error0 ? node->errors[error0] : 0;
200 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
203 = vlib_add_trace (vm, node, b0, sizeof (*tr));
204 tr->next_index = next0;
206 tr->sw_if_index = tx_sw_if_index0;
207 tr->cp_if_index = pem->cp_if_index;
208 tr->pppoe_code = pppoe0->code;
209 tr->ppp_proto = clib_net_to_host_u16(pppoe0->ppp_proto);
211 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
212 to_next, n_left_to_next,
216 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
218 /* Do we still need this now that session tx stats is kept? */
219 vlib_node_increment_counter (vm, pppoe_input_node.index,
220 PPPOE_ERROR_DECAPSULATED,
223 /* Increment any remaining batch stats */
226 vlib_increment_combined_counter
227 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
228 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
229 node->runtime_data[0] = stats_sw_if_index;
232 return from_frame->n_vectors;
235 VLIB_REGISTER_NODE (pppoe_cp_dispatch_node) = {
236 .name = "pppoe-cp-dispatch",
237 /* Takes a vector of packets. */
238 .vector_size = sizeof (u32),
240 .n_next_nodes = PPPOE_CP_N_NEXT,
242 #define _(s,n) [PPPOE_CP_NEXT_##s] = n,
243 foreach_pppoe_cp_next
247 .format_trace = format_pppoe_cp_trace,