2 * dns.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>
22 #include <vnet/ip/ip.h>
24 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
26 /* Declare message IDs */
27 #include <dns/dns_msg_enum.h>
29 /* define message structures */
31 #include <dns/dns_all_api_h.h>
34 /* declare message handlers for each api */
36 #define vl_endianfun /* define message structures */
37 #include <dns/dns_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...)
43 #include <dns/dns_all_api_h.h>
46 /* Get the API version number. */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <dns/dns_all_api_h.h>
53 /* API message ID base */
58 dns_test_main_t dns_test_main;
60 #define __plugin_msg_base dns_test_main.msg_id_base
61 #include <vlibapi/vat_helper_macros.h>
63 #define foreach_standard_reply_retval_handler \
64 _(dns_enable_disable_reply) \
65 _(dns_name_server_add_del_reply)
68 static void vl_api_##n##_t_handler \
69 (vl_api_##n##_t * mp) \
71 vat_main_t * vam = dns_test_main.vat_main; \
72 i32 retval = (i32) clib_net_to_host_u32(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;
83 static void vl_api_dns_resolve_name_reply_t_handler
84 (vl_api_dns_resolve_name_reply_t * mp)
86 vat_main_t *vam = dns_test_main.vat_main;
87 i32 retval = (i32) clib_net_to_host_u32 (mp->retval);
91 clib_warning ("resolved: %U", format_ip4_address, mp->ip4_address);
93 clib_warning ("resolved: %U", format_ip6_address, mp->ip6_address);
96 vam->async_errors += (retval < 0);
100 vam->result_ready = 1;
104 static void vl_api_dns_resolve_ip_reply_t_handler
105 (vl_api_dns_resolve_ip_reply_t * mp)
107 vat_main_t *vam = dns_test_main.vat_main;
108 i32 retval = (i32) clib_net_to_host_u32 (mp->retval);
110 clib_warning ("resolved: %s", mp->name);
112 vam->async_errors += (retval < 0);
115 vam->retval = retval;
116 vam->result_ready = 1;
121 * Table of message reply handlers, must include boilerplate handlers
124 #define foreach_vpe_api_reply_msg \
125 _(DNS_ENABLE_DISABLE_REPLY, dns_enable_disable_reply) \
126 _(DNS_NAME_SERVER_ADD_DEL_REPLY, dns_name_server_add_del_reply) \
127 _(DNS_RESOLVE_NAME_REPLY, dns_resolve_name_reply) \
128 _(DNS_RESOLVE_IP_REPLY, dns_resolve_ip_reply)
131 api_dns_enable_disable (vat_main_t * vam)
133 vl_api_dns_enable_disable_t *mp;
134 unformat_input_t *i = vam->input;
138 /* Parse args required to build the message */
139 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
141 if (unformat (i, "disable"))
143 else if (unformat (i, "enable"))
149 /* Construct the API message */
150 M (DNS_ENABLE_DISABLE, mp);
156 /* Wait for a reply... */
162 * List of messages that the api test plugin sends,
163 * and that the data plane plugin processes
165 #define foreach_vpe_api_msg \
166 _(dns_enable_disable, "[enable][disable]") \
167 _(dns_name_server_add_del, "<ip-address> [del]") \
168 _(dns_resolve_name, "<hostname>") \
169 _(dns_resolve_ip, "<ip4|ip6>") \
170 _(dns_name_server_add_del, "<ip-address> [del]") \
171 _(dns_resolve_name, "<hostname>")
174 api_dns_resolve_name (vat_main_t * vam)
176 unformat_input_t *line_input = vam->input;
177 vl_api_dns_resolve_name_t *mp;
181 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
183 if (unformat (line_input, "%s", &name))
189 if (vec_len (name) > 127)
191 errmsg ("name too long");
195 /* Construct the API message */
196 M (DNS_RESOLVE_NAME, mp);
197 memcpy (mp->name, name, vec_len (name));
202 /* Wait for the reply */
208 api_dns_resolve_ip (vat_main_t * vam)
210 unformat_input_t *line_input = vam->input;
211 vl_api_dns_resolve_ip_t *mp;
217 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
219 if (unformat (line_input, "%U", unformat_ip6_address, &addr6))
221 else if (unformat (line_input, "%U", unformat_ip4_address, &addr4))
229 errmsg ("missing address");
233 /* Construct the API message */
234 M (DNS_RESOLVE_IP, mp);
237 memcpy (mp->address, &addr6, sizeof (addr6));
239 memcpy (mp->address, &addr4, sizeof (addr4));
243 /* Wait for the reply */
249 api_dns_name_server_add_del (vat_main_t * vam)
251 unformat_input_t *i = vam->input;
252 vl_api_dns_name_server_add_del_t *mp;
254 ip6_address_t ip6_server;
255 ip4_address_t ip4_server;
260 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
262 if (unformat (i, "%U", unformat_ip6_address, &ip6_server))
264 else if (unformat (i, "%U", unformat_ip4_address, &ip4_server))
266 else if (unformat (i, "del"))
270 clib_warning ("parse error '%U'", format_unformat_error, i);
275 if (ip4_set && ip6_set)
277 errmsg ("Only one server address allowed per message");
280 if ((ip4_set + ip6_set) == 0)
282 errmsg ("Server address required");
286 /* Construct the API message */
287 M (DNS_NAME_SERVER_ADD_DEL, mp);
291 memcpy (mp->server_address, &ip6_server, sizeof (ip6_address_t));
296 memcpy (mp->server_address, &ip4_server, sizeof (ip4_address_t));
305 /* Wait for a reply, return good/bad news */
311 dns_api_hookup (vat_main_t * vam)
313 dns_test_main_t *dtmp = &dns_test_main;
314 /* Hook up handlers for replies from the data plane plug-in */
316 vl_msg_api_set_handlers((VL_API_##N + dtmp->msg_id_base), \
318 vl_api_##n##_t_handler, \
320 vl_api_##n##_t_endian, \
321 vl_api_##n##_t_print, \
322 sizeof(vl_api_##n##_t), 1);
323 foreach_vpe_api_reply_msg;
326 /* API messages we can send */
327 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
332 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
337 VAT_PLUGIN_REGISTER (dns);
341 * fd.io coding-style-patch-verification: ON
344 * eval: (c-set-style "gnu")