vxlan: vxlan/vxlan.api API cleanup
[vpp.git] / src / vnet / dpo / pw_cw.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/pw_cw.h>
17 #include <vnet/fib/fib_node.h>
18
19 #ifndef CLIB_MARCH_VARIANT
20
21 /*
22  * pool of all MPLS Label DPOs
23  */
24 pw_cw_dpo_t *pw_cw_dpo_pool;
25
26 static pw_cw_dpo_t *
27 pw_cw_dpo_alloc (void)
28 {
29     pw_cw_dpo_t *pwcw;
30
31     pool_get_aligned_zero(pw_cw_dpo_pool, pwcw, 8);
32
33     return (pwcw);
34 }
35
36 static index_t
37 pw_cw_dpo_get_index (pw_cw_dpo_t *pwcw)
38 {
39     return (pwcw - pw_cw_dpo_pool);
40 }
41
42 void
43 pw_cw_dpo_create (const dpo_id_t *parent,
44                   dpo_id_t *dpo)
45 {
46     pw_cw_dpo_t *pwcw;
47
48     pwcw = pw_cw_dpo_alloc();
49
50     /*
51      * stack this disposition object on the parent given
52      */
53     dpo_stack(DPO_PW_CW,
54               parent->dpoi_proto,
55               &pwcw->pwcw_parent,
56               parent);
57
58     /*
59      * set up the return DPO to refer to this object
60      */
61     dpo_set(dpo,
62             DPO_PW_CW,
63             parent->dpoi_proto,
64             pw_cw_dpo_get_index(pwcw));
65 }
66
67 u8*
68 format_pw_cw_dpo (u8 *s, va_list *args)
69 {
70     index_t pwcwi = va_arg (*args, index_t);
71     u32 indent = va_arg (*args, u32);
72     pw_cw_dpo_t *pwcw;
73
74     if (pool_is_free_index(pw_cw_dpo_pool, pwcwi))
75     {
76         /*
77          * the packet trace can be printed after the DPO has been deleted
78          */
79         return (format(s, "pw-cw[???,%d]:", pwcwi));
80     }
81
82     pwcw = pw_cw_dpo_get(pwcwi);
83     s = format(s, "pw-cw[%d]:", pwcwi);
84
85     s = format(s, "\n%U", format_white_space, indent);
86     s = format(s, "%U", format_dpo_id, &pwcw->pwcw_parent, indent+2);
87
88     return (s);
89 }
90
91 static void
92 pw_cw_dpo_lock (dpo_id_t *dpo)
93 {
94     pw_cw_dpo_t *pwcw;
95
96     pwcw = pw_cw_dpo_get(dpo->dpoi_index);
97
98     pwcw->pwcw_locks++;
99 }
100
101 static void
102 pw_cw_dpo_unlock (dpo_id_t *dpo)
103 {
104     pw_cw_dpo_t *pwcw;
105
106     pwcw = pw_cw_dpo_get(dpo->dpoi_index);
107
108     pwcw->pwcw_locks--;
109
110     if (0 == pwcw->pwcw_locks)
111     {
112         dpo_reset(&pwcw->pwcw_parent);
113         pool_put(pw_cw_dpo_pool, pwcw);
114     }
115 }
116 #endif /* CLIB_MARCH_VARIANT */
117
118 /**
119  * @brief A struct to hold tracing information for the MPLS label imposition
120  * node.
121  */
122 typedef struct pw_cw_trace_t_
123 {
124     /**
125      * The CW popped
126      */
127     u32 cw;
128 } pw_cw_trace_t;
129
130 always_inline uword
131 pw_cw_pop_inline (vlib_main_t * vm,
132                   vlib_node_runtime_t * node,
133                   vlib_frame_t * from_frame)
134 {
135     u32 n_left_from, next_index, * from, * to_next;
136
137     from = vlib_frame_vector_args(from_frame);
138     n_left_from = from_frame->n_vectors;
139     next_index = node->cached_next_index;
140
141     while (n_left_from > 0)
142     {
143         u32 n_left_to_next;
144
145         vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
146
147         while (n_left_from >= 4 && n_left_to_next >= 2)
148         {
149             pw_cw_dpo_t *pwcw0, *pwcw1;
150             u32 bi0, pwcwi0, bi1, pwcwi1;
151             vlib_buffer_t * b0, *b1;
152             u32 next0, next1;
153
154             bi0 = to_next[0] = from[0];
155             bi1 = to_next[1] = from[1];
156
157             /* Prefetch next iteration. */
158             {
159                 vlib_buffer_t * p2, * p3;
160
161                 p2 = vlib_get_buffer(vm, from[2]);
162                 p3 = vlib_get_buffer(vm, from[3]);
163
164                 vlib_prefetch_buffer_header(p2, STORE);
165                 vlib_prefetch_buffer_header(p3, STORE);
166
167                 CLIB_PREFETCH(p2->data, sizeof(pw_cw_t), STORE);
168                 CLIB_PREFETCH(p3->data, sizeof(pw_cw_t), STORE);
169             }
170
171             from += 2;
172             to_next += 2;
173             n_left_from -= 2;
174             n_left_to_next -= 2;
175
176             b0 = vlib_get_buffer(vm, bi0);
177             b1 = vlib_get_buffer(vm, bi1);
178
179             /* get the next parent DPO for the next pop */
180             pwcwi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
181             pwcwi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
182             pwcw0 = pw_cw_dpo_get(pwcwi0);
183             pwcw1 = pw_cw_dpo_get(pwcwi1);
184
185             next0 = pwcw0->pwcw_parent.dpoi_next_node;
186             next1 = pwcw1->pwcw_parent.dpoi_next_node;
187
188             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = pwcw0->pwcw_parent.dpoi_index;
189             vnet_buffer(b1)->ip.adj_index[VLIB_TX] = pwcw1->pwcw_parent.dpoi_index;
190
191             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
192             {
193                 pw_cw_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
194
195                 tr->cw = *((pw_cw_t*) vlib_buffer_get_current(b0));
196             }
197             if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
198             {
199                 pw_cw_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
200
201                 tr->cw = *((pw_cw_t*) vlib_buffer_get_current(b1));
202             }
203
204             /* pop the PW CW */
205             vlib_buffer_advance (b0, sizeof(pw_cw_t));
206             vlib_buffer_advance (b1, sizeof(pw_cw_t));
207
208             vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
209                                             n_left_to_next,
210                                             bi0, bi1, next0, next1);
211         }
212
213         while (n_left_from > 0 && n_left_to_next > 0)
214         {
215             pw_cw_dpo_t *pwcw0;
216             vlib_buffer_t * b0;
217             u32 bi0, pwcwi0;
218             u32 next0;
219
220             bi0 = from[0];
221             to_next[0] = bi0;
222             from += 1;
223             to_next += 1;
224             n_left_from -= 1;
225             n_left_to_next -= 1;
226
227             b0 = vlib_get_buffer(vm, bi0);
228
229             pwcwi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
230             pwcw0 = pw_cw_dpo_get(pwcwi0);
231             next0 = pwcw0->pwcw_parent.dpoi_next_node;
232             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = pwcw0->pwcw_parent.dpoi_index;
233
234             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
235             {
236                 pw_cw_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
237
238                 tr->cw = *((pw_cw_t*) vlib_buffer_get_current(b0));
239             }
240
241             vlib_buffer_advance (b0, sizeof(pw_cw_t));
242
243             vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
244                                             n_left_to_next, bi0, next0);
245         }
246         vlib_put_next_frame(vm, node, next_index, n_left_to_next);
247     }
248     return from_frame->n_vectors;
249 }
250
251 static u8 *
252 format_pw_cw_trace (u8 * s, va_list * args)
253 {
254     CLIB_UNUSED(vlib_main_t * vm) = va_arg(*args, vlib_main_t *);
255     CLIB_UNUSED(vlib_node_t * node) = va_arg(*args, vlib_node_t *);
256     CLIB_UNUSED(pw_cw_trace_t * t);
257
258     t = va_arg(*args, pw_cw_trace_t *);
259
260     s = format(s, "cw:0x%x", t->cw);
261
262     return (s);
263 }
264
265 VLIB_NODE_FN (pw_cw_node) (vlib_main_t * vm,
266                            vlib_node_runtime_t * node,
267                            vlib_frame_t * frame)
268 {
269     return (pw_cw_pop_inline(vm, node, frame));
270 }
271
272 VLIB_REGISTER_NODE(pw_cw_node) = {
273     .name = "pw-cw-pop",
274     .vector_size = sizeof(u32),
275     .format_trace = format_pw_cw_trace,
276 };
277
278 #ifndef CLIB_MARCH_VARIANT
279 static void
280 pw_cw_dpo_mem_show (void)
281 {
282     fib_show_memory_usage("PW-CW",
283                           pool_elts(pw_cw_dpo_pool),
284                           pool_len(pw_cw_dpo_pool),
285                           sizeof(pw_cw_dpo_t));
286 }
287
288 const static dpo_vft_t pwcw_vft = {
289     .dv_lock = pw_cw_dpo_lock,
290     .dv_unlock = pw_cw_dpo_unlock,
291     .dv_format = format_pw_cw_dpo,
292     .dv_mem_show = pw_cw_dpo_mem_show,
293 };
294
295 const static char* const pw_cw_proto_nodes[] =
296 {
297     "pw-cw-pop",
298     NULL,
299 };
300
301 const static char* const * const pw_cw_nodes[DPO_PROTO_NUM] =
302 {
303     [DPO_PROTO_IP4]  = pw_cw_proto_nodes,
304     [DPO_PROTO_IP6]  = pw_cw_proto_nodes,
305     [DPO_PROTO_MPLS] = pw_cw_proto_nodes,
306     [DPO_PROTO_ETHERNET] = pw_cw_proto_nodes,
307 };
308
309 void
310 pw_cw_dpo_module_init (void)
311 {
312     dpo_register(DPO_PW_CW, &pwcw_vft, pw_cw_nodes);
313 }
314
315 #endif /* CLIB_MARCH_VARIANT */