2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/dpo/interface_rx_dpo.h>
17 #include <vnet/fib/fib_node.h>
18 #include <vnet/l2/l2_input.h>
20 #ifndef CLIB_MARCH_VARIANT
21 interface_rx_dpo_t *interface_rx_dpo_pool;
24 * The 'DB' of interface DPOs.
25 * There is only one per-interface per-protocol, so this is a per-interface
28 static index_t *interface_rx_dpo_db[DPO_PROTO_NUM];
30 static interface_rx_dpo_t *
31 interface_rx_dpo_alloc (void)
33 interface_rx_dpo_t *ido;
35 pool_get(interface_rx_dpo_pool, ido);
40 static inline interface_rx_dpo_t *
41 interface_rx_dpo_get_from_dpo (const dpo_id_t *dpo)
43 ASSERT(DPO_INTERFACE_RX == dpo->dpoi_type);
45 return (interface_rx_dpo_get(dpo->dpoi_index));
49 interface_rx_dpo_get_index (interface_rx_dpo_t *ido)
51 return (ido - interface_rx_dpo_pool);
55 interface_rx_dpo_lock (dpo_id_t *dpo)
57 interface_rx_dpo_t *ido;
59 ido = interface_rx_dpo_get_from_dpo(dpo);
64 interface_rx_dpo_unlock (dpo_id_t *dpo)
66 interface_rx_dpo_t *ido;
68 ido = interface_rx_dpo_get_from_dpo(dpo);
71 if (0 == ido->ido_locks)
73 interface_rx_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
75 pool_put(interface_rx_dpo_pool, ido);
80 * interface_rx_dpo_add_or_lock
82 * Add/create and lock a new or lock an existing for the interface DPO
83 * on the interface and protocol given
86 interface_rx_dpo_add_or_lock (dpo_proto_t proto,
90 interface_rx_dpo_t *ido;
92 vec_validate_init_empty(interface_rx_dpo_db[proto],
96 if (INDEX_INVALID == interface_rx_dpo_db[proto][sw_if_index])
98 ido = interface_rx_dpo_alloc();
100 ido->ido_sw_if_index = sw_if_index;
101 ido->ido_proto = proto;
103 interface_rx_dpo_db[proto][sw_if_index] =
104 interface_rx_dpo_get_index(ido);
108 ido = interface_rx_dpo_get(interface_rx_dpo_db[proto][sw_if_index]);
111 dpo_set(dpo, DPO_INTERFACE_RX, proto, interface_rx_dpo_get_index(ido));
113 #endif /* CLIB_MARCH_VARIANT */
116 static clib_error_t *
117 interface_rx_dpo_interface_state_change (vnet_main_t * vnm,
126 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
127 interface_rx_dpo_interface_state_change);
130 * @brief Registered callback for HW interface state changes
132 static clib_error_t *
133 interface_rx_dpo_hw_interface_state_change (vnet_main_t * vnm,
140 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
141 interface_rx_dpo_hw_interface_state_change);
143 static clib_error_t *
144 interface_rx_dpo_interface_delete (vnet_main_t * vnm,
151 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
152 interface_rx_dpo_interface_delete);
154 #ifndef CLIB_MARCH_VARIANT
156 format_interface_rx_dpo (u8* s, va_list *ap)
158 index_t index = va_arg(*ap, index_t);
159 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
160 vnet_main_t * vnm = vnet_get_main();
161 interface_rx_dpo_t *ido = interface_rx_dpo_get(index);
163 return (format(s, "%U-rx-dpo: %U",
164 format_vnet_sw_interface_name,
166 vnet_get_sw_interface(vnm, ido->ido_sw_if_index),
167 format_dpo_proto, ido->ido_proto));
171 interface_rx_dpo_mem_show (void)
173 fib_show_memory_usage("Interface",
174 pool_elts(interface_rx_dpo_pool),
175 pool_len(interface_rx_dpo_pool),
176 sizeof(interface_rx_dpo_t));
180 const static dpo_vft_t interface_rx_dpo_vft = {
181 .dv_lock = interface_rx_dpo_lock,
182 .dv_unlock = interface_rx_dpo_unlock,
183 .dv_format = format_interface_rx_dpo,
184 .dv_mem_show = interface_rx_dpo_mem_show,
188 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
191 * this means that these graph nodes are ones from which a glean is the
192 * parent object in the DPO-graph.
194 const static char* const interface_rx_dpo_ip4_nodes[] =
196 "interface-rx-dpo-ip4",
199 const static char* const interface_rx_dpo_ip6_nodes[] =
201 "interface-rx-dpo-ip6",
204 const static char* const interface_rx_dpo_l2_nodes[] =
206 "interface-rx-dpo-l2",
210 const static char* const * const interface_rx_dpo_nodes[DPO_PROTO_NUM] =
212 [DPO_PROTO_IP4] = interface_rx_dpo_ip4_nodes,
213 [DPO_PROTO_IP6] = interface_rx_dpo_ip6_nodes,
214 [DPO_PROTO_ETHERNET] = interface_rx_dpo_l2_nodes,
215 [DPO_PROTO_MPLS] = NULL,
219 interface_rx_dpo_module_init (void)
221 dpo_register(DPO_INTERFACE_RX,
222 &interface_rx_dpo_vft,
223 interface_rx_dpo_nodes);
225 #endif /* CLIB_MARCH_VARIANT */
228 * @brief Interface DPO trace data
230 typedef struct interface_rx_dpo_trace_t_
233 } interface_rx_dpo_trace_t;
235 typedef enum interface_rx_dpo_next_t_
237 INTERFACE_RX_DPO_DROP = 0,
238 INTERFACE_RX_DPO_INPUT = 1,
239 } interface_rx_dpo_next_t;
242 interface_rx_dpo_inline (vlib_main_t * vm,
243 vlib_node_runtime_t * node,
244 vlib_frame_t * from_frame,
247 u32 n_left_from, next_index, * from, * to_next;
248 u32 thread_index = vm->thread_index;
249 vnet_interface_main_t *im;
251 im = &vnet_get_main ()->interface_main;
252 from = vlib_frame_vector_args (from_frame);
253 n_left_from = from_frame->n_vectors;
255 next_index = INTERFACE_RX_DPO_INPUT;
257 while (n_left_from > 0)
261 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
263 while (n_left_from >= 4 && n_left_to_next > 2)
265 const interface_rx_dpo_t *ido0, *ido1;
266 u32 bi0, idoi0, bi1, idoi1;
267 vlib_buffer_t *b0, *b1;
278 b0 = vlib_get_buffer (vm, bi0);
279 b1 = vlib_get_buffer (vm, bi1);
281 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
282 idoi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
283 ido0 = interface_rx_dpo_get(idoi0);
284 ido1 = interface_rx_dpo_get(idoi1);
286 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
287 vnet_buffer(b1)->sw_if_index[VLIB_RX] = ido1->ido_sw_if_index;
291 vnet_update_l2_len (b0);
292 vnet_update_l2_len (b1);
295 vlib_increment_combined_counter (im->combined_sw_if_counters
296 + VNET_INTERFACE_COUNTER_RX,
298 ido0->ido_sw_if_index,
300 vlib_buffer_length_in_chain (vm, b0));
301 vlib_increment_combined_counter (im->combined_sw_if_counters
302 + VNET_INTERFACE_COUNTER_RX,
304 ido1->ido_sw_if_index,
306 vlib_buffer_length_in_chain (vm, b1));
308 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
310 interface_rx_dpo_trace_t *tr0;
312 tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
313 tr0->sw_if_index = ido0->ido_sw_if_index;
315 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
317 interface_rx_dpo_trace_t *tr1;
319 tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
320 tr1->sw_if_index = ido1->ido_sw_if_index;
324 while (n_left_from > 0 && n_left_to_next > 0)
326 const interface_rx_dpo_t * ido0;
337 b0 = vlib_get_buffer (vm, bi0);
339 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
340 ido0 = interface_rx_dpo_get(idoi0);
342 /* Swap the RX interface of the packet to the one the
343 * interface DPR represents */
344 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
346 /* Update l2_len to make l2 tag rewrite work */
348 vnet_update_l2_len (b0);
350 /* Bump the interface's RX coutners */
351 vlib_increment_combined_counter (im->combined_sw_if_counters
352 + VNET_INTERFACE_COUNTER_RX,
354 ido0->ido_sw_if_index,
356 vlib_buffer_length_in_chain (vm, b0));
358 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
360 interface_rx_dpo_trace_t *tr;
362 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
363 tr->sw_if_index = ido0->ido_sw_if_index;
366 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
368 return from_frame->n_vectors;
372 format_interface_rx_dpo_trace (u8 * s, va_list * args)
374 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
375 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
376 interface_rx_dpo_trace_t * t = va_arg (*args, interface_rx_dpo_trace_t *);
377 u32 indent = format_get_indent (s);
378 s = format (s, "%U sw_if_index:%d",
379 format_white_space, indent,
384 VLIB_NODE_FN (interface_rx_dpo_ip4_node) (vlib_main_t * vm,
385 vlib_node_runtime_t * node,
386 vlib_frame_t * from_frame)
388 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
391 VLIB_NODE_FN (interface_rx_dpo_ip6_node) (vlib_main_t * vm,
392 vlib_node_runtime_t * node,
393 vlib_frame_t * from_frame)
395 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
398 VLIB_NODE_FN (interface_rx_dpo_l2_node) (vlib_main_t * vm,
399 vlib_node_runtime_t * node,
400 vlib_frame_t * from_frame)
402 return (interface_rx_dpo_inline(vm, node, from_frame, 1));
405 VLIB_REGISTER_NODE (interface_rx_dpo_ip4_node) = {
406 .name = "interface-rx-dpo-ip4",
407 .vector_size = sizeof (u32),
408 .format_trace = format_interface_rx_dpo_trace,
412 [INTERFACE_RX_DPO_DROP] = "ip4-drop",
413 [INTERFACE_RX_DPO_INPUT] = "ip4-input",
418 VLIB_REGISTER_NODE (interface_rx_dpo_ip6_node) = {
419 .name = "interface-rx-dpo-ip6",
420 .vector_size = sizeof (u32),
421 .format_trace = format_interface_rx_dpo_trace,
425 [INTERFACE_RX_DPO_DROP] = "ip6-drop",
426 [INTERFACE_RX_DPO_INPUT] = "ip6-input",
431 VLIB_REGISTER_NODE (interface_rx_dpo_l2_node) = {
432 .name = "interface-rx-dpo-l2",
433 .vector_size = sizeof (u32),
434 .format_trace = format_interface_rx_dpo_trace,
438 [INTERFACE_RX_DPO_DROP] = "error-drop",
439 [INTERFACE_RX_DPO_INPUT] = "l2-input",