mactime: add a "top" command to watch device stats
[vpp.git] / src / vnet / dhcp / dhcp6_ia_na_client_cp.c
1 /*
2  * Copyright (c) 2018 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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/vnet_msg_enum.h>
19 #include <vnet/dhcp/dhcp6_packet.h>
20 #include <vnet/dhcp/dhcp6_ia_na_client_dp.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip6.h>
23 #include <float.h>
24 #include <math.h>
25
26 #define vl_typedefs             /* define message structures */
27 #include <vnet/vnet_all_api_h.h>
28 #undef vl_typedefs
29
30 #define vl_endianfun            /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_endianfun
33
34 #include <vlibapi/api_helper_macros.h>
35
36 #define foreach_dhcp6_client_cp_msg                                           \
37 _(DHCP6_CLIENT_ENABLE_DISABLE, dhcp6_client_enable_disable)
38
39 #define vl_api_dhcp6_client_enable_disable_t_print vl_noop_handler
40
41 typedef struct
42 {
43   u32 sw_if_index;
44   ip6_address_t address;
45   u32 preferred_lt;
46   u32 valid_lt;
47   f64 due_time;
48 } address_info_t;
49
50 typedef struct
51 {
52   u8 enabled;
53   u32 server_index;
54   u32 T1;
55   u32 T2;
56   f64 T1_due_time;
57   f64 T2_due_time;
58   u32 address_count;
59   u8 rebinding;
60 } client_state_t;
61
62 typedef struct
63 {
64   address_info_t *address_pool;
65   client_state_t *client_state_by_sw_if_index;
66   u32 n_clients;
67   f64 max_valid_due_time;
68
69   /* convenience */
70   vlib_main_t *vlib_main;
71   vnet_main_t *vnet_main;
72   api_main_t *api_main;
73   u32 node_index;
74 } dhcp6_client_cp_main_t;
75
76 static dhcp6_client_cp_main_t dhcp6_client_cp_main;
77
78 enum
79 {
80   RD_CP_EVENT_INTERRUPT,
81   RD_CP_EVENT_DISABLE,
82 };
83
84 static void
85 send_client_message_start_stop (u32 sw_if_index, u32 server_index,
86                                 u8 msg_type, address_info_t * address_list,
87                                 u8 start)
88 {
89   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
90   dhcp6_send_client_message_params_t params = { 0, };
91   dhcp6_send_client_message_params_address_t *addresses = 0, *addr;
92   u32 i;
93
94   ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
95           rm->client_state_by_sw_if_index[sw_if_index].enabled);
96   client_state_t *client_state =
97     &rm->client_state_by_sw_if_index[sw_if_index];
98
99   params.sw_if_index = sw_if_index;
100   params.server_index = server_index;
101   params.msg_type = msg_type;
102   if (start)
103     {
104       if (msg_type == DHCPV6_MSG_SOLICIT)
105         {
106           params.irt = 1;
107           params.mrt = 120;
108         }
109       else if (msg_type == DHCPV6_MSG_REQUEST)
110         {
111           params.irt = 1;
112           params.mrt = 30;
113           params.mrc = 10;
114         }
115       else if (msg_type == DHCPV6_MSG_RENEW)
116         {
117           params.irt = 10;
118           params.mrt = 600;
119           f64 current_time = vlib_time_now (rm->vlib_main);
120           i32 diff_time = client_state->T2 - current_time;
121           if (diff_time < 0)
122             diff_time = 0;
123           params.mrd = diff_time;
124         }
125       else if (msg_type == DHCPV6_MSG_REBIND)
126         {
127           params.irt = 10;
128           params.mrt = 600;
129           f64 current_time = vlib_time_now (rm->vlib_main);
130           i32 diff_time = rm->max_valid_due_time - current_time;
131           if (diff_time < 0)
132             diff_time = 0;
133           params.mrd = diff_time;
134         }
135       else if (msg_type == DHCPV6_MSG_RELEASE)
136         {
137           params.mrc = 1;
138         }
139     }
140
141   params.T1 = 0;
142   params.T2 = 0;
143   if (vec_len (address_list) != 0)
144     vec_validate (addresses, vec_len (address_list) - 1);
145   for (i = 0; i < vec_len (address_list); i++)
146     {
147       address_info_t *address = &address_list[i];
148       addr = &addresses[i];
149       addr->valid_lt = address->valid_lt;
150       addr->preferred_lt = address->preferred_lt;
151       addr->address = address->address;
152     }
153   params.addresses = addresses;
154
155   dhcp6_send_client_message (rm->vlib_main, sw_if_index, !start, &params);
156
157   vec_free (params.addresses);
158 }
159
160 static void interrupt_process (void);
161
162 static u32
163 ip6_enable (u32 sw_if_index)
164 {
165   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
166   clib_error_t *rv;
167
168   rv = enable_ip6_interface (rm->vlib_main, sw_if_index);
169
170   return rv != 0;
171 }
172
173 static u8
174 ip6_addresses_equal (ip6_address_t * address1, ip6_address_t * address2)
175 {
176   if (address1->as_u64[0] != address2->as_u64[0])
177     return 0;
178   return address1->as_u64[1] == address2->as_u64[1];
179 }
180
181 static clib_error_t *
182 dhcp6_reply_event_handler (vl_api_dhcp6_reply_event_t * mp)
183 {
184   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
185   vlib_main_t *vm = rm->vlib_main;
186   client_state_t *client_state;
187   ip6_address_t *address;
188   u32 sw_if_index;
189   u32 n_addresses;
190   vl_api_dhcp6_address_info_t *api_address;
191   u32 inner_status_code;
192   u32 status_code;
193   u32 server_index;
194   f64 current_time;
195   clib_error_t *error = 0;
196   u32 i;
197
198   current_time = vlib_time_now (vm);
199
200   sw_if_index = ntohl (mp->sw_if_index);
201
202   if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index))
203     return 0;
204
205   client_state = &rm->client_state_by_sw_if_index[sw_if_index];
206
207   if (!client_state->enabled)
208     return 0;
209
210   server_index = ntohl (mp->server_index);
211
212   n_addresses = ntohl (mp->n_addresses);
213
214   inner_status_code = ntohs (mp->inner_status_code);
215   status_code = ntohs (mp->status_code);
216
217   if (mp->msg_type == DHCPV6_MSG_API_ADVERTISE
218       && client_state->server_index == ~0)
219     {
220       address_info_t *address_list = 0, *address_info;
221
222       if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
223         {
224           clib_warning
225             ("Advertise message arrived with NoAddrsAvail status code");
226           return 0;
227         }
228
229       if (n_addresses > 0)
230         vec_validate (address_list, n_addresses - 1);
231       for (i = 0; i < n_addresses; i++)
232         {
233           api_address = &mp->addresses[i];
234           address = (ip6_address_t *) api_address->address;
235
236           address_info = &address_list[i];
237           address_info->address = *address;
238           address_info->preferred_lt = 0;
239           address_info->valid_lt = 0;
240         }
241
242       client_state->server_index = server_index;
243
244       send_client_message_start_stop (sw_if_index, server_index,
245                                       DHCPV6_MSG_REQUEST, address_list, 1);
246       vec_free (address_list);
247     }
248
249   if (mp->msg_type != DHCPV6_MSG_API_REPLY)
250     return 0;
251
252   if (!client_state->rebinding && client_state->server_index != server_index)
253     {
254       clib_warning ("Reply message arrived with Server ID different "
255                     "from that in Request or Renew message");
256       return 0;
257     }
258
259   if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
260     {
261       clib_warning ("Reply message arrived with NoAddrsAvail status code");
262       if (n_addresses > 0)
263         {
264           clib_warning
265             ("Invalid Reply message arrived: It contains NoAddrsAvail "
266              "status code but also contains addresses");
267           return 0;
268         }
269     }
270
271   if (status_code == DHCPV6_STATUS_UNSPEC_FAIL)
272     {
273       clib_warning ("Reply message arrived with UnspecFail status code");
274       return 0;
275     }
276
277   send_client_message_start_stop (sw_if_index, server_index,
278                                   mp->msg_type, 0, 0);
279
280   for (i = 0; i < n_addresses; i++)
281     {
282       address_info_t *address_info = 0;
283       u32 valid_time;
284       u32 preferred_time;
285
286       api_address = &mp->addresses[i];
287
288       address = (ip6_address_t *) api_address->address;
289
290       if (ip6_address_is_link_local_unicast (address))
291         continue;
292
293       valid_time = ntohl (api_address->valid_time);
294       preferred_time = ntohl (api_address->preferred_time);
295
296       if (preferred_time > valid_time)
297         continue;
298
299       u8 address_already_present = 0;
300       /* *INDENT-OFF* */
301       pool_foreach (address_info, rm->address_pool,
302       ({
303         if (address_info->sw_if_index != sw_if_index)
304           ;
305         else if (!ip6_addresses_equal (&address_info->address, address))
306           ;
307         else
308           {
309             address_already_present = 1;
310             goto address_pool_foreach_out;
311           }
312       }));
313       /* *INDENT-ON* */
314     address_pool_foreach_out:
315
316       if (address_already_present)
317         {
318           address_info->preferred_lt = preferred_time;
319           address_info->valid_lt = valid_time;
320           address_info->due_time = current_time + valid_time;
321           if (address_info->due_time > rm->max_valid_due_time)
322             rm->max_valid_due_time = address_info->due_time;
323           continue;
324         }
325
326       if (valid_time == 0)
327         continue;
328
329       pool_get (rm->address_pool, address_info);
330       address_info->sw_if_index = sw_if_index;
331       address_info->address = *address;
332       address_info->preferred_lt = preferred_time;
333       address_info->valid_lt = valid_time;
334       address_info->due_time = current_time + valid_time;
335       if (address_info->due_time > rm->max_valid_due_time)
336         rm->max_valid_due_time = address_info->due_time;
337       rm->client_state_by_sw_if_index[sw_if_index].address_count++;
338
339       error = ip6_add_del_interface_address (vm, sw_if_index,
340                                              &address_info->address, 64, 0);
341       if (error)
342         clib_warning ("Failed to add interface address");
343     }
344
345   client_state->server_index = server_index;
346   client_state->T1 = ntohl (mp->T1);
347   client_state->T2 = ntohl (mp->T2);
348   if (client_state->T1 != 0)
349     client_state->T1_due_time = current_time + client_state->T1;
350   if (client_state->T2 != 0)
351     client_state->T2_due_time = current_time + client_state->T2;
352   client_state->rebinding = 0;
353
354   interrupt_process ();
355
356   return error;
357 }
358
359 static address_info_t *
360 create_address_list (u32 sw_if_index)
361 {
362   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
363   address_info_t *address_info, *address_list = 0;;
364
365   /* *INDENT-OFF* */
366   pool_foreach (address_info, rm->address_pool,
367   ({
368     if (address_info->sw_if_index == sw_if_index)
369       {
370         u32 pos = vec_len (address_list);
371         vec_validate (address_list, pos);
372         clib_memcpy (&address_list[pos], address_info, sizeof (*address_info));
373       }
374   }));
375   /* *INDENT-ON* */
376
377   return address_list;
378 }
379
380 VNET_DHCP6_REPLY_EVENT_FUNCTION (dhcp6_reply_event_handler);
381
382 static uword
383 dhcp6_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
384                          vlib_frame_t * f)
385 {
386   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
387   address_info_t *address_info;
388   client_state_t *client_state;
389   f64 sleep_time = 1e9;
390   clib_error_t *error;
391   f64 current_time;
392   f64 due_time;
393   uword event_type;
394   uword *event_data = 0;
395   int i;
396
397   while (1)
398     {
399       vlib_process_wait_for_event_or_clock (vm, sleep_time);
400       event_type = vlib_process_get_events (vm, &event_data);
401       vec_reset_length (event_data);
402
403       if (event_type == RD_CP_EVENT_DISABLE)
404         {
405           vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED);
406           sleep_time = 1e9;
407           continue;
408         }
409
410       current_time = vlib_time_now (vm);
411       do
412         {
413           due_time = current_time + 1e9;
414           /* *INDENT-OFF* */
415           pool_foreach (address_info, rm->address_pool,
416           ({
417             if (address_info->due_time > current_time)
418               {
419                 if (address_info->due_time < due_time)
420                   due_time = address_info->due_time;
421               }
422             else
423               {
424                 u32 sw_if_index = address_info->sw_if_index;
425                 error = ip6_add_del_interface_address (vm, sw_if_index,
426                                                        &address_info->address,
427                                                        64, 1);
428                 if (error)
429                     clib_warning ("Failed to delete interface address");
430                 pool_put (rm->address_pool, address_info);
431                 /* make sure ip6 stays enabled */
432                 ip6_enable (sw_if_index);
433                 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
434                 if (--client_state->address_count == 0)
435                   {
436                     client_state->rebinding = 0;
437                     client_state->server_index = ~0;
438                     send_client_message_start_stop (sw_if_index, ~0,
439                                                     DHCPV6_MSG_SOLICIT,
440                                                     0, 1);
441                   }
442               }
443           }));
444           /* *INDENT-ON* */
445           for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
446             {
447               client_state_t *cs = &rm->client_state_by_sw_if_index[i];
448               if (cs->enabled && cs->server_index != ~0)
449                 {
450                   if (cs->T2_due_time > current_time)
451                     {
452                       if (cs->T2_due_time < due_time)
453                         due_time = cs->T2_due_time;
454                       if (cs->T1_due_time > current_time)
455                         {
456                           if (cs->T1_due_time < due_time)
457                             due_time = cs->T1_due_time;
458                         }
459                       else
460                         {
461                           cs->T1_due_time = DBL_MAX;
462                           address_info_t *address_list;
463                           address_list = create_address_list (i);
464                           cs->rebinding = 1;
465                           send_client_message_start_stop (i, cs->server_index,
466                                                           DHCPV6_MSG_RENEW,
467                                                           address_list, 1);
468                           vec_free (address_list);
469                         }
470                     }
471                   else
472                     {
473                       cs->T2_due_time = DBL_MAX;
474                       address_info_t *address_list;
475                       address_list = create_address_list (i);
476                       cs->rebinding = 1;
477                       send_client_message_start_stop (i, ~0,
478                                                       DHCPV6_MSG_REBIND,
479                                                       address_list, 1);
480                       vec_free (address_list);
481                     }
482                 }
483             }
484           current_time = vlib_time_now (vm);
485         }
486       while (due_time < current_time);
487
488       sleep_time = due_time - current_time;
489     }
490
491   return 0;
492 }
493
494 /* *INDENT-OFF* */
495 VLIB_REGISTER_NODE (dhcp6_client_cp_process_node) = {
496     .function = dhcp6_client_cp_process,
497     .type = VLIB_NODE_TYPE_PROCESS,
498     .name = "dhcp6-client-cp-process",
499 };
500 /* *INDENT-ON* */
501
502 static void
503 interrupt_process (void)
504 {
505   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
506   vlib_main_t *vm = rm->vlib_main;
507
508   vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
509                              RD_CP_EVENT_INTERRUPT, 0);
510 }
511
512 static void
513 disable_process (void)
514 {
515   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
516   vlib_main_t *vm = rm->vlib_main;
517
518   vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
519                              RD_CP_EVENT_DISABLE, 0);
520 }
521
522 static void
523 enable_process (void)
524 {
525   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
526   vlib_main_t *vm = rm->vlib_main;
527   vlib_node_t *node;
528
529   node = vec_elt (vm->node_main.nodes, rm->node_index);
530
531   vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING);
532   vlib_start_process (vm, node->runtime_index);
533 }
534
535 static clib_error_t *
536 dhcp6_addresses_show_command_function (vlib_main_t * vm,
537                                        unformat_input_t * input,
538                                        vlib_cli_command_t * cmd)
539 {
540   dhcp6_client_cp_main_t *dm = &dhcp6_client_cp_main;
541   clib_error_t *error = 0;
542   address_info_t *address_info;
543   f64 current_time = vlib_time_now (vm);
544
545   /* *INDENT-OFF* */
546   pool_foreach (address_info, dm->address_pool,
547   ({
548     vlib_cli_output (vm, "address: %U, "
549                      "preferred lifetime: %u, valid lifetime: %u "
550                      "(%f remaining)",
551                      format_ip6_address, &address_info->address,
552                      address_info->preferred_lt, address_info->valid_lt,
553                      address_info->due_time - current_time);
554   }));
555   /* *INDENT-ON* */
556
557   return error;
558 }
559
560 /* *INDENT-OFF* */
561 VLIB_CLI_COMMAND (dhcp6_addresses_show_command, static) = {
562   .path = "show dhcp6 addresses",
563   .short_help = "show dhcp6 addresses",
564   .function = dhcp6_addresses_show_command_function,
565 };
566 /* *INDENT-ON* */
567
568 static clib_error_t *
569 dhcp6_clients_show_command_function (vlib_main_t * vm,
570                                      unformat_input_t * input,
571                                      vlib_cli_command_t * cmd)
572 {
573   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
574   clib_error_t *error = 0;
575   client_state_t *cs;
576   f64 current_time = vlib_time_now (vm);
577   char buf1[256];
578   char buf2[256];
579   const char *rebinding;
580   u32 i;
581
582   for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
583     {
584       cs = &rm->client_state_by_sw_if_index[i];
585       if (cs->enabled)
586         {
587           if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time)
588             {
589               sprintf (buf1, "%u remaining",
590                        (u32) round (cs->T1_due_time - current_time));
591             }
592           else
593             sprintf (buf1, "timeout");
594           if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time)
595             sprintf (buf2, "%u remaining",
596                      (u32) round (cs->T2_due_time - current_time));
597           else
598             sprintf (buf2, "timeout");
599           if (cs->rebinding)
600             rebinding = ", REBINDING";
601           else
602             rebinding = "";
603           if (cs->T1)
604             vlib_cli_output (vm,
605                              "sw_if_index: %u, T1: %u (%s), "
606                              "T2: %u (%s), server index: %d%s", i,
607                              cs->T1, buf1, cs->T2, buf2,
608                              cs->server_index, rebinding);
609           else
610             vlib_cli_output (vm, "sw_if_index: %u%s", i, rebinding);
611         }
612     }
613
614   return error;
615 }
616
617 /* *INDENT-OFF* */
618 VLIB_CLI_COMMAND (dhcp6_clients_show_command, static) = {
619   .path = "show dhcp6 clients",
620   .short_help = "show dhcp6 clients",
621   .function = dhcp6_clients_show_command_function,
622 };
623 /* *INDENT-ON* */
624
625 static int
626 dhcp6_client_enable_disable (u32 sw_if_index, u8 enable)
627 {
628   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
629   vnet_main_t *vnm = rm->vnet_main;
630   vlib_main_t *vm = rm->vlib_main;
631   client_state_t *client_state;
632   client_state_t empty_config = { 0 };
633   address_info_t *address_info;
634   clib_error_t *error;
635
636   if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
637     {
638       clib_warning ("Invalid sw_if_index");
639       return 1;
640     }
641
642   vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index,
643                            empty_config);
644   client_state = &rm->client_state_by_sw_if_index[sw_if_index];
645
646   u8 old_enabled = client_state->enabled;
647   if (enable)
648     client_state->enabled = 1;
649   client_state->server_index = ~0;
650
651   if (!old_enabled && enable)
652     {
653       rm->n_clients++;
654       if (rm->n_clients == 1)
655         {
656           enable_process ();
657           dhcp6_clients_enable_disable (1);
658         }
659
660       ip6_enable (sw_if_index);
661       send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT,
662                                       0, 1);
663     }
664   else if (old_enabled && !enable)
665     {
666       send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0);
667
668       rm->n_clients--;
669       if (rm->n_clients == 0)
670         {
671           dhcp6_clients_enable_disable (0);
672           disable_process ();
673         }
674
675       /* *INDENT-OFF* */
676       pool_foreach (address_info, rm->address_pool,
677       ({
678         if (address_info->sw_if_index == sw_if_index)
679           {
680             ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
681                     rm->client_state_by_sw_if_index[sw_if_index].enabled);
682             client_state_t *client_state =
683               &rm->client_state_by_sw_if_index[sw_if_index];
684             send_client_message_start_stop (sw_if_index,
685                                             client_state->server_index,
686                                             DHCPV6_MSG_RELEASE, address_info,
687                                             1);
688             error = ip6_add_del_interface_address (vm, sw_if_index,
689                                                    &address_info->address,
690                                                    64, 1);
691             if (error)
692                 clib_warning ("Failed to delete interface address");
693             pool_put (rm->address_pool, address_info);
694           }
695       }));
696       /* *INDENT-ON* */
697     }
698
699   if (!enable)
700     client_state->enabled = 0;
701
702   return 0;
703 }
704
705 static clib_error_t *
706 dhcp6_client_enable_disable_command_fn (vlib_main_t * vm,
707                                         unformat_input_t * input,
708                                         vlib_cli_command_t * cmd)
709 {
710   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
711   vnet_main_t *vnm = rm->vnet_main;
712   clib_error_t *error = 0;
713   u32 sw_if_index = ~0;
714   u8 enable = 1;
715   unformat_input_t _line_input, *line_input = &_line_input;
716
717   if (!unformat_user (input, unformat_line_input, line_input))
718     return 0;
719
720   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
721     {
722       if (unformat
723           (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
724         ;
725       else if (unformat (line_input, "disable"))
726         enable = 0;
727       else
728         {
729           error = clib_error_return (0, "unexpected input `%U'",
730                                      format_unformat_error, line_input);
731           goto done;
732         }
733     }
734
735   unformat_free (line_input);
736
737   if (sw_if_index != ~0)
738     {
739       if (dhcp6_client_enable_disable (sw_if_index, enable) != 0)
740         error = clib_error_return (0, "Invalid sw_if_index");
741     }
742   else
743     error = clib_error_return (0, "Missing sw_if_index");
744
745 done:
746   return error;
747 }
748
749 /*?
750  * This command is used to enable/disable DHCPv6 client
751  * on particular interface.
752  *
753  * @cliexpar
754  * @parblock
755  * Example of how to enable DHCPv6 client:
756  * @cliexcmd{dhcp6 client GigabitEthernet2/0/0}
757  * Example of how to disable DHCPv6 client:
758  * @cliexcmd{dhcp6 client GigabitEthernet2/0/0 disable}
759  * @endparblock
760 ?*/
761 /* *INDENT-OFF* */
762 VLIB_CLI_COMMAND (dhcp6_client_enable_disable_command, static) = {
763   .path = "dhcp6 client",
764   .short_help = "dhcp6 client <interface> [disable]",
765   .function = dhcp6_client_enable_disable_command_fn,
766 };
767 /* *INDENT-ON* */
768
769 static void
770   vl_api_dhcp6_client_enable_disable_t_handler
771   (vl_api_dhcp6_client_enable_disable_t * mp)
772 {
773   vl_api_dhcp6_client_enable_disable_reply_t *rmp;
774   u32 sw_if_index;
775   int rv = 0;
776
777   VALIDATE_SW_IF_INDEX (mp);
778
779   sw_if_index = ntohl (mp->sw_if_index);
780
781   rv = dhcp6_client_enable_disable (sw_if_index, mp->enable);
782
783   BAD_SW_IF_INDEX_LABEL;
784
785   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
786 }
787
788 #define vl_msg_name_crc_list
789 #include <vnet/dhcp/dhcp6_ia_na_client_cp.api.h>
790 #undef vl_msg_name_crc_list
791
792 static void
793 setup_message_id_table (api_main_t * am)
794 {
795 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
796   foreach_vl_msg_name_crc_dhcp6_ia_na_client_cp;
797 #undef _
798 }
799
800 static clib_error_t *
801 dhcp_client_cp_init (vlib_main_t * vm)
802 {
803   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
804   api_main_t *am = &api_main;
805
806   rm->vlib_main = vm;
807   rm->vnet_main = vnet_get_main ();
808   rm->api_main = am;
809   rm->node_index = dhcp6_client_cp_process_node.index;
810
811 #define _(N,n)                                                  \
812     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
813                            vl_api_##n##_t_handler,              \
814                            vl_noop_handler,                     \
815                            vl_api_##n##_t_endian,               \
816                            vl_api_##n##_t_print,                \
817                            sizeof(vl_api_##n##_t), 0/* do NOT trace! */);
818   foreach_dhcp6_client_cp_msg;
819 #undef _
820
821   /*
822    * Set up the (msg_name, crc, message-id) table
823    */
824   setup_message_id_table (am);
825
826   return 0;
827 }
828
829 VLIB_INIT_FUNCTION (dhcp_client_cp_init);
830
831 /*
832  * fd.io coding-style-patch-verification: ON
833  *
834  * Local Variables:
835  * eval: (c-set-style "gnu")
836  * End:
837  */