ONE-4: Add LISP enable/disable API/CLI
[vpp.git] / vpp-api-test / vat / api_format.c
1 /*
2  *------------------------------------------------------------------
3  * api_format.c 
4  * 
5  * Copyright (c) 2014 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License. 
17  *------------------------------------------------------------------
18  */
19
20 #include <vat/vat.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23 #include <vlibsocket/api.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/sr/sr_packet.h>
26 #include <vnet/l2/l2_input.h>
27 #include <vnet/l2tp/l2tp.h>
28 #include <vnet/vxlan/vxlan.h>
29 #include <vnet/gre/gre.h>
30 #include <vnet/nsh-gre/nsh_gre.h>
31 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
32 #include <vnet/lisp-gpe/lisp_gpe.h>
33
34 #include <api/vpe_msg_enum.h>
35 #include <vnet/l2/l2_classify.h> 
36 #include <vnet/l2/l2_vtr.h>
37 #include <vnet/classify/input_acl.h>
38 #if DPDK > 0
39 #include <vnet/ipsec/ipsec.h>
40 #include <vnet/ipsec/ikev2.h>
41 #else
42 #include <inttypes.h>
43 #endif
44 #include <vnet/map/map.h>
45 #include <vnet/cop/cop.h>
46 #include <vnet/ip/ip6_hop_by_hop.h>
47
48 #include "vat/json_format.h"
49
50 #define vl_typedefs             /* define message structures */
51 #include <api/vpe_all_api_h.h> 
52 #undef vl_typedefs
53
54 /* declare message handlers for each api */
55
56 #define vl_endianfun             /* define message structures */
57 #include <api/vpe_all_api_h.h> 
58 #undef vl_endianfun
59
60 /* instantiate all the print functions we know about */
61 #define vl_print(handle, ...)
62 #define vl_printfun
63 #include <api/vpe_all_api_h.h>
64 #undef vl_printfun
65
66 uword unformat_sw_if_index (unformat_input_t * input, va_list * args)
67 {
68   vat_main_t * vam = va_arg (*args, vat_main_t *);
69   u32 * result = va_arg (*args, u32 *);
70   u8 * if_name;
71   uword * p;
72
73   if (!unformat (input, "%s", &if_name))
74       return 0;
75
76   p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
77   if (p == 0)
78       return 0;
79   *result = p[0];
80   return 1;
81 }
82
83 /* Parse an IP4 address %d.%d.%d.%d. */
84 uword unformat_ip4_address (unformat_input_t * input, va_list * args)
85 {
86   u8 * result = va_arg (*args, u8 *);
87   unsigned a[4];
88
89   if (! unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
90     return 0;
91
92   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
93     return 0;
94
95   result[0] = a[0];
96   result[1] = a[1];
97   result[2] = a[2];
98   result[3] = a[3];
99
100   return 1;
101 }
102
103
104 uword
105 unformat_ethernet_address (unformat_input_t * input, va_list * args)
106 {
107   u8 * result = va_arg (*args, u8 *);
108   u32 i, a[6];
109
110   if (! unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
111                   &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
112     return 0;
113
114   /* Check range. */
115   for (i = 0; i < 6; i++)
116     if (a[i] >= (1 << 8))
117       return 0;
118
119   for (i = 0; i < 6; i++)
120     result[i] = a[i];
121
122   return 1;
123 }
124
125 /* Returns ethernet type as an int in host byte order. */
126 uword
127 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
128                                         va_list * args)
129 {
130   u16 * result = va_arg (*args, u16 *);
131   int type;
132
133   /* Numeric type. */
134   if (unformat (input, "0x%x", &type)
135       || unformat (input, "%d", &type))
136     {
137       if (type >= (1 << 16))
138         return 0;
139       *result = type;
140       return 1;
141     }
142   return 0;
143 }
144
145 /* Parse an IP6 address. */
146 uword unformat_ip6_address (unformat_input_t * input, va_list * args)
147 {
148   ip6_address_t * result = va_arg (*args, ip6_address_t *);
149   u16 hex_quads[8];
150   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
151   uword c, n_colon, double_colon_index;
152
153   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
154   double_colon_index = ARRAY_LEN (hex_quads);
155   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
156     {
157       hex_digit = 16;
158       if (c >= '0' && c <= '9')
159         hex_digit = c - '0';
160       else if (c >= 'a' && c <= 'f')
161         hex_digit = c + 10 - 'a';
162       else if (c >= 'A' && c <= 'F')
163         hex_digit = c + 10 - 'A';
164       else if (c == ':' && n_colon < 2)
165         n_colon++;
166       else
167         {
168           unformat_put_input (input);
169           break;
170         }
171
172       /* Too many hex quads. */
173       if (n_hex_quads >= ARRAY_LEN (hex_quads))
174         return 0;
175
176       if (hex_digit < 16)
177         {
178           hex_quad = (hex_quad << 4) | hex_digit;
179
180           /* Hex quad must fit in 16 bits. */
181           if (n_hex_digits >= 4)
182             return 0;
183
184           n_colon = 0;
185           n_hex_digits++;
186         }
187       
188       /* Save position of :: */
189       if (n_colon == 2)
190         {
191           /* More than one :: ? */
192           if (double_colon_index < ARRAY_LEN (hex_quads))
193             return 0;
194           double_colon_index = n_hex_quads;
195         }
196
197       if (n_colon > 0 && n_hex_digits > 0)
198         {
199           hex_quads[n_hex_quads++] = hex_quad;
200           hex_quad = 0;
201           n_hex_digits = 0;
202         }
203     }
204
205   if (n_hex_digits > 0)
206     hex_quads[n_hex_quads++] = hex_quad;
207
208   {
209     word i;
210
211     /* Expand :: to appropriate number of zero hex quads. */
212     if (double_colon_index < ARRAY_LEN (hex_quads))
213       {
214         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
215
216         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
217           hex_quads[n_zero + i] = hex_quads[i];
218
219         for (i = 0; i < n_zero; i++)
220           hex_quads[double_colon_index + i] = 0;
221
222         n_hex_quads = ARRAY_LEN (hex_quads);
223       }
224
225     /* Too few hex quads given. */
226     if (n_hex_quads < ARRAY_LEN (hex_quads))
227       return 0;
228
229     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
230       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
231
232     return 1;
233   }
234 }
235
236 uword
237 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
238 {
239 #if DPDK > 0
240   u32 * r = va_arg (*args, u32 *);
241
242   if (0) ;
243 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
244   foreach_ipsec_policy_action
245 #undef _
246   else
247     return 0;
248   return 1;
249 #else
250   return 0;
251 #endif
252 }
253
254 uword
255 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
256 {
257 #if DPDK > 0
258   u32 * r = va_arg (*args, u32 *);
259
260   if (0) ;
261 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
262   foreach_ipsec_crypto_alg
263 #undef _
264   else
265     return 0;
266   return 1;
267 #else
268   return 0;
269 #endif
270 }
271
272 u8 *
273 format_ipsec_crypto_alg (u8 * s, va_list * args)
274 {
275 #if DPDK > 0
276   u32 i = va_arg (*args, u32);
277   u8 * t = 0;
278
279   switch (i)
280     {
281 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
282   foreach_ipsec_crypto_alg
283 #undef _
284       default:
285         return format (s, "unknown");
286     }
287   return format (s, "%s", t);
288 #else
289   return format (s, "Unimplemented");
290 #endif
291 }
292
293 uword
294 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
295 {
296 #if DPDK > 0
297   u32 * r = va_arg (*args, u32 *);
298
299   if (0) ;
300 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
301   foreach_ipsec_integ_alg
302 #undef _
303   else
304     return 0;
305   return 1;
306 #else
307   return 0;
308 #endif
309 }
310
311 u8 *
312 format_ipsec_integ_alg (u8 * s, va_list * args)
313 {
314 #if DPDK > 0
315   u32 i = va_arg (*args, u32);
316   u8 * t = 0;
317
318   switch (i)
319     {
320 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
321   foreach_ipsec_integ_alg
322 #undef _
323       default:
324         return format (s, "unknown");
325     }
326   return format (s, "%s", t);
327 #else
328   return format (s, "Unsupported");
329 #endif
330 }
331
332 uword
333 unformat_ikev2_auth_method (unformat_input_t * input, va_list * args)
334 {
335 #if DPDK > 0
336   u32 * r = va_arg (*args, u32 *);
337
338   if (0) ;
339 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_AUTH_METHOD_##f;
340   foreach_ikev2_auth_method
341 #undef _
342   else
343     return 0;
344   return 1;
345 #else
346   return 0;
347 #endif
348 }
349
350 uword
351 unformat_ikev2_id_type (unformat_input_t * input, va_list * args)
352 {
353 #if DPDK > 0
354   u32 * r = va_arg (*args, u32 *);
355
356   if (0) ;
357 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_ID_TYPE_##f;
358   foreach_ikev2_id_type
359 #undef _
360   else
361     return 0;
362   return 1;
363 #else
364   return 0;
365 #endif
366 }
367
368 u8 * format_ip4_address (u8 * s, va_list * args)
369 {
370   u8 * a = va_arg (*args, u8 *);
371   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
372 }
373
374 u8 * format_ip6_address (u8 * s, va_list * args)
375 {
376     ip6_address_t * a = va_arg (*args, ip6_address_t *);
377     u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
378
379     i_max_n_zero = ARRAY_LEN (a->as_u16);
380     max_n_zeros = 0;
381     i_first_zero = i_max_n_zero;
382     n_zeros = 0;
383     for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
384       {
385         u32 is_zero = a->as_u16[i] == 0;
386         if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
387           {
388             i_first_zero = i;
389             n_zeros = 0;
390           }
391         n_zeros += is_zero;
392         if ((! is_zero && n_zeros > max_n_zeros)
393             || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
394           {
395             i_max_n_zero = i_first_zero;
396             max_n_zeros = n_zeros;
397             i_first_zero = ARRAY_LEN (a->as_u16);
398             n_zeros = 0;
399           }
400       }
401
402     last_double_colon = 0;
403     for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
404       {
405         if (i == i_max_n_zero && max_n_zeros > 1)
406           {
407             s = format (s, "::");
408             i += max_n_zeros - 1;
409             last_double_colon = 1;
410           }
411         else
412           {
413             s = format (s, "%s%x",
414                         (last_double_colon || i == 0) ? "" : ":",
415                         clib_net_to_host_u16 (a->as_u16[i]));
416             last_double_colon = 0;
417           }
418       }
419
420     return s;
421 }
422
423 /* Format an IP46 address. */
424 u8 * format_ip46_address (u8 * s, va_list * args)
425 {
426   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
427   return ip46_address_is_ip4(ip46)?
428       format(s, "%U", format_ip4_address, &ip46->ip4):
429       format(s, "%U", format_ip6_address, &ip46->ip6);
430 }
431
432 u8 * format_ethernet_address (u8 * s, va_list * args)
433 {
434   u8 * a = va_arg (*args, u8 *);
435
436   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
437                  a[0], a[1], a[2], a[3], a[4], a[5]);
438 }
439
440 void increment_v4_address (ip4_address_t * a)
441 {
442     u32 v;
443
444     v = ntohl(a->as_u32) + 1;
445     a->as_u32 = ntohl(v);
446 }
447
448 void increment_v6_address (ip6_address_t * a)
449 {
450     u64 v0, v1;
451
452     v0 = clib_net_to_host_u64 (a->as_u64[0]);
453     v1 = clib_net_to_host_u64 (a->as_u64[1]);
454
455     v1 += 1;
456     if (v1 == 0)
457         v0 += 1;
458     a->as_u64[0] = clib_net_to_host_u64 (v0);
459     a->as_u64[1] = clib_net_to_host_u64 (v1);
460 }
461
462
463 static void vl_api_create_loopback_reply_t_handler 
464 (vl_api_create_loopback_reply_t * mp)
465 {
466     vat_main_t * vam = &vat_main;
467     i32 retval = ntohl(mp->retval);
468
469     vam->retval = retval;
470     vam->result_ready = 1;
471     vam->regenerate_interface_table = 1;
472 }
473
474 static void vl_api_create_loopback_reply_t_handler_json
475 (vl_api_create_loopback_reply_t * mp)
476 {
477     vat_main_t * vam = &vat_main;
478     vat_json_node_t node;
479
480     vat_json_init_object(&node);
481     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
482     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
483
484     vat_json_print(vam->ofp, &node);
485     vat_json_free(&node);
486
487     vam->retval = ntohl(mp->retval);
488     vam->result_ready = 1;
489 }
490
491 static void vl_api_create_vlan_subif_reply_t_handler 
492 (vl_api_create_vlan_subif_reply_t * mp)
493 {
494     vat_main_t * vam = &vat_main;
495     i32 retval = ntohl(mp->retval);
496
497     vam->retval = retval;
498     vam->result_ready = 1;
499     vam->regenerate_interface_table = 1;
500 }
501
502 static void vl_api_create_vlan_subif_reply_t_handler_json
503 (vl_api_create_vlan_subif_reply_t * mp)
504 {
505     vat_main_t * vam = &vat_main;
506     vat_json_node_t node;
507
508     vat_json_init_object(&node);
509     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
510     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
511
512     vat_json_print(vam->ofp, &node);
513     vat_json_free(&node);
514
515     vam->retval = ntohl(mp->retval);
516     vam->result_ready = 1;
517 }
518
519 static void vl_api_create_subif_reply_t_handler 
520 (vl_api_create_subif_reply_t * mp)
521 {
522     vat_main_t * vam = &vat_main;
523     i32 retval = ntohl(mp->retval);
524
525     vam->retval = retval;
526     vam->result_ready = 1;
527     vam->regenerate_interface_table = 1;
528 }
529
530 static void vl_api_create_subif_reply_t_handler_json
531 (vl_api_create_subif_reply_t * mp)
532 {
533     vat_main_t * vam = &vat_main;
534     vat_json_node_t node;
535
536     vat_json_init_object(&node);
537     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
538     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
539
540     vat_json_print(vam->ofp, &node);
541     vat_json_free(&node);
542
543     vam->retval = ntohl(mp->retval);
544     vam->result_ready = 1;
545 }
546
547 static void vl_api_interface_name_renumber_reply_t_handler 
548 (vl_api_interface_name_renumber_reply_t * mp)
549 {
550     vat_main_t * vam = &vat_main;
551     i32 retval = ntohl(mp->retval);
552
553     vam->retval = retval;
554     vam->result_ready = 1;
555     vam->regenerate_interface_table = 1;
556 }
557
558 static void vl_api_interface_name_renumber_reply_t_handler_json
559 (vl_api_interface_name_renumber_reply_t * mp)
560 {
561     vat_main_t * vam = &vat_main;
562     vat_json_node_t node;
563
564     vat_json_init_object(&node);
565     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
566
567     vat_json_print(vam->ofp, &node);
568     vat_json_free(&node);
569
570     vam->retval = ntohl(mp->retval);
571     vam->result_ready = 1;
572 }
573
574 /* 
575  * Special-case: build the interface table, maintain
576  * the next loopback sw_if_index vbl.
577  */
578 static void vl_api_sw_interface_details_t_handler
579 (vl_api_sw_interface_details_t * mp)
580 {
581     vat_main_t * vam = &vat_main;
582     u8 * s = format (0, "%s%c", mp->interface_name, 0);
583
584     hash_set_mem (vam->sw_if_index_by_interface_name, s, 
585                   ntohl(mp->sw_if_index));
586
587     /* In sub interface case, fill the sub interface table entry */
588     if (mp->sw_if_index != mp->sup_sw_if_index) {
589         sw_interface_subif_t * sub = NULL;
590
591         vec_add2(vam->sw_if_subif_table, sub, 1);
592
593         vec_validate(sub->interface_name, strlen((char *)s) + 1);
594         strncpy((char *)sub->interface_name, (char *)s,
595                 vec_len(sub->interface_name));
596         sub->sw_if_index = ntohl(mp->sw_if_index);
597         sub->sub_id = ntohl(mp->sub_id);
598
599         sub->sub_dot1ad = mp->sub_dot1ad;
600         sub->sub_number_of_tags = mp->sub_number_of_tags;
601         sub->sub_outer_vlan_id = ntohs(mp->sub_outer_vlan_id);
602         sub->sub_inner_vlan_id = ntohs(mp->sub_inner_vlan_id);
603         sub->sub_exact_match = mp->sub_exact_match;
604         sub->sub_default = mp->sub_default;
605         sub->sub_outer_vlan_id_any = mp->sub_outer_vlan_id_any;
606         sub->sub_inner_vlan_id_any = mp->sub_inner_vlan_id_any;
607
608         /* vlan tag rewrite */
609         sub->vtr_op = ntohl(mp->vtr_op);
610         sub->vtr_push_dot1q = ntohl(mp->vtr_push_dot1q);
611         sub->vtr_tag1 = ntohl(mp->vtr_tag1);
612         sub->vtr_tag2 = ntohl(mp->vtr_tag2);
613     }
614 }
615
616 static void vl_api_sw_interface_details_t_handler_json
617 (vl_api_sw_interface_details_t * mp)
618 {
619     vat_main_t * vam = &vat_main;
620     vat_json_node_t *node = NULL;
621
622     if (VAT_JSON_ARRAY != vam->json_tree.type) {
623         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
624         vat_json_init_array(&vam->json_tree);
625     }
626     node = vat_json_array_add(&vam->json_tree);
627
628     vat_json_init_object(node);
629     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
630     vat_json_object_add_uint(node, "sup_sw_if_index", ntohl(mp->sup_sw_if_index));
631     vat_json_object_add_uint(node, "l2_address_length", ntohl(mp->l2_address_length));
632     vat_json_object_add_bytes(node, "l2_address", mp->l2_address, sizeof(mp->l2_address));
633     vat_json_object_add_string_copy(node, "interface_name", mp->interface_name);
634     vat_json_object_add_uint(node, "admin_up_down", mp->admin_up_down);
635     vat_json_object_add_uint(node, "link_up_down", mp->link_up_down);
636     vat_json_object_add_uint(node, "link_duplex", mp->link_duplex);
637     vat_json_object_add_uint(node, "link_speed", mp->link_speed);
638     vat_json_object_add_uint(node, "mtu", ntohs(mp->link_mtu));
639     vat_json_object_add_uint(node, "sub_id", ntohl(mp->sub_id));
640     vat_json_object_add_uint(node, "sub_dot1ad", mp->sub_dot1ad);
641     vat_json_object_add_uint(node, "sub_number_of_tags", mp->sub_number_of_tags);
642     vat_json_object_add_uint(node, "sub_outer_vlan_id", ntohs(mp->sub_outer_vlan_id));
643     vat_json_object_add_uint(node, "sub_inner_vlan_id", ntohs(mp->sub_inner_vlan_id));
644     vat_json_object_add_uint(node, "sub_exact_match", mp->sub_exact_match);
645     vat_json_object_add_uint(node, "sub_default", mp->sub_default);
646     vat_json_object_add_uint(node, "sub_outer_vlan_id_any", mp->sub_outer_vlan_id_any);
647     vat_json_object_add_uint(node, "sub_inner_vlan_id_any", mp->sub_inner_vlan_id_any);
648     vat_json_object_add_uint(node, "vtr_op", ntohl(mp->vtr_op));
649     vat_json_object_add_uint(node, "vtr_push_dot1q", ntohl(mp->vtr_push_dot1q));
650     vat_json_object_add_uint(node, "vtr_tag1", ntohl(mp->vtr_tag1));
651     vat_json_object_add_uint(node, "vtr_tag2", ntohl(mp->vtr_tag2));
652 }
653
654 static void vl_api_sw_interface_set_flags_t_handler
655 (vl_api_sw_interface_set_flags_t * mp)
656 {
657     vat_main_t * vam = &vat_main;
658     if (vam->interface_event_display)
659         errmsg ("interface flags: sw_if_index %d %s %s\n",
660                 ntohl(mp->sw_if_index),
661                 mp->admin_up_down ? "admin-up" : "admin-down",
662                 mp->link_up_down  ? "link-up"  : "link-down");
663 }
664
665 static void vl_api_sw_interface_set_flags_t_handler_json
666 (vl_api_sw_interface_set_flags_t * mp)
667 {
668     /* JSON output not supported */
669 }
670
671 static void vl_api_cli_reply_t_handler
672 (vl_api_cli_reply_t * mp)
673 {
674     vat_main_t * vam = &vat_main;
675     i32 retval = ntohl(mp->retval);
676
677     vam->retval = retval;
678     vam->shmem_result = (u8 *) mp->reply_in_shmem;
679     vam->result_ready = 1;
680 }
681
682 static void vl_api_cli_reply_t_handler_json
683 (vl_api_cli_reply_t * mp)
684 {
685     vat_main_t * vam = &vat_main;
686     vat_json_node_t node;
687     api_main_t * am = &api_main;
688     void * oldheap;
689     u8 * reply;
690
691     vat_json_init_object(&node);
692     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
693     vat_json_object_add_uint(&node, "reply_in_shmem", 
694                              ntohl(mp->reply_in_shmem));
695     /* Toss the shared-memory original... */
696     pthread_mutex_lock (&am->vlib_rp->mutex);
697     oldheap = svm_push_data_heap (am->vlib_rp);
698
699     reply = (u8 *)(mp->reply_in_shmem);
700     vec_free (reply);
701     
702     svm_pop_heap (oldheap);
703     pthread_mutex_unlock (&am->vlib_rp->mutex);
704
705     vat_json_print(vam->ofp, &node);
706     vat_json_free(&node);
707
708     vam->retval = ntohl(mp->retval);
709     vam->result_ready = 1;
710 }
711
712 static void vl_api_classify_add_del_table_reply_t_handler
713 (vl_api_classify_add_del_table_reply_t * mp)
714 {
715     vat_main_t * vam = &vat_main;
716     i32 retval = ntohl(mp->retval);
717     if (vam->async_mode) {
718         vam->async_errors += (retval < 0);
719     } else {
720         vam->retval = retval;
721         vam->result_ready = 1;
722         if (retval == 0 && 
723             ((mp->new_table_index != 0xFFFFFFFF) ||
724              (mp->skip_n_vectors != 0xFFFFFFFF) ||
725              (mp->match_n_vectors != 0xFFFFFFFF)))
726             /* 
727              * Note: this is just barely thread-safe, depends on
728              * the main thread spinning waiting for an answer...
729              */
730             errmsg ("new index %d, skip_n_vectors %d, match_n_vectors %d\n",
731                     ntohl(mp->new_table_index),
732                     ntohl(mp->skip_n_vectors), ntohl(mp->match_n_vectors));
733     }
734 }
735
736 static void vl_api_classify_add_del_table_reply_t_handler_json
737 (vl_api_classify_add_del_table_reply_t * mp)
738 {
739     vat_main_t * vam = &vat_main;
740     vat_json_node_t node;
741
742     vat_json_init_object(&node);
743     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
744     vat_json_object_add_uint(&node, "new_table_index", ntohl(mp->new_table_index));
745     vat_json_object_add_uint(&node, "skip_n_vectors", ntohl(mp->skip_n_vectors));
746     vat_json_object_add_uint(&node, "match_n_vectors", ntohl(mp->match_n_vectors));
747
748     vat_json_print(vam->ofp, &node);
749     vat_json_free(&node);
750
751     vam->retval = ntohl(mp->retval);
752     vam->result_ready = 1;
753 }
754
755 static void vl_api_get_node_index_reply_t_handler
756 (vl_api_get_node_index_reply_t * mp)
757 {
758     vat_main_t * vam = &vat_main;
759     i32 retval = ntohl(mp->retval);
760     if (vam->async_mode) {
761         vam->async_errors += (retval < 0);
762     } else {
763         vam->retval = retval;
764         vam->result_ready = 1;
765         if (retval == 0)
766             errmsg ("node index %d\n", ntohl(mp->node_index));
767     }
768 }
769
770 static void vl_api_get_node_index_reply_t_handler_json
771 (vl_api_get_node_index_reply_t * mp)
772 {
773     vat_main_t * vam = &vat_main;
774     vat_json_node_t node;
775
776     vat_json_init_object(&node);
777     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
778     vat_json_object_add_uint(&node, "node_index", ntohl(mp->node_index));
779
780     vat_json_print(vam->ofp, &node);
781     vat_json_free(&node);
782
783     vam->retval = ntohl(mp->retval);
784     vam->result_ready = 1;
785 }
786
787 static void vl_api_add_node_next_reply_t_handler
788 (vl_api_add_node_next_reply_t * mp)
789 {
790     vat_main_t * vam = &vat_main;
791     i32 retval = ntohl(mp->retval);
792     if (vam->async_mode) {
793         vam->async_errors += (retval < 0);
794     } else {
795         vam->retval = retval;
796         vam->result_ready = 1;
797         if (retval == 0)
798             errmsg ("next index %d\n", ntohl(mp->next_index));
799     }
800 }
801
802 static void vl_api_add_node_next_reply_t_handler_json
803 (vl_api_add_node_next_reply_t * mp)
804 {
805     vat_main_t * vam = &vat_main;
806     vat_json_node_t node;
807
808     vat_json_init_object(&node);
809     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
810     vat_json_object_add_uint(&node, "next_index", ntohl(mp->next_index));
811
812     vat_json_print(vam->ofp, &node);
813     vat_json_free(&node);
814
815     vam->retval = ntohl(mp->retval);
816     vam->result_ready = 1;
817 }
818
819 static void vl_api_mpls_gre_add_del_tunnel_reply_t_handler 
820 (vl_api_mpls_gre_add_del_tunnel_reply_t * mp)
821 {
822     vat_main_t * vam = &vat_main;
823     i32 retval = ntohl(mp->retval);
824     u32 sw_if_index = ntohl(mp->tunnel_sw_if_index);
825
826     if (retval >= 0 && sw_if_index != (u32)~0) {
827         errmsg ("tunnel_sw_if_index %d\n", sw_if_index);
828     }
829     vam->retval = retval;
830     vam->result_ready = 1;
831 }
832
833 static void vl_api_mpls_gre_add_del_tunnel_reply_t_handler_json
834 (vl_api_mpls_gre_add_del_tunnel_reply_t * mp)
835 {
836     vat_main_t * vam = &vat_main;
837     vat_json_node_t node;
838
839     vat_json_init_object(&node);
840     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
841     vat_json_object_add_uint(&node, "tunnel_sw_if_index", ntohl(mp->tunnel_sw_if_index));
842
843     vat_json_print(vam->ofp, &node);
844     vat_json_free(&node);
845
846     vam->retval = ntohl(mp->retval);
847     vam->result_ready = 1;
848 }
849
850 static void vl_api_nsh_gre_add_del_tunnel_reply_t_handler 
851 (vl_api_nsh_gre_add_del_tunnel_reply_t * mp)
852 {
853     vat_main_t * vam = &vat_main;
854     i32 retval = ntohl(mp->retval);
855     u32 sw_if_index = ntohl(mp->sw_if_index);
856
857     if (retval >= 0 && sw_if_index != (u32)~0) {
858         errmsg ("sw_if_index %d\n", ntohl(mp->sw_if_index));
859     }
860     vam->retval = retval;
861     vam->result_ready = 1;
862 }
863
864 static void vl_api_nsh_gre_add_del_tunnel_reply_t_handler_json
865 (vl_api_nsh_gre_add_del_tunnel_reply_t * mp)
866 {
867     vat_main_t * vam = &vat_main;
868     vat_json_node_t node;
869
870     vat_json_init_object(&node);
871     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
872     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
873
874     vat_json_print(vam->ofp, &node);
875     vat_json_free(&node);
876
877     vam->retval = ntohl(mp->retval);
878     vam->result_ready = 1;
879 }
880
881 static void vl_api_nsh_vxlan_gpe_add_del_tunnel_reply_t_handler 
882 (vl_api_nsh_vxlan_gpe_add_del_tunnel_reply_t * mp)
883 {
884     vat_main_t * vam = &vat_main;
885     i32 retval = ntohl(mp->retval);
886     u32 sw_if_index = ntohl(mp->sw_if_index);
887
888     if (retval >= 0 && sw_if_index != (u32)~0) {
889         errmsg ("sw_if_index %d\n", ntohl(mp->sw_if_index));
890     }
891     vam->retval = retval;
892     vam->result_ready = 1;
893 }
894
895 static void vl_api_nsh_vxlan_gpe_add_del_tunnel_reply_t_handler_json
896 (vl_api_nsh_vxlan_gpe_add_del_tunnel_reply_t * mp)
897 {
898     vat_main_t * vam = &vat_main;
899     vat_json_node_t node;
900
901     vat_json_init_object(&node);
902     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
903     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
904
905     vat_json_print(vam->ofp, &node);
906     vat_json_free(&node);
907
908     vam->retval = ntohl(mp->retval);
909     vam->result_ready = 1;
910 }
911
912 static void vl_api_show_version_reply_t_handler 
913 (vl_api_show_version_reply_t * mp)
914 {
915     vat_main_t * vam = &vat_main;
916     i32 retval = ntohl(mp->retval);
917
918     if (retval >= 0) {
919         errmsg ("        program: %s\n", mp->program);
920         errmsg ("        version: %s\n", mp->version);
921         errmsg ("     build date: %s\n", mp->build_date);
922         errmsg ("build directory: %s\n", mp->build_directory);
923     }
924     vam->retval = retval;
925     vam->result_ready = 1;
926 }
927
928 static void vl_api_show_version_reply_t_handler_json
929 (vl_api_show_version_reply_t * mp)
930 {
931     vat_main_t * vam = &vat_main;
932     vat_json_node_t node;
933
934     vat_json_init_object(&node);
935     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
936     vat_json_object_add_string_copy(&node, "program", mp->program);
937     vat_json_object_add_string_copy(&node, "version", mp->version);
938     vat_json_object_add_string_copy(&node, "build_date", mp->build_date);
939     vat_json_object_add_string_copy(&node, "build_directory", mp->build_directory);
940
941     vat_json_print(vam->ofp, &node);
942     vat_json_free(&node);
943
944     vam->retval = ntohl(mp->retval);
945     vam->result_ready = 1;
946 }
947
948 static void vl_api_ip4_arp_event_t_handler 
949 (vl_api_ip4_arp_event_t * mp)
950 {
951     vat_main_t * vam = &vat_main;
952     errmsg ("arp event: address %U new mac %U sw_if_index %d\n",
953             format_ip4_address, &mp->address,
954             format_ethernet_address, mp->new_mac, mp->sw_if_index);
955 }
956
957 static void vl_api_ip4_arp_event_t_handler_json
958 (vl_api_ip4_arp_event_t * mp)
959 {
960     /* JSON output not supported */
961 }
962
963 /* 
964  * Special-case: build the bridge domain table, maintain
965  * the next bd id vbl.
966  */
967 static void vl_api_bridge_domain_details_t_handler
968 (vl_api_bridge_domain_details_t * mp)
969 {
970     vat_main_t * vam = &vat_main;
971     u32 n_sw_ifs =  ntohl (mp->n_sw_ifs);
972
973     fformat (vam->ofp, "\n%-3s %-3s %-3s %-3s %-3s %-3s\n",
974              " ID", "LRN", "FWD", "FLD", "BVI", "#IF");
975
976     fformat (vam->ofp, "%3d %3d %3d %3d %3d %3d\n",
977              ntohl (mp->bd_id), mp->learn, mp->forward,
978              mp->flood, ntohl (mp->bvi_sw_if_index), n_sw_ifs);
979
980     if (n_sw_ifs)
981         fformat (vam->ofp, "\n\n%s %s  %s\n", "sw_if_index", "SHG",
982                  "Interface Name");
983 }
984
985 static void vl_api_bridge_domain_details_t_handler_json
986 (vl_api_bridge_domain_details_t * mp)
987 {
988     vat_main_t * vam = &vat_main;
989     vat_json_node_t *node, *array = NULL;
990
991     if (VAT_JSON_ARRAY != vam->json_tree.type) {
992         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
993         vat_json_init_array(&vam->json_tree);
994     }
995     node = vat_json_array_add(&vam->json_tree);
996
997     vat_json_init_object(node);
998     vat_json_object_add_uint(node, "bd_id", ntohl(mp->bd_id));
999     vat_json_object_add_uint(node, "flood", mp->flood);
1000     vat_json_object_add_uint(node, "forward", mp->forward);
1001     vat_json_object_add_uint(node, "learn", mp->learn);
1002     vat_json_object_add_uint(node, "bvi_sw_if_index", ntohl(mp->bvi_sw_if_index));
1003     vat_json_object_add_uint(node, "n_sw_ifs", ntohl(mp->n_sw_ifs));
1004     array = vat_json_object_add(node, "sw_if");
1005     vat_json_init_array(array);
1006 }
1007
1008 /* 
1009  * Special-case: build the bridge domain sw if table.
1010  */
1011 static void vl_api_bridge_domain_sw_if_details_t_handler
1012 (vl_api_bridge_domain_sw_if_details_t * mp)
1013 {
1014     vat_main_t * vam = &vat_main;
1015     hash_pair_t * p;
1016     u8 * sw_if_name = 0;
1017     u32 sw_if_index;
1018
1019     sw_if_index = ntohl (mp->sw_if_index);
1020     hash_foreach_pair (p, vam->sw_if_index_by_interface_name, 
1021     ({
1022         if ((u32) p->value[0] == sw_if_index) {
1023             sw_if_name = (u8 *)(p->key);
1024             break;
1025         }
1026     }));
1027    
1028     fformat (vam->ofp, "%7d     %3d  %s", sw_if_index, 
1029              mp->shg, sw_if_name ? (char *)sw_if_name : 
1030              "sw_if_index not found!");
1031 }
1032
1033 static void vl_api_bridge_domain_sw_if_details_t_handler_json
1034 (vl_api_bridge_domain_sw_if_details_t * mp)
1035 {
1036     vat_main_t * vam = &vat_main;
1037     vat_json_node_t *node = NULL;
1038     uword last_index = 0;
1039
1040     ASSERT(VAT_JSON_ARRAY == vam->json_tree.type);
1041     ASSERT(vec_len(vam->json_tree.array) >= 1);
1042     last_index = vec_len(vam->json_tree.array) - 1;
1043     node = &vam->json_tree.array[last_index];
1044     node = vat_json_object_get_element(node, "sw_if");
1045     ASSERT(NULL != node);
1046     node = vat_json_array_add(node);
1047
1048     vat_json_init_object(node);
1049     vat_json_object_add_uint(node, "bd_id", ntohl(mp->bd_id));
1050     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
1051     vat_json_object_add_uint(node, "shg", mp->shg);
1052 }
1053
1054 static void vl_api_control_ping_reply_t_handler
1055 (vl_api_control_ping_reply_t * mp)
1056 {
1057     vat_main_t * vam = &vat_main;
1058     i32 retval = ntohl(mp->retval);
1059     if (vam->async_mode) {
1060         vam->async_errors += (retval < 0);
1061     } else {
1062         vam->retval = retval;
1063         vam->result_ready = 1;
1064     }
1065 }
1066
1067 static void vl_api_control_ping_reply_t_handler_json
1068 (vl_api_control_ping_reply_t * mp)
1069 {
1070     vat_main_t * vam = &vat_main;
1071     i32 retval = ntohl(mp->retval);
1072
1073     if (VAT_JSON_NONE != vam->json_tree.type) {
1074         vat_json_print(vam->ofp, &vam->json_tree);
1075         vat_json_free(&vam->json_tree);
1076         vam->json_tree.type = VAT_JSON_NONE;
1077     } else {
1078         /* just print [] */
1079         vat_json_init_array(&vam->json_tree);
1080         vat_json_print(vam->ofp, &vam->json_tree);
1081         vam->json_tree.type = VAT_JSON_NONE;
1082     }
1083
1084     vam->retval = retval;
1085     vam->result_ready = 1;
1086 }
1087
1088 static void vl_api_l2_flags_reply_t_handler
1089 (vl_api_l2_flags_reply_t * mp)
1090 {
1091     vat_main_t * vam = &vat_main;
1092     i32 retval = ntohl(mp->retval);
1093     if (vam->async_mode) {
1094         vam->async_errors += (retval < 0);
1095     } else {
1096         vam->retval = retval;
1097         vam->result_ready = 1;
1098     }
1099 }
1100
1101 static void vl_api_l2_flags_reply_t_handler_json
1102 (vl_api_l2_flags_reply_t * mp)
1103 {
1104     vat_main_t * vam = &vat_main;
1105     vat_json_node_t node;
1106
1107     vat_json_init_object(&node);
1108     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1109     vat_json_object_add_uint(&node, "resulting_feature_bitmap", ntohl(mp->resulting_feature_bitmap));
1110
1111     vat_json_print(vam->ofp, &node);
1112     vat_json_free(&node);
1113
1114     vam->retval = ntohl(mp->retval);
1115     vam->result_ready = 1;
1116 }
1117
1118 static void vl_api_bridge_flags_reply_t_handler
1119 (vl_api_bridge_flags_reply_t * mp)
1120 {
1121     vat_main_t * vam = &vat_main;
1122     i32 retval = ntohl(mp->retval);
1123     if (vam->async_mode) {
1124         vam->async_errors += (retval < 0);
1125     } else {
1126         vam->retval = retval;
1127         vam->result_ready = 1;
1128     }
1129 }
1130
1131 static void vl_api_bridge_flags_reply_t_handler_json
1132 (vl_api_bridge_flags_reply_t * mp)
1133 {
1134     vat_main_t * vam = &vat_main;
1135     vat_json_node_t node;
1136
1137     vat_json_init_object(&node);
1138     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1139     vat_json_object_add_uint(&node, "resulting_feature_bitmap", ntohl(mp->resulting_feature_bitmap));
1140
1141     vat_json_print(vam->ofp, &node);
1142     vat_json_free(&node);
1143
1144     vam->retval = ntohl(mp->retval);
1145     vam->result_ready = 1;
1146 }
1147
1148 static void vl_api_tap_connect_reply_t_handler
1149 (vl_api_tap_connect_reply_t * mp)
1150 {
1151     vat_main_t * vam = &vat_main;
1152     i32 retval = ntohl(mp->retval);
1153     if (vam->async_mode) {
1154         vam->async_errors += (retval < 0);
1155     } else {
1156         vam->retval = retval;
1157         vam->result_ready = 1;
1158     }
1159 }
1160
1161 static void vl_api_tap_connect_reply_t_handler_json
1162 (vl_api_tap_connect_reply_t * mp)
1163 {
1164     vat_main_t * vam = &vat_main;
1165     vat_json_node_t node;
1166
1167     vat_json_init_object(&node);
1168     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1169     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
1170
1171     vat_json_print(vam->ofp, &node);
1172     vat_json_free(&node);
1173
1174     vam->retval = ntohl(mp->retval);
1175     vam->result_ready = 1;
1176 }
1177
1178 static void vl_api_tap_modify_reply_t_handler
1179 (vl_api_tap_modify_reply_t * mp)
1180 {
1181     vat_main_t * vam = &vat_main;
1182     i32 retval = ntohl(mp->retval);
1183     if (vam->async_mode) {
1184         vam->async_errors += (retval < 0);
1185     } else {
1186         vam->retval = retval;
1187         vam->result_ready = 1;
1188     }
1189 }
1190
1191 static void vl_api_tap_modify_reply_t_handler_json
1192 (vl_api_tap_modify_reply_t * mp)
1193 {
1194     vat_main_t * vam = &vat_main;
1195     vat_json_node_t node;
1196
1197     vat_json_init_object(&node);
1198     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1199     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
1200
1201     vat_json_print(vam->ofp, &node);
1202     vat_json_free(&node);
1203
1204     vam->retval = ntohl(mp->retval);
1205     vam->result_ready = 1;
1206 }
1207
1208 static void vl_api_tap_delete_reply_t_handler
1209 (vl_api_tap_delete_reply_t * mp)
1210 {
1211     vat_main_t * vam = &vat_main;
1212     i32 retval = ntohl(mp->retval);
1213     if (vam->async_mode) {
1214         vam->async_errors += (retval < 0);
1215     } else {
1216         vam->retval = retval;
1217         vam->result_ready = 1;
1218     }
1219 }
1220
1221 static void vl_api_tap_delete_reply_t_handler_json
1222 (vl_api_tap_delete_reply_t * mp)
1223 {
1224     vat_main_t * vam = &vat_main;
1225     vat_json_node_t node;
1226
1227     vat_json_init_object(&node);
1228     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1229
1230     vat_json_print(vam->ofp, &node);
1231     vat_json_free(&node);
1232
1233     vam->retval = ntohl(mp->retval);
1234     vam->result_ready = 1;
1235 }
1236
1237 static void vl_api_mpls_ethernet_add_del_tunnel_reply_t_handler
1238 (vl_api_mpls_ethernet_add_del_tunnel_reply_t * mp)
1239 {
1240     vat_main_t * vam = &vat_main;
1241     i32 retval = ntohl(mp->retval);
1242     if (vam->async_mode) {
1243         vam->async_errors += (retval < 0);
1244     } else {
1245         vam->retval = retval;
1246         vam->result_ready = 1;
1247     }
1248 }
1249
1250 static void vl_api_mpls_ethernet_add_del_tunnel_reply_t_handler_json
1251 (vl_api_mpls_ethernet_add_del_tunnel_reply_t * mp)
1252 {
1253     vat_main_t * vam = &vat_main;
1254     vat_json_node_t node;
1255
1256     vat_json_init_object(&node);
1257     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1258     vat_json_object_add_uint(&node, "tunnel_sw_if_index", ntohl(mp->tunnel_sw_if_index));
1259
1260     vat_json_print(vam->ofp, &node);
1261     vat_json_free(&node);
1262
1263     vam->retval = ntohl(mp->retval);
1264     vam->result_ready = 1;
1265 }
1266
1267 static void vl_api_l2tpv3_create_tunnel_reply_t_handler
1268 (vl_api_l2tpv3_create_tunnel_reply_t * mp)
1269 {
1270     vat_main_t * vam = &vat_main;
1271     i32 retval = ntohl(mp->retval);
1272     if (vam->async_mode) {
1273         vam->async_errors += (retval < 0);
1274     } else {
1275         vam->retval = retval;
1276         vam->result_ready = 1;
1277     }
1278 }
1279
1280 static void vl_api_l2tpv3_create_tunnel_reply_t_handler_json
1281 (vl_api_l2tpv3_create_tunnel_reply_t * mp)
1282 {
1283     vat_main_t * vam = &vat_main;
1284     vat_json_node_t node;
1285
1286     vat_json_init_object(&node);
1287     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1288     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
1289
1290     vat_json_print(vam->ofp, &node);
1291     vat_json_free(&node);
1292
1293     vam->retval = ntohl(mp->retval);
1294     vam->result_ready = 1;
1295 }
1296
1297 static void vl_api_vxlan_add_del_tunnel_reply_t_handler
1298 (vl_api_vxlan_add_del_tunnel_reply_t * mp)
1299 {
1300     vat_main_t * vam = &vat_main;
1301     i32 retval = ntohl(mp->retval);
1302     if (vam->async_mode) {
1303         vam->async_errors += (retval < 0);
1304     } else {
1305         vam->retval = retval;
1306         vam->result_ready = 1;
1307     }
1308 }
1309
1310 static void vl_api_vxlan_add_del_tunnel_reply_t_handler_json
1311 (vl_api_vxlan_add_del_tunnel_reply_t * mp)
1312 {
1313     vat_main_t * vam = &vat_main;
1314     vat_json_node_t node;
1315
1316     vat_json_init_object(&node);
1317     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1318     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
1319
1320     vat_json_print(vam->ofp, &node);
1321     vat_json_free(&node);
1322
1323     vam->retval = ntohl(mp->retval);
1324     vam->result_ready = 1;
1325 }
1326
1327 static void vl_api_gre_add_del_tunnel_reply_t_handler
1328 (vl_api_gre_add_del_tunnel_reply_t * mp)
1329 {
1330     vat_main_t * vam = &vat_main;
1331     i32 retval = ntohl(mp->retval);
1332     if (vam->async_mode) {
1333         vam->async_errors += (retval < 0);
1334     } else {
1335         vam->retval = retval;
1336         vam->result_ready = 1;
1337     }
1338 }
1339
1340 static void vl_api_gre_add_del_tunnel_reply_t_handler_json
1341 (vl_api_gre_add_del_tunnel_reply_t * mp)
1342 {
1343     vat_main_t * vam = &vat_main;
1344     vat_json_node_t node;
1345
1346     vat_json_init_object(&node);
1347     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1348     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
1349
1350     vat_json_print(vam->ofp, &node);
1351     vat_json_free(&node);
1352
1353     vam->retval = ntohl(mp->retval);
1354     vam->result_ready = 1;
1355 }
1356
1357 static void vl_api_create_vhost_user_if_reply_t_handler
1358 (vl_api_create_vhost_user_if_reply_t * mp)
1359 {
1360     vat_main_t * vam = &vat_main;
1361     i32 retval = ntohl(mp->retval);
1362     if (vam->async_mode) {
1363         vam->async_errors += (retval < 0);
1364     } else {
1365         vam->retval = retval;
1366         vam->result_ready = 1;
1367     }
1368 }
1369
1370 static void vl_api_create_vhost_user_if_reply_t_handler_json
1371 (vl_api_create_vhost_user_if_reply_t * mp)
1372 {
1373     vat_main_t * vam = &vat_main;
1374     vat_json_node_t node;
1375
1376     vat_json_init_object(&node);
1377     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1378     vat_json_object_add_uint(&node, "sw_if_index", ntohl(mp->sw_if_index));
1379
1380     vat_json_print(vam->ofp, &node);
1381     vat_json_free(&node);
1382
1383     vam->retval = ntohl(mp->retval);
1384     vam->result_ready = 1;
1385 }
1386
1387 static void vl_api_ip_address_details_t_handler
1388 (vl_api_ip_address_details_t * mp)
1389 {
1390     vat_main_t * vam = &vat_main;
1391     static ip_address_details_t empty_ip_address_details = {{0}};
1392     ip_address_details_t * address = NULL;
1393     ip_details_t * current_ip_details = NULL;
1394     ip_details_t * details = NULL;
1395
1396     details = vam->ip_details_by_sw_if_index[vam->is_ipv6];
1397
1398     if (!details || vam->current_sw_if_index >= vec_len(details)
1399             || !details[vam->current_sw_if_index].present) {
1400         errmsg ("ip address details arrived but not stored\n");
1401         errmsg ("ip_dump should be called first\n");
1402         return;
1403     }
1404
1405     current_ip_details = vec_elt_at_index(details,
1406             vam->current_sw_if_index);
1407
1408 #define addresses (current_ip_details->addr)
1409
1410     vec_validate_init_empty(addresses, vec_len(addresses),
1411             empty_ip_address_details);
1412
1413     address = vec_elt_at_index(addresses, vec_len(addresses) - 1);
1414
1415     clib_memcpy(&address->ip, &mp->ip, sizeof(address->ip));
1416     address->prefix_length = mp->prefix_length;
1417 #undef addresses
1418 }
1419
1420 static void vl_api_ip_address_details_t_handler_json
1421 (vl_api_ip_address_details_t * mp)
1422 {
1423     vat_main_t * vam = &vat_main;
1424     vat_json_node_t *node = NULL;
1425     struct in6_addr ip6;
1426     struct in_addr ip4;
1427
1428     if (VAT_JSON_ARRAY != vam->json_tree.type) {
1429         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
1430         vat_json_init_array(&vam->json_tree);
1431     }
1432     node = vat_json_array_add(&vam->json_tree);
1433
1434     vat_json_init_object(node);
1435     if (vam->is_ipv6) {
1436         clib_memcpy(&ip6, mp->ip, sizeof(ip6));
1437         vat_json_object_add_ip6(node, "ip",  ip6);
1438     } else {
1439         clib_memcpy(&ip4, mp->ip, sizeof(ip4));
1440         vat_json_object_add_ip4(node, "ip", ip4);
1441     }
1442     vat_json_object_add_uint(node, "prefix_length", mp->prefix_length);
1443 }
1444
1445 static void vl_api_ip_details_t_handler (vl_api_ip_details_t * mp)
1446 {
1447     vat_main_t * vam = &vat_main;
1448     static ip_details_t empty_ip_details = {0};
1449     ip_details_t * ip = NULL;
1450     u32 sw_if_index = ~0;
1451
1452     sw_if_index = ntohl(mp->sw_if_index);
1453
1454     vec_validate_init_empty(vam->ip_details_by_sw_if_index[vam->is_ipv6],
1455             sw_if_index, empty_ip_details);
1456
1457     ip = vec_elt_at_index(vam->ip_details_by_sw_if_index[vam->is_ipv6],
1458             sw_if_index);
1459
1460     ip->present = 1;
1461 }
1462
1463 static void vl_api_ip_details_t_handler_json (vl_api_ip_details_t * mp)
1464 {
1465     vat_main_t * vam = &vat_main;
1466
1467     if (VAT_JSON_ARRAY != vam->json_tree.type) {
1468         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
1469         vat_json_init_array(&vam->json_tree);
1470     }
1471     vat_json_array_add_uint(&vam->json_tree, clib_net_to_host_u32(mp->sw_if_index));
1472 }
1473
1474 static void vl_api_map_domain_details_t_handler_json
1475 (vl_api_map_domain_details_t * mp)
1476 {
1477     vat_json_node_t * node = NULL;
1478     vat_main_t * vam = &vat_main;
1479     struct in6_addr ip6;
1480     struct in_addr ip4;
1481
1482     if (VAT_JSON_ARRAY != vam->json_tree.type) {
1483         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
1484         vat_json_init_array(&vam->json_tree);
1485     }
1486
1487     node = vat_json_array_add(&vam->json_tree);
1488     vat_json_init_object(node);
1489
1490     vat_json_object_add_uint(node, "domain_index", clib_net_to_host_u32(mp->domain_index));
1491     clib_memcpy(&ip6, mp->ip6_prefix, sizeof(ip6));
1492     vat_json_object_add_ip6(node, "ip6_prefix", ip6);
1493     clib_memcpy(&ip4, mp->ip4_prefix, sizeof(ip4));
1494     vat_json_object_add_ip4(node, "ip4_prefix", ip4);
1495     clib_memcpy(&ip6, mp->ip6_src, sizeof(ip6));
1496     vat_json_object_add_ip6(node, "ip6_src", ip6);
1497     vat_json_object_add_int(node, "ip6_prefix_len", mp->ip6_prefix_len);
1498     vat_json_object_add_int(node, "ip4_prefix_len", mp->ip4_prefix_len);
1499     vat_json_object_add_int(node, "ip6_src_len", mp->ip6_src_len);
1500     vat_json_object_add_int(node, "ea_bits_len", mp->ea_bits_len);
1501     vat_json_object_add_int(node, "psid_offset", mp->psid_offset);
1502     vat_json_object_add_int(node, "psid_length", mp->psid_length);
1503     vat_json_object_add_uint(node, "flags", mp->flags);
1504     vat_json_object_add_uint(node, "mtu", clib_net_to_host_u16(mp->mtu));
1505     vat_json_object_add_int(node, "is_translation", mp->is_translation);
1506 }
1507
1508 static void vl_api_map_domain_details_t_handler
1509 (vl_api_map_domain_details_t * mp)
1510 {
1511     vat_main_t * vam = &vat_main;
1512
1513     if (mp->is_translation) {
1514         fformat(vam->ofp,  "* %U/%d (ipv4-prefix) %U/%d (ipv6-prefix) %U/%d (ip6-src) index: %u\n",
1515                   format_ip4_address, mp->ip4_prefix, mp->ip4_prefix_len,
1516                   format_ip6_address, mp->ip6_prefix, mp->ip6_prefix_len,
1517                   format_ip6_address, mp->ip6_src, mp->ip6_src_len, clib_net_to_host_u32(mp->domain_index));
1518     } else {
1519         fformat(vam->ofp,  "* %U/%d (ipv4-prefix) %U/%d (ipv6-prefix) %U (ip6-src) index: %u\n",
1520                   format_ip4_address, mp->ip4_prefix, mp->ip4_prefix_len,
1521                   format_ip6_address, mp->ip6_prefix, mp->ip6_prefix_len,
1522                   format_ip6_address, mp->ip6_src, clib_net_to_host_u32(mp->domain_index));
1523     }
1524     fformat(vam->ofp, "  ea-len %d psid-offset %d psid-len %d mtu %d %s\n",
1525             mp->ea_bits_len, mp->psid_offset, mp->psid_length, mp->mtu, mp->is_translation? "map-t":"");
1526 }
1527
1528 static void vl_api_map_rule_details_t_handler_json
1529 (vl_api_map_rule_details_t * mp)
1530 {
1531     struct in6_addr ip6;
1532     vat_json_node_t * node = NULL;
1533     vat_main_t * vam = &vat_main;
1534
1535     if (VAT_JSON_ARRAY != vam->json_tree.type) {
1536         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
1537         vat_json_init_array(&vam->json_tree);
1538     }
1539
1540     node = vat_json_array_add(&vam->json_tree);
1541     vat_json_init_object(node);
1542
1543     vat_json_object_add_uint(node, "psid", clib_net_to_host_u16(mp->psid));
1544     clib_memcpy(&ip6, mp->ip6_dst, sizeof(ip6));
1545     vat_json_object_add_ip6(node, "ip6_dst", ip6);
1546 }
1547
1548 static void vl_api_map_rule_details_t_handler
1549 (vl_api_map_rule_details_t * mp)
1550 {
1551     vat_main_t * vam = &vat_main;
1552     fformat(vam->ofp, " %d (psid) %U (ip6-dst)\n", clib_net_to_host_u16(mp->psid),
1553             format_ip6_address, mp->ip6_dst);
1554 }
1555
1556 static void vl_api_dhcp_compl_event_t_handler
1557 (vl_api_dhcp_compl_event_t * mp)
1558 {
1559     vat_main_t * vam = &vat_main;
1560     errmsg ("DHCP compl event: pid %d %s hostname %s host_addr %U "
1561             "router_addr %U host_mac %U\n",
1562             mp->pid, mp->is_ipv6 ? "ipv6":"ipv4", mp->hostname,
1563             format_ip4_address, &mp->host_address,
1564             format_ip4_address, &mp->router_address,
1565             format_ethernet_address, mp->host_mac);
1566 }
1567
1568 static void vl_api_dhcp_compl_event_t_handler_json
1569 (vl_api_dhcp_compl_event_t * mp)
1570 {
1571     /* JSON output not supported */
1572 }
1573
1574 static void set_simple_interface_counter (u8 vnet_counter_type, u32 sw_if_index,
1575                                           u32 counter)
1576 {
1577     vat_main_t * vam = &vat_main;
1578     static u64 default_counter = 0;
1579
1580     vec_validate_init_empty(vam->simple_interface_counters, vnet_counter_type, NULL);
1581     vec_validate_init_empty(vam->simple_interface_counters[vnet_counter_type],
1582                             sw_if_index, default_counter);
1583     vam->simple_interface_counters[vnet_counter_type][sw_if_index] = counter;
1584 }
1585
1586 static void set_combined_interface_counter (u8 vnet_counter_type, u32 sw_if_index,
1587                                             interface_counter_t counter)
1588 {
1589     vat_main_t * vam = &vat_main;
1590     static interface_counter_t default_counter = {0, };
1591
1592     vec_validate_init_empty(vam->combined_interface_counters, vnet_counter_type, NULL);
1593     vec_validate_init_empty(vam->combined_interface_counters[vnet_counter_type],
1594                             sw_if_index, default_counter);
1595     vam->combined_interface_counters[vnet_counter_type][sw_if_index] = counter;
1596 }
1597
1598 static void vl_api_vnet_interface_counters_t_handler
1599 (vl_api_vnet_interface_counters_t *mp)
1600 {
1601     /* not supported */
1602 }
1603
1604 static void vl_api_vnet_interface_counters_t_handler_json
1605 (vl_api_vnet_interface_counters_t *mp)
1606 {
1607     interface_counter_t counter;
1608     vlib_counter_t *v;
1609     u64 *v_packets;
1610     u64 packets;
1611     u32 count;
1612     u32 first_sw_if_index;
1613     int i;
1614
1615     count = ntohl(mp->count);
1616     first_sw_if_index = ntohl(mp->first_sw_if_index);
1617
1618     if (!mp->is_combined) {
1619         v_packets = (u64*)&mp->data;
1620         for (i = 0; i < count; i++) {
1621             packets = clib_net_to_host_u64(clib_mem_unaligned(v_packets, u64));
1622             set_simple_interface_counter(mp->vnet_counter_type,
1623                     first_sw_if_index + i, packets);
1624             v_packets++;
1625         }
1626     } else {
1627         v = (vlib_counter_t*)&mp->data;
1628         for (i = 0; i < count; i++) {
1629             counter.packets = clib_net_to_host_u64(
1630                     clib_mem_unaligned(&v->packets, u64));
1631             counter.bytes = clib_net_to_host_u64(
1632                     clib_mem_unaligned(&v->bytes, u64));
1633             set_combined_interface_counter(mp->vnet_counter_type,
1634                     first_sw_if_index + i, counter);
1635             v++;
1636         }
1637     }
1638 }
1639
1640 static u32 ip4_fib_counters_get_vrf_index_by_vrf_id (u32 vrf_id)
1641 {
1642     vat_main_t * vam = &vat_main;
1643     u32 i;
1644
1645     for (i = 0; i < vec_len(vam->ip4_fib_counters_vrf_id_by_index); i++) {
1646         if (vam->ip4_fib_counters_vrf_id_by_index[i] == vrf_id) {
1647             return i;
1648         }
1649     }
1650     return ~0;
1651 }
1652
1653 static u32 ip6_fib_counters_get_vrf_index_by_vrf_id (u32 vrf_id)
1654 {
1655     vat_main_t * vam = &vat_main;
1656     u32 i;
1657
1658     for (i = 0; i < vec_len(vam->ip6_fib_counters_vrf_id_by_index); i++) {
1659         if (vam->ip6_fib_counters_vrf_id_by_index[i] == vrf_id) {
1660             return i;
1661         }
1662     }
1663     return ~0;
1664 }
1665
1666 static void vl_api_vnet_ip4_fib_counters_t_handler
1667 (vl_api_vnet_ip4_fib_counters_t *mp)
1668 {
1669     /* not supported */
1670 }
1671
1672 static void vl_api_vnet_ip4_fib_counters_t_handler_json
1673 (vl_api_vnet_ip4_fib_counters_t *mp)
1674 {
1675     vat_main_t * vam = &vat_main;
1676     vl_api_ip4_fib_counter_t *v;
1677     ip4_fib_counter_t *counter;
1678     struct in_addr ip4;
1679     u32 vrf_id;
1680     u32 vrf_index;
1681     u32 count;
1682     int i;
1683
1684     vrf_id = ntohl(mp->vrf_id);
1685     vrf_index = ip4_fib_counters_get_vrf_index_by_vrf_id(vrf_id);
1686     if (~0 == vrf_index) {
1687         vrf_index = vec_len(vam->ip4_fib_counters_vrf_id_by_index);
1688         vec_validate(vam->ip4_fib_counters_vrf_id_by_index, vrf_index);
1689         vam->ip4_fib_counters_vrf_id_by_index[vrf_index] = vrf_id;
1690         vec_validate(vam->ip4_fib_counters, vrf_index);
1691         vam->ip4_fib_counters[vrf_index] = NULL;
1692     }
1693
1694     vec_free(vam->ip4_fib_counters[vrf_index]);
1695     v = (vl_api_ip4_fib_counter_t*)&mp->c;
1696     count = ntohl(mp->count);
1697     for (i = 0; i < count; i++) {
1698         vec_validate(vam->ip4_fib_counters[vrf_index], i);
1699         counter = &vam->ip4_fib_counters[vrf_index][i];
1700         clib_memcpy(&ip4, &v->address, sizeof(ip4));
1701         counter->address = ip4;
1702         counter->address_length = v->address_length;
1703         counter->packets = clib_net_to_host_u64(v->packets);
1704         counter->bytes = clib_net_to_host_u64(v->bytes);
1705         v++;
1706     }
1707 }
1708
1709 static void vl_api_vnet_ip6_fib_counters_t_handler
1710 (vl_api_vnet_ip6_fib_counters_t *mp)
1711 {
1712     /* not supported */
1713 }
1714
1715 static void vl_api_vnet_ip6_fib_counters_t_handler_json
1716 (vl_api_vnet_ip6_fib_counters_t *mp)
1717 {
1718     vat_main_t * vam = &vat_main;
1719     vl_api_ip6_fib_counter_t *v;
1720     ip6_fib_counter_t *counter;
1721     struct in6_addr ip6;
1722     u32 vrf_id;
1723     u32 vrf_index;
1724     u32 count;
1725     int i;
1726
1727     vrf_id = ntohl(mp->vrf_id);
1728     vrf_index = ip6_fib_counters_get_vrf_index_by_vrf_id(vrf_id);
1729     if (~0 == vrf_index) {
1730         vrf_index = vec_len(vam->ip6_fib_counters_vrf_id_by_index);
1731         vec_validate(vam->ip6_fib_counters_vrf_id_by_index, vrf_index);
1732         vam->ip6_fib_counters_vrf_id_by_index[vrf_index] = vrf_id;
1733         vec_validate(vam->ip6_fib_counters, vrf_index);
1734         vam->ip6_fib_counters[vrf_index] = NULL;
1735     }
1736
1737     vec_free(vam->ip6_fib_counters[vrf_index]);
1738     v = (vl_api_ip6_fib_counter_t*)&mp->c;
1739     count = ntohl(mp->count);
1740     for (i = 0; i < count; i++) {
1741         vec_validate(vam->ip6_fib_counters[vrf_index], i);
1742         counter = &vam->ip6_fib_counters[vrf_index][i];
1743         clib_memcpy(&ip6, &v->address, sizeof(ip6));
1744         counter->address = ip6;
1745         counter->address_length = v->address_length;
1746         counter->packets = clib_net_to_host_u64(v->packets);
1747         counter->bytes = clib_net_to_host_u64(v->bytes);
1748         v++;
1749     }
1750 }
1751
1752 static void vl_api_get_first_msg_id_reply_t_handler
1753 (vl_api_get_first_msg_id_reply_t * mp)
1754 {
1755     vat_main_t * vam = &vat_main;
1756     i32 retval = ntohl(mp->retval);
1757     
1758     if (vam->async_mode) {
1759         vam->async_errors += (retval < 0);
1760     } else {
1761         vam->retval = retval;
1762         vam->result_ready = 1;
1763     }
1764     if (retval >= 0) {
1765         errmsg ("first message id %d\n", ntohs(mp->first_msg_id));
1766     }
1767 }
1768
1769 static void vl_api_get_first_msg_id_reply_t_handler_json
1770 (vl_api_get_first_msg_id_reply_t * mp)
1771 {
1772     vat_main_t * vam = &vat_main;
1773     vat_json_node_t node;
1774
1775     vat_json_init_object(&node);
1776     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1777     vat_json_object_add_uint(&node, "first_msg_id", 
1778                              (uint) ntohs(mp->first_msg_id));
1779
1780     vat_json_print(vam->ofp, &node);
1781     vat_json_free(&node);
1782
1783     vam->retval = ntohl(mp->retval);
1784     vam->result_ready = 1;
1785 }
1786
1787 static void vl_api_get_node_graph_reply_t_handler
1788 (vl_api_get_node_graph_reply_t * mp)
1789 {
1790     vat_main_t * vam = &vat_main;
1791     api_main_t * am = &api_main;
1792     i32 retval = ntohl(mp->retval);
1793     u8 * pvt_copy, * reply;
1794     void * oldheap;
1795     vlib_node_t * node;
1796     int i;
1797     
1798     if (vam->async_mode) {
1799         vam->async_errors += (retval < 0);
1800     } else {
1801         vam->retval = retval;
1802         vam->result_ready = 1;
1803     }
1804
1805     /* "Should never happen..." */
1806     if (retval != 0)
1807         return;
1808
1809     reply = (u8 *)(mp->reply_in_shmem);
1810     pvt_copy = vec_dup (reply);
1811
1812     /* Toss the shared-memory original... */
1813     pthread_mutex_lock (&am->vlib_rp->mutex);
1814     oldheap = svm_push_data_heap (am->vlib_rp);
1815
1816     vec_free (reply);
1817     
1818     svm_pop_heap (oldheap);
1819     pthread_mutex_unlock (&am->vlib_rp->mutex);
1820
1821     if (vam->graph_nodes) {
1822         hash_free (vam->graph_node_index_by_name);
1823
1824         for (i = 0; i < vec_len (vam->graph_nodes); i++) {
1825             node = vam->graph_nodes[i];
1826             vec_free (node->name);
1827             vec_free (node->next_nodes);
1828             vec_free (node);
1829         }
1830         vec_free(vam->graph_nodes);
1831     }
1832
1833     vam->graph_node_index_by_name = hash_create_string (0, sizeof(uword));
1834     vam->graph_nodes = vlib_node_unserialize (pvt_copy);
1835     vec_free (pvt_copy);
1836
1837     for (i = 0; i < vec_len (vam->graph_nodes); i++) {
1838         node = vam->graph_nodes[i];
1839         hash_set_mem (vam->graph_node_index_by_name, node->name, i);
1840     }
1841 }
1842
1843 static void vl_api_get_node_graph_reply_t_handler_json
1844 (vl_api_get_node_graph_reply_t * mp)
1845 {
1846     vat_main_t * vam = &vat_main;
1847     api_main_t * am = &api_main;
1848     void * oldheap;
1849     vat_json_node_t node;
1850     u8 * reply;
1851
1852     /* $$$$ make this real? */
1853     vat_json_init_object(&node);
1854     vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
1855     vat_json_object_add_uint(&node, "reply_in_shmem", mp->reply_in_shmem);
1856
1857     reply = (u8 *)(mp->reply_in_shmem);
1858
1859     /* Toss the shared-memory original... */
1860     pthread_mutex_lock (&am->vlib_rp->mutex);
1861     oldheap = svm_push_data_heap (am->vlib_rp);
1862
1863     vec_free (reply);
1864     
1865     svm_pop_heap (oldheap);
1866     pthread_mutex_unlock (&am->vlib_rp->mutex);
1867
1868     vat_json_print(vam->ofp, &node);
1869     vat_json_free(&node);
1870
1871     vam->retval = ntohl(mp->retval);
1872     vam->result_ready = 1;
1873 }
1874
1875 static void
1876 vl_api_lisp_locator_set_details_t_handler (
1877     vl_api_lisp_locator_set_details_t *mp)
1878 {
1879     vat_main_t *vam = &vat_main;
1880
1881     fformat(vam->ofp, "%=20s%=16d%=16d%=16d\n",
1882             mp->locator_set_name,
1883             ntohl(mp->sw_if_index),
1884             mp->priority,
1885             mp->weight);
1886 }
1887
1888 static void
1889 vl_api_lisp_locator_set_details_t_handler_json (
1890     vl_api_lisp_locator_set_details_t *mp)
1891 {
1892     vat_main_t *vam = &vat_main;
1893     vat_json_node_t *node = NULL;
1894
1895     if (VAT_JSON_ARRAY != vam->json_tree.type) {
1896         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
1897         vat_json_init_array(&vam->json_tree);
1898     }
1899     node = vat_json_array_add(&vam->json_tree);
1900
1901     vat_json_init_object(node);
1902     vat_json_object_add_string_copy(node, "locator-set", mp->locator_set_name);
1903     vat_json_object_add_uint(node, "locator", ntohl(mp->sw_if_index));
1904     vat_json_object_add_uint(node, "priority", mp->priority);
1905     vat_json_object_add_uint(node, "weight", mp->weight);
1906 }
1907
1908 static void
1909 vl_api_lisp_local_eid_table_details_t_handler (
1910     vl_api_lisp_local_eid_table_details_t *mp)
1911 {
1912     vat_main_t *vam = &vat_main;
1913     u8 *prefix;
1914
1915     prefix = format(0, "%U/%d",
1916                     mp->eid_is_ipv6 ? format_ip6_address : format_ip4_address,
1917                     mp->eid_ip_address,
1918                     mp->eid_prefix_len);
1919
1920     fformat(vam->ofp, "%=20s%=30s\n",
1921             mp->locator_set_name, prefix);
1922
1923     vec_free(prefix);
1924 }
1925
1926 static void
1927 vl_api_lisp_local_eid_table_details_t_handler_json (
1928     vl_api_lisp_local_eid_table_details_t *mp)
1929 {
1930     vat_main_t *vam = &vat_main;
1931     vat_json_node_t *node = NULL;
1932     struct in6_addr ip6;
1933     struct in_addr ip4;
1934
1935     if (VAT_JSON_ARRAY != vam->json_tree.type) {
1936         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
1937         vat_json_init_array(&vam->json_tree);
1938     }
1939     node = vat_json_array_add(&vam->json_tree);
1940
1941     vat_json_init_object(node);
1942     vat_json_object_add_string_copy(node, "locator-set", mp->locator_set_name);
1943     if (mp->eid_is_ipv6) {
1944         clib_memcpy(&ip6, mp->eid_ip_address, sizeof(ip6));
1945         vat_json_object_add_ip6(node, "eid address", ip6);
1946     } else {
1947         clib_memcpy(&ip4, mp->eid_ip_address, sizeof(ip4));
1948         vat_json_object_add_ip4(node, "eid address", ip4);
1949     }
1950     vat_json_object_add_uint(node, "eid prefix len", mp->eid_prefix_len);
1951 }
1952
1953 static u8 *
1954 format_decap_next (u8 * s, va_list * args)
1955 {
1956   u32 next_index = va_arg (*args, u32);
1957
1958   switch (next_index)
1959     {
1960     case LISP_GPE_INPUT_NEXT_DROP:
1961       return format (s, "drop");
1962     case LISP_GPE_INPUT_NEXT_IP4_INPUT:
1963       return format (s, "ip4");
1964     case LISP_GPE_INPUT_NEXT_IP6_INPUT:
1965       return format (s, "ip6");
1966     default:
1967       return format (s, "unknown %d", next_index);
1968     }
1969   return s;
1970 }
1971
1972 static void
1973 vl_api_lisp_gpe_tunnel_details_t_handler (vl_api_lisp_gpe_tunnel_details_t *mp)
1974 {
1975     vat_main_t *vam = &vat_main;
1976     u8 *iid_str;
1977     u8 *flag_str = NULL;
1978
1979     iid_str = format(0, "%d (0x%x)", ntohl(mp->iid), ntohl(mp->iid));
1980
1981 #define _(n,v) if (mp->flags & v) flag_str = format (flag_str, "%s-bit ", #n);
1982   foreach_lisp_gpe_flag_bit;
1983 #undef _
1984
1985     fformat(vam->ofp, "%=20d%=30U%=16U%=16d%=16d%=16U"
1986             "%=16d%=16d%=16sd=16d%=16s%=16s\n",
1987             mp->tunnels,
1988             mp->is_ipv6 ? format_ip6_address : format_ip4_address,
1989             mp->source_ip,
1990             mp->is_ipv6 ? format_ip6_address : format_ip4_address,
1991             mp->destination_ip,
1992             ntohl(mp->encap_fib_id),
1993             ntohl(mp->decap_fib_id),
1994             format_decap_next, ntohl(mp->dcap_next),
1995             mp->ver_res >> 6,
1996             flag_str,
1997             mp->next_protocol,
1998             mp->ver_res,
1999             mp->res,
2000             iid_str);
2001
2002     vec_free(iid_str);
2003 }
2004
2005 static void
2006 vl_api_lisp_gpe_tunnel_details_t_handler_json (
2007     vl_api_lisp_gpe_tunnel_details_t *mp)
2008 {
2009     vat_main_t *vam = &vat_main;
2010     vat_json_node_t *node = NULL;
2011     struct in6_addr ip6;
2012     struct in_addr ip4;
2013     u8 *next_decap_str;
2014
2015     next_decap_str = format(0, "%U", format_decap_next, htonl(mp->dcap_next));
2016
2017     if (VAT_JSON_ARRAY != vam->json_tree.type) {
2018         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
2019         vat_json_init_array(&vam->json_tree);
2020     }
2021     node = vat_json_array_add(&vam->json_tree);
2022
2023     vat_json_init_object(node);
2024     vat_json_object_add_uint(node, "tunel", mp->tunnels);
2025     if (mp->is_ipv6) {
2026         clib_memcpy(&ip6, mp->source_ip, sizeof(ip6));
2027         vat_json_object_add_ip6(node, "source address", ip6);
2028         clib_memcpy(&ip6, mp->destination_ip, sizeof(ip6));
2029         vat_json_object_add_ip6(node, "destination address", ip6);
2030     } else {
2031         clib_memcpy(&ip4, mp->source_ip, sizeof(ip4));
2032         vat_json_object_add_ip4(node, "source address", ip4);
2033         clib_memcpy(&ip4, mp->destination_ip, sizeof(ip4));
2034         vat_json_object_add_ip4(node, "destination address", ip4);
2035     }
2036     vat_json_object_add_uint(node, "fib encap", ntohl(mp->encap_fib_id));
2037     vat_json_object_add_uint(node, "fib decap", ntohl(mp->decap_fib_id));
2038     vat_json_object_add_string_copy(node, "decap next", next_decap_str);
2039     vat_json_object_add_uint(node, "lisp version", mp->ver_res >> 6);
2040     vat_json_object_add_uint(node, "flags", mp->flags);
2041     vat_json_object_add_uint(node, "next protocol", mp->next_protocol);
2042     vat_json_object_add_uint(node, "ver_res", mp->ver_res);
2043     vat_json_object_add_uint(node, "res", mp->res);
2044     vat_json_object_add_uint(node, "iid", ntohl(mp->iid));
2045
2046     vec_free(next_decap_str);
2047 }
2048
2049 static void
2050 vl_api_lisp_map_resolver_details_t_handler (
2051     vl_api_lisp_map_resolver_details_t *mp)
2052 {
2053     vat_main_t *vam = &vat_main;
2054
2055     fformat(vam->ofp, "%=20U\n",
2056             mp->is_ipv6 ? format_ip6_address : format_ip4_address,
2057             mp->ip_address);
2058 }
2059
2060 static void
2061 vl_api_lisp_map_resolver_details_t_handler_json (
2062     vl_api_lisp_map_resolver_details_t *mp)
2063 {
2064     vat_main_t *vam = &vat_main;
2065     vat_json_node_t *node = NULL;
2066     struct in6_addr ip6;
2067     struct in_addr ip4;
2068
2069     if (VAT_JSON_ARRAY != vam->json_tree.type) {
2070         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
2071         vat_json_init_array(&vam->json_tree);
2072     }
2073     node = vat_json_array_add(&vam->json_tree);
2074
2075     vat_json_init_object(node);
2076     if (mp->is_ipv6) {
2077         clib_memcpy(&ip6, mp->ip_address, sizeof(ip6));
2078         vat_json_object_add_ip6(node, "map resolver", ip6);
2079     } else {
2080         clib_memcpy(&ip4, mp->ip_address, sizeof(ip4));
2081         vat_json_object_add_ip4(node, "map resolver", ip4);
2082     }
2083 }
2084
2085 static void
2086 vl_api_lisp_enable_disable_status_details_t_handler
2087 (vl_api_lisp_enable_disable_status_details_t *mp)
2088 {
2089     vat_main_t *vam = &vat_main;
2090
2091     fformat(vam->ofp, "feature: %s\ngpe: %s\n",
2092             mp->feature_status ? "enabled" : "disabled",
2093             mp->gpe_status ? "enabled" : "disabled");
2094 }
2095
2096 static void
2097 vl_api_lisp_enable_disable_status_details_t_handler_json
2098 (vl_api_lisp_enable_disable_status_details_t *mp)
2099 {
2100     vat_main_t *vam = &vat_main;
2101     vat_json_node_t *node = NULL;
2102     u8 * gpe_status = NULL;
2103     u8 * feature_status = NULL;
2104
2105     gpe_status = format (0, "%s", mp->gpe_status ? "enabled" : "disabled");
2106     feature_status = format (0, "%s",
2107                             mp->feature_status ? "enabled" : "disabled");
2108
2109     if (VAT_JSON_ARRAY != vam->json_tree.type) {
2110         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
2111         vat_json_init_array(&vam->json_tree);
2112     }
2113     node = vat_json_array_add(&vam->json_tree);
2114
2115     vat_json_init_object(node);
2116     vat_json_object_add_string_copy(node, "gpe_status", gpe_status);
2117     vat_json_object_add_string_copy(node, "feature_status", feature_status);
2118
2119     vec_free (gpe_status);
2120     vec_free (feature_status);
2121 }
2122
2123 #define vl_api_vnet_ip4_fib_counters_t_endian vl_noop_handler
2124 #define vl_api_vnet_ip4_fib_counters_t_print vl_noop_handler
2125 #define vl_api_vnet_ip6_fib_counters_t_endian vl_noop_handler
2126 #define vl_api_vnet_ip6_fib_counters_t_print vl_noop_handler
2127
2128 /* 
2129  * Generate boilerplate reply handlers, which 
2130  * dig the return value out of the xxx_reply_t API message,
2131  * stick it into vam->retval, and set vam->result_ready
2132  *
2133  * Could also do this by pointing N message decode slots at
2134  * a single function, but that could break in subtle ways.
2135  */
2136
2137 #define foreach_standard_reply_retval_handler           \
2138 _(sw_interface_set_flags_reply)                         \
2139 _(sw_interface_add_del_address_reply)                   \
2140 _(sw_interface_set_table_reply)                         \
2141 _(sw_interface_set_vpath_reply)                         \
2142 _(sw_interface_set_l2_bridge_reply)                     \
2143 _(bridge_domain_add_del_reply)                          \
2144 _(sw_interface_set_l2_xconnect_reply)                   \
2145 _(l2fib_add_del_reply)                                  \
2146 _(ip_add_del_route_reply)                               \
2147 _(proxy_arp_add_del_reply)                              \
2148 _(proxy_arp_intfc_enable_disable_reply)                 \
2149 _(mpls_add_del_encap_reply)                             \
2150 _(mpls_add_del_decap_reply)                             \
2151 _(mpls_ethernet_add_del_tunnel_2_reply)                 \
2152 _(sw_interface_set_unnumbered_reply)                    \
2153 _(ip_neighbor_add_del_reply)                            \
2154 _(reset_vrf_reply)                                      \
2155 _(oam_add_del_reply)                                    \
2156 _(reset_fib_reply)                                      \
2157 _(dhcp_proxy_config_reply)                              \
2158 _(dhcp_proxy_config_2_reply)                            \
2159 _(dhcp_proxy_set_vss_reply)                             \
2160 _(dhcp_client_config_reply)                             \
2161 _(set_ip_flow_hash_reply)                               \
2162 _(sw_interface_ip6_enable_disable_reply)                \
2163 _(sw_interface_ip6_set_link_local_address_reply)        \
2164 _(sw_interface_ip6nd_ra_prefix_reply)                   \
2165 _(sw_interface_ip6nd_ra_config_reply)                   \
2166 _(set_arp_neighbor_limit_reply)                         \
2167 _(l2_patch_add_del_reply)                               \
2168 _(sr_tunnel_add_del_reply)                              \
2169 _(sr_policy_add_del_reply)                              \
2170 _(sr_multicast_map_add_del_reply)                              \
2171 _(classify_add_del_session_reply)                       \
2172 _(classify_set_interface_ip_table_reply)                \
2173 _(classify_set_interface_l2_tables_reply)               \
2174 _(l2tpv3_set_tunnel_cookies_reply)                      \
2175 _(l2tpv3_interface_enable_disable_reply)                \
2176 _(l2tpv3_set_lookup_key_reply)                          \
2177 _(l2_fib_clear_table_reply)                             \
2178 _(l2_interface_efp_filter_reply)                        \
2179 _(l2_interface_vlan_tag_rewrite_reply)                  \
2180 _(modify_vhost_user_if_reply)                           \
2181 _(delete_vhost_user_if_reply)                           \
2182 _(want_ip4_arp_events_reply)                            \
2183 _(input_acl_set_interface_reply)                        \
2184 _(ipsec_spd_add_del_reply)                              \
2185 _(ipsec_interface_add_del_spd_reply)                    \
2186 _(ipsec_spd_add_del_entry_reply)                        \
2187 _(ipsec_sad_add_del_entry_reply)                        \
2188 _(ipsec_sa_set_key_reply)                               \
2189 _(ikev2_profile_add_del_reply)                          \
2190 _(ikev2_profile_set_auth_reply)                         \
2191 _(ikev2_profile_set_id_reply)                           \
2192 _(ikev2_profile_set_ts_reply)                           \
2193 _(ikev2_set_local_key_reply)                            \
2194 _(delete_loopback_reply)                                \
2195 _(bd_ip_mac_add_del_reply)                              \
2196 _(map_del_domain_reply)                                 \
2197 _(map_add_del_rule_reply)                               \
2198 _(want_interface_events_reply)                          \
2199 _(want_stats_reply)                                     \
2200 _(cop_interface_enable_disable_reply)                   \
2201 _(cop_whitelist_enable_disable_reply)                   \
2202 _(sw_interface_clear_stats_reply)                       \
2203 _(trace_profile_add_reply)                              \
2204 _(trace_profile_apply_reply)                            \
2205 _(trace_profile_del_reply)                              \
2206 _(lisp_add_del_locator_set_reply)                       \
2207 _(lisp_add_del_locator_reply)                           \
2208 _(lisp_add_del_local_eid_reply)                         \
2209 _(lisp_gpe_add_del_fwd_entry_reply)                     \
2210 _(lisp_add_del_map_resolver_reply)                      \
2211 _(lisp_gpe_enable_disable_reply)                        \
2212 _(lisp_gpe_add_del_iface_reply)                         \
2213 _(lisp_enable_disable_reply)                            \
2214 _(af_packet_create_reply)                               \
2215 _(af_packet_delete_reply)
2216
2217 #define _(n)                                    \
2218     static void vl_api_##n##_t_handler          \
2219     (vl_api_##n##_t * mp)                       \
2220     {                                           \
2221         vat_main_t * vam = &vat_main;           \
2222         i32 retval = ntohl(mp->retval);         \
2223         if (vam->async_mode) {                  \
2224             vam->async_errors += (retval < 0);  \
2225         } else {                                \
2226             vam->retval = retval;               \
2227             vam->result_ready = 1;              \
2228         }                                       \
2229     }
2230 foreach_standard_reply_retval_handler;
2231 #undef _
2232
2233 #define _(n)                                    \
2234     static void vl_api_##n##_t_handler_json     \
2235     (vl_api_##n##_t * mp)                       \
2236     {                                           \
2237         vat_main_t * vam = &vat_main;           \
2238         vat_json_node_t node;                   \
2239         vat_json_init_object(&node);            \
2240         vat_json_object_add_int(&node, "retval", ntohl(mp->retval));    \
2241         vat_json_print(vam->ofp, &node);        \
2242         vam->retval = ntohl(mp->retval);        \
2243         vam->result_ready = 1;                  \
2244     }
2245 foreach_standard_reply_retval_handler;
2246 #undef _
2247
2248 /* 
2249  * Table of message reply handlers, must include boilerplate handlers
2250  * we just generated
2251  */
2252
2253 #define foreach_vpe_api_reply_msg                                       \
2254 _(CREATE_LOOPBACK_REPLY, create_loopback_reply)                         \
2255 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
2256 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
2257 _(SW_INTERFACE_SET_FLAGS_REPLY, sw_interface_set_flags_reply)           \
2258 _(CONTROL_PING_REPLY, control_ping_reply)                               \
2259 _(CLI_REPLY, cli_reply)                                                 \
2260 _(SW_INTERFACE_ADD_DEL_ADDRESS_REPLY,                                   \
2261   sw_interface_add_del_address_reply)                                   \
2262 _(SW_INTERFACE_SET_TABLE_REPLY, sw_interface_set_table_reply)           \
2263 _(SW_INTERFACE_SET_VPATH_REPLY, sw_interface_set_vpath_reply)           \
2264 _(SW_INTERFACE_SET_L2_XCONNECT_REPLY,                                   \
2265   sw_interface_set_l2_xconnect_reply)                                   \
2266 _(SW_INTERFACE_SET_L2_BRIDGE_REPLY,                                     \
2267   sw_interface_set_l2_bridge_reply)                                     \
2268 _(BRIDGE_DOMAIN_ADD_DEL_REPLY, bridge_domain_add_del_reply)             \
2269 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
2270 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
2271 _(L2FIB_ADD_DEL_REPLY, l2fib_add_del_reply)                             \
2272 _(L2_FLAGS_REPLY, l2_flags_reply)                                       \
2273 _(BRIDGE_FLAGS_REPLY, bridge_flags_reply)                               \
2274 _(TAP_CONNECT_REPLY, tap_connect_reply)                                 \
2275 _(TAP_MODIFY_REPLY, tap_modify_reply)                                   \
2276 _(TAP_DELETE_REPLY, tap_delete_reply)                                   \
2277 _(SW_INTERFACE_TAP_DETAILS, sw_interface_tap_details)                   \
2278 _(IP_ADD_DEL_ROUTE_REPLY, ip_add_del_route_reply)                       \
2279 _(PROXY_ARP_ADD_DEL_REPLY, proxy_arp_add_del_reply)                     \
2280 _(PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY,                                 \
2281   proxy_arp_intfc_enable_disable_reply)                                 \
2282 _(MPLS_ADD_DEL_ENCAP_REPLY, mpls_add_del_encap_reply)                   \
2283 _(MPLS_ADD_DEL_DECAP_REPLY, mpls_add_del_decap_reply)                   \
2284 _(MPLS_GRE_ADD_DEL_TUNNEL_REPLY, mpls_gre_add_del_tunnel_reply)         \
2285 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,                                   \
2286   mpls_ethernet_add_del_tunnel_reply)                                   \
2287 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY,                                 \
2288   mpls_ethernet_add_del_tunnel_2_reply)                                 \
2289 _(SW_INTERFACE_SET_UNNUMBERED_REPLY,                                    \
2290   sw_interface_set_unnumbered_reply)                                    \
2291 _(IP_NEIGHBOR_ADD_DEL_REPLY, ip_neighbor_add_del_reply)                 \
2292 _(RESET_VRF_REPLY, reset_vrf_reply)                                     \
2293 _(CREATE_VLAN_SUBIF_REPLY, create_vlan_subif_reply)                     \
2294 _(CREATE_SUBIF_REPLY, create_subif_reply)                               \
2295 _(OAM_ADD_DEL_REPLY, oam_add_del_reply)                                 \
2296 _(RESET_FIB_REPLY, reset_fib_reply)                                     \
2297 _(DHCP_PROXY_CONFIG_REPLY, dhcp_proxy_config_reply)                     \
2298 _(DHCP_PROXY_CONFIG_2_REPLY, dhcp_proxy_config_2_reply)                 \
2299 _(DHCP_PROXY_SET_VSS_REPLY, dhcp_proxy_set_vss_reply)                   \
2300 _(DHCP_CLIENT_CONFIG_REPLY, dhcp_client_config_reply)                   \
2301 _(SET_IP_FLOW_HASH_REPLY, set_ip_flow_hash_reply)                       \
2302 _(SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY,                                \
2303   sw_interface_ip6_enable_disable_reply)                                \
2304 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY,                        \
2305   sw_interface_ip6_set_link_local_address_reply)                        \
2306 _(SW_INTERFACE_IP6ND_RA_PREFIX_REPLY,                                   \
2307   sw_interface_ip6nd_ra_prefix_reply)                                   \
2308 _(SW_INTERFACE_IP6ND_RA_CONFIG_REPLY,                                   \
2309   sw_interface_ip6nd_ra_config_reply)                                   \
2310 _(SET_ARP_NEIGHBOR_LIMIT_REPLY, set_arp_neighbor_limit_reply)           \
2311 _(L2_PATCH_ADD_DEL_REPLY, l2_patch_add_del_reply)                       \
2312 _(SR_TUNNEL_ADD_DEL_REPLY, sr_tunnel_add_del_reply)                     \
2313 _(SR_POLICY_ADD_DEL_REPLY, sr_policy_add_del_reply)                     \
2314 _(SR_MULTICAST_MAP_ADD_DEL_REPLY, sr_multicast_map_add_del_reply)                     \
2315 _(CLASSIFY_ADD_DEL_TABLE_REPLY, classify_add_del_table_reply)           \
2316 _(CLASSIFY_ADD_DEL_SESSION_REPLY, classify_add_del_session_reply)       \
2317 _(CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY,                                \
2318 classify_set_interface_ip_table_reply)                                  \
2319 _(CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY,                               \
2320   classify_set_interface_l2_tables_reply)                               \
2321 _(GET_NODE_INDEX_REPLY, get_node_index_reply)                           \
2322 _(ADD_NODE_NEXT_REPLY, add_node_next_reply)                             \
2323 _(L2TPV3_CREATE_TUNNEL_REPLY, l2tpv3_create_tunnel_reply)               \
2324 _(L2TPV3_SET_TUNNEL_COOKIES_REPLY, l2tpv3_set_tunnel_cookies_reply)     \
2325 _(L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY,                                \
2326   l2tpv3_interface_enable_disable_reply)                                \
2327 _(L2TPV3_SET_LOOKUP_KEY_REPLY, l2tpv3_set_lookup_key_reply)             \
2328 _(SW_IF_L2TPV3_TUNNEL_DETAILS, sw_if_l2tpv3_tunnel_details)             \
2329 _(VXLAN_ADD_DEL_TUNNEL_REPLY, vxlan_add_del_tunnel_reply)               \
2330 _(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details)                           \
2331 _(GRE_ADD_DEL_TUNNEL_REPLY, gre_add_del_tunnel_reply)                   \
2332 _(GRE_TUNNEL_DETAILS, gre_tunnel_details)                               \
2333 _(L2_FIB_CLEAR_TABLE_REPLY, l2_fib_clear_table_reply)                   \
2334 _(L2_INTERFACE_EFP_FILTER_REPLY, l2_interface_efp_filter_reply)         \
2335 _(L2_INTERFACE_VLAN_TAG_REWRITE_REPLY, l2_interface_vlan_tag_rewrite_reply) \
2336 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
2337 _(CREATE_VHOST_USER_IF_REPLY, create_vhost_user_if_reply)               \
2338 _(MODIFY_VHOST_USER_IF_REPLY, modify_vhost_user_if_reply)               \
2339 _(DELETE_VHOST_USER_IF_REPLY, delete_vhost_user_if_reply)               \
2340 _(SHOW_VERSION_REPLY, show_version_reply)                               \
2341 _(NSH_GRE_ADD_DEL_TUNNEL_REPLY, nsh_gre_add_del_tunnel_reply)           \
2342 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
2343 _(NSH_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY, nsh_vxlan_gpe_add_del_tunnel_reply) \
2344 _(INTERFACE_NAME_RENUMBER_REPLY, interface_name_renumber_reply)         \
2345 _(WANT_IP4_ARP_EVENTS_REPLY, want_ip4_arp_events_reply)                 \
2346 _(IP4_ARP_EVENT, ip4_arp_event)                                         \
2347 _(INPUT_ACL_SET_INTERFACE_REPLY, input_acl_set_interface_reply)         \
2348 _(IP_ADDRESS_DETAILS, ip_address_details)                               \
2349 _(IP_DETAILS, ip_details)                                               \
2350 _(IPSEC_SPD_ADD_DEL_REPLY, ipsec_spd_add_del_reply)                     \
2351 _(IPSEC_INTERFACE_ADD_DEL_SPD_REPLY, ipsec_interface_add_del_spd_reply) \
2352 _(IPSEC_SPD_ADD_DEL_ENTRY_REPLY, ipsec_spd_add_del_entry_reply)         \
2353 _(IPSEC_SAD_ADD_DEL_ENTRY_REPLY, ipsec_sad_add_del_entry_reply)         \
2354 _(IPSEC_SA_SET_KEY_REPLY, ipsec_sa_set_key_reply)                       \
2355 _(IKEV2_PROFILE_ADD_DEL_REPLY, ikev2_profile_add_del_reply)             \
2356 _(IKEV2_PROFILE_SET_AUTH_REPLY, ikev2_profile_set_auth_reply)           \
2357 _(IKEV2_PROFILE_SET_ID_REPLY, ikev2_profile_set_id_reply)               \
2358 _(IKEV2_PROFILE_SET_TS_REPLY, ikev2_profile_set_ts_reply)               \
2359 _(IKEV2_SET_LOCAL_KEY_REPLY, ikev2_set_local_key_reply)                 \
2360 _(DELETE_LOOPBACK_REPLY, delete_loopback_reply)                         \
2361 _(BD_IP_MAC_ADD_DEL_REPLY, bd_ip_mac_add_del_reply)                     \
2362 _(DHCP_COMPL_EVENT, dhcp_compl_event)                                   \
2363 _(VNET_INTERFACE_COUNTERS, vnet_interface_counters)                     \
2364 _(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters)                         \
2365 _(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters)                         \
2366 _(MAP_ADD_DOMAIN_REPLY, map_add_domain_reply)                           \
2367 _(MAP_DEL_DOMAIN_REPLY, map_del_domain_reply)                           \
2368 _(MAP_ADD_DEL_RULE_REPLY, map_add_del_rule_reply)                       \
2369 _(MAP_DOMAIN_DETAILS, map_domain_details)                               \
2370 _(MAP_RULE_DETAILS, map_rule_details)                                   \
2371 _(WANT_INTERFACE_EVENTS_REPLY, want_interface_events_reply)             \
2372 _(WANT_STATS_REPLY, want_stats_reply)                                   \
2373 _(GET_FIRST_MSG_ID_REPLY, get_first_msg_id_reply)                       \
2374 _(COP_INTERFACE_ENABLE_DISABLE_REPLY, cop_interface_enable_disable_reply) \
2375 _(COP_WHITELIST_ENABLE_DISABLE_REPLY, cop_whitelist_enable_disable_reply) \
2376 _(GET_NODE_GRAPH_REPLY, get_node_graph_reply)                           \
2377 _(SW_INTERFACE_CLEAR_STATS_REPLY, sw_interface_clear_stats_reply)      \
2378 _(TRACE_PROFILE_ADD_REPLY, trace_profile_add_reply)                   \
2379 _(TRACE_PROFILE_APPLY_REPLY, trace_profile_apply_reply)               \
2380 _(TRACE_PROFILE_DEL_REPLY, trace_profile_del_reply)                     \
2381 _(LISP_ADD_DEL_LOCATOR_SET_REPLY, lisp_add_del_locator_set_reply)       \
2382 _(LISP_ADD_DEL_LOCATOR_REPLY, lisp_add_del_locator_reply)               \
2383 _(LISP_ADD_DEL_LOCAL_EID_REPLY, lisp_add_del_local_eid_reply)           \
2384 _(LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY, lisp_gpe_add_del_fwd_entry_reply)   \
2385 _(LISP_ADD_DEL_MAP_RESOLVER_REPLY, lisp_add_del_map_resolver_reply)     \
2386 _(LISP_GPE_ENABLE_DISABLE_REPLY, lisp_gpe_enable_disable_reply)         \
2387 _(LISP_ENABLE_DISABLE_REPLY, lisp_enable_disable_reply)                 \
2388 _(LISP_GPE_ADD_DEL_IFACE_REPLY, lisp_gpe_add_del_iface_reply)           \
2389 _(LISP_LOCATOR_SET_DETAILS, lisp_locator_set_details)                   \
2390 _(LISP_LOCAL_EID_TABLE_DETAILS, lisp_local_eid_table_details)           \
2391 _(LISP_GPE_TUNNEL_DETAILS, lisp_gpe_tunnel_details)                     \
2392 _(LISP_MAP_RESOLVER_DETAILS, lisp_map_resolver_details)                 \
2393 _(LISP_ENABLE_DISABLE_STATUS_DETAILS,                                   \
2394   lisp_enable_disable_status_details)                                   \
2395 _(AF_PACKET_CREATE_REPLY, af_packet_create_reply)                       \
2396 _(AF_PACKET_DELETE_REPLY, af_packet_delete_reply)
2397
2398 /* M: construct, but don't yet send a message */
2399
2400 #define M(T,t)                                  \
2401 do {                                            \
2402     vam->result_ready = 0;                      \
2403     mp = vl_msg_api_alloc(sizeof(*mp));         \
2404     memset (mp, 0, sizeof (*mp));               \
2405     mp->_vl_msg_id = ntohs (VL_API_##T);        \
2406     mp->client_index = vam->my_client_index;    \
2407 } while(0);
2408
2409 #define M2(T,t,n)                               \
2410 do {                                            \
2411     vam->result_ready = 0;                      \
2412     mp = vl_msg_api_alloc(sizeof(*mp)+(n));     \
2413     memset (mp, 0, sizeof (*mp));               \
2414     mp->_vl_msg_id = ntohs (VL_API_##T);        \
2415     mp->client_index = vam->my_client_index;    \
2416 } while(0);
2417
2418
2419 /* S: send a message */
2420 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
2421
2422 /* W: wait for results, with timeout */
2423 #define W                                       \
2424 do {                                            \
2425     timeout = vat_time_now (vam) + 1.0;         \
2426                                                 \
2427     while (vat_time_now (vam) < timeout) {      \
2428         if (vam->result_ready == 1) {           \
2429             return (vam->retval);               \
2430         }                                       \
2431     }                                           \
2432     return -99;                                 \
2433 } while(0);
2434
2435 typedef struct {
2436     u8 * name;
2437     u32 value;
2438 } name_sort_t;
2439
2440
2441 #define STR_VTR_OP_CASE(op)     \
2442     case L2_VTR_ ## op:         \
2443         return "" # op;
2444
2445 static const char *str_vtr_op(u32 vtr_op)
2446 {
2447     switch(vtr_op) {
2448         STR_VTR_OP_CASE(DISABLED);
2449         STR_VTR_OP_CASE(PUSH_1);
2450         STR_VTR_OP_CASE(PUSH_2);
2451         STR_VTR_OP_CASE(POP_1);
2452         STR_VTR_OP_CASE(POP_2);
2453         STR_VTR_OP_CASE(TRANSLATE_1_1);
2454         STR_VTR_OP_CASE(TRANSLATE_1_2);
2455         STR_VTR_OP_CASE(TRANSLATE_2_1);
2456         STR_VTR_OP_CASE(TRANSLATE_2_2);
2457     }
2458
2459     return "UNKNOWN";
2460 }
2461
2462 static int dump_sub_interface_table (vat_main_t * vam)
2463 {
2464     const sw_interface_subif_t * sub = NULL;
2465
2466     if (vam->json_output) {
2467         clib_warning ("JSON output supported only for VPE API calls and dump_stats_table");
2468         return -99;
2469     }
2470
2471     fformat (vam->ofp,
2472              "%-30s%-12s%-11s%-7s%-5s%-9s%-9s%-6s%-8s%-10s%-10s\n",
2473              "Interface", "sw_if_index",
2474              "sub id", "dot1ad", "tags", "outer id",
2475              "inner id", "exact", "default",
2476              "outer any", "inner any");
2477
2478     vec_foreach (sub, vam->sw_if_subif_table) {
2479         fformat (vam->ofp,
2480                  "%-30s%-12d%-11d%-7s%-5d%-9d%-9d%-6d%-8d%-10d%-10d\n",
2481                  sub->interface_name,
2482                  sub->sw_if_index,
2483                  sub->sub_id, sub->sub_dot1ad ? "dot1ad" : "dot1q",
2484                  sub->sub_number_of_tags, sub->sub_outer_vlan_id,
2485                  sub->sub_inner_vlan_id, sub->sub_exact_match, sub->sub_default,
2486                  sub->sub_outer_vlan_id_any, sub->sub_inner_vlan_id_any);
2487         if (sub->vtr_op != L2_VTR_DISABLED) {
2488             fformat (vam->ofp,
2489                      "  vlan-tag-rewrite - op: %-14s [ dot1q: %d "
2490                      "tag1: %d tag2: %d ]\n",
2491                      str_vtr_op(sub->vtr_op), sub->vtr_push_dot1q, 
2492                      sub->vtr_tag1, sub->vtr_tag2);
2493         }
2494     }
2495
2496     return 0;
2497 }
2498
2499 static int name_sort_cmp (void * a1, void * a2)
2500 {
2501   name_sort_t * n1 = a1;
2502   name_sort_t * n2 = a2;
2503
2504   return strcmp ((char *)n1->name, (char *)n2->name);
2505 }
2506
2507 static int dump_interface_table (vat_main_t * vam)
2508 {
2509     hash_pair_t * p;
2510     name_sort_t * nses = 0, * ns;
2511
2512     if (vam->json_output) {
2513         clib_warning ("JSON output supported only for VPE API calls and dump_stats_table");
2514         return -99;
2515     }
2516
2517     hash_foreach_pair (p, vam->sw_if_index_by_interface_name, 
2518     ({
2519         vec_add2 (nses, ns, 1);
2520         ns->name = (u8 *)(p->key);
2521         ns->value = (u32) p->value[0];
2522     }));
2523
2524     vec_sort_with_function (nses, name_sort_cmp);
2525
2526     fformat (vam->ofp, "%-25s%-15s\n", "Interface", "sw_if_index");
2527     vec_foreach (ns, nses) {
2528         fformat (vam->ofp, "%-25s%-15d\n", ns->name, ns->value);
2529     }
2530     vec_free (nses);
2531     return 0;
2532 }
2533
2534 static int dump_ip_table (vat_main_t * vam, int is_ipv6)
2535 {
2536     const ip_details_t * det = NULL;
2537     const ip_address_details_t * address = NULL;
2538     u32 i = ~0;
2539
2540     fformat (vam->ofp,
2541              "%-12s\n",
2542              "sw_if_index");
2543
2544     if (0 == vam) {
2545         return 0;
2546     }
2547
2548     vec_foreach (det, vam->ip_details_by_sw_if_index[is_ipv6]) {
2549         i++;
2550         if (!det->present) {
2551             continue;
2552         }
2553         fformat (vam->ofp,
2554                  "%-12d\n",
2555                  i);
2556         fformat (vam->ofp,
2557                  "            %-30s%-13s\n",
2558                  "Address", "Prefix length");
2559         if (!det->addr) {
2560             continue;
2561         }
2562         vec_foreach (address, det->addr) {
2563             fformat (vam->ofp,
2564                      "            %-30U%-13d\n",
2565                      is_ipv6 ? format_ip6_address : format_ip4_address,
2566                      address->ip,
2567                      address->prefix_length);
2568         }
2569     }
2570
2571     return 0;
2572 }
2573
2574 static int dump_ipv4_table (vat_main_t * vam)
2575 {
2576     if (vam->json_output) {
2577         clib_warning ("JSON output supported only for VPE API calls and dump_stats_table");
2578         return -99;
2579     }
2580
2581     return dump_ip_table (vam, 0);
2582 }
2583
2584 static int dump_ipv6_table (vat_main_t * vam)
2585 {
2586     if (vam->json_output) {
2587         clib_warning ("JSON output supported only for VPE API calls and dump_stats_table");
2588         return -99;
2589     }
2590
2591     return dump_ip_table (vam, 1);
2592 }
2593
2594 static char* counter_type_to_str (u8 counter_type, u8 is_combined)
2595 {
2596     if (!is_combined) {
2597         switch(counter_type) {
2598         case VNET_INTERFACE_COUNTER_DROP:
2599             return "drop";
2600         case VNET_INTERFACE_COUNTER_PUNT:
2601             return "punt";
2602         case VNET_INTERFACE_COUNTER_IP4:
2603             return "ip4";
2604         case VNET_INTERFACE_COUNTER_IP6:
2605             return "ip6";
2606         case VNET_INTERFACE_COUNTER_RX_NO_BUF:
2607             return "rx-no-buf";
2608         case VNET_INTERFACE_COUNTER_RX_MISS:
2609             return "rx-miss";
2610         case VNET_INTERFACE_COUNTER_RX_ERROR:
2611             return "rx-error";
2612         case VNET_INTERFACE_COUNTER_TX_ERROR:
2613             return "tx-error";
2614         default:
2615             return "INVALID-COUNTER-TYPE";
2616         }
2617     } else {
2618         switch(counter_type) {
2619         case VNET_INTERFACE_COUNTER_RX:
2620             return "rx";
2621         case VNET_INTERFACE_COUNTER_TX:
2622             return "tx";
2623         default:
2624             return "INVALID-COUNTER-TYPE";
2625         }
2626     }
2627 }
2628
2629 static int dump_stats_table (vat_main_t * vam)
2630 {
2631     vat_json_node_t node;
2632     vat_json_node_t *msg_array;
2633     vat_json_node_t *msg;
2634     vat_json_node_t *counter_array;
2635     vat_json_node_t *counter;
2636     interface_counter_t c;
2637     u64 packets;
2638     ip4_fib_counter_t *c4;
2639     ip6_fib_counter_t *c6;
2640     int i, j;
2641
2642     if (!vam->json_output) {
2643         clib_warning ("dump_stats_table supported only in JSON format");
2644         return -99;
2645     }
2646
2647     vat_json_init_object(&node);
2648
2649     /* interface counters */
2650     msg_array = vat_json_object_add(&node, "interface_counters");
2651     vat_json_init_array(msg_array);
2652     for (i = 0; i < vec_len(vam->simple_interface_counters); i++) {
2653         msg = vat_json_array_add(msg_array);
2654         vat_json_init_object(msg);
2655         vat_json_object_add_string_copy(msg, "vnet_counter_type",
2656                 (u8*)counter_type_to_str(i, 0));
2657         vat_json_object_add_int(msg, "is_combined", 0);
2658         counter_array = vat_json_object_add(msg, "data");
2659         vat_json_init_array(counter_array);
2660         for (j = 0; j < vec_len(vam->simple_interface_counters[i]); j++) {
2661             packets = vam->simple_interface_counters[i][j];
2662             vat_json_array_add_uint(counter_array, packets);
2663         }
2664     }
2665     for (i = 0; i < vec_len(vam->combined_interface_counters); i++) {
2666         msg = vat_json_array_add(msg_array);
2667         vat_json_init_object(msg);
2668         vat_json_object_add_string_copy(msg, "vnet_counter_type",
2669                 (u8*)counter_type_to_str(i, 1));
2670         vat_json_object_add_int(msg, "is_combined", 1);
2671         counter_array = vat_json_object_add(msg, "data");
2672         vat_json_init_array(counter_array);
2673         for (j = 0; j < vec_len(vam->combined_interface_counters[i]); j++) {
2674             c = vam->combined_interface_counters[i][j];
2675             counter = vat_json_array_add(counter_array);
2676             vat_json_init_object(counter);
2677             vat_json_object_add_uint(counter, "packets", c.packets);
2678             vat_json_object_add_uint(counter, "bytes", c.bytes);
2679         }
2680     }
2681
2682     /* ip4 fib counters */
2683     msg_array = vat_json_object_add(&node, "ip4_fib_counters");
2684     vat_json_init_array(msg_array);
2685     for (i = 0; i < vec_len(vam->ip4_fib_counters); i++) {
2686         msg = vat_json_array_add(msg_array);
2687         vat_json_init_object(msg);
2688         vat_json_object_add_uint(msg, "vrf_id", vam->ip4_fib_counters_vrf_id_by_index[i]);
2689         counter_array = vat_json_object_add(msg, "c");
2690         vat_json_init_array(counter_array);
2691         for (j = 0; j < vec_len(vam->ip4_fib_counters[i]); j++) {
2692             counter = vat_json_array_add(counter_array);
2693             vat_json_init_object(counter);
2694             c4 = &vam->ip4_fib_counters[i][j];
2695             vat_json_object_add_ip4(counter, "address", c4->address);
2696             vat_json_object_add_uint(counter, "address_length", c4->address_length);
2697             vat_json_object_add_uint(counter, "packets", c4->packets);
2698             vat_json_object_add_uint(counter, "bytes", c4->bytes);
2699         }
2700     }
2701
2702     /* ip6 fib counters */
2703     msg_array = vat_json_object_add(&node, "ip6_fib_counters");
2704     vat_json_init_array(msg_array);
2705     for (i = 0; i < vec_len(vam->ip6_fib_counters); i++) {
2706         msg = vat_json_array_add(msg_array);
2707         vat_json_init_object(msg);
2708         vat_json_object_add_uint(msg, "vrf_id", vam->ip6_fib_counters_vrf_id_by_index[i]);
2709         counter_array = vat_json_object_add(msg, "c");
2710         vat_json_init_array(counter_array);
2711         for (j = 0; j < vec_len(vam->ip6_fib_counters[i]); j++) {
2712             counter = vat_json_array_add(counter_array);
2713             vat_json_init_object(counter);
2714             c6 = &vam->ip6_fib_counters[i][j];
2715             vat_json_object_add_ip6(counter, "address", c6->address);
2716             vat_json_object_add_uint(counter, "address_length", c6->address_length);
2717             vat_json_object_add_uint(counter, "packets", c6->packets);
2718             vat_json_object_add_uint(counter, "bytes", c6->bytes);
2719         }
2720     }
2721
2722     vat_json_print(vam->ofp, &node);
2723     vat_json_free(&node);
2724
2725     return 0;
2726 }
2727
2728 int exec (vat_main_t * vam)
2729 {
2730     api_main_t * am = &api_main;
2731     vl_api_cli_request_t *mp;
2732     f64 timeout;
2733     void * oldheap;
2734     u8 * cmd = 0;
2735     unformat_input_t * i = vam->input;
2736
2737     if (vec_len(i->buffer) == 0)
2738         return -1;
2739
2740     if (vam->exec_mode == 0 && unformat (i, "mode")) {        
2741         vam->exec_mode = 1;
2742         return 0;
2743     }
2744     if (vam->exec_mode == 1 && 
2745         (unformat (i, "exit") || unformat (i, "quit"))) {
2746         vam->exec_mode = 0;
2747         return 0;
2748     }
2749     
2750
2751     M(CLI_REQUEST, cli_request);
2752
2753     /* 
2754      * Copy cmd into shared memory.
2755      * In order for the CLI command to work, it
2756      * must be a vector ending in \n, not a C-string ending
2757      * in \n\0.
2758      */
2759     pthread_mutex_lock (&am->vlib_rp->mutex);
2760     oldheap = svm_push_data_heap (am->vlib_rp);
2761
2762     vec_validate (cmd, vec_len(vam->input->buffer)-1);
2763     clib_memcpy (cmd, vam->input->buffer, vec_len(vam->input->buffer));
2764
2765     svm_pop_heap (oldheap);
2766     pthread_mutex_unlock (&am->vlib_rp->mutex);
2767
2768     mp->cmd_in_shmem = (u64) cmd;
2769     S;
2770     timeout = vat_time_now (vam) + 10.0;
2771
2772     while (vat_time_now (vam) < timeout) {
2773         if (vam->result_ready == 1) {
2774             u8 * free_me;
2775             if (vam->shmem_result != NULL)
2776                 fformat (vam->ofp, "%s", vam->shmem_result);
2777             pthread_mutex_lock (&am->vlib_rp->mutex);
2778             oldheap = svm_push_data_heap (am->vlib_rp);
2779             
2780             free_me = (u8 *)vam->shmem_result;
2781             vec_free (free_me);
2782
2783             svm_pop_heap (oldheap);
2784             pthread_mutex_unlock (&am->vlib_rp->mutex);
2785             return 0;
2786         }
2787     }
2788     return -99;
2789 }
2790
2791 static int api_create_loopback (vat_main_t * vam)
2792 {
2793     unformat_input_t * i = vam->input;
2794     vl_api_create_loopback_t *mp;
2795     f64 timeout;
2796     u8 mac_address[6];
2797     u8 mac_set = 0;
2798
2799     memset (mac_address, 0, sizeof (mac_address));
2800
2801     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
2802       {
2803         if (unformat (i, "mac %U", unformat_ethernet_address, mac_address))
2804             mac_set = 1;
2805         else
2806           break;
2807       }
2808
2809     /* Construct the API message */
2810     M(CREATE_LOOPBACK, create_loopback);
2811     if (mac_set)
2812         clib_memcpy (mp->mac_address, mac_address, sizeof (mac_address));
2813
2814     S; W;
2815 }
2816
2817 static int api_delete_loopback (vat_main_t * vam)
2818 {
2819     unformat_input_t * i = vam->input;
2820     vl_api_delete_loopback_t *mp;
2821     f64 timeout;
2822     u32 sw_if_index = ~0;
2823
2824     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
2825       {
2826         if (unformat (i, "sw_if_index %d", &sw_if_index))
2827           ;
2828         else
2829           break;
2830       }
2831
2832     if (sw_if_index == ~0)
2833       {
2834         errmsg ("missing sw_if_index\n");
2835         return -99;
2836       }
2837
2838     /* Construct the API message */
2839     M(DELETE_LOOPBACK, delete_loopback);
2840     mp->sw_if_index = ntohl (sw_if_index);
2841
2842     S; W;
2843 }
2844
2845 static int api_want_stats (vat_main_t * vam)
2846 {
2847     unformat_input_t * i = vam->input;
2848     vl_api_want_stats_t * mp;
2849     f64 timeout;
2850     int enable = -1;
2851
2852     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
2853       {
2854         if (unformat (i, "enable"))
2855           enable = 1;
2856         else if (unformat (i, "disable"))
2857           enable = 0;
2858         else
2859           break;
2860       }
2861
2862     if (enable == -1)
2863       {
2864         errmsg ("missing enable|disable\n");
2865         return -99;
2866       }
2867
2868     M(WANT_STATS, want_stats);
2869     mp->enable_disable = enable;
2870
2871     S; W;
2872 }
2873
2874 static int api_want_interface_events (vat_main_t * vam)
2875 {
2876     unformat_input_t * i = vam->input;
2877     vl_api_want_interface_events_t * mp;
2878     f64 timeout;
2879     int enable = -1;
2880
2881     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
2882       {
2883         if (unformat (i, "enable"))
2884           enable = 1;
2885         else if (unformat (i, "disable"))
2886           enable = 0;
2887         else
2888           break;
2889       }
2890
2891     if (enable == -1)
2892       {
2893         errmsg ("missing enable|disable\n");
2894         return -99;
2895       }
2896
2897     M(WANT_INTERFACE_EVENTS, want_interface_events);
2898     mp->enable_disable = enable;
2899
2900     vam->interface_event_display = enable;
2901
2902     S; W;
2903 }
2904
2905
2906 /* Note: non-static, called once to set up the initial intfc table */
2907 int api_sw_interface_dump (vat_main_t * vam)
2908 {
2909     vl_api_sw_interface_dump_t *mp;
2910     f64 timeout;
2911     hash_pair_t * p;
2912     name_sort_t * nses = 0, * ns;
2913     sw_interface_subif_t * sub = NULL;
2914
2915     /* Toss the old name table */
2916     hash_foreach_pair (p, vam->sw_if_index_by_interface_name, 
2917     ({
2918         vec_add2 (nses, ns, 1);
2919         ns->name = (u8 *)(p->key);
2920         ns->value = (u32) p->value[0];
2921     }));
2922
2923     hash_free (vam->sw_if_index_by_interface_name);
2924
2925     vec_foreach (ns, nses)
2926         vec_free (ns->name);
2927
2928     vec_free (nses);
2929
2930     vec_foreach (sub, vam->sw_if_subif_table) {
2931         vec_free (sub->interface_name);
2932     }
2933     vec_free (vam->sw_if_subif_table);
2934
2935     /* recreate the interface name hash table */
2936     vam->sw_if_index_by_interface_name 
2937         = hash_create_string (0, sizeof(uword));
2938
2939     /* Get list of ethernets */
2940     M(SW_INTERFACE_DUMP, sw_interface_dump);
2941     mp->name_filter_valid = 1;
2942     strncpy ((char *) mp->name_filter, "Ether", sizeof(mp->name_filter)-1);
2943     S;
2944
2945     /* and local / loopback interfaces */
2946     M(SW_INTERFACE_DUMP, sw_interface_dump);
2947     mp->name_filter_valid = 1;
2948     strncpy ((char *) mp->name_filter, "lo", sizeof(mp->name_filter)-1);
2949     S;
2950
2951     /* and vxlan tunnel interfaces */
2952     M(SW_INTERFACE_DUMP, sw_interface_dump);
2953     mp->name_filter_valid = 1;
2954     strncpy ((char *) mp->name_filter, "vxlan", sizeof(mp->name_filter)-1);
2955     S;
2956
2957     /* and host (af_packet) interfaces */
2958     M(SW_INTERFACE_DUMP, sw_interface_dump);
2959     mp->name_filter_valid = 1;
2960     strncpy ((char *) mp->name_filter, "host", sizeof(mp->name_filter)-1);
2961     S;
2962
2963     /* and l2tpv3 tunnel interfaces */
2964     M(SW_INTERFACE_DUMP, sw_interface_dump);
2965     mp->name_filter_valid = 1;
2966     strncpy ((char *) mp->name_filter, "l2tpv3_tunnel", sizeof(mp->name_filter)-1);
2967     S;
2968
2969     /* and GRE tunnel interfaces */
2970     M(SW_INTERFACE_DUMP, sw_interface_dump);
2971     mp->name_filter_valid = 1;
2972     strncpy ((char *) mp->name_filter, "gre", sizeof(mp->name_filter)-1);
2973     S;
2974
2975     /* Use a control ping for synchronization */
2976     {
2977         vl_api_control_ping_t * mp;
2978         M(CONTROL_PING, control_ping);
2979         S;
2980     }
2981     W;
2982 }
2983
2984 static int api_sw_interface_set_flags (vat_main_t * vam)
2985 {
2986     unformat_input_t * i = vam->input;
2987     vl_api_sw_interface_set_flags_t *mp;
2988     f64 timeout;
2989     u32 sw_if_index;
2990     u8 sw_if_index_set = 0;
2991     u8 admin_up = 0, link_up = 0;
2992     
2993     /* Parse args required to build the message */
2994     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
2995         if (unformat (i, "admin-up"))
2996             admin_up = 1;
2997         else if (unformat (i, "admin-down"))
2998             admin_up = 0;
2999         else if (unformat (i, "link-up"))
3000             link_up = 1;
3001         else if (unformat (i, "link-down"))
3002             link_up = 0;
3003         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3004             sw_if_index_set = 1;
3005         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3006             sw_if_index_set = 1;
3007         else
3008             break;
3009     }
3010
3011     if (sw_if_index_set == 0) {
3012         errmsg ("missing interface name or sw_if_index\n");
3013         return -99;
3014     }
3015
3016     /* Construct the API message */
3017     M(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags);
3018     mp->sw_if_index = ntohl (sw_if_index);
3019     mp->admin_up_down = admin_up;
3020     mp->link_up_down = link_up;
3021
3022     /* send it... */
3023     S;
3024
3025     /* Wait for a reply, return the good/bad news... */
3026     W;
3027 }
3028
3029 static int api_sw_interface_clear_stats (vat_main_t * vam)
3030 {
3031     unformat_input_t * i = vam->input;
3032     vl_api_sw_interface_clear_stats_t *mp;
3033     f64 timeout;
3034     u32 sw_if_index;
3035     u8 sw_if_index_set = 0;
3036
3037     /* Parse args required to build the message */
3038     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3039         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3040             sw_if_index_set = 1;
3041         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3042             sw_if_index_set = 1;
3043         else
3044             break;
3045     }
3046
3047     /* Construct the API message */
3048     M(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats);
3049
3050     if (sw_if_index_set == 1)
3051         mp->sw_if_index = ntohl (sw_if_index);
3052     else
3053         mp->sw_if_index = ~0;
3054
3055     /* send it... */
3056     S;
3057
3058     /* Wait for a reply, return the good/bad news... */
3059     W;
3060 }
3061
3062 static int api_sw_interface_add_del_address (vat_main_t * vam)
3063 {
3064     unformat_input_t * i = vam->input;
3065     vl_api_sw_interface_add_del_address_t *mp;
3066     f64 timeout;
3067     u32 sw_if_index;
3068     u8 sw_if_index_set = 0;
3069     u8 is_add = 1, del_all = 0;
3070     u32 address_length = 0;
3071     u8 v4_address_set = 0;
3072     u8 v6_address_set = 0;
3073     ip4_address_t v4address;
3074     ip6_address_t v6address;
3075     
3076     /* Parse args required to build the message */
3077     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3078         if (unformat (i, "del-all"))
3079             del_all = 1;
3080         else if (unformat (i, "del"))
3081             is_add = 0;
3082         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3083             sw_if_index_set = 1;
3084         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3085             sw_if_index_set = 1;
3086         else if (unformat (i, "%U/%d", 
3087                            unformat_ip4_address, &v4address, 
3088                            &address_length))
3089             v4_address_set = 1;
3090         else if (unformat (i, "%U/%d", 
3091                            unformat_ip6_address, &v6address, 
3092                            &address_length))
3093             v6_address_set = 1;
3094         else
3095             break;
3096     }
3097
3098     if (sw_if_index_set == 0) {
3099         errmsg ("missing interface name or sw_if_index\n");
3100         return -99;
3101     }
3102     if (v4_address_set && v6_address_set) {
3103         errmsg ("both v4 and v6 addresses set\n");
3104         return -99;
3105     }
3106     if (!v4_address_set && !v6_address_set && !del_all) {
3107         errmsg ("no addresses set\n");
3108         return -99;
3109     }
3110
3111     /* Construct the API message */
3112     M(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address);
3113
3114     mp->sw_if_index = ntohl (sw_if_index);
3115     mp->is_add = is_add;
3116     mp->del_all = del_all;
3117     if (v6_address_set) {
3118         mp->is_ipv6 = 1;
3119         clib_memcpy (mp->address, &v6address, sizeof (v6address));
3120     } else {
3121         clib_memcpy (mp->address, &v4address, sizeof (v4address));
3122     }
3123     mp->address_length = address_length;
3124
3125     /* send it... */
3126     S;
3127
3128     /* Wait for a reply, return good/bad news  */
3129     W;
3130 }
3131
3132 static int api_sw_interface_set_table (vat_main_t * vam)
3133 {
3134     unformat_input_t * i = vam->input;
3135     vl_api_sw_interface_set_table_t *mp;
3136     f64 timeout;
3137     u32 sw_if_index, vrf_id = 0;
3138     u8 sw_if_index_set = 0;
3139     u8 is_ipv6 = 0;
3140     
3141     /* Parse args required to build the message */
3142     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3143         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3144             sw_if_index_set = 1;
3145         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3146             sw_if_index_set = 1;
3147         else if (unformat (i, "vrf %d", &vrf_id))
3148             ;
3149         else if (unformat (i, "ipv6"))
3150             is_ipv6 = 1;
3151         else
3152             break;
3153     }
3154
3155     if (sw_if_index_set == 0) {
3156         errmsg ("missing interface name or sw_if_index\n");
3157         return -99;
3158     }
3159
3160     /* Construct the API message */
3161     M(SW_INTERFACE_SET_TABLE, sw_interface_set_table);
3162
3163     mp->sw_if_index = ntohl (sw_if_index);
3164     mp->is_ipv6 = is_ipv6;
3165     mp->vrf_id = ntohl (vrf_id);
3166
3167     /* send it... */
3168     S;
3169
3170     /* Wait for a reply... */
3171     W;
3172 }
3173
3174 static int api_sw_interface_set_vpath (vat_main_t * vam)
3175 {
3176     unformat_input_t * i = vam->input;
3177     vl_api_sw_interface_set_vpath_t *mp;
3178     f64 timeout;
3179     u32 sw_if_index = 0;
3180     u8 sw_if_index_set = 0;
3181     u8 is_enable = 0;
3182     
3183     /* Parse args required to build the message */
3184     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3185         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3186             sw_if_index_set = 1;
3187         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3188             sw_if_index_set = 1;
3189         else if (unformat (i, "enable"))
3190             is_enable = 1;
3191         else if (unformat (i, "disable"))
3192             is_enable = 0;
3193         else
3194             break;
3195     }
3196
3197     if (sw_if_index_set == 0) {
3198         errmsg ("missing interface name or sw_if_index\n");
3199         return -99;
3200     }
3201
3202     /* Construct the API message */
3203     M(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath);
3204
3205     mp->sw_if_index = ntohl (sw_if_index);
3206     mp->enable = is_enable;
3207
3208     /* send it... */
3209     S;
3210
3211     /* Wait for a reply... */
3212     W;
3213 }
3214
3215 static int api_sw_interface_set_l2_xconnect (vat_main_t * vam)
3216 {
3217     unformat_input_t * i = vam->input;
3218     vl_api_sw_interface_set_l2_xconnect_t *mp;
3219     f64 timeout;
3220     u32 rx_sw_if_index;
3221     u8 rx_sw_if_index_set = 0;
3222     u32 tx_sw_if_index;
3223     u8 tx_sw_if_index_set = 0;
3224     u8 enable = 1;
3225     
3226     /* Parse args required to build the message */
3227     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3228         if (unformat (i, "rx_sw_if_index %d", &rx_sw_if_index))
3229             rx_sw_if_index_set = 1;     
3230         else if (unformat (i, "tx_sw_if_index %d", &tx_sw_if_index))
3231             tx_sw_if_index_set = 1;
3232         else if (unformat (i, "rx")) {
3233             if (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3234                 if (unformat (i, "%U", unformat_sw_if_index, vam,
3235                               &rx_sw_if_index))
3236                     rx_sw_if_index_set = 1;
3237             } else
3238                 break;
3239         } else if (unformat (i, "tx")) {
3240             if (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3241                 if (unformat (i, "%U", unformat_sw_if_index, vam,
3242                               &tx_sw_if_index))
3243                     tx_sw_if_index_set = 1;
3244             } else
3245                 break;
3246         } else if (unformat (i, "enable"))
3247             enable = 1;
3248         else if (unformat (i, "disable")) 
3249             enable = 0;
3250         else
3251             break;
3252     }
3253
3254     if (rx_sw_if_index_set == 0) {
3255         errmsg ("missing rx interface name or rx_sw_if_index\n");
3256         return -99;
3257     }
3258
3259     if (enable && (tx_sw_if_index_set == 0)) {
3260         errmsg ("missing tx interface name or tx_sw_if_index\n");
3261         return -99;
3262     }
3263     
3264     M(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect);
3265
3266     mp->rx_sw_if_index = ntohl(rx_sw_if_index);
3267     mp->tx_sw_if_index = ntohl(tx_sw_if_index);
3268     mp->enable = enable;
3269
3270     S; W;
3271     /* NOTREACHED */
3272     return 0;
3273 }
3274
3275 static int api_sw_interface_set_l2_bridge (vat_main_t * vam)
3276 {
3277     unformat_input_t * i = vam->input;
3278     vl_api_sw_interface_set_l2_bridge_t *mp;
3279     f64 timeout;
3280     u32 rx_sw_if_index;
3281     u8 rx_sw_if_index_set = 0;
3282     u32 bd_id;
3283     u8 bd_id_set = 0;
3284     u8 bvi = 0;
3285     u32 shg = 0;
3286     u8 enable = 1;
3287     
3288     /* Parse args required to build the message */
3289     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3290         if (unformat (i, "sw_if_index %d", &rx_sw_if_index))
3291             rx_sw_if_index_set = 1;     
3292         else if (unformat (i, "bd_id %d", &bd_id))
3293             bd_id_set = 1;
3294         else if (unformat (i, "%U", unformat_sw_if_index, vam,
3295                            &rx_sw_if_index))
3296             rx_sw_if_index_set = 1;
3297         else if (unformat (i, "shg %d", &shg)) 
3298             ;
3299         else if (unformat (i, "bvi"))
3300             bvi = 1;
3301         else if (unformat (i, "enable"))
3302             enable = 1;
3303         else if (unformat (i, "disable")) 
3304             enable = 0;
3305         else
3306             break;
3307     }
3308
3309     if (rx_sw_if_index_set == 0) {
3310         errmsg ("missing rx interface name or sw_if_index\n");
3311         return -99;
3312     }
3313
3314     if (enable && (bd_id_set == 0)) {
3315         errmsg ("missing bridge domain\n");
3316         return -99;
3317     }
3318     
3319     M(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge);
3320
3321     mp->rx_sw_if_index = ntohl(rx_sw_if_index);
3322     mp->bd_id = ntohl(bd_id);
3323     mp->shg = (u8)shg;
3324     mp->bvi = bvi;
3325     mp->enable = enable;
3326
3327     S; W;
3328     /* NOTREACHED */
3329     return 0;
3330 }
3331
3332 static int api_bridge_domain_dump (vat_main_t * vam)
3333 {
3334     unformat_input_t * i = vam->input;
3335     vl_api_bridge_domain_dump_t *mp;
3336     f64 timeout;
3337     u32 bd_id = ~0;
3338
3339     /* Parse args required to build the message */
3340     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3341         if (unformat (i, "bd_id %d", &bd_id))
3342             ;
3343         else
3344             break;
3345     }
3346
3347     M(BRIDGE_DOMAIN_DUMP, bridge_domain_dump);
3348     mp->bd_id = ntohl(bd_id);
3349     S;
3350
3351     /* Use a control ping for synchronization */
3352     {
3353         vl_api_control_ping_t * mp;
3354         M(CONTROL_PING, control_ping);
3355         S;
3356     }
3357
3358     W;
3359     /* NOTREACHED */
3360     return 0;
3361 }
3362
3363 static int api_bridge_domain_add_del (vat_main_t * vam)
3364 {
3365     unformat_input_t * i = vam->input;
3366     vl_api_bridge_domain_add_del_t *mp;
3367     f64 timeout;
3368     u32 bd_id = ~0;
3369     u8 is_add = 1;
3370     u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
3371
3372     /* Parse args required to build the message */
3373     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3374         if (unformat (i, "bd_id %d", &bd_id))
3375             ;
3376         else if (unformat (i, "flood %d", &flood))
3377              ;
3378         else if (unformat (i, "uu-flood %d", &uu_flood))
3379              ;
3380         else if (unformat (i, "forward %d", &forward))
3381              ;
3382         else if (unformat (i, "learn %d", &learn))
3383              ;
3384         else if (unformat (i, "arp-term %d", &arp_term))
3385              ;
3386         else if (unformat (i, "del")) {
3387              is_add = 0;
3388              flood = uu_flood = forward = learn = 0;
3389         }
3390         else
3391             break;
3392     }
3393
3394     if (bd_id == ~0) {
3395         errmsg ("missing bridge domain\n");
3396         return -99;
3397     }
3398
3399     M(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del);
3400
3401     mp->bd_id = ntohl(bd_id);
3402     mp->flood = flood;
3403     mp->uu_flood = uu_flood;
3404     mp->forward = forward;
3405     mp->learn = learn;
3406     mp->arp_term = arp_term;
3407     mp->is_add = is_add;
3408
3409     S; W;
3410     /* NOTREACHED */
3411     return 0;
3412 }
3413
3414 static int api_l2fib_add_del (vat_main_t * vam)
3415 {
3416     unformat_input_t * i = vam->input;
3417     vl_api_l2fib_add_del_t *mp;
3418     f64 timeout;
3419     u64 mac = 0;
3420     u8 mac_set = 0;
3421     u32 bd_id;
3422     u8 bd_id_set = 0;
3423     u32 sw_if_index;
3424     u8 sw_if_index_set = 0;
3425     u8 is_add = 1;
3426     u8 static_mac = 0;
3427     u8 filter_mac = 0;
3428
3429     /* Parse args required to build the message */
3430     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3431         if (unformat (i, "mac %U", unformat_ethernet_address, &mac))
3432             mac_set = 1;
3433         else if (unformat (i, "bd_id %d", &bd_id))
3434             bd_id_set = 1;
3435         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3436             sw_if_index_set = 1;        
3437         else if (unformat (i, "sw_if")) {
3438             if (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3439                 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3440                     sw_if_index_set = 1;
3441             } else
3442                 break;
3443         } else if (unformat (i, "static"))
3444                 static_mac = 1;
3445         else if (unformat (i, "filter")) {
3446                 filter_mac = 1;
3447                 static_mac = 1;
3448         } else if (unformat (i, "del"))
3449                 is_add = 0;
3450         else
3451             break;
3452     }
3453
3454     if (mac_set == 0) {
3455         errmsg ("missing mac address\n");
3456         return -99;
3457     }
3458
3459     if (bd_id_set == 0) {
3460         errmsg ("missing bridge domain\n");
3461         return -99;
3462     }
3463
3464     if (is_add && (sw_if_index_set == 0)) {
3465         errmsg ("missing interface name or sw_if_index\n");
3466         return -99;
3467     }
3468
3469     M(L2FIB_ADD_DEL, l2fib_add_del);
3470
3471     mp->mac = mac;
3472     mp->bd_id = ntohl(bd_id);
3473     mp->is_add = is_add;
3474
3475     if (is_add) {
3476         mp->sw_if_index = ntohl(sw_if_index);
3477         mp->static_mac = static_mac;
3478         mp->filter_mac = filter_mac;
3479     }
3480     
3481     S; W;
3482     /* NOTREACHED */
3483     return 0;
3484 }
3485
3486 static int api_l2_flags (vat_main_t * vam)
3487 {
3488     unformat_input_t * i = vam->input;
3489     vl_api_l2_flags_t *mp;
3490     f64 timeout;
3491     u32 sw_if_index;
3492     u32 feature_bitmap = 0;
3493     u8 sw_if_index_set = 0;
3494
3495     /* Parse args required to build the message */
3496     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3497         if (unformat (i, "sw_if_index %d", &sw_if_index))
3498             sw_if_index_set = 1;        
3499         else if (unformat (i, "sw_if")) {
3500             if (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3501                 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3502                     sw_if_index_set = 1;
3503             } else
3504                 break;
3505         } else if (unformat (i, "learn"))
3506             feature_bitmap |= L2INPUT_FEAT_LEARN;
3507         else if (unformat (i, "forward"))
3508             feature_bitmap |= L2INPUT_FEAT_FWD;
3509         else if (unformat (i, "flood"))
3510             feature_bitmap |= L2INPUT_FEAT_FLOOD;
3511         else if (unformat (i, "uu-flood"))
3512             feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
3513         else
3514             break;
3515     }
3516
3517     if (sw_if_index_set == 0) {
3518         errmsg ("missing interface name or sw_if_index\n");
3519         return -99;
3520     }
3521
3522     M(L2_FLAGS, l2_flags);
3523
3524     mp->sw_if_index = ntohl(sw_if_index);
3525     mp->feature_bitmap = ntohl(feature_bitmap);
3526
3527     S; W;
3528     /* NOTREACHED */
3529     return 0;
3530 }
3531
3532 static int api_bridge_flags (vat_main_t * vam)
3533 {
3534     unformat_input_t * i = vam->input;
3535     vl_api_bridge_flags_t *mp;
3536     f64 timeout;
3537     u32 bd_id;
3538     u8 bd_id_set = 0;
3539     u8 is_set = 1;
3540     u32 flags = 0;
3541
3542     /* Parse args required to build the message */
3543     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3544         if (unformat (i, "bd_id %d", &bd_id))
3545             bd_id_set = 1;
3546         else if (unformat (i, "learn"))
3547             flags |= L2_LEARN;
3548         else if (unformat (i, "forward"))
3549             flags |= L2_FWD;
3550         else if (unformat (i, "flood"))
3551             flags |= L2_FLOOD;
3552         else if (unformat (i, "uu-flood"))
3553             flags |= L2_UU_FLOOD;
3554         else if (unformat (i, "arp-term"))
3555             flags |= L2_ARP_TERM;
3556         else if (unformat (i, "off"))
3557             is_set = 0;
3558         else if (unformat (i, "disable"))
3559             is_set = 0;
3560         else
3561             break;
3562     }
3563
3564     if (bd_id_set == 0) {
3565         errmsg ("missing bridge domain\n");
3566         return -99;
3567     }
3568
3569     M(BRIDGE_FLAGS, bridge_flags);
3570
3571     mp->bd_id = ntohl(bd_id);
3572     mp->feature_bitmap = ntohl(flags);
3573     mp->is_set = is_set;
3574
3575     S; W;
3576     /* NOTREACHED */
3577     return 0;
3578 }
3579
3580 static int api_bd_ip_mac_add_del (vat_main_t * vam)
3581 {
3582     unformat_input_t * i = vam->input;
3583     vl_api_bd_ip_mac_add_del_t *mp;
3584     f64 timeout;
3585     u32 bd_id;
3586     u8 is_ipv6 = 0;
3587     u8 is_add = 1;
3588     u8 bd_id_set = 0;
3589     u8 ip_set = 0;
3590     u8 mac_set = 0;
3591     ip4_address_t v4addr;
3592     ip6_address_t v6addr;
3593     u8 macaddr[6];
3594     
3595
3596     /* Parse args required to build the message */
3597     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3598         if (unformat (i, "bd_id %d", &bd_id)) {
3599             bd_id_set++;
3600         } else if (unformat (i, "%U", unformat_ip4_address, &v4addr)) {
3601             ip_set++;
3602         } else if (unformat (i, "%U", unformat_ip6_address, &v6addr)) {
3603             ip_set++;
3604             is_ipv6++;
3605         } else if (unformat (i, "%U", unformat_ethernet_address, macaddr)) {
3606             mac_set++;
3607         } else if (unformat (i, "del"))
3608             is_add = 0;
3609         else
3610             break;
3611     }
3612
3613     if (bd_id_set == 0) {
3614         errmsg ("missing bridge domain\n");
3615         return -99;
3616     } else if (ip_set == 0) {
3617         errmsg ("missing IP address\n");
3618         return -99;
3619     } else if (mac_set == 0) {
3620         errmsg ("missing MAC address\n");
3621         return -99;
3622     }
3623
3624     M(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del);
3625
3626     mp->bd_id = ntohl(bd_id);
3627     mp->is_ipv6 = is_ipv6;
3628     mp->is_add = is_add;
3629     if (is_ipv6)
3630          clib_memcpy (mp->ip_address, &v6addr, sizeof (v6addr));
3631     else clib_memcpy (mp->ip_address, &v4addr, sizeof (v4addr));
3632     clib_memcpy (mp->mac_address, macaddr, 6);
3633     S; W;
3634     /* NOTREACHED */
3635     return 0;
3636 }
3637
3638 static int api_tap_connect (vat_main_t * vam)
3639 {
3640     unformat_input_t * i = vam->input;
3641     vl_api_tap_connect_t *mp;
3642     f64 timeout;
3643     u8 mac_address[6];
3644     u8 random_mac = 1;
3645     u8 name_set = 0;
3646     u8 * tap_name;
3647
3648     memset (mac_address, 0, sizeof (mac_address));
3649     
3650     /* Parse args required to build the message */
3651     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3652         if (unformat (i, "mac %U", unformat_ethernet_address, mac_address)) {
3653             random_mac = 0;
3654         }
3655         else if (unformat (i, "random-mac"))
3656             random_mac = 1;
3657         else if (unformat (i, "tapname %s", &tap_name))
3658             name_set = 1;
3659         else
3660             break;
3661     }
3662
3663     if (name_set == 0) {
3664         errmsg ("missing tap name\n");
3665         return -99;
3666     }
3667     if (vec_len (tap_name) > 63) {
3668         errmsg ("tap name too long\n");
3669     }
3670     vec_add1 (tap_name, 0);
3671         
3672     /* Construct the API message */
3673     M(TAP_CONNECT, tap_connect);
3674
3675     mp->use_random_mac = random_mac;
3676     clib_memcpy (mp->mac_address, mac_address, 6);
3677     clib_memcpy (mp->tap_name, tap_name, vec_len (tap_name));
3678     vec_free (tap_name);
3679
3680     /* send it... */
3681     S;
3682
3683     /* Wait for a reply... */
3684     W;
3685 }
3686
3687 static int api_tap_modify (vat_main_t * vam)
3688 {
3689     unformat_input_t * i = vam->input;
3690     vl_api_tap_modify_t *mp;
3691     f64 timeout;
3692     u8 mac_address[6];
3693     u8 random_mac = 1;
3694     u8 name_set = 0;
3695     u8 * tap_name;
3696     u32 sw_if_index = ~0;
3697     u8 sw_if_index_set = 0;
3698
3699     memset (mac_address, 0, sizeof (mac_address));
3700     
3701     /* Parse args required to build the message */
3702     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3703         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3704             sw_if_index_set = 1;
3705         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3706             sw_if_index_set = 1;
3707         else if (unformat (i, "mac %U", unformat_ethernet_address, mac_address)) {
3708             random_mac = 0;
3709         }
3710         else if (unformat (i, "random-mac"))
3711             random_mac = 1;
3712         else if (unformat (i, "tapname %s", &tap_name))
3713             name_set = 1;
3714         else
3715             break;
3716     }
3717
3718     if (sw_if_index_set == 0) {
3719         errmsg ("missing vpp interface name");
3720         return -99;
3721     }
3722     if (name_set == 0) {
3723         errmsg ("missing tap name\n");
3724         return -99;
3725     }
3726     if (vec_len (tap_name) > 63) {
3727         errmsg ("tap name too long\n");
3728     }
3729     vec_add1 (tap_name, 0);
3730         
3731     /* Construct the API message */
3732     M(TAP_MODIFY, tap_modify);
3733
3734     mp->use_random_mac = random_mac;
3735     mp->sw_if_index = ntohl(sw_if_index);
3736     clib_memcpy (mp->mac_address, mac_address, 6);
3737     clib_memcpy (mp->tap_name, tap_name, vec_len (tap_name));
3738     vec_free (tap_name);
3739
3740     /* send it... */
3741     S;
3742
3743     /* Wait for a reply... */
3744     W;
3745 }
3746
3747 static int api_tap_delete (vat_main_t * vam)
3748 {
3749     unformat_input_t * i = vam->input;
3750     vl_api_tap_delete_t *mp;
3751     f64 timeout;
3752     u32 sw_if_index = ~0;
3753     u8 sw_if_index_set = 0;
3754
3755     /* Parse args required to build the message */
3756     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3757         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3758             sw_if_index_set = 1;
3759         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3760             sw_if_index_set = 1;
3761         else
3762             break;
3763     }
3764
3765     if (sw_if_index_set == 0) {
3766         errmsg ("missing vpp interface name");
3767         return -99;
3768     }
3769         
3770     /* Construct the API message */
3771     M(TAP_DELETE, tap_delete);
3772
3773     mp->sw_if_index = ntohl(sw_if_index);
3774
3775     /* send it... */
3776     S;
3777
3778     /* Wait for a reply... */
3779     W;
3780 }
3781
3782 static int api_ip_add_del_route (vat_main_t * vam)
3783 {
3784     unformat_input_t * i = vam->input;
3785     vl_api_ip_add_del_route_t *mp;
3786     f64 timeout;
3787     u32 sw_if_index = 0, vrf_id = 0;
3788     u8 sw_if_index_set = 0;
3789     u8 is_ipv6 = 0;
3790     u8 is_local = 0, is_drop = 0;
3791     u8 create_vrf_if_needed = 0;
3792     u8 is_add = 1;
3793     u8 next_hop_weight = 1;
3794     u8 not_last = 0;
3795     u8 is_multipath = 0;
3796     u8 address_set = 0;
3797     u8 address_length_set = 0;
3798     u32 lookup_in_vrf = 0;
3799     u32 resolve_attempts = 0;
3800     u32 dst_address_length = 0;
3801     u8 next_hop_set = 0;
3802     ip4_address_t v4_dst_address, v4_next_hop_address;
3803     ip6_address_t v6_dst_address, v6_next_hop_address;
3804     int count = 1;
3805     int j;
3806     f64 before = 0;
3807     u32 random_add_del = 0;
3808     u32 * random_vector = 0;
3809     uword * random_hash;
3810     u32 random_seed = 0xdeaddabe;
3811     u32 classify_table_index = ~0;
3812     u8 is_classify = 0;
3813     
3814     /* Parse args required to build the message */
3815     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
3816         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
3817             sw_if_index_set = 1;
3818         else if (unformat (i, "sw_if_index %d", &sw_if_index))
3819             sw_if_index_set = 1;
3820         else if (unformat (i, "%U", unformat_ip4_address,
3821                            &v4_dst_address)) {
3822             address_set = 1;
3823             is_ipv6 = 0;
3824         }
3825         else if (unformat (i, "%U", unformat_ip6_address, &v6_dst_address)) {
3826             address_set = 1;
3827             is_ipv6 = 1;
3828         }
3829         else if (unformat (i, "/%d", &dst_address_length)) {
3830             address_length_set = 1;
3831         }
3832         
3833         else if (is_ipv6 == 0 && unformat (i, "via %U", unformat_ip4_address, 
3834                                            &v4_next_hop_address)) {
3835             next_hop_set = 1;
3836         }
3837         else if (is_ipv6 == 1 && unformat (i, "via %U", unformat_ip6_address, 
3838                                            &v6_next_hop_address)) {
3839             next_hop_set = 1;
3840         }
3841         else if (unformat (i, "resolve-attempts %d", &resolve_attempts))
3842             ;
3843         else if (unformat (i, "weight %d", &next_hop_weight))
3844             ;
3845         else if (unformat (i, "drop")) {
3846             is_drop = 1;
3847         } else if (unformat (i, "local")) {
3848             is_local = 1;
3849         } else if (unformat (i, "classify %d", &classify_table_index)) {
3850             is_classify = 1;
3851         } else if (unformat (i, "del"))
3852             is_add = 0;
3853         else if (unformat (i, "add"))
3854             is_add = 1;
3855         else if (unformat (i, "not-last"))
3856             not_last = 1;
3857         else if (unformat (i, "multipath"))
3858             is_multipath = 1;
3859         else if (unformat (i, "vrf %d", &vrf_id))
3860             ;
3861         else if (unformat (i, "create-vrf"))
3862             create_vrf_if_needed = 1;
3863         else if (unformat (i, "count %d", &count))
3864             ;
3865         else if (unformat (i, "lookup-in-vrf %d", &lookup_in_vrf))
3866             ;
3867         else if (unformat (i, "random"))
3868             random_add_del = 1;
3869         else if (unformat (i, "seed %d", &random_seed))
3870             ;
3871         else {
3872             clib_warning ("parse error '%U'", format_unformat_error, i);
3873             return -99;
3874         }
3875     }
3876
3877     if (resolve_attempts > 0 && sw_if_index_set == 0) {
3878         errmsg ("ARP resolution needs explicit interface or sw_if_index\n");
3879         return -99;
3880     }
3881     
3882     if (!next_hop_set && !is_drop && !is_local && !is_classify) {
3883         errmsg ("next hop / local / drop / classify not set\n");
3884         return -99;
3885     }
3886
3887     if (address_set == 0) {
3888         errmsg ("missing addresses\n");
3889         return -99;
3890     }
3891
3892     if (address_length_set == 0) {
3893         errmsg ("missing address length\n");
3894         return -99;
3895     }
3896     
3897     /* Generate a pile of unique, random routes */
3898     if (random_add_del) {
3899         u32 this_random_address;
3900         random_hash = hash_create (count, sizeof(uword));
3901
3902         hash_set (random_hash, v4_next_hop_address.as_u32, 1);
3903         for (j = 0; j <= count; j++) {
3904             do {
3905                 this_random_address = random_u32 (&random_seed);
3906                 this_random_address = 
3907                     clib_host_to_net_u32 (this_random_address);
3908             } while (hash_get (random_hash, this_random_address));
3909             vec_add1 (random_vector, this_random_address);
3910             hash_set (random_hash, this_random_address, 1);
3911         }
3912         hash_free (random_hash);
3913         v4_dst_address.as_u32 = random_vector[0];
3914     }
3915
3916     if (count > 1) {
3917         /* Turn on async mode */
3918         vam->async_mode = 1;
3919         vam->async_errors = 0;
3920         before = vat_time_now(vam);
3921     }
3922
3923     for (j = 0; j < count; j++) {
3924         /* Construct the API message */
3925         M(IP_ADD_DEL_ROUTE, ip_add_del_route);
3926     
3927         mp->next_hop_sw_if_index = ntohl (sw_if_index);
3928         mp->vrf_id = ntohl (vrf_id);
3929         if (resolve_attempts > 0) {
3930             mp->resolve_attempts = ntohl (resolve_attempts);
3931             mp->resolve_if_needed = 1;
3932         }
3933         mp->create_vrf_if_needed = create_vrf_if_needed;
3934     
3935         mp->is_add = is_add;
3936         mp->is_drop = is_drop;
3937         mp->is_ipv6 = is_ipv6;
3938         mp->is_local = is_local;
3939         mp->is_classify = is_classify;
3940         mp->is_multipath = is_multipath;
3941         mp->not_last = not_last;
3942         mp->next_hop_weight = next_hop_weight;
3943         mp->dst_address_length = dst_address_length;
3944         mp->lookup_in_vrf = ntohl(lookup_in_vrf);
3945         mp->classify_table_index = ntohl(classify_table_index);
3946
3947         if (is_ipv6){
3948             clib_memcpy (mp->dst_address, &v6_dst_address, sizeof (v6_dst_address));
3949             if (next_hop_set)
3950                 clib_memcpy (mp->next_hop_address, &v6_next_hop_address, 
3951                         sizeof (v6_next_hop_address));
3952             increment_v6_address (&v6_dst_address);
3953         } else {
3954             clib_memcpy (mp->dst_address, &v4_dst_address, sizeof (v4_dst_address));
3955             if (next_hop_set)
3956                 clib_memcpy (mp->next_hop_address, &v4_next_hop_address, 
3957                         sizeof (v4_next_hop_address));
3958             if (random_add_del)
3959                 v4_dst_address.as_u32 = random_vector[j+1];
3960             else
3961                 increment_v4_address (&v4_dst_address);
3962         }
3963         /* send it... */
3964         S;
3965     }
3966
3967     /* When testing multiple add/del ops, use a control-ping to sync */
3968     if (count > 1) {
3969         vl_api_control_ping_t * mp;
3970         f64 after;
3971
3972         /* Shut off async mode */
3973         vam->async_mode = 0;
3974
3975         M(CONTROL_PING, control_ping);
3976         S;
3977
3978         timeout = vat_time_now(vam) + 1.0;
3979         while (vat_time_now (vam) < timeout)
3980             if (vam->result_ready == 1)
3981                 goto out;
3982         vam->retval = -99;
3983
3984     out:
3985         if (vam->retval == -99)
3986             errmsg ("timeout\n");
3987
3988         if (vam->async_errors > 0) {
3989             errmsg ("%d asynchronous errors\n", vam->async_errors);
3990             vam->retval = -98;
3991         }
3992         vam->async_errors = 0;
3993         after = vat_time_now(vam);
3994
3995         fformat(vam->ofp, "%d routes in %.6f secs, %.2f routes/sec\n",
3996                 count, after - before, count / (after - before));
3997     } else {
3998         /* Wait for a reply... */
3999         W;
4000     }
4001
4002     /* Return the good/bad news */
4003     return (vam->retval);
4004 }
4005
4006 static int api_proxy_arp_add_del (vat_main_t * vam)
4007 {
4008     unformat_input_t * i = vam->input;
4009     vl_api_proxy_arp_add_del_t *mp;
4010     f64 timeout;
4011     u32 vrf_id = 0;
4012     u8 is_add = 1;
4013     ip4_address_t lo, hi;
4014     u8 range_set = 0;
4015
4016     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4017         if (unformat (i, "vrf %d", &vrf_id))
4018             ;
4019         else if (unformat (i, "%U - %U", unformat_ip4_address, &lo, 
4020                            unformat_ip4_address, &hi))
4021             range_set = 1;
4022         else if (unformat (i, "del"))
4023             is_add = 0;
4024         else {
4025             clib_warning ("parse error '%U'", format_unformat_error, i);
4026             return -99;
4027         }
4028     }
4029     
4030     if (range_set == 0) {
4031         errmsg ("address range not set\n");
4032         return -99;
4033     }
4034
4035     M(PROXY_ARP_ADD_DEL, proxy_arp_add_del);
4036
4037     mp->vrf_id = ntohl(vrf_id);
4038     mp->is_add = is_add;
4039     clib_memcpy(mp->low_address, &lo, sizeof (mp->low_address));
4040     clib_memcpy(mp->hi_address, &hi, sizeof (mp->hi_address));
4041
4042     S; W;
4043     /* NOTREACHED */
4044     return 0;
4045 }
4046
4047 static int api_proxy_arp_intfc_enable_disable (vat_main_t * vam)
4048 {
4049     unformat_input_t * i = vam->input;
4050     vl_api_proxy_arp_intfc_enable_disable_t *mp;
4051     f64 timeout;
4052     u32 sw_if_index;
4053     u8 enable = 1;
4054     u8 sw_if_index_set = 0;
4055
4056     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4057         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
4058             sw_if_index_set = 1;
4059         else if (unformat (i, "sw_if_index %d", &sw_if_index))
4060             sw_if_index_set = 1;
4061         else if (unformat (i, "enable"))
4062             enable = 1;
4063         else if (unformat (i, "disable"))
4064             enable = 0;
4065         else {
4066             clib_warning ("parse error '%U'", format_unformat_error, i);
4067             return -99;
4068         }
4069     }
4070     
4071     if (sw_if_index_set == 0) {
4072         errmsg ("missing interface name or sw_if_index\n");
4073         return -99;
4074     }
4075
4076     M(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable);
4077
4078     mp->sw_if_index = ntohl(sw_if_index);
4079     mp->enable_disable = enable;
4080
4081     S; W;
4082     /* NOTREACHED */
4083     return 0;
4084 }
4085
4086 static int api_mpls_add_del_decap (vat_main_t * vam)
4087 {
4088     unformat_input_t * i = vam->input;
4089     vl_api_mpls_add_del_decap_t *mp;
4090     f64 timeout;
4091     u32 rx_vrf_id = 0;
4092     u32 tx_vrf_id = 0;
4093     u32 label = 0;
4094     u8 is_add = 1;
4095     u8 s_bit = 1;
4096     u32 next_index = 1;
4097
4098     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4099         if (unformat (i, "rx_vrf_id %d", &rx_vrf_id))
4100             ;
4101         else if (unformat (i, "tx_vrf_id %d", &tx_vrf_id))
4102             ;
4103         else if (unformat (i, "label %d", &label))
4104             ;
4105         else if (unformat (i, "next-index %d", &next_index))
4106             ;
4107         else if (unformat (i, "del"))
4108             is_add = 0;
4109         else if (unformat (i, "s-bit-clear"))
4110             s_bit = 0;
4111         else {
4112             clib_warning ("parse error '%U'", format_unformat_error, i);
4113             return -99;
4114         }
4115     }
4116     
4117     M(MPLS_ADD_DEL_DECAP, mpls_add_del_decap);
4118
4119     mp->rx_vrf_id = ntohl(rx_vrf_id);
4120     mp->tx_vrf_id = ntohl(tx_vrf_id);
4121     mp->label = ntohl(label);
4122     mp->next_index = ntohl(next_index);
4123     mp->s_bit = s_bit;
4124     mp->is_add = is_add;
4125
4126     S; W;
4127     /* NOTREACHED */
4128     return 0;
4129 }
4130
4131 static int api_mpls_add_del_encap (vat_main_t * vam)
4132 {
4133     unformat_input_t * i = vam->input;
4134     vl_api_mpls_add_del_encap_t *mp;
4135     f64 timeout;
4136     u32 vrf_id = 0;
4137     u32 *labels = 0;
4138     u32 label;
4139     ip4_address_t dst_address;
4140     u8 is_add = 1;
4141
4142     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4143         if (unformat (i, "vrf %d", &vrf_id))
4144             ;
4145         else if (unformat (i, "label %d", &label))
4146             vec_add1 (labels, ntohl(label));
4147         else if (unformat (i, "dst %U", unformat_ip4_address, &dst_address))
4148             ;
4149         else if (unformat (i, "del"))
4150             is_add = 0;
4151         else {
4152             clib_warning ("parse error '%U'", format_unformat_error, i);
4153             return -99;
4154         }
4155     }
4156
4157     if (vec_len (labels) == 0) {
4158         errmsg ("missing encap label stack\n");
4159         return -99;
4160     }
4161     
4162     M2(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap, 
4163        sizeof (u32) * vec_len (labels));
4164
4165     mp->vrf_id = ntohl(vrf_id);
4166     clib_memcpy(mp->dst_address, &dst_address, sizeof (dst_address));
4167     mp->is_add = is_add;
4168     mp->nlabels = vec_len (labels);
4169     clib_memcpy(mp->labels, labels, sizeof(u32)*mp->nlabels);
4170
4171     vec_free(labels);
4172
4173     S; W;
4174     /* NOTREACHED */
4175     return 0;
4176 }
4177
4178 static int api_mpls_gre_add_del_tunnel (vat_main_t * vam)
4179 {
4180     unformat_input_t * i = vam->input;
4181     vl_api_mpls_gre_add_del_tunnel_t *mp;
4182     f64 timeout;
4183     u32 inner_vrf_id = 0;
4184     u32 outer_vrf_id = 0;
4185     ip4_address_t src_address;
4186     ip4_address_t dst_address;
4187     ip4_address_t intfc_address;
4188     u32 tmp;
4189     u8 intfc_address_length = 0;
4190     u8 is_add = 1;
4191     u8 l2_only = 0;
4192     
4193     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4194         if (unformat (i, "inner_vrf_id %d", &inner_vrf_id))
4195             ;
4196         else if (unformat (i, "outer_vrf_id %d", &outer_vrf_id))
4197             ;
4198         else if (unformat (i, "src %U", unformat_ip4_address, &src_address))
4199             ;
4200         else if (unformat (i, "dst %U", unformat_ip4_address, &dst_address))
4201             ;
4202         else if (unformat (i, "adj %U/%d", unformat_ip4_address,
4203                            &intfc_address, &tmp))
4204             intfc_address_length = tmp;
4205         else if (unformat (i, "l2-only"))
4206             l2_only = 1;
4207         else if (unformat (i, "del"))
4208             is_add = 0;
4209         else {
4210             clib_warning ("parse error '%U'", format_unformat_error, i);
4211             return -99;
4212         }
4213     }
4214     
4215     M(MPLS_GRE_ADD_DEL_TUNNEL, mpls_gre_add_del_tunnel);
4216
4217     mp->inner_vrf_id = ntohl(inner_vrf_id);
4218     mp->outer_vrf_id = ntohl(outer_vrf_id);
4219     clib_memcpy(mp->src_address, &src_address, sizeof (src_address));
4220     clib_memcpy(mp->dst_address, &dst_address, sizeof (dst_address));
4221     clib_memcpy(mp->intfc_address, &intfc_address, sizeof (intfc_address));
4222     mp->intfc_address_length = intfc_address_length;
4223     mp->l2_only = l2_only;
4224     mp->is_add = is_add;
4225
4226     S; W;
4227     /* NOTREACHED */
4228     return 0;
4229 }
4230
4231 static int api_mpls_ethernet_add_del_tunnel (vat_main_t * vam)
4232 {
4233     unformat_input_t * i = vam->input;
4234     vl_api_mpls_ethernet_add_del_tunnel_t *mp;
4235     f64 timeout;
4236     u32 inner_vrf_id = 0;
4237     ip4_address_t intfc_address;
4238     u8 dst_mac_address[6];
4239     int dst_set = 1;
4240     u32 tmp;
4241     u8 intfc_address_length = 0;
4242     u8 is_add = 1;
4243     u8 l2_only = 0;
4244     u32 tx_sw_if_index;
4245     int tx_sw_if_index_set = 0;
4246     
4247     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4248         if (unformat (i, "vrf %d", &inner_vrf_id))
4249             ;
4250         else if (unformat (i, "adj %U/%d", unformat_ip4_address,
4251                            &intfc_address, &tmp))
4252             intfc_address_length = tmp;
4253         else if (unformat (i, "%U", 
4254                            unformat_sw_if_index, vam, &tx_sw_if_index))
4255             tx_sw_if_index_set = 1;
4256         else if (unformat (i, "tx_sw_if_index %d", &tx_sw_if_index))
4257             tx_sw_if_index_set = 1;
4258         else if (unformat (i, "dst %U", unformat_ethernet_address, 
4259                            dst_mac_address))
4260             dst_set = 1;
4261         else if (unformat (i, "l2-only"))
4262             l2_only = 1;
4263         else if (unformat (i, "del"))
4264             is_add = 0;
4265         else {
4266             clib_warning ("parse error '%U'", format_unformat_error, i);
4267             return -99;
4268         }
4269     }
4270
4271     if (!dst_set) {
4272         errmsg ("dst (mac address) not set\n");
4273         return -99;
4274     }
4275     if (!tx_sw_if_index_set) {
4276         errmsg ("tx-intfc not set\n");
4277         return -99;
4278     }
4279     
4280     M(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel);
4281
4282     mp->vrf_id = ntohl(inner_vrf_id);
4283     clib_memcpy (mp->adj_address, &intfc_address, sizeof (intfc_address));
4284     mp->adj_address_length = intfc_address_length;
4285     clib_memcpy (mp->dst_mac_address, dst_mac_address, sizeof (dst_mac_address));
4286     mp->tx_sw_if_index = ntohl(tx_sw_if_index);
4287     mp->l2_only = l2_only;
4288     mp->is_add = is_add;
4289
4290     S; W;
4291     /* NOTREACHED */
4292     return 0;
4293 }
4294
4295 static int api_mpls_ethernet_add_del_tunnel_2 (vat_main_t * vam)
4296 {
4297     unformat_input_t * i = vam->input;
4298     vl_api_mpls_ethernet_add_del_tunnel_2_t *mp;
4299     f64 timeout;
4300     u32 inner_vrf_id = 0;
4301     u32 outer_vrf_id = 0;
4302     ip4_address_t adj_address;
4303     int adj_address_set = 0;
4304     ip4_address_t next_hop_address;
4305     int next_hop_address_set = 0;
4306     u32 tmp;
4307     u8 adj_address_length = 0;
4308     u8 l2_only = 0;
4309     u8 is_add = 1;
4310     u32 resolve_attempts = 5;
4311     u8 resolve_if_needed = 1;
4312     
4313     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4314         if (unformat (i, "inner_vrf_id %d", &inner_vrf_id))
4315             ;
4316         else if (unformat (i, "outer_vrf_id %d", &outer_vrf_id))
4317             ;
4318         else if (unformat (i, "adj %U/%d", unformat_ip4_address,
4319                            &adj_address, &tmp)) {
4320             adj_address_length = tmp;
4321             adj_address_set = 1;
4322         }
4323         else if (unformat (i, "next-hop %U", unformat_ip4_address,
4324                            &next_hop_address))
4325             next_hop_address_set = 1;
4326         else if (unformat (i, "resolve-attempts %d", &resolve_attempts))
4327             ;
4328         else if (unformat (i, "resolve-if-needed %d", &tmp))
4329             resolve_if_needed = tmp;
4330         else if (unformat (i, "l2-only"))
4331             l2_only = 1;
4332         else if (unformat (i, "del"))
4333             is_add = 0;
4334         else {
4335             clib_warning ("parse error '%U'", format_unformat_error, i);
4336             return -99;
4337         }
4338     }
4339     
4340     if (!adj_address_set) {
4341         errmsg ("adjacency address/mask not set\n");
4342         return -99;
4343     }
4344     if (!next_hop_address_set) {
4345         errmsg ("ip4 next hop address (in outer fib) not set\n");
4346         return -99;
4347     }
4348     
4349     M(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2);
4350     
4351     mp->inner_vrf_id = ntohl(inner_vrf_id);
4352     mp->outer_vrf_id = ntohl(outer_vrf_id);
4353     mp->resolve_attempts = ntohl(resolve_attempts);
4354     mp->resolve_if_needed = resolve_if_needed;
4355     mp->is_add = is_add;
4356     mp->l2_only = l2_only;
4357     clib_memcpy (mp->adj_address, &adj_address, sizeof (adj_address));
4358     mp->adj_address_length = adj_address_length;
4359     clib_memcpy (mp->next_hop_ip4_address_in_outer_vrf, &next_hop_address, 
4360             sizeof (next_hop_address));
4361
4362     S; W;
4363     /* NOTREACHED */
4364     return 0;
4365 }
4366
4367 static int api_sw_interface_set_unnumbered (vat_main_t * vam)
4368 {
4369     unformat_input_t * i = vam->input;
4370     vl_api_sw_interface_set_unnumbered_t *mp;
4371     f64 timeout;
4372     u32 sw_if_index;
4373     u32 unnum_sw_index;
4374     u8  is_add = 1;
4375     u8 sw_if_index_set = 0;
4376
4377     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4378         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
4379             sw_if_index_set = 1;
4380         else if (unformat (i, "sw_if_index %d", &sw_if_index))
4381             sw_if_index_set = 1;
4382         else if (unformat (i, "unnum_if_index %d", &unnum_sw_index))
4383             ;
4384         else if (unformat (i, "del"))
4385             is_add = 0;
4386         else {
4387             clib_warning ("parse error '%U'", format_unformat_error, i);
4388             return -99;
4389         }
4390     }
4391     
4392     if (sw_if_index_set == 0) {
4393         errmsg ("missing interface name or sw_if_index\n");
4394         return -99;
4395     }
4396
4397     M(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered);
4398
4399     mp->sw_if_index = ntohl(sw_if_index);
4400     mp->unnumbered_sw_if_index = ntohl(unnum_sw_index);
4401     mp->is_add = is_add;
4402
4403     S; W;
4404     /* NOTREACHED */
4405     return 0;
4406 }
4407
4408 static int api_ip_neighbor_add_del (vat_main_t * vam)
4409 {
4410     unformat_input_t * i = vam->input;
4411     vl_api_ip_neighbor_add_del_t *mp;
4412     f64 timeout;
4413     u32 sw_if_index;
4414     u8 sw_if_index_set = 0;
4415     u32 vrf_id = 0;
4416     u8 is_add = 1;
4417     u8 is_static = 0;
4418     u8 mac_address[6];
4419     u8 mac_set = 0;
4420     u8 v4_address_set = 0;
4421     u8 v6_address_set = 0;
4422     ip4_address_t v4address;
4423     ip6_address_t v6address;
4424     
4425     memset (mac_address, 0, sizeof (mac_address));
4426     
4427     /* Parse args required to build the message */
4428     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4429         if (unformat (i, "mac %U", unformat_ethernet_address, mac_address)) {
4430             mac_set = 1;
4431         }
4432         else if (unformat (i, "del"))
4433             is_add = 0;
4434         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
4435             sw_if_index_set = 1;
4436         else if (unformat (i, "sw_if_index %d", &sw_if_index))
4437             sw_if_index_set = 1;
4438         else if (unformat (i, "is_static"))
4439             is_static = 1;
4440         else if (unformat (i, "vrf %d", &vrf_id))
4441             ;
4442         else if (unformat (i, "dst %U", 
4443                            unformat_ip4_address, &v4address))
4444                 v4_address_set = 1;
4445         else if (unformat (i, "dst %U", 
4446                            unformat_ip6_address, &v6address))
4447                 v6_address_set = 1;
4448         else {
4449             clib_warning ("parse error '%U'", format_unformat_error, i);
4450             return -99;
4451         }
4452     }
4453
4454     if (sw_if_index_set == 0) {
4455         errmsg ("missing interface name or sw_if_index\n");
4456         return -99;
4457     }
4458     if (v4_address_set && v6_address_set) {
4459         errmsg ("both v4 and v6 addresses set\n");
4460         return -99;
4461     }
4462     if (!v4_address_set && !v6_address_set) {
4463         errmsg ("no addresses set\n");
4464         return -99;
4465     }
4466
4467     /* Construct the API message */
4468     M(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del);
4469
4470     mp->sw_if_index = ntohl (sw_if_index);
4471     mp->is_add = is_add;
4472     mp->vrf_id = ntohl (vrf_id);
4473     mp->is_static = is_static;
4474     if (mac_set)
4475         clib_memcpy (mp->mac_address, mac_address, 6);
4476     if (v6_address_set) {
4477         mp->is_ipv6 = 1;
4478         clib_memcpy (mp->dst_address, &v6address, sizeof (v6address));
4479     } else {
4480         /* mp->is_ipv6 = 0; via memset in M macro above */
4481         clib_memcpy (mp->dst_address, &v4address, sizeof (v4address));
4482     }
4483
4484     /* send it... */
4485     S;
4486
4487     /* Wait for a reply, return good/bad news  */
4488     W;
4489
4490     /* NOTREACHED */
4491     return 0;
4492 }
4493
4494 static int api_reset_vrf (vat_main_t * vam)
4495 {
4496     unformat_input_t * i = vam->input;
4497     vl_api_reset_vrf_t *mp;
4498     f64 timeout;
4499     u32 vrf_id = 0;
4500     u8 is_ipv6 = 0;
4501     u8 vrf_id_set = 0;
4502
4503     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4504         if (unformat (i, "vrf %d", &vrf_id))
4505             vrf_id_set = 1;
4506         else if (unformat (i, "ipv6"))
4507             is_ipv6 = 1;
4508         else {
4509             clib_warning ("parse error '%U'", format_unformat_error, i);
4510             return -99;
4511         }
4512     }
4513
4514     if (vrf_id_set == 0) {
4515         errmsg ("missing vrf id\n");
4516         return -99;
4517     }
4518     
4519     M(RESET_VRF, reset_vrf);
4520
4521     mp->vrf_id = ntohl(vrf_id);
4522     mp->is_ipv6 = is_ipv6;
4523
4524     S; W;
4525     /* NOTREACHED */
4526     return 0;
4527 }
4528
4529 static int api_create_vlan_subif (vat_main_t * vam)
4530 {
4531     unformat_input_t * i = vam->input;
4532     vl_api_create_vlan_subif_t *mp;
4533     f64 timeout;
4534     u32 sw_if_index;
4535     u8  sw_if_index_set = 0;
4536     u32 vlan_id;
4537     u8  vlan_id_set = 0;
4538
4539     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4540         if (unformat (i, "sw_if_index %d", &sw_if_index))
4541             sw_if_index_set = 1;
4542         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
4543             sw_if_index_set = 1;
4544         else if (unformat (i, "vlan %d", &vlan_id))
4545             vlan_id_set = 1;
4546         else {
4547             clib_warning ("parse error '%U'", format_unformat_error, i);
4548             return -99;
4549         }
4550     }
4551     
4552     if (sw_if_index_set == 0) {
4553         errmsg ("missing interface name or sw_if_index\n");
4554         return -99;
4555     }
4556
4557     if (vlan_id_set == 0) {
4558         errmsg ("missing vlan_id\n");
4559         return -99;
4560     }
4561     M(CREATE_VLAN_SUBIF, create_vlan_subif);
4562
4563     mp->sw_if_index = ntohl(sw_if_index);
4564     mp->vlan_id = ntohl(vlan_id);
4565
4566     S; W;
4567     /* NOTREACHED */
4568     return 0;
4569 }
4570
4571 #define foreach_create_subif_bit                \
4572 _(no_tags)                                      \
4573 _(one_tag)                                      \
4574 _(two_tags)                                     \
4575 _(dot1ad)                                       \
4576 _(exact_match)                                  \
4577 _(default_sub)                                  \
4578 _(outer_vlan_id_any)                            \
4579 _(inner_vlan_id_any)
4580
4581 static int api_create_subif (vat_main_t * vam)
4582 {
4583     unformat_input_t * i = vam->input;
4584     vl_api_create_subif_t *mp;
4585     f64 timeout;
4586     u32 sw_if_index;
4587     u8  sw_if_index_set = 0;
4588     u32 sub_id;
4589     u8  sub_id_set = 0;
4590     u32 no_tags = 0;
4591     u32 one_tag = 0;
4592     u32 two_tags = 0;
4593     u32 dot1ad = 0;
4594     u32 exact_match = 0;
4595     u32 default_sub = 0;
4596     u32 outer_vlan_id_any = 0;
4597     u32 inner_vlan_id_any = 0;
4598     u32 tmp;
4599     u16 outer_vlan_id = 0;
4600     u16 inner_vlan_id = 0;
4601
4602     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4603         if (unformat (i, "sw_if_index %d", &sw_if_index))
4604             sw_if_index_set = 1;
4605         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
4606             sw_if_index_set = 1;
4607         else if (unformat (i, "sub_id %d", &sub_id))
4608             sub_id_set = 1;
4609         else if (unformat (i, "outer_vlan_id %d", &tmp))
4610             outer_vlan_id = tmp;
4611         else if (unformat (i, "inner_vlan_id %d", &tmp))
4612             inner_vlan_id = tmp;
4613
4614 #define _(a) else if (unformat (i, #a)) a = 1 ;
4615         foreach_create_subif_bit
4616 #undef _
4617
4618         else {
4619             clib_warning ("parse error '%U'", format_unformat_error, i);
4620             return -99;
4621         }
4622     }
4623     
4624     if (sw_if_index_set == 0) {
4625         errmsg ("missing interface name or sw_if_index\n");
4626         return -99;
4627     }
4628
4629     if (sub_id_set == 0) {
4630         errmsg ("missing sub_id\n");
4631         return -99;
4632     }
4633     M(CREATE_SUBIF, create_subif);
4634
4635     mp->sw_if_index = ntohl(sw_if_index);
4636     mp->sub_id = ntohl(sub_id);
4637     
4638 #define _(a) mp->a = a;
4639     foreach_create_subif_bit;
4640 #undef _
4641         
4642     mp->outer_vlan_id = ntohs (outer_vlan_id);
4643     mp->inner_vlan_id = ntohs (inner_vlan_id);
4644
4645     S; W;
4646     /* NOTREACHED */
4647     return 0;
4648 }
4649
4650 static int api_oam_add_del (vat_main_t * vam)
4651 {
4652     unformat_input_t * i = vam->input;
4653     vl_api_oam_add_del_t *mp;
4654     f64 timeout;
4655     u32 vrf_id = 0;
4656     u8 is_add = 1;
4657     ip4_address_t src, dst;
4658     u8 src_set = 0;
4659     u8 dst_set = 0;
4660     
4661     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4662         if (unformat (i, "vrf %d", &vrf_id))
4663             ;
4664         else if (unformat (i, "src %U", unformat_ip4_address, &src))
4665             src_set = 1;
4666         else if (unformat (i, "dst %U", unformat_ip4_address, &dst))
4667             dst_set = 1;
4668         else if (unformat (i, "del"))
4669             is_add = 0;
4670         else {
4671             clib_warning ("parse error '%U'", format_unformat_error, i);
4672             return -99;
4673         }
4674     }
4675     
4676     if (src_set == 0) {
4677         errmsg ("missing src addr\n");
4678         return -99;
4679     }
4680
4681     if (dst_set == 0) {
4682         errmsg ("missing dst addr\n");
4683         return -99;
4684     }
4685
4686     M(OAM_ADD_DEL, oam_add_del);
4687
4688     mp->vrf_id = ntohl(vrf_id);
4689     mp->is_add = is_add;
4690     clib_memcpy(mp->src_address, &src, sizeof (mp->src_address));
4691     clib_memcpy(mp->dst_address, &dst, sizeof (mp->dst_address));
4692
4693     S; W;
4694     /* NOTREACHED */
4695     return 0;
4696 }
4697
4698 static int api_reset_fib (vat_main_t * vam)
4699 {
4700     unformat_input_t * i = vam->input;
4701     vl_api_reset_fib_t *mp;
4702     f64 timeout;
4703     u32 vrf_id = 0;
4704     u8 is_ipv6 = 0;
4705     u8 vrf_id_set = 0;
4706
4707     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4708         if (unformat (i, "vrf %d", &vrf_id))
4709             vrf_id_set = 1;
4710         else if (unformat (i, "ipv6"))
4711             is_ipv6 = 1;
4712         else {
4713             clib_warning ("parse error '%U'", format_unformat_error, i);
4714             return -99;
4715         }
4716     }
4717
4718     if (vrf_id_set == 0) {
4719         errmsg ("missing vrf id\n");
4720         return -99;
4721     }
4722     
4723     M(RESET_FIB, reset_fib);
4724
4725     mp->vrf_id = ntohl(vrf_id);
4726     mp->is_ipv6 = is_ipv6;
4727
4728     S; W;
4729     /* NOTREACHED */
4730     return 0;
4731 }
4732
4733 static int api_dhcp_proxy_config (vat_main_t * vam)
4734 {
4735     unformat_input_t * i = vam->input;
4736     vl_api_dhcp_proxy_config_t *mp;
4737     f64 timeout;
4738     u32 vrf_id = 0;
4739     u8 is_add = 1;
4740     u8 insert_cid = 1;
4741     u8 v4_address_set = 0;
4742     u8 v6_address_set = 0;
4743     ip4_address_t v4address;
4744     ip6_address_t v6address;
4745     u8 v4_src_address_set = 0;
4746     u8 v6_src_address_set = 0;
4747     ip4_address_t v4srcaddress;
4748     ip6_address_t v6srcaddress;
4749     
4750     /* Parse args required to build the message */
4751     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4752         if (unformat (i, "del"))
4753             is_add = 0;
4754         else if (unformat (i, "vrf %d", &vrf_id))
4755             ;
4756         else if (unformat (i, "insert-cid %d", &insert_cid))
4757             ;
4758         else if (unformat (i, "svr %U", 
4759                            unformat_ip4_address, &v4address))
4760                 v4_address_set = 1;
4761         else if (unformat (i, "svr %U", 
4762                            unformat_ip6_address, &v6address))
4763                 v6_address_set = 1;
4764         else if (unformat (i, "src %U", 
4765                            unformat_ip4_address, &v4srcaddress))
4766                 v4_src_address_set = 1;
4767         else if (unformat (i, "src %U", 
4768                            unformat_ip6_address, &v6srcaddress))
4769                 v6_src_address_set = 1;
4770         else
4771             break;
4772     }
4773
4774     if (v4_address_set && v6_address_set) {
4775         errmsg ("both v4 and v6 server addresses set\n");
4776         return -99;
4777     }
4778     if (!v4_address_set && !v6_address_set) {
4779         errmsg ("no server addresses set\n");
4780         return -99;
4781     }
4782
4783     if (v4_src_address_set && v6_src_address_set) {
4784         errmsg ("both v4 and v6  src addresses set\n");
4785         return -99;
4786     }
4787     if (!v4_src_address_set && !v6_src_address_set) {
4788         errmsg ("no src addresses set\n");
4789         return -99;
4790     }
4791
4792     if (!(v4_src_address_set && v4_address_set) &&
4793         !(v6_src_address_set && v6_address_set)) {
4794         errmsg ("no matching server and src addresses set\n");
4795         return -99;
4796     }
4797
4798     /* Construct the API message */
4799     M(DHCP_PROXY_CONFIG, dhcp_proxy_config);
4800
4801     mp->insert_circuit_id = insert_cid;
4802     mp->is_add = is_add;
4803     mp->vrf_id = ntohl (vrf_id);
4804     if (v6_address_set) {
4805         mp->is_ipv6 = 1;
4806         clib_memcpy (mp->dhcp_server, &v6address, sizeof (v6address));
4807         clib_memcpy (mp->dhcp_src_address, &v6srcaddress, sizeof (v6address));
4808     } else {
4809         clib_memcpy (mp->dhcp_server, &v4address, sizeof (v4address));
4810         clib_memcpy (mp->dhcp_src_address, &v4srcaddress, sizeof (v4address));
4811     }
4812
4813     /* send it... */
4814     S;
4815
4816     /* Wait for a reply, return good/bad news  */
4817     W;
4818     /* NOTREACHED */
4819     return 0;
4820 }
4821
4822 static int api_dhcp_proxy_config_2 (vat_main_t * vam)
4823 {
4824     unformat_input_t * i = vam->input;
4825     vl_api_dhcp_proxy_config_2_t *mp;
4826     f64 timeout;
4827     u32 rx_vrf_id = 0;
4828     u32 server_vrf_id = 0;
4829     u8 is_add = 1;
4830     u8 insert_cid = 1;
4831     u8 v4_address_set = 0;
4832     u8 v6_address_set = 0;
4833     ip4_address_t v4address;
4834     ip6_address_t v6address;
4835     u8 v4_src_address_set = 0;
4836     u8 v6_src_address_set = 0;
4837     ip4_address_t v4srcaddress;
4838     ip6_address_t v6srcaddress;
4839     
4840     /* Parse args required to build the message */
4841     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4842         if (unformat (i, "del"))
4843             is_add = 0;
4844         else if (unformat (i, "rx_vrf_id %d", &rx_vrf_id))
4845             ;
4846         else if (unformat (i, "server_vrf_id %d", &server_vrf_id))
4847             ;
4848         else if (unformat (i, "insert-cid %d", &insert_cid))
4849             ;
4850         else if (unformat (i, "svr %U", 
4851                            unformat_ip4_address, &v4address))
4852                 v4_address_set = 1;
4853         else if (unformat (i, "svr %U", 
4854                            unformat_ip6_address, &v6address))
4855                 v6_address_set = 1;
4856         else if (unformat (i, "src %U", 
4857                            unformat_ip4_address, &v4srcaddress))
4858                 v4_src_address_set = 1;
4859         else if (unformat (i, "src %U", 
4860                            unformat_ip6_address, &v6srcaddress))
4861                 v6_src_address_set = 1;
4862         else
4863             break;
4864     }
4865
4866     if (v4_address_set && v6_address_set) {
4867         errmsg ("both v4 and v6 server addresses set\n");
4868         return -99;
4869     }
4870     if (!v4_address_set && !v6_address_set) {
4871         errmsg ("no server addresses set\n");
4872         return -99;
4873     }
4874
4875     if (v4_src_address_set && v6_src_address_set) {
4876         errmsg ("both v4 and v6  src addresses set\n");
4877         return -99;
4878     }
4879     if (!v4_src_address_set && !v6_src_address_set) {
4880         errmsg ("no src addresses set\n");
4881         return -99;
4882     }
4883
4884     if (!(v4_src_address_set && v4_address_set) &&
4885         !(v6_src_address_set && v6_address_set)) {
4886         errmsg ("no matching server and src addresses set\n");
4887         return -99;
4888     }
4889
4890     /* Construct the API message */
4891     M(DHCP_PROXY_CONFIG_2, dhcp_proxy_config_2);
4892
4893     mp->insert_circuit_id = insert_cid;
4894     mp->is_add = is_add;
4895     mp->rx_vrf_id = ntohl (rx_vrf_id);
4896     mp->server_vrf_id = ntohl (server_vrf_id);
4897     if (v6_address_set) {
4898         mp->is_ipv6 = 1;
4899         clib_memcpy (mp->dhcp_server, &v6address, sizeof (v6address));
4900         clib_memcpy (mp->dhcp_src_address, &v6srcaddress, sizeof (v6address));
4901     } else {
4902         clib_memcpy (mp->dhcp_server, &v4address, sizeof (v4address));
4903         clib_memcpy (mp->dhcp_src_address, &v4srcaddress, sizeof (v4address));
4904     }
4905
4906     /* send it... */
4907     S;
4908
4909     /* Wait for a reply, return good/bad news  */
4910     W;
4911     /* NOTREACHED */
4912     return 0;
4913 }
4914
4915 static int api_dhcp_proxy_set_vss (vat_main_t * vam)
4916 {
4917     unformat_input_t * i = vam->input;
4918     vl_api_dhcp_proxy_set_vss_t *mp;
4919     f64 timeout;
4920     u8  is_ipv6 = 0;
4921     u8  is_add = 1;
4922     u32 tbl_id;
4923     u8  tbl_id_set = 0;
4924     u32 oui;
4925     u8  oui_set = 0;
4926     u32 fib_id;
4927     u8  fib_id_set = 0;
4928
4929     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4930         if (unformat (i, "tbl_id %d", &tbl_id))
4931             tbl_id_set = 1;
4932         if (unformat (i, "fib_id %d", &fib_id))
4933             fib_id_set = 1;
4934         if (unformat (i, "oui %d", &oui))
4935             oui_set = 1;
4936         else if (unformat (i, "ipv6"))
4937             is_ipv6 = 1;
4938         else if (unformat (i, "del"))
4939             is_add = 0;
4940         else {
4941             clib_warning ("parse error '%U'", format_unformat_error, i);
4942             return -99;
4943         }
4944     }
4945
4946     if (tbl_id_set == 0) {
4947         errmsg ("missing tbl id\n");
4948         return -99;
4949     }
4950
4951     if (fib_id_set == 0) {
4952         errmsg ("missing fib id\n");
4953         return -99;
4954     }
4955     if (oui_set == 0) {
4956         errmsg ("missing oui\n");
4957         return -99;
4958     }
4959     
4960     M(DHCP_PROXY_SET_VSS, dhcp_proxy_set_vss);
4961     mp->tbl_id = ntohl(tbl_id);
4962     mp->fib_id = ntohl(fib_id);
4963     mp->oui = ntohl(oui);
4964     mp->is_ipv6 = is_ipv6;
4965     mp->is_add = is_add;
4966
4967     S; W;
4968     /* NOTREACHED */
4969     return 0;
4970 }
4971
4972 static int api_dhcp_client_config (vat_main_t * vam)
4973 {
4974     unformat_input_t * i = vam->input;
4975     vl_api_dhcp_client_config_t *mp;
4976     f64 timeout;
4977     u32 sw_if_index;
4978     u8 sw_if_index_set = 0;
4979     u8 is_add = 1;
4980     u8 * hostname = 0;
4981     u8 disable_event = 0;
4982
4983     /* Parse args required to build the message */
4984     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
4985         if (unformat (i, "del"))
4986             is_add = 0;
4987         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
4988             sw_if_index_set = 1;
4989         else if (unformat (i, "sw_if_index %d", &sw_if_index))
4990             sw_if_index_set = 1;
4991         else if (unformat (i, "hostname %s", &hostname))
4992             ;
4993         else if (unformat (i, "disable_event"))
4994             disable_event = 1;
4995         else
4996             break;
4997     }
4998
4999     if (sw_if_index_set == 0) {
5000         errmsg ("missing interface name or sw_if_index\n");
5001         return -99;
5002     }
5003
5004     if (vec_len (hostname) > 63) {
5005         errmsg ("hostname too long\n");
5006     }
5007     vec_add1 (hostname, 0);
5008
5009     /* Construct the API message */
5010     M(DHCP_CLIENT_CONFIG, dhcp_client_config);
5011
5012     mp->sw_if_index = ntohl (sw_if_index);
5013     clib_memcpy (mp->hostname, hostname, vec_len (hostname));
5014     vec_free (hostname);
5015     mp->is_add = is_add;
5016     mp->want_dhcp_event = disable_event ? 0 : 1;
5017     mp->pid = getpid();
5018    
5019     /* send it... */
5020     S;
5021
5022     /* Wait for a reply, return good/bad news  */
5023     W;
5024     /* NOTREACHED */
5025     return 0;
5026 }
5027
5028 static int api_set_ip_flow_hash (vat_main_t * vam)
5029 {
5030     unformat_input_t * i = vam->input;
5031     vl_api_set_ip_flow_hash_t *mp;
5032     f64 timeout;
5033     u32 vrf_id = 0;
5034     u8 is_ipv6 = 0;
5035     u8 vrf_id_set = 0;
5036     u8 src = 0;
5037     u8 dst = 0;
5038     u8 sport = 0;
5039     u8 dport = 0;
5040     u8 proto = 0;
5041     u8 reverse = 0;
5042
5043     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5044         if (unformat (i, "vrf %d", &vrf_id))
5045             vrf_id_set = 1;
5046         else if (unformat (i, "ipv6"))
5047             is_ipv6 = 1;
5048         else if (unformat (i, "src"))
5049             src = 1;
5050         else if (unformat (i, "dst"))
5051             dst = 1;
5052         else if (unformat (i, "sport"))
5053             sport = 1;
5054         else if (unformat (i, "dport"))
5055             dport = 1;
5056         else if (unformat (i, "proto"))
5057             proto = 1;
5058         else if (unformat (i, "reverse"))
5059             reverse = 1;
5060
5061         else {
5062             clib_warning ("parse error '%U'", format_unformat_error, i);
5063             return -99;
5064         }
5065     }
5066
5067     if (vrf_id_set == 0) {
5068         errmsg ("missing vrf id\n");
5069         return -99;
5070     }
5071     
5072     M(SET_IP_FLOW_HASH, set_ip_flow_hash);
5073     mp->src = src;
5074     mp->dst = dst;
5075     mp->sport = sport;
5076     mp->dport = dport;
5077     mp->proto = proto;
5078     mp->reverse = reverse;
5079     mp->vrf_id = ntohl(vrf_id);
5080     mp->is_ipv6 = is_ipv6;
5081
5082     S; W;
5083     /* NOTREACHED */
5084     return 0;
5085 }
5086
5087 static int api_sw_interface_ip6_enable_disable (vat_main_t * vam)
5088 {
5089     unformat_input_t * i = vam->input;
5090     vl_api_sw_interface_ip6_enable_disable_t *mp;
5091     f64 timeout;
5092     u32 sw_if_index;
5093     u8  sw_if_index_set = 0;
5094     u8  enable = 0;
5095
5096     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5097         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
5098             sw_if_index_set = 1;
5099         else if (unformat (i, "sw_if_index %d", &sw_if_index))
5100             sw_if_index_set = 1;
5101         else if (unformat (i, "enable"))
5102             enable = 1;
5103         else if (unformat (i, "disable"))
5104             enable = 0;
5105         else {
5106             clib_warning ("parse error '%U'", format_unformat_error, i);
5107             return -99;
5108         }
5109     }
5110
5111     if (sw_if_index_set == 0) {
5112         errmsg ("missing interface name or sw_if_index\n");
5113         return -99;
5114     }
5115     
5116     M(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable);
5117
5118     mp->sw_if_index = ntohl(sw_if_index);
5119     mp->enable = enable;
5120
5121     S; W;
5122     /* NOTREACHED */
5123     return 0;
5124 }
5125
5126 static int api_sw_interface_ip6_set_link_local_address (vat_main_t * vam)
5127 {
5128     unformat_input_t * i = vam->input;
5129     vl_api_sw_interface_ip6_set_link_local_address_t *mp;
5130     f64 timeout;
5131     u32 sw_if_index;
5132     u8 sw_if_index_set = 0;
5133     u32 address_length = 0;
5134     u8 v6_address_set = 0;
5135     ip6_address_t v6address;
5136     
5137     /* Parse args required to build the message */
5138     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5139         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
5140             sw_if_index_set = 1;
5141         else if (unformat (i, "sw_if_index %d", &sw_if_index))
5142             sw_if_index_set = 1;
5143         else if (unformat (i, "%U/%d", 
5144                            unformat_ip6_address, &v6address, 
5145                            &address_length))
5146             v6_address_set = 1;
5147         else
5148             break;
5149     }
5150
5151     if (sw_if_index_set == 0) {
5152         errmsg ("missing interface name or sw_if_index\n");
5153         return -99;
5154     }
5155     if (!v6_address_set) {
5156         errmsg ("no address set\n");
5157         return -99;
5158     }
5159
5160     /* Construct the API message */
5161     M(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
5162       sw_interface_ip6_set_link_local_address);
5163
5164     mp->sw_if_index = ntohl (sw_if_index);
5165     clib_memcpy (mp->address, &v6address, sizeof (v6address));
5166     mp->address_length = address_length;
5167
5168     /* send it... */
5169     S;
5170
5171     /* Wait for a reply, return good/bad news  */
5172     W;
5173
5174     /* NOTREACHED */
5175     return 0;
5176 }
5177
5178
5179 static int api_sw_interface_ip6nd_ra_prefix (vat_main_t * vam)
5180 {
5181     unformat_input_t * i = vam->input;
5182     vl_api_sw_interface_ip6nd_ra_prefix_t *mp;
5183     f64 timeout;
5184     u32 sw_if_index;
5185     u8 sw_if_index_set = 0;
5186     u32 address_length = 0;
5187     u8 v6_address_set = 0;
5188     ip6_address_t v6address;
5189     u8 use_default = 0;
5190     u8 no_advertise = 0;
5191     u8 off_link = 0;
5192     u8 no_autoconfig = 0;
5193     u8 no_onlink = 0;
5194     u8 is_no = 0;
5195     u32 val_lifetime = 0;
5196     u32 pref_lifetime = 0;
5197     
5198     /* Parse args required to build the message */
5199     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5200         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
5201             sw_if_index_set = 1;
5202         else if (unformat (i, "sw_if_index %d", &sw_if_index))
5203             sw_if_index_set = 1;
5204         else if (unformat (i, "%U/%d", 
5205                            unformat_ip6_address, &v6address, 
5206                            &address_length))
5207             v6_address_set = 1;
5208         else if (unformat (i, "val_life %d", &val_lifetime))
5209             ;
5210         else if (unformat (i, "pref_life %d", &pref_lifetime))
5211             ;
5212         else if (unformat (i, "def"))
5213             use_default = 1;
5214         else if (unformat (i, "noadv"))
5215             no_advertise = 1;
5216         else if (unformat (i, "offl"))
5217             off_link = 1;
5218         else if (unformat (i, "noauto"))
5219             no_autoconfig = 1;
5220         else if (unformat (i, "nolink"))
5221             no_onlink = 1;
5222         else if (unformat (i, "isno"))
5223             is_no = 1;
5224         else {
5225             clib_warning ("parse error '%U'", format_unformat_error, i);
5226             return -99;
5227         }        
5228     }
5229
5230     if (sw_if_index_set == 0) {
5231         errmsg ("missing interface name or sw_if_index\n");
5232         return -99;
5233     }
5234     if (!v6_address_set) {
5235         errmsg ("no address set\n");
5236         return -99;
5237     }
5238
5239     /* Construct the API message */
5240     M(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix);
5241
5242     mp->sw_if_index = ntohl (sw_if_index);
5243     clib_memcpy (mp->address, &v6address, sizeof (v6address));
5244     mp->address_length = address_length;
5245     mp->use_default = use_default;
5246     mp->no_advertise = no_advertise;
5247     mp->off_link = off_link;
5248     mp->no_autoconfig = no_autoconfig;
5249     mp->no_onlink = no_onlink;
5250     mp->is_no = is_no;
5251     mp->val_lifetime = ntohl(val_lifetime);
5252     mp->pref_lifetime = ntohl(pref_lifetime);
5253
5254     /* send it... */
5255     S;
5256
5257     /* Wait for a reply, return good/bad news  */
5258     W;
5259
5260     /* NOTREACHED */
5261     return 0;
5262 }
5263
5264 static int api_sw_interface_ip6nd_ra_config (vat_main_t * vam)
5265 {
5266     unformat_input_t * i = vam->input;
5267     vl_api_sw_interface_ip6nd_ra_config_t *mp;
5268     f64 timeout;
5269     u32 sw_if_index;
5270     u8 sw_if_index_set = 0;
5271     u8 surpress = 0;
5272     u8 managed = 0;
5273     u8 other = 0;
5274     u8 ll_option = 0;
5275     u8 send_unicast = 0;
5276     u8 cease = 0;
5277     u8 is_no = 0;
5278     u8 default_router = 0;
5279     u32 max_interval = 0;
5280     u32 min_interval = 0;
5281     u32 lifetime = 0;
5282     u32 initial_count = 0;
5283     u32 initial_interval = 0;
5284
5285     
5286     /* Parse args required to build the message */
5287     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5288         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
5289             sw_if_index_set = 1;
5290         else if (unformat (i, "sw_if_index %d", &sw_if_index))
5291             sw_if_index_set = 1;
5292         else if (unformat (i, "maxint %d", &max_interval))
5293             ;
5294         else if (unformat (i, "minint %d", &min_interval))
5295             ;
5296         else if (unformat (i, "life %d", &lifetime))
5297             ;
5298         else if (unformat (i, "count %d", &initial_count))
5299             ;
5300         else if (unformat (i, "interval %d", &initial_interval))
5301             ;
5302         else if (unformat (i, "surpress"))
5303             surpress = 1;
5304         else if (unformat (i, "managed"))
5305             managed = 1;
5306         else if (unformat (i, "other"))
5307             other = 1;
5308         else if (unformat (i, "ll"))
5309             ll_option = 1;
5310         else if (unformat (i, "send"))
5311             send_unicast = 1;
5312         else if (unformat (i, "cease"))
5313             cease = 1;
5314         else if (unformat (i, "isno"))
5315             is_no = 1;
5316         else if (unformat (i, "def"))
5317             default_router = 1;
5318         else {
5319             clib_warning ("parse error '%U'", format_unformat_error, i);
5320             return -99;
5321         }        
5322     }
5323
5324     if (sw_if_index_set == 0) {
5325         errmsg ("missing interface name or sw_if_index\n");
5326         return -99;
5327     }
5328
5329     /* Construct the API message */
5330     M(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config);
5331
5332     mp->sw_if_index = ntohl (sw_if_index);
5333     mp->max_interval = ntohl(max_interval);
5334     mp->min_interval = ntohl(min_interval);
5335     mp->lifetime = ntohl(lifetime);
5336     mp->initial_count = ntohl(initial_count);
5337     mp->initial_interval = ntohl(initial_interval);
5338     mp->surpress = surpress;
5339     mp->managed = managed;
5340     mp->other = other;
5341     mp->ll_option = ll_option;
5342     mp->send_unicast = send_unicast;
5343     mp->cease = cease;
5344     mp->is_no = is_no;
5345     mp->default_router = default_router;
5346
5347     /* send it... */
5348     S;
5349
5350     /* Wait for a reply, return good/bad news  */
5351     W;
5352
5353     /* NOTREACHED */
5354     return 0;
5355 }
5356
5357 static int api_set_arp_neighbor_limit (vat_main_t * vam)
5358 {
5359     unformat_input_t * i = vam->input;
5360     vl_api_set_arp_neighbor_limit_t *mp;
5361     f64 timeout;
5362     u32 arp_nbr_limit;
5363     u8 limit_set = 0;
5364     u8 is_ipv6 = 0;
5365
5366     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5367         if (unformat (i, "arp_nbr_limit %d", &arp_nbr_limit))
5368             limit_set = 1;
5369         else if (unformat (i, "ipv6"))
5370             is_ipv6 = 1;
5371         else {
5372             clib_warning ("parse error '%U'", format_unformat_error, i);
5373             return -99;
5374         }
5375     }
5376
5377     if (limit_set == 0) {
5378         errmsg ("missing limit value\n");
5379         return -99;
5380     }
5381     
5382     M(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit);
5383
5384     mp->arp_neighbor_limit = ntohl(arp_nbr_limit);
5385     mp->is_ipv6 = is_ipv6;
5386
5387     S; W;
5388     /* NOTREACHED */
5389     return 0;
5390 }
5391
5392 static int api_l2_patch_add_del (vat_main_t * vam)
5393 {
5394     unformat_input_t * i = vam->input;
5395     vl_api_l2_patch_add_del_t *mp;
5396     f64 timeout;
5397     u32 rx_sw_if_index;
5398     u8 rx_sw_if_index_set = 0;
5399     u32 tx_sw_if_index;
5400     u8 tx_sw_if_index_set = 0;
5401     u8 is_add = 1;
5402     
5403     /* Parse args required to build the message */
5404     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5405         if (unformat (i, "rx_sw_if_index %d", &rx_sw_if_index))
5406             rx_sw_if_index_set = 1;     
5407         else if (unformat (i, "tx_sw_if_index %d", &tx_sw_if_index))
5408             tx_sw_if_index_set = 1;
5409         else if (unformat (i, "rx")) {
5410             if (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5411                 if (unformat (i, "%U", unformat_sw_if_index, vam,
5412                               &rx_sw_if_index))
5413                     rx_sw_if_index_set = 1;
5414             } else
5415                 break;
5416         } else if (unformat (i, "tx")) {
5417             if (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
5418                 if (unformat (i, "%U", unformat_sw_if_index, vam,
5419                               &tx_sw_if_index))
5420                     tx_sw_if_index_set = 1;
5421             } else
5422                 break;
5423         } else if (unformat (i, "del"))
5424             is_add = 0;
5425         else
5426             break;
5427     }
5428
5429     if (rx_sw_if_index_set == 0) {
5430         errmsg ("missing rx interface name or rx_sw_if_index\n");
5431         return -99;
5432     }
5433
5434     if (tx_sw_if_index_set == 0) {
5435         errmsg ("missing tx interface name or tx_sw_if_index\n");
5436         return -99;
5437     }
5438     
5439     M(L2_PATCH_ADD_DEL, l2_patch_add_del);
5440
5441     mp->rx_sw_if_index = ntohl(rx_sw_if_index);
5442     mp->tx_sw_if_index = ntohl(tx_sw_if_index);
5443     mp->is_add = is_add;
5444
5445     S; W;
5446     /* NOTREACHED */
5447     return 0;
5448 }
5449 static int api_trace_profile_add (vat_main_t *vam)
5450 {
5451    unformat_input_t * input = vam->input;
5452    vl_api_trace_profile_add_t *mp;
5453    f64 timeout;
5454    u32 id = 0;
5455    u32 trace_option_elts = 0;
5456    u32 trace_type = 0, node_id = 0, app_data = 0, trace_tsp = 2;
5457    int has_pow_option = 0;
5458    int has_ppc_option = 0;
5459   
5460   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5461     {
5462       if (unformat (input, "id %d trace-type 0x%x trace-elts %d "
5463                            "trace-tsp %d node-id 0x%x app-data 0x%x", 
5464                     &id, &trace_type, &trace_option_elts, &trace_tsp,
5465                       &node_id, &app_data))
5466             ;
5467       else if (unformat (input, "pow"))
5468         has_pow_option = 1;
5469       else if (unformat (input, "ppc encap"))
5470         has_ppc_option = PPC_ENCAP;
5471       else if (unformat (input, "ppc decap"))
5472         has_ppc_option = PPC_DECAP;
5473       else if (unformat (input, "ppc none"))
5474         has_ppc_option = PPC_NONE;
5475       else
5476         break;
5477     }
5478   M(TRACE_PROFILE_ADD, trace_profile_add);
5479   mp->id = htons(id);
5480   mp->trace_type = trace_type;
5481   mp->trace_num_elt = trace_option_elts;
5482   mp->trace_ppc = has_ppc_option;
5483   mp->trace_app_data = htonl(app_data);
5484   mp->pow_enable = has_pow_option;
5485   mp->trace_tsp = trace_tsp;
5486   mp->node_id = htonl(node_id);
5487   
5488   S; W;
5489   
5490   return(0);
5491    
5492 }
5493 static int api_trace_profile_apply (vat_main_t *vam)
5494 {
5495   unformat_input_t * input = vam->input;
5496   vl_api_trace_profile_apply_t *mp;
5497   f64 timeout;
5498   ip6_address_t addr;
5499   u32 mask_width = ~0;
5500   int is_add = 0;
5501   int is_pop = 0;
5502   int is_none = 0;
5503   u32 vrf_id = 0;
5504   u32 id = 0;
5505   
5506   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5507     {
5508       if (unformat (input, "%U/%d",
5509                     unformat_ip6_address, &addr, &mask_width))
5510         ;
5511       else if (unformat (input, "id %d", &id))
5512         ;
5513       else if (unformat (input, "vrf-id %d", &vrf_id))
5514         ;
5515       else if (unformat (input, "add"))
5516         is_add = 1;
5517       else if (unformat (input, "pop"))
5518         is_pop = 1;
5519       else if (unformat (input, "none"))
5520         is_none = 1;
5521       else
5522         break;
5523     }
5524
5525   if ((is_add + is_pop + is_none) != 1) {
5526     errmsg("One of (add, pop, none) required");
5527     return -99;
5528   }
5529   if (mask_width == ~0) {
5530     errmsg("<address>/<mask-width> required");
5531     return -99;
5532   }
5533   M(TRACE_PROFILE_APPLY, trace_profile_apply);
5534   clib_memcpy(mp->dest_ipv6, &addr, sizeof(mp->dest_ipv6));
5535   mp->id = htons(id);
5536   mp->prefix_length = htonl(mask_width);
5537   mp->vrf_id = htonl(vrf_id);
5538   if (is_add)
5539     mp->trace_op = IOAM_HBYH_ADD;
5540   else if (is_pop)
5541     mp->trace_op = IOAM_HBYH_POP;
5542   else
5543     mp->trace_op = IOAM_HBYH_MOD;
5544
5545   if(is_none)
5546     mp->enable = 0;
5547   else
5548     mp->enable = 1;
5549   
5550   S; W;
5551
5552   return 0;
5553 }
5554
5555 static int api_trace_profile_del (vat_main_t *vam)
5556 {
5557    vl_api_trace_profile_del_t *mp;
5558    f64 timeout;
5559    
5560    M(TRACE_PROFILE_DEL, trace_profile_del);
5561    S; W;
5562    return 0;
5563 }
5564
5565 static int api_sr_tunnel_add_del (vat_main_t * vam)
5566 {
5567   unformat_input_t * i = vam->input;
5568   vl_api_sr_tunnel_add_del_t *mp;
5569   f64 timeout;
5570   int is_del = 0;
5571   int pl_index;
5572   ip6_address_t src_address;
5573   int src_address_set = 0;
5574   ip6_address_t dst_address;
5575   u32 dst_mask_width;
5576   int dst_address_set = 0;
5577   u16 flags = 0;
5578   u32 rx_table_id = 0;
5579   u32 tx_table_id = 0;
5580   ip6_address_t * segments = 0;
5581   ip6_address_t * this_seg;
5582   ip6_address_t * tags = 0;
5583   ip6_address_t * this_tag;
5584   ip6_address_t next_address, tag;
5585   u8 * name = 0;
5586   u8 * policy_name = 0;
5587
5588   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
5589     {
5590       if (unformat (i, "del"))
5591         is_del = 1;
5592       else if (unformat (i, "name %s", &name))
5593             ;
5594       else if (unformat (i, "policy %s", &policy_name))
5595             ;
5596       else if (unformat (i, "rx_fib_id %d", &rx_table_id))
5597         ;
5598       else if (unformat (i, "tx_fib_id %d", &tx_table_id))
5599         ;
5600       else if (unformat (i, "src %U", unformat_ip6_address, &src_address))
5601         src_address_set = 1;
5602       else if (unformat (i, "dst %U/%d", 
5603                          unformat_ip6_address, &dst_address,
5604                          &dst_mask_width))
5605         dst_address_set = 1;
5606       else if (unformat (i, "next %U", unformat_ip6_address,
5607                          &next_address))
5608         {
5609           vec_add2 (segments, this_seg, 1);
5610           clib_memcpy (this_seg->as_u8, next_address.as_u8, sizeof (*this_seg));
5611         }
5612       else if (unformat (i, "tag %U", unformat_ip6_address,
5613                          &tag))
5614         {
5615           vec_add2 (tags, this_tag, 1);
5616           clib_memcpy (this_tag->as_u8, tag.as_u8, sizeof (*this_tag));
5617         }
5618       else if (unformat (i, "clean"))
5619         flags |= IP6_SR_HEADER_FLAG_CLEANUP;
5620       else if (unformat (i, "protected"))
5621         flags |= IP6_SR_HEADER_FLAG_PROTECTED;
5622       else if (unformat (i, "InPE %d", &pl_index))
5623         {
5624           if (pl_index <= 0 || pl_index > 4)
5625             {
5626             pl_index_range_error:
5627               errmsg ("pl index %d out of range\n", pl_index);
5628               return -99;
5629             }
5630           flags |= IP6_SR_HEADER_FLAG_PL_ELT_INGRESS_PE << (3*(pl_index - 1));
5631         }
5632       else if (unformat (i, "EgPE %d", &pl_index))
5633         {
5634           if (pl_index <= 0 || pl_index > 4)
5635             goto pl_index_range_error;
5636           flags |= IP6_SR_HEADER_FLAG_PL_ELT_EGRESS_PE << (3*(pl_index - 1));
5637         }
5638       else if (unformat (i, "OrgSrc %d", &pl_index))
5639         {
5640           if (pl_index <= 0 || pl_index > 4)
5641             goto pl_index_range_error;
5642           flags |= IP6_SR_HEADER_FLAG_PL_ELT_ORIG_SRC_ADDR << (3*(pl_index - 1));
5643         }
5644       else 
5645         break;
5646     }
5647
5648   if (!src_address_set)
5649     {
5650       errmsg ("src address required\n");
5651       return -99;
5652     }
5653
5654   if (!dst_address_set)
5655     {
5656       errmsg ("dst address required\n");
5657       return -99;
5658     }
5659
5660   if (!segments)
5661     {
5662       errmsg ("at least one sr segment required\n");
5663       return -99;
5664     }
5665
5666   M2(SR_TUNNEL_ADD_DEL, sr_tunnel_add_del, 
5667      vec_len(segments) * sizeof (ip6_address_t) 
5668      + vec_len(tags) * sizeof (ip6_address_t));
5669
5670   clib_memcpy (mp->src_address, &src_address, sizeof (mp->src_address));
5671   clib_memcpy (mp->dst_address, &dst_address, sizeof (mp->dst_address));
5672   mp->dst_mask_width = dst_mask_width;
5673   mp->flags_net_byte_order = clib_host_to_net_u16 (flags);
5674   mp->n_segments = vec_len (segments);
5675   mp->n_tags = vec_len (tags);
5676   mp->is_add = is_del == 0;
5677   clib_memcpy (mp->segs_and_tags, segments, 
5678           vec_len(segments)* sizeof (ip6_address_t));
5679   clib_memcpy (mp->segs_and_tags + vec_len(segments)*sizeof (ip6_address_t),
5680           tags, vec_len(tags)* sizeof (ip6_address_t));
5681
5682   mp->outer_vrf_id = ntohl (rx_table_id);
5683   mp->inner_vrf_id = ntohl (tx_table_id);
5684   memcpy (mp->name, name, vec_len(name));
5685   memcpy (mp->policy_name, policy_name, vec_len(policy_name));
5686
5687   vec_free (segments);
5688   vec_free (tags);
5689   
5690   S; W;
5691   /* NOTREACHED */
5692 }
5693
5694 static int api_sr_policy_add_del (vat_main_t * vam)
5695 {
5696   unformat_input_t * input = vam->input;
5697   vl_api_sr_policy_add_del_t *mp;
5698   f64 timeout;
5699   int is_del = 0;
5700   u8 * name = 0;
5701   u8 * tunnel_name = 0;
5702   u8 ** tunnel_names = 0;
5703   
5704   int name_set = 0 ;
5705   int tunnel_set = 0;
5706   int j = 0;
5707   int tunnel_names_length = 1; // Init to 1 to offset the #tunnel_names counter byte
5708   int tun_name_len = 0; // Different naming convention used as confusing these would be "bad" (TM)
5709
5710   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5711     {
5712       if (unformat (input, "del"))
5713         is_del = 1;
5714       else if (unformat (input, "name %s", &name))
5715         name_set = 1;
5716       else if (unformat (input, "tunnel %s", &tunnel_name))
5717         {
5718           if (tunnel_name)
5719             {
5720               vec_add1 (tunnel_names, tunnel_name);
5721               /* For serializer: 
5722                  - length = #bytes to store in serial vector
5723                  - +1 = byte to store that length
5724               */
5725               tunnel_names_length += (vec_len (tunnel_name) + 1); 
5726               tunnel_set = 1;
5727               tunnel_name = 0;
5728             }
5729         }
5730       else 
5731         break;
5732     }
5733
5734   if (!name_set)
5735     {
5736       errmsg ("policy name required\n");
5737       return -99;
5738     }
5739
5740   if ((!tunnel_set) && (!is_del))
5741     {
5742       errmsg ("tunnel name required\n");
5743       return -99;
5744     }
5745
5746   M2(SR_POLICY_ADD_DEL, sr_policy_add_del, tunnel_names_length);
5747
5748   
5749
5750   mp->is_add = !is_del;
5751
5752   memcpy (mp->name, name, vec_len(name));
5753   // Since mp->tunnel_names is of type u8[0] and not a u8 *, u8 ** needs to be serialized
5754   u8 * serial_orig = 0;
5755   vec_validate (serial_orig, tunnel_names_length);
5756   *serial_orig = vec_len(tunnel_names); // Store the number of tunnels as length in first byte of serialized vector
5757   serial_orig += 1; // Move along one byte to store the length of first tunnel_name
5758
5759   for (j=0; j < vec_len(tunnel_names); j++)
5760     {
5761       tun_name_len = vec_len (tunnel_names[j]);
5762       *serial_orig = tun_name_len; // Store length of tunnel name in first byte of Length/Value pair
5763       serial_orig += 1; // Move along one byte to store the actual tunnel name
5764       memcpy (serial_orig, tunnel_names[j], tun_name_len);
5765       serial_orig += tun_name_len; // Advance past the copy
5766     }
5767   memcpy (mp->tunnel_names, serial_orig - tunnel_names_length, tunnel_names_length); // Regress serial_orig to head then copy fwd
5768
5769   vec_free (tunnel_names);
5770   vec_free (tunnel_name);
5771   
5772   S; W;
5773   /* NOTREACHED */
5774 }
5775
5776 static int api_sr_multicast_map_add_del (vat_main_t * vam)
5777 {
5778   unformat_input_t * input = vam->input;
5779   vl_api_sr_multicast_map_add_del_t *mp;
5780   f64 timeout;
5781   int is_del = 0;
5782   ip6_address_t multicast_address;
5783   u8 * policy_name = 0;
5784   int multicast_address_set = 0;
5785
5786   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5787     {
5788       if (unformat (input, "del"))
5789         is_del = 1;
5790       else if (unformat (input, "address %U", unformat_ip6_address, &multicast_address))
5791         multicast_address_set = 1;
5792       else if (unformat (input, "sr-policy %s", &policy_name))
5793         ;
5794       else 
5795         break;
5796     }
5797
5798   if (!is_del && !policy_name)
5799     {
5800       errmsg ("sr-policy name required\n");
5801       return -99;
5802     }
5803
5804
5805   if (!multicast_address_set)
5806     {
5807       errmsg ("address required\n");
5808       return -99;
5809     }
5810
5811   M(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del);
5812
5813   mp->is_add = !is_del;
5814   memcpy (mp->policy_name, policy_name, vec_len(policy_name));
5815   clib_memcpy (mp->multicast_address, &multicast_address, sizeof (mp->multicast_address));
5816
5817
5818   vec_free (policy_name);
5819   
5820   S; W;
5821   /* NOTREACHED */
5822 }
5823
5824
5825 #define foreach_ip4_proto_field                 \
5826 _(src_address)                                  \
5827 _(dst_address)                                  \
5828 _(tos)                                          \
5829 _(length)                                       \
5830 _(fragment_id)                                  \
5831 _(ttl)                                          \
5832 _(protocol)                                     \
5833 _(checksum)
5834
5835 uword unformat_ip4_mask (unformat_input_t * input, va_list * args)
5836 {
5837   u8 ** maskp = va_arg (*args, u8 **);
5838   u8 * mask = 0;
5839   u8 found_something = 0;
5840   ip4_header_t * ip;
5841   
5842 #define _(a) u8 a=0;
5843   foreach_ip4_proto_field;
5844 #undef _
5845   u8 version = 0;
5846   u8 hdr_length = 0;
5847   
5848   
5849   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
5850     {
5851       if (unformat (input, "version")) 
5852         version = 1;
5853       else if (unformat (input, "hdr_length"))
5854         hdr_length = 1;
5855       else if (unformat (input, "src"))
5856         src_address = 1;
5857       else if (unformat (input, "dst"))
5858         dst_address = 1;
5859       else if (unformat (input, "proto"))
5860         protocol = 1;
5861       
5862 #define _(a) else if (unformat (input, #a)) a=1;
5863       foreach_ip4_proto_field
5864 #undef _
5865       else
5866         break;
5867     }
5868   
5869 #define _(a) found_something += a;
5870   foreach_ip4_proto_field;
5871 #undef _
5872   
5873   if (found_something == 0)
5874     return 0;
5875   
5876   vec_validate (mask, sizeof (*ip) - 1);
5877   
5878   ip = (ip4_header_t *) mask;
5879   
5880 #define _(a) if (a) memset (&ip->a, 0xff, sizeof (ip->a));
5881   foreach_ip4_proto_field;
5882 #undef _
5883   
5884   ip->ip_version_and_header_length = 0;
5885   
5886   if (version)
5887     ip->ip_version_and_header_length |= 0xF0;
5888   
5889   if (hdr_length)
5890     ip->ip_version_and_header_length |= 0x0F;
5891   
5892   *maskp = mask;
5893   return 1;
5894 }
5895
5896 #define foreach_ip6_proto_field                 \
5897 _(src_address)                                  \
5898 _(dst_address)                                  \
5899 _(payload_length)                               \
5900 _(hop_limit)                                    \
5901 _(protocol)
5902
5903 uword unformat_ip6_mask (unformat_input_t * input, va_list * args)
5904 {
5905   u8 ** maskp = va_arg (*args, u8 **);
5906   u8 * mask = 0;
5907   u8 found_something = 0;
5908   ip6_header_t * ip;
5909   u32 ip_version_traffic_class_and_flow_label;
5910   
5911 #define _(a) u8 a=0;
5912   foreach_ip6_proto_field;
5913 #undef _
5914   u8 version = 0;
5915   u8 traffic_class = 0;
5916   u8 flow_label = 0;
5917   
5918   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
5919     {
5920       if (unformat (input, "version")) 
5921         version = 1;
5922       else if (unformat (input, "traffic-class"))
5923         traffic_class = 1;
5924       else if (unformat (input, "flow-label"))
5925         flow_label = 1;
5926       else if (unformat (input, "src"))
5927         src_address = 1;
5928       else if (unformat (input, "dst"))
5929         dst_address = 1;
5930       else if (unformat (input, "proto"))
5931         protocol = 1;
5932       
5933 #define _(a) else if (unformat (input, #a)) a=1;
5934       foreach_ip6_proto_field
5935 #undef _
5936       else
5937         break;
5938     }
5939   
5940 #define _(a) found_something += a;
5941   foreach_ip6_proto_field;
5942 #undef _
5943   
5944   if (found_something == 0)
5945     return 0;
5946   
5947   vec_validate (mask, sizeof (*ip) - 1);
5948   
5949   ip = (ip6_header_t *) mask;
5950   
5951 #define _(a) if (a) memset (&ip->a, 0xff, sizeof (ip->a));
5952   foreach_ip6_proto_field;
5953 #undef _
5954   
5955   ip_version_traffic_class_and_flow_label = 0;
5956   
5957   if (version)
5958     ip_version_traffic_class_and_flow_label |= 0xF0000000;
5959
5960   if (traffic_class)
5961     ip_version_traffic_class_and_flow_label |= 0x0FF00000;
5962
5963   if (flow_label)
5964     ip_version_traffic_class_and_flow_label |= 0x000FFFFF;
5965
5966   ip->ip_version_traffic_class_and_flow_label = 
5967     clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
5968   
5969   *maskp = mask;
5970   return 1;
5971 }
5972
5973 uword unformat_l3_mask (unformat_input_t * input, va_list * args)
5974 {
5975   u8 ** maskp = va_arg (*args, u8 **);
5976
5977   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
5978     if (unformat (input, "ip4 %U", unformat_ip4_mask, maskp))
5979       return 1;
5980     else if (unformat (input, "ip6 %U", unformat_ip6_mask, maskp))
5981       return 1;
5982     else
5983       break;
5984   }
5985   return 0;
5986 }
5987
5988 uword unformat_l2_mask (unformat_input_t * input, va_list * args)
5989 {
5990   u8 ** maskp = va_arg (*args, u8 **);
5991   u8 * mask = 0;
5992   u8 src = 0;
5993   u8 dst = 0;
5994   u8 proto = 0;
5995   u8 tag1 = 0;
5996   u8 tag2 = 0;
5997   u8 ignore_tag1 = 0;
5998   u8 ignore_tag2 = 0;
5999   u8 cos1 = 0;
6000   u8 cos2 = 0;
6001   u8 dot1q = 0;
6002   u8 dot1ad = 0;
6003   int len = 14;
6004
6005   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
6006     if (unformat (input, "src"))
6007       src = 1;
6008     else if (unformat (input, "dst"))
6009       dst = 1;
6010     else if (unformat (input, "proto"))
6011       proto = 1;
6012     else if (unformat (input, "tag1"))
6013       tag1 = 1;
6014     else if (unformat (input, "tag2"))
6015       tag2 = 1;
6016     else if (unformat (input, "ignore-tag1"))
6017       ignore_tag1 = 1;
6018     else if (unformat (input, "ignore-tag2"))
6019       ignore_tag2 = 1;
6020     else if (unformat (input, "cos1"))
6021       cos1 = 1;
6022     else if (unformat (input, "cos2"))
6023       cos2 = 1;
6024     else if (unformat (input, "dot1q"))
6025       dot1q = 1;
6026     else if (unformat (input, "dot1ad"))
6027       dot1ad = 1;
6028     else
6029       break;
6030   }
6031   if ((src + dst + proto + tag1 + tag2 + dot1q + dot1ad +
6032       ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
6033     return 0;
6034
6035   if (tag1 || ignore_tag1 || cos1 || dot1q)
6036     len = 18;
6037   if (tag2 || ignore_tag2 || cos2 || dot1ad)
6038     len = 22;
6039
6040   vec_validate (mask, len-1);
6041
6042   if (dst)
6043     memset (mask, 0xff, 6);
6044
6045   if (src)
6046     memset (mask + 6, 0xff, 6);
6047   
6048   if (tag2 || dot1ad)
6049     {
6050       /* inner vlan tag */
6051       if (tag2)
6052         {
6053           mask[19] = 0xff;
6054           mask[18] = 0x0f;
6055         }
6056       if (cos2)
6057         mask[18] |= 0xe0;
6058       if (proto)
6059           mask[21] = mask [20] = 0xff;
6060       if (tag1)
6061         {
6062           mask [15] = 0xff;
6063           mask [14] = 0x0f;
6064         }
6065       if (cos1)
6066         mask[14] |= 0xe0;
6067       *maskp = mask;
6068       return 1;
6069     }
6070   if (tag1 | dot1q)
6071     {
6072       if (tag1)
6073         {
6074           mask [15] = 0xff;
6075           mask [14] = 0x0f;
6076         }
6077       if (cos1)
6078         mask[14] |= 0xe0;
6079       if (proto)
6080           mask[16] = mask [17] = 0xff;
6081
6082       *maskp = mask;
6083       return 1;
6084     }
6085   if (cos2)
6086     mask[18] |= 0xe0;
6087   if (cos1)
6088     mask[14] |= 0xe0;
6089   if (proto)
6090     mask[12] = mask [13] = 0xff;
6091     
6092   *maskp = mask;
6093   return 1;
6094 }
6095
6096 uword unformat_classify_mask (unformat_input_t * input, va_list * args)
6097 {
6098   u8 ** maskp = va_arg (*args, u8 **);
6099   u32 * skipp = va_arg (*args, u32 *);
6100   u32 * matchp = va_arg (*args, u32 *);
6101   u32 match;
6102   u8 * mask = 0;
6103   u8 * l2 = 0;
6104   u8 * l3 = 0;
6105   int i;
6106   
6107   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
6108     if (unformat (input, "hex %U", unformat_hex_string, &mask))
6109       ;
6110     else if (unformat (input, "l2 %U", unformat_l2_mask, &l2))
6111       ;
6112     else if (unformat (input, "l3 %U", unformat_l3_mask, &l3))
6113       ;
6114     else
6115       break;
6116   }
6117
6118   if (mask || l2 || l3)
6119     {
6120       if (l2 || l3)
6121         {
6122           /* "With a free Ethernet header in every package" */
6123           if (l2 == 0)
6124             vec_validate (l2, 13);
6125           mask = l2;
6126           vec_append (mask, l3);
6127           vec_free (l3);
6128         }
6129
6130       /* Scan forward looking for the first significant mask octet */
6131       for (i = 0; i < vec_len (mask); i++)
6132         if (mask[i])
6133           break;
6134
6135       /* compute (skip, match) params */
6136       *skipp = i / sizeof(u32x4);
6137       vec_delete (mask, *skipp * sizeof(u32x4), 0);
6138
6139       /* Pad mask to an even multiple of the vector size */
6140       while (vec_len (mask) % sizeof (u32x4))
6141         vec_add1 (mask, 0);
6142
6143       match = vec_len (mask) / sizeof (u32x4);
6144
6145       for (i = match*sizeof(u32x4); i > 0; i-= sizeof(u32x4))
6146         {
6147           u64 *tmp = (u64 *)(mask + (i-sizeof(u32x4)));
6148           if (*tmp || *(tmp+1))
6149             break;
6150           match--;
6151         }
6152       if (match == 0)
6153         clib_warning ("BUG: match 0");
6154
6155       _vec_len (mask) = match * sizeof(u32x4);
6156
6157       *matchp = match;
6158       *maskp = mask;
6159
6160       return 1;
6161     }
6162
6163   return 0;
6164 }
6165
6166 #define foreach_l2_next                         \
6167 _(drop, DROP)                                   \
6168 _(ethernet, ETHERNET_INPUT)                     \
6169 _(ip4, IP4_INPUT)                               \
6170 _(ip6, IP6_INPUT)
6171
6172 uword unformat_l2_next_index (unformat_input_t * input, va_list * args)
6173 {
6174   u32 * miss_next_indexp = va_arg (*args, u32 *);
6175   u32 next_index = 0;
6176   u32 tmp;
6177   
6178 #define _(n,N) \
6179   if (unformat (input, #n)) { next_index = L2_CLASSIFY_NEXT_##N; goto out;}
6180   foreach_l2_next;
6181 #undef _
6182   
6183   if (unformat (input, "%d", &tmp))
6184     { 
6185       next_index = tmp; 
6186       goto out; 
6187     }
6188   
6189   return 0;
6190
6191  out:
6192   *miss_next_indexp = next_index;
6193   return 1;
6194 }
6195
6196 #define foreach_ip_next                         \
6197 _(miss, MISS)                                   \
6198 _(drop, DROP)                                   \
6199 _(local, LOCAL)                                 \
6200 _(rewrite, REWRITE)
6201
6202 uword unformat_ip_next_index (unformat_input_t * input, va_list * args)
6203 {
6204   u32 * miss_next_indexp = va_arg (*args, u32 *);
6205   u32 next_index = 0;
6206   u32 tmp;
6207   
6208 #define _(n,N) \
6209   if (unformat (input, #n)) { next_index = IP_LOOKUP_NEXT_##N; goto out;}
6210   foreach_ip_next;
6211 #undef _
6212   
6213   if (unformat (input, "%d", &tmp))
6214     { 
6215       next_index = tmp; 
6216       goto out; 
6217     }
6218   
6219   return 0;
6220
6221  out:
6222   *miss_next_indexp = next_index;
6223   return 1;
6224 }
6225
6226 #define foreach_acl_next                        \
6227 _(deny, DENY)
6228
6229 uword unformat_acl_next_index (unformat_input_t * input, va_list * args)
6230 {
6231   u32 * miss_next_indexp = va_arg (*args, u32 *);
6232   u32 next_index = 0;
6233   u32 tmp;
6234
6235 #define _(n,N) \
6236   if (unformat (input, #n)) { next_index = ACL_NEXT_INDEX_##N; goto out;}
6237   foreach_acl_next;
6238 #undef _
6239
6240   if (unformat (input, "permit"))
6241     {
6242       next_index = ~0;
6243       goto out;
6244     }
6245   else if (unformat (input, "%d", &tmp))
6246     {
6247       next_index = tmp;
6248       goto out;
6249     }
6250
6251   return 0;
6252
6253  out:
6254   *miss_next_indexp = next_index;
6255   return 1;
6256 }
6257
6258 static int api_classify_add_del_table (vat_main_t * vam)
6259 {
6260   unformat_input_t * i = vam->input;
6261   vl_api_classify_add_del_table_t *mp;
6262
6263   u32 nbuckets = 2;
6264   u32 skip = ~0;
6265   u32 match = ~0;
6266   int is_add = 1;
6267   u32 table_index = ~0;
6268   u32 next_table_index = ~0;
6269   u32 miss_next_index = ~0;
6270   u32 memory_size = 32<<20;
6271   u8 * mask = 0;
6272   f64 timeout;
6273
6274   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6275     if (unformat (i, "del"))
6276       is_add = 0;
6277     else if (unformat (i, "buckets %d", &nbuckets))
6278       ;
6279     else if (unformat (i, "memory_size %d", &memory_size))
6280       ;
6281     else if (unformat (i, "skip %d", &skip))
6282       ;
6283     else if (unformat (i, "match %d", &match))
6284       ;
6285     else if (unformat (i, "table %d", &table_index))
6286       ;
6287     else if (unformat (i, "mask %U", unformat_classify_mask, 
6288                        &mask, &skip, &match))
6289       ;
6290     else if (unformat (i, "next-table %d", &next_table_index))
6291       ;
6292     else if (unformat (i, "miss-next %U", unformat_ip_next_index,
6293                        &miss_next_index))
6294       ;
6295     else if (unformat (i, "l2-miss-next %U", unformat_l2_next_index,
6296                        &miss_next_index))
6297       ;
6298     else if (unformat (i, "acl-miss-next %U", unformat_acl_next_index,
6299                        &miss_next_index))
6300       ;
6301     else
6302       break;
6303   }
6304   
6305   if (is_add && mask == 0) {
6306       errmsg ("Mask required\n");
6307       return -99;
6308   }
6309
6310   if (is_add && skip == ~0) {
6311       errmsg ("skip count required\n");
6312       return -99;
6313   }
6314
6315   if (is_add && match == ~0) {
6316       errmsg ("match count required\n");
6317       return -99;
6318   }
6319       
6320   if (!is_add && table_index == ~0) {
6321       errmsg ("table index required for delete\n");
6322       return -99;
6323   }
6324
6325   M2 (CLASSIFY_ADD_DEL_TABLE, classify_add_del_table,
6326       vec_len(mask));
6327
6328   mp->is_add = is_add;
6329   mp->table_index = ntohl(table_index);
6330   mp->nbuckets = ntohl(nbuckets);
6331   mp->memory_size = ntohl(memory_size);
6332   mp->skip_n_vectors = ntohl(skip);
6333   mp->match_n_vectors = ntohl(match);
6334   mp->next_table_index = ntohl(next_table_index);
6335   mp->miss_next_index = ntohl(miss_next_index);
6336   clib_memcpy (mp->mask, mask, vec_len(mask));
6337
6338   vec_free(mask);
6339
6340   S; W;
6341   /* NOTREACHED */
6342 }
6343
6344 uword unformat_ip4_match (unformat_input_t * input, va_list * args)
6345 {
6346   u8 ** matchp = va_arg (*args, u8 **);
6347   u8 * match = 0;
6348   ip4_header_t * ip;
6349   int version = 0;
6350   u32 version_val;
6351   int hdr_length = 0;
6352   u32 hdr_length_val;
6353   int src = 0, dst = 0;
6354   ip4_address_t src_val, dst_val;
6355   int proto = 0;
6356   u32 proto_val;
6357   int tos = 0;
6358   u32 tos_val;
6359   int length = 0;
6360   u32 length_val;
6361   int fragment_id = 0;
6362   u32 fragment_id_val;
6363   int ttl = 0;
6364   int ttl_val;
6365   int checksum = 0;
6366   u32 checksum_val;
6367
6368   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
6369     {
6370       if (unformat (input, "version %d", &version_val)) 
6371         version = 1;
6372       else if (unformat (input, "hdr_length %d", &hdr_length_val))
6373         hdr_length = 1;
6374       else if (unformat (input, "src %U", unformat_ip4_address, &src_val))
6375         src = 1;
6376       else if (unformat (input, "dst %U", unformat_ip4_address, &dst_val))
6377         dst = 1;
6378       else if (unformat (input, "proto %d", &proto_val))
6379         proto = 1;
6380       else if (unformat (input, "tos %d", &tos_val))
6381         tos = 1;
6382       else if (unformat (input, "length %d", &length_val))
6383         length = 1;
6384       else if (unformat (input, "fragment_id %d", &fragment_id_val))
6385         fragment_id = 1;
6386       else if (unformat (input, "ttl %d", &ttl_val))
6387         ttl = 1;
6388       else if (unformat (input, "checksum %d", &checksum_val))
6389         checksum = 1;
6390       else
6391         break;
6392     }
6393   
6394   if (version + hdr_length + src + dst + proto + tos + length + fragment_id
6395       + ttl + checksum == 0)
6396     return 0;
6397
6398   /* 
6399    * Aligned because we use the real comparison functions
6400    */
6401   vec_validate_aligned (match, sizeof (*ip) - 1, sizeof(u32x4));
6402   
6403   ip = (ip4_header_t *) match;
6404   
6405   /* These are realistically matched in practice */
6406   if (src)
6407     ip->src_address.as_u32 = src_val.as_u32;
6408
6409   if (dst)
6410     ip->dst_address.as_u32 = dst_val.as_u32;
6411   
6412   if (proto)
6413     ip->protocol = proto_val;
6414     
6415
6416   /* These are not, but they're included for completeness */
6417   if (version)
6418     ip->ip_version_and_header_length |= (version_val & 0xF)<<4;
6419
6420   if (hdr_length)
6421     ip->ip_version_and_header_length |= (hdr_length_val & 0xF);
6422     
6423   if (tos)
6424     ip->tos = tos_val;
6425   
6426   if (length)
6427     ip->length = length_val;
6428   
6429   if (ttl)
6430     ip->ttl = ttl_val;
6431
6432   if (checksum)
6433     ip->checksum = checksum_val;
6434
6435   *matchp = match;
6436   return 1;
6437 }
6438
6439 uword unformat_ip6_match (unformat_input_t * input, va_list * args)
6440 {
6441   u8 ** matchp = va_arg (*args, u8 **);
6442   u8 * match = 0;
6443   ip6_header_t * ip;
6444   int version = 0;
6445   u32 version_val;
6446   u8  traffic_class;
6447   u32 traffic_class_val;
6448   u8  flow_label;
6449   u8  flow_label_val;
6450   int src = 0, dst = 0;
6451   ip6_address_t src_val, dst_val;
6452   int proto = 0;
6453   u32 proto_val;
6454   int payload_length = 0;
6455   u32 payload_length_val;
6456   int hop_limit = 0;
6457   int hop_limit_val;
6458   u32 ip_version_traffic_class_and_flow_label;
6459
6460   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
6461     {
6462       if (unformat (input, "version %d", &version_val)) 
6463         version = 1;
6464       else if (unformat (input, "traffic_class %d", &traffic_class_val))
6465         traffic_class = 1;
6466       else if (unformat (input, "flow_label %d", &flow_label_val))
6467         flow_label = 1;
6468       else if (unformat (input, "src %U", unformat_ip6_address, &src_val))
6469         src = 1;
6470       else if (unformat (input, "dst %U", unformat_ip6_address, &dst_val))
6471         dst = 1;
6472       else if (unformat (input, "proto %d", &proto_val))
6473         proto = 1;
6474       else if (unformat (input, "payload_length %d", &payload_length_val))
6475         payload_length = 1;
6476       else if (unformat (input, "hop_limit %d", &hop_limit_val))
6477         hop_limit = 1;
6478       else
6479         break;
6480     }
6481   
6482   if (version + traffic_class + flow_label + src + dst + proto +
6483       payload_length + hop_limit == 0)
6484     return 0;
6485
6486   /* 
6487    * Aligned because we use the real comparison functions
6488    */
6489   vec_validate_aligned (match, sizeof (*ip) - 1, sizeof(u32x4));
6490   
6491   ip = (ip6_header_t *) match;
6492   
6493   if (src)
6494     clib_memcpy (&ip->src_address, &src_val, sizeof (ip->src_address));
6495
6496   if (dst)
6497     clib_memcpy (&ip->dst_address, &dst_val, sizeof (ip->dst_address));
6498   
6499   if (proto)
6500     ip->protocol = proto_val;
6501     
6502   ip_version_traffic_class_and_flow_label = 0;
6503
6504   if (version)
6505     ip_version_traffic_class_and_flow_label |= (version_val & 0xF) << 28;
6506
6507   if (traffic_class)
6508     ip_version_traffic_class_and_flow_label |= (traffic_class_val & 0xFF) << 20;
6509
6510   if (flow_label)
6511     ip_version_traffic_class_and_flow_label |= (flow_label_val & 0xFFFFF);
6512     
6513   ip->ip_version_traffic_class_and_flow_label = 
6514     clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
6515
6516   if (payload_length)
6517     ip->payload_length = clib_host_to_net_u16 (payload_length_val);
6518   
6519   if (hop_limit)
6520     ip->hop_limit = hop_limit_val;
6521
6522   *matchp = match;
6523   return 1;
6524 }
6525
6526 uword unformat_l3_match (unformat_input_t * input, va_list * args)
6527 {
6528   u8 ** matchp = va_arg (*args, u8 **);
6529
6530   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
6531     if (unformat (input, "ip4 %U", unformat_ip4_match, matchp))
6532       return 1;
6533     else if (unformat (input, "ip6 %U", unformat_ip6_match, matchp))
6534       return 1;
6535     else
6536       break;
6537   }
6538   return 0;
6539 }
6540
6541 uword unformat_vlan_tag (unformat_input_t * input, va_list * args)
6542 {
6543   u8 * tagp = va_arg (*args, u8 *);
6544   u32 tag;
6545
6546   if (unformat(input, "%d", &tag))
6547     {
6548       tagp[0] = (tag>>8) & 0x0F;
6549       tagp[1] = tag & 0xFF;
6550       return 1;
6551     }
6552
6553   return 0;
6554 }
6555
6556 uword unformat_l2_match (unformat_input_t * input, va_list * args)
6557 {
6558   u8 ** matchp = va_arg (*args, u8 **);
6559   u8 * match = 0;
6560   u8 src = 0;
6561   u8 src_val[6];
6562   u8 dst = 0;
6563   u8 dst_val[6];
6564   u8 proto = 0;
6565   u16 proto_val;
6566   u8 tag1 = 0;
6567   u8 tag1_val [2];
6568   u8 tag2 = 0;
6569   u8 tag2_val [2];
6570   int len = 14;
6571   u8 ignore_tag1 = 0;
6572   u8 ignore_tag2 = 0;
6573   u8 cos1 = 0;
6574   u8 cos2 = 0;
6575   u32 cos1_val = 0;
6576   u32 cos2_val = 0;
6577
6578   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
6579     if (unformat (input, "src %U", unformat_ethernet_address, &src_val))
6580       src = 1;
6581     else if (unformat (input, "dst %U", unformat_ethernet_address, &dst_val))
6582       dst = 1;
6583     else if (unformat (input, "proto %U", 
6584                        unformat_ethernet_type_host_byte_order, &proto_val))
6585       proto = 1;
6586     else if (unformat (input, "tag1 %U", unformat_vlan_tag, tag1_val))
6587       tag1 = 1;
6588     else if (unformat (input, "tag2 %U", unformat_vlan_tag, tag2_val))
6589       tag2 = 1;
6590     else if (unformat (input, "ignore-tag1"))
6591       ignore_tag1 = 1;
6592     else if (unformat (input, "ignore-tag2"))
6593       ignore_tag2 = 1;
6594     else if (unformat (input, "cos1 %d", &cos1_val))
6595       cos1 = 1;
6596     else if (unformat (input, "cos2 %d", &cos2_val))
6597       cos2 = 1;
6598     else
6599       break;
6600   }
6601   if ((src + dst + proto + tag1 + tag2 +
6602       ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
6603     return 0;
6604
6605   if (tag1 || ignore_tag1 || cos1)
6606     len = 18;
6607   if (tag2 || ignore_tag2 || cos2)
6608     len = 22;
6609
6610   vec_validate_aligned (match, len-1, sizeof(u32x4));
6611
6612   if (dst)
6613     clib_memcpy (match, dst_val, 6);
6614
6615   if (src)
6616     clib_memcpy (match + 6, src_val, 6);
6617   
6618   if (tag2)
6619     {
6620       /* inner vlan tag */
6621       match[19] = tag2_val[1];
6622       match[18] = tag2_val[0];
6623       if (cos2)
6624         match [18] |= (cos2_val & 0x7) << 5;
6625       if (proto)
6626         {
6627           match[21] = proto_val & 0xff;
6628           match[20] = proto_val >> 8;
6629         }
6630       if (tag1)
6631         {
6632           match [15] = tag1_val[1];
6633           match [14] = tag1_val[0];
6634         }
6635       if (cos1)
6636         match [14] |= (cos1_val & 0x7) << 5;
6637       *matchp = match;
6638       return 1;
6639     }
6640   if (tag1)
6641     {
6642       match [15] = tag1_val[1];
6643       match [14] = tag1_val[0];
6644       if (proto)
6645         {
6646           match[17] = proto_val & 0xff;
6647           match[16] = proto_val >> 8;
6648         }
6649       if (cos1)
6650         match [14] |= (cos1_val & 0x7) << 5;
6651
6652       *matchp = match;
6653       return 1;
6654     }
6655   if (cos2)
6656     match [18] |= (cos2_val & 0x7) << 5;
6657   if (cos1)
6658     match [14] |= (cos1_val & 0x7) << 5;
6659   if (proto)
6660     {
6661       match[13] = proto_val & 0xff;
6662       match[12] = proto_val >> 8;
6663     }
6664   
6665   *matchp = match;
6666   return 1;
6667 }
6668
6669
6670 uword unformat_classify_match (unformat_input_t * input, va_list * args)
6671 {
6672   u8 ** matchp = va_arg (*args, u8 **);
6673   u32 skip_n_vectors = va_arg (*args, u32);
6674   u32 match_n_vectors = va_arg (*args, u32);
6675   
6676   u8 * match = 0;
6677   u8 * l2 = 0;
6678   u8 * l3 = 0;
6679
6680   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
6681     if (unformat (input, "hex %U", unformat_hex_string, &match))
6682       ;
6683     else if (unformat (input, "l2 %U", unformat_l2_match, &l2))
6684       ;
6685     else if (unformat (input, "l3 %U", unformat_l3_match, &l3))
6686       ;
6687     else
6688       break;
6689   }
6690
6691   if (match || l2 || l3)
6692     {
6693       if (l2 || l3)
6694         {
6695           /* "Win a free Ethernet header in every packet" */
6696           if (l2 == 0)
6697             vec_validate_aligned (l2, 13, sizeof(u32x4));
6698           match = l2;
6699           vec_append_aligned (match, l3, sizeof(u32x4));
6700           vec_free (l3);
6701         }
6702
6703       /* Make sure the vector is big enough even if key is all 0's */
6704       vec_validate_aligned 
6705           (match, ((match_n_vectors + skip_n_vectors) * sizeof(u32x4)) - 1,
6706            sizeof(u32x4));
6707       
6708       /* Set size, include skipped vectors*/
6709       _vec_len (match) = (match_n_vectors+skip_n_vectors) * sizeof(u32x4);
6710
6711       *matchp = match;
6712
6713       return 1;
6714     }
6715
6716   return 0;
6717 }
6718
6719 static int api_classify_add_del_session (vat_main_t * vam)
6720 {
6721     unformat_input_t * i = vam->input;
6722     vl_api_classify_add_del_session_t *mp;
6723     int is_add = 1;
6724     u32 table_index = ~0;
6725     u32 hit_next_index = ~0;
6726     u32 opaque_index = ~0;
6727     u8 * match = 0;
6728     i32 advance = 0;
6729     f64 timeout;
6730     u32 skip_n_vectors = 0;
6731     u32 match_n_vectors = 0;
6732
6733     /* 
6734      * Warning: you have to supply skip_n and match_n
6735      * because the API client cant simply look at the classify
6736      * table object.
6737      */
6738
6739     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6740         if (unformat (i, "del"))
6741             is_add = 0;
6742         else if (unformat (i, "hit-next %U", unformat_ip_next_index,
6743                            &hit_next_index))
6744             ;
6745         else if (unformat (i, "l2-hit-next %U", unformat_l2_next_index,
6746                            &hit_next_index))
6747             ;
6748         else if (unformat (i, "acl-hit-next %U", unformat_acl_next_index,
6749                            &hit_next_index))
6750             ;
6751         else if (unformat (i, "opaque-index %d", &opaque_index))
6752             ;
6753         else if (unformat (i, "skip_n %d", &skip_n_vectors))
6754             ;
6755         else if (unformat (i, "match_n %d", &match_n_vectors))
6756             ;
6757         else if (unformat (i, "match %U", unformat_classify_match,
6758                            &match, skip_n_vectors, match_n_vectors))
6759             ;
6760         else if (unformat (i, "advance %d", &advance))
6761             ;
6762         else if (unformat (i, "table-index %d", &table_index))
6763             ;
6764         else
6765             break;
6766     }
6767
6768     if (table_index == ~0) {
6769         errmsg ("Table index required\n");
6770         return -99;
6771     }
6772
6773     if (is_add && match == 0) {
6774         errmsg ("Match value required\n");
6775         return -99;
6776     }
6777
6778     M2 (CLASSIFY_ADD_DEL_SESSION, classify_add_del_session,
6779         vec_len(match));
6780
6781     mp->is_add = is_add;
6782     mp->table_index = ntohl(table_index);
6783     mp->hit_next_index = ntohl(hit_next_index);
6784     mp->opaque_index = ntohl(opaque_index);
6785     mp->advance = ntohl(advance);
6786     clib_memcpy (mp->match, match, vec_len(match));
6787     vec_free(match);
6788
6789     S; W;
6790     /* NOTREACHED */
6791 }
6792
6793 static int api_classify_set_interface_ip_table (vat_main_t * vam)
6794 {
6795     unformat_input_t * i = vam->input;
6796     vl_api_classify_set_interface_ip_table_t *mp;
6797     f64 timeout;
6798     u32 sw_if_index;
6799     int sw_if_index_set;
6800     u32 table_index = ~0;
6801     u8  is_ipv6 = 0;
6802
6803     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6804         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
6805             sw_if_index_set = 1;
6806         else if (unformat (i, "sw_if_index %d", &sw_if_index))
6807             sw_if_index_set = 1;
6808         else if (unformat (i, "table %d", &table_index))
6809             ;
6810         else {
6811             clib_warning ("parse error '%U'", format_unformat_error, i);
6812             return -99;
6813         }
6814     }
6815     
6816     if (sw_if_index_set == 0) {
6817         errmsg ("missing interface name or sw_if_index\n");
6818         return -99;
6819     }
6820
6821
6822     M(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table);
6823
6824     mp->sw_if_index = ntohl(sw_if_index);
6825     mp->table_index = ntohl(table_index);
6826     mp->is_ipv6 = is_ipv6;
6827
6828     S; W;
6829     /* NOTREACHED */
6830     return 0;
6831 }
6832
6833 static int api_classify_set_interface_l2_tables (vat_main_t * vam)
6834 {
6835     unformat_input_t * i = vam->input;
6836     vl_api_classify_set_interface_l2_tables_t *mp;
6837     f64 timeout;
6838     u32 sw_if_index;
6839     int sw_if_index_set;
6840     u32 ip4_table_index = ~0;
6841     u32 ip6_table_index = ~0;
6842     u32 other_table_index = ~0;
6843
6844     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6845         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
6846             sw_if_index_set = 1;
6847         else if (unformat (i, "sw_if_index %d", &sw_if_index))
6848             sw_if_index_set = 1;
6849         else if (unformat (i, "ip4-table %d", &ip4_table_index))
6850             ;
6851         else if (unformat (i, "ip6-table %d", &ip6_table_index))
6852             ;
6853         else if (unformat (i, "other-table %d", &other_table_index))
6854             ;
6855         else {
6856             clib_warning ("parse error '%U'", format_unformat_error, i);
6857             return -99;
6858         }
6859     }
6860     
6861     if (sw_if_index_set == 0) {
6862         errmsg ("missing interface name or sw_if_index\n");
6863         return -99;
6864     }
6865
6866
6867     M(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables);
6868
6869     mp->sw_if_index = ntohl(sw_if_index);
6870     mp->ip4_table_index = ntohl(ip4_table_index);
6871     mp->ip6_table_index = ntohl(ip6_table_index);
6872     mp->other_table_index = ntohl(other_table_index);
6873
6874
6875     S; W;
6876     /* NOTREACHED */
6877     return 0;
6878 }
6879
6880 static int api_get_node_index (vat_main_t * vam)
6881 {
6882     unformat_input_t * i = vam->input;
6883     vl_api_get_node_index_t * mp;
6884     f64 timeout;
6885     u8 * name = 0;
6886     
6887     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6888         if (unformat (i, "node %s", &name))
6889             ;
6890         else
6891             break;
6892     }
6893     if (name == 0) {
6894         errmsg ("node name required\n");
6895         return -99;
6896     }
6897     if (vec_len (name) >= ARRAY_LEN(mp->node_name)) {
6898         errmsg ("node name too long, max %d\n", ARRAY_LEN(mp->node_name));
6899         return -99;
6900     }
6901
6902     M(GET_NODE_INDEX, get_node_index);
6903     clib_memcpy (mp->node_name, name, vec_len(name));
6904     vec_free(name);
6905     
6906     S; W;
6907     /* NOTREACHED */
6908     return 0;
6909 }
6910
6911 static int api_add_node_next (vat_main_t * vam)
6912 {
6913     unformat_input_t * i = vam->input;
6914     vl_api_add_node_next_t * mp;
6915     f64 timeout;
6916     u8 * name = 0;
6917     u8 * next = 0;
6918
6919     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6920         if (unformat (i, "node %s", &name))
6921             ;
6922         else if (unformat (i, "next %s", &next))
6923             ;
6924         else
6925             break;
6926     }
6927     if (name == 0) {
6928         errmsg ("node name required\n");
6929         return -99;
6930     }
6931     if (vec_len (name) >= ARRAY_LEN(mp->node_name)) {
6932         errmsg ("node name too long, max %d\n", ARRAY_LEN(mp->node_name));
6933         return -99;
6934     }
6935     if (next == 0) {
6936         errmsg ("next node required\n");
6937         return -99;
6938     }
6939     if (vec_len (next) >= ARRAY_LEN(mp->next_name)) {
6940         errmsg ("next name too long, max %d\n", ARRAY_LEN(mp->next_name));
6941         return -99;
6942     }
6943     
6944     M(ADD_NODE_NEXT, add_node_next);
6945     clib_memcpy (mp->node_name, name, vec_len(name));
6946     clib_memcpy (mp->next_name, next, vec_len(next));
6947     vec_free(name);
6948     vec_free(next);
6949     
6950     S; W;
6951     /* NOTREACHED */
6952     return 0;
6953 }
6954
6955 static int api_l2tpv3_create_tunnel (vat_main_t * vam)
6956 {
6957     unformat_input_t * i = vam->input;
6958     ip6_address_t client_address, our_address;
6959     int client_address_set = 0;
6960     int our_address_set = 0;
6961     u32 local_session_id = 0;
6962     u32 remote_session_id = 0;
6963     u64 local_cookie = 0;
6964     u64 remote_cookie = 0;
6965     u8 l2_sublayer_present = 0;
6966     vl_api_l2tpv3_create_tunnel_t * mp;
6967     f64 timeout;
6968
6969     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
6970         if (unformat (i, "client_address %U", unformat_ip6_address, 
6971                       &client_address))
6972             client_address_set = 1;
6973         else if (unformat (i, "our_address %U", unformat_ip6_address, 
6974                            &our_address))
6975             our_address_set = 1;
6976         else if (unformat (i, "local_session_id %d", &local_session_id))
6977             ;
6978         else if (unformat (i, "remote_session_id %d", &remote_session_id))
6979             ;
6980         else if (unformat (i, "local_cookie %lld", &local_cookie))
6981             ;
6982         else if (unformat (i, "remote_cookie %lld", &remote_cookie))
6983             ;
6984         else if (unformat (i, "l2-sublayer-present"))
6985             l2_sublayer_present = 1;
6986         else
6987             break;
6988     }
6989
6990     if (client_address_set == 0) {
6991         errmsg ("client_address required\n");
6992         return -99;
6993     }
6994
6995     if (our_address_set == 0) {
6996         errmsg ("our_address required\n");
6997         return -99;
6998     }
6999
7000     M(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel);
7001
7002     clib_memcpy (mp->client_address, client_address.as_u8, 
7003             sizeof (mp->client_address));
7004
7005     clib_memcpy (mp->our_address, our_address.as_u8, 
7006             sizeof (mp->our_address));
7007     
7008     mp->local_session_id = ntohl (local_session_id);
7009     mp->remote_session_id = ntohl (remote_session_id);
7010     mp->local_cookie = clib_host_to_net_u64 (local_cookie);
7011     mp->remote_cookie = clib_host_to_net_u64 (remote_cookie);
7012     mp->l2_sublayer_present = l2_sublayer_present;
7013     mp->is_ipv6 = 1;
7014
7015     S; W;
7016     /* NOTREACHED */
7017     return 0;
7018 }
7019
7020 static int api_l2tpv3_set_tunnel_cookies (vat_main_t * vam)
7021 {
7022     unformat_input_t * i = vam->input;
7023     u32 sw_if_index;
7024     u8  sw_if_index_set = 0;
7025     u64 new_local_cookie = 0;
7026     u64 new_remote_cookie = 0;
7027     vl_api_l2tpv3_set_tunnel_cookies_t *mp;
7028     f64 timeout;
7029
7030     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7031         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
7032             sw_if_index_set = 1;
7033         else if (unformat (i, "sw_if_index %d", &sw_if_index))
7034             sw_if_index_set = 1;
7035         else if (unformat (i, "new_local_cookie %lld", &new_local_cookie))
7036             ;
7037         else if (unformat (i, "new_remote_cookie %lld", &new_remote_cookie))
7038             ;
7039         else
7040             break;
7041     }
7042
7043     if (sw_if_index_set == 0) {
7044         errmsg ("missing interface name or sw_if_index\n");
7045         return -99;
7046     }
7047
7048     M(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies);
7049
7050     mp->sw_if_index = ntohl(sw_if_index);
7051     mp->new_local_cookie = clib_host_to_net_u64 (new_local_cookie);
7052     mp->new_remote_cookie = clib_host_to_net_u64 (new_remote_cookie);
7053
7054     S; W;
7055     /* NOTREACHED */
7056     return 0;
7057 }
7058
7059 static int api_l2tpv3_interface_enable_disable (vat_main_t * vam)
7060 {
7061     unformat_input_t * i = vam->input;
7062     vl_api_l2tpv3_interface_enable_disable_t *mp;
7063     f64 timeout;
7064     u32 sw_if_index;
7065     u8  sw_if_index_set = 0;
7066     u8  enable_disable = 1;
7067
7068     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7069         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
7070             sw_if_index_set = 1;
7071         else if (unformat (i, "sw_if_index %d", &sw_if_index))
7072             sw_if_index_set = 1;
7073         else if (unformat (i, "enable"))
7074             enable_disable = 1;
7075         else if (unformat (i, "disable"))
7076             enable_disable = 0;
7077         else
7078             break;
7079     }
7080
7081     if (sw_if_index_set == 0) {
7082         errmsg ("missing interface name or sw_if_index\n");
7083         return -99;
7084     }
7085     
7086     M(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable);
7087
7088     mp->sw_if_index = ntohl(sw_if_index);
7089     mp->enable_disable = enable_disable;
7090
7091     S; W;
7092     /* NOTREACHED */
7093     return 0;
7094 }
7095
7096 static int api_l2tpv3_set_lookup_key (vat_main_t * vam)
7097 {
7098     unformat_input_t * i = vam->input;
7099     vl_api_l2tpv3_set_lookup_key_t * mp;
7100     f64 timeout;
7101     u8 key = ~0;
7102
7103     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7104         if (unformat (i, "lookup_v6_src"))
7105             key = L2T_LOOKUP_SRC_ADDRESS;
7106         else if (unformat (i, "lookup_v6_dst"))
7107             key = L2T_LOOKUP_DST_ADDRESS;
7108         else if (unformat (i, "lookup_session_id"))
7109             key = L2T_LOOKUP_SESSION_ID;
7110         else
7111             break;
7112     }
7113
7114     if (key == (u8) ~0) {
7115         errmsg ("l2tp session lookup key unset\n");
7116         return -99;
7117     }
7118     
7119     M(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key);
7120
7121     mp->key = key;
7122
7123     S; W;
7124     /* NOTREACHED */
7125     return 0;
7126 }
7127
7128 static void vl_api_sw_if_l2tpv3_tunnel_details_t_handler
7129 (vl_api_sw_if_l2tpv3_tunnel_details_t * mp)
7130 {
7131     vat_main_t * vam = &vat_main;
7132
7133     fformat(vam->ofp,  "* %U (our) %U (client) (sw_if_index %d)\n",
7134               format_ip6_address, mp->our_address,
7135               format_ip6_address, mp->client_address,
7136               clib_net_to_host_u32(mp->sw_if_index));
7137
7138     fformat (vam->ofp, "   local cookies %016llx %016llx remote cookie %016llx\n",
7139               clib_net_to_host_u64 (mp->local_cookie[0]),
7140               clib_net_to_host_u64 (mp->local_cookie[1]),
7141               clib_net_to_host_u64 (mp->remote_cookie));
7142
7143     fformat (vam->ofp, "   local session-id %d remote session-id %d\n",
7144               clib_net_to_host_u32 (mp->local_session_id),
7145               clib_net_to_host_u32 (mp->remote_session_id));
7146
7147     fformat (vam->ofp, "   l2 specific sublayer %s\n\n",
7148               mp->l2_sublayer_present ? "preset" : "absent");
7149
7150 }
7151
7152 static void vl_api_sw_if_l2tpv3_tunnel_details_t_handler_json
7153 (vl_api_sw_if_l2tpv3_tunnel_details_t * mp)
7154 {
7155     vat_main_t * vam = &vat_main;
7156     vat_json_node_t *node = NULL;
7157     struct in6_addr addr;
7158
7159     if (VAT_JSON_ARRAY != vam->json_tree.type) {
7160         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
7161         vat_json_init_array(&vam->json_tree);
7162     }
7163     node = vat_json_array_add(&vam->json_tree);
7164
7165     vat_json_init_object(node);
7166
7167     clib_memcpy(&addr, mp->our_address, sizeof(addr));
7168     vat_json_object_add_ip6(node, "our_address", addr);
7169     clib_memcpy(&addr, mp->client_address, sizeof(addr));
7170     vat_json_object_add_ip6(node, "client_address", addr);
7171
7172     vat_json_node_t * lc = vat_json_object_add(node, "local_cookie");
7173     vat_json_init_array(lc);
7174     vat_json_array_add_uint(lc, clib_net_to_host_u64(mp->local_cookie[0]));
7175     vat_json_array_add_uint(lc, clib_net_to_host_u64(mp->local_cookie[1]));
7176     vat_json_object_add_uint(node, "remote_cookie", clib_net_to_host_u64(mp->remote_cookie));
7177
7178     printf("local id: %u", clib_net_to_host_u32(mp->local_session_id));
7179     vat_json_object_add_uint(node, "local_session_id", clib_net_to_host_u32(mp->local_session_id));
7180     vat_json_object_add_uint(node, "remote_session_id", clib_net_to_host_u32(mp->remote_session_id));
7181     vat_json_object_add_string_copy(node, "l2_sublayer", mp->l2_sublayer_present ?
7182             (u8*)"present" : (u8*)"absent");
7183 }
7184
7185 static int api_sw_if_l2tpv3_tunnel_dump (vat_main_t * vam)
7186 {
7187     vl_api_sw_if_l2tpv3_tunnel_dump_t *mp;
7188     f64 timeout;
7189
7190     /* Get list of l2tpv3-tunnel interfaces */
7191     M(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump);
7192     S;
7193
7194     /* Use a control ping for synchronization */
7195     {
7196         vl_api_control_ping_t * mp;
7197         M(CONTROL_PING, control_ping);
7198         S;
7199     }
7200     W;
7201 }
7202
7203
7204 static void vl_api_sw_interface_tap_details_t_handler
7205 (vl_api_sw_interface_tap_details_t * mp)
7206 {
7207     vat_main_t * vam = &vat_main;
7208
7209     fformat(vam->ofp,  "%-16s %d\n",
7210               mp->dev_name,
7211               clib_net_to_host_u32(mp->sw_if_index));
7212 }
7213
7214 static void vl_api_sw_interface_tap_details_t_handler_json
7215 (vl_api_sw_interface_tap_details_t * mp)
7216 {
7217     vat_main_t * vam = &vat_main;
7218     vat_json_node_t *node = NULL;
7219
7220     if (VAT_JSON_ARRAY != vam->json_tree.type) {
7221         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
7222         vat_json_init_array(&vam->json_tree);
7223     }
7224     node = vat_json_array_add(&vam->json_tree);
7225
7226     vat_json_init_object(node);
7227     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
7228     vat_json_object_add_string_copy(node, "dev_name", mp->dev_name);
7229 }
7230
7231 static int api_sw_interface_tap_dump (vat_main_t * vam)
7232 {
7233     vl_api_sw_interface_tap_dump_t *mp;
7234     f64 timeout;
7235
7236     fformat(vam->ofp,  "\n%-16s %s\n", "dev_name", "sw_if_index");
7237     /* Get list of tap interfaces */
7238     M(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump);
7239     S;
7240
7241     /* Use a control ping for synchronization */
7242     {
7243         vl_api_control_ping_t * mp;
7244         M(CONTROL_PING, control_ping);
7245         S;
7246     }
7247     W;
7248 }
7249
7250 static uword unformat_vxlan_decap_next 
7251 (unformat_input_t * input, va_list * args)
7252 {
7253   u32 * result = va_arg (*args, u32 *);
7254   u32 tmp;
7255   
7256   if (unformat (input, "drop"))
7257     *result = VXLAN_INPUT_NEXT_DROP;
7258   else if (unformat (input, "ip4"))
7259     *result = VXLAN_INPUT_NEXT_IP4_INPUT;
7260   else if (unformat (input, "ip6"))
7261     *result = VXLAN_INPUT_NEXT_IP6_INPUT;
7262   else if (unformat (input, "l2"))
7263     *result = VXLAN_INPUT_NEXT_L2_INPUT;
7264   else if (unformat (input, "%d", &tmp))
7265     *result = tmp;
7266   else
7267     return 0;
7268   return 1;
7269 }
7270
7271 static int api_vxlan_add_del_tunnel (vat_main_t * vam)
7272 {
7273     unformat_input_t * line_input = vam->input;
7274     vl_api_vxlan_add_del_tunnel_t *mp;
7275     f64 timeout;
7276     ip4_address_t src4, dst4;
7277     ip6_address_t src6, dst6;
7278     u8 is_add = 1;
7279     u8 ipv4_set = 0, ipv6_set = 0;
7280     u8 src_set = 0;
7281     u8 dst_set = 0;
7282     u32 encap_vrf_id = 0;
7283     u32 decap_next_index = ~0;
7284     u32 vni = 0;
7285
7286     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
7287         if (unformat (line_input, "del"))
7288             is_add = 0;
7289         else if (unformat (line_input, "src %U", 
7290                            unformat_ip4_address, &src4))
7291           {
7292             ipv4_set = 1;
7293             src_set = 1;
7294           }
7295         else if (unformat (line_input, "dst %U",
7296                            unformat_ip4_address, &dst4))
7297           {
7298             ipv4_set = 1;
7299             dst_set = 1;
7300           }
7301         else if (unformat (line_input, "src %U", 
7302                            unformat_ip6_address, &src6))
7303           {
7304             ipv6_set = 1;
7305             src_set = 1;
7306           }
7307         else if (unformat (line_input, "dst %U",
7308                            unformat_ip6_address, &dst6))
7309           {
7310             ipv6_set = 1;
7311             dst_set = 1;
7312           }
7313         else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
7314             ;
7315         else if (unformat (line_input, "decap-next %U", 
7316                            unformat_vxlan_decap_next, &decap_next_index))
7317             ;
7318         else if (unformat (line_input, "vni %d", &vni))
7319             ;
7320         else {
7321             errmsg ("parse error '%U'\n", format_unformat_error, line_input);
7322             return -99;
7323         }
7324     }
7325
7326     if (src_set == 0) {
7327         errmsg ("tunnel src address not specified\n");
7328         return -99;
7329     }
7330     if (dst_set == 0) {
7331         errmsg ("tunnel dst address not specified\n");
7332         return -99;
7333     }
7334
7335     if (ipv4_set && ipv6_set) {
7336         errmsg ("both IPv4 and IPv6 addresses specified");
7337         return -99;
7338     }
7339
7340     if ((vni == 0) || (vni>>24)) {
7341         errmsg ("vni not specified or out of range\n");
7342         return -99;
7343     }
7344
7345     M (VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel);
7346
7347     if (ipv6_set) {
7348         clib_memcpy(&mp->dst_address, &src6, sizeof(src6));
7349         clib_memcpy(&mp->dst_address, &src6, sizeof(dst6));
7350     } else { 
7351         clib_memcpy(&mp->src_address, &src4, sizeof(src4));
7352         clib_memcpy(&mp->dst_address, &dst4, sizeof(dst4));
7353     }
7354     mp->encap_vrf_id = ntohl(encap_vrf_id);
7355     mp->decap_next_index = ntohl(decap_next_index);
7356     mp->vni = ntohl(vni);
7357     mp->is_add = is_add;
7358     mp->is_ipv6 = ipv6_set;
7359
7360     S; W;
7361     /* NOTREACHED */
7362     return 0;
7363 }
7364
7365 static void vl_api_vxlan_tunnel_details_t_handler
7366 (vl_api_vxlan_tunnel_details_t * mp)
7367 {
7368     vat_main_t * vam = &vat_main;
7369
7370     fformat(vam->ofp, "%11d%24U%24U%14d%18d%13d\n",
7371             ntohl(mp->sw_if_index),
7372             format_ip46_address, &(mp->src_address[0]),
7373             format_ip46_address, &(mp->dst_address[0]),
7374             ntohl(mp->encap_vrf_id),
7375             ntohl(mp->decap_next_index),
7376             ntohl(mp->vni));
7377 }
7378
7379 static void vl_api_vxlan_tunnel_details_t_handler_json
7380 (vl_api_vxlan_tunnel_details_t * mp)
7381 {
7382     vat_main_t * vam = &vat_main;
7383     vat_json_node_t *node = NULL;
7384     struct in_addr ip4;
7385     struct in6_addr ip6;
7386
7387     if (VAT_JSON_ARRAY != vam->json_tree.type) {
7388         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
7389         vat_json_init_array(&vam->json_tree);
7390     }
7391     node = vat_json_array_add(&vam->json_tree);
7392
7393     vat_json_init_object(node);
7394     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
7395     if (mp->is_ipv6) {
7396         clib_memcpy(&ip6, &(mp->src_address[0]), sizeof(ip6));
7397         vat_json_object_add_ip6(node, "src_address", ip6);
7398         clib_memcpy(&ip6, &(mp->dst_address[0]), sizeof(ip6));
7399         vat_json_object_add_ip6(node, "dst_address", ip6);
7400     } else {
7401         clib_memcpy(&ip4, &(mp->src_address[0]), sizeof(ip4));
7402         vat_json_object_add_ip4(node, "src_address", ip4);
7403         clib_memcpy(&ip4, &(mp->dst_address[0]), sizeof(ip4));
7404         vat_json_object_add_ip4(node, "dst_address", ip4);
7405     }
7406     vat_json_object_add_uint(node, "encap_vrf_id", ntohl(mp->encap_vrf_id));
7407     vat_json_object_add_uint(node, "decap_next_index", ntohl(mp->decap_next_index));
7408     vat_json_object_add_uint(node, "vni", ntohl(mp->vni));
7409     vat_json_object_add_uint(node, "is_ipv6", mp->is_ipv6 ? 1 : 0);
7410 }
7411
7412 static int api_vxlan_tunnel_dump (vat_main_t * vam)
7413 {
7414     unformat_input_t * i = vam->input;
7415     vl_api_vxlan_tunnel_dump_t *mp;
7416     f64 timeout;
7417     u32 sw_if_index;
7418     u8 sw_if_index_set = 0;
7419
7420     /* Parse args required to build the message */
7421     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7422         if (unformat (i, "sw_if_index %d", &sw_if_index))
7423             sw_if_index_set = 1;
7424         else
7425             break;
7426     }
7427
7428     if (sw_if_index_set == 0) {
7429         sw_if_index = ~0;
7430     }
7431
7432     if (!vam->json_output) {
7433         fformat(vam->ofp, "%11s%24s%24s%14s%18s%13s\n",
7434                 "sw_if_index", "src_address", "dst_address",
7435                 "encap_vrf_id", "decap_next_index", "vni");
7436     }
7437
7438     /* Get list of vxlan-tunnel interfaces */
7439     M(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump);
7440
7441     mp->sw_if_index = htonl(sw_if_index);
7442
7443     S;
7444
7445     /* Use a control ping for synchronization */
7446     {
7447         vl_api_control_ping_t * mp;
7448         M(CONTROL_PING, control_ping);
7449         S;
7450     }
7451     W;
7452 }
7453
7454 static int api_gre_add_del_tunnel (vat_main_t * vam)
7455 {
7456     unformat_input_t * line_input = vam->input;
7457     vl_api_gre_add_del_tunnel_t *mp;
7458     f64 timeout;
7459     ip4_address_t src4, dst4;
7460     u8 is_add = 1;
7461     u8 src_set = 0;
7462     u8 dst_set = 0;
7463     u32 outer_fib_id = 0;
7464
7465     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
7466         if (unformat (line_input, "del"))
7467             is_add = 0;
7468         else if (unformat (line_input, "src %U",
7469                            unformat_ip4_address, &src4))
7470             src_set = 1;
7471         else if (unformat (line_input, "dst %U",
7472                            unformat_ip4_address, &dst4))
7473             dst_set = 1;
7474         else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
7475             ;
7476         else {
7477             errmsg ("parse error '%U'\n", format_unformat_error, line_input);
7478             return -99;
7479         }
7480     }
7481
7482     if (src_set == 0) {
7483         errmsg ("tunnel src address not specified\n");
7484         return -99;
7485     }
7486     if (dst_set == 0) {
7487         errmsg ("tunnel dst address not specified\n");
7488         return -99;
7489     }
7490
7491
7492     M (GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel);
7493
7494     clib_memcpy(&mp->src_address, &src4, sizeof(src4));
7495     clib_memcpy(&mp->dst_address, &dst4, sizeof(dst4));
7496     mp->outer_table_id = ntohl(outer_fib_id);
7497     mp->is_add = is_add;
7498
7499     S; W;
7500     /* NOTREACHED */
7501     return 0;
7502 }
7503
7504 static void vl_api_gre_tunnel_details_t_handler
7505 (vl_api_gre_tunnel_details_t * mp)
7506 {
7507     vat_main_t * vam = &vat_main;
7508
7509     fformat(vam->ofp, "%11d%15U%15U%14d\n",
7510             ntohl(mp->sw_if_index),
7511             format_ip4_address, &mp->src_address,
7512             format_ip4_address, &mp->dst_address,
7513             ntohl(mp->outer_table_id));
7514 }
7515
7516 static void vl_api_gre_tunnel_details_t_handler_json
7517 (vl_api_gre_tunnel_details_t * mp)
7518 {
7519     vat_main_t * vam = &vat_main;
7520     vat_json_node_t *node = NULL;
7521     struct in_addr ip4;
7522
7523     if (VAT_JSON_ARRAY != vam->json_tree.type) {
7524         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
7525         vat_json_init_array(&vam->json_tree);
7526     }
7527     node = vat_json_array_add(&vam->json_tree);
7528
7529     vat_json_init_object(node);
7530     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
7531     clib_memcpy(&ip4, &mp->src_address, sizeof(ip4));
7532     vat_json_object_add_ip4(node, "src_address", ip4);
7533     clib_memcpy(&ip4, &mp->dst_address, sizeof(ip4));
7534     vat_json_object_add_ip4(node, "dst_address", ip4);
7535     vat_json_object_add_uint(node, "outer_fib_id", ntohl(mp->outer_table_id));
7536 }
7537
7538 static int api_gre_tunnel_dump (vat_main_t * vam)
7539 {
7540     unformat_input_t * i = vam->input;
7541     vl_api_gre_tunnel_dump_t *mp;
7542     f64 timeout;
7543     u32 sw_if_index;
7544     u8 sw_if_index_set = 0;
7545
7546     /* Parse args required to build the message */
7547     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7548         if (unformat (i, "sw_if_index %d", &sw_if_index))
7549             sw_if_index_set = 1;
7550         else
7551             break;
7552     }
7553
7554     if (sw_if_index_set == 0) {
7555         sw_if_index = ~0;
7556     }
7557
7558     if (!vam->json_output) {
7559         fformat(vam->ofp, "%11s%15s%15s%14s\n",
7560                 "sw_if_index", "src_address", "dst_address",
7561                 "outer_fib_id");
7562     }
7563
7564     /* Get list of gre-tunnel interfaces */
7565     M(GRE_TUNNEL_DUMP, gre_tunnel_dump);
7566
7567     mp->sw_if_index = htonl(sw_if_index);
7568
7569     S;
7570
7571     /* Use a control ping for synchronization */
7572     {
7573         vl_api_control_ping_t * mp;
7574         M(CONTROL_PING, control_ping);
7575         S;
7576     }
7577     W;
7578 }
7579
7580 static int api_l2_fib_clear_table (vat_main_t * vam)
7581 {
7582 //  unformat_input_t * i = vam->input;
7583     vl_api_l2_fib_clear_table_t *mp;
7584     f64 timeout;
7585
7586     M(L2_FIB_CLEAR_TABLE, l2_fib_clear_table);
7587
7588     S; W;
7589     /* NOTREACHED */
7590     return 0;
7591 }
7592
7593 static int api_l2_interface_efp_filter (vat_main_t * vam)
7594 {
7595     unformat_input_t * i = vam->input;
7596     vl_api_l2_interface_efp_filter_t *mp;
7597     f64 timeout;
7598     u32 sw_if_index;
7599     u8 enable = 1;
7600     u8 sw_if_index_set = 0;
7601
7602     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7603         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
7604             sw_if_index_set = 1;
7605         else if (unformat (i, "sw_if_index %d", &sw_if_index))
7606             sw_if_index_set = 1;
7607         else if (unformat (i, "enable"))
7608             enable = 1;
7609         else if (unformat (i, "disable"))
7610             enable = 0;
7611         else {
7612             clib_warning ("parse error '%U'", format_unformat_error, i);
7613             return -99;
7614         }
7615     }
7616     
7617     if (sw_if_index_set == 0) {
7618         errmsg ("missing sw_if_index\n");
7619         return -99;
7620     }
7621
7622     M(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter);
7623
7624     mp->sw_if_index = ntohl(sw_if_index);
7625     mp->enable_disable = enable;
7626
7627     S; W;
7628     /* NOTREACHED */
7629     return 0;
7630 }
7631
7632 #define foreach_vtr_op                          \
7633 _("disable",  L2_VTR_DISABLED)                  \
7634 _("push-1",  L2_VTR_PUSH_1)                     \
7635 _("push-2",  L2_VTR_PUSH_2)                     \
7636 _("pop-1",  L2_VTR_POP_1)                       \
7637 _("pop-2",  L2_VTR_POP_2)                       \
7638 _("translate-1-1",  L2_VTR_TRANSLATE_1_1)       \
7639 _("translate-1-2",  L2_VTR_TRANSLATE_1_2)       \
7640 _("translate-2-1",  L2_VTR_TRANSLATE_2_1)       \
7641 _("translate-2-2",  L2_VTR_TRANSLATE_2_2)
7642
7643 static int api_l2_interface_vlan_tag_rewrite (vat_main_t * vam)
7644 {
7645     unformat_input_t * i = vam->input;
7646     vl_api_l2_interface_vlan_tag_rewrite_t *mp;
7647     f64 timeout;
7648     u32 sw_if_index;
7649     u8 sw_if_index_set = 0;
7650     u8 vtr_op_set = 0;
7651     u32 vtr_op = 0;
7652     u32 push_dot1q = 1;
7653     u32 tag1 = ~0;
7654     u32 tag2 = ~0;
7655
7656     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7657         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
7658             sw_if_index_set = 1;
7659         else if (unformat (i, "sw_if_index %d", &sw_if_index))
7660             sw_if_index_set = 1;
7661         else if (unformat (i, "vtr_op %d", &vtr_op))
7662             vtr_op_set = 1;
7663 #define _(n,v) else if (unformat(i, n)) {vtr_op = v; vtr_op_set = 1;}
7664         foreach_vtr_op
7665 #undef _
7666         
7667         else if (unformat (i, "push_dot1q %d", &push_dot1q))
7668             ;
7669         else if (unformat (i, "tag1 %d", &tag1))
7670             ;
7671         else if (unformat (i, "tag2 %d", &tag2))
7672             ;
7673         else {
7674             clib_warning ("parse error '%U'", format_unformat_error, i);
7675             return -99;
7676         }
7677     }
7678     
7679     if ((sw_if_index_set == 0)||(vtr_op_set == 0)) {
7680         errmsg ("missing vtr operation or sw_if_index\n");
7681         return -99;
7682     }
7683
7684     M(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)
7685
7686     mp->sw_if_index = ntohl(sw_if_index);
7687     mp->vtr_op = ntohl(vtr_op);
7688     mp->push_dot1q = ntohl(push_dot1q);
7689     mp->tag1 = ntohl(tag1);
7690     mp->tag2 = ntohl(tag2);
7691
7692     S; W;
7693     /* NOTREACHED */
7694     return 0;
7695 }
7696
7697 static int api_create_vhost_user_if (vat_main_t * vam)
7698 {
7699     unformat_input_t * i = vam->input;
7700     vl_api_create_vhost_user_if_t *mp;
7701     f64 timeout;
7702     u8 * file_name;
7703     u8 is_server = 0;
7704     u8 file_name_set = 0;
7705     u32 custom_dev_instance = ~0;
7706     u8 hwaddr[6];
7707     u8 use_custom_mac = 0;
7708
7709     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7710       if (unformat (i, "socket %s", &file_name)) {
7711         file_name_set = 1;
7712       }
7713       else if (unformat (i, "renumber %"PRIu32, &custom_dev_instance))
7714         ;
7715       else if (unformat (i, "mac %U", unformat_ethernet_address, hwaddr))
7716         use_custom_mac = 1;
7717       else if (unformat (i, "server"))
7718         is_server = 1;
7719       else
7720         break;
7721     }
7722
7723     if (file_name_set == 0) {
7724       errmsg ("missing socket file name\n");
7725       return -99;
7726     }
7727
7728     if (vec_len (file_name) > 255) {
7729       errmsg ("socket file name too long\n");
7730       return -99;
7731     }
7732     vec_add1 (file_name, 0);
7733
7734     M(CREATE_VHOST_USER_IF, create_vhost_user_if);
7735
7736     mp->is_server = is_server;
7737     clib_memcpy(mp->sock_filename, file_name, vec_len(file_name));
7738     vec_free(file_name);
7739     if (custom_dev_instance != ~0) {
7740         mp->renumber = 1;
7741         mp->custom_dev_instance = ntohl(custom_dev_instance);
7742     }
7743     mp->use_custom_mac = use_custom_mac;
7744     clib_memcpy(mp->mac_address, hwaddr, 6);
7745
7746     S; W;
7747     /* NOTREACHED */
7748     return 0;
7749 }
7750
7751 static int api_modify_vhost_user_if (vat_main_t * vam)
7752 {
7753     unformat_input_t * i = vam->input;
7754     vl_api_modify_vhost_user_if_t *mp;
7755     f64 timeout;
7756     u8 * file_name;
7757     u8 is_server = 0;
7758     u8 file_name_set = 0;
7759     u32 custom_dev_instance = ~0;
7760     u8 sw_if_index_set = 0;
7761     u32 sw_if_index = (u32)~0;
7762
7763     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7764       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
7765           sw_if_index_set = 1;
7766       else if (unformat (i, "sw_if_index %d", &sw_if_index))
7767           sw_if_index_set = 1;
7768       else if (unformat (i, "socket %s", &file_name)) {
7769         file_name_set = 1;
7770       }
7771       else if (unformat (i, "renumber %"PRIu32, &custom_dev_instance))
7772         ;
7773       else if (unformat (i, "server"))
7774         is_server = 1;
7775       else
7776         break;
7777     }
7778
7779     if (sw_if_index_set == 0) {
7780        errmsg ("missing sw_if_index or interface name\n");
7781        return -99;
7782     }
7783
7784     if (file_name_set == 0) {
7785       errmsg ("missing socket file name\n");
7786       return -99;
7787     }
7788
7789     if (vec_len (file_name) > 255) {
7790       errmsg ("socket file name too long\n");
7791       return -99;
7792     }
7793     vec_add1 (file_name, 0);
7794
7795     M(MODIFY_VHOST_USER_IF, modify_vhost_user_if);
7796
7797     mp->sw_if_index = ntohl(sw_if_index);
7798     mp->is_server = is_server;
7799     clib_memcpy(mp->sock_filename, file_name, vec_len(file_name));
7800     vec_free(file_name);
7801     if (custom_dev_instance != ~0) {
7802         mp->renumber = 1;
7803         mp->custom_dev_instance = ntohl(custom_dev_instance);
7804     }
7805
7806     S; W;
7807     /* NOTREACHED */
7808     return 0;
7809 }
7810
7811 static int api_delete_vhost_user_if (vat_main_t * vam)
7812 {
7813     unformat_input_t * i = vam->input;
7814     vl_api_delete_vhost_user_if_t *mp;
7815     f64 timeout;
7816     u32 sw_if_index = ~0;
7817     u8 sw_if_index_set = 0;
7818
7819     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
7820       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
7821           sw_if_index_set = 1;
7822       else if (unformat (i, "sw_if_index %d", &sw_if_index))
7823           sw_if_index_set = 1;
7824       else
7825         break;
7826     }
7827
7828     if (sw_if_index_set == 0) {
7829        errmsg ("missing sw_if_index or interface name\n");
7830        return -99;
7831     }
7832
7833
7834     M(DELETE_VHOST_USER_IF, delete_vhost_user_if);
7835
7836     mp->sw_if_index = ntohl(sw_if_index);
7837
7838     S; W;
7839     /* NOTREACHED */
7840     return 0;
7841 }
7842
7843 static void vl_api_sw_interface_vhost_user_details_t_handler
7844 (vl_api_sw_interface_vhost_user_details_t * mp)
7845 {
7846     vat_main_t * vam = &vat_main;
7847
7848     fformat(vam->ofp, "%-25s %3" PRIu32 " %6" PRIu32 " %8x %6d %7d %s\n",
7849             (char *)mp->interface_name,
7850             ntohl(mp->sw_if_index), ntohl(mp->virtio_net_hdr_sz),
7851             clib_net_to_host_u64(mp->features), mp->is_server,
7852             ntohl(mp->num_regions), (char *)mp->sock_filename);
7853     fformat(vam->ofp, "    Status: '%s'\n", strerror(ntohl(mp->sock_errno)));
7854 }
7855
7856 static void vl_api_sw_interface_vhost_user_details_t_handler_json
7857 (vl_api_sw_interface_vhost_user_details_t * mp)
7858 {
7859     vat_main_t * vam = &vat_main;
7860     vat_json_node_t *node = NULL;
7861
7862     if (VAT_JSON_ARRAY != vam->json_tree.type) {
7863         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
7864         vat_json_init_array(&vam->json_tree);
7865     }
7866     node = vat_json_array_add(&vam->json_tree);
7867
7868     vat_json_init_object(node);
7869     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
7870     vat_json_object_add_string_copy(node, "interface_name", mp->interface_name);
7871     vat_json_object_add_uint(node, "virtio_net_hdr_sz", ntohl(mp->virtio_net_hdr_sz));
7872     vat_json_object_add_uint(node, "features", clib_net_to_host_u64(mp->features));
7873     vat_json_object_add_uint(node, "is_server", mp->is_server);
7874     vat_json_object_add_string_copy(node, "sock_filename", mp->sock_filename);
7875     vat_json_object_add_uint(node, "num_regions", ntohl(mp->num_regions));
7876     vat_json_object_add_uint(node, "sock_errno", ntohl(mp->sock_errno));
7877 }
7878
7879 static int api_sw_interface_vhost_user_dump (vat_main_t * vam)
7880 {
7881     vl_api_sw_interface_vhost_user_dump_t *mp;
7882     f64 timeout;
7883     fformat(vam->ofp, "Interface name           idx hdr_sz features server regions filename\n");
7884
7885     /* Get list of vhost-user interfaces */
7886     M(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump);
7887     S;
7888
7889     /* Use a control ping for synchronization */
7890     {
7891         vl_api_control_ping_t * mp;
7892         M(CONTROL_PING, control_ping);
7893         S;
7894     }
7895     W;
7896 }
7897
7898 static int api_show_version (vat_main_t * vam)
7899 {
7900     vl_api_show_version_t *mp;
7901     f64 timeout;
7902
7903     M(SHOW_VERSION, show_version);
7904
7905     S; W;
7906     /* NOTREACHED */
7907     return 0;
7908 }
7909
7910 static uword unformat_nsh_gre_decap_next 
7911 (unformat_input_t * input, va_list * args)
7912 {
7913   u32 * result = va_arg (*args, u32 *);
7914   u32 tmp;
7915   
7916   if (unformat (input, "drop"))
7917     *result = NSH_GRE_INPUT_NEXT_DROP;
7918   else if (unformat (input, "ip4"))
7919     *result = NSH_GRE_INPUT_NEXT_IP4_INPUT;
7920   else if (unformat (input, "ip6"))
7921     *result = NSH_GRE_INPUT_NEXT_IP6_INPUT;
7922   else if (unformat (input, "ethernet"))
7923     *result = NSH_GRE_INPUT_NEXT_ETHERNET_INPUT;
7924   else if (unformat (input, "%d", &tmp))
7925     *result = tmp;
7926   else
7927     return 0;
7928   return 1;
7929 }
7930
7931 static int api_nsh_gre_add_del_tunnel (vat_main_t * vam)
7932 {
7933     unformat_input_t * line_input = vam->input;
7934     vl_api_nsh_gre_add_del_tunnel_t *mp;
7935     f64 timeout;
7936     ip4_address_t src, dst;
7937     u8 is_add = 1;
7938     u8 src_set = 0;
7939     u8 dst_set = 0;
7940     u32 encap_vrf_id = 0;
7941     u32 decap_vrf_id = 0;
7942     u8 ver_o_c = 0;
7943     u8 md_type = 0;
7944     u8 next_protocol = 1; /* ip4 */
7945     u32 spi;
7946     u8 spi_set = 0;
7947     u32 si;
7948     u8 si_set = 0;
7949     u32 spi_si;
7950     u32 c1 = 0;
7951     u32 c2 = 0;
7952     u32 c3 = 0;
7953     u32 c4 = 0;
7954     u32 *tlvs = 0;
7955     u32 decap_next_index = NSH_GRE_INPUT_NEXT_IP4_INPUT;
7956     u32 tmp;
7957     int i;
7958
7959     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
7960         if (unformat (line_input, "del"))
7961             is_add = 0;
7962         else if (unformat (line_input, "src %U", 
7963                            unformat_ip4_address, &src))
7964             src_set = 1;
7965         else if (unformat (line_input, "dst %U",
7966                            unformat_ip4_address, &dst))
7967             dst_set = 1;
7968         else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
7969             ;
7970         else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
7971             ;
7972         else if (unformat (line_input, "decap-next %U", 
7973                            unformat_nsh_gre_decap_next, &decap_next_index))
7974             ;
7975         else if (unformat (line_input, "version %d", &tmp))
7976             ver_o_c |= (tmp & 3) << 6;
7977         else if (unformat (line_input, "o-bit %d", &tmp))
7978             ver_o_c |= (tmp & 1) << 5;
7979         else if (unformat (line_input, "c-bit %d", &tmp))
7980             ver_o_c |= (tmp & 1) << 4;
7981         else if (unformat (line_input, "md-type %d", &tmp))
7982             md_type = tmp;
7983         else if (unformat(line_input, "next-ip4"))
7984             next_protocol = 1;
7985         else if (unformat(line_input, "next-ip6"))
7986             next_protocol = 2;
7987         else if (unformat(line_input, "next-ethernet"))
7988             next_protocol = 3;
7989         else if (unformat (line_input, "c1 %d", &c1))
7990             ;
7991         else if (unformat (line_input, "c2 %d", &c2))
7992             ;
7993         else if (unformat (line_input, "c3 %d", &c3))
7994             ;
7995         else if (unformat (line_input, "c4 %d", &c4))
7996             ;
7997         else if (unformat (line_input, "spi %d", &spi))
7998             spi_set = 1;
7999         else if (unformat (line_input, "si %d", &si))
8000             si_set = 1;
8001         else if (unformat (line_input, "tlv %x"))
8002             vec_add1 (tlvs, tmp);
8003         else {
8004             errmsg ("parse error '%U'\n", format_unformat_error, line_input);
8005             return -99;
8006         }
8007     }
8008
8009     if (src_set == 0) {
8010         errmsg ("tunnel src address not specified\n");
8011         return -99;
8012     }
8013     if (dst_set == 0) {
8014         errmsg ("tunnel dst address not specified\n");
8015         return -99;
8016     }
8017
8018     if (spi_set == 0) {
8019         errmsg ("spi not specified\n");
8020         return -99;
8021     }
8022
8023     if (si_set == 0) {
8024         errmsg ("si not specified\n");
8025         return -99;
8026     }
8027
8028     M2 (NSH_GRE_ADD_DEL_TUNNEL, nsh_gre_add_del_tunnel,
8029         sizeof(u32) * vec_len (tlvs));
8030     
8031     spi_si = (spi<<8) | si;
8032
8033     mp->src = src.as_u32;
8034     mp->dst = dst.as_u32;
8035     mp->encap_vrf_id = ntohl(encap_vrf_id);
8036     mp->decap_vrf_id = ntohl(decap_vrf_id);
8037     mp->decap_next_index = ntohl(decap_next_index);
8038     mp->tlv_len_in_words = vec_len (tlvs);
8039     mp->is_add = is_add;
8040     mp->ver_o_c = ver_o_c;
8041     mp->length = 6 + vec_len(tlvs);
8042     mp->md_type = md_type;
8043     mp->next_protocol = next_protocol;
8044     mp->spi_si = ntohl(spi_si);
8045     mp->c1 = ntohl(c1);
8046     mp->c2 = ntohl(c2);
8047     mp->c3 = ntohl(c3);
8048     mp->c4 = ntohl(c4);
8049     
8050     for (i = 0; i < vec_len(tlvs); i++)
8051         mp->tlvs[i] = ntohl(tlvs[i]);
8052
8053     vec_free (tlvs);
8054
8055     S; W;
8056     /* NOTREACHED */
8057     return 0;
8058 }
8059
8060 static uword unformat_nsh_vxlan_gpe_decap_next 
8061 (unformat_input_t * input, va_list * args)
8062 {
8063   u32 * result = va_arg (*args, u32 *);
8064   u32 tmp;
8065   
8066   if (unformat (input, "drop"))
8067     *result = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
8068   else if (unformat (input, "ip4"))
8069     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
8070   else if (unformat (input, "ip6"))
8071     *result = NSH_VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
8072   else if (unformat (input, "ethernet"))
8073     *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
8074   else if (unformat (input, "nsh-vxlan-gpe"))
8075       *result = NSH_VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
8076   else if (unformat (input, "%d", &tmp))
8077     *result = tmp;
8078   else
8079     return 0;
8080   return 1;
8081 }
8082
8083 static int api_nsh_vxlan_gpe_add_del_tunnel (vat_main_t * vam)
8084 {
8085     unformat_input_t * line_input = vam->input;
8086     vl_api_nsh_vxlan_gpe_add_del_tunnel_t *mp;
8087     f64 timeout;
8088     ip4_address_t src, dst;
8089     u8 is_add = 1;
8090     u8 src_set = 0;
8091     u8 dst_set = 0;
8092     u32 encap_vrf_id = 0;
8093     u32 decap_vrf_id = 0;
8094     u8 ver_o_c = 0;
8095     u8 md_type = 0;
8096     u8 next_protocol = 1; /* ip4 */
8097     u32 spi;
8098     u8 spi_set = 0;
8099     u32 si;
8100     u8 si_set = 0;
8101     u32 spi_si;
8102     u32 c1 = 0;
8103     u32 c2 = 0;
8104     u32 c3 = 0;
8105     u32 c4 = 0;
8106     u32 *tlvs = 0;
8107     u32 decap_next_index = NSH_GRE_INPUT_NEXT_IP4_INPUT;
8108     u32 vni;
8109     u8 vni_set = 0;
8110     u32 tmp;
8111     int i;
8112
8113     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
8114         if (unformat (line_input, "del"))
8115             is_add = 0;
8116         else if (unformat (line_input, "src %U", 
8117                            unformat_ip4_address, &src))
8118             src_set = 1;
8119         else if (unformat (line_input, "dst %U",
8120                            unformat_ip4_address, &dst))
8121             dst_set = 1;
8122         else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
8123             ;
8124         else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
8125             ;
8126         else if (unformat (line_input, "decap-next %U", 
8127                            unformat_nsh_vxlan_gpe_decap_next, 
8128                            &decap_next_index))
8129             ;
8130         else if (unformat (line_input, "vni %d", &vni))
8131             vni_set = 1;
8132         else if (unformat (line_input, "version %d", &tmp))
8133             ver_o_c |= (tmp & 3) << 6;
8134         else if (unformat (line_input, "o-bit %d", &tmp))
8135             ver_o_c |= (tmp & 1) << 5;
8136         else if (unformat (line_input, "c-bit %d", &tmp))
8137             ver_o_c |= (tmp & 1) << 4;
8138         else if (unformat (line_input, "md-type %d", &tmp))
8139             md_type = tmp;
8140         else if (unformat(line_input, "next-ip4"))
8141             next_protocol = 1;
8142         else if (unformat(line_input, "next-ip6"))
8143             next_protocol = 2;
8144         else if (unformat(line_input, "next-ethernet"))
8145             next_protocol = 3;
8146         else if (unformat (line_input, "c1 %d", &c1))
8147             ;
8148         else if (unformat (line_input, "c2 %d", &c2))
8149             ;
8150         else if (unformat (line_input, "c3 %d", &c3))
8151             ;
8152         else if (unformat (line_input, "c4 %d", &c4))
8153             ;
8154         else if (unformat (line_input, "spi %d", &spi))
8155             spi_set = 1;
8156         else if (unformat (line_input, "si %d", &si))
8157             si_set = 1;
8158         else if (unformat (line_input, "tlv %x"))
8159             vec_add1 (tlvs, tmp);
8160         else {
8161             errmsg ("parse error '%U'\n", format_unformat_error, line_input);
8162             return -99;
8163         }
8164     }
8165
8166     if (src_set == 0) {
8167         errmsg ("tunnel src address not specified\n");
8168         return -99;
8169     }
8170     if (dst_set == 0) {
8171         errmsg ("tunnel dst address not specified\n");
8172         return -99;
8173     }
8174
8175     if (spi_set == 0) {
8176         errmsg ("spi not specified\n");
8177         return -99;
8178     }
8179
8180     if (si_set == 0) {
8181         errmsg ("si not specified\n");
8182         return -99;
8183     }
8184     if (vni_set == 0) {
8185         errmsg ("vni not specified\n");
8186         return -99;
8187     }
8188
8189     M2 (NSH_VXLAN_GPE_ADD_DEL_TUNNEL, nsh_vxlan_gpe_add_del_tunnel,
8190         sizeof(u32) * vec_len (tlvs));
8191     
8192     spi_si = (spi<<8) | si;
8193
8194     mp->src = src.as_u32;
8195     mp->dst = dst.as_u32;
8196     mp->encap_vrf_id = ntohl(encap_vrf_id);
8197     mp->decap_vrf_id = ntohl(decap_vrf_id);
8198     mp->decap_next_index = ntohl(decap_next_index);
8199     mp->tlv_len_in_words = vec_len (tlvs);
8200     mp->vni = ntohl(vni);
8201     mp->is_add = is_add;
8202     mp->ver_o_c = ver_o_c;
8203     mp->length = 6 + vec_len(tlvs);
8204     mp->md_type = md_type;
8205     mp->next_protocol = next_protocol;
8206     mp->spi_si = ntohl(spi_si);
8207     mp->c1 = ntohl(c1);
8208     mp->c2 = ntohl(c2);
8209     mp->c3 = ntohl(c3);
8210     mp->c4 = ntohl(c4);
8211     
8212     for (i = 0; i < vec_len(tlvs); i++)
8213         mp->tlvs[i] = ntohl(tlvs[i]);
8214
8215     vec_free (tlvs);
8216
8217     S; W;
8218     /* NOTREACHED */
8219     return 0;
8220 }
8221
8222 u8 * format_l2_fib_mac_address (u8 * s, va_list * args)
8223 {
8224   u8 * a = va_arg (*args, u8 *);
8225
8226   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
8227                  a[2], a[3], a[4], a[5], a[6], a[7]);
8228 }
8229
8230 static void vl_api_l2_fib_table_entry_t_handler
8231 (vl_api_l2_fib_table_entry_t * mp)
8232 {
8233     vat_main_t * vam = &vat_main;
8234
8235     fformat(vam->ofp, "%3" PRIu32 "    %U    %3" PRIu32
8236             "       %d       %d     %d\n",
8237             ntohl(mp->bd_id), format_l2_fib_mac_address, &mp->mac,
8238             ntohl(mp->sw_if_index), mp->static_mac, mp->filter_mac,
8239             mp->bvi_mac);
8240 }
8241
8242 static void vl_api_l2_fib_table_entry_t_handler_json
8243 (vl_api_l2_fib_table_entry_t * mp)
8244 {
8245     vat_main_t * vam = &vat_main;
8246     vat_json_node_t *node = NULL;
8247
8248     if (VAT_JSON_ARRAY != vam->json_tree.type) {
8249         ASSERT(VAT_JSON_NONE == vam->json_tree.type);
8250         vat_json_init_array(&vam->json_tree);
8251     }
8252     node = vat_json_array_add(&vam->json_tree);
8253
8254     vat_json_init_object(node);
8255     vat_json_object_add_uint(node, "bd_id", ntohl(mp->bd_id));
8256     vat_json_object_add_uint(node, "mac", clib_net_to_host_u64(mp->mac));
8257     vat_json_object_add_uint(node, "sw_if_index", ntohl(mp->sw_if_index));
8258     vat_json_object_add_uint(node, "static_mac", mp->static_mac);
8259     vat_json_object_add_uint(node, "filter_mac", mp->filter_mac);
8260     vat_json_object_add_uint(node, "bvi_mac", mp->bvi_mac);
8261 }
8262
8263 static int api_l2_fib_table_dump (vat_main_t * vam)
8264 {
8265     unformat_input_t * i = vam->input;
8266     vl_api_l2_fib_table_dump_t *mp;
8267     f64 timeout;
8268     u32 bd_id;
8269     u8 bd_id_set = 0;
8270
8271     /* Parse args required to build the message */
8272     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8273         if (unformat (i, "bd_id %d", &bd_id))
8274             bd_id_set = 1;
8275         else
8276             break;
8277     }
8278
8279     if (bd_id_set == 0) {
8280         errmsg ("missing bridge domain\n");
8281         return -99;
8282     }
8283
8284     fformat(vam->ofp, "BD-ID     Mac Address      sw-ndx  Static  Filter  BVI\n");
8285
8286     /* Get list of l2 fib entries */
8287     M(L2_FIB_TABLE_DUMP, l2_fib_table_dump);
8288
8289     mp->bd_id = ntohl(bd_id);
8290     S;
8291
8292     /* Use a control ping for synchronization */
8293     {
8294         vl_api_control_ping_t * mp;
8295         M(CONTROL_PING, control_ping);
8296         S;
8297     }
8298     W;
8299 }
8300
8301
8302 static int
8303 api_interface_name_renumber (vat_main_t * vam)
8304 {
8305     unformat_input_t * line_input = vam->input;
8306     vl_api_interface_name_renumber_t *mp;
8307     u32 sw_if_index = ~0;
8308     f64 timeout;
8309     u32 new_show_dev_instance = ~0;
8310
8311     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
8312         if (unformat (line_input, "%U", unformat_sw_if_index, vam, 
8313                       &sw_if_index))
8314             ;
8315         else if (unformat (line_input, "sw_if_index %d", &sw_if_index))
8316             ;
8317         else if (unformat (line_input, "new_show_dev_instance %d", 
8318                            &new_show_dev_instance))
8319             ;
8320         else
8321             break;
8322     }
8323
8324     if (sw_if_index == ~0) {
8325         errmsg ("missing interface name or sw_if_index\n");
8326         return -99;
8327     }
8328
8329     if (new_show_dev_instance == ~0) {
8330         errmsg ("missing new_show_dev_instance\n");
8331         return -99;
8332     }
8333
8334     M(INTERFACE_NAME_RENUMBER, interface_name_renumber);
8335
8336     mp->sw_if_index = ntohl (sw_if_index);
8337     mp->new_show_dev_instance = ntohl (new_show_dev_instance);
8338
8339     S; W;
8340 }
8341
8342 static int
8343 api_want_ip4_arp_events (vat_main_t * vam)
8344 {
8345     unformat_input_t * line_input = vam->input;
8346     vl_api_want_ip4_arp_events_t * mp;
8347     f64 timeout;
8348     ip4_address_t address;
8349     int address_set = 0;
8350     u32 enable_disable = 1;
8351
8352     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
8353         if (unformat (line_input, "address %U", 
8354                       unformat_ip4_address, &address))
8355             address_set = 1;
8356         else if (unformat (line_input, "del"))
8357             enable_disable = 0;
8358         else
8359             break;
8360     }
8361     
8362     if (address_set == 0) {
8363         errmsg ("missing addresses\n");
8364         return -99;
8365     }
8366         
8367     M(WANT_IP4_ARP_EVENTS, want_ip4_arp_events);
8368     mp->enable_disable = enable_disable;
8369     mp->pid = getpid();
8370     mp->address = address.as_u32;
8371
8372     S; W; 
8373 }
8374
8375 static int api_input_acl_set_interface (vat_main_t * vam)
8376 {
8377     unformat_input_t * i = vam->input;
8378     vl_api_input_acl_set_interface_t *mp;
8379     f64 timeout;
8380     u32 sw_if_index;
8381     int sw_if_index_set;
8382     u32 ip4_table_index = ~0;
8383     u32 ip6_table_index = ~0;
8384     u32 l2_table_index = ~0;
8385     u8 is_add = 1;
8386
8387     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8388         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
8389             sw_if_index_set = 1;
8390         else if (unformat (i, "sw_if_index %d", &sw_if_index))
8391             sw_if_index_set = 1;
8392         else if (unformat (i, "del"))
8393             is_add = 0;
8394         else if (unformat (i, "ip4-table %d", &ip4_table_index))
8395             ;
8396         else if (unformat (i, "ip6-table %d", &ip6_table_index))
8397             ;
8398         else if (unformat (i, "l2-table %d", &l2_table_index))
8399             ;
8400         else {
8401             clib_warning ("parse error '%U'", format_unformat_error, i);
8402             return -99;
8403         }
8404     }
8405
8406     if (sw_if_index_set == 0) {
8407         errmsg ("missing interface name or sw_if_index\n");
8408         return -99;
8409     }
8410
8411     M(INPUT_ACL_SET_INTERFACE, input_acl_set_interface);
8412
8413     mp->sw_if_index = ntohl(sw_if_index);
8414     mp->ip4_table_index = ntohl(ip4_table_index);
8415     mp->ip6_table_index = ntohl(ip6_table_index);
8416     mp->l2_table_index = ntohl(l2_table_index);
8417     mp->is_add = is_add;
8418
8419     S; W;
8420     /* NOTREACHED */
8421     return 0;
8422 }
8423
8424 static int
8425 api_ip_address_dump (vat_main_t * vam)
8426 {
8427     unformat_input_t * i = vam->input;
8428     vl_api_ip_address_dump_t * mp;
8429     u32 sw_if_index = ~0;
8430     u8 sw_if_index_set = 0;
8431     u8 ipv4_set = 0;
8432     u8 ipv6_set = 0;
8433     f64 timeout;
8434
8435     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8436         if (unformat (i, "sw_if_index %d", &sw_if_index))
8437             sw_if_index_set = 1;
8438         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
8439             sw_if_index_set = 1;
8440         else if (unformat (i, "ipv4"))
8441             ipv4_set = 1;
8442         else if (unformat (i, "ipv6"))
8443             ipv6_set = 1;
8444         else
8445             break;
8446     }
8447
8448     if (ipv4_set && ipv6_set) {
8449         errmsg ("ipv4 and ipv6 flags cannot be both set\n");
8450         return -99;
8451     }
8452
8453     if ((!ipv4_set) && (!ipv6_set)) {
8454         errmsg ("no ipv4 nor ipv6 flag set\n");
8455         return -99;
8456     }
8457
8458     if (sw_if_index_set == 0) {
8459         errmsg ("missing interface name or sw_if_index\n");
8460         return -99;
8461     }
8462
8463     vam->current_sw_if_index = sw_if_index;
8464     vam->is_ipv6 = ipv6_set;
8465
8466     M(IP_ADDRESS_DUMP, ip_address_dump);
8467     mp->sw_if_index = ntohl(sw_if_index);
8468     mp->is_ipv6 = ipv6_set;
8469     S;
8470
8471     /* Use a control ping for synchronization */
8472     {
8473         vl_api_control_ping_t * mp;
8474         M(CONTROL_PING, control_ping);
8475         S;
8476     }
8477     W;
8478 }
8479
8480 static int
8481 api_ip_dump (vat_main_t * vam)
8482 {
8483     vl_api_ip_dump_t * mp;
8484     unformat_input_t * in = vam->input;
8485     int ipv4_set = 0;
8486     int ipv6_set = 0;
8487     int is_ipv6;
8488     f64 timeout;
8489     int i;
8490
8491     while (unformat_check_input (in) != UNFORMAT_END_OF_INPUT) {
8492         if (unformat (in, "ipv4"))
8493             ipv4_set = 1;
8494         else if (unformat (in, "ipv6"))
8495             ipv6_set = 1;
8496         else
8497             break;
8498     }
8499
8500     if (ipv4_set && ipv6_set) {
8501         errmsg ("ipv4 and ipv6 flags cannot be both set\n");
8502         return -99;
8503     }
8504
8505     if ((!ipv4_set) && (!ipv6_set)) {
8506         errmsg ("no ipv4 nor ipv6 flag set\n");
8507         return -99;
8508     }
8509
8510     is_ipv6 = ipv6_set;
8511     vam->is_ipv6 = is_ipv6;
8512
8513     /* free old data */
8514     for (i = 0; i < vec_len(vam->ip_details_by_sw_if_index[is_ipv6]); i++) {
8515         vec_free(vam->ip_details_by_sw_if_index[is_ipv6][i].addr);
8516     }
8517     vec_free(vam->ip_details_by_sw_if_index[is_ipv6]);
8518
8519     M(IP_DUMP, ip_dump);
8520     mp->is_ipv6 = ipv6_set;
8521     S;
8522
8523     /* Use a control ping for synchronization */
8524     {
8525         vl_api_control_ping_t * mp;
8526         M(CONTROL_PING, control_ping);
8527         S;
8528     }
8529     W;
8530 }
8531
8532 static int
8533 api_ipsec_spd_add_del (vat_main_t * vam)
8534 {
8535 #if DPDK > 0
8536     unformat_input_t * i = vam->input;
8537     vl_api_ipsec_spd_add_del_t *mp;
8538     f64 timeout;
8539     u32 spd_id = ~0;
8540     u8 is_add = 1;
8541
8542     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8543         if (unformat (i, "spd_id %d", &spd_id))
8544             ;
8545         else if (unformat (i, "del"))
8546             is_add = 0;
8547         else {
8548             clib_warning ("parse error '%U'", format_unformat_error, i);
8549             return -99;
8550         }
8551     }
8552     if (spd_id == ~0) {
8553         errmsg ("spd_id must be set\n");
8554         return -99;
8555     }
8556
8557     M(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del);
8558
8559     mp->spd_id = ntohl(spd_id);
8560     mp->is_add = is_add;
8561
8562     S; W;
8563     /* NOTREACHED */
8564     return 0;
8565 #else
8566     clib_warning ("unsupported (no dpdk)");
8567     return -99;
8568 #endif
8569 }
8570
8571 static int
8572 api_ipsec_interface_add_del_spd (vat_main_t * vam)
8573 {
8574 #if DPDK > 0
8575     unformat_input_t * i = vam->input;
8576     vl_api_ipsec_interface_add_del_spd_t *mp;
8577     f64 timeout;
8578     u32 sw_if_index;
8579     u8 sw_if_index_set = 0;
8580     u32 spd_id = (u32) ~0;
8581     u8 is_add = 1;
8582
8583     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8584         if (unformat (i, "del"))
8585             is_add = 0;
8586         else if (unformat (i, "spd_id %d", &spd_id))
8587             ;
8588         else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
8589             sw_if_index_set = 1;
8590         else if (unformat (i, "sw_if_index %d", &sw_if_index))
8591             sw_if_index_set = 1;
8592         else {
8593             clib_warning ("parse error '%U'", format_unformat_error, i);
8594             return -99;
8595         }
8596
8597     }
8598
8599     if (spd_id == (u32) ~0) {
8600         errmsg ("spd_id must be set\n");
8601         return -99;
8602     }
8603
8604     if (sw_if_index_set == 0) {
8605         errmsg ("missing interface name or sw_if_index\n");
8606         return -99;
8607     }
8608
8609     M(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd);
8610
8611     mp->spd_id = ntohl(spd_id);
8612     mp->sw_if_index = ntohl (sw_if_index);
8613     mp->is_add = is_add;
8614
8615     S; W;
8616     /* NOTREACHED */
8617     return 0;
8618 #else
8619     clib_warning ("unsupported (no dpdk)");
8620     return -99;
8621 #endif
8622 }
8623
8624 static int
8625 api_ipsec_spd_add_del_entry (vat_main_t * vam)
8626 {
8627 #if DPDK > 0
8628     unformat_input_t * i = vam->input;
8629     vl_api_ipsec_spd_add_del_entry_t *mp;
8630     f64 timeout;
8631     u8 is_add = 1, is_outbound = 0, is_ipv6 = 0, is_ip_any = 1;
8632     u32 spd_id, sa_id, protocol = 0, policy = 0;
8633     i32 priority;
8634     u32 rport_start = 0, rport_stop = (u32) ~0;
8635     u32 lport_start = 0, lport_stop = (u32) ~0;
8636     ip4_address_t laddr4_start, laddr4_stop, raddr4_start, raddr4_stop;
8637     ip6_address_t laddr6_start, laddr6_stop, raddr6_start, raddr6_stop;
8638
8639     laddr4_start.as_u32 = raddr4_start.as_u32 = 0;
8640     laddr4_stop.as_u32 = raddr4_stop.as_u32 = (u32) ~0;
8641     laddr6_start.as_u64[0] = raddr6_start.as_u64[0] = 0;
8642     laddr6_start.as_u64[1] = raddr6_start.as_u64[1] = 0;
8643     laddr6_stop.as_u64[0] = raddr6_stop.as_u64[0] = (u64) ~0;
8644     laddr6_stop.as_u64[1] = raddr6_stop.as_u64[1] = (u64) ~0;
8645
8646     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8647         if (unformat (i, "del"))
8648             is_add = 0;
8649         if (unformat (i, "outbound"))
8650             is_outbound = 1;
8651         if (unformat (i, "inbound"))
8652             is_outbound = 0;
8653         else if (unformat (i, "spd_id %d", &spd_id))
8654             ;
8655         else if (unformat (i, "sa_id %d", &sa_id))
8656             ;
8657         else if (unformat (i, "priority %d", &priority))
8658             ;
8659         else if (unformat (i, "protocol %d", &protocol))
8660             ;
8661         else if (unformat (i, "lport_start %d", &lport_start))
8662             ;
8663         else if (unformat (i, "lport_stop %d", &lport_stop))
8664             ;
8665         else if (unformat (i, "rport_start %d", &rport_start))
8666             ;
8667         else if (unformat (i, "rport_stop %d", &rport_stop))
8668             ;
8669         else if (unformat (i, "laddr_start %U", unformat_ip4_address, &laddr4_start))
8670           {
8671             is_ipv6 = 0;
8672             is_ip_any =0;
8673           }
8674         else if (unformat (i, "laddr_stop %U", unformat_ip4_address, &laddr4_stop))
8675           {
8676             is_ipv6 = 0;
8677             is_ip_any = 0;
8678           }
8679         else if (unformat (i, "raddr_start %U", unformat_ip4_address, &raddr4_start))
8680           {
8681             is_ipv6 = 0;
8682             is_ip_any = 0;
8683           }
8684         else if (unformat (i, "raddr_stop %U", unformat_ip4_address, &raddr4_stop))
8685           {
8686             is_ipv6 = 0;
8687             is_ip_any = 0;
8688           }
8689         else if (unformat (i, "laddr_start %U", unformat_ip6_address, &laddr6_start))
8690           {
8691             is_ipv6 = 1;
8692             is_ip_any = 0;
8693           }
8694         else if (unformat (i, "laddr_stop %U", unformat_ip6_address, &laddr6_stop))
8695           {
8696             is_ipv6 = 1;
8697             is_ip_any = 0;
8698           }
8699         else if (unformat (i, "raddr_start %U", unformat_ip6_address, &raddr6_start))
8700           {
8701             is_ipv6 = 1;
8702             is_ip_any = 0;
8703           }
8704         else if (unformat (i, "raddr_stop %U", unformat_ip6_address, &raddr6_stop))
8705           {
8706             is_ipv6 = 1;
8707             is_ip_any = 0;
8708           }
8709         else if (unformat (i, "action %U", unformat_ipsec_policy_action, &policy))
8710           {
8711             if (policy == IPSEC_POLICY_ACTION_RESOLVE) {
8712                 clib_warning ("unsupported action: 'resolve'");
8713                 return -99;
8714             }
8715           }
8716         else {
8717             clib_warning ("parse error '%U'", format_unformat_error, i);
8718             return -99;
8719         }
8720
8721     }
8722
8723     M(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry);
8724
8725     mp->spd_id = ntohl(spd_id);
8726     mp->priority = ntohl(priority);
8727     mp->is_outbound = is_outbound;
8728
8729     mp->is_ipv6 = is_ipv6;
8730     if (is_ipv6 || is_ip_any) {
8731         clib_memcpy (mp->remote_address_start, &raddr6_start, sizeof(ip6_address_t));
8732         clib_memcpy (mp->remote_address_stop, &raddr6_stop, sizeof(ip6_address_t));
8733         clib_memcpy (mp->local_address_start, &laddr6_start, sizeof(ip6_address_t));
8734         clib_memcpy (mp->local_address_stop, &laddr6_stop, sizeof(ip6_address_t));
8735     } else {
8736         clib_memcpy (mp->remote_address_start, &raddr4_start, sizeof(ip4_address_t));
8737         clib_memcpy (mp->remote_address_stop, &raddr4_stop, sizeof(ip4_address_t));
8738         clib_memcpy (mp->local_address_start, &laddr4_start, sizeof(ip4_address_t));
8739         clib_memcpy (mp->local_address_stop, &laddr4_stop, sizeof(ip4_address_t));
8740     }
8741     mp->protocol = (u8) protocol;
8742     mp->local_port_start = ntohs((u16) lport_start);
8743     mp->local_port_stop = ntohs((u16) lport_stop);
8744     mp->remote_port_start = ntohs((u16) rport_start);
8745     mp->remote_port_stop = ntohs((u16) rport_stop);
8746     mp->policy = (u8) policy;
8747     mp->sa_id = ntohl(sa_id);
8748     mp->is_add = is_add;
8749     mp->is_ip_any = is_ip_any;
8750     S; W;
8751     /* NOTREACHED */
8752     return 0;
8753 #else
8754     clib_warning ("unsupported (no dpdk)");
8755     return -99;
8756 #endif
8757 }
8758
8759 static int
8760 api_ipsec_sad_add_del_entry (vat_main_t * vam)
8761 {
8762 #if DPDK > 0
8763     unformat_input_t * i = vam->input;
8764     vl_api_ipsec_sad_add_del_entry_t *mp;
8765     f64 timeout;
8766     u32 sad_id, spi;
8767     u8 * ck, * ik;
8768     u8 is_add = 1;
8769
8770     u8 protocol = IPSEC_PROTOCOL_AH;
8771     u8 is_tunnel = 0, is_tunnel_ipv6 = 0;
8772     u32 crypto_alg = 0, integ_alg = 0;
8773     ip4_address_t tun_src4;
8774     ip4_address_t tun_dst4;
8775     ip6_address_t tun_src6;
8776     ip6_address_t tun_dst6;
8777
8778     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8779         if (unformat (i, "del"))
8780             is_add = 0;
8781         else if (unformat (i, "sad_id %d", &sad_id))
8782             ;
8783         else if (unformat (i, "spi %d", &spi))
8784             ;
8785         else if (unformat (i, "esp"))
8786             protocol = IPSEC_PROTOCOL_ESP;
8787         else if (unformat (i, "tunnel_src %U", unformat_ip4_address, &tun_src4)) {
8788             is_tunnel = 1;
8789             is_tunnel_ipv6 = 0;
8790         }
8791         else if (unformat (i, "tunnel_dst %U", unformat_ip4_address, &tun_dst4)) {
8792             is_tunnel = 1;
8793             is_tunnel_ipv6 = 0;
8794         }
8795         else if (unformat (i, "tunnel_src %U", unformat_ip6_address, &tun_src6)) {
8796             is_tunnel = 1;
8797             is_tunnel_ipv6 = 1;
8798         }
8799         else if (unformat (i, "tunnel_dst %U", unformat_ip6_address, &tun_dst6)) {
8800             is_tunnel = 1;
8801             is_tunnel_ipv6 = 1;
8802         }
8803         else if (unformat (i, "crypto_alg %U", unformat_ipsec_crypto_alg, &crypto_alg)) {
8804             if (crypto_alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
8805                 crypto_alg > IPSEC_INTEG_ALG_SHA_512_256) {
8806                 clib_warning ("unsupported crypto-alg: '%U'",
8807                               format_ipsec_crypto_alg, crypto_alg);
8808                 return -99;
8809             }
8810         }
8811         else if (unformat (i, "crypto_key %U", unformat_hex_string, &ck))
8812             ;
8813         else if (unformat (i, "integ_alg %U", unformat_ipsec_integ_alg, &integ_alg)) {
8814             if (integ_alg < IPSEC_INTEG_ALG_SHA1_96 ||
8815                 integ_alg > IPSEC_INTEG_ALG_SHA_512_256) {
8816                 clib_warning ("unsupported integ-alg: '%U'",
8817                               format_ipsec_integ_alg, integ_alg);
8818                 return -99;
8819             }
8820         }
8821         else if (unformat (i, "integ_key %U", unformat_hex_string, &ik))
8822             ;
8823         else {
8824             clib_warning ("parse error '%U'", format_unformat_error, i);
8825             return -99;
8826         }
8827
8828     }
8829
8830     M(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry);
8831
8832     mp->sad_id = ntohl(sad_id);
8833     mp->is_add = is_add;
8834     mp->protocol = protocol;
8835     mp->spi = ntohl(spi);
8836     mp->is_tunnel = is_tunnel;
8837     mp->is_tunnel_ipv6 = is_tunnel_ipv6;
8838     mp->crypto_algorithm = crypto_alg;
8839     mp->integrity_algorithm = integ_alg;
8840     mp->crypto_key_length = vec_len(ck);
8841     mp->integrity_key_length = vec_len(ik);
8842
8843     if (mp->crypto_key_length > sizeof(mp->crypto_key))
8844       mp->crypto_key_length = sizeof(mp->crypto_key);
8845
8846     if (mp->integrity_key_length > sizeof(mp->integrity_key))
8847       mp->integrity_key_length = sizeof(mp->integrity_key);
8848
8849     clib_memcpy (mp->crypto_key, ck, mp->crypto_key_length);
8850     clib_memcpy (mp->integrity_key, ik, mp->integrity_key_length);
8851
8852     if (is_tunnel) {
8853       if (is_tunnel_ipv6) {
8854         clib_memcpy (mp->tunnel_src_address, &tun_src6, sizeof(ip6_address_t));
8855         clib_memcpy (mp->tunnel_dst_address, &tun_dst6, sizeof(ip6_address_t));
8856       } else {
8857         clib_memcpy (mp->tunnel_src_address, &tun_src4, sizeof(ip4_address_t));
8858         clib_memcpy (mp->tunnel_dst_address, &tun_dst4, sizeof(ip4_address_t));
8859       }
8860     }
8861
8862     S; W;
8863     /* NOTREACHED */
8864     return 0;
8865 #else
8866     clib_warning ("unsupported (no dpdk)");
8867     return -99;
8868 #endif
8869 }
8870
8871 static int
8872 api_ipsec_sa_set_key (vat_main_t * vam)
8873 {
8874 #if DPDK > 0
8875     unformat_input_t * i = vam->input;
8876     vl_api_ipsec_sa_set_key_t *mp;
8877     f64 timeout;
8878     u32 sa_id;
8879     u8 * ck, * ik;
8880
8881     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8882         if (unformat (i, "sa_id %d", &sa_id))
8883             ;
8884         else if (unformat (i, "crypto_key %U", unformat_hex_string, &ck))
8885             ;
8886         else if (unformat (i, "integ_key %U", unformat_hex_string, &ik))
8887             ;
8888         else {
8889             clib_warning ("parse error '%U'", format_unformat_error, i);
8890             return -99;
8891         }
8892     }
8893
8894     M(IPSEC_SA_SET_KEY, ipsec_set_sa_key);
8895
8896     mp->sa_id = ntohl(sa_id);
8897     mp->crypto_key_length = vec_len(ck);
8898     mp->integrity_key_length = vec_len(ik);
8899
8900     if (mp->crypto_key_length > sizeof(mp->crypto_key))
8901       mp->crypto_key_length = sizeof(mp->crypto_key);
8902
8903     if (mp->integrity_key_length > sizeof(mp->integrity_key))
8904       mp->integrity_key_length = sizeof(mp->integrity_key);
8905
8906     clib_memcpy (mp->crypto_key, ck, mp->crypto_key_length);
8907     clib_memcpy (mp->integrity_key, ik, mp->integrity_key_length);
8908
8909     S; W;
8910     /* NOTREACHED */
8911     return 0;
8912 #else
8913     clib_warning ("unsupported (no dpdk)");
8914     return -99;
8915 #endif
8916 }
8917
8918 static int
8919 api_ikev2_profile_add_del (vat_main_t * vam)
8920 {
8921 #if DPDK > 0
8922     unformat_input_t * i = vam->input;
8923     vl_api_ikev2_profile_add_del_t * mp;
8924     f64 timeout;
8925     u8 is_add = 1;
8926     u8 * name = 0;
8927
8928     const char * valid_chars = "a-zA-Z0-9_";
8929
8930     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8931         if (unformat (i, "del"))
8932             is_add = 0;
8933         else if (unformat (i, "name %U", unformat_token, valid_chars, &name))
8934             vec_add1 (name, 0);
8935         else {
8936             errmsg ("parse error '%U'", format_unformat_error, i);
8937             return -99;
8938         }
8939     }
8940
8941     if (!vec_len (name)) {
8942         errmsg ("profile name must be specified");
8943         return -99;
8944     }
8945
8946     if (vec_len (name) > 64) {
8947         errmsg ("profile name too long");
8948         return -99;
8949     }
8950
8951     M(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del);
8952
8953     clib_memcpy(mp->name, name, vec_len (name));
8954     mp->is_add = is_add;
8955     vec_free (name);
8956
8957     S; W;
8958     /* NOTREACHED */
8959     return 0;
8960 #else
8961     clib_warning ("unsupported (no dpdk)");
8962     return -99;
8963 #endif
8964 }
8965
8966 static int
8967 api_ikev2_profile_set_auth (vat_main_t * vam)
8968 {
8969 #if DPDK > 0
8970     unformat_input_t * i = vam->input;
8971     vl_api_ikev2_profile_set_auth_t * mp;
8972     f64 timeout;
8973     u8 * name = 0;
8974     u8 * data = 0;
8975     u32 auth_method = 0;
8976     u8 is_hex = 0;
8977
8978     const char * valid_chars = "a-zA-Z0-9_";
8979
8980     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
8981         if (unformat (i, "name %U", unformat_token, valid_chars, &name))
8982             vec_add1 (name, 0);
8983         else if (unformat (i, "auth_method %U",
8984                            unformat_ikev2_auth_method, &auth_method))
8985             ;
8986         else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data))
8987             is_hex = 1;
8988         else if (unformat (i, "auth_data %v", &data))
8989             ;
8990         else {
8991             errmsg ("parse error '%U'", format_unformat_error, i);
8992             return -99;
8993         }
8994     }
8995
8996     if (!vec_len (name)) {
8997         errmsg ("profile name must be specified");
8998         return -99;
8999     }
9000
9001     if (vec_len (name) > 64) {
9002         errmsg ("profile name too long");
9003         return -99;
9004     }
9005
9006     if (!vec_len(data)) {
9007         errmsg ("auth_data must be specified");
9008         return -99;
9009     }
9010
9011     if (!auth_method) {
9012         errmsg ("auth_method must be specified");
9013         return -99;
9014     }
9015
9016     M(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth);
9017
9018     mp->is_hex = is_hex;
9019     mp->auth_method = (u8) auth_method;
9020     mp->data_len = vec_len (data);
9021     clib_memcpy (mp->name, name, vec_len (name));
9022     clib_memcpy (mp->data, data, vec_len (data));
9023     vec_free (name);
9024     vec_free (data);
9025
9026     S; W;
9027     /* NOTREACHED */
9028     return 0;
9029 #else
9030     clib_warning ("unsupported (no dpdk)");
9031     return -99;
9032 #endif
9033 }
9034
9035 static int
9036 api_ikev2_profile_set_id (vat_main_t * vam)
9037 {
9038 #if DPDK > 0
9039     unformat_input_t * i = vam->input;
9040     vl_api_ikev2_profile_set_id_t * mp;
9041     f64 timeout;
9042     u8 * name = 0;
9043     u8 * data = 0;
9044     u8 is_local = 0;
9045     u32 id_type = 0;
9046     ip4_address_t ip4;
9047
9048     const char * valid_chars = "a-zA-Z0-9_";
9049
9050     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9051         if (unformat (i, "name %U", unformat_token, valid_chars, &name))
9052             vec_add1 (name, 0);
9053         else if (unformat (i, "id_type %U",
9054                            unformat_ikev2_id_type, &id_type))
9055             ;
9056         else if (unformat (i, "id_data %U", unformat_ip4_address, &ip4))
9057           {
9058             data = vec_new(u8, 4);
9059             clib_memcpy(data, ip4.as_u8, 4);
9060           }
9061         else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data))
9062             ;
9063         else if (unformat (i, "id_data %v", &data))
9064             ;
9065         else if (unformat (i, "local"))
9066             is_local = 1;
9067         else if (unformat (i, "remote"))
9068             is_local = 0;
9069         else {
9070             errmsg ("parse error '%U'", format_unformat_error, i);
9071             return -99;
9072         }
9073     }
9074
9075     if (!vec_len (name)) {
9076         errmsg ("profile name must be specified");
9077         return -99;
9078     }
9079
9080     if (vec_len (name) > 64) {
9081         errmsg ("profile name too long");
9082         return -99;
9083     }
9084
9085     if (!vec_len(data)) {
9086         errmsg ("id_data must be specified");
9087         return -99;
9088     }
9089
9090     if (!id_type) {
9091         errmsg ("id_type must be specified");
9092         return -99;
9093     }
9094
9095     M(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id);
9096
9097     mp->is_local = is_local;
9098     mp->id_type = (u8) id_type;
9099     mp->data_len = vec_len (data);
9100     clib_memcpy (mp->name, name, vec_len (name));
9101     clib_memcpy (mp->data, data, vec_len (data));
9102     vec_free (name);
9103     vec_free (data);
9104
9105     S; W;
9106     /* NOTREACHED */
9107     return 0;
9108 #else
9109     clib_warning ("unsupported (no dpdk)");
9110     return -99;
9111 #endif
9112 }
9113
9114 static int
9115 api_ikev2_profile_set_ts (vat_main_t * vam)
9116 {
9117 #if DPDK > 0
9118     unformat_input_t * i = vam->input;
9119     vl_api_ikev2_profile_set_ts_t * mp;
9120     f64 timeout;
9121     u8 * name = 0;
9122     u8 is_local = 0;
9123     u32 proto = 0, start_port = 0, end_port = (u32) ~0;
9124     ip4_address_t start_addr, end_addr;
9125
9126     const char * valid_chars = "a-zA-Z0-9_";
9127
9128     start_addr.as_u32 = 0;
9129     end_addr.as_u32 = (u32) ~0;
9130
9131     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9132         if (unformat (i, "name %U", unformat_token, valid_chars, &name))
9133             vec_add1 (name, 0);
9134         else if (unformat (i, "protocol %d", &proto))
9135             ;
9136         else if (unformat (i, "start_port %d", &start_port))
9137             ;
9138         else if (unformat (i, "end_port %d", &end_port))
9139             ;
9140         else if (unformat (i, "start_addr %U", unformat_ip4_address, &start_addr))
9141             ;
9142         else if (unformat (i, "end_addr %U", unformat_ip4_address, &end_addr))
9143             ;
9144         else if (unformat (i, "local"))
9145             is_local = 1;
9146         else if (unformat (i, "remote"))
9147             is_local = 0;
9148         else {
9149             errmsg ("parse error '%U'", format_unformat_error, i);
9150             return -99;
9151         }
9152     }
9153
9154     if (!vec_len (name)) {
9155         errmsg ("profile name must be specified");
9156         return -99;
9157     }
9158
9159     if (vec_len (name) > 64) {
9160         errmsg ("profile name too long");
9161         return -99;
9162     }
9163
9164     M(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts);
9165
9166     mp->is_local = is_local;
9167     mp->proto = (u8) proto;
9168     mp->start_port = (u16) start_port;
9169     mp->end_port = (u16) end_port;
9170     mp->start_addr = start_addr.as_u32;
9171     mp->end_addr = end_addr.as_u32;
9172     clib_memcpy (mp->name, name, vec_len (name));
9173     vec_free (name);
9174
9175     S; W;
9176     /* NOTREACHED */
9177     return 0;
9178 #else
9179     clib_warning ("unsupported (no dpdk)");
9180     return -99;
9181 #endif
9182 }
9183
9184 static int
9185 api_ikev2_set_local_key (vat_main_t * vam)
9186 {
9187 #if DPDK > 0
9188     unformat_input_t * i = vam->input;
9189     vl_api_ikev2_set_local_key_t * mp;
9190     f64 timeout;
9191     u8 * file = 0;
9192
9193     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9194         if (unformat (i, "file %v", &file))
9195             vec_add1 (file, 0);
9196         else {
9197             errmsg ("parse error '%U'", format_unformat_error, i);
9198             return -99;
9199         }
9200     }
9201
9202     if (!vec_len (file)) {
9203         errmsg ("RSA key file must be specified");
9204         return -99;
9205     }
9206
9207     if (vec_len (file) > 256) {
9208         errmsg ("file name too long");
9209         return -99;
9210     }
9211
9212     M(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key);
9213
9214     clib_memcpy (mp->key_file, file, vec_len (file));
9215     vec_free (file);
9216
9217     S; W;
9218     /* NOTREACHED */
9219     return 0;
9220 #else
9221     clib_warning ("unsupported (no dpdk)");
9222     return -99;
9223 #endif
9224 }
9225
9226 /*
9227  * MAP
9228  */
9229 static int api_map_add_domain (vat_main_t * vam)
9230 {
9231   unformat_input_t *i = vam->input;
9232   vl_api_map_add_domain_t *mp;
9233   f64 timeout;
9234
9235   ip4_address_t ip4_prefix;
9236   ip6_address_t ip6_prefix;
9237   ip6_address_t ip6_src;
9238   u32 num_m_args = 0;
9239   u32 ip6_prefix_len, ip4_prefix_len, ea_bits_len, psid_offset,
9240     psid_length;
9241   u8 is_translation = 0;
9242   u32 mtu = 0;
9243   u8 ip6_src_len = 128;
9244
9245   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9246     if (unformat (i, "ip4-pfx %U/%d", unformat_ip4_address,
9247                   &ip4_prefix, &ip4_prefix_len))
9248       num_m_args++;
9249     else if (unformat (i, "ip6-pfx %U/%d", unformat_ip6_address,
9250                        &ip6_prefix, &ip6_prefix_len))
9251       num_m_args++;
9252     else if (unformat (i, "ip6-src %U/%d", unformat_ip6_address, &ip6_src, &ip6_src_len))
9253       num_m_args++;
9254     else if (unformat (i, "ip6-src %U", unformat_ip6_address, &ip6_src))
9255       num_m_args++;
9256     else if (unformat (i, "ea-bits-len %d", &ea_bits_len))
9257       num_m_args++;
9258     else if (unformat (i, "psid-offset %d", &psid_offset))
9259       num_m_args++;
9260     else if (unformat (i, "psid-len %d", &psid_length))
9261       num_m_args++;
9262     else if (unformat (i, "mtu %d", &mtu))
9263       num_m_args++;
9264     else if (unformat (i, "map-t"))
9265       is_translation = 1;
9266     else {
9267       clib_warning ("parse error '%U'", format_unformat_error, i);
9268       return -99;
9269     }
9270   }
9271
9272   if (num_m_args != 6) {
9273     errmsg("mandatory argument(s) missing\n");
9274     return -99;
9275   }
9276
9277   /* Construct the API message */
9278   M(MAP_ADD_DOMAIN, map_add_domain);
9279
9280   clib_memcpy(mp->ip4_prefix, &ip4_prefix, sizeof(ip4_prefix));
9281   mp->ip4_prefix_len = ip4_prefix_len;
9282
9283   clib_memcpy(mp->ip6_prefix, &ip6_prefix, sizeof(ip6_prefix));
9284   mp->ip6_prefix_len = ip6_prefix_len;
9285
9286   clib_memcpy(mp->ip6_src, &ip6_src, sizeof(ip6_src));
9287   mp->ip6_src_prefix_len = ip6_src_len;
9288
9289   mp->ea_bits_len = ea_bits_len;
9290   mp->psid_offset = psid_offset;
9291   mp->psid_length = psid_length;
9292   mp->is_translation = is_translation;
9293   mp->mtu = htons(mtu);
9294
9295   /* send it... */
9296   S;
9297
9298   /* Wait for a reply, return good/bad news  */
9299   W;
9300 }
9301
9302 static int api_map_del_domain (vat_main_t * vam)
9303 {
9304   unformat_input_t *i = vam->input;
9305   vl_api_map_del_domain_t *mp;
9306   f64 timeout;
9307
9308   u32 num_m_args = 0;
9309   u32 index;
9310
9311   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9312     if (unformat (i, "index %d", &index))
9313       num_m_args++;
9314     else {
9315       clib_warning ("parse error '%U'", format_unformat_error, i);
9316       return -99;
9317     }
9318   }
9319
9320   if (num_m_args != 1) {
9321     errmsg("mandatory argument(s) missing\n");
9322     return -99;
9323   }
9324
9325   /* Construct the API message */
9326   M(MAP_DEL_DOMAIN, map_del_domain);
9327
9328   mp->index = ntohl(index);
9329
9330   /* send it... */
9331   S;
9332
9333   /* Wait for a reply, return good/bad news  */
9334   W;
9335 }
9336
9337 static int api_map_add_del_rule (vat_main_t * vam)
9338 {
9339   unformat_input_t *i = vam->input;
9340   vl_api_map_add_del_rule_t *mp;
9341   f64 timeout;
9342   u8 is_add = 1;
9343   ip6_address_t ip6_dst;
9344   u32 num_m_args = 0, index, psid;
9345
9346   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9347     if (unformat (i, "index %d", &index))
9348       num_m_args++;
9349     else if (unformat (i, "psid %d", &psid))
9350       num_m_args++;
9351     else if (unformat (i, "dst %U", unformat_ip6_address, &ip6_dst))
9352       num_m_args++;
9353     else if (unformat (i, "del")) {
9354       is_add = 0;
9355     } else {
9356       clib_warning ("parse error '%U'", format_unformat_error, i);
9357       return -99;
9358     }
9359   }
9360
9361   /* Construct the API message */
9362   M(MAP_ADD_DEL_RULE, map_add_del_rule);
9363
9364   mp->index = ntohl(index);
9365   mp->is_add = is_add;
9366   clib_memcpy(mp->ip6_dst, &ip6_dst, sizeof(ip6_dst));
9367   mp->psid = ntohs(psid);
9368
9369   /* send it... */
9370   S;
9371
9372   /* Wait for a reply, return good/bad news  */
9373   W;
9374 }
9375
9376 static int api_map_domain_dump (vat_main_t * vam)
9377 {
9378     vl_api_map_domain_dump_t *mp;
9379     f64 timeout;
9380
9381     /* Construct the API message */
9382     M(MAP_DOMAIN_DUMP, map_domain_dump);
9383
9384     /* send it... */
9385     S;
9386
9387     /* Use a control ping for synchronization */
9388     {
9389         vl_api_control_ping_t * mp;
9390         M(CONTROL_PING, control_ping);
9391         S;
9392     }
9393     W;
9394 }
9395
9396 static int api_map_rule_dump (vat_main_t * vam)
9397 {
9398     unformat_input_t *i = vam->input;
9399     vl_api_map_rule_dump_t *mp;
9400     f64 timeout;
9401     u32 domain_index = ~0;
9402
9403     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9404         if (unformat (i, "index %u", &domain_index))
9405             ;
9406         else
9407             break;
9408     }
9409
9410     if (domain_index == ~0) {
9411         clib_warning("parse error: domain index expected");
9412         return -99;
9413     }
9414
9415     /* Construct the API message */
9416     M(MAP_RULE_DUMP, map_rule_dump);
9417
9418     mp->domain_index = htonl(domain_index);
9419
9420     /* send it... */
9421     S;
9422
9423     /* Use a control ping for synchronization */
9424     {
9425         vl_api_control_ping_t * mp;
9426         M(CONTROL_PING, control_ping);
9427         S;
9428     }
9429     W;
9430 }
9431
9432 static void vl_api_map_add_domain_reply_t_handler
9433 (vl_api_map_add_domain_reply_t * mp)
9434 {
9435   vat_main_t * vam = &vat_main;
9436   i32 retval = ntohl(mp->retval);
9437
9438   if (vam->async_mode) {
9439       vam->async_errors += (retval < 0);
9440   } else {
9441       vam->retval = retval;
9442       vam->result_ready = 1;
9443   }
9444 }
9445
9446 static void vl_api_map_add_domain_reply_t_handler_json
9447 (vl_api_map_add_domain_reply_t * mp)
9448 {
9449   vat_main_t * vam = &vat_main;
9450   vat_json_node_t node;
9451
9452   vat_json_init_object(&node);
9453   vat_json_object_add_int(&node, "retval", ntohl(mp->retval));
9454   vat_json_object_add_uint(&node, "index", ntohl(mp->index));
9455
9456   vat_json_print(vam->ofp, &node);
9457   vat_json_free(&node);
9458
9459   vam->retval = ntohl(mp->retval);
9460   vam->result_ready = 1;
9461 }
9462
9463 static int
9464 api_get_first_msg_id (vat_main_t * vam)
9465 {
9466     vl_api_get_first_msg_id_t * mp;
9467     f64 timeout;
9468     unformat_input_t * i = vam->input;
9469     u8 * name;
9470     u8 name_set = 0;
9471     
9472     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
9473         if (unformat (i, "client %s", &name))
9474             name_set = 1;
9475         else 
9476             break;
9477     }
9478
9479     if (name_set == 0) {
9480         errmsg ("missing client name\n");
9481         return -99;
9482     }
9483     vec_add1 (name, 0);
9484
9485     if (vec_len (name) > 63) {
9486         errmsg ("client name too long\n");
9487         return -99;
9488     }
9489
9490     M(GET_FIRST_MSG_ID, get_first_msg_id);
9491     clib_memcpy (mp->name, name, vec_len(name));
9492     S; W;
9493     /* NOTREACHED */
9494     return 0;
9495 }
9496
9497 static int api_cop_interface_enable_disable (vat_main_t * vam)
9498 {
9499     unformat_input_t * line_input = vam->input;
9500     vl_api_cop_interface_enable_disable_t * mp;
9501     f64 timeout;
9502     u32 sw_if_index = ~0;
9503     u8 enable_disable = 1;
9504
9505     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
9506         if (unformat (line_input, "disable"))
9507             enable_disable = 0;
9508         if (unformat (line_input, "enable"))
9509             enable_disable = 1;
9510         else if (unformat (line_input, "%U", unformat_sw_if_index,
9511                            vam, &sw_if_index))
9512             ;
9513         else if (unformat (line_input, "sw_if_index %d", &sw_if_index))
9514             ;
9515         else
9516             break;
9517     }
9518         
9519     if (sw_if_index == ~0) {
9520         errmsg ("missing interface name or sw_if_index\n");
9521         return -99;
9522     }
9523
9524     /* Construct the API message */
9525     M(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable);
9526     mp->sw_if_index = ntohl(sw_if_index);
9527     mp->enable_disable = enable_disable;
9528
9529     /* send it... */
9530     S;
9531     /* Wait for the reply */
9532     W;
9533 }
9534
9535 static int api_cop_whitelist_enable_disable (vat_main_t * vam)
9536 {
9537     unformat_input_t * line_input = vam->input;
9538     vl_api_cop_whitelist_enable_disable_t * mp;
9539     f64 timeout;
9540     u32 sw_if_index = ~0;
9541     u8 ip4=0, ip6=0, default_cop=0;
9542     u32 fib_id;
9543
9544     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
9545         if (unformat (line_input, "ip4"))
9546             ip4 = 1;
9547         else if (unformat (line_input, "ip6"))
9548             ip6 = 1;
9549         else if (unformat (line_input, "default"))
9550             default_cop = 1;
9551         else if (unformat (line_input, "%U", unformat_sw_if_index,
9552                            vam, &sw_if_index))
9553             ;
9554         else if (unformat (line_input, "sw_if_index %d", &sw_if_index))
9555             ;
9556         else if (unformat (line_input, "fib-id %d", &fib_id))
9557             ;
9558         else
9559             break;
9560     }
9561         
9562     if (sw_if_index == ~0) {
9563         errmsg ("missing interface name or sw_if_index\n");
9564         return -99;
9565     }
9566
9567     /* Construct the API message */
9568     M(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable);
9569     mp->sw_if_index = ntohl(sw_if_index);
9570     mp->fib_id = ntohl(fib_id);
9571     mp->ip4 = ip4;
9572     mp->ip6 = ip6;
9573     mp->default_cop = default_cop;
9574
9575     /* send it... */
9576     S;
9577     /* Wait for the reply */
9578     W;
9579 }
9580
9581 static int api_get_node_graph (vat_main_t * vam)
9582 {
9583     vl_api_get_node_graph_t * mp;
9584     f64 timeout;
9585
9586     M(GET_NODE_GRAPH, get_node_graph);
9587
9588     /* send it... */
9589     S;
9590     /* Wait for the reply */
9591     W;
9592 }
9593
9594 static int
9595 api_lisp_add_del_locator_set(vat_main_t * vam)
9596 {
9597     unformat_input_t * input = vam->input;
9598     vl_api_lisp_add_del_locator_set_t *mp;
9599     f64 timeout = ~0;
9600     u8  is_add = 1;
9601     u8 *locator_set_name = NULL;
9602     u8  locator_set_name_set = 0;
9603
9604     /* Parse args required to build the message */
9605     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
9606         if (unformat(input, "del")) {
9607             is_add = 0;
9608         } else if (unformat(input, "locator-set %s", &locator_set_name)) {
9609             locator_set_name_set = 1;
9610         } else
9611             break;
9612     }
9613
9614     if (locator_set_name_set == 0) {
9615         errmsg ("missing locator-set name");
9616         return -99;
9617     }
9618
9619     if (vec_len(locator_set_name) > 64) {
9620         errmsg ("locator-set name too long\n");
9621         vec_free(locator_set_name);
9622         return -99;
9623     }
9624     vec_add1(locator_set_name, 0);
9625
9626     /* Construct the API message */
9627     M(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set);
9628
9629     mp->is_add = is_add;
9630     clib_memcpy(mp->locator_set_name, locator_set_name,
9631            vec_len(locator_set_name));
9632     vec_free(locator_set_name);
9633
9634     /* send it... */
9635     S;
9636
9637     /* Wait for a reply... */
9638     W;
9639
9640     /* NOTREACHED */
9641     return 0;
9642 }
9643
9644 static int
9645 api_lisp_add_del_locator(vat_main_t * vam)
9646 {
9647     unformat_input_t * input = vam->input;
9648     vl_api_lisp_add_del_locator_t *mp;
9649     f64 timeout = ~0;
9650     u32 tmp_if_index = ~0;
9651     u32 sw_if_index = ~0;
9652     u8  sw_if_index_set = 0;
9653     u8  sw_if_index_if_name_set = 0;
9654     u8  priority = ~0;
9655     u8  priority_set = 0;
9656     u8  weight = ~0;
9657     u8  weight_set = 0;
9658     u8  is_add = 1;
9659     u8  *locator_set_name = NULL;
9660     u8  locator_set_name_set = 0;
9661
9662     /* Parse args required to build the message */
9663     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
9664         if (unformat(input, "del")) {
9665             is_add = 0;
9666         } else if (unformat(input, "locator-set %s", &locator_set_name)) {
9667             locator_set_name_set = 1;
9668         } else if (unformat(input, "iface %U", unformat_sw_if_index, vam,
9669             &tmp_if_index)) {
9670             sw_if_index_if_name_set = 1;
9671             sw_if_index = tmp_if_index;
9672         } else if (unformat(input,"sw_if_index %d", &tmp_if_index)) {
9673             sw_if_index_set = 1;
9674             sw_if_index = tmp_if_index;
9675         } else if (unformat(input, "p %d", &priority)) {
9676             priority_set = 1;
9677         } else if (unformat(input, "w %d", &weight)) {
9678             weight_set = 1;
9679         } else
9680             break;
9681     }
9682
9683     if (locator_set_name_set == 0) {
9684         errmsg ("missing locator-set name");
9685         return -99;
9686     }
9687
9688     if (sw_if_index_set == 0 && sw_if_index_if_name_set == 0) {
9689         errmsg ("missing sw_if_index");
9690         vec_free(locator_set_name);
9691         return -99;
9692     }
9693
9694     if (sw_if_index_set != 0 && sw_if_index_if_name_set != 0) {
9695         errmsg ("cannot use both params interface name and sw_if_index");
9696         vec_free(locator_set_name);
9697         return -99;
9698     }
9699
9700     if (priority_set == 0) {
9701         errmsg ("missing locator-set priority\n");
9702         vec_free(locator_set_name);
9703         return -99;
9704     }
9705
9706     if (weight_set == 0) {
9707         errmsg ("missing locator-set weight\n");
9708         vec_free(locator_set_name);
9709         return -99;
9710     }
9711
9712     if (vec_len(locator_set_name) > 64) {
9713         errmsg ("locator-set name too long\n");
9714         vec_free(locator_set_name);
9715         return -99;
9716     }
9717     vec_add1(locator_set_name, 0);
9718
9719     /* Construct the API message */
9720     M(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator);
9721
9722     mp->is_add = is_add;
9723     mp->sw_if_index = ntohl(sw_if_index);
9724     mp->priority = priority;
9725     mp->weight = weight;
9726     clib_memcpy(mp->locator_set_name, locator_set_name,
9727            vec_len(locator_set_name));
9728     vec_free(locator_set_name);
9729
9730     /* send it... */
9731     S;
9732
9733     /* Wait for a reply... */
9734     W;
9735
9736     /* NOTREACHED */
9737     return 0;
9738 }
9739
9740 static int
9741 api_lisp_add_del_local_eid(vat_main_t * vam)
9742 {
9743     unformat_input_t * input = vam->input;
9744     vl_api_lisp_add_del_local_eid_t *mp;
9745     f64 timeout = ~0;
9746     u8 is_add = 1;
9747     u8 eidv4_set = 0;
9748     u8 eidv6_set = 0;
9749     ip4_address_t eidv4;
9750     ip6_address_t eidv6;
9751     u8 tmp_eid_lenght = ~0;
9752     u8 eid_lenght = ~0;
9753     u8 *locator_set_name = NULL;
9754     u8 locator_set_name_set = 0;
9755
9756     /* Parse args required to build the message */
9757     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
9758         if (unformat(input, "del")) {
9759             is_add = 0;
9760         } else if (unformat(input, "eid %U/%d", unformat_ip4_address,
9761             &eidv4, &tmp_eid_lenght)) {
9762             eid_lenght = tmp_eid_lenght;
9763             eidv4_set = 1;
9764         } else if (unformat(input, "eid %U/%d", unformat_ip6_address,
9765             &eidv6, &tmp_eid_lenght)) {
9766             eid_lenght = tmp_eid_lenght;
9767             eidv6_set = 1;
9768         } else if (unformat(input, "locator-set %s", &locator_set_name)) {
9769             locator_set_name_set = 1;
9770         } else
9771             break;
9772     }
9773
9774     if (locator_set_name_set == 0) {
9775         errmsg ("missing locator-set name\n");
9776         return -99;
9777     }
9778
9779     if (vec_len(locator_set_name) > 64) {
9780         errmsg ("locator-set name too long\n");
9781         vec_free(locator_set_name);
9782         return -99;
9783     }
9784     vec_add1(locator_set_name, 0);
9785
9786     if (eidv4_set && eidv6_set) {
9787         errmsg ("both eid v4 and v6 addresses set\n");
9788         vec_free(locator_set_name);
9789         return -99;
9790     }
9791
9792     if (!eidv4_set && !eidv6_set) {
9793         errmsg ("eid addresses not set\n");
9794         vec_free(locator_set_name);
9795         return -99;
9796     }
9797
9798     /* Construct the API message */
9799     M(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid);
9800
9801     mp->is_add = is_add;
9802     if (eidv6_set) {
9803         mp->is_ipv6 = 1;
9804         clib_memcpy(mp->ip_address, &eidv6, sizeof(eidv6));
9805     } else {
9806         mp->is_ipv6 = 0;
9807         clib_memcpy(mp->ip_address, &eidv4, sizeof(eidv4));
9808     }
9809     mp->prefix_len = eid_lenght;
9810     clib_memcpy(mp->locator_set_name, locator_set_name,
9811            vec_len(locator_set_name));
9812     vec_free(locator_set_name);
9813
9814     /* send it... */
9815     S;
9816
9817     /* Wait for a reply... */
9818     W;
9819
9820     /* NOTREACHED */
9821     return 0;
9822 }
9823
9824 static int
9825 api_lisp_gpe_add_del_fwd_entry(vat_main_t * vam)
9826 {
9827     unformat_input_t * input = vam->input;
9828     vl_api_lisp_gpe_add_del_fwd_entry_t *mp;
9829     f64 timeout = ~0;
9830     u8 is_add = 1;
9831     u8 eidv4_set = 0, slocv4_set = 0, dlocv4_set = 0;
9832     u8 eidv6_set = 0, slocv6_set = 0, dlocv6_set = 0;
9833     ip4_address_t eidv4, slocv4, dlocv4;
9834     ip6_address_t eidv6, slocv6, dlocv6;
9835     u8 tmp_eid_lenght = ~0;
9836     u8 eid_lenght = ~0;
9837
9838     /* Parse args required to build the message */
9839     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
9840         if (unformat(input, "del")) {
9841             is_add = 0;
9842         } else if (unformat(input, "eid %U/%d", unformat_ip4_address,
9843                             &eidv4, &tmp_eid_lenght)) {
9844             eid_lenght = tmp_eid_lenght;
9845             eidv4_set = 1;
9846         } else if (unformat(input, "eid %U/%d", unformat_ip6_address,
9847                             &eidv6, &tmp_eid_lenght)) {
9848             eid_lenght = tmp_eid_lenght;
9849             eidv6_set = 1;
9850         } else if (unformat(input, "sloc %U", unformat_ip4_address, &slocv4)) {
9851             slocv4_set = 1;
9852         } else if (unformat(input, "sloc %U", unformat_ip6_address, &slocv6)) {
9853             slocv6_set = 1;
9854         } else if (unformat(input, "dloc %U", unformat_ip4_address, &dlocv4)) {
9855             dlocv4_set = 1;
9856         } else if (unformat(input, "dloc %U", unformat_ip6_address, &dlocv6)) {
9857             dlocv6_set = 1;
9858         } else
9859             break;
9860     }
9861
9862     if (eidv4_set && eidv6_set) {
9863         errmsg ("both eid v4 and v6 addresses set\n");
9864         return -99;
9865     }
9866
9867     if (!eidv4_set && !eidv6_set) {
9868         errmsg ("eid addresses not set\n");
9869         return -99;
9870     }
9871
9872     if (slocv4_set && slocv6_set) {
9873         errmsg ("both source v4 and v6 addresses set\n");
9874         return -99;
9875     }
9876
9877     if (!slocv4_set && !slocv6_set) {
9878         errmsg ("source addresses not set\n");
9879         return -99;
9880     }
9881
9882     if (dlocv4_set && dlocv6_set) {
9883         errmsg ("both destination v4 and v6 addresses set\n");
9884         return -99;
9885     }
9886
9887     if (dlocv4_set && dlocv6_set) {
9888         errmsg ("destination addresses not set\n");
9889         return -99;
9890     }
9891
9892     if (!(slocv4_set == dlocv4_set && slocv6_set == dlocv6_set)) {
9893         errmsg ("mixing type of source and destination address\n");
9894         return -99;
9895     }
9896
9897     /* Construct the API message */
9898     M(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry);
9899
9900     mp->is_add = is_add;
9901     if (eidv6_set) {
9902         mp->eid_is_ipv6 = 1;
9903         clib_memcpy(mp->eid_ip_address, &eidv6, sizeof(eidv6));
9904     } else {
9905         mp->eid_is_ipv6 = 0;
9906         clib_memcpy(mp->eid_ip_address, &eidv4, sizeof(eidv4));
9907     }
9908     mp->eid_prefix_len = eid_lenght;
9909     if (slocv6_set) {
9910         mp->address_is_ipv6 = 1;
9911         clib_memcpy(mp->source_ip_address, &slocv6, sizeof(slocv6));
9912         clib_memcpy(mp->destination_ip_address, &dlocv6, sizeof(dlocv6));
9913     } else {
9914         mp->address_is_ipv6 = 0;
9915         clib_memcpy(mp->source_ip_address, &slocv4, sizeof(slocv4));
9916         clib_memcpy(mp->destination_ip_address, &dlocv4, sizeof(dlocv4));
9917     }
9918
9919     /* send it... */
9920     S;
9921
9922     /* Wait for a reply... */
9923     W;
9924
9925     /* NOTREACHED */
9926     return 0;
9927 }
9928
9929 static int
9930 api_lisp_add_del_map_resolver(vat_main_t * vam)
9931 {
9932     unformat_input_t * input = vam->input;
9933     vl_api_lisp_add_del_map_resolver_t *mp;
9934     f64 timeout = ~0;
9935     u8 is_add = 1;
9936     u8 ipv4_set = 0;
9937     u8 ipv6_set = 0;
9938     ip4_address_t ipv4;
9939     ip6_address_t ipv6;
9940
9941     /* Parse args required to build the message */
9942     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
9943         if (unformat(input, "del")) {
9944             is_add = 0;
9945         } else if (unformat(input, "%U", unformat_ip4_address, &ipv4)) {
9946             ipv4_set = 1;
9947         } else if (unformat(input, "%U", unformat_ip6_address, &ipv6)) {
9948             ipv6_set = 1;
9949         } else
9950             break;
9951     }
9952
9953     if (ipv4_set && ipv6_set) {
9954         errmsg ("both eid v4 and v6 addresses set\n");
9955         return -99;
9956     }
9957
9958     if (!ipv4_set && !ipv6_set) {
9959         errmsg ("eid addresses not set\n");
9960         return -99;
9961     }
9962
9963     /* Construct the API message */
9964     M(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver);
9965
9966     mp->is_add = is_add;
9967     if (ipv6_set) {
9968         mp->is_ipv6 = 1;
9969         clib_memcpy(mp->ip_address, &ipv6, sizeof(ipv6));
9970     } else {
9971         mp->is_ipv6 = 0;
9972         clib_memcpy(mp->ip_address, &ipv4, sizeof(ipv4));
9973     }
9974
9975     /* send it... */
9976     S;
9977
9978     /* Wait for a reply... */
9979     W;
9980
9981     /* NOTREACHED */
9982     return 0;
9983 }
9984
9985 static int
9986 api_lisp_gpe_enable_disable (vat_main_t * vam)
9987 {
9988   unformat_input_t * input = vam->input;
9989   vl_api_lisp_gpe_enable_disable_t *mp;
9990   f64 timeout = ~0;
9991   u8 is_set = 0;
9992   u8 is_en = 1;
9993
9994   /* Parse args required to build the message */
9995   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
9996       if (unformat(input, "enable")) {
9997           is_set = 1;
9998           is_en = 1;
9999       } else if (unformat(input, "disable")) {
10000           is_set = 1;
10001           is_en = 0;
10002       } else
10003           break;
10004   }
10005
10006   if (is_set == 0) {
10007       errmsg("Value not set\n");
10008       return -99;
10009   }
10010
10011   /* Construct the API message */
10012   M(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable);
10013
10014   mp->is_en = is_en;
10015
10016   /* send it... */
10017   S;
10018
10019   /* Wait for a reply... */
10020   W;
10021
10022   /* NOTREACHED */
10023   return 0;
10024 }
10025
10026 static int
10027 api_lisp_enable_disable (vat_main_t * vam)
10028 {
10029   unformat_input_t * input = vam->input;
10030   vl_api_lisp_enable_disable_t *mp;
10031   f64 timeout = ~0;
10032   u8 is_set = 0;
10033   u8 is_en = 0;
10034
10035   /* Parse args required to build the message */
10036   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
10037     {
10038       if (unformat (input, "enable"))
10039         {
10040           is_set = 1;
10041           is_en = 1;
10042         }
10043       else if (unformat (input, "disable"))
10044         {
10045           is_set = 1;
10046         }
10047       else
10048           break;
10049     }
10050
10051   if (!is_set)
10052     {
10053       errmsg ("Value not set\n");
10054       return -99;
10055     }
10056
10057   /* Construct the API message */
10058   M(LISP_ENABLE_DISABLE, lisp_enable_disable);
10059
10060   mp->is_en = is_en;
10061
10062   /* send it... */
10063   S;
10064
10065   /* Wait for a reply... */
10066   W;
10067
10068   /* NOTREACHED */
10069   return 0;
10070 }
10071
10072 static int
10073 api_lisp_gpe_add_del_iface(vat_main_t * vam)
10074 {
10075     unformat_input_t * input = vam->input;
10076     vl_api_lisp_gpe_add_del_iface_t *mp;
10077     f64 timeout = ~0;
10078     u8 is_set = 0;
10079     u8 is_add = 1;
10080     u32 table_id, vni;
10081
10082     /* Parse args required to build the message */
10083     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
10084         if (unformat(input, "up")) {
10085             is_set = 1;
10086             is_add = 1;
10087         } else if (unformat(input, "down")) {
10088             is_set = 1;
10089             is_add = 0;
10090         } else if (unformat(input, "table_id %d", &table_id)) {
10091             ;
10092         } else if (unformat(input, "vni %d", &vni)) {
10093             ;
10094         } else
10095             break;
10096     }
10097
10098     if (is_set == 0) {
10099         errmsg("Value not set\n");
10100         return -99;
10101     }
10102
10103     /* Construct the API message */
10104     M(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface);
10105
10106     mp->is_add = is_add;
10107     mp->table_id = table_id;
10108     mp->vni = vni;
10109
10110     /* send it... */
10111     S;
10112
10113     /* Wait for a reply... */
10114     W;
10115
10116     /* NOTREACHED */
10117     return 0;
10118 }
10119
10120 static int
10121 api_lisp_locator_set_dump(vat_main_t *vam)
10122 {
10123     vl_api_lisp_locator_set_dump_t *mp;
10124     f64 timeout = ~0;
10125
10126     if (!vam->json_output) {
10127         fformat(vam->ofp, "%=20s%=16s%=16s%=16s\n",
10128                 "Locator-set", "Locator", "Priority", "Weight");
10129     }
10130
10131     M(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump);
10132     /* send it... */
10133     S;
10134
10135     /* Use a control ping for synchronization */
10136     {
10137         vl_api_control_ping_t * mp;
10138         M(CONTROL_PING, control_ping);
10139         S;
10140     }
10141     /* Wait for a reply... */
10142     W;
10143
10144     /* NOTREACHED */
10145     return 0;
10146 }
10147
10148 static int
10149 api_lisp_local_eid_table_dump(vat_main_t *vam)
10150 {
10151     vl_api_lisp_local_eid_table_dump_t *mp;
10152     f64 timeout = ~0;
10153
10154     if (!vam->json_output) {
10155         fformat(vam->ofp, "%=20s%=30s\n",
10156                 "Locator-set", "Eid");
10157     }
10158
10159     M(LISP_LOCAL_EID_TABLE_DUMP, lisp_local_eid_table_dump);
10160     /* send it... */
10161     S;
10162
10163     /* Use a control ping for synchronization */
10164     {
10165         vl_api_control_ping_t * mp;
10166         M(CONTROL_PING, control_ping);
10167         S;
10168     }
10169     /* Wait for a reply... */
10170     W;
10171
10172     /* NOTREACHED */
10173     return 0;
10174 }
10175
10176 static int
10177 api_lisp_gpe_tunnel_dump(vat_main_t *vam)
10178 {
10179     vl_api_lisp_gpe_tunnel_dump_t *mp;
10180     f64 timeout = ~0;
10181
10182     if (!vam->json_output) {
10183         fformat(vam->ofp, "%=20s%=30s%=16s%=16s%=16s%=16s"
10184                 "%=16s%=16s%=16s%=16s%=16s\n",
10185                 "Tunel", "Source", "Destination", "Fib encap", "Fib decap",
10186                 "Decap next", "Lisp version", "Flags", "Next protocol",
10187                 "ver_res", "res", "iid");
10188     }
10189
10190     M(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump);
10191     /* send it... */
10192     S;
10193
10194     /* Use a control ping for synchronization */
10195     {
10196         vl_api_control_ping_t * mp;
10197         M(CONTROL_PING, control_ping);
10198         S;
10199     }
10200     /* Wait for a reply... */
10201     W;
10202
10203     /* NOTREACHED */
10204     return 0;
10205 }
10206
10207 static int
10208 api_lisp_map_resolver_dump(vat_main_t *vam)
10209 {
10210     vl_api_lisp_map_resolver_dump_t *mp;
10211     f64 timeout = ~0;
10212
10213     if (!vam->json_output) {
10214         fformat(vam->ofp, "%=20s\n",
10215                 "Map resolver");
10216     }
10217
10218     M(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump);
10219     /* send it... */
10220     S;
10221
10222     /* Use a control ping for synchronization */
10223     {
10224         vl_api_control_ping_t * mp;
10225         M(CONTROL_PING, control_ping);
10226         S;
10227     }
10228     /* Wait for a reply... */
10229     W;
10230
10231     /* NOTREACHED */
10232     return 0;
10233 }
10234
10235 static int
10236 api_lisp_enable_disable_status_dump(vat_main_t *vam)
10237 {
10238     vl_api_lisp_enable_disable_status_dump_t *mp;
10239     f64 timeout = ~0;
10240
10241     if (!vam->json_output) {
10242         fformat(vam->ofp, "%=20s\n",
10243                 "lisp status:");
10244     }
10245
10246     M(LISP_ENABLE_DISABLE_STATUS_DUMP,
10247       lisp_enable_disable_status_dump);
10248     /* send it... */
10249     S;
10250
10251     /* Use a control ping for synchronization */
10252     {
10253         vl_api_control_ping_t * mp;
10254         M(CONTROL_PING, control_ping);
10255         S;
10256     }
10257     /* Wait for a reply... */
10258     W;
10259
10260     /* NOTREACHED */
10261     return 0;
10262 }
10263
10264 static int
10265 api_af_packet_create (vat_main_t * vam)
10266 {
10267     unformat_input_t * i = vam->input;
10268     vl_api_af_packet_create_t * mp;
10269     f64 timeout;
10270     u8 * host_if_name = 0;
10271     u8 hw_addr[6];
10272     u8 random_hw_addr = 1;
10273
10274     memset (hw_addr, 0, sizeof (hw_addr));
10275
10276     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
10277         if (unformat (i, "name %s", &host_if_name))
10278             vec_add1 (host_if_name, 0);
10279         else if (unformat (i, "hw_addr %U", unformat_ethernet_address, hw_addr))
10280             random_hw_addr = 0;
10281         else
10282           break;
10283     }
10284
10285     if (!vec_len (host_if_name)) {
10286         errmsg ("host-interface name must be specified");
10287         return -99;
10288     }
10289
10290     if (vec_len (host_if_name) > 64) {
10291         errmsg ("host-interface name too long");
10292         return -99;
10293     }
10294
10295     M(AF_PACKET_CREATE, af_packet_create);
10296
10297     clib_memcpy (mp->host_if_name, host_if_name, vec_len (host_if_name));
10298     clib_memcpy (mp->hw_addr, hw_addr, 6);
10299     mp->use_random_hw_addr = random_hw_addr;
10300     vec_free (host_if_name);
10301
10302     S; W;
10303     /* NOTREACHED */
10304     return 0;
10305 }
10306
10307 static int
10308 api_af_packet_delete (vat_main_t * vam)
10309 {
10310     unformat_input_t * i = vam->input;
10311     vl_api_af_packet_delete_t * mp;
10312     f64 timeout;
10313     u8 * host_if_name = 0;
10314
10315     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
10316         if (unformat (i, "name %s", &host_if_name))
10317             vec_add1 (host_if_name, 0);
10318         else
10319           break;
10320     }
10321
10322     if (!vec_len (host_if_name)) {
10323         errmsg ("host-interface name must be specified");
10324         return -99;
10325     }
10326
10327     if (vec_len (host_if_name) > 64) {
10328         errmsg ("host-interface name too long");
10329         return -99;
10330     }
10331
10332     M(AF_PACKET_DELETE, af_packet_delete);
10333
10334     clib_memcpy (mp->host_if_name, host_if_name, vec_len (host_if_name));
10335     vec_free (host_if_name);
10336
10337     S; W;
10338     /* NOTREACHED */
10339     return 0;
10340 }
10341
10342 static int q_or_quit (vat_main_t * vam)
10343 {
10344     longjmp (vam->jump_buf, 1);
10345     return 0; /* not so much */
10346 }
10347 static int q (vat_main_t * vam) {return q_or_quit (vam);}
10348 static int quit (vat_main_t * vam) {return q_or_quit (vam);}
10349
10350 static int comment (vat_main_t * vam)
10351 {
10352     return 0;
10353 }
10354
10355 static int cmd_cmp (void * a1, void * a2)
10356 {
10357   u8 ** c1 = a1;
10358   u8 ** c2 = a2;
10359
10360   return strcmp ((char *)(c1[0]), (char *)(c2[0]));
10361 }
10362
10363 static int help (vat_main_t * vam)
10364 {
10365     u8 ** cmds = 0;
10366     u8 * name = 0;
10367     hash_pair_t * p;
10368     unformat_input_t * i = vam->input;
10369     int j;
10370
10371     if (unformat (i, "%s", &name)) {
10372         uword *hs;
10373
10374         vec_add1(name, 0);
10375
10376         hs = hash_get_mem (vam->help_by_name, name);
10377         if (hs)
10378             fformat (vam->ofp, "usage: %s %s\n", name, hs[0]);
10379         else
10380             fformat (vam->ofp, "No such msg / command '%s'\n", name);
10381         vec_free(name);
10382         return 0;
10383     }
10384
10385     fformat(vam->ofp, "Help is available for the following:\n");
10386
10387     hash_foreach_pair (p, vam->function_by_name, 
10388     ({
10389         vec_add1 (cmds, (u8 *)(p->key));
10390     }));
10391
10392     vec_sort_with_function (cmds, cmd_cmp);
10393
10394     for (j = 0; j < vec_len(cmds); j++)
10395         fformat (vam->ofp, "%s\n", cmds[j]);
10396
10397     vec_free (cmds);
10398     return 0;
10399 }
10400
10401 static int set (vat_main_t * vam)
10402 {
10403     u8 * name = 0, * value = 0;
10404     unformat_input_t * i = vam->input;
10405
10406     if (unformat (i, "%s", &name)) {
10407         /* The input buffer is a vector, not a string. */
10408         value = vec_dup (i->buffer);
10409         vec_delete (value, i->index, 0);
10410         /* Almost certainly has a trailing newline */
10411         if (value[vec_len(value)-1] == '\n')
10412             value[vec_len(value)-1] = 0;
10413         /* Make sure it's a proper string, one way or the other */
10414         vec_add1 (value, 0);
10415         (void) clib_macro_set_value (&vam->macro_main, 
10416                                      (char *)name, (char *)value);
10417     }
10418     else
10419         errmsg ("usage: set <name> <value>\n");
10420
10421     vec_free (name);
10422     vec_free (value);
10423     return 0;
10424 }
10425
10426 static int unset (vat_main_t * vam)
10427 {
10428     u8 * name = 0;
10429
10430     if (unformat (vam->input, "%s", &name))
10431         if (clib_macro_unset (&vam->macro_main, (char *)name) == 1)
10432             errmsg ("unset: %s wasn't set\n", name);
10433     vec_free (name);
10434     return 0;
10435 }
10436
10437 typedef struct {
10438     u8 * name;
10439     u8 * value;
10440 } macro_sort_t;
10441
10442
10443 static int macro_sort_cmp (void * a1, void * a2)
10444 {
10445   macro_sort_t * s1 = a1;
10446   macro_sort_t * s2 = a2;
10447
10448   return strcmp ((char *)(s1->name), (char *)(s2->name));
10449 }
10450
10451 static int dump_macro_table (vat_main_t * vam)
10452 {
10453     macro_sort_t * sort_me = 0, * sm;    
10454     int i;
10455     hash_pair_t * p;
10456
10457     hash_foreach_pair (p, vam->macro_main.the_value_table_hash, 
10458     ({
10459         vec_add2 (sort_me, sm, 1);
10460         sm->name = (u8 *)(p->key);
10461         sm->value = (u8 *) (p->value[0]);
10462     }));
10463     
10464     vec_sort_with_function (sort_me, macro_sort_cmp);
10465
10466     if (vec_len(sort_me))
10467         fformat (vam->ofp, "%-15s%s\n", "Name", "Value");
10468     else
10469         fformat (vam->ofp, "The macro table is empty...\n");
10470
10471     for (i = 0; i < vec_len (sort_me); i++)
10472         fformat (vam->ofp, "%-15s%s\n", sort_me[i].name,
10473                  sort_me[i].value);
10474     return 0;
10475 }
10476
10477 static int dump_node_table (vat_main_t * vam)
10478 {
10479     int i, j;
10480     vlib_node_t * node, * next_node;
10481
10482     if (vec_len (vam->graph_nodes) == 0) {
10483         fformat (vam->ofp, "Node table empty, issue get_node_graph...\n");
10484         return 0;
10485     }
10486
10487     for (i = 0; i < vec_len (vam->graph_nodes); i++) {
10488         node = vam->graph_nodes[i];
10489         fformat (vam->ofp, "[%d] %s\n", i, node->name);
10490         for (j = 0; j < vec_len (node->next_nodes); j++) {
10491             if (node->next_nodes[j] != ~0) {
10492                 next_node = vam->graph_nodes[node->next_nodes[j]];
10493                 fformat (vam->ofp, "  [%d] %s\n", j, next_node->name);
10494             }
10495         }
10496     }
10497     return 0;
10498 }
10499
10500 static int search_node_table (vat_main_t * vam)
10501 {
10502     unformat_input_t * line_input = vam->input;
10503     u8 * node_to_find;
10504     int j;
10505     vlib_node_t * node, * next_node;
10506     uword * p;
10507
10508     if (vam->graph_node_index_by_name == 0) {
10509         fformat (vam->ofp, "Node table empty, issue get_node_graph...\n");
10510         return 0;
10511     }
10512
10513     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
10514         if (unformat (line_input, "%s", &node_to_find)) {
10515             vec_add1 (node_to_find, 0);
10516             p = hash_get_mem (vam->graph_node_index_by_name, node_to_find);
10517             if (p == 0) {
10518                 fformat (vam->ofp, "%s not found...\n", node_to_find);
10519                 goto out;
10520             }
10521             node = vam->graph_nodes[p[0]];
10522             fformat (vam->ofp, "[%d] %s\n", p[0], node->name);
10523             for (j = 0; j < vec_len (node->next_nodes); j++) {
10524                 if (node->next_nodes[j] != ~0) {
10525                     next_node = vam->graph_nodes[node->next_nodes[j]];
10526                     fformat (vam->ofp, "  [%d] %s\n", j, next_node->name);
10527                 }
10528             }
10529         }
10530             
10531         else {
10532             clib_warning ("parse error '%U'", format_unformat_error, 
10533                           line_input);
10534             return -99;
10535         }
10536
10537     out:
10538         vec_free(node_to_find);
10539         
10540     }
10541
10542     return 0;        
10543 }
10544
10545
10546 static int script (vat_main_t * vam)
10547 {
10548     u8 * s = 0;
10549     char * save_current_file;
10550     unformat_input_t save_input;
10551     jmp_buf save_jump_buf;
10552     u32 save_line_number;
10553
10554     FILE * new_fp, * save_ifp;
10555
10556     if (unformat (vam->input, "%s", &s)) {
10557         new_fp = fopen ((char *)s, "r");
10558         if (new_fp == 0) {
10559             errmsg ("Couldn't open script file %s\n", s);
10560             vec_free (s);
10561             return -99;
10562         }
10563     } else {
10564         errmsg ("Missing script name\n");
10565         return -99;
10566     }
10567
10568     clib_memcpy (&save_input, &vam->input, sizeof (save_input));
10569     clib_memcpy (&save_jump_buf, &vam->jump_buf, sizeof (save_jump_buf));
10570     save_ifp = vam->ifp;
10571     save_line_number = vam->input_line_number;
10572     save_current_file = (char *) vam->current_file;
10573
10574     vam->input_line_number = 0;
10575     vam->ifp = new_fp;
10576     vam->current_file = s;
10577     do_one_file (vam);
10578
10579     clib_memcpy (&vam->input, &save_input, sizeof (vam->input));
10580     clib_memcpy (&vam->jump_buf, &save_jump_buf, sizeof (save_jump_buf));
10581     vam->ifp = save_ifp;
10582     vam->input_line_number = save_line_number;
10583     vam->current_file = (u8 *) save_current_file;
10584     vec_free (s);
10585
10586     return 0;
10587 }
10588
10589 static int echo (vat_main_t * vam)
10590 {
10591     fformat (vam->ofp, "%v", vam->input->buffer);
10592     return 0;
10593 }
10594
10595 /* List of API message constructors, CLI names map to api_xxx */
10596 #define foreach_vpe_api_msg                                             \
10597 _(create_loopback,"[mac <mac-addr>]")                                   \
10598 _(sw_interface_dump,"")                                                 \
10599 _(sw_interface_set_flags,                                               \
10600   "<intfc> | sw_if_index <id> admin-up | admin-down link-up | link down") \
10601 _(sw_interface_add_del_address,                                         \
10602   "<intfc> | sw_if_index <id> <ip4-address> | <ip6-address> [del] [del-all] ") \
10603 _(sw_interface_set_table,                                               \
10604   "<intfc> | sw_if_index <id> vrf <table-id> [ipv6]")                   \
10605 _(sw_interface_set_vpath,                                               \
10606   "<intfc> | sw_if_index <id> enable | disable")                        \
10607 _(sw_interface_set_l2_xconnect,                                         \
10608   "rx <intfc> | rx_sw_if_index <id> tx <intfc> | tx_sw_if_index <id>\n" \
10609   "enable | disable")                                                   \
10610 _(sw_interface_set_l2_bridge,                                           \
10611   "rx <intfc> | rx_sw_if_index <id> bd_id <bridge-domain-id>\n"         \
10612   "[shg <split-horizon-group>] [bvi]\n"                                 \
10613   "enable | disable")                                                   \
10614 _(bridge_domain_add_del,                                                \
10615   "bd_id <bridge-domain-id> [flood 1|0] [uu-flood 1|0] [forward 1|0] [learn 1|0] [arp-term 1|0] [del]\n")\
10616 _(bridge_domain_dump, "[bd_id <bridge-domain-id>]\n")     \
10617 _(l2fib_add_del,                                                        \
10618   "mac <mac-addr> bd_id <bridge-domain-id> [del] | sw_if <intfc> | sw_if_index <id> [static] [filter] [bvi]\n") \
10619 _(l2_flags,                                                             \
10620   "sw_if <intfc> | sw_if_index <id> [learn] [forward] [uu-flood] [flood]\n")       \
10621 _(bridge_flags,                                                         \
10622   "bd_id <bridge-domain-id> [learn] [forward] [uu-flood] [flood] [arp-term] [disable]\n") \
10623 _(tap_connect,                                                          \
10624   "tapname <name> mac <mac-addr> | random-mac")                         \
10625 _(tap_modify,                                                           \
10626   "<vpp-if-name> | sw_if_index <id> tapname <name> mac <mac-addr> | random-mac") \
10627 _(tap_delete,                                                           \
10628   "<vpp-if-name> | sw_if_index <id>")                                   \
10629 _(sw_interface_tap_dump, "")                                            \
10630 _(ip_add_del_route,                                                     \
10631   "<addr>/<mask> via <addr> [vrf <n>]\n"                                \
10632   "[<intfc> | sw_if_index <id>] [resolve-attempts <n>]\n"               \
10633   "[weight <n>] [drop] [local] [classify <n>] [del]\n"                  \
10634   "[multipath] [count <n>]")                                            \
10635 _(proxy_arp_add_del,                                                    \
10636   "<lo-ip4-addr> - <hi-ip4-addr> [vrf <n>] [del]")                      \
10637 _(proxy_arp_intfc_enable_disable,                                       \
10638   "<intfc> | sw_if_index <id> enable | disable")                        \
10639 _(mpls_add_del_encap,                                                   \
10640   "label <n> dst <ip4-addr> [vrf <n>] [del]")                           \
10641 _(mpls_add_del_decap,                                                   \
10642   "label <n> [rx_vrf_id <n>] [tx_vrf_id] [s-bit-clear][del]")           \
10643 _(mpls_gre_add_del_tunnel,                                              \
10644   "inner_vrf_id <n> outer_vrf_id <n> src <ip4-address> dst <ip4-address>\n" \
10645   "adj <ip4-address>/<mask-width> [del]")                               \
10646 _(sw_interface_set_unnumbered,                                          \
10647   "<intfc> | sw_if_index <id> unnum_if_index <id> [del]")               \
10648 _(ip_neighbor_add_del,                                                  \
10649   "<intfc> | sw_if_index <id> dst <ip46-address> mac <mac-addr>")       \
10650 _(reset_vrf, "vrf <id> [ipv6]")                                         \
10651 _(create_vlan_subif, "<intfc> | sw_if_index <id> vlan <n>")             \
10652 _(create_subif, "<intfc> | sw_if_index <id> sub_id <n>\n"               \
10653   "[outer_vlan_id <n>][inner_vlan_id <n>]\n"                            \
10654   "[no_tags][one_tag][two_tags][dot1ad][exact_match][default_sub]\n"    \
10655   "[outer_vlan_id_any][inner_vlan_id_any]")                             \
10656 _(oam_add_del, "src <ip4-address> dst <ip4-address> [vrf <n>] [del]")   \
10657 _(reset_fib, "vrf <n> [ipv6]")                                          \
10658 _(dhcp_proxy_config,                                                    \
10659   "svr <v46-address> src <v46-address>\n"                               \
10660    "insert-cid <n> [del]")                                              \
10661 _(dhcp_proxy_config_2,                                                  \
10662   "svr <v46-address> src <v46-address>\n"                               \
10663    "rx_vrf_id <nn> server_vrf_id <nn> insert-cid <n> [del]")            \
10664 _(dhcp_proxy_set_vss,                                                   \
10665   "tbl_id <n> fib_id <n> oui <n> [ipv6] [del]")                         \
10666 _(dhcp_client_config,                                                   \
10667   "<intfc> | sw_if_index <id> [hostname <name>] [disable_event] [del]") \
10668 _(set_ip_flow_hash,                                                     \
10669   "vrf <n> [src] [dst] [sport] [dport] [proto] [reverse] [ipv6]")       \
10670 _(sw_interface_ip6_enable_disable,                                      \
10671   "<intfc> | sw_if_index <id> enable | disable")                        \
10672 _(sw_interface_ip6_set_link_local_address,                              \
10673   "<intfc> | sw_if_index <id> <ip6-address>/<mask-width>")              \
10674 _(sw_interface_ip6nd_ra_prefix,                                         \
10675   "<intfc> | sw_if_index <id> <ip6-address>/<mask-width>\n"             \
10676   "val_life <n> pref_life <n> [def] [noadv] [offl] [noauto]\n"          \
10677   "[nolink] [isno]")                                                    \
10678 _(sw_interface_ip6nd_ra_config,                                         \
10679   "<intfc> | sw_if_index <id> [maxint <n>] [minint <n>]\n"              \
10680   "[life <n>] [count <n>] [interval <n>] [surpress]\n"                  \
10681   "[managed] [other] [ll] [send] [cease] [isno] [def]")                 \
10682 _(set_arp_neighbor_limit, "arp_nbr_limit <n> [ipv6]")                   \
10683 _(l2_patch_add_del,                                                     \
10684   "rx <intfc> | rx_sw_if_index <id> tx <intfc> | tx_sw_if_index <id>\n" \
10685   "enable | disable")                                                   \
10686 _(mpls_ethernet_add_del_tunnel,                                         \
10687   "tx <intfc> | tx_sw_if_index <n> dst <mac-addr>\n"                    \
10688   "adj <ip4-addr>/<mw> dst <mac-addr> [del]")                           \
10689 _(mpls_ethernet_add_del_tunnel_2,                                       \
10690   "inner_vrf_id <n> outer_vrf_id <n> next-hop <ip4-addr>\n"             \
10691   "resolve-attempts <n> resolve-if-needed 0 | 1 [del]")                 \
10692 _(sr_tunnel_add_del,                                                    \
10693   "[name <name>] src <ip6-addr> dst <ip6-addr>/<mw> \n"                 \
10694   "(next <ip6-addr>)+ [tag <ip6-addr>]* [clean] [reroute] \n"           \
10695   "[policy <policy_name>]")                                             \
10696 _(sr_policy_add_del,                                                    \
10697   "name <name> tunnel <tunnel-name> [tunnel <tunnel-name>]* [del]")     \
10698 _(sr_multicast_map_add_del,                                             \
10699   "address [ip6 multicast address] sr-policy [policy name] [del]")      \
10700 _(classify_add_del_table,                                               \
10701   "buckets <nn> [skip <n>] [match <n>] [memory_size <nn-bytes>]\n"      \
10702   "[del] mask <mask-value>\n"                                           \
10703   " [l2-miss-next | miss-next | acl-miss-next] <name|nn>")              \
10704 _(classify_add_del_session,                                             \
10705   "[hit-next|l2-hit-next|acl-hit-next] <name|nn> table-index <nn>\n"    \
10706   "skip_n <nn> match_n <nn> match [hex] [l2] [l3 [ip4|ip6]]")           \
10707 _(classify_set_interface_ip_table,                                      \
10708   "<intfc> | sw_if_index <nn> table <nn>")                              \
10709 _(classify_set_interface_l2_tables,                                     \
10710   "<intfc> | sw_if_index <nn> [ip4-table <nn>] [ip6-table <nn>]\n"      \
10711   "  [other-table <nn>]")                                               \
10712 _(get_node_index, "node <node-name")                                    \
10713 _(add_node_next, "node <node-name> next <next-node-name>")              \
10714 _(l2tpv3_create_tunnel,                                                 \
10715   "client_address <ip6-addr> our_address <ip6-addr>\n"                  \
10716   "[local_session_id <nn>][remote_session_id <nn>][local_cookie <nn>]\n"\
10717   "[remote_cookie <nn>]\n[l2-sublayer-preset]\n")                       \
10718 _(l2tpv3_set_tunnel_cookies,                                            \
10719   "<intfc> | sw_if_index <nn> [new_local_cookie <nn>]\n"                \
10720   "[new_remote_cookie <nn>]\n")                                         \
10721 _(l2tpv3_interface_enable_disable,                                      \
10722   "<intfc> | sw_if_index <nn> enable | disable")                        \
10723 _(l2tpv3_set_lookup_key,                                                \
10724   "lookup_v6_src | lookup_v6_dst | lookup_session_id")                  \
10725 _(sw_if_l2tpv3_tunnel_dump, "")                                         \
10726 _(vxlan_add_del_tunnel,                                                 \
10727   "src <ip4-addr> dst <ip4-addr> vni <vni> [encap-vrf-id <nn>]\n"       \
10728   " [decap-next l2|ip4|ip6] [del]")                                     \
10729 _(vxlan_tunnel_dump, "[<intfc> | sw_if_index <nn>]")                    \
10730 _(gre_add_del_tunnel,                                                   \
10731   "src <ip4-addr> dst <ip4-addr> [outer-fib-id <nn>] [del]\n")          \
10732 _(gre_tunnel_dump, "[<intfc> | sw_if_index <nn>]")                      \
10733 _(l2_fib_clear_table, "")                                               \
10734 _(l2_interface_efp_filter, "sw_if_index <nn> enable | disable")         \
10735 _(l2_interface_vlan_tag_rewrite,                                        \
10736   "<intfc> | sw_if_index <nn> \n"                                       \
10737   "[disable][push-[1|2]][pop-[1|2]][translate-1-[1|2]] \n"              \
10738   "[translate-2-[1|2]] [push_dot1q 0] tag1 <nn> tag2 <nn>")             \
10739 _(create_vhost_user_if,                                                 \
10740         "socket <filename> [server] [renumber <dev_instance>] "         \
10741         "[mac <mac_address>]")                                          \
10742 _(modify_vhost_user_if,                                                 \
10743         "<intfc> | sw_if_index <nn> socket <filename>\n"                \
10744         "[server] [renumber <dev_instance>]")                           \
10745 _(delete_vhost_user_if, "<intfc> | sw_if_index <nn>")                   \
10746 _(sw_interface_vhost_user_dump, "")                                     \
10747 _(show_version, "")                                                     \
10748 _(nsh_gre_add_del_tunnel,                                               \
10749   "src <ip4-addr> dst <ip4-addr>"                                       \
10750   "c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn>\n"                  \
10751   "[encap-fib-id <nn>] [decap-fib-id <nn>] [o-bit <1|0>]\n"             \
10752   "[c-bit <1|0>] [md-type <nn>][next-ip4][next-ip6][next-ethernet]\n"   \
10753   "[tlv <xx>][del]")                                                    \
10754 _(nsh_vxlan_gpe_add_del_tunnel,                                         \
10755   "src <ip4-addr> dst <ip4-addr> vni <nn>\n"                            \
10756   "c1 <nn> c2 <nn> c3 <nn> c4 <nn> spi <nn> si <nn>\n"                  \
10757   "[encap-vrf-id <nn>] [decap-vrf-id <nn>] [o-bit <1|0>]\n"             \
10758   "[c-bit <1|0>] [md-type <nn>][next-ip4][next-ip6][next-ethernet]\n"   \
10759   "[tlv <xx>][del]")                                                    \
10760 _(l2_fib_table_dump, "bd_id <bridge-domain-id>")                        \
10761 _(interface_name_renumber,                                              \
10762   "<intfc> | sw_if_index <nn> new_show_dev_instance <nn>")              \
10763 _(input_acl_set_interface,                                              \
10764   "<intfc> | sw_if_index <nn> [ip4-table <nn>] [ip6-table <nn>]\n"      \
10765   "  [l2-table <nn>] [del]")                                            \
10766 _(want_ip4_arp_events, "address <ip4-address> [del]")                   \
10767 _(ip_address_dump, "(ipv4 | ipv6) (<intfc> | sw_if_index <id>)")        \
10768 _(ip_dump, "ipv4 | ipv6")                                               \
10769 _(ipsec_spd_add_del, "spd_id <n> [del]")                                \
10770 _(ipsec_interface_add_del_spd, "(<intfc> | sw_if_index <id>)\n"         \
10771   "  spid_id <n> ")                                                     \
10772 _(ipsec_sad_add_del_entry, "sad_id <n> spi <n> crypto_alg <alg>\n"      \
10773   "  crypto_key <hex> tunnel_src <ip4|ip6> tunnel_dst <ip4|ip6>\n"      \
10774   "  integ_alg <alg> integ_key <hex>")                                  \
10775 _(ipsec_spd_add_del_entry, "spd_id <n> priority <n> action <action>\n"  \
10776   "  (inbound|outbound) [sa_id <n>] laddr_start <ip4|ip6>\n"            \
10777   "  laddr_stop <ip4|ip6> raddr_start <ip4|ip6> raddr_stop <ip4|ip6>\n" \
10778   "  [lport_start <n> lport_stop <n>] [rport_start <n> rport_stop <n>]" )\
10779 _(ipsec_sa_set_key, "sa_id <n> crypto_key <hex> integ_key <hex>")       \
10780 _(ikev2_profile_add_del, "name <profile_name> [del]")                   \
10781 _(ikev2_profile_set_auth, "name <profile_name> auth_method <method>\n"  \
10782   "(auth_data 0x<data> | auth_data <data>)")                            \
10783 _(ikev2_profile_set_id, "name <profile_name> id_type <type>\n"          \
10784   "(id_data 0x<data> | id_data <data>) (local|remote)")                 \
10785 _(ikev2_profile_set_ts, "name <profile_name> protocol <proto>\n"        \
10786   "start_port <port> end_port <port> start_addr <ip4> end_addr <ip4>\n" \
10787   "(local|remote)")                                                     \
10788 _(ikev2_set_local_key, "file <absolute_file_path>")                     \
10789 _(delete_loopback,"sw_if_index <nn>")                                   \
10790 _(bd_ip_mac_add_del, "bd_id <bridge-domain-id> <ip4/6-addr> <mac-addr> [del]") \
10791 _(map_add_domain,                                                       \
10792   "ip4-pfx <ip4pfx> ip6-pfx <ip6pfx> "                                  \
10793   "ip6-src <ip6addr> "                                                  \
10794   "ea-bits-len <n> psid-offset <n> psid-len <n>")                       \
10795 _(map_del_domain, "index <n>")                                          \
10796 _(map_add_del_rule,                                                     \
10797   "index <n> psid <n> dst <ip6addr> [del]")                             \
10798 _(map_domain_dump, "")                                                  \
10799 _(map_rule_dump, "index <map-domain>")                                  \
10800 _(want_interface_events,  "enable|disable")                             \
10801 _(want_stats,"enable|disable")                                          \
10802 _(get_first_msg_id, "client <name>")                                    \
10803 _(cop_interface_enable_disable, "<intfc> | sw_if_index <nn> [disable]") \
10804 _(cop_whitelist_enable_disable, "<intfc> | sw_if_index <nn>\n"          \
10805   "fib-id <nn> [ip4][ip6][default]")                                    \
10806 _(get_node_graph, " ")                                                  \
10807 _(sw_interface_clear_stats,"<intfc> | sw_if_index <nn>")                \
10808 _(trace_profile_add, "id <nn> trace-type <0x1f|0x3|0x9|0x11|0x19> "     \
10809   "trace-elts <nn> trace-tsp <0|1|2|3> node-id <node id in hex> "       \
10810   "app-data <app_data in hex> [pow] [ppc <encap|decap>]")               \
10811 _(trace_profile_apply, "id <nn> <ip6-address>/<width>"                  \
10812   " vrf_id <nn>  add | pop | none")                                     \
10813 _(trace_profile_del, "")                                                \
10814 _(lisp_add_del_locator_set, "locator-set <locator_name> [del]")         \
10815 _(lisp_add_del_locator, "locator-set <locator_name> "                   \
10816                         "iface <intf> | sw_if_index <sw_if_index> "     \
10817                         "p <priority> w <weight> [del]")                \
10818 _(lisp_add_del_local_eid, "<ipv4|ipv6>/<prefix> "                       \
10819                           "locator-set <locator_name> [del]")           \
10820 _(lisp_gpe_add_del_fwd_entry, "eid <ip4|6-addr>/<prefix> "              \
10821     "sloc <ip4/6-addr> dloc <ip4|6-addr> [del]")                        \
10822 _(lisp_add_del_map_resolver, "<ip4|6-addr> [del]")                      \
10823 _(lisp_gpe_enable_disable, "enable|disable")                            \
10824 _(lisp_enable_disable, "enable|disable")                                \
10825 _(lisp_gpe_add_del_iface, "up|down")                                    \
10826 _(lisp_locator_set_dump, "")                                            \
10827 _(lisp_local_eid_table_dump, "")                                        \
10828 _(lisp_gpe_tunnel_dump, "")                                             \
10829 _(lisp_map_resolver_dump, "")                                           \
10830 _(lisp_enable_disable_status_dump, "")                                  \
10831 _(af_packet_create, "name <host interface name> [hw_addr <mac>]")       \
10832 _(af_packet_delete, "name <host interface name>")
10833
10834 /* List of command functions, CLI names map directly to functions */
10835 #define foreach_cli_function                                    \
10836 _(comment, "usage: comment <ignore-rest-of-line>")              \
10837 _(dump_interface_table, "usage: dump_interface_table")          \
10838 _(dump_sub_interface_table, "usage: dump_sub_interface_table")  \
10839 _(dump_ipv4_table, "usage: dump_ipv4_table")                    \
10840 _(dump_ipv6_table, "usage: dump_ipv6_table")                    \
10841 _(dump_stats_table, "usage: dump_stats_table")                  \
10842 _(dump_macro_table, "usage: dump_macro_table ")                 \
10843 _(dump_node_table, "usage: dump_node_table")                    \
10844 _(echo, "usage: echo <message>")                                \
10845 _(exec, "usage: exec <vpe-debug-CLI-command>")                  \
10846 _(help, "usage: help")                                          \
10847 _(q, "usage: quit")                                             \
10848 _(quit, "usage: quit")                                          \
10849 _(search_node_table, "usage: search_node_table <name>...")      \
10850 _(set, "usage: set <variable-name> <value>")                    \
10851 _(script, "usage: script <file-name>")                          \
10852 _(unset, "usage: unset <variable-name>")
10853
10854 #define _(N,n)                                  \
10855     static void vl_api_##n##_t_handler_uni      \
10856     (vl_api_##n##_t * mp)                       \
10857     {                                           \
10858         vat_main_t * vam = &vat_main;           \
10859         if (vam->json_output) {                 \
10860             vl_api_##n##_t_handler_json(mp);    \
10861         } else {                                \
10862             vl_api_##n##_t_handler(mp);         \
10863         }                                       \
10864     }
10865 foreach_vpe_api_reply_msg;
10866 #undef _
10867
10868 void vat_api_hookup (vat_main_t *vam)
10869 {
10870 #define _(N,n)                                                  \
10871     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
10872                            vl_api_##n##_t_handler_uni,          \
10873                            vl_noop_handler,                     \
10874                            vl_api_##n##_t_endian,               \
10875                            vl_api_##n##_t_print,                \
10876                            sizeof(vl_api_##n##_t), 1); 
10877     foreach_vpe_api_reply_msg;
10878 #undef _
10879
10880     vl_msg_api_set_first_available_msg_id (VL_MSG_FIRST_AVAILABLE);
10881
10882     vam->sw_if_index_by_interface_name = 
10883         hash_create_string (0, sizeof (uword));
10884
10885     vam->function_by_name = 
10886         hash_create_string (0, sizeof(uword));
10887
10888     vam->help_by_name = 
10889         hash_create_string (0, sizeof(uword));
10890
10891     /* API messages we can send */
10892 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
10893     foreach_vpe_api_msg;
10894 #undef _
10895
10896     /* Help strings */
10897 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
10898     foreach_vpe_api_msg;
10899 #undef _
10900
10901     /* CLI functions */
10902 #define _(n,h) hash_set_mem (vam->function_by_name, #n, n);
10903     foreach_cli_function;
10904 #undef _
10905
10906     /* Help strings */
10907 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
10908     foreach_cli_function;
10909 #undef _
10910 }
10911
10912 #undef vl_api_version
10913 #define vl_api_version(n,v) static u32 vpe_api_version = v;
10914 #include <api/vpe.api.h>
10915 #undef vl_api_version
10916
10917 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp) 
10918 {
10919     /* 
10920      * Send the main API signature in slot 0. This bit of code must
10921      * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
10922      */
10923     mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
10924 }