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.
21 #include <vppinfra/xxhash.h>
24 crc_u32 (u32 data, u32 value)
26 u64 tmp = ((u64) data << 32) | (u64) value;
27 return (u32) clib_xxhash (tmp);
32 * This code supports the following MAP modes:
34 * Algorithmic Shared 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 Full IPv4 address (ea_bits_len > 0):
38 * ea_bits_len + ip4_prefix = 32
39 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
40 * Algorithmic IPv4 prefix (ea_bits_len > 0):
41 * ea_bits_len + ip4_prefix < 32
42 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
44 * Independent Shared IPv4 address (ea_bits_len = 0):
47 * Rule IPv6 address = 128, Rule PSID Set
48 * Independent Full IPv4 address (ea_bits_len = 0):
50 * psid_length = 0, ip6_prefix = 128
51 * Independent IPv4 prefix (ea_bits_len = 0):
53 * psid_length = 0, ip6_prefix = 128
58 * This code supports MAP-T:
60 * With DMR prefix length equal to 96.
66 ip4_get_port (ip4_header_t * ip, map_dir_e dir, u16 buffer_len)
68 //TODO: use buffer length
69 if (ip->ip_version_and_header_length != 0x45 ||
70 ip4_get_fragment_offset (ip))
73 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
74 (ip->protocol == IP_PROTOCOL_UDP)))
76 udp_header_t *udp = (void *) (ip + 1);
77 return (dir == MAP_SENDER) ? udp->src_port : udp->dst_port;
79 else if (ip->protocol == IP_PROTOCOL_ICMP)
81 icmp46_header_t *icmp = (void *) (ip + 1);
82 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
84 return *((u16 *) (icmp + 1));
86 else if (clib_net_to_host_u16 (ip->length) >= 64)
88 ip = (ip4_header_t *) (icmp + 2);
89 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
90 (ip->protocol == IP_PROTOCOL_UDP)))
92 udp_header_t *udp = (void *) (ip + 1);
93 return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
95 else if (ip->protocol == IP_PROTOCOL_ICMP)
97 icmp46_header_t *icmp = (void *) (ip + 1);
98 if (icmp->type == ICMP4_echo_request ||
99 icmp->type == ICMP4_echo_reply)
101 return *((u16 *) (icmp + 1));
110 ip6_get_port (ip6_header_t * ip6, map_dir_e dir, u16 buffer_len)
117 if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
120 //TODO: Use buffer length
123 ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
124 u8_ptr_add (ip6, frag_offset))))
125 return -1; //Can't deal with non-first fragment for now
127 l4 = u8_ptr_add (ip6, l4_offset);
128 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
131 MAP_SENDER) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t
135 else if (l4_protocol == IP_PROTOCOL_ICMP6)
137 icmp46_header_t *icmp = (icmp46_header_t *) (l4);
138 if (icmp->type == ICMP6_echo_request)
140 return (dir == MAP_SENDER) ? ((u16 *) (icmp))[2] : -1;
142 else if (icmp->type == ICMP6_echo_reply)
144 return (dir == MAP_SENDER) ? -1 : ((u16 *) (icmp))[2];
152 map_create_domain (ip4_address_t * ip4_prefix,
154 ip6_address_t * ip6_prefix,
156 ip6_address_t * ip6_src,
160 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
162 map_main_t *mm = &map_main;
163 ip4_main_t *im4 = &ip4_main;
164 ip6_main_t *im6 = &ip6_main;
167 ip4_add_del_route_args_t args4;
168 ip6_add_del_route_args_t args6;
169 u8 suffix_len, suffix_shift;
172 /* Sanity check on the src prefix length */
173 if (flags & MAP_DOMAIN_TRANSLATION)
175 if (ip6_src_len != 96)
177 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
183 if (ip6_src_len != 128)
186 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
192 /* How many, and which bits to grab from the IPv4 DA */
193 if (ip4_prefix_len + ea_bits_len < 32)
195 flags |= MAP_DOMAIN_PREFIX;
196 suffix_len = suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
201 suffix_len = 32 - ip4_prefix_len;
204 /* EA bits must be within the first 64 bits */
205 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
206 ip6_prefix_len + suffix_len + psid_length > 64))
209 ("Embedded Address bits must be within the first 64 bits of "
214 /* Get domain index */
215 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
216 memset (d, 0, sizeof (*d));
217 *map_domain_index = d - mm->domains;
219 /* Init domain struct */
220 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
221 d->ip4_prefix_len = ip4_prefix_len;
222 d->ip6_prefix = *ip6_prefix;
223 d->ip6_prefix_len = ip6_prefix_len;
224 d->ip6_src = *ip6_src;
225 d->ip6_src_len = ip6_src_len;
226 d->ea_bits_len = ea_bits_len;
227 d->psid_offset = psid_offset;
228 d->psid_length = psid_length;
231 d->suffix_shift = suffix_shift;
232 d->suffix_mask = (1 << suffix_len) - 1;
234 d->psid_shift = 16 - psid_length - psid_offset;
235 d->psid_mask = (1 << d->psid_length) - 1;
236 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
238 /* Init IP adjacency */
239 memset (&adj, 0, sizeof (adj));
240 adj.explicit_fib_index = ~0;
241 adj.lookup_next_index =
242 (d->flags & MAP_DOMAIN_TRANSLATION) ? IP_LOOKUP_NEXT_MAP_T :
244 p = (uword *) & adj.rewrite_data[0];
245 *p = (uword) (*map_domain_index);
247 if (ip4_get_route (im4, 0, 0, (u8 *) ip4_prefix, ip4_prefix_len))
249 clib_warning ("IPv4 route already defined: %U/%d", format_ip4_address,
250 ip4_prefix, ip4_prefix_len);
251 pool_put (mm->domains, d);
255 /* Create ip4 adjacency */
256 memset (&args4, 0, sizeof (args4));
257 args4.table_index_or_table_id = 0;
258 args4.flags = IP4_ROUTE_FLAG_ADD;
259 args4.dst_address.as_u32 = ip4_prefix->as_u32;
260 args4.dst_address_length = ip4_prefix_len;
262 args4.adj_index = ~0;
263 args4.add_adj = &adj;
265 ip4_add_del_route (im4, &args4);
267 /* Multiple MAP domains may share same source IPv6 TEP */
268 u32 ai = ip6_get_route (im6, 0, 0, ip6_src, ip6_src_len);
271 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
272 ip_adjacency_t *adj6 = ip_get_adjacency (lm6, ai);
273 if (adj6->lookup_next_index != IP_LOOKUP_NEXT_MAP &&
274 adj6->lookup_next_index != IP_LOOKUP_NEXT_MAP_T)
276 clib_warning ("BR source address already assigned: %U",
277 format_ip6_address, ip6_src);
278 pool_put (mm->domains, d);
282 p = (uword *) & adj6->rewrite_data[0];
286 * Add refcount, so we don't accidentially delete the route
293 /* Create ip6 adjacency. */
294 memset (&args6, 0, sizeof (args6));
295 args6.table_index_or_table_id = 0;
296 args6.flags = IP6_ROUTE_FLAG_ADD;
297 args6.dst_address.as_u64[0] = ip6_src->as_u64[0];
298 args6.dst_address.as_u64[1] = ip6_src->as_u64[1];
299 args6.dst_address_length = ip6_src_len;
300 args6.adj_index = ~0;
301 args6.add_adj = &adj;
303 ip6_add_del_route (im6, &args6);
306 /* Validate packet/byte counters */
307 map_domain_counter_lock (mm);
309 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
311 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
313 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
316 for (i = 0; i < vec_len (mm->domain_counters); i++)
318 vlib_validate_combined_counter (&mm->domain_counters[i],
320 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
322 map_domain_counter_unlock (mm);
331 map_delete_domain (u32 map_domain_index)
333 map_main_t *mm = &map_main;
334 ip4_main_t *im4 = &ip4_main;
335 ip6_main_t *im6 = &ip6_main;
338 ip4_add_del_route_args_t args4;
339 ip6_add_del_route_args_t args6;
341 if (pool_is_free_index (mm->domains, map_domain_index))
343 clib_warning ("MAP domain delete: domain does not exist: %d",
348 d = pool_elt_at_index (mm->domains, map_domain_index);
350 memset (&adj, 0, sizeof (adj));
351 adj.explicit_fib_index = ~0;
352 adj.lookup_next_index =
353 (d->flags & MAP_DOMAIN_TRANSLATION) ? IP_LOOKUP_NEXT_MAP_T :
356 /* Delete ip4 adjacency */
357 memset (&args4, 0, sizeof (args4));
358 args4.table_index_or_table_id = 0;
359 args4.flags = IP4_ROUTE_FLAG_DEL;
360 args4.dst_address.as_u32 = d->ip4_prefix.as_u32;
361 args4.dst_address_length = d->ip4_prefix_len;
363 args4.add_adj = &adj;
365 ip4_add_del_route (im4, &args4);
367 /* Delete ip6 adjacency */
368 u32 ai = ip6_get_route (im6, 0, 0, &d->ip6_src, d->ip6_src_len);
371 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
372 ip_adjacency_t *adj6 = ip_get_adjacency (lm6, ai);
374 uword *p = (uword *) & adj6->rewrite_data[0];
375 /* Delete route when no other domains use this source */
378 memset (&args6, 0, sizeof (args6));
379 args6.table_index_or_table_id = 0;
380 args6.flags = IP6_ROUTE_FLAG_DEL;
381 args6.dst_address.as_u64[0] = d->ip6_src.as_u64[0];
382 args6.dst_address.as_u64[1] = d->ip6_src.as_u64[1];
383 args6.dst_address_length = d->ip6_src_len;
385 args6.add_adj = &adj;
387 ip6_add_del_route (im6, &args6);
393 clib_mem_free (d->rules);
395 pool_put (mm->domains, d);
401 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
405 map_main_t *mm = &map_main;
407 if (pool_is_free_index (mm->domains, map_domain_index))
409 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
412 d = pool_elt_at_index (mm->domains, map_domain_index);
414 /* Rules are only used in 1:1 independent case */
415 if (d->ea_bits_len > 0)
420 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
421 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
424 memset (d->rules, 0, l);
427 if (psid >= (0x1 << d->psid_length))
429 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
430 0x1 << d->psid_length);
436 d->rules[psid] = *tep;
440 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
445 #ifdef MAP_SKIP_IP6_LOOKUP
447 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6)
449 map_main_t *mm = &map_main;
450 ip4_main_t *im4 = &ip4_main;
451 ip6_main_t *im6 = &ip6_main;
453 if (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0)
455 mm->adj6_index = ip6_fib_lookup_with_table (im6, 0, ip6);
456 clib_warning ("FIB lookup results in: %u", mm->adj6_index);
458 if (ip4->as_u32 != 0)
460 mm->adj4_index = ip4_fib_lookup_with_table (im4, 0, ip4, 0);
461 clib_warning ("FIB lookup results in: %u", mm->adj4_index);
466 static clib_error_t *
467 map_security_check_command_fn (vlib_main_t * vm,
468 unformat_input_t * input,
469 vlib_cli_command_t * cmd)
471 unformat_input_t _line_input, *line_input = &_line_input;
472 map_main_t *mm = &map_main;
473 /* Get a line of input. */
474 if (!unformat_user (input, unformat_line_input, line_input))
477 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
479 if (unformat (line_input, "off"))
480 mm->sec_check = false;
481 else if (unformat (line_input, "on"))
482 mm->sec_check = true;
484 return clib_error_return (0, "unknown input `%U'",
485 format_unformat_error, input);
487 unformat_free (line_input);
491 static clib_error_t *
492 map_security_check_frag_command_fn (vlib_main_t * vm,
493 unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 unformat_input_t _line_input, *line_input = &_line_input;
497 map_main_t *mm = &map_main;
498 /* Get a line of input. */
499 if (!unformat_user (input, unformat_line_input, line_input))
502 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
504 if (unformat (line_input, "off"))
505 mm->sec_check_frag = false;
506 else if (unformat (line_input, "on"))
507 mm->sec_check_frag = true;
509 return clib_error_return (0, "unknown input `%U'",
510 format_unformat_error, input);
512 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 /* Get a line of input. */
533 if (!unformat_user (input, unformat_line_input, line_input))
536 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
539 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
544 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
549 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
554 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
556 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
558 else if (unformat (line_input, "psid-offset %d", &psid_offset))
560 else if (unformat (line_input, "psid-len %d", &psid_length))
562 else if (unformat (line_input, "mtu %d", &mtu))
564 else if (unformat (line_input, "map-t"))
565 flags |= MAP_DOMAIN_TRANSLATION;
567 return clib_error_return (0, "unknown input `%U'",
568 format_unformat_error, input);
570 unformat_free (line_input);
573 return clib_error_return (0, "mandatory argument(s) missing");
575 map_create_domain (&ip4_prefix, ip4_prefix_len,
576 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
577 ea_bits_len, psid_offset, psid_length, &map_domain_index,
583 static clib_error_t *
584 map_del_domain_command_fn (vlib_main_t * vm,
585 unformat_input_t * input, vlib_cli_command_t * cmd)
587 unformat_input_t _line_input, *line_input = &_line_input;
589 u32 map_domain_index;
591 /* Get a line of input. */
592 if (!unformat_user (input, unformat_line_input, line_input))
595 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
597 if (unformat (line_input, "index %d", &map_domain_index))
600 return clib_error_return (0, "unknown input `%U'",
601 format_unformat_error, input);
603 unformat_free (line_input);
606 return clib_error_return (0, "mandatory argument(s) missing");
608 map_delete_domain (map_domain_index);
613 static clib_error_t *
614 map_add_rule_command_fn (vlib_main_t * vm,
615 unformat_input_t * input, vlib_cli_command_t * cmd)
617 unformat_input_t _line_input, *line_input = &_line_input;
620 u32 psid = 0, map_domain_index;
622 /* Get a line of input. */
623 if (!unformat_user (input, unformat_line_input, line_input))
626 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
628 if (unformat (line_input, "index %d", &map_domain_index))
630 else if (unformat (line_input, "psid %d", &psid))
633 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
636 return clib_error_return (0, "unknown input `%U'",
637 format_unformat_error, input);
639 unformat_free (line_input);
642 return clib_error_return (0, "mandatory argument(s) missing");
644 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
646 return clib_error_return (0, "Failing to add Mapping Rule");
651 #if MAP_SKIP_IP6_LOOKUP
652 static clib_error_t *
653 map_pre_resolve_command_fn (vlib_main_t * vm,
654 unformat_input_t * input,
655 vlib_cli_command_t * cmd)
657 unformat_input_t _line_input, *line_input = &_line_input;
660 map_main_t *mm = &map_main;
662 memset (&ip4nh, 0, sizeof (ip4nh));
663 memset (&ip6nh, 0, sizeof (ip6nh));
665 /* Get a line of input. */
666 if (!unformat_user (input, unformat_line_input, line_input))
669 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
671 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
672 mm->preresolve_ip4 = ip4nh;
674 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
675 mm->preresolve_ip6 = ip6nh;
677 return clib_error_return (0, "unknown input `%U'",
678 format_unformat_error, input);
680 unformat_free (line_input);
682 map_pre_resolve (&ip4nh, &ip6nh);
688 static clib_error_t *
689 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
690 unformat_input_t * input,
691 vlib_cli_command_t * cmd)
693 unformat_input_t _line_input, *line_input = &_line_input;
694 ip4_address_t icmp_src_address;
695 map_main_t *mm = &map_main;
697 mm->icmp4_src_address.as_u32 = 0;
699 /* Get a line of input. */
700 if (!unformat_user (input, unformat_line_input, line_input))
703 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
706 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
707 mm->icmp4_src_address = icmp_src_address;
709 return clib_error_return (0, "unknown input `%U'",
710 format_unformat_error, input);
712 unformat_free (line_input);
717 static clib_error_t *
718 map_icmp_unreachables_command_fn (vlib_main_t * vm,
719 unformat_input_t * input,
720 vlib_cli_command_t * cmd)
722 unformat_input_t _line_input, *line_input = &_line_input;
723 map_main_t *mm = &map_main;
726 /* Get a line of input. */
727 if (!unformat_user (input, unformat_line_input, line_input))
730 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
733 if (unformat (line_input, "on"))
734 mm->icmp6_enabled = true;
735 else if (unformat (line_input, "off"))
736 mm->icmp6_enabled = false;
738 return clib_error_return (0, "unknown input `%U'",
739 format_unformat_error, input);
741 unformat_free (line_input);
745 return clib_error_return (0, "mandatory argument(s) missing");
750 static clib_error_t *
751 map_fragment_command_fn (vlib_main_t * vm,
752 unformat_input_t * input, vlib_cli_command_t * cmd)
754 unformat_input_t _line_input, *line_input = &_line_input;
755 map_main_t *mm = &map_main;
757 /* Get a line of input. */
758 if (!unformat_user (input, unformat_line_input, line_input))
761 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
763 if (unformat (line_input, "inner"))
764 mm->frag_inner = true;
765 else if (unformat (line_input, "outer"))
766 mm->frag_inner = false;
768 return clib_error_return (0, "unknown input `%U'",
769 format_unformat_error, input);
771 unformat_free (line_input);
776 static clib_error_t *
777 map_fragment_df_command_fn (vlib_main_t * vm,
778 unformat_input_t * input,
779 vlib_cli_command_t * cmd)
781 unformat_input_t _line_input, *line_input = &_line_input;
782 map_main_t *mm = &map_main;
784 /* Get a line of input. */
785 if (!unformat_user (input, unformat_line_input, line_input))
788 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
790 if (unformat (line_input, "on"))
791 mm->frag_ignore_df = true;
792 else if (unformat (line_input, "off"))
793 mm->frag_ignore_df = false;
795 return clib_error_return (0, "unknown input `%U'",
796 format_unformat_error, input);
798 unformat_free (line_input);
803 static clib_error_t *
804 map_traffic_class_command_fn (vlib_main_t * vm,
805 unformat_input_t * input,
806 vlib_cli_command_t * cmd)
808 unformat_input_t _line_input, *line_input = &_line_input;
809 map_main_t *mm = &map_main;
814 /* Get a line of input. */
815 if (!unformat_user (input, unformat_line_input, line_input))
818 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
820 if (unformat (line_input, "copy"))
822 else if (unformat (line_input, "%x", &tc))
825 return clib_error_return (0, "unknown input `%U'",
826 format_unformat_error, input);
828 unformat_free (line_input);
834 format_map_domain (u8 * s, va_list * args)
836 map_domain_t *d = va_arg (*args, map_domain_t *);
837 bool counters = va_arg (*args, int);
838 map_main_t *mm = &map_main;
839 ip6_address_t ip6_prefix;
842 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
844 ip6_prefix = d->ip6_prefix;
847 "[%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",
849 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
850 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
851 format_ip6_address, &d->ip6_src, d->ip6_src_len,
852 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
853 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
857 map_domain_counter_lock (mm);
859 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
860 d - mm->domains, &v);
861 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
862 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
863 d - mm->domains, &v);
864 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
865 map_domain_counter_unlock (mm);
867 s = format (s, "\n");
873 for (i = 0; i < (0x1 << d->psid_length); i++)
876 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
879 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
887 format_map_ip4_reass (u8 * s, va_list * args)
889 map_main_t *mm = &map_main;
890 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
891 map_ip4_reass_key_t *k = &r->key;
892 f64 now = vlib_time_now (mm->vlib_main);
893 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
894 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
896 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
897 format_ip4_address, &k->src.as_u8, format_ip4_address,
898 &k->dst.as_u8, k->protocol,
899 clib_net_to_host_u16 (k->fragment_id),
900 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
905 format_map_ip6_reass (u8 * s, va_list * args)
907 map_main_t *mm = &map_main;
908 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
909 map_ip6_reass_key_t *k = &r->key;
910 f64 now = vlib_time_now (mm->vlib_main);
911 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
912 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
914 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
915 format_ip6_address, &k->src.as_u8, format_ip6_address,
916 &k->dst.as_u8, k->protocol,
917 clib_net_to_host_u32 (k->fragment_id), dt);
921 static clib_error_t *
922 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
923 vlib_cli_command_t * cmd)
925 unformat_input_t _line_input, *line_input = &_line_input;
926 map_main_t *mm = &map_main;
928 bool counters = false;
929 u32 map_domain_index = ~0;
931 /* Get a line of input. */
932 if (!unformat_user (input, unformat_line_input, line_input))
935 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
937 if (unformat (line_input, "counters"))
939 else if (unformat (line_input, "index %d", &map_domain_index))
942 return clib_error_return (0, "unknown input `%U'",
943 format_unformat_error, input);
945 unformat_free (line_input);
947 if (pool_elts (mm->domains) == 0)
948 vlib_cli_output (vm, "No MAP domains are configured...");
950 if (map_domain_index == ~0)
953 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
958 if (pool_is_free_index (mm->domains, map_domain_index))
960 return clib_error_return (0, "MAP domain does not exists %d",
964 d = pool_elt_at_index (mm->domains, map_domain_index);
965 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
971 static clib_error_t *
972 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
973 vlib_cli_command_t * cmd)
975 map_main_t *mm = &map_main;
980 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
983 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
989 map_error_counter_get (u32 node_index, map_error_t map_error)
991 vlib_main_t *vm = vlib_get_main ();
992 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
993 vlib_error_main_t *em = &vm->error_main;
994 vlib_error_t e = error_node->errors[map_error];
995 vlib_node_t *n = vlib_get_node (vm, node_index);
998 ci = vlib_error_get_code (e);
999 ASSERT (ci < n->n_errors);
1000 ci += n->error_heap_index;
1002 return (em->counters[ci]);
1005 static clib_error_t *
1006 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1007 vlib_cli_command_t * cmd)
1009 map_main_t *mm = &map_main;
1011 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1012 if (pool_elts (mm->domains) == 0)
1013 vlib_cli_output (vm, "No MAP domains are configured...");
1016 pool_foreach(d, mm->domains, ({
1018 rulecount+= 0x1 << d->psid_length;
1019 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1021 domains += sizeof(*d);
1026 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1027 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1028 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1029 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1031 #if MAP_SKIP_IP6_LOOKUP
1032 vlib_cli_output (vm,
1033 "MAP pre-resolve: IP6 next-hop: %U (%u), IP4 next-hop: %U (%u)\n",
1034 format_ip6_address, &mm->preresolve_ip6, mm->adj6_index,
1035 format_ip4_address, &mm->preresolve_ip4, mm->adj4_index);
1039 vlib_cli_output (vm, "MAP traffic-class: copy");
1041 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1043 vlib_cli_output (vm,
1044 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1045 mm->sec_check ? "enabled" : "disabled",
1046 mm->sec_check_frag ? "enabled" : "disabled");
1048 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1049 format_ip4_address, &mm->icmp4_src_address);
1050 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1051 mm->icmp6_enabled ? "enabled" : "disabled");
1052 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1053 mm->frag_inner ? "enabled" : "disabled");
1054 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1055 mm->frag_ignore_df ? "enabled" : "disabled");
1060 vlib_combined_counter_main_t *cm = mm->domain_counters;
1061 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1062 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1066 memset (total_pkts, 0, sizeof (total_pkts));
1067 memset (total_bytes, 0, sizeof (total_bytes));
1069 map_domain_counter_lock (mm);
1070 vec_foreach (cm, mm->domain_counters)
1072 which = cm - mm->domain_counters;
1074 for (i = 0; i < vec_len (cm->maxi); i++)
1076 vlib_get_combined_counter (cm, i, &v);
1077 total_pkts[which] += v.packets;
1078 total_bytes[which] += v.bytes;
1081 map_domain_counter_unlock (mm);
1083 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1084 total_pkts[MAP_DOMAIN_COUNTER_TX],
1085 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1086 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1087 total_pkts[MAP_DOMAIN_COUNTER_RX],
1088 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1090 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1091 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1096 static clib_error_t *
1097 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1098 vlib_cli_command_t * cmd)
1100 unformat_input_t _line_input, *line_input = &_line_input;
1102 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1104 u64 buffers = ~(0ull);
1105 u8 ip4 = 0, ip6 = 0;
1107 if (!unformat_user (input, unformat_line_input, line_input))
1110 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1112 if (unformat (line_input, "lifetime %u", &lifetime))
1114 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1116 else if (unformat (line_input, "pool-size %u", &pool_size))
1118 else if (unformat (line_input, "buffers %llu", &buffers))
1120 else if (unformat (line_input, "ip4"))
1122 else if (unformat (line_input, "ip6"))
1126 unformat_free (line_input);
1127 return clib_error_return (0, "invalid input");
1130 unformat_free (line_input);
1133 return clib_error_return (0, "must specify ip4 and/or ip6");
1137 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1138 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1139 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1140 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1141 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1142 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1143 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1144 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1145 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1146 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1147 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1148 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1149 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1154 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1155 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1156 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1157 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1158 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1159 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1160 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1161 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1162 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1163 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1164 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1165 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1166 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1171 u32 reass = 0, packets = 0;
1172 if (pool_size != ~0)
1174 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1176 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1180 vlib_cli_output (vm,
1181 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1185 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1187 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1189 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1193 vlib_cli_output (vm,
1194 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1200 if (map_ip4_reass_conf_lifetime (lifetime))
1201 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1203 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1205 if (buffers != ~(0ull))
1207 if (map_ip4_reass_conf_buffers (buffers))
1208 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1210 vlib_cli_output (vm, "Setting ip4-reass buffers");
1213 if (map_main.ip4_reass_conf_buffers >
1214 map_main.ip4_reass_conf_pool_size *
1215 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1217 vlib_cli_output (vm,
1218 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1224 u32 reass = 0, packets = 0;
1225 if (pool_size != ~0)
1227 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1229 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1233 vlib_cli_output (vm,
1234 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1238 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1240 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1242 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1246 vlib_cli_output (vm,
1247 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1253 if (map_ip6_reass_conf_lifetime (lifetime))
1254 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1256 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1258 if (buffers != ~(0ull))
1260 if (map_ip6_reass_conf_buffers (buffers))
1261 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1263 vlib_cli_output (vm, "Setting ip6-reass buffers");
1266 if (map_main.ip6_reass_conf_buffers >
1267 map_main.ip6_reass_conf_pool_size *
1268 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1270 vlib_cli_output (vm,
1271 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1280 * packet trace format function
1283 format_map_trace (u8 * s, va_list * args)
1285 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1286 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1287 map_trace_t *t = va_arg (*args, map_trace_t *);
1288 u32 map_domain_index = t->map_domain_index;
1292 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1293 clib_net_to_host_u16 (port));
1298 static_always_inline map_ip4_reass_t *
1299 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1301 map_main_t *mm = &map_main;
1302 u32 ri = mm->ip4_reass_hash_table[bucket];
1303 while (ri != MAP_REASS_INDEX_NONE)
1305 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1306 if (r->key.as_u64[0] == k->as_u64[0] &&
1307 r->key.as_u64[1] == k->as_u64[1] &&
1308 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1312 ri = r->bucket_next;
1317 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1320 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1322 map_main_t *mm = &map_main;
1323 map_ip4_reass_get_fragments (r, pi_to_drop);
1325 // Unlink in hash bucket
1326 map_ip4_reass_t *r2 = NULL;
1327 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1328 while (r2i != map_ip4_reass_pool_index (r))
1330 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1331 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1332 r2i = r2->bucket_next;
1336 r2->bucket_next = r->bucket_next;
1340 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1344 if (r->fifo_next == map_ip4_reass_pool_index (r))
1346 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1350 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1351 mm->ip4_reass_fifo_last = r->fifo_prev;
1352 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1354 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1358 pool_put (mm->ip4_reass_pool, r);
1359 mm->ip4_reass_allocated--;
1363 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1364 u8 protocol, u32 ** pi_to_drop)
1367 map_main_t *mm = &map_main;
1368 map_ip4_reass_key_t k = {.src.data_u32 = src,
1369 .dst.data_u32 = dst,
1370 .fragment_id = fragment_id,
1371 .protocol = protocol
1375 h = crc_u32 (k.as_u32[0], h);
1376 h = crc_u32 (k.as_u32[1], h);
1377 h = crc_u32 (k.as_u32[2], h);
1378 h = crc_u32 (k.as_u32[3], h);
1379 h = h >> (32 - mm->ip4_reass_ht_log2len);
1381 f64 now = vlib_time_now (mm->vlib_main);
1383 //Cache garbage collection
1384 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1386 map_ip4_reass_t *last =
1387 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1388 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1389 map_ip4_reass_free (last, pi_to_drop);
1394 if ((r = map_ip4_reass_lookup (&k, h, now)))
1397 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1400 pool_get (mm->ip4_reass_pool, r);
1401 mm->ip4_reass_allocated++;
1403 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1404 r->fragments[i] = ~0;
1406 u32 ri = map_ip4_reass_pool_index (r);
1408 //Link in new bucket
1410 r->bucket_next = mm->ip4_reass_hash_table[h];
1411 mm->ip4_reass_hash_table[h] = ri;
1414 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1417 pool_elt_at_index (mm->ip4_reass_pool,
1418 mm->ip4_reass_fifo_last)->fifo_next;
1419 r->fifo_prev = mm->ip4_reass_fifo_last;
1420 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1421 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1425 r->fifo_next = r->fifo_prev = ri;
1426 mm->ip4_reass_fifo_last = ri;
1433 #ifdef MAP_IP4_REASS_COUNT_BYTES
1434 r->expected_total = 0xffff;
1442 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1444 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1448 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1449 if (r->fragments[i] == ~0)
1451 r->fragments[i] = pi;
1452 map_main.ip4_reass_buffered_counter++;
1458 static_always_inline map_ip6_reass_t *
1459 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1461 map_main_t *mm = &map_main;
1462 u32 ri = mm->ip6_reass_hash_table[bucket];
1463 while (ri != MAP_REASS_INDEX_NONE)
1465 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1466 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1467 r->key.as_u64[0] == k->as_u64[0] &&
1468 r->key.as_u64[1] == k->as_u64[1] &&
1469 r->key.as_u64[2] == k->as_u64[2] &&
1470 r->key.as_u64[3] == k->as_u64[3] &&
1471 r->key.as_u64[4] == k->as_u64[4])
1473 ri = r->bucket_next;
1478 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1481 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1483 map_main_t *mm = &map_main;
1485 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1486 if (r->fragments[i].pi != ~0)
1488 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1489 r->fragments[i].pi = ~0;
1490 map_main.ip6_reass_buffered_counter--;
1493 // Unlink in hash bucket
1494 map_ip6_reass_t *r2 = NULL;
1495 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1496 while (r2i != map_ip6_reass_pool_index (r))
1498 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1499 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1500 r2i = r2->bucket_next;
1504 r2->bucket_next = r->bucket_next;
1508 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1512 if (r->fifo_next == map_ip6_reass_pool_index (r))
1514 //Single element in the list, list is now empty
1515 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1519 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1520 mm->ip6_reass_fifo_last = r->fifo_prev;
1521 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1523 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1527 // Free from pool if necessary
1528 pool_put (mm->ip6_reass_pool, r);
1529 mm->ip6_reass_allocated--;
1533 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1534 u8 protocol, u32 ** pi_to_drop)
1537 map_main_t *mm = &map_main;
1538 map_ip6_reass_key_t k = {
1541 .fragment_id = fragment_id,
1542 .protocol = protocol
1547 for (i = 0; i < 10; i++)
1548 h = crc_u32 (k.as_u32[i], h);
1549 h = h >> (32 - mm->ip6_reass_ht_log2len);
1551 f64 now = vlib_time_now (mm->vlib_main);
1553 //Cache garbage collection
1554 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1556 map_ip6_reass_t *last =
1557 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1558 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1559 map_ip6_reass_free (last, pi_to_drop);
1564 if ((r = map_ip6_reass_lookup (&k, h, now)))
1567 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1570 pool_get (mm->ip6_reass_pool, r);
1571 mm->ip6_reass_allocated++;
1572 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1574 r->fragments[i].pi = ~0;
1575 r->fragments[i].next_data_len = 0;
1576 r->fragments[i].next_data_offset = 0;
1579 u32 ri = map_ip6_reass_pool_index (r);
1581 //Link in new bucket
1583 r->bucket_next = mm->ip6_reass_hash_table[h];
1584 mm->ip6_reass_hash_table[h] = ri;
1587 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1590 pool_elt_at_index (mm->ip6_reass_pool,
1591 mm->ip6_reass_fifo_last)->fifo_next;
1592 r->fifo_prev = mm->ip6_reass_fifo_last;
1593 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1594 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1598 r->fifo_next = r->fifo_prev = ri;
1599 mm->ip6_reass_fifo_last = ri;
1605 r->ip4_header.ip_version_and_header_length = 0;
1606 #ifdef MAP_IP6_REASS_COUNT_BYTES
1607 r->expected_total = 0xffff;
1614 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1615 u16 data_offset, u16 next_data_offset,
1616 u8 * data_start, u16 data_len)
1618 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1619 u16 copied_len = (data_len > 20) ? 20 : data_len;
1621 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1624 //Lookup for fragments for the current buffer
1625 //and the one before that
1627 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1629 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1631 prev_f = &r->fragments[i]; // This is buffer for previous packet
1633 else if (r->fragments[i].next_data_offset == next_data_offset)
1635 f = &r->fragments[i]; // This is a buffer for the current packet
1637 else if (r->fragments[i].next_data_offset == 0)
1640 f = &r->fragments[i];
1641 else if (prev_f == NULL)
1642 prev_f = &r->fragments[i];
1646 if (!f || f->pi != ~0)
1654 clib_memcpy (prev_f->next_data, data_start, copied_len);
1655 prev_f->next_data_len = copied_len;
1656 prev_f->next_data_offset = data_offset;
1660 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1663 if (r->ip4_header.ip_version_and_header_length == 0)
1664 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1669 f->next_data_offset = next_data_offset;
1671 map_main.ip6_reass_buffered_counter++;
1677 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1679 map_main_t *mm = &map_main;
1682 if (dropped_packets)
1683 *dropped_packets = mm->ip4_reass_buffered_counter;
1685 *trashed_reass = mm->ip4_reass_allocated;
1686 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1688 u16 ri = mm->ip4_reass_fifo_last;
1691 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1692 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1693 if (r->fragments[i] != ~0)
1694 map_ip4_drop_pi (r->fragments[i]);
1697 pool_put (mm->ip4_reass_pool, r);
1699 while (ri != mm->ip4_reass_fifo_last);
1702 vec_free (mm->ip4_reass_hash_table);
1703 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1704 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1705 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1706 pool_free (mm->ip4_reass_pool);
1707 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1709 mm->ip4_reass_allocated = 0;
1710 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1711 mm->ip4_reass_buffered_counter = 0;
1715 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1717 u32 desired_size = (u32) (pool_size * ht_ratio);
1719 for (i = 1; i < 31; i++)
1720 if ((1 << i) >= desired_size)
1726 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1727 u32 * dropped_packets)
1729 map_main_t *mm = &map_main;
1730 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1733 map_ip4_reass_lock ();
1734 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1735 mm->ip4_reass_ht_log2len =
1736 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1737 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1738 map_ip4_reass_unlock ();
1743 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1744 u32 * dropped_packets)
1746 map_main_t *mm = &map_main;
1747 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1750 map_ip4_reass_lock ();
1751 mm->ip4_reass_conf_pool_size = pool_size;
1752 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1753 map_ip4_reass_unlock ();
1758 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1760 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1765 map_ip4_reass_conf_buffers (u32 buffers)
1767 map_main.ip4_reass_conf_buffers = buffers;
1772 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1774 map_main_t *mm = &map_main;
1775 if (dropped_packets)
1776 *dropped_packets = mm->ip6_reass_buffered_counter;
1778 *trashed_reass = mm->ip6_reass_allocated;
1780 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1782 u16 ri = mm->ip6_reass_fifo_last;
1785 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1786 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1787 if (r->fragments[i].pi != ~0)
1788 map_ip6_drop_pi (r->fragments[i].pi);
1791 pool_put (mm->ip6_reass_pool, r);
1793 while (ri != mm->ip6_reass_fifo_last);
1794 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1797 vec_free (mm->ip6_reass_hash_table);
1798 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1799 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1800 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1801 pool_free (mm->ip6_reass_pool);
1802 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1804 mm->ip6_reass_allocated = 0;
1805 mm->ip6_reass_buffered_counter = 0;
1809 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1810 u32 * dropped_packets)
1812 map_main_t *mm = &map_main;
1813 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1816 map_ip6_reass_lock ();
1817 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1818 mm->ip6_reass_ht_log2len =
1819 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1820 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1821 map_ip6_reass_unlock ();
1826 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1827 u32 * dropped_packets)
1829 map_main_t *mm = &map_main;
1830 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1833 map_ip6_reass_lock ();
1834 mm->ip6_reass_conf_pool_size = pool_size;
1835 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1836 map_ip6_reass_unlock ();
1841 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1843 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1848 map_ip6_reass_conf_buffers (u32 buffers)
1850 map_main.ip6_reass_conf_buffers = buffers;
1855 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1856 .path = "map params reassembly",
1857 .short_help = "[ip4 | ip6] [lifetime <lifetime-ms>] [pool-size <pool-size>] [buffers <buffers>] [ht-ratio <ht-ratio>]",
1858 .function = map_params_reass_command_fn,
1863 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1864 .path = "map params traffic-class",
1866 "traffic-class {0x0-0xff | copy}",
1867 .function = map_traffic_class_command_fn,
1872 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1873 .path = "map params pre-resolve",
1875 "pre-resolve {ip4-nh <address>} | {ip6-nh <address>}",
1876 .function = map_pre_resolve_command_fn,
1881 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1882 .path = "map params security-check",
1884 "security-check on|off",
1885 .function = map_security_check_command_fn,
1890 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
1891 .path = "map params icmp source-address",
1892 .short_help = "source-address <ip4-address>",
1893 .function = map_icmp_relay_source_address_command_fn,
1898 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
1899 .path = "map params icmp6 unreachables",
1900 .short_help = "unreachables {on|off}",
1901 .function = map_icmp_unreachables_command_fn,
1906 VLIB_CLI_COMMAND(map_fragment_command, static) = {
1907 .path = "map params fragment",
1908 .short_help = "[inner|outer] [ignore-df [on|off]]",
1909 .function = map_fragment_command_fn,
1914 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
1915 .path = "map params fragment ignore-df",
1916 .short_help = "on|off",
1917 .function = map_fragment_df_command_fn,
1922 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
1923 .path = "map params security-check fragments",
1926 .function = map_security_check_frag_command_fn,
1931 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
1932 .path = "map add domain",
1934 "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> ip6-src <ip6-pfx> "
1935 "ea-bits-len <n> psid-offset <n> psid-len <n> [map-t] [mtu <mtu>]",
1936 .function = map_add_domain_command_fn,
1941 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
1942 .path = "map add rule",
1944 "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
1945 .function = map_add_rule_command_fn,
1950 VLIB_CLI_COMMAND(map_del_command, static) = {
1951 .path = "map del domain",
1953 "map del domain index <domain>",
1954 .function = map_del_domain_command_fn,
1959 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
1960 .path = "show map domain",
1961 .function = show_map_domain_command_fn,
1966 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
1967 .path = "show map stats",
1968 .function = show_map_stats_command_fn,
1973 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
1974 .path = "show map fragments",
1975 .function = show_map_fragments_command_fn,
1983 map_init (vlib_main_t * vm)
1985 map_main_t *mm = &map_main;
1986 mm->vnet_main = vnet_get_main ();
1989 #ifdef MAP_SKIP_IP6_LOOKUP
1990 memset (&mm->preresolve_ip4, 0, sizeof (mm->preresolve_ip4));
1991 memset (&mm->preresolve_ip6, 0, sizeof (mm->preresolve_ip6));
2000 /* Inbound security check */
2001 mm->sec_check = true;
2002 mm->sec_check_frag = false;
2004 /* ICMP6 Type 1, Code 5 for security check failure */
2005 mm->icmp6_enabled = false;
2007 /* Inner or outer fragmentation */
2008 mm->frag_inner = false;
2009 mm->frag_ignore_df = false;
2011 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2012 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2013 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2015 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2016 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2018 /* IP4 virtual reassembly */
2019 mm->ip4_reass_hash_table = 0;
2020 mm->ip4_reass_pool = 0;
2021 mm->ip4_reass_lock =
2022 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2023 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2024 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2025 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2026 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2027 mm->ip4_reass_ht_log2len =
2028 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2029 mm->ip4_reass_conf_pool_size);
2030 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2031 map_ip4_reass_reinit (NULL, NULL);
2033 /* IP6 virtual reassembly */
2034 mm->ip6_reass_hash_table = 0;
2035 mm->ip6_reass_pool = 0;
2036 mm->ip6_reass_lock =
2037 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2038 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2039 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2040 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2041 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2042 mm->ip6_reass_ht_log2len =
2043 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2044 mm->ip6_reass_conf_pool_size);
2045 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2046 map_ip6_reass_reinit (NULL, NULL);
2051 VLIB_INIT_FUNCTION (map_init);
2054 * fd.io coding-style-patch-verification: ON
2057 * eval: (c-set-style "gnu")