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