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