Add -fno-common compile option
[vpp.git] / src / vnet / dpo / interface_rx_dpo.c
1 /*
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>
18 #include <vnet/l2/l2_input.h>
19
20 interface_rx_dpo_t *interface_rx_dpo_pool;
21
22 /*
23  * The 'DB' of interface DPOs.
24  * There is only one  per-interface per-protocol, so this is a per-interface
25  * vector
26  */
27 static index_t *interface_rx_dpo_db[DPO_PROTO_NUM];
28
29 static interface_rx_dpo_t *
30 interface_rx_dpo_alloc (void)
31 {
32     interface_rx_dpo_t *ido;
33
34     pool_get(interface_rx_dpo_pool, ido);
35
36     return (ido);
37 }
38
39 static inline interface_rx_dpo_t *
40 interface_rx_dpo_get_from_dpo (const dpo_id_t *dpo)
41 {
42     ASSERT(DPO_INTERFACE_RX == dpo->dpoi_type);
43
44     return (interface_rx_dpo_get(dpo->dpoi_index));
45 }
46
47 static inline index_t
48 interface_rx_dpo_get_index (interface_rx_dpo_t *ido)
49 {
50     return (ido - interface_rx_dpo_pool);
51 }
52
53 static void
54 interface_rx_dpo_lock (dpo_id_t *dpo)
55 {
56     interface_rx_dpo_t *ido;
57
58     ido = interface_rx_dpo_get_from_dpo(dpo);
59     ido->ido_locks++;
60 }
61
62 static void
63 interface_rx_dpo_unlock (dpo_id_t *dpo)
64 {
65     interface_rx_dpo_t *ido;
66
67     ido = interface_rx_dpo_get_from_dpo(dpo);
68     ido->ido_locks--;
69
70     if (0 == ido->ido_locks)
71     {
72         interface_rx_dpo_db[ido->ido_proto][ido->ido_sw_if_index] =
73             INDEX_INVALID;
74         pool_put(interface_rx_dpo_pool, ido);
75     }
76 }
77
78 /*
79  * interface_rx_dpo_add_or_lock
80  *
81  * Add/create and lock a new or lock an existing for the interface DPO
82  * on the interface and protocol given
83  */
84 void
85 interface_rx_dpo_add_or_lock (dpo_proto_t proto,
86                               u32 sw_if_index,
87                               dpo_id_t *dpo)
88 {
89     interface_rx_dpo_t *ido;
90
91     vec_validate_init_empty(interface_rx_dpo_db[proto],
92                             sw_if_index,
93                             INDEX_INVALID);
94
95     if (INDEX_INVALID == interface_rx_dpo_db[proto][sw_if_index])
96     {
97         ido = interface_rx_dpo_alloc();
98
99         ido->ido_sw_if_index = sw_if_index;
100         ido->ido_proto = proto;
101
102         interface_rx_dpo_db[proto][sw_if_index] =
103             interface_rx_dpo_get_index(ido);
104     }
105     else
106     {
107         ido = interface_rx_dpo_get(interface_rx_dpo_db[proto][sw_if_index]);
108     }
109
110     dpo_set(dpo, DPO_INTERFACE_RX, proto, interface_rx_dpo_get_index(ido));
111 }
112
113
114 static clib_error_t *
115 interface_rx_dpo_interface_state_change (vnet_main_t * vnm,
116                                          u32 sw_if_index,
117                                          u32 flags)
118 {
119     /*
120      */
121     return (NULL);
122 }
123
124 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(
125     interface_rx_dpo_interface_state_change);
126
127 /**
128  * @brief Registered callback for HW interface state changes
129  */
130 static clib_error_t *
131 interface_rx_dpo_hw_interface_state_change (vnet_main_t * vnm,
132                                             u32 hw_if_index,
133                                             u32 flags)
134 {
135     return (NULL);
136 }
137
138 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
139     interface_rx_dpo_hw_interface_state_change);
140
141 static clib_error_t *
142 interface_rx_dpo_interface_delete (vnet_main_t * vnm,
143                                    u32 sw_if_index,
144                                    u32 is_add)
145 {
146     return (NULL);
147 }
148
149 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(
150     interface_rx_dpo_interface_delete);
151
152 u8*
153 format_interface_rx_dpo (u8* s, va_list *ap)
154 {
155     index_t index = va_arg(*ap, index_t);
156     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
157     vnet_main_t * vnm = vnet_get_main();
158     interface_rx_dpo_t *ido = interface_rx_dpo_get(index);
159
160     return (format(s, "%U-rx-dpo: %U",
161                    format_vnet_sw_interface_name,
162                    vnm,
163                    vnet_get_sw_interface(vnm, ido->ido_sw_if_index),
164                    format_dpo_proto, ido->ido_proto));
165 }
166
167 static void
168 interface_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
177 const 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  */
191 const static char* const interface_rx_dpo_ip4_nodes[] =
192 {
193     "interface-rx-dpo-ip4",
194     NULL,
195 };
196 const static char* const interface_rx_dpo_ip6_nodes[] =
197 {
198     "interface-rx-dpo-ip6",
199     NULL,
200 };
201 const static char* const interface_rx_dpo_l2_nodes[] =
202 {
203     "interface-rx-dpo-l2",
204     NULL,
205 };
206
207 const 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
215 void
216 interface_rx_dpo_module_init (void)
217 {
218     dpo_register(DPO_INTERFACE_RX,
219                  &interface_rx_dpo_vft,
220                  interface_rx_dpo_nodes);
221 }
222
223 /**
224  * @brief Interface DPO trace data
225  */
226 typedef struct interface_rx_dpo_trace_t_
227 {
228     u32 sw_if_index;
229 } interface_rx_dpo_trace_t;
230
231 typedef enum interface_rx_dpo_next_t_
232 {
233     INTERFACE_RX_DPO_DROP = 0,
234     INTERFACE_RX_DPO_INPUT = 1,
235 } interface_rx_dpo_next_t;
236
237 always_inline uword
238 interface_rx_dpo_inline (vlib_main_t * vm,
239                          vlib_node_runtime_t * node,
240                          vlib_frame_t * from_frame,
241                          u8 is_l2)
242 {
243     u32 n_left_from, next_index, * from, * to_next;
244     u32 thread_index = vm->thread_index;
245     vnet_interface_main_t *im;
246
247     im = &vnet_get_main ()->interface_main;
248     from = vlib_frame_vector_args (from_frame);
249     n_left_from = from_frame->n_vectors;
250
251     next_index = INTERFACE_RX_DPO_INPUT;
252
253     while (n_left_from > 0)
254     {
255         u32 n_left_to_next;
256
257         vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
258
259         while (n_left_from >= 4 && n_left_to_next > 2)
260         {
261             const interface_rx_dpo_t *ido0, *ido1;
262             u32 bi0, idoi0, bi1, idoi1;
263             vlib_buffer_t *b0, *b1;
264
265             bi0 = from[0];
266             to_next[0] = bi0;
267             bi1 = from[1];
268             to_next[1] = bi1;
269             from += 2;
270             to_next += 2;
271             n_left_from -= 2;
272             n_left_to_next -= 2;
273
274             b0 = vlib_get_buffer (vm, bi0);
275             b1 = vlib_get_buffer (vm, bi1);
276
277             idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
278             idoi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
279             ido0 = interface_rx_dpo_get(idoi0);
280             ido1 = interface_rx_dpo_get(idoi1);
281
282             vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
283             vnet_buffer(b1)->sw_if_index[VLIB_RX] = ido1->ido_sw_if_index;
284
285             if (is_l2)
286             {
287                 vnet_update_l2_len (b0);
288                 vnet_update_l2_len (b1);
289             }
290
291             vlib_increment_combined_counter (im->combined_sw_if_counters
292                                              + VNET_INTERFACE_COUNTER_RX,
293                                              thread_index,
294                                              ido0->ido_sw_if_index,
295                                              1,
296                                              vlib_buffer_length_in_chain (vm, b0));
297             vlib_increment_combined_counter (im->combined_sw_if_counters
298                                              + VNET_INTERFACE_COUNTER_RX,
299                                              thread_index,
300                                              ido1->ido_sw_if_index,
301                                              1,
302                                              vlib_buffer_length_in_chain (vm, b1));
303
304             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
305             {
306                 interface_rx_dpo_trace_t *tr0;
307
308                 tr0 = vlib_add_trace (vm, node, b0, sizeof (*tr0));
309                 tr0->sw_if_index = ido0->ido_sw_if_index;
310             }
311             if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
312             {
313                 interface_rx_dpo_trace_t *tr1;
314
315                 tr1 = vlib_add_trace (vm, node, b1, sizeof (*tr1));
316                 tr1->sw_if_index = ido1->ido_sw_if_index;
317             }
318         }
319
320         while (n_left_from > 0 && n_left_to_next > 0)
321         {
322             const interface_rx_dpo_t * ido0;
323             vlib_buffer_t * b0;
324             u32 bi0, idoi0;
325
326             bi0 = from[0];
327             to_next[0] = bi0;
328             from += 1;
329             to_next += 1;
330             n_left_from -= 1;
331             n_left_to_next -= 1;
332
333             b0 = vlib_get_buffer (vm, bi0);
334
335             idoi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
336             ido0 = interface_rx_dpo_get(idoi0);
337
338             /* Swap the RX interface of the packet to the one the
339              * interface DPR represents */
340             vnet_buffer(b0)->sw_if_index[VLIB_RX] = ido0->ido_sw_if_index;
341
342             /* Update l2_len to make l2 tag rewrite work */
343             if (is_l2)
344                 vnet_update_l2_len (b0);
345
346             /* Bump the interface's RX coutners */
347             vlib_increment_combined_counter (im->combined_sw_if_counters
348                                              + VNET_INTERFACE_COUNTER_RX,
349                                              thread_index,
350                                              ido0->ido_sw_if_index,
351                                              1,
352                                              vlib_buffer_length_in_chain (vm, b0));
353
354             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
355             {
356                 interface_rx_dpo_trace_t *tr;
357
358                 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
359                 tr->sw_if_index = ido0->ido_sw_if_index;
360             }
361         }
362         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
363     }
364     return from_frame->n_vectors;
365 }
366
367 static u8 *
368 format_interface_rx_dpo_trace (u8 * s, va_list * args)
369 {
370     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
371     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
372     interface_rx_dpo_trace_t * t = va_arg (*args, interface_rx_dpo_trace_t *);
373     u32 indent = format_get_indent (s);
374     s = format (s, "%U sw_if_index:%d",
375                 format_white_space, indent,
376                 t->sw_if_index);
377     return s;
378 }
379
380 static uword
381 interface_rx_dpo_ip4 (vlib_main_t * vm,
382                       vlib_node_runtime_t * node,
383                       vlib_frame_t * from_frame)
384 {
385     return (interface_rx_dpo_inline(vm, node, from_frame, 0));
386 }
387
388 static uword
389 interface_rx_dpo_ip6 (vlib_main_t * vm,
390                       vlib_node_runtime_t * node,
391                       vlib_frame_t * from_frame)
392 {
393     return (interface_rx_dpo_inline(vm, node, from_frame, 0));
394 }
395
396 static uword
397 interface_rx_dpo_l2 (vlib_main_t * vm,
398                      vlib_node_runtime_t * node,
399                      vlib_frame_t * from_frame)
400 {
401     return (interface_rx_dpo_inline(vm, node, from_frame, 1));
402 }
403
404 VLIB_REGISTER_NODE (interface_rx_dpo_ip4_node) = {
405     .function = interface_rx_dpo_ip4,
406     .name = "interface-rx-dpo-ip4",
407     .vector_size = sizeof (u32),
408     .format_trace = format_interface_rx_dpo_trace,
409
410     .n_next_nodes = 2,
411     .next_nodes = {
412         [INTERFACE_RX_DPO_DROP] = "ip4-drop",
413         [INTERFACE_RX_DPO_INPUT] = "ip4-input",
414     },
415 };
416
417 VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip4_node,
418                               interface_rx_dpo_ip4)
419
420 VLIB_REGISTER_NODE (interface_rx_dpo_ip6_node) = {
421     .function = interface_rx_dpo_ip6,
422     .name = "interface-rx-dpo-ip6",
423     .vector_size = sizeof (u32),
424     .format_trace = format_interface_rx_dpo_trace,
425
426     .n_next_nodes = 2,
427     .next_nodes = {
428         [INTERFACE_RX_DPO_DROP] = "ip6-drop",
429         [INTERFACE_RX_DPO_INPUT] = "ip6-input",
430     },
431 };
432
433 VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_ip6_node,
434                               interface_rx_dpo_ip6)
435
436 VLIB_REGISTER_NODE (interface_rx_dpo_l2_node) = {
437     .function = interface_rx_dpo_l2,
438     .name = "interface-rx-dpo-l2",
439     .vector_size = sizeof (u32),
440     .format_trace = format_interface_rx_dpo_trace,
441
442     .n_next_nodes = 2,
443     .next_nodes = {
444         [INTERFACE_RX_DPO_DROP] = "error-drop",
445         [INTERFACE_RX_DPO_INPUT] = "l2-input",
446     },
447 };
448
449 VLIB_NODE_FUNCTION_MULTIARCH (interface_rx_dpo_l2_node,
450                               interface_rx_dpo_l2)