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