Fix remote mapping CLI for unset v6 local eid
[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 static void
22 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index);
23
24 static void
25 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index);
26
27 static u8
28 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
29                   locator_t * new_locators);
30
31 /* Stores mapping in map-cache. It does NOT program data plane forwarding for
32  * remote/learned mappings. */
33 int
34 vnet_lisp_add_del_mapping (vnet_lisp_add_del_mapping_args_t * a,
35                            u32 * map_index_result)
36 {
37   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
38   u32 mi, * map_indexp, map_index, i;
39   mapping_t * m, * old_map;
40   u32 ** eid_indexes;
41
42   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->deid);
43   old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
44   if (a->is_add)
45     {
46       /* TODO check if overwriting and take appropriate actions */
47       if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid,
48                                                      &a->deid))
49         {
50           clib_warning("eid %U found in the eid-table", format_ip_address,
51                        &a->deid);
52           return VNET_API_ERROR_VALUE_EXIST;
53         }
54
55       pool_get(lcm->mapping_pool, m);
56       m->eid = a->deid;
57       m->locator_set_index = a->locator_set_index;
58       m->ttl = a->ttl;
59       m->action = a->action;
60       m->local = a->local;
61
62       map_index = m - lcm->mapping_pool;
63       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->deid, map_index,
64                               1);
65
66       if (pool_is_free_index(lcm->locator_set_pool, a->locator_set_index))
67         {
68           clib_warning("Locator set with index %d doesn't exist",
69                        a->locator_set_index);
70           return VNET_API_ERROR_INVALID_VALUE;
71         }
72
73       /* add eid to list of eids supported by locator-set */
74       vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
75       eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
76                                      a->locator_set_index);
77       vec_add1(eid_indexes[0], map_index);
78
79       if (a->local)
80         {
81           /* mark as local */
82           vec_add1(lcm->local_mappings_indexes, map_index);
83         }
84       map_index_result[0] = map_index;
85     }
86   else
87     {
88       if (mi == GID_LOOKUP_MISS)
89         {
90           clib_warning("eid %U not found in the eid-table", format_ip_address,
91                        &a->deid);
92           return VNET_API_ERROR_INVALID_VALUE;
93         }
94
95       /* clear locator-set to eids binding */
96       eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
97                                      a->locator_set_index);
98       for (i = 0; i < vec_len(eid_indexes[0]); i++)
99         {
100           map_indexp = vec_elt_at_index(eid_indexes[0], i);
101           if (map_indexp[0] == mi)
102               break;
103         }
104       vec_del1(eid_indexes[0], i);
105
106       /* remove local mark if needed */
107       m = pool_elt_at_index(lcm->mapping_pool, mi);
108       if (m->local)
109         {
110           u32 k, * lm_indexp;
111           for (k = 0; k < vec_len(lcm->local_mappings_indexes); k++)
112             {
113               lm_indexp = vec_elt_at_index(lcm->local_mappings_indexes, k);
114               if (lm_indexp[0] == mi)
115                 break;
116             }
117           vec_del1(lcm->local_mappings_indexes, k);
118         }
119       else
120         {
121           /* remove tunnel ??? */
122         }
123
124       /* remove mapping from dictionary */
125       gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->deid, 0, 0);
126       pool_put_index (lcm->mapping_pool, mi);
127     }
128
129   return 0;
130 }
131
132 /* Stores mapping in map-cache and programs data plane for local mappings. */
133 int
134 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
135                                  u32 * map_index_result)
136 {
137   uword * table_id, * refc;
138   u32 rv, vni;
139   vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
140   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
141
142   vni = gid_address_vni(&a->deid);
143
144   /* store/remove mapping from map-cache */
145   rv = vnet_lisp_add_del_mapping (a, map_index_result);
146   if (rv)
147     return rv;
148
149   table_id = hash_get(lcm->table_id_by_vni, vni);
150
151   if (!table_id)
152     {
153       clib_warning ("vni %d not associated to a vrf!", vni);
154       return VNET_API_ERROR_INVALID_VALUE;
155     }
156
157   refc = hash_get(lcm->dp_if_refcount_by_vni, vni);
158
159   /* enable/disable data-plane interface */
160   if (a->is_add)
161     {
162       /* create interface or update refcount */
163       if (!refc)
164         {
165           ai->is_add = 1;
166           ai->vni = vni;
167           ai->table_id = table_id[0];
168           vnet_lisp_gpe_add_del_iface (ai, 0);
169
170           /* counts the number of eids in a vni that use the interface */
171           hash_set(lcm->dp_if_refcount_by_vni, vni, 1);
172         }
173       else
174         {
175           refc[0]++;
176         }
177     }
178   else
179     {
180       /* since this is a remove for an existing eid, the iface should exist */
181       ASSERT(refc != 0);
182       refc[0]--;
183
184       /* remove iface if needed */
185       if (refc[0] == 0)
186         {
187           ai->is_add = 0;
188           ai->vni = vni;
189           ai->table_id = table_id[0];
190           vnet_lisp_gpe_add_del_iface (ai, 0);
191           hash_unset (lcm->dp_if_refcount_by_vni, vni);
192         }
193     }
194
195   return rv;
196 }
197
198 static clib_error_t *
199 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
200                                    vlib_cli_command_t * cmd)
201 {
202   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
203   unformat_input_t _line_input, * line_input = &_line_input;
204   u8 is_add = 1;
205   gid_address_t eid;
206   ip_prefix_t * prefp = &gid_address_ippref(&eid);
207   gid_address_t * eids = 0;
208   clib_error_t * error = 0;
209   u8 * locator_set_name = 0;
210   u32 locator_set_index = 0, map_index = 0;
211   uword * p;
212   vnet_lisp_add_del_mapping_args_t _a, * a = &_a;
213
214   gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
215
216   /* Get a line of input. */
217   if (! unformat_user (input, unformat_line_input, line_input))
218     return 0;
219
220   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
221     {
222       if (unformat (line_input, "add"))
223         is_add = 1;
224       else if (unformat (line_input, "del"))
225         is_add = 0;
226       else if (unformat (line_input, "eid %U", unformat_ip_prefix, prefp))
227         {
228           vec_add1(eids, eid);
229         }
230       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
231         {
232           p = hash_get_mem(lcm->locator_set_index_by_name, locator_set_name);
233           if (!p)
234             {
235               error = clib_error_return(0, "locator-set %s doesn't exist",
236                                         locator_set_name);
237               goto done;
238             }
239           locator_set_index = p[0];
240         }
241       else
242         {
243           error = unformat_parse_error(line_input);
244           goto done;
245         }
246     }
247
248   /* XXX treat batch configuration */
249   a->deid = eid;
250   a->is_add = is_add;
251   a->locator_set_index = locator_set_index;
252   a->local = 1;
253
254   vnet_lisp_add_del_local_mapping (a, &map_index);
255  done:
256   vec_free(eids);
257   if (locator_set_name)
258     vec_free (locator_set_name);
259   return error;
260 }
261
262 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
263     .path = "lisp eid-table",
264     .short_help = "lisp eid-table add/del eid <eid> locator-set <locator-set>",
265     .function = lisp_add_del_local_eid_command_fn,
266 };
267
268 static int
269 lisp_add_del_negative_static_mapping (gid_address_t * deid,
270     vnet_lisp_add_del_locator_set_args_t * ls, u8 action, u8 is_add)
271 {
272   uword * p;
273   mapping_t * map;
274   u32 mi = ~0;
275   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
276   uword * refc;
277   vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
278   int rv = 0;
279   u32 ls_index = 0, dst_map_index;
280   vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
281
282   memset (dm_args, 0, sizeof (dm_args[0]));
283   u32 vni = gid_address_vni (deid);
284   refc = hash_get (lcm->dp_if_refcount_by_vni, vni);
285
286   p = hash_get (lcm->table_id_by_vni, vni);
287   if (!p)
288     {
289       clib_warning ("vni %d not associated to a vrf!", vni);
290       return VNET_API_ERROR_INVALID_VALUE;
291     }
292
293   if (is_add)
294     {
295       vnet_lisp_add_del_locator_set (ls, &ls_index);
296       /* add mapping */
297       gid_address_copy (&dm_args->deid, deid);
298       dm_args->is_add = 1;
299       dm_args->action = action;
300       dm_args->locator_set_index = ls_index;
301
302       /* create interface or update refcount */
303       if (!refc)
304         {
305           vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
306           ai->is_add = 1;
307           ai->vni = vni;
308           ai->table_id = p[0];
309           vnet_lisp_gpe_add_del_iface (ai, 0);
310
311           /* counts the number of eids in a vni that use the interface */
312           hash_set (lcm->dp_if_refcount_by_vni, vni, 1);
313         }
314       else
315         refc[0]++;
316
317       rv = vnet_lisp_add_del_local_mapping (dm_args, &dst_map_index);
318       if (!rv)
319         add_fwd_entry (lcm, lcm->pitr_map_index, dst_map_index);
320     }
321   else
322     {
323       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
324       if ((u32)~0 == mi)
325         {
326           clib_warning ("eid %U marked for removal, but not found in "
327                         "map-cache!", unformat_gid_address, deid);
328           return VNET_API_ERROR_INVALID_VALUE;
329         }
330
331       /* delete forwarding entry */
332       del_fwd_entry (lcm, 0, mi);
333
334       dm_args->is_add = 0;
335       gid_address_copy (&dm_args->deid, deid);
336       map = pool_elt_at_index (lcm->mapping_pool, mi);
337       dm_args->locator_set_index = map->locator_set_index;
338
339       /* delete mapping associated to fwd entry */
340       vnet_lisp_add_del_mapping (dm_args, 0);
341
342       refc = hash_get (lcm->dp_if_refcount_by_vni, vni);
343       ASSERT(refc != 0);
344       refc[0]--;
345
346       /* remove iface if needed */
347       if (refc[0] == 0)
348         {
349           ai->is_add = 0;
350           ai->vni = vni;
351           ai->table_id = p[0];
352           vnet_lisp_gpe_add_del_iface (ai, 0);
353           hash_unset (lcm->dp_if_refcount_by_vni, vni);
354         }
355     }
356   return rv;
357 }
358
359 /**
360  * Adds/removes/updates static remote mapping.
361  *
362  * This function also modifies forwarding entries if needed.
363  *
364  * @param deid destination EID
365  * @param seid source EID
366  * @param rlocs vector of remote locators
367  * @param action action for negative map-reply
368  * @param is_add add mapping if non-zero, delete otherwise
369  * @param del_all if set, delete all remote mappings
370  * @return return code
371  */
372 int
373 vnet_lisp_add_del_remote_mapping (gid_address_t * deid, gid_address_t * seid,
374                                   ip_address_t * rlocs, u8 action, u8 is_add,
375                                   u8 del_all)
376 {
377   vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
378   vnet_lisp_add_del_mapping_args_t _sm_args, * sm_args = &_sm_args;
379   vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
380   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
381   u32 mi, ls_index = 0, dst_map_index, src_map_index;
382   locator_t loc;
383   ip_address_t * dl;
384   int rc = -1;
385
386   if (del_all)
387     return vnet_lisp_clear_all_remote_mappings ();
388
389   memset (sm_args, 0, sizeof (sm_args[0]));
390   memset (dm_args, 0, sizeof (dm_args[0]));
391   memset (ls, 0, sizeof (ls[0]));
392
393   /* prepare remote locator set */
394   vec_foreach (dl, rlocs)
395     {
396       memset (&loc, 0, sizeof (loc));
397       gid_address_ip (&loc.address) = dl[0];
398       vec_add1 (ls->locators, loc);
399     }
400   src_map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
401
402   mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
403   /* new mapping */
404   if ((u32)~0 == mi)
405     {
406       ls->is_add = 1;
407       ls->index = ~0;
408
409       /* process a negative mapping */
410       if (0 == vec_len (rlocs))
411         return lisp_add_del_negative_static_mapping (deid, ls,
412                                                      action, is_add);
413
414       if ((u32)~0 == src_map_index)
415         {
416           clib_warning ("seid %U not found!", format_gid_address, seid);
417           goto done;
418         }
419
420       if (!is_add)
421         {
422           clib_warning ("deid %U marked for removal but not "
423                         "found!", format_gid_address, deid);
424           goto done;
425         }
426
427       vnet_lisp_add_del_locator_set (ls, &ls_index);
428
429       /* add mapping */
430       gid_address_copy (&dm_args->deid, deid);
431       dm_args->is_add = 1;
432       dm_args->action = action;
433       dm_args->locator_set_index = ls_index;
434       vnet_lisp_add_del_mapping (dm_args, &dst_map_index);
435
436       /* add fwd tunnel */
437       add_fwd_entry (lcm, src_map_index, dst_map_index);
438     }
439   else
440     {
441       /* delete mapping */
442       if (!is_add)
443         {
444           /* delete forwarding entry */
445           del_fwd_entry (lcm, 0, mi);
446           dm_args->is_add = 0;
447           gid_address_copy (&dm_args->deid, deid);
448           mapping_t * map = pool_elt_at_index (lcm->mapping_pool, mi);
449           dm_args->locator_set_index = map->locator_set_index;
450
451           /* delete mapping associated to fwd entry */
452           vnet_lisp_add_del_mapping (dm_args, 0);
453
454           ls->is_add = 0;
455           ls->index = map->locator_set_index;
456           /* delete locator set */
457           vnet_lisp_add_del_locator_set (ls, 0);
458         }
459       /* update existing locator set */
460       else
461         {
462           if ((u32)~0 == src_map_index)
463             {
464               clib_warning ("seid %U not found!", format_gid_address, seid);
465               goto done;
466             }
467
468           mapping_t * old_map;
469           locator_set_t * old_ls;
470           old_map = pool_elt_at_index (lcm->mapping_pool, mi);
471
472           /* update mapping attributes */
473           old_map->action = action;
474
475           old_ls = pool_elt_at_index(lcm->locator_set_pool,
476                                      old_map->locator_set_index);
477           if (compare_locators (lcm, old_ls->locator_indices,
478                                 ls->locators))
479             {
480               /* set locator-set index to overwrite */
481               ls->is_add = 1;
482               ls->index = old_map->locator_set_index;
483               vnet_lisp_add_del_locator_set (ls, 0);
484               add_fwd_entry (lcm, src_map_index, mi);
485             }
486         }
487     }
488   /* success */
489   rc = 0;
490 done:
491   vec_free (ls->locators);
492   return rc;
493 }
494
495 int
496 vnet_lisp_clear_all_remote_mappings (void)
497 {
498   int rv = 0;
499   u32 mi, * map_indices = 0, * map_indexp;
500   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
501   vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
502   vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
503
504   pool_foreach_index (mi, lcm->mapping_pool,
505     ({
506       vec_add1 (map_indices, mi);
507     }));
508
509   vec_foreach (map_indexp, map_indices)
510     {
511       mapping_t * map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
512       if (!map->local)
513         {
514           del_fwd_entry (lcm, 0, map_indexp[0]);
515
516           dm_args->is_add = 0;
517           gid_address_copy (&dm_args->deid, &map->eid);
518           dm_args->locator_set_index = map->locator_set_index;
519
520           /* delete mapping associated to fwd entry */
521           vnet_lisp_add_del_mapping (dm_args, 0);
522
523           ls->is_add = 0;
524           ls->local = 0;
525           ls->index = map->locator_set_index;
526           /* delete locator set */
527           rv = vnet_lisp_add_del_locator_set (ls, 0);
528           if (rv != 0)
529             goto cleanup;
530         }
531     }
532
533 cleanup:
534   if (map_indices)
535     vec_free (map_indices);
536   return rv;
537 }
538
539 /**
540  * Handler for add/del remote mapping CLI.
541  *
542  * @param vm vlib context
543  * @param input input from user
544  * @param cmd cmd
545  * @return pointer to clib error structure
546  */
547 static clib_error_t *
548 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
549                                         unformat_input_t * input,
550                                         vlib_cli_command_t * cmd)
551 {
552   clib_error_t * error = 0;
553   unformat_input_t _line_input, * line_input = &_line_input;
554   u8 is_add = 1, del_all = 0;
555   ip_address_t rloc, * rlocs = 0;
556   ip_prefix_t * deid_ippref, * seid_ippref;
557   gid_address_t seid, deid;
558   u8 deid_set = 0, seid_set = 0;
559   u8 * s = 0;
560   u32 vni, action = ~0;
561
562   /* Get a line of input. */
563   if (! unformat_user (input, unformat_line_input, line_input))
564     return 0;
565
566   memset (&deid, 0, sizeof (deid));
567   memset (&seid, 0, sizeof (seid));
568
569   seid_ippref = &gid_address_ippref(&seid);
570   deid_ippref = &gid_address_ippref(&deid);
571
572   gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
573   gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
574
575   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
576     {
577       if (unformat (line_input, "del-all"))
578         del_all = 1;
579       else if (unformat (line_input, "del"))
580         is_add = 0;
581       else if (unformat (line_input, "add"))
582         ;
583       else if (unformat (line_input, "deid %U",
584                          unformat_ip_prefix, deid_ippref))
585         {
586           deid_set = 1;
587         }
588       else if (unformat (line_input, "vni %u", &vni))
589         {
590           gid_address_set_vni (&seid, vni);
591           gid_address_set_vni (&deid, vni);
592         }
593       else if (unformat (line_input, "seid %U",
594                          unformat_ip_prefix, seid_ippref))
595         {
596           seid_set = 1;
597         }
598       else if (unformat (line_input, "rloc %U", unformat_ip_address, &rloc))
599         vec_add1 (rlocs, rloc);
600       else if (unformat (line_input, "action %s", &s))
601         {
602           if (!strcmp ((char *)s, "no-action"))
603             action = ACTION_NONE;
604           if (!strcmp ((char *)s, "natively-forward"))
605             action = ACTION_NATIVELY_FORWARDED;
606           if (!strcmp ((char *)s, "send-map-request"))
607             action = ACTION_SEND_MAP_REQUEST;
608           else if (!strcmp ((char *)s, "drop"))
609             action = ACTION_DROP;
610           else
611             {
612               clib_warning ("invalid action: '%s'", s);
613               goto done;
614             }
615         }
616       else
617         {
618           clib_warning ("parse error");
619           goto done;
620         }
621     }
622
623   if (!del_all)
624     {
625       if (!deid_set)
626         {
627           clib_warning ("missing deid!");
628           goto done;
629         }
630
631       /* if seid not set, make sure the ip version is the same as that of the
632        * deid. This ensures the seid to be configured will be either 0/0 or
633        * ::/0 */
634       if (!seid_set)
635         ip_prefix_version(seid_ippref) = ip_prefix_version(deid_ippref);
636
637       if (is_add &&
638           (ip_prefix_version (deid_ippref) != ip_prefix_version(seid_ippref)))
639         {
640           clib_warning ("source and destination EIDs are not"
641                         " in the same IP family!");
642           goto done;
643         }
644
645       if (is_add && (~0 == action)
646           && 0 == vec_len (rlocs))
647         {
648           clib_warning ("no action set for negative map-reply!");
649           goto done;
650         }
651     }
652
653   int rv = vnet_lisp_add_del_remote_mapping (&deid, &seid, rlocs,
654                                              action, is_add, del_all);
655   if (rv)
656     clib_warning ("failed to %s remote mapping!",
657                   is_add ? "add" : "delete");
658
659 done:
660   unformat_free (line_input);
661   if (s)
662     vec_free (s);
663   return error;
664 }
665
666 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
667     .path = "lisp remote-mapping",
668     .short_help = "lisp remote-mapping add|del [del-all] vni <vni>"
669      "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
670      "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
671     .function = lisp_add_del_remote_mapping_command_fn,
672 };
673
674 static clib_error_t *
675 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
676                                     unformat_input_t * input,
677                                     vlib_cli_command_t * cmd)
678 {
679   ip_address_t * addr;
680   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
681
682   vec_foreach (addr, lcm->map_resolvers)
683     {
684       vlib_cli_output (vm, "%U", format_ip_address, addr);
685     }
686   return 0;
687 }
688
689 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
690     .path = "show lisp map-resolvers",
691     .short_help = "show lisp map-resolvers",
692     .function = lisp_show_map_resolvers_command_fn,
693 };
694
695 int
696 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
697 {
698   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
699   u32 locator_set_index = ~0;
700   mapping_t * m;
701   uword * p;
702
703   p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
704   if (!p)
705     {
706       clib_warning ("locator-set %v doesn't exist", locator_set_name);
707       return -1;
708     }
709   locator_set_index = p[0];
710
711   if (is_add)
712     {
713       pool_get (lcm->mapping_pool, m);
714       m->locator_set_index = locator_set_index;
715       m->local = 1;
716       lcm->pitr_map_index = m - lcm->mapping_pool;
717
718       /* enable pitr mode */
719       lcm->lisp_pitr = 1;
720     }
721   else
722     {
723       /* remove pitr mapping */
724       pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
725
726       /* disable pitr mode */
727       lcm->lisp_pitr = 0;
728     }
729   return 0;
730 }
731
732 static clib_error_t *
733 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
734                                       unformat_input_t * input,
735                                       vlib_cli_command_t * cmd)
736 {
737   u8 locator_name_set = 0;
738   u8 * locator_set_name = 0;
739   u8 is_add = 1;
740   unformat_input_t _line_input, * line_input = &_line_input;
741
742   /* Get a line of input. */
743   if (! unformat_user (input, unformat_line_input, line_input))
744     return 0;
745
746   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
747     {
748       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
749         locator_name_set = 1;
750       else if (unformat (line_input, "disable"))
751         is_add = 0;
752       else
753         return clib_error_return (0, "parse error");
754     }
755
756   if (!locator_name_set)
757     {
758       clib_warning ("No locator set specified!");
759       goto done;
760     }
761   vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
762
763 done:
764   if (locator_set_name)
765     vec_free (locator_set_name);
766   return 0;
767 }
768
769 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
770     .path = "lisp pitr",
771     .short_help = "lisp pitr [disable] ls <locator-set-name>",
772     .function = lisp_pitr_set_locator_set_command_fn,
773 };
774
775 static clib_error_t *
776 lisp_show_local_eid_table_command_fn (vlib_main_t * vm,
777                                       unformat_input_t * input,
778                                       vlib_cli_command_t * cmd)
779 {
780   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
781   mapping_t * mapit;
782
783   vlib_cli_output (vm, "%=20s%=16s", "EID", "Locator");
784   pool_foreach (mapit, lcm->mapping_pool,
785   ({
786     u8 * msg = 0;
787     locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
788                                             mapit->locator_set_index);
789     vlib_cli_output (vm, "%-16U%16v", format_gid_address, &mapit->eid,
790                      ls->name);
791     vec_free (msg);
792   }));
793
794   return 0;
795 }
796
797 VLIB_CLI_COMMAND (lisp_cp_show_local_eid_table_command) = {
798     .path = "show lisp eid-table",
799     .short_help = "Shows local EID table",
800     .function = lisp_show_local_eid_table_command_fn,
801 };
802
803 /* cleans locator to locator-set data and removes locators not part of
804  * any locator-set */
805 static void
806 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
807 {
808   u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
809   locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
810   for (i = 0; i < vec_len(ls->locator_indices); i++)
811     {
812       loc_indexp = vec_elt_at_index(ls->locator_indices, i);
813       ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
814                                     loc_indexp[0]);
815       for (j = 0; j < vec_len(ls_indexes[0]); j++)
816         {
817           ls_indexp = vec_elt_at_index(ls_indexes[0], j);
818           if (ls_indexp[0] == lsi)
819             break;
820         }
821
822       /* delete index for removed locator-set*/
823       vec_del1(ls_indexes[0], j);
824
825       /* delete locator if it's part of no locator-set */
826       if (vec_len (ls_indexes[0]) == 0)
827         {
828           pool_put_index (lcm->locator_pool, loc_indexp[0]);
829           vec_add1 (to_be_deleted, i);
830         }
831     }
832
833   if (to_be_deleted)
834     {
835       for (i = 0; i < vec_len (to_be_deleted); i++)
836         {
837           loc_indexp = vec_elt_at_index (to_be_deleted, i);
838           vec_del1 (ls->locator_indices, loc_indexp[0]);
839         }
840       vec_free (to_be_deleted);
841     }
842 }
843
844 static inline
845 uword *get_locator_set_index(vnet_lisp_add_del_locator_set_args_t * a,
846                              uword * p)
847 {
848   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
849
850   ASSERT(a != NULL);
851   ASSERT(p != NULL);
852
853   /* find locator-set */
854   if (a->local)
855     {
856       p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
857     }
858   else
859     {
860       *p = a->index;
861     }
862
863   return p;
864 }
865
866 static inline
867 int is_locator_in_locator_set(lisp_cp_main_t * lcm, locator_set_t * ls,
868                               locator_t * loc)
869 {
870   locator_t * itloc;
871   u32 * locit;
872
873   ASSERT(ls != NULL);
874   ASSERT(loc != NULL);
875
876   vec_foreach(locit, ls->locator_indices)
877     {
878       itloc = pool_elt_at_index(lcm->locator_pool, locit[0]);
879       if (itloc->sw_if_index == loc->sw_if_index ||
880           !gid_address_cmp(&itloc->address, &loc->address))
881         {
882           clib_warning("Duplicate locator");
883           return VNET_API_ERROR_VALUE_EXIST;
884         }
885     }
886
887   return 0;
888 }
889
890 static inline
891 void remove_locator_from_locator_set(locator_set_t * ls, u32 * locit,
892                                      u32 ls_index, u32 loc_id)
893 {
894   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
895   u32 ** ls_indexes = NULL;
896
897   ASSERT(ls != NULL);
898   ASSERT(locit != NULL);
899
900   ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
901                                 locit[0]);
902   pool_put_index(lcm->locator_pool, locit[0]);
903   vec_del1(ls->locator_indices, loc_id);
904   vec_del1(ls_indexes[0], ls_index);
905 }
906
907 int
908 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
909                            locator_set_t * ls, u32 * ls_result)
910 {
911   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
912   locator_t * loc = NULL, *itloc = NULL;
913   uword _p = (u32)~0, * p = &_p;
914   u32 loc_index = ~0, ls_index = ~0, * locit = NULL, ** ls_indexes = NULL;
915   u32 loc_id = ~0;
916   int ret = 0;
917
918   ASSERT(a != NULL);
919
920   p = get_locator_set_index(a, p);
921   if (!p)
922     {
923       clib_warning("locator-set %v doesn't exist", a->name);
924       return VNET_API_ERROR_INVALID_ARGUMENT;
925     }
926
927   if (ls == 0)
928     {
929       ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
930       if (!ls)
931         {
932           clib_warning("locator-set %d to be overwritten doesn't exist!",
933                        p[0]);
934           return VNET_API_ERROR_INVALID_ARGUMENT;
935         }
936     }
937
938   if (a->is_add)
939     {
940
941         if (ls_result)
942           ls_result[0] = p[0];
943
944         /* allocate locators */
945         vec_foreach (itloc, a->locators)
946           {
947             ret = is_locator_in_locator_set(lcm, ls, itloc);
948             if (0 != ret)
949               {
950                 return ret;
951               }
952
953             pool_get(lcm->locator_pool, loc);
954             loc[0] = itloc[0];
955             loc_index = loc - lcm->locator_pool;
956
957             vec_add1(ls->locator_indices, loc_index);
958
959             vec_validate (lcm->locator_to_locator_sets, loc_index);
960             ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
961                                           loc_index);
962             vec_add1(ls_indexes[0], ls_index);
963           }
964       }
965     else
966       {
967         ls_index = p[0];
968
969         itloc = a->locators;
970         loc_id = 0;
971         vec_foreach (locit, ls->locator_indices)
972           {
973             loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
974
975             if (loc->local && loc->sw_if_index == itloc->sw_if_index)
976               {
977                 remove_locator_from_locator_set(ls, locit,
978                                                 ls_index, loc_id);
979               }
980             if (0 == loc->local &&
981                 !gid_address_cmp(&loc->address, &itloc->address))
982               {
983                 remove_locator_from_locator_set(ls, locit,
984                                                 ls_index, loc_id);
985               }
986
987             loc_id++;
988           }
989       }
990
991   return 0;
992 }
993
994 int
995 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
996                                u32 * ls_result)
997 {
998   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
999   locator_set_t * ls;
1000   uword _p = (u32)~0, * p = &_p;
1001   u32 ls_index;
1002   u32 ** eid_indexes;
1003   int ret = 0;
1004
1005   if (a->is_add)
1006     {
1007       p = get_locator_set_index(a, p);
1008
1009       /* overwrite */
1010       if (p && p[0] != (u32)~0)
1011         {
1012           ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1013           if (!ls)
1014             {
1015               clib_warning("locator-set %d to be overwritten doesn't exist!",
1016                            p[0]);
1017               return -1;
1018             }
1019
1020           /* clean locator to locator-set vectors and remove locators if
1021            * they're not part of another locator-set */
1022           clean_locator_to_locator_set (lcm, p[0]);
1023
1024           /* remove locator indices from locator set */
1025           vec_free(ls->locator_indices);
1026
1027           ls_index = p[0];
1028
1029           if (ls_result)
1030             ls_result[0] = p[0];
1031         }
1032       /* new locator-set */
1033       else
1034         {
1035           pool_get(lcm->locator_set_pool, ls);
1036           memset(ls, 0, sizeof(*ls));
1037           ls_index = ls - lcm->locator_set_pool;
1038
1039           if (a->local)
1040             {
1041               ls->name = vec_dup(a->name);
1042
1043               if (!lcm->locator_set_index_by_name)
1044                 lcm->locator_set_index_by_name = hash_create_vec(
1045                     /* size */0, sizeof(ls->name[0]), sizeof(uword));
1046               hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
1047
1048               /* mark as local locator-set */
1049               vec_add1(lcm->local_locator_set_indexes, ls_index);
1050             }
1051           ls->local = a->local;
1052           if (ls_result)
1053             ls_result[0] = ls_index;
1054         }
1055
1056       ret = vnet_lisp_add_del_locator(a, ls, NULL);
1057       if (0 != ret)
1058         {
1059           return ret;
1060         }
1061     }
1062   else
1063     {
1064       p = get_locator_set_index(a, p);
1065       if (!p)
1066         {
1067           clib_warning("locator-set %v doesn't exists", a->name);
1068           return -1;
1069         }
1070
1071       ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1072       if (!ls)
1073         {
1074           clib_warning("locator-set with index %d doesn't exists", p[0]);
1075           return -1;
1076         }
1077
1078       if (vec_len(lcm->locator_set_to_eids) != 0)
1079       {
1080           eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
1081           if (vec_len(eid_indexes[0]) != 0)
1082           {
1083               clib_warning ("Can't delete a locator that supports a mapping!");
1084               return -1;
1085           }
1086       }
1087
1088       /* clean locator to locator-sets data */
1089       clean_locator_to_locator_set (lcm, p[0]);
1090
1091       if (ls->local)
1092         {
1093           u32 it, lsi;
1094
1095           vec_foreach_index(it, lcm->local_locator_set_indexes)
1096           {
1097             lsi = vec_elt(lcm->local_locator_set_indexes, it);
1098             if (lsi == p[0])
1099               {
1100                 vec_del1(lcm->local_locator_set_indexes, it);
1101                 break;
1102               }
1103           }
1104           hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
1105         }
1106       vec_free(ls->name);
1107       vec_free(ls->locator_indices);
1108       pool_put(lcm->locator_set_pool, ls);
1109     }
1110   return 0;
1111 }
1112
1113 clib_error_t *
1114 vnet_lisp_enable_disable (u8 is_enabled)
1115 {
1116   vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
1117   uword * table_id, * refc;
1118   u32 i;
1119   clib_error_t * error = 0;
1120   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1121   vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1122
1123   a->is_en = is_enabled;
1124   error = vnet_lisp_gpe_enable_disable (a);
1125   if (error)
1126     {
1127       return clib_error_return (0, "failed to %s data-plane!",
1128                                 a->is_en ? "enable" : "disable");
1129     }
1130
1131   if (is_enabled)
1132     {
1133       /* enable all ifaces */
1134       for (i = 0; i < vec_len (lcm->local_mappings_indexes); i++)
1135         {
1136           mapping_t * m = vec_elt_at_index (lcm->mapping_pool, i);
1137           ai->is_add = 1;
1138           ai->vni = gid_address_vni (&m->eid);
1139
1140           refc = hash_get (lcm->dp_if_refcount_by_vni, ai->vni);
1141           if (!refc)
1142             {
1143               table_id = hash_get (lcm->table_id_by_vni, ai->vni);
1144               if (table_id)
1145                 {
1146                   ai->table_id = table_id[0];
1147                   /* enables interface and adds defaults */
1148                   vnet_lisp_gpe_add_del_iface (ai, 0);
1149                 }
1150               else
1151                 return clib_error_return (0, "no table_id found for vni %u!",
1152                                           ai->vni);
1153
1154               hash_set (lcm->dp_if_refcount_by_vni, ai->vni, 1);
1155             }
1156           else
1157             {
1158               refc[0]++;
1159             }
1160         }
1161     }
1162   else
1163     {
1164       /* clear refcount table */
1165       hash_free (lcm->dp_if_refcount_by_vni);
1166       hash_free (lcm->fwd_entry_by_mapping_index);
1167       pool_free (lcm->fwd_entry_pool);
1168     }
1169
1170   /* update global flag */
1171   lcm->is_enabled = is_enabled;
1172
1173   return 0;
1174 }
1175
1176 static clib_error_t *
1177 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1178                                    vlib_cli_command_t * cmd)
1179 {
1180   unformat_input_t _line_input, * line_input = &_line_input;
1181   u8 is_enabled = 0;
1182   u8 is_set = 0;
1183
1184   /* Get a line of input. */
1185   if (! unformat_user (input, unformat_line_input, line_input))
1186     return 0;
1187
1188   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1189     {
1190       if (unformat (line_input, "enable"))
1191         {
1192           is_set = 1;
1193           is_enabled = 1;
1194         }
1195       else if (unformat (line_input, "disable"))
1196         is_set = 1;
1197       else
1198         {
1199           return clib_error_return (0, "parse error: '%U'",
1200                                    format_unformat_error, line_input);
1201         }
1202     }
1203
1204   if (!is_set)
1205       return clib_error_return (0, "state not set");
1206
1207   return vnet_lisp_enable_disable (is_enabled);
1208 }
1209
1210 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1211     .path = "lisp",
1212     .short_help = "lisp [enable|disable]",
1213     .function = lisp_enable_disable_command_fn,
1214 };
1215
1216 u8
1217 vnet_lisp_enable_disable_status (void)
1218 {
1219   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1220   return lcm->is_enabled;
1221 }
1222
1223 static u8 *
1224 format_lisp_status (u8 * s, va_list * args)
1225 {
1226   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1227   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1228 }
1229
1230 static clib_error_t *
1231 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1232                              vlib_cli_command_t * cmd)
1233 {
1234   u8 * msg = 0;
1235   msg = format (msg, "feature: %U\ngpe: %U\n",
1236                 format_lisp_status, format_vnet_lisp_gpe_status);
1237   vlib_cli_output (vm, "%v", msg);
1238   vec_free (msg);
1239   return 0;
1240 }
1241
1242 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1243     .path = "show lisp status",
1244     .short_help = "show lisp status",
1245     .function = lisp_show_status_command_fn,
1246 };
1247 static clib_error_t *
1248 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1249                                      vlib_cli_command_t * cmd)
1250 {
1251   lisp_gpe_main_t * lgm = &lisp_gpe_main;
1252   vnet_main_t * vnm = lgm->vnet_main;
1253   unformat_input_t _line_input, * line_input = &_line_input;
1254   u8 is_add = 1;
1255   clib_error_t * error = 0;
1256   u8 * locator_set_name = 0;
1257   locator_t locator, * locators = 0;
1258   vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1259   u32 ls_index = 0;
1260
1261   memset(&locator, 0, sizeof(locator));
1262   memset(a, 0, sizeof(a[0]));
1263
1264   /* Get a line of input. */
1265   if (! unformat_user (input, unformat_line_input, line_input))
1266     return 0;
1267
1268   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1269     {
1270       if (unformat (line_input, "add %_%v%_", &locator_set_name))
1271         is_add = 1;
1272       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1273         is_add = 0;
1274       else if (unformat (line_input, "iface %U p %d w %d",
1275                          unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
1276                          &locator.priority, &locator.weight))
1277         {
1278           locator.local = 1;
1279           vec_add1(locators, locator);
1280         }
1281       else
1282         {
1283           error = unformat_parse_error(line_input);
1284           goto done;
1285         }
1286     }
1287
1288   a->name = locator_set_name;
1289   a->locators = locators;
1290   a->is_add = is_add;
1291   a->local = 1;
1292
1293   vnet_lisp_add_del_locator_set(a, &ls_index);
1294
1295  done:
1296   vec_free(locators);
1297   if (locator_set_name)
1298     vec_free (locator_set_name);
1299   return error;
1300 }
1301
1302 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
1303     .path = "lisp locator-set",
1304     .short_help = "lisp locator-set add/del <name> iface <iface-name> "
1305         "p <priority> w <weight>",
1306     .function = lisp_add_del_locator_set_command_fn,
1307 };
1308
1309 static clib_error_t *
1310 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1311                                       unformat_input_t * input,
1312                                       vlib_cli_command_t * cmd)
1313 {
1314   locator_set_t * lsit;
1315   locator_t * loc;
1316   u32 * locit;
1317   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1318
1319   vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
1320                    "Priority", "Weight");
1321   pool_foreach (lsit, lcm->locator_set_pool,
1322   ({
1323     u8 * msg = 0;
1324     msg = format (msg, "%-16v", lsit->name);
1325     vec_foreach (locit, lsit->locator_indices)
1326       {
1327         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1328         if (loc->local)
1329           msg = format (msg, "%16d%16d%16d", loc->sw_if_index, loc->priority,
1330                         loc->weight);
1331         else
1332           msg = format (msg, "%16U%16d%16d", format_gid_address, &loc->address,
1333                         loc->priority, loc->weight);
1334       }
1335     vlib_cli_output (vm, "%v", msg);
1336     vec_free (msg);
1337   }));
1338   return 0;
1339 }
1340
1341 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
1342     .path = "show lisp locator-set",
1343     .short_help = "Shows locator-sets",
1344     .function = lisp_cp_show_locator_sets_command_fn,
1345 };
1346
1347 int
1348 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1349 {
1350   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1351   ip_address_t * addr;
1352   u32 i;
1353
1354   if (a->is_add)
1355     {
1356       vec_foreach(addr, lcm->map_resolvers)
1357         {
1358           if (!ip_address_cmp (addr, &a->address))
1359             {
1360               clib_warning("map-resolver %U already exists!", format_ip_address,
1361                            &a->address);
1362               return -1;
1363             }
1364         }
1365       vec_add1(lcm->map_resolvers, a->address);
1366     }
1367   else
1368     {
1369       for (i = 0; i < vec_len(lcm->map_resolvers); i++)
1370         {
1371           addr = vec_elt_at_index(lcm->map_resolvers, i);
1372           if (!ip_address_cmp (addr, &a->address))
1373             {
1374               vec_delete(lcm->map_resolvers, 1, i);
1375               break;
1376             }
1377         }
1378     }
1379   return 0;
1380 }
1381
1382 static clib_error_t *
1383 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1384                                          unformat_input_t * input,
1385                                          vlib_cli_command_t * cmd)
1386 {
1387   unformat_input_t _line_input, * line_input = &_line_input;
1388   u8 is_add = 1;
1389   ip_address_t ip_addr;
1390   clib_error_t * error = 0;
1391   vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
1392
1393   /* Get a line of input. */
1394   if (! unformat_user (input, unformat_line_input, line_input))
1395     return 0;
1396
1397   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1398     {
1399       if (unformat (line_input, "add"))
1400         is_add = 1;
1401       else if (unformat (line_input, "del"))
1402         is_add = 0;
1403       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1404         ;
1405       else
1406         {
1407           error = unformat_parse_error(line_input);
1408           goto done;
1409         }
1410     }
1411   a->is_add = is_add;
1412   a->address = ip_addr;
1413   vnet_lisp_add_del_map_resolver (a);
1414
1415  done:
1416   return error;
1417 }
1418
1419 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
1420     .path = "lisp map-resolver",
1421     .short_help = "lisp map-resolver add/del <ip_address>",
1422     .function = lisp_add_del_map_resolver_command_fn,
1423 };
1424
1425 /* Statistics (not really errors) */
1426 #define foreach_lisp_cp_lookup_error           \
1427 _(DROP, "drop")                                \
1428 _(MAP_REQUESTS_SENT, "map-request sent")
1429
1430 static char * lisp_cp_lookup_error_strings[] = {
1431 #define _(sym,string) string,
1432   foreach_lisp_cp_lookup_error
1433 #undef _
1434 };
1435
1436 typedef enum
1437 {
1438 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1439     foreach_lisp_cp_lookup_error
1440 #undef _
1441     LISP_CP_LOOKUP_N_ERROR,
1442 } lisp_cp_lookup_error_t;
1443
1444 typedef enum
1445 {
1446   LISP_CP_LOOKUP_NEXT_DROP,
1447   LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
1448   LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
1449   LISP_CP_LOOKUP_N_NEXT,
1450 } lisp_cp_lookup_next_t;
1451
1452 typedef struct
1453 {
1454   gid_address_t dst_eid;
1455   ip_address_t map_resolver_ip;
1456 } lisp_cp_lookup_trace_t;
1457
1458 u8 *
1459 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1460 {
1461   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1462   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1463   lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
1464
1465   s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1466               format_ip_address, &t->map_resolver_ip, format_gid_address,
1467               &t->dst_eid);
1468   return s;
1469 }
1470
1471 static u32
1472 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
1473                           ip_address_t * dst)
1474 {
1475   if (ip_addr_version (dst) == IP4)
1476       return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
1477                                         0);
1478   else
1479       return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
1480 }
1481
1482 void
1483 get_mr_and_local_iface_ip (lisp_cp_main_t *lcm, ip_address_t * mr_ip,
1484                             ip_address_t * sloc)
1485 {
1486   u32 adj_index;
1487   ip_adjacency_t * adj;
1488   ip_interface_address_t * ia = 0;
1489   ip_lookup_main_t * lm;
1490   ip4_address_t * l4 = 0;
1491   ip6_address_t * l6 = 0;
1492   ip_address_t * mrit;
1493
1494   if (vec_len(lcm->map_resolvers) == 0)
1495     {
1496       clib_warning("No map-resolver configured");
1497       return;
1498     }
1499
1500   /* find the first mr ip we have a route to and the ip of the
1501    * iface that has a route to it */
1502   vec_foreach(mrit, lcm->map_resolvers)
1503     {
1504       lm = ip_addr_version (mrit) == IP4 ?
1505           &lcm->im4->lookup_main : &lcm->im6->lookup_main;
1506
1507       adj_index = ip_fib_lookup_with_table (lcm, 0, mrit);
1508       adj = ip_get_adjacency (lm, adj_index);
1509
1510       if (adj == 0)
1511         continue;
1512
1513       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
1514         {
1515           ia = pool_elt_at_index(lm->if_address_pool, adj->if_address_index);
1516           if (ip_addr_version(mrit) == IP4)
1517             {
1518               l4 = ip_interface_address_get_address (lm, ia);
1519             }
1520           else
1521             {
1522               l6 = ip_interface_address_get_address (lm, ia);
1523             }
1524         }
1525       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1526         {
1527           /* find sw_if_index in rewrite header */
1528           u32 sw_if_index = adj->rewrite_header.sw_if_index;
1529
1530           /* find suitable address */
1531           if (ip_addr_version(mrit) == IP4)
1532             {
1533               /* find the first ip address */
1534               foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1535                                             sw_if_index, 1 /* unnumbered */,
1536               ({
1537                 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main,
1538                                                        ia);
1539                 break;
1540               }));
1541             }
1542           else
1543             {
1544               /* find the first ip address */
1545               foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1546                                             sw_if_index, 1 /* unnumbered */,
1547               ({
1548                 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main,
1549                                                        ia);
1550                 break;
1551               }));
1552             }
1553         }
1554
1555       if (l4)
1556         {
1557           ip_addr_v4(sloc).as_u32 = l4->as_u32;
1558           ip_addr_version(sloc) = IP4;
1559           ip_address_copy(mr_ip, mrit);
1560           return;
1561         }
1562       else if (l6)
1563         {
1564           clib_memcpy (&ip_addr_v6(sloc), l6, sizeof(*l6));
1565           ip_addr_version(sloc) = IP6;
1566           ip_address_copy(mr_ip, mrit);
1567           return;
1568         }
1569     }
1570
1571   clib_warning("Can't find map-resolver and local interface ip!");
1572   return;
1573 }
1574
1575 static gid_address_t *
1576 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1577 {
1578   ip4_address_t * l4;
1579   ip6_address_t * l6;
1580   u32 i;
1581   locator_t * loc;
1582   u32 * loc_indexp;
1583   ip_interface_address_t * ia = 0;
1584   gid_address_t gid_data, * gid = &gid_data;
1585   gid_address_t * rlocs = 0;
1586   ip_prefix_t * ippref = &gid_address_ippref (gid);
1587   ip_address_t * rloc = &ip_prefix_addr (ippref);
1588
1589   gid_address_type (gid) = GID_ADDR_IP_PREFIX;
1590   for (i = 0; i < vec_len(loc_set->locator_indices); i++)
1591     {
1592       loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
1593       loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1594
1595       ip_addr_version(rloc) = IP4;
1596       /* Add ipv4 locators first TODO sort them */
1597       foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1598                                     loc->sw_if_index, 1 /* unnumbered */,
1599       ({
1600         l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1601         ip_addr_v4 (rloc) = l4[0];
1602         ip_prefix_len (ippref) = 32;
1603         vec_add1 (rlocs, gid[0]);
1604       }));
1605
1606       ip_addr_version(rloc) = IP6;
1607       /* Add ipv6 locators */
1608       foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1609                                     loc->sw_if_index, 1 /* unnumbered */,
1610       ({
1611         l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1612         ip_addr_v6 (rloc) = l6[0];
1613         ip_prefix_len (ippref) = 128;
1614         vec_add1 (rlocs, gid[0]);
1615       }));
1616     }
1617   return rlocs;
1618 }
1619
1620 static vlib_buffer_t *
1621 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
1622                                 gid_address_t * seid, gid_address_t * deid,
1623                                 locator_set_t * loc_set, ip_address_t * mr_ip,
1624                                 ip_address_t * sloc, u8 is_smr_invoked,
1625                                 u64 *nonce_res, u32 * bi_res)
1626 {
1627   vlib_buffer_t * b;
1628   u32 bi;
1629   gid_address_t * rlocs = 0;
1630
1631   if (vlib_buffer_alloc (vm, &bi, 1) != 1)
1632     {
1633       clib_warning ("Can't allocate buffer for Map-Request!");
1634       return 0;
1635     }
1636
1637   b = vlib_get_buffer (vm, bi);
1638
1639   /* leave some space for the encap headers */
1640   vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
1641
1642   /* get rlocs */
1643   rlocs = build_itr_rloc_list (lcm, loc_set);
1644
1645   /* put lisp msg */
1646   lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
1647
1648   /* push ecm: udp-ip-lisp */
1649   lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
1650
1651   /* push outer ip header */
1652   pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
1653                        mr_ip);
1654
1655   bi_res[0] = bi;
1656
1657   if (rlocs)
1658     vec_free(rlocs);
1659   return b;
1660 }
1661
1662 static void
1663 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
1664                                gid_address_t * seid, gid_address_t * deid,
1665                                u8 is_smr_invoked)
1666 {
1667   u32 next_index, bi = 0, * to_next, map_index;
1668   vlib_buffer_t * b;
1669   vlib_frame_t * f;
1670   u64 nonce = 0;
1671   locator_set_t * loc_set;
1672   mapping_t * map;
1673   pending_map_request_t * pmr;
1674   ip_address_t mr_ip, sloc;
1675
1676   /* get locator-set for seid */
1677   if (!lcm->lisp_pitr)
1678     {
1679       map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
1680       if (map_index == ~0)
1681         {
1682           clib_warning("No local mapping found in eid-table for %U!",
1683                        format_gid_address, seid);
1684           return;
1685         }
1686
1687       map = pool_elt_at_index (lcm->mapping_pool, map_index);
1688
1689       if (!map->local)
1690         {
1691           clib_warning("Mapping found for src eid %U is not marked as local!",
1692                        format_gid_address, seid);
1693           return;
1694         }
1695     }
1696   else
1697     {
1698       map_index = lcm->pitr_map_index;
1699       map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1700     }
1701
1702   loc_set = pool_elt_at_index (lcm->locator_set_pool, map->locator_set_index);
1703
1704   /* get local iface ip to use in map-request XXX fib 0 for now*/
1705   get_mr_and_local_iface_ip (lcm, &mr_ip, &sloc);
1706
1707   /* build the encapsulated map request */
1708   b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set, &mr_ip,
1709                                       &sloc, is_smr_invoked, &nonce, &bi);
1710
1711   if (!b)
1712     return;
1713
1714   /* set fib index and lookup node */
1715   vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0;
1716   next_index = (ip_addr_version(&mr_ip) == IP4) ?
1717       ip4_lookup_node.index : ip6_lookup_node.index;
1718
1719   f = vlib_get_frame_to_node (vm, next_index);
1720
1721   /* Enqueue the packet */
1722   to_next = vlib_frame_vector_args (f);
1723   to_next[0] = bi;
1724   f->n_vectors = 1;
1725   vlib_put_frame_to_node (vm, next_index, f);
1726
1727   /* add map-request to pending requests table */
1728   pool_get(lcm->pending_map_requests_pool, pmr);
1729   gid_address_copy (&pmr->src, seid);
1730   gid_address_copy (&pmr->dst, deid);
1731   pmr->src_mapping_index = map_index;
1732   hash_set(lcm->pending_map_requests_by_nonce, nonce,
1733            pmr - lcm->pending_map_requests_pool);
1734 }
1735
1736 static void
1737 get_src_and_dst (void *hdr, ip_address_t * src, ip_address_t *dst)
1738 {
1739   ip4_header_t * ip4 = hdr;
1740   ip6_header_t * ip6;
1741
1742   if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
1743     {
1744       ip_addr_v4(src).as_u32 = ip4->src_address.as_u32;
1745       ip_addr_version(src) = IP4;
1746       ip_addr_v4(dst).as_u32 = ip4->dst_address.as_u32;
1747       ip_addr_version(dst) = IP4;
1748     }
1749   else
1750     {
1751       ip6 = hdr;
1752       clib_memcpy (&ip_addr_v6(src), &ip6->src_address, sizeof(ip6->src_address));
1753       ip_addr_version(src) = IP6;
1754       clib_memcpy (&ip_addr_v6(dst), &ip6->dst_address, sizeof(ip6->dst_address));
1755       ip_addr_version(dst) = IP6;
1756     }
1757 }
1758
1759 static uword
1760 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
1761               vlib_frame_t * from_frame)
1762 {
1763   u32 * from, * to_next_drop, di, si;
1764   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
1765   u32 pkts_mapped = 0;
1766   uword n_left_from, n_left_to_next_drop;
1767
1768   from = vlib_frame_vector_args (from_frame);
1769   n_left_from = from_frame->n_vectors;
1770
1771   while (n_left_from > 0)
1772     {
1773       vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
1774                            to_next_drop, n_left_to_next_drop);
1775
1776       while (n_left_from > 0 && n_left_to_next_drop > 0)
1777         {
1778           u32 pi0;
1779           vlib_buffer_t * p0;
1780           ip4_header_t * ip0;
1781           gid_address_t src, dst;
1782           ip_prefix_t * spref, * dpref;
1783
1784           gid_address_type (&src) = GID_ADDR_IP_PREFIX;
1785           spref = &gid_address_ippref(&src);
1786           gid_address_type (&dst) = GID_ADDR_IP_PREFIX;
1787           dpref = &gid_address_ippref(&dst);
1788
1789           pi0 = from[0];
1790           from += 1;
1791           n_left_from -= 1;
1792           to_next_drop[0] = pi0;
1793           to_next_drop += 1;
1794           n_left_to_next_drop -= 1;
1795
1796           p0 = vlib_get_buffer (vm, pi0);
1797           p0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
1798
1799           /* src/dst eid pair */
1800           ip0 = vlib_buffer_get_current (p0);
1801           get_src_and_dst (ip0, &ip_prefix_addr(spref), &ip_prefix_addr(dpref));
1802           ip_prefix_len(spref) = ip_address_max_len (ip_prefix_version(spref));
1803           ip_prefix_len(dpref) = ip_address_max_len (ip_prefix_version(dpref));
1804
1805           /* if we have remote mapping for destination already in map-chache
1806              add forwarding tunnel directly. If not send a map-request */
1807           di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
1808           if (~0 != di)
1809             {
1810               mapping_t * m =  vec_elt_at_index (lcm->mapping_pool, di);
1811               /* send a map-request also in case of negative mapping entry
1812                 with corresponding action */
1813               if (m->action == ACTION_SEND_MAP_REQUEST)
1814                 {
1815                   /* send map-request */
1816                   send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1817                   pkts_mapped++;
1818                 }
1819               else
1820                 {
1821                   si =  gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1822                                                &src);
1823                   if (~0 != si)
1824                     {
1825                       add_fwd_entry (lcm, si, di);
1826                     }
1827                 }
1828             }
1829           else
1830             {
1831               /* send map-request */
1832               send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1833               pkts_mapped++;
1834             }
1835
1836           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
1837             {
1838               lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, p0,
1839                                                           sizeof(*tr));
1840
1841               memset(tr, 0, sizeof(*tr));
1842               gid_address_copy (&tr->dst_eid, &dst);
1843               if (vec_len(lcm->map_resolvers) > 0)
1844                 {
1845                   clib_memcpy (&tr->map_resolver_ip,
1846                                vec_elt_at_index(lcm->map_resolvers, 0),
1847                                sizeof(ip_address_t));
1848                 }
1849             }
1850         }
1851
1852       vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
1853     }
1854   vlib_node_increment_counter (vm, node->node_index,
1855                                LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
1856                                pkts_mapped);
1857   return from_frame->n_vectors;
1858 }
1859
1860 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
1861   .function = lisp_cp_lookup,
1862   .name = "lisp-cp-lookup",
1863   .vector_size = sizeof (u32),
1864   .format_trace = format_lisp_cp_lookup_trace,
1865   .type = VLIB_NODE_TYPE_INTERNAL,
1866
1867   .n_errors = LISP_CP_LOOKUP_N_ERROR,
1868   .error_strings = lisp_cp_lookup_error_strings,
1869
1870   .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
1871
1872   .next_nodes = {
1873       [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
1874       [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
1875       [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
1876   },
1877 };
1878
1879 /* lisp_cp_input statistics */
1880 #define foreach_lisp_cp_input_error                   \
1881 _(DROP, "drop")                                        \
1882 _(MAP_REPLIES_RECEIVED, "map-replies received")
1883
1884 static char * lisp_cp_input_error_strings[] = {
1885 #define _(sym,string) string,
1886   foreach_lisp_cp_input_error
1887 #undef _
1888 };
1889
1890 typedef enum
1891 {
1892 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
1893     foreach_lisp_cp_input_error
1894 #undef _
1895     LISP_CP_INPUT_N_ERROR,
1896 } lisp_cp_input_error_t;
1897
1898 typedef enum
1899 {
1900   LISP_CP_INPUT_NEXT_DROP,
1901   LISP_CP_INPUT_N_NEXT,
1902 } lisp_cp_input_next_t;
1903
1904 typedef struct
1905 {
1906   gid_address_t dst_eid;
1907   ip4_address_t map_resolver_ip;
1908 } lisp_cp_input_trace_t;
1909
1910 u8 *
1911 format_lisp_cp_input_trace (u8 * s, va_list * args)
1912 {
1913   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1914   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1915   CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
1916
1917   s = format (s, "LISP-CP-INPUT: TODO");
1918   return s;
1919 }
1920
1921 ip_interface_address_t *
1922 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
1923                                           u8 loop)
1924 {
1925   vnet_main_t *vnm = vnet_get_main ();
1926   vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
1927   if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
1928     sw_if_index = swif->unnumbered_sw_if_index;
1929   u32 ia =
1930       (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
1931           vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
1932           (u32) ~0;
1933   return pool_elt_at_index((lm)->if_address_pool, ia);
1934 }
1935
1936 void *
1937 ip_interface_get_first_ip_addres (ip_lookup_main_t *lm, u32 sw_if_index,
1938                                    u8 loop)
1939 {
1940   ip_interface_address_t * ia = ip_interface_get_first_interface_address (
1941       lm, sw_if_index, loop);
1942   return ip_interface_address_get_address (lm, ia);
1943 }
1944
1945 static void
1946 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index,
1947                u32 dst_map_index)
1948 {
1949   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1950   fwd_entry_t * fe = 0;
1951   uword * feip = 0;
1952   memset(a, 0, sizeof(*a));
1953
1954   feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
1955   if (!feip)
1956     return;
1957
1958   fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
1959
1960   /* delete dp fwd entry */
1961   u32 sw_if_index;
1962   a->is_add = 0;
1963   a->dlocator = fe->dst_loc;
1964   a->slocator = fe->src_loc;
1965   a->vni = gid_address_vni(&a->deid);
1966   gid_address_copy(&a->deid, &fe->deid);
1967
1968   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
1969
1970   /* delete entry in fwd table */
1971   hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
1972   pool_put(lcm->fwd_entry_pool, fe);
1973 }
1974
1975 static void
1976 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
1977 {
1978   mapping_t * src_map, * dst_map;
1979   locator_set_t * dst_ls, * src_ls;
1980   u32 i, minp = ~0, sw_if_index;
1981   locator_t * dl = 0;
1982   uword * feip = 0, * tidp;
1983   fwd_entry_t* fe;
1984   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1985
1986   memset (a, 0, sizeof(*a));
1987
1988   /* remove entry if it already exists */
1989   feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
1990   if (feip)
1991     del_fwd_entry (lcm, src_map_index, dst_map_index);
1992
1993   src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
1994   dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
1995
1996   gid_address_copy (&a->deid, &dst_map->eid);
1997   a->vni = gid_address_vni(&a->deid);
1998
1999   tidp = hash_get(lcm->table_id_by_vni, a->vni);
2000   if (!tidp)
2001     {
2002       clib_warning("vni %d not associated to a vrf!", a->vni);
2003       return;
2004     }
2005   a->table_id = tidp[0];
2006
2007   /* XXX simple forwarding policy: first lowest (value) priority locator */
2008   dst_ls = pool_elt_at_index (lcm->locator_set_pool,
2009                               dst_map->locator_set_index);
2010   for (i = 0; i < vec_len (dst_ls->locator_indices); i++)
2011     {
2012       u32 li = vec_elt (dst_ls->locator_indices, i);
2013       locator_t * l = pool_elt_at_index (lcm->locator_pool, li);
2014       if (l->priority < minp && gid_address_type(&l->address)
2015             == GID_ADDR_IP_PREFIX)
2016         {
2017           minp = l->priority;
2018           dl = l;
2019         }
2020     }
2021   if (dl)
2022     {
2023       src_ls = pool_elt_at_index(lcm->locator_set_pool,
2024                                  src_map->locator_set_index);
2025       for (i = 0; i < vec_len(src_ls->locator_indices); i++)
2026         {
2027           u32 li = vec_elt (src_ls->locator_indices, i);
2028           locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
2029
2030           if (ip_addr_version(&gid_address_ip(&dl->address)) == IP4)
2031             {
2032               ip4_address_t * l4;
2033               l4 = ip_interface_get_first_ip_addres (&lcm->im4->lookup_main,
2034                                                      sl->sw_if_index,
2035                                                      1 /* unnumbered */);
2036               ip_addr_v4(&a->slocator) = *l4;
2037               ip_addr_version(&a->slocator) = IP4;
2038             }
2039           else
2040             {
2041               ip6_address_t * l6;
2042               l6 = ip_interface_get_first_ip_addres (&lcm->im6->lookup_main,
2043                                                      sl->sw_if_index,
2044                                                      1 /* unnumbered */);
2045               ip_addr_v6(&a->slocator) = *l6;
2046               ip_addr_version(&a->slocator) = IP6;
2047             }
2048         }
2049     }
2050
2051   /* insert data plane forwarding entry */
2052   a->is_add = 1;
2053   if (dl)
2054     a->dlocator = gid_address_ip(&dl->address);
2055   else
2056     {
2057       a->is_negative = 1;
2058       a->action = dst_map->action;
2059     }
2060
2061   /* TODO remove */
2062   u8 ipver = ip_prefix_version(&gid_address_ippref(&a->deid));
2063   a->decap_next_index = (ipver == IP4) ?
2064           LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
2065
2066   vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
2067
2068   /* add tunnel to fwd entry table XXX check return value from DP insertion */
2069   pool_get (lcm->fwd_entry_pool, fe);
2070   fe->dst_loc = a->dlocator;
2071   fe->src_loc = a->slocator;
2072   gid_address_copy (&fe->deid, &a->deid);
2073   hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
2074             fe - lcm->fwd_entry_pool);
2075 }
2076
2077 /* return 0 if the two locator sets are identical 1 otherwise */
2078 static u8
2079 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
2080                   locator_t * new_locators)
2081 {
2082   u32 i, old_li;
2083   locator_t * old_loc, * new_loc;
2084
2085   if (vec_len (old_ls_indexes) != vec_len(new_locators))
2086     return 1;
2087
2088   for (i = 0; i < vec_len(new_locators); i++)
2089     {
2090       old_li = vec_elt(old_ls_indexes, i);
2091       old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
2092
2093       new_loc = vec_elt_at_index(new_locators, i);
2094
2095       if (locator_cmp (old_loc, new_loc))
2096         return 1;
2097     }
2098   return 0;
2099 }
2100
2101 void
2102 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2103 {
2104   mapping_t * old_map;
2105   locator_t * loc;
2106   u32 len = 0, i, ls_index = 0;
2107   void * h;
2108   vnet_lisp_add_del_locator_set_args_t _ls_arg, * ls_arg = &_ls_arg;
2109   vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
2110   pending_map_request_t * pmr;
2111   locator_t probed;
2112   map_reply_hdr_t * mrep_hdr;
2113   u64 nonce;
2114   u32 dst_map_index, mi;
2115   uword * pmr_index;
2116
2117   mrep_hdr = vlib_buffer_get_current (b);
2118
2119   /* Check pending requests table and nonce */
2120   nonce = MREP_NONCE(mrep_hdr);
2121   pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
2122   if (!pmr_index)
2123     {
2124       clib_warning("No pending map-request entry with nonce %lu!", nonce);
2125       return;
2126     }
2127   pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
2128
2129   vlib_buffer_pull (b, sizeof(*mrep_hdr));
2130
2131   for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
2132     {
2133       memset (ls_arg, 0, sizeof(*ls_arg));
2134       memset (m_args, 0, sizeof(*m_args));
2135
2136       h = vlib_buffer_get_current (b);
2137       m_args->ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
2138       m_args->action = MAP_REC_ACTION(h);
2139       m_args->authoritative = MAP_REC_AUTH(h);
2140
2141       len = lisp_msg_parse_mapping_record (b, &m_args->deid, &ls_arg->locators,
2142                                            &probed);
2143       if (len == ~0)
2144         {
2145           clib_warning ("Failed to parse mapping record!");
2146           vec_foreach (loc, ls_arg->locators)
2147             {
2148               locator_free (loc);
2149             }
2150           vec_free(ls_arg->locators);
2151           return;
2152         }
2153
2154       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &m_args->deid);
2155       old_map = mi != ~0 ? pool_elt_at_index(lcm->mapping_pool, mi) : 0;
2156
2157       /* if mapping already exists, decide if locators (and forwarding) should
2158        * be updated and be done */
2159       if (old_map != 0 && !gid_address_cmp (&old_map->eid, &m_args->deid))
2160         {
2161           locator_set_t * old_ls;
2162
2163           /* update mapping attributes */
2164           old_map->action = m_args->action;
2165           old_map->authoritative = m_args->authoritative;
2166           old_map->ttl = m_args->ttl;
2167
2168           old_ls = pool_elt_at_index(lcm->locator_set_pool,
2169                                      old_map->locator_set_index);
2170           /* if the two locators are not equal, update them and forwarding
2171            * otherwise there's nothing to be done */
2172           if (compare_locators (lcm, old_ls->locator_indices, ls_arg->locators))
2173             {
2174               /* set locator-set index to overwrite */
2175               ls_arg->is_add = 1;
2176               ls_arg->index = old_map->locator_set_index;
2177               vnet_lisp_add_del_locator_set (ls_arg, 0);
2178               add_fwd_entry (lcm, pmr->src_mapping_index, mi);
2179             }
2180         }
2181       /* new mapping */
2182       else
2183         {
2184           /* add locator-set */
2185           ls_arg->is_add = 1;
2186           ls_arg->index = ~0;
2187           vnet_lisp_add_del_locator_set (ls_arg, &ls_index);
2188
2189           /* add mapping */
2190           m_args->is_add = 1;
2191           m_args->locator_set_index = ls_index;
2192           vnet_lisp_add_del_mapping (m_args, &dst_map_index);
2193
2194           /* add forwarding tunnel */
2195           add_fwd_entry (lcm, pmr->src_mapping_index, dst_map_index);
2196         }
2197       vec_free(ls_arg->locators);
2198     }
2199
2200   /* remove pending map request entry */
2201   hash_unset(lcm->pending_map_requests_by_nonce, nonce);
2202   pool_put(lcm->pending_map_requests_pool, pmr);
2203 }
2204
2205 void
2206 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
2207 {
2208   map_request_hdr_t * mreq_hdr;
2209   gid_address_t src, dst;
2210 //  u64 nonce;
2211   u32 i, len = 0;
2212   gid_address_t * itr_rlocs = 0, * rloc;
2213
2214   mreq_hdr = vlib_buffer_get_current (b);
2215   vlib_buffer_pull (b, sizeof(*mreq_hdr));
2216
2217 //  nonce = MREQ_NONCE(mreq_hdr);
2218
2219   if (!MREQ_SMR(mreq_hdr)) {
2220       clib_warning("Only SMR Map-Requests supported for now!");
2221       return;
2222   }
2223
2224   /* parse src eid */
2225   len = lisp_msg_parse_addr (b, &src);
2226   if (len == ~0)
2227     return;
2228
2229   /* for now we don't do anything with the itr's rlocs */
2230   len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
2231   if (len == ~0)
2232     return;
2233
2234   /* TODO: RLOCs are currently unused, so free them for now */
2235   vec_foreach (rloc, itr_rlocs)
2236     {
2237       gid_address_free (rloc);
2238     }
2239
2240   /* parse eid records and send SMR-invoked map-requests */
2241   for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
2242     {
2243       memset(&dst, 0, sizeof(dst));
2244       len = lisp_msg_parse_eid_rec (b, &dst);
2245       if (len == ~0)
2246         {
2247           clib_warning("Can't parse map-request EID-record");
2248           return;
2249         }
2250       /* send SMR-invoked map-requests */
2251       send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
2252     }
2253 }
2254
2255 static uword
2256 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2257                vlib_frame_t * from_frame)
2258 {
2259   u32 n_left_from, * from, * to_next_drop;
2260   lisp_msg_type_e type;
2261   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2262
2263   from = vlib_frame_vector_args (from_frame);
2264   n_left_from = from_frame->n_vectors;
2265
2266
2267   while (n_left_from > 0)
2268     {
2269       u32 n_left_to_next_drop;
2270
2271       vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
2272                            to_next_drop, n_left_to_next_drop);
2273       while (n_left_from > 0 && n_left_to_next_drop > 0)
2274         {
2275           u32 bi0;
2276           vlib_buffer_t * b0;
2277
2278           bi0 = from[0];
2279           from += 1;
2280           n_left_from -= 1;
2281           to_next_drop[0] = bi0;
2282           to_next_drop += 1;
2283           n_left_to_next_drop -= 1;
2284
2285           b0 = vlib_get_buffer (vm, bi0);
2286
2287           type = lisp_msg_type(vlib_buffer_get_current (b0));
2288           switch (type)
2289             {
2290             case LISP_MAP_REPLY:
2291               process_map_reply (lcm, b0);
2292               break;
2293             case LISP_MAP_REQUEST:
2294               process_map_request(vm, lcm, b0);
2295               break;
2296             default:
2297               clib_warning("Unsupported LISP message type %d", type);
2298               break;
2299             }
2300
2301           b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
2302
2303           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2304             {
2305
2306             }
2307         }
2308
2309       vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
2310     }
2311   return from_frame->n_vectors;
2312 }
2313
2314 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
2315   .function = lisp_cp_input,
2316   .name = "lisp-cp-input",
2317   .vector_size = sizeof (u32),
2318   .format_trace = format_lisp_cp_input_trace,
2319   .type = VLIB_NODE_TYPE_INTERNAL,
2320
2321   .n_errors = LISP_CP_INPUT_N_ERROR,
2322   .error_strings = lisp_cp_input_error_strings,
2323
2324   .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2325
2326   .next_nodes = {
2327       [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2328   },
2329 };
2330
2331 clib_error_t *
2332 lisp_cp_init (vlib_main_t *vm)
2333 {
2334   lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2335   clib_error_t * error = 0;
2336
2337   if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
2338     return error;
2339
2340   lcm->im4 = &ip4_main;
2341   lcm->im6 = &ip6_main;
2342   lcm->vlib_main = vm;
2343   lcm->vnet_main = vnet_get_main();
2344
2345   gid_dictionary_init (&lcm->mapping_index_by_gid);
2346
2347   /* default vrf mapped to vni 0 */
2348   hash_set(lcm->table_id_by_vni, 0, 0);
2349
2350   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2351                          lisp_cp_input_node.index, 1 /* is_ip4 */);
2352   udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2353                          lisp_cp_input_node.index, 0 /* is_ip4 */);
2354
2355   return 0;
2356 }
2357
2358 VLIB_INIT_FUNCTION(lisp_cp_init);