ipv6 connection tracking plugin
[vpp.git] / src / plugins / ct6 / ct6_test.c
1 /*
2  * ct6.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21
22 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
23
24 /* Declare message IDs */
25 #include <ct6/ct6_msg_enum.h>
26
27 /* define message structures */
28 #define vl_typedefs
29 #include <ct6/ct6_all_api_h.h>
30 #undef vl_typedefs
31
32 /* declare message handlers for each api */
33
34 #define vl_endianfun            /* define message structures */
35 #include <ct6/ct6_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
40 #define vl_printfun
41 #include <ct6/ct6_all_api_h.h>
42 #undef vl_printfun
43
44 /* Get the API version number. */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <ct6/ct6_all_api_h.h>
47 #undef vl_api_version
48
49
50 typedef struct
51 {
52   /* API message ID base */
53   u16 msg_id_base;
54   vat_main_t *vat_main;
55 } ct6_test_main_t;
56
57 ct6_test_main_t ct6_test_main;
58
59 #define __plugin_msg_base ct6_test_main.msg_id_base
60 #include <vlibapi/vat_helper_macros.h>
61
62 #define foreach_standard_reply_retval_handler   \
63 _(ct6_enable_disable_reply)
64
65 #define _(n)                                            \
66     static void vl_api_##n##_t_handler                  \
67     (vl_api_##n##_t * mp)                               \
68     {                                                   \
69         vat_main_t * vam = ct6_test_main.vat_main;   \
70         i32 retval = ntohl(mp->retval);                 \
71         if (vam->async_mode) {                          \
72             vam->async_errors += (retval < 0);          \
73         } else {                                        \
74             vam->retval = retval;                       \
75             vam->result_ready = 1;                      \
76         }                                               \
77     }
78 foreach_standard_reply_retval_handler;
79 #undef _
80
81 /*
82  * Table of message reply handlers, must include boilerplate handlers
83  * we just generated
84  */
85 #define foreach_vpe_api_reply_msg                                       \
86 _(CT6_ENABLE_DISABLE_REPLY, ct6_enable_disable_reply)
87
88
89 static int
90 api_ct6_enable_disable (vat_main_t * vam)
91 {
92   unformat_input_t *i = vam->input;
93   int enable_disable = 1;
94   u32 sw_if_index = ~0;
95   vl_api_ct6_enable_disable_t *mp;
96   u32 inside = ~0;
97   int ret;
98
99   /* Parse args required to build the message */
100   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
101     {
102       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
103         ;
104       else if (unformat (i, "sw_if_index %d", &sw_if_index))
105         ;
106       else if (unformat (i, "disable"))
107         enable_disable = 0;
108       else if (unformat (i, "inside") || unformat (i, "in"))
109         inside = 1;
110       else if (unformat (i, "outside") || unformat (i, "out"))
111         inside = 0;
112       else
113         break;
114     }
115
116   if (inside == ~0)
117     {
118       errmsg ("must specify inside or outside");
119       return -99;
120     }
121
122   if (sw_if_index == ~0)
123     {
124       errmsg ("missing interface name / explicit sw_if_index number \n");
125       return -99;
126     }
127
128   /* Construct the API message */
129   M (CT6_ENABLE_DISABLE, mp);
130   mp->sw_if_index = ntohl (sw_if_index);
131   mp->enable_disable = enable_disable;
132   mp->is_inside = (u8) inside;
133
134   /* send it... */
135   S (mp);
136
137   /* Wait for a reply... */
138   W (ret);
139   return ret;
140 }
141
142 /*
143  * List of messages that the api test plugin sends,
144  * and that the data plane plugin processes
145  */
146 #define foreach_vpe_api_msg \
147 _(ct6_enable_disable, "<intfc> [disable]")
148
149 static void
150 ct6_api_hookup (vat_main_t * vam)
151 {
152   ct6_test_main_t *ctmp = &ct6_test_main;
153   /* Hook up handlers for replies from the data plane plug-in */
154 #define _(N,n)                                                  \
155     vl_msg_api_set_handlers((VL_API_##N + ctmp->msg_id_base),     \
156                            #n,                                  \
157                            vl_api_##n##_t_handler,              \
158                            vl_noop_handler,                     \
159                            vl_api_##n##_t_endian,               \
160                            vl_api_##n##_t_print,                \
161                            sizeof(vl_api_##n##_t), 1);
162   foreach_vpe_api_reply_msg;
163 #undef _
164
165   /* API messages we can send */
166 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
167   foreach_vpe_api_msg;
168 #undef _
169
170   /* Help strings */
171 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
172   foreach_vpe_api_msg;
173 #undef _
174 }
175
176 clib_error_t *
177 vat_plugin_register (vat_main_t * vam)
178 {
179   ct6_test_main_t *ctmp = &ct6_test_main;
180   u8 *name;
181
182   ctmp->vat_main = vam;
183
184   /* Ask the vpp engine for the first assigned message-id */
185   name = format (0, "ct6_%08x%c", api_version, 0);
186   ctmp->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
187
188   if (ctmp->msg_id_base != (u16) ~ 0)
189     ct6_api_hookup (vam);
190
191   vec_free (name);
192
193   return 0;
194 }
195
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */