misc: sprintf be gone
[vpp.git] / src / plugins / dhcp / dhcp6_pd_client_cp.c
index 1846627..077b4a3 100644 (file)
@@ -70,6 +70,8 @@ typedef struct
 {
   prefix_info_t *prefix_pool;
   const u8 **prefix_group_name_by_index;
+  /* vector of active prefix pool indicies, prep-H for pool_foreach(..) */
+  u32 *indices;
 } ip6_prefix_main_t;
 
 static ip6_prefix_main_t ip6_prefix_main;
@@ -277,6 +279,7 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp)
   f64 current_time;
   clib_error_t *error = 0;
   u32 i;
+  prefix_info_t *prefix_info;
 
   current_time = vlib_time_now (vm);
 
@@ -363,13 +366,26 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp)
   send_client_message_start_stop (sw_if_index, server_index,
                                  mp->msg_type, 0, 0);
 
+  vec_reset_length (pm->indices);
+  /*
+   * We're going to loop through the pool multiple times,
+   * so collect active indices.
+   */
+  /* *INDENT-OFF* */
+  pool_foreach (prefix_info, pm->prefix_pool,
+  ({
+    vec_add1 (pm->indices, prefix_info - pm->prefix_pool);
+  }));
+  /* *INDENT-ON* */
+
   for (i = 0; i < n_prefixes; i++)
     {
-      prefix_info_t *prefix_info = 0;
       u8 prefix_length;
       u32 valid_time;
       u32 preferred_time;
+      int j;
 
+      prefix_info = 0;
       api_prefix = &mp->prefixes[i];
 
       prefix = (ip6_address_t *) api_prefix->prefix.address;
@@ -386,32 +402,40 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp)
        continue;
 
       u8 address_prefix_present = 0;
-      /* *INDENT-OFF* */
-      pool_foreach (prefix_info, pm->prefix_pool,
-      ({
-        if (is_dhcpv6_pd_prefix (prefix_info) &&
-            prefix_info->opaque_data == sw_if_index &&
-            prefix_info->prefix_length == prefix_length &&
-            ip6_prefixes_equal (&prefix_info->prefix, prefix, prefix_length))
-          {
-            address_prefix_present = 1;
-            goto prefix_pool_foreach_out;
-          }
-      }));
-      /* *INDENT-ON* */
-    prefix_pool_foreach_out:
+
+      /* Look for a matching prefix_info */
+      for (j = 0; j < vec_len (pm->indices); j++)
+       {
+         prefix_info = pool_elt_at_index (pm->prefix_pool, pm->indices[j]);
+
+         if (is_dhcpv6_pd_prefix (prefix_info) &&
+             prefix_info->opaque_data == sw_if_index &&
+             prefix_info->prefix_length == prefix_length &&
+             ip6_prefixes_equal (&prefix_info->prefix, prefix,
+                                 prefix_length))
+           {
+             address_prefix_present = 1;
+             break;
+           }
+       }
 
       if (address_prefix_present)
        {
+         /* Found the (primary) prefix, update prefix timers */
+         prefix_info->preferred_lt = preferred_time;
+         prefix_info->valid_lt = valid_time;
+         prefix_info->due_time = current_time + valid_time;
+         if (prefix_info->due_time > rm->max_valid_due_time)
+           rm->max_valid_due_time = prefix_info->due_time;
+
          /*
-          * We found the prefix. Move along.
-          * Don't touch the prefix timers!
-          * If we happen to receive a renew reply just before we
-          * would have sent a solicit to renew the prefix delegation,
-          * we forget to renew the delegation. Worse luck, we start
-          * sending router advertisements with a valid time of zero,
-          * and the wheels fall off...
+          * Tell the RA code to update any secondary per-interface
+          * timers that it might be hoarding.
           */
+         ip6_ra_update_secondary_radv_info
+           (prefix, prefix_length,
+            prefix_info->opaque_data /* sw_if_index */ ,
+            valid_time, preferred_time);
          continue;
        }
 
@@ -419,6 +443,7 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp)
        continue;
 
       pool_get (pm->prefix_pool, prefix_info);
+      vec_add1 (pm->indices, prefix_info - pm->prefix_pool);
       prefix_info->prefix_group_index = client_state->prefix_group_index;
       set_is_dhcpv6_pd_prefix (prefix_info, 1);
       prefix_info->opaque_data = sw_if_index;
@@ -1151,8 +1176,8 @@ ip6_pd_clients_show_command_function (vlib_main_t * vm,
   client_state_t *cs;
   f64 current_time = vlib_time_now (vm);
   const u8 *prefix_group;
-  char buf1[256];
-  char buf2[256];
+  u8 *buf1 = 0;
+  u8 *buf2 = 0;
   const char *rebinding;
   u32 i;
 
@@ -1161,18 +1186,20 @@ ip6_pd_clients_show_command_function (vlib_main_t * vm,
       cs = &rm->client_state_by_sw_if_index[i];
       if (cs->enabled)
        {
+         vec_reset_length (buf1);
+         vec_reset_length (buf2);
          if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time)
            {
-             sprintf (buf1, "%u remaining",
-                      (u32) round (cs->T1_due_time - current_time));
+             buf1 = format (buf1, "%u remaining",
+                            (u32) round (cs->T1_due_time - current_time));
            }
          else
-           sprintf (buf1, "timeout");
+           buf1 = format (buf1, "timeout");
          if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time)
-           sprintf (buf2, "%u remaining",
-                    (u32) round (cs->T2_due_time - current_time));
+           buf2 = format (buf2, "%u remaining",
+                          (u32) round (cs->T2_due_time - current_time));
          else
-           sprintf (buf2, "timeout");
+           buf2 = format (buf2, "timeout");
          if (cs->rebinding)
            rebinding = ", REBINDING";
          else
@@ -1181,8 +1208,8 @@ ip6_pd_clients_show_command_function (vlib_main_t * vm,
            pm->prefix_group_name_by_index[cs->prefix_group_index];
          if (cs->T1)
            vlib_cli_output (vm,
-                            "sw_if_index: %u, prefix group: %s, T1: %u (%s), "
-                            "T2: %u (%s), server index: %d%s", i,
+                            "sw_if_index: %u, prefix group: %s, T1: %u (%v), "
+                            "T2: %u (%v), server index: %d%s", i,
                             prefix_group, cs->T1, buf1, cs->T2, buf2,
                             cs->server_index, rebinding);
          else
@@ -1191,6 +1218,9 @@ ip6_pd_clients_show_command_function (vlib_main_t * vm,
        }
     }
 
+  vec_free (buf1);
+  vec_free (buf2);
+
   return error;
 }