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/ip/lookup.h>
17 #include <vnet/dpo/replicate_dpo.h>
18 #include <vnet/dpo/drop_dpo.h>
19 #include <vnet/adj/adj.h>
24 #define REP_DBG(_rep, _fmt, _args...) \
27 clib_warning("rep:[%s]:" _fmt, \
28 replicate_format(replicate_get_index((_rep)), \
34 #define REP_DBG(_p, _fmt, _args...)
37 #define foreach_replicate_dpo_error \
38 _(BUFFER_ALLOCATION_FAILURE, "Buffer Allocation Failure")
41 #define _(sym,str) REPLICATE_DPO_ERROR_##sym,
42 foreach_replicate_dpo_error
44 REPLICATE_DPO_N_ERROR,
45 } replicate_dpo_error_t;
47 static char * replicate_dpo_error_strings[] = {
48 #define _(sym,string) string,
49 foreach_replicate_dpo_error
54 * Pool of all DPOs. It's not static so the DP can have fast access
56 replicate_t *replicate_pool;
59 * The one instance of replicate main
61 replicate_main_t replicate_main;
64 replicate_get_index (const replicate_t *rep)
66 return (rep - replicate_pool);
69 static inline dpo_id_t*
70 replicate_get_buckets (replicate_t *rep)
72 if (REP_HAS_INLINE_BUCKETS(rep))
74 return (rep->rep_buckets_inline);
78 return (rep->rep_buckets);
83 replicate_alloc_i (void)
87 pool_get_aligned(replicate_pool, rep, CLIB_CACHE_LINE_BYTES);
88 memset(rep, 0, sizeof(*rep));
90 vlib_validate_combined_counter(&(replicate_main.repm_counters),
91 replicate_get_index(rep));
92 vlib_zero_combined_counter(&(replicate_main.repm_counters),
93 replicate_get_index(rep));
99 replicate_format (index_t repi,
100 replicate_format_flags_t flags,
109 rep = replicate_get(repi);
110 vlib_get_combined_counter(&(replicate_main.repm_counters), repi, &to);
111 buckets = replicate_get_buckets(rep);
113 s = format(s, "%U: ", format_dpo_type, DPO_REPLICATE);
114 s = format(s, "[index:%d buckets:%d ", repi, rep->rep_n_buckets);
115 s = format(s, "to:[%Ld:%Ld]]", to.packets, to.bytes);
117 for (i = 0; i < rep->rep_n_buckets; i++)
119 s = format(s, "\n%U", format_white_space, indent+2);
120 s = format(s, "[%d]", i);
121 s = format(s, " %U", format_dpo_id, &buckets[i], indent+6);
127 format_replicate (u8 * s, va_list * args)
129 index_t repi = va_arg(*args, index_t);
130 replicate_format_flags_t flags = va_arg(*args, replicate_format_flags_t);
132 return (replicate_format(repi, flags, 0, s));
135 format_replicate_dpo (u8 * s, va_list * args)
137 index_t repi = va_arg(*args, index_t);
138 u32 indent = va_arg(*args, u32);
140 return (replicate_format(repi, REPLICATE_FORMAT_DETAIL, indent, s));
145 replicate_create_i (u32 num_buckets,
146 dpo_proto_t rep_proto)
150 rep = replicate_alloc_i();
151 rep->rep_n_buckets = num_buckets;
152 rep->rep_proto = rep_proto;
154 if (!REP_HAS_INLINE_BUCKETS(rep))
156 vec_validate_aligned(rep->rep_buckets,
157 rep->rep_n_buckets - 1,
158 CLIB_CACHE_LINE_BYTES);
161 REP_DBG(rep, "create");
167 replicate_create (u32 n_buckets,
168 dpo_proto_t rep_proto)
170 return (replicate_get_index(replicate_create_i(n_buckets, rep_proto)));
174 replicate_set_bucket_i (replicate_t *rep,
177 const dpo_id_t *next)
179 dpo_stack(DPO_REPLICATE, rep->rep_proto, &buckets[bucket], next);
183 replicate_set_bucket (index_t repi,
185 const dpo_id_t *next)
190 rep = replicate_get(repi);
191 buckets = replicate_get_buckets(rep);
193 ASSERT(bucket < rep->rep_n_buckets);
195 replicate_set_bucket_i(rep, bucket, buckets, next);
199 replicate_is_drop (const dpo_id_t *dpo)
203 if (DPO_REPLICATE != dpo->dpoi_type)
206 rep = replicate_get(dpo->dpoi_index);
208 if (1 == rep->rep_n_buckets)
210 return (dpo_is_drop(replicate_get_bucket_i(rep, 0)));
216 replicate_get_bucket (index_t repi,
221 rep = replicate_get(repi);
223 return (replicate_get_bucket_i(rep, bucket));
227 static load_balance_path_t *
228 replicate_multipath_next_hop_fixup (load_balance_path_t *nhs,
229 dpo_proto_t drop_proto)
231 if (0 == vec_len(nhs))
233 load_balance_path_t *nh;
236 * we need something for the replicate. so use the drop
238 vec_add2(nhs, nh, 1);
241 dpo_copy(&nh->path_dpo, drop_dpo_get(drop_proto));
248 * Fill in adjacencies in block based on corresponding
249 * next hop adjacencies.
252 replicate_fill_buckets (replicate_t *rep,
253 load_balance_path_t *nhs,
257 load_balance_path_t * nh;
263 * the next-hops have normalised weights. that means their sum is the number
264 * of buckets we need to fill.
266 vec_foreach (nh, nhs)
268 for (ii = 0; ii < nh->path_weight; ii++)
270 ASSERT(bucket < n_buckets);
271 replicate_set_bucket_i(rep, bucket++, buckets, &nh->path_dpo);
277 replicate_set_n_buckets (replicate_t *rep,
280 rep->rep_n_buckets = n_buckets;
284 replicate_multipath_update (const dpo_id_t *dpo,
285 load_balance_path_t * next_hops)
287 load_balance_path_t * nh, * nhs;
292 ASSERT(DPO_REPLICATE == dpo->dpoi_type);
293 rep = replicate_get(dpo->dpoi_index);
294 nhs = replicate_multipath_next_hop_fixup(next_hops,
296 n_buckets = vec_len(nhs);
298 if (0 == rep->rep_n_buckets)
301 * first time initialisation. no packets inflight, so we can write
304 replicate_set_n_buckets(rep, n_buckets);
306 if (!REP_HAS_INLINE_BUCKETS(rep))
307 vec_validate_aligned(rep->rep_buckets,
308 rep->rep_n_buckets - 1,
309 CLIB_CACHE_LINE_BYTES);
311 replicate_fill_buckets(rep, nhs,
312 replicate_get_buckets(rep),
318 * This is a modification of an existing replicate.
319 * We need to ensure that packets in flight see a consistent state, that
320 * is the number of reported buckets the REP has
321 * is not more than it actually has. So if the
322 * number of buckets is increasing, we must update the bucket array first,
323 * then the reported number. vice-versa if the number of buckets goes down.
325 if (n_buckets == rep->rep_n_buckets)
328 * no change in the number of buckets. we can simply fill what
329 * is new over what is old.
331 replicate_fill_buckets(rep, nhs,
332 replicate_get_buckets(rep),
335 else if (n_buckets > rep->rep_n_buckets)
338 * we have more buckets. the old replicate map (if there is one)
339 * will remain valid, i.e. mapping to indices within range, so we
342 if (n_buckets > REP_NUM_INLINE_BUCKETS &&
343 rep->rep_n_buckets <= REP_NUM_INLINE_BUCKETS)
346 * the new increased number of buckets is crossing the threshold
347 * from the inline storage to out-line. Alloc the outline buckets
348 * first, then fixup the number. then reset the inlines.
350 ASSERT(NULL == rep->rep_buckets);
351 vec_validate_aligned(rep->rep_buckets,
353 CLIB_CACHE_LINE_BYTES);
355 replicate_fill_buckets(rep, nhs,
358 CLIB_MEMORY_BARRIER();
359 replicate_set_n_buckets(rep, n_buckets);
361 CLIB_MEMORY_BARRIER();
363 for (ii = 0; ii < REP_NUM_INLINE_BUCKETS; ii++)
365 dpo_reset(&rep->rep_buckets_inline[ii]);
370 if (n_buckets <= REP_NUM_INLINE_BUCKETS)
373 * we are not crossing the threshold and it's still inline buckets.
374 * we can write the new on the old..
376 replicate_fill_buckets(rep, nhs,
377 replicate_get_buckets(rep),
379 CLIB_MEMORY_BARRIER();
380 replicate_set_n_buckets(rep, n_buckets);
385 * we are not crossing the threshold. We need a new bucket array to
386 * hold the increased number of choices.
388 dpo_id_t *new_buckets, *old_buckets, *tmp_dpo;
391 old_buckets = replicate_get_buckets(rep);
393 vec_validate_aligned(new_buckets,
395 CLIB_CACHE_LINE_BYTES);
397 replicate_fill_buckets(rep, nhs, new_buckets, n_buckets);
398 CLIB_MEMORY_BARRIER();
399 rep->rep_buckets = new_buckets;
400 CLIB_MEMORY_BARRIER();
401 replicate_set_n_buckets(rep, n_buckets);
403 vec_foreach(tmp_dpo, old_buckets)
407 vec_free(old_buckets);
414 * bucket size shrinkage.
416 if (n_buckets <= REP_NUM_INLINE_BUCKETS &&
417 rep->rep_n_buckets > REP_NUM_INLINE_BUCKETS)
420 * the new decreased number of buckets is crossing the threshold
421 * from out-line storage to inline:
422 * 1 - Fill the inline buckets,
423 * 2 - fixup the number (and this point the inline buckets are
425 * 3 - free the outline buckets
427 replicate_fill_buckets(rep, nhs,
428 rep->rep_buckets_inline,
430 CLIB_MEMORY_BARRIER();
431 replicate_set_n_buckets(rep, n_buckets);
432 CLIB_MEMORY_BARRIER();
434 vec_foreach(tmp_dpo, rep->rep_buckets)
438 vec_free(rep->rep_buckets);
443 * not crossing the threshold.
444 * 1 - update the number to the smaller size
445 * 2 - write the new buckets
446 * 3 - reset those no longer used.
451 old_n_buckets = rep->rep_n_buckets;
452 buckets = replicate_get_buckets(rep);
454 replicate_set_n_buckets(rep, n_buckets);
455 CLIB_MEMORY_BARRIER();
457 replicate_fill_buckets(rep, nhs,
461 for (ii = n_buckets; ii < old_n_buckets; ii++)
463 dpo_reset(&buckets[ii]);
469 vec_foreach (nh, nhs)
471 dpo_reset(&nh->path_dpo);
477 replicate_lock (dpo_id_t *dpo)
481 rep = replicate_get(dpo->dpoi_index);
487 replicate_destroy (replicate_t *rep)
492 buckets = replicate_get_buckets(rep);
494 for (i = 0; i < rep->rep_n_buckets; i++)
496 dpo_reset(&buckets[i]);
499 REP_DBG(rep, "destroy");
500 if (!REP_HAS_INLINE_BUCKETS(rep))
502 vec_free(rep->rep_buckets);
505 pool_put(replicate_pool, rep);
509 replicate_unlock (dpo_id_t *dpo)
513 rep = replicate_get(dpo->dpoi_index);
517 if (0 == rep->rep_locks)
519 replicate_destroy(rep);
524 replicate_mem_show (void)
526 fib_show_memory_usage("replicate",
527 pool_elts(replicate_pool),
528 pool_len(replicate_pool),
529 sizeof(replicate_t));
532 const static dpo_vft_t rep_vft = {
533 .dv_lock = replicate_lock,
534 .dv_unlock = replicate_unlock,
535 .dv_format = format_replicate_dpo,
536 .dv_mem_show = replicate_mem_show,
540 * @brief The per-protocol VLIB graph nodes that are assigned to a replicate
543 * this means that these graph nodes are ones from which a replicate is the
544 * parent object in the DPO-graph.
546 const static char* const replicate_ip4_nodes[] =
551 const static char* const replicate_ip6_nodes[] =
556 const static char* const replicate_mpls_nodes[] =
562 const static char* const * const replicate_nodes[DPO_PROTO_NUM] =
564 [DPO_PROTO_IP4] = replicate_ip4_nodes,
565 [DPO_PROTO_IP6] = replicate_ip6_nodes,
566 [DPO_PROTO_MPLS] = replicate_mpls_nodes,
570 replicate_module_init (void)
572 dpo_register(DPO_REPLICATE, &rep_vft, replicate_nodes);
575 static clib_error_t *
576 replicate_show (vlib_main_t * vm,
577 unformat_input_t * input,
578 vlib_cli_command_t * cmd)
580 index_t repi = INDEX_INVALID;
582 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
584 if (unformat (input, "%d", &repi))
590 if (INDEX_INVALID != repi)
592 vlib_cli_output (vm, "%U", format_replicate, repi,
593 REPLICATE_FORMAT_DETAIL);
599 pool_foreach(rep, replicate_pool,
601 vlib_cli_output (vm, "%U", format_replicate,
602 replicate_get_index(rep),
603 REPLICATE_FORMAT_NONE);
610 VLIB_CLI_COMMAND (replicate_show_command, static) = {
611 .path = "show replicate",
612 .short_help = "show replicate [<index>]",
613 .function = replicate_show,
616 typedef struct replicate_trace_t_
623 replicate_inline (vlib_main_t * vm,
624 vlib_node_runtime_t * node,
625 vlib_frame_t * frame)
627 vlib_combined_counter_main_t * cm = &replicate_main.repm_counters;
628 replicate_main_t * rm = &replicate_main;
629 u32 n_left_from, * from, * to_next, next_index;
630 u32 cpu_index = os_get_cpu_number();
632 from = vlib_frame_vector_args (frame);
633 n_left_from = frame->n_vectors;
634 next_index = node->cached_next_index;
636 while (n_left_from > 0)
640 vlib_get_next_frame (vm, node, next_index,
641 to_next, n_left_to_next);
643 while (n_left_from > 0 && n_left_to_next > 0)
645 u32 next0, ci0, bi0, bucket, repi0;
646 const replicate_t *rep0;
647 vlib_buffer_t * b0, *c0;
648 const dpo_id_t *dpo0;
655 b0 = vlib_get_buffer (vm, bi0);
656 repi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
657 rep0 = replicate_get(repi0);
659 vlib_increment_combined_counter(
660 cm, cpu_index, repi0, 1,
661 vlib_buffer_length_in_chain(vm, b0));
663 vec_validate (rm->clones[cpu_index], rep0->rep_n_buckets - 1);
665 num_cloned = vlib_buffer_clone (vm, bi0, rm->clones[cpu_index], rep0->rep_n_buckets, 128);
667 if (num_cloned != rep0->rep_n_buckets)
669 vlib_node_increment_counter
670 (vm, node->node_index,
671 REPLICATE_DPO_ERROR_BUFFER_ALLOCATION_FAILURE, 1);
674 for (bucket = 0; bucket < num_cloned; bucket++)
676 ci0 = rm->clones[cpu_index][bucket];
677 c0 = vlib_get_buffer(vm, ci0);
683 dpo0 = replicate_get_bucket_i(rep0, bucket);
684 next0 = dpo0->dpoi_next_node;
685 vnet_buffer (c0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
687 if (PREDICT_FALSE(c0->flags & VLIB_BUFFER_IS_TRACED))
689 replicate_trace_t *t = vlib_add_trace (vm, node, c0, sizeof (*t));
690 t->rep_index = repi0;
694 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
695 to_next, n_left_to_next,
697 if (PREDICT_FALSE (n_left_to_next == 0))
699 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
700 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
703 vec_reset_length (rm->clones[cpu_index]);
706 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
709 return frame->n_vectors;
713 format_replicate_trace (u8 * s, va_list * args)
715 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
716 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
717 replicate_trace_t *t = va_arg (*args, replicate_trace_t *);
719 s = format (s, "replicate: %d via %U",
721 format_dpo_id, &t->dpo);
726 ip4_replicate (vlib_main_t * vm,
727 vlib_node_runtime_t * node,
728 vlib_frame_t * frame)
730 return (replicate_inline (vm, node, frame));
736 VLIB_REGISTER_NODE (ip4_replicate_node) = {
737 .function = ip4_replicate,
738 .name = "ip4-replicate",
739 .vector_size = sizeof (u32),
741 .n_errors = ARRAY_LEN(replicate_dpo_error_strings),
742 .error_strings = replicate_dpo_error_strings,
744 .format_trace = format_replicate_trace,
752 ip6_replicate (vlib_main_t * vm,
753 vlib_node_runtime_t * node,
754 vlib_frame_t * frame)
756 return (replicate_inline (vm, node, frame));
762 VLIB_REGISTER_NODE (ip6_replicate_node) = {
763 .function = ip6_replicate,
764 .name = "ip6-replicate",
765 .vector_size = sizeof (u32),
767 .n_errors = ARRAY_LEN(replicate_dpo_error_strings),
768 .error_strings = replicate_dpo_error_strings,
770 .format_trace = format_replicate_trace,
778 replicate_dpo_init (vlib_main_t * vm)
780 replicate_main_t * rm = &replicate_main;
782 vec_validate (rm->clones, vlib_num_workers());
787 VLIB_INIT_FUNCTION (replicate_dpo_init);