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 /* Declare message IDs */
25 #include <mactime/mactime_msg_enum.h>
27 /* define message structures */
29 #include <mactime/mactime_all_api_h.h>
32 /* declare message handlers for each api */
34 #define vl_endianfun /* define message structures */
35 #include <mactime/mactime_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
41 #include <mactime/mactime_all_api_h.h>
44 /* Get the API version number. */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <mactime/mactime_all_api_h.h>
51 /* API message ID base */
54 } mactime_test_main_t;
56 mactime_test_main_t mactime_test_main;
58 #define __plugin_msg_base mactime_test_main.msg_id_base
59 #include <vlibapi/vat_helper_macros.h>
61 #define foreach_standard_reply_retval_handler \
62 _(mactime_enable_disable_reply) \
63 _(mactime_add_del_range_reply)
66 static void vl_api_##n##_t_handler \
67 (vl_api_##n##_t * mp) \
69 vat_main_t * vam = mactime_test_main.vat_main; \
70 i32 retval = ntohl(mp->retval); \
71 if (vam->async_mode) { \
72 vam->async_errors += (retval < 0); \
74 vam->retval = retval; \
75 vam->result_ready = 1; \
78 foreach_standard_reply_retval_handler;
82 * Table of message reply handlers, must include boilerplate handlers
85 #define foreach_vpe_api_reply_msg \
86 _(MACTIME_ENABLE_DISABLE_REPLY, mactime_enable_disable_reply) \
87 _(MACTIME_ADD_DEL_RANGE_REPLY, mactime_add_del_range_reply)
90 api_mactime_enable_disable (vat_main_t * vam)
92 unformat_input_t *i = vam->input;
93 int enable_disable = 1;
95 vl_api_mactime_enable_disable_t *mp;
98 /* Parse args required to build the message */
99 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
101 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
103 else if (unformat (i, "sw_if_index %d", &sw_if_index))
105 else if (unformat (i, "disable"))
111 if (sw_if_index == ~0)
113 errmsg ("missing interface name / explicit sw_if_index number \n");
117 /* Construct the API message */
118 M (MACTIME_ENABLE_DISABLE, mp);
119 mp->sw_if_index = ntohl (sw_if_index);
120 mp->enable_disable = enable_disable;
125 /* Wait for a reply... */
130 /* These two ought to be in a library somewhere but they aren't */
132 my_unformat_mac_address (unformat_input_t * input, va_list * args)
134 u8 *a = va_arg (*args, u8 *);
135 return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
140 my_format_mac_address (u8 * s, va_list * args)
142 u8 *a = va_arg (*args, u8 *);
143 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
144 a[0], a[1], a[2], a[3], a[4], a[5]);
148 api_mactime_add_del_range (vat_main_t * vam)
150 unformat_input_t *i = vam->input;
151 vl_api_mactime_add_del_range_t *mp;
154 clib_timebase_range_t *rp = 0;
166 /* Parse args required to build the message */
167 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
169 if (unformat (i, "name %s", &device_name))
171 vec_add1 (device_name, 0);
174 else if (unformat (i, "allow-range %U",
175 unformat_clib_timebase_range_vector, &rp))
177 else if (unformat (i, "allow-quota-range %U",
178 unformat_clib_timebase_range_vector, &rp))
180 else if (unformat (i, "drop-range %U",
181 unformat_clib_timebase_range_vector, &rp))
183 else if (unformat (i, "allow-static"))
185 else if (unformat (i, "drop-static"))
187 else if (unformat (i, "no-udp-10001"))
189 else if (unformat (i, "mac %U", my_unformat_mac_address, mac_address))
191 else if (unformat (i, "del"))
193 else if (unformat (i, "data-quota %lldM", &data_quota))
195 else if (unformat (i, "data-quota %lldG", &data_quota))
205 vec_free (device_name);
206 errmsg ("mac address required, not set\n");
210 /* allow-range / drop-range parse errors cause this condition */
211 if (is_add && allow == 0 && drop == 0 && allow_quota == 0)
214 vec_free (device_name);
215 errmsg ("parse error...\n");
219 /* Unlikely, but check anyhow */
220 if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
223 vec_free (device_name);
224 errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
228 /* Cough up a device name if none set */
231 device_name = format (0, "mac %U%c", my_format_mac_address,
235 /* Construct the API message */
236 M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
240 mp->allow_quota = allow_quota;
241 mp->no_udp_10001 = no_udp_10001;
242 mp->data_quota = clib_host_to_net_u64 (data_quota);
243 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
244 memcpy (mp->device_name, device_name, vec_len (device_name));
245 mp->count = clib_host_to_net_u32 (vec_len (rp));
247 for (ii = 0; ii < vec_len (rp); ii++)
249 mp->ranges[ii].start = rp[ii].start;
250 mp->ranges[ii].end = rp[ii].end;
254 vec_free (device_name);
259 /* Wait for a reply... */
265 * List of messages that the api test plugin sends,
266 * and that the data plane plugin processes
268 #define foreach_vpe_api_msg \
269 _(mactime_enable_disable, "<intfc> [disable]") \
270 _(mactime_add_del_range, \
271 "name <devname> mac <mac-addr> allow drop\n" \
272 "allow-range Mon - Fri 9:00 - 17:00")
275 mactime_api_hookup (vat_main_t * vam)
277 mactime_test_main_t *sm = &mactime_test_main;
278 /* Hook up handlers for replies from the data plane plug-in */
280 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
282 vl_api_##n##_t_handler, \
284 vl_api_##n##_t_endian, \
285 vl_api_##n##_t_print, \
286 sizeof(vl_api_##n##_t), 1);
287 foreach_vpe_api_reply_msg;
290 /* API messages we can send */
291 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
296 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
301 VAT_PLUGIN_REGISTER (mactime);
304 * fd.io coding-style-patch-verification: ON
307 * eval: (c-set-style "gnu")