2 * Copyright (c) 2018 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 <plugins/l3span/l3_span_dpo.h>
17 #include <vnet/fib/fib_path_list.h>
18 #include <vnet/dpo/replicate_dpo.h>
19 #include <vnet/dpo/load_balance.h>
22 * ID registered for L3 span DPOs
24 dpo_type_t l3_span_dpo_type;
27 * pool of all L3 Span DPOs
29 l3_span_dpo_t *l3_span_dpo_pool;
31 static l3_span_dpo_t *
32 l3_span_dpo_alloc (void)
36 pool_get_aligned(l3_span_dpo_pool, l3sd, CLIB_CACHE_LINE_BYTES);
37 memset(l3sd, 0, sizeof(*l3sd));
43 l3_span_dpo_get_index (l3_span_dpo_t *l3s)
45 return (l3s - l3_span_dpo_pool);
48 static fib_forward_chain_type_t
49 l3_span_dpo_proto_to_chain_type (dpo_proto_t dproto)
54 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
56 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
61 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
65 l3_span_dpo_create_and_lock (dpo_proto_t dproto,
73 * alloc an L3 span DPO and a replicate DPO
75 l3sd = l3_span_dpo_alloc();
78 * have the path-list contribute a load-balance DPO we can clone
80 fib_path_list_contribute_forwarding
82 l3_span_dpo_proto_to_chain_type(dproto),
83 FIB_PATH_LIST_FWD_FLAG_NONE,
86 ASSERT(DPO_LOAD_BALANCE == l3sd->l3sd_dpo.dpoi_type);
88 dpo_set(dpo, l3_span_dpo_type, dproto, l3_span_dpo_get_index(l3sd));
92 l3_span_dpo_update (index_t l3sdi,
97 l3sd = l3_span_dpo_get(l3sdi);
99 fib_path_list_contribute_forwarding
101 l3_span_dpo_proto_to_chain_type(l3sd->l3sd_dpo.dpoi_proto),
102 FIB_PATH_LIST_FWD_FLAG_NONE,
107 format_l3_span_dpo (u8 *s, va_list *args)
109 index_t index = va_arg (*args, index_t);
110 u32 indent = va_arg (*args, u32);
113 s = format(s, "l3-span:[%d]:", index);
115 if (pool_is_free_index(l3_span_dpo_pool, index))
118 * the packet trace can be printed after the DPO has been deleted
123 l3sd = l3_span_dpo_get(index);
125 if (l3sd->l3sd_flags & L3_SPAN_DPO_FLAG_CLONE)
126 s = format(s, " orig:%d", l3sd->l3sd_orig);
127 s = format(s, "\n%U", format_white_space, indent);
128 s = format(s, "%U", format_dpo_id, &l3sd->l3sd_dpo, indent+2);
134 l3_span_dpo_lock (dpo_id_t *dpo)
138 l3sd = l3_span_dpo_get(dpo->dpoi_index);
144 l3_span_dpo_unlock_dpo (dpo_id_t *dpo)
146 l3_span_dpo_unlock(dpo->dpoi_index);
150 l3_span_dpo_unlock (index_t l3sdi)
154 l3sd = l3_span_dpo_get(l3sdi);
158 if (0 == l3sd->l3sd_locks)
160 dpo_reset(&l3sd->l3sd_dpo);
161 pool_put(l3_span_dpo_pool, l3sd);
166 * @brief A struct to hold tracing information for the span
169 typedef struct l3_span_trace_t_
172 * The index of the original L3-span
178 l3_span_dpo_inline (vlib_main_t * vm,
179 vlib_node_runtime_t * node,
180 vlib_frame_t * from_frame)
182 u32 n_left_from, next_index, * from, * to_next;
184 from = vlib_frame_vector_args (from_frame);
185 n_left_from = from_frame->n_vectors;
187 next_index = node->cached_next_index;
189 while (n_left_from > 0)
193 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
195 while (n_left_from > 0 && n_left_to_next > 0)
197 l3_span_dpo_t *l3sd0;
209 b0 = vlib_get_buffer (vm, bi0);
211 /* dst lookup was done by ip4 lookup */
212 l3sdi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
213 l3sd0 = l3_span_dpo_get(l3sdi0);
215 next0 = l3sd0->l3sd_dpo.dpoi_next_node;
216 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = l3sd0->l3sd_dpo.dpoi_index;
218 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
220 l3_span_trace_t *tr =
221 vlib_add_trace (vm, node, b0, sizeof (*tr));
222 tr->orig = l3sd0->l3sd_orig;
225 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
226 n_left_to_next, bi0, next0);
228 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
230 return from_frame->n_vectors;
234 format_l3_span_trace (u8 * s, va_list * args)
236 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
237 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
240 t = va_arg (*args, l3_span_trace_t *);
242 s = format (s, "original:%d", t->orig);
248 ip4_l3_span (vlib_main_t * vm,
249 vlib_node_runtime_t * node,
250 vlib_frame_t * frame)
252 return (l3_span_dpo_inline(vm, node, frame));
255 VLIB_REGISTER_NODE (ip4_l3_span_node) = {
256 .function = ip4_l3_span,
257 .name = "ip4-l3-span",
258 .vector_size = sizeof (u32),
260 .format_trace = format_l3_span_trace,
266 VLIB_NODE_FUNCTION_MULTIARCH (ip4_l3_span_node,
270 ip6_l3_span (vlib_main_t * vm,
271 vlib_node_runtime_t * node,
272 vlib_frame_t * frame)
274 return (l3_span_dpo_inline(vm, node, frame));
277 VLIB_REGISTER_NODE (ip6_l3_span_node) = {
278 .function = ip6_l3_span,
279 .name = "ip6-l3-span",
280 .vector_size = sizeof (u32),
282 .format_trace = format_l3_span_trace,
288 VLIB_NODE_FUNCTION_MULTIARCH (ip6_l3_span_node,
292 l3_span_dpo_mk_interpose (const dpo_id_t *original,
293 const dpo_id_t *parent,
296 l3_span_dpo_t *l3sd_orig, *l3sd_clone;
301 l3sd_clone = l3_span_dpo_alloc();
302 l3sd_orig = l3_span_dpo_get(original->dpoi_index);
303 ASSERT(!(l3sd_orig->l3sd_flags & L3_SPAN_DPO_FLAG_CLONE));
305 l3sd_clone->l3sd_flags |= L3_SPAN_DPO_FLAG_CLONE;
306 l3sd_clone->l3sd_orig = original->dpoi_index;
308 n_buckets = load_balance_n_buckets(l3sd_orig->l3sd_dpo.dpoi_index);
311 * create a new replicate with one extra bucket. We'll
312 * use that extra bucket to stack the parent FIB gives us
315 repi = replicate_create(n_buckets + 1,
316 l3sd_orig->l3sd_dpo.dpoi_proto);
319 * set the first buckets in the replicte to match the original's.
320 * these are the paths to the collectors
322 for (ii = 0; ii <n_buckets; ii++)
326 load_balance_get_bucket(l3sd_orig->l3sd_dpo.dpoi_index, ii));
330 * in the last bucket stack the DPO given by FIB - this is how
331 * the FIB entry will forward.
333 replicate_set_bucket(repi, n_buckets, parent);
336 * save the replicate we just created on the cloned span
338 dpo_set(&l3sd_clone->l3sd_dpo,
340 l3sd_orig->l3sd_dpo.dpoi_proto,
344 * construct the Span clone for return to the caller
348 l3sd_orig->l3sd_dpo.dpoi_proto,
349 l3_span_dpo_get_index(l3sd_clone));
354 l3_span_dpo_mem_show (void)
356 fib_show_memory_usage("L3 Span",
357 pool_elts(l3_span_dpo_pool),
358 pool_len(l3_span_dpo_pool),
359 sizeof(l3_span_dpo_t));
362 const static dpo_vft_t l3sd_vft = {
363 .dv_lock = l3_span_dpo_lock,
364 .dv_unlock = l3_span_dpo_unlock_dpo,
365 .dv_format = format_l3_span_dpo,
366 .dv_mem_show = l3_span_dpo_mem_show,
367 .dv_mk_interpose = l3_span_dpo_mk_interpose,
370 const static char* const l3_span_ip4_nodes[] =
375 const static char* const l3_span_ip6_nodes[] =
381 const static char* const * const l3_span_dpo_nodes[DPO_PROTO_NUM] =
383 [DPO_PROTO_IP4] = l3_span_ip4_nodes,
384 [DPO_PROTO_IP6] = l3_span_ip6_nodes,
388 l3_span_dpo_module_init (void)
391 dpo_register_new_type(&l3sd_vft, l3_span_dpo_nodes);