blob: 4e5b1a5eeeb3bbd72e560ddae5488c95f37983e4 [file] [log] [blame]
Neale Ranns43161a82017-08-12 02:12:00 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/dpo/interface_rx_dpo.h>
17#include <vnet/fib/fib_node.h>
John Lofc23f122018-05-25 21:10:23 -040018#include <vnet/l2/l2_input.h>
Neale Ranns43161a82017-08-12 02:12:00 -070019
20/*
21 * The 'DB' of interface DPOs.
22 * There is only one per-interface per-protocol, so this is a per-interface
23 * vector
24 */
25static index_t *interface_rx_dpo_db[DPO_PROTO_NUM];
26
27static interface_rx_dpo_t *
28interface_rx_dpo_alloc (void)
29{
30 interface_rx_dpo_t *ido;
31
32 pool_get(interface_rx_dpo_pool, ido);
33
34 return (ido);
35}
36
37static inline interface_rx_dpo_t *
38interface_rx_dpo_get_from_dpo (const dpo_id_t *dpo)
39{
40 ASSERT(DPO_INTERFACE_RX == dpo->dpoi_type);
41
42 return (interface_rx_dpo_get(dpo->dpoi_index));
43}
44
45static inline index_t
46interface_rx_dpo_get_index (interface_rx_dpo_t *ido)
47{
48 return (ido - interface_rx_dpo_pool);
49}
50
51static void
52interface_rx_dpo_lock (dpo_id_t *dpo)
53{
54 interface_rx_dpo_t *ido;
55
56 ido = interface_rx_dpo_get_from_dpo(dpo);
57 ido->ido_locks++;
58}
59
60static void
61interface_rx_dpo_unlock (dpo_id_t *dpo)
62{
63 interface_rx_dpo_t *ido;
64
65 ido = interface_rx_dpo_get_from_dpo(dpo);
66 ido->ido_locks--;
67
68 if (0 == ido->ido_locks)
69 {
70 interface_rx_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
71 INDEX_INVALID;
72 pool_put(interface_rx_dpo_pool, ido);
73 }
74}
75
76/*
77 * interface_rx_dpo_add_or_lock
78 *
79 * Add/create and lock a new or lock an existing for the interface DPO
80 * on the interface and protocol given
81 */
82void
83interface_rx_dpo_add_or_lock (dpo_proto_t proto,
84 u32 sw_if_index,
85 dpo_id_t *dpo)
86{
87 interface_rx_dpo_t *ido;
88
89 vec_validate_init_empty(interface_rx_dpo_db[proto],
90 sw_if_index,
91 INDEX_INVALID);
92
93 if (INDEX_INVALID == interface_rx_dpo_db[proto][sw_if_index])
94 {
95 ido = interface_rx_dpo_alloc();
96
97 ido->ido_sw_if_index = sw_if_index;
98 ido->ido_proto = proto;
99
100 interface_rx_dpo_db[proto][sw_if_index] =
101 interface_rx_dpo_get_index(ido);
102 }
103 else
104 {
105 ido = interface_rx_dpo_get(interface_rx_dpo_db[proto][sw_if_index]);
106 }
107
108 dpo_set(dpo, DPO_INTERFACE_RX, proto, interface_rx_dpo_get_index(ido));
109}
110
111
112static clib_error_t *
113interface_rx_dpo_interface_state_change (vnet_main_t * vnm,
114 u32 sw_if_index,
115 u32 flags)
116{
117 /*
118 */
119 return (NULL);
120}
121
122VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
123 interface_rx_dpo_interface_state_change);
124
125/**
126 * @brief Registered callback for HW interface state changes
127 */
128static clib_error_t *
129interface_rx_dpo_hw_interface_state_change (vnet_main_t * vnm,
130 u32 hw_if_index,
131 u32 flags)
132{
133 return (NULL);
134}
135
136VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
137 interface_rx_dpo_hw_interface_state_change);
138
139static clib_error_t *
140interface_rx_dpo_interface_delete (vnet_main_t * vnm,
141 u32 sw_if_index,
142 u32 is_add)
143{
144 return (NULL);
145}
146
147VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
148 interface_rx_dpo_interface_delete);
149
150u8*
151format_interface_rx_dpo (u8* s, va_list *ap)
152{
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);
157
Ole Troan37d39672017-11-03 14:02:26 +0100158 return (format(s, "%U-rx-dpo: %U",
Neale Ranns43161a82017-08-12 02:12:00 -0700159 format_vnet_sw_interface_name,
160 vnm,
161 vnet_get_sw_interface(vnm, ido->ido_sw_if_index),
162 format_dpo_proto, ido->ido_proto));
163}
164
165static void
166interface_rx_dpo_mem_show (void)
167{
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));
172}
173
174
175const 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,
180};
181
182/**
183 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
184 * object.
185 *
186 * this means that these graph nodes are ones from which a glean is the
187 * parent object in the DPO-graph.
188 */
189const static char* const interface_rx_dpo_ip4_nodes[] =
190{
191 "interface-rx-dpo-ip4",
192 NULL,
193};
194const static char* const interface_rx_dpo_ip6_nodes[] =
195{
196 "interface-rx-dpo-ip6",
197 NULL,
198};
199const static char* const interface_rx_dpo_l2_nodes[] =
200{
201 "interface-rx-dpo-l2",
202 NULL,
203};
204
205const static char* const * const interface_rx_dpo_nodes[DPO_PROTO_NUM] =
206{
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,
211};
212
213void
214interface_rx_dpo_module_init (void)
215{
216 dpo_register(DPO_INTERFACE_RX,
217 &interface_rx_dpo_vft,
218 interface_rx_dpo_nodes);
219}
220
221/**
222 * @brief Interface DPO trace data
223 */
224typedef struct interface_rx_dpo_trace_t_
225{
226 u32 sw_if_index;
227} interface_rx_dpo_trace_t;
228
229typedef enum interface_rx_dpo_next_t_
230{
231 INTERFACE_RX_DPO_DROP = 0,
232 INTERFACE_RX_DPO_INPUT = 1,
233} interface_rx_dpo_next_t;
234
235always_inline uword
236interface_rx_dpo_inline (vlib_main_t * vm,
237 vlib_node_runtime_t * node,
John Lofc23f122018-05-25 21:10:23 -0400238 vlib_frame_t * from_frame,
239 u8 is_l2)
Neale Ranns43161a82017-08-12 02:12:00 -0700240{
241 u32 n_left_from, next_index, * from, * to_next;
242 u32 thread_index = vlib_get_thread_index ();
243 vnet_interface_main_t *im;
244
245 im = &vnet_get_main ()->interface_main;
246 from = vlib_frame_vector_args (from_frame);
247 n_left_from = from_frame->n_vectors;
248
Neale Ranns756cd942018-04-06 09:18:11 -0700249 next_index = INTERFACE_RX_DPO_INPUT;
Neale Ranns43161a82017-08-12 02:12:00 -0700250
251 while (n_left_from > 0)
252 {
253 u32 n_left_to_next;
254
255 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
256
257 while (n_left_from >= 4 && n_left_to_next > 2)
258 {
259 const interface_rx_dpo_t *ido0, *ido1;
260 u32 bi0, idoi0, bi1, idoi1;
261 vlib_buffer_t *b0, *b1;
262
263 bi0 = from[0];
264 to_next[0] = bi0;
265 bi1 = from[1];
266 to_next[1] = bi1;
267 from += 2;
268 to_next += 2;
269 n_left_from -= 2;
270 n_left_to_next -= 2;
271
272 b0 = vlib_get_buffer (vm, bi0);
273 b1 = vlib_get_buffer (vm, bi1);
274
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);
279
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;
282
John Lofc23f122018-05-25 21:10:23 -0400283 if (is_l2)
284 {
285 vnet_update_l2_len (b0);
286 vnet_update_l2_len (b1);
287 }
288
Neale Ranns43161a82017-08-12 02:12:00 -0700289 vlib_increment_combined_counter (im->combined_sw_if_counters
290 + VNET_INTERFACE_COUNTER_RX,
291 thread_index,
292 ido0->ido_sw_if_index,
293 1,
294 vlib_buffer_length_in_chain (vm, b0));
295 vlib_increment_combined_counter (im->combined_sw_if_counters
296 + VNET_INTERFACE_COUNTER_RX,
297 thread_index,
298 ido1->ido_sw_if_index,
299 1,
300 vlib_buffer_length_in_chain (vm, b1));
301
302 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
303 {
304 interface_rx_dpo_trace_t *tr0;
305
306 tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
307 tr0->sw_if_index = ido0->ido_sw_if_index;
308 }
309 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
310 {
311 interface_rx_dpo_trace_t *tr1;
312
313 tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
314 tr1->sw_if_index = ido1->ido_sw_if_index;
315 }
Neale Ranns43161a82017-08-12 02:12:00 -0700316 }
317
318 while (n_left_from > 0 && n_left_to_next > 0)
319 {
320 const interface_rx_dpo_t * ido0;
321 vlib_buffer_t * b0;
322 u32 bi0, idoi0;
323
324 bi0 = from[0];
325 to_next[0] = bi0;
326 from += 1;
327 to_next += 1;
328 n_left_from -= 1;
329 n_left_to_next -= 1;
330
331 b0 = vlib_get_buffer (vm, bi0);
332
333 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
334 ido0 = interface_rx_dpo_get(idoi0);
335
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;
339
John Lofc23f122018-05-25 21:10:23 -0400340 /* Update l2_len to make l2 tag rewrite work */
341 if (is_l2)
342 vnet_update_l2_len (b0);
343
Neale Ranns43161a82017-08-12 02:12:00 -0700344 /* Bump the interface's RX coutners */
345 vlib_increment_combined_counter (im->combined_sw_if_counters
346 + VNET_INTERFACE_COUNTER_RX,
347 thread_index,
348 ido0->ido_sw_if_index,
349 1,
350 vlib_buffer_length_in_chain (vm, b0));
351
352 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
353 {
354 interface_rx_dpo_trace_t *tr;
355
356 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
357 tr->sw_if_index = ido0->ido_sw_if_index;
358 }
Neale Ranns43161a82017-08-12 02:12:00 -0700359 }
360 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
361 }
362 return from_frame->n_vectors;
363}
364
365static u8 *
366format_interface_rx_dpo_trace (u8 * s, va_list * args)
367{
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 *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200371 u32 indent = format_get_indent (s);
Neale Ranns43161a82017-08-12 02:12:00 -0700372 s = format (s, "%U sw_if_index:%d",
373 format_white_space, indent,
374 t->sw_if_index);
375 return s;
376}
377
378static uword
379interface_rx_dpo_ip4 (vlib_main_t * vm,
380 vlib_node_runtime_t * node,
381 vlib_frame_t * from_frame)
382{
John Lofc23f122018-05-25 21:10:23 -0400383 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
Neale Ranns43161a82017-08-12 02:12:00 -0700384}
385
386static uword
387interface_rx_dpo_ip6 (vlib_main_t * vm,
388 vlib_node_runtime_t * node,
389 vlib_frame_t * from_frame)
390{
John Lofc23f122018-05-25 21:10:23 -0400391 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
Neale Ranns43161a82017-08-12 02:12:00 -0700392}
393
394static uword
395interface_rx_dpo_l2 (vlib_main_t * vm,
396 vlib_node_runtime_t * node,
397 vlib_frame_t * from_frame)
398{
John Lofc23f122018-05-25 21:10:23 -0400399 return (interface_rx_dpo_inline(vm, node, from_frame, 1));
Neale Ranns43161a82017-08-12 02:12:00 -0700400}
401
402VLIB_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,
407
408 .n_next_nodes = 2,
409 .next_nodes = {
410 [INTERFACE_RX_DPO_DROP] = "ip4-drop",
411 [INTERFACE_RX_DPO_INPUT] = "ip4-input",
412 },
413};
414
415VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip4_node,
416 interface_rx_dpo_ip4)
417
418VLIB_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,
423
424 .n_next_nodes = 2,
425 .next_nodes = {
426 [INTERFACE_RX_DPO_DROP] = "ip6-drop",
427 [INTERFACE_RX_DPO_INPUT] = "ip6-input",
428 },
429};
430
431VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip6_node,
432 interface_rx_dpo_ip6)
433
434VLIB_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,
439
440 .n_next_nodes = 2,
441 .next_nodes = {
442 [INTERFACE_RX_DPO_DROP] = "error-drop",
443 [INTERFACE_RX_DPO_INPUT] = "l2-input",
444 },
445};
446
447VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_l2_node,
448 interface_rx_dpo_l2)