blob: 5a519d344c12776858c6f434c299a4c652799801 [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
Filip Tehlareb9a27f2019-03-07 01:42:11 -080020#ifndef CLIB_MARCH_VARIANT
BenoƮt Ganne47727c02019-02-12 13:35:08 +010021interface_rx_dpo_t *interface_rx_dpo_pool;
22
Neale Ranns43161a82017-08-12 02:12:00 -070023/*
24 * The 'DB' of interface DPOs.
25 * There is only one per-interface per-protocol, so this is a per-interface
26 * vector
27 */
28static index_t *interface_rx_dpo_db[DPO_PROTO_NUM];
29
30static interface_rx_dpo_t *
31interface_rx_dpo_alloc (void)
32{
33 interface_rx_dpo_t *ido;
34
35 pool_get(interface_rx_dpo_pool, ido);
36
37 return (ido);
38}
39
40static inline interface_rx_dpo_t *
41interface_rx_dpo_get_from_dpo (const dpo_id_t *dpo)
42{
43 ASSERT(DPO_INTERFACE_RX == dpo->dpoi_type);
44
45 return (interface_rx_dpo_get(dpo->dpoi_index));
46}
47
48static inline index_t
49interface_rx_dpo_get_index (interface_rx_dpo_t *ido)
50{
51 return (ido - interface_rx_dpo_pool);
52}
53
54static void
55interface_rx_dpo_lock (dpo_id_t *dpo)
56{
57 interface_rx_dpo_t *ido;
58
59 ido = interface_rx_dpo_get_from_dpo(dpo);
60 ido->ido_locks++;
61}
62
63static void
64interface_rx_dpo_unlock (dpo_id_t *dpo)
65{
66 interface_rx_dpo_t *ido;
67
68 ido = interface_rx_dpo_get_from_dpo(dpo);
69 ido->ido_locks--;
70
71 if (0 == ido->ido_locks)
72 {
73 interface_rx_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
74 INDEX_INVALID;
75 pool_put(interface_rx_dpo_pool, ido);
76 }
77}
78
79/*
80 * interface_rx_dpo_add_or_lock
81 *
82 * Add/create and lock a new or lock an existing for the interface DPO
83 * on the interface and protocol given
84 */
85void
86interface_rx_dpo_add_or_lock (dpo_proto_t proto,
87 u32 sw_if_index,
88 dpo_id_t *dpo)
89{
90 interface_rx_dpo_t *ido;
91
92 vec_validate_init_empty(interface_rx_dpo_db[proto],
93 sw_if_index,
94 INDEX_INVALID);
95
96 if (INDEX_INVALID == interface_rx_dpo_db[proto][sw_if_index])
97 {
98 ido = interface_rx_dpo_alloc();
99
100 ido->ido_sw_if_index = sw_if_index;
101 ido->ido_proto = proto;
102
103 interface_rx_dpo_db[proto][sw_if_index] =
104 interface_rx_dpo_get_index(ido);
105 }
106 else
107 {
108 ido = interface_rx_dpo_get(interface_rx_dpo_db[proto][sw_if_index]);
109 }
110
111 dpo_set(dpo, DPO_INTERFACE_RX, proto, interface_rx_dpo_get_index(ido));
112}
Filip Tehlareb9a27f2019-03-07 01:42:11 -0800113#endif /* CLIB_MARCH_VARIANT */
Neale Ranns43161a82017-08-12 02:12:00 -0700114
115
116static clib_error_t *
117interface_rx_dpo_interface_state_change (vnet_main_t * vnm,
118 u32 sw_if_index,
119 u32 flags)
120{
121 /*
122 */
123 return (NULL);
124}
125
126VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
127 interface_rx_dpo_interface_state_change);
128
129/**
130 * @brief Registered callback for HW interface state changes
131 */
132static clib_error_t *
133interface_rx_dpo_hw_interface_state_change (vnet_main_t * vnm,
134 u32 hw_if_index,
135 u32 flags)
136{
137 return (NULL);
138}
139
140VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
141 interface_rx_dpo_hw_interface_state_change);
142
143static clib_error_t *
144interface_rx_dpo_interface_delete (vnet_main_t * vnm,
145 u32 sw_if_index,
146 u32 is_add)
147{
148 return (NULL);
149}
150
151VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
152 interface_rx_dpo_interface_delete);
153
Filip Tehlareb9a27f2019-03-07 01:42:11 -0800154#ifndef CLIB_MARCH_VARIANT
155static u8*
Neale Ranns43161a82017-08-12 02:12:00 -0700156format_interface_rx_dpo (u8* s, va_list *ap)
157{
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);
162
Vladislav Grishenkofb9d1ac2022-08-04 20:36:45 +0500163 return format (s, "%U-rx-dpo: %U", format_vnet_sw_if_index_name, vnm,
164 ido->ido_sw_if_index, format_dpo_proto, ido->ido_proto);
Neale Ranns43161a82017-08-12 02:12:00 -0700165}
166
167static void
168interface_rx_dpo_mem_show (void)
169{
170 fib_show_memory_usage("Interface",
171 pool_elts(interface_rx_dpo_pool),
172 pool_len(interface_rx_dpo_pool),
173 sizeof(interface_rx_dpo_t));
174}
175
176
177const static dpo_vft_t interface_rx_dpo_vft = {
178 .dv_lock = interface_rx_dpo_lock,
179 .dv_unlock = interface_rx_dpo_unlock,
180 .dv_format = format_interface_rx_dpo,
181 .dv_mem_show = interface_rx_dpo_mem_show,
182};
183
184/**
185 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
186 * object.
187 *
188 * this means that these graph nodes are ones from which a glean is the
189 * parent object in the DPO-graph.
190 */
191const static char* const interface_rx_dpo_ip4_nodes[] =
192{
193 "interface-rx-dpo-ip4",
194 NULL,
195};
196const static char* const interface_rx_dpo_ip6_nodes[] =
197{
198 "interface-rx-dpo-ip6",
199 NULL,
200};
201const static char* const interface_rx_dpo_l2_nodes[] =
202{
203 "interface-rx-dpo-l2",
204 NULL,
205};
206
207const static char* const * const interface_rx_dpo_nodes[DPO_PROTO_NUM] =
208{
209 [DPO_PROTO_IP4] = interface_rx_dpo_ip4_nodes,
210 [DPO_PROTO_IP6] = interface_rx_dpo_ip6_nodes,
211 [DPO_PROTO_ETHERNET] = interface_rx_dpo_l2_nodes,
212 [DPO_PROTO_MPLS] = NULL,
213};
214
215void
216interface_rx_dpo_module_init (void)
217{
218 dpo_register(DPO_INTERFACE_RX,
219 &interface_rx_dpo_vft,
220 interface_rx_dpo_nodes);
221}
Filip Tehlareb9a27f2019-03-07 01:42:11 -0800222#endif /* CLIB_MARCH_VARIANT */
Neale Ranns43161a82017-08-12 02:12:00 -0700223
224/**
225 * @brief Interface DPO trace data
226 */
227typedef struct interface_rx_dpo_trace_t_
228{
229 u32 sw_if_index;
230} interface_rx_dpo_trace_t;
231
232typedef enum interface_rx_dpo_next_t_
233{
234 INTERFACE_RX_DPO_DROP = 0,
235 INTERFACE_RX_DPO_INPUT = 1,
236} interface_rx_dpo_next_t;
237
238always_inline uword
239interface_rx_dpo_inline (vlib_main_t * vm,
240 vlib_node_runtime_t * node,
John Lofc23f122018-05-25 21:10:23 -0400241 vlib_frame_t * from_frame,
242 u8 is_l2)
Neale Ranns43161a82017-08-12 02:12:00 -0700243{
244 u32 n_left_from, next_index, * from, * to_next;
Damjan Marion067cd622018-07-11 12:47:43 +0200245 u32 thread_index = vm->thread_index;
Neale Ranns43161a82017-08-12 02:12:00 -0700246 vnet_interface_main_t *im;
247
248 im = &vnet_get_main ()->interface_main;
249 from = vlib_frame_vector_args (from_frame);
250 n_left_from = from_frame->n_vectors;
251
Neale Ranns756cd942018-04-06 09:18:11 -0700252 next_index = INTERFACE_RX_DPO_INPUT;
Neale Ranns43161a82017-08-12 02:12:00 -0700253
254 while (n_left_from > 0)
255 {
256 u32 n_left_to_next;
257
258 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
259
260 while (n_left_from >= 4 && n_left_to_next > 2)
261 {
262 const interface_rx_dpo_t *ido0, *ido1;
263 u32 bi0, idoi0, bi1, idoi1;
264 vlib_buffer_t *b0, *b1;
265
266 bi0 = from[0];
267 to_next[0] = bi0;
268 bi1 = from[1];
269 to_next[1] = bi1;
270 from += 2;
271 to_next += 2;
272 n_left_from -= 2;
273 n_left_to_next -= 2;
274
275 b0 = vlib_get_buffer (vm, bi0);
276 b1 = vlib_get_buffer (vm, bi1);
277
278 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
279 idoi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
280 ido0 = interface_rx_dpo_get(idoi0);
281 ido1 = interface_rx_dpo_get(idoi1);
282
283 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
284 vnet_buffer(b1)->sw_if_index[VLIB_RX] = ido1->ido_sw_if_index;
285
John Lofc23f122018-05-25 21:10:23 -0400286 if (is_l2)
287 {
288 vnet_update_l2_len (b0);
289 vnet_update_l2_len (b1);
290 }
291
Neale Ranns43161a82017-08-12 02:12:00 -0700292 vlib_increment_combined_counter (im->combined_sw_if_counters
293 + VNET_INTERFACE_COUNTER_RX,
294 thread_index,
295 ido0->ido_sw_if_index,
296 1,
297 vlib_buffer_length_in_chain (vm, b0));
298 vlib_increment_combined_counter (im->combined_sw_if_counters
299 + VNET_INTERFACE_COUNTER_RX,
300 thread_index,
301 ido1->ido_sw_if_index,
302 1,
303 vlib_buffer_length_in_chain (vm, b1));
304
305 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
306 {
307 interface_rx_dpo_trace_t *tr0;
308
309 tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
310 tr0->sw_if_index = ido0->ido_sw_if_index;
311 }
312 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
313 {
314 interface_rx_dpo_trace_t *tr1;
315
316 tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
317 tr1->sw_if_index = ido1->ido_sw_if_index;
318 }
Neale Ranns43161a82017-08-12 02:12:00 -0700319 }
320
321 while (n_left_from > 0 && n_left_to_next > 0)
322 {
323 const interface_rx_dpo_t * ido0;
324 vlib_buffer_t * b0;
325 u32 bi0, idoi0;
326
327 bi0 = from[0];
328 to_next[0] = bi0;
329 from += 1;
330 to_next += 1;
331 n_left_from -= 1;
332 n_left_to_next -= 1;
333
334 b0 = vlib_get_buffer (vm, bi0);
335
336 idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
337 ido0 = interface_rx_dpo_get(idoi0);
338
339 /* Swap the RX interface of the packet to the one the
340 * interface DPR represents */
341 vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
342
John Lofc23f122018-05-25 21:10:23 -0400343 /* Update l2_len to make l2 tag rewrite work */
344 if (is_l2)
345 vnet_update_l2_len (b0);
346
Neale Ranns43161a82017-08-12 02:12:00 -0700347 /* Bump the interface's RX coutners */
348 vlib_increment_combined_counter (im->combined_sw_if_counters
349 + VNET_INTERFACE_COUNTER_RX,
350 thread_index,
351 ido0->ido_sw_if_index,
352 1,
353 vlib_buffer_length_in_chain (vm, b0));
354
355 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
356 {
357 interface_rx_dpo_trace_t *tr;
358
359 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
360 tr->sw_if_index = ido0->ido_sw_if_index;
361 }
Neale Ranns43161a82017-08-12 02:12:00 -0700362 }
363 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
364 }
365 return from_frame->n_vectors;
366}
367
368static u8 *
369format_interface_rx_dpo_trace (u8 * s, va_list * args)
370{
371 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
372 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
373 interface_rx_dpo_trace_t * t = va_arg (*args, interface_rx_dpo_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200374 u32 indent = format_get_indent (s);
Neale Ranns43161a82017-08-12 02:12:00 -0700375 s = format (s, "%U sw_if_index:%d",
376 format_white_space, indent,
377 t->sw_if_index);
378 return s;
379}
380
Filip Tehlareb9a27f2019-03-07 01:42:11 -0800381VLIB_NODE_FN (interface_rx_dpo_ip4_node) (vlib_main_t * vm,
Neale Ranns43161a82017-08-12 02:12:00 -0700382 vlib_node_runtime_t * node,
383 vlib_frame_t * from_frame)
384{
John Lofc23f122018-05-25 21:10:23 -0400385 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
Neale Ranns43161a82017-08-12 02:12:00 -0700386}
387
Filip Tehlareb9a27f2019-03-07 01:42:11 -0800388VLIB_NODE_FN (interface_rx_dpo_ip6_node) (vlib_main_t * vm,
Neale Ranns43161a82017-08-12 02:12:00 -0700389 vlib_node_runtime_t * node,
390 vlib_frame_t * from_frame)
391{
John Lofc23f122018-05-25 21:10:23 -0400392 return (interface_rx_dpo_inline(vm, node, from_frame, 0));
Neale Ranns43161a82017-08-12 02:12:00 -0700393}
394
Filip Tehlareb9a27f2019-03-07 01:42:11 -0800395VLIB_NODE_FN (interface_rx_dpo_l2_node) (vlib_main_t * vm,
Neale Ranns43161a82017-08-12 02:12:00 -0700396 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) = {
Neale Ranns43161a82017-08-12 02:12:00 -0700403 .name = "interface-rx-dpo-ip4",
404 .vector_size = sizeof (u32),
405 .format_trace = format_interface_rx_dpo_trace,
406
407 .n_next_nodes = 2,
408 .next_nodes = {
409 [INTERFACE_RX_DPO_DROP] = "ip4-drop",
410 [INTERFACE_RX_DPO_INPUT] = "ip4-input",
411 },
412};
413
Neale Ranns43161a82017-08-12 02:12:00 -0700414
415VLIB_REGISTER_NODE (interface_rx_dpo_ip6_node) = {
Neale Ranns43161a82017-08-12 02:12:00 -0700416 .name = "interface-rx-dpo-ip6",
417 .vector_size = sizeof (u32),
418 .format_trace = format_interface_rx_dpo_trace,
419
420 .n_next_nodes = 2,
421 .next_nodes = {
422 [INTERFACE_RX_DPO_DROP] = "ip6-drop",
423 [INTERFACE_RX_DPO_INPUT] = "ip6-input",
424 },
425};
426
Neale Ranns43161a82017-08-12 02:12:00 -0700427
428VLIB_REGISTER_NODE (interface_rx_dpo_l2_node) = {
Neale Ranns43161a82017-08-12 02:12:00 -0700429 .name = "interface-rx-dpo-l2",
430 .vector_size = sizeof (u32),
431 .format_trace = format_interface_rx_dpo_trace,
432
433 .n_next_nodes = 2,
434 .next_nodes = {
435 [INTERFACE_RX_DPO_DROP] = "error-drop",
436 [INTERFACE_RX_DPO_INPUT] = "l2-input",
437 },
438};
439