3e0c4c0a3d755cdbb5dcaed9c74e1e60eceff88a
[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   if (is_add)
491     {
492       vnet_lisp_add_del_mapping_args_t _map_args, *map_args = &_map_args;
493       memset (map_args, 0, sizeof (map_args[0]));
494       gid_address_copy (&map_args->eid, &eid);
495       map_args->action = action;
496       map_args->is_static = 1;
497       map_args->authoritative = 0;
498       map_args->ttl = ~0;
499       rv = vnet_lisp_add_mapping (map_args, rlocs, NULL, NULL);
500     }
501   else
502     rv = vnet_lisp_del_mapping (&eid, NULL);
503
504   if (rv)
505     clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
506
507 done:
508   vec_free (rlocs);
509   unformat_free (line_input);
510   return error;
511 }
512
513 /* *INDENT-OFF* */
514 VLIB_CLI_COMMAND (one_add_del_remote_mapping_command) = {
515   .path = "one remote-mapping",
516   .short_help =
517     "one remote-mapping add|del [del-all] vni <vni> "
518     "eid <est-eid> [action <no-action|natively-forward|"
519     "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
520     "[rloc <dst-locator> ... ]",
521   .function = lisp_add_del_remote_mapping_command_fn,
522 };
523 /* *INDENT-ON* */
524
525 /**
526  * Handler for add/del adjacency CLI.
527  */
528 static clib_error_t *
529 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
530                                    vlib_cli_command_t * cmd)
531 {
532   clib_error_t *error = 0;
533   unformat_input_t _line_input, *line_input = &_line_input;
534   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
535   u8 is_add = 1;
536   ip_prefix_t *reid_ippref, *leid_ippref;
537   gid_address_t leid, reid;
538   u8 *dmac = gid_address_mac (&reid);
539   u8 *smac = gid_address_mac (&leid);
540   u8 reid_set = 0, leid_set = 0;
541   u32 vni;
542
543   /* Get a line of input. */
544   if (!unformat_user (input, unformat_line_input, line_input))
545     return 0;
546
547   memset (&reid, 0, sizeof (reid));
548   memset (&leid, 0, sizeof (leid));
549
550   leid_ippref = &gid_address_ippref (&leid);
551   reid_ippref = &gid_address_ippref (&reid);
552
553   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
554     {
555       if (unformat (line_input, "del"))
556         is_add = 0;
557       else if (unformat (line_input, "add"))
558         ;
559       else if (unformat (line_input, "reid %U",
560                          unformat_ip_prefix, reid_ippref))
561         {
562           gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
563           reid_set = 1;
564         }
565       else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
566         {
567           gid_address_type (&reid) = GID_ADDR_MAC;
568           reid_set = 1;
569         }
570       else if (unformat (line_input, "vni %u", &vni))
571         {
572           gid_address_vni (&leid) = vni;
573           gid_address_vni (&reid) = vni;
574         }
575       else if (unformat (line_input, "leid %U",
576                          unformat_ip_prefix, leid_ippref))
577         {
578           gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
579           leid_set = 1;
580         }
581       else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
582         {
583           gid_address_type (&leid) = GID_ADDR_MAC;
584           leid_set = 1;
585         }
586       else
587         {
588           clib_warning ("parse error");
589           goto done;
590         }
591     }
592
593   if (!reid_set || !leid_set)
594     {
595       clib_warning ("missing remote or local eid!");
596       goto done;
597     }
598
599   if ((gid_address_type (&leid) != gid_address_type (&reid))
600       || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
601           && ip_prefix_version (reid_ippref)
602           != ip_prefix_version (leid_ippref)))
603     {
604       clib_warning ("remote and local EIDs are of different types!");
605       goto done;
606     }
607
608   memset (a, 0, sizeof (a[0]));
609   gid_address_copy (&a->leid, &leid);
610   gid_address_copy (&a->reid, &reid);
611   a->is_add = is_add;
612
613   if (vnet_lisp_add_del_adjacency (a))
614     clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
615
616 done:
617   unformat_free (line_input);
618   return error;
619 }
620
621 /* *INDENT-OFF* */
622 VLIB_CLI_COMMAND (one_add_del_adjacency_command) = {
623     .path = "one adjacency",
624     .short_help = "one adjacency add|del vni <vni> reid <remote-eid> "
625       "leid <local-eid>",
626     .function = lisp_add_del_adjacency_command_fn,
627 };
628 /* *INDENT-ON* */
629
630
631 static clib_error_t *
632 lisp_map_request_mode_command_fn (vlib_main_t * vm,
633                                   unformat_input_t * input,
634                                   vlib_cli_command_t * cmd)
635 {
636   unformat_input_t _i, *i = &_i;
637   map_request_mode_t mr_mode = _MR_MODE_MAX;
638
639   /* Get a line of input. */
640   if (!unformat_user (input, unformat_line_input, i))
641     return 0;
642
643   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
644     {
645       if (unformat (i, "dst-only"))
646         mr_mode = MR_MODE_DST_ONLY;
647       else if (unformat (i, "src-dst"))
648         mr_mode = MR_MODE_SRC_DST;
649       else
650         {
651           clib_warning ("parse error '%U'", format_unformat_error, i);
652           goto done;
653         }
654     }
655
656   if (_MR_MODE_MAX == mr_mode)
657     {
658       clib_warning ("No map request mode entered!");
659       goto done;
660     }
661
662   vnet_lisp_set_map_request_mode (mr_mode);
663
664 done:
665   unformat_free (i);
666
667   return 0;
668 }
669
670 /* *INDENT-OFF* */
671 VLIB_CLI_COMMAND (one_map_request_mode_command) = {
672     .path = "one map-request mode",
673     .short_help = "one map-request mode dst-only|src-dst",
674     .function = lisp_map_request_mode_command_fn,
675 };
676 /* *INDENT-ON* */
677
678
679 static u8 *
680 format_lisp_map_request_mode (u8 * s, va_list * args)
681 {
682   u32 mode = va_arg (*args, u32);
683
684   switch (mode)
685     {
686     case 0:
687       return format (0, "dst-only");
688     case 1:
689       return format (0, "src-dst");
690     }
691   return 0;
692 }
693
694 static clib_error_t *
695 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
696                                        unformat_input_t * input,
697                                        vlib_cli_command_t * cmd)
698 {
699   vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
700                    vnet_lisp_get_map_request_mode ());
701   return 0;
702 }
703
704 /* *INDENT-OFF* */
705 VLIB_CLI_COMMAND (one_show_map_request_mode_command) = {
706     .path = "show one map-request mode",
707     .short_help = "show one map-request mode",
708     .function = lisp_show_map_request_mode_command_fn,
709 };
710 /* *INDENT-ON* */
711
712 static clib_error_t *
713 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
714                                     unformat_input_t * input,
715                                     vlib_cli_command_t * cmd)
716 {
717   lisp_msmr_t *mr;
718   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
719
720   vec_foreach (mr, lcm->map_resolvers)
721   {
722     vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
723   }
724   return 0;
725 }
726
727 /* *INDENT-OFF* */
728 VLIB_CLI_COMMAND (one_show_map_resolvers_command) = {
729     .path = "show one map-resolvers",
730     .short_help = "show one map-resolvers",
731     .function = lisp_show_map_resolvers_command_fn,
732 };
733 /* *INDENT-ON* */
734
735 static clib_error_t *
736 lisp_nsh_set_locator_set_command_fn (vlib_main_t * vm,
737                                      unformat_input_t * input,
738                                      vlib_cli_command_t * cmd)
739 {
740   u8 locator_name_set = 0;
741   u8 *locator_set_name = 0;
742   u8 is_add = 1;
743   unformat_input_t _line_input, *line_input = &_line_input;
744   clib_error_t *error = 0;
745   int rv = 0;
746
747   /* Get a line of input. */
748   if (!unformat_user (input, unformat_line_input, line_input))
749     return 0;
750
751   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
752     {
753       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
754         locator_name_set = 1;
755       else if (unformat (line_input, "disable"))
756         is_add = 0;
757       else
758         {
759           error = clib_error_return (0, "parse error");
760           goto done;
761         }
762     }
763
764   if (!locator_name_set)
765     {
766       clib_warning ("No locator set specified!");
767       goto done;
768     }
769
770   rv = vnet_lisp_nsh_set_locator_set (locator_set_name, is_add);
771   if (0 != rv)
772     {
773       error = clib_error_return (0, "failed to %s NSH mapping!",
774                                  is_add ? "add" : "delete");
775     }
776
777 done:
778   vec_free (locator_set_name);
779   unformat_free (line_input);
780   return error;
781 }
782
783 /* *INDENT-OFF* */
784 VLIB_CLI_COMMAND (one_nsh_set_locator_set_command) = {
785     .path = "one nsh-mapping",
786     .short_help = "one nsh-mapping [del] ls <locator-set-name>",
787     .function = lisp_nsh_set_locator_set_command_fn,
788 };
789 /* *INDENT-ON* */
790
791 static clib_error_t *
792 lisp_map_register_fallback_threshold_show_command_fn (vlib_main_t * vm,
793                                                       unformat_input_t *
794                                                       input,
795                                                       vlib_cli_command_t *
796                                                       cmd)
797 {
798   u32 val = vnet_lisp_map_register_fallback_threshold_get ();
799   vlib_cli_output (vm, "map register fallback treshold value: %d", val);
800   return 0;
801 }
802
803 /* *INDENT-OFF* */
804 VLIB_CLI_COMMAND (one_map_register_fallback_threshold_show_command) = {
805     .path = "show one map-register fallback-threshold",
806     .short_help = "show one map-register fallback-threshold",
807     .function = lisp_map_register_fallback_threshold_show_command_fn,
808 };
809
810 /* *INDENT-ON* */
811
812 static clib_error_t *
813 lisp_map_register_fallback_threshold_command_fn (vlib_main_t * vm,
814                                                  unformat_input_t * input,
815                                                  vlib_cli_command_t * cmd)
816 {
817   unformat_input_t _line_input, *line_input = &_line_input;
818   clib_error_t *error = 0;
819   u32 val = 0;
820   int rv = 0;
821
822   /* Get a line of input. */
823   if (!unformat_user (input, unformat_line_input, line_input))
824     return 0;
825
826   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
827     {
828       if (unformat (line_input, "%d", &val))
829         ;
830       else
831         {
832           error = clib_error_return (0, "parse error");
833           goto done;
834         }
835     }
836
837   rv = vnet_lisp_map_register_fallback_threshold_set (val);
838   if (rv)
839     {
840       error = clib_error_return (0, "setting fallback threshold failed!");
841     }
842
843 done:
844   unformat_free (line_input);
845   return error;
846 }
847
848 /* *INDENT-OFF* */
849 VLIB_CLI_COMMAND (one_map_register_fallback_threshold_command) = {
850     .path = "one map-register fallback-threshold",
851     .short_help = "one map-register fallback-threshold <count>",
852     .function = lisp_map_register_fallback_threshold_command_fn,
853 };
854 /* *INDENT-ON* */
855
856 static clib_error_t *
857 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
858                                       unformat_input_t * input,
859                                       vlib_cli_command_t * cmd)
860 {
861   u8 locator_name_set = 0;
862   u8 *locator_set_name = 0;
863   u8 is_add = 1;
864   unformat_input_t _line_input, *line_input = &_line_input;
865   clib_error_t *error = 0;
866   int rv = 0;
867
868   /* Get a line of input. */
869   if (!unformat_user (input, unformat_line_input, line_input))
870     return 0;
871
872   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
873     {
874       if (unformat (line_input, "ls %_%v%_", &locator_set_name))
875         locator_name_set = 1;
876       else if (unformat (line_input, "disable"))
877         is_add = 0;
878       else
879         {
880           error = clib_error_return (0, "parse error");
881           goto done;
882         }
883     }
884
885   if (!locator_name_set)
886     {
887       clib_warning ("No locator set specified!");
888       goto done;
889     }
890   rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
891   if (0 != rv)
892     {
893       error = clib_error_return (0, "failed to %s pitr!",
894                                  is_add ? "add" : "delete");
895     }
896
897 done:
898   if (locator_set_name)
899     vec_free (locator_set_name);
900   unformat_free (line_input);
901   return error;
902 }
903
904 /* *INDENT-OFF* */
905 VLIB_CLI_COMMAND (one_pitr_set_locator_set_command) = {
906     .path = "one pitr",
907     .short_help = "one pitr [disable] ls <locator-set-name>",
908     .function = lisp_pitr_set_locator_set_command_fn,
909 };
910 /* *INDENT-ON* */
911
912 static clib_error_t *
913 lisp_show_pitr_command_fn (vlib_main_t * vm,
914                            unformat_input_t * input, vlib_cli_command_t * cmd)
915 {
916   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
917   mapping_t *m;
918   locator_set_t *ls;
919   u8 *tmp_str = 0;
920
921   vlib_cli_output (vm, "%=20s%=16s",
922                    "pitr", lcm->lisp_pitr ? "locator-set" : "");
923
924   if (!lcm->lisp_pitr)
925     {
926       vlib_cli_output (vm, "%=20s", "disable");
927       return 0;
928     }
929
930   if (~0 == lcm->pitr_map_index)
931     {
932       tmp_str = format (0, "N/A");
933     }
934   else
935     {
936       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
937       if (~0 != m->locator_set_index)
938         {
939           ls =
940             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
941           tmp_str = format (0, "%s", ls->name);
942         }
943       else
944         {
945           tmp_str = format (0, "N/A");
946         }
947     }
948   vec_add1 (tmp_str, 0);
949
950   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
951
952   vec_free (tmp_str);
953
954   return 0;
955 }
956
957 /* *INDENT-OFF* */
958 VLIB_CLI_COMMAND (one_show_pitr_command) = {
959     .path = "show one pitr",
960     .short_help = "Show pitr",
961     .function = lisp_show_pitr_command_fn,
962 };
963 /* *INDENT-ON* */
964
965 static u8 *
966 format_eid_entry (u8 * s, va_list * args)
967 {
968   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
969   lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
970   mapping_t *mapit = va_arg (*args, mapping_t *);
971   locator_set_t *ls = va_arg (*args, locator_set_t *);
972   gid_address_t *gid = &mapit->eid;
973   u32 ttl = mapit->ttl;
974   u8 aut = mapit->authoritative;
975   u32 *loc_index;
976   u8 first_line = 1;
977   u8 *loc;
978
979   u8 *type = ls->local ? format (0, "local(%s)", ls->name)
980     : format (0, "remote");
981
982   if (vec_len (ls->locator_indices) == 0)
983     {
984       s = format (s, "%-35U%-20saction:%-30U%-20u%-u", format_gid_address,
985                   gid, type, format_negative_mapping_action, mapit->action,
986                   ttl, aut);
987     }
988   else
989     {
990       vec_foreach (loc_index, ls->locator_indices)
991       {
992         locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
993         if (l->local)
994           loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
995                         l->sw_if_index);
996         else
997           loc = format (0, "%U", format_ip_address,
998                         &gid_address_ip (&l->address));
999
1000         if (first_line)
1001           {
1002             s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1003                         gid, type, loc, ttl, aut);
1004             first_line = 0;
1005           }
1006         else
1007           s = format (s, "%55s%v\n", "", loc);
1008       }
1009     }
1010   return s;
1011 }
1012
1013 static clib_error_t *
1014 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1015                                 unformat_input_t * input,
1016                                 vlib_cli_command_t * cmd)
1017 {
1018   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1019   mapping_t *mapit;
1020   unformat_input_t _line_input, *line_input = &_line_input;
1021   u32 mi;
1022   gid_address_t eid;
1023   u8 print_all = 1;
1024   u8 filter = 0;
1025   clib_error_t *error = NULL;
1026
1027   memset (&eid, 0, sizeof (eid));
1028
1029   /* Get a line of input. */
1030   if (!unformat_user (input, unformat_line_input, line_input))
1031     return 0;
1032
1033   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1034     {
1035       if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1036         print_all = 0;
1037       else if (unformat (line_input, "local"))
1038         filter = 1;
1039       else if (unformat (line_input, "remote"))
1040         filter = 2;
1041       else
1042         {
1043           error = clib_error_return (0, "parse error: '%U'",
1044                                      format_unformat_error, line_input);
1045           goto done;
1046         }
1047     }
1048
1049   vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1050                    "EID", "type", "locators", "ttl", "autoritative");
1051
1052   if (print_all)
1053     {
1054       /* *INDENT-OFF* */
1055       pool_foreach (mapit, lcm->mapping_pool,
1056       ({
1057         if (mapit->pitr_set || mapit->nsh_set)
1058           continue;
1059
1060         locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1061                                                 mapit->locator_set_index);
1062         if (filter && !((1 == filter && ls->local) ||
1063           (2 == filter && !ls->local)))
1064           {
1065             continue;
1066           }
1067         vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1068                          lcm, mapit, ls);
1069       }));
1070       /* *INDENT-ON* */
1071     }
1072   else
1073     {
1074       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1075       if ((u32) ~ 0 == mi)
1076         goto done;
1077
1078       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1079       locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
1080                                              mapit->locator_set_index);
1081
1082       if (filter && !((1 == filter && ls->local) ||
1083                       (2 == filter && !ls->local)))
1084         {
1085           goto done;
1086         }
1087
1088       vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
1089                        lcm, mapit, ls);
1090     }
1091
1092 done:
1093   unformat_free (line_input);
1094
1095   return error;
1096 }
1097
1098 /* *INDENT-OFF* */
1099 VLIB_CLI_COMMAND (one_cp_show_eid_table_command) = {
1100     .path = "show one eid-table",
1101     .short_help = "Shows EID table",
1102     .function = lisp_show_eid_table_command_fn,
1103 };
1104 /* *INDENT-ON* */
1105
1106
1107 static clib_error_t *
1108 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1109                                 vlib_cli_command_t * cmd)
1110 {
1111   unformat_input_t _line_input, *line_input = &_line_input;
1112   u8 is_enabled = 0;
1113   u8 is_set = 0;
1114   clib_error_t *error = NULL;
1115
1116   /* Get a line of input. */
1117   if (!unformat_user (input, unformat_line_input, line_input))
1118     return 0;
1119
1120   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1121     {
1122       if (unformat (line_input, "enable"))
1123         {
1124           is_set = 1;
1125           is_enabled = 1;
1126         }
1127       else if (unformat (line_input, "disable"))
1128         is_set = 1;
1129       else
1130         {
1131           error = clib_error_return (0, "parse error: '%U'",
1132                                      format_unformat_error, line_input);
1133           goto done;
1134         }
1135     }
1136
1137   if (!is_set)
1138     {
1139       error = clib_error_return (0, "state not set");
1140       goto done;
1141     }
1142
1143   vnet_lisp_enable_disable (is_enabled);
1144
1145 done:
1146   unformat_free (line_input);
1147
1148   return error;
1149 }
1150
1151 /* *INDENT-OFF* */
1152 VLIB_CLI_COMMAND (one_cp_enable_disable_command) = {
1153     .path = "one",
1154     .short_help = "one [enable|disable]",
1155     .function = lisp_enable_disable_command_fn,
1156 };
1157 /* *INDENT-ON* */
1158
1159 static clib_error_t *
1160 lisp_map_register_set_ttl_command_fn (vlib_main_t * vm,
1161                                       unformat_input_t * input,
1162                                       vlib_cli_command_t * cmd)
1163 {
1164   unformat_input_t _line_input, *line_input = &_line_input;
1165   u32 ttl = 0;
1166   u8 is_set = 0;
1167   clib_error_t *error = NULL;
1168
1169   /* Get a line of input. */
1170   if (!unformat_user (input, unformat_line_input, line_input))
1171     return 0;
1172
1173   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1174     {
1175       if (unformat (line_input, "%u", &ttl))
1176         is_set = 1;
1177       else
1178         {
1179           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1180                            line_input);
1181           goto done;
1182         }
1183     }
1184
1185   if (!is_set)
1186     {
1187       vlib_cli_output (vm, "expected integer value for TTL!");
1188       goto done;
1189     }
1190
1191   vnet_lisp_map_register_set_ttl (ttl);
1192
1193 done:
1194   unformat_free (line_input);
1195   return error;
1196 }
1197
1198 /* *INDENT-OFF* */
1199 VLIB_CLI_COMMAND (one_map_register_set_ttl_command) = {
1200     .path = "one map-register ttl",
1201     .short_help = "one map-register ttl",
1202     .function = lisp_map_register_set_ttl_command_fn,
1203 };
1204 /* *INDENT-ON* */
1205
1206 static clib_error_t *
1207 lisp_map_register_show_ttl_command_fn (vlib_main_t * vm,
1208                                        unformat_input_t * input,
1209                                        vlib_cli_command_t * cmd)
1210 {
1211   u32 ttl = vnet_lisp_map_register_get_ttl ();
1212
1213   vlib_cli_output (vm, "map-register TTL: %u", ttl);
1214   return 0;
1215 }
1216
1217 /* *INDENT-OFF* */
1218 VLIB_CLI_COMMAND (one_map_register_show_ttl_command) = {
1219     .path = "show one map-register ttl",
1220     .short_help = "show one map-register ttl",
1221     .function = lisp_map_register_show_ttl_command_fn,
1222 };
1223
1224 /* *INDENT-ON* */
1225
1226 static clib_error_t *
1227 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
1228                                              unformat_input_t * input,
1229                                              vlib_cli_command_t * cmd)
1230 {
1231   unformat_input_t _line_input, *line_input = &_line_input;
1232   u8 is_enabled = 0;
1233   u8 is_set = 0;
1234   clib_error_t *error = NULL;
1235
1236   /* Get a line of input. */
1237   if (!unformat_user (input, unformat_line_input, line_input))
1238     return 0;
1239
1240   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1241     {
1242       if (unformat (line_input, "enable"))
1243         {
1244           is_set = 1;
1245           is_enabled = 1;
1246         }
1247       else if (unformat (line_input, "disable"))
1248         is_set = 1;
1249       else
1250         {
1251           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1252                            line_input);
1253           goto done;
1254         }
1255     }
1256
1257   if (!is_set)
1258     {
1259       vlib_cli_output (vm, "state not set!");
1260       goto done;
1261     }
1262
1263   vnet_lisp_map_register_enable_disable (is_enabled);
1264
1265 done:
1266   unformat_free (line_input);
1267
1268   return error;
1269 }
1270
1271 /* *INDENT-OFF* */
1272 VLIB_CLI_COMMAND (one_map_register_enable_disable_command) = {
1273     .path = "one map-register",
1274     .short_help = "one map-register [enable|disable]",
1275     .function = lisp_map_register_enable_disable_command_fn,
1276 };
1277 /* *INDENT-ON* */
1278
1279 static clib_error_t *
1280 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
1281                                            unformat_input_t * input,
1282                                            vlib_cli_command_t * cmd)
1283 {
1284   unformat_input_t _line_input, *line_input = &_line_input;
1285   u8 is_enabled = 0;
1286   u8 is_set = 0;
1287   clib_error_t *error = NULL;
1288
1289   /* Get a line of input. */
1290   if (!unformat_user (input, unformat_line_input, line_input))
1291     return 0;
1292
1293   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1294     {
1295       if (unformat (line_input, "enable"))
1296         {
1297           is_set = 1;
1298           is_enabled = 1;
1299         }
1300       else if (unformat (line_input, "disable"))
1301         is_set = 1;
1302       else
1303         {
1304           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1305                            line_input);
1306           goto done;
1307         }
1308     }
1309
1310   if (!is_set)
1311     {
1312       vlib_cli_output (vm, "state not set!");
1313       goto done;
1314     }
1315
1316   vnet_lisp_rloc_probe_enable_disable (is_enabled);
1317
1318 done:
1319   unformat_free (line_input);
1320
1321   return error;
1322 }
1323
1324 /* *INDENT-OFF* */
1325 VLIB_CLI_COMMAND (one_rloc_probe_enable_disable_command) = {
1326     .path = "one rloc-probe",
1327     .short_help = "one rloc-probe [enable|disable]",
1328     .function = lisp_rloc_probe_enable_disable_command_fn,
1329 };
1330 /* *INDENT-ON* */
1331
1332 static u8 *
1333 format_lisp_status (u8 * s, va_list * args)
1334 {
1335   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1336   return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1337 }
1338
1339 static clib_error_t *
1340 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1341                              vlib_cli_command_t * cmd)
1342 {
1343   u8 *msg = 0;
1344   msg = format (msg, "feature: %U\ngpe: %U\n",
1345                 format_lisp_status, format_vnet_lisp_gpe_status);
1346   vlib_cli_output (vm, "%v", msg);
1347   vec_free (msg);
1348   return 0;
1349 }
1350
1351 /* *INDENT-OFF* */
1352 VLIB_CLI_COMMAND (one_show_status_command) = {
1353     .path = "show one status",
1354     .short_help = "show one status",
1355     .function = lisp_show_status_command_fn,
1356 };
1357 /* *INDENT-ON* */
1358
1359 static clib_error_t *
1360 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
1361                                     unformat_input_t * input,
1362                                     vlib_cli_command_t * cmd)
1363 {
1364   hash_pair_t *p;
1365   unformat_input_t _line_input, *line_input = &_line_input;
1366   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1367   uword *vni_table = 0;
1368   u8 is_l2 = 0;
1369   clib_error_t *error = NULL;
1370
1371   /* Get a line of input. */
1372   if (!unformat_user (input, unformat_line_input, line_input))
1373     return 0;
1374
1375   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1376     {
1377       if (unformat (line_input, "l2"))
1378         {
1379           vni_table = lcm->bd_id_by_vni;
1380           is_l2 = 1;
1381         }
1382       else if (unformat (line_input, "l3"))
1383         {
1384           vni_table = lcm->table_id_by_vni;
1385           is_l2 = 0;
1386         }
1387       else
1388         {
1389           error = clib_error_return (0, "parse error: '%U'",
1390                                      format_unformat_error, line_input);
1391           goto done;
1392         }
1393     }
1394
1395   if (!vni_table)
1396     {
1397       vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
1398       goto done;
1399     }
1400
1401   vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
1402
1403   /* *INDENT-OFF* */
1404   hash_foreach_pair (p, vni_table,
1405   ({
1406     vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
1407   }));
1408   /* *INDENT-ON* */
1409
1410 done:
1411   unformat_free (line_input);
1412
1413   return error;
1414 }
1415
1416 /* *INDENT-OFF* */
1417 VLIB_CLI_COMMAND (one_show_eid_table_map_command) = {
1418     .path = "show one eid-table map",
1419     .short_help = "show one eid-table l2|l3",
1420     .function = lisp_show_eid_table_map_command_fn,
1421 };
1422 /* *INDENT-ON* */
1423
1424
1425 static clib_error_t *
1426 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
1427                                      unformat_input_t * input,
1428                                      vlib_cli_command_t * cmd)
1429 {
1430   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1431   vnet_main_t *vnm = lgm->vnet_main;
1432   unformat_input_t _line_input, *line_input = &_line_input;
1433   u8 is_add = 1;
1434   clib_error_t *error = 0;
1435   u8 *locator_set_name = 0;
1436   locator_t locator, *locators = 0;
1437   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
1438   u32 ls_index = 0;
1439   int rv = 0;
1440
1441   memset (&locator, 0, sizeof (locator));
1442   memset (a, 0, sizeof (a[0]));
1443
1444   /* Get a line of input. */
1445   if (!unformat_user (input, unformat_line_input, line_input))
1446     return 0;
1447
1448   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1449     {
1450       if (unformat (line_input, "add %_%v%_", &locator_set_name))
1451         is_add = 1;
1452       else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1453         is_add = 0;
1454       else if (unformat (line_input, "iface %U p %d w %d",
1455                          unformat_vnet_sw_interface, vnm,
1456                          &locator.sw_if_index, &locator.priority,
1457                          &locator.weight))
1458         {
1459           locator.local = 1;
1460           vec_add1 (locators, locator);
1461         }
1462       else
1463         {
1464           error = unformat_parse_error (line_input);
1465           goto done;
1466         }
1467     }
1468
1469   a->name = locator_set_name;
1470   a->locators = locators;
1471   a->is_add = is_add;
1472   a->local = 1;
1473
1474   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
1475   if (0 != rv)
1476     {
1477       error = clib_error_return (0, "failed to %s locator-set!",
1478                                  is_add ? "add" : "delete");
1479     }
1480
1481 done:
1482   vec_free (locators);
1483   if (locator_set_name)
1484     vec_free (locator_set_name);
1485   unformat_free (line_input);
1486   return error;
1487 }
1488
1489 /* *INDENT-OFF* */
1490 VLIB_CLI_COMMAND (one_cp_add_del_locator_set_command) = {
1491     .path = "one locator-set",
1492     .short_help = "one locator-set add/del <name> [iface <iface-name> "
1493         "p <priority> w <weight>]",
1494     .function = lisp_add_del_locator_set_command_fn,
1495 };
1496 /* *INDENT-ON* */
1497
1498 static clib_error_t *
1499 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
1500                                         unformat_input_t * input,
1501                                         vlib_cli_command_t * cmd)
1502 {
1503   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1504   vnet_main_t *vnm = lgm->vnet_main;
1505   unformat_input_t _line_input, *line_input = &_line_input;
1506   u8 is_add = 1;
1507   clib_error_t *error = 0;
1508   u8 *locator_set_name = 0;
1509   u8 locator_set_name_set = 0;
1510   locator_t locator, *locators = 0;
1511   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
1512   u32 ls_index = 0;
1513
1514   memset (&locator, 0, sizeof (locator));
1515   memset (a, 0, sizeof (a[0]));
1516
1517   /* Get a line of input. */
1518   if (!unformat_user (input, unformat_line_input, line_input))
1519     return 0;
1520
1521   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1522     {
1523       if (unformat (line_input, "add"))
1524         is_add = 1;
1525       else if (unformat (line_input, "del"))
1526         is_add = 0;
1527       else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
1528         locator_set_name_set = 1;
1529       else if (unformat (line_input, "iface %U p %d w %d",
1530                          unformat_vnet_sw_interface, vnm,
1531                          &locator.sw_if_index, &locator.priority,
1532                          &locator.weight))
1533         {
1534           locator.local = 1;
1535           vec_add1 (locators, locator);
1536         }
1537       else
1538         {
1539           error = unformat_parse_error (line_input);
1540           goto done;
1541         }
1542     }
1543
1544   if (!locator_set_name_set)
1545     {
1546       error = clib_error_return (0, "locator_set name not set!");
1547       goto done;
1548     }
1549
1550   a->name = locator_set_name;
1551   a->locators = locators;
1552   a->is_add = is_add;
1553   a->local = 1;
1554
1555   vnet_lisp_add_del_locator (a, 0, &ls_index);
1556
1557 done:
1558   vec_free (locators);
1559   vec_free (locator_set_name);
1560   unformat_free (line_input);
1561   return error;
1562 }
1563
1564 /* *INDENT-OFF* */
1565 VLIB_CLI_COMMAND (one_cp_add_del_locator_in_set_command) = {
1566     .path = "one locator",
1567     .short_help = "one locator add/del locator-set <name> iface <iface-name> "
1568                   "p <priority> w <weight>",
1569     .function = lisp_add_del_locator_in_set_command_fn,
1570 };
1571 /* *INDENT-ON* */
1572
1573 static clib_error_t *
1574 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1575                                       unformat_input_t * input,
1576                                       vlib_cli_command_t * cmd)
1577 {
1578   locator_set_t *lsit;
1579   locator_t *loc;
1580   u32 *locit;
1581   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1582
1583   vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
1584                    "Priority", "Weight");
1585
1586   /* *INDENT-OFF* */
1587   pool_foreach (lsit, lcm->locator_set_pool,
1588   ({
1589     u8 * msg = 0;
1590     int next_line = 0;
1591     if (lsit->local)
1592       {
1593         msg = format (msg, "%v", lsit->name);
1594       }
1595     else
1596       {
1597         msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
1598       }
1599     vec_foreach (locit, lsit->locator_indices)
1600       {
1601         if (next_line)
1602           {
1603             msg = format (msg, "%16s", " ");
1604           }
1605         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1606         if (loc->local)
1607           msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
1608                         loc->weight);
1609         else
1610           msg = format (msg, "%16U%16d%16d\n", format_ip_address,
1611                         &gid_address_ip(&loc->address), loc->priority,
1612                         loc->weight);
1613         next_line = 1;
1614       }
1615     vlib_cli_output (vm, "%v", msg);
1616     vec_free (msg);
1617   }));
1618   /* *INDENT-ON* */
1619   return 0;
1620 }
1621
1622 /* *INDENT-OFF* */
1623 VLIB_CLI_COMMAND (one_cp_show_locator_sets_command) = {
1624     .path = "show one locator-set",
1625     .short_help = "Shows locator-sets",
1626     .function = lisp_cp_show_locator_sets_command_fn,
1627 };
1628 /* *INDENT-ON* */
1629
1630
1631 static clib_error_t *
1632 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1633                                       unformat_input_t * input,
1634                                       vlib_cli_command_t * cmd)
1635 {
1636   unformat_input_t _line_input, *line_input = &_line_input;
1637   u8 is_add = 1, addr_set = 0;
1638   ip_address_t ip_addr;
1639   clib_error_t *error = 0;
1640   int rv = 0;
1641   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
1642
1643   /* Get a line of input. */
1644   if (!unformat_user (input, unformat_line_input, line_input))
1645     return 0;
1646
1647   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1648     {
1649       if (unformat (line_input, "add"))
1650         is_add = 1;
1651       else if (unformat (line_input, "del"))
1652         is_add = 0;
1653       else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1654         addr_set = 1;
1655       else
1656         {
1657           error = unformat_parse_error (line_input);
1658           goto done;
1659         }
1660     }
1661
1662   if (!addr_set)
1663     {
1664       error = clib_error_return (0, "Map-resolver address must be set!");
1665       goto done;
1666     }
1667
1668   a->is_add = is_add;
1669   a->address = ip_addr;
1670   rv = vnet_lisp_add_del_map_resolver (a);
1671   if (0 != rv)
1672     {
1673       error = clib_error_return (0, "failed to %s map-resolver!",
1674                                  is_add ? "add" : "delete");
1675     }
1676
1677 done:
1678   unformat_free (line_input);
1679   return error;
1680 }
1681
1682 /* *INDENT-OFF* */
1683 VLIB_CLI_COMMAND (one_add_del_map_resolver_command) = {
1684     .path = "one map-resolver",
1685     .short_help = "one map-resolver add/del <ip_address>",
1686     .function = lisp_add_del_map_resolver_command_fn,
1687 };
1688 /* *INDENT-ON* */
1689
1690
1691 static clib_error_t *
1692 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1693                                         unformat_input_t * input,
1694                                         vlib_cli_command_t * cmd)
1695 {
1696   unformat_input_t _line_input, *line_input = &_line_input;
1697   u8 is_add = 1;
1698   u8 *locator_set_name = 0;
1699   clib_error_t *error = 0;
1700   int rv = 0;
1701   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
1702
1703   /* Get a line of input. */
1704   if (!unformat_user (input, unformat_line_input, line_input))
1705     return 0;
1706
1707   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1708     {
1709       if (unformat (line_input, "del"))
1710         is_add = 0;
1711       else if (unformat (line_input, "add %_%v%_", &locator_set_name))
1712         is_add = 1;
1713       else
1714         {
1715           error = unformat_parse_error (line_input);
1716           goto done;
1717         }
1718     }
1719
1720   a->is_add = is_add;
1721   a->locator_set_name = locator_set_name;
1722   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
1723   if (0 != rv)
1724     {
1725       error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
1726                                  is_add ? "add" : "delete");
1727     }
1728
1729 done:
1730   vec_free (locator_set_name);
1731   unformat_free (line_input);
1732   return error;
1733
1734 }
1735
1736 /* *INDENT-OFF* */
1737 VLIB_CLI_COMMAND (one_add_del_map_request_command) = {
1738     .path = "one map-request itr-rlocs",
1739     .short_help = "one map-request itr-rlocs add/del <locator_set_name>",
1740     .function = lisp_add_del_mreq_itr_rlocs_command_fn,
1741 };
1742 /* *INDENT-ON* */
1743
1744 static clib_error_t *
1745 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1746                                      unformat_input_t * input,
1747                                      vlib_cli_command_t * cmd)
1748 {
1749   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1750   locator_set_t *loc_set;
1751
1752   vlib_cli_output (vm, "%=20s", "itr-rlocs");
1753
1754   if (~0 == lcm->mreq_itr_rlocs)
1755     {
1756       return 0;
1757     }
1758
1759   loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
1760
1761   vlib_cli_output (vm, "%=20s", loc_set->name);
1762
1763   return 0;
1764 }
1765
1766 /* *INDENT-OFF* */
1767 VLIB_CLI_COMMAND (one_show_map_request_command) = {
1768     .path = "show one map-request itr-rlocs",
1769     .short_help = "Shows map-request itr-rlocs",
1770     .function = lisp_show_mreq_itr_rlocs_command_fn,
1771 };
1772 /* *INDENT-ON* */
1773
1774 static clib_error_t *
1775 lisp_use_petr_set_locator_set_command_fn (vlib_main_t * vm,
1776                                           unformat_input_t * input,
1777                                           vlib_cli_command_t * cmd)
1778 {
1779   u8 is_add = 1, ip_set = 0;
1780   unformat_input_t _line_input, *line_input = &_line_input;
1781   clib_error_t *error = 0;
1782   ip_address_t ip;
1783
1784   /* Get a line of input. */
1785   if (!unformat_user (input, unformat_line_input, line_input))
1786     return 0;
1787
1788   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1789     {
1790       if (unformat (line_input, "%U", unformat_ip_address, &ip))
1791         ip_set = 1;
1792       else if (unformat (line_input, "disable"))
1793         is_add = 0;
1794       else
1795         {
1796           error = clib_error_return (0, "parse error");
1797           goto done;
1798         }
1799     }
1800
1801   if (!ip_set)
1802     {
1803       clib_warning ("No petr IP specified!");
1804       goto done;
1805     }
1806
1807   if (vnet_lisp_use_petr (&ip, is_add))
1808     {
1809       error = clib_error_return (0, "failed to %s petr!",
1810                                  is_add ? "add" : "delete");
1811     }
1812
1813 done:
1814   unformat_free (line_input);
1815   return error;
1816 }
1817
1818 /* *INDENT-OFF* */
1819 VLIB_CLI_COMMAND (one_use_petr_set_locator_set_command) = {
1820     .path = "one use-petr",
1821     .short_help = "one use-petr [disable] <petr-ip>",
1822     .function = lisp_use_petr_set_locator_set_command_fn,
1823 };
1824
1825 static clib_error_t *
1826 lisp_show_petr_command_fn (vlib_main_t * vm,
1827                            unformat_input_t * input, vlib_cli_command_t * cmd)
1828 {
1829   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1830   mapping_t *m;
1831   locator_set_t *ls;
1832   locator_t *loc;
1833   u8 *tmp_str = 0;
1834   u8 use_petr = lcm->flags & LISP_FLAG_USE_PETR;
1835   vlib_cli_output (vm, "%=20s%=16s", "petr", use_petr ? "ip" : "");
1836
1837   if (!use_petr)
1838     {
1839       vlib_cli_output (vm, "%=20s", "disable");
1840       return 0;
1841     }
1842
1843   if (~0 == lcm->petr_map_index)
1844     {
1845       tmp_str = format (0, "N/A");
1846     }
1847   else
1848     {
1849       m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1850       if (~0 != m->locator_set_index)
1851         {
1852           ls = pool_elt_at_index(lcm->locator_set_pool, m->locator_set_index);
1853           loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
1854           tmp_str = format (0, "%U", format_ip_address, &loc->address);
1855         }
1856       else
1857         {
1858           tmp_str = format (0, "N/A");
1859         }
1860     }
1861   vec_add1 (tmp_str, 0);
1862
1863   vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1864
1865   vec_free (tmp_str);
1866
1867   return 0;
1868 }
1869
1870 /* *INDENT-OFF* */
1871 VLIB_CLI_COMMAND (one_show_petr_command) = {
1872     .path = "show one petr",
1873     .short_help = "Show petr",
1874     .function = lisp_show_petr_command_fn,
1875 };
1876 /* *INDENT-ON* */
1877
1878 static clib_error_t *
1879 lisp_show_map_servers_command_fn (vlib_main_t * vm,
1880                                   unformat_input_t * input,
1881                                   vlib_cli_command_t * cmd)
1882 {
1883   lisp_msmr_t *ms;
1884   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1885
1886   vec_foreach (ms, lcm->map_servers)
1887   {
1888     vlib_cli_output (vm, "%U", format_ip_address, &ms->address);
1889   }
1890   return 0;
1891 }
1892
1893 /* *INDENT-OFF* */
1894 VLIB_CLI_COMMAND (one_show_map_servers_command) = {
1895     .path = "show one map-servers",
1896     .short_help = "show one map servers",
1897     .function = lisp_show_map_servers_command_fn,
1898 };
1899 /* *INDENT-ON* */
1900
1901 static clib_error_t *
1902 lisp_show_map_register_state_command_fn (vlib_main_t * vm,
1903                                          unformat_input_t * input,
1904                                          vlib_cli_command_t * cmd)
1905 {
1906   u8 *msg = 0;
1907   u8 is_enabled = vnet_lisp_map_register_state_get ();
1908
1909   msg = format (msg, "%s\n", is_enabled ? "enabled" : "disabled");
1910   vlib_cli_output (vm, "%v", msg);
1911   vec_free (msg);
1912   return 0;
1913 }
1914
1915 /* *INDENT-OFF* */
1916 VLIB_CLI_COMMAND (one_show_map_register_state_command) = {
1917     .path = "show one map-register state",
1918     .short_help = "show one map-register state",
1919     .function = lisp_show_map_register_state_command_fn,
1920 };
1921 /* *INDENT-ON* */
1922
1923 static clib_error_t *
1924 lisp_show_rloc_probe_state_command_fn (vlib_main_t * vm,
1925                                        unformat_input_t * input,
1926                                        vlib_cli_command_t * cmd)
1927 {
1928   u8 *msg = 0;
1929   u8 is_enabled = vnet_lisp_rloc_probe_state_get ();
1930
1931   msg = format (msg, "%s\n", is_enabled ? "enabled" : "disabled");
1932   vlib_cli_output (vm, "%v", msg);
1933   vec_free (msg);
1934   return 0;
1935 }
1936
1937 /* *INDENT-OFF* */
1938 VLIB_CLI_COMMAND (one_show_rloc_probe_state_command) = {
1939     .path = "show one rloc state",
1940     .short_help = "show one RLOC state",
1941     .function = lisp_show_rloc_probe_state_command_fn,
1942 };
1943 /* *INDENT-ON* */
1944
1945 static clib_error_t *
1946 lisp_show_stats_command_fn (vlib_main_t * vm,
1947                             unformat_input_t * input,
1948                             vlib_cli_command_t * cmd)
1949 {
1950   u8 is_enabled = vnet_lisp_stats_enable_disable_state ();
1951   vlib_cli_output (vm, "%s\n", is_enabled ? "enabled" : "disabled");
1952   return 0;
1953 }
1954
1955 /* *INDENT-OFF* */
1956 VLIB_CLI_COMMAND (one_show_stats_command) = {
1957     .path = "show one statistics status",
1958     .short_help = "show ONE statistics enable/disable status",
1959     .function = lisp_show_stats_command_fn,
1960 };
1961 /* *INDENT-ON* */
1962
1963 static clib_error_t *
1964 lisp_show_stats_details_command_fn (vlib_main_t * vm,
1965                                     unformat_input_t * input,
1966                                     vlib_cli_command_t * cmd)
1967 {
1968   lisp_api_stats_t *stat, *stats = vnet_lisp_get_stats ();
1969
1970   if (vec_len (stats) > 0)
1971     vlib_cli_output (vm,
1972                      "[src-EID, dst-EID] [loc-rloc, rmt-rloc] count bytes\n");
1973   else
1974     vlib_cli_output (vm, "No statistics found.\n");
1975
1976   vec_foreach (stat, stats)
1977   {
1978     vlib_cli_output (vm, "[%U, %U] [%U, %U] %7u %7u\n",
1979                      format_fid_address, &stat->seid,
1980                      format_fid_address, &stat->deid,
1981                      format_ip_address, &stat->loc_rloc,
1982                      format_ip_address, &stat->rmt_rloc,
1983                      stat->counters.packets, stat->counters.bytes);
1984   }
1985   vec_free (stats);
1986   return 0;
1987 }
1988
1989 /* *INDENT-OFF* */
1990 VLIB_CLI_COMMAND (one_show_stats_details_command) = {
1991     .path = "show one statistics details",
1992     .short_help = "show ONE statistics",
1993     .function = lisp_show_stats_details_command_fn,
1994 };
1995 /* *INDENT-ON* */
1996
1997 static clib_error_t *
1998 lisp_stats_enable_disable_command_fn (vlib_main_t * vm,
1999                                       unformat_input_t * input,
2000                                       vlib_cli_command_t * cmd)
2001 {
2002   unformat_input_t _line_input, *line_input = &_line_input;
2003   u8 enable = 0;
2004
2005   /* Get a line of input. */
2006   if (!unformat_user (input, unformat_line_input, line_input))
2007     return 0;
2008
2009   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2010     {
2011       if (unformat (line_input, "enable"))
2012         enable = 1;
2013       else if (unformat (line_input, "disable"))
2014         enable = 0;
2015       else
2016         {
2017           clib_warning ("Error: expected enable/disable!");
2018           goto done;
2019         }
2020     }
2021   vnet_lisp_stats_enable_disable (enable);
2022 done:
2023   unformat_free (line_input);
2024   return 0;
2025 }
2026
2027 /* *INDENT-OFF* */
2028 VLIB_CLI_COMMAND (one_stats_enable_disable_command) = {
2029     .path = "one statistics",
2030     .short_help = "enable/disable ONE statistics collecting",
2031     .function = lisp_stats_enable_disable_command_fn,
2032 };
2033 /* *INDENT-ON* */
2034
2035 static clib_error_t *
2036 lisp_stats_flush_command_fn (vlib_main_t * vm,
2037                              unformat_input_t * input,
2038                              vlib_cli_command_t * cmd)
2039 {
2040   vnet_lisp_flush_stats ();
2041   return 0;
2042 }
2043
2044 /* *INDENT-OFF* */
2045 VLIB_CLI_COMMAND (one_stats_flush_command) = {
2046     .path = "one statistics flush",
2047     .short_help = "Flush ONE statistics",
2048     .function = lisp_stats_flush_command_fn,
2049 };
2050 /* *INDENT-ON* */
2051
2052 /*
2053  * fd.io coding-style-patch-verification: ON
2054  *
2055  * Local Variables:
2056  * eval: (c-set-style "gnu")
2057  * End:
2058  */