sr: show the hop-limit value used for SRv6 encapsulation
[vpp.git] / src / plugins / nat / nat_test.c
1 /*
2  * nat.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20
21 #include <vppinfra/error.h>
22 #include <vnet/ip/ip.h>
23 #include <nat/nat.h>
24
25 #define __plugin_msg_base snat_test_main.msg_id_base
26 #include <vlibapi/vat_helper_macros.h>
27
28 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
29
30 /* Declare message IDs */
31 #include <nat/nat_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <nat/nat_all_api_h.h>
36 #undef vl_typedefs
37
38 /* declare message handlers for each api */
39
40 #define vl_endianfun             /* define message structures */
41 #include <nat/nat_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
46 #define vl_printfun
47 #include <nat/nat_all_api_h.h>
48 #undef vl_printfun
49
50 /* Get the API version number. */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <nat/nat_all_api_h.h>
53 #undef vl_api_version
54
55 typedef struct {
56     /* API message ID base */
57     u16 msg_id_base;
58     vat_main_t *vat_main;
59 } snat_test_main_t;
60
61 snat_test_main_t snat_test_main;
62
63 #define foreach_standard_reply_retval_handler    \
64 _(nat44_add_del_address_range_reply)             \
65 _(nat44_interface_add_del_feature_reply)         \
66 _(nat44_interface_add_del_output_feature_reply)  \
67 _(nat44_add_del_static_mapping_reply)            \
68 _(nat_set_workers_reply)                         \
69 _(nat44_add_del_interface_addr_reply)            \
70 _(nat_ipfix_enable_disable_reply)                \
71 _(nat_set_timeouts_reply)
72
73 #define _(n)                                            \
74     static void vl_api_##n##_t_handler                  \
75     (vl_api_##n##_t * mp)                               \
76     {                                                   \
77         vat_main_t * vam = snat_test_main.vat_main;     \
78         i32 retval = ntohl(mp->retval);                 \
79         if (vam->async_mode) {                          \
80             vam->async_errors += (retval < 0);          \
81         } else {                                        \
82             vam->retval = retval;                       \
83             vam->result_ready = 1;                      \
84         }                                               \
85     }
86 foreach_standard_reply_retval_handler;
87 #undef _
88
89 /*
90  * Table of message reply handlers, must include boilerplate handlers
91  * we just generated
92  */
93 #define foreach_vpe_api_reply_msg                               \
94 _(NAT44_ADD_DEL_ADDRESS_RANGE_REPLY,                            \
95   nat44_add_del_address_range_reply)                            \
96 _(NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY,                        \
97   nat44_interface_add_del_feature_reply)                        \
98 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY,                 \
99   nat44_interface_add_del_output_feature_reply)                 \
100 _(NAT44_ADD_DEL_STATIC_MAPPING_REPLY,                           \
101   nat44_add_del_static_mapping_reply)                           \
102 _(NAT_CONTROL_PING_REPLY, nat_control_ping_reply)               \
103 _(NAT44_STATIC_MAPPING_DETAILS, nat44_static_mapping_details)   \
104 _(NAT_SHOW_CONFIG_REPLY, nat_show_config_reply)                 \
105 _(NAT_SHOW_CONFIG_2_REPLY, nat_show_config_2_reply)             \
106 _(NAT44_ADDRESS_DETAILS, nat44_address_details)                 \
107 _(NAT44_INTERFACE_DETAILS, nat44_interface_details)             \
108 _(NAT_SET_WORKERS_REPLY, nat_set_workers_reply)                 \
109 _(NAT_WORKER_DETAILS, nat_worker_details)                       \
110 _(NAT44_ADD_DEL_INTERFACE_ADDR_REPLY,                           \
111   nat44_add_del_interface_addr_reply)                           \
112 _(NAT44_INTERFACE_ADDR_DETAILS, nat44_interface_addr_details)   \
113 _(NAT_IPFIX_ENABLE_DISABLE_REPLY,                               \
114   nat_ipfix_enable_disable_reply)                               \
115 _(NAT44_USER_DETAILS, nat44_user_details)                       \
116 _(NAT44_USER_SESSION_DETAILS, nat44_user_session_details)       \
117 _(NAT_SET_TIMEOUTS_REPLY, nat_set_timeouts_reply)               \
118 _(NAT_GET_TIMEOUTS_REPLY, nat_get_timeouts_reply)
119
120 static int api_nat44_add_del_address_range (vat_main_t * vam)
121 {
122   unformat_input_t * i = vam->input;
123   ip4_address_t start_addr, end_addr;
124   u32 start_host_order, end_host_order;
125   vl_api_nat44_add_del_address_range_t * mp;
126   u8 is_add = 1;
127   u8 twice_nat = 0;
128   int vrf_id = ~0;
129   int count;
130   int ret;
131
132   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
133     {
134       if (unformat (i, "%U - %U",
135                     unformat_ip4_address, &start_addr,
136                     unformat_ip4_address, &end_addr))
137         ;
138       else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
139         end_addr = start_addr;
140       else if (unformat (i, "twice-nat"))
141         twice_nat = 1;
142       else if (unformat (i, "vrf %u", &vrf_id))
143         ;
144       else if (unformat (i, "del"))
145         is_add = 0;
146       else
147         {
148           clib_warning("unknown input '%U'", format_unformat_error, i);
149           return -99;
150         }
151     }
152
153   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
154   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
155
156   if (end_host_order < start_host_order)
157     {
158       errmsg ("end address less than start address\n");
159       return -99;
160     }
161
162   count = (end_host_order - start_host_order) + 1;
163
164   if (count > 1024)
165     {
166     errmsg ("%U - %U, %d addresses...\n",
167            format_ip4_address, &start_addr,
168            format_ip4_address, &end_addr,
169            count);
170     }
171
172   M(NAT44_ADD_DEL_ADDRESS_RANGE, mp);
173
174   memcpy (mp->first_ip_address, &start_addr, 4);
175   memcpy (mp->last_ip_address, &end_addr, 4);
176   mp->vrf_id = vrf_id;
177   if (twice_nat)
178     mp->flags = (vl_api_nat_config_flags_t)NAT_API_IS_TWICE_NAT;
179   mp->is_add = is_add;
180
181   S(mp);
182   W (ret);
183   return ret;
184 }
185
186 static int api_nat44_interface_add_del_feature (vat_main_t * vam)
187 {
188   unformat_input_t * i = vam->input;
189   vl_api_nat44_interface_add_del_feature_t * mp;
190   u32 sw_if_index;
191   u8 sw_if_index_set = 0;
192   u8 is_inside = 1;
193   u8 is_add = 1;
194   int ret;
195
196   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
197     {
198       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
199         sw_if_index_set = 1;
200       else if (unformat (i, "sw_if_index %d", &sw_if_index))
201         sw_if_index_set = 1;
202       else if (unformat (i, "out"))
203         is_inside = 0;
204       else if (unformat (i, "in"))
205         is_inside = 1;
206       else if (unformat (i, "del"))
207         is_add = 0;
208       else
209         {
210           clib_warning("unknown input '%U'", format_unformat_error, i);
211           return -99;
212         }
213     }
214
215   if (sw_if_index_set == 0)
216     {
217       errmsg ("interface / sw_if_index required\n");
218       return -99;
219     }
220
221   M(NAT44_INTERFACE_ADD_DEL_FEATURE, mp);
222   mp->sw_if_index = ntohl(sw_if_index);
223   mp->is_add = is_add;
224   if (is_inside)
225     mp->flags |= NAT_API_IS_INSIDE;
226
227   S(mp);
228   W (ret);
229   return ret;
230 }
231
232 static int api_nat44_interface_add_del_output_feature (vat_main_t * vam)
233 {
234   unformat_input_t * i = vam->input;
235   vl_api_nat44_interface_add_del_output_feature_t * mp;
236   u32 sw_if_index;
237   u8 sw_if_index_set = 0;
238   u8 is_inside = 1;
239   u8 is_add = 1;
240   int ret;
241
242   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
243     {
244       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
245         sw_if_index_set = 1;
246       else if (unformat (i, "sw_if_index %d", &sw_if_index))
247         sw_if_index_set = 1;
248       else if (unformat (i, "out"))
249         is_inside = 0;
250       else if (unformat (i, "in"))
251         is_inside = 1;
252       else if (unformat (i, "del"))
253         is_add = 0;
254       else
255         {
256           clib_warning("unknown input '%U'", format_unformat_error, i);
257           return -99;
258         }
259     }
260
261   if (sw_if_index_set == 0)
262     {
263       errmsg ("interface / sw_if_index required\n");
264       return -99;
265     }
266
267   M(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, mp);
268   mp->sw_if_index = ntohl(sw_if_index);
269   mp->is_add = is_add;
270   if (is_inside)
271     mp->flags |= NAT_API_IS_INSIDE;
272
273   S(mp);
274   W (ret);
275   return ret;
276 }
277
278 static int api_nat44_add_del_static_mapping(vat_main_t * vam)
279 {
280   unformat_input_t * i = vam->input;
281   vl_api_nat44_add_del_static_mapping_t * mp;
282   u8 external_addr_set = 0;
283   u8 local_addr_set = 0;
284   u8 is_add = 1;
285   u8 addr_only = 1;
286   ip4_address_t local_addr, external_addr;
287   u32 local_port = 0, external_port = 0, vrf_id = ~0;
288   u32 sw_if_index = ~0;
289   u8 sw_if_index_set = 0;
290   u32 proto = NAT_PROTOCOL_OTHER;
291   u8 proto_set = 0;
292   int ret;
293
294   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
295     {
296       if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
297         local_addr_set = 1;
298       else if (unformat (i, "external_addr %U", unformat_ip4_address,
299                          &external_addr))
300         external_addr_set = 1;
301       else if (unformat (i, "local_port %u", &local_port))
302         addr_only = 0;
303       else if (unformat (i, "external_port %u", &external_port))
304         addr_only = 0;
305       else if (unformat (i, "external_if %U", unformat_sw_if_index, vam,
306                          &sw_if_index))
307         sw_if_index_set = 1;
308       else if (unformat (i, "external_sw_if_index %d", &sw_if_index))
309         sw_if_index_set = 1;
310       else if (unformat (i, "vrf %u", &vrf_id))
311         ;
312       else if (unformat (i, "protocol %u", &proto))
313         proto_set = 1;
314       else if (unformat (i, "del"))
315         is_add = 0;
316       else
317         {
318           clib_warning("unknown input '%U'", format_unformat_error, i);
319           return -99;
320         }
321     }
322
323   if (!addr_only && !proto_set)
324     {
325       errmsg ("protocol required\n");
326       return -99;
327     }
328
329   if (!local_addr_set)
330     {
331       errmsg ("local addr required\n");
332       return -99;
333     }
334   if (!external_addr_set && !sw_if_index_set)
335     {
336       errmsg ("external addr or interface required\n");
337       return -99;
338     }
339
340   M(NAT44_ADD_DEL_STATIC_MAPPING, mp);
341
342   mp->is_add = is_add;
343   if (addr_only)
344     mp->flags |= NAT_API_IS_ADDR_ONLY;
345
346   mp->local_port = ntohs ((u16) local_port);
347   mp->external_port = ntohs ((u16) external_port);
348   mp->external_sw_if_index = ntohl (sw_if_index);
349   mp->vrf_id = ntohl (vrf_id);
350   mp->protocol = (u8) proto;
351   memcpy (mp->local_ip_address, &local_addr, 4);
352   memcpy (mp->external_ip_address, &external_addr, 4);
353
354   S(mp);
355   W (ret);
356   return ret;
357 }
358
359 static void vl_api_nat_control_ping_reply_t_handler
360   (vl_api_nat_control_ping_reply_t * mp)
361 {
362   vat_main_t *vam = &vat_main;
363   i32 retval = ntohl (mp->retval);
364   if (vam->async_mode)
365     {
366       vam->async_errors += (retval < 0);
367     }
368   else
369     {
370       vam->retval = retval;
371       vam->result_ready = 1;
372     }
373 }
374
375 static void vl_api_nat44_static_mapping_details_t_handler
376   (vl_api_nat44_static_mapping_details_t *mp)
377 {
378   snat_test_main_t * sm = &snat_test_main;
379   vat_main_t *vam = sm->vat_main;
380   nat_config_flags_t flags = (nat_config_flags_t) mp->flags;
381
382   if (flags & NAT_API_IS_ADDR_ONLY && mp->external_sw_if_index != ~0)
383       fformat (vam->ofp, "%15U%6s%15d%6s%11d%6d\n",
384                format_ip4_address, &mp->local_ip_address, "",
385                ntohl (mp->external_sw_if_index), "",
386                ntohl (mp->vrf_id),
387                mp->protocol);
388   else if (flags & NAT_API_IS_ADDR_ONLY && mp->external_sw_if_index == ~0)
389       fformat (vam->ofp, "%15U%6s%15U%6s%11d%6d\n",
390                format_ip4_address, &mp->local_ip_address, "",
391                format_ip4_address, &mp->external_ip_address, "",
392                ntohl (mp->vrf_id),
393                mp->protocol);
394   else if (!(flags & NAT_API_IS_ADDR_ONLY) && mp->external_sw_if_index != ~0)
395       fformat (vam->ofp, "%15U%6d%15d%6d%11d%6d\n",
396                format_ip4_address, &mp->local_ip_address,
397                ntohs (mp->local_port),
398                ntohl (mp->external_sw_if_index),
399                ntohs (mp->external_port),
400                ntohl (mp->vrf_id),
401                mp->protocol);
402   else
403       fformat (vam->ofp, "%15U%6d%15U%6d%11d%6d\n",
404                format_ip4_address, &mp->local_ip_address,
405                ntohs (mp->local_port),
406                format_ip4_address, &mp->external_ip_address,
407                ntohs (mp->external_port),
408                ntohl (mp->vrf_id),
409                mp->protocol);
410
411 }
412
413 static int api_nat44_static_mapping_dump(vat_main_t * vam)
414 {
415   vl_api_nat44_static_mapping_dump_t * mp;
416   vl_api_nat_control_ping_t *mp_ping;
417   int ret;
418
419   if (vam->json_output)
420     {
421       clib_warning ("JSON output not supported for nat44_static_mapping_dump");
422       return -99;
423     }
424
425   fformat (vam->ofp, "%21s%21s\n", "local", "external");
426   fformat (vam->ofp, "%15s%6s%15s%6s%11s%6s\n", "address", "port",
427            "address/if_idx", "port", "vrf", "proto");
428
429   M(NAT44_STATIC_MAPPING_DUMP, mp);
430   S(mp);
431
432   /* Use a control ping for synchronization */
433   M(NAT_CONTROL_PING, mp_ping);
434   S(mp_ping);
435
436   W (ret);
437   return ret;
438 }
439
440 static void vl_api_nat_show_config_reply_t_handler
441   (vl_api_nat_show_config_reply_t *mp)
442 {
443   snat_test_main_t * sm = &snat_test_main;
444   vat_main_t *vam = sm->vat_main;
445   i32 retval = ntohl (mp->retval);
446
447   if (retval >= 0)
448     {
449       fformat (vam->ofp, "translation hash buckets %d\n",
450                ntohl (mp->translation_buckets));
451       fformat (vam->ofp, "translation hash memory %d\n",
452                ntohl (mp->translation_memory_size));
453       fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
454       fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
455       fformat (vam->ofp, "max translations per user %d\n",
456                ntohl (mp->max_translations_per_user));
457       fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
458       fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
459       if (mp->static_mapping_only)
460         {
461           fformat (vam->ofp, "static mapping only");
462           if (mp->static_mapping_connection_tracking)
463             fformat (vam->ofp, " connection tracking");
464           fformat (vam->ofp, "\n");
465         }
466     }
467   vam->retval = retval;
468   vam->result_ready = 1;
469 }
470
471 static void vl_api_nat_show_config_2_reply_t_handler
472   (vl_api_nat_show_config_2_reply_t *mp)
473 {
474   snat_test_main_t * sm = &snat_test_main;
475   vat_main_t *vam = sm->vat_main;
476   i32 retval = ntohl (mp->retval);
477
478   if (retval >= 0)
479     {
480       fformat (vam->ofp, "translation hash buckets %d\n",
481                ntohl (mp->translation_buckets));
482       fformat (vam->ofp, "translation hash memory %d\n",
483                ntohl (mp->translation_memory_size));
484       fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
485       fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
486       fformat (vam->ofp, "max translations per user %d\n",
487                ntohl (mp->max_translations_per_user));
488       fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
489       fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
490       if (mp->static_mapping_only)
491         {
492           fformat (vam->ofp, "static mapping only");
493           if (mp->static_mapping_connection_tracking)
494             fformat (vam->ofp, " connection tracking");
495           fformat (vam->ofp, "\n");
496         }
497     }
498   vam->retval = retval;
499   vam->result_ready = 1;
500 }
501
502 static int api_nat_show_config(vat_main_t * vam)
503 {
504   vl_api_nat_show_config_t * mp;
505   int ret;
506
507   if (vam->json_output)
508     {
509       clib_warning ("JSON output not supported for nat_show_config");
510       return -99;
511     }
512
513   M(NAT_SHOW_CONFIG, mp);
514   S(mp);
515   W (ret);
516   return ret;
517 }
518
519 static int api_nat_show_config_2(vat_main_t * vam)
520 {
521   vl_api_nat_show_config_2_t * mp;
522   int ret;
523
524   if (vam->json_output)
525     {
526       clib_warning ("JSON output not supported for nat_show_config_2");
527       return -99;
528     }
529
530   M(NAT_SHOW_CONFIG_2, mp);
531   S(mp);
532   W (ret);
533   return ret;
534 }
535
536 static void vl_api_nat44_address_details_t_handler
537   (vl_api_nat44_address_details_t *mp)
538 {
539   snat_test_main_t * sm = &snat_test_main;
540   vat_main_t *vam = sm->vat_main;
541
542   fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
543 }
544
545 static int api_nat44_address_dump(vat_main_t * vam)
546 {
547   vl_api_nat44_address_dump_t * mp;
548   vl_api_nat_control_ping_t *mp_ping;
549   int ret;
550
551   if (vam->json_output)
552     {
553       clib_warning ("JSON output not supported for nat44_address_dump");
554       return -99;
555     }
556
557   M(NAT44_ADDRESS_DUMP, mp);
558   S(mp);
559
560   /* Use a control ping for synchronization */
561   M(NAT_CONTROL_PING, mp_ping);
562   S(mp_ping);
563
564   W (ret);
565   return ret;
566 }
567
568 static void vl_api_nat44_interface_details_t_handler
569   (vl_api_nat44_interface_details_t *mp)
570 {
571   nat_config_flags_t flags = (nat_config_flags_t) mp->flags;
572   snat_test_main_t * sm = &snat_test_main;
573   vat_main_t *vam = sm->vat_main;
574
575   if ((flags & NAT_API_IS_INSIDE) && (flags & NAT_API_IS_OUTSIDE))
576     {
577       fformat (vam->ofp, "sw_if_index %d in & out\n", ntohl (mp->sw_if_index));
578     }
579   else
580     {
581       fformat (vam->ofp, "sw_if_index %d %s\n", ntohl (mp->sw_if_index),
582                flags & NAT_API_IS_INSIDE ? "in" : "out");
583     }
584 }
585
586 static int api_nat44_interface_dump(vat_main_t * vam)
587 {
588   vl_api_nat44_interface_dump_t * mp;
589   vl_api_nat_control_ping_t *mp_ping;
590   int ret;
591
592   if (vam->json_output)
593     {
594       clib_warning ("JSON output not supported for nat44_address_dump");
595       return -99;
596     }
597
598   M(NAT44_INTERFACE_DUMP, mp);
599   S(mp);
600
601   /* Use a control ping for synchronization */
602   M(NAT_CONTROL_PING, mp_ping);
603   S(mp_ping);
604
605   W (ret);
606   return ret;
607 }
608
609 static int api_nat_set_workers (vat_main_t * vam)
610 {
611   unformat_input_t * i = vam->input;
612   vl_api_nat_set_workers_t * mp;
613   uword *bitmap;
614   int ret;
615
616   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
617     {
618       if (unformat (i, "%U", unformat_bitmap_list, &bitmap))
619         ;
620       else
621         {
622           clib_warning("unknown input '%U'", format_unformat_error, i);
623           return -99;
624         }
625     }
626
627   M(NAT_SET_WORKERS, mp);
628   mp->worker_mask = clib_host_to_net_u64 (bitmap[0]);
629
630   S(mp);
631   W (ret);
632   return ret;
633 }
634
635 static void vl_api_nat_worker_details_t_handler
636   (vl_api_nat_worker_details_t *mp)
637 {
638   snat_test_main_t * sm = &snat_test_main;
639   vat_main_t *vam = sm->vat_main;
640
641   fformat (vam->ofp, "worker_index %d (%s at lcore %u)\n",
642            ntohl (mp->worker_index), mp->name, ntohl (mp->lcore_id));
643 }
644
645 static int api_nat_worker_dump(vat_main_t * vam)
646 {
647   vl_api_nat_worker_dump_t * mp;
648   vl_api_nat_control_ping_t *mp_ping;
649   int ret;
650
651   if (vam->json_output)
652     {
653       clib_warning ("JSON output not supported for nat_address_dump");
654       return -99;
655     }
656
657   M(NAT_WORKER_DUMP, mp);
658   S(mp);
659
660   /* Use a control ping for synchronization */
661   M(NAT_CONTROL_PING, mp_ping);
662   S(mp_ping);
663
664   W (ret);
665   return ret;
666 }
667
668 static int api_nat44_add_del_interface_addr (vat_main_t * vam)
669 {
670   unformat_input_t * i = vam->input;
671   vl_api_nat44_add_del_interface_addr_t * mp;
672   u32 sw_if_index;
673   u8 sw_if_index_set = 0;
674   u8 is_add = 1;
675   u8 twice_nat = 0;
676   int ret;
677
678   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
679     {
680       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
681         sw_if_index_set = 1;
682       else if (unformat (i, "sw_if_index %d", &sw_if_index))
683         sw_if_index_set = 1;
684       else if (unformat (i, "twice-nat"))
685         twice_nat = 1;
686       else if (unformat (i, "del"))
687         is_add = 0;
688       else
689         {
690           clib_warning("unknown input '%U'", format_unformat_error, i);
691           return -99;
692         }
693     }
694
695   if (sw_if_index_set == 0)
696     {
697       errmsg ("interface / sw_if_index required\n");
698       return -99;
699     }
700
701   M(NAT44_ADD_DEL_INTERFACE_ADDR, mp);
702   mp->sw_if_index = ntohl(sw_if_index);
703   if (twice_nat)
704     mp->flags = (vl_api_nat_config_flags_t)NAT_API_IS_TWICE_NAT;
705   mp->is_add = is_add;
706
707   S(mp);
708   W (ret);
709   return ret;
710 }
711
712 static void vl_api_nat44_interface_addr_details_t_handler
713   (vl_api_nat44_interface_addr_details_t *mp)
714 {
715   snat_test_main_t * sm = &snat_test_main;
716   vat_main_t *vam = sm->vat_main;
717
718   fformat (vam->ofp, "sw_if_index %d\n", ntohl (mp->sw_if_index));
719 }
720
721 static int api_nat44_interface_addr_dump(vat_main_t * vam)
722 {
723   vl_api_nat44_interface_addr_dump_t * mp;
724   vl_api_nat_control_ping_t *mp_ping;
725   int ret;
726
727   if (vam->json_output)
728     {
729       clib_warning ("JSON output not supported for nat44_address_dump");
730       return -99;
731     }
732
733   M(NAT44_INTERFACE_ADDR_DUMP, mp);
734   S(mp);
735
736   /* Use a control ping for synchronization */
737   M(NAT_CONTROL_PING, mp_ping);
738   S(mp_ping);
739
740   W (ret);
741   return ret;
742 }
743
744 static int api_nat_ipfix_enable_disable (vat_main_t * vam)
745 {
746   unformat_input_t * i = vam->input;
747   vl_api_nat_ipfix_enable_disable_t * mp;
748   u32 domain_id = 0;
749   u32 src_port = 0;
750   u8 enable = 1;
751   int ret;
752
753   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
754     {
755       if (unformat (i, "domain %d", &domain_id))
756         ;
757       else if (unformat (i, "src_port %d", &src_port))
758         ;
759       else if (unformat (i, "disable"))
760         enable = 0;
761       else
762         {
763           clib_warning("unknown input '%U'", format_unformat_error, i);
764           return -99;
765         }
766     }
767
768   M(NAT_IPFIX_ENABLE_DISABLE, mp);
769   mp->domain_id = htonl(domain_id);
770   mp->src_port = htons((u16) src_port);
771   mp->enable = enable;
772
773   S(mp);
774   W (ret);
775   return ret;
776 }
777
778 static void vl_api_nat44_user_session_details_t_handler
779   (vl_api_nat44_user_session_details_t *mp)
780 {
781   snat_test_main_t * sm = &snat_test_main;
782   vat_main_t *vam = sm->vat_main;
783
784   fformat(vam->ofp, "%s session %U:%d to %U:%d protocol id %d "
785                     "total packets %d total bytes %lld\n",
786           mp->flags & NAT_API_IS_STATIC ? "static" : "dynamic",
787           format_ip4_address, mp->inside_ip_address, ntohs(mp->inside_port),
788           format_ip4_address, mp->outside_ip_address, ntohs(mp->outside_port),
789           ntohs(mp->protocol), ntohl(mp->total_pkts),
790           clib_net_to_host_u64(mp->total_bytes));
791 }
792
793 static int api_nat44_user_session_dump(vat_main_t * vam)
794 {
795   unformat_input_t* i = vam->input;
796   vl_api_nat44_user_session_dump_t * mp;
797   vl_api_nat_control_ping_t *mp_ping;
798   ip4_address_t addr;
799   u32 vrf_id = ~0;
800   int ret;
801
802   if (vam->json_output)
803     {
804       clib_warning ("JSON output not supported for nat44_address_dump");
805       return -99;
806     }
807
808   if (unformat (i, "ip_address %U vrf_id %d",
809                 unformat_ip4_address, &addr, &vrf_id))
810     ;
811   else
812     {
813       clib_warning("unknown input '%U'", format_unformat_error, i);
814       return -99;
815     }
816
817   M(NAT44_USER_SESSION_DUMP, mp);
818   S(mp);
819
820   /* Use a control ping for synchronization */
821   M(NAT_CONTROL_PING, mp_ping);
822   clib_memset(mp->ip_address, 0, 16);
823   clib_memcpy(mp->ip_address, &addr, 4);
824   mp->vrf_id = htonl(vrf_id);
825   S(mp_ping);
826
827   W (ret);
828   return ret;
829 }
830
831 static void vl_api_nat44_user_details_t_handler
832   (vl_api_nat44_user_details_t *mp)
833 {
834   snat_test_main_t * sm = &snat_test_main;
835   vat_main_t *vam = sm->vat_main;
836
837   fformat(vam->ofp, "user with ip %U with vrf_id %d "
838                     "with %d sessions and %d static sessions\n",
839           format_ip4_address, mp->ip_address, ntohl(mp->vrf_id),
840           ntohl(mp->nsessions), ntohl(mp->nstaticsessions));
841 }
842
843 static int api_nat44_user_dump(vat_main_t * vam)
844 {
845   vl_api_nat44_user_dump_t * mp;
846   vl_api_nat_control_ping_t *mp_ping;
847   int ret;
848
849   if (vam->json_output)
850     {
851       clib_warning ("JSON output not supported for nat44_address_dump");
852       return -99;
853     }
854
855   M(NAT44_USER_DUMP, mp);
856   S(mp);
857
858   /* Use a control ping for synchronization */
859   M(NAT_CONTROL_PING, mp_ping);
860   S(mp_ping);
861
862   W (ret);
863   return ret;
864 }
865
866 static int api_nat_set_timeouts (vat_main_t * vam)
867 {
868   unformat_input_t * i = vam->input;
869   vl_api_nat_set_timeouts_t * mp;
870   u32 udp = SNAT_UDP_TIMEOUT;
871   u32 tcp_established = SNAT_TCP_ESTABLISHED_TIMEOUT;
872   u32 tcp_transitory = SNAT_TCP_TRANSITORY_TIMEOUT;
873   u32 icmp = SNAT_ICMP_TIMEOUT;
874   int ret;
875
876   if (unformat (i, "udp %d", &udp))
877     ;
878   else if (unformat (i, "tcp_established %d", &tcp_established))
879     ;
880   else if (unformat (i, "tcp_transitory %d", &tcp_transitory))
881     ;
882   else if (unformat (i, "icmp %d", &icmp))
883     ;
884   else
885     {
886       clib_warning("unknown input '%U'", format_unformat_error, i);
887       return -99;
888     }
889
890   M(NAT_SET_TIMEOUTS, mp);
891   mp->udp = htonl(udp);
892   mp->tcp_established = htonl(tcp_established);
893   mp->tcp_transitory = htonl(tcp_transitory);
894   mp->icmp = htonl(icmp);
895
896   S(mp);
897   W (ret);
898   return ret;
899 }
900
901 static void vl_api_nat_get_timeouts_reply_t_handler
902   (vl_api_nat_get_timeouts_reply_t *mp)
903 {
904   snat_test_main_t * sm = &snat_test_main;
905   vat_main_t *vam = sm->vat_main;
906   i32 retval = ntohl (mp->retval);
907
908   if (retval >= 0)
909     {
910       fformat (vam->ofp, "udp timeout: %dsec\n", ntohl (mp->udp));
911       fformat (vam->ofp, "tcp-established timeout: %dsec",
912                ntohl (mp->tcp_established));
913       fformat (vam->ofp, "tcp-transitory timeout: %dsec",
914                ntohl (mp->tcp_transitory));
915       fformat (vam->ofp, "icmp timeout: %dsec", ntohl (mp->icmp));
916     }
917   vam->retval = retval;
918   vam->result_ready = 1;
919 }
920
921 static int api_nat_get_timeouts(vat_main_t * vam)
922 {
923   vl_api_nat_get_timeouts_t * mp;
924   int ret;
925
926   if (vam->json_output)
927     {
928       clib_warning ("JSON output not supported for nat_get_timeouts");
929       return -99;
930     }
931
932   M(NAT_GET_TIMEOUTS, mp);
933   S(mp);
934   W (ret);
935   return ret;
936 }
937
938 /*
939  * List of messages that the api test plugin sends,
940  * and that the data plane plugin processes
941  */
942 #define foreach_vpe_api_msg                                       \
943 _(nat44_add_del_address_range,                                    \
944   "<start-addr> [- <end-addr>] [vrf <table-id>] [twice-nat] [del]") \
945 _(nat44_interface_add_del_feature,                                \
946   "<intfc> | sw_if_index <id> [in] [out] [del]")                  \
947 _(nat44_interface_add_del_output_feature,                         \
948   "<intfc> | sw_if_index <id> [in] [out] [del]")                  \
949 _(nat44_add_del_static_mapping, "local_addr <ip>"                 \
950   " (external_addr <ip> | external_if <intfc> |"                  \
951   " external_sw_if_ndex <id>) [local_port <n>]"                   \
952   " [external_port <n>] [vrf <table-id>] [del] protocol <n>")     \
953 _(nat_set_workers, "<wokrers_bitmap>")                            \
954 _(nat44_static_mapping_dump, "")                                  \
955 _(nat_show_config, "")                                            \
956 _(nat_show_config_2, "")                                          \
957 _(nat44_address_dump, "")                                         \
958 _(nat44_interface_dump, "")                                       \
959 _(nat_worker_dump, "")                                            \
960 _(nat44_add_del_interface_addr,                                   \
961   "<intfc> | sw_if_index <id> [twice-nat] [del]")                 \
962 _(nat44_interface_addr_dump, "")                                  \
963 _(nat_ipfix_enable_disable, "[domain <id>] [src_port <n>] "       \
964   "[disable]")                                                    \
965 _(nat44_user_dump, "")                                            \
966 _(nat44_user_session_dump, "ip_address <ip> vrf_id <table-id>")   \
967 _(nat_set_timeouts, "[udp <sec> | tcp_established <sec> | "       \
968   "tcp_transitory <sec> | icmp <sec>]")                           \
969 _(nat_get_timeouts, "")
970
971 static void
972 snat_vat_api_hookup (vat_main_t *vam)
973 {
974   snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
975   /* Hook up handlers for replies from the data plane plug-in */
976 #define _(N,n)                                                  \
977   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),       \
978                           #n,                                   \
979                           vl_api_##n##_t_handler,               \
980                           vl_noop_handler,                      \
981                           vl_api_##n##_t_endian,                \
982                           vl_api_##n##_t_print,                 \
983                           sizeof(vl_api_##n##_t), 1);
984   foreach_vpe_api_reply_msg;
985 #undef _
986
987   /* API messages we can send */
988 #define _(n,h)                                          \
989   hash_set_mem (vam->function_by_name, #n, api_##n);
990   foreach_vpe_api_msg;
991 #undef _
992
993   /* Help strings */
994 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
995   foreach_vpe_api_msg;
996 #undef _
997 }
998
999 clib_error_t * vat_plugin_register (vat_main_t *vam)
1000 {
1001   snat_test_main_t * sm = &snat_test_main;
1002   u8 * name;
1003
1004   sm->vat_main = vam;
1005
1006   /* Ask the vpp engine for the first assigned message-id */
1007   name = format (0, "nat_%08x%c", api_version, 0);
1008   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1009   vec_free(name);
1010
1011   if (sm->msg_id_base != (u16) ~0)
1012     snat_vat_api_hookup (vam);
1013   else
1014     return clib_error_return (0, "nat plugin not loaded...");
1015
1016   return 0;
1017 }