LISP - Bug fix, can`t remove static remote mapping
[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 <vnet/lisp-cp/control.h>
17 #include <vnet/lisp-cp/packets.h>
18 #include <vnet/lisp-cp/lisp_msg_serdes.h>
19 #include <vnet/lisp-gpe/lisp_gpe.h>
20
21 ip_interface_address_t *
22 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
23                                           u8 loop)
24 {
25   vnet_main_t *vnm = vnet_get_main ();
26   vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
27   if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
28     sw_if_index = swif->unnumbered_sw_if_index;
29   u32 ia =
30       (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
31           vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
32           (u32) ~0;
33   return pool_elt_at_index((lm)->if_address_pool, ia);
34 }
35
36 void *
37 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
38                                 u8 version)
39 {
40   ip_interface_address_t * ia;
41
42   ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
43   if (!ia)
44     return 0;
45   return ip_interface_address_get_address (lm, ia);
46 }
47
48 int
49 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
50                                    u8 version, ip_address_t * result)
51 {
52   ip_lookup_main_t * lm;
53   void * addr;
54
55   lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
56   addr = ip_interface_get_first_address (lm, sw_if_index, version);
57   if (!addr)
58     return 0;
59
60   ip_address_set (result, addr, version);
61   return 1;
62 }
63
64 static u32
65 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
66                           ip_address_t * dst)
67 {
68   if (ip_addr_version (dst) == IP4)
69       return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
70                                         0);
71   else
72       return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
73 }
74
75 u32
76 ip_fib_get_egress_iface_for_dst_with_lm (lisp_cp_main_t * lcm,
77                                          ip_address_t * dst,
78                                          ip_lookup_main_t * lm)
79 {
80   u32 adj_index;
81   ip_adjacency_t * adj;
82
83   adj_index = ip_fib_lookup_with_table (lcm, 0, dst);
84   adj = ip_get_adjacency (lm, adj_index);
85
86   if (adj == 0)
87     return ~0;
88
89   /* we only want outgoing routes */
90   if (adj->lookup_next_index != IP_LOOKUP_NEXT_ARP
91       && adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
92     return ~0;
93
94   return adj->rewrite_header.sw_if_index;
95 }
96
97 /**
98  * Find the sw_if_index of the interface that would be used to egress towards
99  * dst.
100  */
101 u32
102 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
103 {
104   ip_lookup_main_t * lm;
105
106   lm = ip_addr_version (dst) == IP4 ?
107       &lcm->im4->lookup_main : &lcm->im6->lookup_main;
108
109   return ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
110 }
111
112 /**
113  * Find first IP of the interface that would be used to egress towards dst.
114  * Returns 1 if the address is found 0 otherwise.
115  */
116 int
117 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
118                                     ip_address_t * result)
119 {
120   u32 si;
121   ip_lookup_main_t * lm;
122   void * addr = 0;
123   u8 ipver;
124
125   ASSERT(result != 0);
126
127   ipver = ip_addr_version(dst);
128
129   lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
130   si = ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
131
132   if ((u32) ~0 == si)
133     return 0;
134
135   /* find the first ip address */
136   addr = ip_interface_get_first_address (lm, si, ipver);
137   if (0 == addr)
138     return 0;
139
140   ip_address_set (result, addr, ipver);
141   return 1;
142 }
143
144 static int
145 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_add)
146 {
147   uword * table_id, * intf;
148   vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
149
150   table_id = hash_get(lcm->table_id_by_vni, vni);
151
152   if (!table_id)
153     {
154       clib_warning ("vni %d not associated to a vrf!", vni);
155       return VNET_API_ERROR_INVALID_VALUE;
156     }
157
158   intf = hash_get(lcm->dp_intf_by_vni, vni);
159
160   /* enable/disable data-plane interface */
161   if (is_add)
162     {
163       /* create interface */
164       if (!intf)
165         {
166           ai->is_add = 1;
167           ai->vni = vni;
168           ai->table_id = table_id[0];
169           vnet_lisp_gpe_add_del_iface (ai, 0);
170
171           /* keep track of vnis for which interfaces have been created */
172           hash_set(lcm->dp_intf_by_vni, vni, 1);
173         }
174     }
175   else
176     {
177       if (intf == 0)
178         {
179           clib_warning("interface for vni %d doesn't exist!", vni);
180           return VNET_API_ERROR_INVALID_VALUE;
181         }
182
183       ai->is_add = 0;
184       ai->vni = vni;
185       ai->table_id = table_id[0];
186       vnet_lisp_gpe_add_del_iface (ai, 0);
187       hash_unset(lcm->dp_intf_by_vni, vni);
188     }
189
190   return 0;
191 }
192
193 static void
194 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
195 {
196   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
197   fwd_entry_t * fe = 0;
198   uword * feip = 0;
199   memset(a, 0, sizeof(*a));
200
201   feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
202   if (!feip)
203     return;
204
205   fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
206
207   /* delete dp fwd entry */
208   u32 sw_if_index;
209   a->is_add = 0;
210   a->dlocator = fe->dst_loc;
211   a->slocator = fe->src_loc;
212   a->vni = gid_address_vni(&a->deid);
213   gid_address_copy(&a->deid, &fe->deid);
214
215   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
216
217   /* delete entry in fwd table */
218   hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
219   pool_put(lcm->fwd_entry_pool, fe);
220 }
221
222 /**
223  * Finds first remote locator with best (lowest) priority that has a local
224  * peer locator with an underlying route to it.
225  *
226  */
227 static u32
228 get_locator_pair (lisp_cp_main_t* lcm, mapping_t * lcl_map, mapping_t * rmt_map,
229                   ip_address_t * lcl_loc, ip_address_t * rmt_loc)
230 {
231   u32 i, minp = ~0, limitp = 0, li, check_index = 0, done = 0, esi;
232   locator_set_t * rmt_ls, * lcl_ls;
233   ip_address_t _lcl, * lcl = &_lcl;
234   locator_t * l, * rmt = 0;
235   uword * checked = 0;
236
237   rmt_ls = pool_elt_at_index(lcm->locator_set_pool, rmt_map->locator_set_index);
238   lcl_ls = pool_elt_at_index(lcm->locator_set_pool, lcl_map->locator_set_index);
239
240   if (!rmt_ls || vec_len(rmt_ls->locator_indices) == 0)
241     return 0;
242
243   while (!done)
244     {
245       rmt = 0;
246
247       /* find unvisited remote locator with best priority */
248       for (i = 0; i < vec_len(rmt_ls->locator_indices); i++)
249         {
250           if (0 != hash_get(checked, i))
251             continue;
252
253           li = vec_elt(rmt_ls->locator_indices, i);
254           l = pool_elt_at_index(lcm->locator_pool, li);
255
256           /* we don't support non-IP locators for now */
257           if (gid_address_type(&l->address) != GID_ADDR_IP_PREFIX)
258             continue;
259
260           if (l->priority < minp && l->priority >= limitp)
261             {
262               minp = l->priority;
263               rmt = l;
264               check_index = i;
265             }
266         }
267       /* check if a local locator with a route to remote locator exists */
268       if (rmt != 0)
269         {
270           esi = ip_fib_get_egress_iface_for_dst (
271               lcm, &gid_address_ip(&rmt->address));
272           if ((u32) ~0 == esi)
273             continue;
274
275           for (i = 0; i < vec_len(lcl_ls->locator_indices); i++)
276             {
277               li = vec_elt (lcl_ls->locator_indices, i);
278               locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
279
280               /* found local locator */
281               if (sl->sw_if_index == esi)
282                 {
283                   if (0 == ip_interface_get_first_ip_address (lcm,
284                              sl->sw_if_index,
285                              gid_address_ip_version(&rmt->address), lcl))
286                     continue;
287
288                   ip_address_copy(rmt_loc, &gid_address_ip(&rmt->address));
289                   ip_address_copy(lcl_loc, lcl);
290                   done = 2;
291                 }
292             }
293
294           /* skip this remote locator in next searches */
295           limitp = minp;
296           hash_set(checked, check_index, 1);
297         }
298       else
299         done = 1;
300     }
301   hash_free(checked);
302   return (done == 2) ? 1 : 0;
303 }
304
305 static void
306 dp_add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
307 {
308   mapping_t * src_map, * dst_map;
309   u32 sw_if_index;
310   uword * feip = 0, * tidp;
311   fwd_entry_t* fe;
312   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
313
314   memset (a, 0, sizeof(*a));
315
316   /* remove entry if it already exists */
317   feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
318   if (feip)
319     dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
320
321   src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
322   dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
323
324   gid_address_copy (&a->deid, &dst_map->eid);
325   a->vni = gid_address_vni(&a->deid);
326
327   tidp = hash_get(lcm->table_id_by_vni, a->vni);
328   if (!tidp)
329     {
330       clib_warning("vni %d not associated to a vrf!", a->vni);
331       return;
332     }
333   a->table_id = tidp[0];
334
335   /* insert data plane forwarding entry */
336   a->is_add = 1;
337
338   /* find best locator pair that 1) verifies LISP policy 2) are connected */
339   if (0 == get_locator_pair (lcm, src_map, dst_map, &a->slocator, &a->dlocator))
340     {
341       /* negative entry */
342       a->is_negative = 1;
343       a->action = dst_map->action;
344     }
345
346   /* TODO remove */
347   u8 ipver = ip_prefix_version(&gid_address_ippref(&a->deid));
348   a->decap_next_index = (ipver == IP4) ?
349           LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
350
351   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
352
353   /* add tunnel to fwd entry table XXX check return value from DP insertion */
354   pool_get (lcm->fwd_entry_pool, fe);
355   fe->dst_loc = a->dlocator;
356   fe->src_loc = a->slocator;
357   gid_address_copy (&fe->deid, &a->deid);
358   hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
359             fe - lcm->fwd_entry_pool);
360 }
361
362 /**
363  * Add/remove mapping to/from map-cache. Overwriting not allowed.
364  */
365 int
366 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
367                              u32 * map_index_result)
368 {
369   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
370   u32 mi, * map_indexp, map_index, i;
371   mapping_t * m, * old_map;
372   u32 ** eid_indexes;
373
374   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
375   old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
376   if (a->is_add)
377     {
378       /* TODO check if overwriting and take appropriate actions */
379       if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid,
380                                                      &a->eid))
381         {
382           clib_warning ("eid %U found in the eid-table", format_gid_address,
383                        &a->eid);
384           return VNET_API_ERROR_VALUE_EXIST;
385         }
386
387       pool_get(lcm->mapping_pool, m);
388       gid_address_copy (&m->eid, &a->eid);
389       m->locator_set_index = a->locator_set_index;
390       m->ttl = a->ttl;
391       m->action = a->action;
392       m->local = a->local;
393
394       map_index = m - lcm->mapping_pool;
395       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
396                               1);
397
398       if (pool_is_free_index(lcm->locator_set_pool, a->locator_set_index))
399         {
400           clib_warning("Locator set with index %d doesn't exist",
401                        a->locator_set_index);
402           return VNET_API_ERROR_INVALID_VALUE;
403         }
404
405       /* add eid to list of eids supported by locator-set */
406       vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
407       eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
408                                      a->locator_set_index);
409       vec_add1(eid_indexes[0], map_index);
410
411       if (a->local)
412         {
413           /* mark as local */
414           vec_add1(lcm->local_mappings_indexes, map_index);
415         }
416       map_index_result[0] = map_index;
417     }
418   else
419     {
420       if (mi == GID_LOOKUP_MISS)
421         {
422           clib_warning("eid %U not found in the eid-table", format_gid_address,
423                        &a->eid);
424           return VNET_API_ERROR_INVALID_VALUE;
425         }
426
427       /* clear locator-set to eids binding */
428       eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
429                                      a->locator_set_index);
430       for (i = 0; i < vec_len(eid_indexes[0]); i++)
431         {
432           map_indexp = vec_elt_at_index(eid_indexes[0], i);
433           if (map_indexp[0] == mi)
434               break;
435         }
436       vec_del1(eid_indexes[0], i);
437
438       /* remove local mark if needed */
439       m = pool_elt_at_index(lcm->mapping_pool, mi);
440       if (m->local)
441         {
442           u32 k, * lm_indexp;
443           for (k = 0; k < vec_len(lcm->local_mappings_indexes); k++)
444             {
445               lm_indexp = vec_elt_at_index(lcm->local_mappings_indexes, k);
446               if (lm_indexp[0] == mi)
447                 break;
448             }
449           vec_del1(lcm->local_mappings_indexes, k);
450         }
451
452       /* remove mapping from dictionary */
453       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
454       gid_address_free (&m->eid);
455       pool_put_index (lcm->mapping_pool, mi);
456     }
457
458   return 0;
459 }
460
461 /**
462  *  Add/update/delete mapping to/in/from map-cache.
463  */
464 int
465 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
466                                  u32 * map_index_result)
467 {
468   uword * table_id;
469   u32 vni;
470
471   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
472
473   if (vnet_lisp_enable_disable_status () == 0)
474     {
475       clib_warning ("LISP is disabled!");
476       return VNET_API_ERROR_LISP_DISABLED;
477     }
478
479   vni = gid_address_vni(&a->eid);
480   table_id = hash_get(lcm->table_id_by_vni, vni);
481
482   if (!table_id)
483     {
484       clib_warning ("vni %d not associated to a vrf!", vni);
485       return VNET_API_ERROR_INVALID_VALUE;
486     }
487
488   /* store/remove mapping from map-cache */
489   return vnet_lisp_map_cache_add_del (a, map_index_result);
490 }
491
492 static clib_error_t *
493 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
494                                    vlib_cli_command_t * cmd)
495 {
496   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
497   unformat_input_t _line_input, * line_input = &_line_input;
498   u8 is_add = 1;
499   gid_address_t eid;
500   ip_prefix_t * prefp = &gid_address_ippref(&eid);
501   u8 * mac = gid_address_mac(&eid);
502   gid_address_t * eids = 0;
503   clib_error_t * error = 0;
504   u8 * locator_set_name = 0;
505   u32 locator_set_index = 0, map_index = 0;
506   uword * p;
507   vnet_lisp_add_del_mapping_args_t _a, * a = &_a;
508   int rv = 0;
509   u32 vni = 0;
510
511   memset (&eid, 0, sizeof (eid));
512   /* Get a line of input. */
513   if (! unformat_user (input, unformat_line_input, line_input))
514     return 0;
515
516   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
517     {
518       if (unformat (line_input, "add"))
519         is_add = 1;
520       else if (unformat (line_input, "del"))
521         is_add = 0;
522       else if (unformat (line_input, "vni %d", &vni))
523         gid_address_vni (&eid) = vni;
524       else if (unformat (line_input, "eid %U", unformat_ip_prefix, prefp))
525         {
526           gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
527           vec_add1(eids, eid);
528         }
529       else if (unformat (line_input, "eid %U", unformat_mac_address, mac))
530         {
531           gid_address_type (&eid) = GID_ADDR_MAC;
532           vec_add1(eids, eid);
533         }
534       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
535         {
536           p = hash_get_mem(lcm->locator_set_index_by_name, locator_set_name);
537           if (!p)
538             {
539               error = clib_error_return(0, "locator-set %s doesn't exist",
540                                         locator_set_name);
541               goto done;
542             }
543           locator_set_index = p[0];
544         }
545       else
546         {
547           error = unformat_parse_error(line_input);
548           goto done;
549         }
550     }
551   /* XXX treat batch configuration */
552
553   a->eid = eid;
554   a->is_add = is_add;
555   a->locator_set_index = locator_set_index;
556   a->local = 1;
557
558   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
559   if (0 != rv)
560    {
561       error = clib_error_return(0, "failed to %s local mapping!",
562                                 is_add ? "add" : "delete");
563    }
564  done:
565   vec_free(eids);
566   if (locator_set_name)
567     vec_free (locator_set_name);
568   gid_address_free (&a->eid);
569   return error;
570 }
571
572 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
573     .path = "lisp eid-table",
574     .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
575       "locator-set <locator-set>",
576     .function = lisp_add_del_local_eid_command_fn,
577 };
578
579 int
580 vnet_lisp_eid_table_map (u32 vni, u32 vrf, u8 is_add)
581 {
582   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
583   uword * table_id, * vnip;
584
585   if (vnet_lisp_enable_disable_status () == 0)
586     {
587       clib_warning ("LISP is disabled!");
588       return -1;
589     }
590
591   if (vni == 0 || vrf == 0)
592     {
593       clib_warning ("can't add/del default vni-vrf mapping!");
594       return -1;
595     }
596
597   table_id = hash_get (lcm->table_id_by_vni, vni);
598   vnip = hash_get (lcm->vni_by_table_id, vrf);
599
600   if (is_add)
601     {
602       if (table_id || vnip)
603         {
604           clib_warning ("vni %d or vrf %d already used in vrf/vni "
605                         "mapping!", vni, vrf);
606           return -1;
607         }
608       hash_set (lcm->table_id_by_vni, vni, vrf);
609       hash_set (lcm->vni_by_table_id, vrf, vni);
610
611       /* create dp iface */
612       dp_add_del_iface (lcm, vni, 1);
613     }
614   else
615     {
616       if (!table_id || !vnip)
617         {
618           clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
619                         "mapping!", vni, vrf);
620           return -1;
621         }
622       hash_unset (lcm->table_id_by_vni, vni);
623       hash_unset (lcm->vni_by_table_id, vrf);
624
625       /* remove dp iface */
626       dp_add_del_iface (lcm, vni, 0);
627     }
628   return 0;
629 }
630
631 static clib_error_t *
632 lisp_eid_table_map_command_fn (vlib_main_t * vm,
633                                unformat_input_t * input,
634                                vlib_cli_command_t * cmd)
635 {
636   u8 is_add = 1;
637   u32 vni = 0, vrf = 0;
638   unformat_input_t _line_input, * line_input = &_line_input;
639
640   /* Get a line of input. */
641   if (! unformat_user (input, unformat_line_input, line_input))
642     return 0;
643
644   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
645     {
646       if (unformat (line_input, "del"))
647         is_add = 0;
648       else if (unformat (line_input, "vni %d", &vni))
649         ;
650       else if (unformat (line_input, "vrf %d", &vrf))
651         ;
652       else
653         {
654           return unformat_parse_error (line_input);
655         }
656     }
657   vnet_lisp_eid_table_map (vni, vrf, is_add);
658   return 0;
659 }
660
661 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
662     .path = "lisp eid-table map",
663     .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf>",
664     .function = lisp_eid_table_map_command_fn,
665 };
666
667
668 /* return 0 if the two locator sets are identical 1 otherwise */
669 static u8
670 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
671                   locator_t * new_locators)
672 {
673   u32 i, old_li;
674   locator_t * old_loc, * new_loc;
675
676   if (vec_len (old_ls_indexes) != vec_len(new_locators))
677     return 1;
678
679   for (i = 0; i < vec_len(new_locators); i++)
680     {
681       old_li = vec_elt(old_ls_indexes, i);
682       old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
683
684       new_loc = vec_elt_at_index(new_locators, i);
685
686       if (locator_cmp (old_loc, new_loc))
687         return 1;
688     }
689   return 0;
690 }
691
692 /**
693  * Adds/removes/updates mapping. Does not program forwarding.
694  *
695  * @param eid end-host identifier
696  * @param rlocs vector of remote locators
697  * @param action action for negative map-reply
698  * @param is_add add mapping if non-zero, delete otherwise
699  * @param res_map_index the map-index that was created/updated/removed. It is
700  *                      set to ~0 if no action is taken.
701  * @return return code
702  */
703 int
704 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
705                            u8 authoritative, u32 ttl, u8 is_add,
706                            u32 * res_map_index)
707 {
708   vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
709   vnet_lisp_add_del_locator_set_args_t _ls_args, * ls_args = &_ls_args;
710   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
711   u32 mi, ls_index = 0, dst_map_index;
712   mapping_t * old_map;
713
714   if (vnet_lisp_enable_disable_status() == 0)
715     {
716       clib_warning ("LISP is disabled!");
717       return VNET_API_ERROR_LISP_DISABLED;
718     }
719
720   if (res_map_index)
721     res_map_index[0] = ~0;
722
723   memset (m_args, 0, sizeof (m_args[0]));
724   memset (ls_args, 0, sizeof (ls_args[0]));
725
726   ls_args->locators = rlocs;
727
728   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
729   old_map = ((u32) ~0 != mi) ? pool_elt_at_index(lcm->mapping_pool, mi) : 0;
730
731   if (is_add)
732     {
733       /* overwrite: if mapping already exists, decide if locators should be
734        * updated and be done */
735       if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
736         {
737           locator_set_t * old_ls;
738
739           /* update mapping attributes */
740           old_map->action = action;
741           old_map->authoritative = authoritative;
742           old_map->ttl = ttl;
743
744           old_ls = pool_elt_at_index(lcm->locator_set_pool,
745                                      old_map->locator_set_index);
746           if (compare_locators (lcm, old_ls->locator_indices,
747                                 ls_args->locators))
748             {
749               /* set locator-set index to overwrite */
750               ls_args->is_add = 1;
751               ls_args->index = old_map->locator_set_index;
752               vnet_lisp_add_del_locator_set (ls_args, 0);
753               if (res_map_index)
754                 res_map_index[0] = mi;
755             }
756         }
757       /* new mapping */
758       else
759         {
760           ls_args->is_add = 1;
761           ls_args->index = ~0;
762
763           vnet_lisp_add_del_locator_set (ls_args, &ls_index);
764
765           /* add mapping */
766           gid_address_copy (&m_args->eid, eid);
767           m_args->is_add = 1;
768           m_args->action = action;
769           m_args->locator_set_index = ls_index;
770           vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
771
772           if (res_map_index)
773             res_map_index[0] = dst_map_index;
774         }
775     }
776   else
777     {
778       if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
779         {
780           clib_warning("cannot delete mapping for eid %U", format_gid_address,
781                        eid);
782           return -1;
783         }
784
785       m_args->is_add = 0;
786       gid_address_copy (&m_args->eid, eid);
787       m_args->locator_set_index = old_map->locator_set_index;
788
789       /* delete mapping associated from map-cache */
790       vnet_lisp_map_cache_add_del (m_args, 0);
791
792       ls_args->is_add = 0;
793       ls_args->index = old_map->locator_set_index;
794       /* delete locator set */
795       vnet_lisp_add_del_locator_set (ls_args, 0);
796
797       /* return old mapping index */
798       if (res_map_index)
799         res_map_index[0] = mi;
800     }
801
802   /* success */
803   return 0;
804 }
805
806 int
807 vnet_lisp_clear_all_remote_adjacencies (void)
808 {
809   int rv = 0;
810   u32 mi, * map_indices = 0, * map_indexp;
811   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
812   vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
813   vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
814
815   pool_foreach_index (mi, lcm->mapping_pool,
816     ({
817       vec_add1 (map_indices, mi);
818     }));
819
820   vec_foreach (map_indexp, map_indices)
821     {
822       mapping_t * map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
823       if (!map->local)
824         {
825           dp_del_fwd_entry (lcm, 0, map_indexp[0]);
826
827           dm_args->is_add = 0;
828           gid_address_copy (&dm_args->eid, &map->eid);
829           dm_args->locator_set_index = map->locator_set_index;
830
831           /* delete mapping associated to fwd entry */
832           vnet_lisp_map_cache_add_del (dm_args, 0);
833
834           ls->is_add = 0;
835           ls->local = 0;
836           ls->index = map->locator_set_index;
837           /* delete locator set */
838           rv = vnet_lisp_add_del_locator_set (ls, 0);
839           if (rv != 0)
840             goto cleanup;
841         }
842     }
843
844 cleanup:
845   if (map_indices)
846     vec_free (map_indices);
847   return rv;
848 }
849
850 /**
851  * Adds adjacency or removes forwarding entry associated to remote mapping.
852  * Note that adjacencies are not stored, they only result in forwarding entries
853  * being created.
854  */
855 int
856 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
857                         gid_address_t * remote_eid, u8 is_add)
858 {
859   u32 local_mi, remote_mi = ~0;
860
861   if (vnet_lisp_enable_disable_status () == 0)
862     {
863       clib_warning ("LISP is disabled!");
864       return VNET_API_ERROR_LISP_DISABLED;
865     }
866
867   remote_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, remote_eid);
868   if (GID_LOOKUP_MISS == remote_mi)
869     {
870       clib_warning("Remote eid %U not found. Cannot add adjacency!",
871                    format_gid_address, remote_eid);
872
873       return -1;
874     }
875
876   if (is_add)
877     {
878       /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
879        * delete*/
880
881       /* check if source eid has an associated mapping. If pitr mode is on,
882        * just use the pitr's mapping */
883       local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
884               gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
885
886
887       if (GID_LOOKUP_MISS == local_mi)
888         {
889           clib_warning("Local eid %U not found. Cannot add adjacency!",
890                        format_gid_address, local_eid);
891
892           return -1;
893         }
894
895       /* update forwarding */
896       dp_add_fwd_entry (lcm, local_mi, remote_mi);
897     }
898   else
899     dp_del_fwd_entry (lcm, 0, remote_mi);
900
901   return 0;
902 }
903
904 int
905 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
906 {
907   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
908   return lisp_add_del_adjacency(lcm, &a->seid, &a->deid, a->is_add);
909 }
910
911 /**
912  * Handler for add/del remote mapping CLI.
913  *
914  * @param vm vlib context
915  * @param input input from user
916  * @param cmd cmd
917  * @return pointer to clib error structure
918  */
919 static clib_error_t *
920 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
921                                         unformat_input_t * input,
922                                         vlib_cli_command_t * cmd)
923 {
924   clib_error_t * error = 0;
925   unformat_input_t _line_input, * line_input = &_line_input;
926   u8 is_add = 1, del_all = 0;
927   locator_t rloc, * rlocs = 0, * curr_rloc = 0;
928   ip_prefix_t * eid_ippref;
929   gid_address_t eid;
930   u8 * dmac = gid_address_mac (&eid);
931   u8 eid_set = 0;
932   u8 * s = 0;
933   u32 vni, action = ~0, p, w;
934   int rv;
935
936   /* Get a line of input. */
937   if (! unformat_user (input, unformat_line_input, line_input))
938     return 0;
939
940   memset(&eid, 0, sizeof(eid));
941   memset(&rloc, 0, sizeof(rloc));
942
943   eid_ippref = &gid_address_ippref(&eid);
944
945   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
946     {
947       if (unformat (line_input, "del-all"))
948         del_all = 1;
949       else if (unformat (line_input, "del"))
950         is_add = 0;
951       else if (unformat (line_input, "add"))
952         ;
953       else if (unformat (line_input, "eid %U",
954                          unformat_ip_prefix, eid_ippref))
955         {
956           gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
957           eid_set = 1;
958         }
959       else if (unformat (line_input, "eid %U",
960                          unformat_mac_address, dmac))
961         {
962           gid_address_type (&eid) = GID_ADDR_MAC;
963           eid_set = 1;
964         }
965       else if (unformat (line_input, "vni %u", &vni))
966         {
967           gid_address_vni (&eid) = vni;
968         }
969       else if (unformat (line_input, "p %d w %d", &p, &w))
970         {
971           if (!curr_rloc)
972             {
973               clib_warning ("No RLOC configured for setting priority/weight!");
974               goto done;
975             }
976           curr_rloc->priority = p;
977           curr_rloc->weight = w;
978         }
979       else if (unformat (line_input, "rloc %U", unformat_ip_address,
980                          &rloc.address))
981         {
982           vec_add1 (rlocs, rloc);
983           curr_rloc = &rlocs[vec_len (rlocs) - 1];
984         }
985       else if (unformat (line_input, "action %s", &s))
986         {
987           if (!strcmp ((char *)s, "no-action"))
988             action = ACTION_NONE;
989           if (!strcmp ((char *)s, "natively-forward"))
990             action = ACTION_NATIVELY_FORWARDED;
991           if (!strcmp ((char *)s, "send-map-request"))
992             action = ACTION_SEND_MAP_REQUEST;
993           else if (!strcmp ((char *)s, "drop"))
994             action = ACTION_DROP;
995           else
996             {
997               clib_warning ("invalid action: '%s'", s);
998               goto done;
999             }
1000         }
1001       else
1002         {
1003           clib_warning ("parse error");
1004           goto done;
1005         }
1006     }
1007
1008   if (!eid_set)
1009     {
1010       clib_warning ("missing eid!");
1011       goto done;
1012     }
1013
1014   if (!del_all)
1015     {
1016
1017
1018       if (is_add && (~0 == action)
1019           && 0 == vec_len (rlocs))
1020         {
1021           clib_warning ("no action set for negative map-reply!");
1022           goto done;
1023         }
1024     }
1025   else
1026     {
1027       vnet_lisp_clear_all_remote_adjacencies ();
1028       goto done;
1029     }
1030
1031   /* TODO build src/dst with seid*/
1032
1033   /* if it's a delete, clean forwarding */
1034   if (!is_add)
1035     {
1036       lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1037       rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1038       if (rv)
1039         {
1040           goto done;
1041         }
1042     }
1043
1044   /* add as static remote mapping, i.e., not authoritative and infinite
1045    * ttl */
1046   rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add, 0);
1047
1048   if (rv)
1049     clib_warning("failed to %s remote mapping!", is_add ? "add" : "delete");
1050
1051 done:
1052   vec_free (rlocs);
1053   unformat_free (line_input);
1054   if (s)
1055     vec_free (s);
1056   return error;
1057 }
1058
1059 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
1060     .path = "lisp remote-mapping",
1061     .short_help = "lisp remote-mapping add|del [del-all] vni <vni> "
1062      "eid <est-eid> [action <no-action|natively-forward|"
1063      "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
1064     .function = lisp_add_del_remote_mapping_command_fn,
1065 };
1066
1067 /**
1068  * Handler for add/del adjacency CLI.
1069  */
1070 static clib_error_t *
1071 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1072                                    vlib_cli_command_t * cmd)
1073 {
1074   clib_error_t * error = 0;
1075   unformat_input_t _line_input, * line_input = &_line_input;
1076   vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
1077   u8 is_add = 1;
1078   locator_t rloc, * rlocs = 0;
1079   ip_prefix_t * deid_ippref, * seid_ippref;
1080   gid_address_t seid, deid;
1081   u8 * dmac = gid_address_mac (&deid);
1082   u8 * smac = gid_address_mac (&seid);
1083   u8 deid_set = 0, seid_set = 0;
1084   u8 * s = 0;
1085   u32 vni, action = ~0;
1086   int rv;
1087
1088   /* Get a line of input. */
1089   if (! unformat_user (input, unformat_line_input, line_input))
1090     return 0;
1091
1092   memset(&deid, 0, sizeof(deid));
1093   memset(&seid, 0, sizeof(seid));
1094   memset(&rloc, 0, sizeof(rloc));
1095
1096   seid_ippref = &gid_address_ippref(&seid);
1097   deid_ippref = &gid_address_ippref(&deid);
1098
1099   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1100     {
1101       if (unformat (line_input, "del"))
1102         is_add = 0;
1103       else if (unformat (line_input, "add"))
1104         ;
1105       else if (unformat (line_input, "deid %U",
1106                          unformat_ip_prefix, deid_ippref))
1107         {
1108           gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
1109           deid_set = 1;
1110         }
1111       else if (unformat (line_input, "deid %U",
1112                          unformat_mac_address, dmac))
1113         {
1114           gid_address_type (&deid) = GID_ADDR_MAC;
1115           deid_set = 1;
1116         }
1117       else if (unformat (line_input, "vni %u", &vni))
1118         {
1119           gid_address_vni (&seid) = vni;
1120           gid_address_vni (&deid) = vni;
1121         }
1122       else if (unformat (line_input, "seid %U",
1123                          unformat_ip_prefix, seid_ippref))
1124         {
1125           gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
1126           seid_set = 1;
1127         }
1128       else if (unformat (line_input, "seid %U",
1129                          unformat_mac_address, smac))
1130         {
1131           gid_address_type (&seid) = GID_ADDR_MAC;
1132           seid_set = 1;
1133         }
1134       else
1135         {
1136           clib_warning ("parse error");
1137           goto done;
1138         }
1139     }
1140
1141   if (!deid_set)
1142     {
1143       clib_warning ("missing deid!");
1144       goto done;
1145     }
1146
1147   if (GID_ADDR_IP_PREFIX == gid_address_type (&deid))
1148     {
1149       /* if seid not set, make sure the ip version is the same as that
1150        * of the deid. This ensures the seid to be configured will be
1151        * either 0/0 or ::/0 */
1152       if (!seid_set)
1153         ip_prefix_version(seid_ippref) = ip_prefix_version(deid_ippref);
1154
1155       if (is_add &&
1156           (ip_prefix_version (deid_ippref)
1157            != ip_prefix_version(seid_ippref)))
1158         {
1159           clib_warning ("source and destination EIDs are not"
1160                         " in the same IP family!");
1161           goto done;
1162         }
1163     }
1164
1165   if (is_add && (~0 == action)
1166       && 0 == vec_len (rlocs))
1167     {
1168       clib_warning ("no action set for negative map-reply!");
1169       goto done;
1170     }
1171
1172   memset(a, 0, sizeof(a[0]));
1173   gid_address_copy (&a->seid, &deid);
1174   gid_address_copy (&a->deid, &seid);
1175
1176   a->is_add = is_add;
1177   rv = vnet_lisp_add_del_adjacency (a);
1178
1179   if (rv)
1180     clib_warning("failed to %s adjacency!", is_add ? "add" : "delete");
1181
1182 done:
1183   unformat_free (line_input);
1184   if (s)
1185     vec_free (s);
1186   return error;
1187 }
1188
1189 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1190     .path = "lisp adjacency",
1191     .short_help = "lisp adjacency add|del vni <vni>"
1192      "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
1193      "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
1194     .function = lisp_add_del_adjacency_command_fn,
1195 };
1196
1197
1198 static clib_error_t *
1199 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1200                                     unformat_input_t * input,
1201                                     vlib_cli_command_t * cmd)
1202 {
1203   ip_address_t * addr;
1204   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1205
1206   vec_foreach (addr, lcm->map_resolvers)
1207     {
1208       vlib_cli_output (vm, "%U", format_ip_address, addr);
1209     }
1210   return 0;
1211 }
1212
1213 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1214     .path = "show lisp map-resolvers",
1215     .short_help = "show lisp map-resolvers",
1216     .function = lisp_show_map_resolvers_command_fn,
1217 };
1218
1219 int
1220 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1221 {
1222   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1223   u32 locator_set_index = ~0;
1224   mapping_t * m;
1225   uword * p;
1226
1227   if (vnet_lisp_enable_disable_status () == 0)
1228     {
1229       clib_warning ("LISP is disabled!");
1230       return VNET_API_ERROR_LISP_DISABLED;
1231     }
1232
1233   p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1234   if (!p)
1235     {
1236       clib_warning ("locator-set %v doesn't exist", locator_set_name);
1237       return -1;
1238     }
1239   locator_set_index = p[0];
1240
1241   if (is_add)
1242     {
1243       pool_get (lcm->mapping_pool, m);
1244       m->locator_set_index = locator_set_index;
1245       m->local = 1;
1246       lcm->pitr_map_index = m - lcm->mapping_pool;
1247
1248       /* enable pitr mode */
1249       lcm->lisp_pitr = 1;
1250     }
1251   else
1252     {
1253       /* remove pitr mapping */
1254       pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1255
1256       /* disable pitr mode */
1257       lcm->lisp_pitr = 0;
1258     }
1259   return 0;
1260 }
1261
1262 static clib_error_t *
1263 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1264                                       unformat_input_t * input,
1265                                       vlib_cli_command_t * cmd)
1266 {
1267   u8 locator_name_set = 0;
1268   u8 * locator_set_name = 0;
1269   u8 is_add = 1;
1270   unformat_input_t _line_input, * line_input = &_line_input;
1271   clib_error_t * error = 0;
1272   int rv = 0;
1273
1274   /* Get a line of input. */
1275   if (! unformat_user (input, unformat_line_input, line_input))
1276     return 0;
1277
1278   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1279     {
1280       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1281         locator_name_set = 1;
1282       else if (unformat (line_input, "disable"))
1283         is_add = 0;
1284       else
1285         return clib_error_return (0, "parse error");
1286     }
1287
1288   if (!locator_name_set)
1289     {
1290       clib_warning ("No locator set specified!");
1291       goto done;
1292     }
1293   rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1294   if (0 != rv)
1295     {
1296       error = clib_error_return(0, "failed to %s pitr!",
1297                                 is_add ? "add" : "delete");
1298     }
1299
1300 done:
1301   if (locator_set_name)
1302     vec_free (locator_set_name);
1303   return error;
1304 }
1305
1306 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1307     .path = "lisp pitr",
1308     .short_help = "lisp pitr [disable] ls <locator-set-name>",
1309     .function = lisp_pitr_set_locator_set_command_fn,
1310 };
1311
1312 static clib_error_t *
1313 lisp_show_pitr_command_fn (vlib_main_t * vm,
1314                            unformat_input_t * input,
1315                            vlib_cli_command_t * cmd)
1316 {
1317   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1318   mapping_t * m;
1319   locator_set_t * ls;
1320   u8 * tmp_str = 0;
1321
1322   vlib_cli_output (vm, "%=20s%=16s",
1323                    "pitr", lcm->lisp_pitr ? "locator-set" : "");
1324
1325   if (!lcm->lisp_pitr) {
1326     vlib_cli_output (vm, "%=20s", "disable");
1327     return 0;
1328   }
1329
1330   if (~0 == lcm->pitr_map_index) {
1331     tmp_str = format(0, "N/A");
1332   } else {
1333     m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1334     if (~0 != m->locator_set_index) {
1335       ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1336       tmp_str = format(0, "%s", ls->name);
1337     } else {
1338       tmp_str = format(0, "N/A");
1339     }
1340   }
1341   vec_add1(tmp_str, 0);
1342
1343   vlib_cli_output (vm, "%=20s%=16s",
1344                    "enable", tmp_str);
1345
1346   vec_free(tmp_str);
1347
1348   return 0;
1349 }
1350
1351 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1352     .path = "show lisp pitr",
1353     .short_help = "Show pitr",
1354     .function = lisp_show_pitr_command_fn,
1355 };
1356
1357 static u8 *
1358 format_eid_entry (u8 * s, va_list * args)
1359 {
1360   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
1361   lisp_cp_main_t * lcm = va_arg (*args, lisp_cp_main_t *);
1362   gid_address_t * gid = va_arg (*args, gid_address_t *);
1363   locator_set_t * ls = va_arg (*args, locator_set_t *);
1364   u32 * loc_index;
1365   u8 first_line = 1;
1366   u8 * loc;
1367
1368   u8 * type = ls->local ? format(0, "local(%s)", ls->name)
1369                         : format(0, "remote");
1370
1371   if (vec_len (ls->locator_indices) == 0)
1372     {
1373       s = format (s, "%-35U%-20s", format_gid_address, gid, type);
1374     }
1375   else
1376     {
1377       vec_foreach (loc_index, ls->locator_indices)
1378         {
1379           locator_t * l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1380           if (l->local)
1381             loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1382                           l->sw_if_index);
1383           else
1384             loc = format (0, "%U", format_ip_address,
1385                           &gid_address_ip (&l->address));
1386
1387           if (first_line)
1388             {
1389               s = format (s, "%-35U%-20s%-v\n", format_gid_address,
1390                           gid, type, loc);
1391               first_line = 0;
1392             }
1393           else
1394             s = format (s, "%55s%v\n", "", loc);
1395         }
1396     }
1397   return s;
1398 }
1399
1400 static clib_error_t *
1401 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1402                                 unformat_input_t * input,
1403                                 vlib_cli_command_t * cmd)
1404 {
1405   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1406   mapping_t * mapit;
1407   unformat_input_t _line_input, * line_input = &_line_input;
1408   u32 mi;
1409   gid_address_t eid;
1410   u8 print_all = 1;
1411
1412   memset (&eid, 0, sizeof(eid));
1413
1414   /* Get a line of input. */
1415   if (!unformat_user (input, unformat_line_input, line_input))
1416     return 0;
1417
1418   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1419     {
1420       if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1421         print_all = 0;
1422       else
1423         return clib_error_return (0, "parse error: '%U'",
1424                                   format_unformat_error, line_input);
1425     }
1426
1427   vlib_cli_output (vm, "%-35s%-20s%-s", "EID", "type", "locators");
1428
1429   if (print_all)
1430     {
1431       pool_foreach (mapit, lcm->mapping_pool,
1432       ({
1433         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1434                                                 mapit->locator_set_index);
1435         vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1436                          lcm, &mapit->eid, ls);
1437       }));
1438     }
1439   else
1440     {
1441       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1442       if ((u32)~0 == mi)
1443         return 0;
1444
1445       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1446       locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1447                                               mapit->locator_set_index);
1448       vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1449                        lcm, &mapit->eid, ls);
1450     }
1451
1452   return 0;
1453 }
1454
1455 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
1456     .path = "show lisp eid-table",
1457     .short_help = "Shows EID table",
1458     .function = lisp_show_eid_table_command_fn,
1459 };
1460
1461 /* cleans locator to locator-set data and removes locators not part of
1462  * any locator-set */
1463 static void
1464 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1465 {
1466   u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1467   locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
1468   for (i = 0; i < vec_len(ls->locator_indices); i++)
1469     {
1470       loc_indexp = vec_elt_at_index(ls->locator_indices, i);
1471       ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1472                                     loc_indexp[0]);
1473       for (j = 0; j < vec_len(ls_indexes[0]); j++)
1474         {
1475           ls_indexp = vec_elt_at_index(ls_indexes[0], j);
1476           if (ls_indexp[0] == lsi)
1477             break;
1478         }
1479
1480       /* delete index for removed locator-set*/
1481       vec_del1(ls_indexes[0], j);
1482
1483       /* delete locator if it's part of no locator-set */
1484       if (vec_len (ls_indexes[0]) == 0)
1485         {
1486           pool_put_index (lcm->locator_pool, loc_indexp[0]);
1487           vec_add1 (to_be_deleted, i);
1488         }
1489     }
1490
1491   if (to_be_deleted)
1492     {
1493       for (i = 0; i < vec_len (to_be_deleted); i++)
1494         {
1495           loc_indexp = vec_elt_at_index (to_be_deleted, i);
1496           vec_del1 (ls->locator_indices, loc_indexp[0]);
1497         }
1498       vec_free (to_be_deleted);
1499     }
1500 }
1501
1502 static inline uword *
1503 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1504 {
1505   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1506
1507   ASSERT(a != NULL);
1508   ASSERT(p != NULL);
1509
1510   /* find locator-set */
1511   if (a->local)
1512     {
1513       p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
1514     }
1515   else
1516     {
1517       *p = a->index;
1518     }
1519
1520   return p;
1521 }
1522
1523 static inline int
1524 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1525                            locator_t * loc)
1526 {
1527   locator_t * itloc;
1528   u32 * locit;
1529
1530   ASSERT(ls != NULL);
1531   ASSERT(loc != NULL);
1532
1533   vec_foreach(locit, ls->locator_indices)
1534     {
1535       itloc = pool_elt_at_index(lcm->locator_pool, locit[0]);
1536       if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1537           (!ls->local && !gid_address_cmp(&itloc->address, &loc->address)))
1538         {
1539           clib_warning("Duplicate locator");
1540           return VNET_API_ERROR_VALUE_EXIST;
1541         }
1542     }
1543
1544   return 0;
1545 }
1546
1547 static inline void
1548 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit, u32 ls_index,
1549                                  u32 loc_id)
1550 {
1551   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1552   u32 ** ls_indexes = NULL;
1553
1554   ASSERT(ls != NULL);
1555   ASSERT(locit != NULL);
1556
1557   ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1558                                 locit[0]);
1559   pool_put_index(lcm->locator_pool, locit[0]);
1560   vec_del1(ls->locator_indices, loc_id);
1561   vec_del1(ls_indexes[0], ls_index);
1562 }
1563
1564 int
1565 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1566                            locator_set_t * ls, u32 * ls_result)
1567 {
1568   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1569   locator_t * loc = NULL, *itloc = NULL;
1570   uword _p = (u32)~0, * p = &_p;
1571   u32 loc_index = ~0, ls_index = ~0, * locit = NULL, ** ls_indexes = NULL;
1572   u32 loc_id = ~0;
1573   int ret = 0;
1574
1575   ASSERT(a != NULL);
1576
1577   if (vnet_lisp_enable_disable_status () == 0)
1578     {
1579       clib_warning ("LISP is disabled!");
1580       return VNET_API_ERROR_LISP_DISABLED;
1581     }
1582
1583   p = get_locator_set_index(a, p);
1584   if (!p)
1585     {
1586       clib_warning("locator-set %v doesn't exist", a->name);
1587       return VNET_API_ERROR_INVALID_ARGUMENT;
1588     }
1589
1590   if (ls == 0)
1591     {
1592       ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1593       if (!ls)
1594         {
1595           clib_warning("locator-set %d to be overwritten doesn't exist!",
1596                        p[0]);
1597           return VNET_API_ERROR_INVALID_ARGUMENT;
1598         }
1599     }
1600
1601   if (a->is_add)
1602     {
1603
1604         if (ls_result)
1605           ls_result[0] = p[0];
1606
1607         /* allocate locators */
1608         vec_foreach (itloc, a->locators)
1609           {
1610             ret = is_locator_in_locator_set(lcm, ls, itloc);
1611             if (0 != ret)
1612               {
1613                 return ret;
1614               }
1615
1616             pool_get(lcm->locator_pool, loc);
1617             loc[0] = itloc[0];
1618             loc_index = loc - lcm->locator_pool;
1619
1620             vec_add1(ls->locator_indices, loc_index);
1621
1622             vec_validate (lcm->locator_to_locator_sets, loc_index);
1623             ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1624                                           loc_index);
1625             vec_add1(ls_indexes[0], ls_index);
1626           }
1627       }
1628     else
1629       {
1630         ls_index = p[0];
1631
1632         itloc = a->locators;
1633         loc_id = 0;
1634         vec_foreach (locit, ls->locator_indices)
1635           {
1636             loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
1637
1638             if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1639               {
1640                 remove_locator_from_locator_set(ls, locit,
1641                                                 ls_index, loc_id);
1642               }
1643             if (0 == loc->local &&
1644                 !gid_address_cmp(&loc->address, &itloc->address))
1645               {
1646                 remove_locator_from_locator_set(ls, locit,
1647                                                 ls_index, loc_id);
1648               }
1649
1650             loc_id++;
1651           }
1652       }
1653
1654   return 0;
1655 }
1656
1657 int
1658 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1659                                u32 * ls_result)
1660 {
1661   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1662   locator_set_t * ls;
1663   uword _p = (u32)~0, * p = &_p;
1664   u32 ls_index;
1665   u32 ** eid_indexes;
1666   int ret = 0;
1667
1668   if (vnet_lisp_enable_disable_status () == 0)
1669     {
1670       clib_warning ("LISP is disabled!");
1671       return VNET_API_ERROR_LISP_DISABLED;
1672     }
1673
1674   if (a->is_add)
1675     {
1676       p = get_locator_set_index(a, p);
1677
1678       /* overwrite */
1679       if (p && p[0] != (u32)~0)
1680         {
1681           ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1682           if (!ls)
1683             {
1684               clib_warning("locator-set %d to be overwritten doesn't exist!",
1685                            p[0]);
1686               return -1;
1687             }
1688
1689           /* clean locator to locator-set vectors and remove locators if
1690            * they're not part of another locator-set */
1691           clean_locator_to_locator_set (lcm, p[0]);
1692
1693           /* remove locator indices from locator set */
1694           vec_free(ls->locator_indices);
1695
1696           ls_index = p[0];
1697
1698           if (ls_result)
1699             ls_result[0] = p[0];
1700         }
1701       /* new locator-set */
1702       else
1703         {
1704           pool_get(lcm->locator_set_pool, ls);
1705           memset(ls, 0, sizeof(*ls));
1706           ls_index = ls - lcm->locator_set_pool;
1707
1708           if (a->local)
1709             {
1710               ls->name = vec_dup(a->name);
1711
1712               if (!lcm->locator_set_index_by_name)
1713                 lcm->locator_set_index_by_name = hash_create_vec(
1714                     /* size */0, sizeof(ls->name[0]), sizeof(uword));
1715               hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
1716
1717               /* mark as local locator-set */
1718               vec_add1(lcm->local_locator_set_indexes, ls_index);
1719             }
1720           ls->local = a->local;
1721           if (ls_result)
1722             ls_result[0] = ls_index;
1723         }
1724
1725       ret = vnet_lisp_add_del_locator(a, ls, NULL);
1726       if (0 != ret)
1727         {
1728           return ret;
1729         }
1730     }
1731   else
1732     {
1733       p = get_locator_set_index(a, p);
1734       if (!p)
1735         {
1736           clib_warning("locator-set %v doesn't exists", a->name);
1737           return -1;
1738         }
1739
1740       ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1741       if (!ls)
1742         {
1743           clib_warning("locator-set with index %d doesn't exists", p[0]);
1744           return -1;
1745         }
1746
1747       if (lcm->mreq_itr_rlocs == p[0])
1748         {
1749           clib_warning ("Can't delete the locator-set used to constrain "
1750                         "the itr-rlocs in map-requests!");
1751           return -1;
1752         }
1753
1754       if (vec_len(lcm->locator_set_to_eids) != 0)
1755       {
1756           eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
1757           if (vec_len(eid_indexes[0]) != 0)
1758           {
1759               clib_warning ("Can't delete a locator that supports a mapping!");
1760               return -1;
1761           }
1762       }
1763
1764       /* clean locator to locator-sets data */
1765       clean_locator_to_locator_set (lcm, p[0]);
1766
1767       if (ls->local)
1768         {
1769           u32 it, lsi;
1770
1771           vec_foreach_index(it, lcm->local_locator_set_indexes)
1772           {
1773             lsi = vec_elt(lcm->local_locator_set_indexes, it);
1774             if (lsi == p[0])
1775               {
1776                 vec_del1(lcm->local_locator_set_indexes, it);
1777                 break;
1778               }
1779           }
1780           hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
1781         }
1782       vec_free(ls->name);
1783       vec_free(ls->locator_indices);
1784       pool_put(lcm->locator_set_pool, ls);
1785     }
1786   return 0;
1787 }
1788
1789 clib_error_t *
1790 vnet_lisp_enable_disable (u8 is_enable)
1791 {
1792   u32 vni, table_id;
1793   clib_error_t * error = 0;
1794   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1795   vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1796
1797   a->is_en = is_enable;
1798   error = vnet_lisp_gpe_enable_disable (a);
1799   if (error)
1800     {
1801       return clib_error_return (0, "failed to %s data-plane!",
1802                                 a->is_en ? "enable" : "disable");
1803     }
1804
1805   if (is_enable)
1806     {
1807       /* enable all ifaces */
1808       hash_foreach(vni, table_id, lcm->table_id_by_vni, ({
1809         dp_add_del_iface(lcm, vni, 1);
1810       }));
1811     }
1812   else
1813     {
1814       /* clear interface table */
1815       hash_free(lcm->dp_intf_by_vni);
1816       hash_free(lcm->fwd_entry_by_mapping_index);
1817       pool_free(lcm->fwd_entry_pool);
1818     }
1819
1820   /* update global flag */
1821   lcm->is_enabled = is_enable;
1822
1823   return 0;
1824 }
1825
1826 static clib_error_t *
1827 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1828                                 vlib_cli_command_t * cmd)
1829 {
1830   unformat_input_t _line_input, * line_input = &_line_input;
1831   u8 is_enabled = 0;
1832   u8 is_set = 0;
1833
1834   /* Get a line of input. */
1835   if (! unformat_user (input, unformat_line_input, line_input))
1836     return 0;
1837
1838   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1839     {
1840       if (unformat (line_input, "enable"))
1841         {
1842           is_set = 1;
1843           is_enabled = 1;
1844         }
1845       else if (unformat (line_input, "disable"))
1846         is_set = 1;
1847       else
1848         {
1849           return clib_error_return (0, "parse error: '%U'",
1850                                    format_unformat_error, line_input);
1851         }
1852     }
1853
1854   if (!is_set)
1855       return clib_error_return (0, "state not set");
1856
1857   return vnet_lisp_enable_disable (is_enabled);
1858 }
1859
1860 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1861     .path = "lisp",
1862     .short_help = "lisp [enable|disable]",
1863     .function = lisp_enable_disable_command_fn,
1864 };
1865
1866 u8
1867 vnet_lisp_enable_disable_status (void)
1868 {
1869   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1870   return lcm->is_enabled;
1871 }
1872
1873 static u8 *
1874 format_lisp_status (u8 * s, va_list * args)
1875 {
1876   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1877   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1878 }
1879
1880 static clib_error_t *
1881 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1882                              vlib_cli_command_t * cmd)
1883 {
1884   u8 * msg = 0;
1885   msg = format (msg, "feature: %U\ngpe: %U\n",
1886                 format_lisp_status, format_vnet_lisp_gpe_status);
1887   vlib_cli_output (vm, "%v", msg);
1888   vec_free (msg);
1889   return 0;
1890 }
1891
1892 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1893     .path = "show lisp status",
1894     .short_help = "show lisp status",
1895     .function = lisp_show_status_command_fn,
1896 };
1897
1898 static clib_error_t *
1899 lisp_show_eid_table_map_command_fn (vlib_main_t * vm, unformat_input_t * input,
1900                                     vlib_cli_command_t * cmd)
1901 {
1902   hash_pair_t * p;
1903   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1904
1905   vlib_cli_output (vm, "%=10s%=10s", "VNI", "VRF");
1906   hash_foreach_pair (p, lcm->table_id_by_vni,
1907     {
1908       vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
1909     });
1910   return 0;
1911 }
1912
1913 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
1914     .path = "show lisp eid-table map",
1915     .short_help = "show lisp eid-table vni to vrf mappings",
1916     .function = lisp_show_eid_table_map_command_fn,
1917 };
1918
1919 static clib_error_t *
1920 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1921                                      vlib_cli_command_t * cmd)
1922 {
1923   lisp_gpe_main_t * lgm = &lisp_gpe_main;
1924   vnet_main_t * vnm = lgm->vnet_main;
1925   unformat_input_t _line_input, * line_input = &_line_input;
1926   u8 is_add = 1;
1927   clib_error_t * error = 0;
1928   u8 * locator_set_name = 0;
1929   locator_t locator, * locators = 0;
1930   vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1931   u32 ls_index = 0;
1932   int rv = 0;
1933
1934   memset(&locator, 0, sizeof(locator));
1935   memset(a, 0, sizeof(a[0]));
1936
1937   /* Get a line of input. */
1938   if (! unformat_user (input, unformat_line_input, line_input))
1939     return 0;
1940
1941   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1942     {
1943       if (unformat (line_input, "add %_%v%_", &locator_set_name))
1944         is_add = 1;
1945       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1946         is_add = 0;
1947       else if (unformat (line_input, "iface %U p %d w %d",
1948                          unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
1949                          &locator.priority, &locator.weight))
1950         {
1951           locator.local = 1;
1952           vec_add1(locators, locator);
1953         }
1954       else
1955         {
1956           error = unformat_parse_error(line_input);
1957           goto done;
1958         }
1959     }
1960
1961   a->name = locator_set_name;
1962   a->locators = locators;
1963   a->is_add = is_add;
1964   a->local = 1;
1965
1966   rv = vnet_lisp_add_del_locator_set(a, &ls_index);
1967   if (0 != rv)
1968     {
1969       error = clib_error_return(0, "failed to %s locator-set!",
1970                                 is_add ? "add" : "delete");
1971     }
1972
1973  done:
1974   vec_free(locators);
1975   if (locator_set_name)
1976     vec_free (locator_set_name);
1977   return error;
1978 }
1979
1980 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
1981     .path = "lisp locator-set",
1982     .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
1983         "p <priority> w <weight>]",
1984     .function = lisp_add_del_locator_set_command_fn,
1985 };
1986
1987 static clib_error_t *
1988 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1989                                      vlib_cli_command_t * cmd)
1990 {
1991   lisp_gpe_main_t * lgm = &lisp_gpe_main;
1992   vnet_main_t * vnm = lgm->vnet_main;
1993   unformat_input_t _line_input, * line_input = &_line_input;
1994   u8 is_add = 1;
1995   clib_error_t * error = 0;
1996   u8 * locator_set_name = 0;
1997   u8 locator_set_name_set = 0;
1998   locator_t locator, * locators = 0;
1999   vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
2000   u32 ls_index = 0;
2001
2002   memset(&locator, 0, sizeof(locator));
2003   memset(a, 0, sizeof(a[0]));
2004
2005   /* Get a line of input. */
2006   if (! unformat_user (input, unformat_line_input, line_input))
2007     return 0;
2008
2009   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2010     {
2011       if (unformat (line_input, "add"))
2012         is_add = 1;
2013       else if (unformat (line_input, "del"))
2014         is_add = 0;
2015       else if (unformat(line_input, "locator-set %_%v%_", &locator_set_name))
2016         locator_set_name_set = 1;
2017       else if (unformat (line_input, "iface %U p %d w %d",
2018                          unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
2019                          &locator.priority, &locator.weight))
2020         {
2021           locator.local = 1;
2022           vec_add1(locators, locator);
2023         }
2024       else
2025         {
2026           error = unformat_parse_error(line_input);
2027           goto done;
2028         }
2029     }
2030
2031   if (!locator_set_name_set)
2032     {
2033       error = clib_error_return(0, "locator_set name not set!");
2034       goto done;
2035   }
2036
2037   a->name = locator_set_name;
2038   a->locators = locators;
2039   a->is_add = is_add;
2040   a->local = 1;
2041
2042   vnet_lisp_add_del_locator(a, 0, &ls_index);
2043
2044  done:
2045   vec_free(locators);
2046   vec_free (locator_set_name);
2047   return error;
2048 }
2049
2050 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2051     .path = "lisp locator",
2052     .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2053                   "p <priority> w <weight>",
2054     .function = lisp_add_del_locator_in_set_command_fn,
2055 };
2056
2057 static clib_error_t *
2058 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2059                                       unformat_input_t * input,
2060                                       vlib_cli_command_t * cmd)
2061 {
2062   locator_set_t * lsit;
2063   locator_t * loc;
2064   u32 * locit;
2065   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2066
2067   vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
2068                    "Priority", "Weight");
2069   pool_foreach (lsit, lcm->locator_set_pool,
2070   ({
2071     u8 * msg = 0;
2072     int next_line = 0;
2073     if (lsit->local)
2074       {
2075         msg = format (msg, "%=16v", lsit->name);
2076       }
2077     else
2078       {
2079         msg = format (msg, "%=16s", "remote");
2080       }
2081     vec_foreach (locit, lsit->locator_indices)
2082       {
2083         if (next_line)
2084           {
2085             msg = format (msg, "%16s", " ");
2086           }
2087         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2088         if (loc->local)
2089           msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2090                         loc->weight);
2091         else
2092           msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2093                         &gid_address_ip(&loc->address), loc->priority,
2094                         loc->weight);
2095         next_line = 1;
2096       }
2097     vlib_cli_output (vm, "%v", msg);
2098     vec_free (msg);
2099   }));
2100   return 0;
2101 }
2102
2103 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2104     .path = "show lisp locator-set",
2105     .short_help = "Shows locator-sets",
2106     .function = lisp_cp_show_locator_sets_command_fn,
2107 };
2108
2109 int
2110 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2111 {
2112   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2113   ip_address_t * addr;
2114   u32 i;
2115
2116   if (vnet_lisp_enable_disable_status () == 0)
2117     {
2118       clib_warning ("LISP is disabled!");
2119       return VNET_API_ERROR_LISP_DISABLED;
2120     }
2121
2122   if (a->is_add)
2123     {
2124       vec_foreach(addr, lcm->map_resolvers)
2125         {
2126           if (!ip_address_cmp (addr, &a->address))
2127             {
2128               clib_warning("map-resolver %U already exists!", format_ip_address,
2129                            &a->address);
2130               return -1;
2131             }
2132         }
2133       vec_add1(lcm->map_resolvers, a->address);
2134     }
2135   else
2136     {
2137       for (i = 0; i < vec_len(lcm->map_resolvers); i++)
2138         {
2139           addr = vec_elt_at_index(lcm->map_resolvers, i);
2140           if (!ip_address_cmp (addr, &a->address))
2141             {
2142               vec_delete(lcm->map_resolvers, 1, i);
2143               break;
2144             }
2145         }
2146     }
2147   return 0;
2148 }
2149
2150 static clib_error_t *
2151 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2152                                       unformat_input_t * input,
2153                                       vlib_cli_command_t * cmd)
2154 {
2155   unformat_input_t _line_input, * line_input = &_line_input;
2156   u8 is_add = 1, addr_set = 0;
2157   ip_address_t ip_addr;
2158   clib_error_t * error = 0;
2159   int rv = 0;
2160   vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
2161
2162   /* Get a line of input. */
2163   if (! unformat_user (input, unformat_line_input, line_input))
2164     return 0;
2165
2166   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2167     {
2168       if (unformat (line_input, "add"))
2169         is_add = 1;
2170       else if (unformat (line_input, "del"))
2171         is_add = 0;
2172       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2173         addr_set = 1;
2174       else
2175         {
2176           error = unformat_parse_error(line_input);
2177           goto done;
2178         }
2179     }
2180
2181   if (!addr_set)
2182     {
2183       error = clib_error_return(0, "Map-resolver address must be set!");
2184       goto done;
2185     }
2186
2187   a->is_add = is_add;
2188   a->address = ip_addr;
2189   rv = vnet_lisp_add_del_map_resolver (a);
2190   if (0 != rv)
2191     {
2192       error = clib_error_return(0, "failed to %s map-resolver!",
2193                                 is_add ? "add" : "delete");
2194     }
2195
2196  done:
2197   return error;
2198 }
2199
2200 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2201     .path = "lisp map-resolver",
2202     .short_help = "lisp map-resolver add/del <ip_address>",
2203     .function = lisp_add_del_map_resolver_command_fn,
2204 };
2205
2206 int
2207 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2208 {
2209   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2210   uword * p = 0;
2211
2212   if (vnet_lisp_enable_disable_status () == 0)
2213     {
2214       clib_warning("LISP is disabled!");
2215       return VNET_API_ERROR_LISP_DISABLED;
2216     }
2217
2218   if (a->is_add)
2219     {
2220       p = hash_get_mem(lcm->locator_set_index_by_name, a->locator_set_name);
2221       if (!p)
2222         {
2223           clib_warning("locator-set %v doesn't exist", a->locator_set_name);
2224           return VNET_API_ERROR_INVALID_ARGUMENT;
2225         }
2226
2227       lcm->mreq_itr_rlocs = p[0];
2228     }
2229   else
2230     {
2231       lcm->mreq_itr_rlocs = ~0;
2232     }
2233
2234   return 0;
2235 }
2236
2237 static clib_error_t *
2238 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2239                                         unformat_input_t * input,
2240                                         vlib_cli_command_t * cmd)
2241 {
2242   unformat_input_t _line_input, * line_input = &_line_input;
2243   u8 is_add = 1;
2244   u8 * locator_set_name = 0;
2245   clib_error_t * error = 0;
2246   int rv = 0;
2247   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, * a = &_a;
2248
2249   /* Get a line of input. */
2250   if (! unformat_user (input, unformat_line_input, line_input))
2251     return 0;
2252
2253   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2254     {
2255       if (unformat (line_input, "del"))
2256         is_add = 0;
2257       else if (unformat (line_input, "add %s", &locator_set_name))
2258         is_add = 1;
2259       else
2260         {
2261           error = unformat_parse_error(line_input);
2262           goto done;
2263         }
2264     }
2265
2266   a->is_add = is_add;
2267   a->locator_set_name = locator_set_name;
2268   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
2269   if (0 != rv)
2270     {
2271       error = clib_error_return(0, "failed to %s map-request itr-rlocs!",
2272                                 is_add ? "add" : "delete");
2273     }
2274
2275   vec_free(locator_set_name);
2276
2277  done:
2278   return error;
2279
2280 }
2281
2282 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
2283     .path = "lisp map-request itr-rlocs",
2284     .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
2285     .function = lisp_add_del_mreq_itr_rlocs_command_fn,
2286 };
2287
2288 static clib_error_t *
2289 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2290                                     unformat_input_t * input,
2291                                     vlib_cli_command_t * cmd)
2292 {
2293   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2294   locator_set_t * loc_set;
2295
2296   vlib_cli_output (vm, "%=20s", "itr-rlocs");
2297
2298   if (~0 == lcm->mreq_itr_rlocs)
2299     {
2300       return 0;
2301     }
2302
2303   loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
2304
2305   vlib_cli_output (vm, "%=20s", loc_set->name);
2306
2307   return 0;
2308 }
2309
2310 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
2311     .path = "show lisp map-request itr-rlocs",
2312     .short_help = "Shows map-request itr-rlocs",
2313     .function = lisp_show_mreq_itr_rlocs_command_fn,
2314 };
2315
2316 /* Statistics (not really errors) */
2317 #define foreach_lisp_cp_lookup_error           \
2318 _(DROP, "drop")                                \
2319 _(MAP_REQUESTS_SENT, "map-request sent")
2320
2321 static char * lisp_cp_lookup_error_strings[] = {
2322 #define _(sym,string) string,
2323   foreach_lisp_cp_lookup_error
2324 #undef _
2325 };
2326
2327 typedef enum
2328 {
2329 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2330     foreach_lisp_cp_lookup_error
2331 #undef _
2332     LISP_CP_LOOKUP_N_ERROR,
2333 } lisp_cp_lookup_error_t;
2334
2335 typedef enum
2336 {
2337   LISP_CP_LOOKUP_NEXT_DROP,
2338   LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
2339   LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
2340   LISP_CP_LOOKUP_N_NEXT,
2341 } lisp_cp_lookup_next_t;
2342
2343 typedef struct
2344 {
2345   gid_address_t dst_eid;
2346   ip_address_t map_resolver_ip;
2347 } lisp_cp_lookup_trace_t;
2348
2349 u8 *
2350 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2351 {
2352   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2353   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2354   lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
2355
2356   s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2357               format_ip_address, &t->map_resolver_ip, format_gid_address,
2358               &t->dst_eid);
2359   return s;
2360 }
2361
2362 int
2363 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2364                            ip_address_t * sloc)
2365 {
2366   ip_address_t * mrit;
2367
2368   if (vec_len(lcm->map_resolvers) == 0)
2369     {
2370       clib_warning("No map-resolver configured");
2371       return 0;
2372     }
2373
2374   /* find the first mr ip we have a route to and the ip of the
2375    * iface that has a route to it */
2376   vec_foreach(mrit, lcm->map_resolvers)
2377     {
2378       if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, mrit, sloc)) {
2379           ip_address_copy(mr_ip, mrit);
2380           return 1;
2381       }
2382     }
2383
2384   clib_warning("Can't find map-resolver and local interface ip!");
2385   return 0;
2386 }
2387
2388 static gid_address_t *
2389 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2390 {
2391   void * addr;
2392   u32 i;
2393   locator_t * loc;
2394   u32 * loc_indexp;
2395   ip_interface_address_t * ia = 0;
2396   gid_address_t gid_data, * gid = &gid_data;
2397   gid_address_t * rlocs = 0;
2398   ip_prefix_t * ippref = &gid_address_ippref (gid);
2399   ip_address_t * rloc = &ip_prefix_addr (ippref);
2400
2401   memset (gid, 0, sizeof (gid[0]));
2402   gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2403   for (i = 0; i < vec_len(loc_set->locator_indices); i++)
2404     {
2405       loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
2406       loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2407
2408       /* Add ipv4 locators first TODO sort them */
2409       foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2410                                     loc->sw_if_index, 1 /* unnumbered */,
2411       ({
2412         addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2413         ip_address_set (rloc, addr, IP4);
2414         ip_prefix_len (ippref) = 32;
2415         ip_prefix_normalize (ippref);
2416         vec_add1 (rlocs, gid[0]);
2417       }));
2418
2419       /* Add ipv6 locators */
2420       foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2421                                     loc->sw_if_index, 1 /* unnumbered */,
2422       ({
2423         addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2424         ip_address_set (rloc, addr, IP6);
2425         ip_prefix_len (ippref) = 128;
2426         ip_prefix_normalize (ippref);
2427         vec_add1 (rlocs, gid[0]);
2428       }));
2429     }
2430   return rlocs;
2431 }
2432
2433 static vlib_buffer_t *
2434 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
2435                                 gid_address_t * seid, gid_address_t * deid,
2436                                 locator_set_t * loc_set, ip_address_t * mr_ip,
2437                                 ip_address_t * sloc, u8 is_smr_invoked,
2438                                 u64 *nonce_res, u32 * bi_res)
2439 {
2440   vlib_buffer_t * b;
2441   u32 bi;
2442   gid_address_t * rlocs = 0;
2443
2444   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2445     {
2446       clib_warning ("Can't allocate buffer for Map-Request!");
2447       return 0;
2448     }
2449
2450   b = vlib_get_buffer (vm, bi);
2451
2452   /* leave some space for the encap headers */
2453   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2454
2455   /* get rlocs */
2456   rlocs = build_itr_rloc_list (lcm, loc_set);
2457
2458   /* put lisp msg */
2459   lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
2460
2461   /* push ecm: udp-ip-lisp */
2462   lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2463
2464   /* push outer ip header */
2465   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2466                        mr_ip);
2467
2468   bi_res[0] = bi;
2469
2470   vec_free(rlocs);
2471   return b;
2472 }
2473
2474 static void
2475 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
2476                                gid_address_t * seid, gid_address_t * deid,
2477                                u8 is_smr_invoked)
2478 {
2479   u32 next_index, bi = 0, * to_next, map_index;
2480   vlib_buffer_t * b;
2481   vlib_frame_t * f;
2482   u64 nonce = 0;
2483   locator_set_t * loc_set;
2484   mapping_t * map;
2485   pending_map_request_t * pmr;
2486   ip_address_t mr_ip, sloc;
2487   u32 ls_index;
2488
2489   /* get locator-set for seid */
2490   if (!lcm->lisp_pitr)
2491     {
2492       map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2493       if (map_index == ~0)
2494         {
2495           clib_warning("No local mapping found in eid-table for %U!",
2496                        format_gid_address, seid);
2497           return;
2498         }
2499
2500       map = pool_elt_at_index (lcm->mapping_pool, map_index);
2501
2502       if (!map->local)
2503         {
2504           clib_warning("Mapping found for src eid %U is not marked as local!",
2505                        format_gid_address, seid);
2506           return;
2507         }
2508       ls_index = map->locator_set_index;
2509     }
2510   else
2511     {
2512       map_index = lcm->pitr_map_index;
2513       map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2514       ls_index = map->locator_set_index;
2515     }
2516
2517   /* overwrite locator set if map-request itr-rlocs configured */
2518   if (~0 != lcm->mreq_itr_rlocs)
2519     {
2520       ls_index = lcm->mreq_itr_rlocs;
2521     }
2522
2523   loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2524
2525   /* get local iface ip to use in map-request */
2526   if (0 == get_mr_and_local_iface_ip (lcm, &mr_ip, &sloc))
2527     return;
2528
2529   /* build the encapsulated map request */
2530   b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set, &mr_ip,
2531                                       &sloc, is_smr_invoked, &nonce, &bi);
2532
2533   if (!b)
2534     return;
2535
2536   /* set fib index to default and lookup node */
2537   vnet_buffer(b)->sw_if_index[VLIB_TX] = 0;
2538   next_index = (ip_addr_version(&mr_ip) == IP4) ?
2539       ip4_lookup_node.index : ip6_lookup_node.index;
2540
2541   f = vlib_get_frame_to_node (vm, next_index);
2542
2543   /* Enqueue the packet */
2544   to_next = vlib_frame_vector_args (f);
2545   to_next[0] = bi;
2546   f->n_vectors = 1;
2547   vlib_put_frame_to_node (vm, next_index, f);
2548
2549   /* add map-request to pending requests table */
2550   pool_get(lcm->pending_map_requests_pool, pmr);
2551   gid_address_copy (&pmr->src, seid);
2552   gid_address_copy (&pmr->dst, deid);
2553   hash_set(lcm->pending_map_requests_by_nonce, nonce,
2554            pmr - lcm->pending_map_requests_pool);
2555 }
2556
2557 static void
2558 get_src_and_dst (void *hdr, ip_address_t * src, ip_address_t *dst)
2559 {
2560   ip4_header_t * ip4 = hdr;
2561   ip6_header_t * ip6;
2562
2563   if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2564     {
2565       ip_address_set(src, &ip4->src_address, IP4);
2566       ip_address_set(dst, &ip4->dst_address, IP4);
2567     }
2568   else
2569     {
2570       ip6 = hdr;
2571       ip_address_set(src, &ip6->src_address, IP6);
2572       ip_address_set(dst, &ip6->dst_address, IP6);
2573     }
2574 }
2575
2576 static u32
2577 lisp_get_vni_from_buffer (vlib_buffer_t * b, u8 version)
2578 {
2579   uword * vnip;
2580   u32 vni = ~0, table_id = ~0, fib_index;
2581   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
2582
2583   if (version == IP4)
2584     {
2585       ip4_fib_t * fib;
2586       ip4_main_t * im4 = &ip4_main;
2587       fib_index = vec_elt (im4->fib_index_by_sw_if_index,
2588                            vnet_buffer (b)->sw_if_index[VLIB_RX]);
2589       fib = find_ip4_fib_by_table_index_or_id (im4, fib_index,
2590                                                IP4_ROUTE_FLAG_FIB_INDEX);
2591       table_id = fib->table_id;
2592     }
2593   else
2594     {
2595       ip6_fib_t * fib;
2596       ip6_main_t * im6 = &ip6_main;
2597       fib_index = vec_elt (im6->fib_index_by_sw_if_index,
2598                            vnet_buffer (b)->sw_if_index[VLIB_RX]);
2599       fib = find_ip6_fib_by_table_index_or_id (im6, fib_index,
2600                                                IP6_ROUTE_FLAG_FIB_INDEX);
2601       table_id = fib->table_id;
2602     }
2603
2604   vnip = hash_get (lcm->vni_by_table_id, table_id);
2605   if (vnip)
2606     vni = vnip[0];
2607   else
2608     clib_warning ("vrf %d is not mapped to any vni!", table_id);
2609
2610   return vni;
2611 }
2612
2613 static uword
2614 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
2615               vlib_frame_t * from_frame)
2616 {
2617   u32 * from, * to_next_drop, di, si;
2618   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
2619   u32 pkts_mapped = 0;
2620   uword n_left_from, n_left_to_next_drop;
2621
2622   from = vlib_frame_vector_args (from_frame);
2623   n_left_from = from_frame->n_vectors;
2624
2625   while (n_left_from > 0)
2626     {
2627       vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2628                            to_next_drop, n_left_to_next_drop);
2629
2630       while (n_left_from > 0 && n_left_to_next_drop > 0)
2631         {
2632           u32 pi0, vni;
2633           vlib_buffer_t * p0;
2634           ip4_header_t * ip0;
2635           gid_address_t src, dst;
2636           ip_prefix_t * spref, * dpref;
2637
2638           gid_address_type (&src) = GID_ADDR_IP_PREFIX;
2639           spref = &gid_address_ippref(&src);
2640           gid_address_type (&dst) = GID_ADDR_IP_PREFIX;
2641           dpref = &gid_address_ippref(&dst);
2642
2643           pi0 = from[0];
2644           from += 1;
2645           n_left_from -= 1;
2646           to_next_drop[0] = pi0;
2647           to_next_drop += 1;
2648           n_left_to_next_drop -= 1;
2649
2650           p0 = vlib_get_buffer (vm, pi0);
2651           p0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2652
2653           /* src/dst eid pair */
2654           ip0 = vlib_buffer_get_current (p0);
2655           get_src_and_dst (ip0, &ip_prefix_addr(spref), &ip_prefix_addr(dpref));
2656           ip_prefix_len(spref) = ip_address_max_len (ip_prefix_version(spref));
2657           ip_prefix_len(dpref) = ip_address_max_len (ip_prefix_version(dpref));
2658
2659           vni = lisp_get_vni_from_buffer (p0, ip_prefix_version (spref));
2660           gid_address_vni (&dst) = vni;
2661           gid_address_vni (&src) = vni;
2662
2663           /* if we have remote mapping for destination already in map-chache
2664              add forwarding tunnel directly. If not send a map-request */
2665           di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
2666           if (~0 != di)
2667             {
2668               mapping_t * m =  vec_elt_at_index (lcm->mapping_pool, di);
2669               /* send a map-request also in case of negative mapping entry
2670                 with corresponding action */
2671               if (m->action == ACTION_SEND_MAP_REQUEST)
2672                 {
2673                   /* send map-request */
2674                   send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
2675                   pkts_mapped++;
2676                 }
2677               else
2678                 {
2679                   si =  gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2680                                                &src);
2681                   if (~0 != si)
2682                     {
2683                       dp_add_fwd_entry (lcm, si, di);
2684                     }
2685                 }
2686             }
2687           else
2688             {
2689               /* send map-request */
2690               send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
2691               pkts_mapped++;
2692             }
2693
2694           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
2695             {
2696               lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, p0,
2697                                                           sizeof(*tr));
2698
2699               memset(tr, 0, sizeof(*tr));
2700               gid_address_copy (&tr->dst_eid, &dst);
2701               if (vec_len(lcm->map_resolvers) > 0)
2702                 {
2703                   clib_memcpy (&tr->map_resolver_ip,
2704                                vec_elt_at_index(lcm->map_resolvers, 0),
2705                                sizeof(ip_address_t));
2706                 }
2707             }
2708           gid_address_free (&dst);
2709           gid_address_free (&src);
2710         }
2711
2712       vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
2713     }
2714   vlib_node_increment_counter (vm, node->node_index,
2715                                LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2716                                pkts_mapped);
2717   return from_frame->n_vectors;
2718 }
2719
2720 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
2721   .function = lisp_cp_lookup,
2722   .name = "lisp-cp-lookup",
2723   .vector_size = sizeof (u32),
2724   .format_trace = format_lisp_cp_lookup_trace,
2725   .type = VLIB_NODE_TYPE_INTERNAL,
2726
2727   .n_errors = LISP_CP_LOOKUP_N_ERROR,
2728   .error_strings = lisp_cp_lookup_error_strings,
2729
2730   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2731
2732   .next_nodes = {
2733       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2734       [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
2735       [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
2736   },
2737 };
2738
2739 /* lisp_cp_input statistics */
2740 #define foreach_lisp_cp_input_error                     \
2741 _(DROP, "drop")                                         \
2742 _(MAP_REPLIES_RECEIVED, "map-replies received")
2743
2744 static char * lisp_cp_input_error_strings[] = {
2745 #define _(sym,string) string,
2746   foreach_lisp_cp_input_error
2747 #undef _
2748 };
2749
2750 typedef enum
2751 {
2752 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
2753     foreach_lisp_cp_input_error
2754 #undef _
2755     LISP_CP_INPUT_N_ERROR,
2756 } lisp_cp_input_error_t;
2757
2758 typedef enum
2759 {
2760   LISP_CP_INPUT_NEXT_DROP,
2761   LISP_CP_INPUT_N_NEXT,
2762 } lisp_cp_input_next_t;
2763
2764 typedef struct
2765 {
2766   gid_address_t dst_eid;
2767   ip4_address_t map_resolver_ip;
2768 } lisp_cp_input_trace_t;
2769
2770 u8 *
2771 format_lisp_cp_input_trace (u8 * s, va_list * args)
2772 {
2773   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2774   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2775   CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
2776
2777   s = format (s, "LISP-CP-INPUT: TODO");
2778   return s;
2779 }
2780
2781 void
2782 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2783 {
2784   u32 len = 0, i, ttl, dst_map_index = 0;
2785   void * h;
2786   pending_map_request_t * pmr;
2787   locator_t probed;
2788   map_reply_hdr_t * mrep_hdr;
2789   u64 nonce;
2790   gid_address_t deid;
2791   uword * pmr_index;
2792   u8 authoritative, action;
2793   locator_t * locators = 0, * loc;
2794
2795   mrep_hdr = vlib_buffer_get_current (b);
2796
2797   /* Check pending requests table and nonce */
2798   nonce = MREP_NONCE(mrep_hdr);
2799   pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
2800   if (!pmr_index)
2801     {
2802       clib_warning("No pending map-request entry with nonce %lu!", nonce);
2803       return;
2804     }
2805   pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
2806
2807   vlib_buffer_pull (b, sizeof(*mrep_hdr));
2808
2809   for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
2810     {
2811
2812       h = vlib_buffer_get_current (b);
2813       ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
2814       action = MAP_REC_ACTION(h);
2815       authoritative = MAP_REC_AUTH(h);
2816
2817       len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
2818       if (len == ~0)
2819         {
2820           clib_warning ("Failed to parse mapping record!");
2821           vec_foreach (loc, locators)
2822             {
2823               locator_free (loc);
2824             }
2825           vec_free(locators);
2826           return;
2827         }
2828
2829       /* insert/update mappings cache */
2830       vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl, 1,
2831                                  &dst_map_index);
2832
2833       /* try to program forwarding only if mapping saved or updated*/
2834       if ((u32) ~0 != dst_map_index)
2835         lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
2836
2837       vec_free(locators);
2838     }
2839
2840   /* remove pending map request entry */
2841   hash_unset(lcm->pending_map_requests_by_nonce, nonce);
2842   pool_put(lcm->pending_map_requests_pool, pmr);
2843 }
2844
2845 void
2846 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
2847 {
2848   map_request_hdr_t * mreq_hdr;
2849   gid_address_t src, dst;
2850 //  u64 nonce;
2851   u32 i, len = 0;
2852   gid_address_t * itr_rlocs = 0, * rloc;
2853
2854   mreq_hdr = vlib_buffer_get_current (b);
2855   vlib_buffer_pull (b, sizeof(*mreq_hdr));
2856
2857 //  nonce = MREQ_NONCE(mreq_hdr);
2858
2859   if (!MREQ_SMR(mreq_hdr)) {
2860       clib_warning("Only SMR Map-Requests supported for now!");
2861       return;
2862   }
2863
2864   /* parse src eid */
2865   len = lisp_msg_parse_addr (b, &src);
2866   if (len == ~0)
2867     return;
2868
2869   /* for now we don't do anything with the itr's rlocs */
2870   len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
2871   if (len == ~0)
2872     return;
2873
2874   /* TODO: RLOCs are currently unused, so free them for now */
2875   vec_foreach (rloc, itr_rlocs)
2876     {
2877       gid_address_free (rloc);
2878     }
2879
2880   /* parse eid records and send SMR-invoked map-requests */
2881   for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
2882     {
2883       memset(&dst, 0, sizeof(dst));
2884       len = lisp_msg_parse_eid_rec (b, &dst);
2885       if (len == ~0)
2886         {
2887           clib_warning("Can't parse map-request EID-record");
2888           return;
2889         }
2890       /* send SMR-invoked map-requests */
2891       send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
2892     }
2893 }
2894
2895 static uword
2896 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2897                vlib_frame_t * from_frame)
2898 {
2899   u32 n_left_from, * from, * to_next_drop;
2900   lisp_msg_type_e type;
2901   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2902
2903   from = vlib_frame_vector_args (from_frame);
2904   n_left_from = from_frame->n_vectors;
2905
2906
2907   while (n_left_from > 0)
2908     {
2909       u32 n_left_to_next_drop;
2910
2911       vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
2912                            to_next_drop, n_left_to_next_drop);
2913       while (n_left_from > 0 && n_left_to_next_drop > 0)
2914         {
2915           u32 bi0;
2916           vlib_buffer_t * b0;
2917
2918           bi0 = from[0];
2919           from += 1;
2920           n_left_from -= 1;
2921           to_next_drop[0] = bi0;
2922           to_next_drop += 1;
2923           n_left_to_next_drop -= 1;
2924
2925           b0 = vlib_get_buffer (vm, bi0);
2926
2927           type = lisp_msg_type(vlib_buffer_get_current (b0));
2928           switch (type)
2929             {
2930             case LISP_MAP_REPLY:
2931               process_map_reply (lcm, b0);
2932               break;
2933             case LISP_MAP_REQUEST:
2934               process_map_request(vm, lcm, b0);
2935               break;
2936             default:
2937               clib_warning("Unsupported LISP message type %d", type);
2938               break;
2939             }
2940
2941           b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
2942
2943           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2944             {
2945
2946             }
2947         }
2948
2949       vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
2950     }
2951   return from_frame->n_vectors;
2952 }
2953
2954 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
2955   .function = lisp_cp_input,
2956   .name = "lisp-cp-input",
2957   .vector_size = sizeof (u32),
2958   .format_trace = format_lisp_cp_input_trace,
2959   .type = VLIB_NODE_TYPE_INTERNAL,
2960
2961   .n_errors = LISP_CP_INPUT_N_ERROR,
2962   .error_strings = lisp_cp_input_error_strings,
2963
2964   .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2965
2966   .next_nodes = {
2967       [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2968   },
2969 };
2970
2971 clib_error_t *
2972 lisp_cp_init (vlib_main_t *vm)
2973 {
2974   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2975   clib_error_t * error = 0;
2976
2977   if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
2978     return error;
2979
2980   lcm->im4 = &ip4_main;
2981   lcm->im6 = &ip6_main;
2982   lcm->vlib_main = vm;
2983   lcm->vnet_main = vnet_get_main();
2984   lcm->mreq_itr_rlocs = ~0;
2985   lcm->lisp_pitr = 0;
2986
2987   gid_dictionary_init (&lcm->mapping_index_by_gid);
2988
2989   /* default vrf mapped to vni 0 */
2990   hash_set(lcm->table_id_by_vni, 0, 0);
2991   hash_set(lcm->vni_by_table_id, 0, 0);
2992
2993   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2994                          lisp_cp_input_node.index, 1 /* is_ip4 */);
2995   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2996                          lisp_cp_input_node.index, 0 /* is_ip4 */);
2997
2998   return 0;
2999 }
3000
3001 VLIB_INIT_FUNCTION(lisp_cp_init);