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>
31 /* API message ID base */
36 dhcp_test_main_t dhcp_test_main;
38 #define __plugin_msg_base dhcp_test_main.msg_id_base
39 #include <vlibapi/vat_helper_macros.h>
41 /* Macro to finish up custom dump fns */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 vl_print (handle, (char *)s); \
50 api_dhcp_proxy_config (vat_main_t * vam)
52 unformat_input_t *i = vam->input;
53 vl_api_dhcp_proxy_config_t *mp;
55 u32 server_vrf_id = 0;
57 u8 v4_address_set = 0;
58 u8 v6_address_set = 0;
59 ip4_address_t v4address;
60 ip6_address_t v6address;
61 u8 v4_src_address_set = 0;
62 u8 v6_src_address_set = 0;
63 ip4_address_t v4srcaddress;
64 ip6_address_t v6srcaddress;
67 /* Parse args required to build the message */
68 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
70 if (unformat (i, "del"))
72 else if (unformat (i, "rx_vrf_id %d", &rx_vrf_id))
74 else if (unformat (i, "server_vrf_id %d", &server_vrf_id))
76 else if (unformat (i, "svr %U", unformat_ip4_address, &v4address))
78 else if (unformat (i, "svr %U", unformat_ip6_address, &v6address))
80 else if (unformat (i, "src %U", unformat_ip4_address, &v4srcaddress))
81 v4_src_address_set = 1;
82 else if (unformat (i, "src %U", unformat_ip6_address, &v6srcaddress))
83 v6_src_address_set = 1;
88 if (v4_address_set && v6_address_set)
90 errmsg ("both v4 and v6 server addresses set");
93 if (!v4_address_set && !v6_address_set)
95 errmsg ("no server addresses set");
99 if (v4_src_address_set && v6_src_address_set)
101 errmsg ("both v4 and v6 src addresses set");
104 if (!v4_src_address_set && !v6_src_address_set)
106 errmsg ("no src addresses set");
110 if (!(v4_src_address_set && v4_address_set) &&
111 !(v6_src_address_set && v6_address_set))
113 errmsg ("no matching server and src addresses set");
117 /* Construct the API message */
118 M (DHCP_PROXY_CONFIG, mp);
121 mp->rx_vrf_id = ntohl (rx_vrf_id);
122 mp->server_vrf_id = ntohl (server_vrf_id);
125 clib_memcpy (&mp->dhcp_server.un, &v6address, sizeof (v6address));
126 clib_memcpy (&mp->dhcp_src_address.un, &v6srcaddress,
131 clib_memcpy (&mp->dhcp_server.un, &v4address, sizeof (v4address));
132 clib_memcpy (&mp->dhcp_src_address.un, &v4srcaddress,
139 /* Wait for a reply, return good/bad news */
144 #define vl_api_dhcp_proxy_details_t_endian vl_noop_handler
145 #define vl_api_dhcp_proxy_details_t_print vl_noop_handler
148 vl_api_dhcp_proxy_details_t_handler (vl_api_dhcp_proxy_details_t * mp)
150 vat_main_t *vam = &vat_main;
151 u32 i, count = mp->count;
152 vl_api_dhcp_server_t *s;
156 "RX Table-ID %d, Source Address %U, VSS Type %d, "
157 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
158 ntohl (mp->rx_vrf_id),
159 format_ip6_address, mp->dhcp_src_address,
160 mp->vss_type, mp->vss_vpn_ascii_id,
161 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
164 "RX Table-ID %d, Source Address %U, VSS Type %d, "
165 "VSS ASCII VPN-ID '%s', VSS RFC2685 VPN-ID (oui:id) %d:%d",
166 ntohl (mp->rx_vrf_id),
167 format_ip4_address, mp->dhcp_src_address,
168 mp->vss_type, mp->vss_vpn_ascii_id,
169 ntohl (mp->vss_oui), ntohl (mp->vss_fib_id));
171 for (i = 0; i < count; i++)
177 " Server Table-ID %d, Server Address %U",
178 ntohl (s->server_vrf_id), format_ip6_address, s->dhcp_server);
181 " Server Table-ID %d, Server Address %U",
182 ntohl (s->server_vrf_id), format_ip4_address, s->dhcp_server);
187 api_dhcp_proxy_dump (vat_main_t * vam)
189 unformat_input_t *i = vam->input;
190 vl_api_dhcp_plugin_control_ping_t *mp_ping;
191 vl_api_dhcp_proxy_dump_t *mp;
195 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
197 if (unformat (i, "ipv6"))
201 clib_warning ("parse error '%U'", format_unformat_error, i);
206 M (DHCP_PROXY_DUMP, mp);
208 mp->is_ip6 = is_ipv6;
211 /* Use a control ping for synchronization */
212 MPING (DHCP_PLUGIN_CONTROL_PING, mp_ping);
220 api_dhcp_proxy_set_vss (vat_main_t * vam)
222 unformat_input_t *i = vam->input;
223 vl_api_dhcp_proxy_set_vss_t *mp;
227 u8 vss_type = VSS_TYPE_DEFAULT;
228 u8 *vpn_ascii_id = 0;
233 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
235 if (unformat (i, "tbl_id %d", &tbl_id))
237 else if (unformat (i, "vpn_ascii_id %s", &vpn_ascii_id))
238 vss_type = VSS_TYPE_ASCII;
239 else if (unformat (i, "fib_id %d", &fib_id))
240 vss_type = VSS_TYPE_VPN_ID;
241 else if (unformat (i, "oui %d", &oui))
242 vss_type = VSS_TYPE_VPN_ID;
243 else if (unformat (i, "ipv6"))
245 else if (unformat (i, "del"))
253 errmsg ("missing tbl_id ");
254 vec_free (vpn_ascii_id);
258 if ((vpn_ascii_id) && (vec_len (vpn_ascii_id) > 128))
260 errmsg ("vpn_ascii_id cannot be longer than 128 ");
261 vec_free (vpn_ascii_id);
265 M (DHCP_PROXY_SET_VSS, mp);
266 mp->tbl_id = ntohl (tbl_id);
267 mp->vss_type = vss_type;
270 clib_memcpy (mp->vpn_ascii_id, vpn_ascii_id, vec_len (vpn_ascii_id));
271 mp->vpn_ascii_id[vec_len (vpn_ascii_id)] = 0;
273 mp->vpn_index = ntohl (fib_id);
274 mp->oui = ntohl (oui);
275 mp->is_ipv6 = is_ipv6;
281 vec_free (vpn_ascii_id);
286 api_dhcp_client_config (vat_main_t * vam)
288 unformat_input_t *i = vam->input;
289 vl_api_dhcp_client_config_t *mp;
291 u8 sw_if_index_set = 0;
294 u8 disable_event = 0;
297 /* Parse args required to build the message */
298 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
300 if (unformat (i, "del"))
303 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 */
345 /* static void *vl_api_dhcp_proxy_config_t_print */
346 /* (vl_api_dhcp_proxy_config_t * mp, void *handle) */
350 /* s = format (0, "SCRIPT: dhcp_proxy_config_2 "); */
352 /* s = format (s, "rx_vrf_id %d ", (mp->rx_vrf_id)); */
353 /* s = format (s, "server_vrf_id %d ", (mp->server_vrf_id)); */
355 /* s = format (s, "svr %U ", format_ip46_address, */
356 /* (ip46_address_t *) & mp->dhcp_server.un); */
357 /* s = format (s, "src %U ", format_ip46_address, */
358 /* (ip46_address_t *) & mp->dhcp_src_address.un); */
360 /* if (mp->is_add == 0) */
361 /* s = format (s, "del "); */
366 /* static void *vl_api_dhcp_proxy_set_vss_t_print */
367 /* (vl_api_dhcp_proxy_set_vss_t * mp, void *handle) */
371 /* s = format (0, "SCRIPT: dhcp_proxy_set_vss "); */
373 /* s = format (s, "tbl_id %d ", (mp->tbl_id)); */
375 /* if (mp->vss_type == VSS_TYPE_VPN_ID) */
377 /* s = format (s, "fib_id %d ", (mp->vpn_index)); */
378 /* s = format (s, "oui %d ", (mp->oui)); */
380 /* else if (mp->vss_type == VSS_TYPE_ASCII) */
381 /* s = format (s, "vpn_ascii_id %s", mp->vpn_ascii_id); */
383 /* if (mp->is_ipv6 != 0) */
384 /* s = format (s, "ipv6 "); */
386 /* if (mp->is_add == 0) */
387 /* s = format (s, "del "); */
392 /* static void *vl_api_dhcp_client_config_t_print */
393 /* (vl_api_dhcp_client_config_t * mp, void *handle) */
397 /* s = format (0, "SCRIPT: dhcp_client_config "); */
399 /* s = format (s, "sw_if_index %d ", (mp->client.sw_if_index)); */
401 /* s = format (s, "hostname %s ", mp->client.hostname); */
403 /* s = format (s, "want_dhcp_event %d ", mp->client.want_dhcp_event); */
405 /* s = format (s, "pid %d ", (mp->client.pid)); */
407 /* if (mp->is_add == 0) */
408 /* s = format (s, "del "); */
414 api_want_dhcp6_reply_events (vat_main_t * vam)
419 api_want_dhcp6_pd_reply_events (vat_main_t * vam)
424 api_dhcp6_send_client_message (vat_main_t * vam)
429 api_dhcp6_pd_send_client_message (vat_main_t * vam)
434 api_dhcp_client_dump (vat_main_t * vam)
439 api_dhcp6_duid_ll_set (vat_main_t * vam)
444 api_dhcp6_clients_enable_disable (vat_main_t * vam)
449 api_dhcp_plugin_control_ping (vat_main_t * vam)
454 api_dhcp_plugin_get_version (vat_main_t * vam)
459 #define vl_api_dhcp_client_details_t_handler vl_noop_handler
462 vl_api_dhcp_plugin_get_version_reply_t_handler (vl_api_dhcp_plugin_get_version_reply_t * mp)
467 vl_api_dhcp_plugin_control_ping_reply_t_handler (vl_api_dhcp_plugin_get_version_reply_t * mp)
472 /* vl_api_dhcp_compl_event_t_handler (vl_api_dhcp_compl_event_t * mp) */
476 /* s = format (0, "DHCP compl event: pid %d hostname %s host_addr %U " */
477 /* "host_mac %U router_addr %U", */
478 /* ntohl (mp->pid), mp->lease.hostname, */
479 /* format_ip4_address, mp->lease.host_address, */
480 /* format_ethernet_address, mp->lease.host_mac, */
481 /* format_ip4_address, mp->lease.router_address); */
483 /* for (i = 0; i < mp->lease.count; i++) */
485 /* format (s, " domain_server_addr %U", format_ip4_address, */
486 /* mp->lease.domain_server[i].address); */
488 /* errmsg ((char *) s); */
492 #include <dhcp/dhcp.api_test.c>