LISP mapping timers
[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/lisp-gpe/lisp_gpe_fwd_entry.h>
22 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_table.h>
25
26 #include <openssl/evp.h>
27 #include <openssl/hmac.h>
28
29 typedef struct
30 {
31   u8 is_resend;
32   gid_address_t seid;
33   gid_address_t deid;
34   u8 smr_invoked;
35 } map_request_args_t;
36
37 u8
38 vnet_lisp_get_map_request_mode (void)
39 {
40   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
41   return lcm->map_request_mode;
42 }
43
44 static u16
45 auth_data_len_by_key_id (lisp_key_type_t key_id)
46 {
47   switch (key_id)
48     {
49     case HMAC_SHA_1_96:
50       return SHA1_AUTH_DATA_LEN;
51     case HMAC_SHA_256_128:
52       return SHA256_AUTH_DATA_LEN;
53     default:
54       clib_warning ("unsupported key type: %d!", key_id);
55       return (u16) ~ 0;
56     }
57   return (u16) ~ 0;
58 }
59
60 static const EVP_MD *
61 get_encrypt_fcn (lisp_key_type_t key_id)
62 {
63   switch (key_id)
64     {
65     case HMAC_SHA_1_96:
66       return EVP_sha1 ();
67     case HMAC_SHA_256_128:
68       return EVP_sha256 ();
69     default:
70       clib_warning ("unsupported encryption key type: %d!", key_id);
71       break;
72     }
73   return 0;
74 }
75
76 static int
77 queue_map_request (gid_address_t * seid, gid_address_t * deid,
78                    u8 smr_invoked, u8 is_resend);
79
80 ip_interface_address_t *
81 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
82                                           u32 sw_if_index, u8 loop)
83 {
84   vnet_main_t *vnm = vnet_get_main ();
85   vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
86   if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
87     sw_if_index = swif->unnumbered_sw_if_index;
88   u32 ia =
89     (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
90     vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
91     (u32) ~ 0;
92   return pool_elt_at_index ((lm)->if_address_pool, ia);
93 }
94
95 void *
96 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
97                                 u8 version)
98 {
99   ip_interface_address_t *ia;
100
101   ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
102   if (!ia)
103     return 0;
104   return ip_interface_address_get_address (lm, ia);
105 }
106
107 int
108 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
109                                    u8 version, ip_address_t * result)
110 {
111   ip_lookup_main_t *lm;
112   void *addr;
113
114   lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
115   addr = ip_interface_get_first_address (lm, sw_if_index, version);
116   if (!addr)
117     return 0;
118
119   ip_address_set (result, addr, version);
120   return 1;
121 }
122
123 /**
124  * convert from a LISP address to a FIB prefix
125  */
126 void
127 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
128 {
129   if (addr->version == IP4)
130     {
131       prefix->fp_len = 32;
132       prefix->fp_proto = FIB_PROTOCOL_IP4;
133       memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
134       memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
135     }
136   else
137     {
138       prefix->fp_len = 128;
139       prefix->fp_proto = FIB_PROTOCOL_IP6;
140       memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
141     }
142 }
143
144 /**
145  * convert from a LISP to a FIB prefix
146  */
147 void
148 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
149                          fib_prefix_t * fib_prefix)
150 {
151   ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
152   fib_prefix->fp_len = ip_prefix->len;
153 }
154
155 /**
156  * Find the sw_if_index of the interface that would be used to egress towards
157  * dst.
158  */
159 u32
160 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
161 {
162   fib_node_index_t fei;
163   fib_prefix_t prefix;
164
165   ip_address_to_fib_prefix (dst, &prefix);
166
167   fei = fib_table_lookup (0, &prefix);
168
169   return (fib_entry_get_resolving_interface (fei));
170 }
171
172 /**
173  * Find first IP of the interface that would be used to egress towards dst.
174  * Returns 1 if the address is found 0 otherwise.
175  */
176 int
177 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
178                                     ip_address_t * result)
179 {
180   u32 si;
181   ip_lookup_main_t *lm;
182   void *addr = 0;
183   u8 ipver;
184
185   ASSERT (result != 0);
186
187   ipver = ip_addr_version (dst);
188
189   lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
190   si = ip_fib_get_egress_iface_for_dst (lcm, dst);
191
192   if ((u32) ~ 0 == si)
193     return 0;
194
195   /* find the first ip address */
196   addr = ip_interface_get_first_address (lm, si, ipver);
197   if (0 == addr)
198     return 0;
199
200   ip_address_set (result, addr, ipver);
201   return 1;
202 }
203
204 static int
205 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
206 {
207   uword *dp_table;
208
209   if (!is_l2)
210     {
211       dp_table = hash_get (lcm->table_id_by_vni, vni);
212
213       if (!dp_table)
214         {
215           clib_warning ("vni %d not associated to a vrf!", vni);
216           return VNET_API_ERROR_INVALID_VALUE;
217         }
218     }
219   else
220     {
221       dp_table = hash_get (lcm->bd_id_by_vni, vni);
222       if (!dp_table)
223         {
224           clib_warning ("vni %d not associated to a bridge domain!", vni);
225           return VNET_API_ERROR_INVALID_VALUE;
226         }
227     }
228
229   /* enable/disable data-plane interface */
230   if (is_add)
231     {
232       if (is_l2)
233         lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
234       else
235         lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
236     }
237   else
238     {
239       if (is_l2)
240         lisp_gpe_tenant_l2_iface_unlock (vni);
241       else
242         lisp_gpe_tenant_l3_iface_unlock (vni);
243     }
244
245   return 0;
246 }
247
248 static void
249 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
250 {
251   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
252   fwd_entry_t *fe = 0;
253   uword *feip = 0;
254   memset (a, 0, sizeof (*a));
255
256   feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
257   if (!feip)
258     return;
259
260   fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
261
262   /* delete dp fwd entry */
263   u32 sw_if_index;
264   a->is_add = 0;
265   a->locator_pairs = fe->locator_pairs;
266   a->vni = gid_address_vni (&fe->reid);
267   gid_address_copy (&a->rmt_eid, &fe->reid);
268   if (fe->is_src_dst)
269     gid_address_copy (&a->lcl_eid, &fe->leid);
270
271   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
272
273   /* delete entry in fwd table */
274   hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
275   vec_free (fe->locator_pairs);
276   pool_put (lcm->fwd_entry_pool, fe);
277 }
278
279 /**
280  * Finds first remote locator with best (lowest) priority that has a local
281  * peer locator with an underlying route to it.
282  *
283  */
284 static u32
285 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
286                    mapping_t * rmt_map, locator_pair_t ** locator_pairs)
287 {
288   u32 i, limitp = 0, li, found = 0, esi;
289   locator_set_t *rmt_ls, *lcl_ls;
290   ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
291   locator_t *lp, *rmt = 0;
292   uword *checked = 0;
293   locator_pair_t pair;
294
295   rmt_ls =
296     pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
297   lcl_ls =
298     pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
299
300   if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
301     return 0;
302
303   while (1)
304     {
305       rmt = 0;
306
307       /* find unvisited remote locator with best priority */
308       for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
309         {
310           if (0 != hash_get (checked, i))
311             continue;
312
313           li = vec_elt (rmt_ls->locator_indices, i);
314           lp = pool_elt_at_index (lcm->locator_pool, li);
315
316           /* we don't support non-IP locators for now */
317           if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
318             continue;
319
320           if ((found && lp->priority == limitp)
321               || (!found && lp->priority >= limitp))
322             {
323               rmt = lp;
324
325               /* don't search for locators with lower priority and don't
326                * check this locator again*/
327               limitp = lp->priority;
328               hash_set (checked, i, 1);
329               break;
330             }
331         }
332       /* check if a local locator with a route to remote locator exists */
333       if (rmt != 0)
334         {
335           /* find egress sw_if_index for rmt locator */
336           esi =
337             ip_fib_get_egress_iface_for_dst (lcm,
338                                              &gid_address_ip (&rmt->address));
339           if ((u32) ~ 0 == esi)
340             continue;
341
342           for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
343             {
344               li = vec_elt (lcl_ls->locator_indices, i);
345               locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
346
347               /* found local locator with the needed sw_if_index */
348               if (sl->sw_if_index == esi)
349                 {
350                   /* and it has an address */
351                   if (0 == ip_interface_get_first_ip_address (lcm,
352                                                               sl->sw_if_index,
353                                                               gid_address_ip_version
354                                                               (&rmt->address),
355                                                               lcl_addr))
356                     continue;
357
358                   memset (&pair, 0, sizeof (pair));
359                   ip_address_copy (&pair.rmt_loc,
360                                    &gid_address_ip (&rmt->address));
361                   ip_address_copy (&pair.lcl_loc, lcl_addr);
362                   pair.weight = rmt->weight;
363                   vec_add1 (locator_pairs[0], pair);
364                   found = 1;
365                 }
366             }
367         }
368       else
369         break;
370     }
371
372   hash_free (checked);
373   return found;
374 }
375
376 static void
377 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
378                         fid_address_t * fid)
379 {
380   ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
381
382   dst[0] = src[0];
383
384   switch (fid_addr_type (fid))
385     {
386     case FID_ADDR_IP_PREF:
387       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
388       gid_address_ippref (dst) = fid_addr_ippref (fid);
389       break;
390     case FID_ADDR_MAC:
391       gid_address_type (dst) = GID_ADDR_MAC;
392       mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
393       break;
394     default:
395       clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
396       break;
397     }
398 }
399
400 u8
401 vnet_lisp_map_register_state_get (void)
402 {
403   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
404   return lcm->map_registering;
405 }
406
407 u8
408 vnet_lisp_rloc_probe_state_get (void)
409 {
410   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
411   return lcm->rloc_probing;
412 }
413
414 static void
415 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
416 {
417   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
418   mapping_t *src_map, *dst_map;
419   u32 sw_if_index;
420   uword *feip = 0, *dpid;
421   fwd_entry_t *fe;
422   u8 type, is_src_dst = 0;
423
424   memset (a, 0, sizeof (*a));
425
426   /* remove entry if it already exists */
427   feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
428   if (feip)
429     dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
430
431   if (lcm->lisp_pitr)
432     src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
433   else
434     src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
435   dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
436
437   /* insert data plane forwarding entry */
438   a->is_add = 1;
439
440   if (MR_MODE_SRC_DST == lcm->map_request_mode)
441     {
442       if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
443         {
444           gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
445                                   &gid_address_sd_dst (&dst_map->eid));
446           gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
447                                   &gid_address_sd_src (&dst_map->eid));
448         }
449       else
450         {
451           gid_address_copy (&a->rmt_eid, &dst_map->eid);
452           gid_address_copy (&a->lcl_eid, &src_map->eid);
453         }
454       is_src_dst = 1;
455     }
456   else
457     gid_address_copy (&a->rmt_eid, &dst_map->eid);
458
459   a->vni = gid_address_vni (&a->rmt_eid);
460
461   /* get vrf or bd_index associated to vni */
462   type = gid_address_type (&a->rmt_eid);
463   if (GID_ADDR_IP_PREFIX == type)
464     {
465       dpid = hash_get (lcm->table_id_by_vni, a->vni);
466       if (!dpid)
467         {
468           clib_warning ("vni %d not associated to a vrf!", a->vni);
469           return;
470         }
471       a->table_id = dpid[0];
472     }
473   else if (GID_ADDR_MAC == type)
474     {
475       dpid = hash_get (lcm->bd_id_by_vni, a->vni);
476       if (!dpid)
477         {
478           clib_warning ("vni %d not associated to a bridge domain !", a->vni);
479           return;
480         }
481       a->bd_id = dpid[0];
482     }
483
484   /* find best locator pair that 1) verifies LISP policy 2) are connected */
485   if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
486     {
487       /* negative entry */
488       a->is_negative = 1;
489       a->action = dst_map->action;
490     }
491
492   /* TODO remove */
493   u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
494   a->decap_next_index = (ipver == IP4) ?
495     LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
496
497   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
498
499   /* add tunnel to fwd entry table XXX check return value from DP insertion */
500   pool_get (lcm->fwd_entry_pool, fe);
501   fe->locator_pairs = a->locator_pairs;
502   gid_address_copy (&fe->reid, &a->rmt_eid);
503   gid_address_copy (&fe->leid, &src_map->eid);
504   fe->is_src_dst = is_src_dst;
505   hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
506             fe - lcm->fwd_entry_pool);
507 }
508
509 /**
510  * Returns vector of adjacencies.
511  *
512  * The caller must free the vector returned by this function.
513  *
514  * @param vni virtual network identifier
515  * @return vector of adjacencies
516  */
517 lisp_adjacency_t *
518 vnet_lisp_adjacencies_get_by_vni (u32 vni)
519 {
520   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
521   fwd_entry_t *fwd;
522   lisp_adjacency_t *adjs = 0, adj;
523
524   /* *INDENT-OFF* */
525   pool_foreach(fwd, lcm->fwd_entry_pool,
526   ({
527     if (gid_address_vni (&fwd->reid) != vni)
528       continue;
529
530     gid_address_copy (&adj.reid, &fwd->reid);
531     gid_address_copy (&adj.leid, &fwd->leid);
532     vec_add1 (adjs, adj);
533   }));
534   /* *INDENT-ON* */
535
536   return adjs;
537 }
538
539 static clib_error_t *
540 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
541                                   unformat_input_t * input,
542                                   vlib_cli_command_t * cmd)
543 {
544   lisp_adjacency_t *adjs, *adj;
545   vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
546   unformat_input_t _line_input, *line_input = &_line_input;
547   u32 vni = ~0;
548
549   /* Get a line of input. */
550   if (!unformat_user (input, unformat_line_input, line_input))
551     return 0;
552
553   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
554     {
555       if (unformat (line_input, "vni %d", &vni))
556         ;
557       else
558         {
559           vlib_cli_output (vm, "parse error: '%U'",
560                            format_unformat_error, line_input);
561           return 0;
562         }
563     }
564
565   if (~0 == vni)
566     {
567       vlib_cli_output (vm, "error: no vni specified!");
568       return 0;
569     }
570
571   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
572
573   vec_foreach (adj, adjs)
574   {
575     vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
576                      format_gid_address, &adj->reid);
577   }
578   vec_free (adjs);
579
580   return 0;
581 }
582
583 /* *INDENT-OFF* */
584 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
585     .path = "show lisp adjacencies",
586     .short_help = "show lisp adjacencies",
587     .function = lisp_show_adjacencies_command_fn,
588 };
589 /* *INDENT-ON* */
590
591 static lisp_msmr_t *
592 get_map_server (ip_address_t * a)
593 {
594   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
595   lisp_msmr_t *m;
596
597   vec_foreach (m, lcm->map_servers)
598   {
599     if (!ip_address_cmp (&m->address, a))
600       {
601         return m;
602       }
603   }
604   return 0;
605 }
606
607 static lisp_msmr_t *
608 get_map_resolver (ip_address_t * a)
609 {
610   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
611   lisp_msmr_t *m;
612
613   vec_foreach (m, lcm->map_resolvers)
614   {
615     if (!ip_address_cmp (&m->address, a))
616       {
617         return m;
618       }
619   }
620   return 0;
621 }
622
623 int
624 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
625 {
626   u32 i;
627   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
628   lisp_msmr_t _ms, *ms = &_ms;
629
630   if (vnet_lisp_enable_disable_status () == 0)
631     {
632       clib_warning ("LISP is disabled!");
633       return VNET_API_ERROR_LISP_DISABLED;
634     }
635
636   if (is_add)
637     {
638       if (get_map_server (addr))
639         {
640           clib_warning ("map-server %U already exists!", format_ip_address,
641                         addr);
642           return -1;
643         }
644
645       memset (ms, 0, sizeof (*ms));
646       ip_address_copy (&ms->address, addr);
647       vec_add1 (lcm->map_servers, ms[0]);
648     }
649   else
650     {
651       for (i = 0; i < vec_len (lcm->map_servers); i++)
652         {
653           ms = vec_elt_at_index (lcm->map_servers, i);
654           if (!ip_address_cmp (&ms->address, addr))
655             {
656               vec_del1 (lcm->map_servers, i);
657               break;
658             }
659         }
660     }
661
662   return 0;
663 }
664
665 static clib_error_t *
666 lisp_add_del_map_server_command_fn (vlib_main_t * vm,
667                                     unformat_input_t * input,
668                                     vlib_cli_command_t * cmd)
669 {
670   int rv = 0;
671   u8 is_add = 1, ip_set = 0;
672   ip_address_t ip;
673   unformat_input_t _line_input, *line_input = &_line_input;
674
675   /* Get a line of input. */
676   if (!unformat_user (input, unformat_line_input, line_input))
677     return 0;
678
679   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
680     {
681       if (unformat (line_input, "add"))
682         is_add = 1;
683       else if (unformat (line_input, "del"))
684         is_add = 0;
685       else if (unformat (line_input, "%U", unformat_ip_address, &ip))
686         ip_set = 1;
687       else
688         {
689           vlib_cli_output (vm, "parse error: '%U'",
690                            format_unformat_error, line_input);
691           return 0;
692         }
693     }
694
695   if (!ip_set)
696     {
697       vlib_cli_output (vm, "map-server ip address not set!");
698       return 0;
699     }
700
701   rv = vnet_lisp_add_del_map_server (&ip, is_add);
702   if (!rv)
703     vlib_cli_output (vm, "failed to %s map-server!",
704                      is_add ? "add" : "delete");
705
706   return 0;
707 }
708
709 /* *INDENT-OFF* */
710 VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
711     .path = "lisp map-server",
712     .short_help = "lisp map-server add|del <ip>",
713     .function = lisp_add_del_map_server_command_fn,
714 };
715 /* *INDENT-ON* */
716
717 /**
718  * Add/remove mapping to/from map-cache. Overwriting not allowed.
719  */
720 int
721 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
722                              u32 * map_index_result)
723 {
724   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
725   u32 mi, *map_indexp, map_index, i;
726   mapping_t *m, *old_map;
727   u32 **eid_indexes;
728
729   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
730   old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
731   if (a->is_add)
732     {
733       /* TODO check if overwriting and take appropriate actions */
734       if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
735         {
736           clib_warning ("eid %U found in the eid-table", format_gid_address,
737                         &a->eid);
738           return VNET_API_ERROR_VALUE_EXIST;
739         }
740
741       pool_get (lcm->mapping_pool, m);
742       gid_address_copy (&m->eid, &a->eid);
743       m->locator_set_index = a->locator_set_index;
744       m->ttl = a->ttl;
745       m->action = a->action;
746       m->local = a->local;
747       m->is_static = a->is_static;
748       m->key = vec_dup (a->key);
749       m->key_id = a->key_id;
750
751       map_index = m - lcm->mapping_pool;
752       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
753                               1);
754
755       if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
756         {
757           clib_warning ("Locator set with index %d doesn't exist",
758                         a->locator_set_index);
759           return VNET_API_ERROR_INVALID_VALUE;
760         }
761
762       /* add eid to list of eids supported by locator-set */
763       vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
764       eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
765                                       a->locator_set_index);
766       vec_add1 (eid_indexes[0], map_index);
767
768       if (a->local)
769         {
770           /* mark as local */
771           vec_add1 (lcm->local_mappings_indexes, map_index);
772         }
773       map_index_result[0] = map_index;
774     }
775   else
776     {
777       if (mi == GID_LOOKUP_MISS)
778         {
779           clib_warning ("eid %U not found in the eid-table",
780                         format_gid_address, &a->eid);
781           return VNET_API_ERROR_INVALID_VALUE;
782         }
783
784       /* clear locator-set to eids binding */
785       eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
786                                       a->locator_set_index);
787       for (i = 0; i < vec_len (eid_indexes[0]); i++)
788         {
789           map_indexp = vec_elt_at_index (eid_indexes[0], i);
790           if (map_indexp[0] == mi)
791             break;
792         }
793       vec_del1 (eid_indexes[0], i);
794
795       /* remove local mark if needed */
796       m = pool_elt_at_index (lcm->mapping_pool, mi);
797       if (m->local)
798         {
799           u32 k, *lm_indexp;
800           for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
801             {
802               lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
803               if (lm_indexp[0] == mi)
804                 break;
805             }
806           vec_del1 (lcm->local_mappings_indexes, k);
807         }
808
809       /* remove mapping from dictionary */
810       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
811       gid_address_free (&m->eid);
812       pool_put_index (lcm->mapping_pool, mi);
813     }
814
815   return 0;
816 }
817
818 /**
819  *  Add/update/delete mapping to/in/from map-cache.
820  */
821 int
822 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
823                                  u32 * map_index_result)
824 {
825   uword *dp_table = 0;
826   u32 vni;
827   u8 type;
828
829   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
830
831   if (vnet_lisp_enable_disable_status () == 0)
832     {
833       clib_warning ("LISP is disabled!");
834       return VNET_API_ERROR_LISP_DISABLED;
835     }
836
837   vni = gid_address_vni (&a->eid);
838   type = gid_address_type (&a->eid);
839   if (GID_ADDR_IP_PREFIX == type)
840     dp_table = hash_get (lcm->table_id_by_vni, vni);
841   else if (GID_ADDR_MAC == type)
842     dp_table = hash_get (lcm->bd_id_by_vni, vni);
843
844   if (!dp_table)
845     {
846       clib_warning ("vni %d not associated to a %s!", vni,
847                     GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
848       return VNET_API_ERROR_INVALID_VALUE;
849     }
850
851   /* store/remove mapping from map-cache */
852   return vnet_lisp_map_cache_add_del (a, map_index_result);
853 }
854
855 static clib_error_t *
856 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
857                                    vlib_cli_command_t * cmd)
858 {
859   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
860   unformat_input_t _line_input, *line_input = &_line_input;
861   u8 is_add = 1;
862   gid_address_t eid;
863   gid_address_t *eids = 0;
864   clib_error_t *error = 0;
865   u8 *locator_set_name = 0;
866   u32 locator_set_index = 0, map_index = 0;
867   uword *p;
868   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
869   int rv = 0;
870   u32 vni = 0;
871   u8 *key = 0;
872   u32 key_id = 0;
873
874   memset (&eid, 0, sizeof (eid));
875   memset (a, 0, sizeof (*a));
876
877   /* Get a line of input. */
878   if (!unformat_user (input, unformat_line_input, line_input))
879     return 0;
880
881   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
882     {
883       if (unformat (line_input, "add"))
884         is_add = 1;
885       else if (unformat (line_input, "del"))
886         is_add = 0;
887       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
888         ;
889       else if (unformat (line_input, "vni %d", &vni))
890         gid_address_vni (&eid) = vni;
891       else if (unformat (line_input, "secret-key %_%v%_", &key))
892         ;
893       else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
894                          &key_id))
895         ;
896       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
897         {
898           p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
899           if (!p)
900             {
901               error = clib_error_return (0, "locator-set %s doesn't exist",
902                                          locator_set_name);
903               goto done;
904             }
905           locator_set_index = p[0];
906         }
907       else
908         {
909           error = unformat_parse_error (line_input);
910           goto done;
911         }
912     }
913   /* XXX treat batch configuration */
914
915   if (GID_ADDR_SRC_DST == gid_address_type (&eid))
916     {
917       error =
918         clib_error_return (0, "src/dst is not supported for local EIDs!");
919       goto done;
920     }
921
922   if (key && (0 == key_id))
923     {
924       vlib_cli_output (vm, "invalid key_id!");
925       return 0;
926     }
927
928   gid_address_copy (&a->eid, &eid);
929   a->is_add = is_add;
930   a->locator_set_index = locator_set_index;
931   a->local = 1;
932   a->key = key;
933   a->key_id = key_id;
934
935   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
936   if (0 != rv)
937     {
938       error = clib_error_return (0, "failed to %s local mapping!",
939                                  is_add ? "add" : "delete");
940     }
941 done:
942   vec_free (eids);
943   if (locator_set_name)
944     vec_free (locator_set_name);
945   gid_address_free (&a->eid);
946   vec_free (a->key);
947   return error;
948 }
949
950 /* *INDENT-OFF* */
951 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
952     .path = "lisp eid-table",
953     .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
954       "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
955     .function = lisp_add_del_local_eid_command_fn,
956 };
957 /* *INDENT-ON* */
958
959 int
960 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
961 {
962   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
963   uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
964
965   if (vnet_lisp_enable_disable_status () == 0)
966     {
967       clib_warning ("LISP is disabled!");
968       return -1;
969     }
970
971   dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
972   vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
973
974   if (!is_l2 && (vni == 0 || dp_id == 0))
975     {
976       clib_warning ("can't add/del default vni-vrf mapping!");
977       return -1;
978     }
979
980   dp_idp = hash_get (dp_table_by_vni[0], vni);
981   vnip = hash_get (vni_by_dp_table[0], dp_id);
982
983   if (is_add)
984     {
985       if (dp_idp || vnip)
986         {
987           clib_warning ("vni %d or vrf %d already used in vrf/vni "
988                         "mapping!", vni, dp_id);
989           return -1;
990         }
991       hash_set (dp_table_by_vni[0], vni, dp_id);
992       hash_set (vni_by_dp_table[0], dp_id, vni);
993
994       /* create dp iface */
995       dp_add_del_iface (lcm, vni, is_l2, 1);
996     }
997   else
998     {
999       if (!dp_idp || !vnip)
1000         {
1001           clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1002                         "mapping!", vni, dp_id);
1003           return -1;
1004         }
1005       hash_unset (dp_table_by_vni[0], vni);
1006       hash_unset (vni_by_dp_table[0], dp_id);
1007
1008       /* remove dp iface */
1009       dp_add_del_iface (lcm, vni, is_l2, 0);
1010     }
1011   return 0;
1012
1013 }
1014
1015 static clib_error_t *
1016 lisp_eid_table_map_command_fn (vlib_main_t * vm,
1017                                unformat_input_t * input,
1018                                vlib_cli_command_t * cmd)
1019 {
1020   u8 is_add = 1, is_l2 = 0;
1021   u32 vni = 0, dp_id = 0;
1022   unformat_input_t _line_input, *line_input = &_line_input;
1023
1024   /* Get a line of input. */
1025   if (!unformat_user (input, unformat_line_input, line_input))
1026     return 0;
1027
1028   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1029     {
1030       if (unformat (line_input, "del"))
1031         is_add = 0;
1032       else if (unformat (line_input, "vni %d", &vni))
1033         ;
1034       else if (unformat (line_input, "vrf %d", &dp_id))
1035         ;
1036       else if (unformat (line_input, "bd %d", &dp_id))
1037         is_l2 = 1;
1038       else
1039         {
1040           return unformat_parse_error (line_input);
1041         }
1042     }
1043   vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
1044   return 0;
1045 }
1046
1047 /* *INDENT-OFF* */
1048 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1049     .path = "lisp eid-table map",
1050     .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
1051     .function = lisp_eid_table_map_command_fn,
1052 };
1053 /* *INDENT-ON* */
1054
1055 /* return 0 if the two locator sets are identical 1 otherwise */
1056 static u8
1057 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1058                   locator_t * new_locators)
1059 {
1060   u32 i, old_li;
1061   locator_t *old_loc, *new_loc;
1062
1063   if (vec_len (old_ls_indexes) != vec_len (new_locators))
1064     return 1;
1065
1066   for (i = 0; i < vec_len (new_locators); i++)
1067     {
1068       old_li = vec_elt (old_ls_indexes, i);
1069       old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1070
1071       new_loc = vec_elt_at_index (new_locators, i);
1072
1073       if (locator_cmp (old_loc, new_loc))
1074         return 1;
1075     }
1076   return 0;
1077 }
1078
1079 typedef struct
1080 {
1081   u8 is_negative;
1082   void *lcm;
1083   gid_address_t *eids_to_be_deleted;
1084 } remove_mapping_args_t;
1085
1086 /**
1087  * Callback invoked when a sub-prefix is found
1088  */
1089 static void
1090 remove_mapping_if_needed (u32 mi, void *arg)
1091 {
1092   u8 delete = 0;
1093   remove_mapping_args_t *a = arg;
1094   lisp_cp_main_t *lcm = a->lcm;
1095   mapping_t *m;
1096   locator_set_t *ls;
1097
1098   m = pool_elt_at_index (lcm->mapping_pool, mi);
1099   if (!m)
1100     return;
1101
1102   ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1103
1104   if (a->is_negative)
1105     {
1106       if (0 != vec_len (ls->locator_indices))
1107         delete = 1;
1108     }
1109   else
1110     {
1111       if (0 == vec_len (ls->locator_indices))
1112         delete = 1;
1113     }
1114
1115   if (delete)
1116     vec_add1 (a->eids_to_be_deleted, m->eid);
1117 }
1118
1119 /**
1120  * This function searches map cache and looks for IP prefixes that are subset
1121  * of the provided one. If such prefix is found depending on 'is_negative'
1122  * it does follows:
1123  *
1124  * 1) if is_negative is true and found prefix points to positive mapping,
1125  *    then the mapping is removed
1126  * 2) if is_negative is false and found prefix points to negative mapping,
1127  *    then the mapping is removed
1128  */
1129 static void
1130 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1131                                  u8 is_negative)
1132 {
1133   gid_address_t *e;
1134   remove_mapping_args_t a;
1135   memset (&a, 0, sizeof (a));
1136
1137   /* do this only in src/dst mode ... */
1138   if (MR_MODE_SRC_DST != lcm->map_request_mode)
1139     return;
1140
1141   /* ... and  only for IP prefix */
1142   if (GID_ADDR_SRC_DST != gid_address_type (eid)
1143       || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1144     return;
1145
1146   a.is_negative = is_negative;
1147   a.lcm = lcm;
1148
1149   gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1150                               remove_mapping_if_needed, &a);
1151
1152   vec_foreach (e, a.eids_to_be_deleted)
1153     vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1154
1155   vec_free (a.eids_to_be_deleted);
1156 }
1157
1158 static void
1159 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1160 {
1161   timing_wheel_delete (&lcm->wheel, mi);
1162 }
1163
1164 /**
1165  * Adds/removes/updates mapping. Does not program forwarding.
1166  *
1167  * @param eid end-host identifier
1168  * @param rlocs vector of remote locators
1169  * @param action action for negative map-reply
1170  * @param is_add add mapping if non-zero, delete otherwise
1171  * @param res_map_index the map-index that was created/updated/removed. It is
1172  *                      set to ~0 if no action is taken.
1173  * @param is_static used for distinguishing between statically learned
1174                     remote mappings and mappings obtained from MR
1175  * @return return code
1176  */
1177 int
1178 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1179                            u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1180                            u32 * res_map_index)
1181 {
1182   vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1183   vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1184   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1185   u32 mi, ls_index = 0, dst_map_index;
1186   mapping_t *old_map;
1187
1188   if (vnet_lisp_enable_disable_status () == 0)
1189     {
1190       clib_warning ("LISP is disabled!");
1191       return VNET_API_ERROR_LISP_DISABLED;
1192     }
1193
1194   if (res_map_index)
1195     res_map_index[0] = ~0;
1196
1197   memset (m_args, 0, sizeof (m_args[0]));
1198   memset (ls_args, 0, sizeof (ls_args[0]));
1199
1200   ls_args->locators = rlocs;
1201
1202   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1203   old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1204
1205   if (is_add)
1206     {
1207       /* overwrite: if mapping already exists, decide if locators should be
1208        * updated and be done */
1209       if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1210         {
1211           if (!is_static && (old_map->is_static || old_map->local))
1212             {
1213               /* do not overwrite local or static remote mappings */
1214               clib_warning ("mapping %U rejected due to collision with local "
1215                             "or static remote mapping!", format_gid_address,
1216                             eid);
1217               return 0;
1218             }
1219
1220           locator_set_t *old_ls;
1221
1222           /* update mapping attributes */
1223           old_map->action = action;
1224           old_map->authoritative = authoritative;
1225           old_map->ttl = ttl;
1226
1227           old_ls = pool_elt_at_index (lcm->locator_set_pool,
1228                                       old_map->locator_set_index);
1229           if (compare_locators (lcm, old_ls->locator_indices,
1230                                 ls_args->locators))
1231             {
1232               /* set locator-set index to overwrite */
1233               ls_args->is_add = 1;
1234               ls_args->index = old_map->locator_set_index;
1235               vnet_lisp_add_del_locator_set (ls_args, 0);
1236               if (res_map_index)
1237                 res_map_index[0] = mi;
1238             }
1239         }
1240       /* new mapping */
1241       else
1242         {
1243           remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1244
1245           ls_args->is_add = 1;
1246           ls_args->index = ~0;
1247
1248           vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1249
1250           /* add mapping */
1251           gid_address_copy (&m_args->eid, eid);
1252           m_args->is_add = 1;
1253           m_args->action = action;
1254           m_args->locator_set_index = ls_index;
1255           m_args->is_static = is_static;
1256           m_args->ttl = ttl;
1257           vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1258
1259           if (res_map_index)
1260             res_map_index[0] = dst_map_index;
1261         }
1262     }
1263   else
1264     {
1265       if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1266         {
1267           clib_warning ("cannot delete mapping for eid %U",
1268                         format_gid_address, eid);
1269           return -1;
1270         }
1271
1272       m_args->is_add = 0;
1273       gid_address_copy (&m_args->eid, eid);
1274       m_args->locator_set_index = old_map->locator_set_index;
1275
1276       /* delete mapping associated from map-cache */
1277       vnet_lisp_map_cache_add_del (m_args, 0);
1278
1279       ls_args->is_add = 0;
1280       ls_args->index = old_map->locator_set_index;
1281       /* delete locator set */
1282       vnet_lisp_add_del_locator_set (ls_args, 0);
1283
1284       /* delete timer associated to the mapping if any */
1285       if (old_map->timer_set)
1286         mapping_delete_timer (lcm, mi);
1287
1288       /* return old mapping index */
1289       if (res_map_index)
1290         res_map_index[0] = mi;
1291     }
1292
1293   /* success */
1294   return 0;
1295 }
1296
1297 int
1298 vnet_lisp_clear_all_remote_adjacencies (void)
1299 {
1300   int rv = 0;
1301   u32 mi, *map_indices = 0, *map_indexp;
1302   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1303   vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1304   vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1305
1306   /* *INDENT-OFF* */
1307   pool_foreach_index (mi, lcm->mapping_pool,
1308   ({
1309     vec_add1 (map_indices, mi);
1310   }));
1311   /* *INDENT-ON* */
1312
1313   vec_foreach (map_indexp, map_indices)
1314   {
1315     mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1316     if (!map->local)
1317       {
1318         dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1319
1320         dm_args->is_add = 0;
1321         gid_address_copy (&dm_args->eid, &map->eid);
1322         dm_args->locator_set_index = map->locator_set_index;
1323
1324         /* delete mapping associated to fwd entry */
1325         vnet_lisp_map_cache_add_del (dm_args, 0);
1326
1327         ls->is_add = 0;
1328         ls->local = 0;
1329         ls->index = map->locator_set_index;
1330         /* delete locator set */
1331         rv = vnet_lisp_add_del_locator_set (ls, 0);
1332         if (rv != 0)
1333           goto cleanup;
1334       }
1335   }
1336
1337 cleanup:
1338   if (map_indices)
1339     vec_free (map_indices);
1340   return rv;
1341 }
1342
1343 /**
1344  * Adds adjacency or removes forwarding entry associated to remote mapping.
1345  * Note that adjacencies are not stored, they only result in forwarding entries
1346  * being created.
1347  */
1348 int
1349 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1350                         gid_address_t * remote_eid, u8 is_add)
1351 {
1352   u32 local_mi, remote_mi = ~0;
1353
1354   if (vnet_lisp_enable_disable_status () == 0)
1355     {
1356       clib_warning ("LISP is disabled!");
1357       return VNET_API_ERROR_LISP_DISABLED;
1358     }
1359
1360   remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1361                                         remote_eid, local_eid);
1362   if (GID_LOOKUP_MISS == remote_mi)
1363     {
1364       clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1365                     format_gid_address, remote_eid);
1366
1367       return -1;
1368     }
1369
1370   if (is_add)
1371     {
1372       /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1373        * delete*/
1374
1375       /* check if source eid has an associated mapping. If pitr mode is on,
1376        * just use the pitr's mapping */
1377       local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1378         gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1379
1380
1381       if (GID_LOOKUP_MISS == local_mi)
1382         {
1383           clib_warning ("Local eid %U not found. Cannot add adjacency!",
1384                         format_gid_address, local_eid);
1385
1386           return -1;
1387         }
1388
1389       /* update forwarding */
1390       dp_add_fwd_entry (lcm, local_mi, remote_mi);
1391     }
1392   else
1393     dp_del_fwd_entry (lcm, 0, remote_mi);
1394
1395   return 0;
1396 }
1397
1398 int
1399 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1400 {
1401   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1402   return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1403 }
1404
1405 /**
1406  * Handler for add/del remote mapping CLI.
1407  *
1408  * @param vm vlib context
1409  * @param input input from user
1410  * @param cmd cmd
1411  * @return pointer to clib error structure
1412  */
1413 static clib_error_t *
1414 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1415                                         unformat_input_t * input,
1416                                         vlib_cli_command_t * cmd)
1417 {
1418   clib_error_t *error = 0;
1419   unformat_input_t _line_input, *line_input = &_line_input;
1420   u8 is_add = 1, del_all = 0;
1421   locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1422   gid_address_t eid;
1423   u8 eid_set = 0;
1424   u32 vni, action = ~0, p, w;
1425   int rv;
1426
1427   /* Get a line of input. */
1428   if (!unformat_user (input, unformat_line_input, line_input))
1429     return 0;
1430
1431   memset (&eid, 0, sizeof (eid));
1432   memset (&rloc, 0, sizeof (rloc));
1433
1434   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1435     {
1436       if (unformat (line_input, "del-all"))
1437         del_all = 1;
1438       else if (unformat (line_input, "del"))
1439         is_add = 0;
1440       else if (unformat (line_input, "add"))
1441         ;
1442       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1443         eid_set = 1;
1444       else if (unformat (line_input, "vni %u", &vni))
1445         {
1446           gid_address_vni (&eid) = vni;
1447         }
1448       else if (unformat (line_input, "p %d w %d", &p, &w))
1449         {
1450           if (!curr_rloc)
1451             {
1452               clib_warning
1453                 ("No RLOC configured for setting priority/weight!");
1454               goto done;
1455             }
1456           curr_rloc->priority = p;
1457           curr_rloc->weight = w;
1458         }
1459       else if (unformat (line_input, "rloc %U", unformat_ip_address,
1460                          &gid_address_ip (&rloc.address)))
1461         {
1462           /* since rloc is stored in ip prefix we need to set prefix length */
1463           ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1464
1465           u8 version = gid_address_ip_version (&rloc.address);
1466           ip_prefix_len (pref) = ip_address_max_len (version);
1467
1468           vec_add1 (rlocs, rloc);
1469           curr_rloc = &rlocs[vec_len (rlocs) - 1];
1470         }
1471       else if (unformat (line_input, "action %U",
1472                          unformat_negative_mapping_action, &action))
1473         ;
1474       else
1475         {
1476           clib_warning ("parse error");
1477           goto done;
1478         }
1479     }
1480
1481   if (!eid_set)
1482     {
1483       clib_warning ("missing eid!");
1484       goto done;
1485     }
1486
1487   if (!del_all)
1488     {
1489       if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1490         {
1491           clib_warning ("no action set for negative map-reply!");
1492           goto done;
1493         }
1494     }
1495   else
1496     {
1497       vnet_lisp_clear_all_remote_adjacencies ();
1498       goto done;
1499     }
1500
1501   /* TODO build src/dst with seid */
1502
1503   /* if it's a delete, clean forwarding */
1504   if (!is_add)
1505     {
1506       lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1507       rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1508       if (rv)
1509         {
1510           goto done;
1511         }
1512     }
1513
1514   /* add as static remote mapping, i.e., not authoritative and infinite
1515    * ttl */
1516   rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1517                                   1 /* is_static */ , 0);
1518
1519   if (rv)
1520     clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1521
1522 done:
1523   vec_free (rlocs);
1524   unformat_free (line_input);
1525   return error;
1526 }
1527
1528 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1529 {
1530 .path = "lisp remote-mapping",.short_help =
1531     "lisp remote-mapping add|del [del-all] vni <vni> "
1532     "eid <est-eid> [action <no-action|natively-forward|"
1533     "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1534     "[rloc <dst-locator> ... ]",.function =
1535     lisp_add_del_remote_mapping_command_fn,};
1536
1537 /**
1538  * Handler for add/del adjacency CLI.
1539  */
1540 static clib_error_t *
1541 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1542                                    vlib_cli_command_t * cmd)
1543 {
1544   clib_error_t *error = 0;
1545   unformat_input_t _line_input, *line_input = &_line_input;
1546   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1547   u8 is_add = 1;
1548   ip_prefix_t *reid_ippref, *leid_ippref;
1549   gid_address_t leid, reid;
1550   u8 *dmac = gid_address_mac (&reid);
1551   u8 *smac = gid_address_mac (&leid);
1552   u8 reid_set = 0, leid_set = 0;
1553   u32 vni;
1554   int rv;
1555
1556   /* Get a line of input. */
1557   if (!unformat_user (input, unformat_line_input, line_input))
1558     return 0;
1559
1560   memset (&reid, 0, sizeof (reid));
1561   memset (&leid, 0, sizeof (leid));
1562
1563   leid_ippref = &gid_address_ippref (&leid);
1564   reid_ippref = &gid_address_ippref (&reid);
1565
1566   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1567     {
1568       if (unformat (line_input, "del"))
1569         is_add = 0;
1570       else if (unformat (line_input, "add"))
1571         ;
1572       else if (unformat (line_input, "reid %U",
1573                          unformat_ip_prefix, reid_ippref))
1574         {
1575           gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1576           reid_set = 1;
1577         }
1578       else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1579         {
1580           gid_address_type (&reid) = GID_ADDR_MAC;
1581           reid_set = 1;
1582         }
1583       else if (unformat (line_input, "vni %u", &vni))
1584         {
1585           gid_address_vni (&leid) = vni;
1586           gid_address_vni (&reid) = vni;
1587         }
1588       else if (unformat (line_input, "leid %U",
1589                          unformat_ip_prefix, leid_ippref))
1590         {
1591           gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1592           leid_set = 1;
1593         }
1594       else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1595         {
1596           gid_address_type (&leid) = GID_ADDR_MAC;
1597           leid_set = 1;
1598         }
1599       else
1600         {
1601           clib_warning ("parse error");
1602           goto done;
1603         }
1604     }
1605
1606   if (!reid_set || !leid_set)
1607     {
1608       clib_warning ("missing remote or local eid!");
1609       goto done;
1610     }
1611
1612   if ((gid_address_type (&leid) != gid_address_type (&reid))
1613       || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1614           && ip_prefix_version (reid_ippref)
1615           != ip_prefix_version (leid_ippref)))
1616     {
1617       clib_warning ("remote and local EIDs are of different types!");
1618       return error;
1619     }
1620
1621   memset (a, 0, sizeof (a[0]));
1622   gid_address_copy (&a->leid, &leid);
1623   gid_address_copy (&a->reid, &reid);
1624
1625   a->is_add = is_add;
1626   rv = vnet_lisp_add_del_adjacency (a);
1627
1628   if (rv)
1629     clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1630
1631 done:
1632   unformat_free (line_input);
1633   return error;
1634 }
1635
1636 /* *INDENT-OFF* */
1637 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1638     .path = "lisp adjacency",
1639     .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1640       "leid <local-eid>",
1641     .function = lisp_add_del_adjacency_command_fn,
1642 };
1643 /* *INDENT-ON* */
1644
1645 int
1646 vnet_lisp_set_map_request_mode (u8 mode)
1647 {
1648   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1649
1650   if (vnet_lisp_enable_disable_status () == 0)
1651     {
1652       clib_warning ("LISP is disabled!");
1653       return VNET_API_ERROR_LISP_DISABLED;
1654     }
1655
1656   if (mode >= _MR_MODE_MAX)
1657     {
1658       clib_warning ("Invalid LISP map request mode %d!", mode);
1659       return VNET_API_ERROR_INVALID_ARGUMENT;
1660     }
1661
1662   lcm->map_request_mode = mode;
1663   return 0;
1664 }
1665
1666 static clib_error_t *
1667 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1668                                   unformat_input_t * input,
1669                                   vlib_cli_command_t * cmd)
1670 {
1671   unformat_input_t _i, *i = &_i;
1672   map_request_mode_t mr_mode = _MR_MODE_MAX;
1673
1674   /* Get a line of input. */
1675   if (!unformat_user (input, unformat_line_input, i))
1676     return 0;
1677
1678   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1679     {
1680       if (unformat (i, "dst-only"))
1681         mr_mode = MR_MODE_DST_ONLY;
1682       else if (unformat (i, "src-dst"))
1683         mr_mode = MR_MODE_SRC_DST;
1684       else
1685         {
1686           clib_warning ("parse error '%U'", format_unformat_error, i);
1687           goto done;
1688         }
1689     }
1690
1691   if (_MR_MODE_MAX == mr_mode)
1692     {
1693       clib_warning ("No LISP map request mode entered!");
1694       return 0;
1695     }
1696
1697   vnet_lisp_set_map_request_mode (mr_mode);
1698 done:
1699   return 0;
1700 }
1701
1702 /* *INDENT-OFF* */
1703 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1704     .path = "lisp map-request mode",
1705     .short_help = "lisp map-request mode dst-only|src-dst",
1706     .function = lisp_map_request_mode_command_fn,
1707 };
1708 /* *INDENT-ON* */
1709
1710 static u8 *
1711 format_lisp_map_request_mode (u8 * s, va_list * args)
1712 {
1713   u32 mode = va_arg (*args, u32);
1714
1715   switch (mode)
1716     {
1717     case 0:
1718       return format (0, "dst-only");
1719     case 1:
1720       return format (0, "src-dst");
1721     }
1722   return 0;
1723 }
1724
1725 static clib_error_t *
1726 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1727                                        unformat_input_t * input,
1728                                        vlib_cli_command_t * cmd)
1729 {
1730   vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1731                    vnet_lisp_get_map_request_mode ());
1732   return 0;
1733 }
1734
1735 /* *INDENT-OFF* */
1736 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1737     .path = "show lisp map-request mode",
1738     .short_help = "show lisp map-request mode",
1739     .function = lisp_show_map_request_mode_command_fn,
1740 };
1741 /* *INDENT-ON* */
1742
1743 static clib_error_t *
1744 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1745                                     unformat_input_t * input,
1746                                     vlib_cli_command_t * cmd)
1747 {
1748   lisp_msmr_t *mr;
1749   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1750
1751   vec_foreach (mr, lcm->map_resolvers)
1752   {
1753     vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1754   }
1755   return 0;
1756 }
1757
1758 /* *INDENT-OFF* */
1759 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1760     .path = "show lisp map-resolvers",
1761     .short_help = "show lisp map-resolvers",
1762     .function = lisp_show_map_resolvers_command_fn,
1763 };
1764 /* *INDENT-ON* */
1765
1766 int
1767 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1768 {
1769   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1770   u32 locator_set_index = ~0;
1771   mapping_t *m;
1772   uword *p;
1773
1774   if (vnet_lisp_enable_disable_status () == 0)
1775     {
1776       clib_warning ("LISP is disabled!");
1777       return VNET_API_ERROR_LISP_DISABLED;
1778     }
1779
1780   p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1781   if (!p)
1782     {
1783       clib_warning ("locator-set %v doesn't exist", locator_set_name);
1784       return -1;
1785     }
1786   locator_set_index = p[0];
1787
1788   if (is_add)
1789     {
1790       pool_get (lcm->mapping_pool, m);
1791       m->locator_set_index = locator_set_index;
1792       m->local = 1;
1793       lcm->pitr_map_index = m - lcm->mapping_pool;
1794
1795       /* enable pitr mode */
1796       lcm->lisp_pitr = 1;
1797     }
1798   else
1799     {
1800       /* remove pitr mapping */
1801       pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1802
1803       /* disable pitr mode */
1804       lcm->lisp_pitr = 0;
1805     }
1806   return 0;
1807 }
1808
1809 static clib_error_t *
1810 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1811                                       unformat_input_t * input,
1812                                       vlib_cli_command_t * cmd)
1813 {
1814   u8 locator_name_set = 0;
1815   u8 *locator_set_name = 0;
1816   u8 is_add = 1;
1817   unformat_input_t _line_input, *line_input = &_line_input;
1818   clib_error_t *error = 0;
1819   int rv = 0;
1820
1821   /* Get a line of input. */
1822   if (!unformat_user (input, unformat_line_input, line_input))
1823     return 0;
1824
1825   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1826     {
1827       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1828         locator_name_set = 1;
1829       else if (unformat (line_input, "disable"))
1830         is_add = 0;
1831       else
1832         return clib_error_return (0, "parse error");
1833     }
1834
1835   if (!locator_name_set)
1836     {
1837       clib_warning ("No locator set specified!");
1838       goto done;
1839     }
1840   rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1841   if (0 != rv)
1842     {
1843       error = clib_error_return (0, "failed to %s pitr!",
1844                                  is_add ? "add" : "delete");
1845     }
1846
1847 done:
1848   if (locator_set_name)
1849     vec_free (locator_set_name);
1850   return error;
1851 }
1852
1853 /* *INDENT-OFF* */
1854 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1855     .path = "lisp pitr",
1856     .short_help = "lisp pitr [disable] ls <locator-set-name>",
1857     .function = lisp_pitr_set_locator_set_command_fn,
1858 };
1859 /* *INDENT-ON* */
1860
1861 static clib_error_t *
1862 lisp_show_pitr_command_fn (vlib_main_t * vm,
1863                            unformat_input_t * input, vlib_cli_command_t * cmd)
1864 {
1865   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1866   mapping_t *m;
1867   locator_set_t *ls;
1868   u8 *tmp_str = 0;
1869
1870   vlib_cli_output (vm, "%=20s%=16s",
1871                    "pitr", lcm->lisp_pitr ? "locator-set" : "");
1872
1873   if (!lcm->lisp_pitr)
1874     {
1875       vlib_cli_output (vm, "%=20s", "disable");
1876       return 0;
1877     }
1878
1879   if (~0 == lcm->pitr_map_index)
1880     {
1881       tmp_str = format (0, "N/A");
1882     }
1883   else
1884     {
1885       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1886       if (~0 != m->locator_set_index)
1887         {
1888           ls =
1889             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1890           tmp_str = format (0, "%s", ls->name);
1891         }
1892       else
1893         {
1894           tmp_str = format (0, "N/A");
1895         }
1896     }
1897   vec_add1 (tmp_str, 0);
1898
1899   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1900
1901   vec_free (tmp_str);
1902
1903   return 0;
1904 }
1905
1906 /* *INDENT-OFF* */
1907 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1908     .path = "show lisp pitr",
1909     .short_help = "Show pitr",
1910     .function = lisp_show_pitr_command_fn,
1911 };
1912 /* *INDENT-ON* */
1913
1914 static u8 *
1915 format_eid_entry (u8 * s, va_list * args)
1916 {
1917   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1918   lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1919   mapping_t *mapit = va_arg (*args, mapping_t *);
1920   locator_set_t *ls = va_arg (*args, locator_set_t *);
1921   gid_address_t *gid = &mapit->eid;
1922   u32 ttl = mapit->ttl;
1923   u8 aut = mapit->authoritative;
1924   u32 *loc_index;
1925   u8 first_line = 1;
1926   u8 *loc;
1927
1928   u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1929     : format (0, "remote");
1930
1931   if (vec_len (ls->locator_indices) == 0)
1932     {
1933       s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1934                   type, ttl, aut);
1935     }
1936   else
1937     {
1938       vec_foreach (loc_index, ls->locator_indices)
1939       {
1940         locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1941         if (l->local)
1942           loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1943                         l->sw_if_index);
1944         else
1945           loc = format (0, "%U", format_ip_address,
1946                         &gid_address_ip (&l->address));
1947
1948         if (first_line)
1949           {
1950             s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1951                         gid, type, loc, ttl, aut);
1952             first_line = 0;
1953           }
1954         else
1955           s = format (s, "%55s%v\n", "", loc);
1956       }
1957     }
1958   return s;
1959 }
1960
1961 static clib_error_t *
1962 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1963                                 unformat_input_t * input,
1964                                 vlib_cli_command_t * cmd)
1965 {
1966   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1967   mapping_t *mapit;
1968   unformat_input_t _line_input, *line_input = &_line_input;
1969   u32 mi;
1970   gid_address_t eid;
1971   u8 print_all = 1;
1972   u8 filter = 0;
1973
1974   memset (&eid, 0, sizeof (eid));
1975
1976   /* Get a line of input. */
1977   if (!unformat_user (input, unformat_line_input, line_input))
1978     return 0;
1979
1980   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1981     {
1982       if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1983         print_all = 0;
1984       else if (unformat (line_input, "local"))
1985         filter = 1;
1986       else if (unformat (line_input, "remote"))
1987         filter = 2;
1988       else
1989         return clib_error_return (0, "parse error: '%U'",
1990                                   format_unformat_error, line_input);
1991     }
1992
1993   vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1994                    "EID", "type", "locators", "ttl", "autoritative");
1995
1996   if (print_all)
1997     {
1998       /* *INDENT-OFF* */
1999       pool_foreach (mapit, lcm->mapping_pool,
2000       ({
2001         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2002                                                 mapit->locator_set_index);
2003         if (filter && !((1 == filter && ls->local) ||
2004           (2 == filter && !ls->local)))
2005           {
2006             continue;
2007           }
2008         vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
2009                          lcm, mapit, ls);
2010       }));
2011       /* *INDENT-ON* */
2012     }
2013   else
2014     {
2015       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
2016       if ((u32) ~ 0 == mi)
2017         return 0;
2018
2019       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
2020       locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2021                                              mapit->locator_set_index);
2022
2023       if (filter && !((1 == filter && ls->local) ||
2024                       (2 == filter && !ls->local)))
2025         {
2026           return 0;
2027         }
2028
2029       vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
2030                        lcm, mapit, ls);
2031     }
2032
2033   return 0;
2034 }
2035
2036 /* *INDENT-OFF* */
2037 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
2038     .path = "show lisp eid-table",
2039     .short_help = "Shows EID table",
2040     .function = lisp_show_eid_table_command_fn,
2041 };
2042 /* *INDENT-ON* */
2043
2044 /* cleans locator to locator-set data and removes locators not part of
2045  * any locator-set */
2046 static void
2047 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2048 {
2049   u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
2050   locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2051   for (i = 0; i < vec_len (ls->locator_indices); i++)
2052     {
2053       loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2054       ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2055                                      loc_indexp[0]);
2056       for (j = 0; j < vec_len (ls_indexes[0]); j++)
2057         {
2058           ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2059           if (ls_indexp[0] == lsi)
2060             break;
2061         }
2062
2063       /* delete index for removed locator-set */
2064       vec_del1 (ls_indexes[0], j);
2065
2066       /* delete locator if it's part of no locator-set */
2067       if (vec_len (ls_indexes[0]) == 0)
2068         {
2069           pool_put_index (lcm->locator_pool, loc_indexp[0]);
2070           vec_add1 (to_be_deleted, i);
2071         }
2072     }
2073
2074   if (to_be_deleted)
2075     {
2076       for (i = 0; i < vec_len (to_be_deleted); i++)
2077         {
2078           loc_indexp = vec_elt_at_index (to_be_deleted, i);
2079           vec_del1 (ls->locator_indices, loc_indexp[0]);
2080         }
2081       vec_free (to_be_deleted);
2082     }
2083 }
2084
2085 static inline uword *
2086 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
2087 {
2088   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2089
2090   ASSERT (a != NULL);
2091   ASSERT (p != NULL);
2092
2093   /* find locator-set */
2094   if (a->local)
2095     {
2096       p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
2097     }
2098   else
2099     {
2100       *p = a->index;
2101     }
2102
2103   return p;
2104 }
2105
2106 static inline int
2107 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
2108                            locator_t * loc)
2109 {
2110   locator_t *itloc;
2111   u32 *locit;
2112
2113   ASSERT (ls != NULL);
2114   ASSERT (loc != NULL);
2115
2116   vec_foreach (locit, ls->locator_indices)
2117   {
2118     itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2119     if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2120         (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2121       {
2122         clib_warning ("Duplicate locator");
2123         return VNET_API_ERROR_VALUE_EXIST;
2124       }
2125   }
2126
2127   return 0;
2128 }
2129
2130 static inline void
2131 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2132                                  u32 ls_index, u32 loc_id)
2133 {
2134   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2135   u32 **ls_indexes = NULL;
2136
2137   ASSERT (ls != NULL);
2138   ASSERT (locit != NULL);
2139
2140   ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2141   pool_put_index (lcm->locator_pool, locit[0]);
2142   vec_del1 (ls->locator_indices, loc_id);
2143   vec_del1 (ls_indexes[0], ls_index);
2144 }
2145
2146 int
2147 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
2148                            locator_set_t * ls, u32 * ls_result)
2149 {
2150   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2151   locator_t *loc = NULL, *itloc = NULL;
2152   uword _p = (u32) ~ 0, *p = &_p;
2153   u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
2154   u32 loc_id = ~0;
2155   int ret = 0;
2156
2157   ASSERT (a != NULL);
2158
2159   if (vnet_lisp_enable_disable_status () == 0)
2160     {
2161       clib_warning ("LISP is disabled!");
2162       return VNET_API_ERROR_LISP_DISABLED;
2163     }
2164
2165   p = get_locator_set_index (a, p);
2166   if (!p)
2167     {
2168       clib_warning ("locator-set %v doesn't exist", a->name);
2169       return VNET_API_ERROR_INVALID_ARGUMENT;
2170     }
2171
2172   if (ls == 0)
2173     {
2174       ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2175       if (!ls)
2176         {
2177           clib_warning ("locator-set %d to be overwritten doesn't exist!",
2178                         p[0]);
2179           return VNET_API_ERROR_INVALID_ARGUMENT;
2180         }
2181     }
2182
2183   if (a->is_add)
2184     {
2185       if (ls_result)
2186         ls_result[0] = p[0];
2187
2188       /* allocate locators */
2189       vec_foreach (itloc, a->locators)
2190       {
2191         ret = is_locator_in_locator_set (lcm, ls, itloc);
2192         if (0 != ret)
2193           {
2194             return ret;
2195           }
2196
2197         pool_get (lcm->locator_pool, loc);
2198         loc[0] = itloc[0];
2199         loc_index = loc - lcm->locator_pool;
2200
2201         vec_add1 (ls->locator_indices, loc_index);
2202
2203         vec_validate (lcm->locator_to_locator_sets, loc_index);
2204         ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2205                                        loc_index);
2206         vec_add1 (ls_indexes[0], p[0]);
2207       }
2208     }
2209   else
2210     {
2211       ls_index = p[0];
2212
2213       itloc = a->locators;
2214       loc_id = 0;
2215       vec_foreach (locit, ls->locator_indices)
2216       {
2217         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2218
2219         if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2220           {
2221             remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2222           }
2223         if (0 == loc->local &&
2224             !gid_address_cmp (&loc->address, &itloc->address))
2225           {
2226             remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2227           }
2228
2229         loc_id++;
2230       }
2231     }
2232
2233   return 0;
2234 }
2235
2236 int
2237 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2238                                u32 * ls_result)
2239 {
2240   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2241   locator_set_t *ls;
2242   uword _p = (u32) ~ 0, *p = &_p;
2243   u32 ls_index;
2244   u32 **eid_indexes;
2245   int ret = 0;
2246
2247   if (vnet_lisp_enable_disable_status () == 0)
2248     {
2249       clib_warning ("LISP is disabled!");
2250       return VNET_API_ERROR_LISP_DISABLED;
2251     }
2252
2253   if (a->is_add)
2254     {
2255       p = get_locator_set_index (a, p);
2256
2257       /* overwrite */
2258       if (p && p[0] != (u32) ~ 0)
2259         {
2260           ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2261           if (!ls)
2262             {
2263               clib_warning ("locator-set %d to be overwritten doesn't exist!",
2264                             p[0]);
2265               return -1;
2266             }
2267
2268           /* clean locator to locator-set vectors and remove locators if
2269            * they're not part of another locator-set */
2270           clean_locator_to_locator_set (lcm, p[0]);
2271
2272           /* remove locator indices from locator set */
2273           vec_free (ls->locator_indices);
2274
2275           ls_index = p[0];
2276
2277           if (ls_result)
2278             ls_result[0] = p[0];
2279         }
2280       /* new locator-set */
2281       else
2282         {
2283           pool_get (lcm->locator_set_pool, ls);
2284           memset (ls, 0, sizeof (*ls));
2285           ls_index = ls - lcm->locator_set_pool;
2286
2287           if (a->local)
2288             {
2289               ls->name = vec_dup (a->name);
2290
2291               if (!lcm->locator_set_index_by_name)
2292                 lcm->locator_set_index_by_name = hash_create_vec (
2293                                                                    /* size */
2294                                                                    0,
2295                                                                    sizeof
2296                                                                    (ls->name
2297                                                                     [0]),
2298                                                                    sizeof
2299                                                                    (uword));
2300               hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2301                             ls_index);
2302
2303               /* mark as local locator-set */
2304               vec_add1 (lcm->local_locator_set_indexes, ls_index);
2305             }
2306           ls->local = a->local;
2307           if (ls_result)
2308             ls_result[0] = ls_index;
2309         }
2310
2311       ret = vnet_lisp_add_del_locator (a, ls, NULL);
2312       if (0 != ret)
2313         {
2314           return ret;
2315         }
2316     }
2317   else
2318     {
2319       p = get_locator_set_index (a, p);
2320       if (!p)
2321         {
2322           clib_warning ("locator-set %v doesn't exists", a->name);
2323           return -1;
2324         }
2325
2326       ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2327       if (!ls)
2328         {
2329           clib_warning ("locator-set with index %d doesn't exists", p[0]);
2330           return -1;
2331         }
2332
2333       if (lcm->mreq_itr_rlocs == p[0])
2334         {
2335           clib_warning ("Can't delete the locator-set used to constrain "
2336                         "the itr-rlocs in map-requests!");
2337           return -1;
2338         }
2339
2340       if (vec_len (lcm->locator_set_to_eids) != 0)
2341         {
2342           eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2343           if (vec_len (eid_indexes[0]) != 0)
2344             {
2345               clib_warning
2346                 ("Can't delete a locator that supports a mapping!");
2347               return -1;
2348             }
2349         }
2350
2351       /* clean locator to locator-sets data */
2352       clean_locator_to_locator_set (lcm, p[0]);
2353
2354       if (ls->local)
2355         {
2356           u32 it, lsi;
2357
2358           vec_foreach_index (it, lcm->local_locator_set_indexes)
2359           {
2360             lsi = vec_elt (lcm->local_locator_set_indexes, it);
2361             if (lsi == p[0])
2362               {
2363                 vec_del1 (lcm->local_locator_set_indexes, it);
2364                 break;
2365               }
2366           }
2367           hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2368         }
2369       vec_free (ls->name);
2370       vec_free (ls->locator_indices);
2371       pool_put (lcm->locator_set_pool, ls);
2372     }
2373   return 0;
2374 }
2375
2376 int
2377 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2378 {
2379   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2380
2381   lcm->rloc_probing = is_enable;
2382   return 0;
2383 }
2384
2385 int
2386 vnet_lisp_map_register_enable_disable (u8 is_enable)
2387 {
2388   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2389
2390   lcm->map_registering = is_enable;
2391   return 0;
2392 }
2393
2394 clib_error_t *
2395 vnet_lisp_enable_disable (u8 is_enable)
2396 {
2397   u32 vni, dp_table;
2398   clib_error_t *error = 0;
2399   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2400   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2401
2402   a->is_en = is_enable;
2403   error = vnet_lisp_gpe_enable_disable (a);
2404   if (error)
2405     {
2406       return clib_error_return (0, "failed to %s data-plane!",
2407                                 a->is_en ? "enable" : "disable");
2408     }
2409
2410   if (is_enable)
2411     {
2412       /* enable all l2 and l3 ifaces */
2413
2414       /* *INDENT-OFF* */
2415       hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2416         dp_add_del_iface(lcm, vni, 0, 1);
2417       }));
2418       hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2419         dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2420       }));
2421       /* *INDENT-ON* */
2422     }
2423   else
2424     {
2425       /* clear interface table */
2426       hash_free (lcm->fwd_entry_by_mapping_index);
2427       pool_free (lcm->fwd_entry_pool);
2428     }
2429
2430   /* update global flag */
2431   lcm->is_enabled = is_enable;
2432
2433   return 0;
2434 }
2435
2436 static clib_error_t *
2437 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2438                                 vlib_cli_command_t * cmd)
2439 {
2440   unformat_input_t _line_input, *line_input = &_line_input;
2441   u8 is_enabled = 0;
2442   u8 is_set = 0;
2443
2444   /* Get a line of input. */
2445   if (!unformat_user (input, unformat_line_input, line_input))
2446     return 0;
2447
2448   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2449     {
2450       if (unformat (line_input, "enable"))
2451         {
2452           is_set = 1;
2453           is_enabled = 1;
2454         }
2455       else if (unformat (line_input, "disable"))
2456         is_set = 1;
2457       else
2458         {
2459           return clib_error_return (0, "parse error: '%U'",
2460                                     format_unformat_error, line_input);
2461         }
2462     }
2463
2464   if (!is_set)
2465     return clib_error_return (0, "state not set");
2466
2467   vnet_lisp_enable_disable (is_enabled);
2468   return 0;
2469 }
2470
2471 /* *INDENT-OFF* */
2472 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2473     .path = "lisp",
2474     .short_help = "lisp [enable|disable]",
2475     .function = lisp_enable_disable_command_fn,
2476 };
2477 /* *INDENT-ON* */
2478
2479 static clib_error_t *
2480 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2481                                              unformat_input_t * input,
2482                                              vlib_cli_command_t * cmd)
2483 {
2484   unformat_input_t _line_input, *line_input = &_line_input;
2485   u8 is_enabled = 0;
2486   u8 is_set = 0;
2487
2488   /* Get a line of input. */
2489   if (!unformat_user (input, unformat_line_input, line_input))
2490     return 0;
2491
2492   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2493     {
2494       if (unformat (line_input, "enable"))
2495         {
2496           is_set = 1;
2497           is_enabled = 1;
2498         }
2499       else if (unformat (line_input, "disable"))
2500         is_set = 1;
2501       else
2502         {
2503           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2504                            line_input);
2505           return 0;
2506         }
2507     }
2508
2509   if (!is_set)
2510     {
2511       vlib_cli_output (vm, "state not set!");
2512       return 0;
2513     }
2514
2515   vnet_lisp_map_register_enable_disable (is_enabled);
2516   return 0;
2517 }
2518
2519 /* *INDENT-OFF* */
2520 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2521     .path = "lisp map-register",
2522     .short_help = "lisp map-register [enable|disable]",
2523     .function = lisp_map_register_enable_disable_command_fn,
2524 };
2525 /* *INDENT-ON* */
2526
2527 static clib_error_t *
2528 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2529                                            unformat_input_t * input,
2530                                            vlib_cli_command_t * cmd)
2531 {
2532   unformat_input_t _line_input, *line_input = &_line_input;
2533   u8 is_enabled = 0;
2534   u8 is_set = 0;
2535
2536   /* Get a line of input. */
2537   if (!unformat_user (input, unformat_line_input, line_input))
2538     return 0;
2539
2540   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2541     {
2542       if (unformat (line_input, "enable"))
2543         {
2544           is_set = 1;
2545           is_enabled = 1;
2546         }
2547       else if (unformat (line_input, "disable"))
2548         is_set = 1;
2549       else
2550         {
2551           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2552                            line_input);
2553           return 0;
2554         }
2555     }
2556
2557   if (!is_set)
2558     {
2559       vlib_cli_output (vm, "state not set!");
2560       return 0;
2561     }
2562
2563   vnet_lisp_rloc_probe_enable_disable (is_enabled);
2564   return 0;
2565 }
2566
2567 /* *INDENT-OFF* */
2568 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2569     .path = "lisp rloc-probe",
2570     .short_help = "lisp rloc-probe [enable|disable]",
2571     .function = lisp_rloc_probe_enable_disable_command_fn,
2572 };
2573 /* *INDENT-ON* */
2574
2575 u8
2576 vnet_lisp_enable_disable_status (void)
2577 {
2578   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2579   return lcm->is_enabled;
2580 }
2581
2582 static u8 *
2583 format_lisp_status (u8 * s, va_list * args)
2584 {
2585   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2586   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2587 }
2588
2589 static clib_error_t *
2590 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2591                              vlib_cli_command_t * cmd)
2592 {
2593   u8 *msg = 0;
2594   msg = format (msg, "feature: %U\ngpe: %U\n",
2595                 format_lisp_status, format_vnet_lisp_gpe_status);
2596   vlib_cli_output (vm, "%v", msg);
2597   vec_free (msg);
2598   return 0;
2599 }
2600
2601 /* *INDENT-OFF* */
2602 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2603     .path = "show lisp status",
2604     .short_help = "show lisp status",
2605     .function = lisp_show_status_command_fn,
2606 };
2607 /* *INDENT-ON* */
2608
2609 static clib_error_t *
2610 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2611                                     unformat_input_t * input,
2612                                     vlib_cli_command_t * cmd)
2613 {
2614   hash_pair_t *p;
2615   unformat_input_t _line_input, *line_input = &_line_input;
2616   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2617   uword *vni_table = 0;
2618   u8 is_l2 = 0;
2619
2620   /* Get a line of input. */
2621   if (!unformat_user (input, unformat_line_input, line_input))
2622     return 0;
2623
2624   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2625     {
2626       if (unformat (line_input, "l2"))
2627         {
2628           vni_table = lcm->bd_id_by_vni;
2629           is_l2 = 1;
2630         }
2631       else if (unformat (line_input, "l3"))
2632         {
2633           vni_table = lcm->table_id_by_vni;
2634           is_l2 = 0;
2635         }
2636       else
2637         return clib_error_return (0, "parse error: '%U'",
2638                                   format_unformat_error, line_input);
2639     }
2640
2641   if (!vni_table)
2642     {
2643       vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2644       return 0;
2645     }
2646
2647   vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2648
2649   /* *INDENT-OFF* */
2650   hash_foreach_pair (p, vni_table,
2651   ({
2652     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2653   }));
2654   /* *INDENT-ON* */
2655
2656   return 0;
2657 }
2658
2659 /* *INDENT-OFF* */
2660 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2661     .path = "show lisp eid-table map",
2662     .short_help = "show lisp eid-table l2|l3",
2663     .function = lisp_show_eid_table_map_command_fn,
2664 };
2665 /* *INDENT-ON* */
2666
2667 static clib_error_t *
2668 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2669                                      unformat_input_t * input,
2670                                      vlib_cli_command_t * cmd)
2671 {
2672   lisp_gpe_main_t *lgm = &lisp_gpe_main;
2673   vnet_main_t *vnm = lgm->vnet_main;
2674   unformat_input_t _line_input, *line_input = &_line_input;
2675   u8 is_add = 1;
2676   clib_error_t *error = 0;
2677   u8 *locator_set_name = 0;
2678   locator_t locator, *locators = 0;
2679   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2680   u32 ls_index = 0;
2681   int rv = 0;
2682
2683   memset (&locator, 0, sizeof (locator));
2684   memset (a, 0, sizeof (a[0]));
2685
2686   /* Get a line of input. */
2687   if (!unformat_user (input, unformat_line_input, line_input))
2688     return 0;
2689
2690   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2691     {
2692       if (unformat (line_input, "add %_%v%_", &locator_set_name))
2693         is_add = 1;
2694       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2695         is_add = 0;
2696       else if (unformat (line_input, "iface %U p %d w %d",
2697                          unformat_vnet_sw_interface, vnm,
2698                          &locator.sw_if_index, &locator.priority,
2699                          &locator.weight))
2700         {
2701           locator.local = 1;
2702           vec_add1 (locators, locator);
2703         }
2704       else
2705         {
2706           error = unformat_parse_error (line_input);
2707           goto done;
2708         }
2709     }
2710
2711   a->name = locator_set_name;
2712   a->locators = locators;
2713   a->is_add = is_add;
2714   a->local = 1;
2715
2716   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2717   if (0 != rv)
2718     {
2719       error = clib_error_return (0, "failed to %s locator-set!",
2720                                  is_add ? "add" : "delete");
2721     }
2722
2723 done:
2724   vec_free (locators);
2725   if (locator_set_name)
2726     vec_free (locator_set_name);
2727   return error;
2728 }
2729
2730 /* *INDENT-OFF* */
2731 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2732     .path = "lisp locator-set",
2733     .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2734         "p <priority> w <weight>]",
2735     .function = lisp_add_del_locator_set_command_fn,
2736 };
2737 /* *INDENT-ON* */
2738
2739 static clib_error_t *
2740 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2741                                         unformat_input_t * input,
2742                                         vlib_cli_command_t * cmd)
2743 {
2744   lisp_gpe_main_t *lgm = &lisp_gpe_main;
2745   vnet_main_t *vnm = lgm->vnet_main;
2746   unformat_input_t _line_input, *line_input = &_line_input;
2747   u8 is_add = 1;
2748   clib_error_t *error = 0;
2749   u8 *locator_set_name = 0;
2750   u8 locator_set_name_set = 0;
2751   locator_t locator, *locators = 0;
2752   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2753   u32 ls_index = 0;
2754
2755   memset (&locator, 0, sizeof (locator));
2756   memset (a, 0, sizeof (a[0]));
2757
2758   /* Get a line of input. */
2759   if (!unformat_user (input, unformat_line_input, line_input))
2760     return 0;
2761
2762   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2763     {
2764       if (unformat (line_input, "add"))
2765         is_add = 1;
2766       else if (unformat (line_input, "del"))
2767         is_add = 0;
2768       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2769         locator_set_name_set = 1;
2770       else if (unformat (line_input, "iface %U p %d w %d",
2771                          unformat_vnet_sw_interface, vnm,
2772                          &locator.sw_if_index, &locator.priority,
2773                          &locator.weight))
2774         {
2775           locator.local = 1;
2776           vec_add1 (locators, locator);
2777         }
2778       else
2779         {
2780           error = unformat_parse_error (line_input);
2781           goto done;
2782         }
2783     }
2784
2785   if (!locator_set_name_set)
2786     {
2787       error = clib_error_return (0, "locator_set name not set!");
2788       goto done;
2789     }
2790
2791   a->name = locator_set_name;
2792   a->locators = locators;
2793   a->is_add = is_add;
2794   a->local = 1;
2795
2796   vnet_lisp_add_del_locator (a, 0, &ls_index);
2797
2798 done:
2799   vec_free (locators);
2800   vec_free (locator_set_name);
2801   return error;
2802 }
2803
2804 /* *INDENT-OFF* */
2805 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2806     .path = "lisp locator",
2807     .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2808                   "p <priority> w <weight>",
2809     .function = lisp_add_del_locator_in_set_command_fn,
2810 };
2811 /* *INDENT-ON* */
2812
2813 static clib_error_t *
2814 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2815                                       unformat_input_t * input,
2816                                       vlib_cli_command_t * cmd)
2817 {
2818   locator_set_t *lsit;
2819   locator_t *loc;
2820   u32 *locit;
2821   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2822
2823   vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2824                    "Priority", "Weight");
2825
2826   /* *INDENT-OFF* */
2827   pool_foreach (lsit, lcm->locator_set_pool,
2828   ({
2829     u8 * msg = 0;
2830     int next_line = 0;
2831     if (lsit->local)
2832       {
2833         msg = format (msg, "%v", lsit->name);
2834       }
2835     else
2836       {
2837         msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2838       }
2839     vec_foreach (locit, lsit->locator_indices)
2840       {
2841         if (next_line)
2842           {
2843             msg = format (msg, "%16s", " ");
2844           }
2845         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2846         if (loc->local)
2847           msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2848                         loc->weight);
2849         else
2850           msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2851                         &gid_address_ip(&loc->address), loc->priority,
2852                         loc->weight);
2853         next_line = 1;
2854       }
2855     vlib_cli_output (vm, "%v", msg);
2856     vec_free (msg);
2857   }));
2858   /* *INDENT-ON* */
2859   return 0;
2860 }
2861
2862 /* *INDENT-OFF* */
2863 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2864     .path = "show lisp locator-set",
2865     .short_help = "Shows locator-sets",
2866     .function = lisp_cp_show_locator_sets_command_fn,
2867 };
2868 /* *INDENT-ON* */
2869
2870 int
2871 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2872 {
2873   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2874   u32 i;
2875   lisp_msmr_t _mr, *mr = &_mr;
2876
2877   if (vnet_lisp_enable_disable_status () == 0)
2878     {
2879       clib_warning ("LISP is disabled!");
2880       return VNET_API_ERROR_LISP_DISABLED;
2881     }
2882
2883   if (a->is_add)
2884     {
2885
2886       if (get_map_resolver (&a->address))
2887         {
2888           clib_warning ("map-resolver %U already exists!", format_ip_address,
2889                         &a->address);
2890           return -1;
2891         }
2892
2893       memset (mr, 0, sizeof (*mr));
2894       ip_address_copy (&mr->address, &a->address);
2895       vec_add1 (lcm->map_resolvers, *mr);
2896
2897       if (vec_len (lcm->map_resolvers) == 1)
2898         lcm->do_map_resolver_election = 1;
2899     }
2900   else
2901     {
2902       for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2903         {
2904           mr = vec_elt_at_index (lcm->map_resolvers, i);
2905           if (!ip_address_cmp (&mr->address, &a->address))
2906             {
2907               if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2908                 lcm->do_map_resolver_election = 1;
2909
2910               vec_del1 (lcm->map_resolvers, i);
2911               break;
2912             }
2913         }
2914     }
2915   return 0;
2916 }
2917
2918 static clib_error_t *
2919 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2920                                       unformat_input_t * input,
2921                                       vlib_cli_command_t * cmd)
2922 {
2923   unformat_input_t _line_input, *line_input = &_line_input;
2924   u8 is_add = 1, addr_set = 0;
2925   ip_address_t ip_addr;
2926   clib_error_t *error = 0;
2927   int rv = 0;
2928   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2929
2930   /* Get a line of input. */
2931   if (!unformat_user (input, unformat_line_input, line_input))
2932     return 0;
2933
2934   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2935     {
2936       if (unformat (line_input, "add"))
2937         is_add = 1;
2938       else if (unformat (line_input, "del"))
2939         is_add = 0;
2940       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2941         addr_set = 1;
2942       else
2943         {
2944           error = unformat_parse_error (line_input);
2945           goto done;
2946         }
2947     }
2948
2949   if (!addr_set)
2950     {
2951       error = clib_error_return (0, "Map-resolver address must be set!");
2952       goto done;
2953     }
2954
2955   a->is_add = is_add;
2956   a->address = ip_addr;
2957   rv = vnet_lisp_add_del_map_resolver (a);
2958   if (0 != rv)
2959     {
2960       error = clib_error_return (0, "failed to %s map-resolver!",
2961                                  is_add ? "add" : "delete");
2962     }
2963
2964 done:
2965   return error;
2966 }
2967
2968 /* *INDENT-OFF* */
2969 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2970     .path = "lisp map-resolver",
2971     .short_help = "lisp map-resolver add/del <ip_address>",
2972     .function = lisp_add_del_map_resolver_command_fn,
2973 };
2974 /* *INDENT-ON* */
2975
2976 int
2977 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2978 {
2979   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2980   uword *p = 0;
2981
2982   if (vnet_lisp_enable_disable_status () == 0)
2983     {
2984       clib_warning ("LISP is disabled!");
2985       return VNET_API_ERROR_LISP_DISABLED;
2986     }
2987
2988   if (a->is_add)
2989     {
2990       p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2991       if (!p)
2992         {
2993           clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2994           return VNET_API_ERROR_INVALID_ARGUMENT;
2995         }
2996
2997       lcm->mreq_itr_rlocs = p[0];
2998     }
2999   else
3000     {
3001       lcm->mreq_itr_rlocs = ~0;
3002     }
3003
3004   return 0;
3005 }
3006
3007 static clib_error_t *
3008 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3009                                         unformat_input_t * input,
3010                                         vlib_cli_command_t * cmd)
3011 {
3012   unformat_input_t _line_input, *line_input = &_line_input;
3013   u8 is_add = 1;
3014   u8 *locator_set_name = 0;
3015   clib_error_t *error = 0;
3016   int rv = 0;
3017   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
3018
3019   /* Get a line of input. */
3020   if (!unformat_user (input, unformat_line_input, line_input))
3021     return 0;
3022
3023   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3024     {
3025       if (unformat (line_input, "del"))
3026         is_add = 0;
3027       else if (unformat (line_input, "add %_%v%_", &locator_set_name))
3028         is_add = 1;
3029       else
3030         {
3031           error = unformat_parse_error (line_input);
3032           goto done;
3033         }
3034     }
3035
3036   a->is_add = is_add;
3037   a->locator_set_name = locator_set_name;
3038   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3039   if (0 != rv)
3040     {
3041       error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3042                                  is_add ? "add" : "delete");
3043     }
3044
3045   vec_free (locator_set_name);
3046
3047 done:
3048   return error;
3049
3050 }
3051
3052 /* *INDENT-OFF* */
3053 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3054     .path = "lisp map-request itr-rlocs",
3055     .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3056     .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3057 };
3058 /* *INDENT-ON* */
3059
3060 static clib_error_t *
3061 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3062                                      unformat_input_t * input,
3063                                      vlib_cli_command_t * cmd)
3064 {
3065   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3066   locator_set_t *loc_set;
3067
3068   vlib_cli_output (vm, "%=20s", "itr-rlocs");
3069
3070   if (~0 == lcm->mreq_itr_rlocs)
3071     {
3072       return 0;
3073     }
3074
3075   loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3076
3077   vlib_cli_output (vm, "%=20s", loc_set->name);
3078
3079   return 0;
3080 }
3081
3082 /* *INDENT-OFF* */
3083 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3084     .path = "show lisp map-request itr-rlocs",
3085     .short_help = "Shows map-request itr-rlocs",
3086     .function = lisp_show_mreq_itr_rlocs_command_fn,
3087 };
3088 /* *INDENT-ON* */
3089
3090 /* Statistics (not really errors) */
3091 #define foreach_lisp_cp_lookup_error           \
3092 _(DROP, "drop")                                \
3093 _(MAP_REQUESTS_SENT, "map-request sent")
3094
3095 static char *lisp_cp_lookup_error_strings[] = {
3096 #define _(sym,string) string,
3097   foreach_lisp_cp_lookup_error
3098 #undef _
3099 };
3100
3101 typedef enum
3102 {
3103 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
3104   foreach_lisp_cp_lookup_error
3105 #undef _
3106     LISP_CP_LOOKUP_N_ERROR,
3107 } lisp_cp_lookup_error_t;
3108
3109 typedef enum
3110 {
3111   LISP_CP_LOOKUP_NEXT_DROP,
3112   LISP_CP_LOOKUP_N_NEXT,
3113 } lisp_cp_lookup_next_t;
3114
3115 typedef struct
3116 {
3117   gid_address_t dst_eid;
3118   ip_address_t map_resolver_ip;
3119 } lisp_cp_lookup_trace_t;
3120
3121 u8 *
3122 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3123 {
3124   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3125   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3126   lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
3127
3128   s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
3129               format_ip_address, &t->map_resolver_ip, format_gid_address,
3130               &t->dst_eid);
3131   return s;
3132 }
3133
3134 int
3135 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
3136                            ip_address_t * sloc)
3137 {
3138   lisp_msmr_t *mrit;
3139   ip_address_t *a;
3140
3141   if (vec_len (lcm->map_resolvers) == 0)
3142     {
3143       clib_warning ("No map-resolver configured");
3144       return 0;
3145     }
3146
3147   /* find the first mr ip we have a route to and the ip of the
3148    * iface that has a route to it */
3149   vec_foreach (mrit, lcm->map_resolvers)
3150   {
3151     a = &mrit->address;
3152     if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3153       {
3154         ip_address_copy (mr_ip, a);
3155
3156         /* also update globals */
3157         return 1;
3158       }
3159   }
3160
3161   clib_warning ("Can't find map-resolver and local interface ip!");
3162   return 0;
3163 }
3164
3165 static gid_address_t *
3166 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3167 {
3168   void *addr;
3169   u32 i;
3170   locator_t *loc;
3171   u32 *loc_indexp;
3172   ip_interface_address_t *ia = 0;
3173   gid_address_t gid_data, *gid = &gid_data;
3174   gid_address_t *rlocs = 0;
3175   ip_prefix_t *ippref = &gid_address_ippref (gid);
3176   ip_address_t *rloc = &ip_prefix_addr (ippref);
3177
3178   memset (gid, 0, sizeof (gid[0]));
3179   gid_address_type (gid) = GID_ADDR_IP_PREFIX;
3180   for (i = 0; i < vec_len (loc_set->locator_indices); i++)
3181     {
3182       loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
3183       loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3184
3185       /* Add ipv4 locators first TODO sort them */
3186
3187       /* *INDENT-OFF* */
3188       foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3189                                     loc->sw_if_index, 1 /* unnumbered */,
3190       ({
3191         addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3192         ip_address_set (rloc, addr, IP4);
3193         ip_prefix_len (ippref) = 32;
3194         ip_prefix_normalize (ippref);
3195         vec_add1 (rlocs, gid[0]);
3196       }));
3197
3198       /* Add ipv6 locators */
3199       foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3200                                     loc->sw_if_index, 1 /* unnumbered */,
3201       ({
3202         addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3203         ip_address_set (rloc, addr, IP6);
3204         ip_prefix_len (ippref) = 128;
3205         ip_prefix_normalize (ippref);
3206         vec_add1 (rlocs, gid[0]);
3207       }));
3208       /* *INDENT-ON* */
3209
3210     }
3211   return rlocs;
3212 }
3213
3214 static vlib_buffer_t *
3215 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3216                    ip_address_t * sloc, ip_address_t * rloc,
3217                    gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3218 {
3219   vlib_buffer_t *b;
3220   u32 bi;
3221   vlib_main_t *vm = lcm->vlib_main;
3222
3223   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3224     {
3225       clib_warning ("Can't allocate buffer for Map-Request!");
3226       return 0;
3227     }
3228
3229   b = vlib_get_buffer (vm, bi);
3230
3231   /* leave some space for the encap headers */
3232   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3233
3234   /* put lisp msg */
3235   lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3236                      1 /* rloc probe */ , nonce_res);
3237
3238   /* push outer ip header */
3239   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3240                        rloc);
3241
3242   bi_res[0] = bi;
3243
3244   return b;
3245 }
3246
3247 static vlib_buffer_t *
3248 build_encapsulated_map_request (lisp_cp_main_t * lcm,
3249                                 gid_address_t * seid, gid_address_t * deid,
3250                                 locator_set_t * loc_set, ip_address_t * mr_ip,
3251                                 ip_address_t * sloc, u8 is_smr_invoked,
3252                                 u64 * nonce_res, u32 * bi_res)
3253 {
3254   vlib_buffer_t *b;
3255   u32 bi;
3256   gid_address_t *rlocs = 0;
3257   vlib_main_t *vm = lcm->vlib_main;
3258
3259   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3260     {
3261       clib_warning ("Can't allocate buffer for Map-Request!");
3262       return 0;
3263     }
3264
3265   b = vlib_get_buffer (vm, bi);
3266
3267   /* leave some space for the encap headers */
3268   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3269
3270   /* get rlocs */
3271   rlocs = build_itr_rloc_list (lcm, loc_set);
3272
3273   if (MR_MODE_SRC_DST == lcm->map_request_mode
3274       && GID_ADDR_SRC_DST != gid_address_type (deid))
3275     {
3276       gid_address_t sd;
3277       memset (&sd, 0, sizeof (sd));
3278       build_src_dst (&sd, seid, deid);
3279       lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3280                          0 /* rloc probe */ , nonce_res);
3281     }
3282   else
3283     {
3284       /* put lisp msg */
3285       lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
3286                          0 /* rloc probe */ , nonce_res);
3287     }
3288
3289   /* push ecm: udp-ip-lisp */
3290   lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3291
3292   /* push outer ip header */
3293   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3294                        mr_ip);
3295
3296   bi_res[0] = bi;
3297
3298   vec_free (rlocs);
3299   return b;
3300 }
3301
3302 static void
3303 reset_pending_mr_counters (pending_map_request_t * r)
3304 {
3305   r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3306   r->retries_num = 0;
3307 }
3308
3309 static int
3310 elect_map_resolver (lisp_cp_main_t * lcm)
3311 {
3312   lisp_msmr_t *mr;
3313
3314   vec_foreach (mr, lcm->map_resolvers)
3315   {
3316     if (!mr->is_down)
3317       {
3318         ip_address_copy (&lcm->active_map_resolver, &mr->address);
3319         lcm->do_map_resolver_election = 0;
3320         return 1;
3321       }
3322   }
3323   return 0;
3324 }
3325
3326 static void
3327 free_map_register_records (mapping_t * maps)
3328 {
3329   mapping_t *map;
3330   vec_foreach (map, maps) vec_free (map->locators);
3331
3332   vec_free (maps);
3333 }
3334
3335 static void
3336 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3337               ip_address_t * probed_loc)
3338 {
3339   u32 *li;
3340   locator_t *loc, new;
3341   ip_interface_address_t *ia = 0;
3342   void *addr;
3343   ip_address_t *new_ip = &gid_address_ip (&new.address);
3344
3345   m->locators = 0;
3346   locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3347                                          locator_set_index);
3348   vec_foreach (li, ls->locator_indices)
3349   {
3350     loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3351     new = loc[0];
3352     if (loc->local)
3353       {
3354           /* *INDENT-OFF* */
3355           foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3356                                         loc->sw_if_index, 1 /* unnumbered */,
3357           ({
3358             addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3359                                                      ia);
3360             ip_address_set (new_ip, addr, IP4);
3361           }));
3362
3363           /* Add ipv6 locators */
3364           foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3365                                         loc->sw_if_index, 1 /* unnumbered */,
3366           ({
3367             addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3368                                                      ia);
3369             ip_address_set (new_ip, addr, IP6);
3370           }));
3371           /* *INDENT-ON* */
3372
3373         if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3374           new.probed = 1;
3375       }
3376     vec_add1 (m->locators, new);
3377   }
3378 }
3379
3380 static mapping_t *
3381 build_map_register_record_list (lisp_cp_main_t * lcm)
3382 {
3383   mapping_t *recs = 0, rec, *m;
3384
3385   /* *INDENT-OFF* */
3386   pool_foreach(m, lcm->mapping_pool,
3387   {
3388     /* for now build only local mappings */
3389     if (!m->local)
3390       continue;
3391
3392     rec = m[0];
3393     add_locators (lcm, &rec, m->locator_set_index, NULL);
3394     vec_add1 (recs, rec);
3395   });
3396   /* *INDENT-ON* */
3397
3398   return recs;
3399 }
3400
3401 static int
3402 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3403                                lisp_key_type_t key_id, u8 * key,
3404                                u16 auth_data_len, u32 msg_len)
3405 {
3406   MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3407   MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3408
3409   unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3410                                 (unsigned char *) map_reg_hdr, msg_len, NULL,
3411                                 NULL);
3412   clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3413
3414   return 0;
3415 }
3416
3417 static vlib_buffer_t *
3418 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3419                     ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3420                     mapping_t * records, lisp_key_type_t key_id, u8 * key,
3421                     u32 * bi_res)
3422 {
3423   void *map_reg_hdr;
3424   vlib_buffer_t *b;
3425   u32 bi, auth_data_len = 0, msg_len = 0;
3426   vlib_main_t *vm = lcm->vlib_main;
3427
3428   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3429     {
3430       clib_warning ("Can't allocate buffer for Map-Register!");
3431       return 0;
3432     }
3433
3434   b = vlib_get_buffer (vm, bi);
3435
3436   /* leave some space for the encap headers */
3437   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3438
3439   auth_data_len = auth_data_len_by_key_id (key_id);
3440   map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3441                                            auth_data_len, nonce_res,
3442                                            &msg_len);
3443
3444   update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3445                                  msg_len);
3446
3447   /* push outer ip header */
3448   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3449                        ms_ip);
3450
3451   bi_res[0] = bi;
3452   return b;
3453 }
3454
3455 static int
3456 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3457 {
3458   lisp_msmr_t *mr;
3459   while (lcm->do_map_resolver_election
3460          | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3461                                                      &lcm->active_map_resolver,
3462                                                      ip)))
3463     {
3464       if (0 == elect_map_resolver (lcm))
3465         /* all map resolvers are down */
3466         {
3467           /* restart MR checking by marking all of them up */
3468           vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3469           return -1;
3470         }
3471     }
3472   return 0;
3473 }
3474
3475 static int
3476 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3477                  u32 local_locator_set_index, ip_address_t * sloc,
3478                  ip_address_t * rloc)
3479 {
3480   locator_set_t *ls;
3481   u32 bi;
3482   vlib_buffer_t *b;
3483   vlib_frame_t *f;
3484   u64 nonce = 0;
3485   u32 next_index, *to_next;
3486   gid_address_t *itr_rlocs;
3487
3488   ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3489   itr_rlocs = build_itr_rloc_list (lcm, ls);
3490
3491   b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3492   vec_free (itr_rlocs);
3493   if (!b)
3494     return -1;
3495
3496   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3497
3498   next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3499     ip4_lookup_node.index : ip6_lookup_node.index;
3500
3501   f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3502
3503   /* Enqueue the packet */
3504   to_next = vlib_frame_vector_args (f);
3505   to_next[0] = bi;
3506   f->n_vectors = 1;
3507   vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3508
3509   hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3510   return 0;
3511 }
3512
3513 static int
3514 send_rloc_probes (lisp_cp_main_t * lcm)
3515 {
3516   mapping_t *lm;
3517   fwd_entry_t *e;
3518   u32 si;
3519
3520   /* *INDENT-OFF* */
3521   pool_foreach (e, lcm->fwd_entry_pool,
3522   {
3523     si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3524     if (~0 == si)
3525       {
3526         clib_warning ("internal error: cannot find local eid %U in "
3527                       "map-cache!", format_gid_address, &e->leid);
3528         continue;
3529       }
3530     lm = pool_elt_at_index (lcm->mapping_pool, si);
3531
3532     if (vec_len (e->locator_pairs) == 0)
3533       continue;
3534
3535     /* TODO send rloc-probe for each pair? for now assume there is only one
3536       pair at a time */
3537     locator_pair_t *lp = &e->locator_pairs[0];
3538
3539     /* get first remote locator */
3540     send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3541                      &lp->rmt_loc);
3542   });
3543   /* *INDENT-ON* */
3544
3545   return 0;
3546 }
3547
3548 static int
3549 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3550 {
3551   u32 bi;
3552   vlib_buffer_t *b;
3553   ip_address_t sloc;
3554   vlib_frame_t *f;
3555   u64 nonce = 0;
3556   u32 next_index, *to_next;
3557   ip_address_t *ms = 0;
3558
3559   // TODO: support multiple map servers and do election
3560   if (0 == vec_len (lcm->map_servers))
3561     return -1;
3562
3563   ms = &lcm->map_servers[0].address;
3564
3565   if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3566     {
3567       clib_warning ("no eligible interface address found for %U!",
3568                     format_ip_address, &lcm->map_servers[0]);
3569       return -1;
3570     }
3571
3572   /* TODO build mapping records grouped by secret key and send one map-register
3573      per group. Currently only one secret key per vpp is supported  */
3574   mapping_t *records = build_map_register_record_list (lcm);
3575   if (!records)
3576     return -1;
3577
3578   u8 *key = records[0].key;
3579   u8 key_id = records[0].key_id;
3580
3581   if (!key)
3582     return 0;                   /* no secret key -> map-register cannot be sent */
3583
3584   b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, records,
3585                           key_id, key, &bi);
3586   if (!b)
3587     return -1;
3588   free_map_register_records (records);
3589
3590   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3591
3592   next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3593     ip4_lookup_node.index : ip6_lookup_node.index;
3594
3595   f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3596
3597   /* Enqueue the packet */
3598   to_next = vlib_frame_vector_args (f);
3599   to_next[0] = bi;
3600   f->n_vectors = 1;
3601   vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3602
3603   hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3604   return 0;
3605 }
3606
3607 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3608   _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3609
3610 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3611   _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3612
3613 static int
3614 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3615                                 gid_address_t * seid, gid_address_t * deid,
3616                                 u8 is_smr_invoked, u8 is_resend)
3617 {
3618   u32 next_index, bi = 0, *to_next, map_index;
3619   vlib_buffer_t *b;
3620   vlib_frame_t *f;
3621   u64 nonce = 0;
3622   locator_set_t *loc_set;
3623   mapping_t *map;
3624   pending_map_request_t *pmr, *duplicate_pmr = 0;
3625   ip_address_t sloc;
3626   u32 ls_index;
3627
3628   ASSERT (*lcm->pending_map_request_lock);
3629
3630   /* if there is already a pending request remember it */
3631
3632   /* *INDENT-OFF* */
3633   pool_foreach(pmr, lcm->pending_map_requests_pool,
3634   ({
3635     if (!gid_address_cmp (&pmr->src, seid)
3636         && !gid_address_cmp (&pmr->dst, deid))
3637       {
3638         duplicate_pmr = pmr;
3639         break;
3640       }
3641   }));
3642   /* *INDENT-ON* */
3643
3644   if (!is_resend && duplicate_pmr)
3645     {
3646       /* don't send the request if there is a pending map request already */
3647       return 0;
3648     }
3649
3650   /* get locator-set for seid */
3651   if (!lcm->lisp_pitr)
3652     {
3653       map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3654       if (map_index == ~0)
3655         {
3656           clib_warning ("No local mapping found in eid-table for %U!",
3657                         format_gid_address, seid);
3658           return -1;
3659         }
3660
3661       map = pool_elt_at_index (lcm->mapping_pool, map_index);
3662
3663       if (!map->local)
3664         {
3665           clib_warning
3666             ("Mapping found for src eid %U is not marked as local!",
3667              format_gid_address, seid);
3668           return -1;
3669         }
3670       ls_index = map->locator_set_index;
3671     }
3672   else
3673     {
3674       map_index = lcm->pitr_map_index;
3675       map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3676       ls_index = map->locator_set_index;
3677     }
3678
3679   /* overwrite locator set if map-request itr-rlocs configured */
3680   if (~0 != lcm->mreq_itr_rlocs)
3681     {
3682       ls_index = lcm->mreq_itr_rlocs;
3683     }
3684
3685   loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3686
3687   if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3688     {
3689       if (duplicate_pmr)
3690         duplicate_pmr->to_be_removed = 1;
3691       return -1;
3692     }
3693
3694   /* build the encapsulated map request */
3695   b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3696                                       &lcm->active_map_resolver,
3697                                       &sloc, is_smr_invoked, &nonce, &bi);
3698
3699   if (!b)
3700     return -1;
3701
3702   /* set fib index to default and lookup node */
3703   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3704   next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3705     ip4_lookup_node.index : ip6_lookup_node.index;
3706
3707   f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3708
3709   /* Enqueue the packet */
3710   to_next = vlib_frame_vector_args (f);
3711   to_next[0] = bi;
3712   f->n_vectors = 1;
3713   vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3714
3715   if (duplicate_pmr)
3716     /* if there is a pending request already update it */
3717     {
3718       if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3719         {
3720           /* remove the oldest nonce */
3721           u64 CLIB_UNUSED (tmp), *nonce_del;
3722           nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3723           hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3724           clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3725         }
3726
3727       clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3728       hash_set (lcm->pending_map_requests_by_nonce, nonce,
3729                 duplicate_pmr - lcm->pending_map_requests_pool);
3730     }
3731   else
3732     {
3733       /* add map-request to pending requests table */
3734       pool_get (lcm->pending_map_requests_pool, pmr);
3735       memset (pmr, 0, sizeof (*pmr));
3736       gid_address_copy (&pmr->src, seid);
3737       gid_address_copy (&pmr->dst, deid);
3738       clib_fifo_add1 (pmr->nonces, nonce);
3739       pmr->is_smr_invoked = is_smr_invoked;
3740       reset_pending_mr_counters (pmr);
3741       hash_set (lcm->pending_map_requests_by_nonce, nonce,
3742                 pmr - lcm->pending_map_requests_pool);
3743     }
3744
3745   return 0;
3746 }
3747
3748 static void
3749 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3750 {
3751   ip4_header_t *ip4 = hdr;
3752   ip6_header_t *ip6;
3753
3754   if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3755     {
3756       ip_address_set (src, &ip4->src_address, IP4);
3757       ip_address_set (dst, &ip4->dst_address, IP4);
3758     }
3759   else
3760     {
3761       ip6 = hdr;
3762       ip_address_set (src, &ip6->src_address, IP6);
3763       ip_address_set (dst, &ip6->dst_address, IP6);
3764     }
3765 }
3766
3767 static u32
3768 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3769                              u8 version)
3770 {
3771   uword *vnip;
3772   u32 vni = ~0, table_id = ~0;
3773
3774   table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3775                                                       IP4 ? FIB_PROTOCOL_IP4 :
3776                                                       FIB_PROTOCOL_IP6),
3777                                                      vnet_buffer
3778                                                      (b)->sw_if_index
3779                                                      [VLIB_RX]);
3780
3781   vnip = hash_get (lcm->vni_by_table_id, table_id);
3782   if (vnip)
3783     vni = vnip[0];
3784   else
3785     clib_warning ("vrf %d is not mapped to any vni!", table_id);
3786
3787   return vni;
3788 }
3789
3790 always_inline u32
3791 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3792 {
3793   uword *vnip;
3794   u32 vni = ~0;
3795   u32 sw_if_index0;
3796
3797   l2input_main_t *l2im = &l2input_main;
3798   l2_input_config_t *config;
3799   l2_bridge_domain_t *bd_config;
3800
3801   sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3802   config = vec_elt_at_index (l2im->configs, sw_if_index0);
3803   bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3804
3805   vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3806   if (vnip)
3807     vni = vnip[0];
3808   else
3809     clib_warning ("bridge domain %d is not mapped to any vni!",
3810                   config->bd_index);
3811
3812   return vni;
3813 }
3814
3815 always_inline void
3816 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3817                                   gid_address_t * src, gid_address_t * dst)
3818 {
3819   u32 vni = 0;
3820   u16 type;
3821
3822   memset (src, 0, sizeof (*src));
3823   memset (dst, 0, sizeof (*dst));
3824   type = vnet_buffer (b)->lisp.overlay_afi;
3825
3826   if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3827     {
3828       ip4_header_t *ip;
3829       u8 version, preflen;
3830
3831       gid_address_type (src) = GID_ADDR_IP_PREFIX;
3832       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3833
3834       ip = vlib_buffer_get_current (b);
3835       get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3836
3837       version = gid_address_ip_version (src);
3838       preflen = ip_address_max_len (version);
3839       gid_address_ippref_len (src) = preflen;
3840       gid_address_ippref_len (dst) = preflen;
3841
3842       vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3843       gid_address_vni (dst) = vni;
3844       gid_address_vni (src) = vni;
3845     }
3846   else if (LISP_AFI_MAC == type)
3847     {
3848       ethernet_header_t *eh;
3849
3850       eh = vlib_buffer_get_current (b);
3851
3852       gid_address_type (src) = GID_ADDR_MAC;
3853       gid_address_type (dst) = GID_ADDR_MAC;
3854       mac_copy (&gid_address_mac (src), eh->src_address);
3855       mac_copy (&gid_address_mac (dst), eh->dst_address);
3856
3857       /* get vni */
3858       vni = lisp_get_vni_from_buffer_eth (lcm, b);
3859
3860       gid_address_vni (dst) = vni;
3861       gid_address_vni (src) = vni;
3862     }
3863 }
3864
3865 static uword
3866 lisp_cp_lookup_inline (vlib_main_t * vm,
3867                        vlib_node_runtime_t * node,
3868                        vlib_frame_t * from_frame, int overlay)
3869 {
3870   u32 *from, *to_next_drop, di, si;
3871   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3872   u32 pkts_mapped = 0;
3873   uword n_left_from, n_left_to_next_drop;
3874
3875   from = vlib_frame_vector_args (from_frame);
3876   n_left_from = from_frame->n_vectors;
3877
3878   while (n_left_from > 0)
3879     {
3880       vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3881                            to_next_drop, n_left_to_next_drop);
3882
3883       while (n_left_from > 0 && n_left_to_next_drop > 0)
3884         {
3885           u32 pi0;
3886           vlib_buffer_t *b0;
3887           gid_address_t src, dst;
3888
3889           pi0 = from[0];
3890           from += 1;
3891           n_left_from -= 1;
3892           to_next_drop[0] = pi0;
3893           to_next_drop += 1;
3894           n_left_to_next_drop -= 1;
3895
3896           b0 = vlib_get_buffer (vm, pi0);
3897           b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3898           vnet_buffer (b0)->lisp.overlay_afi = overlay;
3899
3900           /* src/dst eid pair */
3901           get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3902
3903           /* if we have remote mapping for destination already in map-chache
3904              add forwarding tunnel directly. If not send a map-request */
3905           di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3906                                          &src);
3907           if (~0 != di)
3908             {
3909               mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3910               /* send a map-request also in case of negative mapping entry
3911                  with corresponding action */
3912               if (m->action == LISP_SEND_MAP_REQUEST)
3913                 {
3914                   /* send map-request */
3915                   queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3916                                      0 /* is_resend */ );
3917                   pkts_mapped++;
3918                 }
3919               else
3920                 {
3921                   si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3922                                               &src);
3923                   if (~0 != si)
3924                     {
3925                       dp_add_fwd_entry (lcm, si, di);
3926                     }
3927                 }
3928             }
3929           else
3930             {
3931               /* send map-request */
3932               queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3933                                  0 /* is_resend */ );
3934               pkts_mapped++;
3935             }
3936
3937           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3938             {
3939               lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3940                                                            sizeof (*tr));
3941
3942               memset (tr, 0, sizeof (*tr));
3943               gid_address_copy (&tr->dst_eid, &dst);
3944               ip_address_copy (&tr->map_resolver_ip,
3945                                &lcm->active_map_resolver);
3946             }
3947           gid_address_free (&dst);
3948           gid_address_free (&src);
3949         }
3950
3951       vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3952                            n_left_to_next_drop);
3953     }
3954   vlib_node_increment_counter (vm, node->node_index,
3955                                LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3956                                pkts_mapped);
3957   return from_frame->n_vectors;
3958 }
3959
3960 static uword
3961 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3962                     vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3963 {
3964   return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3965 }
3966
3967 static uword
3968 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3969                     vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3970 {
3971   return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3972 }
3973
3974 static uword
3975 lisp_cp_lookup_l2 (vlib_main_t * vm,
3976                    vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3977 {
3978   return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3979 }
3980
3981 /* *INDENT-OFF* */
3982 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3983   .function = lisp_cp_lookup_ip4,
3984   .name = "lisp-cp-lookup-ip4",
3985   .vector_size = sizeof (u32),
3986   .format_trace = format_lisp_cp_lookup_trace,
3987   .type = VLIB_NODE_TYPE_INTERNAL,
3988
3989   .n_errors = LISP_CP_LOOKUP_N_ERROR,
3990   .error_strings = lisp_cp_lookup_error_strings,
3991
3992   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3993
3994   .next_nodes = {
3995       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3996   },
3997 };
3998 /* *INDENT-ON* */
3999
4000 /* *INDENT-OFF* */
4001 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4002   .function = lisp_cp_lookup_ip6,
4003   .name = "lisp-cp-lookup-ip6",
4004   .vector_size = sizeof (u32),
4005   .format_trace = format_lisp_cp_lookup_trace,
4006   .type = VLIB_NODE_TYPE_INTERNAL,
4007
4008   .n_errors = LISP_CP_LOOKUP_N_ERROR,
4009   .error_strings = lisp_cp_lookup_error_strings,
4010
4011   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4012
4013   .next_nodes = {
4014       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4015   },
4016 };
4017 /* *INDENT-ON* */
4018
4019 /* *INDENT-OFF* */
4020 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4021   .function = lisp_cp_lookup_l2,
4022   .name = "lisp-cp-lookup-l2",
4023   .vector_size = sizeof (u32),
4024   .format_trace = format_lisp_cp_lookup_trace,
4025   .type = VLIB_NODE_TYPE_INTERNAL,
4026
4027   .n_errors = LISP_CP_LOOKUP_N_ERROR,
4028   .error_strings = lisp_cp_lookup_error_strings,
4029
4030   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4031
4032   .next_nodes = {
4033       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4034   },
4035 };
4036 /* *INDENT-ON* */
4037
4038 /* lisp_cp_input statistics */
4039 #define foreach_lisp_cp_input_error                     \
4040 _(DROP, "drop")                                         \
4041 _(MAP_REPLIES_RECEIVED, "map-replies received")
4042
4043 static char *lisp_cp_input_error_strings[] = {
4044 #define _(sym,string) string,
4045   foreach_lisp_cp_input_error
4046 #undef _
4047 };
4048
4049 typedef enum
4050 {
4051 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
4052   foreach_lisp_cp_input_error
4053 #undef _
4054     LISP_CP_INPUT_N_ERROR,
4055 } lisp_cp_input_error_t;
4056
4057 typedef enum
4058 {
4059   LISP_CP_INPUT_NEXT_DROP,
4060   LISP_CP_INPUT_N_NEXT,
4061 } lisp_cp_input_next_t;
4062
4063 typedef struct
4064 {
4065   gid_address_t dst_eid;
4066   ip4_address_t map_resolver_ip;
4067 } lisp_cp_input_trace_t;
4068
4069 u8 *
4070 format_lisp_cp_input_trace (u8 * s, va_list * args)
4071 {
4072   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4073   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
4074   CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4075     va_arg (*args, lisp_cp_input_trace_t *);
4076
4077   s = format (s, "LISP-CP-INPUT: TODO");
4078   return s;
4079 }
4080
4081 static void
4082 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4083 {
4084   mapping_t *m;
4085
4086   m = pool_elt_at_index (lcm->mapping_pool, mi);
4087   lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4088   vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4089                              0 /* is_static */ , 0);
4090   mapping_delete_timer (lcm, mi);
4091 }
4092
4093 static void
4094 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4095                                 f64 expiration_time)
4096 {
4097   mapping_t *m;
4098   u64 now = clib_cpu_time_now ();
4099   u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4100   u64 exp_clock_time = now + expiration_time * cpu_cps;
4101
4102   m = pool_elt_at_index (lcm->mapping_pool, mi);
4103
4104   m->timer_set = 1;
4105   timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4106 }
4107
4108 void *
4109 process_map_reply (void *arg)
4110 {
4111   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4112   vlib_buffer_t *b = arg;
4113   u32 len = 0, i, ttl, dst_map_index = 0;
4114   void *h;
4115   pending_map_request_t *pmr;
4116   locator_t probed;
4117   map_reply_hdr_t *mrep_hdr;
4118   u64 nonce, *noncep;
4119   gid_address_t deid;
4120   uword *pmr_index;
4121   u8 authoritative, action;
4122   locator_t *locators = 0, *loc;
4123
4124   mrep_hdr = vlib_buffer_get_current (b);
4125
4126   lisp_pending_map_request_lock (lcm);
4127
4128   /* Check pending requests table and nonce */
4129   nonce = MREP_NONCE (mrep_hdr);
4130   pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
4131   if (!pmr_index)
4132     {
4133       clib_warning ("No pending map-request entry with nonce %lu!", nonce);
4134       goto done;
4135     }
4136   pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
4137
4138   vlib_buffer_pull (b, sizeof (*mrep_hdr));
4139
4140   for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4141     {
4142       h = vlib_buffer_get_current (b);
4143       ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4144       action = MAP_REC_ACTION (h);
4145       authoritative = MAP_REC_AUTH (h);
4146
4147       len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4148       if (len == ~0)
4149         {
4150           clib_warning ("Failed to parse mapping record!");
4151           vec_foreach (loc, locators)
4152           {
4153             locator_free (loc);
4154           }
4155           vec_free (locators);
4156           goto done;
4157         }
4158
4159       /* insert/update mappings cache */
4160       vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
4161                                  1, 0 /* is_static */ , &dst_map_index);
4162
4163       /* try to program forwarding only if mapping saved or updated */
4164       if ((u32) ~ 0 != dst_map_index)
4165         {
4166           lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
4167           if ((u32) ~ 0 != ttl)
4168             mapping_start_expiration_timer (lcm, dst_map_index, ttl * 60);
4169         }
4170
4171       vec_free (locators);
4172     }
4173
4174   /* remove pending map request entry */
4175
4176   /* *INDENT-OFF* */
4177   clib_fifo_foreach (noncep, pmr->nonces, ({
4178     hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
4179   }));
4180   /* *INDENT-ON* */
4181
4182   clib_fifo_free (pmr->nonces);
4183   pool_put (lcm->pending_map_requests_pool, pmr);
4184
4185 done:
4186   lisp_pending_map_request_unlock (lcm);
4187   return 0;
4188 }
4189
4190 static int
4191 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4192                     lisp_key_type_t key_id, u8 * key)
4193 {
4194   u8 *auth_data = 0;
4195   u16 auth_data_len;
4196   int result;
4197
4198   auth_data_len = auth_data_len_by_key_id (key_id);
4199   if ((u16) ~ 0 == auth_data_len)
4200     {
4201       clib_warning ("invalid length for key_id %d!", key_id);
4202       return 0;
4203     }
4204
4205   /* save auth data */
4206   vec_validate (auth_data, auth_data_len - 1);
4207   clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4208
4209   /* clear auth data */
4210   memset (MNOTIFY_DATA (h), 0, auth_data_len);
4211
4212   /* get hash of the message */
4213   unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4214                               (unsigned char *) h, msg_len, NULL, NULL);
4215
4216   result = memcmp (code, auth_data, auth_data_len);
4217
4218   vec_free (auth_data);
4219
4220   return !result;
4221 }
4222
4223 static void
4224 process_map_notify (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
4225 {
4226   lisp_key_type_t key_id;
4227   gid_address_t deid;
4228   mapping_t *m;
4229   u32 i, mi, len;
4230   map_notify_hdr_t *mnotif_hdr;
4231   u64 nonce;
4232   uword *pmr_index;
4233   locator_t *locators = 0, *loc;
4234   u16 auth_data_len = 0;
4235
4236   mnotif_hdr = vlib_buffer_get_current (b);
4237   vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4238   memset (&deid, 0, sizeof (deid));
4239
4240   nonce = MNOTIFY_NONCE (mnotif_hdr);
4241   key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4242   auth_data_len = auth_data_len_by_key_id (key_id);
4243
4244   pmr_index = hash_get (lcm->map_register_messages_by_nonce, nonce);
4245   if (!pmr_index)
4246     {
4247       clib_warning ("No pending map-register entry with nonce %lu!", nonce);
4248       return;
4249     }
4250
4251   /* advance buffer by authentication data */
4252   vlib_buffer_pull (b, auth_data_len);
4253
4254   /* parse record eid */
4255   for (i = 0; i < MNOTIFY_REC_COUNT (mnotif_hdr); i++)
4256     {
4257       len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4258       if (len == ~0)
4259         {
4260           clib_warning ("Failed to parse mapping record!");
4261           vec_foreach (loc, locators)
4262           {
4263             locator_free (loc);
4264           }
4265           vec_free (locators);
4266           return;
4267         }
4268
4269       vec_free (locators);
4270     }
4271
4272   /* lookup eid in map-cache */
4273   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &deid);
4274   if (~0 == mi)
4275     {
4276       clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4277                     &deid);
4278       return;
4279     }
4280
4281   m = pool_elt_at_index (lcm->mapping_pool, mi);
4282   if (!m->key)
4283     {
4284       clib_warning ("There is no secret key assigned to %U!",
4285                     format_gid_address, &deid);
4286       goto done;
4287     }
4288
4289   /* verify authentication data */
4290   if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4291                            - (u8 *) mnotif_hdr, m->key_id, m->key))
4292     {
4293       clib_warning ("Map-notify auth data verification failed for nonce %lu!",
4294                     nonce);
4295       goto done;
4296     }
4297
4298 done:
4299   hash_unset (lcm->map_register_messages_by_nonce, nonce);
4300   gid_address_free (&deid);
4301 }
4302
4303 static vlib_buffer_t *
4304 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4305                  ip_address_t * dst, u64 nonce, u8 probe_bit,
4306                  mapping_t * records, u16 dst_port, u32 * bi_res)
4307 {
4308   vlib_buffer_t *b;
4309   u32 bi;
4310   vlib_main_t *vm = lcm->vlib_main;
4311
4312   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4313     {
4314       clib_warning ("Can't allocate buffer for Map-Register!");
4315       return 0;
4316     }
4317
4318   b = vlib_get_buffer (vm, bi);
4319
4320   /* leave some space for the encap headers */
4321   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4322
4323   lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4324
4325   /* push outer ip header */
4326   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4327
4328   bi_res[0] = bi;
4329   return b;
4330 }
4331
4332 static int
4333 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4334                 u8 probe_bit, u64 nonce, u16 dst_port,
4335                 ip_address_t * probed_loc)
4336 {
4337   ip_address_t src;
4338   u32 bi;
4339   vlib_buffer_t *b;
4340   vlib_frame_t *f;
4341   u32 next_index, *to_next;
4342   mapping_t *records = 0, *m;
4343
4344   m = pool_elt_at_index (lcm->mapping_pool, mi);
4345   if (!m)
4346     return -1;
4347
4348   vec_add1 (records, m[0]);
4349   add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4350   memset (&src, 0, sizeof (src));
4351
4352   if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4353     {
4354       clib_warning ("can't find inteface address for %U", format_ip_address,
4355                     dst);
4356       return -1;
4357     }
4358
4359   b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4360                        &bi);
4361   if (!b)
4362     return -1;
4363   free_map_register_records (records);
4364
4365   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4366   next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4367     ip4_lookup_node.index : ip6_lookup_node.index;
4368
4369   f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4370
4371   /* Enqueue the packet */
4372   to_next = vlib_frame_vector_args (f);
4373   to_next[0] = bi;
4374   f->n_vectors = 1;
4375   vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4376   return 0;
4377 }
4378
4379 void
4380 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4381                      vlib_buffer_t * b)
4382 {
4383   u8 *ip_hdr = 0, *udp_hdr;
4384   ip4_header_t *ip4;
4385   ip6_header_t *ip6;
4386   ip_address_t *dst_loc = 0, probed_loc, src_loc;
4387   mapping_t m;
4388   map_request_hdr_t *mreq_hdr;
4389   gid_address_t src, dst;
4390   u64 nonce;
4391   u32 i, len = 0;
4392   gid_address_t *itr_rlocs = 0, *rloc;
4393
4394   mreq_hdr = vlib_buffer_get_current (b);
4395
4396   // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4397   // and needs to be fixed
4398   udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4399   ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4400   ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4401
4402   if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4403     ip_hdr = (u8 *) ip4;
4404   else
4405     {
4406       u32 flags = clib_net_to_host_u32
4407         (ip6->ip_version_traffic_class_and_flow_label);
4408       if ((flags & 0xF0000000) == 0x60000000)
4409         ip_hdr = (u8 *) ip6;
4410       else
4411         {
4412           clib_warning ("internal error: cannot determine whether packet "
4413                         "is ip4 or 6!");
4414           return;
4415         }
4416     }
4417
4418   vlib_buffer_pull (b, sizeof (*mreq_hdr));
4419
4420   nonce = MREQ_NONCE (mreq_hdr);
4421
4422   if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4423     {
4424       clib_warning
4425         ("Only SMR Map-Requests and RLOC probe supported for now!");
4426       return;
4427     }
4428
4429   /* parse src eid */
4430   len = lisp_msg_parse_addr (b, &src);
4431   if (len == ~0)
4432     return;
4433
4434   /* for now we don't do anything with the itr's rlocs */
4435   len =
4436     lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4437                               MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4438   if (len == ~0)
4439     return;
4440
4441   /* parse eid records and send SMR-invoked map-requests */
4442   for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4443     {
4444       memset (&dst, 0, sizeof (dst));
4445       len = lisp_msg_parse_eid_rec (b, &dst);
4446       if (len == ~0)
4447         {
4448           clib_warning ("Can't parse map-request EID-record");
4449           goto done;
4450         }
4451
4452       if (MREQ_SMR (mreq_hdr))
4453         {
4454           /* send SMR-invoked map-requests */
4455           queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4456         }
4457       else if (MREQ_RLOC_PROBE (mreq_hdr))
4458         {
4459           memset (&m, 0, sizeof (m));
4460           u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4461
4462           // TODO: select best locator; for now use the first one
4463           dst_loc = &gid_address_ip (&itr_rlocs[0]);
4464
4465           /* get src/dst IP addresses */
4466           get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4467
4468           // TODO get source port from buffer
4469           u16 src_port = LISP_CONTROL_PORT;
4470
4471           send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4472                           src_port, &probed_loc);
4473         }
4474     }
4475
4476 done:
4477   vec_foreach (rloc, itr_rlocs)
4478   {
4479     gid_address_free (rloc);
4480   }
4481 }
4482
4483 static void
4484 queue_map_reply (vlib_buffer_t * b)
4485 {
4486   vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
4487
4488   clib_memcpy (a->data, b->data + b->current_data, b->current_length);
4489   a->current_length = b->current_length;
4490   a->current_data = 0;
4491
4492   vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
4493                                + a->current_length);
4494   clib_mem_free (a);
4495 }
4496
4497 static uword
4498 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4499                vlib_frame_t * from_frame)
4500 {
4501   u32 n_left_from, *from, *to_next_drop;
4502   lisp_msg_type_e type;
4503   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4504
4505   from = vlib_frame_vector_args (from_frame);
4506   n_left_from = from_frame->n_vectors;
4507
4508
4509   while (n_left_from > 0)
4510     {
4511       u32 n_left_to_next_drop;
4512
4513       vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4514                            to_next_drop, n_left_to_next_drop);
4515       while (n_left_from > 0 && n_left_to_next_drop > 0)
4516         {
4517           u32 bi0;
4518           vlib_buffer_t *b0;
4519
4520           bi0 = from[0];
4521           from += 1;
4522           n_left_from -= 1;
4523           to_next_drop[0] = bi0;
4524           to_next_drop += 1;
4525           n_left_to_next_drop -= 1;
4526
4527           b0 = vlib_get_buffer (vm, bi0);
4528
4529           type = lisp_msg_type (vlib_buffer_get_current (b0));
4530           switch (type)
4531             {
4532             case LISP_MAP_REPLY:
4533               queue_map_reply (b0);
4534               break;
4535             case LISP_MAP_REQUEST:
4536               process_map_request (vm, lcm, b0);
4537               break;
4538             case LISP_MAP_NOTIFY:
4539               process_map_notify (vm, lcm, b0);
4540               break;
4541             default:
4542               clib_warning ("Unsupported LISP message type %d", type);
4543               break;
4544             }
4545
4546           b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4547
4548           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4549             {
4550
4551             }
4552         }
4553
4554       vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4555                            n_left_to_next_drop);
4556     }
4557   return from_frame->n_vectors;
4558 }
4559
4560 /* *INDENT-OFF* */
4561 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4562   .function = lisp_cp_input,
4563   .name = "lisp-cp-input",
4564   .vector_size = sizeof (u32),
4565   .format_trace = format_lisp_cp_input_trace,
4566   .type = VLIB_NODE_TYPE_INTERNAL,
4567
4568   .n_errors = LISP_CP_INPUT_N_ERROR,
4569   .error_strings = lisp_cp_input_error_strings,
4570
4571   .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4572
4573   .next_nodes = {
4574       [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4575   },
4576 };
4577 /* *INDENT-ON* */
4578
4579 clib_error_t *
4580 lisp_cp_init (vlib_main_t * vm)
4581 {
4582   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4583   clib_error_t *error = 0;
4584
4585   if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4586     return error;
4587
4588   lcm->im4 = &ip4_main;
4589   lcm->im6 = &ip6_main;
4590   lcm->vlib_main = vm;
4591   lcm->vnet_main = vnet_get_main ();
4592   lcm->mreq_itr_rlocs = ~0;
4593   lcm->lisp_pitr = 0;
4594   memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
4595
4596   lcm->pending_map_request_lock =
4597     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
4598
4599   lcm->pending_map_request_lock[0] = 0;
4600   gid_dictionary_init (&lcm->mapping_index_by_gid);
4601   lcm->do_map_resolver_election = 1;
4602   lcm->map_request_mode = MR_MODE_DST_ONLY;
4603
4604   /* default vrf mapped to vni 0 */
4605   hash_set (lcm->table_id_by_vni, 0, 0);
4606   hash_set (lcm->vni_by_table_id, 0, 0);
4607
4608   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4609                          lisp_cp_input_node.index, 1 /* is_ip4 */ );
4610   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4611                          lisp_cp_input_node.index, 0 /* is_ip4 */ );
4612
4613   u64 now = clib_cpu_time_now ();
4614   timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
4615   return 0;
4616 }
4617
4618 static void *
4619 send_map_request_thread_fn (void *arg)
4620 {
4621   map_request_args_t *a = arg;
4622   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4623
4624   if (a->is_resend)
4625     /* if resending, we already have the lock */
4626     resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4627   else
4628     {
4629       /* get lock before sending map-request */
4630       lisp_pending_map_request_lock (lcm);
4631       send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4632       lisp_pending_map_request_unlock (lcm);
4633     }
4634
4635
4636   return 0;
4637 }
4638
4639 static int
4640 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4641                    u8 smr_invoked, u8 is_resend)
4642 {
4643   map_request_args_t a;
4644
4645   a.is_resend = is_resend;
4646   gid_address_copy (&a.seid, seid);
4647   gid_address_copy (&a.deid, deid);
4648   a.smr_invoked = smr_invoked;
4649
4650   vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4651                                (u8 *) & a, sizeof (a));
4652   return 0;
4653 }
4654
4655 /**
4656  * Take an action with a pending map request depending on expiration time
4657  * and re-try counters.
4658  */
4659 static void
4660 update_pending_request (pending_map_request_t * r, f64 dt)
4661 {
4662   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4663   lisp_msmr_t *mr;
4664
4665   if (r->time_to_expire - dt < 0)
4666     /* it's time to decide what to do with this pending request */
4667     {
4668       if (r->retries_num >= NUMBER_OF_RETRIES)
4669         /* too many retries -> assume current map resolver is not available */
4670         {
4671           mr = get_map_resolver (&lcm->active_map_resolver);
4672           if (!mr)
4673             {
4674               clib_warning ("Map resolver %U not found - probably deleted "
4675                             "by the user recently.", format_ip_address,
4676                             &lcm->active_map_resolver);
4677             }
4678           else
4679             {
4680               clib_warning ("map resolver %U is unreachable, ignoring",
4681                             format_ip_address, &lcm->active_map_resolver);
4682
4683               /* mark current map resolver unavailable so it won't be
4684                * selected next time */
4685               mr->is_down = 1;
4686               mr->last_update = vlib_time_now (lcm->vlib_main);
4687             }
4688
4689           reset_pending_mr_counters (r);
4690           elect_map_resolver (lcm);
4691
4692           /* try to find a next eligible map resolver and re-send */
4693           queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4694                              1 /* resend */ );
4695         }
4696       else
4697         {
4698           /* try again */
4699           queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4700                              1 /* resend */ );
4701           r->retries_num++;
4702           r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4703         }
4704     }
4705   else
4706     r->time_to_expire -= dt;
4707 }
4708
4709 static void
4710 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4711 {
4712   u64 *nonce;
4713   pending_map_request_t *pmr;
4714   u32 *to_be_removed = 0, *pmr_index;
4715
4716   ASSERT (*lcm->pending_map_request_lock);
4717
4718   /* *INDENT-OFF* */
4719   pool_foreach (pmr, lcm->pending_map_requests_pool,
4720   ({
4721     if (pmr->to_be_removed)
4722       {
4723         clib_fifo_foreach (nonce, pmr->nonces, ({
4724           hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4725         }));
4726
4727         vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4728       }
4729   }));
4730   /* *INDENT-ON* */
4731
4732   vec_foreach (pmr_index, to_be_removed)
4733     pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4734
4735   vec_free (to_be_removed);
4736 }
4737
4738 static void
4739 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4740 {
4741   static f64 time_left = RLOC_PROBING_INTERVAL;
4742
4743   if (!lcm->is_enabled || !lcm->rloc_probing)
4744     return;
4745
4746   time_left -= dt;
4747   if (time_left <= 0)
4748     {
4749       time_left = RLOC_PROBING_INTERVAL;
4750       send_rloc_probes (lcm);
4751     }
4752 }
4753
4754 static void
4755 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4756 {
4757   static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4758   static u64 mreg_sent_counter = 0;
4759
4760   if (!lcm->is_enabled || !lcm->map_registering)
4761     return;
4762
4763   time_left -= dt;
4764   if (time_left <= 0)
4765     {
4766       if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4767         time_left = MAP_REGISTER_INTERVAL;
4768       else
4769         {
4770           mreg_sent_counter++;
4771           time_left = QUICK_MAP_REGISTER_INTERVAL;
4772         }
4773       send_map_register (lcm, 1 /* want map notify */ );
4774     }
4775 }
4776
4777 static uword
4778 send_map_resolver_service (vlib_main_t * vm,
4779                            vlib_node_runtime_t * rt, vlib_frame_t * f)
4780 {
4781   u32 *expired = 0;
4782   f64 period = 2.0;
4783   pending_map_request_t *pmr;
4784   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4785
4786   while (1)
4787     {
4788       vlib_process_wait_for_event_or_clock (vm, period);
4789
4790       /* currently no signals are expected - just wait for clock */
4791       (void) vlib_process_get_events (vm, 0);
4792
4793       lisp_pending_map_request_lock (lcm);
4794
4795       /* *INDENT-OFF* */
4796       pool_foreach (pmr, lcm->pending_map_requests_pool,
4797       ({
4798         if (!pmr->to_be_removed)
4799           update_pending_request (pmr, period);
4800       }));
4801       /* *INDENT-ON* */
4802
4803       remove_dead_pending_map_requests (lcm);
4804       lisp_pending_map_request_unlock (lcm);
4805
4806       update_map_register (lcm, period);
4807       update_rloc_probing (lcm, period);
4808
4809       u64 now = clib_cpu_time_now ();
4810
4811       expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4812       if (vec_len (expired) > 0)
4813         {
4814           u32 *mi = 0;
4815           vec_foreach (mi, expired)
4816           {
4817             remove_expired_mapping (lcm, mi[0]);
4818           }
4819           _vec_len (expired) = 0;
4820         }
4821     }
4822
4823   /* unreachable */
4824   return 0;
4825 }
4826
4827 /* *INDENT-OFF* */
4828 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4829     .function = send_map_resolver_service,
4830     .type = VLIB_NODE_TYPE_PROCESS,
4831     .name = "lisp-retry-service",
4832     .process_log2_n_stack_bytes = 16,
4833 };
4834 /* *INDENT-ON* */
4835
4836 VLIB_INIT_FUNCTION (lisp_cp_init);
4837
4838 /*
4839  * fd.io coding-style-patch-verification: ON
4840  *
4841  * Local Variables:
4842  * eval: (c-set-style "gnu")
4843  * End:
4844  */