vlib: clean up r2 plugin registration relocator
[vpp.git] / src / plugins / lisp / lisp-cp / lisp_cli.c
1 /*
2  * Copyright (c) 2017 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 <lisp/lisp-cp/control.h>
17 #include <lisp/lisp-gpe/lisp_gpe.h>
18
19 static clib_error_t *
20 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
21                                   unformat_input_t * input,
22                                   vlib_cli_command_t * cmd)
23 {
24   lisp_adjacency_t *adjs, *adj;
25   vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
26   unformat_input_t _line_input, *line_input = &_line_input;
27   u32 vni = ~0;
28   clib_error_t *error = NULL;
29
30   /* Get a line of input. */
31   if (!unformat_user (input, unformat_line_input, line_input))
32     return 0;
33
34   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
35     {
36       if (unformat (line_input, "vni %d", &vni))
37         ;
38       else
39         {
40           vlib_cli_output (vm, "parse error: '%U'",
41                            format_unformat_error, line_input);
42           goto done;
43         }
44     }
45
46   if (~0 == vni)
47     {
48       vlib_cli_output (vm, "error: no vni specified!");
49       goto done;
50     }
51
52   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
53
54   vec_foreach (adj, adjs)
55   {
56     vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
57                      format_gid_address, &adj->reid);
58   }
59   vec_free (adjs);
60
61 done:
62   unformat_free (line_input);
63
64   return error;
65 }
66
67 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
68     .path = "show lisp adjacencies",
69     .short_help = "show lisp adjacencies",
70     .function = lisp_show_adjacencies_command_fn,
71 };
72
73 static clib_error_t *
74 lisp_add_del_map_server_command_fn (vlib_main_t * vm,
75                                     unformat_input_t * input,
76                                     vlib_cli_command_t * cmd)
77 {
78   int rv = 0;
79   u8 is_add = 1, ip_set = 0;
80   ip_address_t ip;
81   unformat_input_t _line_input, *line_input = &_line_input;
82   clib_error_t *error = NULL;
83
84   /* Get a line of input. */
85   if (!unformat_user (input, unformat_line_input, line_input))
86     return 0;
87
88   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
89     {
90       if (unformat (line_input, "add"))
91         is_add = 1;
92       else if (unformat (line_input, "del"))
93         is_add = 0;
94       else if (unformat (line_input, "%U", unformat_ip_address, &ip))
95         ip_set = 1;
96       else
97         {
98           vlib_cli_output (vm, "parse error: '%U'",
99                            format_unformat_error, line_input);
100           goto done;
101         }
102     }
103
104   if (!ip_set)
105     {
106       vlib_cli_output (vm, "map-server ip address not set!");
107       goto done;
108     }
109
110   rv = vnet_lisp_add_del_map_server (&ip, is_add);
111   if (!rv)
112     vlib_cli_output (vm, "failed to %s map-server!",
113                      is_add ? "add" : "delete");
114
115 done:
116   unformat_free (line_input);
117
118   return error;
119 }
120
121 VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
122     .path = "lisp map-server",
123     .short_help = "lisp map-server add|del <ip>",
124     .function = lisp_add_del_map_server_command_fn,
125 };
126
127
128 static clib_error_t *
129 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
130                                    vlib_cli_command_t * cmd)
131 {
132   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
133   unformat_input_t _line_input, *line_input = &_line_input;
134   u8 is_add = 1;
135   gid_address_t eid;
136   gid_address_t *eids = 0;
137   clib_error_t *error = 0;
138   u8 *locator_set_name = 0;
139   u32 locator_set_index = 0, map_index = 0;
140   uword *p;
141   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
142   int rv = 0;
143   u32 vni = 0;
144   u8 *key = 0;
145   u32 key_id = 0;
146
147   clib_memset (&eid, 0, sizeof (eid));
148   clib_memset (a, 0, sizeof (*a));
149
150   /* Get a line of input. */
151   if (!unformat_user (input, unformat_line_input, line_input))
152     return 0;
153
154   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
155     {
156       if (unformat (line_input, "add"))
157         is_add = 1;
158       else if (unformat (line_input, "del"))
159         is_add = 0;
160       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
161         ;
162       else if (unformat (line_input, "vni %d", &vni))
163         gid_address_vni (&eid) = vni;
164       else if (unformat (line_input, "secret-key %_%v%_", &key))
165         ;
166       else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
167                          &key_id))
168         ;
169       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
170         {
171           vec_terminate_c_string (locator_set_name);
172           p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
173           if (!p)
174             {
175               error = clib_error_return (0, "locator-set %s doesn't exist",
176                                          locator_set_name);
177               goto done;
178             }
179           locator_set_index = p[0];
180         }
181       else
182         {
183           error = unformat_parse_error (line_input);
184           goto done;
185         }
186     }
187   /* XXX treat batch configuration */
188
189   if (GID_ADDR_SRC_DST == gid_address_type (&eid))
190     {
191       error =
192         clib_error_return (0, "src/dst is not supported for local EIDs!");
193       goto done;
194     }
195
196   if (key && (0 == key_id))
197     {
198       vlib_cli_output (vm, "invalid key_id!");
199       goto done;;
200     }
201
202   gid_address_copy (&a->eid, &eid);
203   a->is_add = is_add;
204   a->locator_set_index = locator_set_index;
205   a->local = 1;
206   a->key = key;
207   a->key_id = key_id;
208
209   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
210   if (0 != rv)
211     {
212       error = clib_error_return (0, "failed to %s local mapping!",
213                                  is_add ? "add" : "delete");
214     }
215 done:
216   vec_free (eids);
217   if (locator_set_name)
218     vec_free (locator_set_name);
219   gid_address_free (&a->eid);
220   vec_free (a->key);
221   unformat_free (line_input);
222
223   return error;
224 }
225
226 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
227     .path = "lisp eid-table",
228     .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
229       "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
230     .function = lisp_add_del_local_eid_command_fn,
231 };
232
233 static clib_error_t *
234 lisp_eid_table_map_command_fn (vlib_main_t * vm,
235                                unformat_input_t * input,
236                                vlib_cli_command_t * cmd)
237 {
238   u8 is_add = 1, is_l2 = 0;
239   u32 vni = 0, dp_id = 0;
240   unformat_input_t _line_input, *line_input = &_line_input;
241   clib_error_t *error = NULL;
242
243   /* Get a line of input. */
244   if (!unformat_user (input, unformat_line_input, line_input))
245     return 0;
246
247   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
248     {
249       if (unformat (line_input, "del"))
250         is_add = 0;
251       else if (unformat (line_input, "vni %d", &vni))
252         ;
253       else if (unformat (line_input, "vrf %d", &dp_id))
254         ;
255       else if (unformat (line_input, "bd %d", &dp_id))
256         is_l2 = 1;
257       else
258         {
259           error = unformat_parse_error (line_input);
260           goto done;
261         }
262     }
263   vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
264
265 done:
266   unformat_free (line_input);
267
268   return error;
269 }
270
271 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
272     .path = "lisp eid-table map",
273     .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
274     .function = lisp_eid_table_map_command_fn,
275 };
276
277 /**
278  * Handler for add/del remote mapping CLI.
279  *
280  * @param vm vlib context
281  * @param input input from user
282  * @param cmd cmd
283  * @return pointer to clib error structure
284  */
285 static clib_error_t *
286 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
287                                         unformat_input_t * input,
288                                         vlib_cli_command_t * cmd)
289 {
290   clib_error_t *error = 0;
291   unformat_input_t _line_input, *line_input = &_line_input;
292   u8 is_add = 1, del_all = 0;
293   locator_t rloc, *rlocs = 0, *curr_rloc = 0;
294   gid_address_t eid;
295   u8 eid_set = 0;
296   u32 vni, action = ~0, p, w;
297   int rv;
298
299   /* Get a line of input. */
300   if (!unformat_user (input, unformat_line_input, line_input))
301     return 0;
302
303   clib_memset (&eid, 0, sizeof (eid));
304   clib_memset (&rloc, 0, sizeof (rloc));
305
306   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
307     {
308       if (unformat (line_input, "del-all"))
309         del_all = 1;
310       else if (unformat (line_input, "del"))
311         is_add = 0;
312       else if (unformat (line_input, "add"))
313         ;
314       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
315         eid_set = 1;
316       else if (unformat (line_input, "vni %u", &vni))
317         {
318           gid_address_vni (&eid) = vni;
319         }
320       else if (unformat (line_input, "p %d w %d", &p, &w))
321         {
322           if (!curr_rloc)
323             {
324               clib_warning
325                 ("No RLOC configured for setting priority/weight!");
326               goto done;
327             }
328           curr_rloc->priority = p;
329           curr_rloc->weight = w;
330         }
331       else if (unformat (line_input, "rloc %U", unformat_ip_address,
332                          &gid_address_ip (&rloc.address)))
333         {
334           /* since rloc is stored in ip prefix we need to set prefix length */
335           ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
336
337           u8 version = gid_address_ip_version (&rloc.address);
338           ip_prefix_len (pref) = ip_address_max_len (version);
339
340           vec_add1 (rlocs, rloc);
341           curr_rloc = &rlocs[vec_len (rlocs) - 1];
342         }
343       else if (unformat (line_input, "action %U",
344                          unformat_negative_mapping_action, &action))
345         ;
346       else
347         {
348           clib_warning ("parse error");
349           goto done;
350         }
351     }
352
353   if (!eid_set)
354     {
355       clib_warning ("missing eid!");
356       goto done;
357     }
358
359   if (!del_all)
360     {
361       if (is_add && (~0 == action) && 0 == vec_len (rlocs))
362         {
363           clib_warning ("no action set for negative map-reply!");
364           goto done;
365         }
366     }
367   else
368     {
369       vnet_lisp_clear_all_remote_adjacencies ();
370       goto done;
371     }
372
373   /* TODO build src/dst with seid */
374
375   /* if it's a delete, clean forwarding */
376   if (!is_add)
377     {
378       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
379       clib_memset (a, 0, sizeof (a[0]));
380       gid_address_copy (&a->reid, &eid);
381       if (vnet_lisp_add_del_adjacency (a))
382         {
383           clib_warning ("failed to delete adjacency!");
384           goto done;
385         }
386     }
387
388   /* add as static remote mapping, i.e., not authoritative and infinite
389    * ttl */
390   if (is_add)
391     {
392       vnet_lisp_add_del_mapping_args_t _map_args, *map_args = &_map_args;
393       clib_memset (map_args, 0, sizeof (map_args[0]));
394       gid_address_copy (&map_args->eid, &eid);
395       map_args->action = action;
396       map_args->is_static = 1;
397       map_args->authoritative = 0;
398       map_args->ttl = ~0;
399       rv = vnet_lisp_add_mapping (map_args, rlocs, NULL, NULL);
400     }
401   else
402     rv = vnet_lisp_del_mapping (&eid, NULL);
403
404   if (rv)
405     clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
406
407 done:
408   vec_free (rlocs);
409   unformat_free (line_input);
410   return error;
411 }
412
413 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
414     .path = "lisp remote-mapping",
415     .short_help = "lisp remote-mapping add|del [del-all] vni <vni> "
416                   "eid <est-eid> [action <no-action|natively-forward|"
417                   "send-map-request|drop>] rloc <dst-locator> p <prio> "
418                   "w <weight> [rloc <dst-locator> ... ]",
419     .function = lisp_add_del_remote_mapping_command_fn,
420 };
421
422 /**
423  * Handler for add/del adjacency CLI.
424  */
425 static clib_error_t *
426 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
427                                    vlib_cli_command_t * cmd)
428 {
429   clib_error_t *error = 0;
430   unformat_input_t _line_input, *line_input = &_line_input;
431   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
432   u8 is_add = 1;
433   ip_prefix_t *reid_ippref, *leid_ippref;
434   gid_address_t leid, reid;
435   u8 *dmac = gid_address_mac (&reid);
436   u8 *smac = gid_address_mac (&leid);
437   u8 reid_set = 0, leid_set = 0;
438   u32 vni;
439
440   /* Get a line of input. */
441   if (!unformat_user (input, unformat_line_input, line_input))
442     return 0;
443
444   clib_memset (&reid, 0, sizeof (reid));
445   clib_memset (&leid, 0, sizeof (leid));
446
447   leid_ippref = &gid_address_ippref (&leid);
448   reid_ippref = &gid_address_ippref (&reid);
449
450   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
451     {
452       if (unformat (line_input, "del"))
453         is_add = 0;
454       else if (unformat (line_input, "add"))
455         ;
456       else if (unformat (line_input, "reid %U",
457                          unformat_ip_prefix, reid_ippref))
458         {
459           gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
460           reid_set = 1;
461         }
462       else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
463         {
464           gid_address_type (&reid) = GID_ADDR_MAC;
465           reid_set = 1;
466         }
467       else if (unformat (line_input, "vni %u", &vni))
468         {
469           gid_address_vni (&leid) = vni;
470           gid_address_vni (&reid) = vni;
471         }
472       else if (unformat (line_input, "leid %U",
473                          unformat_ip_prefix, leid_ippref))
474         {
475           gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
476           leid_set = 1;
477         }
478       else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
479         {
480           gid_address_type (&leid) = GID_ADDR_MAC;
481           leid_set = 1;
482         }
483       else
484         {
485           clib_warning ("parse error");
486           goto done;
487         }
488     }
489
490   if (!reid_set || !leid_set)
491     {
492       clib_warning ("missing remote or local eid!");
493       goto done;
494     }
495
496   if ((gid_address_type (&leid) != gid_address_type (&reid))
497       || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
498           && ip_prefix_version (reid_ippref)
499           != ip_prefix_version (leid_ippref)))
500     {
501       clib_warning ("remote and local EIDs are of different types!");
502       goto done;
503     }
504
505   clib_memset (a, 0, sizeof (a[0]));
506   gid_address_copy (&a->leid, &leid);
507   gid_address_copy (&a->reid, &reid);
508   a->is_add = is_add;
509
510   if (vnet_lisp_add_del_adjacency (a))
511     clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
512
513 done:
514   unformat_free (line_input);
515   return error;
516 }
517
518 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
519     .path = "lisp adjacency",
520     .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
521       "leid <local-eid>",
522     .function = lisp_add_del_adjacency_command_fn,
523 };
524
525
526 static clib_error_t *
527 lisp_map_request_mode_command_fn (vlib_main_t * vm,
528                                   unformat_input_t * input,
529                                   vlib_cli_command_t * cmd)
530 {
531   unformat_input_t _i, *i = &_i;
532   map_request_mode_t mr_mode = _MR_MODE_MAX;
533   clib_error_t *error = NULL;
534
535   /* Get a line of input. */
536   if (!unformat_user (input, unformat_line_input, i))
537     return 0;
538
539   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
540     {
541       if (unformat (i, "dst-only"))
542         mr_mode = MR_MODE_DST_ONLY;
543       else if (unformat (i, "src-dst"))
544         mr_mode = MR_MODE_SRC_DST;
545       else
546         {
547           clib_warning ("parse error '%U'", format_unformat_error, i);
548           goto done;
549         }
550     }
551
552   if (_MR_MODE_MAX == mr_mode)
553     {
554       clib_warning ("No LISP map request mode entered!");
555       goto done;
556     }
557
558   vnet_lisp_set_map_request_mode (mr_mode);
559
560 done:
561   unformat_free (i);
562
563   return error;
564 }
565
566 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
567     .path = "lisp map-request mode",
568     .short_help = "lisp map-request mode dst-only|src-dst",
569     .function = lisp_map_request_mode_command_fn,
570 };
571
572
573 static u8 *
574 format_lisp_map_request_mode (u8 * s, va_list * args)
575 {
576   u32 mode = va_arg (*args, u32);
577
578   switch (mode)
579     {
580     case 0:
581       return format (0, "dst-only");
582     case 1:
583       return format (0, "src-dst");
584     }
585   return 0;
586 }
587
588 static clib_error_t *
589 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
590                                        unformat_input_t * input,
591                                        vlib_cli_command_t * cmd)
592 {
593   vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
594                    vnet_lisp_get_map_request_mode ());
595   return 0;
596 }
597
598 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
599     .path = "show lisp map-request mode",
600     .short_help = "show lisp map-request mode",
601     .function = lisp_show_map_request_mode_command_fn,
602 };
603
604 static clib_error_t *
605 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
606                                     unformat_input_t * input,
607                                     vlib_cli_command_t * cmd)
608 {
609   lisp_msmr_t *mr;
610   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
611
612   vec_foreach (mr, lcm->map_resolvers)
613   {
614     vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
615   }
616   return 0;
617 }
618
619 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
620     .path = "show lisp map-resolvers",
621     .short_help = "show lisp map-resolvers",
622     .function = lisp_show_map_resolvers_command_fn,
623 };
624
625
626 static clib_error_t *
627 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
628                                       unformat_input_t * input,
629                                       vlib_cli_command_t * cmd)
630 {
631   u8 locator_name_set = 0;
632   u8 *locator_set_name = 0;
633   u8 is_add = 1;
634   unformat_input_t _line_input, *line_input = &_line_input;
635   clib_error_t *error = 0;
636   int rv = 0;
637
638   /* Get a line of input. */
639   if (!unformat_user (input, unformat_line_input, line_input))
640     return 0;
641
642   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
643     {
644       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
645         locator_name_set = 1;
646       else if (unformat (line_input, "disable"))
647         is_add = 0;
648       else
649         {
650           error = clib_error_return (0, "parse error");
651           goto done;
652         }
653     }
654
655   if (!locator_name_set)
656     {
657       clib_warning ("No locator set specified!");
658       goto done;
659     }
660   vec_terminate_c_string (locator_set_name);
661   rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
662   if (0 != rv)
663     {
664       error = clib_error_return (0, "failed to %s pitr!",
665                                  is_add ? "add" : "delete");
666     }
667
668 done:
669   if (locator_set_name)
670     vec_free (locator_set_name);
671   unformat_free (line_input);
672
673   return error;
674 }
675
676 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
677     .path = "lisp pitr",
678     .short_help = "lisp pitr [disable] ls <locator-set-name>",
679     .function = lisp_pitr_set_locator_set_command_fn,
680 };
681
682 static clib_error_t *
683 lisp_show_pitr_command_fn (vlib_main_t * vm,
684                            unformat_input_t * input, vlib_cli_command_t * cmd)
685 {
686   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
687   mapping_t *m;
688   locator_set_t *ls;
689   u8 *tmp_str = 0;
690   u8 status = lcm->flags & LISP_FLAG_PITR_MODE;
691
692   vlib_cli_output (vm, "%=20s%=16s", "pitr", status ? "locator-set" : "");
693
694   if (!status)
695     {
696       vlib_cli_output (vm, "%=20s", "disable");
697       return 0;
698     }
699
700   if (~0 == lcm->pitr_map_index)
701     {
702       tmp_str = format (0, "N/A");
703     }
704   else
705     {
706       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
707       if (~0 != m->locator_set_index)
708         {
709           ls =
710             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
711           tmp_str = format (0, "%s", ls->name);
712         }
713       else
714         {
715           tmp_str = format (0, "N/A");
716         }
717     }
718   vec_add1 (tmp_str, 0);
719
720   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
721
722   vec_free (tmp_str);
723
724   return 0;
725 }
726
727 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
728     .path = "show lisp pitr",
729     .short_help = "Show pitr",
730     .function = lisp_show_pitr_command_fn,
731 };
732
733 static u8 *
734 format_eid_entry (u8 * s, va_list * args)
735 {
736   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
737   lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
738   mapping_t *mapit = va_arg (*args, mapping_t *);
739   locator_set_t *ls = va_arg (*args, locator_set_t *);
740   gid_address_t *gid = &mapit->eid;
741   u32 ttl = mapit->ttl;
742   u8 aut = mapit->authoritative;
743   u32 *loc_index;
744   u8 first_line = 1;
745   u8 *loc;
746
747   u8 *type = ls->local ? format (0, "local(%s)", ls->name)
748     : format (0, "remote");
749
750   if (vec_len (ls->locator_indices) == 0)
751     {
752       s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
753                   type, ttl, aut);
754     }
755   else
756     {
757       vec_foreach (loc_index, ls->locator_indices)
758       {
759         locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
760         if (l->local)
761           loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
762                         l->sw_if_index);
763         else
764           loc = format (0, "%U", format_ip_address,
765                         &gid_address_ip (&l->address));
766
767         if (first_line)
768           {
769             s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
770                         gid, type, loc, ttl, aut);
771             first_line = 0;
772           }
773         else
774           s = format (s, "%55s%v\n", "", loc);
775       }
776     }
777   return s;
778 }
779
780 static clib_error_t *
781 lisp_show_eid_table_command_fn (vlib_main_t * vm,
782                                 unformat_input_t * input,
783                                 vlib_cli_command_t * cmd)
784 {
785   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
786   mapping_t *mapit;
787   unformat_input_t _line_input, *line_input = &_line_input;
788   u32 mi;
789   gid_address_t eid;
790   u8 print_all = 1;
791   u8 filter = 0;
792   clib_error_t *error = NULL;
793
794   clib_memset (&eid, 0, sizeof (eid));
795
796   /* Get a line of input. */
797   if (!unformat_user (input, unformat_line_input, line_input))
798     return 0;
799
800   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
801     {
802       if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
803         print_all = 0;
804       else if (unformat (line_input, "local"))
805         filter = 1;
806       else if (unformat (line_input, "remote"))
807         filter = 2;
808       else
809         {
810           error = clib_error_return (0, "parse error: '%U'",
811                                      format_unformat_error, line_input);
812           goto done;
813         }
814     }
815
816   vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
817                    "EID", "type", "locators", "ttl", "authoritative");
818
819   if (print_all)
820     {
821       pool_foreach (mapit, lcm->mapping_pool)
822        {
823         if (mapit->pitr_set)
824           continue;
825
826         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
827                                                 mapit->locator_set_index);
828         if (filter && !((1 == filter && ls->local) ||
829           (2 == filter && !ls->local)))
830           {
831             continue;
832           }
833         vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
834                          lcm, mapit, ls);
835       }
836     }
837   else
838     {
839       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
840       if ((u32) ~ 0 == mi)
841         goto done;
842
843       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
844       locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
845                                              mapit->locator_set_index);
846
847       if (filter && !((1 == filter && ls->local) ||
848                       (2 == filter && !ls->local)))
849         {
850           goto done;
851         }
852
853       vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
854                        lcm, mapit, ls);
855     }
856
857 done:
858   unformat_free (line_input);
859
860   return error;
861 }
862
863 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
864     .path = "show lisp eid-table",
865     .short_help = "show lisp eid-table [local|remote|eid <eid>]",
866     .function = lisp_show_eid_table_command_fn,
867 };
868
869
870 static clib_error_t *
871 lisp_enable_command_fn (vlib_main_t * vm, unformat_input_t * input,
872                         vlib_cli_command_t * cmd)
873 {
874   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
875     return clib_error_return (0, "parse error: '%U'", format_unformat_error,
876                               input);
877
878   vnet_lisp_enable_disable (1);
879
880   return 0;
881 }
882
883 VLIB_CLI_COMMAND (lisp_cp_enable_command) = {
884     .path = "lisp enable",
885     .short_help = "lisp enable",
886     .function = lisp_enable_command_fn,
887 };
888
889 static clib_error_t *
890 lisp_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
891                          vlib_cli_command_t * cmd)
892 {
893   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
894     return clib_error_return (0, "parse error: '%U'", format_unformat_error,
895                               input);
896
897   vnet_lisp_enable_disable (0);
898
899   return 0;
900 }
901
902 VLIB_CLI_COMMAND (lisp_cp_disable_command) = {
903     .path = "lisp disable",
904     .short_help = "lisp disable",
905     .function = lisp_disable_command_fn,
906 };
907
908 static clib_error_t *
909 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
910                                              unformat_input_t * input,
911                                              vlib_cli_command_t * cmd)
912 {
913   unformat_input_t _line_input, *line_input = &_line_input;
914   u8 is_enabled = 0;
915   u8 is_set = 0;
916   clib_error_t *error = NULL;
917
918   /* Get a line of input. */
919   if (!unformat_user (input, unformat_line_input, line_input))
920     return clib_error_return (0, "expected enable | disable");
921
922   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
923     {
924       if (unformat (line_input, "enable"))
925         {
926           is_set = 1;
927           is_enabled = 1;
928         }
929       else if (unformat (line_input, "disable"))
930         is_set = 1;
931       else
932         {
933           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
934                            line_input);
935           goto done;
936         }
937     }
938
939   if (!is_set)
940     {
941       vlib_cli_output (vm, "state not set!");
942       goto done;
943     }
944
945   vnet_lisp_map_register_enable_disable (is_enabled);
946
947 done:
948   unformat_free (line_input);
949
950   return error;
951 }
952
953 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
954     .path = "lisp map-register",
955     .short_help = "lisp map-register [enable|disable]",
956     .function = lisp_map_register_enable_disable_command_fn,
957 };
958
959 static clib_error_t *
960 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
961                                            unformat_input_t * input,
962                                            vlib_cli_command_t * cmd)
963 {
964   unformat_input_t _line_input, *line_input = &_line_input;
965   u8 is_enabled = 0;
966   u8 is_set = 0;
967   clib_error_t *error = NULL;
968
969   /* Get a line of input. */
970   if (!unformat_user (input, unformat_line_input, line_input))
971     return clib_error_return (0, "expected enable | disable");
972
973   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
974     {
975       if (unformat (line_input, "enable"))
976         {
977           is_set = 1;
978           is_enabled = 1;
979         }
980       else if (unformat (line_input, "disable"))
981         is_set = 1;
982       else
983         {
984           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
985                            line_input);
986           goto done;
987         }
988     }
989
990   if (!is_set)
991     {
992       vlib_cli_output (vm, "state not set!");
993       goto done;
994     }
995
996   vnet_lisp_rloc_probe_enable_disable (is_enabled);
997
998 done:
999   unformat_free (line_input);
1000
1001   return error;
1002 }
1003
1004 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
1005     .path = "lisp rloc-probe",
1006     .short_help = "lisp rloc-probe [enable|disable]",
1007     .function = lisp_rloc_probe_enable_disable_command_fn,
1008 };
1009
1010 static u8 *
1011 format_lisp_status (u8 * s, va_list * args)
1012 {
1013   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1014   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1015 }
1016
1017 static clib_error_t *
1018 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1019                              vlib_cli_command_t * cmd)
1020 {
1021   u8 *msg = 0;
1022   msg = format (msg, "feature: %U\ngpe: %U\n",
1023                 format_lisp_status, format_vnet_lisp_gpe_status);
1024   vlib_cli_output (vm, "%v", msg);
1025   vec_free (msg);
1026   return 0;
1027 }
1028
1029 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1030     .path = "show lisp status",
1031     .short_help = "show lisp status",
1032     .function = lisp_show_status_command_fn,
1033 };
1034
1035 static clib_error_t *
1036 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
1037                                     unformat_input_t * input,
1038                                     vlib_cli_command_t * cmd)
1039 {
1040   hash_pair_t *p;
1041   unformat_input_t _line_input, *line_input = &_line_input;
1042   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1043   uword *vni_table = 0;
1044   u8 is_l2 = 0;
1045   clib_error_t *error = NULL;
1046
1047   /* Get a line of input. */
1048   if (!unformat_user (input, unformat_line_input, line_input))
1049     return 0;
1050
1051   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1052     {
1053       if (unformat (line_input, "l2"))
1054         {
1055           vni_table = lcm->bd_id_by_vni;
1056           is_l2 = 1;
1057         }
1058       else if (unformat (line_input, "l3"))
1059         {
1060           vni_table = lcm->table_id_by_vni;
1061           is_l2 = 0;
1062         }
1063       else
1064         {
1065           error = clib_error_return (0, "parse error: '%U'",
1066                                      format_unformat_error, line_input);
1067           goto done;
1068         }
1069     }
1070
1071   if (!vni_table)
1072     {
1073       vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
1074       goto done;
1075     }
1076
1077   vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
1078
1079   hash_foreach_pair (p, vni_table,
1080   ({
1081     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
1082   }));
1083
1084 done:
1085   unformat_free (line_input);
1086
1087   return error;
1088 }
1089
1090 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
1091     .path = "show lisp eid-table map",
1092     .short_help = "show lisp eid-table map l2|l3",
1093     .function = lisp_show_eid_table_map_command_fn,
1094 };
1095
1096
1097 static clib_error_t *
1098 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
1099                                      unformat_input_t * input,
1100                                      vlib_cli_command_t * cmd)
1101 {
1102   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1103   vnet_main_t *vnm = lgm->vnet_main;
1104   unformat_input_t _line_input, *line_input = &_line_input;
1105   u8 is_add = 1;
1106   clib_error_t *error = 0;
1107   u8 *locator_set_name = 0;
1108   locator_t locator, *locators = 0;
1109   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
1110   u32 ls_index = 0;
1111   int rv = 0;
1112
1113   clib_memset (&locator, 0, sizeof (locator));
1114   clib_memset (a, 0, sizeof (a[0]));
1115
1116   /* Get a line of input. */
1117   if (!unformat_user (input, unformat_line_input, line_input))
1118     return clib_error_return (0, "missing parameters");
1119
1120   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1121     {
1122       if (unformat (line_input, "add %_%v%_", &locator_set_name))
1123         is_add = 1;
1124       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1125         is_add = 0;
1126       else if (unformat (line_input, "iface %U p %d w %d",
1127                          unformat_vnet_sw_interface, vnm,
1128                          &locator.sw_if_index, &locator.priority,
1129                          &locator.weight))
1130         {
1131           locator.local = 1;
1132           locator.state = 1;
1133           vec_add1 (locators, locator);
1134         }
1135       else
1136         {
1137           error = unformat_parse_error (line_input);
1138           goto done;
1139         }
1140     }
1141
1142   vec_terminate_c_string (locator_set_name);
1143   a->name = locator_set_name;
1144   a->locators = locators;
1145   a->is_add = is_add;
1146   a->local = 1;
1147
1148   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
1149   if (0 != rv)
1150     {
1151       error = clib_error_return (0, "failed to %s locator-set!",
1152                                  is_add ? "add" : "delete");
1153     }
1154
1155 done:
1156   vec_free (locators);
1157   if (locator_set_name)
1158     vec_free (locator_set_name);
1159   unformat_free (line_input);
1160
1161   return error;
1162 }
1163
1164 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
1165     .path = "lisp locator-set",
1166     .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
1167         "p <priority> w <weight>]",
1168     .function = lisp_add_del_locator_set_command_fn,
1169 };
1170
1171 static clib_error_t *
1172 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
1173                                         unformat_input_t * input,
1174                                         vlib_cli_command_t * cmd)
1175 {
1176   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1177   vnet_main_t *vnm = lgm->vnet_main;
1178   unformat_input_t _line_input, *line_input = &_line_input;
1179   u8 is_add = 1;
1180   clib_error_t *error = 0;
1181   u8 *locator_set_name = 0;
1182   u8 locator_set_name_set = 0;
1183   locator_t locator, *locators = 0;
1184   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
1185   u32 ls_index = 0;
1186
1187   clib_memset (&locator, 0, sizeof (locator));
1188   clib_memset (a, 0, sizeof (a[0]));
1189
1190   /* Get a line of input. */
1191   if (!unformat_user (input, unformat_line_input, line_input))
1192     return 0;
1193
1194   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1195     {
1196       if (unformat (line_input, "add"))
1197         is_add = 1;
1198       else if (unformat (line_input, "del"))
1199         is_add = 0;
1200       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
1201         locator_set_name_set = 1;
1202       else if (unformat (line_input, "iface %U p %d w %d",
1203                          unformat_vnet_sw_interface, vnm,
1204                          &locator.sw_if_index, &locator.priority,
1205                          &locator.weight))
1206         {
1207           locator.local = 1;
1208           vec_add1 (locators, locator);
1209         }
1210       else
1211         {
1212           error = unformat_parse_error (line_input);
1213           goto done;
1214         }
1215     }
1216
1217   if (!locator_set_name_set)
1218     {
1219       error = clib_error_return (0, "locator_set name not set!");
1220       goto done;
1221     }
1222
1223   a->name = locator_set_name;
1224   a->locators = locators;
1225   a->is_add = is_add;
1226   a->local = 1;
1227
1228   vnet_lisp_add_del_locator (a, 0, &ls_index);
1229
1230 done:
1231   vec_free (locators);
1232   vec_free (locator_set_name);
1233   unformat_free (line_input);
1234
1235   return error;
1236 }
1237
1238 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
1239     .path = "lisp locator",
1240     .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
1241                   "p <priority> w <weight>",
1242     .function = lisp_add_del_locator_in_set_command_fn,
1243 };
1244
1245 static clib_error_t *
1246 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1247                                       unformat_input_t * input,
1248                                       vlib_cli_command_t * cmd)
1249 {
1250   locator_set_t *lsit;
1251   locator_t *loc;
1252   u32 *locit;
1253   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1254
1255   vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
1256                    "Priority", "Weight");
1257
1258   pool_foreach (lsit, lcm->locator_set_pool)
1259    {
1260     u8 * msg = 0;
1261     int next_line = 0;
1262     if (lsit->local)
1263       {
1264         msg = format (msg, "%v", lsit->name);
1265       }
1266     else
1267       {
1268         msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
1269       }
1270     vec_foreach (locit, lsit->locator_indices)
1271       {
1272         if (next_line)
1273           {
1274             msg = format (msg, "%16s", " ");
1275           }
1276         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1277         if (loc->local)
1278           msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
1279                         loc->weight);
1280         else
1281           msg = format (msg, "%16U%16d%16d\n", format_ip_address,
1282                         &gid_address_ip(&loc->address), loc->priority,
1283                         loc->weight);
1284         next_line = 1;
1285       }
1286     vlib_cli_output (vm, "%v", msg);
1287     vec_free (msg);
1288   }
1289   return 0;
1290 }
1291
1292 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
1293     .path = "show lisp locator-set",
1294     .short_help = "Shows locator-sets",
1295     .function = lisp_cp_show_locator_sets_command_fn,
1296 };
1297
1298
1299 static clib_error_t *
1300 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1301                                       unformat_input_t * input,
1302                                       vlib_cli_command_t * cmd)
1303 {
1304   unformat_input_t _line_input, *line_input = &_line_input;
1305   u8 is_add = 1, addr_set = 0;
1306   ip_address_t ip_addr;
1307   clib_error_t *error = 0;
1308   int rv = 0;
1309   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
1310
1311   /* Get a line of input. */
1312   if (!unformat_user (input, unformat_line_input, line_input))
1313     return 0;
1314
1315   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1316     {
1317       if (unformat (line_input, "add"))
1318         is_add = 1;
1319       else if (unformat (line_input, "del"))
1320         is_add = 0;
1321       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1322         addr_set = 1;
1323       else
1324         {
1325           error = unformat_parse_error (line_input);
1326           goto done;
1327         }
1328     }
1329
1330   if (!addr_set)
1331     {
1332       error = clib_error_return (0, "Map-resolver address must be set!");
1333       goto done;
1334     }
1335
1336   a->is_add = is_add;
1337   a->address = ip_addr;
1338   rv = vnet_lisp_add_del_map_resolver (a);
1339   if (0 != rv)
1340     {
1341       error = clib_error_return (0, "failed to %s map-resolver!",
1342                                  is_add ? "add" : "delete");
1343     }
1344
1345 done:
1346   unformat_free (line_input);
1347
1348   return error;
1349 }
1350
1351 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
1352     .path = "lisp map-resolver",
1353     .short_help = "lisp map-resolver add/del <ip_address>",
1354     .function = lisp_add_del_map_resolver_command_fn,
1355 };
1356
1357
1358 static clib_error_t *
1359 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1360                                         unformat_input_t * input,
1361                                         vlib_cli_command_t * cmd)
1362 {
1363   unformat_input_t _line_input, *line_input = &_line_input;
1364   u8 is_add = 1;
1365   u8 *locator_set_name = 0;
1366   clib_error_t *error = 0;
1367   int rv = 0;
1368   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
1369
1370   /* Get a line of input. */
1371   if (!unformat_user (input, unformat_line_input, line_input))
1372     return 0;
1373
1374   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1375     {
1376       if (unformat (line_input, "del"))
1377         is_add = 0;
1378       else if (unformat (line_input, "add %_%v%_", &locator_set_name))
1379         is_add = 1;
1380       else
1381         {
1382           error = unformat_parse_error (line_input);
1383           goto done;
1384         }
1385     }
1386
1387   vec_terminate_c_string (locator_set_name);
1388   a->is_add = is_add;
1389   a->locator_set_name = locator_set_name;
1390   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
1391   if (0 != rv)
1392     {
1393       error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
1394                                  is_add ? "add" : "delete");
1395     }
1396
1397 done:
1398   vec_free (locator_set_name);
1399   unformat_free (line_input);
1400
1401   return error;
1402 }
1403
1404 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
1405     .path = "lisp map-request itr-rlocs",
1406     .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
1407     .function = lisp_add_del_mreq_itr_rlocs_command_fn,
1408 };
1409
1410 static clib_error_t *
1411 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1412                                      unformat_input_t * input,
1413                                      vlib_cli_command_t * cmd)
1414 {
1415   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1416   locator_set_t *loc_set;
1417
1418   vlib_cli_output (vm, "%=20s", "itr-rlocs");
1419
1420   if (~0 == lcm->mreq_itr_rlocs)
1421     {
1422       return 0;
1423     }
1424
1425   loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
1426
1427   vlib_cli_output (vm, "%=20s", loc_set->name);
1428
1429   return 0;
1430 }
1431
1432 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
1433     .path = "show lisp map-request itr-rlocs",
1434     .short_help = "Shows map-request itr-rlocs",
1435     .function = lisp_show_mreq_itr_rlocs_command_fn,
1436 };
1437
1438 static clib_error_t *
1439 lisp_use_petr_set_locator_set_command_fn (vlib_main_t * vm,
1440                                           unformat_input_t * input,
1441                                           vlib_cli_command_t * cmd)
1442 {
1443   u8 is_add = 1, ip_set = 0;
1444   unformat_input_t _line_input, *line_input = &_line_input;
1445   clib_error_t *error = 0;
1446   ip_address_t ip;
1447
1448   /* Get a line of input. */
1449   if (!unformat_user (input, unformat_line_input, line_input))
1450     return 0;
1451
1452   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1453     {
1454       if (unformat (line_input, "%U", unformat_ip_address, &ip))
1455         ip_set = 1;
1456       else if (unformat (line_input, "disable"))
1457         is_add = 0;
1458       else
1459         {
1460           error = clib_error_return (0, "parse error");
1461           goto done;
1462         }
1463     }
1464
1465   if (!ip_set)
1466     {
1467       clib_warning ("No petr IP specified!");
1468       goto done;
1469     }
1470
1471   if (vnet_lisp_use_petr (&ip, is_add))
1472     {
1473       error = clib_error_return (0, "failed to %s petr!",
1474                                  is_add ? "add" : "delete");
1475     }
1476
1477 done:
1478   unformat_free (line_input);
1479
1480   return error;
1481 }
1482
1483 VLIB_CLI_COMMAND (lisp_use_petr_set_locator_set_command) = {
1484     .path = "lisp use-petr",
1485     .short_help = "lisp use-petr [disable] <petr-ip>",
1486     .function = lisp_use_petr_set_locator_set_command_fn,
1487 };
1488
1489 static clib_error_t *
1490 lisp_show_petr_command_fn (vlib_main_t * vm,
1491                            unformat_input_t * input, vlib_cli_command_t * cmd)
1492 {
1493   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1494   mapping_t *m;
1495   locator_set_t *ls;
1496   locator_t *loc;
1497   u8 *tmp_str = 0;
1498   u8 use_petr = lcm->flags & LISP_FLAG_USE_PETR;
1499   vlib_cli_output (vm, "%=20s%=16s", "petr", use_petr ? "ip" : "");
1500
1501   if (!use_petr)
1502     {
1503       vlib_cli_output (vm, "%=20s", "disable");
1504       return 0;
1505     }
1506
1507   if (~0 == lcm->petr_map_index)
1508     {
1509       tmp_str = format (0, "N/A");
1510     }
1511   else
1512     {
1513       m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1514       if (~0 != m->locator_set_index)
1515         {
1516           ls = pool_elt_at_index(lcm->locator_set_pool, m->locator_set_index);
1517           loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
1518           tmp_str = format (0, "%U", format_ip_address, &loc->address);
1519         }
1520       else
1521         {
1522           tmp_str = format (0, "N/A");
1523         }
1524     }
1525   vec_add1 (tmp_str, 0);
1526
1527   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1528
1529   vec_free (tmp_str);
1530
1531   return 0;
1532 }
1533
1534 VLIB_CLI_COMMAND (lisp_show_petr_command) = {
1535     .path = "show lisp petr",
1536     .short_help = "Show petr",
1537     .function = lisp_show_petr_command_fn,
1538 };
1539
1540 /*
1541  * fd.io coding-style-patch-verification: ON
1542  *
1543  * Local Variables:
1544  * eval: (c-set-style "gnu")
1545  * End:
1546  */