blob: 702b5f80e5288fcaf459f298a76766b62d7af4de [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Neale Ranns0bfe5d82016-08-25 15:29:12 +010018#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>
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +020022#include <vppinfra/crc32.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include "map.h"
25
Dave Wallace71612d62017-10-24 01:32:41 -040026map_main_t map_main;
27
Ed Warnickecb9cada2015-12-08 15:45:58 -070028/*
29 * This code supports the following MAP modes:
Damjan Marion607de1a2016-08-16 22:53:54 +020030 *
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 * 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 DMR prefix length equal to 96.
58 *
59 */
60
61
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
63int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070064map_create_domain (ip4_address_t * ip4_prefix,
65 u8 ip4_prefix_len,
66 ip6_address_t * ip6_prefix,
67 u8 ip6_prefix_len,
68 ip6_address_t * ip6_src,
69 u8 ip6_src_len,
70 u8 ea_bits_len,
71 u8 psid_offset,
72 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Ole Troan07e7eab2016-08-25 00:24:08 +020074 u8 suffix_len, suffix_shift;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010075 map_main_t *mm = &map_main;
Neale Ranns948e00f2016-10-20 13:39:34 +010076 dpo_id_t dpo_v4 = DPO_INVALID;
77 dpo_id_t dpo_v6 = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010078 map_domain_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 /* Sanity check on the src prefix length */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070081 if (flags & MAP_DOMAIN_TRANSLATION)
82 {
83 if (ip6_src_len != 96)
84 {
85 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 return -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070087 }
Ole Troane31d9562017-11-08 11:10:54 +010088 if ((flags & MAP_DOMAIN_RFC6052) && ip6_prefix_len != 96)
89 {
90 clib_warning ("RFC6052 translation only supports ip6_prefix_len = "
91 "96 for now");
92 return -1;
93 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070094 }
95 else
96 {
97 if (ip6_src_len != 128)
98 {
99 clib_warning
Ole Troan07e7eab2016-08-25 00:24:08 +0200100 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
101 "be 128).");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 return -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700103 }
104 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Ole Troan07e7eab2016-08-25 00:24:08 +0200106 /* How many, and which bits to grab from the IPv4 DA */
107 if (ip4_prefix_len + ea_bits_len < 32)
108 {
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100109 if (!(flags & MAP_DOMAIN_TRANSLATION))
110 flags |= MAP_DOMAIN_PREFIX;
Ole Troand575e692016-08-25 12:26:47 +0200111 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
112 suffix_len = ea_bits_len;
Ole Troan07e7eab2016-08-25 00:24:08 +0200113 }
114 else
115 {
116 suffix_shift = 0;
117 suffix_len = 32 - ip4_prefix_len;
118 }
119
120 /* EA bits must be within the first 64 bits */
121 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
122 ip6_prefix_len + suffix_len + psid_length > 64))
123 {
124 clib_warning
125 ("Embedded Address bits must be within the first 64 bits of "
126 "the IPv6 prefix");
127 return -1;
128 }
129
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100130 if (mm->is_ce && !(flags & MAP_DOMAIN_TRANSLATION))
131 {
132 clib_warning ("MAP-E CE is not supported yet");
133 return -1;
134 }
135
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 /* Get domain index */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700137 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
138 memset (d, 0, sizeof (*d));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 *map_domain_index = d - mm->domains;
140
141 /* Init domain struct */
142 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
143 d->ip4_prefix_len = ip4_prefix_len;
144 d->ip6_prefix = *ip6_prefix;
145 d->ip6_prefix_len = ip6_prefix_len;
146 d->ip6_src = *ip6_src;
147 d->ip6_src_len = ip6_src_len;
148 d->ea_bits_len = ea_bits_len;
149 d->psid_offset = psid_offset;
150 d->psid_length = psid_length;
151 d->mtu = mtu;
152 d->flags = flags;
Ole Troan07e7eab2016-08-25 00:24:08 +0200153 d->suffix_shift = suffix_shift;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700154 d->suffix_mask = (1 << suffix_len) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
156 d->psid_shift = 16 - psid_length - psid_offset;
157 d->psid_mask = (1 << d->psid_length) - 1;
158 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
159
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100160 /* MAP data-plane object */
161 if (d->flags & MAP_DOMAIN_TRANSLATION)
162 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700163 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
165
166 /* Create ip4 route */
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100167 u8 ip4_pfx_len;
168 ip4_address_t ip4_pfx;
169 if (mm->is_ce)
170 {
171 ip4_pfx_len = 0;
172 ip4_pfx.as_u32 = 0;
173 }
174 else
175 {
176 ip4_pfx_len = d->ip4_prefix_len;
177 ip4_pfx = d->ip4_prefix;
178 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 fib_prefix_t pfx = {
180 .fp_proto = FIB_PROTOCOL_IP4,
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100181 .fp_len = ip4_pfx_len,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100182 .fp_addr = {
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100183 .ip4 = ip4_pfx,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100184 }
185 ,
186 };
187 fib_table_entry_special_dpo_add (0, &pfx,
188 FIB_SOURCE_MAP,
189 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
190 dpo_reset (&dpo_v4);
191
192 /*
Neale Ranns9705c382017-02-20 20:29:41 -0800193 * construct a DPO to use the v6 domain
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100194 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100195 if (d->flags & MAP_DOMAIN_TRANSLATION)
196 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
197 else
198 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
199
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100200 /*
Neale Ranns9705c382017-02-20 20:29:41 -0800201 * Multiple MAP domains may share same source IPv6 TEP. Which is just dandy.
202 * We are not tracking the sharing. So a v4 lookup to find the correct
203 * domain post decap/trnaslate is always done
204 *
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100205 * Create ip6 route. This is a reference counted add. If the prefix
206 * already exists and is MAP sourced, it is now MAP source n+1 times
207 * and will need to be removed n+1 times.
208 */
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100209 u8 ip6_pfx_len;
210 ip6_address_t ip6_pfx;
211 if (mm->is_ce)
212 {
213 ip6_pfx_len = d->ip6_prefix_len;
214 ip6_pfx = d->ip6_prefix;
215 }
216 else
217 {
218 ip6_pfx_len = d->ip6_src_len;
219 ip6_pfx = d->ip6_src;
220 }
Neale Ranns9705c382017-02-20 20:29:41 -0800221 fib_prefix_t pfx6 = {
222 .fp_proto = FIB_PROTOCOL_IP6,
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +0100223 .fp_len = ip6_pfx_len,
224 .fp_addr.ip6 = ip6_pfx,
Neale Ranns9705c382017-02-20 20:29:41 -0800225 };
226
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100227 fib_table_entry_special_dpo_add (0, &pfx6,
228 FIB_SOURCE_MAP,
229 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
230 dpo_reset (&dpo_v6);
231
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 /* Validate packet/byte counters */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700233 map_domain_counter_lock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700235 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
236 {
237 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
238 *map_domain_index);
239 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
240 *map_domain_index);
241 }
242 for (i = 0; i < vec_len (mm->domain_counters); i++)
243 {
244 vlib_validate_combined_counter (&mm->domain_counters[i],
245 *map_domain_index);
246 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
247 }
248 map_domain_counter_unlock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 return 0;
251}
252
253/*
254 * map_delete_domain
255 */
256int
257map_delete_domain (u32 map_domain_index)
258{
259 map_main_t *mm = &map_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 map_domain_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700262 if (pool_is_free_index (mm->domains, map_domain_index))
263 {
264 clib_warning ("MAP domain delete: domain does not exist: %d",
265 map_domain_index);
266 return -1;
267 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700269 d = pool_elt_at_index (mm->domains, map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100271 fib_prefix_t pfx = {
272 .fp_proto = FIB_PROTOCOL_IP4,
273 .fp_len = d->ip4_prefix_len,
274 .fp_addr = {
275 .ip4 = d->ip4_prefix,
276 }
277 ,
278 };
279 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100281 fib_prefix_t pfx6 = {
282 .fp_proto = FIB_PROTOCOL_IP6,
283 .fp_len = d->ip6_src_len,
284 .fp_addr = {
285 .ip6 = d->ip6_src,
286 }
287 ,
288 };
289 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 /* Deleting rules */
292 if (d->rules)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700293 clib_mem_free (d->rules);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700295 pool_put (mm->domains, d);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
297 return 0;
298}
299
300int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700301map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 u8 is_add)
303{
304 map_domain_t *d;
305 map_main_t *mm = &map_main;
306
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700307 if (pool_is_free_index (mm->domains, map_domain_index))
308 {
309 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
310 return -1;
311 }
312 d = pool_elt_at_index (mm->domains, map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
314 /* Rules are only used in 1:1 independent case */
315 if (d->ea_bits_len > 0)
316 return (-1);
317
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700318 if (!d->rules)
319 {
320 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
321 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
322 if (!d->rules)
323 return -1;
324 memset (d->rules, 0, l);
325 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700327 if (psid >= (0x1 << d->psid_length))
328 {
329 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
330 0x1 << d->psid_length);
331 return -1;
332 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700334 if (is_add)
335 {
336 d->rules[psid] = *tep;
337 }
338 else
339 {
340 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
341 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 return 0;
343}
344
345#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -0800346/**
347 * Pre-resolvd per-protocol global next-hops
348 */
349map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
350
351static void
352map_pre_resolve_init (map_main_pre_resolved_t * pr)
353{
354 pr->fei = FIB_NODE_INDEX_INVALID;
355 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
356}
357
358static u8 *
Christophe Fontained3c008d2017-10-02 18:10:54 +0200359format_map_pre_resolve (u8 * s, va_list * ap)
Neale Ranns80823802017-02-20 18:23:41 -0800360{
Christophe Fontained3c008d2017-10-02 18:10:54 +0200361 map_main_pre_resolved_t *pr = va_arg (*ap, map_main_pre_resolved_t *);
Neale Ranns80823802017-02-20 18:23:41 -0800362
363 if (FIB_NODE_INDEX_INVALID != pr->fei)
364 {
365 fib_prefix_t pfx;
366
367 fib_entry_get_prefix (pr->fei, &pfx);
368
369 return (format (s, "%U (%u)",
370 format_ip46_address, &pfx.fp_addr, IP46_TYPE_ANY,
371 pr->dpo.dpoi_index));
372 }
373 else
374 {
375 return (format (s, "un-set"));
376 }
377}
378
379
380/**
381 * Function definition to inform the FIB node that its last lock has gone.
382 */
383static void
384map_last_lock_gone (fib_node_t * node)
385{
386 /*
387 * The MAP is a root of the graph. As such
388 * it never has children and thus is never locked.
389 */
390 ASSERT (0);
391}
392
393static map_main_pre_resolved_t *
394map_from_fib_node (fib_node_t * node)
395{
Neale Ranns80823802017-02-20 18:23:41 -0800396 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
Neale Ranns80823802017-02-20 18:23:41 -0800397 return ((map_main_pre_resolved_t *)
398 (((char *) node) -
399 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
400}
401
402static void
403map_stack (map_main_pre_resolved_t * pr)
404{
405 const dpo_id_t *dpo;
406
407 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
408
409 dpo_copy (&pr->dpo, dpo);
410}
411
412/**
413 * Function definition to backwalk a FIB node
414 */
415static fib_node_back_walk_rc_t
416map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
417{
418 map_stack (map_from_fib_node (node));
419
420 return (FIB_NODE_BACK_WALK_CONTINUE);
421}
422
423/**
424 * Function definition to get a FIB node from its index
425 */
426static fib_node_t *
427map_fib_node_get (fib_node_index_t index)
428{
429 return (&pre_resolved[index].node);
430}
431
432/*
433 * Virtual function table registered by MPLS GRE tunnels
434 * for participation in the FIB object graph.
435 */
436const static fib_node_vft_t map_vft = {
437 .fnv_get = map_fib_node_get,
438 .fnv_last_lock = map_last_lock_gone,
439 .fnv_back_walk = map_back_walk,
440};
441
442static void
443map_fib_resolve (map_main_pre_resolved_t * pr,
444 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
445{
446 fib_prefix_t pfx = {
447 .fp_proto = proto,
448 .fp_len = len,
449 .fp_addr = *addr,
450 };
451
452 pr->fei = fib_table_entry_special_add (0, // default fib
453 &pfx,
Neale Rannsa0558302017-04-13 00:44:52 -0700454 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
Neale Ranns80823802017-02-20 18:23:41 -0800455 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
456 map_stack (pr);
457}
458
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459static void
Neale Ranns69b7aa42017-03-10 03:04:12 -0800460map_fib_unresolve (map_main_pre_resolved_t * pr,
461 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
462{
463 fib_prefix_t pfx = {
464 .fp_proto = proto,
465 .fp_len = len,
466 .fp_addr = *addr,
467 };
468
469 fib_entry_child_remove (pr->fei, pr->sibling);
470
471 fib_table_entry_special_remove (0, // default fib
472 &pfx, FIB_SOURCE_RR);
473 dpo_reset (&pr->dpo);
474
475 pr->fei = FIB_NODE_INDEX_INVALID;
476 pr->sibling = FIB_NODE_INDEX_INVALID;
477}
478
479static void
480map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Neale Ranns80823802017-02-20 18:23:41 -0800482 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700483 {
Neale Ranns80823802017-02-20 18:23:41 -0800484 ip46_address_t addr = {
485 .ip6 = *ip6,
486 };
Neale Ranns69b7aa42017-03-10 03:04:12 -0800487 if (is_del)
488 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
489 FIB_PROTOCOL_IP6, 128, &addr);
490 else
491 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
492 FIB_PROTOCOL_IP6, 128, &addr);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700493 }
Neale Ranns80823802017-02-20 18:23:41 -0800494 if (ip4 && (ip4->as_u32 != 0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700495 {
Neale Ranns80823802017-02-20 18:23:41 -0800496 ip46_address_t addr = {
497 .ip4 = *ip4,
498 };
Neale Ranns69b7aa42017-03-10 03:04:12 -0800499 if (is_del)
500 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
501 FIB_PROTOCOL_IP4, 32, &addr);
502 else
503 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
504 FIB_PROTOCOL_IP4, 32, &addr);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700505 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506}
507#endif
508
509static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700510map_security_check_command_fn (vlib_main_t * vm,
511 unformat_input_t * input,
512 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513{
514 unformat_input_t _line_input, *line_input = &_line_input;
515 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500516 clib_error_t *error = NULL;
517
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700519 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700521
522 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
523 {
524 if (unformat (line_input, "off"))
525 mm->sec_check = false;
526 else if (unformat (line_input, "on"))
527 mm->sec_check = true;
528 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500529 {
530 error = clib_error_return (0, "unknown input `%U'",
531 format_unformat_error, line_input);
532 goto done;
533 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700534 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500535
536done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700537 unformat_free (line_input);
Billy McFalla9a20e72017-02-15 11:39:12 -0500538
539 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540}
541
542static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700543map_security_check_frag_command_fn (vlib_main_t * vm,
544 unformat_input_t * input,
545 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546{
547 unformat_input_t _line_input, *line_input = &_line_input;
548 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500549 clib_error_t *error = NULL;
550
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700552 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700554
555 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
556 {
557 if (unformat (line_input, "off"))
558 mm->sec_check_frag = false;
559 else if (unformat (line_input, "on"))
560 mm->sec_check_frag = true;
561 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500562 {
563 error = clib_error_return (0, "unknown input `%U'",
564 format_unformat_error, line_input);
565 goto done;
566 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700567 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500568
569done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700570 unformat_free (line_input);
Billy McFalla9a20e72017-02-15 11:39:12 -0500571
572 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573}
574
575static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700576map_add_domain_command_fn (vlib_main_t * vm,
577 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578{
579 unformat_input_t _line_input, *line_input = &_line_input;
580 ip4_address_t ip4_prefix;
581 ip6_address_t ip6_prefix;
582 ip6_address_t ip6_src;
Dave Barach042ffb42016-08-12 09:26:47 -0400583 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 u32 num_m_args = 0;
585 /* Optional arguments */
Dave Barach042ffb42016-08-12 09:26:47 -0400586 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 u32 mtu = 0;
588 u8 flags = 0;
589 ip6_src_len = 128;
Billy McFalla9a20e72017-02-15 11:39:12 -0500590 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
592 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700593 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700595
596 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
597 {
598 if (unformat
599 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
600 &ip4_prefix_len))
601 num_m_args++;
602 else
603 if (unformat
604 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
605 &ip6_prefix_len))
606 num_m_args++;
607 else
608 if (unformat
609 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
610 &ip6_src_len))
611 num_m_args++;
612 else
613 if (unformat
614 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
615 num_m_args++;
616 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
617 num_m_args++;
618 else if (unformat (line_input, "psid-offset %d", &psid_offset))
619 num_m_args++;
620 else if (unformat (line_input, "psid-len %d", &psid_length))
621 num_m_args++;
622 else if (unformat (line_input, "mtu %d", &mtu))
623 num_m_args++;
624 else if (unformat (line_input, "map-t"))
625 flags |= MAP_DOMAIN_TRANSLATION;
Ole Troane31d9562017-11-08 11:10:54 +0100626 else if (unformat (line_input, "rfc6052"))
627 flags |= (MAP_DOMAIN_TRANSLATION | MAP_DOMAIN_RFC6052);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700628 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500629 {
630 error = clib_error_return (0, "unknown input `%U'",
631 format_unformat_error, line_input);
632 goto done;
633 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700634 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
636 if (num_m_args < 3)
Billy McFalla9a20e72017-02-15 11:39:12 -0500637 {
638 error = clib_error_return (0, "mandatory argument(s) missing");
639 goto done;
640 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700642 map_create_domain (&ip4_prefix, ip4_prefix_len,
643 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
644 ea_bits_len, psid_offset, psid_length, &map_domain_index,
645 mtu, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Billy McFalla9a20e72017-02-15 11:39:12 -0500647done:
648 unformat_free (line_input);
649
650 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651}
652
653static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700654map_del_domain_command_fn (vlib_main_t * vm,
655 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656{
657 unformat_input_t _line_input, *line_input = &_line_input;
658 u32 num_m_args = 0;
659 u32 map_domain_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500660 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
662 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700663 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700665
666 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
667 {
668 if (unformat (line_input, "index %d", &map_domain_index))
669 num_m_args++;
670 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500671 {
672 error = clib_error_return (0, "unknown input `%U'",
673 format_unformat_error, line_input);
674 goto done;
675 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700676 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677
678 if (num_m_args != 1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500679 {
680 error = clib_error_return (0, "mandatory argument(s) missing");
681 goto done;
682 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700684 map_delete_domain (map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685
Billy McFalla9a20e72017-02-15 11:39:12 -0500686done:
687 unformat_free (line_input);
688
689 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690}
691
692static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700693map_add_rule_command_fn (vlib_main_t * vm,
694 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695{
696 unformat_input_t _line_input, *line_input = &_line_input;
697 ip6_address_t tep;
698 u32 num_m_args = 0;
Dave Barach042ffb42016-08-12 09:26:47 -0400699 u32 psid = 0, map_domain_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500700 clib_error_t *error = NULL;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700701
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700703 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 return 0;
705
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700706 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
707 {
708 if (unformat (line_input, "index %d", &map_domain_index))
709 num_m_args++;
710 else if (unformat (line_input, "psid %d", &psid))
711 num_m_args++;
712 else
713 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
714 num_m_args++;
715 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500716 {
717 error = clib_error_return (0, "unknown input `%U'",
718 format_unformat_error, line_input);
719 goto done;
720 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700721 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
723 if (num_m_args != 3)
Billy McFalla9a20e72017-02-15 11:39:12 -0500724 {
725 error = clib_error_return (0, "mandatory argument(s) missing");
726 goto done;
727 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700729 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
730 {
Billy McFalla9a20e72017-02-15 11:39:12 -0500731 error = clib_error_return (0, "Failing to add Mapping Rule");
732 goto done;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700733 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500734
735done:
736 unformat_free (line_input);
737
738 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739}
740
741#if MAP_SKIP_IP6_LOOKUP
742static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700743map_pre_resolve_command_fn (vlib_main_t * vm,
744 unformat_input_t * input,
745 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746{
747 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns80823802017-02-20 18:23:41 -0800748 ip4_address_t ip4nh, *p_v4 = NULL;
749 ip6_address_t ip6nh, *p_v6 = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500750 clib_error_t *error = NULL;
Neale Ranns69b7aa42017-03-10 03:04:12 -0800751 int is_del = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700753 memset (&ip4nh, 0, sizeof (ip4nh));
754 memset (&ip6nh, 0, sizeof (ip6nh));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
756 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700757 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700760 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
761 {
762 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
Neale Ranns80823802017-02-20 18:23:41 -0800763 p_v4 = &ip4nh;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700764 else
765 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
Neale Ranns80823802017-02-20 18:23:41 -0800766 p_v6 = &ip6nh;
Neale Ranns69b7aa42017-03-10 03:04:12 -0800767 else if (unformat (line_input, "del"))
768 is_del = 1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700769 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500770 {
771 error = clib_error_return (0, "unknown input `%U'",
772 format_unformat_error, line_input);
773 goto done;
774 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700775 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700776
Neale Ranns69b7aa42017-03-10 03:04:12 -0800777 map_pre_resolve (p_v4, p_v6, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778
Billy McFalla9a20e72017-02-15 11:39:12 -0500779done:
780 unformat_free (line_input);
781
782 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783}
784#endif
785
786static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700787map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
788 unformat_input_t * input,
789 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790{
791 unformat_input_t _line_input, *line_input = &_line_input;
792 ip4_address_t icmp_src_address;
793 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500794 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Ole Troancda94822016-01-07 14:37:25 +0100796 mm->icmp4_src_address.as_u32 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
798 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700799 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700801
802 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
803 {
804 if (unformat
805 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
806 mm->icmp4_src_address = icmp_src_address;
807 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500808 {
809 error = clib_error_return (0, "unknown input `%U'",
810 format_unformat_error, line_input);
811 goto done;
812 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700813 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500814
815done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700816 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
Billy McFalla9a20e72017-02-15 11:39:12 -0500818 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819}
820
821static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700822map_icmp_unreachables_command_fn (vlib_main_t * vm,
823 unformat_input_t * input,
824 vlib_cli_command_t * cmd)
Ole Troancda94822016-01-07 14:37:25 +0100825{
826 unformat_input_t _line_input, *line_input = &_line_input;
827 map_main_t *mm = &map_main;
828 int num_m_args = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500829 clib_error_t *error = NULL;
Ole Troancda94822016-01-07 14:37:25 +0100830
831 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700832 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troancda94822016-01-07 14:37:25 +0100833 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700834
835 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
836 {
837 num_m_args++;
838 if (unformat (line_input, "on"))
839 mm->icmp6_enabled = true;
840 else if (unformat (line_input, "off"))
841 mm->icmp6_enabled = false;
842 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500843 {
844 error = clib_error_return (0, "unknown input `%U'",
845 format_unformat_error, line_input);
846 goto done;
847 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700848 }
Ole Troancda94822016-01-07 14:37:25 +0100849
850
851 if (num_m_args != 1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500852 error = clib_error_return (0, "mandatory argument(s) missing");
Ole Troancda94822016-01-07 14:37:25 +0100853
Billy McFalla9a20e72017-02-15 11:39:12 -0500854done:
855 unformat_free (line_input);
856
857 return error;
Ole Troancda94822016-01-07 14:37:25 +0100858}
859
860static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700861map_fragment_command_fn (vlib_main_t * vm,
862 unformat_input_t * input, vlib_cli_command_t * cmd)
Ole Troan9fb87552016-01-13 22:30:43 +0100863{
864 unformat_input_t _line_input, *line_input = &_line_input;
865 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500866 clib_error_t *error = NULL;
Ole Troan9fb87552016-01-13 22:30:43 +0100867
868 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700869 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troan9fb87552016-01-13 22:30:43 +0100870 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700871
872 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
873 {
874 if (unformat (line_input, "inner"))
875 mm->frag_inner = true;
876 else if (unformat (line_input, "outer"))
877 mm->frag_inner = false;
878 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500879 {
880 error = clib_error_return (0, "unknown input `%U'",
881 format_unformat_error, line_input);
882 goto done;
883 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700884 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500885
886done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700887 unformat_free (line_input);
Ole Troan9fb87552016-01-13 22:30:43 +0100888
Billy McFalla9a20e72017-02-15 11:39:12 -0500889 return error;
Ole Troan9fb87552016-01-13 22:30:43 +0100890}
891
892static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700893map_fragment_df_command_fn (vlib_main_t * vm,
894 unformat_input_t * input,
895 vlib_cli_command_t * cmd)
Ole Troan9fb87552016-01-13 22:30:43 +0100896{
897 unformat_input_t _line_input, *line_input = &_line_input;
898 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500899 clib_error_t *error = NULL;
Ole Troan9fb87552016-01-13 22:30:43 +0100900
901 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700902 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troan9fb87552016-01-13 22:30:43 +0100903 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700904
905 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
906 {
907 if (unformat (line_input, "on"))
908 mm->frag_ignore_df = true;
909 else if (unformat (line_input, "off"))
910 mm->frag_ignore_df = false;
911 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500912 {
913 error = clib_error_return (0, "unknown input `%U'",
914 format_unformat_error, line_input);
915 goto done;
916 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700917 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500918
919done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700920 unformat_free (line_input);
Ole Troan9fb87552016-01-13 22:30:43 +0100921
Billy McFalla9a20e72017-02-15 11:39:12 -0500922 return error;
Ole Troan9fb87552016-01-13 22:30:43 +0100923}
924
925static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700926map_traffic_class_command_fn (vlib_main_t * vm,
927 unformat_input_t * input,
928 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929{
930 unformat_input_t _line_input, *line_input = &_line_input;
931 map_main_t *mm = &map_main;
932 u32 tc = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500933 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
935 mm->tc_copy = false;
936
937 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700938 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700940
941 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
942 {
943 if (unformat (line_input, "copy"))
944 mm->tc_copy = true;
945 else if (unformat (line_input, "%x", &tc))
946 mm->tc = tc & 0xff;
947 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500948 {
949 error = clib_error_return (0, "unknown input `%U'",
950 format_unformat_error, line_input);
951 goto done;
952 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700953 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500954
955done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700956 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
Billy McFalla9a20e72017-02-15 11:39:12 -0500958 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959}
960
Ole Troane31d9562017-11-08 11:10:54 +0100961static char *
962map_flags_to_string (u32 flags)
963{
964 if (flags & MAP_DOMAIN_RFC6052)
965 return "rfc6052";
966 if (flags & MAP_DOMAIN_PREFIX)
967 return "prefix";
968 if (flags & MAP_DOMAIN_TRANSLATION)
969 return "map-t";
970 return "";
971}
972
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700974format_map_domain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700976 map_domain_t *d = va_arg (*args, map_domain_t *);
977 bool counters = va_arg (*args, int);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 map_main_t *mm = &map_main;
979 ip6_address_t ip6_prefix;
980
981 if (d->rules)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700982 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983 else
984 ip6_prefix = d->ip6_prefix;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700986 s = format (s,
Ole Troane31d9562017-11-08 11:10:54 +0100987 "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea_bits_len %d "
988 "psid-offset %d psid-len %d mtu %d %s",
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700989 d - mm->domains,
990 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
991 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
992 format_ip6_address, &d->ip6_src, d->ip6_src_len,
993 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
Ole Troane31d9562017-11-08 11:10:54 +0100994 map_flags_to_string (d->flags));
Ole Troan366ac6e2016-01-06 12:40:28 +0100995
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700996 if (counters)
997 {
998 map_domain_counter_lock (mm);
999 vlib_counter_t v;
1000 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
1001 d - mm->domains, &v);
1002 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
1003 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
1004 d - mm->domains, &v);
1005 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
1006 map_domain_counter_unlock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001008 s = format (s, "\n");
1009
1010 if (d->rules)
1011 {
1012 int i;
1013 ip6_address_t dst;
1014 for (i = 0; i < (0x1 << d->psid_length); i++)
1015 {
1016 dst = d->rules[i];
1017 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
1018 continue;
1019 s = format (s,
1020 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
1021 &dst);
1022 }
1023 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 return s;
1025}
1026
1027static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001028format_map_ip4_reass (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029{
1030 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001031 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032 map_ip4_reass_key_t *k = &r->key;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001033 f64 now = vlib_time_now (mm->vlib_main);
1034 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001036 s = format (s,
1037 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
1038 format_ip4_address, &k->src.as_u8, format_ip4_address,
1039 &k->dst.as_u8, k->protocol,
1040 clib_net_to_host_u16 (k->fragment_id),
1041 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042 return s;
1043}
1044
1045static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001046format_map_ip6_reass (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047{
1048 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001049 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050 map_ip6_reass_key_t *k = &r->key;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001051 f64 now = vlib_time_now (mm->vlib_main);
1052 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001054 s = format (s,
1055 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1056 format_ip6_address, &k->src.as_u8, format_ip6_address,
1057 &k->dst.as_u8, k->protocol,
1058 clib_net_to_host_u32 (k->fragment_id), dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 return s;
1060}
1061
1062static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001063show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1064 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065{
1066 unformat_input_t _line_input, *line_input = &_line_input;
1067 map_main_t *mm = &map_main;
1068 map_domain_t *d;
1069 bool counters = false;
1070 u32 map_domain_index = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001071 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
1073 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001074 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001077 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1078 {
1079 if (unformat (line_input, "counters"))
1080 counters = true;
1081 else if (unformat (line_input, "index %d", &map_domain_index))
1082 ;
1083 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001084 {
1085 error = clib_error_return (0, "unknown input `%U'",
1086 format_unformat_error, line_input);
1087 goto done;
1088 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 }
1090
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001091 if (pool_elts (mm->domains) == 0)
1092 vlib_cli_output (vm, "No MAP domains are configured...");
1093
1094 if (map_domain_index == ~0)
1095 {
1096 /* *INDENT-OFF* */
1097 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1098 /* *INDENT-ON* */
1099 }
1100 else
1101 {
1102 if (pool_is_free_index (mm->domains, map_domain_index))
1103 {
Billy McFalla9a20e72017-02-15 11:39:12 -05001104 error = clib_error_return (0, "MAP domain does not exists %d",
1105 map_domain_index);
1106 goto done;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001107 }
1108
1109 d = pool_elt_at_index (mm->domains, map_domain_index);
1110 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1111 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112
Billy McFalla9a20e72017-02-15 11:39:12 -05001113done:
1114 unformat_free (line_input);
1115
1116 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117}
1118
1119static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001120show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1121 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122{
1123 map_main_t *mm = &map_main;
1124 map_ip4_reass_t *f4;
1125 map_ip6_reass_t *f6;
1126
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001127 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001129 /* *INDENT-ON* */
1130 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001132 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133 return (0);
1134}
1135
1136u64
1137map_error_counter_get (u32 node_index, map_error_t map_error)
1138{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001139 vlib_main_t *vm = vlib_get_main ();
1140 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141 vlib_error_main_t *em = &vm->error_main;
1142 vlib_error_t e = error_node->errors[map_error];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001143 vlib_node_t *n = vlib_get_node (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 u32 ci;
1145
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001146 ci = vlib_error_get_code (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147 ASSERT (ci < n->n_errors);
1148 ci += n->error_heap_index;
1149
1150 return (em->counters[ci]);
1151}
1152
1153static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001154show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1155 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156{
1157 map_main_t *mm = &map_main;
1158 map_domain_t *d;
1159 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1160 if (pool_elts (mm->domains) == 0)
Ole Troanc08b0962017-06-26 18:12:37 +02001161 {
1162 vlib_cli_output (vm, "No MAP domains are configured...");
1163 return 0;
1164 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001166 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167 pool_foreach(d, mm->domains, ({
1168 if (d->rules) {
1169 rulecount+= 0x1 << d->psid_length;
1170 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1171 }
1172 domains += sizeof(*d);
1173 domaincount++;
1174 }));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001175 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001177 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1178 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1179 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1180 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181
1182#if MAP_SKIP_IP6_LOOKUP
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001183 vlib_cli_output (vm,
Neale Ranns80823802017-02-20 18:23:41 -08001184 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1185 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1186 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1187
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188#endif
1189
1190 if (mm->tc_copy)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001191 vlib_cli_output (vm, "MAP traffic-class: copy");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 else
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001193 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001195 vlib_cli_output (vm,
1196 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1197 mm->sec_check ? "enabled" : "disabled",
1198 mm->sec_check_frag ? "enabled" : "disabled");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001200 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1201 format_ip4_address, &mm->icmp4_src_address);
1202 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1203 mm->icmp6_enabled ? "enabled" : "disabled");
1204 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1205 mm->frag_inner ? "enabled" : "disabled");
1206 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1207 mm->frag_ignore_df ? "enabled" : "disabled");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
1209 /*
1210 * Counters
1211 */
1212 vlib_combined_counter_main_t *cm = mm->domain_counters;
1213 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1214 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1215 int which, i;
1216 vlib_counter_t v;
1217
1218 memset (total_pkts, 0, sizeof (total_pkts));
1219 memset (total_bytes, 0, sizeof (total_bytes));
1220
1221 map_domain_counter_lock (mm);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001222 vec_foreach (cm, mm->domain_counters)
1223 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224 which = cm - mm->domain_counters;
1225
Neale Ranns1bd01092017-03-15 15:41:17 -04001226 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001227 {
1228 vlib_get_combined_counter (cm, i, &v);
1229 total_pkts[which] += v.packets;
1230 total_bytes[which] += v.bytes;
1231 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232 }
1233 map_domain_counter_unlock (mm);
1234
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001235 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1236 total_pkts[MAP_DOMAIN_COUNTER_TX],
1237 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1238 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1239 total_pkts[MAP_DOMAIN_COUNTER_RX],
1240 total_bytes[MAP_DOMAIN_COUNTER_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001242 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1243 vlib_get_simple_counter (&mm->icmp_relayed, 0));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244
1245 return 0;
1246}
1247
1248static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001249map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1250 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251{
1252 unformat_input_t _line_input, *line_input = &_line_input;
1253 u32 lifetime = ~0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001254 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255 u32 pool_size = ~0;
1256 u64 buffers = ~(0ull);
1257 u8 ip4 = 0, ip6 = 0;
1258
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001259 if (!unformat_user (input, unformat_line_input, line_input))
1260 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001262 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1263 {
Ole Troan58a90a42016-08-04 10:19:17 +02001264 if (unformat (line_input, "lifetime %u", &lifetime))
1265 ;
1266 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1267 ;
1268 else if (unformat (line_input, "pool-size %u", &pool_size))
1269 ;
1270 else if (unformat (line_input, "buffers %llu", &buffers))
1271 ;
1272 else if (unformat (line_input, "ip4"))
1273 ip4 = 1;
1274 else if (unformat (line_input, "ip6"))
1275 ip6 = 1;
1276 else
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001277 {
1278 unformat_free (line_input);
1279 return clib_error_return (0, "invalid input");
1280 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001282 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283
1284 if (!ip4 && !ip6)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001285 return clib_error_return (0, "must specify ip4 and/or ip6");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001287 if (ip4)
1288 {
1289 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1290 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1291 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1292 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1293 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1294 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1295 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1296 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1297 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1298 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1299 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1300 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1301 MAP_IP4_REASS_CONF_BUFFERS_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302 }
1303
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001304 if (ip6)
1305 {
1306 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1307 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1308 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1309 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1310 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1311 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1312 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1313 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1314 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1315 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1316 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1317 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1318 MAP_IP6_REASS_CONF_BUFFERS_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319 }
1320
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001321 if (ip4)
1322 {
1323 u32 reass = 0, packets = 0;
1324 if (pool_size != ~0)
1325 {
1326 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1327 {
1328 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1329 }
1330 else
1331 {
1332 vlib_cli_output (vm,
1333 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1334 reass, packets);
1335 }
1336 }
1337 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1338 {
1339 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1340 {
1341 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1342 }
1343 else
1344 {
1345 vlib_cli_output (vm,
1346 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1347 reass, packets);
1348 }
1349 }
1350 if (lifetime != ~0)
1351 {
1352 if (map_ip4_reass_conf_lifetime (lifetime))
1353 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1354 else
1355 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1356 }
1357 if (buffers != ~(0ull))
1358 {
1359 if (map_ip4_reass_conf_buffers (buffers))
1360 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1361 else
1362 vlib_cli_output (vm, "Setting ip4-reass buffers");
1363 }
1364
1365 if (map_main.ip4_reass_conf_buffers >
1366 map_main.ip4_reass_conf_pool_size *
1367 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1368 {
1369 vlib_cli_output (vm,
1370 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1371 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001373
1374 if (ip6)
1375 {
1376 u32 reass = 0, packets = 0;
1377 if (pool_size != ~0)
1378 {
1379 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1380 {
1381 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1382 }
1383 else
1384 {
1385 vlib_cli_output (vm,
1386 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1387 reass, packets);
1388 }
1389 }
1390 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1391 {
1392 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1393 {
1394 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1395 }
1396 else
1397 {
1398 vlib_cli_output (vm,
1399 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1400 reass, packets);
1401 }
1402 }
1403 if (lifetime != ~0)
1404 {
1405 if (map_ip6_reass_conf_lifetime (lifetime))
1406 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1407 else
1408 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1409 }
1410 if (buffers != ~(0ull))
1411 {
1412 if (map_ip6_reass_conf_buffers (buffers))
1413 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1414 else
1415 vlib_cli_output (vm, "Setting ip6-reass buffers");
1416 }
1417
1418 if (map_main.ip6_reass_conf_buffers >
1419 map_main.ip6_reass_conf_pool_size *
1420 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1421 {
1422 vlib_cli_output (vm,
1423 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1424 }
1425 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001426
1427 return 0;
1428}
1429
1430
1431/*
1432 * packet trace format function
1433 */
1434u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001435format_map_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001437 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1438 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 map_trace_t *t = va_arg (*args, map_trace_t *);
1440 u32 map_domain_index = t->map_domain_index;
1441 u16 port = t->port;
1442
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001443 s =
1444 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1445 clib_net_to_host_u16 (port));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
1447 return s;
1448}
1449
1450static_always_inline map_ip4_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001451map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452{
1453 map_main_t *mm = &map_main;
1454 u32 ri = mm->ip4_reass_hash_table[bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001455 while (ri != MAP_REASS_INDEX_NONE)
1456 {
1457 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1458 if (r->key.as_u64[0] == k->as_u64[0] &&
1459 r->key.as_u64[1] == k->as_u64[1] &&
1460 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1461 {
1462 return r;
1463 }
1464 ri = r->bucket_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001466 return NULL;
1467}
1468
1469#define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1470
1471void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001472map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473{
1474 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001475 map_ip4_reass_get_fragments (r, pi_to_drop);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476
1477 // Unlink in hash bucket
1478 map_ip4_reass_t *r2 = NULL;
1479 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001480 while (r2i != map_ip4_reass_pool_index (r))
1481 {
1482 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1483 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1484 r2i = r2->bucket_next;
1485 }
1486 if (r2)
1487 {
1488 r2->bucket_next = r->bucket_next;
1489 }
1490 else
1491 {
1492 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1493 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494
1495 // Unlink in list
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001496 if (r->fifo_next == map_ip4_reass_pool_index (r))
1497 {
1498 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1499 }
1500 else
1501 {
1502 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1503 mm->ip4_reass_fifo_last = r->fifo_prev;
1504 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1505 r->fifo_next;
1506 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1507 r->fifo_prev;
1508 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001509
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001510 pool_put (mm->ip4_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511 mm->ip4_reass_allocated--;
1512}
1513
1514map_ip4_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001515map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1516 u8 protocol, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001518 map_ip4_reass_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519 map_main_t *mm = &map_main;
1520 map_ip4_reass_key_t k = {.src.data_u32 = src,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001521 .dst.data_u32 = dst,
1522 .fragment_id = fragment_id,
1523 .protocol = protocol
1524 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001525
1526 u32 h = 0;
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +02001527#ifdef clib_crc32c_uses_intrinsics
1528 h = clib_crc32c ((u8 *) k.as_u32, 16);
1529#else
1530 u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1531 h = clib_xxhash (tmp);
1532#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533 h = h >> (32 - mm->ip4_reass_ht_log2len);
1534
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001535 f64 now = vlib_time_now (mm->vlib_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001536
1537 //Cache garbage collection
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001538 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1539 {
1540 map_ip4_reass_t *last =
1541 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1542 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1543 map_ip4_reass_free (last, pi_to_drop);
1544 else
1545 break;
1546 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001548 if ((r = map_ip4_reass_lookup (&k, h, now)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549 return r;
1550
1551 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1552 return NULL;
1553
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001554 pool_get (mm->ip4_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555 mm->ip4_reass_allocated++;
1556 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001557 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558 r->fragments[i] = ~0;
1559
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001560 u32 ri = map_ip4_reass_pool_index (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561
1562 //Link in new bucket
1563 r->bucket = h;
1564 r->bucket_next = mm->ip4_reass_hash_table[h];
1565 mm->ip4_reass_hash_table[h] = ri;
1566
1567 //Link in fifo
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001568 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1569 {
1570 r->fifo_next =
1571 pool_elt_at_index (mm->ip4_reass_pool,
1572 mm->ip4_reass_fifo_last)->fifo_next;
1573 r->fifo_prev = mm->ip4_reass_fifo_last;
1574 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1575 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1576 }
1577 else
1578 {
1579 r->fifo_next = r->fifo_prev = ri;
1580 mm->ip4_reass_fifo_last = ri;
1581 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001582
1583 //Set other fields
1584 r->ts = now;
1585 r->key = k;
1586 r->port = -1;
1587#ifdef MAP_IP4_REASS_COUNT_BYTES
1588 r->expected_total = 0xffff;
1589 r->forwarded = 0;
1590#endif
1591
1592 return r;
1593}
1594
1595int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001596map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001597{
1598 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1599 return -1;
1600
1601 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001602 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1603 if (r->fragments[i] == ~0)
1604 {
1605 r->fragments[i] = pi;
1606 map_main.ip4_reass_buffered_counter++;
1607 return 0;
1608 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001609 return -1;
1610}
1611
1612static_always_inline map_ip6_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001613map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614{
1615 map_main_t *mm = &map_main;
1616 u32 ri = mm->ip6_reass_hash_table[bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001617 while (ri != MAP_REASS_INDEX_NONE)
1618 {
1619 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1620 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1621 r->key.as_u64[0] == k->as_u64[0] &&
1622 r->key.as_u64[1] == k->as_u64[1] &&
1623 r->key.as_u64[2] == k->as_u64[2] &&
1624 r->key.as_u64[3] == k->as_u64[3] &&
1625 r->key.as_u64[4] == k->as_u64[4])
1626 return r;
1627 ri = r->bucket_next;
1628 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629 return NULL;
1630}
1631
1632#define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1633
1634void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001635map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001636{
1637 map_main_t *mm = &map_main;
1638 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001639 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1640 if (r->fragments[i].pi != ~0)
1641 {
1642 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1643 r->fragments[i].pi = ~0;
1644 map_main.ip6_reass_buffered_counter--;
1645 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646
1647 // Unlink in hash bucket
1648 map_ip6_reass_t *r2 = NULL;
1649 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001650 while (r2i != map_ip6_reass_pool_index (r))
1651 {
1652 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1653 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1654 r2i = r2->bucket_next;
1655 }
1656 if (r2)
1657 {
1658 r2->bucket_next = r->bucket_next;
1659 }
1660 else
1661 {
1662 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1663 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001664
1665 // Unlink in list
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001666 if (r->fifo_next == map_ip6_reass_pool_index (r))
1667 {
1668 //Single element in the list, list is now empty
1669 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1670 }
1671 else
1672 {
1673 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1674 mm->ip6_reass_fifo_last = r->fifo_prev;
1675 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1676 r->fifo_next;
1677 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1678 r->fifo_prev;
1679 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001680
1681 // Free from pool if necessary
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001682 pool_put (mm->ip6_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683 mm->ip6_reass_allocated--;
1684}
1685
1686map_ip6_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001687map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1688 u8 protocol, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001689{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001690 map_ip6_reass_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691 map_main_t *mm = &map_main;
1692 map_ip6_reass_key_t k = {
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001693 .src = *src,
1694 .dst = *dst,
1695 .fragment_id = fragment_id,
1696 .protocol = protocol
1697 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
1699 u32 h = 0;
1700 int i;
Christophe Fontaineb4bd28a2017-05-31 11:27:19 +02001701
1702#ifdef clib_crc32c_uses_intrinsics
1703 h = clib_crc32c ((u8 *) k.as_u32, 40);
1704#else
1705 u64 tmp =
1706 k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1707 h = clib_xxhash (tmp);
1708#endif
1709
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710 h = h >> (32 - mm->ip6_reass_ht_log2len);
1711
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001712 f64 now = vlib_time_now (mm->vlib_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001713
1714 //Cache garbage collection
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001715 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1716 {
1717 map_ip6_reass_t *last =
1718 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1719 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1720 map_ip6_reass_free (last, pi_to_drop);
1721 else
1722 break;
1723 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001724
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001725 if ((r = map_ip6_reass_lookup (&k, h, now)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726 return r;
1727
1728 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1729 return NULL;
1730
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001731 pool_get (mm->ip6_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001732 mm->ip6_reass_allocated++;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001733 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1734 {
1735 r->fragments[i].pi = ~0;
1736 r->fragments[i].next_data_len = 0;
1737 r->fragments[i].next_data_offset = 0;
1738 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001740 u32 ri = map_ip6_reass_pool_index (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001741
1742 //Link in new bucket
1743 r->bucket = h;
1744 r->bucket_next = mm->ip6_reass_hash_table[h];
1745 mm->ip6_reass_hash_table[h] = ri;
1746
1747 //Link in fifo
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001748 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1749 {
1750 r->fifo_next =
1751 pool_elt_at_index (mm->ip6_reass_pool,
1752 mm->ip6_reass_fifo_last)->fifo_next;
1753 r->fifo_prev = mm->ip6_reass_fifo_last;
1754 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1755 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1756 }
1757 else
1758 {
1759 r->fifo_next = r->fifo_prev = ri;
1760 mm->ip6_reass_fifo_last = ri;
1761 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001762
1763 //Set other fields
1764 r->ts = now;
1765 r->key = k;
1766 r->ip4_header.ip_version_and_header_length = 0;
1767#ifdef MAP_IP6_REASS_COUNT_BYTES
1768 r->expected_total = 0xffff;
1769 r->forwarded = 0;
1770#endif
1771 return r;
1772}
1773
1774int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001775map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1776 u16 data_offset, u16 next_data_offset,
1777 u8 * data_start, u16 data_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001778{
1779 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1780 u16 copied_len = (data_len > 20) ? 20 : data_len;
1781
1782 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1783 return -1;
1784
1785 //Lookup for fragments for the current buffer
1786 //and the one before that
1787 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001788 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1789 {
1790 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1791 {
1792 prev_f = &r->fragments[i]; // This is buffer for previous packet
1793 }
1794 else if (r->fragments[i].next_data_offset == next_data_offset)
1795 {
1796 f = &r->fragments[i]; // This is a buffer for the current packet
1797 }
1798 else if (r->fragments[i].next_data_offset == 0)
1799 { //Available
1800 if (f == NULL)
1801 f = &r->fragments[i];
1802 else if (prev_f == NULL)
1803 prev_f = &r->fragments[i];
1804 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001805 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001806
1807 if (!f || f->pi != ~0)
1808 return -1;
1809
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001810 if (data_offset)
1811 {
1812 if (!prev_f)
1813 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001814
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001815 clib_memcpy (prev_f->next_data, data_start, copied_len);
1816 prev_f->next_data_len = copied_len;
1817 prev_f->next_data_offset = data_offset;
1818 }
1819 else
1820 {
1821 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1822 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001823
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001824 if (r->ip4_header.ip_version_and_header_length == 0)
1825 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1826 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001827
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001828 if (data_len > 20)
1829 {
1830 f->next_data_offset = next_data_offset;
1831 f->pi = pi;
1832 map_main.ip6_reass_buffered_counter++;
1833 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001834 return 0;
1835}
1836
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001837void
1838map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001839{
1840 map_main_t *mm = &map_main;
1841 int i;
1842
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001843 if (dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001844 *dropped_packets = mm->ip4_reass_buffered_counter;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001845 if (trashed_reass)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001846 *trashed_reass = mm->ip4_reass_allocated;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001847 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1848 {
1849 u16 ri = mm->ip4_reass_fifo_last;
1850 do
1851 {
1852 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1853 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1854 if (r->fragments[i] != ~0)
1855 map_ip4_drop_pi (r->fragments[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001856
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001857 ri = r->fifo_next;
1858 pool_put (mm->ip4_reass_pool, r);
1859 }
1860 while (ri != mm->ip4_reass_fifo_last);
1861 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001862
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001863 vec_free (mm->ip4_reass_hash_table);
1864 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1865 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001866 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001867 pool_free (mm->ip4_reass_pool);
1868 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001869
1870 mm->ip4_reass_allocated = 0;
1871 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1872 mm->ip4_reass_buffered_counter = 0;
1873}
1874
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001875u8
1876map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001877{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001878 u32 desired_size = (u32) (pool_size * ht_ratio);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001879 u8 i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001880 for (i = 1; i < 31; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001881 if ((1 << i) >= desired_size)
1882 return i;
1883 return 4;
1884}
1885
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001886int
1887map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1888 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001889{
1890 map_main_t *mm = &map_main;
1891 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1892 return -1;
1893
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001894 map_ip4_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001895 mm->ip4_reass_conf_ht_ratio = ht_ratio;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001896 mm->ip4_reass_ht_log2len =
1897 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1898 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1899 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001900 return 0;
1901}
1902
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001903int
1904map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1905 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906{
1907 map_main_t *mm = &map_main;
1908 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1909 return -1;
1910
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001911 map_ip4_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001912 mm->ip4_reass_conf_pool_size = pool_size;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001913 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1914 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001915 return 0;
1916}
1917
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001918int
1919map_ip4_reass_conf_lifetime (u16 lifetime_ms)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920{
1921 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1922 return 0;
1923}
1924
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001925int
1926map_ip4_reass_conf_buffers (u32 buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001927{
1928 map_main.ip4_reass_conf_buffers = buffers;
1929 return 0;
1930}
1931
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001932void
1933map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001934{
1935 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001936 if (dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001937 *dropped_packets = mm->ip6_reass_buffered_counter;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001938 if (trashed_reass)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001939 *trashed_reass = mm->ip6_reass_allocated;
1940 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001941 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1942 {
1943 u16 ri = mm->ip6_reass_fifo_last;
1944 do
1945 {
1946 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1947 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1948 if (r->fragments[i].pi != ~0)
1949 map_ip6_drop_pi (r->fragments[i].pi);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001950
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001951 ri = r->fifo_next;
1952 pool_put (mm->ip6_reass_pool, r);
1953 }
1954 while (ri != mm->ip6_reass_fifo_last);
1955 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1956 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001957
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001958 vec_free (mm->ip6_reass_hash_table);
1959 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1960 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001961 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001962 pool_free (mm->ip6_reass_pool);
1963 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001964
1965 mm->ip6_reass_allocated = 0;
1966 mm->ip6_reass_buffered_counter = 0;
1967}
1968
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001969int
1970map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1971 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972{
1973 map_main_t *mm = &map_main;
1974 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1975 return -1;
1976
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001977 map_ip6_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001978 mm->ip6_reass_conf_ht_ratio = ht_ratio;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001979 mm->ip6_reass_ht_log2len =
1980 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1981 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1982 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001983 return 0;
1984}
1985
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001986int
1987map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1988 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001989{
1990 map_main_t *mm = &map_main;
1991 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1992 return -1;
1993
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001994 map_ip6_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001995 mm->ip6_reass_conf_pool_size = pool_size;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001996 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1997 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001998 return 0;
1999}
2000
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002001int
2002map_ip6_reass_conf_lifetime (u16 lifetime_ms)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002003{
2004 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
2005 return 0;
2006}
2007
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002008int
2009map_ip6_reass_conf_buffers (u32 buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002010{
2011 map_main.ip6_reass_conf_buffers = buffers;
2012 return 0;
2013}
2014
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002015/* *INDENT-OFF* */
Ole Troan96eefd42016-09-12 12:25:30 +02002016
2017/*?
2018 * Configure MAP reassembly behaviour
2019 *
2020 * @cliexpar
2021 * @cliexstart{map params reassembly}
2022 * @cliexend
2023 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002024VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2025 .path = "map params reassembly",
Ole Troan96eefd42016-09-12 12:25:30 +02002026 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2027 "[pool-size <pool-size>] [buffers <buffers>] "
2028 "[ht-ratio <ht-ratio>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029 .function = map_params_reass_command_fn,
2030};
2031
Ole Troan96eefd42016-09-12 12:25:30 +02002032/*?
2033 * Set or copy the IP TOS/Traffic Class field
2034 *
2035 * @cliexpar
2036 * @cliexstart{map params traffic-class}
2037 *
2038 * This command is used to set the traffic-class field in translated
2039 * or encapsulated packets. If copy is specifed (the default) then the
2040 * traffic-class/TOS field is copied from the original packet to the
2041 * translated / encapsulating header.
2042 * @cliexend
2043 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002044VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2045 .path = "map params traffic-class",
Ole Troan96eefd42016-09-12 12:25:30 +02002046 .short_help = "map params traffic-class {0x0-0xff | copy}",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002047 .function = map_traffic_class_command_fn,
2048};
2049
Ole Troan96eefd42016-09-12 12:25:30 +02002050/*?
2051 * Bypass IP4/IP6 lookup
2052 *
2053 * @cliexpar
2054 * @cliexstart{map params pre-resolve}
2055 *
2056 * Bypass a second FIB lookup of the translated or encapsulated
2057 * packet, and forward the packet directly to the specified
2058 * next-hop. This optimization trades forwarding flexibility for
2059 * performance.
2060 * @cliexend
2061 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002062VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2063 .path = "map params pre-resolve",
Ole Troan96eefd42016-09-12 12:25:30 +02002064 .short_help = " map params pre-resolve {ip4-nh <address>} "
2065 "| {ip6-nh <address>}",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002066 .function = map_pre_resolve_command_fn,
2067};
2068
Ole Troan96eefd42016-09-12 12:25:30 +02002069/*?
2070 * Enable or disable the MAP-E inbound security check
2071 *
2072 * @cliexpar
2073 * @cliexstart{map params security-check}
2074 *
2075 * By default, a decapsulated packet's IPv4 source address will be
2076 * verified against the outer header's IPv6 source address. Disabling
2077 * this feature will allow IPv4 source address spoofing.
2078 * @cliexend
2079 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002080VLIB_CLI_COMMAND(map_security_check_command, static) = {
2081 .path = "map params security-check",
Ole Troan96eefd42016-09-12 12:25:30 +02002082 .short_help = "map params security-check on|off",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002083 .function = map_security_check_command_fn,
2084};
2085
Ole Troan96eefd42016-09-12 12:25:30 +02002086/*?
2087 * Specifiy the IPv4 source address used for relayed ICMP error messages
2088 *
2089 * @cliexpar
2090 * @cliexstart{map params icmp source-address}
2091 *
2092 * This command specifies which IPv4 source address (must be local to
2093 * the system), that is used for relayed received IPv6 ICMP error
2094 * messages.
2095 * @cliexend
2096 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002097VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
Ole Troancda94822016-01-07 14:37:25 +01002098 .path = "map params icmp source-address",
Ole Troan96eefd42016-09-12 12:25:30 +02002099 .short_help = "map params icmp source-address <ip4-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002100 .function = map_icmp_relay_source_address_command_fn,
2101};
2102
Ole Troan96eefd42016-09-12 12:25:30 +02002103/*?
2104 * Send IPv6 ICMP unreachables
2105 *
2106 * @cliexpar
2107 * @cliexstart{map params icmp6 unreachables}
2108 *
2109 * Send IPv6 ICMP unreachable messages back if security check fails or
2110 * no MAP domain exists.
2111 * @cliexend
2112 ?*/
Ole Troancda94822016-01-07 14:37:25 +01002113VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
Ole Troan9fb87552016-01-13 22:30:43 +01002114 .path = "map params icmp6 unreachables",
Ole Troan96eefd42016-09-12 12:25:30 +02002115 .short_help = "map params icmp6 unreachables {on|off}",
Ole Troancda94822016-01-07 14:37:25 +01002116 .function = map_icmp_unreachables_command_fn,
2117};
2118
Ole Troan96eefd42016-09-12 12:25:30 +02002119/*?
2120 * Configure MAP fragmentation behaviour
2121 *
2122 * @cliexpar
2123 * @cliexstart{map params fragment}
2124 * @cliexend
2125 ?*/
Ole Troan9fb87552016-01-13 22:30:43 +01002126VLIB_CLI_COMMAND(map_fragment_command, static) = {
2127 .path = "map params fragment",
Ole Troan96eefd42016-09-12 12:25:30 +02002128 .short_help = "map params fragment inner|outer",
Ole Troan9fb87552016-01-13 22:30:43 +01002129 .function = map_fragment_command_fn,
2130};
2131
Ole Troan96eefd42016-09-12 12:25:30 +02002132/*?
2133 * Ignore the IPv4 Don't fragment bit
2134 *
2135 * @cliexpar
2136 * @cliexstart{map params fragment ignore-df}
2137 *
2138 * Allows fragmentation of the IPv4 packet even if the DF bit is
2139 * set. The choice between inner or outer fragmentation of tunnel
2140 * packets is complicated. The benefit of inner fragmentation is that
2141 * the ultimate endpoint must reassemble, instead of the tunnel
2142 * endpoint.
2143 * @cliexend
2144 ?*/
Ole Troan9fb87552016-01-13 22:30:43 +01002145VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
2146 .path = "map params fragment ignore-df",
Ole Troan96eefd42016-09-12 12:25:30 +02002147 .short_help = "map params fragment ignore-df on|off",
Ole Troan9fb87552016-01-13 22:30:43 +01002148 .function = map_fragment_df_command_fn,
2149};
2150
Ole Troan96eefd42016-09-12 12:25:30 +02002151/*?
2152 * Specifiy if the inbound security check should be done on fragments
2153 *
2154 * @cliexpar
2155 * @cliexstart{map params security-check fragments}
2156 *
2157 * Typically the inbound on-decapsulation security check is only done
2158 * on the first packet. The packet that contains the L4
2159 * information. While a security check on every fragment is possible,
2160 * it has a cost. State must be created on the first fragment.
2161 * @cliexend
2162 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002163VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
2164 .path = "map params security-check fragments",
Ole Troan96eefd42016-09-12 12:25:30 +02002165 .short_help = "map params security-check fragments on|off",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002166 .function = map_security_check_frag_command_fn,
2167};
2168
Ole Troan96eefd42016-09-12 12:25:30 +02002169/*?
2170 * Add MAP domain
2171 *
2172 * @cliexpar
2173 * @cliexstart{map add domain}
2174 * @cliexend
2175 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002176VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2177 .path = "map add domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002178 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2179 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +01002180 "[map-t] [map-ce] [mtu <mtu>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002181 .function = map_add_domain_command_fn,
2182};
2183
Ole Troan96eefd42016-09-12 12:25:30 +02002184/*?
2185 * Add MAP rule to a domain
2186 *
2187 * @cliexpar
2188 * @cliexstart{map add rule}
2189 * @cliexend
2190 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002191VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2192 .path = "map add rule",
Ole Troan96eefd42016-09-12 12:25:30 +02002193 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002194 .function = map_add_rule_command_fn,
2195};
2196
Ole Troan96eefd42016-09-12 12:25:30 +02002197/*?
2198 * Delete MAP domain
2199 *
2200 * @cliexpar
2201 * @cliexstart{map del domain}
2202 * @cliexend
2203 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002204VLIB_CLI_COMMAND(map_del_command, static) = {
2205 .path = "map del domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002206 .short_help = "map del domain index <domain>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002207 .function = map_del_domain_command_fn,
2208};
2209
Ole Troan96eefd42016-09-12 12:25:30 +02002210/*?
2211 * Show MAP domains
2212 *
2213 * @cliexpar
2214 * @cliexstart{show map domain}
2215 * @cliexend
2216 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002217VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2218 .path = "show map domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002219 .short_help = "show map domain index <n> [counters]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002220 .function = show_map_domain_command_fn,
2221};
2222
Ole Troan96eefd42016-09-12 12:25:30 +02002223/*?
2224 * Show MAP statistics
2225 *
2226 * @cliexpar
2227 * @cliexstart{show map stats}
2228 * @cliexend
2229 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002230VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2231 .path = "show map stats",
Ole Troan96eefd42016-09-12 12:25:30 +02002232 .short_help = "show map stats",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002233 .function = show_map_stats_command_fn,
2234};
2235
Ole Troan96eefd42016-09-12 12:25:30 +02002236/*?
2237 * Show MAP fragmentation information
2238 *
2239 * @cliexpar
2240 * @cliexstart{show map fragments}
2241 * @cliexend
2242 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002243VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2244 .path = "show map fragments",
Ole Troan96eefd42016-09-12 12:25:30 +02002245 .short_help = "show map fragments",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002246 .function = show_map_fragments_command_fn,
2247};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002248/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002249
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +01002250static clib_error_t *
2251map_config (vlib_main_t * vm, unformat_input_t * input)
2252{
2253 map_main_t *mm = &map_main;
2254 u8 is_ce = false;
2255
2256 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2257 {
2258 if (unformat (input, "customer edge"))
2259 is_ce = true;
2260 else
2261 return clib_error_return (0, "unknown input '%U'",
2262 format_unformat_error, input);
2263 }
2264
2265 mm->is_ce = is_ce;
2266
2267 return 0;
2268}
2269
2270VLIB_CONFIG_FUNCTION (map_config, "map");
2271
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272/*
2273 * map_init
2274 */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002275clib_error_t *
2276map_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002277{
2278 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002279 mm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002280 mm->vlib_main = vm;
2281
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002282#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -08002283 fib_protocol_t proto;
2284
2285 FOR_EACH_FIB_PROTOCOL (proto)
2286 {
2287 map_pre_resolve_init (&pre_resolved[proto]);
2288 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002289#endif
2290
2291 /* traffic class */
2292 mm->tc = 0;
2293 mm->tc_copy = true;
2294
2295 /* Inbound security check */
2296 mm->sec_check = true;
2297 mm->sec_check_frag = false;
2298
Ole Troancda94822016-01-07 14:37:25 +01002299 /* ICMP6 Type 1, Code 5 for security check failure */
2300 mm->icmp6_enabled = false;
2301
Juraj Sloboda0ae15ed2017-12-19 20:57:48 +01002302 mm->is_ce = false;
2303
Ole Troan9fb87552016-01-13 22:30:43 +01002304 /* Inner or outer fragmentation */
2305 mm->frag_inner = false;
2306 mm->frag_ignore_df = false;
2307
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002308 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002309 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2310 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2311
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002312 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2313 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002314
2315 /* IP4 virtual reassembly */
2316 mm->ip4_reass_hash_table = 0;
2317 mm->ip4_reass_pool = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002318 mm->ip4_reass_lock =
2319 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
dongjuan01e078f2017-09-07 14:58:56 +08002320 *mm->ip4_reass_lock = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002321 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2322 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2323 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2324 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002325 mm->ip4_reass_ht_log2len =
2326 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2327 mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002328 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002329 map_ip4_reass_reinit (NULL, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002330
2331 /* IP6 virtual reassembly */
2332 mm->ip6_reass_hash_table = 0;
2333 mm->ip6_reass_pool = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002334 mm->ip6_reass_lock =
2335 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
dongjuan01e078f2017-09-07 14:58:56 +08002336 *mm->ip6_reass_lock = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002337 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2338 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2339 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2340 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002341 mm->ip6_reass_ht_log2len =
2342 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2343 mm->ip6_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002344 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002345 map_ip6_reass_reinit (NULL, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002346
Neale Ranns80823802017-02-20 18:23:41 -08002347#ifdef MAP_SKIP_IP6_LOOKUP
2348 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2349#endif
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002350 map_dpo_module_init ();
2351
Ed Warnickecb9cada2015-12-08 15:45:58 -07002352 return 0;
2353}
2354
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002355VLIB_INIT_FUNCTION (map_init);
2356
2357/*
2358 * fd.io coding-style-patch-verification: ON
2359 *
2360 * Local Variables:
2361 * eval: (c-set-style "gnu")
2362 * End:
2363 */