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