6823a46e24aede6cccfc932bed2adbcb53acc2d6
[vpp.git] / src / vnet / map / map.c
1 /*
2  * map.c : MAP support
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/fib/ip6_fib.h>
20 #include <vnet/adj/adj.h>
21 #include <vnet/map/map_dpo.h>
22
23 #include "map.h"
24
25 #ifdef __SSE4_2__
26 static inline u32
27 crc_u32 (u32 data, u32 value)
28 {
29   __asm__ volatile ("crc32l %[data], %[value];":[value] "+r" (value):[data]
30                     "rm" (data));
31   return value;
32 }
33 #else
34 #include <vppinfra/xxhash.h>
35
36 static inline u32
37 crc_u32 (u32 data, u32 value)
38 {
39   u64 tmp = ((u64) data << 32) | (u64) value;
40   return (u32) clib_xxhash (tmp);
41 }
42 #endif
43
44
45 /*
46  * This code supports the following MAP modes:
47  *
48  * Algorithmic Shared IPv4 address (ea_bits_len > 0):
49  *   ea_bits_len + ip4_prefix > 32
50  *   psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
51  * Algorithmic Full IPv4 address (ea_bits_len > 0):
52  *   ea_bits_len + ip4_prefix = 32
53  *   psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
54  * Algorithmic IPv4 prefix (ea_bits_len > 0):
55  *   ea_bits_len + ip4_prefix < 32
56  *   psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
57  *
58  * Independent Shared IPv4 address (ea_bits_len = 0):
59  *   ip4_prefix = 32
60  *   psid_length > 0
61  *   Rule IPv6 address = 128, Rule PSID Set
62  * Independent Full IPv4 address (ea_bits_len = 0):
63  *   ip4_prefix = 32
64  *   psid_length = 0, ip6_prefix = 128
65  * Independent IPv4 prefix (ea_bits_len = 0):
66  *   ip4_prefix < 32
67  *   psid_length = 0, ip6_prefix = 128
68  *
69  */
70
71 /*
72  * This code supports MAP-T:
73  *
74  * With DMR prefix length equal to 96.
75  *
76  */
77
78
79 i32
80 ip4_get_port (ip4_header_t * ip, map_dir_e dir, u16 buffer_len)
81 {
82   //TODO: use buffer length
83   if (ip->ip_version_and_header_length != 0x45 ||
84       ip4_get_fragment_offset (ip))
85     return -1;
86
87   if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
88                     (ip->protocol == IP_PROTOCOL_UDP)))
89     {
90       udp_header_t *udp = (void *) (ip + 1);
91       return (dir == MAP_SENDER) ? udp->src_port : udp->dst_port;
92     }
93   else if (ip->protocol == IP_PROTOCOL_ICMP)
94     {
95       icmp46_header_t *icmp = (void *) (ip + 1);
96       if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
97         {
98           return *((u16 *) (icmp + 1));
99         }
100       else if (clib_net_to_host_u16 (ip->length) >= 64)
101         {
102           ip = (ip4_header_t *) (icmp + 2);
103           if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
104                             (ip->protocol == IP_PROTOCOL_UDP)))
105             {
106               udp_header_t *udp = (void *) (ip + 1);
107               return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
108             }
109           else if (ip->protocol == IP_PROTOCOL_ICMP)
110             {
111               icmp46_header_t *icmp = (void *) (ip + 1);
112               if (icmp->type == ICMP4_echo_request ||
113                   icmp->type == ICMP4_echo_reply)
114                 {
115                   return *((u16 *) (icmp + 1));
116                 }
117             }
118         }
119     }
120   return -1;
121 }
122
123 i32
124 ip6_get_port (ip6_header_t * ip6, map_dir_e dir, u16 buffer_len)
125 {
126   u8 l4_protocol;
127   u16 l4_offset;
128   u16 frag_offset;
129   u8 *l4;
130
131   if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
132     return -1;
133
134   //TODO: Use buffer length
135
136   if (frag_offset &&
137       ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
138                             u8_ptr_add (ip6, frag_offset))))
139     return -1;                  //Can't deal with non-first fragment for now
140
141   l4 = u8_ptr_add (ip6, l4_offset);
142   if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
143     {
144       return (dir ==
145               MAP_SENDER) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t
146                                                                   *)
147                                                                  (l4))->dst_port;
148     }
149   else if (l4_protocol == IP_PROTOCOL_ICMP6)
150     {
151       icmp46_header_t *icmp = (icmp46_header_t *) (l4);
152       if (icmp->type == ICMP6_echo_request)
153         {
154           return (dir == MAP_SENDER) ? ((u16 *) (icmp))[2] : -1;
155         }
156       else if (icmp->type == ICMP6_echo_reply)
157         {
158           return (dir == MAP_SENDER) ? -1 : ((u16 *) (icmp))[2];
159         }
160     }
161   return -1;
162 }
163
164
165 int
166 map_create_domain (ip4_address_t * ip4_prefix,
167                    u8 ip4_prefix_len,
168                    ip6_address_t * ip6_prefix,
169                    u8 ip6_prefix_len,
170                    ip6_address_t * ip6_src,
171                    u8 ip6_src_len,
172                    u8 ea_bits_len,
173                    u8 psid_offset,
174                    u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
175 {
176   u8 suffix_len, suffix_shift;
177   map_main_t *mm = &map_main;
178   dpo_id_t dpo_v4 = DPO_INVALID;
179   dpo_id_t dpo_v6 = DPO_INVALID;
180   fib_node_index_t fei;
181   map_domain_t *d;
182
183   /* Sanity check on the src prefix length */
184   if (flags & MAP_DOMAIN_TRANSLATION)
185     {
186       if (ip6_src_len != 96)
187         {
188           clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
189           return -1;
190         }
191     }
192   else
193     {
194       if (ip6_src_len != 128)
195         {
196           clib_warning
197             ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
198              "be 128).");
199           return -1;
200         }
201     }
202
203   /* How many, and which bits to grab from the IPv4 DA */
204   if (ip4_prefix_len + ea_bits_len < 32)
205     {
206       flags |= MAP_DOMAIN_PREFIX;
207       suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
208       suffix_len = ea_bits_len;
209     }
210   else
211     {
212       suffix_shift = 0;
213       suffix_len = 32 - ip4_prefix_len;
214     }
215
216   /* EA bits must be within the first 64 bits */
217   if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
218                           ip6_prefix_len + suffix_len + psid_length > 64))
219     {
220       clib_warning
221         ("Embedded Address bits must be within the first 64 bits of "
222          "the IPv6 prefix");
223       return -1;
224     }
225
226   /* Get domain index */
227   pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
228   memset (d, 0, sizeof (*d));
229   *map_domain_index = d - mm->domains;
230
231   /* Init domain struct */
232   d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
233   d->ip4_prefix_len = ip4_prefix_len;
234   d->ip6_prefix = *ip6_prefix;
235   d->ip6_prefix_len = ip6_prefix_len;
236   d->ip6_src = *ip6_src;
237   d->ip6_src_len = ip6_src_len;
238   d->ea_bits_len = ea_bits_len;
239   d->psid_offset = psid_offset;
240   d->psid_length = psid_length;
241   d->mtu = mtu;
242   d->flags = flags;
243   d->suffix_shift = suffix_shift;
244   d->suffix_mask = (1 << suffix_len) - 1;
245
246   d->psid_shift = 16 - psid_length - psid_offset;
247   d->psid_mask = (1 << d->psid_length) - 1;
248   d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
249
250   /* MAP data-plane object */
251   if (d->flags & MAP_DOMAIN_TRANSLATION)
252     map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
253   else
254     map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
255
256   /* Create ip4 route */
257   fib_prefix_t pfx = {
258     .fp_proto = FIB_PROTOCOL_IP4,
259     .fp_len = d->ip4_prefix_len,
260     .fp_addr = {
261                 .ip4 = d->ip4_prefix,
262                 }
263     ,
264   };
265   fib_table_entry_special_dpo_add (0, &pfx,
266                                    FIB_SOURCE_MAP,
267                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
268   dpo_reset (&dpo_v4);
269
270   /*
271    * Multiple MAP domains may share same source IPv6 TEP.
272    * In this case the route will exist and be MAP sourced.
273    * Find the adj (if any) already contributed and modify it
274    */
275   fib_prefix_t pfx6 = {
276     .fp_proto = FIB_PROTOCOL_IP6,
277     .fp_len = d->ip6_src_len,
278     .fp_addr = {
279                 .ip6 = d->ip6_src,
280                 }
281     ,
282   };
283   fei = fib_table_lookup_exact_match (0, &pfx6);
284
285   if (FIB_NODE_INDEX_INVALID != fei)
286     {
287       dpo_id_t dpo = DPO_INVALID;
288
289       if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_MAP, &dpo))
290         {
291           /*
292            * modify the existing MAP to indicate it's shared
293            * skip to route add.
294            */
295           const dpo_id_t *md_dpo;
296           map_dpo_t *md;
297
298           ASSERT (DPO_LOAD_BALANCE == dpo.dpoi_type);
299
300           md_dpo = load_balance_get_bucket (dpo.dpoi_index, 0);
301           md = map_dpo_get (md_dpo->dpoi_index);
302
303           md->md_domain = ~0;
304           dpo_copy (&dpo_v6, md_dpo);
305           dpo_reset (&dpo);
306
307           goto route_add;
308         }
309     }
310
311   if (d->flags & MAP_DOMAIN_TRANSLATION)
312     map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
313   else
314     map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
315
316 route_add:
317   /*
318    * Create ip6 route. This is a reference counted add. If the prefix
319    * already exists and is MAP sourced, it is now MAP source n+1 times
320    * and will need to be removed n+1 times.
321    */
322   fib_table_entry_special_dpo_add (0, &pfx6,
323                                    FIB_SOURCE_MAP,
324                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
325   dpo_reset (&dpo_v6);
326
327   /* Validate packet/byte counters */
328   map_domain_counter_lock (mm);
329   int i;
330   for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
331     {
332       vlib_validate_simple_counter (&mm->simple_domain_counters[i],
333                                     *map_domain_index);
334       vlib_zero_simple_counter (&mm->simple_domain_counters[i],
335                                 *map_domain_index);
336     }
337   for (i = 0; i < vec_len (mm->domain_counters); i++)
338     {
339       vlib_validate_combined_counter (&mm->domain_counters[i],
340                                       *map_domain_index);
341       vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
342     }
343   map_domain_counter_unlock (mm);
344
345   return 0;
346 }
347
348 /*
349  * map_delete_domain
350  */
351 int
352 map_delete_domain (u32 map_domain_index)
353 {
354   map_main_t *mm = &map_main;
355   map_domain_t *d;
356
357   if (pool_is_free_index (mm->domains, map_domain_index))
358     {
359       clib_warning ("MAP domain delete: domain does not exist: %d",
360                     map_domain_index);
361       return -1;
362     }
363
364   d = pool_elt_at_index (mm->domains, map_domain_index);
365
366   fib_prefix_t pfx = {
367     .fp_proto = FIB_PROTOCOL_IP4,
368     .fp_len = d->ip4_prefix_len,
369     .fp_addr = {
370                 .ip4 = d->ip4_prefix,
371                 }
372     ,
373   };
374   fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
375
376   fib_prefix_t pfx6 = {
377     .fp_proto = FIB_PROTOCOL_IP6,
378     .fp_len = d->ip6_src_len,
379     .fp_addr = {
380                 .ip6 = d->ip6_src,
381                 }
382     ,
383   };
384   fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
385
386   /* Deleting rules */
387   if (d->rules)
388     clib_mem_free (d->rules);
389
390   pool_put (mm->domains, d);
391
392   return 0;
393 }
394
395 int
396 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
397                   u8 is_add)
398 {
399   map_domain_t *d;
400   map_main_t *mm = &map_main;
401
402   if (pool_is_free_index (mm->domains, map_domain_index))
403     {
404       clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
405       return -1;
406     }
407   d = pool_elt_at_index (mm->domains, map_domain_index);
408
409   /* Rules are only used in 1:1 independent case */
410   if (d->ea_bits_len > 0)
411     return (-1);
412
413   if (!d->rules)
414     {
415       u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
416       d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
417       if (!d->rules)
418         return -1;
419       memset (d->rules, 0, l);
420     }
421
422   if (psid >= (0x1 << d->psid_length))
423     {
424       clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
425                     0x1 << d->psid_length);
426       return -1;
427     }
428
429   if (is_add)
430     {
431       d->rules[psid] = *tep;
432     }
433   else
434     {
435       memset (&d->rules[psid], 0, sizeof (ip6_address_t));
436     }
437   return 0;
438 }
439
440 #ifdef MAP_SKIP_IP6_LOOKUP
441 /**
442  * Pre-resolvd per-protocol global next-hops
443  */
444 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
445
446 static void
447 map_pre_resolve_init (map_main_pre_resolved_t * pr)
448 {
449   pr->fei = FIB_NODE_INDEX_INVALID;
450   fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
451 }
452
453 static u8 *
454 format_map_pre_resolve (u8 * s, va_list ap)
455 {
456   map_main_pre_resolved_t *pr = va_arg (ap, map_main_pre_resolved_t *);
457
458   if (FIB_NODE_INDEX_INVALID != pr->fei)
459     {
460       fib_prefix_t pfx;
461
462       fib_entry_get_prefix (pr->fei, &pfx);
463
464       return (format (s, "%U (%u)",
465                       format_ip46_address, &pfx.fp_addr, IP46_TYPE_ANY,
466                       pr->dpo.dpoi_index));
467     }
468   else
469     {
470       return (format (s, "un-set"));
471     }
472 }
473
474
475 /**
476  * Function definition to inform the FIB node that its last lock has gone.
477  */
478 static void
479 map_last_lock_gone (fib_node_t * node)
480 {
481   /*
482    * The MAP is a root of the graph. As such
483    * it never has children and thus is never locked.
484    */
485   ASSERT (0);
486 }
487
488 static map_main_pre_resolved_t *
489 map_from_fib_node (fib_node_t * node)
490 {
491 #if (CLIB_DEBUG > 0)
492   ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
493 #endif
494   return ((map_main_pre_resolved_t *)
495           (((char *) node) -
496            STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
497 }
498
499 static void
500 map_stack (map_main_pre_resolved_t * pr)
501 {
502   const dpo_id_t *dpo;
503
504   dpo = fib_entry_contribute_ip_forwarding (pr->fei);
505
506   dpo_copy (&pr->dpo, dpo);
507 }
508
509 /**
510  * Function definition to backwalk a FIB node
511  */
512 static fib_node_back_walk_rc_t
513 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
514 {
515   map_stack (map_from_fib_node (node));
516
517   return (FIB_NODE_BACK_WALK_CONTINUE);
518 }
519
520 /**
521  * Function definition to get a FIB node from its index
522  */
523 static fib_node_t *
524 map_fib_node_get (fib_node_index_t index)
525 {
526   return (&pre_resolved[index].node);
527 }
528
529 /*
530  * Virtual function table registered by MPLS GRE tunnels
531  * for participation in the FIB object graph.
532  */
533 const static fib_node_vft_t map_vft = {
534   .fnv_get = map_fib_node_get,
535   .fnv_last_lock = map_last_lock_gone,
536   .fnv_back_walk = map_back_walk,
537 };
538
539 static void
540 map_fib_resolve (map_main_pre_resolved_t * pr,
541                  fib_protocol_t proto, u8 len, const ip46_address_t * addr)
542 {
543   fib_prefix_t pfx = {
544     .fp_proto = proto,
545     .fp_len = len,
546     .fp_addr = *addr,
547   };
548
549   pr->fei = fib_table_entry_special_add (0,     // default fib
550                                          &pfx,
551                                          FIB_SOURCE_RR,
552                                          FIB_ENTRY_FLAG_NONE,
553                                          ADJ_INDEX_INVALID);
554   pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
555   map_stack (pr);
556 }
557
558 static void
559 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6)
560 {
561   if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
562     {
563       ip46_address_t addr = {
564         .ip6 = *ip6,
565       };
566       map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
567                        FIB_PROTOCOL_IP6, 128, &addr);
568     }
569   if (ip4 && (ip4->as_u32 != 0))
570     {
571       ip46_address_t addr = {
572         .ip4 = *ip4,
573       };
574       map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
575                        FIB_PROTOCOL_IP4, 32, &addr);
576     }
577 }
578 #endif
579
580 static clib_error_t *
581 map_security_check_command_fn (vlib_main_t * vm,
582                                unformat_input_t * input,
583                                vlib_cli_command_t * cmd)
584 {
585   unformat_input_t _line_input, *line_input = &_line_input;
586   map_main_t *mm = &map_main;
587   clib_error_t *error = NULL;
588
589   /* Get a line of input. */
590   if (!unformat_user (input, unformat_line_input, line_input))
591     return 0;
592
593   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
594     {
595       if (unformat (line_input, "off"))
596         mm->sec_check = false;
597       else if (unformat (line_input, "on"))
598         mm->sec_check = true;
599       else
600         {
601           error = clib_error_return (0, "unknown input `%U'",
602                                      format_unformat_error, line_input);
603           goto done;
604         }
605     }
606
607 done:
608   unformat_free (line_input);
609
610   return error;
611 }
612
613 static clib_error_t *
614 map_security_check_frag_command_fn (vlib_main_t * vm,
615                                     unformat_input_t * input,
616                                     vlib_cli_command_t * cmd)
617 {
618   unformat_input_t _line_input, *line_input = &_line_input;
619   map_main_t *mm = &map_main;
620   clib_error_t *error = NULL;
621
622   /* Get a line of input. */
623   if (!unformat_user (input, unformat_line_input, line_input))
624     return 0;
625
626   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
627     {
628       if (unformat (line_input, "off"))
629         mm->sec_check_frag = false;
630       else if (unformat (line_input, "on"))
631         mm->sec_check_frag = true;
632       else
633         {
634           error = clib_error_return (0, "unknown input `%U'",
635                                      format_unformat_error, line_input);
636           goto done;
637         }
638     }
639
640 done:
641   unformat_free (line_input);
642
643   return error;
644 }
645
646 static clib_error_t *
647 map_add_domain_command_fn (vlib_main_t * vm,
648                            unformat_input_t * input, vlib_cli_command_t * cmd)
649 {
650   unformat_input_t _line_input, *line_input = &_line_input;
651   ip4_address_t ip4_prefix;
652   ip6_address_t ip6_prefix;
653   ip6_address_t ip6_src;
654   u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
655   u32 num_m_args = 0;
656   /* Optional arguments */
657   u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
658   u32 mtu = 0;
659   u8 flags = 0;
660   ip6_src_len = 128;
661   clib_error_t *error = NULL;
662
663   /* Get a line of input. */
664   if (!unformat_user (input, unformat_line_input, line_input))
665     return 0;
666
667   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
668     {
669       if (unformat
670           (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
671            &ip4_prefix_len))
672         num_m_args++;
673       else
674         if (unformat
675             (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
676              &ip6_prefix_len))
677         num_m_args++;
678       else
679         if (unformat
680             (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
681              &ip6_src_len))
682         num_m_args++;
683       else
684         if (unformat
685             (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
686         num_m_args++;
687       else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
688         num_m_args++;
689       else if (unformat (line_input, "psid-offset %d", &psid_offset))
690         num_m_args++;
691       else if (unformat (line_input, "psid-len %d", &psid_length))
692         num_m_args++;
693       else if (unformat (line_input, "mtu %d", &mtu))
694         num_m_args++;
695       else if (unformat (line_input, "map-t"))
696         flags |= MAP_DOMAIN_TRANSLATION;
697       else
698         {
699           error = clib_error_return (0, "unknown input `%U'",
700                                      format_unformat_error, line_input);
701           goto done;
702         }
703     }
704
705   if (num_m_args < 3)
706     {
707       error = clib_error_return (0, "mandatory argument(s) missing");
708       goto done;
709     }
710
711   map_create_domain (&ip4_prefix, ip4_prefix_len,
712                      &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
713                      ea_bits_len, psid_offset, psid_length, &map_domain_index,
714                      mtu, flags);
715
716 done:
717   unformat_free (line_input);
718
719   return error;
720 }
721
722 static clib_error_t *
723 map_del_domain_command_fn (vlib_main_t * vm,
724                            unformat_input_t * input, vlib_cli_command_t * cmd)
725 {
726   unformat_input_t _line_input, *line_input = &_line_input;
727   u32 num_m_args = 0;
728   u32 map_domain_index;
729   clib_error_t *error = NULL;
730
731   /* Get a line of input. */
732   if (!unformat_user (input, unformat_line_input, line_input))
733     return 0;
734
735   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
736     {
737       if (unformat (line_input, "index %d", &map_domain_index))
738         num_m_args++;
739       else
740         {
741           error = clib_error_return (0, "unknown input `%U'",
742                                      format_unformat_error, line_input);
743           goto done;
744         }
745     }
746
747   if (num_m_args != 1)
748     {
749       error = clib_error_return (0, "mandatory argument(s) missing");
750       goto done;
751     }
752
753   map_delete_domain (map_domain_index);
754
755 done:
756   unformat_free (line_input);
757
758   return error;
759 }
760
761 static clib_error_t *
762 map_add_rule_command_fn (vlib_main_t * vm,
763                          unformat_input_t * input, vlib_cli_command_t * cmd)
764 {
765   unformat_input_t _line_input, *line_input = &_line_input;
766   ip6_address_t tep;
767   u32 num_m_args = 0;
768   u32 psid = 0, map_domain_index;
769   clib_error_t *error = NULL;
770
771   /* Get a line of input. */
772   if (!unformat_user (input, unformat_line_input, line_input))
773     return 0;
774
775   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
776     {
777       if (unformat (line_input, "index %d", &map_domain_index))
778         num_m_args++;
779       else if (unformat (line_input, "psid %d", &psid))
780         num_m_args++;
781       else
782         if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
783         num_m_args++;
784       else
785         {
786           error = clib_error_return (0, "unknown input `%U'",
787                                      format_unformat_error, line_input);
788           goto done;
789         }
790     }
791
792   if (num_m_args != 3)
793     {
794       error = clib_error_return (0, "mandatory argument(s) missing");
795       goto done;
796     }
797
798   if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
799     {
800       error = clib_error_return (0, "Failing to add Mapping Rule");
801       goto done;
802     }
803
804 done:
805   unformat_free (line_input);
806
807   return error;
808 }
809
810 #if MAP_SKIP_IP6_LOOKUP
811 static clib_error_t *
812 map_pre_resolve_command_fn (vlib_main_t * vm,
813                             unformat_input_t * input,
814                             vlib_cli_command_t * cmd)
815 {
816   unformat_input_t _line_input, *line_input = &_line_input;
817   ip4_address_t ip4nh, *p_v4 = NULL;
818   ip6_address_t ip6nh, *p_v6 = NULL;
819   clib_error_t *error = NULL;
820
821   memset (&ip4nh, 0, sizeof (ip4nh));
822   memset (&ip6nh, 0, sizeof (ip6nh));
823
824   /* Get a line of input. */
825   if (!unformat_user (input, unformat_line_input, line_input))
826     return 0;
827
828   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
829     {
830       if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
831         p_v4 = &ip4nh;
832       else
833         if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
834         p_v6 = &ip6nh;
835       else
836         {
837           error = clib_error_return (0, "unknown input `%U'",
838                                      format_unformat_error, line_input);
839           goto done;
840         }
841     }
842
843   map_pre_resolve (p_v4, p_v6);
844
845 done:
846   unformat_free (line_input);
847
848   return error;
849 }
850 #endif
851
852 static clib_error_t *
853 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
854                                           unformat_input_t * input,
855                                           vlib_cli_command_t * cmd)
856 {
857   unformat_input_t _line_input, *line_input = &_line_input;
858   ip4_address_t icmp_src_address;
859   map_main_t *mm = &map_main;
860   clib_error_t *error = NULL;
861
862   mm->icmp4_src_address.as_u32 = 0;
863
864   /* Get a line of input. */
865   if (!unformat_user (input, unformat_line_input, line_input))
866     return 0;
867
868   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
869     {
870       if (unformat
871           (line_input, "%U", unformat_ip4_address, &icmp_src_address))
872         mm->icmp4_src_address = icmp_src_address;
873       else
874         {
875           error = clib_error_return (0, "unknown input `%U'",
876                                      format_unformat_error, line_input);
877           goto done;
878         }
879     }
880
881 done:
882   unformat_free (line_input);
883
884   return error;
885 }
886
887 static clib_error_t *
888 map_icmp_unreachables_command_fn (vlib_main_t * vm,
889                                   unformat_input_t * input,
890                                   vlib_cli_command_t * cmd)
891 {
892   unformat_input_t _line_input, *line_input = &_line_input;
893   map_main_t *mm = &map_main;
894   int num_m_args = 0;
895   clib_error_t *error = NULL;
896
897   /* Get a line of input. */
898   if (!unformat_user (input, unformat_line_input, line_input))
899     return 0;
900
901   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
902     {
903       num_m_args++;
904       if (unformat (line_input, "on"))
905         mm->icmp6_enabled = true;
906       else if (unformat (line_input, "off"))
907         mm->icmp6_enabled = false;
908       else
909         {
910           error = clib_error_return (0, "unknown input `%U'",
911                                      format_unformat_error, line_input);
912           goto done;
913         }
914     }
915
916
917   if (num_m_args != 1)
918     error = clib_error_return (0, "mandatory argument(s) missing");
919
920 done:
921   unformat_free (line_input);
922
923   return error;
924 }
925
926 static clib_error_t *
927 map_fragment_command_fn (vlib_main_t * vm,
928                          unformat_input_t * input, vlib_cli_command_t * cmd)
929 {
930   unformat_input_t _line_input, *line_input = &_line_input;
931   map_main_t *mm = &map_main;
932   clib_error_t *error = NULL;
933
934   /* Get a line of input. */
935   if (!unformat_user (input, unformat_line_input, line_input))
936     return 0;
937
938   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
939     {
940       if (unformat (line_input, "inner"))
941         mm->frag_inner = true;
942       else if (unformat (line_input, "outer"))
943         mm->frag_inner = false;
944       else
945         {
946           error = clib_error_return (0, "unknown input `%U'",
947                                      format_unformat_error, line_input);
948           goto done;
949         }
950     }
951
952 done:
953   unformat_free (line_input);
954
955   return error;
956 }
957
958 static clib_error_t *
959 map_fragment_df_command_fn (vlib_main_t * vm,
960                             unformat_input_t * input,
961                             vlib_cli_command_t * cmd)
962 {
963   unformat_input_t _line_input, *line_input = &_line_input;
964   map_main_t *mm = &map_main;
965   clib_error_t *error = NULL;
966
967   /* Get a line of input. */
968   if (!unformat_user (input, unformat_line_input, line_input))
969     return 0;
970
971   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
972     {
973       if (unformat (line_input, "on"))
974         mm->frag_ignore_df = true;
975       else if (unformat (line_input, "off"))
976         mm->frag_ignore_df = false;
977       else
978         {
979           error = clib_error_return (0, "unknown input `%U'",
980                                      format_unformat_error, line_input);
981           goto done;
982         }
983     }
984
985 done:
986   unformat_free (line_input);
987
988   return error;
989 }
990
991 static clib_error_t *
992 map_traffic_class_command_fn (vlib_main_t * vm,
993                               unformat_input_t * input,
994                               vlib_cli_command_t * cmd)
995 {
996   unformat_input_t _line_input, *line_input = &_line_input;
997   map_main_t *mm = &map_main;
998   u32 tc = 0;
999   clib_error_t *error = NULL;
1000
1001   mm->tc_copy = false;
1002
1003   /* Get a line of input. */
1004   if (!unformat_user (input, unformat_line_input, line_input))
1005     return 0;
1006
1007   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1008     {
1009       if (unformat (line_input, "copy"))
1010         mm->tc_copy = true;
1011       else if (unformat (line_input, "%x", &tc))
1012         mm->tc = tc & 0xff;
1013       else
1014         {
1015           error = clib_error_return (0, "unknown input `%U'",
1016                                      format_unformat_error, line_input);
1017           goto done;
1018         }
1019     }
1020
1021 done:
1022   unformat_free (line_input);
1023
1024   return error;
1025 }
1026
1027 static u8 *
1028 format_map_domain (u8 * s, va_list * args)
1029 {
1030   map_domain_t *d = va_arg (*args, map_domain_t *);
1031   bool counters = va_arg (*args, int);
1032   map_main_t *mm = &map_main;
1033   ip6_address_t ip6_prefix;
1034
1035   if (d->rules)
1036     memset (&ip6_prefix, 0, sizeof (ip6_prefix));
1037   else
1038     ip6_prefix = d->ip6_prefix;
1039
1040   s = format (s,
1041               "[%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",
1042               d - mm->domains,
1043               format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
1044               format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
1045               format_ip6_address, &d->ip6_src, d->ip6_src_len,
1046               d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
1047               (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
1048
1049   if (counters)
1050     {
1051       map_domain_counter_lock (mm);
1052       vlib_counter_t v;
1053       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
1054                                  d - mm->domains, &v);
1055       s = format (s, "  TX: %lld/%lld", v.packets, v.bytes);
1056       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
1057                                  d - mm->domains, &v);
1058       s = format (s, "  RX: %lld/%lld", v.packets, v.bytes);
1059       map_domain_counter_unlock (mm);
1060     }
1061   s = format (s, "\n");
1062
1063   if (d->rules)
1064     {
1065       int i;
1066       ip6_address_t dst;
1067       for (i = 0; i < (0x1 << d->psid_length); i++)
1068         {
1069           dst = d->rules[i];
1070           if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
1071             continue;
1072           s = format (s,
1073                       " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
1074                       &dst);
1075         }
1076     }
1077   return s;
1078 }
1079
1080 static u8 *
1081 format_map_ip4_reass (u8 * s, va_list * args)
1082 {
1083   map_main_t *mm = &map_main;
1084   map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
1085   map_ip4_reass_key_t *k = &r->key;
1086   f64 now = vlib_time_now (mm->vlib_main);
1087   f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
1088   f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1089   s = format (s,
1090               "ip4-reass src=%U  dst=%U  protocol=%d  identifier=%d  port=%d  lifetime=%.3lf\n",
1091               format_ip4_address, &k->src.as_u8, format_ip4_address,
1092               &k->dst.as_u8, k->protocol,
1093               clib_net_to_host_u16 (k->fragment_id),
1094               (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
1095   return s;
1096 }
1097
1098 static u8 *
1099 format_map_ip6_reass (u8 * s, va_list * args)
1100 {
1101   map_main_t *mm = &map_main;
1102   map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
1103   map_ip6_reass_key_t *k = &r->key;
1104   f64 now = vlib_time_now (mm->vlib_main);
1105   f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
1106   f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1107   s = format (s,
1108               "ip6-reass src=%U  dst=%U  protocol=%d  identifier=%d  lifetime=%.3lf\n",
1109               format_ip6_address, &k->src.as_u8, format_ip6_address,
1110               &k->dst.as_u8, k->protocol,
1111               clib_net_to_host_u32 (k->fragment_id), dt);
1112   return s;
1113 }
1114
1115 static clib_error_t *
1116 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1117                             vlib_cli_command_t * cmd)
1118 {
1119   unformat_input_t _line_input, *line_input = &_line_input;
1120   map_main_t *mm = &map_main;
1121   map_domain_t *d;
1122   bool counters = false;
1123   u32 map_domain_index = ~0;
1124   clib_error_t *error = NULL;
1125
1126   /* Get a line of input. */
1127   if (!unformat_user (input, unformat_line_input, line_input))
1128     return 0;
1129
1130   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1131     {
1132       if (unformat (line_input, "counters"))
1133         counters = true;
1134       else if (unformat (line_input, "index %d", &map_domain_index))
1135         ;
1136       else
1137         {
1138           error = clib_error_return (0, "unknown input `%U'",
1139                                      format_unformat_error, line_input);
1140           goto done;
1141         }
1142     }
1143
1144   if (pool_elts (mm->domains) == 0)
1145     vlib_cli_output (vm, "No MAP domains are configured...");
1146
1147   if (map_domain_index == ~0)
1148     {
1149     /* *INDENT-OFF* */
1150     pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1151     /* *INDENT-ON* */
1152     }
1153   else
1154     {
1155       if (pool_is_free_index (mm->domains, map_domain_index))
1156         {
1157           error = clib_error_return (0, "MAP domain does not exists %d",
1158                                      map_domain_index);
1159           goto done;
1160         }
1161
1162       d = pool_elt_at_index (mm->domains, map_domain_index);
1163       vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1164     }
1165
1166 done:
1167   unformat_free (line_input);
1168
1169   return error;
1170 }
1171
1172 static clib_error_t *
1173 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1174                                vlib_cli_command_t * cmd)
1175 {
1176   map_main_t *mm = &map_main;
1177   map_ip4_reass_t *f4;
1178   map_ip6_reass_t *f6;
1179
1180   /* *INDENT-OFF* */
1181   pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
1182   /* *INDENT-ON* */
1183   /* *INDENT-OFF* */
1184   pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
1185   /* *INDENT-ON* */
1186   return (0);
1187 }
1188
1189 u64
1190 map_error_counter_get (u32 node_index, map_error_t map_error)
1191 {
1192   vlib_main_t *vm = vlib_get_main ();
1193   vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1194   vlib_error_main_t *em = &vm->error_main;
1195   vlib_error_t e = error_node->errors[map_error];
1196   vlib_node_t *n = vlib_get_node (vm, node_index);
1197   u32 ci;
1198
1199   ci = vlib_error_get_code (e);
1200   ASSERT (ci < n->n_errors);
1201   ci += n->error_heap_index;
1202
1203   return (em->counters[ci]);
1204 }
1205
1206 static clib_error_t *
1207 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1208                            vlib_cli_command_t * cmd)
1209 {
1210   map_main_t *mm = &map_main;
1211   map_domain_t *d;
1212   int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1213   if (pool_elts (mm->domains) == 0)
1214     vlib_cli_output (vm, "No MAP domains are configured...");
1215
1216   /* *INDENT-OFF* */
1217   pool_foreach(d, mm->domains, ({
1218     if (d->rules) {
1219       rulecount+= 0x1 << d->psid_length;
1220       rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1221     }
1222     domains += sizeof(*d);
1223     domaincount++;
1224   }));
1225   /* *INDENT-ON* */
1226
1227   vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1228   vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1229   vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1230   vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1231
1232 #if MAP_SKIP_IP6_LOOKUP
1233   vlib_cli_output (vm,
1234                    "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1235                    format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1236                    format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1237
1238 #endif
1239
1240   if (mm->tc_copy)
1241     vlib_cli_output (vm, "MAP traffic-class: copy");
1242   else
1243     vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1244
1245   vlib_cli_output (vm,
1246                    "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1247                    mm->sec_check ? "enabled" : "disabled",
1248                    mm->sec_check_frag ? "enabled" : "disabled");
1249
1250   vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1251                    format_ip4_address, &mm->icmp4_src_address);
1252   vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1253                    mm->icmp6_enabled ? "enabled" : "disabled");
1254   vlib_cli_output (vm, "Inner fragmentation: %s\n",
1255                    mm->frag_inner ? "enabled" : "disabled");
1256   vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1257                    mm->frag_ignore_df ? "enabled" : "disabled");
1258
1259   /*
1260    * Counters
1261    */
1262   vlib_combined_counter_main_t *cm = mm->domain_counters;
1263   u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1264   u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1265   int which, i;
1266   vlib_counter_t v;
1267
1268   memset (total_pkts, 0, sizeof (total_pkts));
1269   memset (total_bytes, 0, sizeof (total_bytes));
1270
1271   map_domain_counter_lock (mm);
1272   vec_foreach (cm, mm->domain_counters)
1273   {
1274     which = cm - mm->domain_counters;
1275
1276     for (i = 0; i < vec_len (cm->maxi); i++)
1277       {
1278         vlib_get_combined_counter (cm, i, &v);
1279         total_pkts[which] += v.packets;
1280         total_bytes[which] += v.bytes;
1281       }
1282   }
1283   map_domain_counter_unlock (mm);
1284
1285   vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1286                    total_pkts[MAP_DOMAIN_COUNTER_TX],
1287                    total_bytes[MAP_DOMAIN_COUNTER_TX]);
1288   vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1289                    total_pkts[MAP_DOMAIN_COUNTER_RX],
1290                    total_bytes[MAP_DOMAIN_COUNTER_RX]);
1291
1292   vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1293                    vlib_get_simple_counter (&mm->icmp_relayed, 0));
1294
1295   return 0;
1296 }
1297
1298 static clib_error_t *
1299 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1300                              vlib_cli_command_t * cmd)
1301 {
1302   unformat_input_t _line_input, *line_input = &_line_input;
1303   u32 lifetime = ~0;
1304   f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1305   u32 pool_size = ~0;
1306   u64 buffers = ~(0ull);
1307   u8 ip4 = 0, ip6 = 0;
1308
1309   if (!unformat_user (input, unformat_line_input, line_input))
1310     return 0;
1311
1312   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1313     {
1314       if (unformat (line_input, "lifetime %u", &lifetime))
1315         ;
1316       else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1317         ;
1318       else if (unformat (line_input, "pool-size %u", &pool_size))
1319         ;
1320       else if (unformat (line_input, "buffers %llu", &buffers))
1321         ;
1322       else if (unformat (line_input, "ip4"))
1323         ip4 = 1;
1324       else if (unformat (line_input, "ip6"))
1325         ip6 = 1;
1326       else
1327         {
1328           unformat_free (line_input);
1329           return clib_error_return (0, "invalid input");
1330         }
1331     }
1332   unformat_free (line_input);
1333
1334   if (!ip4 && !ip6)
1335     return clib_error_return (0, "must specify ip4 and/or ip6");
1336
1337   if (ip4)
1338     {
1339       if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1340         return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1341                                   MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1342       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1343           && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1344         return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1345                                   MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1346       if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1347         return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1348                                   MAP_IP4_REASS_CONF_LIFETIME_MAX);
1349       if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1350         return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1351                                   MAP_IP4_REASS_CONF_BUFFERS_MAX);
1352     }
1353
1354   if (ip6)
1355     {
1356       if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1357         return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1358                                   MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1359       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1360           && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1361         return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1362                                   MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1363       if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1364         return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1365                                   MAP_IP6_REASS_CONF_LIFETIME_MAX);
1366       if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1367         return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1368                                   MAP_IP6_REASS_CONF_BUFFERS_MAX);
1369     }
1370
1371   if (ip4)
1372     {
1373       u32 reass = 0, packets = 0;
1374       if (pool_size != ~0)
1375         {
1376           if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1377             {
1378               vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1379             }
1380           else
1381             {
1382               vlib_cli_output (vm,
1383                                "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1384                                reass, packets);
1385             }
1386         }
1387       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1388         {
1389           if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1390             {
1391               vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1392             }
1393           else
1394             {
1395               vlib_cli_output (vm,
1396                                "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1397                                reass, packets);
1398             }
1399         }
1400       if (lifetime != ~0)
1401         {
1402           if (map_ip4_reass_conf_lifetime (lifetime))
1403             vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1404           else
1405             vlib_cli_output (vm, "Setting ip4-reass lifetime");
1406         }
1407       if (buffers != ~(0ull))
1408         {
1409           if (map_ip4_reass_conf_buffers (buffers))
1410             vlib_cli_output (vm, "Could not set ip4-reass buffers");
1411           else
1412             vlib_cli_output (vm, "Setting ip4-reass buffers");
1413         }
1414
1415       if (map_main.ip4_reass_conf_buffers >
1416           map_main.ip4_reass_conf_pool_size *
1417           MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1418         {
1419           vlib_cli_output (vm,
1420                            "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1421         }
1422     }
1423
1424   if (ip6)
1425     {
1426       u32 reass = 0, packets = 0;
1427       if (pool_size != ~0)
1428         {
1429           if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1430             {
1431               vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1432             }
1433           else
1434             {
1435               vlib_cli_output (vm,
1436                                "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1437                                reass, packets);
1438             }
1439         }
1440       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1441         {
1442           if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1443             {
1444               vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1445             }
1446           else
1447             {
1448               vlib_cli_output (vm,
1449                                "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1450                                reass, packets);
1451             }
1452         }
1453       if (lifetime != ~0)
1454         {
1455           if (map_ip6_reass_conf_lifetime (lifetime))
1456             vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1457           else
1458             vlib_cli_output (vm, "Setting ip6-reass lifetime");
1459         }
1460       if (buffers != ~(0ull))
1461         {
1462           if (map_ip6_reass_conf_buffers (buffers))
1463             vlib_cli_output (vm, "Could not set ip6-reass buffers");
1464           else
1465             vlib_cli_output (vm, "Setting ip6-reass buffers");
1466         }
1467
1468       if (map_main.ip6_reass_conf_buffers >
1469           map_main.ip6_reass_conf_pool_size *
1470           MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1471         {
1472           vlib_cli_output (vm,
1473                            "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1474         }
1475     }
1476
1477   return 0;
1478 }
1479
1480
1481 /*
1482  * packet trace format function
1483  */
1484 u8 *
1485 format_map_trace (u8 * s, va_list * args)
1486 {
1487   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1488   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1489   map_trace_t *t = va_arg (*args, map_trace_t *);
1490   u32 map_domain_index = t->map_domain_index;
1491   u16 port = t->port;
1492
1493   s =
1494     format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1495             clib_net_to_host_u16 (port));
1496
1497   return s;
1498 }
1499
1500 static_always_inline map_ip4_reass_t *
1501 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1502 {
1503   map_main_t *mm = &map_main;
1504   u32 ri = mm->ip4_reass_hash_table[bucket];
1505   while (ri != MAP_REASS_INDEX_NONE)
1506     {
1507       map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1508       if (r->key.as_u64[0] == k->as_u64[0] &&
1509           r->key.as_u64[1] == k->as_u64[1] &&
1510           now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1511         {
1512           return r;
1513         }
1514       ri = r->bucket_next;
1515     }
1516   return NULL;
1517 }
1518
1519 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1520
1521 void
1522 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1523 {
1524   map_main_t *mm = &map_main;
1525   map_ip4_reass_get_fragments (r, pi_to_drop);
1526
1527   // Unlink in hash bucket
1528   map_ip4_reass_t *r2 = NULL;
1529   u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1530   while (r2i != map_ip4_reass_pool_index (r))
1531     {
1532       ASSERT (r2i != MAP_REASS_INDEX_NONE);
1533       r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1534       r2i = r2->bucket_next;
1535     }
1536   if (r2)
1537     {
1538       r2->bucket_next = r->bucket_next;
1539     }
1540   else
1541     {
1542       mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1543     }
1544
1545   // Unlink in list
1546   if (r->fifo_next == map_ip4_reass_pool_index (r))
1547     {
1548       mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1549     }
1550   else
1551     {
1552       if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1553         mm->ip4_reass_fifo_last = r->fifo_prev;
1554       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1555         r->fifo_next;
1556       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1557         r->fifo_prev;
1558     }
1559
1560   pool_put (mm->ip4_reass_pool, r);
1561   mm->ip4_reass_allocated--;
1562 }
1563
1564 map_ip4_reass_t *
1565 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1566                    u8 protocol, u32 ** pi_to_drop)
1567 {
1568   map_ip4_reass_t *r;
1569   map_main_t *mm = &map_main;
1570   map_ip4_reass_key_t k = {.src.data_u32 = src,
1571     .dst.data_u32 = dst,
1572     .fragment_id = fragment_id,
1573     .protocol = protocol
1574   };
1575
1576   u32 h = 0;
1577   h = crc_u32 (k.as_u32[0], h);
1578   h = crc_u32 (k.as_u32[1], h);
1579   h = crc_u32 (k.as_u32[2], h);
1580   h = crc_u32 (k.as_u32[3], h);
1581   h = h >> (32 - mm->ip4_reass_ht_log2len);
1582
1583   f64 now = vlib_time_now (mm->vlib_main);
1584
1585   //Cache garbage collection
1586   while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1587     {
1588       map_ip4_reass_t *last =
1589         pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1590       if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1591         map_ip4_reass_free (last, pi_to_drop);
1592       else
1593         break;
1594     }
1595
1596   if ((r = map_ip4_reass_lookup (&k, h, now)))
1597     return r;
1598
1599   if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1600     return NULL;
1601
1602   pool_get (mm->ip4_reass_pool, r);
1603   mm->ip4_reass_allocated++;
1604   int i;
1605   for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1606     r->fragments[i] = ~0;
1607
1608   u32 ri = map_ip4_reass_pool_index (r);
1609
1610   //Link in new bucket
1611   r->bucket = h;
1612   r->bucket_next = mm->ip4_reass_hash_table[h];
1613   mm->ip4_reass_hash_table[h] = ri;
1614
1615   //Link in fifo
1616   if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1617     {
1618       r->fifo_next =
1619         pool_elt_at_index (mm->ip4_reass_pool,
1620                            mm->ip4_reass_fifo_last)->fifo_next;
1621       r->fifo_prev = mm->ip4_reass_fifo_last;
1622       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1623       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1624     }
1625   else
1626     {
1627       r->fifo_next = r->fifo_prev = ri;
1628       mm->ip4_reass_fifo_last = ri;
1629     }
1630
1631   //Set other fields
1632   r->ts = now;
1633   r->key = k;
1634   r->port = -1;
1635 #ifdef MAP_IP4_REASS_COUNT_BYTES
1636   r->expected_total = 0xffff;
1637   r->forwarded = 0;
1638 #endif
1639
1640   return r;
1641 }
1642
1643 int
1644 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1645 {
1646   if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1647     return -1;
1648
1649   int i;
1650   for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1651     if (r->fragments[i] == ~0)
1652       {
1653         r->fragments[i] = pi;
1654         map_main.ip4_reass_buffered_counter++;
1655         return 0;
1656       }
1657   return -1;
1658 }
1659
1660 static_always_inline map_ip6_reass_t *
1661 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1662 {
1663   map_main_t *mm = &map_main;
1664   u32 ri = mm->ip6_reass_hash_table[bucket];
1665   while (ri != MAP_REASS_INDEX_NONE)
1666     {
1667       map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1668       if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1669           r->key.as_u64[0] == k->as_u64[0] &&
1670           r->key.as_u64[1] == k->as_u64[1] &&
1671           r->key.as_u64[2] == k->as_u64[2] &&
1672           r->key.as_u64[3] == k->as_u64[3] &&
1673           r->key.as_u64[4] == k->as_u64[4])
1674         return r;
1675       ri = r->bucket_next;
1676     }
1677   return NULL;
1678 }
1679
1680 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1681
1682 void
1683 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1684 {
1685   map_main_t *mm = &map_main;
1686   int i;
1687   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1688     if (r->fragments[i].pi != ~0)
1689       {
1690         vec_add1 (*pi_to_drop, r->fragments[i].pi);
1691         r->fragments[i].pi = ~0;
1692         map_main.ip6_reass_buffered_counter--;
1693       }
1694
1695   // Unlink in hash bucket
1696   map_ip6_reass_t *r2 = NULL;
1697   u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1698   while (r2i != map_ip6_reass_pool_index (r))
1699     {
1700       ASSERT (r2i != MAP_REASS_INDEX_NONE);
1701       r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1702       r2i = r2->bucket_next;
1703     }
1704   if (r2)
1705     {
1706       r2->bucket_next = r->bucket_next;
1707     }
1708   else
1709     {
1710       mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1711     }
1712
1713   // Unlink in list
1714   if (r->fifo_next == map_ip6_reass_pool_index (r))
1715     {
1716       //Single element in the list, list is now empty
1717       mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1718     }
1719   else
1720     {
1721       if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r))      //First element
1722         mm->ip6_reass_fifo_last = r->fifo_prev;
1723       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1724         r->fifo_next;
1725       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1726         r->fifo_prev;
1727     }
1728
1729   // Free from pool if necessary
1730   pool_put (mm->ip6_reass_pool, r);
1731   mm->ip6_reass_allocated--;
1732 }
1733
1734 map_ip6_reass_t *
1735 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1736                    u8 protocol, u32 ** pi_to_drop)
1737 {
1738   map_ip6_reass_t *r;
1739   map_main_t *mm = &map_main;
1740   map_ip6_reass_key_t k = {
1741     .src = *src,
1742     .dst = *dst,
1743     .fragment_id = fragment_id,
1744     .protocol = protocol
1745   };
1746
1747   u32 h = 0;
1748   int i;
1749   for (i = 0; i < 10; i++)
1750     h = crc_u32 (k.as_u32[i], h);
1751   h = h >> (32 - mm->ip6_reass_ht_log2len);
1752
1753   f64 now = vlib_time_now (mm->vlib_main);
1754
1755   //Cache garbage collection
1756   while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1757     {
1758       map_ip6_reass_t *last =
1759         pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1760       if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1761         map_ip6_reass_free (last, pi_to_drop);
1762       else
1763         break;
1764     }
1765
1766   if ((r = map_ip6_reass_lookup (&k, h, now)))
1767     return r;
1768
1769   if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1770     return NULL;
1771
1772   pool_get (mm->ip6_reass_pool, r);
1773   mm->ip6_reass_allocated++;
1774   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1775     {
1776       r->fragments[i].pi = ~0;
1777       r->fragments[i].next_data_len = 0;
1778       r->fragments[i].next_data_offset = 0;
1779     }
1780
1781   u32 ri = map_ip6_reass_pool_index (r);
1782
1783   //Link in new bucket
1784   r->bucket = h;
1785   r->bucket_next = mm->ip6_reass_hash_table[h];
1786   mm->ip6_reass_hash_table[h] = ri;
1787
1788   //Link in fifo
1789   if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1790     {
1791       r->fifo_next =
1792         pool_elt_at_index (mm->ip6_reass_pool,
1793                            mm->ip6_reass_fifo_last)->fifo_next;
1794       r->fifo_prev = mm->ip6_reass_fifo_last;
1795       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1796       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1797     }
1798   else
1799     {
1800       r->fifo_next = r->fifo_prev = ri;
1801       mm->ip6_reass_fifo_last = ri;
1802     }
1803
1804   //Set other fields
1805   r->ts = now;
1806   r->key = k;
1807   r->ip4_header.ip_version_and_header_length = 0;
1808 #ifdef MAP_IP6_REASS_COUNT_BYTES
1809   r->expected_total = 0xffff;
1810   r->forwarded = 0;
1811 #endif
1812   return r;
1813 }
1814
1815 int
1816 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1817                             u16 data_offset, u16 next_data_offset,
1818                             u8 * data_start, u16 data_len)
1819 {
1820   map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1821   u16 copied_len = (data_len > 20) ? 20 : data_len;
1822
1823   if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1824     return -1;
1825
1826   //Lookup for fragments for the current buffer
1827   //and the one before that
1828   int i;
1829   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1830     {
1831       if (data_offset && r->fragments[i].next_data_offset == data_offset)
1832         {
1833           prev_f = &r->fragments[i];    // This is buffer for previous packet
1834         }
1835       else if (r->fragments[i].next_data_offset == next_data_offset)
1836         {
1837           f = &r->fragments[i]; // This is a buffer for the current packet
1838         }
1839       else if (r->fragments[i].next_data_offset == 0)
1840         {                       //Available
1841           if (f == NULL)
1842             f = &r->fragments[i];
1843           else if (prev_f == NULL)
1844             prev_f = &r->fragments[i];
1845         }
1846     }
1847
1848   if (!f || f->pi != ~0)
1849     return -1;
1850
1851   if (data_offset)
1852     {
1853       if (!prev_f)
1854         return -1;
1855
1856       clib_memcpy (prev_f->next_data, data_start, copied_len);
1857       prev_f->next_data_len = copied_len;
1858       prev_f->next_data_offset = data_offset;
1859     }
1860   else
1861     {
1862       if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1863         return -1;
1864
1865       if (r->ip4_header.ip_version_and_header_length == 0)
1866         clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1867     }
1868
1869   if (data_len > 20)
1870     {
1871       f->next_data_offset = next_data_offset;
1872       f->pi = pi;
1873       map_main.ip6_reass_buffered_counter++;
1874     }
1875   return 0;
1876 }
1877
1878 void
1879 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1880 {
1881   map_main_t *mm = &map_main;
1882   int i;
1883
1884   if (dropped_packets)
1885     *dropped_packets = mm->ip4_reass_buffered_counter;
1886   if (trashed_reass)
1887     *trashed_reass = mm->ip4_reass_allocated;
1888   if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1889     {
1890       u16 ri = mm->ip4_reass_fifo_last;
1891       do
1892         {
1893           map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1894           for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1895             if (r->fragments[i] != ~0)
1896               map_ip4_drop_pi (r->fragments[i]);
1897
1898           ri = r->fifo_next;
1899           pool_put (mm->ip4_reass_pool, r);
1900         }
1901       while (ri != mm->ip4_reass_fifo_last);
1902     }
1903
1904   vec_free (mm->ip4_reass_hash_table);
1905   vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1906   for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1907     mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1908   pool_free (mm->ip4_reass_pool);
1909   pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1910
1911   mm->ip4_reass_allocated = 0;
1912   mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1913   mm->ip4_reass_buffered_counter = 0;
1914 }
1915
1916 u8
1917 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1918 {
1919   u32 desired_size = (u32) (pool_size * ht_ratio);
1920   u8 i;
1921   for (i = 1; i < 31; i++)
1922     if ((1 << i) >= desired_size)
1923       return i;
1924   return 4;
1925 }
1926
1927 int
1928 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1929                              u32 * dropped_packets)
1930 {
1931   map_main_t *mm = &map_main;
1932   if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1933     return -1;
1934
1935   map_ip4_reass_lock ();
1936   mm->ip4_reass_conf_ht_ratio = ht_ratio;
1937   mm->ip4_reass_ht_log2len =
1938     map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1939   map_ip4_reass_reinit (trashed_reass, dropped_packets);
1940   map_ip4_reass_unlock ();
1941   return 0;
1942 }
1943
1944 int
1945 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1946                               u32 * dropped_packets)
1947 {
1948   map_main_t *mm = &map_main;
1949   if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1950     return -1;
1951
1952   map_ip4_reass_lock ();
1953   mm->ip4_reass_conf_pool_size = pool_size;
1954   map_ip4_reass_reinit (trashed_reass, dropped_packets);
1955   map_ip4_reass_unlock ();
1956   return 0;
1957 }
1958
1959 int
1960 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1961 {
1962   map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1963   return 0;
1964 }
1965
1966 int
1967 map_ip4_reass_conf_buffers (u32 buffers)
1968 {
1969   map_main.ip4_reass_conf_buffers = buffers;
1970   return 0;
1971 }
1972
1973 void
1974 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1975 {
1976   map_main_t *mm = &map_main;
1977   if (dropped_packets)
1978     *dropped_packets = mm->ip6_reass_buffered_counter;
1979   if (trashed_reass)
1980     *trashed_reass = mm->ip6_reass_allocated;
1981   int i;
1982   if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1983     {
1984       u16 ri = mm->ip6_reass_fifo_last;
1985       do
1986         {
1987           map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1988           for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1989             if (r->fragments[i].pi != ~0)
1990               map_ip6_drop_pi (r->fragments[i].pi);
1991
1992           ri = r->fifo_next;
1993           pool_put (mm->ip6_reass_pool, r);
1994         }
1995       while (ri != mm->ip6_reass_fifo_last);
1996       mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1997     }
1998
1999   vec_free (mm->ip6_reass_hash_table);
2000   vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
2001   for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
2002     mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
2003   pool_free (mm->ip6_reass_pool);
2004   pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
2005
2006   mm->ip6_reass_allocated = 0;
2007   mm->ip6_reass_buffered_counter = 0;
2008 }
2009
2010 int
2011 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
2012                              u32 * dropped_packets)
2013 {
2014   map_main_t *mm = &map_main;
2015   if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
2016     return -1;
2017
2018   map_ip6_reass_lock ();
2019   mm->ip6_reass_conf_ht_ratio = ht_ratio;
2020   mm->ip6_reass_ht_log2len =
2021     map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
2022   map_ip6_reass_reinit (trashed_reass, dropped_packets);
2023   map_ip6_reass_unlock ();
2024   return 0;
2025 }
2026
2027 int
2028 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
2029                               u32 * dropped_packets)
2030 {
2031   map_main_t *mm = &map_main;
2032   if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
2033     return -1;
2034
2035   map_ip6_reass_lock ();
2036   mm->ip6_reass_conf_pool_size = pool_size;
2037   map_ip6_reass_reinit (trashed_reass, dropped_packets);
2038   map_ip6_reass_unlock ();
2039   return 0;
2040 }
2041
2042 int
2043 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
2044 {
2045   map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
2046   return 0;
2047 }
2048
2049 int
2050 map_ip6_reass_conf_buffers (u32 buffers)
2051 {
2052   map_main.ip6_reass_conf_buffers = buffers;
2053   return 0;
2054 }
2055
2056 /* *INDENT-OFF* */
2057
2058 /*?
2059  * Configure MAP reassembly behaviour
2060  *
2061  * @cliexpar
2062  * @cliexstart{map params reassembly}
2063  * @cliexend
2064  ?*/
2065 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2066   .path = "map params reassembly",
2067   .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2068                 "[pool-size <pool-size>] [buffers <buffers>] "
2069                 "[ht-ratio <ht-ratio>]",
2070   .function = map_params_reass_command_fn,
2071 };
2072
2073 /*?
2074  * Set or copy the IP TOS/Traffic Class field
2075  *
2076  * @cliexpar
2077  * @cliexstart{map params traffic-class}
2078  *
2079  * This command is used to set the traffic-class field in translated
2080  * or encapsulated packets. If copy is specifed (the default) then the
2081  * traffic-class/TOS field is copied from the original packet to the
2082  * translated / encapsulating header.
2083  * @cliexend
2084  ?*/
2085 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2086   .path = "map params traffic-class",
2087   .short_help = "map params traffic-class {0x0-0xff | copy}",
2088   .function = map_traffic_class_command_fn,
2089 };
2090
2091 /*?
2092  * Bypass IP4/IP6 lookup
2093  *
2094  * @cliexpar
2095  * @cliexstart{map params pre-resolve}
2096  *
2097  * Bypass a second FIB lookup of the translated or encapsulated
2098  * packet, and forward the packet directly to the specified
2099  * next-hop. This optimization trades forwarding flexibility for
2100  * performance.
2101  * @cliexend
2102  ?*/
2103 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2104   .path = "map params pre-resolve",
2105   .short_help = " map params pre-resolve {ip4-nh <address>} "
2106                 "| {ip6-nh <address>}",
2107   .function = map_pre_resolve_command_fn,
2108 };
2109
2110 /*?
2111  * Enable or disable the MAP-E inbound security check
2112  *
2113  * @cliexpar
2114  * @cliexstart{map params security-check}
2115  *
2116  * By default, a decapsulated packet's IPv4 source address will be
2117  * verified against the outer header's IPv6 source address. Disabling
2118  * this feature will allow IPv4 source address spoofing.
2119  * @cliexend
2120  ?*/
2121 VLIB_CLI_COMMAND(map_security_check_command, static) = {
2122   .path = "map params security-check",
2123   .short_help = "map params security-check on|off",
2124   .function = map_security_check_command_fn,
2125 };
2126
2127 /*?
2128  * Specifiy the IPv4 source address used for relayed ICMP error messages
2129  *
2130  * @cliexpar
2131  * @cliexstart{map params icmp source-address}
2132  *
2133  * This command specifies which IPv4 source address (must be local to
2134  * the system), that is used for relayed received IPv6 ICMP error
2135  * messages.
2136  * @cliexend
2137  ?*/
2138 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2139   .path = "map params icmp source-address",
2140   .short_help = "map params icmp source-address <ip4-address>",
2141   .function = map_icmp_relay_source_address_command_fn,
2142 };
2143
2144 /*?
2145  * Send IPv6 ICMP unreachables
2146  *
2147  * @cliexpar
2148  * @cliexstart{map params icmp6 unreachables}
2149  *
2150  * Send IPv6 ICMP unreachable messages back if security check fails or
2151  * no MAP domain exists.
2152  * @cliexend
2153  ?*/
2154 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2155   .path = "map params icmp6 unreachables",
2156   .short_help = "map params icmp6 unreachables {on|off}",
2157   .function = map_icmp_unreachables_command_fn,
2158 };
2159
2160 /*?
2161  * Configure MAP fragmentation behaviour
2162  *
2163  * @cliexpar
2164  * @cliexstart{map params fragment}
2165  * @cliexend
2166  ?*/
2167 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2168   .path = "map params fragment",
2169   .short_help = "map params fragment inner|outer",
2170   .function = map_fragment_command_fn,
2171 };
2172
2173 /*?
2174  * Ignore the IPv4 Don't fragment bit
2175  *
2176  * @cliexpar
2177  * @cliexstart{map params fragment ignore-df}
2178  *
2179  * Allows fragmentation of the IPv4 packet even if the DF bit is
2180  * set. The choice between inner or outer fragmentation of tunnel
2181  * packets is complicated. The benefit of inner fragmentation is that
2182  * the ultimate endpoint must reassemble, instead of the tunnel
2183  * endpoint.
2184  * @cliexend
2185  ?*/
2186 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
2187   .path = "map params fragment ignore-df",
2188   .short_help = "map params fragment ignore-df on|off",
2189   .function = map_fragment_df_command_fn,
2190 };
2191
2192 /*?
2193  * Specifiy if the inbound security check should be done on fragments
2194  *
2195  * @cliexpar
2196  * @cliexstart{map params security-check fragments}
2197  *
2198  * Typically the inbound on-decapsulation security check is only done
2199  * on the first packet. The packet that contains the L4
2200  * information. While a security check on every fragment is possible,
2201  * it has a cost. State must be created on the first fragment.
2202  * @cliexend
2203  ?*/
2204 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
2205   .path = "map params security-check fragments",
2206   .short_help = "map params security-check fragments on|off",
2207   .function = map_security_check_frag_command_fn,
2208 };
2209
2210 /*?
2211  * Add MAP domain
2212  *
2213  * @cliexpar
2214  * @cliexstart{map add domain}
2215  * @cliexend
2216  ?*/
2217 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2218   .path = "map add domain",
2219   .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2220       "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2221       "[map-t] [mtu <mtu>]",
2222   .function = map_add_domain_command_fn,
2223 };
2224
2225 /*?
2226  * Add MAP rule to a domain
2227  *
2228  * @cliexpar
2229  * @cliexstart{map add rule}
2230  * @cliexend
2231  ?*/
2232 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2233   .path = "map add rule",
2234   .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2235   .function = map_add_rule_command_fn,
2236 };
2237
2238 /*?
2239  * Delete MAP domain
2240  *
2241  * @cliexpar
2242  * @cliexstart{map del domain}
2243  * @cliexend
2244  ?*/
2245 VLIB_CLI_COMMAND(map_del_command, static) = {
2246   .path = "map del domain",
2247   .short_help = "map del domain index <domain>",
2248   .function = map_del_domain_command_fn,
2249 };
2250
2251 /*?
2252  * Show MAP domains
2253  *
2254  * @cliexpar
2255  * @cliexstart{show map domain}
2256  * @cliexend
2257  ?*/
2258 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2259   .path = "show map domain",
2260   .short_help = "show map domain index <n> [counters]",
2261   .function = show_map_domain_command_fn,
2262 };
2263
2264 /*?
2265  * Show MAP statistics
2266  *
2267  * @cliexpar
2268  * @cliexstart{show map stats}
2269  * @cliexend
2270  ?*/
2271 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2272   .path = "show map stats",
2273   .short_help = "show map stats",
2274   .function = show_map_stats_command_fn,
2275 };
2276
2277 /*?
2278  * Show MAP fragmentation information
2279  *
2280  * @cliexpar
2281  * @cliexstart{show map fragments}
2282  * @cliexend
2283  ?*/
2284 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2285   .path = "show map fragments",
2286   .short_help = "show map fragments",
2287   .function = show_map_fragments_command_fn,
2288 };
2289 /* *INDENT-ON* */
2290
2291 /*
2292  * map_init
2293  */
2294 clib_error_t *
2295 map_init (vlib_main_t * vm)
2296 {
2297   map_main_t *mm = &map_main;
2298   mm->vnet_main = vnet_get_main ();
2299   mm->vlib_main = vm;
2300
2301 #ifdef MAP_SKIP_IP6_LOOKUP
2302   fib_protocol_t proto;
2303
2304   FOR_EACH_FIB_PROTOCOL (proto)
2305   {
2306     map_pre_resolve_init (&pre_resolved[proto]);
2307   }
2308 #endif
2309
2310   /* traffic class */
2311   mm->tc = 0;
2312   mm->tc_copy = true;
2313
2314   /* Inbound security check */
2315   mm->sec_check = true;
2316   mm->sec_check_frag = false;
2317
2318   /* ICMP6 Type 1, Code 5 for security check failure */
2319   mm->icmp6_enabled = false;
2320
2321   /* Inner or outer fragmentation */
2322   mm->frag_inner = false;
2323   mm->frag_ignore_df = false;
2324
2325   vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2326   mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2327   mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2328
2329   vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2330   vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2331
2332   /* IP4 virtual reassembly */
2333   mm->ip4_reass_hash_table = 0;
2334   mm->ip4_reass_pool = 0;
2335   mm->ip4_reass_lock =
2336     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2337   mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2338   mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2339   mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2340   mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2341   mm->ip4_reass_ht_log2len =
2342     map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2343                         mm->ip4_reass_conf_pool_size);
2344   mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2345   map_ip4_reass_reinit (NULL, NULL);
2346
2347   /* IP6 virtual reassembly */
2348   mm->ip6_reass_hash_table = 0;
2349   mm->ip6_reass_pool = 0;
2350   mm->ip6_reass_lock =
2351     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2352   mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2353   mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2354   mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2355   mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2356   mm->ip6_reass_ht_log2len =
2357     map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2358                         mm->ip6_reass_conf_pool_size);
2359   mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2360   map_ip6_reass_reinit (NULL, NULL);
2361
2362 #ifdef MAP_SKIP_IP6_LOOKUP
2363   fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2364 #endif
2365   map_dpo_module_init ();
2366
2367   return 0;
2368 }
2369
2370 VLIB_INIT_FUNCTION (map_init);
2371
2372 /*
2373  * fd.io coding-style-patch-verification: ON
2374  *
2375  * Local Variables:
2376  * eval: (c-set-style "gnu")
2377  * End:
2378  */