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