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>
44 vlib_cli_output (handle, (char *) s); \
49 api_dhcp_proxy_config (vat_main_t * vam)
51 unformat_input_t *i = vam->input;
52 vl_api_dhcp_proxy_config_t *mp;
54 u32 server_vrf_id = 0;
56 u8 v4_address_set = 0;
57 u8 v6_address_set = 0;
58 ip4_address_t v4address;
59 ip6_address_t v6address;
60 u8 v4_src_address_set = 0;
61 u8 v6_src_address_set = 0;
62 ip4_address_t v4srcaddress;
63 ip6_address_t v6srcaddress;
66 /* Parse args required to build the message */
67 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
69 if (unformat (i, "del"))
71 else if (unformat (i, "rx_vrf_id %d", &rx_vrf_id))
73 else if (unformat (i, "server_vrf_id %d", &server_vrf_id))
75 else if (unformat (i, "svr %U", unformat_ip4_address, &v4address))
77 else if (unformat (i, "svr %U", unformat_ip6_address, &v6address))
79 else if (unformat (i, "src %U", unformat_ip4_address, &v4srcaddress))
80 v4_src_address_set = 1;
81 else if (unformat (i, "src %U", unformat_ip6_address, &v6srcaddress))
82 v6_src_address_set = 1;
87 if (v4_address_set && v6_address_set)
89 errmsg ("both v4 and v6 server addresses set");
92 if (!v4_address_set && !v6_address_set)
94 errmsg ("no server addresses set");
98 if (v4_src_address_set && v6_src_address_set)
100 errmsg ("both v4 and v6 src addresses set");
103 if (!v4_src_address_set && !v6_src_address_set)
105 errmsg ("no src addresses set");
109 if (!(v4_src_address_set && v4_address_set) &&
110 !(v6_src_address_set && v6_address_set))
112 errmsg ("no matching server and src addresses set");
116 /* Construct the API message */
117 M (DHCP_PROXY_CONFIG, mp);
120 mp->rx_vrf_id = ntohl (rx_vrf_id);
121 mp->server_vrf_id = ntohl (server_vrf_id);
124 clib_memcpy (&mp->dhcp_server.un, &v6address, sizeof (v6address));
125 clib_memcpy (&mp->dhcp_src_address.un, &v6srcaddress,
130 clib_memcpy (&mp->dhcp_server.un, &v4address, sizeof (v4address));
131 clib_memcpy (&mp->dhcp_src_address.un, &v4srcaddress,
138 /* Wait for a reply, return good/bad news */
144 vl_api_dhcp_proxy_details_t_handler (vl_api_dhcp_proxy_details_t * mp)
146 vat_main_t *vam = &vat_main;
147 u32 i, count = mp->count;
148 vl_api_dhcp_server_t *s;
152 "RX Table-ID %d, Source Address %U, VSS Type %d, "
153 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
154 ntohl (mp->rx_vrf_id),
155 format_ip6_address, mp->dhcp_src_address,
156 mp->vss_type, mp->vss_vpn_ascii_id,
157 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
160 "RX Table-ID %d, Source Address %U, VSS Type %d, "
161 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
162 ntohl (mp->rx_vrf_id),
163 format_ip4_address, mp->dhcp_src_address,
164 mp->vss_type, mp->vss_vpn_ascii_id,
165 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
167 for (i = 0; i < count; i++)
173 " Server Table-ID %d, Server Address %U",
174 ntohl (s->server_vrf_id), format_ip6_address, s->dhcp_server);
177 " Server Table-ID %d, Server Address %U",
178 ntohl (s->server_vrf_id), format_ip4_address, s->dhcp_server);
183 api_dhcp_proxy_dump (vat_main_t * vam)
185 unformat_input_t *i = vam->input;
186 vl_api_dhcp_plugin_control_ping_t *mp_ping;
187 vl_api_dhcp_proxy_dump_t *mp;
191 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
193 if (unformat (i, "ipv6"))
197 clib_warning ("parse error '%U'", format_unformat_error, i);
202 M (DHCP_PROXY_DUMP, mp);
204 mp->is_ip6 = is_ipv6;
207 /* Use a control ping for synchronization */
208 MPING (DHCP_PLUGIN_CONTROL_PING, mp_ping);
216 api_dhcp_proxy_set_vss (vat_main_t * vam)
218 unformat_input_t *i = vam->input;
219 vl_api_dhcp_proxy_set_vss_t *mp;
223 u8 vss_type = VSS_TYPE_DEFAULT;
224 u8 *vpn_ascii_id = 0;
229 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
231 if (unformat (i, "tbl_id %d", &tbl_id))
233 else if (unformat (i, "vpn_ascii_id %s", &vpn_ascii_id))
234 vss_type = VSS_TYPE_ASCII;
235 else if (unformat (i, "fib_id %d", &fib_id))
236 vss_type = VSS_TYPE_VPN_ID;
237 else if (unformat (i, "oui %d", &oui))
238 vss_type = VSS_TYPE_VPN_ID;
239 else if (unformat (i, "ipv6"))
241 else if (unformat (i, "del"))
249 errmsg ("missing tbl_id ");
250 vec_free (vpn_ascii_id);
254 if ((vpn_ascii_id) && (vec_len (vpn_ascii_id) > 128))
256 errmsg ("vpn_ascii_id cannot be longer than 128 ");
257 vec_free (vpn_ascii_id);
261 M (DHCP_PROXY_SET_VSS, mp);
262 mp->tbl_id = ntohl (tbl_id);
263 mp->vss_type = vss_type;
266 clib_memcpy (mp->vpn_ascii_id, vpn_ascii_id, vec_len (vpn_ascii_id));
267 mp->vpn_ascii_id[vec_len (vpn_ascii_id)] = 0;
269 mp->vpn_index = ntohl (fib_id);
270 mp->oui = ntohl (oui);
271 mp->is_ipv6 = is_ipv6;
277 vec_free (vpn_ascii_id);
282 api_dhcp_client_config (vat_main_t * vam)
284 unformat_input_t *i = vam->input;
285 vl_api_dhcp_client_config_t *mp;
287 u8 sw_if_index_set = 0;
290 u8 disable_event = 0;
293 /* Parse args required to build the message */
294 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
296 if (unformat (i, "del"))
298 else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
300 else if (unformat (i, "sw_if_index %d", &sw_if_index))
302 else if (unformat (i, "hostname %s", &hostname))
304 else if (unformat (i, "disable_event"))
310 if (sw_if_index_set == 0)
312 errmsg ("missing interface name or sw_if_index");
316 if (vec_len (hostname) > 63)
318 errmsg ("hostname too long");
320 vec_add1 (hostname, 0);
322 /* Construct the API message */
323 M (DHCP_CLIENT_CONFIG, mp);
326 mp->client.sw_if_index = htonl (sw_if_index);
327 clib_memcpy (mp->client.hostname, hostname, vec_len (hostname));
329 mp->client.want_dhcp_event = disable_event ? 0 : 1;
330 mp->client.pid = htonl (getpid ());
335 /* Wait for a reply, return good/bad news */
341 api_want_dhcp6_reply_events (vat_main_t * vam)
347 api_want_dhcp6_pd_reply_events (vat_main_t * vam)
353 api_dhcp6_send_client_message (vat_main_t * vam)
359 api_dhcp6_pd_send_client_message (vat_main_t * vam)
365 vl_api_dhcp_client_details_t_handler (vl_api_dhcp_client_details_t * mp)
367 vat_main_t *vam = &vat_main;
368 vl_api_dhcp_client_t *cp;
369 vl_api_dhcp_lease_t *lp;
374 print (vam->ofp, "sw_if_index %d, id '%s'", ntohl (cp->sw_if_index),
377 print (vam->ofp, "leased address %U, router address %U",
378 format_ip4_address, &lp->host_address.un,
379 format_ip4_address, &lp->router_address.un);
383 api_dhcp_client_dump (vat_main_t * vam)
385 vl_api_dhcp_plugin_control_ping_t *mp_ping;
386 vl_api_dhcp_client_dump_t *mp;
389 M (DHCP_CLIENT_DUMP, mp);
393 /* Use a control ping for synchronization */
394 MPING (DHCP_PLUGIN_CONTROL_PING, mp_ping);
402 api_dhcp6_duid_ll_set (vat_main_t * vam)
408 api_dhcp6_clients_enable_disable (vat_main_t * vam)
414 api_dhcp_plugin_control_ping (vat_main_t * vam)
420 api_dhcp_plugin_get_version (vat_main_t * vam)
426 vl_api_dhcp_plugin_get_version_reply_t_handler
427 (vl_api_dhcp_plugin_get_version_reply_t * mp)
429 vat_main_t *vam = dhcp_test_main.vat_main;
430 clib_warning ("DHCP plugin version: %d.%d", ntohl (mp->major),
432 vam->result_ready = 1;
436 vl_api_dhcp_plugin_control_ping_reply_t_handler
437 (vl_api_dhcp_plugin_control_ping_reply_t * mp)
439 vat_main_t *vam = dhcp_test_main.vat_main;
440 i32 retval = ntohl (mp->retval);
443 vam->async_errors += (retval < 0);
447 vam->retval = retval;
448 vam->result_ready = 1;
452 #include <dhcp/dhcp.api_test.c>
455 * fd.io coding-style-patch-verification: ON
458 * eval: (c-set-style "gnu")