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