nat: use correct data types for memory sizes
[vpp.git] / src / plugins / nat / nat_test.c
1
2 /*
3  * nat.c - skeleton vpp-api-test plug-in
4  *
5  * Copyright (c) <current-year> <your-organization>
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 #include <vat/vat.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21
22 #include <vppinfra/error.h>
23 #include <vnet/ip/ip.h>
24 #include <nat/nat.h>
25
26 #define __plugin_msg_base snat_test_main.msg_id_base
27 #include <vlibapi/vat_helper_macros.h>
28
29 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
30
31 /* Declare message IDs */
32 #include <nat/nat_msg_enum.h>
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <nat/nat_all_api_h.h>
37 #undef vl_typedefs
38
39 /* declare message handlers for each api */
40
41 #define vl_endianfun             /* define message structures */
42 #include <nat/nat_all_api_h.h>
43 #undef vl_endianfun
44
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...)
47 #define vl_printfun
48 #include <nat/nat_all_api_h.h>
49 #undef vl_printfun
50
51 /* Get the API version number. */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <nat/nat_all_api_h.h>
54 #undef vl_api_version
55
56 typedef struct {
57     /* API message ID base */
58     u16 msg_id_base;
59     vat_main_t *vat_main;
60 } snat_test_main_t;
61
62 snat_test_main_t snat_test_main;
63
64 #define foreach_standard_reply_retval_handler    \
65 _(nat44_add_del_address_range_reply)             \
66 _(nat44_interface_add_del_feature_reply)         \
67 _(nat44_interface_add_del_output_feature_reply)  \
68 _(nat44_add_del_static_mapping_reply)            \
69 _(nat_set_workers_reply)                         \
70 _(nat44_add_del_interface_addr_reply)            \
71 _(nat_ipfix_enable_disable_reply)                \
72 _(nat_det_add_del_map_reply)                     \
73 _(nat_set_timeouts_reply)                        \
74 _(nat_det_close_session_out_reply)               \
75 _(nat_det_close_session_in_reply)
76
77 #define _(n)                                            \
78     static void vl_api_##n##_t_handler                  \
79     (vl_api_##n##_t * mp)                               \
80     {                                                   \
81         vat_main_t * vam = snat_test_main.vat_main;   \
82         i32 retval = ntohl(mp->retval);                 \
83         if (vam->async_mode) {                          \
84             vam->async_errors += (retval < 0);          \
85         } else {                                        \
86             vam->retval = retval;                       \
87             vam->result_ready = 1;                      \
88         }                                               \
89     }
90 foreach_standard_reply_retval_handler;
91 #undef _
92
93 /*
94  * Table of message reply handlers, must include boilerplate handlers
95  * we just generated
96  */
97 #define foreach_vpe_api_reply_msg                               \
98 _(NAT44_ADD_DEL_ADDRESS_RANGE_REPLY,                            \
99   nat44_add_del_address_range_reply)                            \
100 _(NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY,                        \
101   nat44_interface_add_del_feature_reply)                        \
102 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY,                 \
103   nat44_interface_add_del_output_feature_reply)                 \
104 _(NAT44_ADD_DEL_STATIC_MAPPING_REPLY,                           \
105   nat44_add_del_static_mapping_reply)                           \
106 _(NAT_CONTROL_PING_REPLY, nat_control_ping_reply)               \
107 _(NAT44_STATIC_MAPPING_DETAILS, nat44_static_mapping_details)   \
108 _(NAT_SHOW_CONFIG_REPLY, nat_show_config_reply)                 \
109 _(NAT44_ADDRESS_DETAILS, nat44_address_details)                 \
110 _(NAT44_INTERFACE_DETAILS, nat44_interface_details)             \
111 _(NAT_SET_WORKERS_REPLY, nat_set_workers_reply)                 \
112 _(NAT_WORKER_DETAILS, nat_worker_details)                       \
113 _(NAT44_ADD_DEL_INTERFACE_ADDR_REPLY,                           \
114   nat44_add_del_interface_addr_reply)                           \
115 _(NAT44_INTERFACE_ADDR_DETAILS, nat44_interface_addr_details)   \
116 _(NAT_IPFIX_ENABLE_DISABLE_REPLY,                               \
117   nat_ipfix_enable_disable_reply)                               \
118 _(NAT44_USER_DETAILS, nat44_user_details)                       \
119 _(NAT44_USER_SESSION_DETAILS, nat44_user_session_details)       \
120 _(NAT_DET_ADD_DEL_MAP_REPLY, nat_det_add_del_map_reply)         \
121 _(NAT_DET_FORWARD_REPLY, nat_det_forward_reply)                 \
122 _(NAT_DET_REVERSE_REPLY, nat_det_reverse_reply)                 \
123 _(NAT_DET_MAP_DETAILS, nat_det_map_details)                     \
124 _(NAT_SET_TIMEOUTS_REPLY, nat_set_timeouts_reply)               \
125 _(NAT_GET_TIMEOUTS_REPLY, nat_get_timeouts_reply)               \
126 _(NAT_DET_CLOSE_SESSION_OUT_REPLY,                              \
127   nat_det_close_session_out_reply)                              \
128 _(NAT_DET_CLOSE_SESSION_IN_REPLY,                               \
129   nat_det_close_session_in_reply)                               \
130 _(NAT_DET_SESSION_DETAILS, nat_det_session_details)
131
132 static int api_nat44_add_del_address_range (vat_main_t * vam)
133 {
134   unformat_input_t * i = vam->input;
135   ip4_address_t start_addr, end_addr;
136   u32 start_host_order, end_host_order;
137   vl_api_nat44_add_del_address_range_t * mp;
138   u8 is_add = 1;
139   u8 twice_nat = 0;
140   int vrf_id = ~0;
141   int count;
142   int ret;
143
144   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
145     {
146       if (unformat (i, "%U - %U",
147                     unformat_ip4_address, &start_addr,
148                     unformat_ip4_address, &end_addr))
149         ;
150       else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
151         end_addr = start_addr;
152       else if (unformat (i, "twice-nat"))
153         twice_nat = 1;
154       else if (unformat (i, "vrf %u", &vrf_id))
155         ;
156       else if (unformat (i, "del"))
157         is_add = 0;
158       else
159         {
160           clib_warning("unknown input '%U'", format_unformat_error, i);
161           return -99;
162         }
163     }
164
165   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
166   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
167
168   if (end_host_order < start_host_order)
169     {
170       errmsg ("end address less than start address\n");
171       return -99;
172     }
173
174   count = (end_host_order - start_host_order) + 1;
175
176   if (count > 1024)
177     {
178     errmsg ("%U - %U, %d addresses...\n",
179            format_ip4_address, &start_addr,
180            format_ip4_address, &end_addr,
181            count);
182     }
183
184   M(NAT44_ADD_DEL_ADDRESS_RANGE, mp);
185
186   memcpy (mp->first_ip_address, &start_addr, 4);
187   memcpy (mp->last_ip_address, &end_addr, 4);
188   mp->vrf_id = vrf_id;
189   if (twice_nat)
190     mp->flags = (vl_api_nat_config_flags_t)NAT_API_IS_TWICE_NAT;
191   mp->is_add = is_add;
192
193   S(mp);
194   W (ret);
195   return ret;
196 }
197
198 static int api_nat44_interface_add_del_feature (vat_main_t * vam)
199 {
200   unformat_input_t * i = vam->input;
201   vl_api_nat44_interface_add_del_feature_t * mp;
202   u32 sw_if_index;
203   u8 sw_if_index_set = 0;
204   u8 is_inside = 1;
205   u8 is_add = 1;
206   int ret;
207
208   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
209     {
210       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
211         sw_if_index_set = 1;
212       else if (unformat (i, "sw_if_index %d", &sw_if_index))
213         sw_if_index_set = 1;
214       else if (unformat (i, "out"))
215         is_inside = 0;
216       else if (unformat (i, "in"))
217         is_inside = 1;
218       else if (unformat (i, "del"))
219         is_add = 0;
220       else
221         {
222           clib_warning("unknown input '%U'", format_unformat_error, i);
223           return -99;
224         }
225     }
226
227   if (sw_if_index_set == 0)
228     {
229       errmsg ("interface / sw_if_index required\n");
230       return -99;
231     }
232
233   M(NAT44_INTERFACE_ADD_DEL_FEATURE, mp);
234   mp->sw_if_index = ntohl(sw_if_index);
235   mp->is_add = is_add;
236   if (is_inside)
237     mp->flags |= NAT_API_IS_INSIDE;
238
239   S(mp);
240   W (ret);
241   return ret;
242 }
243
244 static int api_nat44_interface_add_del_output_feature (vat_main_t * vam)
245 {
246   unformat_input_t * i = vam->input;
247   vl_api_nat44_interface_add_del_output_feature_t * mp;
248   u32 sw_if_index;
249   u8 sw_if_index_set = 0;
250   u8 is_inside = 1;
251   u8 is_add = 1;
252   int ret;
253
254   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
255     {
256       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
257         sw_if_index_set = 1;
258       else if (unformat (i, "sw_if_index %d", &sw_if_index))
259         sw_if_index_set = 1;
260       else if (unformat (i, "out"))
261         is_inside = 0;
262       else if (unformat (i, "in"))
263         is_inside = 1;
264       else if (unformat (i, "del"))
265         is_add = 0;
266       else
267         {
268           clib_warning("unknown input '%U'", format_unformat_error, i);
269           return -99;
270         }
271     }
272
273   if (sw_if_index_set == 0)
274     {
275       errmsg ("interface / sw_if_index required\n");
276       return -99;
277     }
278
279   M(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, mp);
280   mp->sw_if_index = ntohl(sw_if_index);
281   mp->is_add = is_add;
282   if (is_inside)
283     mp->flags |= NAT_API_IS_INSIDE;
284
285   S(mp);
286   W (ret);
287   return ret;
288 }
289
290 static int api_nat44_add_del_static_mapping(vat_main_t * vam)
291 {
292   unformat_input_t * i = vam->input;
293   vl_api_nat44_add_del_static_mapping_t * mp;
294   u8 external_addr_set = 0;
295   u8 local_addr_set = 0;
296   u8 is_add = 1;
297   u8 addr_only = 1;
298   ip4_address_t local_addr, external_addr;
299   u32 local_port = 0, external_port = 0, vrf_id = ~0;
300   u32 sw_if_index = ~0;
301   u8 sw_if_index_set = 0;
302   u32 proto = ~0;
303   u8 proto_set = 0;
304   int ret;
305
306   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
307     {
308       if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
309         local_addr_set = 1;
310       else if (unformat (i, "external_addr %U", unformat_ip4_address,
311                          &external_addr))
312         external_addr_set = 1;
313       else if (unformat (i, "local_port %u", &local_port))
314         addr_only = 0;
315       else if (unformat (i, "external_port %u", &external_port))
316         addr_only = 0;
317       else if (unformat (i, "external_if %U", unformat_sw_if_index, vam,
318                          &sw_if_index))
319         sw_if_index_set = 1;
320       else if (unformat (i, "external_sw_if_index %d", &sw_if_index))
321         sw_if_index_set = 1;
322       else if (unformat (i, "vrf %u", &vrf_id))
323         ;
324       else if (unformat (i, "protocol %u", &proto))
325         proto_set = 1;
326       else if (unformat (i, "del"))
327         is_add = 0;
328       else
329         {
330           clib_warning("unknown input '%U'", format_unformat_error, i);
331           return -99;
332         }
333     }
334
335   if (!addr_only && !proto_set)
336     {
337       errmsg ("protocol required\n");
338       return -99;
339     }
340
341   if (!local_addr_set)
342     {
343       errmsg ("local addr required\n");
344       return -99;
345     }
346   if (!external_addr_set && !sw_if_index_set)
347     {
348       errmsg ("external addr or interface required\n");
349       return -99;
350     }
351
352   M(NAT44_ADD_DEL_STATIC_MAPPING, mp);
353
354   mp->is_add = is_add;
355   if (addr_only)
356     mp->flags |= NAT_API_IS_ADDR_ONLY;
357
358   mp->local_port = ntohs ((u16) local_port);
359   mp->external_port = ntohs ((u16) external_port);
360   mp->external_sw_if_index = ntohl (sw_if_index);
361   mp->vrf_id = ntohl (vrf_id);
362   mp->protocol = (u8) proto;
363   memcpy (mp->local_ip_address, &local_addr, 4);
364   memcpy (mp->external_ip_address, &external_addr, 4);
365
366   S(mp);
367   W (ret);
368   return ret;
369 }
370
371 static void vl_api_nat_control_ping_reply_t_handler
372   (vl_api_nat_control_ping_reply_t * mp)
373 {
374   vat_main_t *vam = &vat_main;
375   i32 retval = ntohl (mp->retval);
376   if (vam->async_mode)
377     {
378       vam->async_errors += (retval < 0);
379     }
380   else
381     {
382       vam->retval = retval;
383       vam->result_ready = 1;
384     }
385 }
386
387 static void vl_api_nat44_static_mapping_details_t_handler
388   (vl_api_nat44_static_mapping_details_t *mp)
389 {
390   snat_test_main_t * sm = &snat_test_main;
391   vat_main_t *vam = sm->vat_main;
392   nat_config_flags_t flags = (nat_config_flags_t) mp->flags;
393
394   if (flags & NAT_API_IS_ADDR_ONLY && mp->external_sw_if_index != ~0)
395       fformat (vam->ofp, "%15U%6s%15d%6s%11d%6d\n",
396                format_ip4_address, &mp->local_ip_address, "",
397                ntohl (mp->external_sw_if_index), "",
398                ntohl (mp->vrf_id),
399                mp->protocol);
400   else if (flags & NAT_API_IS_ADDR_ONLY && mp->external_sw_if_index == ~0)
401       fformat (vam->ofp, "%15U%6s%15U%6s%11d%6d\n",
402                format_ip4_address, &mp->local_ip_address, "",
403                format_ip4_address, &mp->external_ip_address, "",
404                ntohl (mp->vrf_id),
405                mp->protocol);
406   else if (!(flags & NAT_API_IS_ADDR_ONLY) && mp->external_sw_if_index != ~0)
407       fformat (vam->ofp, "%15U%6d%15d%6d%11d%6d\n",
408                format_ip4_address, &mp->local_ip_address,
409                ntohs (mp->local_port),
410                ntohl (mp->external_sw_if_index),
411                ntohs (mp->external_port),
412                ntohl (mp->vrf_id),
413                mp->protocol);
414   else
415       fformat (vam->ofp, "%15U%6d%15U%6d%11d%6d\n",
416                format_ip4_address, &mp->local_ip_address,
417                ntohs (mp->local_port),
418                format_ip4_address, &mp->external_ip_address,
419                ntohs (mp->external_port),
420                ntohl (mp->vrf_id),
421                mp->protocol);
422
423 }
424
425 static int api_nat44_static_mapping_dump(vat_main_t * vam)
426 {
427   vl_api_nat44_static_mapping_dump_t * mp;
428   vl_api_nat_control_ping_t *mp_ping;
429   int ret;
430
431   if (vam->json_output)
432     {
433       clib_warning ("JSON output not supported for nat44_static_mapping_dump");
434       return -99;
435     }
436
437   fformat (vam->ofp, "%21s%21s\n", "local", "external");
438   fformat (vam->ofp, "%15s%6s%15s%6s%11s%6s\n", "address", "port",
439            "address/if_idx", "port", "vrf", "proto");
440
441   M(NAT44_STATIC_MAPPING_DUMP, mp);
442   S(mp);
443
444   /* Use a control ping for synchronization */
445   M(NAT_CONTROL_PING, mp_ping);
446   S(mp_ping);
447
448   W (ret);
449   return ret;
450 }
451
452 static void vl_api_nat_show_config_reply_t_handler
453   (vl_api_nat_show_config_reply_t *mp)
454 {
455   snat_test_main_t * sm = &snat_test_main;
456   vat_main_t *vam = sm->vat_main;
457   i32 retval = ntohl (mp->retval);
458
459   if (retval >= 0)
460     {
461       fformat (vam->ofp, "translation hash buckets %d\n",
462                ntohl (mp->translation_buckets));
463       fformat (vam->ofp, "translation hash memory %d\n",
464                ntohl (mp->translation_memory_size));
465       fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
466       fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
467       fformat (vam->ofp, "max translations per user %d\n",
468                ntohl (mp->max_translations_per_user));
469       fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
470       fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
471       if (mp->static_mapping_only)
472         {
473           fformat (vam->ofp, "static mapping only");
474           if (mp->static_mapping_connection_tracking)
475             fformat (vam->ofp, " connection tracking");
476           fformat (vam->ofp, "\n");
477         }
478     }
479   vam->retval = retval;
480   vam->result_ready = 1;
481 }
482
483 static int api_nat_show_config(vat_main_t * vam)
484 {
485   vl_api_nat_show_config_t * mp;
486   int ret;
487
488   if (vam->json_output)
489     {
490       clib_warning ("JSON output not supported for nat_show_config");
491       return -99;
492     }
493
494   M(NAT_SHOW_CONFIG, mp);
495   S(mp);
496   W (ret);
497   return ret;
498 }
499
500 static void vl_api_nat44_address_details_t_handler
501   (vl_api_nat44_address_details_t *mp)
502 {
503   snat_test_main_t * sm = &snat_test_main;
504   vat_main_t *vam = sm->vat_main;
505
506   fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
507 }
508
509 static int api_nat44_address_dump(vat_main_t * vam)
510 {
511   vl_api_nat44_address_dump_t * mp;
512   vl_api_nat_control_ping_t *mp_ping;
513   int ret;
514
515   if (vam->json_output)
516     {
517       clib_warning ("JSON output not supported for nat44_address_dump");
518       return -99;
519     }
520
521   M(NAT44_ADDRESS_DUMP, mp);
522   S(mp);
523
524   /* Use a control ping for synchronization */
525   M(NAT_CONTROL_PING, mp_ping);
526   S(mp_ping);
527
528   W (ret);
529   return ret;
530 }
531
532 static void vl_api_nat44_interface_details_t_handler
533   (vl_api_nat44_interface_details_t *mp)
534 {
535   nat_config_flags_t flags = (nat_config_flags_t) mp->flags;
536   snat_test_main_t * sm = &snat_test_main;
537   vat_main_t *vam = sm->vat_main;
538
539   if ((flags & NAT_API_IS_INSIDE) && (flags & NAT_API_IS_OUTSIDE))
540     {
541       fformat (vam->ofp, "sw_if_index %d in & out\n", ntohl (mp->sw_if_index));
542     }
543   else
544     {
545       fformat (vam->ofp, "sw_if_index %d %s\n", ntohl (mp->sw_if_index),
546                flags & NAT_API_IS_INSIDE ? "in" : "out");
547     }
548 }
549
550 static int api_nat44_interface_dump(vat_main_t * vam)
551 {
552   vl_api_nat44_interface_dump_t * mp;
553   vl_api_nat_control_ping_t *mp_ping;
554   int ret;
555
556   if (vam->json_output)
557     {
558       clib_warning ("JSON output not supported for nat44_address_dump");
559       return -99;
560     }
561
562   M(NAT44_INTERFACE_DUMP, mp);
563   S(mp);
564
565   /* Use a control ping for synchronization */
566   M(NAT_CONTROL_PING, mp_ping);
567   S(mp_ping);
568
569   W (ret);
570   return ret;
571 }
572
573 static int api_nat_set_workers (vat_main_t * vam)
574 {
575   unformat_input_t * i = vam->input;
576   vl_api_nat_set_workers_t * mp;
577   uword *bitmap;
578   int ret;
579
580   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
581     {
582       if (unformat (i, "%U", unformat_bitmap_list, &bitmap))
583         ;
584       else
585         {
586           clib_warning("unknown input '%U'", format_unformat_error, i);
587           return -99;
588         }
589     }
590
591   M(NAT_SET_WORKERS, mp);
592   mp->worker_mask = clib_host_to_net_u64 (bitmap[0]);
593
594   S(mp);
595   W (ret);
596   return ret;
597 }
598
599 static void vl_api_nat_worker_details_t_handler
600   (vl_api_nat_worker_details_t *mp)
601 {
602   snat_test_main_t * sm = &snat_test_main;
603   vat_main_t *vam = sm->vat_main;
604
605   fformat (vam->ofp, "worker_index %d (%s at lcore %u)\n",
606            ntohl (mp->worker_index), mp->name, ntohl (mp->lcore_id));
607 }
608
609 static int api_nat_worker_dump(vat_main_t * vam)
610 {
611   vl_api_nat_worker_dump_t * mp;
612   vl_api_nat_control_ping_t *mp_ping;
613   int ret;
614
615   if (vam->json_output)
616     {
617       clib_warning ("JSON output not supported for nat_address_dump");
618       return -99;
619     }
620
621   M(NAT_WORKER_DUMP, mp);
622   S(mp);
623
624   /* Use a control ping for synchronization */
625   M(NAT_CONTROL_PING, mp_ping);
626   S(mp_ping);
627
628   W (ret);
629   return ret;
630 }
631
632 static int api_nat44_add_del_interface_addr (vat_main_t * vam)
633 {
634   unformat_input_t * i = vam->input;
635   vl_api_nat44_add_del_interface_addr_t * mp;
636   u32 sw_if_index;
637   u8 sw_if_index_set = 0;
638   u8 is_add = 1;
639   u8 twice_nat = 0;
640   int ret;
641
642   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
643     {
644       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
645         sw_if_index_set = 1;
646       else if (unformat (i, "sw_if_index %d", &sw_if_index))
647         sw_if_index_set = 1;
648       else if (unformat (i, "twice-nat"))
649         twice_nat = 1;
650       else if (unformat (i, "del"))
651         is_add = 0;
652       else
653         {
654           clib_warning("unknown input '%U'", format_unformat_error, i);
655           return -99;
656         }
657     }
658
659   if (sw_if_index_set == 0)
660     {
661       errmsg ("interface / sw_if_index required\n");
662       return -99;
663     }
664
665   M(NAT44_ADD_DEL_INTERFACE_ADDR, mp);
666   mp->sw_if_index = ntohl(sw_if_index);
667   if (twice_nat)
668     mp->flags = (vl_api_nat_config_flags_t)NAT_API_IS_TWICE_NAT;
669   mp->is_add = is_add;
670
671   S(mp);
672   W (ret);
673   return ret;
674 }
675
676 static void vl_api_nat44_interface_addr_details_t_handler
677   (vl_api_nat44_interface_addr_details_t *mp)
678 {
679   snat_test_main_t * sm = &snat_test_main;
680   vat_main_t *vam = sm->vat_main;
681
682   fformat (vam->ofp, "sw_if_index %d\n", ntohl (mp->sw_if_index));
683 }
684
685 static int api_nat44_interface_addr_dump(vat_main_t * vam)
686 {
687   vl_api_nat44_interface_addr_dump_t * mp;
688   vl_api_nat_control_ping_t *mp_ping;
689   int ret;
690
691   if (vam->json_output)
692     {
693       clib_warning ("JSON output not supported for nat44_address_dump");
694       return -99;
695     }
696
697   M(NAT44_INTERFACE_ADDR_DUMP, mp);
698   S(mp);
699
700   /* Use a control ping for synchronization */
701   M(NAT_CONTROL_PING, mp_ping);
702   S(mp_ping);
703
704   W (ret);
705   return ret;
706 }
707
708 static int api_nat_ipfix_enable_disable (vat_main_t * vam)
709 {
710   unformat_input_t * i = vam->input;
711   vl_api_nat_ipfix_enable_disable_t * mp;
712   u32 domain_id = 0;
713   u32 src_port = 0;
714   u8 enable = 1;
715   int ret;
716
717   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
718     {
719       if (unformat (i, "domain %d", &domain_id))
720         ;
721       else if (unformat (i, "src_port %d", &src_port))
722         ;
723       else if (unformat (i, "disable"))
724         enable = 0;
725       else
726         {
727           clib_warning("unknown input '%U'", format_unformat_error, i);
728           return -99;
729         }
730     }
731
732   M(NAT_IPFIX_ENABLE_DISABLE, mp);
733   mp->domain_id = htonl(domain_id);
734   mp->src_port = htons((u16) src_port);
735   mp->enable = enable;
736
737   S(mp);
738   W (ret);
739   return ret;
740 }
741
742 static void vl_api_nat44_user_session_details_t_handler
743   (vl_api_nat44_user_session_details_t *mp)
744 {
745   snat_test_main_t * sm = &snat_test_main;
746   vat_main_t *vam = sm->vat_main;
747
748   fformat(vam->ofp, "%s session %U:%d to %U:%d protocol id %d "
749                     "total packets %d total bytes %lld\n",
750           mp->flags & NAT_API_IS_STATIC ? "static" : "dynamic",
751           format_ip4_address, mp->inside_ip_address, ntohs(mp->inside_port),
752           format_ip4_address, mp->outside_ip_address, ntohs(mp->outside_port),
753           ntohs(mp->protocol), ntohl(mp->total_pkts),
754           clib_net_to_host_u64(mp->total_bytes));
755 }
756
757 static int api_nat44_user_session_dump(vat_main_t * vam)
758 {
759   unformat_input_t* i = vam->input;
760   vl_api_nat44_user_session_dump_t * mp;
761   vl_api_nat_control_ping_t *mp_ping;
762   ip4_address_t addr;
763   u32 vrf_id = ~0;
764   int ret;
765
766   if (vam->json_output)
767     {
768       clib_warning ("JSON output not supported for nat44_address_dump");
769       return -99;
770     }
771
772   if (unformat (i, "ip_address %U vrf_id %d",
773                 unformat_ip4_address, &addr, &vrf_id))
774     ;
775   else
776     {
777       clib_warning("unknown input '%U'", format_unformat_error, i);
778       return -99;
779     }
780
781   M(NAT44_USER_SESSION_DUMP, mp);
782   S(mp);
783
784   /* Use a control ping for synchronization */
785   M(NAT_CONTROL_PING, mp_ping);
786   clib_memset(mp->ip_address, 0, 16);
787   clib_memcpy(mp->ip_address, &addr, 4);
788   mp->vrf_id = htonl(vrf_id);
789   S(mp_ping);
790
791   W (ret);
792   return ret;
793 }
794
795 static void vl_api_nat44_user_details_t_handler
796   (vl_api_nat44_user_details_t *mp)
797 {
798   snat_test_main_t * sm = &snat_test_main;
799   vat_main_t *vam = sm->vat_main;
800
801   fformat(vam->ofp, "user with ip %U with vrf_id %d "
802                     "with %d sessions and %d static sessions\n",
803           format_ip4_address, mp->ip_address, ntohl(mp->vrf_id),
804           ntohl(mp->nsessions), ntohl(mp->nstaticsessions));
805 }
806
807 static int api_nat44_user_dump(vat_main_t * vam)
808 {
809   vl_api_nat44_user_dump_t * mp;
810   vl_api_nat_control_ping_t *mp_ping;
811   int ret;
812
813   if (vam->json_output)
814     {
815       clib_warning ("JSON output not supported for nat44_address_dump");
816       return -99;
817     }
818
819   M(NAT44_USER_DUMP, mp);
820   S(mp);
821
822   /* Use a control ping for synchronization */
823   M(NAT_CONTROL_PING, mp_ping);
824   S(mp_ping);
825
826   W (ret);
827   return ret;
828 }
829
830 static int api_nat_det_add_del_map (vat_main_t * vam)
831 {
832   unformat_input_t * i = vam->input;
833   vl_api_nat_det_add_del_map_t * mp;
834   ip4_address_t in_addr, out_addr;
835   u32 in_plen, out_plen;
836   u8 is_add = 1;
837   int ret;
838
839   if (unformat (i, "in %U/%d out %U/%d",
840                 unformat_ip4_address, &in_addr, &in_plen,
841                 unformat_ip4_address, &out_addr, &out_plen))
842     ;
843   else if (unformat (i, "del"))
844     is_add = 0;
845   else
846     {
847       clib_warning("unknown input '%U'", format_unformat_error, i);
848       return -99;
849     }
850
851   M(NAT_DET_ADD_DEL_MAP, mp);
852   clib_memcpy(mp->in_addr, &in_addr, 4);
853   mp->in_plen = in_plen;
854   clib_memcpy(mp->out_addr, &out_addr, 4);
855   mp->out_plen = out_plen;
856   mp->is_add = is_add;
857
858   S(mp);
859   W (ret);
860   return ret;
861 }
862
863 static void vl_api_nat_det_forward_reply_t_handler
864   (vl_api_nat_det_forward_reply_t *mp)
865 {
866   snat_test_main_t * sm = &snat_test_main;
867   vat_main_t *vam = sm->vat_main;
868   i32 retval = ntohl(mp->retval);
869
870   if (retval >= 0)
871   {
872     fformat (vam->ofp, "outside address %U", format_ip4_address, &mp->out_addr);
873     fformat (vam->ofp, " outside port range start %d", ntohs(mp->out_port_lo));
874     fformat (vam->ofp, " outside port range end %d\n", ntohs(mp->out_port_hi));
875   }
876
877   vam->retval = retval;
878   vam->result_ready = 1;
879 }
880
881 static int api_nat_det_forward (vat_main_t * vam)
882 {
883   unformat_input_t * i = vam->input;
884   vl_api_nat_det_forward_t * mp;
885   ip4_address_t in_addr;
886   int ret;
887
888   if (unformat (i, "%U", unformat_ip4_address, &in_addr))
889     ;
890   else
891     {
892       clib_warning("unknown input '%U'", format_unformat_error, i);
893       return -99;
894     }
895
896   M(NAT_DET_FORWARD, mp);
897   clib_memcpy(mp->in_addr, &in_addr, 4);
898
899   S(mp);
900   W(ret);
901   return ret;
902 }
903
904 static void vl_api_nat_det_reverse_reply_t_handler
905   (vl_api_nat_det_reverse_reply_t *mp)
906 {
907   snat_test_main_t * sm = &snat_test_main;
908   vat_main_t *vam = sm->vat_main;
909   i32 retval = ntohl(mp->retval);
910
911   if (retval >= 0)
912   {
913     fformat (vam->ofp, "inside address %U\n", format_ip4_address, &mp->in_addr);
914   }
915
916   vam->retval = retval;
917   vam->result_ready = 1;
918 }
919
920 static int api_nat_det_reverse (vat_main_t * vam)
921 {
922   unformat_input_t * i = vam->input;
923   vl_api_nat_det_reverse_t * mp;
924   ip4_address_t out_addr;
925   u32 out_port;
926   int ret;
927
928   if (unformat (i, "%U %d", unformat_ip4_address, &out_addr, &out_port))
929     ;
930   else
931     {
932       clib_warning("unknown input '%U'", format_unformat_error, i);
933       return -99;
934     }
935
936   M(NAT_DET_REVERSE, mp);
937   clib_memcpy(mp->out_addr, &out_addr, 4);
938   mp->out_port = htons((u16)out_port);
939
940   S(mp);
941   W(ret);
942   return ret;
943 }
944
945 static void vl_api_nat_det_map_details_t_handler
946   (vl_api_nat_det_map_details_t *mp)
947 {
948   snat_test_main_t * sm = &snat_test_main;
949   vat_main_t *vam = sm->vat_main;
950
951   fformat (vam->ofp, "Deterministic S-NAT mapping in %U/%d out %U/%d "
952                      "ports per host %d sharing ratio %d "
953                      "number of sessions %d",
954            format_ip4_address, mp->in_addr, mp->in_plen,
955            format_ip4_address, mp->out_addr, mp->out_plen,
956            ntohs(mp->ports_per_host), ntohl(mp->sharing_ratio),
957            ntohl(mp->ses_num));
958 }
959
960 static int api_nat_det_map_dump(vat_main_t * vam)
961 {
962   vl_api_nat_det_map_dump_t * mp;
963   vl_api_nat_control_ping_t *mp_ping;
964   int ret;
965
966   if (vam->json_output)
967     {
968       clib_warning ("JSON output not supported for nat_det_map_dump");
969       return -99;
970     }
971
972   M(NAT_DET_MAP_DUMP, mp);
973   S(mp);
974
975   /* Use a control ping for synchronization */
976   M(NAT_CONTROL_PING, mp_ping);
977   S(mp_ping);
978
979   W (ret);
980   return ret;
981 }
982
983 static int api_nat_set_timeouts (vat_main_t * vam)
984 {
985   unformat_input_t * i = vam->input;
986   vl_api_nat_set_timeouts_t * mp;
987   u32 udp = SNAT_UDP_TIMEOUT;
988   u32 tcp_established = SNAT_TCP_ESTABLISHED_TIMEOUT;
989   u32 tcp_transitory = SNAT_TCP_TRANSITORY_TIMEOUT;
990   u32 icmp = SNAT_ICMP_TIMEOUT;
991   int ret;
992
993   if (unformat (i, "udp %d", &udp))
994     ;
995   else if (unformat (i, "tcp_established %d", &tcp_established))
996     ;
997   else if (unformat (i, "tcp_transitory %d", &tcp_transitory))
998     ;
999   else if (unformat (i, "icmp %d", &icmp))
1000     ;
1001   else
1002     {
1003       clib_warning("unknown input '%U'", format_unformat_error, i);
1004       return -99;
1005     }
1006
1007   M(NAT_SET_TIMEOUTS, mp);
1008   mp->udp = htonl(udp);
1009   mp->tcp_established = htonl(tcp_established);
1010   mp->tcp_transitory = htonl(tcp_transitory);
1011   mp->icmp = htonl(icmp);
1012
1013   S(mp);
1014   W (ret);
1015   return ret;
1016 }
1017
1018 static void vl_api_nat_get_timeouts_reply_t_handler
1019   (vl_api_nat_get_timeouts_reply_t *mp)
1020 {
1021   snat_test_main_t * sm = &snat_test_main;
1022   vat_main_t *vam = sm->vat_main;
1023   i32 retval = ntohl (mp->retval);
1024
1025   if (retval >= 0)
1026     {
1027       fformat (vam->ofp, "udp timeout: %dsec\n", ntohl (mp->udp));
1028       fformat (vam->ofp, "tcp-established timeout: %dsec",
1029                ntohl (mp->tcp_established));
1030       fformat (vam->ofp, "tcp-transitory timeout: %dsec",
1031                ntohl (mp->tcp_transitory));
1032       fformat (vam->ofp, "icmp timeout: %dsec", ntohl (mp->icmp));
1033     }
1034   vam->retval = retval;
1035   vam->result_ready = 1;
1036 }
1037
1038 static int api_nat_get_timeouts(vat_main_t * vam)
1039 {
1040   vl_api_nat_get_timeouts_t * mp;
1041   int ret;
1042
1043   if (vam->json_output)
1044     {
1045       clib_warning ("JSON output not supported for nat_get_timeouts");
1046       return -99;
1047     }
1048
1049   M(NAT_GET_TIMEOUTS, mp);
1050   S(mp);
1051   W (ret);
1052   return ret;
1053 }
1054
1055 static int api_nat_det_close_session_out (vat_main_t * vam)
1056 {
1057   unformat_input_t * i = vam->input;
1058   vl_api_nat_det_close_session_out_t * mp;
1059   ip4_address_t out_addr, ext_addr;
1060   u32 out_port, ext_port;
1061   int ret;
1062
1063   if (unformat (i, "%U:%d %U:%d",
1064                 unformat_ip4_address, &out_addr, &out_port,
1065                 unformat_ip4_address, &ext_addr, &ext_port))
1066     ;
1067   else
1068     {
1069       clib_warning("unknown input '%U'", format_unformat_error, i);
1070       return -99;
1071     }
1072
1073   M(NAT_DET_CLOSE_SESSION_OUT, mp);
1074   clib_memcpy(mp->out_addr, &out_addr, 4);
1075   mp->out_port = ntohs((u16)out_port);
1076   clib_memcpy(mp->ext_addr, &ext_addr, 4);
1077   mp->ext_port = ntohs((u16)ext_port);
1078
1079   S(mp);
1080   W (ret);
1081   return ret;
1082 }
1083
1084 static int api_nat_det_close_session_in (vat_main_t * vam)
1085 {
1086   unformat_input_t * i = vam->input;
1087   vl_api_nat_det_close_session_in_t * mp;
1088   ip4_address_t in_addr, ext_addr;
1089   u32 in_port, ext_port;
1090   int ret;
1091
1092   if (unformat (i, "%U:%d %U:%d",
1093                 unformat_ip4_address, &in_addr, &in_port,
1094                 unformat_ip4_address, &ext_addr, &ext_port))
1095     ;
1096   else
1097     {
1098       clib_warning("unknown input '%U'", format_unformat_error, i);
1099       return -99;
1100     }
1101
1102   M(NAT_DET_CLOSE_SESSION_IN, mp);
1103   clib_memcpy(mp->in_addr, &in_addr, 4);
1104   mp->in_port = ntohs((u16)in_port);
1105   clib_memcpy(mp->ext_addr, &ext_addr, 4);
1106   mp->ext_port = ntohs((u16)ext_port);
1107
1108   S(mp);
1109   W (ret);
1110   return ret;
1111 }
1112
1113 static void vl_api_nat_det_session_details_t_handler
1114   (vl_api_nat_det_session_details_t *mp)
1115 {
1116   snat_test_main_t * sm = &snat_test_main;
1117   vat_main_t *vam = sm->vat_main;
1118
1119   fformat(vam->ofp, "deterministic session, external host address %U, "
1120                     "external host port %d, outer port %d, inside port %d",
1121           format_ip4_address, mp->ext_addr, mp->ext_port,
1122           mp->out_port, mp->in_port);
1123 }
1124
1125 static int api_nat_det_session_dump(vat_main_t * vam)
1126 {
1127   unformat_input_t* i = vam->input;
1128   vl_api_nat_det_session_dump_t * mp;
1129   vl_api_nat_control_ping_t *mp_ping;
1130   ip4_address_t user_addr;
1131   int ret;
1132
1133   if (vam->json_output)
1134     {
1135       clib_warning ("JSON output not supported for nat_det_session_dump");
1136       return -99;
1137     }
1138
1139   if (unformat (i, "user_addr %U", unformat_ip4_address, &user_addr))
1140     ;
1141   else
1142     {
1143       clib_warning ("unknown input '%U'", format_unformat_error, i);
1144       return -99;
1145     }
1146
1147   M(NAT_DET_SESSION_DUMP, mp);
1148   clib_memcpy (&mp->user_addr, &user_addr, 4);
1149   S(mp);
1150
1151   /* Use a control ping for synchronization */
1152   M(NAT_CONTROL_PING, mp_ping);
1153   S(mp_ping);
1154
1155   W (ret);
1156   return ret;
1157 }
1158
1159 /*
1160  * List of messages that the api test plugin sends,
1161  * and that the data plane plugin processes
1162  */
1163 #define foreach_vpe_api_msg                                       \
1164 _(nat44_add_del_address_range,                                    \
1165   "<start-addr> [- <end-addr>] [vrf <table-id>] [twice-nat] [del]") \
1166 _(nat44_interface_add_del_feature,                                \
1167   "<intfc> | sw_if_index <id> [in] [out] [del]")                  \
1168 _(nat44_interface_add_del_output_feature,                         \
1169   "<intfc> | sw_if_index <id> [in] [out] [del]")                  \
1170 _(nat44_add_del_static_mapping, "local_addr <ip>"                 \
1171   " (external_addr <ip> | external_if <intfc> |"                  \
1172   " external_sw_if_ndex <id>) [local_port <n>]"                   \
1173   " [external_port <n>] [vrf <table-id>] [del] protocol <n>")     \
1174 _(nat_set_workers, "<wokrers_bitmap>")                            \
1175 _(nat44_static_mapping_dump, "")                                  \
1176 _(nat_show_config, "")                                            \
1177 _(nat44_address_dump, "")                                         \
1178 _(nat44_interface_dump, "")                                       \
1179 _(nat_worker_dump, "")                                            \
1180 _(nat44_add_del_interface_addr,                                   \
1181   "<intfc> | sw_if_index <id> [twice-nat] [del]")                 \
1182 _(nat44_interface_addr_dump, "")                                  \
1183 _(nat_ipfix_enable_disable, "[domain <id>] [src_port <n>] "       \
1184   "[disable]")                                                    \
1185 _(nat44_user_dump, "")                                            \
1186 _(nat44_user_session_dump, "ip_address <ip> vrf_id <table-id>")   \
1187 _(nat_det_add_del_map, "in <in_addr>/<in_plen> out "              \
1188   "<out_addr>/<out_plen> [del]")                                  \
1189 _(nat_det_forward, "<in_addr>")                                   \
1190 _(nat_det_reverse, "<out_addr> <out_port>")                       \
1191 _(nat_det_map_dump, "")                                           \
1192 _(nat_set_timeouts, "[udp <sec> | tcp_established <sec> | "       \
1193   "tcp_transitory <sec> | icmp <sec>]")                           \
1194 _(nat_get_timeouts, "")                                           \
1195 _(nat_det_close_session_out, "<out_addr>:<out_port> "             \
1196   "<ext_addr>:<ext_port>")                                        \
1197 _(nat_det_close_session_in, "<in_addr>:<in_port> "                \
1198   "<out_addr>:<out_port>")                                        \
1199 _(nat_det_session_dump, "ip_address <user_addr>")
1200
1201 static void
1202 snat_vat_api_hookup (vat_main_t *vam)
1203 {
1204   snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
1205   /* Hook up handlers for replies from the data plane plug-in */
1206 #define _(N,n)                                                  \
1207   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),       \
1208                           #n,                                   \
1209                           vl_api_##n##_t_handler,               \
1210                           vl_noop_handler,                      \
1211                           vl_api_##n##_t_endian,                \
1212                           vl_api_##n##_t_print,                 \
1213                           sizeof(vl_api_##n##_t), 1);
1214   foreach_vpe_api_reply_msg;
1215 #undef _
1216
1217   /* API messages we can send */
1218 #define _(n,h)                                          \
1219   hash_set_mem (vam->function_by_name, #n, api_##n);
1220   foreach_vpe_api_msg;
1221 #undef _
1222
1223   /* Help strings */
1224 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1225   foreach_vpe_api_msg;
1226 #undef _
1227 }
1228
1229 clib_error_t * vat_plugin_register (vat_main_t *vam)
1230 {
1231   snat_test_main_t * sm = &snat_test_main;
1232   u8 * name;
1233
1234   sm->vat_main = vam;
1235
1236   /* Ask the vpp engine for the first assigned message-id */
1237   name = format (0, "nat_%08x%c", api_version, 0);
1238   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1239   vec_free(name);
1240
1241   if (sm->msg_id_base != (u16) ~0)
1242     snat_vat_api_hookup (vam);
1243   else
1244     return clib_error_return (0, "nat plugin not loaded...");
1245
1246   return 0;
1247 }