vlog: Apply rate-limiting everywhere it seems warranted.
[openvswitch] / lib / learning-switch.c
index b477e7051cbb49bbd527cf81842d51a8a48c4a3e..f056b91610718e12aef3fc7e54c46c6db46485bd 100644 (file)
@@ -47,6 +47,7 @@
 #include "openflow.h"
 #include "queue.h"
 #include "rconn.h"
+#include "timeval.h"
 #include "vconn.h"
 #include "xtoxll.h"
 
@@ -62,8 +63,15 @@ struct lswitch {
     uint64_t datapath_id;
     time_t last_features_request;
     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
+
+    /* Number of outgoing queued packets on the rconn. */
+    int n_queued;
 };
 
+/* The log messages here could actually be useful in debugging, so keep the
+ * rate limit relatively high. */
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
+
 static void queue_tx(struct lswitch *, struct rconn *, struct buffer *);
 static void send_features_request(struct lswitch *, struct rconn *);
 static void process_packet_in(struct lswitch *, struct rconn *,
@@ -84,11 +92,10 @@ static void process_echo_request(struct lswitch *, struct rconn *,
 struct lswitch *
 lswitch_create(struct rconn *rconn, bool learn_macs, int max_idle)
 {
-    struct lswitch *sw = xmalloc(sizeof *sw);
-    memset(sw, 0, sizeof *sw);
+    struct lswitch *sw = xcalloc(1, sizeof *sw);
     sw->max_idle = max_idle;
     sw->datapath_id = 0;
-    sw->last_features_request = time(0) - 1;
+    sw->last_features_request = time_now() - 1;
     sw->ml = learn_macs ? mac_learning_create() : NULL;
     send_features_request(sw, rconn);
     return sw;
@@ -121,9 +128,10 @@ lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
 
     oh = msg->data;
     if (msg->size < min_size[oh->type]) {
-        VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
-                  rconn_get_name(rconn),
-                  msg->size, oh->type, min_size[oh->type]);
+        VLOG_WARN_RL(&rl,
+                     "%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
+                     rconn_get_name(rconn),
+                     msg->size, oh->type, min_size[oh->type]);
         return;
     }
 
@@ -139,7 +147,7 @@ lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
     } else {
         if (VLOG_IS_DBG_ENABLED()) {
             char *p = ofp_to_string(msg->data, msg->size, 2);
-            VLOG_DBG("OpenFlow packet ignored: %s", p);
+            VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", p);
             free(p);
         }
     }
@@ -148,7 +156,7 @@ lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
 static void
 send_features_request(struct lswitch *sw, struct rconn *rconn)
 {
-    time_t now = time(0);
+    time_t now = time_now();
     if (now >= sw->last_features_request + 1) {
         struct buffer *b;
         struct ofp_header *ofr;
@@ -181,18 +189,14 @@ send_features_request(struct lswitch *sw, struct rconn *rconn)
 static void
 queue_tx(struct lswitch *sw, struct rconn *rconn, struct buffer *b)
 {
-    int retval = rconn_send(rconn, b);
-    if (retval) {
+    int retval = rconn_send_with_limit(rconn, b, &sw->n_queued, 10);
+    if (retval && retval != ENOTCONN) {
         if (retval == EAGAIN) {
-            /* FIXME: ratelimit. */
-            VLOG_WARN("%s: tx queue overflow", rconn_get_name(rconn));
-        } else if (retval == ENOTCONN) {
-            /* Ignore. */
+            VLOG_WARN_RL(&rl, "%s: tx queue overflow", rconn_get_name(rconn));
         } else {
-            /* FIXME: ratelimit. */
-            VLOG_WARN("%s: send: %s", rconn_get_name(rconn), strerror(retval));
+            VLOG_WARN_RL(&rl, "%s: send: %s",
+                         rconn_get_name(rconn), strerror(retval));
         }
-        buffer_delete(b);
     }
 }
 
@@ -216,14 +220,18 @@ process_packet_in(struct lswitch *sw, struct rconn *rconn,
 
     if (sw->ml) {
         if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
-            VLOG_DBG("learned that "ETH_ADDR_FMT" is on datapath %"
-                     PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
-                     ntohll(sw->datapath_id), in_port);
+            VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on datapath %"
+                        PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
+                        ntohll(sw->datapath_id), in_port);
         }
         out_port = mac_learning_lookup(sw->ml, flow.dl_dst);
     }
 
-    if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
+    if (in_port == out_port) {
+        /* The input and output port match.  Set up a flow to drop packets. */
+        queue_tx(sw, rconn, make_add_flow(&flow, ntohl(opi->buffer_id),
+                                          sw->max_idle, 0));
+    } else if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
         /* The output port is known, or we always flood everything, so add a
          * new flow. */
         queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),