vlog: Apply rate-limiting everywhere it seems warranted.
[sliver-openvswitch.git] / lib / dhcp-client.c
index 2c7e487..a43c01c 100644 (file)
@@ -51,6 +51,7 @@
 #include "netdev.h"
 #include "ofp-print.h"
 #include "poll-loop.h"
+#include "sat-math.h"
 #include "timeval.h"
 
 #define THIS_MODULE VLM_dhcp_client
@@ -72,6 +73,8 @@ enum dhclient_state {
 #undef DHCLIENT_STATE
 };
 
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
+
 static const char *
 state_name(enum dhclient_state state)
 {
@@ -140,9 +143,6 @@ static unsigned int calc_t2(unsigned int lease);
 static unsigned int calc_t1(unsigned int lease, unsigned int t2);
 
 static unsigned int clamp(unsigned int x, unsigned int min, unsigned int max);
-static unsigned int sat_add(unsigned int x, unsigned int y);
-static unsigned int sat_sub(unsigned int x, unsigned int y);
-static unsigned int sat_mul(unsigned int x, unsigned int y);
 
 /* Creates a new DHCP client to configure the network device 'netdev_name'
  * (e.g. "eth0").
@@ -305,6 +305,29 @@ dhclient_changed(struct dhclient *cli)
     return changed;
 }
 
+/* Returns 'cli''s current state, as a string.  The caller must not modify or
+ * free the string. */
+const char *
+dhclient_get_state(const struct dhclient *cli)
+{
+    return state_name(cli->state);
+}
+
+/* Returns the number of seconds spent so far in 'cli''s current state. */
+unsigned int
+dhclient_get_state_elapsed(const struct dhclient *cli)
+{
+    return elapsed_in_this_state(cli);
+}
+
+/* If 'cli' is bound, returns the number of seconds remaining in its lease;
+ * otherwise, returns 0. */
+unsigned int
+dhclient_get_lease_remaining(const struct dhclient *cli)
+{
+    return dhclient_is_bound(cli) ? cli->lease_expiration - time_now() : 0;
+}
+
 /* If 'cli' is bound to an IP address, returns that IP address; otherwise,
  * returns 0. */
 uint32_t
@@ -520,14 +543,15 @@ dhcp_receive(struct dhclient *cli, unsigned int msgs, struct dhcp_msg *msg)
 {
     while (do_receive_msg(cli, msg)) {
         if (msg->type < 0 || msg->type > 31 || !((1u << msg->type) & msgs)) {
-            VLOG_DBG("received unexpected %s in %s state: %s",
-                     dhcp_type_name(msg->type), state_name(cli->state),
-                     dhcp_msg_to_string(msg, false, &cli->s));
+            VLOG_DBG_RL(&rl, "received unexpected %s in %s state: %s",
+                        dhcp_type_name(msg->type), state_name(cli->state),
+                        dhcp_msg_to_string(msg, false, &cli->s));
         } else if (msg->xid != cli->xid) {
-            VLOG_DBG("ignoring %s with xid != %08"PRIx32" in %s state: %s",
-                     dhcp_type_name(msg->type), msg->xid,
-                     state_name(cli->state),
-                     dhcp_msg_to_string(msg, false, &cli->s));
+            VLOG_DBG_RL(&rl,
+                        "ignoring %s with xid != %08"PRIx32" in %s state: %s",
+                        dhcp_type_name(msg->type), msg->xid,
+                        state_name(cli->state),
+                        dhcp_msg_to_string(msg, false, &cli->s));
         } else {
             return true;
         }
@@ -541,18 +565,18 @@ validate_offered_options(struct dhclient *cli, const struct dhcp_msg *msg)
 {
     uint32_t lease, netmask;
     if (!dhcp_msg_get_secs(msg, DHCP_CODE_LEASE_TIME, 0, &lease)) {
-        VLOG_WARN("%s lacks lease time: %s", dhcp_type_name(msg->type),
-                  dhcp_msg_to_string(msg, false, &cli->s));
+        VLOG_WARN_RL(&rl, "%s lacks lease time: %s", dhcp_type_name(msg->type),
+                     dhcp_msg_to_string(msg, false, &cli->s));
     } else if (!dhcp_msg_get_ip(msg, DHCP_CODE_SUBNET_MASK, 0, &netmask)) {
-        VLOG_WARN("%s lacks netmask: %s", dhcp_type_name(msg->type),
-                  dhcp_msg_to_string(msg, false, &cli->s));
+        VLOG_WARN_RL(&rl, "%s lacks netmask: %s", dhcp_type_name(msg->type),
+                     dhcp_msg_to_string(msg, false, &cli->s));
     } else if (lease < MIN_ACCEPTABLE_LEASE) {
-        VLOG_WARN("Ignoring %s with %"PRIu32"-second lease time: %s",
-                  dhcp_type_name(msg->type), lease,
-                  dhcp_msg_to_string(msg, false, &cli->s));
+        VLOG_WARN_RL(&rl, "Ignoring %s with %"PRIu32"-second lease time: %s",
+                     dhcp_type_name(msg->type), lease,
+                     dhcp_msg_to_string(msg, false, &cli->s));
     } else if (cli->validate_offer && !cli->validate_offer(msg, cli->aux)) {
-        VLOG_DBG("client validation hook refused offer: %s",
-                 dhcp_msg_to_string(msg, false, &cli->s));
+        VLOG_DBG_RL(&rl, "client validation hook refused offer: %s",
+                    dhcp_msg_to_string(msg, false, &cli->s));
     } else {
         return true;
     }
@@ -575,13 +599,13 @@ dhclient_run_SELECTING(struct dhclient *cli)
         }
         if (!dhcp_msg_get_ip(&msg, DHCP_CODE_SERVER_IDENTIFIER,
                              0, &cli->server_ip)) {
-            VLOG_WARN("DHCPOFFER lacks server identifier: %s",
-                      dhcp_msg_to_string(&msg, false, &cli->s));
+            VLOG_WARN_RL(&rl, "DHCPOFFER lacks server identifier: %s",
+                         dhcp_msg_to_string(&msg, false, &cli->s));
             continue;
         }
 
-        VLOG_DBG("accepting DHCPOFFER: %s",
-                 dhcp_msg_to_string(&msg, false, &cli->s));
+        VLOG_DBG_RL(&rl, "accepting DHCPOFFER: %s",
+                    dhcp_msg_to_string(&msg, false, &cli->s));
         cli->ipaddr = msg.yiaddr;
         state_transition(cli, S_REQUESTING);
         break;
@@ -880,20 +904,21 @@ do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg)
         ip = b.l3;
         if (IP_IS_FRAGMENT(ip->ip_frag_off)) {
             /* We don't do reassembly. */
-            VLOG_WARN("ignoring fragmented DHCP datagram");
+            VLOG_WARN_RL(&rl, "ignoring fragmented DHCP datagram");
             continue;
         }
 
         dhcp = b.l7;
         if (!dhcp) {
-            VLOG_WARN("ignoring DHCP datagram with missing payload");
+            VLOG_WARN_RL(&rl, "ignoring DHCP datagram with missing payload");
             continue;
         }
 
         buffer_pull(&b, b.l7 - b.data);
         error = dhcp_parse(msg, &b);
         if (!error) {
-            VLOG_DBG("received %s", dhcp_msg_to_string(msg, false, &cli->s));
+            VLOG_DBG_RL(&rl, "received %s",
+                        dhcp_msg_to_string(msg, false, &cli->s));
             buffer_uninit(&b);
             return true;
         }
@@ -990,25 +1015,6 @@ fuzz(unsigned int x, int max_fuzz)
     return fuzz >= 0 ? (y >= x ? y : UINT_MAX) : (y <= x ? y : 0);
 }
 
-static unsigned int
-sat_add(unsigned int x, unsigned int y)
-{
-    return x + y >= x ? x + y : UINT_MAX;
-}
-
-static unsigned int
-sat_sub(unsigned int x, unsigned int y)
-{
-    return x >= y ? x - y : 0;
-}
-
-static unsigned int
-sat_mul(unsigned int x, unsigned int y)
-{
-    assert(y);
-    return x <= UINT_MAX / y ? x * y : UINT_MAX;
-}
-
 static unsigned int
 clamp(unsigned int x, unsigned int min, unsigned int max)
 {