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 <vppinfra/crc32.h>
22 #include <vnet/plugin/plugin.h>
23 #include <vpp/app/version.h>
29 * This code supports the following MAP modes:
31 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
32 * ea_bits_len + ip4_prefix > 32
33 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
34 * Algorithmic Full IPv4 address (ea_bits_len > 0):
35 * ea_bits_len + ip4_prefix = 32
36 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
37 * Algorithmic IPv4 prefix (ea_bits_len > 0):
38 * ea_bits_len + ip4_prefix < 32
39 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
41 * Independent Shared IPv4 address (ea_bits_len = 0):
44 * Rule IPv6 address = 128, Rule PSID Set
45 * Independent Full IPv4 address (ea_bits_len = 0):
47 * psid_length = 0, ip6_prefix = 128
48 * Independent IPv4 prefix (ea_bits_len = 0):
50 * psid_length = 0, ip6_prefix = 128
55 * This code supports MAP-T:
57 * With a DMR prefix length of 64 or 96 (RFC6052).
63 * Save user-assigned MAP domain names ("tags") in a vector of
64 * extra domain information.
67 map_save_extras (u32 map_domain_index, u8 * tag)
69 map_main_t *mm = &map_main;
70 map_domain_extra_t *de;
72 if (map_domain_index == ~0)
75 vec_validate (mm->domain_extras, map_domain_index);
76 de = vec_elt_at_index (mm->domain_extras, map_domain_index);
77 clib_memset (de, 0, sizeof (*de));
82 de->tag = vec_dup (tag);
87 map_free_extras (u32 map_domain_index)
89 map_main_t *mm = &map_main;
90 map_domain_extra_t *de;
93 if (map_domain_index == ~0)
96 de = vec_elt_at_index (mm->domain_extras, map_domain_index);
107 map_create_domain (ip4_address_t * ip4_prefix,
109 ip6_address_t * ip6_prefix,
111 ip6_address_t * ip6_src,
116 u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag)
118 u8 suffix_len, suffix_shift;
119 map_main_t *mm = &map_main;
122 /* How many, and which bits to grab from the IPv4 DA */
123 if (ip4_prefix_len + ea_bits_len < 32)
125 flags |= MAP_DOMAIN_PREFIX;
126 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
127 suffix_len = ea_bits_len;
132 suffix_len = 32 - ip4_prefix_len;
135 /* EA bits must be within the first 64 bits */
136 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
137 ip6_prefix_len + suffix_len + psid_length > 64))
140 ("Embedded Address bits must be within the first 64 bits of "
145 /* Get domain index */
146 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
147 clib_memset (d, 0, sizeof (*d));
148 *map_domain_index = d - mm->domains;
150 /* Init domain struct */
151 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
152 d->ip4_prefix_len = ip4_prefix_len;
153 d->ip6_prefix = *ip6_prefix;
154 d->ip6_prefix_len = ip6_prefix_len;
155 d->ip6_src = *ip6_src;
156 d->ip6_src_len = ip6_src_len;
157 d->ea_bits_len = ea_bits_len;
158 d->psid_offset = psid_offset;
159 d->psid_length = psid_length;
162 d->suffix_shift = suffix_shift;
163 d->suffix_mask = (1 << suffix_len) - 1;
165 d->psid_shift = 16 - psid_length - psid_offset;
166 d->psid_mask = (1 << d->psid_length) - 1;
167 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
169 /* Save a user-assigned MAP domain name if provided. */
171 map_save_extras (*map_domain_index, tag);
173 /* MAP longest match lookup table (input feature / FIB) */
174 mm->ip4_prefix_tbl->add (mm->ip4_prefix_tbl, &d->ip4_prefix,
175 d->ip4_prefix_len, *map_domain_index);
177 /* Really needed? Or always use FIB? */
178 mm->ip6_src_prefix_tbl->add (mm->ip6_src_prefix_tbl, &d->ip6_src,
179 d->ip6_src_len, *map_domain_index);
181 /* Validate packet/byte counters */
182 map_domain_counter_lock (mm);
184 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
186 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
188 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
191 for (i = 0; i < vec_len (mm->domain_counters); i++)
193 vlib_validate_combined_counter (&mm->domain_counters[i],
195 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
197 map_domain_counter_unlock (mm);
206 map_delete_domain (u32 map_domain_index)
208 map_main_t *mm = &map_main;
211 if (pool_is_free_index (mm->domains, map_domain_index))
213 clib_warning ("MAP domain delete: domain does not exist: %d",
218 d = pool_elt_at_index (mm->domains, map_domain_index);
219 mm->ip4_prefix_tbl->delete (mm->ip4_prefix_tbl, &d->ip4_prefix,
221 mm->ip6_src_prefix_tbl->delete (mm->ip6_src_prefix_tbl, &d->ip6_src,
224 /* Release user-assigned MAP domain name. */
225 map_free_extras (map_domain_index);
229 clib_mem_free (d->rules);
231 pool_put (mm->domains, d);
237 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
241 map_main_t *mm = &map_main;
243 if (pool_is_free_index (mm->domains, map_domain_index))
245 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
248 d = pool_elt_at_index (mm->domains, map_domain_index);
250 /* Rules are only used in 1:1 independent case */
251 if (d->ea_bits_len > 0)
256 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
257 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
260 clib_memset (d->rules, 0, l);
263 if (psid >= (0x1 << d->psid_length))
265 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
266 0x1 << d->psid_length);
272 d->rules[psid] = *tep;
276 clib_memset (&d->rules[psid], 0, sizeof (ip6_address_t));
281 #ifdef MAP_SKIP_IP6_LOOKUP
283 * Pre-resolved per-protocol global next-hops
285 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
288 map_pre_resolve_init (map_main_pre_resolved_t * pr)
290 pr->fei = FIB_NODE_INDEX_INVALID;
291 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
295 format_map_pre_resolve (u8 * s, va_list * ap)
297 map_main_pre_resolved_t *pr = va_arg (*ap, map_main_pre_resolved_t *);
299 if (FIB_NODE_INDEX_INVALID != pr->fei)
301 const fib_prefix_t *pfx;
303 pfx = fib_entry_get_prefix (pr->fei);
305 return (format (s, "%U (%u)",
306 format_ip46_address, &pfx->fp_addr, IP46_TYPE_ANY,
307 pr->dpo.dpoi_index));
311 return (format (s, "un-set"));
317 * Function definition to inform the FIB node that its last lock has gone.
320 map_last_lock_gone (fib_node_t * node)
323 * The MAP is a root of the graph. As such
324 * it never has children and thus is never locked.
329 static map_main_pre_resolved_t *
330 map_from_fib_node (fib_node_t * node)
332 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
333 return ((map_main_pre_resolved_t *)
335 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
339 map_stack (map_main_pre_resolved_t * pr)
343 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
345 dpo_copy (&pr->dpo, dpo);
349 * Function definition to backwalk a FIB node
351 static fib_node_back_walk_rc_t
352 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
354 map_stack (map_from_fib_node (node));
356 return (FIB_NODE_BACK_WALK_CONTINUE);
360 * Function definition to get a FIB node from its index
363 map_fib_node_get (fib_node_index_t index)
365 return (&pre_resolved[index].node);
369 * Virtual function table registered by MPLS GRE tunnels
370 * for participation in the FIB object graph.
372 const static fib_node_vft_t map_vft = {
373 .fnv_get = map_fib_node_get,
374 .fnv_last_lock = map_last_lock_gone,
375 .fnv_back_walk = map_back_walk,
379 map_fib_resolve (map_main_pre_resolved_t * pr,
380 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
388 pr->fei = fib_table_entry_special_add (0, // default fib
390 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
391 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
396 map_fib_unresolve (map_main_pre_resolved_t * pr,
397 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
405 if (pr->fei != FIB_NODE_INDEX_INVALID)
407 fib_entry_child_remove (pr->fei, pr->sibling);
409 fib_table_entry_special_remove (0, // default fib
410 &pfx, FIB_SOURCE_RR);
411 dpo_reset (&pr->dpo);
413 pr->fei = FIB_NODE_INDEX_INVALID;
414 pr->sibling = FIB_NODE_INDEX_INVALID;
419 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del)
421 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
423 ip46_address_t addr = {
427 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
428 FIB_PROTOCOL_IP6, 128, &addr);
430 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
431 FIB_PROTOCOL_IP6, 128, &addr);
433 if (ip4 && (ip4->as_u32 != 0))
435 ip46_address_t addr = {
439 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
440 FIB_PROTOCOL_IP4, 32, &addr);
442 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
443 FIB_PROTOCOL_IP4, 32, &addr);
448 static clib_error_t *
449 map_security_check_command_fn (vlib_main_t * vm,
450 unformat_input_t * input,
451 vlib_cli_command_t * cmd)
453 unformat_input_t _line_input, *line_input = &_line_input;
454 clib_error_t *error = NULL;
456 bool check_frag = false;
457 bool saw_enable = false;
458 bool saw_frag = false;
460 /* Get a line of input. */
461 if (!unformat_user (input, unformat_line_input, line_input))
464 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
466 if (unformat (line_input, "enable"))
471 else if (unformat (line_input, "disable"))
476 else if (unformat (line_input, "fragments on"))
481 else if (unformat (line_input, "fragments off"))
488 error = clib_error_return (0, "unknown input `%U'",
489 format_unformat_error, line_input);
496 error = clib_error_return (0,
497 "Must specify enable 'enable' or 'disable'");
503 error = clib_error_return (0, "Must specify fragments 'on' or 'off'");
507 map_param_set_security_check (enable, check_frag);
510 unformat_free (line_input);
516 static clib_error_t *
517 map_add_domain_command_fn (vlib_main_t * vm,
518 unformat_input_t * input, vlib_cli_command_t * cmd)
520 unformat_input_t _line_input, *line_input = &_line_input;
521 ip4_address_t ip4_prefix;
522 ip6_address_t ip6_prefix;
523 ip6_address_t ip6_src;
524 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
526 /* Optional arguments */
527 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
532 clib_error_t *error = NULL;
534 /* Get a line of input. */
535 if (!unformat_user (input, unformat_line_input, line_input))
538 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
541 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
546 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
551 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
556 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
558 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
560 else if (unformat (line_input, "psid-offset %d", &psid_offset))
562 else if (unformat (line_input, "psid-len %d", &psid_length))
564 else if (unformat (line_input, "mtu %d", &mtu))
566 else if (unformat (line_input, "tag %v", &tag))
570 error = clib_error_return (0, "unknown input `%U'",
571 format_unformat_error, line_input);
578 error = clib_error_return (0, "mandatory argument(s) missing");
582 map_create_domain (&ip4_prefix, ip4_prefix_len,
583 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
584 ea_bits_len, psid_offset, psid_length, &map_domain_index,
588 unformat_free (line_input);
593 static clib_error_t *
594 map_del_domain_command_fn (vlib_main_t * vm,
595 unformat_input_t * input, vlib_cli_command_t * cmd)
597 unformat_input_t _line_input, *line_input = &_line_input;
599 u32 map_domain_index;
600 clib_error_t *error = NULL;
602 /* Get a line of input. */
603 if (!unformat_user (input, unformat_line_input, line_input))
606 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
608 if (unformat (line_input, "index %d", &map_domain_index))
612 error = clib_error_return (0, "unknown input `%U'",
613 format_unformat_error, line_input);
620 error = clib_error_return (0, "mandatory argument(s) missing");
624 map_delete_domain (map_domain_index);
627 unformat_free (line_input);
632 static clib_error_t *
633 map_add_rule_command_fn (vlib_main_t * vm,
634 unformat_input_t * input, vlib_cli_command_t * cmd)
636 unformat_input_t _line_input, *line_input = &_line_input;
639 u32 psid = 0, map_domain_index;
640 clib_error_t *error = NULL;
642 /* Get a line of input. */
643 if (!unformat_user (input, unformat_line_input, line_input))
646 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
648 if (unformat (line_input, "index %d", &map_domain_index))
650 else if (unformat (line_input, "psid %d", &psid))
653 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
657 error = clib_error_return (0, "unknown input `%U'",
658 format_unformat_error, line_input);
665 error = clib_error_return (0, "mandatory argument(s) missing");
669 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
671 error = clib_error_return (0, "Failing to add Mapping Rule");
676 unformat_free (line_input);
681 #if MAP_SKIP_IP6_LOOKUP
682 static clib_error_t *
683 map_pre_resolve_command_fn (vlib_main_t * vm,
684 unformat_input_t * input,
685 vlib_cli_command_t * cmd)
687 unformat_input_t _line_input, *line_input = &_line_input;
688 ip4_address_t ip4nh, *p_v4 = NULL;
689 ip6_address_t ip6nh, *p_v6 = NULL;
690 clib_error_t *error = NULL;
693 clib_memset (&ip4nh, 0, sizeof (ip4nh));
694 clib_memset (&ip6nh, 0, sizeof (ip6nh));
696 /* Get a line of input. */
697 if (!unformat_user (input, unformat_line_input, line_input))
700 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
702 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
705 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
707 else if (unformat (line_input, "del"))
711 error = clib_error_return (0, "unknown input `%U'",
712 format_unformat_error, line_input);
717 map_pre_resolve (p_v4, p_v6, is_del);
720 unformat_free (line_input);
726 static clib_error_t *
727 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
728 unformat_input_t * input,
729 vlib_cli_command_t * cmd)
731 unformat_input_t _line_input, *line_input = &_line_input;
732 ip4_address_t icmp_src_address;
733 ip4_address_t *p_icmp_addr = 0;
734 map_main_t *mm = &map_main;
735 clib_error_t *error = NULL;
737 mm->icmp4_src_address.as_u32 = 0;
739 /* Get a line of input. */
740 if (!unformat_user (input, unformat_line_input, line_input))
743 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
746 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
748 mm->icmp4_src_address = icmp_src_address;
749 p_icmp_addr = &icmp_src_address;
753 error = clib_error_return (0, "unknown input `%U'",
754 format_unformat_error, line_input);
759 map_param_set_icmp (p_icmp_addr);
762 unformat_free (line_input);
767 static clib_error_t *
768 map_icmp_unreachables_command_fn (vlib_main_t * vm,
769 unformat_input_t * input,
770 vlib_cli_command_t * cmd)
772 unformat_input_t _line_input, *line_input = &_line_input;
774 clib_error_t *error = NULL;
775 bool enabled = false;
777 /* Get a line of input. */
778 if (!unformat_user (input, unformat_line_input, line_input))
781 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
784 if (unformat (line_input, "on"))
786 else if (unformat (line_input, "off"))
790 error = clib_error_return (0, "unknown input `%U'",
791 format_unformat_error, line_input);
798 error = clib_error_return (0, "mandatory argument(s) missing");
801 map_param_set_icmp6 (enabled);
804 unformat_free (line_input);
810 static clib_error_t *
811 map_fragment_command_fn (vlib_main_t * vm,
812 unformat_input_t * input, vlib_cli_command_t * cmd)
814 unformat_input_t _line_input, *line_input = &_line_input;
815 clib_error_t *error = NULL;
816 bool frag_inner = false;
817 bool frag_ignore_df = false;
818 bool saw_in_out = false;
821 /* Get a line of input. */
822 if (!unformat_user (input, unformat_line_input, line_input))
825 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
827 if (unformat (line_input, "inner"))
832 else if (unformat (line_input, "outer"))
837 else if (unformat (line_input, "ignore-df"))
839 frag_ignore_df = true;
842 else if (unformat (line_input, "honor-df"))
844 frag_ignore_df = false;
849 error = clib_error_return (0, "unknown input `%U'",
850 format_unformat_error, line_input);
857 error = clib_error_return (0, "Must specify 'inner' or 'outer'");
863 error = clib_error_return (0, "Must specify 'ignore-df' or 'honor-df'");
867 map_param_set_fragmentation (frag_inner, frag_ignore_df);
870 unformat_free (line_input);
875 static clib_error_t *
876 map_traffic_class_command_fn (vlib_main_t * vm,
877 unformat_input_t * input,
878 vlib_cli_command_t * cmd)
880 unformat_input_t _line_input, *line_input = &_line_input;
882 clib_error_t *error = NULL;
883 bool tc_copy = false;
886 /* Get a line of input. */
887 if (!unformat_user (input, unformat_line_input, line_input))
890 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
892 if (unformat (line_input, "copy"))
894 else if (unformat (line_input, "%x", &tc))
898 error = clib_error_return (0, "unknown input `%U'",
899 format_unformat_error, line_input);
904 map_param_set_traffic_class (tc_copy, tc);
907 unformat_free (line_input);
913 map_flags_to_string (u32 flags)
915 if (flags & MAP_DOMAIN_PREFIX)
921 format_map_domain (u8 * s, va_list * args)
923 map_domain_t *d = va_arg (*args, map_domain_t *);
924 bool counters = va_arg (*args, int);
925 map_main_t *mm = &map_main;
926 ip6_address_t ip6_prefix;
927 u32 map_domain_index = d - mm->domains;
928 map_domain_extra_t *de;
931 clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
933 ip6_prefix = d->ip6_prefix;
935 de = vec_elt_at_index (mm->domain_extras, map_domain_index);
938 "[%d] tag {%v} ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d "
939 "ea-bits-len %d psid-offset %d psid-len %d mtu %d %s",
940 map_domain_index, de->tag,
941 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
942 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
943 format_ip6_address, &d->ip6_src, d->ip6_src_len,
944 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
945 map_flags_to_string (d->flags));
949 map_domain_counter_lock (mm);
951 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
952 map_domain_index, &v);
953 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
954 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
955 map_domain_index, &v);
956 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
957 map_domain_counter_unlock (mm);
959 s = format (s, "\n");
965 for (i = 0; i < (0x1 << d->psid_length); i++)
968 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
971 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
979 format_map_ip4_reass (u8 * s, va_list * args)
981 map_main_t *mm = &map_main;
982 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
983 map_ip4_reass_key_t *k = &r->key;
984 f64 now = vlib_time_now (mm->vlib_main);
985 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
986 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
988 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
989 format_ip4_address, &k->src.as_u8, format_ip4_address,
990 &k->dst.as_u8, k->protocol,
991 clib_net_to_host_u16 (k->fragment_id),
992 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
997 format_map_ip6_reass (u8 * s, va_list * args)
999 map_main_t *mm = &map_main;
1000 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
1001 map_ip6_reass_key_t *k = &r->key;
1002 f64 now = vlib_time_now (mm->vlib_main);
1003 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
1004 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1006 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1007 format_ip6_address, &k->src.as_u8, format_ip6_address,
1008 &k->dst.as_u8, k->protocol,
1009 clib_net_to_host_u32 (k->fragment_id), dt);
1013 static clib_error_t *
1014 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1015 vlib_cli_command_t * cmd)
1017 unformat_input_t _line_input, *line_input = &_line_input;
1018 map_main_t *mm = &map_main;
1020 bool counters = false;
1021 u32 map_domain_index = ~0;
1022 clib_error_t *error = NULL;
1024 /* Get a line of input. */
1025 if (!unformat_user (input, unformat_line_input, line_input))
1028 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1030 if (unformat (line_input, "counters"))
1032 else if (unformat (line_input, "index %d", &map_domain_index))
1036 error = clib_error_return (0, "unknown input `%U'",
1037 format_unformat_error, line_input);
1042 if (pool_elts (mm->domains) == 0)
1043 vlib_cli_output (vm, "No MAP domains are configured...");
1045 if (map_domain_index == ~0)
1048 pool_foreach(d, mm->domains,
1049 ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1054 if (pool_is_free_index (mm->domains, map_domain_index))
1056 error = clib_error_return (0, "MAP domain does not exists %d",
1061 d = pool_elt_at_index (mm->domains, map_domain_index);
1062 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1066 unformat_free (line_input);
1071 static clib_error_t *
1072 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1073 vlib_cli_command_t * cmd)
1075 map_main_t *mm = &map_main;
1076 map_ip4_reass_t *f4;
1077 map_ip6_reass_t *f6;
1080 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
1083 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
1089 map_error_counter_get (u32 node_index, map_error_t map_error)
1091 vlib_main_t *vm = vlib_get_main ();
1092 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1093 vlib_error_main_t *em = &vm->error_main;
1094 vlib_error_t e = error_node->errors[map_error];
1095 vlib_node_t *n = vlib_get_node (vm, node_index);
1098 ci = vlib_error_get_code (e);
1099 ASSERT (ci < n->n_errors);
1100 ci += n->error_heap_index;
1102 return (em->counters[ci]);
1105 static clib_error_t *
1106 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1107 vlib_cli_command_t * cmd)
1109 map_main_t *mm = &map_main;
1111 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1112 if (pool_elts (mm->domains) == 0)
1114 vlib_cli_output (vm, "No MAP domains are configured...");
1119 pool_foreach(d, mm->domains, ({
1121 rulecount+= 0x1 << d->psid_length;
1122 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1124 domains += sizeof(*d);
1129 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1130 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1131 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1132 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1134 #if MAP_SKIP_IP6_LOOKUP
1135 vlib_cli_output (vm,
1136 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1137 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1138 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1143 vlib_cli_output (vm, "MAP traffic-class: copy");
1145 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1148 vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
1150 vlib_cli_output (vm,
1151 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1152 mm->sec_check ? "enabled" : "disabled",
1153 mm->sec_check_frag ? "enabled" : "disabled");
1155 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1156 format_ip4_address, &mm->icmp4_src_address);
1157 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1158 mm->icmp6_enabled ? "enabled" : "disabled");
1159 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1160 mm->frag_inner ? "enabled" : "disabled");
1161 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1162 mm->frag_ignore_df ? "enabled" : "disabled");
1167 vlib_combined_counter_main_t *cm = mm->domain_counters;
1168 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1169 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1173 clib_memset (total_pkts, 0, sizeof (total_pkts));
1174 clib_memset (total_bytes, 0, sizeof (total_bytes));
1176 map_domain_counter_lock (mm);
1177 vec_foreach (cm, mm->domain_counters)
1179 which = cm - mm->domain_counters;
1181 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1183 vlib_get_combined_counter (cm, i, &v);
1184 total_pkts[which] += v.packets;
1185 total_bytes[which] += v.bytes;
1188 map_domain_counter_unlock (mm);
1190 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1191 total_pkts[MAP_DOMAIN_COUNTER_TX],
1192 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1193 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1194 total_pkts[MAP_DOMAIN_COUNTER_RX],
1195 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1197 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1198 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1203 static clib_error_t *
1204 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1205 vlib_cli_command_t * cmd)
1207 unformat_input_t _line_input, *line_input = &_line_input;
1209 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1211 u64 buffers = ~(0ull);
1212 u8 ip4 = 0, ip6 = 0;
1214 if (!unformat_user (input, unformat_line_input, line_input))
1217 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1219 if (unformat (line_input, "lifetime %u", &lifetime))
1221 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1223 else if (unformat (line_input, "pool-size %u", &pool_size))
1225 else if (unformat (line_input, "buffers %llu", &buffers))
1227 else if (unformat (line_input, "ip4"))
1229 else if (unformat (line_input, "ip6"))
1233 unformat_free (line_input);
1234 return clib_error_return (0, "invalid input");
1237 unformat_free (line_input);
1240 return clib_error_return (0, "must specify ip4 and/or ip6");
1244 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1245 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1246 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1247 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1248 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1249 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1250 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1251 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1252 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1253 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1254 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1255 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1256 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1261 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1262 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1263 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1264 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1265 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1266 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1267 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1268 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1269 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1270 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1271 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1272 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1273 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1277 u32 reass = 0, packets = 0;
1278 rv = map_param_set_reassembly (!ip4, lifetime, pool_size, buffers, ht_ratio,
1284 vlib_cli_output (vm,
1285 "Note: destroyed-reassembly=%u , dropped-fragments=%u",
1289 case MAP_ERR_BAD_POOL_SIZE:
1290 return clib_error_return (0, "Could not set reass pool-size");
1292 case MAP_ERR_BAD_HT_RATIO:
1293 return clib_error_return (0, "Could not set reass ht-log2len");
1295 case MAP_ERR_BAD_LIFETIME:
1296 return clib_error_return (0, "Could not set ip6-reass lifetime");
1298 case MAP_ERR_BAD_BUFFERS:
1299 return clib_error_return (0, "Could not set ip6-reass buffers");
1301 case MAP_ERR_BAD_BUFFERS_TOO_LARGE:
1302 return clib_error_return (0,
1303 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1310 static clib_error_t *
1311 map_if_command_fn (vlib_main_t * vm,
1312 unformat_input_t * input, vlib_cli_command_t * cmd)
1314 unformat_input_t _line_input, *line_input = &_line_input;
1315 clib_error_t *error = NULL;
1316 bool is_enable = true, is_translation = false;
1317 vnet_main_t *vnm = vnet_get_main ();
1318 u32 sw_if_index = ~0;
1320 /* Get a line of input. */
1321 if (!unformat_user (input, unformat_line_input, line_input))
1324 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1327 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1329 else if (unformat (line_input, "del"))
1331 else if (unformat (line_input, "map-t"))
1332 is_translation = true;
1335 error = clib_error_return (0, "unknown input `%U'",
1336 format_unformat_error, line_input);
1342 unformat_free (line_input);
1344 if (sw_if_index == ~0)
1346 error = clib_error_return (0, "unknown interface");
1350 int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
1353 error = clib_error_return (0, "failure enabling MAP on interface");
1361 * packet trace format function
1364 format_map_trace (u8 * s, va_list * args)
1366 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1367 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1368 map_trace_t *t = va_arg (*args, map_trace_t *);
1369 u32 map_domain_index = t->map_domain_index;
1373 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1374 clib_net_to_host_u16 (port));
1379 static_always_inline map_ip4_reass_t *
1380 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1382 map_main_t *mm = &map_main;
1383 u32 ri = mm->ip4_reass_hash_table[bucket];
1384 while (ri != MAP_REASS_INDEX_NONE)
1386 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1387 if (r->key.as_u64[0] == k->as_u64[0] &&
1388 r->key.as_u64[1] == k->as_u64[1] &&
1389 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1393 ri = r->bucket_next;
1398 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1401 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1403 map_main_t *mm = &map_main;
1404 map_ip4_reass_get_fragments (r, pi_to_drop);
1406 // Unlink in hash bucket
1407 map_ip4_reass_t *r2 = NULL;
1408 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1409 while (r2i != map_ip4_reass_pool_index (r))
1411 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1412 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1413 r2i = r2->bucket_next;
1417 r2->bucket_next = r->bucket_next;
1421 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1425 if (r->fifo_next == map_ip4_reass_pool_index (r))
1427 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1431 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1432 mm->ip4_reass_fifo_last = r->fifo_prev;
1433 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1435 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1439 pool_put (mm->ip4_reass_pool, r);
1440 mm->ip4_reass_allocated--;
1444 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1445 u8 protocol, u32 ** pi_to_drop)
1448 map_main_t *mm = &map_main;
1449 map_ip4_reass_key_t k = {.src.data_u32 = src,
1450 .dst.data_u32 = dst,
1451 .fragment_id = fragment_id,
1452 .protocol = protocol
1456 #ifdef clib_crc32c_uses_intrinsics
1457 h = clib_crc32c ((u8 *) k.as_u32, 16);
1459 u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1460 h = clib_xxhash (tmp);
1462 h = h >> (32 - mm->ip4_reass_ht_log2len);
1464 f64 now = vlib_time_now (mm->vlib_main);
1466 //Cache garbage collection
1467 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1469 map_ip4_reass_t *last =
1470 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1471 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1472 map_ip4_reass_free (last, pi_to_drop);
1477 if ((r = map_ip4_reass_lookup (&k, h, now)))
1480 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1483 pool_get (mm->ip4_reass_pool, r);
1484 mm->ip4_reass_allocated++;
1486 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1487 r->fragments[i] = ~0;
1489 u32 ri = map_ip4_reass_pool_index (r);
1491 //Link in new bucket
1493 r->bucket_next = mm->ip4_reass_hash_table[h];
1494 mm->ip4_reass_hash_table[h] = ri;
1497 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1500 pool_elt_at_index (mm->ip4_reass_pool,
1501 mm->ip4_reass_fifo_last)->fifo_next;
1502 r->fifo_prev = mm->ip4_reass_fifo_last;
1503 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1504 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1508 r->fifo_next = r->fifo_prev = ri;
1509 mm->ip4_reass_fifo_last = ri;
1516 #ifdef MAP_IP4_REASS_COUNT_BYTES
1517 r->expected_total = 0xffff;
1525 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1527 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1531 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1532 if (r->fragments[i] == ~0)
1534 r->fragments[i] = pi;
1535 map_main.ip4_reass_buffered_counter++;
1541 static_always_inline map_ip6_reass_t *
1542 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1544 map_main_t *mm = &map_main;
1545 u32 ri = mm->ip6_reass_hash_table[bucket];
1546 while (ri != MAP_REASS_INDEX_NONE)
1548 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1549 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1550 r->key.as_u64[0] == k->as_u64[0] &&
1551 r->key.as_u64[1] == k->as_u64[1] &&
1552 r->key.as_u64[2] == k->as_u64[2] &&
1553 r->key.as_u64[3] == k->as_u64[3] &&
1554 r->key.as_u64[4] == k->as_u64[4])
1556 ri = r->bucket_next;
1561 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1564 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1566 map_main_t *mm = &map_main;
1568 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1569 if (r->fragments[i].pi != ~0)
1571 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1572 r->fragments[i].pi = ~0;
1573 map_main.ip6_reass_buffered_counter--;
1576 // Unlink in hash bucket
1577 map_ip6_reass_t *r2 = NULL;
1578 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1579 while (r2i != map_ip6_reass_pool_index (r))
1581 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1582 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1583 r2i = r2->bucket_next;
1587 r2->bucket_next = r->bucket_next;
1591 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1595 if (r->fifo_next == map_ip6_reass_pool_index (r))
1597 //Single element in the list, list is now empty
1598 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1602 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1603 mm->ip6_reass_fifo_last = r->fifo_prev;
1604 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1606 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1610 // Free from pool if necessary
1611 pool_put (mm->ip6_reass_pool, r);
1612 mm->ip6_reass_allocated--;
1616 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1617 u8 protocol, u32 ** pi_to_drop)
1620 map_main_t *mm = &map_main;
1621 map_ip6_reass_key_t k = {
1624 .fragment_id = fragment_id,
1625 .protocol = protocol
1631 #ifdef clib_crc32c_uses_intrinsics
1632 h = clib_crc32c ((u8 *) k.as_u32, 40);
1635 k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1636 h = clib_xxhash (tmp);
1639 h = h >> (32 - mm->ip6_reass_ht_log2len);
1641 f64 now = vlib_time_now (mm->vlib_main);
1643 //Cache garbage collection
1644 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1646 map_ip6_reass_t *last =
1647 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1648 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1649 map_ip6_reass_free (last, pi_to_drop);
1654 if ((r = map_ip6_reass_lookup (&k, h, now)))
1657 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1660 pool_get (mm->ip6_reass_pool, r);
1661 mm->ip6_reass_allocated++;
1662 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1664 r->fragments[i].pi = ~0;
1665 r->fragments[i].next_data_len = 0;
1666 r->fragments[i].next_data_offset = 0;
1669 u32 ri = map_ip6_reass_pool_index (r);
1671 //Link in new bucket
1673 r->bucket_next = mm->ip6_reass_hash_table[h];
1674 mm->ip6_reass_hash_table[h] = ri;
1677 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1680 pool_elt_at_index (mm->ip6_reass_pool,
1681 mm->ip6_reass_fifo_last)->fifo_next;
1682 r->fifo_prev = mm->ip6_reass_fifo_last;
1683 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1684 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1688 r->fifo_next = r->fifo_prev = ri;
1689 mm->ip6_reass_fifo_last = ri;
1695 r->ip4_header.ip_version_and_header_length = 0;
1696 #ifdef MAP_IP6_REASS_COUNT_BYTES
1697 r->expected_total = 0xffff;
1704 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1705 u16 data_offset, u16 next_data_offset,
1706 u8 * data_start, u16 data_len)
1708 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1709 u16 copied_len = (data_len > 20) ? 20 : data_len;
1711 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1714 //Lookup for fragments for the current buffer
1715 //and the one before that
1717 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1719 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1721 prev_f = &r->fragments[i]; // This is buffer for previous packet
1723 else if (r->fragments[i].next_data_offset == next_data_offset)
1725 f = &r->fragments[i]; // This is a buffer for the current packet
1727 else if (r->fragments[i].next_data_offset == 0)
1730 f = &r->fragments[i];
1731 else if (prev_f == NULL)
1732 prev_f = &r->fragments[i];
1736 if (!f || f->pi != ~0)
1744 clib_memcpy_fast (prev_f->next_data, data_start, copied_len);
1745 prev_f->next_data_len = copied_len;
1746 prev_f->next_data_offset = data_offset;
1750 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1753 if (r->ip4_header.ip_version_and_header_length == 0)
1754 clib_memcpy_fast (&r->ip4_header, data_start, sizeof (ip4_header_t));
1759 f->next_data_offset = next_data_offset;
1761 map_main.ip6_reass_buffered_counter++;
1767 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1769 map_main_t *mm = &map_main;
1772 if (dropped_packets)
1773 *dropped_packets = mm->ip4_reass_buffered_counter;
1775 *trashed_reass = mm->ip4_reass_allocated;
1776 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1778 u16 ri = mm->ip4_reass_fifo_last;
1781 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1782 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1783 if (r->fragments[i] != ~0)
1784 map_ip4_drop_pi (r->fragments[i]);
1787 pool_put (mm->ip4_reass_pool, r);
1789 while (ri != mm->ip4_reass_fifo_last);
1792 vec_free (mm->ip4_reass_hash_table);
1793 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1794 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1795 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1796 pool_free (mm->ip4_reass_pool);
1797 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1799 mm->ip4_reass_allocated = 0;
1800 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1801 mm->ip4_reass_buffered_counter = 0;
1805 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1807 u32 desired_size = (u32) (pool_size * ht_ratio);
1809 for (i = 1; i < 31; i++)
1810 if ((1 << i) >= desired_size)
1816 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1817 u32 * dropped_packets)
1819 map_main_t *mm = &map_main;
1820 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1823 map_ip4_reass_lock ();
1824 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1825 mm->ip4_reass_ht_log2len =
1826 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1827 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1828 map_ip4_reass_unlock ();
1833 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1834 u32 * dropped_packets)
1836 map_main_t *mm = &map_main;
1837 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1840 map_ip4_reass_lock ();
1841 mm->ip4_reass_conf_pool_size = pool_size;
1842 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1843 map_ip4_reass_unlock ();
1848 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1850 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1855 map_ip4_reass_conf_buffers (u32 buffers)
1857 map_main.ip4_reass_conf_buffers = buffers;
1862 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1864 map_main_t *mm = &map_main;
1865 if (dropped_packets)
1866 *dropped_packets = mm->ip6_reass_buffered_counter;
1868 *trashed_reass = mm->ip6_reass_allocated;
1870 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1872 u16 ri = mm->ip6_reass_fifo_last;
1875 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1876 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1877 if (r->fragments[i].pi != ~0)
1878 map_ip6_drop_pi (r->fragments[i].pi);
1881 pool_put (mm->ip6_reass_pool, r);
1883 while (ri != mm->ip6_reass_fifo_last);
1884 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1887 vec_free (mm->ip6_reass_hash_table);
1888 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1889 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1890 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1891 pool_free (mm->ip6_reass_pool);
1892 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1894 mm->ip6_reass_allocated = 0;
1895 mm->ip6_reass_buffered_counter = 0;
1899 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1900 u32 * dropped_packets)
1902 map_main_t *mm = &map_main;
1903 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1906 map_ip6_reass_lock ();
1907 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1908 mm->ip6_reass_ht_log2len =
1909 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1910 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1911 map_ip6_reass_unlock ();
1916 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1917 u32 * dropped_packets)
1919 map_main_t *mm = &map_main;
1920 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1923 map_ip6_reass_lock ();
1924 mm->ip6_reass_conf_pool_size = pool_size;
1925 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1926 map_ip6_reass_unlock ();
1931 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1933 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1938 map_ip6_reass_conf_buffers (u32 buffers)
1940 map_main.ip6_reass_conf_buffers = buffers;
1944 static clib_error_t *
1945 map_tcp_mss_command_fn (vlib_main_t * vm,
1946 unformat_input_t * input, vlib_cli_command_t * cmd)
1948 unformat_input_t _line_input, *line_input = &_line_input;
1949 clib_error_t *error = NULL;
1952 /* Get a line of input. */
1953 if (!unformat_user (input, unformat_line_input, line_input))
1956 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1958 if (unformat (line_input, "%u", &tcp_mss))
1962 error = clib_error_return (0, "unknown input `%U'",
1963 format_unformat_error, line_input);
1968 if (tcp_mss >= (0x1 << 16))
1970 error = clib_error_return (0, "invalid value `%u'", tcp_mss);
1974 map_param_set_tcp (tcp_mss);
1977 unformat_free (line_input);
1986 * Configure MAP reassembly behaviour
1989 * @cliexstart{map params reassembly}
1992 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1993 .path = "map params reassembly",
1994 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
1995 "[pool-size <pool-size>] [buffers <buffers>] "
1996 "[ht-ratio <ht-ratio>]",
1997 .function = map_params_reass_command_fn,
2001 * Set or copy the IP TOS/Traffic Class field
2004 * @cliexstart{map params traffic-class}
2006 * This command is used to set the traffic-class field in translated
2007 * or encapsulated packets. If copy is specifed (the default) then the
2008 * traffic-class/TOS field is copied from the original packet to the
2009 * translated / encapsulating header.
2012 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2013 .path = "map params traffic-class",
2014 .short_help = "map params traffic-class {0x0-0xff | copy}",
2015 .function = map_traffic_class_command_fn,
2022 * @cliexstart{map params tcp-mss}
2024 * This command is used to set the TCP MSS in translated
2025 * or encapsulated packets.
2028 VLIB_CLI_COMMAND(map_tcp_mss_command, static) = {
2029 .path = "map params tcp-mss",
2030 .short_help = "map params tcp-mss <value>",
2031 .function = map_tcp_mss_command_fn,
2035 * Bypass IP4/IP6 lookup
2038 * @cliexstart{map params pre-resolve}
2040 * Bypass a second FIB lookup of the translated or encapsulated
2041 * packet, and forward the packet directly to the specified
2042 * next-hop. This optimization trades forwarding flexibility for
2046 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2047 .path = "map params pre-resolve",
2048 .short_help = " map params pre-resolve {ip4-nh <address>} "
2049 "| {ip6-nh <address>}",
2050 .function = map_pre_resolve_command_fn,
2054 * Enable or disable the MAP-E inbound security check
2055 * Specifiy if the inbound security check should be done on fragments
2058 * @cliexstart{map params security-check}
2060 * By default, a decapsulated packet's IPv4 source address will be
2061 * verified against the outer header's IPv6 source address. Disabling
2062 * this feature will allow IPv4 source address spoofing.
2064 * Typically the inbound on-decapsulation security check is only done
2065 * on the first packet. The packet that contains the L4
2066 * information. While a security check on every fragment is possible,
2067 * it has a cost. State must be created on the first fragment.
2070 VLIB_CLI_COMMAND(map_security_check_command, static) = {
2071 .path = "map params security-check",
2072 .short_help = "map params security-check enable|disable fragments on|off",
2073 .function = map_security_check_command_fn,
2078 * Specifiy the IPv4 source address used for relayed ICMP error messages
2081 * @cliexstart{map params icmp source-address}
2083 * This command specifies which IPv4 source address (must be local to
2084 * the system), that is used for relayed received IPv6 ICMP error
2088 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2089 .path = "map params icmp source-address",
2090 .short_help = "map params icmp source-address <ip4-address>",
2091 .function = map_icmp_relay_source_address_command_fn,
2095 * Send IPv6 ICMP unreachables
2098 * @cliexstart{map params icmp6 unreachables}
2100 * Send IPv6 ICMP unreachable messages back if security check fails or
2101 * no MAP domain exists.
2104 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2105 .path = "map params icmp6 unreachables",
2106 .short_help = "map params icmp6 unreachables {on|off}",
2107 .function = map_icmp_unreachables_command_fn,
2111 * Configure MAP fragmentation behaviour
2114 * @cliexstart{map params fragment}
2116 * Allows fragmentation of the IPv4 packet even if the DF bit is
2117 * set. The choice between inner or outer fragmentation of tunnel
2118 * packets is complicated. The benefit of inner fragmentation is that
2119 * the ultimate endpoint must reassemble, instead of the tunnel
2123 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2124 .path = "map params fragment",
2125 .short_help = "map params fragment inner|outer ignore-df|honor-df",
2126 .function = map_fragment_command_fn,
2134 * @cliexstart{map add domain}
2137 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2138 .path = "map add domain",
2139 .short_help = "map add domain [tag <tag>] ip4-pfx <ip4-pfx> "
2140 "ip6-pfx <ip6-pfx> "
2141 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2142 "[map-t] [mtu <mtu>]",
2143 .function = map_add_domain_command_fn,
2147 * Add MAP rule to a domain
2150 * @cliexstart{map add rule}
2153 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2154 .path = "map add rule",
2155 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2156 .function = map_add_rule_command_fn,
2163 * @cliexstart{map del domain}
2166 VLIB_CLI_COMMAND(map_del_command, static) = {
2167 .path = "map del domain",
2168 .short_help = "map del domain index <domain>",
2169 .function = map_del_domain_command_fn,
2176 * @cliexstart{show map domain}
2179 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2180 .path = "show map domain",
2181 .short_help = "show map domain index <n> [counters]",
2182 .function = show_map_domain_command_fn,
2186 * Show MAP statistics
2189 * @cliexstart{show map stats}
2192 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2193 .path = "show map stats",
2194 .short_help = "show map stats",
2195 .function = show_map_stats_command_fn,
2199 * Show MAP fragmentation information
2202 * @cliexstart{show map fragments}
2205 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2206 .path = "show map fragments",
2207 .short_help = "show map fragments",
2208 .function = show_map_fragments_command_fn,
2212 * Enable MAP processing on interface (input feature)
2215 VLIB_CLI_COMMAND(map_if_command, static) = {
2216 .path = "map interface",
2217 .short_help = "map interface <interface-name> [map-t] [del]",
2218 .function = map_if_command_fn,
2221 VLIB_PLUGIN_REGISTER() = {
2222 .version = VPP_BUILD_VER,
2223 .description = "Mapping of Address and Port (MAP)",
2232 map_init (vlib_main_t * vm)
2234 map_main_t *mm = &map_main;
2235 clib_error_t *error = 0;
2237 memset (mm, 0, sizeof (*mm));
2239 mm->vnet_main = vnet_get_main ();
2242 #ifdef MAP_SKIP_IP6_LOOKUP
2243 fib_protocol_t proto;
2245 FOR_EACH_FIB_PROTOCOL (proto)
2247 map_pre_resolve_init (&pre_resolved[proto]);
2255 /* Inbound security check */
2256 mm->sec_check = true;
2257 mm->sec_check_frag = false;
2259 /* ICMP6 Type 1, Code 5 for security check failure */
2260 mm->icmp6_enabled = false;
2262 /* Inner or outer fragmentation */
2263 mm->frag_inner = false;
2264 mm->frag_ignore_df = false;
2266 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2267 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
2268 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
2270 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2271 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2272 mm->icmp_relayed.stat_segment_name = "/map/icmp-relayed";
2274 /* IP4 virtual reassembly */
2275 mm->ip4_reass_hash_table = 0;
2276 mm->ip4_reass_pool = 0;
2277 mm->ip4_reass_lock =
2278 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2279 *mm->ip4_reass_lock = 0;
2280 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2281 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2282 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2283 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2284 mm->ip4_reass_ht_log2len =
2285 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2286 mm->ip4_reass_conf_pool_size);
2287 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2288 map_ip4_reass_reinit (NULL, NULL);
2290 /* IP6 virtual reassembly */
2291 mm->ip6_reass_hash_table = 0;
2292 mm->ip6_reass_pool = 0;
2293 mm->ip6_reass_lock =
2294 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2295 *mm->ip6_reass_lock = 0;
2296 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2297 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2298 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2299 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2300 mm->ip6_reass_ht_log2len =
2301 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2302 mm->ip6_reass_conf_pool_size);
2303 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2304 map_ip6_reass_reinit (NULL, NULL);
2306 #ifdef MAP_SKIP_IP6_LOOKUP
2307 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2310 /* LPM lookup tables */
2311 mm->ip4_prefix_tbl = lpm_table_init (LPM_TYPE_KEY32);
2312 mm->ip6_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
2313 mm->ip6_src_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
2315 mm->bm_trans_enabled_by_sw_if = 0;
2316 mm->bm_encap_enabled_by_sw_if = 0;
2318 error = map_plugin_api_hookup (vm);
2323 VLIB_INIT_FUNCTION (map_init);
2326 * fd.io coding-style-patch-verification: ON
2329 * eval: (c-set-style "gnu")