API refactoring : dhcp
[vpp.git] / src / vnet / dhcp / dhcp_api.c
1 /*
2  *------------------------------------------------------------------
3  * dhcp_api.c - dhcp api
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
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>
28
29 #include <vnet/vnet_msg_enum.h>
30
31 #define vl_typedefs             /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_typedefs
34
35 #define vl_endianfun            /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_printfun
44
45 #include <vlibapi/api_helper_macros.h>
46
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)
52
53 static void
54 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
55 {
56   vl_api_dhcp_proxy_config_reply_t *rmp;
57   int rv;
58
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));
64
65   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
66 }
67
68
69 static void
70 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
71 {
72   vl_api_dhcp_proxy_config_reply_t *rmp;
73   int rv = -1;
74
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));
80
81   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
82 }
83
84 static void
85 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
86 {
87   vl_api_dhcp_proxy_config_reply_t *rmp;
88   int rv;
89
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));
96
97   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
98 }
99
100
101 static void
102 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
103 {
104   vl_api_dhcp_proxy_config_reply_t *rmp;
105   int rv = -1;
106
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));
113
114   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
115 }
116
117
118 static void
119 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
120 {
121   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
122   int rv;
123   if (!mp->is_ipv6)
124     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
125                                       ntohl (mp->oui),
126                                       ntohl (mp->fib_id),
127                                       (int) mp->is_add == 0);
128   else
129     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
130                                ntohl (mp->oui),
131                                ntohl (mp->fib_id), (int) mp->is_add == 0);
132
133   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
134 }
135
136
137 static void vl_api_dhcp_proxy_config_t_handler
138   (vl_api_dhcp_proxy_config_t * mp)
139 {
140   if (mp->is_ipv6 == 0)
141     dhcpv4_proxy_config (mp);
142   else
143     dhcpv6_proxy_config (mp);
144 }
145
146 void
147 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
148                            u8 is_ipv6, u8 * host_address, u8 * router_address,
149                            u8 * host_mac)
150 {
151   unix_shared_memory_queue_t *q;
152   vl_api_dhcp_compl_event_t *mp;
153
154   q = vl_api_client_index_to_input_queue (client_index);
155   if (!q)
156     return;
157
158   mp = vl_msg_api_alloc (sizeof (*mp));
159   mp->client_index = client_index;
160   mp->pid = pid;
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);
166
167   if (NULL != host_mac)
168     clib_memcpy (&mp->host_mac[0], host_mac, 6);
169
170   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
171
172   vl_msg_api_send_shmem (q, (u8 *) & mp);
173 }
174
175 static void vl_api_dhcp_proxy_config_2_t_handler
176   (vl_api_dhcp_proxy_config_2_t * mp)
177 {
178   if (mp->is_ipv6 == 0)
179     dhcpv4_proxy_config_2 (mp);
180   else
181     dhcpv6_proxy_config_2 (mp);
182 }
183
184 static void vl_api_dhcp_client_config_t_handler
185   (vl_api_dhcp_client_config_t * mp)
186 {
187   vlib_main_t *vm = vlib_get_main ();
188   vl_api_dhcp_client_config_reply_t *rmp;
189   int rv = 0;
190
191   VALIDATE_SW_IF_INDEX (mp);
192
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 :
196                            NULL, mp->pid);
197
198   BAD_SW_IF_INDEX_LABEL;
199
200   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
201 }
202
203 /*
204  * dhcp_api_hookup
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()
209  */
210 #define vl_msg_name_crc_list
211 #include <vnet/vnet_all_api_h.h>
212 #undef vl_msg_name_crc_list
213
214 static void
215 setup_message_id_table (api_main_t * am)
216 {
217 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
218   foreach_vl_msg_name_crc_dhcp;
219 #undef _
220 }
221
222 static clib_error_t *
223 dhcp_api_hookup (vlib_main_t * vm)
224 {
225   api_main_t *am = &api_main;
226
227 #define _(N,n)                                                  \
228     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
229                            vl_api_##n##_t_handler,              \
230                            vl_noop_handler,                     \
231                            vl_api_##n##_t_endian,               \
232                            vl_api_##n##_t_print,                \
233                            sizeof(vl_api_##n##_t), 1);
234   foreach_vpe_api_msg;
235 #undef _
236
237   /*
238    * Set up the (msg_name, crc, message-id) table
239    */
240   setup_message_id_table (am);
241
242   return 0;
243 }
244
245 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
246
247 /*
248  * fd.io coding-style-patch-verification: ON
249  *
250  * Local Variables:
251  * eval: (c-set-style "gnu")
252  * End:
253  */