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 <vnet/map/map_dpo.h>
22 #include <vppinfra/crc32.h>
27 * This code supports the following MAP modes:
29 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
30 * ea_bits_len + ip4_prefix > 32
31 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
32 * Algorithmic Full 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 IPv4 prefix (ea_bits_len > 0):
36 * ea_bits_len + ip4_prefix < 32
37 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
39 * Independent Shared IPv4 address (ea_bits_len = 0):
42 * Rule IPv6 address = 128, Rule PSID Set
43 * Independent Full IPv4 address (ea_bits_len = 0):
45 * psid_length = 0, ip6_prefix = 128
46 * Independent IPv4 prefix (ea_bits_len = 0):
48 * psid_length = 0, ip6_prefix = 128
53 * This code supports MAP-T:
55 * With DMR prefix length equal to 96.
62 map_create_domain (ip4_address_t * ip4_prefix,
64 ip6_address_t * ip6_prefix,
66 ip6_address_t * ip6_src,
70 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
72 u8 suffix_len, suffix_shift;
73 map_main_t *mm = &map_main;
74 dpo_id_t dpo_v4 = DPO_INVALID;
75 dpo_id_t dpo_v6 = DPO_INVALID;
78 /* Sanity check on the src prefix length */
79 if (flags & MAP_DOMAIN_TRANSLATION)
81 if (ip6_src_len != 96)
83 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
89 if (ip6_src_len != 128)
92 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
98 /* How many, and which bits to grab from the IPv4 DA */
99 if (ip4_prefix_len + ea_bits_len < 32)
101 flags |= MAP_DOMAIN_PREFIX;
102 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
103 suffix_len = ea_bits_len;
108 suffix_len = 32 - ip4_prefix_len;
111 /* EA bits must be within the first 64 bits */
112 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
113 ip6_prefix_len + suffix_len + psid_length > 64))
116 ("Embedded Address bits must be within the first 64 bits of "
121 /* Get domain index */
122 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
123 memset (d, 0, sizeof (*d));
124 *map_domain_index = d - mm->domains;
126 /* Init domain struct */
127 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
128 d->ip4_prefix_len = ip4_prefix_len;
129 d->ip6_prefix = *ip6_prefix;
130 d->ip6_prefix_len = ip6_prefix_len;
131 d->ip6_src = *ip6_src;
132 d->ip6_src_len = ip6_src_len;
133 d->ea_bits_len = ea_bits_len;
134 d->psid_offset = psid_offset;
135 d->psid_length = psid_length;
138 d->suffix_shift = suffix_shift;
139 d->suffix_mask = (1 << suffix_len) - 1;
141 d->psid_shift = 16 - psid_length - psid_offset;
142 d->psid_mask = (1 << d->psid_length) - 1;
143 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
145 /* MAP data-plane object */
146 if (d->flags & MAP_DOMAIN_TRANSLATION)
147 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
149 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
151 /* Create ip4 route */
153 .fp_proto = FIB_PROTOCOL_IP4,
154 .fp_len = d->ip4_prefix_len,
156 .ip4 = d->ip4_prefix,
160 fib_table_entry_special_dpo_add (0, &pfx,
162 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
166 * construct a DPO to use the v6 domain
168 if (d->flags & MAP_DOMAIN_TRANSLATION)
169 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
171 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
174 * Multiple MAP domains may share same source IPv6 TEP. Which is just dandy.
175 * We are not tracking the sharing. So a v4 lookup to find the correct
176 * domain post decap/trnaslate is always done
178 * Create ip6 route. This is a reference counted add. If the prefix
179 * already exists and is MAP sourced, it is now MAP source n+1 times
180 * and will need to be removed n+1 times.
182 fib_prefix_t pfx6 = {
183 .fp_proto = FIB_PROTOCOL_IP6,
184 .fp_len = d->ip6_src_len,
185 .fp_addr.ip6 = d->ip6_src,
188 fib_table_entry_special_dpo_add (0, &pfx6,
190 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
193 /* Validate packet/byte counters */
194 map_domain_counter_lock (mm);
196 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
198 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
200 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
203 for (i = 0; i < vec_len (mm->domain_counters); i++)
205 vlib_validate_combined_counter (&mm->domain_counters[i],
207 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
209 map_domain_counter_unlock (mm);
218 map_delete_domain (u32 map_domain_index)
220 map_main_t *mm = &map_main;
223 if (pool_is_free_index (mm->domains, map_domain_index))
225 clib_warning ("MAP domain delete: domain does not exist: %d",
230 d = pool_elt_at_index (mm->domains, map_domain_index);
233 .fp_proto = FIB_PROTOCOL_IP4,
234 .fp_len = d->ip4_prefix_len,
236 .ip4 = d->ip4_prefix,
240 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
242 fib_prefix_t pfx6 = {
243 .fp_proto = FIB_PROTOCOL_IP6,
244 .fp_len = d->ip6_src_len,
250 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
254 clib_mem_free (d->rules);
256 pool_put (mm->domains, d);
262 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
266 map_main_t *mm = &map_main;
268 if (pool_is_free_index (mm->domains, map_domain_index))
270 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
273 d = pool_elt_at_index (mm->domains, map_domain_index);
275 /* Rules are only used in 1:1 independent case */
276 if (d->ea_bits_len > 0)
281 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
282 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
285 memset (d->rules, 0, l);
288 if (psid >= (0x1 << d->psid_length))
290 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
291 0x1 << d->psid_length);
297 d->rules[psid] = *tep;
301 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
306 #ifdef MAP_SKIP_IP6_LOOKUP
308 * Pre-resolvd per-protocol global next-hops
310 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
313 map_pre_resolve_init (map_main_pre_resolved_t * pr)
315 pr->fei = FIB_NODE_INDEX_INVALID;
316 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
320 format_map_pre_resolve (u8 * s, va_list ap)
322 map_main_pre_resolved_t *pr = va_arg (ap, map_main_pre_resolved_t *);
324 if (FIB_NODE_INDEX_INVALID != pr->fei)
328 fib_entry_get_prefix (pr->fei, &pfx);
330 return (format (s, "%U (%u)",
331 format_ip46_address, &pfx.fp_addr, IP46_TYPE_ANY,
332 pr->dpo.dpoi_index));
336 return (format (s, "un-set"));
342 * Function definition to inform the FIB node that its last lock has gone.
345 map_last_lock_gone (fib_node_t * node)
348 * The MAP is a root of the graph. As such
349 * it never has children and thus is never locked.
354 static map_main_pre_resolved_t *
355 map_from_fib_node (fib_node_t * node)
358 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
360 return ((map_main_pre_resolved_t *)
362 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
366 map_stack (map_main_pre_resolved_t * pr)
370 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
372 dpo_copy (&pr->dpo, dpo);
376 * Function definition to backwalk a FIB node
378 static fib_node_back_walk_rc_t
379 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
381 map_stack (map_from_fib_node (node));
383 return (FIB_NODE_BACK_WALK_CONTINUE);
387 * Function definition to get a FIB node from its index
390 map_fib_node_get (fib_node_index_t index)
392 return (&pre_resolved[index].node);
396 * Virtual function table registered by MPLS GRE tunnels
397 * for participation in the FIB object graph.
399 const static fib_node_vft_t map_vft = {
400 .fnv_get = map_fib_node_get,
401 .fnv_last_lock = map_last_lock_gone,
402 .fnv_back_walk = map_back_walk,
406 map_fib_resolve (map_main_pre_resolved_t * pr,
407 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
415 pr->fei = fib_table_entry_special_add (0, // default fib
417 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
418 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
423 map_fib_unresolve (map_main_pre_resolved_t * pr,
424 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
432 fib_entry_child_remove (pr->fei, pr->sibling);
434 fib_table_entry_special_remove (0, // default fib
435 &pfx, FIB_SOURCE_RR);
436 dpo_reset (&pr->dpo);
438 pr->fei = FIB_NODE_INDEX_INVALID;
439 pr->sibling = FIB_NODE_INDEX_INVALID;
443 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
445 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
447 ip46_address_t addr = {
451 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
452 FIB_PROTOCOL_IP6, 128, &addr);
454 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
455 FIB_PROTOCOL_IP6, 128, &addr);
457 if (ip4 && (ip4->as_u32 != 0))
459 ip46_address_t addr = {
463 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
464 FIB_PROTOCOL_IP4, 32, &addr);
466 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
467 FIB_PROTOCOL_IP4, 32, &addr);
472 static clib_error_t *
473 map_security_check_command_fn (vlib_main_t * vm,
474 unformat_input_t * input,
475 vlib_cli_command_t * cmd)
477 unformat_input_t _line_input, *line_input = &_line_input;
478 map_main_t *mm = &map_main;
479 clib_error_t *error = NULL;
481 /* Get a line of input. */
482 if (!unformat_user (input, unformat_line_input, line_input))
485 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
487 if (unformat (line_input, "off"))
488 mm->sec_check = false;
489 else if (unformat (line_input, "on"))
490 mm->sec_check = true;
493 error = clib_error_return (0, "unknown input `%U'",
494 format_unformat_error, line_input);
500 unformat_free (line_input);
505 static clib_error_t *
506 map_security_check_frag_command_fn (vlib_main_t * vm,
507 unformat_input_t * input,
508 vlib_cli_command_t * cmd)
510 unformat_input_t _line_input, *line_input = &_line_input;
511 map_main_t *mm = &map_main;
512 clib_error_t *error = NULL;
514 /* Get a line of input. */
515 if (!unformat_user (input, unformat_line_input, line_input))
518 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
520 if (unformat (line_input, "off"))
521 mm->sec_check_frag = false;
522 else if (unformat (line_input, "on"))
523 mm->sec_check_frag = true;
526 error = clib_error_return (0, "unknown input `%U'",
527 format_unformat_error, line_input);
533 unformat_free (line_input);
538 static clib_error_t *
539 map_add_domain_command_fn (vlib_main_t * vm,
540 unformat_input_t * input, vlib_cli_command_t * cmd)
542 unformat_input_t _line_input, *line_input = &_line_input;
543 ip4_address_t ip4_prefix;
544 ip6_address_t ip6_prefix;
545 ip6_address_t ip6_src;
546 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
548 /* Optional arguments */
549 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
553 clib_error_t *error = NULL;
555 /* Get a line of input. */
556 if (!unformat_user (input, unformat_line_input, line_input))
559 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
562 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
567 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
572 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
577 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
579 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
581 else if (unformat (line_input, "psid-offset %d", &psid_offset))
583 else if (unformat (line_input, "psid-len %d", &psid_length))
585 else if (unformat (line_input, "mtu %d", &mtu))
587 else if (unformat (line_input, "map-t"))
588 flags |= MAP_DOMAIN_TRANSLATION;
591 error = clib_error_return (0, "unknown input `%U'",
592 format_unformat_error, line_input);
599 error = clib_error_return (0, "mandatory argument(s) missing");
603 map_create_domain (&ip4_prefix, ip4_prefix_len,
604 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
605 ea_bits_len, psid_offset, psid_length, &map_domain_index,
609 unformat_free (line_input);
614 static clib_error_t *
615 map_del_domain_command_fn (vlib_main_t * vm,
616 unformat_input_t * input, vlib_cli_command_t * cmd)
618 unformat_input_t _line_input, *line_input = &_line_input;
620 u32 map_domain_index;
621 clib_error_t *error = NULL;
623 /* Get a line of input. */
624 if (!unformat_user (input, unformat_line_input, line_input))
627 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
629 if (unformat (line_input, "index %d", &map_domain_index))
633 error = clib_error_return (0, "unknown input `%U'",
634 format_unformat_error, line_input);
641 error = clib_error_return (0, "mandatory argument(s) missing");
645 map_delete_domain (map_domain_index);
648 unformat_free (line_input);
653 static clib_error_t *
654 map_add_rule_command_fn (vlib_main_t * vm,
655 unformat_input_t * input, vlib_cli_command_t * cmd)
657 unformat_input_t _line_input, *line_input = &_line_input;
660 u32 psid = 0, map_domain_index;
661 clib_error_t *error = NULL;
663 /* Get a line of input. */
664 if (!unformat_user (input, unformat_line_input, line_input))
667 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
669 if (unformat (line_input, "index %d", &map_domain_index))
671 else if (unformat (line_input, "psid %d", &psid))
674 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
678 error = clib_error_return (0, "unknown input `%U'",
679 format_unformat_error, line_input);
686 error = clib_error_return (0, "mandatory argument(s) missing");
690 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
692 error = clib_error_return (0, "Failing to add Mapping Rule");
697 unformat_free (line_input);
702 #if MAP_SKIP_IP6_LOOKUP
703 static clib_error_t *
704 map_pre_resolve_command_fn (vlib_main_t * vm,
705 unformat_input_t * input,
706 vlib_cli_command_t * cmd)
708 unformat_input_t _line_input, *line_input = &_line_input;
709 ip4_address_t ip4nh, *p_v4 = NULL;
710 ip6_address_t ip6nh, *p_v6 = NULL;
711 clib_error_t *error = NULL;
714 memset (&ip4nh, 0, sizeof (ip4nh));
715 memset (&ip6nh, 0, sizeof (ip6nh));
717 /* Get a line of input. */
718 if (!unformat_user (input, unformat_line_input, line_input))
721 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
723 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
726 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
728 else if (unformat (line_input, "del"))
732 error = clib_error_return (0, "unknown input `%U'",
733 format_unformat_error, line_input);
738 map_pre_resolve (p_v4, p_v6, is_del);
741 unformat_free (line_input);
747 static clib_error_t *
748 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
749 unformat_input_t * input,
750 vlib_cli_command_t * cmd)
752 unformat_input_t _line_input, *line_input = &_line_input;
753 ip4_address_t icmp_src_address;
754 map_main_t *mm = &map_main;
755 clib_error_t *error = NULL;
757 mm->icmp4_src_address.as_u32 = 0;
759 /* Get a line of input. */
760 if (!unformat_user (input, unformat_line_input, line_input))
763 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
766 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
767 mm->icmp4_src_address = icmp_src_address;
770 error = clib_error_return (0, "unknown input `%U'",
771 format_unformat_error, line_input);
777 unformat_free (line_input);
782 static clib_error_t *
783 map_icmp_unreachables_command_fn (vlib_main_t * vm,
784 unformat_input_t * input,
785 vlib_cli_command_t * cmd)
787 unformat_input_t _line_input, *line_input = &_line_input;
788 map_main_t *mm = &map_main;
790 clib_error_t *error = NULL;
792 /* Get a line of input. */
793 if (!unformat_user (input, unformat_line_input, line_input))
796 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
799 if (unformat (line_input, "on"))
800 mm->icmp6_enabled = true;
801 else if (unformat (line_input, "off"))
802 mm->icmp6_enabled = false;
805 error = clib_error_return (0, "unknown input `%U'",
806 format_unformat_error, line_input);
813 error = clib_error_return (0, "mandatory argument(s) missing");
816 unformat_free (line_input);
821 static clib_error_t *
822 map_fragment_command_fn (vlib_main_t * vm,
823 unformat_input_t * input, vlib_cli_command_t * cmd)
825 unformat_input_t _line_input, *line_input = &_line_input;
826 map_main_t *mm = &map_main;
827 clib_error_t *error = NULL;
829 /* Get a line of input. */
830 if (!unformat_user (input, unformat_line_input, line_input))
833 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
835 if (unformat (line_input, "inner"))
836 mm->frag_inner = true;
837 else if (unformat (line_input, "outer"))
838 mm->frag_inner = false;
841 error = clib_error_return (0, "unknown input `%U'",
842 format_unformat_error, line_input);
848 unformat_free (line_input);
853 static clib_error_t *
854 map_fragment_df_command_fn (vlib_main_t * vm,
855 unformat_input_t * input,
856 vlib_cli_command_t * cmd)
858 unformat_input_t _line_input, *line_input = &_line_input;
859 map_main_t *mm = &map_main;
860 clib_error_t *error = NULL;
862 /* Get a line of input. */
863 if (!unformat_user (input, unformat_line_input, line_input))
866 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
868 if (unformat (line_input, "on"))
869 mm->frag_ignore_df = true;
870 else if (unformat (line_input, "off"))
871 mm->frag_ignore_df = false;
874 error = clib_error_return (0, "unknown input `%U'",
875 format_unformat_error, line_input);
881 unformat_free (line_input);
886 static clib_error_t *
887 map_traffic_class_command_fn (vlib_main_t * vm,
888 unformat_input_t * input,
889 vlib_cli_command_t * cmd)
891 unformat_input_t _line_input, *line_input = &_line_input;
892 map_main_t *mm = &map_main;
894 clib_error_t *error = NULL;
898 /* Get a line of input. */
899 if (!unformat_user (input, unformat_line_input, line_input))
902 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
904 if (unformat (line_input, "copy"))
906 else if (unformat (line_input, "%x", &tc))
910 error = clib_error_return (0, "unknown input `%U'",
911 format_unformat_error, line_input);
917 unformat_free (line_input);
923 format_map_domain (u8 * s, va_list * args)
925 map_domain_t *d = va_arg (*args, map_domain_t *);
926 bool counters = va_arg (*args, int);
927 map_main_t *mm = &map_main;
928 ip6_address_t ip6_prefix;
931 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
933 ip6_prefix = d->ip6_prefix;
936 "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea_bits_len %d psid-offset %d psid-len %d mtu %d %s",
938 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
939 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
940 format_ip6_address, &d->ip6_src, d->ip6_src_len,
941 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
942 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
946 map_domain_counter_lock (mm);
948 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
949 d - mm->domains, &v);
950 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
951 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
952 d - mm->domains, &v);
953 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
954 map_domain_counter_unlock (mm);
956 s = format (s, "\n");
962 for (i = 0; i < (0x1 << d->psid_length); i++)
965 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
968 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
976 format_map_ip4_reass (u8 * s, va_list * args)
978 map_main_t *mm = &map_main;
979 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
980 map_ip4_reass_key_t *k = &r->key;
981 f64 now = vlib_time_now (mm->vlib_main);
982 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
983 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
985 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
986 format_ip4_address, &k->src.as_u8, format_ip4_address,
987 &k->dst.as_u8, k->protocol,
988 clib_net_to_host_u16 (k->fragment_id),
989 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
994 format_map_ip6_reass (u8 * s, va_list * args)
996 map_main_t *mm = &map_main;
997 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
998 map_ip6_reass_key_t *k = &r->key;
999 f64 now = vlib_time_now (mm->vlib_main);
1000 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
1001 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1003 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1004 format_ip6_address, &k->src.as_u8, format_ip6_address,
1005 &k->dst.as_u8, k->protocol,
1006 clib_net_to_host_u32 (k->fragment_id), dt);
1010 static clib_error_t *
1011 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1012 vlib_cli_command_t * cmd)
1014 unformat_input_t _line_input, *line_input = &_line_input;
1015 map_main_t *mm = &map_main;
1017 bool counters = false;
1018 u32 map_domain_index = ~0;
1019 clib_error_t *error = NULL;
1021 /* Get a line of input. */
1022 if (!unformat_user (input, unformat_line_input, line_input))
1025 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1027 if (unformat (line_input, "counters"))
1029 else if (unformat (line_input, "index %d", &map_domain_index))
1033 error = clib_error_return (0, "unknown input `%U'",
1034 format_unformat_error, line_input);
1039 if (pool_elts (mm->domains) == 0)
1040 vlib_cli_output (vm, "No MAP domains are configured...");
1042 if (map_domain_index == ~0)
1045 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1050 if (pool_is_free_index (mm->domains, map_domain_index))
1052 error = clib_error_return (0, "MAP domain does not exists %d",
1057 d = pool_elt_at_index (mm->domains, map_domain_index);
1058 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1062 unformat_free (line_input);
1067 static clib_error_t *
1068 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1069 vlib_cli_command_t * cmd)
1071 map_main_t *mm = &map_main;
1072 map_ip4_reass_t *f4;
1073 map_ip6_reass_t *f6;
1076 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
1079 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
1085 map_error_counter_get (u32 node_index, map_error_t map_error)
1087 vlib_main_t *vm = vlib_get_main ();
1088 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1089 vlib_error_main_t *em = &vm->error_main;
1090 vlib_error_t e = error_node->errors[map_error];
1091 vlib_node_t *n = vlib_get_node (vm, node_index);
1094 ci = vlib_error_get_code (e);
1095 ASSERT (ci < n->n_errors);
1096 ci += n->error_heap_index;
1098 return (em->counters[ci]);
1101 static clib_error_t *
1102 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1103 vlib_cli_command_t * cmd)
1105 map_main_t *mm = &map_main;
1107 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1108 if (pool_elts (mm->domains) == 0)
1109 vlib_cli_output (vm, "No MAP domains are configured...");
1112 pool_foreach(d, mm->domains, ({
1114 rulecount+= 0x1 << d->psid_length;
1115 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1117 domains += sizeof(*d);
1122 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1123 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1124 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1125 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1127 #if MAP_SKIP_IP6_LOOKUP
1128 vlib_cli_output (vm,
1129 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1130 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1131 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1136 vlib_cli_output (vm, "MAP traffic-class: copy");
1138 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1140 vlib_cli_output (vm,
1141 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1142 mm->sec_check ? "enabled" : "disabled",
1143 mm->sec_check_frag ? "enabled" : "disabled");
1145 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1146 format_ip4_address, &mm->icmp4_src_address);
1147 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1148 mm->icmp6_enabled ? "enabled" : "disabled");
1149 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1150 mm->frag_inner ? "enabled" : "disabled");
1151 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1152 mm->frag_ignore_df ? "enabled" : "disabled");
1157 vlib_combined_counter_main_t *cm = mm->domain_counters;
1158 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1159 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1163 memset (total_pkts, 0, sizeof (total_pkts));
1164 memset (total_bytes, 0, sizeof (total_bytes));
1166 map_domain_counter_lock (mm);
1167 vec_foreach (cm, mm->domain_counters)
1169 which = cm - mm->domain_counters;
1171 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1173 vlib_get_combined_counter (cm, i, &v);
1174 total_pkts[which] += v.packets;
1175 total_bytes[which] += v.bytes;
1178 map_domain_counter_unlock (mm);
1180 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1181 total_pkts[MAP_DOMAIN_COUNTER_TX],
1182 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1183 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1184 total_pkts[MAP_DOMAIN_COUNTER_RX],
1185 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1187 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1188 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1193 static clib_error_t *
1194 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1195 vlib_cli_command_t * cmd)
1197 unformat_input_t _line_input, *line_input = &_line_input;
1199 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1201 u64 buffers = ~(0ull);
1202 u8 ip4 = 0, ip6 = 0;
1204 if (!unformat_user (input, unformat_line_input, line_input))
1207 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1209 if (unformat (line_input, "lifetime %u", &lifetime))
1211 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1213 else if (unformat (line_input, "pool-size %u", &pool_size))
1215 else if (unformat (line_input, "buffers %llu", &buffers))
1217 else if (unformat (line_input, "ip4"))
1219 else if (unformat (line_input, "ip6"))
1223 unformat_free (line_input);
1224 return clib_error_return (0, "invalid input");
1227 unformat_free (line_input);
1230 return clib_error_return (0, "must specify ip4 and/or ip6");
1234 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1235 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1236 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1237 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1238 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1239 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1240 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1241 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1242 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1243 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1244 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1245 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1246 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1251 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1252 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1253 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1254 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1255 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1256 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1257 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1258 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1259 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1260 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1261 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1262 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1263 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1268 u32 reass = 0, packets = 0;
1269 if (pool_size != ~0)
1271 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1273 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1277 vlib_cli_output (vm,
1278 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1282 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1284 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1286 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1290 vlib_cli_output (vm,
1291 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1297 if (map_ip4_reass_conf_lifetime (lifetime))
1298 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1300 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1302 if (buffers != ~(0ull))
1304 if (map_ip4_reass_conf_buffers (buffers))
1305 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1307 vlib_cli_output (vm, "Setting ip4-reass buffers");
1310 if (map_main.ip4_reass_conf_buffers >
1311 map_main.ip4_reass_conf_pool_size *
1312 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1314 vlib_cli_output (vm,
1315 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1321 u32 reass = 0, packets = 0;
1322 if (pool_size != ~0)
1324 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1326 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1330 vlib_cli_output (vm,
1331 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1335 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1337 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1339 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1343 vlib_cli_output (vm,
1344 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1350 if (map_ip6_reass_conf_lifetime (lifetime))
1351 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1353 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1355 if (buffers != ~(0ull))
1357 if (map_ip6_reass_conf_buffers (buffers))
1358 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1360 vlib_cli_output (vm, "Setting ip6-reass buffers");
1363 if (map_main.ip6_reass_conf_buffers >
1364 map_main.ip6_reass_conf_pool_size *
1365 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1367 vlib_cli_output (vm,
1368 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1377 * packet trace format function
1380 format_map_trace (u8 * s, va_list * args)
1382 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1383 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1384 map_trace_t *t = va_arg (*args, map_trace_t *);
1385 u32 map_domain_index = t->map_domain_index;
1389 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1390 clib_net_to_host_u16 (port));
1395 static_always_inline map_ip4_reass_t *
1396 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1398 map_main_t *mm = &map_main;
1399 u32 ri = mm->ip4_reass_hash_table[bucket];
1400 while (ri != MAP_REASS_INDEX_NONE)
1402 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1403 if (r->key.as_u64[0] == k->as_u64[0] &&
1404 r->key.as_u64[1] == k->as_u64[1] &&
1405 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1409 ri = r->bucket_next;
1414 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1417 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1419 map_main_t *mm = &map_main;
1420 map_ip4_reass_get_fragments (r, pi_to_drop);
1422 // Unlink in hash bucket
1423 map_ip4_reass_t *r2 = NULL;
1424 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1425 while (r2i != map_ip4_reass_pool_index (r))
1427 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1428 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1429 r2i = r2->bucket_next;
1433 r2->bucket_next = r->bucket_next;
1437 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1441 if (r->fifo_next == map_ip4_reass_pool_index (r))
1443 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1447 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1448 mm->ip4_reass_fifo_last = r->fifo_prev;
1449 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1451 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1455 pool_put (mm->ip4_reass_pool, r);
1456 mm->ip4_reass_allocated--;
1460 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1461 u8 protocol, u32 ** pi_to_drop)
1464 map_main_t *mm = &map_main;
1465 map_ip4_reass_key_t k = {.src.data_u32 = src,
1466 .dst.data_u32 = dst,
1467 .fragment_id = fragment_id,
1468 .protocol = protocol
1472 #ifdef clib_crc32c_uses_intrinsics
1473 h = clib_crc32c ((u8 *) k.as_u32, 16);
1475 u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1476 h = clib_xxhash (tmp);
1478 h = h >> (32 - mm->ip4_reass_ht_log2len);
1480 f64 now = vlib_time_now (mm->vlib_main);
1482 //Cache garbage collection
1483 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1485 map_ip4_reass_t *last =
1486 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1487 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1488 map_ip4_reass_free (last, pi_to_drop);
1493 if ((r = map_ip4_reass_lookup (&k, h, now)))
1496 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1499 pool_get (mm->ip4_reass_pool, r);
1500 mm->ip4_reass_allocated++;
1502 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1503 r->fragments[i] = ~0;
1505 u32 ri = map_ip4_reass_pool_index (r);
1507 //Link in new bucket
1509 r->bucket_next = mm->ip4_reass_hash_table[h];
1510 mm->ip4_reass_hash_table[h] = ri;
1513 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1516 pool_elt_at_index (mm->ip4_reass_pool,
1517 mm->ip4_reass_fifo_last)->fifo_next;
1518 r->fifo_prev = mm->ip4_reass_fifo_last;
1519 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1520 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1524 r->fifo_next = r->fifo_prev = ri;
1525 mm->ip4_reass_fifo_last = ri;
1532 #ifdef MAP_IP4_REASS_COUNT_BYTES
1533 r->expected_total = 0xffff;
1541 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1543 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1547 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1548 if (r->fragments[i] == ~0)
1550 r->fragments[i] = pi;
1551 map_main.ip4_reass_buffered_counter++;
1557 static_always_inline map_ip6_reass_t *
1558 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1560 map_main_t *mm = &map_main;
1561 u32 ri = mm->ip6_reass_hash_table[bucket];
1562 while (ri != MAP_REASS_INDEX_NONE)
1564 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1565 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1566 r->key.as_u64[0] == k->as_u64[0] &&
1567 r->key.as_u64[1] == k->as_u64[1] &&
1568 r->key.as_u64[2] == k->as_u64[2] &&
1569 r->key.as_u64[3] == k->as_u64[3] &&
1570 r->key.as_u64[4] == k->as_u64[4])
1572 ri = r->bucket_next;
1577 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1580 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1582 map_main_t *mm = &map_main;
1584 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1585 if (r->fragments[i].pi != ~0)
1587 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1588 r->fragments[i].pi = ~0;
1589 map_main.ip6_reass_buffered_counter--;
1592 // Unlink in hash bucket
1593 map_ip6_reass_t *r2 = NULL;
1594 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1595 while (r2i != map_ip6_reass_pool_index (r))
1597 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1598 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1599 r2i = r2->bucket_next;
1603 r2->bucket_next = r->bucket_next;
1607 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1611 if (r->fifo_next == map_ip6_reass_pool_index (r))
1613 //Single element in the list, list is now empty
1614 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1618 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1619 mm->ip6_reass_fifo_last = r->fifo_prev;
1620 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1622 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1626 // Free from pool if necessary
1627 pool_put (mm->ip6_reass_pool, r);
1628 mm->ip6_reass_allocated--;
1632 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1633 u8 protocol, u32 ** pi_to_drop)
1636 map_main_t *mm = &map_main;
1637 map_ip6_reass_key_t k = {
1640 .fragment_id = fragment_id,
1641 .protocol = protocol
1647 #ifdef clib_crc32c_uses_intrinsics
1648 h = clib_crc32c ((u8 *) k.as_u32, 40);
1651 k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1652 h = clib_xxhash (tmp);
1655 h = h >> (32 - mm->ip6_reass_ht_log2len);
1657 f64 now = vlib_time_now (mm->vlib_main);
1659 //Cache garbage collection
1660 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1662 map_ip6_reass_t *last =
1663 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1664 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1665 map_ip6_reass_free (last, pi_to_drop);
1670 if ((r = map_ip6_reass_lookup (&k, h, now)))
1673 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1676 pool_get (mm->ip6_reass_pool, r);
1677 mm->ip6_reass_allocated++;
1678 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1680 r->fragments[i].pi = ~0;
1681 r->fragments[i].next_data_len = 0;
1682 r->fragments[i].next_data_offset = 0;
1685 u32 ri = map_ip6_reass_pool_index (r);
1687 //Link in new bucket
1689 r->bucket_next = mm->ip6_reass_hash_table[h];
1690 mm->ip6_reass_hash_table[h] = ri;
1693 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1696 pool_elt_at_index (mm->ip6_reass_pool,
1697 mm->ip6_reass_fifo_last)->fifo_next;
1698 r->fifo_prev = mm->ip6_reass_fifo_last;
1699 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1700 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1704 r->fifo_next = r->fifo_prev = ri;
1705 mm->ip6_reass_fifo_last = ri;
1711 r->ip4_header.ip_version_and_header_length = 0;
1712 #ifdef MAP_IP6_REASS_COUNT_BYTES
1713 r->expected_total = 0xffff;
1720 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1721 u16 data_offset, u16 next_data_offset,
1722 u8 * data_start, u16 data_len)
1724 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1725 u16 copied_len = (data_len > 20) ? 20 : data_len;
1727 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1730 //Lookup for fragments for the current buffer
1731 //and the one before that
1733 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1735 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1737 prev_f = &r->fragments[i]; // This is buffer for previous packet
1739 else if (r->fragments[i].next_data_offset == next_data_offset)
1741 f = &r->fragments[i]; // This is a buffer for the current packet
1743 else if (r->fragments[i].next_data_offset == 0)
1746 f = &r->fragments[i];
1747 else if (prev_f == NULL)
1748 prev_f = &r->fragments[i];
1752 if (!f || f->pi != ~0)
1760 clib_memcpy (prev_f->next_data, data_start, copied_len);
1761 prev_f->next_data_len = copied_len;
1762 prev_f->next_data_offset = data_offset;
1766 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1769 if (r->ip4_header.ip_version_and_header_length == 0)
1770 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1775 f->next_data_offset = next_data_offset;
1777 map_main.ip6_reass_buffered_counter++;
1783 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1785 map_main_t *mm = &map_main;
1788 if (dropped_packets)
1789 *dropped_packets = mm->ip4_reass_buffered_counter;
1791 *trashed_reass = mm->ip4_reass_allocated;
1792 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1794 u16 ri = mm->ip4_reass_fifo_last;
1797 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1798 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1799 if (r->fragments[i] != ~0)
1800 map_ip4_drop_pi (r->fragments[i]);
1803 pool_put (mm->ip4_reass_pool, r);
1805 while (ri != mm->ip4_reass_fifo_last);
1808 vec_free (mm->ip4_reass_hash_table);
1809 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1810 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1811 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1812 pool_free (mm->ip4_reass_pool);
1813 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1815 mm->ip4_reass_allocated = 0;
1816 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1817 mm->ip4_reass_buffered_counter = 0;
1821 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1823 u32 desired_size = (u32) (pool_size * ht_ratio);
1825 for (i = 1; i < 31; i++)
1826 if ((1 << i) >= desired_size)
1832 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1833 u32 * dropped_packets)
1835 map_main_t *mm = &map_main;
1836 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1839 map_ip4_reass_lock ();
1840 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1841 mm->ip4_reass_ht_log2len =
1842 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1843 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1844 map_ip4_reass_unlock ();
1849 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1850 u32 * dropped_packets)
1852 map_main_t *mm = &map_main;
1853 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1856 map_ip4_reass_lock ();
1857 mm->ip4_reass_conf_pool_size = pool_size;
1858 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1859 map_ip4_reass_unlock ();
1864 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1866 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1871 map_ip4_reass_conf_buffers (u32 buffers)
1873 map_main.ip4_reass_conf_buffers = buffers;
1878 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1880 map_main_t *mm = &map_main;
1881 if (dropped_packets)
1882 *dropped_packets = mm->ip6_reass_buffered_counter;
1884 *trashed_reass = mm->ip6_reass_allocated;
1886 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1888 u16 ri = mm->ip6_reass_fifo_last;
1891 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1892 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1893 if (r->fragments[i].pi != ~0)
1894 map_ip6_drop_pi (r->fragments[i].pi);
1897 pool_put (mm->ip6_reass_pool, r);
1899 while (ri != mm->ip6_reass_fifo_last);
1900 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1903 vec_free (mm->ip6_reass_hash_table);
1904 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1905 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1906 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1907 pool_free (mm->ip6_reass_pool);
1908 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1910 mm->ip6_reass_allocated = 0;
1911 mm->ip6_reass_buffered_counter = 0;
1915 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1916 u32 * dropped_packets)
1918 map_main_t *mm = &map_main;
1919 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1922 map_ip6_reass_lock ();
1923 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1924 mm->ip6_reass_ht_log2len =
1925 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1926 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1927 map_ip6_reass_unlock ();
1932 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1933 u32 * dropped_packets)
1935 map_main_t *mm = &map_main;
1936 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1939 map_ip6_reass_lock ();
1940 mm->ip6_reass_conf_pool_size = pool_size;
1941 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1942 map_ip6_reass_unlock ();
1947 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1949 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1954 map_ip6_reass_conf_buffers (u32 buffers)
1956 map_main.ip6_reass_conf_buffers = buffers;
1963 * Configure MAP reassembly behaviour
1966 * @cliexstart{map params reassembly}
1969 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1970 .path = "map params reassembly",
1971 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
1972 "[pool-size <pool-size>] [buffers <buffers>] "
1973 "[ht-ratio <ht-ratio>]",
1974 .function = map_params_reass_command_fn,
1978 * Set or copy the IP TOS/Traffic Class field
1981 * @cliexstart{map params traffic-class}
1983 * This command is used to set the traffic-class field in translated
1984 * or encapsulated packets. If copy is specifed (the default) then the
1985 * traffic-class/TOS field is copied from the original packet to the
1986 * translated / encapsulating header.
1989 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1990 .path = "map params traffic-class",
1991 .short_help = "map params traffic-class {0x0-0xff | copy}",
1992 .function = map_traffic_class_command_fn,
1996 * Bypass IP4/IP6 lookup
1999 * @cliexstart{map params pre-resolve}
2001 * Bypass a second FIB lookup of the translated or encapsulated
2002 * packet, and forward the packet directly to the specified
2003 * next-hop. This optimization trades forwarding flexibility for
2007 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2008 .path = "map params pre-resolve",
2009 .short_help = " map params pre-resolve {ip4-nh <address>} "
2010 "| {ip6-nh <address>}",
2011 .function = map_pre_resolve_command_fn,
2015 * Enable or disable the MAP-E inbound security check
2018 * @cliexstart{map params security-check}
2020 * By default, a decapsulated packet's IPv4 source address will be
2021 * verified against the outer header's IPv6 source address. Disabling
2022 * this feature will allow IPv4 source address spoofing.
2025 VLIB_CLI_COMMAND(map_security_check_command, static) = {
2026 .path = "map params security-check",
2027 .short_help = "map params security-check on|off",
2028 .function = map_security_check_command_fn,
2032 * Specifiy the IPv4 source address used for relayed ICMP error messages
2035 * @cliexstart{map params icmp source-address}
2037 * This command specifies which IPv4 source address (must be local to
2038 * the system), that is used for relayed received IPv6 ICMP error
2042 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2043 .path = "map params icmp source-address",
2044 .short_help = "map params icmp source-address <ip4-address>",
2045 .function = map_icmp_relay_source_address_command_fn,
2049 * Send IPv6 ICMP unreachables
2052 * @cliexstart{map params icmp6 unreachables}
2054 * Send IPv6 ICMP unreachable messages back if security check fails or
2055 * no MAP domain exists.
2058 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2059 .path = "map params icmp6 unreachables",
2060 .short_help = "map params icmp6 unreachables {on|off}",
2061 .function = map_icmp_unreachables_command_fn,
2065 * Configure MAP fragmentation behaviour
2068 * @cliexstart{map params fragment}
2071 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2072 .path = "map params fragment",
2073 .short_help = "map params fragment inner|outer",
2074 .function = map_fragment_command_fn,
2078 * Ignore the IPv4 Don't fragment bit
2081 * @cliexstart{map params fragment ignore-df}
2083 * Allows fragmentation of the IPv4 packet even if the DF bit is
2084 * set. The choice between inner or outer fragmentation of tunnel
2085 * packets is complicated. The benefit of inner fragmentation is that
2086 * the ultimate endpoint must reassemble, instead of the tunnel
2090 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
2091 .path = "map params fragment ignore-df",
2092 .short_help = "map params fragment ignore-df on|off",
2093 .function = map_fragment_df_command_fn,
2097 * Specifiy if the inbound security check should be done on fragments
2100 * @cliexstart{map params security-check fragments}
2102 * Typically the inbound on-decapsulation security check is only done
2103 * on the first packet. The packet that contains the L4
2104 * information. While a security check on every fragment is possible,
2105 * it has a cost. State must be created on the first fragment.
2108 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
2109 .path = "map params security-check fragments",
2110 .short_help = "map params security-check fragments on|off",
2111 .function = map_security_check_frag_command_fn,
2118 * @cliexstart{map add domain}
2121 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2122 .path = "map add domain",
2123 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2124 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2125 "[map-t] [mtu <mtu>]",
2126 .function = map_add_domain_command_fn,
2130 * Add MAP rule to a domain
2133 * @cliexstart{map add rule}
2136 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2137 .path = "map add rule",
2138 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2139 .function = map_add_rule_command_fn,
2146 * @cliexstart{map del domain}
2149 VLIB_CLI_COMMAND(map_del_command, static) = {
2150 .path = "map del domain",
2151 .short_help = "map del domain index <domain>",
2152 .function = map_del_domain_command_fn,
2159 * @cliexstart{show map domain}
2162 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2163 .path = "show map domain",
2164 .short_help = "show map domain index <n> [counters]",
2165 .function = show_map_domain_command_fn,
2169 * Show MAP statistics
2172 * @cliexstart{show map stats}
2175 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2176 .path = "show map stats",
2177 .short_help = "show map stats",
2178 .function = show_map_stats_command_fn,
2182 * Show MAP fragmentation information
2185 * @cliexstart{show map fragments}
2188 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2189 .path = "show map fragments",
2190 .short_help = "show map fragments",
2191 .function = show_map_fragments_command_fn,
2199 map_init (vlib_main_t * vm)
2201 map_main_t *mm = &map_main;
2202 mm->vnet_main = vnet_get_main ();
2205 #ifdef MAP_SKIP_IP6_LOOKUP
2206 fib_protocol_t proto;
2208 FOR_EACH_FIB_PROTOCOL (proto)
2210 map_pre_resolve_init (&pre_resolved[proto]);
2218 /* Inbound security check */
2219 mm->sec_check = true;
2220 mm->sec_check_frag = false;
2222 /* ICMP6 Type 1, Code 5 for security check failure */
2223 mm->icmp6_enabled = false;
2225 /* Inner or outer fragmentation */
2226 mm->frag_inner = false;
2227 mm->frag_ignore_df = false;
2229 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2230 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2231 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2233 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2234 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2236 /* IP4 virtual reassembly */
2237 mm->ip4_reass_hash_table = 0;
2238 mm->ip4_reass_pool = 0;
2239 mm->ip4_reass_lock =
2240 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2241 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2242 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2243 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2244 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2245 mm->ip4_reass_ht_log2len =
2246 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2247 mm->ip4_reass_conf_pool_size);
2248 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2249 map_ip4_reass_reinit (NULL, NULL);
2251 /* IP6 virtual reassembly */
2252 mm->ip6_reass_hash_table = 0;
2253 mm->ip6_reass_pool = 0;
2254 mm->ip6_reass_lock =
2255 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2256 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2257 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2258 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2259 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2260 mm->ip6_reass_ht_log2len =
2261 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2262 mm->ip6_reass_conf_pool_size);
2263 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2264 map_ip6_reass_reinit (NULL, NULL);
2266 #ifdef MAP_SKIP_IP6_LOOKUP
2267 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2269 map_dpo_module_init ();
2274 VLIB_INIT_FUNCTION (map_init);
2277 * fd.io coding-style-patch-verification: ON
2280 * eval: (c-set-style "gnu")