2 * mactime.c - skeleton vpp-api-test plug-in
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/time_range.h>
22 #include <vnet/ethernet/ethernet.h>
24 uword vat_unformat_sw_if_index (unformat_input_t * input, va_list * args);
26 /* Declare message IDs */
27 #include <mactime/mactime_msg_enum.h>
29 /* define message structures */
31 #include <mactime/mactime_all_api_h.h>
34 /* declare message handlers for each api */
36 #define vl_endianfun /* define message structures */
37 #include <mactime/mactime_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...)
43 #include <mactime/mactime_all_api_h.h>
46 /* Get the API version number. */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <mactime/mactime_all_api_h.h>
53 /* API message ID base */
56 } mactime_test_main_t;
58 mactime_test_main_t mactime_test_main;
60 #define __plugin_msg_base mactime_test_main.msg_id_base
61 #include <vlibapi/vat_helper_macros.h>
63 #define foreach_standard_reply_retval_handler \
64 _(mactime_enable_disable_reply) \
65 _(mactime_add_del_range_reply)
68 static void vl_api_##n##_t_handler \
69 (vl_api_##n##_t * mp) \
71 vat_main_t * vam = mactime_test_main.vat_main; \
72 i32 retval = ntohl(mp->retval); \
73 if (vam->async_mode) { \
74 vam->async_errors += (retval < 0); \
76 vam->retval = retval; \
77 vam->result_ready = 1; \
80 foreach_standard_reply_retval_handler;
84 * Table of message reply handlers, must include boilerplate handlers
87 #define foreach_vpe_api_reply_msg \
88 _(MACTIME_ENABLE_DISABLE_REPLY, mactime_enable_disable_reply) \
89 _(MACTIME_ADD_DEL_RANGE_REPLY, mactime_add_del_range_reply)
92 api_mactime_enable_disable (vat_main_t * vam)
94 unformat_input_t *i = vam->input;
95 int enable_disable = 1;
97 vl_api_mactime_enable_disable_t *mp;
100 /* Parse args required to build the message */
101 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
103 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
105 else if (unformat (i, "sw_if_index %d", &sw_if_index))
107 else if (unformat (i, "disable"))
113 if (sw_if_index == ~0)
115 errmsg ("missing interface name / explicit sw_if_index number \n");
119 /* Construct the API message */
120 M (MACTIME_ENABLE_DISABLE, mp);
121 mp->sw_if_index = ntohl (sw_if_index);
122 mp->enable_disable = enable_disable;
127 /* Wait for a reply... */
133 api_mactime_add_del_range (vat_main_t * vam)
135 unformat_input_t *i = vam->input;
136 vl_api_mactime_add_del_range_t *mp;
139 clib_timebase_range_t *rp = 0;
148 /* Parse args required to build the message */
149 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
151 if (unformat (i, "name %s", &device_name))
153 vec_add1 (device_name, 0);
156 else if (unformat (i, "allow-range %U",
157 unformat_clib_timebase_range_vector, &rp))
159 else if (unformat (i, "drop-range %U",
160 unformat_clib_timebase_range_vector, &rp))
162 else if (unformat (i, "allow-static"))
164 else if (unformat (i, "drop-static"))
166 else if (unformat (i, "mac %U", unformat_mac_address, mac_address))
168 else if (unformat (i, "del"))
178 vec_free (device_name);
179 errmsg ("mac address required, not set\n");
183 /* allow-range / drop-range parse errors cause this condition */
184 if (is_add && allow == 0 && drop == 0)
187 vec_free (device_name);
188 errmsg ("neither allow nor drop set, parse error...\n");
191 /* Unlikely, but check anyhow */
192 if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
195 vec_free (device_name);
196 errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
200 /* Cough up a device name if none set */
203 device_name = format (0, "mac %U%c", format_mac_address,
207 /* Construct the API message */
208 M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
212 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
213 memcpy (mp->device_name, device_name, vec_len (device_name));
214 mp->count = clib_host_to_net_u32 (vec_len (rp));
216 for (ii = 0; ii < vec_len (rp); ii++)
218 mp->ranges[ii].start = rp[ii].start;
219 mp->ranges[ii].end = rp[ii].end;
223 vec_free (device_name);
228 /* Wait for a reply... */
234 * List of messages that the api test plugin sends,
235 * and that the data plane plugin processes
237 #define foreach_vpe_api_msg \
238 _(mactime_enable_disable, "<intfc> [disable]") \
239 _(mactime_add_del_range, \
240 "name <devname> mac <mac-addr> allow drop\n" \
241 "allow-range Mon - Fri 9:00 - 17:00")
244 mactime_api_hookup (vat_main_t * vam)
246 mactime_test_main_t *sm = &mactime_test_main;
247 /* Hook up handlers for replies from the data plane plug-in */
249 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
251 vl_api_##n##_t_handler, \
253 vl_api_##n##_t_endian, \
254 vl_api_##n##_t_print, \
255 sizeof(vl_api_##n##_t), 1);
256 foreach_vpe_api_reply_msg;
259 /* API messages we can send */
260 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
265 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
271 vat_plugin_register (vat_main_t * vam)
273 mactime_test_main_t *sm = &mactime_test_main;
278 /* Ask the vpp engine for the first assigned message-id */
279 name = format (0, "mactime_%08x%c", api_version, 0);
280 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
282 if (sm->msg_id_base != (u16) ~ 0)
283 mactime_api_hookup (vam);
291 * fd.io coding-style-patch-verification: ON
294 * eval: (c-set-style "gnu")