4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/fib/ip6_fib.h>
20 #include <vnet/adj/adj.h>
21 #include <map/map_dpo.h>
22 #include <vppinfra/crc32.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
30 * This code supports the following MAP modes:
32 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
33 * ea_bits_len + ip4_prefix > 32
34 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
35 * Algorithmic Full IPv4 address (ea_bits_len > 0):
36 * ea_bits_len + ip4_prefix = 32
37 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
38 * Algorithmic IPv4 prefix (ea_bits_len > 0):
39 * ea_bits_len + ip4_prefix < 32
40 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
42 * Independent Shared IPv4 address (ea_bits_len = 0):
45 * Rule IPv6 address = 128, Rule PSID Set
46 * Independent Full IPv4 address (ea_bits_len = 0):
48 * psid_length = 0, ip6_prefix = 128
49 * Independent IPv4 prefix (ea_bits_len = 0):
51 * psid_length = 0, ip6_prefix = 128
56 * This code supports MAP-T:
58 * With a DMR prefix length of 64 or 96 (RFC6052).
65 map_create_domain (ip4_address_t * ip4_prefix,
67 ip6_address_t * ip6_prefix,
69 ip6_address_t * ip6_src,
73 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
75 u8 suffix_len, suffix_shift;
76 map_main_t *mm = &map_main;
77 dpo_id_t dpo_v4 = DPO_INVALID;
78 dpo_id_t dpo_v6 = DPO_INVALID;
81 /* Sanity check on the src prefix length */
82 if (flags & MAP_DOMAIN_TRANSLATION)
84 if (ip6_src_len != 96 && ip6_src_len != 64)
86 clib_warning ("MAP-T only supports prefix lengths of 64 and 96.");
92 if (ip6_src_len != 128)
95 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
101 /* How many, and which bits to grab from the IPv4 DA */
102 if (ip4_prefix_len + ea_bits_len < 32)
104 flags |= MAP_DOMAIN_PREFIX;
105 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
106 suffix_len = ea_bits_len;
111 suffix_len = 32 - ip4_prefix_len;
114 /* EA bits must be within the first 64 bits */
115 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
116 ip6_prefix_len + suffix_len + psid_length > 64))
119 ("Embedded Address bits must be within the first 64 bits of "
124 /* Get domain index */
125 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
126 clib_memset (d, 0, sizeof (*d));
127 *map_domain_index = d - mm->domains;
129 /* Init domain struct */
130 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
131 d->ip4_prefix_len = ip4_prefix_len;
132 d->ip6_prefix = *ip6_prefix;
133 d->ip6_prefix_len = ip6_prefix_len;
134 d->ip6_src = *ip6_src;
135 d->ip6_src_len = ip6_src_len;
136 d->ea_bits_len = ea_bits_len;
137 d->psid_offset = psid_offset;
138 d->psid_length = psid_length;
141 d->suffix_shift = suffix_shift;
142 d->suffix_mask = (1 << suffix_len) - 1;
144 d->psid_shift = 16 - psid_length - psid_offset;
145 d->psid_mask = (1 << d->psid_length) - 1;
146 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
148 /* MAP data-plane object */
149 if (d->flags & MAP_DOMAIN_TRANSLATION)
150 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
152 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
154 /* Create ip4 route */
156 .fp_proto = FIB_PROTOCOL_IP4,
157 .fp_len = d->ip4_prefix_len,
159 .ip4 = d->ip4_prefix,
163 fib_table_entry_special_dpo_add (0, &pfx,
165 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
169 * construct a DPO to use the v6 domain
171 if (d->flags & MAP_DOMAIN_TRANSLATION)
172 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
174 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
177 * Multiple MAP domains may share same source IPv6 TEP. Which is just dandy.
178 * We are not tracking the sharing. So a v4 lookup to find the correct
179 * domain post decap/trnaslate is always done
181 * Create ip6 route. This is a reference counted add. If the prefix
182 * already exists and is MAP sourced, it is now MAP source n+1 times
183 * and will need to be removed n+1 times.
185 fib_prefix_t pfx6 = {
186 .fp_proto = FIB_PROTOCOL_IP6,
187 .fp_len = d->ip6_src_len,
188 .fp_addr.ip6 = d->ip6_src,
191 fib_table_entry_special_dpo_add (0, &pfx6,
193 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
196 /* Validate packet/byte counters */
197 map_domain_counter_lock (mm);
199 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
201 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
203 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
206 for (i = 0; i < vec_len (mm->domain_counters); i++)
208 vlib_validate_combined_counter (&mm->domain_counters[i],
210 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
212 map_domain_counter_unlock (mm);
221 map_delete_domain (u32 map_domain_index)
223 map_main_t *mm = &map_main;
226 if (pool_is_free_index (mm->domains, map_domain_index))
228 clib_warning ("MAP domain delete: domain does not exist: %d",
233 d = pool_elt_at_index (mm->domains, map_domain_index);
236 .fp_proto = FIB_PROTOCOL_IP4,
237 .fp_len = d->ip4_prefix_len,
239 .ip4 = d->ip4_prefix,
243 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
245 fib_prefix_t pfx6 = {
246 .fp_proto = FIB_PROTOCOL_IP6,
247 .fp_len = d->ip6_src_len,
253 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
257 clib_mem_free (d->rules);
259 pool_put (mm->domains, d);
265 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
269 map_main_t *mm = &map_main;
271 if (pool_is_free_index (mm->domains, map_domain_index))
273 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
276 d = pool_elt_at_index (mm->domains, map_domain_index);
278 /* Rules are only used in 1:1 independent case */
279 if (d->ea_bits_len > 0)
284 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
285 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
288 clib_memset (d->rules, 0, l);
291 if (psid >= (0x1 << d->psid_length))
293 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
294 0x1 << d->psid_length);
300 d->rules[psid] = *tep;
304 clib_memset (&d->rules[psid], 0, sizeof (ip6_address_t));
309 #ifdef MAP_SKIP_IP6_LOOKUP
311 * Pre-resolvd per-protocol global next-hops
313 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
316 map_pre_resolve_init (map_main_pre_resolved_t * pr)
318 pr->fei = FIB_NODE_INDEX_INVALID;
319 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
323 format_map_pre_resolve (u8 * s, va_list * ap)
325 map_main_pre_resolved_t *pr = va_arg (*ap, map_main_pre_resolved_t *);
327 if (FIB_NODE_INDEX_INVALID != pr->fei)
329 const fib_prefix_t *pfx;
331 pfx = fib_entry_get_prefix (pr->fei);
333 return (format (s, "%U (%u)",
334 format_ip46_address, &pfx->fp_addr, IP46_TYPE_ANY,
335 pr->dpo.dpoi_index));
339 return (format (s, "un-set"));
345 * Function definition to inform the FIB node that its last lock has gone.
348 map_last_lock_gone (fib_node_t * node)
351 * The MAP is a root of the graph. As such
352 * it never has children and thus is never locked.
357 static map_main_pre_resolved_t *
358 map_from_fib_node (fib_node_t * node)
360 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
361 return ((map_main_pre_resolved_t *)
363 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
367 map_stack (map_main_pre_resolved_t * pr)
371 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
373 dpo_copy (&pr->dpo, dpo);
377 * Function definition to backwalk a FIB node
379 static fib_node_back_walk_rc_t
380 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
382 map_stack (map_from_fib_node (node));
384 return (FIB_NODE_BACK_WALK_CONTINUE);
388 * Function definition to get a FIB node from its index
391 map_fib_node_get (fib_node_index_t index)
393 return (&pre_resolved[index].node);
397 * Virtual function table registered by MPLS GRE tunnels
398 * for participation in the FIB object graph.
400 const static fib_node_vft_t map_vft = {
401 .fnv_get = map_fib_node_get,
402 .fnv_last_lock = map_last_lock_gone,
403 .fnv_back_walk = map_back_walk,
407 map_fib_resolve (map_main_pre_resolved_t * pr,
408 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
416 pr->fei = fib_table_entry_special_add (0, // default fib
418 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
419 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
424 map_fib_unresolve (map_main_pre_resolved_t * pr,
425 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
433 fib_entry_child_remove (pr->fei, pr->sibling);
435 fib_table_entry_special_remove (0, // default fib
436 &pfx, FIB_SOURCE_RR);
437 dpo_reset (&pr->dpo);
439 pr->fei = FIB_NODE_INDEX_INVALID;
440 pr->sibling = FIB_NODE_INDEX_INVALID;
444 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
446 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
448 ip46_address_t addr = {
452 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
453 FIB_PROTOCOL_IP6, 128, &addr);
455 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
456 FIB_PROTOCOL_IP6, 128, &addr);
458 if (ip4 && (ip4->as_u32 != 0))
460 ip46_address_t addr = {
464 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
465 FIB_PROTOCOL_IP4, 32, &addr);
467 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
468 FIB_PROTOCOL_IP4, 32, &addr);
473 static clib_error_t *
474 map_security_check_command_fn (vlib_main_t * vm,
475 unformat_input_t * input,
476 vlib_cli_command_t * cmd)
478 unformat_input_t _line_input, *line_input = &_line_input;
479 clib_error_t *error = NULL;
481 bool check_frag = false;
482 bool saw_enable = false;
483 bool saw_frag = false;
485 /* Get a line of input. */
486 if (!unformat_user (input, unformat_line_input, line_input))
489 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
491 if (unformat (line_input, "enable"))
496 else if (unformat (line_input, "disable"))
501 else if (unformat (line_input, "fragments on"))
506 else if (unformat (line_input, "fragments off"))
513 error = clib_error_return (0, "unknown input `%U'",
514 format_unformat_error, line_input);
521 error = clib_error_return (0,
522 "Must specify enable 'enable' or 'disable'");
528 error = clib_error_return (0, "Must specify fragments 'on' or 'off'");
532 map_param_set_security_check (enable, check_frag);
535 unformat_free (line_input);
541 static clib_error_t *
542 map_add_domain_command_fn (vlib_main_t * vm,
543 unformat_input_t * input, vlib_cli_command_t * cmd)
545 unformat_input_t _line_input, *line_input = &_line_input;
546 ip4_address_t ip4_prefix;
547 ip6_address_t ip6_prefix;
548 ip6_address_t ip6_src;
549 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
551 /* Optional arguments */
552 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
556 clib_error_t *error = NULL;
558 /* Get a line of input. */
559 if (!unformat_user (input, unformat_line_input, line_input))
562 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
565 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
570 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
575 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
580 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
582 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
584 else if (unformat (line_input, "psid-offset %d", &psid_offset))
586 else if (unformat (line_input, "psid-len %d", &psid_length))
588 else if (unformat (line_input, "mtu %d", &mtu))
590 else if (unformat (line_input, "map-t"))
591 flags |= MAP_DOMAIN_TRANSLATION;
592 else if (unformat (line_input, "rfc6052"))
593 flags |= (MAP_DOMAIN_TRANSLATION | MAP_DOMAIN_RFC6052);
596 error = clib_error_return (0, "unknown input `%U'",
597 format_unformat_error, line_input);
604 error = clib_error_return (0, "mandatory argument(s) missing");
608 map_create_domain (&ip4_prefix, ip4_prefix_len,
609 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
610 ea_bits_len, psid_offset, psid_length, &map_domain_index,
614 unformat_free (line_input);
619 static clib_error_t *
620 map_del_domain_command_fn (vlib_main_t * vm,
621 unformat_input_t * input, vlib_cli_command_t * cmd)
623 unformat_input_t _line_input, *line_input = &_line_input;
625 u32 map_domain_index;
626 clib_error_t *error = NULL;
628 /* Get a line of input. */
629 if (!unformat_user (input, unformat_line_input, line_input))
632 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
634 if (unformat (line_input, "index %d", &map_domain_index))
638 error = clib_error_return (0, "unknown input `%U'",
639 format_unformat_error, line_input);
646 error = clib_error_return (0, "mandatory argument(s) missing");
650 map_delete_domain (map_domain_index);
653 unformat_free (line_input);
658 static clib_error_t *
659 map_add_rule_command_fn (vlib_main_t * vm,
660 unformat_input_t * input, vlib_cli_command_t * cmd)
662 unformat_input_t _line_input, *line_input = &_line_input;
665 u32 psid = 0, map_domain_index;
666 clib_error_t *error = NULL;
668 /* Get a line of input. */
669 if (!unformat_user (input, unformat_line_input, line_input))
672 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
674 if (unformat (line_input, "index %d", &map_domain_index))
676 else if (unformat (line_input, "psid %d", &psid))
679 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
683 error = clib_error_return (0, "unknown input `%U'",
684 format_unformat_error, line_input);
691 error = clib_error_return (0, "mandatory argument(s) missing");
695 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
697 error = clib_error_return (0, "Failing to add Mapping Rule");
702 unformat_free (line_input);
707 #if MAP_SKIP_IP6_LOOKUP
708 static clib_error_t *
709 map_pre_resolve_command_fn (vlib_main_t * vm,
710 unformat_input_t * input,
711 vlib_cli_command_t * cmd)
713 unformat_input_t _line_input, *line_input = &_line_input;
714 ip4_address_t ip4nh, *p_v4 = NULL;
715 ip6_address_t ip6nh, *p_v6 = NULL;
716 clib_error_t *error = NULL;
719 clib_memset (&ip4nh, 0, sizeof (ip4nh));
720 clib_memset (&ip6nh, 0, sizeof (ip6nh));
722 /* Get a line of input. */
723 if (!unformat_user (input, unformat_line_input, line_input))
726 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
728 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
731 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
733 else if (unformat (line_input, "del"))
737 error = clib_error_return (0, "unknown input `%U'",
738 format_unformat_error, line_input);
743 map_pre_resolve (p_v4, p_v6, is_del);
746 unformat_free (line_input);
752 static clib_error_t *
753 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
754 unformat_input_t * input,
755 vlib_cli_command_t * cmd)
757 unformat_input_t _line_input, *line_input = &_line_input;
758 ip4_address_t icmp_src_address;
759 ip4_address_t *p_icmp_addr = 0;
760 map_main_t *mm = &map_main;
761 clib_error_t *error = NULL;
763 mm->icmp4_src_address.as_u32 = 0;
765 /* Get a line of input. */
766 if (!unformat_user (input, unformat_line_input, line_input))
769 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
772 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
774 mm->icmp4_src_address = icmp_src_address;
775 p_icmp_addr = &icmp_src_address;
779 error = clib_error_return (0, "unknown input `%U'",
780 format_unformat_error, line_input);
785 map_param_set_icmp (p_icmp_addr);
788 unformat_free (line_input);
793 static clib_error_t *
794 map_icmp_unreachables_command_fn (vlib_main_t * vm,
795 unformat_input_t * input,
796 vlib_cli_command_t * cmd)
798 unformat_input_t _line_input, *line_input = &_line_input;
800 clib_error_t *error = NULL;
801 bool enabled = false;
803 /* Get a line of input. */
804 if (!unformat_user (input, unformat_line_input, line_input))
807 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
810 if (unformat (line_input, "on"))
812 else if (unformat (line_input, "off"))
816 error = clib_error_return (0, "unknown input `%U'",
817 format_unformat_error, line_input);
824 error = clib_error_return (0, "mandatory argument(s) missing");
827 map_param_set_icmp6 (enabled);
830 unformat_free (line_input);
836 static clib_error_t *
837 map_fragment_command_fn (vlib_main_t * vm,
838 unformat_input_t * input, vlib_cli_command_t * cmd)
840 unformat_input_t _line_input, *line_input = &_line_input;
841 clib_error_t *error = NULL;
842 bool frag_inner = false;
843 bool frag_ignore_df = false;
844 bool saw_in_out = false;
847 /* Get a line of input. */
848 if (!unformat_user (input, unformat_line_input, line_input))
851 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
853 if (unformat (line_input, "inner"))
858 else if (unformat (line_input, "outer"))
863 else if (unformat (line_input, "ignore-df"))
865 frag_ignore_df = true;
868 else if (unformat (line_input, "honor-df"))
870 frag_ignore_df = false;
875 error = clib_error_return (0, "unknown input `%U'",
876 format_unformat_error, line_input);
883 error = clib_error_return (0, "Must specify 'inner' or 'outer'");
889 error = clib_error_return (0, "Must specify 'ignore-df' or 'honor-df'");
893 map_param_set_fragmentation (frag_inner, frag_ignore_df);
896 unformat_free (line_input);
901 static clib_error_t *
902 map_traffic_class_command_fn (vlib_main_t * vm,
903 unformat_input_t * input,
904 vlib_cli_command_t * cmd)
906 unformat_input_t _line_input, *line_input = &_line_input;
908 clib_error_t *error = NULL;
909 bool tc_copy = false;
912 /* Get a line of input. */
913 if (!unformat_user (input, unformat_line_input, line_input))
916 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
918 if (unformat (line_input, "copy"))
920 else if (unformat (line_input, "%x", &tc))
924 error = clib_error_return (0, "unknown input `%U'",
925 format_unformat_error, line_input);
930 map_param_set_traffic_class (tc_copy, tc);
933 unformat_free (line_input);
939 map_flags_to_string (u32 flags)
941 if (flags & MAP_DOMAIN_RFC6052)
943 if (flags & MAP_DOMAIN_PREFIX)
945 if (flags & MAP_DOMAIN_TRANSLATION)
951 format_map_domain (u8 * s, va_list * args)
953 map_domain_t *d = va_arg (*args, map_domain_t *);
954 bool counters = va_arg (*args, int);
955 map_main_t *mm = &map_main;
956 ip6_address_t ip6_prefix;
959 clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
961 ip6_prefix = d->ip6_prefix;
964 "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea-bits-len %d "
965 "psid-offset %d psid-len %d mtu %d %s",
967 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
968 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
969 format_ip6_address, &d->ip6_src, d->ip6_src_len,
970 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
971 map_flags_to_string (d->flags));
975 map_domain_counter_lock (mm);
977 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
978 d - mm->domains, &v);
979 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
980 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
981 d - mm->domains, &v);
982 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
983 map_domain_counter_unlock (mm);
985 s = format (s, "\n");
991 for (i = 0; i < (0x1 << d->psid_length); i++)
994 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
997 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
1005 format_map_ip4_reass (u8 * s, va_list * args)
1007 map_main_t *mm = &map_main;
1008 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
1009 map_ip4_reass_key_t *k = &r->key;
1010 f64 now = vlib_time_now (mm->vlib_main);
1011 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
1012 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1014 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
1015 format_ip4_address, &k->src.as_u8, format_ip4_address,
1016 &k->dst.as_u8, k->protocol,
1017 clib_net_to_host_u16 (k->fragment_id),
1018 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
1023 format_map_ip6_reass (u8 * s, va_list * args)
1025 map_main_t *mm = &map_main;
1026 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
1027 map_ip6_reass_key_t *k = &r->key;
1028 f64 now = vlib_time_now (mm->vlib_main);
1029 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
1030 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1032 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1033 format_ip6_address, &k->src.as_u8, format_ip6_address,
1034 &k->dst.as_u8, k->protocol,
1035 clib_net_to_host_u32 (k->fragment_id), dt);
1039 static clib_error_t *
1040 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1041 vlib_cli_command_t * cmd)
1043 unformat_input_t _line_input, *line_input = &_line_input;
1044 map_main_t *mm = &map_main;
1046 bool counters = false;
1047 u32 map_domain_index = ~0;
1048 clib_error_t *error = NULL;
1050 /* Get a line of input. */
1051 if (!unformat_user (input, unformat_line_input, line_input))
1054 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1056 if (unformat (line_input, "counters"))
1058 else if (unformat (line_input, "index %d", &map_domain_index))
1062 error = clib_error_return (0, "unknown input `%U'",
1063 format_unformat_error, line_input);
1068 if (pool_elts (mm->domains) == 0)
1069 vlib_cli_output (vm, "No MAP domains are configured...");
1071 if (map_domain_index == ~0)
1074 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1079 if (pool_is_free_index (mm->domains, map_domain_index))
1081 error = clib_error_return (0, "MAP domain does not exists %d",
1086 d = pool_elt_at_index (mm->domains, map_domain_index);
1087 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1091 unformat_free (line_input);
1096 static clib_error_t *
1097 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1098 vlib_cli_command_t * cmd)
1100 map_main_t *mm = &map_main;
1101 map_ip4_reass_t *f4;
1102 map_ip6_reass_t *f6;
1105 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
1108 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
1114 map_error_counter_get (u32 node_index, map_error_t map_error)
1116 vlib_main_t *vm = vlib_get_main ();
1117 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1118 vlib_error_main_t *em = &vm->error_main;
1119 vlib_error_t e = error_node->errors[map_error];
1120 vlib_node_t *n = vlib_get_node (vm, node_index);
1123 ci = vlib_error_get_code (e);
1124 ASSERT (ci < n->n_errors);
1125 ci += n->error_heap_index;
1127 return (em->counters[ci]);
1130 static clib_error_t *
1131 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1132 vlib_cli_command_t * cmd)
1134 map_main_t *mm = &map_main;
1136 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1137 if (pool_elts (mm->domains) == 0)
1139 vlib_cli_output (vm, "No MAP domains are configured...");
1144 pool_foreach(d, mm->domains, ({
1146 rulecount+= 0x1 << d->psid_length;
1147 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1149 domains += sizeof(*d);
1154 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1155 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1156 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1157 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1159 #if MAP_SKIP_IP6_LOOKUP
1160 vlib_cli_output (vm,
1161 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1162 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1163 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1168 vlib_cli_output (vm, "MAP traffic-class: copy");
1170 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1172 vlib_cli_output (vm,
1173 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1174 mm->sec_check ? "enabled" : "disabled",
1175 mm->sec_check_frag ? "enabled" : "disabled");
1177 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1178 format_ip4_address, &mm->icmp4_src_address);
1179 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1180 mm->icmp6_enabled ? "enabled" : "disabled");
1181 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1182 mm->frag_inner ? "enabled" : "disabled");
1183 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1184 mm->frag_ignore_df ? "enabled" : "disabled");
1189 vlib_combined_counter_main_t *cm = mm->domain_counters;
1190 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1191 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1195 clib_memset (total_pkts, 0, sizeof (total_pkts));
1196 clib_memset (total_bytes, 0, sizeof (total_bytes));
1198 map_domain_counter_lock (mm);
1199 vec_foreach (cm, mm->domain_counters)
1201 which = cm - mm->domain_counters;
1203 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1205 vlib_get_combined_counter (cm, i, &v);
1206 total_pkts[which] += v.packets;
1207 total_bytes[which] += v.bytes;
1210 map_domain_counter_unlock (mm);
1212 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1213 total_pkts[MAP_DOMAIN_COUNTER_TX],
1214 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1215 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1216 total_pkts[MAP_DOMAIN_COUNTER_RX],
1217 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1219 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1220 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1225 static clib_error_t *
1226 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1227 vlib_cli_command_t * cmd)
1229 unformat_input_t _line_input, *line_input = &_line_input;
1231 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1233 u64 buffers = ~(0ull);
1234 u8 ip4 = 0, ip6 = 0;
1236 if (!unformat_user (input, unformat_line_input, line_input))
1239 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1241 if (unformat (line_input, "lifetime %u", &lifetime))
1243 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1245 else if (unformat (line_input, "pool-size %u", &pool_size))
1247 else if (unformat (line_input, "buffers %llu", &buffers))
1249 else if (unformat (line_input, "ip4"))
1251 else if (unformat (line_input, "ip6"))
1255 unformat_free (line_input);
1256 return clib_error_return (0, "invalid input");
1259 unformat_free (line_input);
1262 return clib_error_return (0, "must specify ip4 and/or ip6");
1266 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1267 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1268 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1269 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1270 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1271 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1272 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1273 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1274 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1275 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1276 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1277 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1278 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1283 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1284 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1285 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1286 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1287 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1288 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1289 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1290 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1291 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1292 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1293 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1294 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1295 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1299 u32 reass = 0, packets = 0;
1300 rv = map_param_set_reassembly (!ip4, lifetime, pool_size, buffers, ht_ratio,
1306 vlib_cli_output (vm,
1307 "Note: destroyed-reassembly=%u , dropped-fragments=%u",
1311 case MAP_ERR_BAD_POOL_SIZE:
1312 return clib_error_return (0, "Could not set reass pool-size");
1314 case MAP_ERR_BAD_HT_RATIO:
1315 return clib_error_return (0, "Could not set reass ht-log2len");
1317 case MAP_ERR_BAD_LIFETIME:
1318 return clib_error_return (0, "Could not set ip6-reass lifetime");
1320 case MAP_ERR_BAD_BUFFERS:
1321 return clib_error_return (0, "Could not set ip6-reass buffers");
1323 case MAP_ERR_BAD_BUFFERS_TOO_LARGE:
1324 return clib_error_return (0,
1325 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1333 * packet trace format function
1336 format_map_trace (u8 * s, va_list * args)
1338 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1339 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1340 map_trace_t *t = va_arg (*args, map_trace_t *);
1341 u32 map_domain_index = t->map_domain_index;
1345 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1346 clib_net_to_host_u16 (port));
1351 static_always_inline map_ip4_reass_t *
1352 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1354 map_main_t *mm = &map_main;
1355 u32 ri = mm->ip4_reass_hash_table[bucket];
1356 while (ri != MAP_REASS_INDEX_NONE)
1358 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1359 if (r->key.as_u64[0] == k->as_u64[0] &&
1360 r->key.as_u64[1] == k->as_u64[1] &&
1361 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1365 ri = r->bucket_next;
1370 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1373 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1375 map_main_t *mm = &map_main;
1376 map_ip4_reass_get_fragments (r, pi_to_drop);
1378 // Unlink in hash bucket
1379 map_ip4_reass_t *r2 = NULL;
1380 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1381 while (r2i != map_ip4_reass_pool_index (r))
1383 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1384 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1385 r2i = r2->bucket_next;
1389 r2->bucket_next = r->bucket_next;
1393 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1397 if (r->fifo_next == map_ip4_reass_pool_index (r))
1399 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1403 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1404 mm->ip4_reass_fifo_last = r->fifo_prev;
1405 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1407 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1411 pool_put (mm->ip4_reass_pool, r);
1412 mm->ip4_reass_allocated--;
1416 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1417 u8 protocol, u32 ** pi_to_drop)
1420 map_main_t *mm = &map_main;
1421 map_ip4_reass_key_t k = {.src.data_u32 = src,
1422 .dst.data_u32 = dst,
1423 .fragment_id = fragment_id,
1424 .protocol = protocol
1428 #ifdef clib_crc32c_uses_intrinsics
1429 h = clib_crc32c ((u8 *) k.as_u32, 16);
1431 u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1432 h = clib_xxhash (tmp);
1434 h = h >> (32 - mm->ip4_reass_ht_log2len);
1436 f64 now = vlib_time_now (mm->vlib_main);
1438 //Cache garbage collection
1439 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1441 map_ip4_reass_t *last =
1442 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1443 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1444 map_ip4_reass_free (last, pi_to_drop);
1449 if ((r = map_ip4_reass_lookup (&k, h, now)))
1452 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1455 pool_get (mm->ip4_reass_pool, r);
1456 mm->ip4_reass_allocated++;
1458 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1459 r->fragments[i] = ~0;
1461 u32 ri = map_ip4_reass_pool_index (r);
1463 //Link in new bucket
1465 r->bucket_next = mm->ip4_reass_hash_table[h];
1466 mm->ip4_reass_hash_table[h] = ri;
1469 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1472 pool_elt_at_index (mm->ip4_reass_pool,
1473 mm->ip4_reass_fifo_last)->fifo_next;
1474 r->fifo_prev = mm->ip4_reass_fifo_last;
1475 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1476 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1480 r->fifo_next = r->fifo_prev = ri;
1481 mm->ip4_reass_fifo_last = ri;
1488 #ifdef MAP_IP4_REASS_COUNT_BYTES
1489 r->expected_total = 0xffff;
1497 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1499 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1503 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1504 if (r->fragments[i] == ~0)
1506 r->fragments[i] = pi;
1507 map_main.ip4_reass_buffered_counter++;
1513 static_always_inline map_ip6_reass_t *
1514 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1516 map_main_t *mm = &map_main;
1517 u32 ri = mm->ip6_reass_hash_table[bucket];
1518 while (ri != MAP_REASS_INDEX_NONE)
1520 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1521 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1522 r->key.as_u64[0] == k->as_u64[0] &&
1523 r->key.as_u64[1] == k->as_u64[1] &&
1524 r->key.as_u64[2] == k->as_u64[2] &&
1525 r->key.as_u64[3] == k->as_u64[3] &&
1526 r->key.as_u64[4] == k->as_u64[4])
1528 ri = r->bucket_next;
1533 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1536 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1538 map_main_t *mm = &map_main;
1540 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1541 if (r->fragments[i].pi != ~0)
1543 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1544 r->fragments[i].pi = ~0;
1545 map_main.ip6_reass_buffered_counter--;
1548 // Unlink in hash bucket
1549 map_ip6_reass_t *r2 = NULL;
1550 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1551 while (r2i != map_ip6_reass_pool_index (r))
1553 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1554 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1555 r2i = r2->bucket_next;
1559 r2->bucket_next = r->bucket_next;
1563 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1567 if (r->fifo_next == map_ip6_reass_pool_index (r))
1569 //Single element in the list, list is now empty
1570 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1574 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1575 mm->ip6_reass_fifo_last = r->fifo_prev;
1576 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1578 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1582 // Free from pool if necessary
1583 pool_put (mm->ip6_reass_pool, r);
1584 mm->ip6_reass_allocated--;
1588 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1589 u8 protocol, u32 ** pi_to_drop)
1592 map_main_t *mm = &map_main;
1593 map_ip6_reass_key_t k = {
1596 .fragment_id = fragment_id,
1597 .protocol = protocol
1603 #ifdef clib_crc32c_uses_intrinsics
1604 h = clib_crc32c ((u8 *) k.as_u32, 40);
1607 k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1608 h = clib_xxhash (tmp);
1611 h = h >> (32 - mm->ip6_reass_ht_log2len);
1613 f64 now = vlib_time_now (mm->vlib_main);
1615 //Cache garbage collection
1616 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1618 map_ip6_reass_t *last =
1619 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1620 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1621 map_ip6_reass_free (last, pi_to_drop);
1626 if ((r = map_ip6_reass_lookup (&k, h, now)))
1629 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1632 pool_get (mm->ip6_reass_pool, r);
1633 mm->ip6_reass_allocated++;
1634 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1636 r->fragments[i].pi = ~0;
1637 r->fragments[i].next_data_len = 0;
1638 r->fragments[i].next_data_offset = 0;
1641 u32 ri = map_ip6_reass_pool_index (r);
1643 //Link in new bucket
1645 r->bucket_next = mm->ip6_reass_hash_table[h];
1646 mm->ip6_reass_hash_table[h] = ri;
1649 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1652 pool_elt_at_index (mm->ip6_reass_pool,
1653 mm->ip6_reass_fifo_last)->fifo_next;
1654 r->fifo_prev = mm->ip6_reass_fifo_last;
1655 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1656 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1660 r->fifo_next = r->fifo_prev = ri;
1661 mm->ip6_reass_fifo_last = ri;
1667 r->ip4_header.ip_version_and_header_length = 0;
1668 #ifdef MAP_IP6_REASS_COUNT_BYTES
1669 r->expected_total = 0xffff;
1676 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1677 u16 data_offset, u16 next_data_offset,
1678 u8 * data_start, u16 data_len)
1680 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1681 u16 copied_len = (data_len > 20) ? 20 : data_len;
1683 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1686 //Lookup for fragments for the current buffer
1687 //and the one before that
1689 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1691 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1693 prev_f = &r->fragments[i]; // This is buffer for previous packet
1695 else if (r->fragments[i].next_data_offset == next_data_offset)
1697 f = &r->fragments[i]; // This is a buffer for the current packet
1699 else if (r->fragments[i].next_data_offset == 0)
1702 f = &r->fragments[i];
1703 else if (prev_f == NULL)
1704 prev_f = &r->fragments[i];
1708 if (!f || f->pi != ~0)
1716 clib_memcpy_fast (prev_f->next_data, data_start, copied_len);
1717 prev_f->next_data_len = copied_len;
1718 prev_f->next_data_offset = data_offset;
1722 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1725 if (r->ip4_header.ip_version_and_header_length == 0)
1726 clib_memcpy_fast (&r->ip4_header, data_start, sizeof (ip4_header_t));
1731 f->next_data_offset = next_data_offset;
1733 map_main.ip6_reass_buffered_counter++;
1739 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1741 map_main_t *mm = &map_main;
1744 if (dropped_packets)
1745 *dropped_packets = mm->ip4_reass_buffered_counter;
1747 *trashed_reass = mm->ip4_reass_allocated;
1748 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1750 u16 ri = mm->ip4_reass_fifo_last;
1753 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1754 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1755 if (r->fragments[i] != ~0)
1756 map_ip4_drop_pi (r->fragments[i]);
1759 pool_put (mm->ip4_reass_pool, r);
1761 while (ri != mm->ip4_reass_fifo_last);
1764 vec_free (mm->ip4_reass_hash_table);
1765 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1766 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1767 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1768 pool_free (mm->ip4_reass_pool);
1769 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1771 mm->ip4_reass_allocated = 0;
1772 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1773 mm->ip4_reass_buffered_counter = 0;
1777 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1779 u32 desired_size = (u32) (pool_size * ht_ratio);
1781 for (i = 1; i < 31; i++)
1782 if ((1 << i) >= desired_size)
1788 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1789 u32 * dropped_packets)
1791 map_main_t *mm = &map_main;
1792 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1795 map_ip4_reass_lock ();
1796 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1797 mm->ip4_reass_ht_log2len =
1798 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1799 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1800 map_ip4_reass_unlock ();
1805 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1806 u32 * dropped_packets)
1808 map_main_t *mm = &map_main;
1809 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1812 map_ip4_reass_lock ();
1813 mm->ip4_reass_conf_pool_size = pool_size;
1814 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1815 map_ip4_reass_unlock ();
1820 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1822 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1827 map_ip4_reass_conf_buffers (u32 buffers)
1829 map_main.ip4_reass_conf_buffers = buffers;
1834 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1836 map_main_t *mm = &map_main;
1837 if (dropped_packets)
1838 *dropped_packets = mm->ip6_reass_buffered_counter;
1840 *trashed_reass = mm->ip6_reass_allocated;
1842 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1844 u16 ri = mm->ip6_reass_fifo_last;
1847 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1848 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1849 if (r->fragments[i].pi != ~0)
1850 map_ip6_drop_pi (r->fragments[i].pi);
1853 pool_put (mm->ip6_reass_pool, r);
1855 while (ri != mm->ip6_reass_fifo_last);
1856 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1859 vec_free (mm->ip6_reass_hash_table);
1860 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1861 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1862 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1863 pool_free (mm->ip6_reass_pool);
1864 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1866 mm->ip6_reass_allocated = 0;
1867 mm->ip6_reass_buffered_counter = 0;
1871 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1872 u32 * dropped_packets)
1874 map_main_t *mm = &map_main;
1875 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1878 map_ip6_reass_lock ();
1879 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1880 mm->ip6_reass_ht_log2len =
1881 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1882 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1883 map_ip6_reass_unlock ();
1888 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1889 u32 * dropped_packets)
1891 map_main_t *mm = &map_main;
1892 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1895 map_ip6_reass_lock ();
1896 mm->ip6_reass_conf_pool_size = pool_size;
1897 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1898 map_ip6_reass_unlock ();
1903 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1905 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1910 map_ip6_reass_conf_buffers (u32 buffers)
1912 map_main.ip6_reass_conf_buffers = buffers;
1919 * Configure MAP reassembly behaviour
1922 * @cliexstart{map params reassembly}
1925 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1926 .path = "map params reassembly",
1927 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
1928 "[pool-size <pool-size>] [buffers <buffers>] "
1929 "[ht-ratio <ht-ratio>]",
1930 .function = map_params_reass_command_fn,
1934 * Set or copy the IP TOS/Traffic Class field
1937 * @cliexstart{map params traffic-class}
1939 * This command is used to set the traffic-class field in translated
1940 * or encapsulated packets. If copy is specifed (the default) then the
1941 * traffic-class/TOS field is copied from the original packet to the
1942 * translated / encapsulating header.
1945 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1946 .path = "map params traffic-class",
1947 .short_help = "map params traffic-class {0x0-0xff | copy}",
1948 .function = map_traffic_class_command_fn,
1952 * Bypass IP4/IP6 lookup
1955 * @cliexstart{map params pre-resolve}
1957 * Bypass a second FIB lookup of the translated or encapsulated
1958 * packet, and forward the packet directly to the specified
1959 * next-hop. This optimization trades forwarding flexibility for
1963 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1964 .path = "map params pre-resolve",
1965 .short_help = " map params pre-resolve {ip4-nh <address>} "
1966 "| {ip6-nh <address>}",
1967 .function = map_pre_resolve_command_fn,
1971 * Enable or disable the MAP-E inbound security check
1972 * Specifiy if the inbound security check should be done on fragments
1975 * @cliexstart{map params security-check}
1977 * By default, a decapsulated packet's IPv4 source address will be
1978 * verified against the outer header's IPv6 source address. Disabling
1979 * this feature will allow IPv4 source address spoofing.
1981 * Typically the inbound on-decapsulation security check is only done
1982 * on the first packet. The packet that contains the L4
1983 * information. While a security check on every fragment is possible,
1984 * it has a cost. State must be created on the first fragment.
1987 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1988 .path = "map params security-check",
1989 .short_help = "map params security-check enable|disable fragments on|off",
1990 .function = map_security_check_command_fn,
1995 * Specifiy the IPv4 source address used for relayed ICMP error messages
1998 * @cliexstart{map params icmp source-address}
2000 * This command specifies which IPv4 source address (must be local to
2001 * the system), that is used for relayed received IPv6 ICMP error
2005 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2006 .path = "map params icmp source-address",
2007 .short_help = "map params icmp source-address <ip4-address>",
2008 .function = map_icmp_relay_source_address_command_fn,
2012 * Send IPv6 ICMP unreachables
2015 * @cliexstart{map params icmp6 unreachables}
2017 * Send IPv6 ICMP unreachable messages back if security check fails or
2018 * no MAP domain exists.
2021 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2022 .path = "map params icmp6 unreachables",
2023 .short_help = "map params icmp6 unreachables {on|off}",
2024 .function = map_icmp_unreachables_command_fn,
2028 * Configure MAP fragmentation behaviour
2031 * @cliexstart{map params fragment}
2033 * Allows fragmentation of the IPv4 packet even if the DF bit is
2034 * set. The choice between inner or outer fragmentation of tunnel
2035 * packets is complicated. The benefit of inner fragmentation is that
2036 * the ultimate endpoint must reassemble, instead of the tunnel
2040 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2041 .path = "map params fragment",
2042 .short_help = "map params fragment inner|outer ignore-df|honor-df",
2043 .function = map_fragment_command_fn,
2051 * @cliexstart{map add domain}
2054 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2055 .path = "map add domain",
2056 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2057 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2058 "[map-t] [mtu <mtu>]",
2059 .function = map_add_domain_command_fn,
2063 * Add MAP rule to a domain
2066 * @cliexstart{map add rule}
2069 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2070 .path = "map add rule",
2071 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2072 .function = map_add_rule_command_fn,
2079 * @cliexstart{map del domain}
2082 VLIB_CLI_COMMAND(map_del_command, static) = {
2083 .path = "map del domain",
2084 .short_help = "map del domain index <domain>",
2085 .function = map_del_domain_command_fn,
2092 * @cliexstart{show map domain}
2095 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2096 .path = "show map domain",
2097 .short_help = "show map domain index <n> [counters]",
2098 .function = show_map_domain_command_fn,
2102 * Show MAP statistics
2105 * @cliexstart{show map stats}
2108 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2109 .path = "show map stats",
2110 .short_help = "show map stats",
2111 .function = show_map_stats_command_fn,
2115 * Show MAP fragmentation information
2118 * @cliexstart{show map fragments}
2121 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2122 .path = "show map fragments",
2123 .short_help = "show map fragments",
2124 .function = show_map_fragments_command_fn,
2127 VLIB_PLUGIN_REGISTER() = {
2128 .version = VPP_BUILD_VER,
2129 .description = "Mapping of address and port (MAP)",
2138 map_init (vlib_main_t * vm)
2140 map_main_t *mm = &map_main;
2141 clib_error_t *error = 0;
2142 mm->vnet_main = vnet_get_main ();
2145 #ifdef MAP_SKIP_IP6_LOOKUP
2146 fib_protocol_t proto;
2148 FOR_EACH_FIB_PROTOCOL (proto)
2150 map_pre_resolve_init (&pre_resolved[proto]);
2158 /* Inbound security check */
2159 mm->sec_check = true;
2160 mm->sec_check_frag = false;
2162 /* ICMP6 Type 1, Code 5 for security check failure */
2163 mm->icmp6_enabled = false;
2165 /* Inner or outer fragmentation */
2166 mm->frag_inner = false;
2167 mm->frag_ignore_df = false;
2169 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2170 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
2171 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
2173 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2174 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2176 /* IP4 virtual reassembly */
2177 mm->ip4_reass_hash_table = 0;
2178 mm->ip4_reass_pool = 0;
2179 mm->ip4_reass_lock =
2180 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2181 *mm->ip4_reass_lock = 0;
2182 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2183 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2184 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2185 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2186 mm->ip4_reass_ht_log2len =
2187 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2188 mm->ip4_reass_conf_pool_size);
2189 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2190 map_ip4_reass_reinit (NULL, NULL);
2192 /* IP6 virtual reassembly */
2193 mm->ip6_reass_hash_table = 0;
2194 mm->ip6_reass_pool = 0;
2195 mm->ip6_reass_lock =
2196 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2197 *mm->ip6_reass_lock = 0;
2198 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2199 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2200 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2201 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2202 mm->ip6_reass_ht_log2len =
2203 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2204 mm->ip6_reass_conf_pool_size);
2205 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2206 map_ip6_reass_reinit (NULL, NULL);
2208 #ifdef MAP_SKIP_IP6_LOOKUP
2209 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2211 map_dpo_module_init ();
2213 error = map_plugin_api_hookup (vm);
2218 VLIB_INIT_FUNCTION (map_init);
2221 * fd.io coding-style-patch-verification: ON
2224 * eval: (c-set-style "gnu")