2 *------------------------------------------------------------------
3 * dhcp_api.c - dhcp api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/dhcp/proxy.h>
26 #include <vnet/dhcp/client.h>
27 #include <vnet/dhcpv6/proxy.h>
29 #include <vnet/vnet_msg_enum.h>
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include <vnet/vnet_all_api_h.h>
45 #include <vlibapi/api_helper_macros.h>
47 #define foreach_vpe_api_msg \
48 _(DHCP_PROXY_CONFIG,dhcp_proxy_config) \
49 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2) \
50 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
51 _(DHCP_CLIENT_CONFIG, dhcp_client_config)
54 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
56 vl_api_dhcp_proxy_config_reply_t *rmp;
59 rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
60 (ip4_address_t *) (&mp->dhcp_src_address),
61 (u32) ntohl (mp->vrf_id),
62 (int) mp->insert_circuit_id,
63 (int) (mp->is_add == 0));
65 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
70 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
72 vl_api_dhcp_proxy_config_reply_t *rmp;
75 rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
76 (ip6_address_t *) (&mp->dhcp_src_address),
77 (u32) ntohl (mp->vrf_id),
78 (int) mp->insert_circuit_id,
79 (int) (mp->is_add == 0));
81 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
85 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
87 vl_api_dhcp_proxy_config_reply_t *rmp;
90 rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
91 (ip4_address_t *) (&mp->dhcp_src_address),
92 (u32) ntohl (mp->rx_vrf_id),
93 (u32) ntohl (mp->server_vrf_id),
94 (int) mp->insert_circuit_id,
95 (int) (mp->is_add == 0));
97 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
102 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
104 vl_api_dhcp_proxy_config_reply_t *rmp;
107 rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
108 (ip6_address_t *) (&mp->dhcp_src_address),
109 (u32) ntohl (mp->rx_vrf_id),
110 (u32) ntohl (mp->server_vrf_id),
111 (int) mp->insert_circuit_id,
112 (int) (mp->is_add == 0));
114 REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
119 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
121 vl_api_dhcp_proxy_set_vss_reply_t *rmp;
124 rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
127 (int) mp->is_add == 0);
129 rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
131 ntohl (mp->fib_id), (int) mp->is_add == 0);
133 REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
137 static void vl_api_dhcp_proxy_config_t_handler
138 (vl_api_dhcp_proxy_config_t * mp)
140 if (mp->is_ipv6 == 0)
141 dhcpv4_proxy_config (mp);
143 dhcpv6_proxy_config (mp);
147 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
148 u8 is_ipv6, u8 * host_address, u8 * router_address,
151 unix_shared_memory_queue_t *q;
152 vl_api_dhcp_compl_event_t *mp;
154 q = vl_api_client_index_to_input_queue (client_index);
158 mp = vl_msg_api_alloc (sizeof (*mp));
159 mp->client_index = client_index;
161 mp->is_ipv6 = is_ipv6;
162 clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
163 mp->hostname[vec_len (hostname) + 1] = '\n';
164 clib_memcpy (&mp->host_address[0], host_address, 16);
165 clib_memcpy (&mp->router_address[0], router_address, 16);
167 if (NULL != host_mac)
168 clib_memcpy (&mp->host_mac[0], host_mac, 6);
170 mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
172 vl_msg_api_send_shmem (q, (u8 *) & mp);
175 static void vl_api_dhcp_proxy_config_2_t_handler
176 (vl_api_dhcp_proxy_config_2_t * mp)
178 if (mp->is_ipv6 == 0)
179 dhcpv4_proxy_config_2 (mp);
181 dhcpv6_proxy_config_2 (mp);
184 static void vl_api_dhcp_client_config_t_handler
185 (vl_api_dhcp_client_config_t * mp)
187 vlib_main_t *vm = vlib_get_main ();
188 vl_api_dhcp_client_config_reply_t *rmp;
191 VALIDATE_SW_IF_INDEX (mp);
193 rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
194 mp->hostname, mp->is_add, mp->client_index,
195 mp->want_dhcp_event ? dhcp_compl_event_callback :
198 BAD_SW_IF_INDEX_LABEL;
200 REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
205 * Add vpe's API message handlers to the table.
206 * vlib has alread mapped shared memory and
207 * added the client registration handlers.
208 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
210 #define vl_msg_name_crc_list
211 #include <vnet/vnet_all_api_h.h>
212 #undef vl_msg_name_crc_list
215 setup_message_id_table (api_main_t * am)
217 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
218 foreach_vl_msg_name_crc_dhcp;
222 static clib_error_t *
223 dhcp_api_hookup (vlib_main_t * vm)
225 api_main_t *am = &api_main;
228 vl_msg_api_set_handlers(VL_API_##N, #n, \
229 vl_api_##n##_t_handler, \
231 vl_api_##n##_t_endian, \
232 vl_api_##n##_t_print, \
233 sizeof(vl_api_##n##_t), 1);
238 * Set up the (msg_name, crc, message-id) table
240 setup_message_id_table (am);
245 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
248 * fd.io coding-style-patch-verification: ON
251 * eval: (c-set-style "gnu")