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/pw_cw.h>
17 #include <vnet/fib/fib_node.h>
19 #ifndef CLIB_MARCH_VARIANT
22 * pool of all MPLS Label DPOs
24 pw_cw_dpo_t *pw_cw_dpo_pool;
27 pw_cw_dpo_alloc (void)
31 pool_get_aligned_zero(pw_cw_dpo_pool, pwcw, 8);
37 pw_cw_dpo_get_index (pw_cw_dpo_t *pwcw)
39 return (pwcw - pw_cw_dpo_pool);
43 pw_cw_dpo_create (const dpo_id_t *parent,
48 pwcw = pw_cw_dpo_alloc();
51 * stack this disposition object on the parent given
59 * set up the return DPO to refer to this object
64 pw_cw_dpo_get_index(pwcw));
68 format_pw_cw_dpo (u8 *s, va_list *args)
70 index_t pwcwi = va_arg (*args, index_t);
71 u32 indent = va_arg (*args, u32);
74 if (pool_is_free_index(pw_cw_dpo_pool, pwcwi))
77 * the packet trace can be printed after the DPO has been deleted
79 return (format(s, "pw-cw[???,%d]:", pwcwi));
82 pwcw = pw_cw_dpo_get(pwcwi);
83 s = format(s, "pw-cw[%d]:", pwcwi);
85 s = format(s, "\n%U", format_white_space, indent);
86 s = format(s, "%U", format_dpo_id, &pwcw->pwcw_parent, indent+2);
92 pw_cw_dpo_lock (dpo_id_t *dpo)
96 pwcw = pw_cw_dpo_get(dpo->dpoi_index);
102 pw_cw_dpo_unlock (dpo_id_t *dpo)
106 pwcw = pw_cw_dpo_get(dpo->dpoi_index);
110 if (0 == pwcw->pwcw_locks)
112 dpo_reset(&pwcw->pwcw_parent);
113 pool_put(pw_cw_dpo_pool, pwcw);
116 #endif /* CLIB_MARCH_VARIANT */
119 * @brief A struct to hold tracing information for the MPLS label imposition
122 typedef struct pw_cw_trace_t_
131 pw_cw_pop_inline (vlib_main_t * vm,
132 vlib_node_runtime_t * node,
133 vlib_frame_t * from_frame)
135 u32 n_left_from, next_index, * from, * to_next;
137 from = vlib_frame_vector_args(from_frame);
138 n_left_from = from_frame->n_vectors;
139 next_index = node->cached_next_index;
141 while (n_left_from > 0)
145 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
147 while (n_left_from >= 4 && n_left_to_next >= 2)
149 pw_cw_dpo_t *pwcw0, *pwcw1;
150 u32 bi0, pwcwi0, bi1, pwcwi1;
151 vlib_buffer_t * b0, *b1;
154 bi0 = to_next[0] = from[0];
155 bi1 = to_next[1] = from[1];
157 /* Prefetch next iteration. */
159 vlib_buffer_t * p2, * p3;
161 p2 = vlib_get_buffer(vm, from[2]);
162 p3 = vlib_get_buffer(vm, from[3]);
164 vlib_prefetch_buffer_header(p2, STORE);
165 vlib_prefetch_buffer_header(p3, STORE);
167 CLIB_PREFETCH(p2->data, sizeof(pw_cw_t), STORE);
168 CLIB_PREFETCH(p3->data, sizeof(pw_cw_t), STORE);
176 b0 = vlib_get_buffer(vm, bi0);
177 b1 = vlib_get_buffer(vm, bi1);
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);
185 next0 = pwcw0->pwcw_parent.dpoi_next_node;
186 next1 = pwcw1->pwcw_parent.dpoi_next_node;
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;
191 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
193 pw_cw_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
195 tr->cw = *((pw_cw_t*) vlib_buffer_get_current(b0));
197 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
199 pw_cw_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
201 tr->cw = *((pw_cw_t*) vlib_buffer_get_current(b1));
205 vlib_buffer_advance (b0, sizeof(pw_cw_t));
206 vlib_buffer_advance (b1, sizeof(pw_cw_t));
208 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
210 bi0, bi1, next0, next1);
213 while (n_left_from > 0 && n_left_to_next > 0)
227 b0 = vlib_get_buffer(vm, bi0);
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;
234 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
236 pw_cw_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
238 tr->cw = *((pw_cw_t*) vlib_buffer_get_current(b0));
241 vlib_buffer_advance (b0, sizeof(pw_cw_t));
243 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
244 n_left_to_next, bi0, next0);
246 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
248 return from_frame->n_vectors;
252 format_pw_cw_trace (u8 * s, va_list * args)
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);
258 t = va_arg(*args, pw_cw_trace_t *);
260 s = format(s, "cw:0x%x", t->cw);
265 VLIB_NODE_FN (pw_cw_node) (vlib_main_t * vm,
266 vlib_node_runtime_t * node,
267 vlib_frame_t * frame)
269 return (pw_cw_pop_inline(vm, node, frame));
272 VLIB_REGISTER_NODE(pw_cw_node) = {
274 .vector_size = sizeof(u32),
275 .format_trace = format_pw_cw_trace,
278 #ifndef CLIB_MARCH_VARIANT
280 pw_cw_dpo_mem_show (void)
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));
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,
295 const static char* const pw_cw_proto_nodes[] =
301 const static char* const * const pw_cw_nodes[DPO_PROTO_NUM] =
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,
310 pw_cw_dpo_module_init (void)
312 dpo_register(DPO_PW_CW, &pwcw_vft, pw_cw_nodes);
315 #endif /* CLIB_MARCH_VARIANT */