LISP: L2 ARP handling
[vpp.git] / src / vnet / lisp-cp / one_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 <vnet/lisp-cp/control.h>
17 #include <vnet/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
29   /* Get a line of input. */
30   if (!unformat_user (input, unformat_line_input, line_input))
31     return 0;
32
33   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
34     {
35       if (unformat (line_input, "vni %d", &vni))
36         ;
37       else
38         {
39           vlib_cli_output (vm, "parse error: '%U'",
40                            format_unformat_error, line_input);
41           unformat_free (line_input);
42           return 0;
43         }
44     }
45   unformat_free (line_input);
46
47   if (~0 == vni)
48     {
49       vlib_cli_output (vm, "error: no vni specified!");
50       return 0;
51     }
52
53   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
54
55   vec_foreach (adj, adjs)
56   {
57     vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
58                      format_gid_address, &adj->reid);
59   }
60   vec_free (adjs);
61
62   return 0;
63 }
64
65 /* *INDENT-OFF* */
66 VLIB_CLI_COMMAND (one_show_adjacencies_command) = {
67     .path = "show one adjacencies",
68     .short_help = "show one adjacencies",
69     .function = lisp_show_adjacencies_command_fn,
70 };
71 /* *INDENT-ON* */
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
83   /* Get a line of input. */
84   if (!unformat_user (input, unformat_line_input, line_input))
85     return 0;
86
87   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
88     {
89       if (unformat (line_input, "add"))
90         is_add = 1;
91       else if (unformat (line_input, "del"))
92         is_add = 0;
93       else if (unformat (line_input, "%U", unformat_ip_address, &ip))
94         ip_set = 1;
95       else
96         {
97           vlib_cli_output (vm, "parse error: '%U'",
98                            format_unformat_error, line_input);
99           unformat_free (line_input);
100           return 0;
101         }
102     }
103   unformat_free (line_input);
104
105   if (!ip_set)
106     {
107       vlib_cli_output (vm, "map-server ip address not set!");
108       return 0;
109     }
110
111   rv = vnet_lisp_add_del_map_server (&ip, is_add);
112   if (!rv)
113     vlib_cli_output (vm, "failed to %s map-server!",
114                      is_add ? "add" : "delete");
115
116   return 0;
117 }
118
119 /* *INDENT-OFF* */
120 VLIB_CLI_COMMAND (one_add_del_map_server_command) = {
121     .path = "one map-server",
122     .short_help = "one map-server add|del <ip>",
123     .function = lisp_add_del_map_server_command_fn,
124 };
125 /* *INDENT-ON* */
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   memset (&eid, 0, sizeof (eid));
148   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           p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
172           if (!p)
173             {
174               error = clib_error_return (0, "locator-set %s doesn't exist",
175                                          locator_set_name);
176               goto done;
177             }
178           locator_set_index = p[0];
179         }
180       else
181         {
182           error = unformat_parse_error (line_input);
183           goto done;
184         }
185     }
186   /* XXX treat batch configuration */
187
188   if (GID_ADDR_SRC_DST == gid_address_type (&eid))
189     {
190       error =
191         clib_error_return (0, "src/dst is not supported for local EIDs!");
192       goto done;
193     }
194
195   if (key && (0 == key_id))
196     {
197       vlib_cli_output (vm, "invalid key_id!");
198       goto done;
199     }
200
201   gid_address_copy (&a->eid, &eid);
202   a->is_add = is_add;
203   a->locator_set_index = locator_set_index;
204   a->local = 1;
205   a->key = key;
206   a->key_id = key_id;
207
208   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
209   if (0 != rv)
210     {
211       error = clib_error_return (0, "failed to %s local mapping!",
212                                  is_add ? "add" : "delete");
213     }
214 done:
215   vec_free (eids);
216   if (locator_set_name)
217     vec_free (locator_set_name);
218   gid_address_free (&a->eid);
219   vec_free (a->key);
220   unformat_free (line_input);
221   return error;
222 }
223
224 /* *INDENT-OFF* */
225 VLIB_CLI_COMMAND (one_add_del_local_eid_command) = {
226     .path = "one eid-table",
227     .short_help = "one eid-table add/del [vni <vni>] eid <eid> "
228       "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
229     .function = lisp_add_del_local_eid_command_fn,
230 };
231 /* *INDENT-ON* */
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 /* *INDENT-OFF* */
272 VLIB_CLI_COMMAND (one_eid_table_map_command) = {
273     .path = "one eid-table map",
274     .short_help = "one eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
275     .function = lisp_eid_table_map_command_fn,
276 };
277 /* *INDENT-ON* */
278
279 static clib_error_t *
280 lisp_add_del_l2_arp_entry_command_fn (vlib_main_t * vm,
281                                       unformat_input_t * input,
282                                       vlib_cli_command_t * cmd)
283 {
284   unformat_input_t _line_input, *line_input = &_line_input;
285   clib_error_t *error = NULL;
286   int rc = 0;
287   u8 hw_addr[6], bd = 0;
288   ip4_address_t ip4;
289   u32 hw_addr_set = 0, ip_set = 0, is_add = 1;
290   gid_address_t _arp, *arp = &_arp;
291
292   memset (&ip4, 0, sizeof (ip4));
293   memset (hw_addr, 0, sizeof (hw_addr));
294   memset (arp, 0, sizeof (*arp));
295
296   if (!unformat_user (input, unformat_line_input, line_input))
297     return 0;
298
299   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
300     {
301       if (unformat (line_input, "mac %U", unformat_mac_address, hw_addr))
302         hw_addr_set = 1;
303       else if (unformat (line_input, "ip %U", unformat_ip4_address, &ip4))
304         ip_set = 1;
305       else if (unformat (line_input, "del"))
306         is_add = 0;
307       else if (unformat (line_input, "bd %d", &bd))
308         ;
309       else
310         {
311           error = clib_error_return (0, "parse error");
312           goto done;
313         }
314     }
315
316   if (!ip_set || (!hw_addr_set && is_add))
317     {
318       vlib_cli_output (vm, "expected IP and MAC addresses!");
319       return 0;
320     }
321
322   /* build GID address */
323   gid_address_arp_ip4 (arp) = ip4;
324   gid_address_arp_bd (arp) = bd;
325   gid_address_type (arp) = GID_ADDR_ARP;
326   rc = vnet_lisp_add_del_l2_arp_entry (arp, hw_addr, is_add);
327   if (rc)
328     clib_warning ("Failed to %s l2 arp entry!", is_add ? "add" : "delete");
329
330 done:
331   unformat_free (line_input);
332   return error;
333 }
334
335 /* *INDENT-OFF* */
336 VLIB_CLI_COMMAND (one_add_del_l2_arp_entry_command) = {
337     .path = "one l2 arp",
338     .short_help = "one l2 arp [del] bd <bd> mac <mac> ip <ipv4>",
339     .function = lisp_add_del_l2_arp_entry_command_fn,
340 };
341 /* *INDENT-ON* */
342
343 static clib_error_t *
344 lisp_show_l2_arp_entries_command_fn (vlib_main_t * vm,
345                                      unformat_input_t * input,
346                                      vlib_cli_command_t * cmd)
347 {
348   u32 *ht = vnet_lisp_l2_arp_bds_get ();
349   lisp_api_l2_arp_entry_t *entries, *e;
350   hash_pair_t *p;
351
352   /* *INDENT-OFF* */
353   hash_foreach_pair (p, ht,
354   ({
355     entries = vnet_lisp_l2_arp_entries_get_by_bd (p->key);
356     vlib_cli_output (vm, "Table: %d", p->key);
357
358     vec_foreach (e, entries)
359       {
360         vlib_cli_output (vm, "\t%U -> %U", format_ip4_address, &e->ip4,
361                          format_mac_address, e->mac);
362       }
363     vec_free (entries);
364   }));
365   /* *INDENT-ON* */
366
367   hash_free (ht);
368   return 0;
369 }
370
371 /* *INDENT-OFF* */
372 VLIB_CLI_COMMAND (one_show_l2_arp_entries_command) = {
373     .path = "show one l2 arp entries",
374     .short_help = "Show ONE L2 ARP entries",
375     .function = lisp_show_l2_arp_entries_command_fn,
376 };
377 /* *INDENT-ON* */
378
379 /**
380  * Handler for add/del remote mapping CLI.
381  *
382  * @param vm vlib context
383  * @param input input from user
384  * @param cmd cmd
385  * @return pointer to clib error structure
386  */
387 static clib_error_t *
388 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
389                                         unformat_input_t * input,
390                                         vlib_cli_command_t * cmd)
391 {
392   clib_error_t *error = 0;
393   unformat_input_t _line_input, *line_input = &_line_input;
394   u8 is_add = 1, del_all = 0;
395   locator_t rloc, *rlocs = 0, *curr_rloc = 0;
396   gid_address_t eid;
397   u8 eid_set = 0;
398   u32 vni, action = ~0, p, w;
399   int rv;
400
401   /* Get a line of input. */
402   if (!unformat_user (input, unformat_line_input, line_input))
403     return 0;
404
405   memset (&eid, 0, sizeof (eid));
406   memset (&rloc, 0, sizeof (rloc));
407
408   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
409     {
410       if (unformat (line_input, "del-all"))
411         del_all = 1;
412       else if (unformat (line_input, "del"))
413         is_add = 0;
414       else if (unformat (line_input, "add"))
415         ;
416       else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
417         eid_set = 1;
418       else if (unformat (line_input, "vni %u", &vni))
419         {
420           gid_address_vni (&eid) = vni;
421         }
422       else if (unformat (line_input, "p %d w %d", &p, &w))
423         {
424           if (!curr_rloc)
425             {
426               clib_warning
427                 ("No RLOC configured for setting priority/weight!");
428               goto done;
429             }
430           curr_rloc->priority = p;
431           curr_rloc->weight = w;
432         }
433       else if (unformat (line_input, "rloc %U", unformat_ip_address,
434                          &gid_address_ip (&rloc.address)))
435         {
436           /* since rloc is stored in ip prefix we need to set prefix length */
437           ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
438
439           u8 version = gid_address_ip_version (&rloc.address);
440           ip_prefix_len (pref) = ip_address_max_len (version);
441
442           vec_add1 (rlocs, rloc);
443           curr_rloc = &rlocs[vec_len (rlocs) - 1];
444         }
445       else if (unformat (line_input, "action %U",
446                          unformat_negative_mapping_action, &action))
447         ;
448       else
449         {
450           clib_warning ("parse error");
451           goto done;
452         }
453     }
454
455   if (!del_all && !eid_set)
456     {
457       clib_warning ("missing eid!");
458       goto done;
459     }
460
461   if (!del_all)
462     {
463       if (is_add && (~0 == action) && 0 == vec_len (rlocs))
464         {
465           clib_warning ("no action set for negative map-reply!");
466           goto done;
467         }
468     }
469   else
470     {
471       vnet_lisp_clear_all_remote_adjacencies ();
472       goto done;
473     }
474
475   /* if it's a delete, clean forwarding */
476   if (!is_add)
477     {
478       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
479       memset (a, 0, sizeof (a[0]));
480       gid_address_copy (&a->reid, &eid);
481       if (vnet_lisp_add_del_adjacency (a))
482         {
483           clib_warning ("failed to delete adjacency!");
484           goto done;
485         }
486     }
487
488   /* add as static remote mapping, i.e., not authoritative and infinite
489    * ttl */
490   rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
491                                   1 /* is_static */ , 0);
492
493   if (rv)
494     clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
495
496 done:
497   vec_free (rlocs);
498   unformat_free (line_input);
499   return error;
500 }
501
502 /* *INDENT-OFF* */
503 VLIB_CLI_COMMAND (one_add_del_remote_mapping_command) = {
504   .path = "one remote-mapping",
505   .short_help =
506     "one remote-mapping add|del [del-all] vni <vni> "
507     "eid <est-eid> [action <no-action|natively-forward|"
508     "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
509     "[rloc <dst-locator> ... ]",
510   .function = lisp_add_del_remote_mapping_command_fn,
511 };
512 /* *INDENT-ON* */
513
514 /**
515  * Handler for add/del adjacency CLI.
516  */
517 static clib_error_t *
518 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
519                                    vlib_cli_command_t * cmd)
520 {
521   clib_error_t *error = 0;
522   unformat_input_t _line_input, *line_input = &_line_input;
523   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
524   u8 is_add = 1;
525   ip_prefix_t *reid_ippref, *leid_ippref;
526   gid_address_t leid, reid;
527   u8 *dmac = gid_address_mac (&reid);
528   u8 *smac = gid_address_mac (&leid);
529   u8 reid_set = 0, leid_set = 0;
530   u32 vni;
531
532   /* Get a line of input. */
533   if (!unformat_user (input, unformat_line_input, line_input))
534     return 0;
535
536   memset (&reid, 0, sizeof (reid));
537   memset (&leid, 0, sizeof (leid));
538
539   leid_ippref = &gid_address_ippref (&leid);
540   reid_ippref = &gid_address_ippref (&reid);
541
542   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
543     {
544       if (unformat (line_input, "del"))
545         is_add = 0;
546       else if (unformat (line_input, "add"))
547         ;
548       else if (unformat (line_input, "reid %U",
549                          unformat_ip_prefix, reid_ippref))
550         {
551           gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
552           reid_set = 1;
553         }
554       else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
555         {
556           gid_address_type (&reid) = GID_ADDR_MAC;
557           reid_set = 1;
558         }
559       else if (unformat (line_input, "vni %u", &vni))
560         {
561           gid_address_vni (&leid) = vni;
562           gid_address_vni (&reid) = vni;
563         }
564       else if (unformat (line_input, "leid %U",
565                          unformat_ip_prefix, leid_ippref))
566         {
567           gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
568           leid_set = 1;
569         }
570       else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
571         {
572           gid_address_type (&leid) = GID_ADDR_MAC;
573           leid_set = 1;
574         }
575       else
576         {
577           clib_warning ("parse error");
578           goto done;
579         }
580     }
581
582   if (!reid_set || !leid_set)
583     {
584       clib_warning ("missing remote or local eid!");
585       goto done;
586     }
587
588   if ((gid_address_type (&leid) != gid_address_type (&reid))
589       || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
590           && ip_prefix_version (reid_ippref)
591           != ip_prefix_version (leid_ippref)))
592     {
593       clib_warning ("remote and local EIDs are of different types!");
594       goto done;
595     }
596
597   memset (a, 0, sizeof (a[0]));
598   gid_address_copy (&a->leid, &leid);
599   gid_address_copy (&a->reid, &reid);
600   a->is_add = is_add;
601
602   if (vnet_lisp_add_del_adjacency (a))
603     clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
604
605 done:
606   unformat_free (line_input);
607   return error;
608 }
609
610 /* *INDENT-OFF* */
611 VLIB_CLI_COMMAND (one_add_del_adjacency_command) = {
612     .path = "one adjacency",
613     .short_help = "one adjacency add|del vni <vni> reid <remote-eid> "
614       "leid <local-eid>",
615     .function = lisp_add_del_adjacency_command_fn,
616 };
617 /* *INDENT-ON* */
618
619
620 static clib_error_t *
621 lisp_map_request_mode_command_fn (vlib_main_t * vm,
622                                   unformat_input_t * input,
623                                   vlib_cli_command_t * cmd)
624 {
625   unformat_input_t _i, *i = &_i;
626   map_request_mode_t mr_mode = _MR_MODE_MAX;
627
628   /* Get a line of input. */
629   if (!unformat_user (input, unformat_line_input, i))
630     return 0;
631
632   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
633     {
634       if (unformat (i, "dst-only"))
635         mr_mode = MR_MODE_DST_ONLY;
636       else if (unformat (i, "src-dst"))
637         mr_mode = MR_MODE_SRC_DST;
638       else
639         {
640           clib_warning ("parse error '%U'", format_unformat_error, i);
641           goto done;
642         }
643     }
644
645   if (_MR_MODE_MAX == mr_mode)
646     {
647       clib_warning ("No map request mode entered!");
648       goto done;
649     }
650
651   vnet_lisp_set_map_request_mode (mr_mode);
652
653 done:
654   unformat_free (i);
655
656   return 0;
657 }
658
659 /* *INDENT-OFF* */
660 VLIB_CLI_COMMAND (one_map_request_mode_command) = {
661     .path = "one map-request mode",
662     .short_help = "one map-request mode dst-only|src-dst",
663     .function = lisp_map_request_mode_command_fn,
664 };
665 /* *INDENT-ON* */
666
667
668 static u8 *
669 format_lisp_map_request_mode (u8 * s, va_list * args)
670 {
671   u32 mode = va_arg (*args, u32);
672
673   switch (mode)
674     {
675     case 0:
676       return format (0, "dst-only");
677     case 1:
678       return format (0, "src-dst");
679     }
680   return 0;
681 }
682
683 static clib_error_t *
684 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
685                                        unformat_input_t * input,
686                                        vlib_cli_command_t * cmd)
687 {
688   vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
689                    vnet_lisp_get_map_request_mode ());
690   return 0;
691 }
692
693 /* *INDENT-OFF* */
694 VLIB_CLI_COMMAND (one_show_map_request_mode_command) = {
695     .path = "show one map-request mode",
696     .short_help = "show one map-request mode",
697     .function = lisp_show_map_request_mode_command_fn,
698 };
699 /* *INDENT-ON* */
700
701 static clib_error_t *
702 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
703                                     unformat_input_t * input,
704                                     vlib_cli_command_t * cmd)
705 {
706   lisp_msmr_t *mr;
707   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
708
709   vec_foreach (mr, lcm->map_resolvers)
710   {
711     vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
712   }
713   return 0;
714 }
715
716 /* *INDENT-OFF* */
717 VLIB_CLI_COMMAND (one_show_map_resolvers_command) = {
718     .path = "show one map-resolvers",
719     .short_help = "show one map-resolvers",
720     .function = lisp_show_map_resolvers_command_fn,
721 };
722 /* *INDENT-ON* */
723
724
725 static clib_error_t *
726 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
727                                       unformat_input_t * input,
728                                       vlib_cli_command_t * cmd)
729 {
730   u8 locator_name_set = 0;
731   u8 *locator_set_name = 0;
732   u8 is_add = 1;
733   unformat_input_t _line_input, *line_input = &_line_input;
734   clib_error_t *error = 0;
735   int rv = 0;
736
737   /* Get a line of input. */
738   if (!unformat_user (input, unformat_line_input, line_input))
739     return 0;
740
741   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
742     {
743       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
744         locator_name_set = 1;
745       else if (unformat (line_input, "disable"))
746         is_add = 0;
747       else
748         {
749           error = clib_error_return (0, "parse error");
750           goto done;
751         }
752     }
753
754   if (!locator_name_set)
755     {
756       clib_warning ("No locator set specified!");
757       goto done;
758     }
759   rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
760   if (0 != rv)
761     {
762       error = clib_error_return (0, "failed to %s pitr!",
763                                  is_add ? "add" : "delete");
764     }
765
766 done:
767   if (locator_set_name)
768     vec_free (locator_set_name);
769   unformat_free (line_input);
770   return error;
771 }
772
773 /* *INDENT-OFF* */
774 VLIB_CLI_COMMAND (one_pitr_set_locator_set_command) = {
775     .path = "one pitr",
776     .short_help = "one pitr [disable] ls <locator-set-name>",
777     .function = lisp_pitr_set_locator_set_command_fn,
778 };
779 /* *INDENT-ON* */
780
781 static clib_error_t *
782 lisp_show_pitr_command_fn (vlib_main_t * vm,
783                            unformat_input_t * input, vlib_cli_command_t * cmd)
784 {
785   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
786   mapping_t *m;
787   locator_set_t *ls;
788   u8 *tmp_str = 0;
789
790   vlib_cli_output (vm, "%=20s%=16s",
791                    "pitr", lcm->lisp_pitr ? "locator-set" : "");
792
793   if (!lcm->lisp_pitr)
794     {
795       vlib_cli_output (vm, "%=20s", "disable");
796       return 0;
797     }
798
799   if (~0 == lcm->pitr_map_index)
800     {
801       tmp_str = format (0, "N/A");
802     }
803   else
804     {
805       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
806       if (~0 != m->locator_set_index)
807         {
808           ls =
809             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
810           tmp_str = format (0, "%s", ls->name);
811         }
812       else
813         {
814           tmp_str = format (0, "N/A");
815         }
816     }
817   vec_add1 (tmp_str, 0);
818
819   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
820
821   vec_free (tmp_str);
822
823   return 0;
824 }
825
826 /* *INDENT-OFF* */
827 VLIB_CLI_COMMAND (one_show_pitr_command) = {
828     .path = "show one pitr",
829     .short_help = "Show pitr",
830     .function = lisp_show_pitr_command_fn,
831 };
832 /* *INDENT-ON* */
833
834 static u8 *
835 format_eid_entry (u8 * s, va_list * args)
836 {
837   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
838   lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
839   mapping_t *mapit = va_arg (*args, mapping_t *);
840   locator_set_t *ls = va_arg (*args, locator_set_t *);
841   gid_address_t *gid = &mapit->eid;
842   u32 ttl = mapit->ttl;
843   u8 aut = mapit->authoritative;
844   u32 *loc_index;
845   u8 first_line = 1;
846   u8 *loc;
847
848   u8 *type = ls->local ? format (0, "local(%s)", ls->name)
849     : format (0, "remote");
850
851   if (vec_len (ls->locator_indices) == 0)
852     {
853       s = format (s, "%-35U%-20saction:%-30U%-20u%-u", format_gid_address,
854                   gid, type, format_negative_mapping_action, mapit->action,
855                   ttl, aut);
856     }
857   else
858     {
859       vec_foreach (loc_index, ls->locator_indices)
860       {
861         locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
862         if (l->local)
863           loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
864                         l->sw_if_index);
865         else
866           loc = format (0, "%U", format_ip_address,
867                         &gid_address_ip (&l->address));
868
869         if (first_line)
870           {
871             s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
872                         gid, type, loc, ttl, aut);
873             first_line = 0;
874           }
875         else
876           s = format (s, "%55s%v\n", "", loc);
877       }
878     }
879   return s;
880 }
881
882 static clib_error_t *
883 lisp_show_eid_table_command_fn (vlib_main_t * vm,
884                                 unformat_input_t * input,
885                                 vlib_cli_command_t * cmd)
886 {
887   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
888   mapping_t *mapit;
889   unformat_input_t _line_input, *line_input = &_line_input;
890   u32 mi;
891   gid_address_t eid;
892   u8 print_all = 1;
893   u8 filter = 0;
894   clib_error_t *error = NULL;
895
896   memset (&eid, 0, sizeof (eid));
897
898   /* Get a line of input. */
899   if (!unformat_user (input, unformat_line_input, line_input))
900     return 0;
901
902   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
903     {
904       if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
905         print_all = 0;
906       else if (unformat (line_input, "local"))
907         filter = 1;
908       else if (unformat (line_input, "remote"))
909         filter = 2;
910       else
911         {
912           error = clib_error_return (0, "parse error: '%U'",
913                                      format_unformat_error, line_input);
914           goto done;
915         }
916     }
917
918   vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
919                    "EID", "type", "locators", "ttl", "autoritative");
920
921   if (print_all)
922     {
923       /* *INDENT-OFF* */
924       pool_foreach (mapit, lcm->mapping_pool,
925       ({
926         if (mapit->pitr_set)
927           continue;
928
929         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
930                                                 mapit->locator_set_index);
931         if (filter && !((1 == filter && ls->local) ||
932           (2 == filter && !ls->local)))
933           {
934             continue;
935           }
936         vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
937                          lcm, mapit, ls);
938       }));
939       /* *INDENT-ON* */
940     }
941   else
942     {
943       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
944       if ((u32) ~ 0 == mi)
945         goto done;
946
947       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
948       locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
949                                              mapit->locator_set_index);
950
951       if (filter && !((1 == filter && ls->local) ||
952                       (2 == filter && !ls->local)))
953         {
954           goto done;
955         }
956
957       vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
958                        lcm, mapit, ls);
959     }
960
961 done:
962   unformat_free (line_input);
963
964   return error;
965 }
966
967 /* *INDENT-OFF* */
968 VLIB_CLI_COMMAND (one_cp_show_eid_table_command) = {
969     .path = "show one eid-table",
970     .short_help = "Shows EID table",
971     .function = lisp_show_eid_table_command_fn,
972 };
973 /* *INDENT-ON* */
974
975
976 static clib_error_t *
977 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
978                                 vlib_cli_command_t * cmd)
979 {
980   unformat_input_t _line_input, *line_input = &_line_input;
981   u8 is_enabled = 0;
982   u8 is_set = 0;
983   clib_error_t *error = NULL;
984
985   /* Get a line of input. */
986   if (!unformat_user (input, unformat_line_input, line_input))
987     return 0;
988
989   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
990     {
991       if (unformat (line_input, "enable"))
992         {
993           is_set = 1;
994           is_enabled = 1;
995         }
996       else if (unformat (line_input, "disable"))
997         is_set = 1;
998       else
999         {
1000           error = clib_error_return (0, "parse error: '%U'",
1001                                      format_unformat_error, line_input);
1002           goto done;
1003         }
1004     }
1005
1006   if (!is_set)
1007     {
1008       error = clib_error_return (0, "state not set");
1009       goto done;
1010     }
1011
1012   vnet_lisp_enable_disable (is_enabled);
1013
1014 done:
1015   unformat_free (line_input);
1016
1017   return error;
1018 }
1019
1020 /* *INDENT-OFF* */
1021 VLIB_CLI_COMMAND (one_cp_enable_disable_command) = {
1022     .path = "one",
1023     .short_help = "one [enable|disable]",
1024     .function = lisp_enable_disable_command_fn,
1025 };
1026 /* *INDENT-ON* */
1027
1028 static clib_error_t *
1029 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
1030                                              unformat_input_t * input,
1031                                              vlib_cli_command_t * cmd)
1032 {
1033   unformat_input_t _line_input, *line_input = &_line_input;
1034   u8 is_enabled = 0;
1035   u8 is_set = 0;
1036   clib_error_t *error = NULL;
1037
1038   /* Get a line of input. */
1039   if (!unformat_user (input, unformat_line_input, line_input))
1040     return 0;
1041
1042   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1043     {
1044       if (unformat (line_input, "enable"))
1045         {
1046           is_set = 1;
1047           is_enabled = 1;
1048         }
1049       else if (unformat (line_input, "disable"))
1050         is_set = 1;
1051       else
1052         {
1053           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1054                            line_input);
1055           goto done;
1056         }
1057     }
1058
1059   if (!is_set)
1060     {
1061       vlib_cli_output (vm, "state not set!");
1062       goto done;
1063     }
1064
1065   vnet_lisp_map_register_enable_disable (is_enabled);
1066
1067 done:
1068   unformat_free (line_input);
1069
1070   return error;
1071 }
1072
1073 /* *INDENT-OFF* */
1074 VLIB_CLI_COMMAND (one_map_register_enable_disable_command) = {
1075     .path = "one map-register",
1076     .short_help = "one map-register [enable|disable]",
1077     .function = lisp_map_register_enable_disable_command_fn,
1078 };
1079 /* *INDENT-ON* */
1080
1081 static clib_error_t *
1082 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
1083                                            unformat_input_t * input,
1084                                            vlib_cli_command_t * cmd)
1085 {
1086   unformat_input_t _line_input, *line_input = &_line_input;
1087   u8 is_enabled = 0;
1088   u8 is_set = 0;
1089   clib_error_t *error = NULL;
1090
1091   /* Get a line of input. */
1092   if (!unformat_user (input, unformat_line_input, line_input))
1093     return 0;
1094
1095   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1096     {
1097       if (unformat (line_input, "enable"))
1098         {
1099           is_set = 1;
1100           is_enabled = 1;
1101         }
1102       else if (unformat (line_input, "disable"))
1103         is_set = 1;
1104       else
1105         {
1106           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1107                            line_input);
1108           goto done;
1109         }
1110     }
1111
1112   if (!is_set)
1113     {
1114       vlib_cli_output (vm, "state not set!");
1115       goto done;
1116     }
1117
1118   vnet_lisp_rloc_probe_enable_disable (is_enabled);
1119
1120 done:
1121   unformat_free (line_input);
1122
1123   return error;
1124 }
1125
1126 /* *INDENT-OFF* */
1127 VLIB_CLI_COMMAND (one_rloc_probe_enable_disable_command) = {
1128     .path = "one rloc-probe",
1129     .short_help = "one rloc-probe [enable|disable]",
1130     .function = lisp_rloc_probe_enable_disable_command_fn,
1131 };
1132 /* *INDENT-ON* */
1133
1134 static u8 *
1135 format_lisp_status (u8 * s, va_list * args)
1136 {
1137   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1138   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1139 }
1140
1141 static clib_error_t *
1142 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1143                              vlib_cli_command_t * cmd)
1144 {
1145   u8 *msg = 0;
1146   msg = format (msg, "feature: %U\ngpe: %U\n",
1147                 format_lisp_status, format_vnet_lisp_gpe_status);
1148   vlib_cli_output (vm, "%v", msg);
1149   vec_free (msg);
1150   return 0;
1151 }
1152
1153 /* *INDENT-OFF* */
1154 VLIB_CLI_COMMAND (one_show_status_command) = {
1155     .path = "show one status",
1156     .short_help = "show one status",
1157     .function = lisp_show_status_command_fn,
1158 };
1159 /* *INDENT-ON* */
1160
1161 static clib_error_t *
1162 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
1163                                     unformat_input_t * input,
1164                                     vlib_cli_command_t * cmd)
1165 {
1166   hash_pair_t *p;
1167   unformat_input_t _line_input, *line_input = &_line_input;
1168   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1169   uword *vni_table = 0;
1170   u8 is_l2 = 0;
1171   clib_error_t *error = NULL;
1172
1173   /* Get a line of input. */
1174   if (!unformat_user (input, unformat_line_input, line_input))
1175     return 0;
1176
1177   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1178     {
1179       if (unformat (line_input, "l2"))
1180         {
1181           vni_table = lcm->bd_id_by_vni;
1182           is_l2 = 1;
1183         }
1184       else if (unformat (line_input, "l3"))
1185         {
1186           vni_table = lcm->table_id_by_vni;
1187           is_l2 = 0;
1188         }
1189       else
1190         {
1191           error = clib_error_return (0, "parse error: '%U'",
1192                                      format_unformat_error, line_input);
1193           goto done;
1194         }
1195     }
1196
1197   if (!vni_table)
1198     {
1199       vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
1200       goto done;
1201     }
1202
1203   vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
1204
1205   /* *INDENT-OFF* */
1206   hash_foreach_pair (p, vni_table,
1207   ({
1208     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
1209   }));
1210   /* *INDENT-ON* */
1211
1212 done:
1213   unformat_free (line_input);
1214
1215   return error;
1216 }
1217
1218 /* *INDENT-OFF* */
1219 VLIB_CLI_COMMAND (one_show_eid_table_map_command) = {
1220     .path = "show one eid-table map",
1221     .short_help = "show one eid-table l2|l3",
1222     .function = lisp_show_eid_table_map_command_fn,
1223 };
1224 /* *INDENT-ON* */
1225
1226
1227 static clib_error_t *
1228 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
1229                                      unformat_input_t * input,
1230                                      vlib_cli_command_t * cmd)
1231 {
1232   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1233   vnet_main_t *vnm = lgm->vnet_main;
1234   unformat_input_t _line_input, *line_input = &_line_input;
1235   u8 is_add = 1;
1236   clib_error_t *error = 0;
1237   u8 *locator_set_name = 0;
1238   locator_t locator, *locators = 0;
1239   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
1240   u32 ls_index = 0;
1241   int rv = 0;
1242
1243   memset (&locator, 0, sizeof (locator));
1244   memset (a, 0, sizeof (a[0]));
1245
1246   /* Get a line of input. */
1247   if (!unformat_user (input, unformat_line_input, line_input))
1248     return 0;
1249
1250   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1251     {
1252       if (unformat (line_input, "add %_%v%_", &locator_set_name))
1253         is_add = 1;
1254       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1255         is_add = 0;
1256       else if (unformat (line_input, "iface %U p %d w %d",
1257                          unformat_vnet_sw_interface, vnm,
1258                          &locator.sw_if_index, &locator.priority,
1259                          &locator.weight))
1260         {
1261           locator.local = 1;
1262           vec_add1 (locators, locator);
1263         }
1264       else
1265         {
1266           error = unformat_parse_error (line_input);
1267           goto done;
1268         }
1269     }
1270
1271   a->name = locator_set_name;
1272   a->locators = locators;
1273   a->is_add = is_add;
1274   a->local = 1;
1275
1276   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
1277   if (0 != rv)
1278     {
1279       error = clib_error_return (0, "failed to %s locator-set!",
1280                                  is_add ? "add" : "delete");
1281     }
1282
1283 done:
1284   vec_free (locators);
1285   if (locator_set_name)
1286     vec_free (locator_set_name);
1287   unformat_free (line_input);
1288   return error;
1289 }
1290
1291 /* *INDENT-OFF* */
1292 VLIB_CLI_COMMAND (one_cp_add_del_locator_set_command) = {
1293     .path = "one locator-set",
1294     .short_help = "one locator-set add/del <name> [iface <iface-name> "
1295         "p <priority> w <weight>]",
1296     .function = lisp_add_del_locator_set_command_fn,
1297 };
1298 /* *INDENT-ON* */
1299
1300 static clib_error_t *
1301 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
1302                                         unformat_input_t * input,
1303                                         vlib_cli_command_t * cmd)
1304 {
1305   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1306   vnet_main_t *vnm = lgm->vnet_main;
1307   unformat_input_t _line_input, *line_input = &_line_input;
1308   u8 is_add = 1;
1309   clib_error_t *error = 0;
1310   u8 *locator_set_name = 0;
1311   u8 locator_set_name_set = 0;
1312   locator_t locator, *locators = 0;
1313   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
1314   u32 ls_index = 0;
1315
1316   memset (&locator, 0, sizeof (locator));
1317   memset (a, 0, sizeof (a[0]));
1318
1319   /* Get a line of input. */
1320   if (!unformat_user (input, unformat_line_input, line_input))
1321     return 0;
1322
1323   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1324     {
1325       if (unformat (line_input, "add"))
1326         is_add = 1;
1327       else if (unformat (line_input, "del"))
1328         is_add = 0;
1329       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
1330         locator_set_name_set = 1;
1331       else if (unformat (line_input, "iface %U p %d w %d",
1332                          unformat_vnet_sw_interface, vnm,
1333                          &locator.sw_if_index, &locator.priority,
1334                          &locator.weight))
1335         {
1336           locator.local = 1;
1337           vec_add1 (locators, locator);
1338         }
1339       else
1340         {
1341           error = unformat_parse_error (line_input);
1342           goto done;
1343         }
1344     }
1345
1346   if (!locator_set_name_set)
1347     {
1348       error = clib_error_return (0, "locator_set name not set!");
1349       goto done;
1350     }
1351
1352   a->name = locator_set_name;
1353   a->locators = locators;
1354   a->is_add = is_add;
1355   a->local = 1;
1356
1357   vnet_lisp_add_del_locator (a, 0, &ls_index);
1358
1359 done:
1360   vec_free (locators);
1361   vec_free (locator_set_name);
1362   unformat_free (line_input);
1363   return error;
1364 }
1365
1366 /* *INDENT-OFF* */
1367 VLIB_CLI_COMMAND (one_cp_add_del_locator_in_set_command) = {
1368     .path = "one locator",
1369     .short_help = "one locator add/del locator-set <name> iface <iface-name> "
1370                   "p <priority> w <weight>",
1371     .function = lisp_add_del_locator_in_set_command_fn,
1372 };
1373 /* *INDENT-ON* */
1374
1375 static clib_error_t *
1376 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1377                                       unformat_input_t * input,
1378                                       vlib_cli_command_t * cmd)
1379 {
1380   locator_set_t *lsit;
1381   locator_t *loc;
1382   u32 *locit;
1383   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1384
1385   vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
1386                    "Priority", "Weight");
1387
1388   /* *INDENT-OFF* */
1389   pool_foreach (lsit, lcm->locator_set_pool,
1390   ({
1391     u8 * msg = 0;
1392     int next_line = 0;
1393     if (lsit->local)
1394       {
1395         msg = format (msg, "%v", lsit->name);
1396       }
1397     else
1398       {
1399         msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
1400       }
1401     vec_foreach (locit, lsit->locator_indices)
1402       {
1403         if (next_line)
1404           {
1405             msg = format (msg, "%16s", " ");
1406           }
1407         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1408         if (loc->local)
1409           msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
1410                         loc->weight);
1411         else
1412           msg = format (msg, "%16U%16d%16d\n", format_ip_address,
1413                         &gid_address_ip(&loc->address), loc->priority,
1414                         loc->weight);
1415         next_line = 1;
1416       }
1417     vlib_cli_output (vm, "%v", msg);
1418     vec_free (msg);
1419   }));
1420   /* *INDENT-ON* */
1421   return 0;
1422 }
1423
1424 /* *INDENT-OFF* */
1425 VLIB_CLI_COMMAND (one_cp_show_locator_sets_command) = {
1426     .path = "show one locator-set",
1427     .short_help = "Shows locator-sets",
1428     .function = lisp_cp_show_locator_sets_command_fn,
1429 };
1430 /* *INDENT-ON* */
1431
1432
1433 static clib_error_t *
1434 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1435                                       unformat_input_t * input,
1436                                       vlib_cli_command_t * cmd)
1437 {
1438   unformat_input_t _line_input, *line_input = &_line_input;
1439   u8 is_add = 1, addr_set = 0;
1440   ip_address_t ip_addr;
1441   clib_error_t *error = 0;
1442   int rv = 0;
1443   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
1444
1445   /* Get a line of input. */
1446   if (!unformat_user (input, unformat_line_input, line_input))
1447     return 0;
1448
1449   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1450     {
1451       if (unformat (line_input, "add"))
1452         is_add = 1;
1453       else if (unformat (line_input, "del"))
1454         is_add = 0;
1455       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1456         addr_set = 1;
1457       else
1458         {
1459           error = unformat_parse_error (line_input);
1460           goto done;
1461         }
1462     }
1463
1464   if (!addr_set)
1465     {
1466       error = clib_error_return (0, "Map-resolver address must be set!");
1467       goto done;
1468     }
1469
1470   a->is_add = is_add;
1471   a->address = ip_addr;
1472   rv = vnet_lisp_add_del_map_resolver (a);
1473   if (0 != rv)
1474     {
1475       error = clib_error_return (0, "failed to %s map-resolver!",
1476                                  is_add ? "add" : "delete");
1477     }
1478
1479 done:
1480   unformat_free (line_input);
1481   return error;
1482 }
1483
1484 /* *INDENT-OFF* */
1485 VLIB_CLI_COMMAND (one_add_del_map_resolver_command) = {
1486     .path = "one map-resolver",
1487     .short_help = "one map-resolver add/del <ip_address>",
1488     .function = lisp_add_del_map_resolver_command_fn,
1489 };
1490 /* *INDENT-ON* */
1491
1492
1493 static clib_error_t *
1494 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1495                                         unformat_input_t * input,
1496                                         vlib_cli_command_t * cmd)
1497 {
1498   unformat_input_t _line_input, *line_input = &_line_input;
1499   u8 is_add = 1;
1500   u8 *locator_set_name = 0;
1501   clib_error_t *error = 0;
1502   int rv = 0;
1503   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
1504
1505   /* Get a line of input. */
1506   if (!unformat_user (input, unformat_line_input, line_input))
1507     return 0;
1508
1509   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1510     {
1511       if (unformat (line_input, "del"))
1512         is_add = 0;
1513       else if (unformat (line_input, "add %_%v%_", &locator_set_name))
1514         is_add = 1;
1515       else
1516         {
1517           error = unformat_parse_error (line_input);
1518           goto done;
1519         }
1520     }
1521
1522   a->is_add = is_add;
1523   a->locator_set_name = locator_set_name;
1524   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
1525   if (0 != rv)
1526     {
1527       error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
1528                                  is_add ? "add" : "delete");
1529     }
1530
1531 done:
1532   vec_free (locator_set_name);
1533   unformat_free (line_input);
1534   return error;
1535
1536 }
1537
1538 /* *INDENT-OFF* */
1539 VLIB_CLI_COMMAND (one_add_del_map_request_command) = {
1540     .path = "one map-request itr-rlocs",
1541     .short_help = "one map-request itr-rlocs add/del <locator_set_name>",
1542     .function = lisp_add_del_mreq_itr_rlocs_command_fn,
1543 };
1544 /* *INDENT-ON* */
1545
1546 static clib_error_t *
1547 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1548                                      unformat_input_t * input,
1549                                      vlib_cli_command_t * cmd)
1550 {
1551   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1552   locator_set_t *loc_set;
1553
1554   vlib_cli_output (vm, "%=20s", "itr-rlocs");
1555
1556   if (~0 == lcm->mreq_itr_rlocs)
1557     {
1558       return 0;
1559     }
1560
1561   loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
1562
1563   vlib_cli_output (vm, "%=20s", loc_set->name);
1564
1565   return 0;
1566 }
1567
1568 /* *INDENT-OFF* */
1569 VLIB_CLI_COMMAND (one_show_map_request_command) = {
1570     .path = "show one map-request itr-rlocs",
1571     .short_help = "Shows map-request itr-rlocs",
1572     .function = lisp_show_mreq_itr_rlocs_command_fn,
1573 };
1574 /* *INDENT-ON* */
1575
1576 static clib_error_t *
1577 lisp_use_petr_set_locator_set_command_fn (vlib_main_t * vm,
1578                                           unformat_input_t * input,
1579                                           vlib_cli_command_t * cmd)
1580 {
1581   u8 is_add = 1, ip_set = 0;
1582   unformat_input_t _line_input, *line_input = &_line_input;
1583   clib_error_t *error = 0;
1584   ip_address_t ip;
1585
1586   /* Get a line of input. */
1587   if (!unformat_user (input, unformat_line_input, line_input))
1588     return 0;
1589
1590   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1591     {
1592       if (unformat (line_input, "%U", unformat_ip_address, &ip))
1593         ip_set = 1;
1594       else if (unformat (line_input, "disable"))
1595         is_add = 0;
1596       else
1597         {
1598           error = clib_error_return (0, "parse error");
1599           goto done;
1600         }
1601     }
1602
1603   if (!ip_set)
1604     {
1605       clib_warning ("No petr IP specified!");
1606       goto done;
1607     }
1608
1609   if (vnet_lisp_use_petr (&ip, is_add))
1610     {
1611       error = clib_error_return (0, "failed to %s petr!",
1612                                  is_add ? "add" : "delete");
1613     }
1614
1615 done:
1616   unformat_free (line_input);
1617   return error;
1618 }
1619
1620 /* *INDENT-OFF* */
1621 VLIB_CLI_COMMAND (one_use_petr_set_locator_set_command) = {
1622     .path = "one use-petr",
1623     .short_help = "one use-petr [disable] <petr-ip>",
1624     .function = lisp_use_petr_set_locator_set_command_fn,
1625 };
1626
1627 static clib_error_t *
1628 lisp_show_petr_command_fn (vlib_main_t * vm,
1629                            unformat_input_t * input, vlib_cli_command_t * cmd)
1630 {
1631   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1632   mapping_t *m;
1633   locator_set_t *ls;
1634   locator_t *loc;
1635   u8 *tmp_str = 0;
1636   u8 use_petr = lcm->flags & LISP_FLAG_USE_PETR;
1637   vlib_cli_output (vm, "%=20s%=16s", "petr", use_petr ? "ip" : "");
1638
1639   if (!use_petr)
1640     {
1641       vlib_cli_output (vm, "%=20s", "disable");
1642       return 0;
1643     }
1644
1645   if (~0 == lcm->petr_map_index)
1646     {
1647       tmp_str = format (0, "N/A");
1648     }
1649   else
1650     {
1651       m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1652       if (~0 != m->locator_set_index)
1653         {
1654           ls = pool_elt_at_index(lcm->locator_set_pool, m->locator_set_index);
1655           loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
1656           tmp_str = format (0, "%U", format_ip_address, &loc->address);
1657         }
1658       else
1659         {
1660           tmp_str = format (0, "N/A");
1661         }
1662     }
1663   vec_add1 (tmp_str, 0);
1664
1665   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1666
1667   vec_free (tmp_str);
1668
1669   return 0;
1670 }
1671
1672 /* *INDENT-OFF* */
1673 VLIB_CLI_COMMAND (one_show_petr_command) = {
1674     .path = "show one petr",
1675     .short_help = "Show petr",
1676     .function = lisp_show_petr_command_fn,
1677 };
1678 /* *INDENT-ON* */
1679
1680 static clib_error_t *
1681 lisp_show_map_servers_command_fn (vlib_main_t * vm,
1682                                   unformat_input_t * input,
1683                                   vlib_cli_command_t * cmd)
1684 {
1685   lisp_msmr_t *ms;
1686   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1687
1688   vec_foreach (ms, lcm->map_servers)
1689   {
1690     vlib_cli_output (vm, "%U", format_ip_address, &ms->address);
1691   }
1692   return 0;
1693 }
1694
1695 /* *INDENT-OFF* */
1696 VLIB_CLI_COMMAND (one_show_map_servers_command) = {
1697     .path = "show one map-servers",
1698     .short_help = "show one map servers",
1699     .function = lisp_show_map_servers_command_fn,
1700 };
1701 /* *INDENT-ON* */
1702
1703 static clib_error_t *
1704 lisp_show_map_register_state_command_fn (vlib_main_t * vm,
1705                                          unformat_input_t * input,
1706                                          vlib_cli_command_t * cmd)
1707 {
1708   u8 *msg = 0;
1709   u8 is_enabled = vnet_lisp_map_register_state_get ();
1710
1711   msg = format (msg, "%s\n", is_enabled ? "enabled" : "disabled");
1712   vlib_cli_output (vm, "%v", msg);
1713   vec_free (msg);
1714   return 0;
1715 }
1716
1717 /* *INDENT-OFF* */
1718 VLIB_CLI_COMMAND (one_show_map_register_state_command) = {
1719     .path = "show one map-register state",
1720     .short_help = "show one map-register state",
1721     .function = lisp_show_map_register_state_command_fn,
1722 };
1723 /* *INDENT-ON* */
1724
1725 static clib_error_t *
1726 lisp_show_rloc_probe_state_command_fn (vlib_main_t * vm,
1727                                        unformat_input_t * input,
1728                                        vlib_cli_command_t * cmd)
1729 {
1730   u8 *msg = 0;
1731   u8 is_enabled = vnet_lisp_rloc_probe_state_get ();
1732
1733   msg = format (msg, "%s\n", is_enabled ? "enabled" : "disabled");
1734   vlib_cli_output (vm, "%v", msg);
1735   vec_free (msg);
1736   return 0;
1737 }
1738
1739 /* *INDENT-OFF* */
1740 VLIB_CLI_COMMAND (one_show_rloc_probe_state_command) = {
1741     .path = "show one rloc state",
1742     .short_help = "show one RLOC state",
1743     .function = lisp_show_rloc_probe_state_command_fn,
1744 };
1745 /* *INDENT-ON* */
1746
1747 static clib_error_t *
1748 lisp_show_stats_command_fn (vlib_main_t * vm,
1749                             unformat_input_t * input,
1750                             vlib_cli_command_t * cmd)
1751 {
1752   u8 is_enabled = vnet_lisp_stats_enable_disable_state ();
1753   vlib_cli_output (vm, "%s\n", is_enabled ? "enabled" : "disabled");
1754   return 0;
1755 }
1756
1757 /* *INDENT-OFF* */
1758 VLIB_CLI_COMMAND (one_show_stats_command) = {
1759     .path = "show one statistics status",
1760     .short_help = "show ONE statistics enable/disable status",
1761     .function = lisp_show_stats_command_fn,
1762 };
1763 /* *INDENT-ON* */
1764
1765 static clib_error_t *
1766 lisp_show_stats_details_command_fn (vlib_main_t * vm,
1767                                     unformat_input_t * input,
1768                                     vlib_cli_command_t * cmd)
1769 {
1770   lisp_api_stats_t *stat, *stats = vnet_lisp_get_stats ();
1771
1772   if (vec_len (stats) > 0)
1773     vlib_cli_output (vm,
1774                      "[src-EID, dst-EID] [loc-rloc, rmt-rloc] count bytes\n");
1775   else
1776     vlib_cli_output (vm, "No statistics found.\n");
1777
1778   vec_foreach (stat, stats)
1779   {
1780     vlib_cli_output (vm, "[%U, %U] [%U, %U] %7u %7u\n",
1781                      format_fid_address, &stat->seid,
1782                      format_fid_address, &stat->deid,
1783                      format_ip_address, &stat->loc_rloc,
1784                      format_ip_address, &stat->rmt_rloc,
1785                      stat->counters.packets, stat->counters.bytes);
1786   }
1787   vec_free (stats);
1788   return 0;
1789 }
1790
1791 /* *INDENT-OFF* */
1792 VLIB_CLI_COMMAND (one_show_stats_details_command) = {
1793     .path = "show one statistics details",
1794     .short_help = "show ONE statistics",
1795     .function = lisp_show_stats_details_command_fn,
1796 };
1797 /* *INDENT-ON* */
1798
1799 static clib_error_t *
1800 lisp_stats_enable_disable_command_fn (vlib_main_t * vm,
1801                                       unformat_input_t * input,
1802                                       vlib_cli_command_t * cmd)
1803 {
1804   unformat_input_t _line_input, *line_input = &_line_input;
1805   u8 enable = 0;
1806
1807   /* Get a line of input. */
1808   if (!unformat_user (input, unformat_line_input, line_input))
1809     return 0;
1810
1811   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1812     {
1813       if (unformat (line_input, "enable"))
1814         enable = 1;
1815       else if (unformat (line_input, "disable"))
1816         enable = 0;
1817       else
1818         {
1819           clib_warning ("Error: expected enable/disable!");
1820           goto done;
1821         }
1822     }
1823   vnet_lisp_stats_enable_disable (enable);
1824 done:
1825   unformat_free (line_input);
1826   return 0;
1827 }
1828
1829 /* *INDENT-OFF* */
1830 VLIB_CLI_COMMAND (one_stats_enable_disable_command) = {
1831     .path = "one statistics",
1832     .short_help = "enable/disable ONE statistics collecting",
1833     .function = lisp_stats_enable_disable_command_fn,
1834 };
1835 /* *INDENT-ON* */
1836
1837 static clib_error_t *
1838 lisp_stats_flush_command_fn (vlib_main_t * vm,
1839                              unformat_input_t * input,
1840                              vlib_cli_command_t * cmd)
1841 {
1842   vnet_lisp_flush_stats ();
1843   return 0;
1844 }
1845
1846 /* *INDENT-OFF* */
1847 VLIB_CLI_COMMAND (one_stats_flush_command) = {
1848     .path = "one statistics flush",
1849     .short_help = "Flush ONE statistics",
1850     .function = lisp_stats_flush_command_fn,
1851 };
1852 /* *INDENT-ON* */
1853
1854 /*
1855  * fd.io coding-style-patch-verification: ON
1856  *
1857  * Local Variables:
1858  * eval: (c-set-style "gnu")
1859  * End:
1860  */