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>
21 * The 'DB' of interface DPOs.
22 * There is only one per-interface per-protocol, so this is a per-interface
25 static index_t *interface_rx_dpo_db[DPO_PROTO_NUM];
27 static interface_rx_dpo_t *
28 interface_rx_dpo_alloc (void)
30 interface_rx_dpo_t *ido;
32 pool_get(interface_rx_dpo_pool, ido);
37 static inline interface_rx_dpo_t *
38 interface_rx_dpo_get_from_dpo (const dpo_id_t *dpo)
40 ASSERT(DPO_INTERFACE_RX == dpo->dpoi_type);
42 return (interface_rx_dpo_get(dpo->dpoi_index));
46 interface_rx_dpo_get_index (interface_rx_dpo_t *ido)
48 return (ido - interface_rx_dpo_pool);
52 interface_rx_dpo_lock (dpo_id_t *dpo)
54 interface_rx_dpo_t *ido;
56 ido = interface_rx_dpo_get_from_dpo(dpo);
61 interface_rx_dpo_unlock (dpo_id_t *dpo)
63 interface_rx_dpo_t *ido;
65 ido = interface_rx_dpo_get_from_dpo(dpo);
68 if (0 == ido->ido_locks)
70 interface_rx_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
72 pool_put(interface_rx_dpo_pool, ido);
77 * interface_rx_dpo_add_or_lock
79 * Add/create and lock a new or lock an existing for the interface DPO
80 * on the interface and protocol given
83 interface_rx_dpo_add_or_lock (dpo_proto_t proto,
87 interface_rx_dpo_t *ido;
89 vec_validate_init_empty(interface_rx_dpo_db[proto],
93 if (INDEX_INVALID == interface_rx_dpo_db[proto][sw_if_index])
95 ido = interface_rx_dpo_alloc();
97 ido->ido_sw_if_index = sw_if_index;
98 ido->ido_proto = proto;
100 interface_rx_dpo_db[proto][sw_if_index] =
101 interface_rx_dpo_get_index(ido);
105 ido = interface_rx_dpo_get(interface_rx_dpo_db[proto][sw_if_index]);
108 dpo_set(dpo, DPO_INTERFACE_RX, proto, interface_rx_dpo_get_index(ido));
112 static clib_error_t *
113 interface_rx_dpo_interface_state_change (vnet_main_t * vnm,
122 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
123 interface_rx_dpo_interface_state_change);
126 * @brief Registered callback for HW interface state changes
128 static clib_error_t *
129 interface_rx_dpo_hw_interface_state_change (vnet_main_t * vnm,
136 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
137 interface_rx_dpo_hw_interface_state_change);
139 static clib_error_t *
140 interface_rx_dpo_interface_delete (vnet_main_t * vnm,
147 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
148 interface_rx_dpo_interface_delete);
151 format_interface_rx_dpo (u8* s, va_list *ap)
153 index_t index = va_arg(*ap, index_t);
154 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
155 vnet_main_t * vnm = vnet_get_main();
156 interface_rx_dpo_t *ido = interface_rx_dpo_get(index);
158 return (format(s, "%U-rx-dpo: %U",
159 format_vnet_sw_interface_name,
161 vnet_get_sw_interface(vnm, ido->ido_sw_if_index),
162 format_dpo_proto, ido->ido_proto));
166 interface_rx_dpo_mem_show (void)
168 fib_show_memory_usage("Interface",
169 pool_elts(interface_rx_dpo_pool),
170 pool_len(interface_rx_dpo_pool),
171 sizeof(interface_rx_dpo_t));
175 const static dpo_vft_t interface_rx_dpo_vft = {
176 .dv_lock = interface_rx_dpo_lock,
177 .dv_unlock = interface_rx_dpo_unlock,
178 .dv_format = format_interface_rx_dpo,
179 .dv_mem_show = interface_rx_dpo_mem_show,
183 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
186 * this means that these graph nodes are ones from which a glean is the
187 * parent object in the DPO-graph.
189 const static char* const interface_rx_dpo_ip4_nodes[] =
191 "interface-rx-dpo-ip4",
194 const static char* const interface_rx_dpo_ip6_nodes[] =
196 "interface-rx-dpo-ip6",
199 const static char* const interface_rx_dpo_l2_nodes[] =
201 "interface-rx-dpo-l2",
205 const static char* const * const interface_rx_dpo_nodes[DPO_PROTO_NUM] =
207 [DPO_PROTO_IP4] = interface_rx_dpo_ip4_nodes,
208 [DPO_PROTO_IP6] = interface_rx_dpo_ip6_nodes,
209 [DPO_PROTO_ETHERNET] = interface_rx_dpo_l2_nodes,
210 [DPO_PROTO_MPLS] = NULL,
214 interface_rx_dpo_module_init (void)
216 dpo_register(DPO_INTERFACE_RX,
217 &interface_rx_dpo_vft,
218 interface_rx_dpo_nodes);
222 * @brief Interface DPO trace data
224 typedef struct interface_rx_dpo_trace_t_
227 } interface_rx_dpo_trace_t;
229 typedef enum interface_rx_dpo_next_t_
231 INTERFACE_RX_DPO_DROP = 0,
232 INTERFACE_RX_DPO_INPUT = 1,
233 } interface_rx_dpo_next_t;
236 interface_rx_dpo_inline (vlib_main_t * vm,
237 vlib_node_runtime_t * node,
238 vlib_frame_t * from_frame,
241 u32 n_left_from, next_index, * from, * to_next;
242 u32 thread_index = vm->thread_index;
243 vnet_interface_main_t *im;
245 im = &vnet_get_main ()->interface_main;
246 from = vlib_frame_vector_args (from_frame);
247 n_left_from = from_frame->n_vectors;
249 next_index = INTERFACE_RX_DPO_INPUT;
251 while (n_left_from > 0)
255 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
257 while (n_left_from >= 4 && n_left_to_next > 2)
259 const interface_rx_dpo_t *ido0, *ido1;
260 u32 bi0, idoi0, bi1, idoi1;
261 vlib_buffer_t *b0, *b1;
272 b0 = vlib_get_buffer (vm, bi0);
273 b1 = vlib_get_buffer (vm, bi1);
275 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
276 idoi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
277 ido0 = interface_rx_dpo_get(idoi0);
278 ido1 = interface_rx_dpo_get(idoi1);
280 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
281 vnet_buffer(b1)->sw_if_index[VLIB_RX] = ido1->ido_sw_if_index;
285 vnet_update_l2_len (b0);
286 vnet_update_l2_len (b1);
289 vlib_increment_combined_counter (im->combined_sw_if_counters
290 + VNET_INTERFACE_COUNTER_RX,
292 ido0->ido_sw_if_index,
294 vlib_buffer_length_in_chain (vm, b0));
295 vlib_increment_combined_counter (im->combined_sw_if_counters
296 + VNET_INTERFACE_COUNTER_RX,
298 ido1->ido_sw_if_index,
300 vlib_buffer_length_in_chain (vm, b1));
302 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
304 interface_rx_dpo_trace_t *tr0;
306 tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
307 tr0->sw_if_index = ido0->ido_sw_if_index;
309 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
311 interface_rx_dpo_trace_t *tr1;
313 tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
314 tr1->sw_if_index = ido1->ido_sw_if_index;
318 while (n_left_from > 0 && n_left_to_next > 0)
320 const interface_rx_dpo_t * ido0;
331 b0 = vlib_get_buffer (vm, bi0);
333 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
334 ido0 = interface_rx_dpo_get(idoi0);
336 /* Swap the RX interface of the packet to the one the
337 * interface DPR represents */
338 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
340 /* Update l2_len to make l2 tag rewrite work */
342 vnet_update_l2_len (b0);
344 /* Bump the interface's RX coutners */
345 vlib_increment_combined_counter (im->combined_sw_if_counters
346 + VNET_INTERFACE_COUNTER_RX,
348 ido0->ido_sw_if_index,
350 vlib_buffer_length_in_chain (vm, b0));
352 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
354 interface_rx_dpo_trace_t *tr;
356 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
357 tr->sw_if_index = ido0->ido_sw_if_index;
360 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
362 return from_frame->n_vectors;
366 format_interface_rx_dpo_trace (u8 * s, va_list * args)
368 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
369 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
370 interface_rx_dpo_trace_t * t = va_arg (*args, interface_rx_dpo_trace_t *);
371 u32 indent = format_get_indent (s);
372 s = format (s, "%U sw_if_index:%d",
373 format_white_space, indent,
379 interface_rx_dpo_ip4 (vlib_main_t * vm,
380 vlib_node_runtime_t * node,
381 vlib_frame_t * from_frame)
383 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
387 interface_rx_dpo_ip6 (vlib_main_t * vm,
388 vlib_node_runtime_t * node,
389 vlib_frame_t * from_frame)
391 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
395 interface_rx_dpo_l2 (vlib_main_t * vm,
396 vlib_node_runtime_t * node,
397 vlib_frame_t * from_frame)
399 return (interface_rx_dpo_inline(vm, node, from_frame, 1));
402 VLIB_REGISTER_NODE (interface_rx_dpo_ip4_node) = {
403 .function = interface_rx_dpo_ip4,
404 .name = "interface-rx-dpo-ip4",
405 .vector_size = sizeof (u32),
406 .format_trace = format_interface_rx_dpo_trace,
410 [INTERFACE_RX_DPO_DROP] = "ip4-drop",
411 [INTERFACE_RX_DPO_INPUT] = "ip4-input",
415 VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip4_node,
416 interface_rx_dpo_ip4)
418 VLIB_REGISTER_NODE (interface_rx_dpo_ip6_node) = {
419 .function = interface_rx_dpo_ip6,
420 .name = "interface-rx-dpo-ip6",
421 .vector_size = sizeof (u32),
422 .format_trace = format_interface_rx_dpo_trace,
426 [INTERFACE_RX_DPO_DROP] = "ip6-drop",
427 [INTERFACE_RX_DPO_INPUT] = "ip6-input",
431 VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip6_node,
432 interface_rx_dpo_ip6)
434 VLIB_REGISTER_NODE (interface_rx_dpo_l2_node) = {
435 .function = interface_rx_dpo_l2,
436 .name = "interface-rx-dpo-l2",
437 .vector_size = sizeof (u32),
438 .format_trace = format_interface_rx_dpo_trace,
442 [INTERFACE_RX_DPO_DROP] = "error-drop",
443 [INTERFACE_RX_DPO_INPUT] = "l2-input",
447 VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_l2_node,