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);
1173 vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
1175 vlib_cli_output (vm,
1176 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1177 mm->sec_check ? "enabled" : "disabled",
1178 mm->sec_check_frag ? "enabled" : "disabled");
1180 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1181 format_ip4_address, &mm->icmp4_src_address);
1182 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1183 mm->icmp6_enabled ? "enabled" : "disabled");
1184 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1185 mm->frag_inner ? "enabled" : "disabled");
1186 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1187 mm->frag_ignore_df ? "enabled" : "disabled");
1192 vlib_combined_counter_main_t *cm = mm->domain_counters;
1193 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1194 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1198 clib_memset (total_pkts, 0, sizeof (total_pkts));
1199 clib_memset (total_bytes, 0, sizeof (total_bytes));
1201 map_domain_counter_lock (mm);
1202 vec_foreach (cm, mm->domain_counters)
1204 which = cm - mm->domain_counters;
1206 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1208 vlib_get_combined_counter (cm, i, &v);
1209 total_pkts[which] += v.packets;
1210 total_bytes[which] += v.bytes;
1213 map_domain_counter_unlock (mm);
1215 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1216 total_pkts[MAP_DOMAIN_COUNTER_TX],
1217 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1218 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1219 total_pkts[MAP_DOMAIN_COUNTER_RX],
1220 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1222 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1223 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1228 static clib_error_t *
1229 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1230 vlib_cli_command_t * cmd)
1232 unformat_input_t _line_input, *line_input = &_line_input;
1234 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1236 u64 buffers = ~(0ull);
1237 u8 ip4 = 0, ip6 = 0;
1239 if (!unformat_user (input, unformat_line_input, line_input))
1242 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1244 if (unformat (line_input, "lifetime %u", &lifetime))
1246 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1248 else if (unformat (line_input, "pool-size %u", &pool_size))
1250 else if (unformat (line_input, "buffers %llu", &buffers))
1252 else if (unformat (line_input, "ip4"))
1254 else if (unformat (line_input, "ip6"))
1258 unformat_free (line_input);
1259 return clib_error_return (0, "invalid input");
1262 unformat_free (line_input);
1265 return clib_error_return (0, "must specify ip4 and/or ip6");
1269 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1270 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1271 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1272 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1273 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1274 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1275 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1276 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1277 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1278 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1279 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1280 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1281 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1286 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1287 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1288 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1289 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1290 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1291 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1292 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1293 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1294 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1295 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1296 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1297 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1298 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1302 u32 reass = 0, packets = 0;
1303 rv = map_param_set_reassembly (!ip4, lifetime, pool_size, buffers, ht_ratio,
1309 vlib_cli_output (vm,
1310 "Note: destroyed-reassembly=%u , dropped-fragments=%u",
1314 case MAP_ERR_BAD_POOL_SIZE:
1315 return clib_error_return (0, "Could not set reass pool-size");
1317 case MAP_ERR_BAD_HT_RATIO:
1318 return clib_error_return (0, "Could not set reass ht-log2len");
1320 case MAP_ERR_BAD_LIFETIME:
1321 return clib_error_return (0, "Could not set ip6-reass lifetime");
1323 case MAP_ERR_BAD_BUFFERS:
1324 return clib_error_return (0, "Could not set ip6-reass buffers");
1326 case MAP_ERR_BAD_BUFFERS_TOO_LARGE:
1327 return clib_error_return (0,
1328 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1335 static clib_error_t *
1336 map_if_command_fn (vlib_main_t * vm,
1337 unformat_input_t * input, vlib_cli_command_t * cmd)
1339 unformat_input_t _line_input, *line_input = &_line_input;
1340 clib_error_t *error = NULL;
1341 bool is_enable = true, is_translation = false;
1342 vnet_main_t *vnm = vnet_get_main ();
1343 u32 sw_if_index = ~0;
1345 /* Get a line of input. */
1346 if (!unformat_user (input, unformat_line_input, line_input))
1349 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1352 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1354 else if (unformat (line_input, "del"))
1356 else if (unformat (line_input, "map-t"))
1357 is_translation = true;
1360 error = clib_error_return (0, "unknown input `%U'",
1361 format_unformat_error, line_input);
1367 unformat_free (line_input);
1369 if (sw_if_index == ~0)
1371 error = clib_error_return (0, "unknown interface");
1375 int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
1378 error = clib_error_return (0, "failure enabling MAP on interface");
1386 * packet trace format function
1389 format_map_trace (u8 * s, va_list * args)
1391 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1392 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1393 map_trace_t *t = va_arg (*args, map_trace_t *);
1394 u32 map_domain_index = t->map_domain_index;
1398 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1399 clib_net_to_host_u16 (port));
1404 static_always_inline map_ip4_reass_t *
1405 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1407 map_main_t *mm = &map_main;
1408 u32 ri = mm->ip4_reass_hash_table[bucket];
1409 while (ri != MAP_REASS_INDEX_NONE)
1411 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1412 if (r->key.as_u64[0] == k->as_u64[0] &&
1413 r->key.as_u64[1] == k->as_u64[1] &&
1414 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1418 ri = r->bucket_next;
1423 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1426 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1428 map_main_t *mm = &map_main;
1429 map_ip4_reass_get_fragments (r, pi_to_drop);
1431 // Unlink in hash bucket
1432 map_ip4_reass_t *r2 = NULL;
1433 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1434 while (r2i != map_ip4_reass_pool_index (r))
1436 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1437 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1438 r2i = r2->bucket_next;
1442 r2->bucket_next = r->bucket_next;
1446 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1450 if (r->fifo_next == map_ip4_reass_pool_index (r))
1452 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1456 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1457 mm->ip4_reass_fifo_last = r->fifo_prev;
1458 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1460 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1464 pool_put (mm->ip4_reass_pool, r);
1465 mm->ip4_reass_allocated--;
1469 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1470 u8 protocol, u32 ** pi_to_drop)
1473 map_main_t *mm = &map_main;
1474 map_ip4_reass_key_t k = {.src.data_u32 = src,
1475 .dst.data_u32 = dst,
1476 .fragment_id = fragment_id,
1477 .protocol = protocol
1481 #ifdef clib_crc32c_uses_intrinsics
1482 h = clib_crc32c ((u8 *) k.as_u32, 16);
1484 u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1485 h = clib_xxhash (tmp);
1487 h = h >> (32 - mm->ip4_reass_ht_log2len);
1489 f64 now = vlib_time_now (mm->vlib_main);
1491 //Cache garbage collection
1492 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1494 map_ip4_reass_t *last =
1495 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1496 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1497 map_ip4_reass_free (last, pi_to_drop);
1502 if ((r = map_ip4_reass_lookup (&k, h, now)))
1505 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1508 pool_get (mm->ip4_reass_pool, r);
1509 mm->ip4_reass_allocated++;
1511 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1512 r->fragments[i] = ~0;
1514 u32 ri = map_ip4_reass_pool_index (r);
1516 //Link in new bucket
1518 r->bucket_next = mm->ip4_reass_hash_table[h];
1519 mm->ip4_reass_hash_table[h] = ri;
1522 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1525 pool_elt_at_index (mm->ip4_reass_pool,
1526 mm->ip4_reass_fifo_last)->fifo_next;
1527 r->fifo_prev = mm->ip4_reass_fifo_last;
1528 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1529 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1533 r->fifo_next = r->fifo_prev = ri;
1534 mm->ip4_reass_fifo_last = ri;
1541 #ifdef MAP_IP4_REASS_COUNT_BYTES
1542 r->expected_total = 0xffff;
1550 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1552 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1556 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1557 if (r->fragments[i] == ~0)
1559 r->fragments[i] = pi;
1560 map_main.ip4_reass_buffered_counter++;
1566 static_always_inline map_ip6_reass_t *
1567 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1569 map_main_t *mm = &map_main;
1570 u32 ri = mm->ip6_reass_hash_table[bucket];
1571 while (ri != MAP_REASS_INDEX_NONE)
1573 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1574 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1575 r->key.as_u64[0] == k->as_u64[0] &&
1576 r->key.as_u64[1] == k->as_u64[1] &&
1577 r->key.as_u64[2] == k->as_u64[2] &&
1578 r->key.as_u64[3] == k->as_u64[3] &&
1579 r->key.as_u64[4] == k->as_u64[4])
1581 ri = r->bucket_next;
1586 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1589 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1591 map_main_t *mm = &map_main;
1593 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1594 if (r->fragments[i].pi != ~0)
1596 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1597 r->fragments[i].pi = ~0;
1598 map_main.ip6_reass_buffered_counter--;
1601 // Unlink in hash bucket
1602 map_ip6_reass_t *r2 = NULL;
1603 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1604 while (r2i != map_ip6_reass_pool_index (r))
1606 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1607 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1608 r2i = r2->bucket_next;
1612 r2->bucket_next = r->bucket_next;
1616 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1620 if (r->fifo_next == map_ip6_reass_pool_index (r))
1622 //Single element in the list, list is now empty
1623 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1627 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1628 mm->ip6_reass_fifo_last = r->fifo_prev;
1629 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1631 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1635 // Free from pool if necessary
1636 pool_put (mm->ip6_reass_pool, r);
1637 mm->ip6_reass_allocated--;
1641 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1642 u8 protocol, u32 ** pi_to_drop)
1645 map_main_t *mm = &map_main;
1646 map_ip6_reass_key_t k = {
1649 .fragment_id = fragment_id,
1650 .protocol = protocol
1656 #ifdef clib_crc32c_uses_intrinsics
1657 h = clib_crc32c ((u8 *) k.as_u32, 40);
1660 k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1661 h = clib_xxhash (tmp);
1664 h = h >> (32 - mm->ip6_reass_ht_log2len);
1666 f64 now = vlib_time_now (mm->vlib_main);
1668 //Cache garbage collection
1669 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1671 map_ip6_reass_t *last =
1672 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1673 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1674 map_ip6_reass_free (last, pi_to_drop);
1679 if ((r = map_ip6_reass_lookup (&k, h, now)))
1682 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1685 pool_get (mm->ip6_reass_pool, r);
1686 mm->ip6_reass_allocated++;
1687 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1689 r->fragments[i].pi = ~0;
1690 r->fragments[i].next_data_len = 0;
1691 r->fragments[i].next_data_offset = 0;
1694 u32 ri = map_ip6_reass_pool_index (r);
1696 //Link in new bucket
1698 r->bucket_next = mm->ip6_reass_hash_table[h];
1699 mm->ip6_reass_hash_table[h] = ri;
1702 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1705 pool_elt_at_index (mm->ip6_reass_pool,
1706 mm->ip6_reass_fifo_last)->fifo_next;
1707 r->fifo_prev = mm->ip6_reass_fifo_last;
1708 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1709 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1713 r->fifo_next = r->fifo_prev = ri;
1714 mm->ip6_reass_fifo_last = ri;
1720 r->ip4_header.ip_version_and_header_length = 0;
1721 #ifdef MAP_IP6_REASS_COUNT_BYTES
1722 r->expected_total = 0xffff;
1729 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1730 u16 data_offset, u16 next_data_offset,
1731 u8 * data_start, u16 data_len)
1733 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1734 u16 copied_len = (data_len > 20) ? 20 : data_len;
1736 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1739 //Lookup for fragments for the current buffer
1740 //and the one before that
1742 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1744 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1746 prev_f = &r->fragments[i]; // This is buffer for previous packet
1748 else if (r->fragments[i].next_data_offset == next_data_offset)
1750 f = &r->fragments[i]; // This is a buffer for the current packet
1752 else if (r->fragments[i].next_data_offset == 0)
1755 f = &r->fragments[i];
1756 else if (prev_f == NULL)
1757 prev_f = &r->fragments[i];
1761 if (!f || f->pi != ~0)
1769 clib_memcpy_fast (prev_f->next_data, data_start, copied_len);
1770 prev_f->next_data_len = copied_len;
1771 prev_f->next_data_offset = data_offset;
1775 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1778 if (r->ip4_header.ip_version_and_header_length == 0)
1779 clib_memcpy_fast (&r->ip4_header, data_start, sizeof (ip4_header_t));
1784 f->next_data_offset = next_data_offset;
1786 map_main.ip6_reass_buffered_counter++;
1792 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1794 map_main_t *mm = &map_main;
1797 if (dropped_packets)
1798 *dropped_packets = mm->ip4_reass_buffered_counter;
1800 *trashed_reass = mm->ip4_reass_allocated;
1801 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1803 u16 ri = mm->ip4_reass_fifo_last;
1806 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1807 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1808 if (r->fragments[i] != ~0)
1809 map_ip4_drop_pi (r->fragments[i]);
1812 pool_put (mm->ip4_reass_pool, r);
1814 while (ri != mm->ip4_reass_fifo_last);
1817 vec_free (mm->ip4_reass_hash_table);
1818 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1819 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1820 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1821 pool_free (mm->ip4_reass_pool);
1822 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1824 mm->ip4_reass_allocated = 0;
1825 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1826 mm->ip4_reass_buffered_counter = 0;
1830 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1832 u32 desired_size = (u32) (pool_size * ht_ratio);
1834 for (i = 1; i < 31; i++)
1835 if ((1 << i) >= desired_size)
1841 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1842 u32 * dropped_packets)
1844 map_main_t *mm = &map_main;
1845 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1848 map_ip4_reass_lock ();
1849 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1850 mm->ip4_reass_ht_log2len =
1851 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1852 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1853 map_ip4_reass_unlock ();
1858 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1859 u32 * dropped_packets)
1861 map_main_t *mm = &map_main;
1862 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1865 map_ip4_reass_lock ();
1866 mm->ip4_reass_conf_pool_size = pool_size;
1867 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1868 map_ip4_reass_unlock ();
1873 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1875 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1880 map_ip4_reass_conf_buffers (u32 buffers)
1882 map_main.ip4_reass_conf_buffers = buffers;
1887 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1889 map_main_t *mm = &map_main;
1890 if (dropped_packets)
1891 *dropped_packets = mm->ip6_reass_buffered_counter;
1893 *trashed_reass = mm->ip6_reass_allocated;
1895 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1897 u16 ri = mm->ip6_reass_fifo_last;
1900 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1901 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1902 if (r->fragments[i].pi != ~0)
1903 map_ip6_drop_pi (r->fragments[i].pi);
1906 pool_put (mm->ip6_reass_pool, r);
1908 while (ri != mm->ip6_reass_fifo_last);
1909 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1912 vec_free (mm->ip6_reass_hash_table);
1913 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1914 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1915 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1916 pool_free (mm->ip6_reass_pool);
1917 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1919 mm->ip6_reass_allocated = 0;
1920 mm->ip6_reass_buffered_counter = 0;
1924 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1925 u32 * dropped_packets)
1927 map_main_t *mm = &map_main;
1928 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1931 map_ip6_reass_lock ();
1932 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1933 mm->ip6_reass_ht_log2len =
1934 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1935 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1936 map_ip6_reass_unlock ();
1941 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1942 u32 * dropped_packets)
1944 map_main_t *mm = &map_main;
1945 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1948 map_ip6_reass_lock ();
1949 mm->ip6_reass_conf_pool_size = pool_size;
1950 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1951 map_ip6_reass_unlock ();
1956 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1958 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1963 map_ip6_reass_conf_buffers (u32 buffers)
1965 map_main.ip6_reass_conf_buffers = buffers;
1969 static clib_error_t *
1970 map_tcp_mss_command_fn (vlib_main_t * vm,
1971 unformat_input_t * input, vlib_cli_command_t * cmd)
1973 unformat_input_t _line_input, *line_input = &_line_input;
1974 clib_error_t *error = NULL;
1977 /* Get a line of input. */
1978 if (!unformat_user (input, unformat_line_input, line_input))
1981 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1983 if (unformat (line_input, "%u", &tcp_mss))
1987 error = clib_error_return (0, "unknown input `%U'",
1988 format_unformat_error, line_input);
1993 if (tcp_mss >= (0x1 << 16))
1995 error = clib_error_return (0, "invalid value `%u'", tcp_mss);
1999 map_param_set_tcp (tcp_mss);
2002 unformat_free (line_input);
2011 * Configure MAP reassembly behaviour
2014 * @cliexstart{map params reassembly}
2017 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2018 .path = "map params reassembly",
2019 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2020 "[pool-size <pool-size>] [buffers <buffers>] "
2021 "[ht-ratio <ht-ratio>]",
2022 .function = map_params_reass_command_fn,
2026 * Set or copy the IP TOS/Traffic Class field
2029 * @cliexstart{map params traffic-class}
2031 * This command is used to set the traffic-class field in translated
2032 * or encapsulated packets. If copy is specifed (the default) then the
2033 * traffic-class/TOS field is copied from the original packet to the
2034 * translated / encapsulating header.
2037 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2038 .path = "map params traffic-class",
2039 .short_help = "map params traffic-class {0x0-0xff | copy}",
2040 .function = map_traffic_class_command_fn,
2047 * @cliexstart{map params tcp-mss}
2049 * This command is used to set the TCP MSS in translated
2050 * or encapsulated packets.
2053 VLIB_CLI_COMMAND(map_tcp_mss_command, static) = {
2054 .path = "map params tcp-mss",
2055 .short_help = "map params tcp-mss <value>",
2056 .function = map_tcp_mss_command_fn,
2060 * Bypass IP4/IP6 lookup
2063 * @cliexstart{map params pre-resolve}
2065 * Bypass a second FIB lookup of the translated or encapsulated
2066 * packet, and forward the packet directly to the specified
2067 * next-hop. This optimization trades forwarding flexibility for
2071 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2072 .path = "map params pre-resolve",
2073 .short_help = " map params pre-resolve {ip4-nh <address>} "
2074 "| {ip6-nh <address>}",
2075 .function = map_pre_resolve_command_fn,
2079 * Enable or disable the MAP-E inbound security check
2080 * Specifiy if the inbound security check should be done on fragments
2083 * @cliexstart{map params security-check}
2085 * By default, a decapsulated packet's IPv4 source address will be
2086 * verified against the outer header's IPv6 source address. Disabling
2087 * this feature will allow IPv4 source address spoofing.
2089 * Typically the inbound on-decapsulation security check is only done
2090 * on the first packet. The packet that contains the L4
2091 * information. While a security check on every fragment is possible,
2092 * it has a cost. State must be created on the first fragment.
2095 VLIB_CLI_COMMAND(map_security_check_command, static) = {
2096 .path = "map params security-check",
2097 .short_help = "map params security-check enable|disable fragments on|off",
2098 .function = map_security_check_command_fn,
2103 * Specifiy the IPv4 source address used for relayed ICMP error messages
2106 * @cliexstart{map params icmp source-address}
2108 * This command specifies which IPv4 source address (must be local to
2109 * the system), that is used for relayed received IPv6 ICMP error
2113 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2114 .path = "map params icmp source-address",
2115 .short_help = "map params icmp source-address <ip4-address>",
2116 .function = map_icmp_relay_source_address_command_fn,
2120 * Send IPv6 ICMP unreachables
2123 * @cliexstart{map params icmp6 unreachables}
2125 * Send IPv6 ICMP unreachable messages back if security check fails or
2126 * no MAP domain exists.
2129 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2130 .path = "map params icmp6 unreachables",
2131 .short_help = "map params icmp6 unreachables {on|off}",
2132 .function = map_icmp_unreachables_command_fn,
2136 * Configure MAP fragmentation behaviour
2139 * @cliexstart{map params fragment}
2141 * Allows fragmentation of the IPv4 packet even if the DF bit is
2142 * set. The choice between inner or outer fragmentation of tunnel
2143 * packets is complicated. The benefit of inner fragmentation is that
2144 * the ultimate endpoint must reassemble, instead of the tunnel
2148 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2149 .path = "map params fragment",
2150 .short_help = "map params fragment inner|outer ignore-df|honor-df",
2151 .function = map_fragment_command_fn,
2159 * @cliexstart{map add domain}
2162 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2163 .path = "map add domain",
2164 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2165 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2166 "[map-t] [mtu <mtu>]",
2167 .function = map_add_domain_command_fn,
2171 * Add MAP rule to a domain
2174 * @cliexstart{map add rule}
2177 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2178 .path = "map add rule",
2179 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2180 .function = map_add_rule_command_fn,
2187 * @cliexstart{map del domain}
2190 VLIB_CLI_COMMAND(map_del_command, static) = {
2191 .path = "map del domain",
2192 .short_help = "map del domain index <domain>",
2193 .function = map_del_domain_command_fn,
2200 * @cliexstart{show map domain}
2203 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2204 .path = "show map domain",
2205 .short_help = "show map domain index <n> [counters]",
2206 .function = show_map_domain_command_fn,
2210 * Show MAP statistics
2213 * @cliexstart{show map stats}
2216 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2217 .path = "show map stats",
2218 .short_help = "show map stats",
2219 .function = show_map_stats_command_fn,
2223 * Show MAP fragmentation information
2226 * @cliexstart{show map fragments}
2229 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2230 .path = "show map fragments",
2231 .short_help = "show map fragments",
2232 .function = show_map_fragments_command_fn,
2236 * Enable MAP processing on interface (input feature)
2239 VLIB_CLI_COMMAND(map_if_command, static) = {
2240 .path = "map interface",
2241 .short_help = "map interface <interface-name> [map-t] [del]",
2242 .function = map_if_command_fn,
2245 VLIB_PLUGIN_REGISTER() = {
2246 .version = VPP_BUILD_VER,
2247 .description = "Mapping of address and port (MAP)",
2256 map_init (vlib_main_t * vm)
2258 map_main_t *mm = &map_main;
2259 clib_error_t *error = 0;
2260 mm->vnet_main = vnet_get_main ();
2263 #ifdef MAP_SKIP_IP6_LOOKUP
2264 fib_protocol_t proto;
2266 FOR_EACH_FIB_PROTOCOL (proto)
2268 map_pre_resolve_init (&pre_resolved[proto]);
2276 /* Inbound security check */
2277 mm->sec_check = true;
2278 mm->sec_check_frag = false;
2280 /* ICMP6 Type 1, Code 5 for security check failure */
2281 mm->icmp6_enabled = false;
2283 /* Inner or outer fragmentation */
2284 mm->frag_inner = false;
2285 mm->frag_ignore_df = false;
2287 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2288 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
2289 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
2291 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2292 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2294 /* IP4 virtual reassembly */
2295 mm->ip4_reass_hash_table = 0;
2296 mm->ip4_reass_pool = 0;
2297 mm->ip4_reass_lock =
2298 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2299 *mm->ip4_reass_lock = 0;
2300 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2301 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2302 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2303 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2304 mm->ip4_reass_ht_log2len =
2305 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2306 mm->ip4_reass_conf_pool_size);
2307 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2308 map_ip4_reass_reinit (NULL, NULL);
2310 /* IP6 virtual reassembly */
2311 mm->ip6_reass_hash_table = 0;
2312 mm->ip6_reass_pool = 0;
2313 mm->ip6_reass_lock =
2314 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2315 *mm->ip6_reass_lock = 0;
2316 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2317 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2318 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2319 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2320 mm->ip6_reass_ht_log2len =
2321 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2322 mm->ip6_reass_conf_pool_size);
2323 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2324 map_ip6_reass_reinit (NULL, NULL);
2326 #ifdef MAP_SKIP_IP6_LOOKUP
2327 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2329 map_dpo_module_init ();
2331 error = map_plugin_api_hookup (vm);
2336 VLIB_INIT_FUNCTION (map_init);
2339 * fd.io coding-style-patch-verification: ON
2342 * eval: (c-set-style "gnu")