learning-switch: Make lswitch own its rconn.
[openvswitch] / lib / learning-switch.c
index b7a435c0ecb49e2c41a312b182fd6485c6ec4552..d53f147a3656952caa6568e2ed7314021f344a1e 100644 (file)
@@ -53,6 +53,8 @@ struct lswitch_port {
 };
 
 struct lswitch {
+    struct rconn *rconn;
+
     /* If nonnegative, the switch sets up flows that expire after the given
      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
      * Otherwise, the switch processes every packet. */
@@ -72,21 +74,24 @@ struct lswitch {
 
     /* Number of outgoing queued packets on the rconn. */
     struct rconn_packet_counter *queued;
+
+    /* If true, do not reply to any messages from the switch (for debugging
+     * fail-open mode). */
+    bool mute;
 };
 
 /* 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 ofpbuf *);
-static void send_features_request(struct lswitch *, struct rconn *);
+static void queue_tx(struct lswitch *, struct ofpbuf *);
+static void send_features_request(struct lswitch *);
 
+static void lswitch_process_packet(struct lswitch *, const struct ofpbuf *);
 static enum ofperr process_switch_features(struct lswitch *,
                                            struct ofp_header *);
-static void process_packet_in(struct lswitch *, struct rconn *,
-                              const struct ofp_header *);
-static void process_echo_request(struct lswitch *, struct rconn *,
-                                 const struct ofp_header *);
+static void process_packet_in(struct lswitch *, const struct ofp_header *);
+static void process_echo_request(struct lswitch *, const struct ofp_header *);
 
 /* Creates and returns a new learning switch whose configuration is given by
  * 'cfg'.
@@ -99,6 +104,7 @@ lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
     struct lswitch *sw;
 
     sw = xzalloc(sizeof *sw);
+    sw->rconn = rconn;
     sw->max_idle = cfg->max_idle;
     sw->datapath_id = 0;
     sw->last_features_request = time_now() - 1;
@@ -141,7 +147,7 @@ lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
     }
 
     sw->queued = rconn_packet_counter_create();
-    send_features_request(sw, rconn);
+    send_features_request(sw);
 
     protocol = ofputil_protocol_from_ofp_version(rconn_get_version(rconn));
     if (cfg->default_flows) {
@@ -185,6 +191,12 @@ lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
     return sw;
 }
 
+bool
+lswitch_is_alive(const struct lswitch *sw)
+{
+    return rconn_is_alive(sw->rconn);
+}
+
 /* Destroys 'sw'. */
 void
 lswitch_destroy(struct lswitch *sw)
@@ -192,6 +204,7 @@ lswitch_destroy(struct lswitch *sw)
     if (sw) {
         struct lswitch_port *node, *next;
 
+        rconn_destroy(sw->rconn);
         HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
             hmap_remove(&sw->queue_numbers, &node->hmap_node);
             free(node);
@@ -208,9 +221,27 @@ lswitch_destroy(struct lswitch *sw)
 void
 lswitch_run(struct lswitch *sw)
 {
+    int i;
+
     if (sw->ml) {
         mac_learning_run(sw->ml, NULL);
     }
+
+    rconn_run(sw->rconn);
+
+    for (i = 0; i < 50; i++) {
+        struct ofpbuf *msg;
+
+        msg = rconn_recv(sw->rconn);
+        if (!msg) {
+            break;
+        }
+
+        if (!sw->mute) {
+            lswitch_process_packet(sw, msg);
+        }
+        ofpbuf_delete(msg);
+    }
 }
 
 void
@@ -219,15 +250,16 @@ lswitch_wait(struct lswitch *sw)
     if (sw->ml) {
         mac_learning_wait(sw->ml);
     }
+    rconn_run(sw->rconn);
+    rconn_recv_wait(sw->rconn);
 }
 
 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
  * to the learning switch state in 'sw'.  The most likely result of processing
  * is that flow-setup and packet-out OpenFlow messages will be sent out on
  * 'rconn'.  */
-void
-lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
-                       const struct ofpbuf *msg)
+static void
+lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
 {
     enum ofptype type;
     struct ofpbuf b;
@@ -240,13 +272,13 @@ lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
     if (sw->datapath_id == 0
         && type != OFPTYPE_ECHO_REQUEST
         && type != OFPTYPE_FEATURES_REPLY) {
-        send_features_request(sw, rconn);
+        send_features_request(sw);
         return;
     }
 
     switch (type) {
     case OFPTYPE_ECHO_REQUEST:
-        process_echo_request(sw, rconn, msg->data);
+        process_echo_request(sw, msg->data);
         break;
 
     case OFPTYPE_FEATURES_REPLY:
@@ -254,7 +286,7 @@ lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
         break;
 
     case OFPTYPE_PACKET_IN:
-        process_packet_in(sw, rconn, msg->data);
+        process_packet_in(sw, msg->data);
         break;
 
     case OFPTYPE_FLOW_REMOVED:
@@ -312,41 +344,41 @@ lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
 }
 \f
 static void
-send_features_request(struct lswitch *sw, struct rconn *rconn)
+send_features_request(struct lswitch *sw)
 {
     time_t now = time_now();
     if (now >= sw->last_features_request + 1) {
         struct ofpbuf *b;
         struct ofp_switch_config *osc;
-        int ofp_version = rconn_get_version(rconn);
+        int ofp_version = rconn_get_version(sw->rconn);
 
         assert(ofp_version > 0 && ofp_version < 0xff);
 
         /* Send OFPT_FEATURES_REQUEST. */
         b = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, ofp_version, 0);
-        queue_tx(sw, rconn, b);
+        queue_tx(sw, b);
 
         /* Send OFPT_SET_CONFIG. */
         b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, ofp_version, sizeof *osc);
         osc = ofpbuf_put_zeros(b, sizeof *osc);
         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
-        queue_tx(sw, rconn, b);
+        queue_tx(sw, b);
 
         sw->last_features_request = now;
     }
 }
 
 static void
-queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
+queue_tx(struct lswitch *sw, struct ofpbuf *b)
 {
-    int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
+    int retval = rconn_send_with_limit(sw->rconn, b, sw->queued, 10);
     if (retval && retval != ENOTCONN) {
         if (retval == EAGAIN) {
             VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
-                         sw->datapath_id, rconn_get_name(rconn));
+                         sw->datapath_id, rconn_get_name(sw->rconn));
         } else {
             VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
-                         sw->datapath_id, rconn_get_name(rconn),
+                         sw->datapath_id, rconn_get_name(sw->rconn),
                          strerror(retval));
         }
     }
@@ -441,8 +473,7 @@ get_queue_id(const struct lswitch *sw, uint16_t in_port)
 }
 
 static void
-process_packet_in(struct lswitch *sw, struct rconn *rconn,
-                  const struct ofp_header *oh)
+process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
 {
     struct ofputil_packet_in pi;
     uint32_t queue_id;
@@ -523,24 +554,23 @@ process_packet_in(struct lswitch *sw, struct rconn *rconn,
         fm.ofpacts_len = ofpacts.size;
         buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
 
-        queue_tx(sw, rconn, buffer);
+        queue_tx(sw, buffer);
 
         /* If the switch didn't buffer the packet, we need to send a copy. */
         if (pi.buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
-            queue_tx(sw, rconn, ofputil_encode_packet_out(&po));
+            queue_tx(sw, ofputil_encode_packet_out(&po));
         }
     } else {
         /* We don't know that MAC, or we don't set up flows.  Send along the
          * packet without setting up a flow. */
         if (pi.buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
-            queue_tx(sw, rconn, ofputil_encode_packet_out(&po));
+            queue_tx(sw, ofputil_encode_packet_out(&po));
         }
     }
 }
 
 static void
-process_echo_request(struct lswitch *sw, struct rconn *rconn,
-                     const struct ofp_header *rq)
+process_echo_request(struct lswitch *sw, const struct ofp_header *rq)
 {
-    queue_tx(sw, rconn, make_echo_reply(rq));
+    queue_tx(sw, make_echo_reply(rq));
 }