2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
21 #include <dhcp/client.h>
22 #include <dhcp/dhcp_proxy.h>
23 #include <vnet/ip/ip_format_fns.h>
24 #include <vnet/ethernet/ethernet_format_fns.h>
26 /* define message IDs */
27 #include <dhcp/dhcp.api_enum.h>
28 #include <dhcp/dhcp.api_types.h>
32 /* API message ID base */
37 dhcp_test_main_t dhcp_test_main;
39 #define __plugin_msg_base dhcp_test_main.msg_id_base
40 #include <vlibapi/vat_helper_macros.h>
42 /* Macro to finish up custom dump fns */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 vl_print (handle, (char *)s); \
51 api_dhcp_proxy_config (vat_main_t * vam)
53 unformat_input_t *i = vam->input;
54 vl_api_dhcp_proxy_config_t *mp;
56 u32 server_vrf_id = 0;
58 u8 v4_address_set = 0;
59 u8 v6_address_set = 0;
60 ip4_address_t v4address;
61 ip6_address_t v6address;
62 u8 v4_src_address_set = 0;
63 u8 v6_src_address_set = 0;
64 ip4_address_t v4srcaddress;
65 ip6_address_t v6srcaddress;
68 /* Parse args required to build the message */
69 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
71 if (unformat (i, "del"))
73 else if (unformat (i, "rx_vrf_id %d", &rx_vrf_id))
75 else if (unformat (i, "server_vrf_id %d", &server_vrf_id))
77 else if (unformat (i, "svr %U", unformat_ip4_address, &v4address))
79 else if (unformat (i, "svr %U", unformat_ip6_address, &v6address))
81 else if (unformat (i, "src %U", unformat_ip4_address, &v4srcaddress))
82 v4_src_address_set = 1;
83 else if (unformat (i, "src %U", unformat_ip6_address, &v6srcaddress))
84 v6_src_address_set = 1;
89 if (v4_address_set && v6_address_set)
91 errmsg ("both v4 and v6 server addresses set");
94 if (!v4_address_set && !v6_address_set)
96 errmsg ("no server addresses set");
100 if (v4_src_address_set && v6_src_address_set)
102 errmsg ("both v4 and v6 src addresses set");
105 if (!v4_src_address_set && !v6_src_address_set)
107 errmsg ("no src addresses set");
111 if (!(v4_src_address_set && v4_address_set) &&
112 !(v6_src_address_set && v6_address_set))
114 errmsg ("no matching server and src addresses set");
118 /* Construct the API message */
119 M (DHCP_PROXY_CONFIG, mp);
122 mp->rx_vrf_id = ntohl (rx_vrf_id);
123 mp->server_vrf_id = ntohl (server_vrf_id);
126 clib_memcpy (&mp->dhcp_server.un, &v6address, sizeof (v6address));
127 clib_memcpy (&mp->dhcp_src_address.un, &v6srcaddress,
132 clib_memcpy (&mp->dhcp_server.un, &v4address, sizeof (v4address));
133 clib_memcpy (&mp->dhcp_src_address.un, &v4srcaddress,
140 /* Wait for a reply, return good/bad news */
146 vl_api_dhcp_proxy_details_t_handler (vl_api_dhcp_proxy_details_t * mp)
148 vat_main_t *vam = &vat_main;
149 u32 i, count = mp->count;
150 vl_api_dhcp_server_t *s;
154 "RX Table-ID %d, Source Address %U, VSS Type %d, "
155 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
156 ntohl (mp->rx_vrf_id),
157 format_ip6_address, mp->dhcp_src_address,
158 mp->vss_type, mp->vss_vpn_ascii_id,
159 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
162 "RX Table-ID %d, Source Address %U, VSS Type %d, "
163 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
164 ntohl (mp->rx_vrf_id),
165 format_ip4_address, mp->dhcp_src_address,
166 mp->vss_type, mp->vss_vpn_ascii_id,
167 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
169 for (i = 0; i < count; i++)
175 " Server Table-ID %d, Server Address %U",
176 ntohl (s->server_vrf_id), format_ip6_address, s->dhcp_server);
179 " Server Table-ID %d, Server Address %U",
180 ntohl (s->server_vrf_id), format_ip4_address, s->dhcp_server);
185 api_dhcp_proxy_dump (vat_main_t * vam)
187 unformat_input_t *i = vam->input;
188 vl_api_dhcp_plugin_control_ping_t *mp_ping;
189 vl_api_dhcp_proxy_dump_t *mp;
193 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
195 if (unformat (i, "ipv6"))
199 clib_warning ("parse error '%U'", format_unformat_error, i);
204 M (DHCP_PROXY_DUMP, mp);
206 mp->is_ip6 = is_ipv6;
209 /* Use a control ping for synchronization */
210 MPING (DHCP_PLUGIN_CONTROL_PING, mp_ping);
218 api_dhcp_proxy_set_vss (vat_main_t * vam)
220 unformat_input_t *i = vam->input;
221 vl_api_dhcp_proxy_set_vss_t *mp;
225 u8 vss_type = VSS_TYPE_DEFAULT;
226 u8 *vpn_ascii_id = 0;
231 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
233 if (unformat (i, "tbl_id %d", &tbl_id))
235 else if (unformat (i, "vpn_ascii_id %s", &vpn_ascii_id))
236 vss_type = VSS_TYPE_ASCII;
237 else if (unformat (i, "fib_id %d", &fib_id))
238 vss_type = VSS_TYPE_VPN_ID;
239 else if (unformat (i, "oui %d", &oui))
240 vss_type = VSS_TYPE_VPN_ID;
241 else if (unformat (i, "ipv6"))
243 else if (unformat (i, "del"))
251 errmsg ("missing tbl_id ");
252 vec_free (vpn_ascii_id);
256 if ((vpn_ascii_id) && (vec_len (vpn_ascii_id) > 128))
258 errmsg ("vpn_ascii_id cannot be longer than 128 ");
259 vec_free (vpn_ascii_id);
263 M (DHCP_PROXY_SET_VSS, mp);
264 mp->tbl_id = ntohl (tbl_id);
265 mp->vss_type = vss_type;
268 clib_memcpy (mp->vpn_ascii_id, vpn_ascii_id, vec_len (vpn_ascii_id));
269 mp->vpn_ascii_id[vec_len (vpn_ascii_id)] = 0;
271 mp->vpn_index = ntohl (fib_id);
272 mp->oui = ntohl (oui);
273 mp->is_ipv6 = is_ipv6;
279 vec_free (vpn_ascii_id);
284 api_dhcp_client_config (vat_main_t * vam)
286 unformat_input_t *i = vam->input;
287 vl_api_dhcp_client_config_t *mp;
289 u8 sw_if_index_set = 0;
292 u8 disable_event = 0;
295 /* Parse args required to build the message */
296 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
298 if (unformat (i, "del"))
300 else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
302 else if (unformat (i, "sw_if_index %d", &sw_if_index))
304 else if (unformat (i, "hostname %s", &hostname))
306 else if (unformat (i, "disable_event"))
312 if (sw_if_index_set == 0)
314 errmsg ("missing interface name or sw_if_index");
318 if (vec_len (hostname) > 63)
320 errmsg ("hostname too long");
322 vec_add1 (hostname, 0);
324 /* Construct the API message */
325 M (DHCP_CLIENT_CONFIG, mp);
328 mp->client.sw_if_index = htonl (sw_if_index);
329 clib_memcpy (mp->client.hostname, hostname, vec_len (hostname));
331 mp->client.want_dhcp_event = disable_event ? 0 : 1;
332 mp->client.pid = htonl (getpid ());
337 /* Wait for a reply, return good/bad news */
343 api_want_dhcp6_reply_events (vat_main_t * vam)
349 api_want_dhcp6_pd_reply_events (vat_main_t * vam)
355 api_dhcp6_send_client_message (vat_main_t * vam)
361 api_dhcp6_pd_send_client_message (vat_main_t * vam)
367 vl_api_dhcp_client_details_t_handler (vl_api_dhcp_client_details_t * mp)
369 vat_main_t *vam = &vat_main;
370 vl_api_dhcp_client_t *cp;
371 vl_api_dhcp_lease_t *lp;
376 print (vam->ofp, "sw_if_index %d, id '%s'", ntohl (cp->sw_if_index),
379 print (vam->ofp, "leased address %U, router address %U",
380 format_ip4_address, &lp->host_address.un,
381 format_ip4_address, &lp->router_address.un);
385 api_dhcp_client_dump (vat_main_t * vam)
387 vl_api_dhcp_plugin_control_ping_t *mp_ping;
388 vl_api_dhcp_client_dump_t *mp;
391 M (DHCP_CLIENT_DUMP, mp);
395 /* Use a control ping for synchronization */
396 MPING (DHCP_PLUGIN_CONTROL_PING, mp_ping);
404 api_dhcp6_duid_ll_set (vat_main_t * vam)
410 api_dhcp6_clients_enable_disable (vat_main_t * vam)
416 api_dhcp_plugin_control_ping (vat_main_t * vam)
422 api_dhcp_plugin_get_version (vat_main_t * vam)
428 vl_api_dhcp_plugin_get_version_reply_t_handler
429 (vl_api_dhcp_plugin_get_version_reply_t * mp)
431 vat_main_t *vam = dhcp_test_main.vat_main;
432 clib_warning ("DHCP plugin version: %d.%d", ntohl (mp->major),
434 vam->result_ready = 1;
438 vl_api_dhcp_plugin_control_ping_reply_t_handler
439 (vl_api_dhcp_plugin_control_ping_reply_t * mp)
441 vat_main_t *vam = dhcp_test_main.vat_main;
442 i32 retval = ntohl (mp->retval);
445 vam->async_errors += (retval < 0);
449 vam->retval = retval;
450 vam->result_ready = 1;
454 #include <dhcp/dhcp.api_test.c>
457 * fd.io coding-style-patch-verification: ON
460 * eval: (c-set-style "gnu")