dhcp: Move to plugin
[vpp.git] / src / plugins / 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 <dhcp/dhcp_proxy.h>
26 #include <dhcp/client.h>
27 #include <dhcp/dhcp6_pd_client_dp.h>
28 #include <dhcp/dhcp6_ia_na_client_dp.h>
29 #include <dhcp/dhcp6_client_common_dp.h>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/ip/ip_types_api.h>
32
33 /* define message IDs */
34 #include <vnet/format_fns.h>
35 #include <dhcp/dhcp.api_enum.h>
36 #include <dhcp/dhcp.api_types.h>
37
38 /**
39  * Base message ID fot the plugin
40  */
41 static u32 dhcp_base_msg_id;
42 #define REPLY_MSG_ID_BASE dhcp_base_msg_id
43
44 #include <vlibapi/api_helper_macros.h>
45
46 #define  DHCP_PLUGIN_VERSION_MAJOR 1
47 #define  DHCP_PLUGIN_VERSION_MINOR 0
48
49 static void
50 vl_api_dhcp_plugin_get_version_t_handler (vl_api_dhcp_plugin_get_version_t *
51                                           mp)
52 {
53   vl_api_dhcp_plugin_get_version_reply_t *rmp;
54   int msg_size = sizeof (*rmp);
55   vl_api_registration_t *reg;
56
57   reg = vl_api_client_index_to_registration (mp->client_index);
58   if (!reg)
59     return;
60
61   rmp = vl_msg_api_alloc (msg_size);
62   clib_memset (rmp, 0, msg_size);
63   rmp->_vl_msg_id =
64     ntohs (VL_API_DHCP_PLUGIN_GET_VERSION_REPLY + REPLY_MSG_ID_BASE);
65   rmp->context = mp->context;
66   rmp->major = htonl (DHCP_PLUGIN_VERSION_MAJOR);
67   rmp->minor = htonl (DHCP_PLUGIN_VERSION_MINOR);
68
69   vl_api_send_msg (reg, (u8 *) rmp);
70 }
71
72 static void
73 vl_api_dhcp_plugin_control_ping_t_handler (vl_api_dhcp_plugin_control_ping_t *
74                                            mp)
75 {
76   vl_api_dhcp_plugin_control_ping_reply_t *rmp;
77   int rv = 0;
78
79   /* *INDENT-OFF* */
80   REPLY_MACRO2 (VL_API_DHCP_PLUGIN_CONTROL_PING_REPLY,
81   ({
82     rmp->vpe_pid = ntohl (getpid ());
83   }));
84   /* *INDENT-ON* */
85 }
86
87 static void
88 vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp)
89 {
90   vl_api_dhcp6_duid_ll_set_reply_t *rmp;
91   dhcpv6_duid_ll_string_t *duid;
92   int rv = 0;
93
94   duid = (dhcpv6_duid_ll_string_t *) mp->duid_ll;
95   if (duid->duid_type != htonl (DHCPV6_DUID_LL))
96     {
97       rv = VNET_API_ERROR_INVALID_VALUE;
98       goto reply;
99     }
100   clib_memcpy (&client_duid, &duid, sizeof (client_duid));
101
102 reply:
103   REPLY_MACRO (VL_API_DHCP6_DUID_LL_SET_REPLY);
104 }
105
106 static void
107 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
108 {
109   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
110   u8 *vpn_ascii_id;
111   int rv;
112
113   mp->vpn_ascii_id[sizeof (mp->vpn_ascii_id) - 1] = 0;
114   vpn_ascii_id = format (0, "%s", mp->vpn_ascii_id);
115   rv =
116     dhcp_proxy_set_vss ((mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4),
117                         ntohl (mp->tbl_id), ntohl (mp->vss_type),
118                         vpn_ascii_id, ntohl (mp->oui), ntohl (mp->vpn_index),
119                         mp->is_add == 0);
120
121   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
122 }
123
124
125 static void vl_api_dhcp_proxy_config_t_handler
126   (vl_api_dhcp_proxy_config_t * mp)
127 {
128   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
129   ip46_address_t src, server;
130   int rv = -1;
131
132   if (mp->dhcp_src_address.af != mp->dhcp_server.af)
133     {
134       rv = VNET_API_ERROR_INVALID_ARGUMENT;
135       goto reply;
136     }
137
138   ip_address_decode (&mp->dhcp_src_address, &src);
139   ip_address_decode (&mp->dhcp_server, &server);
140
141   if (mp->dhcp_src_address.af == ADDRESS_IP4)
142     {
143       rv = dhcp4_proxy_set_server (&server,
144                                    &src,
145                                    (u32) ntohl (mp->rx_vrf_id),
146                                    (u32) ntohl (mp->server_vrf_id),
147                                    (int) (mp->is_add == 0));
148     }
149   else
150     {
151       rv = dhcp6_proxy_set_server (&server,
152                                    &src,
153                                    (u32) ntohl (mp->rx_vrf_id),
154                                    (u32) ntohl (mp->server_vrf_id),
155                                    (int) (mp->is_add == 0));
156     }
157
158 reply:
159   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
160 }
161
162 static void
163 vl_api_dhcp_proxy_dump_t_handler (vl_api_dhcp_proxy_dump_t * mp)
164 {
165   vl_api_registration_t *reg;
166
167   reg = vl_api_client_index_to_registration (mp->client_index);
168   if (!reg)
169     return;;
170
171   dhcp_proxy_dump ((mp->is_ip6 == 1 ?
172                     FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4), reg, mp->context);
173 }
174
175 void
176 dhcp_send_details (fib_protocol_t proto,
177                    void *opaque, u32 context, dhcp_proxy_t * proxy)
178 {
179   vl_api_dhcp_proxy_details_t *mp;
180   vl_api_registration_t *reg = opaque;
181   vl_api_dhcp_server_t *v_server;
182   dhcp_server_t *server;
183   fib_table_t *s_fib;
184   dhcp_vss_t *vss;
185   u32 count;
186   size_t n;
187
188   count = vec_len (proxy->dhcp_servers);
189   n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
190   mp = vl_msg_api_alloc (n);
191   if (!mp)
192     return;
193   clib_memset (mp, 0, n);
194   mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS + REPLY_MSG_ID_BASE);
195   mp->context = context;
196   mp->count = count;
197
198   mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
199   mp->rx_vrf_id =
200     htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
201
202   vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
203
204   if (vss)
205     {
206       mp->vss_type = ntohl (vss->vss_type);
207       if (vss->vss_type == VSS_TYPE_ASCII)
208         {
209           u32 id_len = vec_len (vss->vpn_ascii_id);
210           clib_memcpy (mp->vss_vpn_ascii_id, vss->vpn_ascii_id, id_len);
211         }
212       else if (vss->vss_type == VSS_TYPE_VPN_ID)
213         {
214           u32 oui = ((u32) vss->vpn_id[0] << 16) + ((u32) vss->vpn_id[1] << 8)
215             + ((u32) vss->vpn_id[2]);
216           u32 fib_id = ((u32) vss->vpn_id[3] << 24) +
217             ((u32) vss->vpn_id[4] << 16) + ((u32) vss->vpn_id[5] << 8) +
218             ((u32) vss->vpn_id[6]);
219           mp->vss_oui = htonl (oui);
220           mp->vss_fib_id = htonl (fib_id);
221         }
222     }
223   else
224     mp->vss_type = VSS_TYPE_INVALID;
225
226   vec_foreach_index (count, proxy->dhcp_servers)
227   {
228     server = &proxy->dhcp_servers[count];
229     v_server = &mp->servers[count];
230
231     s_fib = fib_table_get (server->server_fib_index, proto);
232
233     v_server->server_vrf_id = htonl (s_fib->ft_table_id);
234
235     if (mp->is_ipv6)
236       {
237         memcpy (&v_server->dhcp_server.un, &server->dhcp_server.ip6, 16);
238       }
239     else
240       {
241         /* put the address in the first bytes */
242         memcpy (&v_server->dhcp_server.un, &server->dhcp_server.ip4, 4);
243       }
244   }
245
246   if (mp->is_ipv6)
247     {
248       memcpy (&mp->dhcp_src_address.un, &proxy->dhcp_src_address.ip6, 16);
249     }
250   else
251     {
252       /* put the address in the first bytes */
253       memcpy (&mp->dhcp_src_address.un, &proxy->dhcp_src_address.ip4, 4);
254     }
255   vl_api_send_msg (reg, (u8 *) mp);
256 }
257
258 static void
259 dhcp_client_lease_encode (vl_api_dhcp_lease_t * lease,
260                           const dhcp_client_t * client)
261 {
262   size_t len;
263   u8 i;
264
265   lease->is_ipv6 = 0;           // only support IPv6 clients
266   lease->sw_if_index = ntohl (client->sw_if_index);
267   lease->state = ntohl (client->state);
268   len = clib_min (sizeof (lease->hostname) - 1, vec_len (client->hostname));
269   clib_memcpy (&lease->hostname, client->hostname, len);
270   lease->hostname[len] = 0;
271
272   lease->mask_width = client->subnet_mask_width;
273   clib_memcpy (&lease->host_address.un, (u8 *) & client->leased_address,
274                sizeof (ip4_address_t));
275   clib_memcpy (&lease->router_address.un, (u8 *) & client->router_address,
276                sizeof (ip4_address_t));
277
278   lease->count = vec_len (client->domain_server_address);
279   for (i = 0; i < lease->count; i++)
280     clib_memcpy (&lease->domain_server[i].address,
281                  (u8 *) & client->domain_server_address[i],
282                  sizeof (ip4_address_t));
283
284   clib_memcpy (&lease->host_mac[0], client->client_hardware_address, 6);
285 }
286
287 static void
288 dhcp_client_data_encode (vl_api_dhcp_client_t * vclient,
289                          const dhcp_client_t * client)
290 {
291   size_t len;
292
293   vclient->sw_if_index = ntohl (client->sw_if_index);
294   len = clib_min (sizeof (vclient->hostname) - 1, vec_len (client->hostname));
295   clib_memcpy (&vclient->hostname, client->hostname, len);
296   vclient->hostname[len] = 0;
297
298   len = clib_min (sizeof (vclient->id) - 1,
299                   vec_len (client->client_identifier));
300   clib_memcpy (&vclient->id, client->client_identifier, len);
301   vclient->id[len] = 0;
302
303   if (NULL != client->event_callback)
304     vclient->want_dhcp_event = 1;
305   else
306     vclient->want_dhcp_event = 0;
307   vclient->set_broadcast_flag = client->set_broadcast_flag;
308   vclient->dscp = ip_dscp_encode (client->dscp);
309   vclient->pid = client->pid;
310 }
311
312 static void
313 dhcp_compl_event_callback (u32 client_index, const dhcp_client_t * client)
314 {
315   vl_api_registration_t *reg;
316   vl_api_dhcp_compl_event_t *mp;
317
318   reg = vl_api_client_index_to_registration (client_index);
319   if (!reg)
320     return;
321
322   mp = vl_msg_api_alloc (sizeof (*mp));
323   mp->client_index = client_index;
324   mp->pid = client->pid;
325   dhcp_client_lease_encode (&mp->lease, client);
326
327   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT + REPLY_MSG_ID_BASE);
328
329   vl_api_send_msg (reg, (u8 *) mp);
330 }
331
332 static void vl_api_dhcp_client_config_t_handler
333   (vl_api_dhcp_client_config_t * mp)
334 {
335   vlib_main_t *vm = vlib_get_main ();
336   vl_api_dhcp_client_config_reply_t *rmp;
337   u32 sw_if_index;
338   ip_dscp_t dscp;
339   int rv = 0;
340
341   VALIDATE_SW_IF_INDEX (&(mp->client));
342
343   sw_if_index = ntohl (mp->client.sw_if_index);
344   dscp = ip_dscp_decode (mp->client.dscp);
345
346   rv = dhcp_client_config (mp->is_add,
347                            mp->client_index,
348                            vm,
349                            sw_if_index,
350                            mp->client.hostname,
351                            mp->client.id,
352                            (mp->client.want_dhcp_event ?
353                             dhcp_compl_event_callback :
354                             NULL),
355                            mp->client.set_broadcast_flag,
356                            dscp, mp->client.pid);
357
358   BAD_SW_IF_INDEX_LABEL;
359   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
360 }
361
362 typedef struct dhcp_client_send_walk_ctx_t_
363 {
364   vl_api_registration_t *reg;
365   u32 context;
366 } dhcp_client_send_walk_ctx_t;
367
368 static int
369 send_dhcp_client_entry (const dhcp_client_t * client, void *arg)
370 {
371   dhcp_client_send_walk_ctx_t *ctx;
372   vl_api_dhcp_client_details_t *mp;
373
374   ctx = arg;
375
376   mp = vl_msg_api_alloc (sizeof (*mp));
377   clib_memset (mp, 0, sizeof (*mp));
378
379   mp->_vl_msg_id = ntohs (VL_API_DHCP_CLIENT_DETAILS + REPLY_MSG_ID_BASE);
380   mp->context = ctx->context;
381
382   dhcp_client_data_encode (&mp->client, client);
383   dhcp_client_lease_encode (&mp->lease, client);
384
385   vl_api_send_msg (ctx->reg, (u8 *) mp);
386
387   return (1);
388 }
389
390 static void
391 vl_api_dhcp_client_dump_t_handler (vl_api_dhcp_client_dump_t * mp)
392 {
393   vl_api_registration_t *reg;
394
395   reg = vl_api_client_index_to_registration (mp->client_index);
396   if (!reg)
397     return;
398
399   dhcp_client_send_walk_ctx_t ctx = {
400     .reg = reg,
401     .context = mp->context,
402   };
403   dhcp_client_walk (send_dhcp_client_entry, &ctx);
404 }
405
406 static void
407   vl_api_dhcp6_clients_enable_disable_t_handler
408   (vl_api_dhcp6_clients_enable_disable_t * mp)
409 {
410   vl_api_dhcp6_clients_enable_disable_reply_t *rmp;
411   int rv = 0;
412
413   dhcp6_clients_enable_disable (mp->enable);
414
415   REPLY_MACRO (VL_API_DHCP6_CLIENTS_ENABLE_DISABLE_REPLY);
416 }
417
418 void
419   vl_api_want_dhcp6_reply_events_t_handler
420   (vl_api_want_dhcp6_reply_events_t * mp)
421 {
422   vpe_api_main_t *am = &vpe_api_main;
423   vl_api_want_dhcp6_reply_events_reply_t *rmp;
424   int rv = 0;
425
426   uword *p =
427     hash_get (am->dhcp6_reply_events_registration_hash, mp->client_index);
428   vpe_client_registration_t *rp;
429   if (p)
430     {
431       if (mp->enable_disable)
432         {
433           clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
434           rv = VNET_API_ERROR_INVALID_REGISTRATION;
435           goto reply;
436         }
437       else
438         {
439           rp = pool_elt_at_index (am->dhcp6_reply_events_registrations, p[0]);
440           pool_put (am->dhcp6_reply_events_registrations, rp);
441           hash_unset (am->dhcp6_reply_events_registration_hash,
442                       mp->client_index);
443           if (pool_elts (am->dhcp6_reply_events_registrations) == 0)
444             dhcp6_set_publisher_node (~0, DHCP6_DP_REPORT_MAX);
445           goto reply;
446         }
447     }
448   if (mp->enable_disable == 0)
449     {
450       clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
451       rv = VNET_API_ERROR_INVALID_REGISTRATION;
452       goto reply;
453     }
454   pool_get (am->dhcp6_reply_events_registrations, rp);
455   rp->client_index = mp->client_index;
456   rp->client_pid = ntohl (mp->pid);
457   hash_set (am->dhcp6_reply_events_registration_hash, rp->client_index,
458             rp - am->dhcp6_reply_events_registrations);
459   dhcp6_set_publisher_node (dhcp6_reply_process_node.index,
460                             DHCP6_DP_REPLY_REPORT);
461
462 reply:
463   REPLY_MACRO (VL_API_WANT_DHCP6_REPLY_EVENTS_REPLY);
464 }
465
466 void
467   vl_api_want_dhcp6_pd_reply_events_t_handler
468   (vl_api_want_dhcp6_pd_reply_events_t * mp)
469 {
470   vpe_api_main_t *am = &vpe_api_main;
471   vl_api_want_dhcp6_pd_reply_events_reply_t *rmp;
472   int rv = 0;
473
474   uword *p =
475     hash_get (am->dhcp6_pd_reply_events_registration_hash, mp->client_index);
476   vpe_client_registration_t *rp;
477   if (p)
478     {
479       if (mp->enable_disable)
480         {
481           clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
482           rv = VNET_API_ERROR_INVALID_REGISTRATION;
483           goto reply;
484         }
485       else
486         {
487           rp =
488             pool_elt_at_index (am->dhcp6_pd_reply_events_registrations, p[0]);
489           pool_put (am->dhcp6_pd_reply_events_registrations, rp);
490           hash_unset (am->dhcp6_pd_reply_events_registration_hash,
491                       mp->client_index);
492           if (pool_elts (am->dhcp6_pd_reply_events_registrations) == 0)
493             dhcp6_pd_set_publisher_node (~0, DHCP6_PD_DP_REPORT_MAX);
494           goto reply;
495         }
496     }
497   if (mp->enable_disable == 0)
498     {
499       clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
500       rv = VNET_API_ERROR_INVALID_REGISTRATION;
501       goto reply;
502     }
503   pool_get (am->dhcp6_pd_reply_events_registrations, rp);
504   rp->client_index = mp->client_index;
505   rp->client_pid = ntohl (mp->pid);
506   hash_set (am->dhcp6_pd_reply_events_registration_hash, rp->client_index,
507             rp - am->dhcp6_pd_reply_events_registrations);
508   dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
509                                DHCP6_PD_DP_REPLY_REPORT);
510
511 reply:
512   REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY);
513 }
514
515 void
516   vl_api_dhcp6_send_client_message_t_handler
517   (vl_api_dhcp6_send_client_message_t * mp)
518 {
519   vl_api_dhcp6_send_client_message_reply_t *rmp;
520   dhcp6_send_client_message_params_t params;
521   vlib_main_t *vm = vlib_get_main ();
522   u32 n_addresses;
523   u32 i;
524   int rv = 0;
525
526   VALIDATE_SW_IF_INDEX (mp);
527
528   BAD_SW_IF_INDEX_LABEL;
529   REPLY_MACRO (VL_API_DHCP6_SEND_CLIENT_MESSAGE_REPLY);
530
531   if (rv != 0)
532     return;
533
534   params.sw_if_index = ntohl (mp->sw_if_index);
535   params.server_index = ntohl (mp->server_index);
536   params.irt = ntohl (mp->irt);
537   params.mrt = ntohl (mp->mrt);
538   params.mrc = ntohl (mp->mrc);
539   params.mrd = ntohl (mp->mrd);
540   params.msg_type = ntohl (mp->msg_type);
541   params.T1 = ntohl (mp->T1);
542   params.T2 = ntohl (mp->T2);
543   n_addresses = ntohl (mp->n_addresses);
544   params.addresses = 0;
545   if (n_addresses > 0)
546     vec_validate (params.addresses, n_addresses - 1);
547   for (i = 0; i < n_addresses; i++)
548     {
549       vl_api_dhcp6_address_info_t *ai = &mp->addresses[i];
550       dhcp6_send_client_message_params_address_t *addr = &params.addresses[i];
551       addr->preferred_lt = ntohl (ai->preferred_time);
552       addr->valid_lt = ntohl (ai->valid_time);
553       ip6_address_decode (ai->address, &addr->address);
554     }
555
556   dhcp6_send_client_message (vm, ntohl (mp->sw_if_index), mp->stop, &params);
557 }
558
559 void
560   vl_api_dhcp6_pd_send_client_message_t_handler
561   (vl_api_dhcp6_pd_send_client_message_t * mp)
562 {
563   vl_api_dhcp6_pd_send_client_message_reply_t *rmp;
564   dhcp6_pd_send_client_message_params_t params;
565   vlib_main_t *vm = vlib_get_main ();
566   u32 n_prefixes;
567   u32 i;
568   int rv = 0;
569
570   VALIDATE_SW_IF_INDEX (mp);
571
572   BAD_SW_IF_INDEX_LABEL;
573   REPLY_MACRO (VL_API_DHCP6_PD_SEND_CLIENT_MESSAGE_REPLY);
574
575   if (rv != 0)
576     return;
577
578   params.sw_if_index = ntohl (mp->sw_if_index);
579   params.server_index = ntohl (mp->server_index);
580   params.irt = ntohl (mp->irt);
581   params.mrt = ntohl (mp->mrt);
582   params.mrc = ntohl (mp->mrc);
583   params.mrd = ntohl (mp->mrd);
584   params.msg_type = ntohl (mp->msg_type);
585   params.T1 = ntohl (mp->T1);
586   params.T2 = ntohl (mp->T2);
587   n_prefixes = ntohl (mp->n_prefixes);
588   params.prefixes = 0;
589   if (n_prefixes > 0)
590     vec_validate (params.prefixes, n_prefixes - 1);
591   for (i = 0; i < n_prefixes; i++)
592     {
593       vl_api_dhcp6_pd_prefix_info_t *pi = &mp->prefixes[i];
594       dhcp6_pd_send_client_message_params_prefix_t *pref =
595         &params.prefixes[i];
596       pref->preferred_lt = ntohl (pi->preferred_time);
597       pref->valid_lt = ntohl (pi->valid_time);
598       ip6_address_decode (pi->prefix.address, &pref->prefix);
599       pref->prefix_length = pi->prefix.len;
600     }
601
602   dhcp6_pd_send_client_message (vm, ntohl (mp->sw_if_index), mp->stop,
603                                 &params);
604 }
605
606 static clib_error_t *
607 call_dhcp6_reply_event_callbacks (void *data,
608                                   _vnet_dhcp6_reply_event_function_list_elt_t
609                                   * elt)
610 {
611   clib_error_t *error = 0;
612
613   while (elt)
614     {
615       error = elt->fp (data);
616       if (error)
617         return error;
618       elt = elt->next_dhcp6_reply_event_function;
619     }
620
621   return error;
622 }
623
624 static uword
625 dhcp6_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
626                      vlib_frame_t * f)
627 {
628   /* These cross the longjmp  boundary (vlib_process_wait_for_event)
629    * and need to be volatile - to prevent them from being optimized into
630    * a register - which could change during suspension */
631
632   while (1)
633     {
634       vlib_process_wait_for_event (vm);
635       uword event_type = DHCP6_DP_REPLY_REPORT;
636       void *event_data = vlib_process_get_event_data (vm, &event_type);
637
638       int i;
639       if (event_type == DHCP6_DP_REPLY_REPORT)
640         {
641           address_report_t *events = event_data;
642           for (i = 0; i < vec_len (events); i++)
643             {
644               u32 event_size =
645                 sizeof (vl_api_dhcp6_reply_event_t) +
646                 vec_len (events[i].addresses) *
647                 sizeof (vl_api_dhcp6_address_info_t);
648               vl_api_dhcp6_reply_event_t *event = clib_mem_alloc (event_size);
649               clib_memset (event, 0, event_size);
650
651               event->sw_if_index = htonl (events[i].body.sw_if_index);
652               event->server_index = htonl (events[i].body.server_index);
653               event->msg_type = events[i].body.msg_type;
654               event->T1 = htonl (events[i].body.T1);
655               event->T2 = htonl (events[i].body.T2);
656               event->inner_status_code =
657                 htons (events[i].body.inner_status_code);
658               event->status_code = htons (events[i].body.status_code);
659               event->preference = events[i].body.preference;
660
661               event->n_addresses = htonl (vec_len (events[i].addresses));
662               vl_api_dhcp6_address_info_t *address =
663                 (typeof (address)) event->addresses;
664               u32 j;
665               for (j = 0; j < vec_len (events[i].addresses); j++)
666                 {
667                   dhcp6_address_info_t *info = &events[i].addresses[j];
668                   ip6_address_encode (&info->address, address->address);
669                   address->valid_time = htonl (info->valid_time);
670                   address->preferred_time = htonl (info->preferred_time);
671                   address++;
672                 }
673               vec_free (events[i].addresses);
674
675               dhcp6_ia_na_client_public_main_t *dcpm =
676                 &dhcp6_ia_na_client_public_main;
677               call_dhcp6_reply_event_callbacks (event, dcpm->functions);
678
679               vpe_client_registration_t *reg;
680               /* *INDENT-OFF* */
681               pool_foreach(reg, vpe_api_main.dhcp6_reply_events_registrations,
682               ({
683                 vl_api_registration_t *vl_reg;
684                 vl_reg =
685                   vl_api_client_index_to_registration (reg->client_index);
686                 if (vl_reg && vl_api_can_send_msg (vl_reg))
687                   {
688                     vl_api_dhcp6_reply_event_t *msg =
689                       vl_msg_api_alloc (event_size);
690                     clib_memcpy (msg, event, event_size);
691                     msg->_vl_msg_id = htons (VL_API_DHCP6_REPLY_EVENT + REPLY_MSG_ID_BASE);
692                     msg->client_index = reg->client_index;
693                     msg->pid = reg->client_pid;
694                     vl_api_send_msg (vl_reg, (u8 *) msg);
695                   }
696               }));
697               /* *INDENT-ON* */
698
699               clib_mem_free (event);
700             }
701         }
702       vlib_process_put_event_data (vm, event_data);
703     }
704
705   return 0;
706 }
707
708 /* *INDENT-OFF* */
709 VLIB_REGISTER_NODE (dhcp6_reply_process_node) = {
710   .function = dhcp6_reply_process,
711   .type = VLIB_NODE_TYPE_PROCESS,
712   .name = "dhcp6-reply-publisher-process",
713 };
714 /* *INDENT-ON* */
715
716 static clib_error_t *
717 call_dhcp6_pd_reply_event_callbacks (void *data,
718                                      _vnet_dhcp6_pd_reply_event_function_list_elt_t
719                                      * elt)
720 {
721   clib_error_t *error = 0;
722
723   while (elt)
724     {
725       error = elt->fp (data);
726       if (error)
727         return error;
728       elt = elt->next_dhcp6_pd_reply_event_function;
729     }
730
731   return error;
732 }
733
734 static uword
735 dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
736                         vlib_frame_t * f)
737 {
738   /* These cross the longjmp  boundary (vlib_process_wait_for_event)
739    * and need to be volatile - to prevent them from being optimized into
740    * a register - which could change during suspension */
741
742   while (1)
743     {
744       vlib_process_wait_for_event (vm);
745       uword event_type = DHCP6_PD_DP_REPLY_REPORT;
746       void *event_data = vlib_process_get_event_data (vm, &event_type);
747
748       int i;
749       if (event_type == DHCP6_PD_DP_REPLY_REPORT)
750         {
751           prefix_report_t *events = event_data;
752           for (i = 0; i < vec_len (events); i++)
753             {
754               u32 event_size =
755                 sizeof (vl_api_dhcp6_pd_reply_event_t) +
756                 vec_len (events[i].prefixes) *
757                 sizeof (vl_api_dhcp6_pd_prefix_info_t);
758               vl_api_dhcp6_pd_reply_event_t *event =
759                 clib_mem_alloc (event_size);
760               clib_memset (event, 0, event_size);
761
762               event->sw_if_index = htonl (events[i].body.sw_if_index);
763               event->server_index = htonl (events[i].body.server_index);
764               event->msg_type = events[i].body.msg_type;
765               event->T1 = htonl (events[i].body.T1);
766               event->T2 = htonl (events[i].body.T2);
767               event->inner_status_code =
768                 htons (events[i].body.inner_status_code);
769               event->status_code = htons (events[i].body.status_code);
770               event->preference = events[i].body.preference;
771
772               event->n_prefixes = htonl (vec_len (events[i].prefixes));
773               vl_api_dhcp6_pd_prefix_info_t *prefix =
774                 (typeof (prefix)) event->prefixes;
775               u32 j;
776               for (j = 0; j < vec_len (events[i].prefixes); j++)
777                 {
778                   dhcp6_prefix_info_t *info = &events[i].prefixes[j];
779                   ip6_address_encode (&info->prefix, prefix->prefix.address);
780                   prefix->prefix.len = info->prefix_length;
781                   prefix->valid_time = htonl (info->valid_time);
782                   prefix->preferred_time = htonl (info->preferred_time);
783                   prefix++;
784                 }
785               vec_free (events[i].prefixes);
786
787               dhcp6_pd_client_public_main_t *dpcpm =
788                 &dhcp6_pd_client_public_main;
789               call_dhcp6_pd_reply_event_callbacks (event, dpcpm->functions);
790
791               vpe_client_registration_t *reg;
792               /* *INDENT-OFF* */
793               pool_foreach(reg, vpe_api_main.dhcp6_pd_reply_events_registrations,
794               ({
795                 vl_api_registration_t *vl_reg;
796                 vl_reg =
797                   vl_api_client_index_to_registration (reg->client_index);
798                 if (vl_reg && vl_api_can_send_msg (vl_reg))
799                   {
800                     vl_api_dhcp6_pd_reply_event_t *msg =
801                       vl_msg_api_alloc (event_size);
802                     clib_memcpy (msg, event, event_size);
803                     msg->_vl_msg_id = htons (VL_API_DHCP6_PD_REPLY_EVENT + REPLY_MSG_ID_BASE);
804                     msg->client_index = reg->client_index;
805                     msg->pid = reg->client_pid;
806                     vl_api_send_msg (vl_reg, (u8 *) msg);
807                   }
808               }));
809               /* *INDENT-ON* */
810
811               clib_mem_free (event);
812             }
813         }
814       vlib_process_put_event_data (vm, event_data);
815     }
816
817   return 0;
818 }
819
820 /* *INDENT-OFF* */
821 VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node) = {
822   .function = dhcp6_pd_reply_process,
823   .type = VLIB_NODE_TYPE_PROCESS,
824   .name = "dhcp6-pd-reply-publisher-process",
825 };
826 /* *INDENT-ON* */
827
828 /*
829  * dhcp_api_hookup
830  * Add vpe's API message handlers to the table.
831  * vlib has already mapped shared memory and
832  * added the client registration handlers.
833  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
834  */
835 #include <dhcp/dhcp.api.c>
836
837 static clib_error_t *
838 dhcp_api_hookup (vlib_main_t * vm)
839 {
840   /*
841    * Set up the (msg_name, crc, message-id) table
842    */
843   dhcp_base_msg_id = setup_message_id_table ();
844
845   dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index,
846                                DHCP6_PD_DP_REPLY_REPORT);
847   dhcp6_set_publisher_node (dhcp6_reply_process_node.index,
848                             DHCP6_DP_REPLY_REPORT);
849
850   return 0;
851 }
852
853 VLIB_API_INIT_FUNCTION (dhcp_api_hookup);
854
855 #include <vlib/unix/plugin.h>
856 #include <vpp/app/version.h>
857
858 /* *INDENT-OFF* */
859 VLIB_PLUGIN_REGISTER () = {
860     .version = VPP_BUILD_VER,
861     .description = "Dynamic Host Configuration Protocol (DHCP)",
862 };
863 /* *INDENT-ON* */
864
865 /*
866  * fd.io coding-style-patch-verification: ON
867  *
868  * Local Variables:
869  * eval: (c-set-style "gnu")
870  * End:
871  */