acl-plugin: fix pretty-printing in "api trace custom-dump" (VPP-683)
[vpp.git] / src / plugins / snat / snat_test.c
1
2 /*
3  * snat.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 #include <vlibsocket/api.h>
22 #include <vppinfra/error.h>
23 #include <vnet/ip/ip.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 <snat/snat_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <snat/snat_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 <snat/snat_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 <snat/snat_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 <snat/snat_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 _(snat_add_address_range_reply)                 \
65 _(snat_interface_add_del_feature_reply)         \
66 _(snat_add_static_mapping_reply)                \
67 _(snat_set_workers_reply)                       \
68 _(snat_add_del_interface_addr_reply)            \
69 _(snat_ipfix_enable_disable_reply)              \
70 _(snat_add_det_map_reply)
71
72 #define _(n)                                            \
73     static void vl_api_##n##_t_handler                  \
74     (vl_api_##n##_t * mp)                               \
75     {                                                   \
76         vat_main_t * vam = snat_test_main.vat_main;   \
77         i32 retval = ntohl(mp->retval);                 \
78         if (vam->async_mode) {                          \
79             vam->async_errors += (retval < 0);          \
80         } else {                                        \
81             vam->retval = retval;                       \
82             vam->result_ready = 1;                      \
83         }                                               \
84     }
85 foreach_standard_reply_retval_handler;
86 #undef _
87
88 /* 
89  * Table of message reply handlers, must include boilerplate handlers
90  * we just generated
91  */
92 #define foreach_vpe_api_reply_msg                               \
93 _(SNAT_ADD_ADDRESS_RANGE_REPLY, snat_add_address_range_reply)   \
94 _(SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY,                         \
95   snat_interface_add_del_feature_reply)                         \
96 _(SNAT_ADD_STATIC_MAPPING_REPLY, snat_add_static_mapping_reply) \
97 _(SNAT_CONTROL_PING_REPLY, snat_control_ping_reply)             \
98 _(SNAT_STATIC_MAPPING_DETAILS, snat_static_mapping_details)     \
99 _(SNAT_SHOW_CONFIG_REPLY, snat_show_config_reply)               \
100 _(SNAT_ADDRESS_DETAILS, snat_address_details)                   \
101 _(SNAT_INTERFACE_DETAILS, snat_interface_details)               \
102 _(SNAT_SET_WORKERS_REPLY, snat_set_workers_reply)               \
103 _(SNAT_WORKER_DETAILS, snat_worker_details)                     \
104 _(SNAT_ADD_DEL_INTERFACE_ADDR_REPLY,                            \
105   snat_add_del_interface_addr_reply)                            \
106 _(SNAT_INTERFACE_ADDR_DETAILS, snat_interface_addr_details)     \
107 _(SNAT_IPFIX_ENABLE_DISABLE_REPLY,                              \
108   snat_ipfix_enable_disable_reply)                              \
109 _(SNAT_USER_DETAILS, snat_user_details)                         \
110 _(SNAT_USER_SESSION_DETAILS, snat_user_session_details)         \
111 _(SNAT_ADD_DET_MAP_REPLY, snat_add_det_map_reply)               \
112 _(SNAT_DET_FORWARD_REPLY, snat_det_forward_reply)               \
113 _(SNAT_DET_REVERSE_REPLY, snat_det_reverse_reply)               \
114 _(SNAT_DET_MAP_DETAILS, snat_det_map_details)
115
116 static int api_snat_add_address_range (vat_main_t * vam)
117 {
118   unformat_input_t * i = vam->input;
119   ip4_address_t start_addr, end_addr;
120   u32 start_host_order, end_host_order;
121   vl_api_snat_add_address_range_t * mp;
122   u8 is_add = 1;
123   int count;
124   int ret;
125
126   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
127     {
128       if (unformat (i, "%U - %U",
129                     unformat_ip4_address, &start_addr,
130                     unformat_ip4_address, &end_addr))
131         ;
132       else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
133         end_addr = start_addr;
134       else if (unformat (i, "del"))
135         is_add = 0;
136       else
137         {
138           clib_warning("unknown input '%U'", format_unformat_error, i);
139           return -99;
140         }
141     }
142
143   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
144   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
145   
146   if (end_host_order < start_host_order)
147     {
148       errmsg ("end address less than start address\n");
149       return -99;
150     }
151
152   count = (end_host_order - start_host_order) + 1;
153
154   if (count > 1024)
155     {
156     errmsg ("%U - %U, %d addresses...\n",
157            format_ip4_address, &start_addr,
158            format_ip4_address, &end_addr,
159            count);
160     }
161   
162   M(SNAT_ADD_ADDRESS_RANGE, mp);
163
164   memcpy (mp->first_ip_address, &start_addr, 4);
165   memcpy (mp->last_ip_address, &end_addr, 4);
166   mp->is_ip4 = 1;
167   mp->is_add = is_add;
168
169   S(mp);
170   W (ret);
171   return ret;
172 }
173
174 static int api_snat_interface_add_del_feature (vat_main_t * vam)
175 {
176   unformat_input_t * i = vam->input;
177   vl_api_snat_interface_add_del_feature_t * mp;
178   u32 sw_if_index;
179   u8 sw_if_index_set = 0;
180   u8 is_inside = 1; 
181   u8 is_add = 1;
182   int ret;
183
184   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
185     {
186       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
187         sw_if_index_set = 1;
188       else if (unformat (i, "sw_if_index %d", &sw_if_index))
189         sw_if_index_set = 1;
190       else if (unformat (i, "out"))
191         is_inside = 0;
192       else if (unformat (i, "in"))
193         is_inside = 1;
194       else if (unformat (i, "del"))
195         is_add = 0;
196       else
197         {
198           clib_warning("unknown input '%U'", format_unformat_error, i);
199           return -99;
200         }
201     }
202
203   if (sw_if_index_set == 0)
204     {
205       errmsg ("interface / sw_if_index required\n");
206       return -99;
207     }
208
209   M(SNAT_INTERFACE_ADD_DEL_FEATURE, mp);
210   mp->sw_if_index = ntohl(sw_if_index);
211   mp->is_add = is_add;
212   mp->is_inside = is_inside;
213   
214   S(mp);
215   W (ret);
216   return ret;
217 }
218
219 static int api_snat_add_static_mapping(vat_main_t * vam)
220 {
221   unformat_input_t * i = vam->input;
222   vl_api_snat_add_static_mapping_t * mp;
223   u8 external_addr_set = 0;
224   u8 local_addr_set = 0;
225   u8 is_add = 1;
226   u8 addr_only = 1;
227   ip4_address_t local_addr, external_addr;
228   u32 local_port = 0, external_port = 0, vrf_id = ~0;
229   u32 sw_if_index = ~0;
230   u8 sw_if_index_set = 0;
231   u32 proto = ~0;
232   u8 proto_set = 0;
233   int ret;
234
235   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
236     {
237       if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
238         local_addr_set = 1;
239       else if (unformat (i, "external_addr %U", unformat_ip4_address,
240                          &external_addr))
241         external_addr_set = 1;
242       else if (unformat (i, "local_port %u", &local_port))
243         addr_only = 0;
244       else if (unformat (i, "external_port %u", &external_port))
245         addr_only = 0;
246       else if (unformat (i, "external_if %U", unformat_sw_if_index, vam,
247                          &sw_if_index))
248         sw_if_index_set = 1;
249       else if (unformat (i, "external_sw_if_index %d", &sw_if_index))
250         sw_if_index_set = 1;
251       else if (unformat (i, "vrf %u", &vrf_id))
252         ;
253       else if (unformat (i, "protocol %u", &proto))
254         proto_set = 1;
255       else if (unformat (i, "del"))
256         is_add = 0;
257       else
258         {
259           clib_warning("unknown input '%U'", format_unformat_error, i);
260           return -99;
261         }
262     }
263
264   if (!addr_only && !proto_set)
265     {
266       errmsg ("protocol required\n");
267       return -99;
268     }
269
270   if (!local_addr_set)
271     {
272       errmsg ("local addr required\n");
273       return -99;
274     }
275   if (!external_addr_set && !sw_if_index_set)
276     {
277       errmsg ("external addr or interface required\n");
278       return -99;
279     }
280
281   M(SNAT_ADD_STATIC_MAPPING, mp);
282   mp->is_add = is_add;
283   mp->is_ip4 = 1;
284   mp->addr_only = addr_only;
285   mp->local_port = ntohs ((u16) local_port);
286   mp->external_port = ntohs ((u16) external_port);
287   mp->external_sw_if_index = ntohl (sw_if_index);
288   mp->vrf_id = ntohl (vrf_id);
289   mp->protocol = (u8) proto;
290   memcpy (mp->local_ip_address, &local_addr, 4);
291   memcpy (mp->external_ip_address, &external_addr, 4);
292
293   S(mp);
294   W (ret);
295   return ret;
296 }
297
298 static void vl_api_snat_control_ping_reply_t_handler
299   (vl_api_snat_control_ping_reply_t * mp)
300 {
301   vat_main_t *vam = &vat_main;
302   i32 retval = ntohl (mp->retval);
303   if (vam->async_mode)
304     {
305       vam->async_errors += (retval < 0);
306     }
307   else
308     {
309       vam->retval = retval;
310       vam->result_ready = 1;
311     }
312 }
313
314 static void vl_api_snat_static_mapping_details_t_handler
315   (vl_api_snat_static_mapping_details_t *mp)
316 {
317   snat_test_main_t * sm = &snat_test_main;
318   vat_main_t *vam = sm->vat_main;
319
320   if (mp->addr_only && mp->external_sw_if_index != ~0)
321       fformat (vam->ofp, "%15U%6s%15d%6s%11d%6d\n",
322                format_ip4_address, &mp->local_ip_address, "",
323                ntohl (mp->external_sw_if_index), "",
324                ntohl (mp->vrf_id),
325                mp->protocol);
326   else if (mp->addr_only && mp->external_sw_if_index == ~0)
327       fformat (vam->ofp, "%15U%6s%15U%6s%11d%6d\n",
328                format_ip4_address, &mp->local_ip_address, "",
329                format_ip4_address, &mp->external_ip_address, "",
330                ntohl (mp->vrf_id),
331                mp->protocol);
332   else if (!mp->addr_only && mp->external_sw_if_index != ~0)
333       fformat (vam->ofp, "%15U%6d%15d%6d%11d%6d\n",
334                format_ip4_address, &mp->local_ip_address,
335                ntohs (mp->local_port),
336                ntohl (mp->external_sw_if_index),
337                ntohs (mp->external_port),
338                ntohl (mp->vrf_id),
339                mp->protocol);
340   else
341       fformat (vam->ofp, "%15U%6d%15U%6d%11d%6d\n",
342                format_ip4_address, &mp->local_ip_address,
343                ntohs (mp->local_port),
344                format_ip4_address, &mp->external_ip_address,
345                ntohs (mp->external_port),
346                ntohl (mp->vrf_id),
347                mp->protocol);
348
349 }
350
351 static int api_snat_static_mapping_dump(vat_main_t * vam)
352 {
353   vl_api_snat_static_mapping_dump_t * mp;
354   vl_api_snat_control_ping_t *mp_ping;
355   int ret;
356
357   if (vam->json_output)
358     {
359       clib_warning ("JSON output not supported for snat_static_mapping_dump");
360       return -99;
361     }
362
363   fformat (vam->ofp, "%21s%21s\n", "local", "external");
364   fformat (vam->ofp, "%15s%6s%15s%6s%11s%6s\n", "address", "port",
365            "address/if_idx", "port", "vrf", "proto");
366
367   M(SNAT_STATIC_MAPPING_DUMP, mp);
368   S(mp);
369
370   /* Use a control ping for synchronization */
371   M(SNAT_CONTROL_PING, mp_ping);
372   S(mp_ping);
373
374   W (ret);
375   return ret;
376 }
377
378 static void vl_api_snat_show_config_reply_t_handler
379   (vl_api_snat_show_config_reply_t *mp)
380 {
381   snat_test_main_t * sm = &snat_test_main;
382   vat_main_t *vam = sm->vat_main;
383   i32 retval = ntohl (mp->retval);
384
385   if (retval >= 0)
386     {
387       fformat (vam->ofp, "translation hash buckets %d\n",
388                ntohl (mp->translation_buckets));
389       fformat (vam->ofp, "translation hash memory %d\n",
390                ntohl (mp->translation_memory_size));
391       fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
392       fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
393       fformat (vam->ofp, "max translations per user %d\n",
394                ntohl (mp->max_translations_per_user));
395       fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
396       fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
397       if (mp->static_mapping_only)
398         {
399           fformat (vam->ofp, "static mapping only");
400           if (mp->static_mapping_connection_tracking)
401             fformat (vam->ofp, " connection tracking");
402           fformat (vam->ofp, "\n");
403         }
404     }
405   vam->retval = retval;
406   vam->result_ready = 1;
407 }
408
409 static int api_snat_show_config(vat_main_t * vam)
410 {
411   vl_api_snat_show_config_t * mp;
412   int ret;
413
414   if (vam->json_output)
415     {
416       clib_warning ("JSON output not supported for snat_show_config");
417       return -99;
418     }
419
420   M(SNAT_SHOW_CONFIG, mp);
421   S(mp);
422   W (ret);
423   return ret;
424 }
425
426 static void vl_api_snat_address_details_t_handler
427   (vl_api_snat_address_details_t *mp)
428 {
429   snat_test_main_t * sm = &snat_test_main;
430   vat_main_t *vam = sm->vat_main;
431
432   fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
433 }
434
435 static int api_snat_address_dump(vat_main_t * vam)
436 {
437   vl_api_snat_address_dump_t * mp;
438   vl_api_snat_control_ping_t *mp_ping;
439   int ret;
440
441   if (vam->json_output)
442     {
443       clib_warning ("JSON output not supported for snat_address_dump");
444       return -99;
445     }
446
447   M(SNAT_ADDRESS_DUMP, mp);
448   S(mp);
449
450   /* Use a control ping for synchronization */
451   M(SNAT_CONTROL_PING, mp_ping);
452   S(mp_ping);
453
454   W (ret);
455   return ret;
456 }
457
458 static void vl_api_snat_interface_details_t_handler
459   (vl_api_snat_interface_details_t *mp)
460 {
461   snat_test_main_t * sm = &snat_test_main;
462   vat_main_t *vam = sm->vat_main;
463
464   fformat (vam->ofp, "sw_if_index %d %s\n", ntohl (mp->sw_if_index),
465            mp->is_inside ? "in" : "out");
466 }
467
468 static int api_snat_interface_dump(vat_main_t * vam)
469 {
470   vl_api_snat_interface_dump_t * mp;
471   vl_api_snat_control_ping_t *mp_ping;
472   int ret;
473
474   if (vam->json_output)
475     {
476       clib_warning ("JSON output not supported for snat_address_dump");
477       return -99;
478     }
479
480   M(SNAT_INTERFACE_DUMP, mp);
481   S(mp);
482
483   /* Use a control ping for synchronization */
484   M(SNAT_CONTROL_PING, mp_ping);
485   S(mp_ping);
486
487   W (ret);
488   return ret;
489 }
490
491 static int api_snat_set_workers (vat_main_t * vam)
492 {
493   unformat_input_t * i = vam->input;
494   vl_api_snat_set_workers_t * mp;
495   uword *bitmap;
496   int ret;
497
498   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
499     {
500       if (unformat (i, "%U", unformat_bitmap_list, &bitmap))
501         ;
502       else
503         {
504           clib_warning("unknown input '%U'", format_unformat_error, i);
505           return -99;
506         }
507     }
508
509   M(SNAT_SET_WORKERS, mp);
510   mp->worker_mask = clib_host_to_net_u64 (bitmap[0]);
511
512   S(mp);
513   W (ret);
514   return ret;
515 }
516
517 static void vl_api_snat_worker_details_t_handler
518   (vl_api_snat_worker_details_t *mp)
519 {
520   snat_test_main_t * sm = &snat_test_main;
521   vat_main_t *vam = sm->vat_main;
522
523   fformat (vam->ofp, "worker_index %d (%s at lcore %u)\n",
524            ntohl (mp->worker_index), mp->name, ntohl (mp->lcore_id));
525 }
526
527 static int api_snat_worker_dump(vat_main_t * vam)
528 {
529   vl_api_snat_worker_dump_t * mp;
530   vl_api_snat_control_ping_t *mp_ping;
531   int ret;
532
533   if (vam->json_output)
534     {
535       clib_warning ("JSON output not supported for snat_address_dump");
536       return -99;
537     }
538
539   M(SNAT_WORKER_DUMP, mp);
540   S(mp);
541
542   /* Use a control ping for synchronization */
543   M(SNAT_CONTROL_PING, mp_ping);
544   S(mp_ping);
545
546   W (ret);
547   return ret;
548 }
549
550 static int api_snat_add_del_interface_addr (vat_main_t * vam)
551 {
552   unformat_input_t * i = vam->input;
553   vl_api_snat_add_del_interface_addr_t * mp;
554   u32 sw_if_index;
555   u8 sw_if_index_set = 0;
556   u8 is_add = 1;
557   int ret;
558
559   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
560     {
561       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
562         sw_if_index_set = 1;
563       else if (unformat (i, "sw_if_index %d", &sw_if_index))
564         sw_if_index_set = 1;
565       else if (unformat (i, "del"))
566         is_add = 0;
567       else
568         {
569           clib_warning("unknown input '%U'", format_unformat_error, i);
570           return -99;
571         }
572     }
573
574   if (sw_if_index_set == 0)
575     {
576       errmsg ("interface / sw_if_index required\n");
577       return -99;
578     }
579
580   M(SNAT_ADD_DEL_INTERFACE_ADDR, mp);
581   mp->sw_if_index = ntohl(sw_if_index);
582   mp->is_add = is_add;
583   
584   S(mp);
585   W (ret);
586   return ret;
587 }
588
589 static void vl_api_snat_interface_addr_details_t_handler
590   (vl_api_snat_interface_addr_details_t *mp)
591 {
592   snat_test_main_t * sm = &snat_test_main;
593   vat_main_t *vam = sm->vat_main;
594
595   fformat (vam->ofp, "sw_if_index %d\n", ntohl (mp->sw_if_index));
596 }
597
598 static int api_snat_interface_addr_dump(vat_main_t * vam)
599 {
600   vl_api_snat_interface_addr_dump_t * mp;
601   vl_api_snat_control_ping_t *mp_ping;
602   int ret;
603
604   if (vam->json_output)
605     {
606       clib_warning ("JSON output not supported for snat_address_dump");
607       return -99;
608     }
609
610   M(SNAT_INTERFACE_ADDR_DUMP, mp);
611   S(mp);
612
613   /* Use a control ping for synchronization */
614   M(SNAT_CONTROL_PING, mp_ping);
615   S(mp_ping);
616
617   W (ret);
618   return ret;
619 }
620
621 static int api_snat_ipfix_enable_disable (vat_main_t * vam)
622 {
623   unformat_input_t * i = vam->input;
624   vl_api_snat_ipfix_enable_disable_t * mp;
625   u32 domain_id = 0;
626   u32 src_port = 0;
627   u8 enable = 1;
628   int ret;
629
630   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
631     {
632       if (unformat (i, "domain %d", &domain_id))
633         ;
634       else if (unformat (i, "src_port %d", &src_port))
635         ;
636       else if (unformat (i, "disable"))
637         enable = 0;
638       else
639         {
640           clib_warning("unknown input '%U'", format_unformat_error, i);
641           return -99;
642         }
643     }
644
645   M(SNAT_IPFIX_ENABLE_DISABLE, mp);
646   mp->domain_id = htonl(domain_id);
647   mp->src_port = htons((u16) src_port);
648   mp->enable = enable;
649
650   S(mp);
651   W (ret);
652   return ret;
653 }
654
655 static void vl_api_snat_user_session_details_t_handler
656   (vl_api_snat_user_session_details_t *mp)
657 {
658   snat_test_main_t * sm = &snat_test_main;
659   vat_main_t *vam = sm->vat_main;
660
661   fformat(vam->ofp, "%s session %U:%d to %U:%d protocol id %d "
662                     "total packets %d total bytes %d\n",
663           mp->is_static ? "static" : "dynamic",
664           format_ip4_address, mp->inside_ip_address, ntohl(mp->inside_port),
665           format_ip4_address, mp->outside_ip_address, ntohl(mp->outside_port),
666           ntohl(mp->protocol), ntohl(mp->total_pkts), ntohl(mp->total_bytes));
667 }
668
669 static int api_snat_user_session_dump(vat_main_t * vam)
670 {
671   unformat_input_t* i = vam->input;
672   vl_api_snat_user_session_dump_t * mp;
673   vl_api_snat_control_ping_t *mp_ping;
674   ip4_address_t addr;
675   u32 vrf_id = ~0;
676   int ret;
677
678   if (vam->json_output)
679     {
680       clib_warning ("JSON output not supported for snat_address_dump");
681       return -99;
682     }
683
684   if (unformat (i, "ip_address %U vrf_id %d",
685                 unformat_ip4_address, &addr, &vrf_id))
686     ;
687   else
688     {
689       clib_warning("unknown input '%U'", format_unformat_error, i);
690       return -99;
691     }
692
693   M(SNAT_USER_SESSION_DUMP, mp);
694   S(mp);
695
696   /* Use a control ping for synchronization */
697   M(SNAT_CONTROL_PING, mp_ping);
698   memset(mp->ip_address, 0, 16);
699   clib_memcpy(mp->ip_address, &addr, 4);
700   mp->vrf_id = htonl(vrf_id);
701   mp->is_ip4 = 1;
702   S(mp_ping);
703
704   W (ret);
705   return ret;
706 }
707
708 static void vl_api_snat_user_details_t_handler
709   (vl_api_snat_user_details_t *mp)
710 {
711   snat_test_main_t * sm = &snat_test_main;
712   vat_main_t *vam = sm->vat_main;
713
714   fformat(vam->ofp, "user with ip %U with vrf_id %d "
715                     "with %d sessions and %d static sessions\n",
716           format_ip4_address, mp->ip_address, ntohl(mp->vrf_id),
717           ntohl(mp->nsessions), ntohl(mp->nstaticsessions));
718 }
719
720 static int api_snat_user_dump(vat_main_t * vam)
721 {
722   vl_api_snat_user_dump_t * mp;
723   vl_api_snat_control_ping_t *mp_ping;
724   int ret;
725
726   if (vam->json_output)
727     {
728       clib_warning ("JSON output not supported for snat_address_dump");
729       return -99;
730     }
731
732   M(SNAT_USER_DUMP, mp);
733   S(mp);
734
735   /* Use a control ping for synchronization */
736   M(SNAT_CONTROL_PING, mp_ping);
737   S(mp_ping);
738
739   W (ret);
740   return ret;
741 }
742
743 static int api_snat_add_det_map (vat_main_t * vam)
744 {
745   unformat_input_t * i = vam->input;
746   vl_api_snat_add_det_map_t * mp;
747   ip4_address_t in_addr, out_addr;
748   u32 in_plen, out_plen;
749   u8 is_add = 1;
750   int ret;
751
752   if (unformat (i, "in %U/%d out %U/%d",
753                 unformat_ip4_address, &in_addr, &in_plen,
754                 unformat_ip4_address, &out_addr, &out_plen))
755     ;
756   else if (unformat (i, "del"))
757     is_add = 0;
758   else
759     {
760       clib_warning("unknown input '%U'", format_unformat_error, i);
761       return -99;
762     }
763
764   M(SNAT_ADD_DET_MAP, mp);
765   clib_memcpy(mp->in_addr, &in_addr, 4);
766   mp->in_plen = in_plen;
767   clib_memcpy(mp->out_addr, &out_addr, 4);
768   mp->out_plen = out_plen;
769   mp->is_add = is_add;
770
771   S(mp);
772   W (ret);
773   return ret;
774 }
775
776 static void vl_api_snat_det_forward_reply_t_handler
777   (vl_api_snat_det_forward_reply_t *mp)
778 {
779   snat_test_main_t * sm = &snat_test_main;
780   vat_main_t *vam = sm->vat_main;
781   i32 retval = ntohl(mp->retval);
782
783   if (retval >= 0)
784   {
785     fformat (vam->ofp, "outside address %U", format_ip4_address, &mp->out_addr);
786     fformat (vam->ofp, " outside port range start %d", ntohs(mp->out_port_lo));
787     fformat (vam->ofp, " outside port range end %d\n", ntohs(mp->out_port_hi));
788   }
789
790   vam->retval = retval;
791   vam->result_ready = 1;
792 }
793
794 static int api_snat_det_forward (vat_main_t * vam)
795 {
796   unformat_input_t * i = vam->input;
797   vl_api_snat_det_forward_t * mp;
798   ip4_address_t in_addr;
799   int ret;
800
801   if (unformat (i, "%U", unformat_ip4_address, &in_addr))
802     ;
803   else
804     {
805       clib_warning("unknown input '%U'", format_unformat_error, i);
806       return -99;
807     }
808
809   M(SNAT_DET_FORWARD, mp);
810   clib_memcpy(mp->in_addr, &in_addr, 4);
811
812   S(mp);
813   W(ret);
814   return ret;
815 }
816
817 static void vl_api_snat_det_reverse_reply_t_handler
818   (vl_api_snat_det_reverse_reply_t *mp)
819 {
820   snat_test_main_t * sm = &snat_test_main;
821   vat_main_t *vam = sm->vat_main;
822   i32 retval = ntohl(mp->retval);
823
824   if (retval >= 0)
825   {
826     fformat (vam->ofp, "inside address %U\n", format_ip4_address, &mp->in_addr);
827   }
828
829   vam->retval = retval;
830   vam->result_ready = 1;
831 }
832
833 static int api_snat_det_reverse (vat_main_t * vam)
834 {
835   unformat_input_t * i = vam->input;
836   vl_api_snat_det_reverse_t * mp;
837   ip4_address_t out_addr;
838   u16 out_port;
839   int ret;
840
841   if (unformat (i, "%U %d", unformat_ip4_address, &out_addr, &out_port))
842     ;
843   else
844     {
845       clib_warning("unknown input '%U'", format_unformat_error, i);
846       return -99;
847     }
848
849   M(SNAT_DET_REVERSE, mp);
850   clib_memcpy(mp->out_addr, &out_addr, 4);
851   mp->out_port = htons(out_port);
852
853   S(mp);
854   W(ret);
855   return ret;
856 }
857
858 static void vl_api_snat_det_map_details_t_handler
859   (vl_api_snat_det_map_details_t *mp)
860 {
861   snat_test_main_t * sm = &snat_test_main;
862   vat_main_t *vam = sm->vat_main;
863
864   fformat (vam->ofp, "Deterministic S-NAT mapping in %U/%d out %U/%d "
865                      "ports per host %d sharing ratio %d "
866                      "number of sessions %d",
867            format_ip4_address, mp->in_addr, mp->in_plen,
868            format_ip4_address, mp->out_addr, mp->out_plen,
869            ntohs(mp->ports_per_host), ntohl(mp->sharing_ratio),
870            ntohl(mp->ses_num));
871 }
872
873 static int api_snat_det_map_dump(vat_main_t * vam)
874 {
875   vl_api_snat_det_map_dump_t * mp;
876   vl_api_snat_control_ping_t *mp_ping;
877   int ret;
878
879   if (vam->json_output)
880     {
881       clib_warning ("JSON output not supported for snat_det_map_dump");
882       return -99;
883     }
884
885   M(SNAT_DET_MAP_DUMP, mp);
886   S(mp);
887
888   /* Use a control ping for synchronization */
889   M(SNAT_CONTROL_PING, mp_ping);
890   S(mp_ping);
891
892   W (ret);
893   return ret;
894 }
895
896 /* 
897  * List of messages that the api test plugin sends,
898  * and that the data plane plugin processes
899  */
900 #define foreach_vpe_api_msg                                      \
901 _(snat_add_address_range, "<start-addr> [- <end-addr] [del]")    \
902 _(snat_interface_add_del_feature,                                \
903   "<intfc> | sw_if_index <id> [in] [out] [del]")                 \
904 _(snat_add_static_mapping, "local_addr <ip> (external_addr <ip>" \
905   " | external_if <intfc> | external_sw_if_ndex <id>) "          \
906   "[local_port <n>] [external_port <n>] [vrf <table-id>] [del] " \
907   "protocol <n>")                                                \
908 _(snat_set_workers, "<wokrers_bitmap>")                          \
909 _(snat_static_mapping_dump, "")                                  \
910 _(snat_show_config, "")                                          \
911 _(snat_address_dump, "")                                         \
912 _(snat_interface_dump, "")                                       \
913 _(snat_worker_dump, "")                                          \
914 _(snat_add_del_interface_addr,                                   \
915   "<intfc> | sw_if_index <id> [del]")                            \
916 _(snat_interface_addr_dump, "")                                  \
917 _(snat_ipfix_enable_disable, "[domain <id>] [src_port <n>] "     \
918   "[disable]")                                                   \
919 _(snat_user_dump, "")                                            \
920 _(snat_user_session_dump, "ip_address <ip> vrf_id <table-id>")   \
921 _(snat_add_det_map, "in <in_addr>/<in_plen> out "                \
922   "<out_addr>/<out_plen> [del]")                                 \
923 _(snat_det_forward, "<in_addr>")                                 \
924 _(snat_det_reverse, "<out_addr> <out_port>")                     \
925 _(snat_det_map_dump, "")
926
927 static void 
928 snat_vat_api_hookup (vat_main_t *vam)
929 {
930   snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
931   /* Hook up handlers for replies from the data plane plug-in */
932 #define _(N,n)                                                  \
933   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),       \
934                           #n,                                   \
935                           vl_api_##n##_t_handler,               \
936                           vl_noop_handler,                      \
937                           vl_api_##n##_t_endian,                \
938                           vl_api_##n##_t_print,                 \
939                           sizeof(vl_api_##n##_t), 1); 
940   foreach_vpe_api_reply_msg;
941 #undef _
942
943   /* API messages we can send */
944 #define _(n,h)                                          \
945   hash_set_mem (vam->function_by_name, #n, api_##n);
946   foreach_vpe_api_msg;
947 #undef _    
948     
949   /* Help strings */
950 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
951   foreach_vpe_api_msg;
952 #undef _
953 }
954
955 clib_error_t * vat_plugin_register (vat_main_t *vam)
956 {
957   snat_test_main_t * sm = &snat_test_main;
958   u8 * name;
959
960   sm->vat_main = vam;
961
962   /* Ask the vpp engine for the first assigned message-id */
963   name = format (0, "snat_%08x%c", api_version, 0);
964   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
965
966   if (sm->msg_id_base != (u16) ~0)
967     snat_vat_api_hookup (vam);
968   
969   vec_free(name);
970   
971   return 0;
972 }