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 */
145 #define vl_api_dhcp_proxy_details_t_endian vl_noop_handler
146 #define vl_api_dhcp_proxy_details_t_print vl_noop_handler
149 vl_api_dhcp_proxy_details_t_handler (vl_api_dhcp_proxy_details_t * mp)
151 vat_main_t *vam = &vat_main;
152 u32 i, count = mp->count;
153 vl_api_dhcp_server_t *s;
157 "RX Table-ID %d, Source Address %U, VSS Type %d, "
158 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
159 ntohl (mp->rx_vrf_id),
160 format_ip6_address, mp->dhcp_src_address,
161 mp->vss_type, mp->vss_vpn_ascii_id,
162 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
165 "RX Table-ID %d, Source Address %U, VSS Type %d, "
166 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
167 ntohl (mp->rx_vrf_id),
168 format_ip4_address, mp->dhcp_src_address,
169 mp->vss_type, mp->vss_vpn_ascii_id,
170 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
172 for (i = 0; i < count; i++)
178 " Server Table-ID %d, Server Address %U",
179 ntohl (s->server_vrf_id), format_ip6_address, s->dhcp_server);
182 " Server Table-ID %d, Server Address %U",
183 ntohl (s->server_vrf_id), format_ip4_address, s->dhcp_server);
188 api_dhcp_proxy_dump (vat_main_t * vam)
190 unformat_input_t *i = vam->input;
191 vl_api_dhcp_plugin_control_ping_t *mp_ping;
192 vl_api_dhcp_proxy_dump_t *mp;
196 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
198 if (unformat (i, "ipv6"))
202 clib_warning ("parse error '%U'", format_unformat_error, i);
207 M (DHCP_PROXY_DUMP, mp);
209 mp->is_ip6 = is_ipv6;
212 /* Use a control ping for synchronization */
213 MPING (DHCP_PLUGIN_CONTROL_PING, mp_ping);
221 api_dhcp_proxy_set_vss (vat_main_t * vam)
223 unformat_input_t *i = vam->input;
224 vl_api_dhcp_proxy_set_vss_t *mp;
228 u8 vss_type = VSS_TYPE_DEFAULT;
229 u8 *vpn_ascii_id = 0;
234 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
236 if (unformat (i, "tbl_id %d", &tbl_id))
238 else if (unformat (i, "vpn_ascii_id %s", &vpn_ascii_id))
239 vss_type = VSS_TYPE_ASCII;
240 else if (unformat (i, "fib_id %d", &fib_id))
241 vss_type = VSS_TYPE_VPN_ID;
242 else if (unformat (i, "oui %d", &oui))
243 vss_type = VSS_TYPE_VPN_ID;
244 else if (unformat (i, "ipv6"))
246 else if (unformat (i, "del"))
254 errmsg ("missing tbl_id ");
255 vec_free (vpn_ascii_id);
259 if ((vpn_ascii_id) && (vec_len (vpn_ascii_id) > 128))
261 errmsg ("vpn_ascii_id cannot be longer than 128 ");
262 vec_free (vpn_ascii_id);
266 M (DHCP_PROXY_SET_VSS, mp);
267 mp->tbl_id = ntohl (tbl_id);
268 mp->vss_type = vss_type;
271 clib_memcpy (mp->vpn_ascii_id, vpn_ascii_id, vec_len (vpn_ascii_id));
272 mp->vpn_ascii_id[vec_len (vpn_ascii_id)] = 0;
274 mp->vpn_index = ntohl (fib_id);
275 mp->oui = ntohl (oui);
276 mp->is_ipv6 = is_ipv6;
282 vec_free (vpn_ascii_id);
287 api_dhcp_client_config (vat_main_t * vam)
289 unformat_input_t *i = vam->input;
290 vl_api_dhcp_client_config_t *mp;
292 u8 sw_if_index_set = 0;
295 u8 disable_event = 0;
298 /* Parse args required to build the message */
299 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
301 if (unformat (i, "del"))
303 else if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
305 else if (unformat (i, "sw_if_index %d", &sw_if_index))
307 else if (unformat (i, "hostname %s", &hostname))
309 else if (unformat (i, "disable_event"))
315 if (sw_if_index_set == 0)
317 errmsg ("missing interface name or sw_if_index");
321 if (vec_len (hostname) > 63)
323 errmsg ("hostname too long");
325 vec_add1 (hostname, 0);
327 /* Construct the API message */
328 M (DHCP_CLIENT_CONFIG, mp);
331 mp->client.sw_if_index = htonl (sw_if_index);
332 clib_memcpy (mp->client.hostname, hostname, vec_len (hostname));
334 mp->client.want_dhcp_event = disable_event ? 0 : 1;
335 mp->client.pid = htonl (getpid ());
340 /* Wait for a reply, return good/bad news */
346 api_want_dhcp6_reply_events (vat_main_t * vam)
352 api_want_dhcp6_pd_reply_events (vat_main_t * vam)
358 api_dhcp6_send_client_message (vat_main_t * vam)
364 api_dhcp6_pd_send_client_message (vat_main_t * vam)
370 api_dhcp_client_dump (vat_main_t * vam)
376 api_dhcp6_duid_ll_set (vat_main_t * vam)
382 api_dhcp6_clients_enable_disable (vat_main_t * vam)
388 api_dhcp_plugin_control_ping (vat_main_t * vam)
394 api_dhcp_plugin_get_version (vat_main_t * vam)
399 #define vl_api_dhcp_client_details_t_handler vl_noop_handler
402 vl_api_dhcp_plugin_get_version_reply_t_handler
403 (vl_api_dhcp_plugin_get_version_reply_t * mp)
405 vat_main_t *vam = dhcp_test_main.vat_main;
406 clib_warning ("DHCP plugin version: %d.%d", ntohl (mp->major),
408 vam->result_ready = 1;
412 vl_api_dhcp_plugin_control_ping_reply_t_handler
413 (vl_api_dhcp_plugin_control_ping_reply_t * mp)
415 vat_main_t *vam = dhcp_test_main.vat_main;
416 i32 retval = ntohl (mp->retval);
419 vam->async_errors += (retval < 0);
423 vam->retval = retval;
424 vam->result_ready = 1;
428 #include <dhcp/dhcp.api_test.c>
431 * fd.io coding-style-patch-verification: ON
434 * eval: (c-set-style "gnu")