972a3b073a8eb5e24c2ff96fe521443d58f026d5
[vpp.git] / src / plugins / flowperpkt / flowperpkt_test.c
1 /*
2  * flowperpkt.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 <vlibsocket/api.h>
21 #include <vppinfra/error.h>
22
23 #define __plugin_msg_base flowperpkt_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
25
26 /**
27  * @file vpp_api_test plugin
28  */
29
30 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
31
32 /* Declare message IDs */
33 #include <flowperpkt/flowperpkt_msg_enum.h>
34
35 /* define message structures */
36 #define vl_typedefs
37 #include <flowperpkt/flowperpkt_all_api_h.h>
38 #undef vl_typedefs
39
40 /* declare message handlers for each api */
41
42 #define vl_endianfun            /* define message structures */
43 #include <flowperpkt/flowperpkt_all_api_h.h>
44 #undef vl_endianfun
45
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...)
48 #define vl_printfun
49 #include <flowperpkt/flowperpkt_all_api_h.h>
50 #undef vl_printfun
51
52 /* Get the API version number. */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <flowperpkt/flowperpkt_all_api_h.h>
55 #undef vl_api_version
56
57 typedef struct
58 {
59     /** API message ID base */
60   u16 msg_id_base;
61     /** vat_main_t pointer */
62   vat_main_t *vat_main;
63 } flowperpkt_test_main_t;
64
65 flowperpkt_test_main_t flowperpkt_test_main;
66
67 #define foreach_standard_reply_retval_handler   \
68 _(flowperpkt_tx_interface_add_del_reply)
69
70 #define _(n)                                            \
71     static void vl_api_##n##_t_handler                  \
72     (vl_api_##n##_t * mp)                               \
73     {                                                   \
74         vat_main_t * vam = flowperpkt_test_main.vat_main;   \
75         i32 retval = ntohl(mp->retval);                 \
76         if (vam->async_mode) {                          \
77             vam->async_errors += (retval < 0);          \
78         } else {                                        \
79             vam->retval = retval;                       \
80             vam->result_ready = 1;                      \
81         }                                               \
82     }
83 foreach_standard_reply_retval_handler;
84 #undef _
85
86 /*
87  * Table of message reply handlers, must include boilerplate handlers
88  * we just generated
89  */
90 #define foreach_vpe_api_reply_msg               \
91 _(FLOWPERPKT_TX_INTERFACE_ADD_DEL_REPLY,        \
92   flowperpkt_tx_interface_add_del_reply)
93
94 static int
95 api_flowperpkt_tx_interface_add_del (vat_main_t * vam)
96 {
97   unformat_input_t *i = vam->input;
98   int enable_disable = 1;
99   u8 which = 0;                 /* ipv4 by default */
100   u32 sw_if_index = ~0;
101   vl_api_flowperpkt_tx_interface_add_del_t *mp;
102   int ret;
103
104   /* Parse args required to build the message */
105   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
106     {
107       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
108         ;
109       else if (unformat (i, "sw_if_index %d", &sw_if_index))
110         ;
111       else if (unformat (i, "disable"))
112         enable_disable = 0;
113       else if (unformat (i, "l2"))
114         which = 1;
115       else
116         break;
117     }
118
119   if (sw_if_index == ~0)
120     {
121       errmsg ("missing interface name / explicit sw_if_index number \n");
122       return -99;
123     }
124
125   /* Construct the API message */
126   M (FLOWPERPKT_TX_INTERFACE_ADD_DEL, mp);
127   mp->sw_if_index = ntohl (sw_if_index);
128   mp->is_add = enable_disable;
129   mp->which = which;
130
131   /* send it... */
132   S (mp);
133
134   /* Wait for a reply... */
135   W (ret);
136   return ret;
137 }
138
139 /*
140  * List of messages that the api test plugin sends,
141  * and that the data plane plugin processes
142  */
143 #define foreach_vpe_api_msg \
144 _(flowperpkt_tx_interface_add_del, "<intfc> [disable]")
145
146 static void
147 flowperpkt_vat_api_hookup (vat_main_t * vam)
148 {
149   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
150   /* Hook up handlers for replies from the data plane plug-in */
151 #define _(N,n)                                                  \
152     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
153                            #n,                                  \
154                            vl_api_##n##_t_handler,              \
155                            vl_noop_handler,                     \
156                            vl_api_##n##_t_endian,               \
157                            vl_api_##n##_t_print,                \
158                            sizeof(vl_api_##n##_t), 1);
159   foreach_vpe_api_reply_msg;
160 #undef _
161
162   /* API messages we can send */
163 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
164   foreach_vpe_api_msg;
165 #undef _
166
167   /* Help strings */
168 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
169   foreach_vpe_api_msg;
170 #undef _
171 }
172
173 clib_error_t *
174 vat_plugin_register (vat_main_t * vam)
175 {
176   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
177   u8 *name;
178
179   sm->vat_main = vam;
180
181   /* Ask the vpp engine for the first assigned message-id */
182   name = format (0, "flowperpkt_%08x%c", api_version, 0);
183   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
184
185   /* Don't attempt to hook up API messages if the data plane plugin is AWOL */
186   if (sm->msg_id_base != (u16) ~ 0)
187     flowperpkt_vat_api_hookup (vam);
188
189   vec_free (name);
190
191   return 0;
192 }
193
194 /*
195  * fd.io coding-style-patch-verification: ON
196  *
197  * Local Variables:
198  * eval: (c-set-style "gnu")
199  * End:
200  */