2e89286ddc435abfc03c24add9c47ab68d958b5d
[vpp.git] / vnet / vnet / lisp-cp / control.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vlibmemory/api.h>
17 #include <vnet/lisp-cp/control.h>
18 #include <vnet/lisp-cp/packets.h>
19 #include <vnet/lisp-cp/lisp_msg_serdes.h>
20 #include <vnet/lisp-gpe/lisp_gpe.h>
21 #include <vnet/fib/fib_entry.h>
22 #include <vnet/fib/fib_table.h>
23
24 typedef struct
25 {
26   u8 is_resend;
27   gid_address_t seid;
28   gid_address_t deid;
29   u8 smr_invoked;
30 } map_request_args_t;
31
32 u8
33 vnet_lisp_get_map_request_mode (void)
34 {
35   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
36   return lcm->map_request_mode;
37 }
38
39 static int
40 queue_map_request (gid_address_t * seid, gid_address_t * deid,
41                    u8 smr_invoked, u8 is_resend);
42
43 ip_interface_address_t *
44 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
45                                           u32 sw_if_index, u8 loop)
46 {
47   vnet_main_t *vnm = vnet_get_main ();
48   vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
49   if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
50     sw_if_index = swif->unnumbered_sw_if_index;
51   u32 ia =
52     (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
53     vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
54     (u32) ~ 0;
55   return pool_elt_at_index ((lm)->if_address_pool, ia);
56 }
57
58 void *
59 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
60                                 u8 version)
61 {
62   ip_interface_address_t *ia;
63
64   ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
65   if (!ia)
66     return 0;
67   return ip_interface_address_get_address (lm, ia);
68 }
69
70 int
71 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
72                                    u8 version, ip_address_t * result)
73 {
74   ip_lookup_main_t *lm;
75   void *addr;
76
77   lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
78   addr = ip_interface_get_first_address (lm, sw_if_index, version);
79   if (!addr)
80     return 0;
81
82   ip_address_set (result, addr, version);
83   return 1;
84 }
85
86 /**
87  * convert from a LISP address to a FIB prefix
88  */
89 void
90 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
91 {
92   if (addr->version == IP4)
93     {
94       prefix->fp_len = 32;
95       prefix->fp_proto = FIB_PROTOCOL_IP4;
96       memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
97       memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
98     }
99   else
100     {
101       prefix->fp_len = 128;
102       prefix->fp_proto = FIB_PROTOCOL_IP6;
103       memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
104     }
105 }
106
107 /**
108  * convert from a LISP to a FIB prefix
109  */
110 void
111 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
112                          fib_prefix_t * fib_prefix)
113 {
114   ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
115   fib_prefix->fp_len = ip_prefix->len;
116 }
117
118 /**
119  * Find the sw_if_index of the interface that would be used to egress towards
120  * dst.
121  */
122 u32
123 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
124 {
125   fib_node_index_t fei;
126   fib_prefix_t prefix;
127
128   ip_address_to_fib_prefix (dst, &prefix);
129
130   fei = fib_table_lookup (0, &prefix);
131
132   return (fib_entry_get_resolving_interface (fei));
133 }
134
135 /**
136  * Find first IP of the interface that would be used to egress towards dst.
137  * Returns 1 if the address is found 0 otherwise.
138  */
139 int
140 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
141                                     ip_address_t * result)
142 {
143   u32 si;
144   ip_lookup_main_t *lm;
145   void *addr = 0;
146   u8 ipver;
147
148   ASSERT (result != 0);
149
150   ipver = ip_addr_version (dst);
151
152   lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
153   si = ip_fib_get_egress_iface_for_dst (lcm, dst);
154
155   if ((u32) ~ 0 == si)
156     return 0;
157
158   /* find the first ip address */
159   addr = ip_interface_get_first_address (lm, si, ipver);
160   if (0 == addr)
161     return 0;
162
163   ip_address_set (result, addr, ipver);
164   return 1;
165 }
166
167 static int
168 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
169 {
170   uword *dp_table, *intf;
171   vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
172
173   if (!is_l2)
174     {
175       dp_table = hash_get (lcm->table_id_by_vni, vni);
176
177       if (!dp_table)
178         {
179           clib_warning ("vni %d not associated to a vrf!", vni);
180           return VNET_API_ERROR_INVALID_VALUE;
181         }
182     }
183   else
184     {
185       dp_table = hash_get (lcm->bd_id_by_vni, vni);
186       if (!dp_table)
187         {
188           clib_warning ("vni %d not associated to a bridge domain!", vni);
189           return VNET_API_ERROR_INVALID_VALUE;
190         }
191     }
192
193   intf = hash_get (is_l2 ? lcm->l2_dp_intf_by_vni : lcm->dp_intf_by_vni, vni);
194
195   /* enable/disable data-plane interface */
196   if (is_add)
197     {
198       /* create interface */
199       if (!intf)
200         {
201           ai->is_add = 1;
202           ai->vni = vni;
203           ai->is_l2 = is_l2;
204           ai->dp_table = dp_table[0];
205
206           vnet_lisp_gpe_add_del_iface (ai, 0);
207
208           /* keep track of vnis for which interfaces have been created */
209           hash_set (lcm->dp_intf_by_vni, vni, 1);
210         }
211     }
212   else
213     {
214       if (intf == 0)
215         {
216           clib_warning ("interface for vni %d doesn't exist!", vni);
217           return VNET_API_ERROR_INVALID_VALUE;
218         }
219
220       ai->is_add = 0;
221       ai->vni = vni;
222       ai->dp_table = dp_table[0];
223       vnet_lisp_gpe_add_del_iface (ai, 0);
224       hash_unset (lcm->dp_intf_by_vni, vni);
225     }
226
227   return 0;
228 }
229
230 static void
231 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
232 {
233   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
234   fwd_entry_t *fe = 0;
235   uword *feip = 0;
236   memset (a, 0, sizeof (*a));
237
238   feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
239   if (!feip)
240     return;
241
242   fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
243
244   /* delete dp fwd entry */
245   u32 sw_if_index;
246   a->is_add = 0;
247   a->locator_pairs = fe->locator_pairs;
248   a->vni = gid_address_vni (&fe->deid);
249   gid_address_copy (&a->rmt_eid, &fe->deid);
250
251   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
252
253   /* delete entry in fwd table */
254   hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
255   vec_free (fe->locator_pairs);
256   pool_put (lcm->fwd_entry_pool, fe);
257 }
258
259 /**
260  * Finds first remote locator with best (lowest) priority that has a local
261  * peer locator with an underlying route to it.
262  *
263  */
264 static u32
265 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
266                    mapping_t * rmt_map, locator_pair_t ** locator_pairs)
267 {
268   u32 i, limitp = 0, li, found = 0, esi;
269   locator_set_t *rmt_ls, *lcl_ls;
270   ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
271   locator_t *lp, *rmt = 0;
272   uword *checked = 0;
273   locator_pair_t pair;
274
275   rmt_ls =
276     pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
277   lcl_ls =
278     pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
279
280   if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
281     return 0;
282
283   while (1)
284     {
285       rmt = 0;
286
287       /* find unvisited remote locator with best priority */
288       for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
289         {
290           if (0 != hash_get (checked, i))
291             continue;
292
293           li = vec_elt (rmt_ls->locator_indices, i);
294           lp = pool_elt_at_index (lcm->locator_pool, li);
295
296           /* we don't support non-IP locators for now */
297           if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
298             continue;
299
300           if ((found && lp->priority == limitp)
301               || (!found && lp->priority >= limitp))
302             {
303               rmt = lp;
304
305               /* don't search for locators with lower priority and don't
306                * check this locator again*/
307               limitp = lp->priority;
308               hash_set (checked, i, 1);
309               break;
310             }
311         }
312       /* check if a local locator with a route to remote locator exists */
313       if (rmt != 0)
314         {
315           /* find egress sw_if_index for rmt locator */
316           esi =
317             ip_fib_get_egress_iface_for_dst (lcm,
318                                              &gid_address_ip (&rmt->address));
319           if ((u32) ~ 0 == esi)
320             continue;
321
322           for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
323             {
324               li = vec_elt (lcl_ls->locator_indices, i);
325               locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
326
327               /* found local locator with the needed sw_if_index */
328               if (sl->sw_if_index == esi)
329                 {
330                   /* and it has an address */
331                   if (0 == ip_interface_get_first_ip_address (lcm,
332                                                               sl->sw_if_index,
333                                                               gid_address_ip_version
334                                                               (&rmt->address),
335                                                               lcl_addr))
336                     continue;
337
338                   memset (&pair, 0, sizeof (pair));
339                   ip_address_copy (&pair.rmt_loc,
340                                    &gid_address_ip (&rmt->address));
341                   ip_address_copy (&pair.lcl_loc, lcl_addr);
342                   pair.weight = rmt->weight;
343                   vec_add1 (locator_pairs[0], pair);
344                   found = 1;
345                 }
346             }
347         }
348       else
349         break;
350     }
351
352   hash_free (checked);
353   return found;
354 }
355
356 static void
357 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
358                         fid_address_t * fid)
359 {
360   ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
361
362   dst[0] = src[0];
363
364   switch (fid_addr_type (fid))
365     {
366     case FID_ADDR_IP_PREF:
367       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
368       gid_address_ippref (dst) = fid_addr_ippref (fid);
369       break;
370     case FID_ADDR_MAC:
371       gid_address_type (dst) = GID_ADDR_MAC;
372       mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
373       break;
374     default:
375       clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
376       break;
377     }
378 }
379
380 static void
381 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
382 {
383   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
384   mapping_t *src_map, *dst_map;
385   u32 sw_if_index;
386   uword *feip = 0, *dpid;
387   fwd_entry_t *fe;
388   u8 type;
389
390   memset (a, 0, sizeof (*a));
391
392   /* remove entry if it already exists */
393   feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
394   if (feip)
395     dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
396
397   src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
398   dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
399
400   /* insert data plane forwarding entry */
401   a->is_add = 1;
402
403   if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
404     {
405       gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
406                               &gid_address_sd_dst (&dst_map->eid));
407       gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
408                               &gid_address_sd_src (&dst_map->eid));
409     }
410   else
411     gid_address_copy (&a->rmt_eid, &dst_map->eid);
412
413   a->vni = gid_address_vni (&a->rmt_eid);
414
415   /* get vrf or bd_index associated to vni */
416   type = gid_address_type (&a->rmt_eid);
417   if (GID_ADDR_IP_PREFIX == type)
418     {
419       dpid = hash_get (lcm->table_id_by_vni, a->vni);
420       if (!dpid)
421         {
422           clib_warning ("vni %d not associated to a vrf!", a->vni);
423           return;
424         }
425       a->table_id = dpid[0];
426     }
427   else if (GID_ADDR_MAC == type)
428     {
429       dpid = hash_get (lcm->bd_id_by_vni, a->vni);
430       if (!dpid)
431         {
432           clib_warning ("vni %d not associated to a bridge domain !", a->vni);
433           return;
434         }
435       a->bd_id = dpid[0];
436     }
437
438   /* find best locator pair that 1) verifies LISP policy 2) are connected */
439   if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
440     {
441       /* negative entry */
442       a->is_negative = 1;
443       a->action = dst_map->action;
444     }
445
446   /* TODO remove */
447   u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
448   a->decap_next_index = (ipver == IP4) ?
449     LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
450
451   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
452
453   /* add tunnel to fwd entry table XXX check return value from DP insertion */
454   pool_get (lcm->fwd_entry_pool, fe);
455   fe->locator_pairs = a->locator_pairs;
456   gid_address_copy (&fe->deid, &a->rmt_eid);
457   hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
458             fe - lcm->fwd_entry_pool);
459 }
460
461 /**
462  * Add/remove mapping to/from map-cache. Overwriting not allowed.
463  */
464 int
465 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
466                              u32 * map_index_result)
467 {
468   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
469   u32 mi, *map_indexp, map_index, i;
470   mapping_t *m, *old_map;
471   u32 **eid_indexes;
472
473   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
474   old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
475   if (a->is_add)
476     {
477       /* TODO check if overwriting and take appropriate actions */
478       if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
479         {
480           clib_warning ("eid %U found in the eid-table", format_gid_address,
481                         &a->eid);
482           return VNET_API_ERROR_VALUE_EXIST;
483         }
484
485       pool_get (lcm->mapping_pool, m);
486       gid_address_copy (&m->eid, &a->eid);
487       m->locator_set_index = a->locator_set_index;
488       m->ttl = a->ttl;
489       m->action = a->action;
490       m->local = a->local;
491       m->is_static = a->is_static;
492
493       map_index = m - lcm->mapping_pool;
494       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
495                               1);
496
497       if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
498         {
499           clib_warning ("Locator set with index %d doesn't exist",
500                         a->locator_set_index);
501           return VNET_API_ERROR_INVALID_VALUE;
502         }
503
504       /* add eid to list of eids supported by locator-set */
505       vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
506       eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
507                                       a->locator_set_index);
508       vec_add1 (eid_indexes[0], map_index);
509
510       if (a->local)
511         {
512           /* mark as local */
513           vec_add1 (lcm->local_mappings_indexes, map_index);
514         }
515       map_index_result[0] = map_index;
516     }
517   else
518     {
519       if (mi == GID_LOOKUP_MISS)
520         {
521           clib_warning ("eid %U not found in the eid-table",
522                         format_gid_address, &a->eid);
523           return VNET_API_ERROR_INVALID_VALUE;
524         }
525
526       /* clear locator-set to eids binding */
527       eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
528                                       a->locator_set_index);
529       for (i = 0; i < vec_len (eid_indexes[0]); i++)
530         {
531           map_indexp = vec_elt_at_index (eid_indexes[0], i);
532           if (map_indexp[0] == mi)
533             break;
534         }
535       vec_del1 (eid_indexes[0], i);
536
537       /* remove local mark if needed */
538       m = pool_elt_at_index (lcm->mapping_pool, mi);
539       if (m->local)
540         {
541           u32 k, *lm_indexp;
542           for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
543             {
544               lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
545               if (lm_indexp[0] == mi)
546                 break;
547             }
548           vec_del1 (lcm->local_mappings_indexes, k);
549         }
550
551       /* remove mapping from dictionary */
552       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
553       gid_address_free (&m->eid);
554       pool_put_index (lcm->mapping_pool, mi);
555     }
556
557   return 0;
558 }
559
560 /**
561  *  Add/update/delete mapping to/in/from map-cache.
562  */
563 int
564 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
565                                  u32 * map_index_result)
566 {
567   uword *dp_table = 0;
568   u32 vni;
569   u8 type;
570
571   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
572
573   if (vnet_lisp_enable_disable_status () == 0)
574     {
575       clib_warning ("LISP is disabled!");
576       return VNET_API_ERROR_LISP_DISABLED;
577     }
578
579   vni = gid_address_vni (&a->eid);
580   type = gid_address_type (&a->eid);
581   if (GID_ADDR_IP_PREFIX == type)
582     dp_table = hash_get (lcm->table_id_by_vni, vni);
583   else if (GID_ADDR_MAC == type)
584     dp_table = hash_get (lcm->bd_id_by_vni, vni);
585
586   if (!dp_table)
587     {
588       clib_warning ("vni %d not associated to a %s!", vni,
589                     GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
590       return VNET_API_ERROR_INVALID_VALUE;
591     }
592
593   /* store/remove mapping from map-cache */
594   return vnet_lisp_map_cache_add_del (a, map_index_result);
595 }
596
597 static clib_error_t *
598 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
599                                    vlib_cli_command_t * cmd)
600 {
601   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
602   unformat_input_t _line_input, *line_input = &_line_input;
603   u8 is_add = 1;
604   gid_address_t eid;
605   gid_address_t *eids = 0;
606   clib_error_t *error = 0;
607   u8 *locator_set_name = 0;
608   u32 locator_set_index = 0, map_index = 0;
609   uword *p;
610   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
611   int rv = 0;
612   u32 vni = 0;
613
614   memset (&eid, 0, sizeof (eid));
615   memset (a, 0, sizeof (*a));
616
617   /* Get a line of input. */
618   if (!unformat_user (input, unformat_line_input, line_input))
619     return 0;
620
621   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
622     {
623       if (unformat (line_input, "add"))
624         is_add = 1;
625       else if (unformat (line_input, "del"))
626         is_add = 0;
627       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
628         ;
629       else if (unformat (line_input, "vni %d", &vni))
630         gid_address_vni (&eid) = vni;
631       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
632         {
633           p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
634           if (!p)
635             {
636               error = clib_error_return (0, "locator-set %s doesn't exist",
637                                          locator_set_name);
638               goto done;
639             }
640           locator_set_index = p[0];
641         }
642       else
643         {
644           error = unformat_parse_error (line_input);
645           goto done;
646         }
647     }
648   /* XXX treat batch configuration */
649
650   if (GID_ADDR_SRC_DST == gid_address_type (&eid))
651     {
652       error =
653         clib_error_return (0, "src/dst is not supported for local EIDs!");
654       goto done;
655     }
656
657   gid_address_copy (&a->eid, &eid);
658   a->is_add = is_add;
659   a->locator_set_index = locator_set_index;
660   a->local = 1;
661
662   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
663   if (0 != rv)
664     {
665       error = clib_error_return (0, "failed to %s local mapping!",
666                                  is_add ? "add" : "delete");
667     }
668 done:
669   vec_free (eids);
670   if (locator_set_name)
671     vec_free (locator_set_name);
672   gid_address_free (&a->eid);
673   return error;
674 }
675
676 /* *INDENT-OFF* */
677 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
678     .path = "lisp eid-table",
679     .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
680       "locator-set <locator-set>",
681     .function = lisp_add_del_local_eid_command_fn,
682 };
683 /* *INDENT-ON* */
684
685 int
686 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
687 {
688   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
689   uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
690
691   if (vnet_lisp_enable_disable_status () == 0)
692     {
693       clib_warning ("LISP is disabled!");
694       return -1;
695     }
696
697   dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
698   vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
699
700   if (!is_l2 && (vni == 0 || dp_id == 0))
701     {
702       clib_warning ("can't add/del default vni-vrf mapping!");
703       return -1;
704     }
705
706   dp_idp = hash_get (dp_table_by_vni[0], vni);
707   vnip = hash_get (vni_by_dp_table[0], dp_id);
708
709   if (is_add)
710     {
711       if (dp_idp || vnip)
712         {
713           clib_warning ("vni %d or vrf %d already used in vrf/vni "
714                         "mapping!", vni, dp_id);
715           return -1;
716         }
717       hash_set (dp_table_by_vni[0], vni, dp_id);
718       hash_set (vni_by_dp_table[0], dp_id, vni);
719
720       /* create dp iface */
721       dp_add_del_iface (lcm, vni, is_l2, 1);
722     }
723   else
724     {
725       if (!dp_idp || !vnip)
726         {
727           clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
728                         "mapping!", vni, dp_id);
729           return -1;
730         }
731       hash_unset (dp_table_by_vni[0], vni);
732       hash_unset (vni_by_dp_table[0], dp_id);
733
734       /* remove dp iface */
735       dp_add_del_iface (lcm, vni, is_l2, 0);
736     }
737   return 0;
738
739 }
740
741 static clib_error_t *
742 lisp_eid_table_map_command_fn (vlib_main_t * vm,
743                                unformat_input_t * input,
744                                vlib_cli_command_t * cmd)
745 {
746   u8 is_add = 1, is_l2 = 0;
747   u32 vni = 0, dp_id = 0;
748   unformat_input_t _line_input, *line_input = &_line_input;
749
750   /* Get a line of input. */
751   if (!unformat_user (input, unformat_line_input, line_input))
752     return 0;
753
754   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
755     {
756       if (unformat (line_input, "del"))
757         is_add = 0;
758       else if (unformat (line_input, "vni %d", &vni))
759         ;
760       else if (unformat (line_input, "vrf %d", &dp_id))
761         ;
762       else if (unformat (line_input, "bd %d", &dp_id))
763         is_l2 = 1;
764       else
765         {
766           return unformat_parse_error (line_input);
767         }
768     }
769   vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
770   return 0;
771 }
772
773 /* *INDENT-OFF* */
774 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
775     .path = "lisp eid-table map",
776     .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
777     .function = lisp_eid_table_map_command_fn,
778 };
779 /* *INDENT-ON* */
780
781 /* return 0 if the two locator sets are identical 1 otherwise */
782 static u8
783 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
784                   locator_t * new_locators)
785 {
786   u32 i, old_li;
787   locator_t *old_loc, *new_loc;
788
789   if (vec_len (old_ls_indexes) != vec_len (new_locators))
790     return 1;
791
792   for (i = 0; i < vec_len (new_locators); i++)
793     {
794       old_li = vec_elt (old_ls_indexes, i);
795       old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
796
797       new_loc = vec_elt_at_index (new_locators, i);
798
799       if (locator_cmp (old_loc, new_loc))
800         return 1;
801     }
802   return 0;
803 }
804
805 /**
806  * Adds/removes/updates mapping. Does not program forwarding.
807  *
808  * @param eid end-host identifier
809  * @param rlocs vector of remote locators
810  * @param action action for negative map-reply
811  * @param is_add add mapping if non-zero, delete otherwise
812  * @param res_map_index the map-index that was created/updated/removed. It is
813  *                      set to ~0 if no action is taken.
814  * @param is_static used for distinguishing between statically learned
815                     remote mappings and mappings obtained from MR
816  * @return return code
817  */
818 int
819 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
820                            u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
821                            u32 * res_map_index)
822 {
823   vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
824   vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
825   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
826   u32 mi, ls_index = 0, dst_map_index;
827   mapping_t *old_map;
828
829   if (vnet_lisp_enable_disable_status () == 0)
830     {
831       clib_warning ("LISP is disabled!");
832       return VNET_API_ERROR_LISP_DISABLED;
833     }
834
835   if (res_map_index)
836     res_map_index[0] = ~0;
837
838   memset (m_args, 0, sizeof (m_args[0]));
839   memset (ls_args, 0, sizeof (ls_args[0]));
840
841   ls_args->locators = rlocs;
842
843   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
844   old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
845
846   if (is_add)
847     {
848       /* overwrite: if mapping already exists, decide if locators should be
849        * updated and be done */
850       if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
851         {
852           if (!is_static && (old_map->is_static || old_map->local))
853             {
854               /* do not overwrite local or static remote mappings */
855               clib_warning ("mapping %U rejected due to collision with local "
856                             "or static remote mapping!", format_gid_address,
857                             &eid);
858               return 0;
859             }
860
861           locator_set_t *old_ls;
862
863           /* update mapping attributes */
864           old_map->action = action;
865           old_map->authoritative = authoritative;
866           old_map->ttl = ttl;
867
868           old_ls = pool_elt_at_index (lcm->locator_set_pool,
869                                       old_map->locator_set_index);
870           if (compare_locators (lcm, old_ls->locator_indices,
871                                 ls_args->locators))
872             {
873               /* set locator-set index to overwrite */
874               ls_args->is_add = 1;
875               ls_args->index = old_map->locator_set_index;
876               vnet_lisp_add_del_locator_set (ls_args, 0);
877               if (res_map_index)
878                 res_map_index[0] = mi;
879             }
880         }
881       /* new mapping */
882       else
883         {
884           ls_args->is_add = 1;
885           ls_args->index = ~0;
886
887           vnet_lisp_add_del_locator_set (ls_args, &ls_index);
888
889           /* add mapping */
890           gid_address_copy (&m_args->eid, eid);
891           m_args->is_add = 1;
892           m_args->action = action;
893           m_args->locator_set_index = ls_index;
894           m_args->is_static = is_static;
895           vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
896
897           if (res_map_index)
898             res_map_index[0] = dst_map_index;
899         }
900     }
901   else
902     {
903       if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
904         {
905           clib_warning ("cannot delete mapping for eid %U",
906                         format_gid_address, eid);
907           return -1;
908         }
909
910       m_args->is_add = 0;
911       gid_address_copy (&m_args->eid, eid);
912       m_args->locator_set_index = old_map->locator_set_index;
913
914       /* delete mapping associated from map-cache */
915       vnet_lisp_map_cache_add_del (m_args, 0);
916
917       ls_args->is_add = 0;
918       ls_args->index = old_map->locator_set_index;
919       /* delete locator set */
920       vnet_lisp_add_del_locator_set (ls_args, 0);
921
922       /* return old mapping index */
923       if (res_map_index)
924         res_map_index[0] = mi;
925     }
926
927   /* success */
928   return 0;
929 }
930
931 int
932 vnet_lisp_clear_all_remote_adjacencies (void)
933 {
934   int rv = 0;
935   u32 mi, *map_indices = 0, *map_indexp;
936   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
937   vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
938   vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
939
940   /* *INDENT-OFF* */
941   pool_foreach_index (mi, lcm->mapping_pool,
942   ({
943     vec_add1 (map_indices, mi);
944   }));
945   /* *INDENT-ON* */
946
947   vec_foreach (map_indexp, map_indices)
948   {
949     mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
950     if (!map->local)
951       {
952         dp_del_fwd_entry (lcm, 0, map_indexp[0]);
953
954         dm_args->is_add = 0;
955         gid_address_copy (&dm_args->eid, &map->eid);
956         dm_args->locator_set_index = map->locator_set_index;
957
958         /* delete mapping associated to fwd entry */
959         vnet_lisp_map_cache_add_del (dm_args, 0);
960
961         ls->is_add = 0;
962         ls->local = 0;
963         ls->index = map->locator_set_index;
964         /* delete locator set */
965         rv = vnet_lisp_add_del_locator_set (ls, 0);
966         if (rv != 0)
967           goto cleanup;
968       }
969   }
970
971 cleanup:
972   if (map_indices)
973     vec_free (map_indices);
974   return rv;
975 }
976
977 /**
978  * Adds adjacency or removes forwarding entry associated to remote mapping.
979  * Note that adjacencies are not stored, they only result in forwarding entries
980  * being created.
981  */
982 int
983 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
984                         gid_address_t * remote_eid, u8 is_add)
985 {
986   u32 local_mi, remote_mi = ~0;
987
988   if (vnet_lisp_enable_disable_status () == 0)
989     {
990       clib_warning ("LISP is disabled!");
991       return VNET_API_ERROR_LISP_DISABLED;
992     }
993
994   remote_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, remote_eid);
995   if (GID_LOOKUP_MISS == remote_mi)
996     {
997       clib_warning ("Remote eid %U not found. Cannot add adjacency!",
998                     format_gid_address, remote_eid);
999
1000       return -1;
1001     }
1002
1003   if (is_add)
1004     {
1005       /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1006        * delete*/
1007
1008       /* check if source eid has an associated mapping. If pitr mode is on,
1009        * just use the pitr's mapping */
1010       local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1011         gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1012
1013
1014       if (GID_LOOKUP_MISS == local_mi)
1015         {
1016           clib_warning ("Local eid %U not found. Cannot add adjacency!",
1017                         format_gid_address, local_eid);
1018
1019           return -1;
1020         }
1021
1022       /* update forwarding */
1023       dp_add_fwd_entry (lcm, local_mi, remote_mi);
1024     }
1025   else
1026     dp_del_fwd_entry (lcm, 0, remote_mi);
1027
1028   return 0;
1029 }
1030
1031 int
1032 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1033 {
1034   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1035   return lisp_add_del_adjacency (lcm, &a->seid, &a->deid, a->is_add);
1036 }
1037
1038 /**
1039  * Handler for add/del remote mapping CLI.
1040  *
1041  * @param vm vlib context
1042  * @param input input from user
1043  * @param cmd cmd
1044  * @return pointer to clib error structure
1045  */
1046 static clib_error_t *
1047 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1048                                         unformat_input_t * input,
1049                                         vlib_cli_command_t * cmd)
1050 {
1051   clib_error_t *error = 0;
1052   unformat_input_t _line_input, *line_input = &_line_input;
1053   u8 is_add = 1, del_all = 0;
1054   locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1055   gid_address_t eid;
1056   u8 eid_set = 0;
1057   u32 vni, action = ~0, p, w;
1058   int rv;
1059
1060   /* Get a line of input. */
1061   if (!unformat_user (input, unformat_line_input, line_input))
1062     return 0;
1063
1064   memset (&eid, 0, sizeof (eid));
1065   memset (&rloc, 0, sizeof (rloc));
1066
1067   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1068     {
1069       if (unformat (line_input, "del-all"))
1070         del_all = 1;
1071       else if (unformat (line_input, "del"))
1072         is_add = 0;
1073       else if (unformat (line_input, "add"))
1074         ;
1075       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1076         eid_set = 1;
1077       else if (unformat (line_input, "vni %u", &vni))
1078         {
1079           gid_address_vni (&eid) = vni;
1080         }
1081       else if (unformat (line_input, "p %d w %d", &p, &w))
1082         {
1083           if (!curr_rloc)
1084             {
1085               clib_warning
1086                 ("No RLOC configured for setting priority/weight!");
1087               goto done;
1088             }
1089           curr_rloc->priority = p;
1090           curr_rloc->weight = w;
1091         }
1092       else if (unformat (line_input, "rloc %U", unformat_ip_address,
1093                          &gid_address_ip (&rloc.address)))
1094         {
1095           /* since rloc is stored in ip prefix we need to set prefix length */
1096           ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1097
1098           u8 version = gid_address_ip_version (&rloc.address);
1099           ip_prefix_len (pref) = ip_address_max_len (version);
1100
1101           vec_add1 (rlocs, rloc);
1102           curr_rloc = &rlocs[vec_len (rlocs) - 1];
1103         }
1104       else if (unformat (line_input, "action %U",
1105                          unformat_negative_mapping_action, &action))
1106         ;
1107       else
1108         {
1109           clib_warning ("parse error");
1110           goto done;
1111         }
1112     }
1113
1114   if (!eid_set)
1115     {
1116       clib_warning ("missing eid!");
1117       goto done;
1118     }
1119
1120   if (!del_all)
1121     {
1122       if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1123         {
1124           clib_warning ("no action set for negative map-reply!");
1125           goto done;
1126         }
1127     }
1128   else
1129     {
1130       vnet_lisp_clear_all_remote_adjacencies ();
1131       goto done;
1132     }
1133
1134   /* TODO build src/dst with seid */
1135
1136   /* if it's a delete, clean forwarding */
1137   if (!is_add)
1138     {
1139       lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1140       rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1141       if (rv)
1142         {
1143           goto done;
1144         }
1145     }
1146
1147   /* add as static remote mapping, i.e., not authoritative and infinite
1148    * ttl */
1149   rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1150                                   1 /* is_static */ , 0);
1151
1152   if (rv)
1153     clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1154
1155 done:
1156   vec_free (rlocs);
1157   unformat_free (line_input);
1158   return error;
1159 }
1160
1161 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1162 {
1163 .path = "lisp remote-mapping",.short_help =
1164     "lisp remote-mapping add|del [del-all] vni <vni> "
1165     "eid <est-eid> [action <no-action|natively-forward|"
1166     "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1167     "[rloc <dst-locator> ... ]",.function =
1168     lisp_add_del_remote_mapping_command_fn,};
1169
1170 /**
1171  * Handler for add/del adjacency CLI.
1172  */
1173 static clib_error_t *
1174 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1175                                    vlib_cli_command_t * cmd)
1176 {
1177   clib_error_t *error = 0;
1178   unformat_input_t _line_input, *line_input = &_line_input;
1179   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1180   u8 is_add = 1;
1181   locator_t rloc, *rlocs = 0;
1182   ip_prefix_t *deid_ippref, *seid_ippref;
1183   gid_address_t seid, deid;
1184   u8 *dmac = gid_address_mac (&deid);
1185   u8 *smac = gid_address_mac (&seid);
1186   u8 deid_set = 0, seid_set = 0;
1187   u8 *s = 0;
1188   u32 vni, action = ~0;
1189   int rv;
1190
1191   /* Get a line of input. */
1192   if (!unformat_user (input, unformat_line_input, line_input))
1193     return 0;
1194
1195   memset (&deid, 0, sizeof (deid));
1196   memset (&seid, 0, sizeof (seid));
1197   memset (&rloc, 0, sizeof (rloc));
1198
1199   seid_ippref = &gid_address_ippref (&seid);
1200   deid_ippref = &gid_address_ippref (&deid);
1201
1202   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1203     {
1204       if (unformat (line_input, "del"))
1205         is_add = 0;
1206       else if (unformat (line_input, "add"))
1207         ;
1208       else if (unformat (line_input, "deid %U",
1209                          unformat_ip_prefix, deid_ippref))
1210         {
1211           gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
1212           deid_set = 1;
1213         }
1214       else if (unformat (line_input, "deid %U", unformat_mac_address, dmac))
1215         {
1216           gid_address_type (&deid) = GID_ADDR_MAC;
1217           deid_set = 1;
1218         }
1219       else if (unformat (line_input, "vni %u", &vni))
1220         {
1221           gid_address_vni (&seid) = vni;
1222           gid_address_vni (&deid) = vni;
1223         }
1224       else if (unformat (line_input, "seid %U",
1225                          unformat_ip_prefix, seid_ippref))
1226         {
1227           gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
1228           seid_set = 1;
1229         }
1230       else if (unformat (line_input, "seid %U", unformat_mac_address, smac))
1231         {
1232           gid_address_type (&seid) = GID_ADDR_MAC;
1233           seid_set = 1;
1234         }
1235       else
1236         {
1237           clib_warning ("parse error");
1238           goto done;
1239         }
1240     }
1241
1242   if (!deid_set)
1243     {
1244       clib_warning ("missing deid!");
1245       goto done;
1246     }
1247
1248   if (GID_ADDR_IP_PREFIX == gid_address_type (&deid))
1249     {
1250       /* if seid not set, make sure the ip version is the same as that
1251        * of the deid. This ensures the seid to be configured will be
1252        * either 0/0 or ::/0 */
1253       if (!seid_set)
1254         ip_prefix_version (seid_ippref) = ip_prefix_version (deid_ippref);
1255
1256       if (is_add &&
1257           (ip_prefix_version (deid_ippref)
1258            != ip_prefix_version (seid_ippref)))
1259         {
1260           clib_warning ("source and destination EIDs are not"
1261                         " in the same IP family!");
1262           goto done;
1263         }
1264     }
1265
1266   if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1267     {
1268       clib_warning ("no action set for negative map-reply!");
1269       goto done;
1270     }
1271
1272   memset (a, 0, sizeof (a[0]));
1273   gid_address_copy (&a->seid, &deid);
1274   gid_address_copy (&a->deid, &seid);
1275
1276   a->is_add = is_add;
1277   rv = vnet_lisp_add_del_adjacency (a);
1278
1279   if (rv)
1280     clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1281
1282 done:
1283   unformat_free (line_input);
1284   if (s)
1285     vec_free (s);
1286   return error;
1287 }
1288
1289 /* *INDENT-OFF* */
1290 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1291     .path = "lisp adjacency",
1292     .short_help = "lisp adjacency add|del vni <vni>"
1293      "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
1294      "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
1295     .function = lisp_add_del_adjacency_command_fn,
1296 };
1297 /* *INDENT-ON* */
1298
1299 int
1300 vnet_lisp_set_map_request_mode (u8 mode)
1301 {
1302   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1303
1304   if (vnet_lisp_enable_disable_status () == 0)
1305     {
1306       clib_warning ("LISP is disabled!");
1307       return VNET_API_ERROR_LISP_DISABLED;
1308     }
1309
1310   if (mode >= _MR_MODE_MAX)
1311     {
1312       clib_warning ("Invalid LISP map request mode %d!", mode);
1313       return VNET_API_ERROR_INVALID_ARGUMENT;
1314     }
1315
1316   lcm->map_request_mode = mode;
1317   return 0;
1318 }
1319
1320 static clib_error_t *
1321 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1322                                   unformat_input_t * input,
1323                                   vlib_cli_command_t * cmd)
1324 {
1325   unformat_input_t _i, *i = &_i;
1326   map_request_mode_t mr_mode = _MR_MODE_MAX;
1327
1328   /* Get a line of input. */
1329   if (!unformat_user (input, unformat_line_input, i))
1330     return 0;
1331
1332   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1333     {
1334       if (unformat (i, "dst-only"))
1335         mr_mode = MR_MODE_DST_ONLY;
1336       else if (unformat (i, "src-dst"))
1337         mr_mode = MR_MODE_SRC_DST;
1338       else
1339         {
1340           clib_warning ("parse error '%U'", format_unformat_error, i);
1341           goto done;
1342         }
1343     }
1344
1345   if (_MR_MODE_MAX == mr_mode)
1346     {
1347       clib_warning ("No LISP map request mode entered!");
1348       return 0;
1349     }
1350
1351   vnet_lisp_set_map_request_mode (mr_mode);
1352 done:
1353   return 0;
1354 }
1355
1356 /* *INDENT-OFF* */
1357 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1358     .path = "lisp map-request mode",
1359     .short_help = "lisp map-request mode dst-only|src-dst",
1360     .function = lisp_map_request_mode_command_fn,
1361 };
1362 /* *INDENT-ON* */
1363
1364 static u8 *
1365 format_lisp_map_request_mode (u8 * s, va_list * args)
1366 {
1367   u32 mode = va_arg (*args, u32);
1368
1369   switch (mode)
1370     {
1371     case 0:
1372       return format (0, "dst-only");
1373     case 1:
1374       return format (0, "src-dst");
1375     }
1376   return 0;
1377 }
1378
1379 static clib_error_t *
1380 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1381                                        unformat_input_t * input,
1382                                        vlib_cli_command_t * cmd)
1383 {
1384   vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1385                    vnet_lisp_get_map_request_mode ());
1386   return 0;
1387 }
1388
1389 /* *INDENT-OFF* */
1390 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1391     .path = "show lisp map-request mode",
1392     .short_help = "show lisp map-request mode",
1393     .function = lisp_show_map_request_mode_command_fn,
1394 };
1395 /* *INDENT-ON* */
1396
1397 static clib_error_t *
1398 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1399                                     unformat_input_t * input,
1400                                     vlib_cli_command_t * cmd)
1401 {
1402   map_resolver_t *mr;
1403   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1404
1405   vec_foreach (mr, lcm->map_resolvers)
1406   {
1407     vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1408   }
1409   return 0;
1410 }
1411
1412 /* *INDENT-OFF* */
1413 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1414     .path = "show lisp map-resolvers",
1415     .short_help = "show lisp map-resolvers",
1416     .function = lisp_show_map_resolvers_command_fn,
1417 };
1418 /* *INDENT-ON* */
1419
1420 int
1421 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1422 {
1423   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1424   u32 locator_set_index = ~0;
1425   mapping_t *m;
1426   uword *p;
1427
1428   if (vnet_lisp_enable_disable_status () == 0)
1429     {
1430       clib_warning ("LISP is disabled!");
1431       return VNET_API_ERROR_LISP_DISABLED;
1432     }
1433
1434   p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1435   if (!p)
1436     {
1437       clib_warning ("locator-set %v doesn't exist", locator_set_name);
1438       return -1;
1439     }
1440   locator_set_index = p[0];
1441
1442   if (is_add)
1443     {
1444       pool_get (lcm->mapping_pool, m);
1445       m->locator_set_index = locator_set_index;
1446       m->local = 1;
1447       lcm->pitr_map_index = m - lcm->mapping_pool;
1448
1449       /* enable pitr mode */
1450       lcm->lisp_pitr = 1;
1451     }
1452   else
1453     {
1454       /* remove pitr mapping */
1455       pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1456
1457       /* disable pitr mode */
1458       lcm->lisp_pitr = 0;
1459     }
1460   return 0;
1461 }
1462
1463 static clib_error_t *
1464 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1465                                       unformat_input_t * input,
1466                                       vlib_cli_command_t * cmd)
1467 {
1468   u8 locator_name_set = 0;
1469   u8 *locator_set_name = 0;
1470   u8 is_add = 1;
1471   unformat_input_t _line_input, *line_input = &_line_input;
1472   clib_error_t *error = 0;
1473   int rv = 0;
1474
1475   /* Get a line of input. */
1476   if (!unformat_user (input, unformat_line_input, line_input))
1477     return 0;
1478
1479   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1480     {
1481       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1482         locator_name_set = 1;
1483       else if (unformat (line_input, "disable"))
1484         is_add = 0;
1485       else
1486         return clib_error_return (0, "parse error");
1487     }
1488
1489   if (!locator_name_set)
1490     {
1491       clib_warning ("No locator set specified!");
1492       goto done;
1493     }
1494   rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1495   if (0 != rv)
1496     {
1497       error = clib_error_return (0, "failed to %s pitr!",
1498                                  is_add ? "add" : "delete");
1499     }
1500
1501 done:
1502   if (locator_set_name)
1503     vec_free (locator_set_name);
1504   return error;
1505 }
1506
1507 /* *INDENT-OFF* */
1508 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1509     .path = "lisp pitr",
1510     .short_help = "lisp pitr [disable] ls <locator-set-name>",
1511     .function = lisp_pitr_set_locator_set_command_fn,
1512 };
1513 /* *INDENT-ON* */
1514
1515 static clib_error_t *
1516 lisp_show_pitr_command_fn (vlib_main_t * vm,
1517                            unformat_input_t * input, vlib_cli_command_t * cmd)
1518 {
1519   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1520   mapping_t *m;
1521   locator_set_t *ls;
1522   u8 *tmp_str = 0;
1523
1524   vlib_cli_output (vm, "%=20s%=16s",
1525                    "pitr", lcm->lisp_pitr ? "locator-set" : "");
1526
1527   if (!lcm->lisp_pitr)
1528     {
1529       vlib_cli_output (vm, "%=20s", "disable");
1530       return 0;
1531     }
1532
1533   if (~0 == lcm->pitr_map_index)
1534     {
1535       tmp_str = format (0, "N/A");
1536     }
1537   else
1538     {
1539       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1540       if (~0 != m->locator_set_index)
1541         {
1542           ls =
1543             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1544           tmp_str = format (0, "%s", ls->name);
1545         }
1546       else
1547         {
1548           tmp_str = format (0, "N/A");
1549         }
1550     }
1551   vec_add1 (tmp_str, 0);
1552
1553   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1554
1555   vec_free (tmp_str);
1556
1557   return 0;
1558 }
1559
1560 /* *INDENT-OFF* */
1561 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1562     .path = "show lisp pitr",
1563     .short_help = "Show pitr",
1564     .function = lisp_show_pitr_command_fn,
1565 };
1566 /* *INDENT-ON* */
1567
1568 static u8 *
1569 format_eid_entry (u8 * s, va_list * args)
1570 {
1571   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1572   lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1573   mapping_t *mapit = va_arg (*args, mapping_t *);
1574   locator_set_t *ls = va_arg (*args, locator_set_t *);
1575   gid_address_t *gid = &mapit->eid;
1576   u32 ttl = mapit->ttl;
1577   u8 aut = mapit->authoritative;
1578   u32 *loc_index;
1579   u8 first_line = 1;
1580   u8 *loc;
1581
1582   u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1583     : format (0, "remote");
1584
1585   if (vec_len (ls->locator_indices) == 0)
1586     {
1587       s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1588                   type, ttl, aut);
1589     }
1590   else
1591     {
1592       vec_foreach (loc_index, ls->locator_indices)
1593       {
1594         locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1595         if (l->local)
1596           loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1597                         l->sw_if_index);
1598         else
1599           loc = format (0, "%U", format_ip_address,
1600                         &gid_address_ip (&l->address));
1601
1602         if (first_line)
1603           {
1604             s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1605                         gid, type, loc, ttl, aut);
1606             first_line = 0;
1607           }
1608         else
1609           s = format (s, "%55s%v\n", "", loc);
1610       }
1611     }
1612   return s;
1613 }
1614
1615 static clib_error_t *
1616 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1617                                 unformat_input_t * input,
1618                                 vlib_cli_command_t * cmd)
1619 {
1620   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1621   mapping_t *mapit;
1622   unformat_input_t _line_input, *line_input = &_line_input;
1623   u32 mi;
1624   gid_address_t eid;
1625   u8 print_all = 1;
1626   u8 filter = 0;
1627
1628   memset (&eid, 0, sizeof (eid));
1629
1630   /* Get a line of input. */
1631   if (!unformat_user (input, unformat_line_input, line_input))
1632     return 0;
1633
1634   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1635     {
1636       if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1637         print_all = 0;
1638       else if (unformat (line_input, "local"))
1639         filter = 1;
1640       else if (unformat (line_input, "remote"))
1641         filter = 2;
1642       else
1643         return clib_error_return (0, "parse error: '%U'",
1644                                   format_unformat_error, line_input);
1645     }
1646
1647   vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1648                    "EID", "type", "locators", "ttl", "autoritative");
1649
1650   if (print_all)
1651     {
1652       /* *INDENT-OFF* */
1653       pool_foreach (mapit, lcm->mapping_pool,
1654       ({
1655         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1656                                                 mapit->locator_set_index);
1657         if (filter && !((1 == filter && ls->local) ||
1658           (2 == filter && !ls->local)))
1659           {
1660             continue;
1661           }
1662         vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1663                          lcm, mapit, ls);
1664       }));
1665       /* *INDENT-ON* */
1666     }
1667   else
1668     {
1669       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1670       if ((u32) ~ 0 == mi)
1671         return 0;
1672
1673       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1674       locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
1675                                              mapit->locator_set_index);
1676
1677       if (filter && !((1 == filter && ls->local) ||
1678                       (2 == filter && !ls->local)))
1679         {
1680           return 0;
1681         }
1682
1683       vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
1684                        lcm, mapit, ls);
1685     }
1686
1687   return 0;
1688 }
1689
1690 /* *INDENT-OFF* */
1691 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
1692     .path = "show lisp eid-table",
1693     .short_help = "Shows EID table",
1694     .function = lisp_show_eid_table_command_fn,
1695 };
1696 /* *INDENT-ON* */
1697
1698 /* cleans locator to locator-set data and removes locators not part of
1699  * any locator-set */
1700 static void
1701 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1702 {
1703   u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1704   locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1705   for (i = 0; i < vec_len (ls->locator_indices); i++)
1706     {
1707       loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1708       ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1709                                      loc_indexp[0]);
1710       for (j = 0; j < vec_len (ls_indexes[0]); j++)
1711         {
1712           ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1713           if (ls_indexp[0] == lsi)
1714             break;
1715         }
1716
1717       /* delete index for removed locator-set */
1718       vec_del1 (ls_indexes[0], j);
1719
1720       /* delete locator if it's part of no locator-set */
1721       if (vec_len (ls_indexes[0]) == 0)
1722         {
1723           pool_put_index (lcm->locator_pool, loc_indexp[0]);
1724           vec_add1 (to_be_deleted, i);
1725         }
1726     }
1727
1728   if (to_be_deleted)
1729     {
1730       for (i = 0; i < vec_len (to_be_deleted); i++)
1731         {
1732           loc_indexp = vec_elt_at_index (to_be_deleted, i);
1733           vec_del1 (ls->locator_indices, loc_indexp[0]);
1734         }
1735       vec_free (to_be_deleted);
1736     }
1737 }
1738
1739 static inline uword *
1740 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1741 {
1742   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1743
1744   ASSERT (a != NULL);
1745   ASSERT (p != NULL);
1746
1747   /* find locator-set */
1748   if (a->local)
1749     {
1750       p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1751     }
1752   else
1753     {
1754       *p = a->index;
1755     }
1756
1757   return p;
1758 }
1759
1760 static inline int
1761 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1762                            locator_t * loc)
1763 {
1764   locator_t *itloc;
1765   u32 *locit;
1766
1767   ASSERT (ls != NULL);
1768   ASSERT (loc != NULL);
1769
1770   vec_foreach (locit, ls->locator_indices)
1771   {
1772     itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1773     if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1774         (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1775       {
1776         clib_warning ("Duplicate locator");
1777         return VNET_API_ERROR_VALUE_EXIST;
1778       }
1779   }
1780
1781   return 0;
1782 }
1783
1784 static inline void
1785 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1786                                  u32 ls_index, u32 loc_id)
1787 {
1788   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1789   u32 **ls_indexes = NULL;
1790
1791   ASSERT (ls != NULL);
1792   ASSERT (locit != NULL);
1793
1794   ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1795   pool_put_index (lcm->locator_pool, locit[0]);
1796   vec_del1 (ls->locator_indices, loc_id);
1797   vec_del1 (ls_indexes[0], ls_index);
1798 }
1799
1800 int
1801 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1802                            locator_set_t * ls, u32 * ls_result)
1803 {
1804   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1805   locator_t *loc = NULL, *itloc = NULL;
1806   uword _p = (u32) ~ 0, *p = &_p;
1807   u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1808   u32 loc_id = ~0;
1809   int ret = 0;
1810
1811   ASSERT (a != NULL);
1812
1813   if (vnet_lisp_enable_disable_status () == 0)
1814     {
1815       clib_warning ("LISP is disabled!");
1816       return VNET_API_ERROR_LISP_DISABLED;
1817     }
1818
1819   p = get_locator_set_index (a, p);
1820   if (!p)
1821     {
1822       clib_warning ("locator-set %v doesn't exist", a->name);
1823       return VNET_API_ERROR_INVALID_ARGUMENT;
1824     }
1825
1826   if (ls == 0)
1827     {
1828       ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1829       if (!ls)
1830         {
1831           clib_warning ("locator-set %d to be overwritten doesn't exist!",
1832                         p[0]);
1833           return VNET_API_ERROR_INVALID_ARGUMENT;
1834         }
1835     }
1836
1837   if (a->is_add)
1838     {
1839       if (ls_result)
1840         ls_result[0] = p[0];
1841
1842       /* allocate locators */
1843       vec_foreach (itloc, a->locators)
1844       {
1845         ret = is_locator_in_locator_set (lcm, ls, itloc);
1846         if (0 != ret)
1847           {
1848             return ret;
1849           }
1850
1851         pool_get (lcm->locator_pool, loc);
1852         loc[0] = itloc[0];
1853         loc_index = loc - lcm->locator_pool;
1854
1855         vec_add1 (ls->locator_indices, loc_index);
1856
1857         vec_validate (lcm->locator_to_locator_sets, loc_index);
1858         ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1859                                        loc_index);
1860         vec_add1 (ls_indexes[0], p[0]);
1861       }
1862     }
1863   else
1864     {
1865       ls_index = p[0];
1866
1867       itloc = a->locators;
1868       loc_id = 0;
1869       vec_foreach (locit, ls->locator_indices)
1870       {
1871         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1872
1873         if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1874           {
1875             remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1876           }
1877         if (0 == loc->local &&
1878             !gid_address_cmp (&loc->address, &itloc->address))
1879           {
1880             remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1881           }
1882
1883         loc_id++;
1884       }
1885     }
1886
1887   return 0;
1888 }
1889
1890 int
1891 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1892                                u32 * ls_result)
1893 {
1894   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1895   locator_set_t *ls;
1896   uword _p = (u32) ~ 0, *p = &_p;
1897   u32 ls_index;
1898   u32 **eid_indexes;
1899   int ret = 0;
1900
1901   if (vnet_lisp_enable_disable_status () == 0)
1902     {
1903       clib_warning ("LISP is disabled!");
1904       return VNET_API_ERROR_LISP_DISABLED;
1905     }
1906
1907   if (a->is_add)
1908     {
1909       p = get_locator_set_index (a, p);
1910
1911       /* overwrite */
1912       if (p && p[0] != (u32) ~ 0)
1913         {
1914           ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1915           if (!ls)
1916             {
1917               clib_warning ("locator-set %d to be overwritten doesn't exist!",
1918                             p[0]);
1919               return -1;
1920             }
1921
1922           /* clean locator to locator-set vectors and remove locators if
1923            * they're not part of another locator-set */
1924           clean_locator_to_locator_set (lcm, p[0]);
1925
1926           /* remove locator indices from locator set */
1927           vec_free (ls->locator_indices);
1928
1929           ls_index = p[0];
1930
1931           if (ls_result)
1932             ls_result[0] = p[0];
1933         }
1934       /* new locator-set */
1935       else
1936         {
1937           pool_get (lcm->locator_set_pool, ls);
1938           memset (ls, 0, sizeof (*ls));
1939           ls_index = ls - lcm->locator_set_pool;
1940
1941           if (a->local)
1942             {
1943               ls->name = vec_dup (a->name);
1944
1945               if (!lcm->locator_set_index_by_name)
1946                 lcm->locator_set_index_by_name = hash_create_vec (
1947                                                                    /* size */
1948                                                                    0,
1949                                                                    sizeof
1950                                                                    (ls->name
1951                                                                     [0]),
1952                                                                    sizeof
1953                                                                    (uword));
1954               hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1955                             ls_index);
1956
1957               /* mark as local locator-set */
1958               vec_add1 (lcm->local_locator_set_indexes, ls_index);
1959             }
1960           ls->local = a->local;
1961           if (ls_result)
1962             ls_result[0] = ls_index;
1963         }
1964
1965       ret = vnet_lisp_add_del_locator (a, ls, NULL);
1966       if (0 != ret)
1967         {
1968           return ret;
1969         }
1970     }
1971   else
1972     {
1973       p = get_locator_set_index (a, p);
1974       if (!p)
1975         {
1976           clib_warning ("locator-set %v doesn't exists", a->name);
1977           return -1;
1978         }
1979
1980       ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1981       if (!ls)
1982         {
1983           clib_warning ("locator-set with index %d doesn't exists", p[0]);
1984           return -1;
1985         }
1986
1987       if (lcm->mreq_itr_rlocs == p[0])
1988         {
1989           clib_warning ("Can't delete the locator-set used to constrain "
1990                         "the itr-rlocs in map-requests!");
1991           return -1;
1992         }
1993
1994       if (vec_len (lcm->locator_set_to_eids) != 0)
1995         {
1996           eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1997           if (vec_len (eid_indexes[0]) != 0)
1998             {
1999               clib_warning
2000                 ("Can't delete a locator that supports a mapping!");
2001               return -1;
2002             }
2003         }
2004
2005       /* clean locator to locator-sets data */
2006       clean_locator_to_locator_set (lcm, p[0]);
2007
2008       if (ls->local)
2009         {
2010           u32 it, lsi;
2011
2012           vec_foreach_index (it, lcm->local_locator_set_indexes)
2013           {
2014             lsi = vec_elt (lcm->local_locator_set_indexes, it);
2015             if (lsi == p[0])
2016               {
2017                 vec_del1 (lcm->local_locator_set_indexes, it);
2018                 break;
2019               }
2020           }
2021           hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2022         }
2023       vec_free (ls->name);
2024       vec_free (ls->locator_indices);
2025       pool_put (lcm->locator_set_pool, ls);
2026     }
2027   return 0;
2028 }
2029
2030 clib_error_t *
2031 vnet_lisp_enable_disable (u8 is_enable)
2032 {
2033   u32 vni, dp_table;
2034   clib_error_t *error = 0;
2035   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2036   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2037
2038   a->is_en = is_enable;
2039   error = vnet_lisp_gpe_enable_disable (a);
2040   if (error)
2041     {
2042       return clib_error_return (0, "failed to %s data-plane!",
2043                                 a->is_en ? "enable" : "disable");
2044     }
2045
2046   if (is_enable)
2047     {
2048       /* enable all l2 and l3 ifaces */
2049
2050       /* *INDENT-OFF* */
2051       hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2052         dp_add_del_iface(lcm, vni, 0, 1);
2053       }));
2054       hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2055         dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2056       }));
2057       /* *INDENT-ON* */
2058     }
2059   else
2060     {
2061       /* clear interface table */
2062       hash_free (lcm->dp_intf_by_vni);
2063       hash_free (lcm->fwd_entry_by_mapping_index);
2064       pool_free (lcm->fwd_entry_pool);
2065     }
2066
2067   /* update global flag */
2068   lcm->is_enabled = is_enable;
2069
2070   return 0;
2071 }
2072
2073 static clib_error_t *
2074 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2075                                 vlib_cli_command_t * cmd)
2076 {
2077   unformat_input_t _line_input, *line_input = &_line_input;
2078   u8 is_enabled = 0;
2079   u8 is_set = 0;
2080
2081   /* Get a line of input. */
2082   if (!unformat_user (input, unformat_line_input, line_input))
2083     return 0;
2084
2085   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2086     {
2087       if (unformat (line_input, "enable"))
2088         {
2089           is_set = 1;
2090           is_enabled = 1;
2091         }
2092       else if (unformat (line_input, "disable"))
2093         is_set = 1;
2094       else
2095         {
2096           return clib_error_return (0, "parse error: '%U'",
2097                                     format_unformat_error, line_input);
2098         }
2099     }
2100
2101   if (!is_set)
2102     return clib_error_return (0, "state not set");
2103
2104   return vnet_lisp_enable_disable (is_enabled);
2105 }
2106
2107 /* *INDENT-OFF* */
2108 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2109     .path = "lisp",
2110     .short_help = "lisp [enable|disable]",
2111     .function = lisp_enable_disable_command_fn,
2112 };
2113 /* *INDENT-ON* */
2114
2115 u8
2116 vnet_lisp_enable_disable_status (void)
2117 {
2118   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2119   return lcm->is_enabled;
2120 }
2121
2122 static u8 *
2123 format_lisp_status (u8 * s, va_list * args)
2124 {
2125   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2126   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2127 }
2128
2129 static clib_error_t *
2130 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2131                              vlib_cli_command_t * cmd)
2132 {
2133   u8 *msg = 0;
2134   msg = format (msg, "feature: %U\ngpe: %U\n",
2135                 format_lisp_status, format_vnet_lisp_gpe_status);
2136   vlib_cli_output (vm, "%v", msg);
2137   vec_free (msg);
2138   return 0;
2139 }
2140
2141 /* *INDENT-OFF* */
2142 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2143     .path = "show lisp status",
2144     .short_help = "show lisp status",
2145     .function = lisp_show_status_command_fn,
2146 };
2147 /* *INDENT-ON* */
2148
2149 static clib_error_t *
2150 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2151                                     unformat_input_t * input,
2152                                     vlib_cli_command_t * cmd)
2153 {
2154   hash_pair_t *p;
2155   unformat_input_t _line_input, *line_input = &_line_input;
2156   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2157   uword *vni_table = 0;
2158   u8 is_l2 = 0;
2159
2160   /* Get a line of input. */
2161   if (!unformat_user (input, unformat_line_input, line_input))
2162     return 0;
2163
2164   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2165     {
2166       if (unformat (line_input, "l2"))
2167         {
2168           vni_table = lcm->bd_id_by_vni;
2169           is_l2 = 1;
2170         }
2171       else if (unformat (line_input, "l3"))
2172         {
2173           vni_table = lcm->table_id_by_vni;
2174           is_l2 = 0;
2175         }
2176       else
2177         return clib_error_return (0, "parse error: '%U'",
2178                                   format_unformat_error, line_input);
2179     }
2180
2181   if (!vni_table)
2182     {
2183       vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2184       return 0;
2185     }
2186
2187   vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2188
2189   /* *INDENT-OFF* */
2190   hash_foreach_pair (p, vni_table,
2191   ({
2192     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2193   }));
2194   /* *INDENT-ON* */
2195
2196   return 0;
2197 }
2198
2199 /* *INDENT-OFF* */
2200 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2201     .path = "show lisp eid-table map",
2202     .short_help = "show lisp eid-table l2|l3",
2203     .function = lisp_show_eid_table_map_command_fn,
2204 };
2205 /* *INDENT-ON* */
2206
2207 static clib_error_t *
2208 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2209                                      unformat_input_t * input,
2210                                      vlib_cli_command_t * cmd)
2211 {
2212   lisp_gpe_main_t *lgm = &lisp_gpe_main;
2213   vnet_main_t *vnm = lgm->vnet_main;
2214   unformat_input_t _line_input, *line_input = &_line_input;
2215   u8 is_add = 1;
2216   clib_error_t *error = 0;
2217   u8 *locator_set_name = 0;
2218   locator_t locator, *locators = 0;
2219   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2220   u32 ls_index = 0;
2221   int rv = 0;
2222
2223   memset (&locator, 0, sizeof (locator));
2224   memset (a, 0, sizeof (a[0]));
2225
2226   /* Get a line of input. */
2227   if (!unformat_user (input, unformat_line_input, line_input))
2228     return 0;
2229
2230   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2231     {
2232       if (unformat (line_input, "add %_%v%_", &locator_set_name))
2233         is_add = 1;
2234       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2235         is_add = 0;
2236       else if (unformat (line_input, "iface %U p %d w %d",
2237                          unformat_vnet_sw_interface, vnm,
2238                          &locator.sw_if_index, &locator.priority,
2239                          &locator.weight))
2240         {
2241           locator.local = 1;
2242           vec_add1 (locators, locator);
2243         }
2244       else
2245         {
2246           error = unformat_parse_error (line_input);
2247           goto done;
2248         }
2249     }
2250
2251   a->name = locator_set_name;
2252   a->locators = locators;
2253   a->is_add = is_add;
2254   a->local = 1;
2255
2256   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2257   if (0 != rv)
2258     {
2259       error = clib_error_return (0, "failed to %s locator-set!",
2260                                  is_add ? "add" : "delete");
2261     }
2262
2263 done:
2264   vec_free (locators);
2265   if (locator_set_name)
2266     vec_free (locator_set_name);
2267   return error;
2268 }
2269
2270 /* *INDENT-OFF* */
2271 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2272     .path = "lisp locator-set",
2273     .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2274         "p <priority> w <weight>]",
2275     .function = lisp_add_del_locator_set_command_fn,
2276 };
2277 /* *INDENT-ON* */
2278
2279 static clib_error_t *
2280 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2281                                         unformat_input_t * input,
2282                                         vlib_cli_command_t * cmd)
2283 {
2284   lisp_gpe_main_t *lgm = &lisp_gpe_main;
2285   vnet_main_t *vnm = lgm->vnet_main;
2286   unformat_input_t _line_input, *line_input = &_line_input;
2287   u8 is_add = 1;
2288   clib_error_t *error = 0;
2289   u8 *locator_set_name = 0;
2290   u8 locator_set_name_set = 0;
2291   locator_t locator, *locators = 0;
2292   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2293   u32 ls_index = 0;
2294
2295   memset (&locator, 0, sizeof (locator));
2296   memset (a, 0, sizeof (a[0]));
2297
2298   /* Get a line of input. */
2299   if (!unformat_user (input, unformat_line_input, line_input))
2300     return 0;
2301
2302   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2303     {
2304       if (unformat (line_input, "add"))
2305         is_add = 1;
2306       else if (unformat (line_input, "del"))
2307         is_add = 0;
2308       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2309         locator_set_name_set = 1;
2310       else if (unformat (line_input, "iface %U p %d w %d",
2311                          unformat_vnet_sw_interface, vnm,
2312                          &locator.sw_if_index, &locator.priority,
2313                          &locator.weight))
2314         {
2315           locator.local = 1;
2316           vec_add1 (locators, locator);
2317         }
2318       else
2319         {
2320           error = unformat_parse_error (line_input);
2321           goto done;
2322         }
2323     }
2324
2325   if (!locator_set_name_set)
2326     {
2327       error = clib_error_return (0, "locator_set name not set!");
2328       goto done;
2329     }
2330
2331   a->name = locator_set_name;
2332   a->locators = locators;
2333   a->is_add = is_add;
2334   a->local = 1;
2335
2336   vnet_lisp_add_del_locator (a, 0, &ls_index);
2337
2338 done:
2339   vec_free (locators);
2340   vec_free (locator_set_name);
2341   return error;
2342 }
2343
2344 /* *INDENT-OFF* */
2345 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2346     .path = "lisp locator",
2347     .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2348                   "p <priority> w <weight>",
2349     .function = lisp_add_del_locator_in_set_command_fn,
2350 };
2351 /* *INDENT-ON* */
2352
2353 static clib_error_t *
2354 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2355                                       unformat_input_t * input,
2356                                       vlib_cli_command_t * cmd)
2357 {
2358   locator_set_t *lsit;
2359   locator_t *loc;
2360   u32 *locit;
2361   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2362
2363   vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2364                    "Priority", "Weight");
2365
2366   /* *INDENT-OFF* */
2367   pool_foreach (lsit, lcm->locator_set_pool,
2368   ({
2369     u8 * msg = 0;
2370     int next_line = 0;
2371     if (lsit->local)
2372       {
2373         msg = format (msg, "%v", lsit->name);
2374       }
2375     else
2376       {
2377         msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2378       }
2379     vec_foreach (locit, lsit->locator_indices)
2380       {
2381         if (next_line)
2382           {
2383             msg = format (msg, "%16s", " ");
2384           }
2385         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2386         if (loc->local)
2387           msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2388                         loc->weight);
2389         else
2390           msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2391                         &gid_address_ip(&loc->address), loc->priority,
2392                         loc->weight);
2393         next_line = 1;
2394       }
2395     vlib_cli_output (vm, "%v", msg);
2396     vec_free (msg);
2397   }));
2398   /* *INDENT-ON* */
2399   return 0;
2400 }
2401
2402 /* *INDENT-OFF* */
2403 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2404     .path = "show lisp locator-set",
2405     .short_help = "Shows locator-sets",
2406     .function = lisp_cp_show_locator_sets_command_fn,
2407 };
2408 /* *INDENT-ON* */
2409
2410 static map_resolver_t *
2411 get_map_resolver (ip_address_t * a)
2412 {
2413   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2414   map_resolver_t *mr;
2415
2416   vec_foreach (mr, lcm->map_resolvers)
2417   {
2418     if (!ip_address_cmp (&mr->address, a))
2419       {
2420         return mr;
2421       }
2422   }
2423   return 0;
2424 }
2425
2426 int
2427 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2428 {
2429   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2430   u32 i;
2431   map_resolver_t _mr, *mr = &_mr;
2432
2433   if (vnet_lisp_enable_disable_status () == 0)
2434     {
2435       clib_warning ("LISP is disabled!");
2436       return VNET_API_ERROR_LISP_DISABLED;
2437     }
2438
2439   if (a->is_add)
2440     {
2441
2442       if (get_map_resolver (&a->address))
2443         {
2444           clib_warning ("map-resolver %U already exists!", format_ip_address,
2445                         &a->address);
2446           return -1;
2447         }
2448
2449       memset (mr, 0, sizeof (*mr));
2450       ip_address_copy (&mr->address, &a->address);
2451       vec_add1 (lcm->map_resolvers, *mr);
2452
2453       if (vec_len (lcm->map_resolvers) == 1)
2454         lcm->do_map_resolver_election = 1;
2455     }
2456   else
2457     {
2458       for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2459         {
2460           mr = vec_elt_at_index (lcm->map_resolvers, i);
2461           if (!ip_address_cmp (&mr->address, &a->address))
2462             {
2463               if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2464                 lcm->do_map_resolver_election = 1;
2465
2466               vec_del1 (lcm->map_resolvers, i);
2467               break;
2468             }
2469         }
2470     }
2471   return 0;
2472 }
2473
2474 static clib_error_t *
2475 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2476                                       unformat_input_t * input,
2477                                       vlib_cli_command_t * cmd)
2478 {
2479   unformat_input_t _line_input, *line_input = &_line_input;
2480   u8 is_add = 1, addr_set = 0;
2481   ip_address_t ip_addr;
2482   clib_error_t *error = 0;
2483   int rv = 0;
2484   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2485
2486   /* Get a line of input. */
2487   if (!unformat_user (input, unformat_line_input, line_input))
2488     return 0;
2489
2490   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2491     {
2492       if (unformat (line_input, "add"))
2493         is_add = 1;
2494       else if (unformat (line_input, "del"))
2495         is_add = 0;
2496       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2497         addr_set = 1;
2498       else
2499         {
2500           error = unformat_parse_error (line_input);
2501           goto done;
2502         }
2503     }
2504
2505   if (!addr_set)
2506     {
2507       error = clib_error_return (0, "Map-resolver address must be set!");
2508       goto done;
2509     }
2510
2511   a->is_add = is_add;
2512   a->address = ip_addr;
2513   rv = vnet_lisp_add_del_map_resolver (a);
2514   if (0 != rv)
2515     {
2516       error = clib_error_return (0, "failed to %s map-resolver!",
2517                                  is_add ? "add" : "delete");
2518     }
2519
2520 done:
2521   return error;
2522 }
2523
2524 /* *INDENT-OFF* */
2525 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2526     .path = "lisp map-resolver",
2527     .short_help = "lisp map-resolver add/del <ip_address>",
2528     .function = lisp_add_del_map_resolver_command_fn,
2529 };
2530 /* *INDENT-ON* */
2531
2532 int
2533 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2534 {
2535   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2536   uword *p = 0;
2537
2538   if (vnet_lisp_enable_disable_status () == 0)
2539     {
2540       clib_warning ("LISP is disabled!");
2541       return VNET_API_ERROR_LISP_DISABLED;
2542     }
2543
2544   if (a->is_add)
2545     {
2546       p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2547       if (!p)
2548         {
2549           clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2550           return VNET_API_ERROR_INVALID_ARGUMENT;
2551         }
2552
2553       lcm->mreq_itr_rlocs = p[0];
2554     }
2555   else
2556     {
2557       lcm->mreq_itr_rlocs = ~0;
2558     }
2559
2560   return 0;
2561 }
2562
2563 static clib_error_t *
2564 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2565                                         unformat_input_t * input,
2566                                         vlib_cli_command_t * cmd)
2567 {
2568   unformat_input_t _line_input, *line_input = &_line_input;
2569   u8 is_add = 1;
2570   u8 *locator_set_name = 0;
2571   clib_error_t *error = 0;
2572   int rv = 0;
2573   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
2574
2575   /* Get a line of input. */
2576   if (!unformat_user (input, unformat_line_input, line_input))
2577     return 0;
2578
2579   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2580     {
2581       if (unformat (line_input, "del"))
2582         is_add = 0;
2583       else if (unformat (line_input, "add %s", &locator_set_name))
2584         is_add = 1;
2585       else
2586         {
2587           error = unformat_parse_error (line_input);
2588           goto done;
2589         }
2590     }
2591
2592   a->is_add = is_add;
2593   a->locator_set_name = locator_set_name;
2594   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
2595   if (0 != rv)
2596     {
2597       error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
2598                                  is_add ? "add" : "delete");
2599     }
2600
2601   vec_free (locator_set_name);
2602
2603 done:
2604   return error;
2605
2606 }
2607
2608 /* *INDENT-OFF* */
2609 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
2610     .path = "lisp map-request itr-rlocs",
2611     .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
2612     .function = lisp_add_del_mreq_itr_rlocs_command_fn,
2613 };
2614 /* *INDENT-ON* */
2615
2616 static clib_error_t *
2617 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2618                                      unformat_input_t * input,
2619                                      vlib_cli_command_t * cmd)
2620 {
2621   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2622   locator_set_t *loc_set;
2623
2624   vlib_cli_output (vm, "%=20s", "itr-rlocs");
2625
2626   if (~0 == lcm->mreq_itr_rlocs)
2627     {
2628       return 0;
2629     }
2630
2631   loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
2632
2633   vlib_cli_output (vm, "%=20s", loc_set->name);
2634
2635   return 0;
2636 }
2637
2638 /* *INDENT-OFF* */
2639 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
2640     .path = "show lisp map-request itr-rlocs",
2641     .short_help = "Shows map-request itr-rlocs",
2642     .function = lisp_show_mreq_itr_rlocs_command_fn,
2643 };
2644 /* *INDENT-ON* */
2645
2646 /* Statistics (not really errors) */
2647 #define foreach_lisp_cp_lookup_error           \
2648 _(DROP, "drop")                                \
2649 _(MAP_REQUESTS_SENT, "map-request sent")
2650
2651 static char *lisp_cp_lookup_error_strings[] = {
2652 #define _(sym,string) string,
2653   foreach_lisp_cp_lookup_error
2654 #undef _
2655 };
2656
2657 typedef enum
2658 {
2659 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2660   foreach_lisp_cp_lookup_error
2661 #undef _
2662     LISP_CP_LOOKUP_N_ERROR,
2663 } lisp_cp_lookup_error_t;
2664
2665 typedef enum
2666 {
2667   LISP_CP_LOOKUP_NEXT_DROP,
2668   LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
2669   LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
2670   LISP_CP_LOOKUP_N_NEXT,
2671 } lisp_cp_lookup_next_t;
2672
2673 typedef struct
2674 {
2675   gid_address_t dst_eid;
2676   ip_address_t map_resolver_ip;
2677 } lisp_cp_lookup_trace_t;
2678
2679 u8 *
2680 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2681 {
2682   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2683   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2684   lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2685
2686   s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2687               format_ip_address, &t->map_resolver_ip, format_gid_address,
2688               &t->dst_eid);
2689   return s;
2690 }
2691
2692 int
2693 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2694                            ip_address_t * sloc)
2695 {
2696   map_resolver_t *mrit;
2697   ip_address_t *a;
2698
2699   if (vec_len (lcm->map_resolvers) == 0)
2700     {
2701       clib_warning ("No map-resolver configured");
2702       return 0;
2703     }
2704
2705   /* find the first mr ip we have a route to and the ip of the
2706    * iface that has a route to it */
2707   vec_foreach (mrit, lcm->map_resolvers)
2708   {
2709     a = &mrit->address;
2710     if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2711       {
2712         ip_address_copy (mr_ip, a);
2713
2714         /* also update globals */
2715         return 1;
2716       }
2717   }
2718
2719   clib_warning ("Can't find map-resolver and local interface ip!");
2720   return 0;
2721 }
2722
2723 static gid_address_t *
2724 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2725 {
2726   void *addr;
2727   u32 i;
2728   locator_t *loc;
2729   u32 *loc_indexp;
2730   ip_interface_address_t *ia = 0;
2731   gid_address_t gid_data, *gid = &gid_data;
2732   gid_address_t *rlocs = 0;
2733   ip_prefix_t *ippref = &gid_address_ippref (gid);
2734   ip_address_t *rloc = &ip_prefix_addr (ippref);
2735
2736   memset (gid, 0, sizeof (gid[0]));
2737   gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2738   for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2739     {
2740       loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2741       loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2742
2743       /* Add ipv4 locators first TODO sort them */
2744
2745       /* *INDENT-OFF* */
2746       foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2747                                     loc->sw_if_index, 1 /* unnumbered */,
2748       ({
2749         addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2750         ip_address_set (rloc, addr, IP4);
2751         ip_prefix_len (ippref) = 32;
2752         ip_prefix_normalize (ippref);
2753         vec_add1 (rlocs, gid[0]);
2754       }));
2755
2756       /* Add ipv6 locators */
2757       foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2758                                     loc->sw_if_index, 1 /* unnumbered */,
2759       ({
2760         addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2761         ip_address_set (rloc, addr, IP6);
2762         ip_prefix_len (ippref) = 128;
2763         ip_prefix_normalize (ippref);
2764         vec_add1 (rlocs, gid[0]);
2765       }));
2766       /* *INDENT-ON* */
2767
2768     }
2769   return rlocs;
2770 }
2771
2772 static vlib_buffer_t *
2773 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2774                                 gid_address_t * seid, gid_address_t * deid,
2775                                 locator_set_t * loc_set, ip_address_t * mr_ip,
2776                                 ip_address_t * sloc, u8 is_smr_invoked,
2777                                 u64 * nonce_res, u32 * bi_res)
2778 {
2779   vlib_buffer_t *b;
2780   u32 bi;
2781   gid_address_t *rlocs = 0;
2782   vlib_main_t *vm = lcm->vlib_main;
2783
2784   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2785     {
2786       clib_warning ("Can't allocate buffer for Map-Request!");
2787       return 0;
2788     }
2789
2790   b = vlib_get_buffer (vm, bi);
2791
2792   /* leave some space for the encap headers */
2793   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2794
2795   /* get rlocs */
2796   rlocs = build_itr_rloc_list (lcm, loc_set);
2797
2798   if (MR_MODE_SRC_DST == lcm->map_request_mode)
2799     {
2800       gid_address_t sd;
2801       memset (&sd, 0, sizeof (sd));
2802       build_src_dst (&sd, seid, deid);
2803       lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked, nonce_res);
2804     }
2805   else
2806     {
2807       /* put lisp msg */
2808       lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2809                          nonce_res);
2810     }
2811
2812   /* push ecm: udp-ip-lisp */
2813   lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2814
2815   /* push outer ip header */
2816   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2817                        mr_ip);
2818
2819   bi_res[0] = bi;
2820
2821   vec_free (rlocs);
2822   return b;
2823 }
2824
2825 static void
2826 reset_pending_mr_counters (pending_map_request_t * r)
2827 {
2828   r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2829   r->retries_num = 0;
2830 }
2831
2832 static int
2833 elect_map_resolver (lisp_cp_main_t * lcm)
2834 {
2835   map_resolver_t *mr;
2836
2837   vec_foreach (mr, lcm->map_resolvers)
2838   {
2839     if (!mr->is_down)
2840       {
2841         ip_address_copy (&lcm->active_map_resolver, &mr->address);
2842         lcm->do_map_resolver_election = 0;
2843         return 1;
2844       }
2845   }
2846   return 0;
2847 }
2848
2849 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2850   _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2851
2852 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2853   _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2854
2855 static int
2856 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2857                                 gid_address_t * seid, gid_address_t * deid,
2858                                 u8 is_smr_invoked, u8 is_resend)
2859 {
2860   map_resolver_t *mr;
2861   u32 next_index, bi = 0, *to_next, map_index;
2862   vlib_buffer_t *b;
2863   vlib_frame_t *f;
2864   u64 nonce = 0;
2865   locator_set_t *loc_set;
2866   mapping_t *map;
2867   pending_map_request_t *pmr, *duplicate_pmr = 0;
2868   ip_address_t sloc;
2869   u32 ls_index;
2870
2871   ASSERT (*lcm->pending_map_request_lock);
2872
2873   /* if there is already a pending request remember it */
2874
2875   /* *INDENT-OFF* */
2876   pool_foreach(pmr, lcm->pending_map_requests_pool,
2877   ({
2878     if (!gid_address_cmp (&pmr->src, seid)
2879         && !gid_address_cmp (&pmr->dst, deid))
2880       {
2881         duplicate_pmr = pmr;
2882         break;
2883       }
2884   }));
2885   /* *INDENT-ON* */
2886
2887   if (!is_resend && duplicate_pmr)
2888     {
2889       /* don't send the request if there is a pending map request already */
2890       return 0;
2891     }
2892
2893   /* get locator-set for seid */
2894   if (!lcm->lisp_pitr)
2895     {
2896       map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2897       if (map_index == ~0)
2898         {
2899           clib_warning ("No local mapping found in eid-table for %U!",
2900                         format_gid_address, seid);
2901           return -1;
2902         }
2903
2904       map = pool_elt_at_index (lcm->mapping_pool, map_index);
2905
2906       if (!map->local)
2907         {
2908           clib_warning
2909             ("Mapping found for src eid %U is not marked as local!",
2910              format_gid_address, seid);
2911           return -1;
2912         }
2913       ls_index = map->locator_set_index;
2914     }
2915   else
2916     {
2917       map_index = lcm->pitr_map_index;
2918       map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2919       ls_index = map->locator_set_index;
2920     }
2921
2922   /* overwrite locator set if map-request itr-rlocs configured */
2923   if (~0 != lcm->mreq_itr_rlocs)
2924     {
2925       ls_index = lcm->mreq_itr_rlocs;
2926     }
2927
2928   loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2929
2930   while (lcm->do_map_resolver_election
2931          | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2932                                                      &lcm->active_map_resolver,
2933                                                      &sloc)))
2934     {
2935       if (0 == elect_map_resolver (lcm))
2936         /* all Mrs are down */
2937         {
2938           if (duplicate_pmr)
2939             duplicate_pmr->to_be_removed = 1;
2940
2941           /* restart MR checking by marking all of them up */
2942           vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2943
2944           return -1;
2945         }
2946     }
2947
2948   /* build the encapsulated map request */
2949   b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2950                                       &lcm->active_map_resolver,
2951                                       &sloc, is_smr_invoked, &nonce, &bi);
2952
2953   if (!b)
2954     return -1;
2955
2956   /* set fib index to default and lookup node */
2957   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2958   next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2959     ip4_lookup_node.index : ip6_lookup_node.index;
2960
2961   f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2962
2963   /* Enqueue the packet */
2964   to_next = vlib_frame_vector_args (f);
2965   to_next[0] = bi;
2966   f->n_vectors = 1;
2967   vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2968
2969   if (duplicate_pmr)
2970     /* if there is a pending request already update it */
2971     {
2972       if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2973         {
2974           /* remove the oldest nonce */
2975           u64 CLIB_UNUSED (tmp), *nonce_del;
2976           nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2977           hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2978           clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2979         }
2980
2981       clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2982       hash_set (lcm->pending_map_requests_by_nonce, nonce,
2983                 duplicate_pmr - lcm->pending_map_requests_pool);
2984     }
2985   else
2986     {
2987       /* add map-request to pending requests table */
2988       pool_get (lcm->pending_map_requests_pool, pmr);
2989       memset (pmr, 0, sizeof (*pmr));
2990       gid_address_copy (&pmr->src, seid);
2991       gid_address_copy (&pmr->dst, deid);
2992       clib_fifo_add1 (pmr->nonces, nonce);
2993       pmr->is_smr_invoked = is_smr_invoked;
2994       reset_pending_mr_counters (pmr);
2995       hash_set (lcm->pending_map_requests_by_nonce, nonce,
2996                 pmr - lcm->pending_map_requests_pool);
2997     }
2998
2999   return 0;
3000 }
3001
3002 static void
3003 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3004 {
3005   ip4_header_t *ip4 = hdr;
3006   ip6_header_t *ip6;
3007
3008   if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3009     {
3010       ip_address_set (src, &ip4->src_address, IP4);
3011       ip_address_set (dst, &ip4->dst_address, IP4);
3012     }
3013   else
3014     {
3015       ip6 = hdr;
3016       ip_address_set (src, &ip6->src_address, IP6);
3017       ip_address_set (dst, &ip6->dst_address, IP6);
3018     }
3019 }
3020
3021 static u32
3022 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3023                              u8 version)
3024 {
3025   uword *vnip;
3026   u32 vni = ~0, table_id = ~0;
3027
3028   table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3029                                                       IP4 ? FIB_PROTOCOL_IP4 :
3030                                                       FIB_PROTOCOL_IP6),
3031                                                      vnet_buffer
3032                                                      (b)->sw_if_index
3033                                                      [VLIB_RX]);
3034
3035   vnip = hash_get (lcm->vni_by_table_id, table_id);
3036   if (vnip)
3037     vni = vnip[0];
3038   else
3039     clib_warning ("vrf %d is not mapped to any vni!", table_id);
3040
3041   return vni;
3042 }
3043
3044 always_inline u32
3045 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3046 {
3047   uword *vnip;
3048   u32 vni = ~0;
3049   u32 sw_if_index0;
3050
3051   l2input_main_t *l2im = &l2input_main;
3052   l2_input_config_t *config;
3053   l2_bridge_domain_t *bd_config;
3054
3055   sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3056   config = vec_elt_at_index (l2im->configs, sw_if_index0);
3057   bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3058
3059   vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3060   if (vnip)
3061     vni = vnip[0];
3062   else
3063     clib_warning ("bridge domain %d is not mapped to any vni!",
3064                   config->bd_index);
3065
3066   return vni;
3067 }
3068
3069 always_inline void
3070 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3071                                   gid_address_t * src, gid_address_t * dst)
3072 {
3073   u32 vni = 0;
3074   u16 type;
3075
3076   memset (src, 0, sizeof (*src));
3077   memset (dst, 0, sizeof (*dst));
3078   type = vnet_buffer (b)->lisp.overlay_afi;
3079
3080   if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3081     {
3082       ip4_header_t *ip;
3083       u8 version, preflen;
3084
3085       gid_address_type (src) = GID_ADDR_IP_PREFIX;
3086       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3087
3088       ip = vlib_buffer_get_current (b);
3089       get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3090
3091       version = gid_address_ip_version (src);
3092       preflen = ip_address_max_len (version);
3093       gid_address_ippref_len (src) = preflen;
3094       gid_address_ippref_len (dst) = preflen;
3095
3096       vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3097       gid_address_vni (dst) = vni;
3098       gid_address_vni (src) = vni;
3099     }
3100   else if (LISP_AFI_MAC == type)
3101     {
3102       ethernet_header_t *eh;
3103
3104       eh = vlib_buffer_get_current (b);
3105
3106       gid_address_type (src) = GID_ADDR_MAC;
3107       gid_address_type (dst) = GID_ADDR_MAC;
3108       mac_copy (&gid_address_mac (src), eh->src_address);
3109       mac_copy (&gid_address_mac (dst), eh->dst_address);
3110
3111       /* get vni */
3112       vni = lisp_get_vni_from_buffer_eth (lcm, b);
3113
3114       gid_address_vni (dst) = vni;
3115       gid_address_vni (src) = vni;
3116     }
3117 }
3118
3119 static uword
3120 lisp_cp_lookup_inline (vlib_main_t * vm,
3121                        vlib_node_runtime_t * node,
3122                        vlib_frame_t * from_frame, int overlay)
3123 {
3124   u32 *from, *to_next_drop, di, si;
3125   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3126   u32 pkts_mapped = 0;
3127   uword n_left_from, n_left_to_next_drop;
3128
3129   from = vlib_frame_vector_args (from_frame);
3130   n_left_from = from_frame->n_vectors;
3131
3132   while (n_left_from > 0)
3133     {
3134       vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3135                            to_next_drop, n_left_to_next_drop);
3136
3137       while (n_left_from > 0 && n_left_to_next_drop > 0)
3138         {
3139           u32 pi0;
3140           vlib_buffer_t *b0;
3141           gid_address_t src, dst;
3142
3143           pi0 = from[0];
3144           from += 1;
3145           n_left_from -= 1;
3146           to_next_drop[0] = pi0;
3147           to_next_drop += 1;
3148           n_left_to_next_drop -= 1;
3149
3150           b0 = vlib_get_buffer (vm, pi0);
3151           b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3152           vnet_buffer (b0)->lisp.overlay_afi = overlay;
3153
3154           /* src/dst eid pair */
3155           get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3156
3157           /* if we have remote mapping for destination already in map-chache
3158              add forwarding tunnel directly. If not send a map-request */
3159           di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3160                                          &src);
3161           if (~0 != di)
3162             {
3163               mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3164               /* send a map-request also in case of negative mapping entry
3165                  with corresponding action */
3166               if (m->action == LISP_SEND_MAP_REQUEST)
3167                 {
3168                   /* send map-request */
3169                   queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3170                                      0 /* is_resend */ );
3171                   pkts_mapped++;
3172                 }
3173               else
3174                 {
3175                   si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3176                                               &src);
3177                   if (~0 != si)
3178                     {
3179                       dp_add_fwd_entry (lcm, si, di);
3180                     }
3181                 }
3182             }
3183           else
3184             {
3185               /* send map-request */
3186               queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3187                                  0 /* is_resend */ );
3188               pkts_mapped++;
3189             }
3190
3191           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3192             {
3193               lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3194                                                            sizeof (*tr));
3195
3196               memset (tr, 0, sizeof (*tr));
3197               gid_address_copy (&tr->dst_eid, &dst);
3198               ip_address_copy (&tr->map_resolver_ip,
3199                                &lcm->active_map_resolver);
3200             }
3201           gid_address_free (&dst);
3202           gid_address_free (&src);
3203         }
3204
3205       vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3206                            n_left_to_next_drop);
3207     }
3208   vlib_node_increment_counter (vm, node->node_index,
3209                                LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3210                                pkts_mapped);
3211   return from_frame->n_vectors;
3212 }
3213
3214 static uword
3215 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3216                     vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3217 {
3218   return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3219 }
3220
3221 static uword
3222 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3223                     vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3224 {
3225   return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3226 }
3227
3228 /* *INDENT-OFF* */
3229 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3230   .function = lisp_cp_lookup_ip4,
3231   .name = "lisp-cp-lookup-ip4",
3232   .vector_size = sizeof (u32),
3233   .format_trace = format_lisp_cp_lookup_trace,
3234   .type = VLIB_NODE_TYPE_INTERNAL,
3235
3236   .n_errors = LISP_CP_LOOKUP_N_ERROR,
3237   .error_strings = lisp_cp_lookup_error_strings,
3238
3239   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3240
3241   .next_nodes = {
3242       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3243       [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
3244       [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
3245   },
3246 };
3247 /* *INDENT-ON* */
3248
3249 /* *INDENT-OFF* */
3250 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3251   .function = lisp_cp_lookup_ip6,
3252   .name = "lisp-cp-lookup-ip6",
3253   .vector_size = sizeof (u32),
3254   .format_trace = format_lisp_cp_lookup_trace,
3255   .type = VLIB_NODE_TYPE_INTERNAL,
3256
3257   .n_errors = LISP_CP_LOOKUP_N_ERROR,
3258   .error_strings = lisp_cp_lookup_error_strings,
3259
3260   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3261
3262   .next_nodes = {
3263       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3264       [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
3265       [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
3266   },
3267 };
3268 /* *INDENT-ON* */
3269
3270 /* lisp_cp_input statistics */
3271 #define foreach_lisp_cp_input_error                     \
3272 _(DROP, "drop")                                         \
3273 _(MAP_REPLIES_RECEIVED, "map-replies received")
3274
3275 static char *lisp_cp_input_error_strings[] = {
3276 #define _(sym,string) string,
3277   foreach_lisp_cp_input_error
3278 #undef _
3279 };
3280
3281 typedef enum
3282 {
3283 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3284   foreach_lisp_cp_input_error
3285 #undef _
3286     LISP_CP_INPUT_N_ERROR,
3287 } lisp_cp_input_error_t;
3288
3289 typedef enum
3290 {
3291   LISP_CP_INPUT_NEXT_DROP,
3292   LISP_CP_INPUT_N_NEXT,
3293 } lisp_cp_input_next_t;
3294
3295 typedef struct
3296 {
3297   gid_address_t dst_eid;
3298   ip4_address_t map_resolver_ip;
3299 } lisp_cp_input_trace_t;
3300
3301 u8 *
3302 format_lisp_cp_input_trace (u8 * s, va_list * args)
3303 {
3304   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3305   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3306   CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3307     va_arg (*args, lisp_cp_input_trace_t *);
3308
3309   s = format (s, "LISP-CP-INPUT: TODO");
3310   return s;
3311 }
3312
3313 void *
3314 process_map_reply (void *arg)
3315 {
3316   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3317   vlib_buffer_t *b = arg;
3318   u32 len = 0, i, ttl, dst_map_index = 0;
3319   void *h;
3320   pending_map_request_t *pmr;
3321   locator_t probed;
3322   map_reply_hdr_t *mrep_hdr;
3323   u64 nonce, *noncep;
3324   gid_address_t deid;
3325   uword *pmr_index;
3326   u8 authoritative, action;
3327   locator_t *locators = 0, *loc;
3328
3329   mrep_hdr = vlib_buffer_get_current (b);
3330
3331   lisp_pending_map_request_lock (lcm);
3332
3333   /* Check pending requests table and nonce */
3334   nonce = MREP_NONCE (mrep_hdr);
3335   pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
3336   if (!pmr_index)
3337     {
3338       clib_warning ("No pending map-request entry with nonce %lu!", nonce);
3339       goto done;
3340     }
3341   pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3342
3343   vlib_buffer_pull (b, sizeof (*mrep_hdr));
3344
3345   for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3346     {
3347       h = vlib_buffer_get_current (b);
3348       ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3349       action = MAP_REC_ACTION (h);
3350       authoritative = MAP_REC_AUTH (h);
3351
3352       len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3353       if (len == ~0)
3354         {
3355           clib_warning ("Failed to parse mapping record!");
3356           vec_foreach (loc, locators)
3357           {
3358             locator_free (loc);
3359           }
3360           vec_free (locators);
3361           goto done;
3362         }
3363
3364       /* insert/update mappings cache */
3365       vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
3366                                  1, 0 /* is_static */ , &dst_map_index);
3367
3368       /* try to program forwarding only if mapping saved or updated */
3369       if ((u32) ~ 0 != dst_map_index)
3370         lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
3371
3372       vec_free (locators);
3373     }
3374
3375   /* remove pending map request entry */
3376
3377   /* *INDENT-OFF* */
3378   clib_fifo_foreach (noncep, pmr->nonces, ({
3379     hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3380   }));
3381   /* *INDENT-ON* */
3382
3383   clib_fifo_free (pmr->nonces);
3384   pool_put (lcm->pending_map_requests_pool, pmr);
3385
3386 done:
3387   lisp_pending_map_request_unlock (lcm);
3388   return 0;
3389 }
3390
3391 void
3392 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
3393                      vlib_buffer_t * b)
3394 {
3395   map_request_hdr_t *mreq_hdr;
3396   gid_address_t src, dst;
3397 //  u64 nonce;
3398   u32 i, len = 0;
3399   gid_address_t *itr_rlocs = 0, *rloc;
3400
3401   mreq_hdr = vlib_buffer_get_current (b);
3402   vlib_buffer_pull (b, sizeof (*mreq_hdr));
3403
3404 //  nonce = MREQ_NONCE(mreq_hdr);
3405
3406   if (!MREQ_SMR (mreq_hdr))
3407     {
3408       clib_warning ("Only SMR Map-Requests supported for now!");
3409       return;
3410     }
3411
3412   /* parse src eid */
3413   len = lisp_msg_parse_addr (b, &src);
3414   if (len == ~0)
3415     return;
3416
3417   /* for now we don't do anything with the itr's rlocs */
3418   len =
3419     lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3420                               MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3421   if (len == ~0)
3422     return;
3423
3424   /* TODO: RLOCs are currently unused, so free them for now */
3425   vec_foreach (rloc, itr_rlocs)
3426   {
3427     gid_address_free (rloc);
3428   }
3429
3430   /* parse eid records and send SMR-invoked map-requests */
3431   for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3432     {
3433       memset (&dst, 0, sizeof (dst));
3434       len = lisp_msg_parse_eid_rec (b, &dst);
3435       if (len == ~0)
3436         {
3437           clib_warning ("Can't parse map-request EID-record");
3438           return;
3439         }
3440       /* send SMR-invoked map-requests */
3441       queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3442     }
3443 }
3444
3445 static void
3446 queue_map_reply (vlib_buffer_t * b)
3447 {
3448   vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
3449
3450   clib_memcpy (a->data, b->data + b->current_data, b->current_length);
3451   a->current_length = b->current_length;
3452   a->current_data = 0;
3453
3454   vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
3455                                + a->current_length);
3456   clib_mem_free (a);
3457 }
3458
3459 static uword
3460 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3461                vlib_frame_t * from_frame)
3462 {
3463   u32 n_left_from, *from, *to_next_drop;
3464   lisp_msg_type_e type;
3465   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3466
3467   from = vlib_frame_vector_args (from_frame);
3468   n_left_from = from_frame->n_vectors;
3469
3470
3471   while (n_left_from > 0)
3472     {
3473       u32 n_left_to_next_drop;
3474
3475       vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3476                            to_next_drop, n_left_to_next_drop);
3477       while (n_left_from > 0 && n_left_to_next_drop > 0)
3478         {
3479           u32 bi0;
3480           vlib_buffer_t *b0;
3481
3482           bi0 = from[0];
3483           from += 1;
3484           n_left_from -= 1;
3485           to_next_drop[0] = bi0;
3486           to_next_drop += 1;
3487           n_left_to_next_drop -= 1;
3488
3489           b0 = vlib_get_buffer (vm, bi0);
3490
3491           type = lisp_msg_type (vlib_buffer_get_current (b0));
3492           switch (type)
3493             {
3494             case LISP_MAP_REPLY:
3495               queue_map_reply (b0);
3496               break;
3497             case LISP_MAP_REQUEST:
3498               process_map_request (vm, lcm, b0);
3499               break;
3500             default:
3501               clib_warning ("Unsupported LISP message type %d", type);
3502               break;
3503             }
3504
3505           b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3506
3507           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3508             {
3509
3510             }
3511         }
3512
3513       vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3514                            n_left_to_next_drop);
3515     }
3516   return from_frame->n_vectors;
3517 }
3518
3519 /* *INDENT-OFF* */
3520 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3521   .function = lisp_cp_input,
3522   .name = "lisp-cp-input",
3523   .vector_size = sizeof (u32),
3524   .format_trace = format_lisp_cp_input_trace,
3525   .type = VLIB_NODE_TYPE_INTERNAL,
3526
3527   .n_errors = LISP_CP_INPUT_N_ERROR,
3528   .error_strings = lisp_cp_input_error_strings,
3529
3530   .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3531
3532   .next_nodes = {
3533       [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3534   },
3535 };
3536 /* *INDENT-ON* */
3537
3538 clib_error_t *
3539 lisp_cp_init (vlib_main_t * vm)
3540 {
3541   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3542   clib_error_t *error = 0;
3543
3544   if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3545     return error;
3546
3547   lcm->im4 = &ip4_main;
3548   lcm->im6 = &ip6_main;
3549   lcm->vlib_main = vm;
3550   lcm->vnet_main = vnet_get_main ();
3551   lcm->mreq_itr_rlocs = ~0;
3552   lcm->lisp_pitr = 0;
3553   memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3554
3555   lcm->pending_map_request_lock =
3556     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
3557
3558   lcm->pending_map_request_lock[0] = 0;
3559   gid_dictionary_init (&lcm->mapping_index_by_gid);
3560   lcm->do_map_resolver_election = 1;
3561   lcm->map_request_mode = MR_MODE_DST_ONLY;
3562
3563   /* default vrf mapped to vni 0 */
3564   hash_set (lcm->table_id_by_vni, 0, 0);
3565   hash_set (lcm->vni_by_table_id, 0, 0);
3566
3567   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3568                          lisp_cp_input_node.index, 1 /* is_ip4 */ );
3569   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3570                          lisp_cp_input_node.index, 0 /* is_ip4 */ );
3571
3572   return 0;
3573 }
3574
3575 static void *
3576 send_map_request_thread_fn (void *arg)
3577 {
3578   map_request_args_t *a = arg;
3579   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3580
3581   lisp_pending_map_request_lock (lcm);
3582
3583   if (a->is_resend)
3584     resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3585   else
3586     send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3587
3588   lisp_pending_map_request_unlock (lcm);
3589
3590   return 0;
3591 }
3592
3593 static int
3594 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3595                    u8 smr_invoked, u8 is_resend)
3596 {
3597   map_request_args_t a;
3598
3599   a.is_resend = is_resend;
3600   gid_address_copy (&a.seid, seid);
3601   gid_address_copy (&a.deid, deid);
3602   a.smr_invoked = smr_invoked;
3603
3604   vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3605                                (u8 *) & a, sizeof (a));
3606   return 0;
3607 }
3608
3609 /**
3610  * Take an action with a pending map request depending on expiration time
3611  * and re-try counters.
3612  */
3613 static void
3614 update_pending_request (pending_map_request_t * r, f64 dt)
3615 {
3616   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3617   map_resolver_t *mr;
3618
3619   if (r->time_to_expire - dt < 0)
3620     /* it's time to decide what to do with this pending request */
3621     {
3622       if (r->retries_num >= NUMBER_OF_RETRIES)
3623         /* too many retries -> assume current map resolver is not available */
3624         {
3625           mr = get_map_resolver (&lcm->active_map_resolver);
3626           if (!mr)
3627             {
3628               clib_warning ("Map resolver %U not found - probably deleted "
3629                             "by the user recently.", format_ip_address,
3630                             &lcm->active_map_resolver);
3631             }
3632           else
3633             {
3634               clib_warning ("map resolver %U is unreachable, ignoring",
3635                             format_ip_address, &lcm->active_map_resolver);
3636
3637               /* mark current map resolver unavailable so it won't be
3638                * selected next time */
3639               mr->is_down = 1;
3640               mr->last_update = vlib_time_now (lcm->vlib_main);
3641             }
3642
3643           reset_pending_mr_counters (r);
3644           elect_map_resolver (lcm);
3645
3646           /* try to find a next eligible map resolver and re-send */
3647           queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3648                              1 /* resend */ );
3649         }
3650       else
3651         {
3652           /* try again */
3653           queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3654                              1 /* resend */ );
3655           r->retries_num++;
3656           r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3657         }
3658     }
3659   else
3660     r->time_to_expire -= dt;
3661 }
3662
3663 static void
3664 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3665 {
3666   u64 *nonce;
3667   pending_map_request_t *pmr;
3668   u32 *to_be_removed = 0, *pmr_index;
3669
3670   ASSERT (*lcm->pending_map_request_lock);
3671
3672   /* *INDENT-OFF* */
3673   pool_foreach (pmr, lcm->pending_map_requests_pool,
3674   ({
3675     if (pmr->to_be_removed)
3676       {
3677         clib_fifo_foreach (nonce, pmr->nonces, ({
3678           hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3679         }));
3680
3681         vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3682       }
3683   }));
3684   /* *INDENT-ON* */
3685
3686   vec_foreach (pmr_index, to_be_removed)
3687     pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3688
3689   vec_free (to_be_removed);
3690 }
3691
3692 static uword
3693 send_map_resolver_service (vlib_main_t * vm,
3694                            vlib_node_runtime_t * rt, vlib_frame_t * f)
3695 {
3696   f64 period = 2.0;
3697   pending_map_request_t *pmr;
3698   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3699
3700   while (1)
3701     {
3702       vlib_process_wait_for_event_or_clock (vm, period);
3703
3704       /* currently no signals are expected - just wait for clock */
3705       (void) vlib_process_get_events (vm, 0);
3706
3707       lisp_pending_map_request_lock (lcm);
3708
3709       /* *INDENT-OFF* */
3710       pool_foreach (pmr, lcm->pending_map_requests_pool,
3711       ({
3712         if (!pmr->to_be_removed)
3713           update_pending_request (pmr, period);
3714       }));
3715       /* *INDENT-ON* */
3716
3717       remove_dead_pending_map_requests (lcm);
3718
3719       lisp_pending_map_request_unlock (lcm);
3720     }
3721
3722   /* unreachable */
3723   return 0;
3724 }
3725
3726 /* *INDENT-OFF* */
3727 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3728     .function = send_map_resolver_service,
3729     .type = VLIB_NODE_TYPE_PROCESS,
3730     .name = "lisp-retry-service",
3731     .process_log2_n_stack_bytes = 16,
3732 };
3733 /* *INDENT-ON* */
3734
3735 VLIB_INIT_FUNCTION (lisp_cp_init);
3736
3737 /*
3738  * fd.io coding-style-patch-verification: ON
3739  *
3740  * Local Variables:
3741  * eval: (c-set-style "gnu")
3742  * End:
3743  */