Implement serializing the state of packet traversal in "continuations".
[cascardo/ovs.git] / lib / learning-switch.c
index b3f79ea..19a90db 100644 (file)
@@ -354,12 +354,9 @@ lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
         return;
     }
 
-    switch (type) {
-    case OFPTYPE_ECHO_REQUEST:
+    if (type == OFPTYPE_ECHO_REQUEST) {
         process_echo_request(sw, msg->data);
-        break;
-
-    case OFPTYPE_FEATURES_REPLY:
+    } else if (type == OFPTYPE_FEATURES_REPLY) {
         if (sw->state == S_FEATURES_REPLY) {
             if (!process_switch_features(sw, msg->data)) {
                 sw->state = S_SWITCHING;
@@ -367,93 +364,15 @@ lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
                 rconn_disconnect(sw->rconn);
             }
         }
-        break;
-
-    case OFPTYPE_PACKET_IN:
+    } else if (type == OFPTYPE_PACKET_IN) {
         process_packet_in(sw, msg->data);
-        break;
-
-    case OFPTYPE_FLOW_REMOVED:
+    } else if (type == OFPTYPE_FLOW_REMOVED) {
         /* Nothing to do. */
-        break;
-
-    case OFPTYPE_HELLO:
-    case OFPTYPE_ERROR:
-    case OFPTYPE_ECHO_REPLY:
-    case OFPTYPE_FEATURES_REQUEST:
-    case OFPTYPE_GET_CONFIG_REQUEST:
-    case OFPTYPE_GET_CONFIG_REPLY:
-    case OFPTYPE_SET_CONFIG:
-    case OFPTYPE_PORT_STATUS:
-    case OFPTYPE_PACKET_OUT:
-    case OFPTYPE_FLOW_MOD:
-    case OFPTYPE_GROUP_MOD:
-    case OFPTYPE_PORT_MOD:
-    case OFPTYPE_TABLE_MOD:
-    case OFPTYPE_BARRIER_REQUEST:
-    case OFPTYPE_BARRIER_REPLY:
-    case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
-    case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
-    case OFPTYPE_DESC_STATS_REQUEST:
-    case OFPTYPE_DESC_STATS_REPLY:
-    case OFPTYPE_FLOW_STATS_REQUEST:
-    case OFPTYPE_FLOW_STATS_REPLY:
-    case OFPTYPE_AGGREGATE_STATS_REQUEST:
-    case OFPTYPE_AGGREGATE_STATS_REPLY:
-    case OFPTYPE_TABLE_STATS_REQUEST:
-    case OFPTYPE_TABLE_STATS_REPLY:
-    case OFPTYPE_PORT_STATS_REQUEST:
-    case OFPTYPE_PORT_STATS_REPLY:
-    case OFPTYPE_QUEUE_STATS_REQUEST:
-    case OFPTYPE_QUEUE_STATS_REPLY:
-    case OFPTYPE_PORT_DESC_STATS_REQUEST:
-    case OFPTYPE_PORT_DESC_STATS_REPLY:
-    case OFPTYPE_ROLE_REQUEST:
-    case OFPTYPE_ROLE_REPLY:
-    case OFPTYPE_ROLE_STATUS:
-    case OFPTYPE_REQUESTFORWARD:
-    case OFPTYPE_SET_FLOW_FORMAT:
-    case OFPTYPE_FLOW_MOD_TABLE_ID:
-    case OFPTYPE_SET_PACKET_IN_FORMAT:
-    case OFPTYPE_FLOW_AGE:
-    case OFPTYPE_SET_CONTROLLER_ID:
-    case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
-    case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
-    case OFPTYPE_FLOW_MONITOR_CANCEL:
-    case OFPTYPE_FLOW_MONITOR_PAUSED:
-    case OFPTYPE_FLOW_MONITOR_RESUMED:
-    case OFPTYPE_GET_ASYNC_REQUEST:
-    case OFPTYPE_GET_ASYNC_REPLY:
-    case OFPTYPE_SET_ASYNC_CONFIG:
-    case OFPTYPE_METER_MOD:
-    case OFPTYPE_GROUP_STATS_REQUEST:
-    case OFPTYPE_GROUP_STATS_REPLY:
-    case OFPTYPE_GROUP_DESC_STATS_REQUEST:
-    case OFPTYPE_GROUP_DESC_STATS_REPLY:
-    case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
-    case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
-    case OFPTYPE_METER_STATS_REQUEST:
-    case OFPTYPE_METER_STATS_REPLY:
-    case OFPTYPE_METER_CONFIG_STATS_REQUEST:
-    case OFPTYPE_METER_CONFIG_STATS_REPLY:
-    case OFPTYPE_METER_FEATURES_STATS_REQUEST:
-    case OFPTYPE_METER_FEATURES_STATS_REPLY:
-    case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
-    case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
-    case OFPTYPE_TABLE_DESC_REQUEST:
-    case OFPTYPE_TABLE_DESC_REPLY:
-    case OFPTYPE_BUNDLE_CONTROL:
-    case OFPTYPE_BUNDLE_ADD_MESSAGE:
-    case OFPTYPE_NXT_TLV_TABLE_MOD:
-    case OFPTYPE_NXT_TLV_TABLE_REQUEST:
-    case OFPTYPE_NXT_TLV_TABLE_REPLY:
-    default:
-        if (VLOG_IS_DBG_ENABLED()) {
-            char *s = ofp_to_string(msg->data, msg->size, 2);
-            VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
-                        sw->datapath_id, s);
-            free(s);
-        }
+    } else if (VLOG_IS_DBG_ENABLED()) {
+        char *s = ofp_to_string(msg->data, msg->size, 2);
+        VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
+                    sw->datapath_id, s);
+        free(s);
     }
 }
 \f
@@ -497,10 +416,9 @@ process_switch_features(struct lswitch *sw, struct ofp_header *oh)
 {
     struct ofputil_switch_features features;
     struct ofputil_phy_port port;
-    enum ofperr error;
-    struct ofpbuf b;
 
-    error = ofputil_decode_switch_features(oh, &features, &b);
+    struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
+    enum ofperr error = ofputil_pull_switch_features(&b, &features);
     if (error) {
         VLOG_ERR("received invalid switch feature reply (%s)",
                  ofperr_to_string(error));
@@ -593,6 +511,7 @@ static void
 process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
 {
     struct ofputil_packet_in pi;
+    uint32_t buffer_id;
     uint32_t queue_id;
     ofp_port_t out_port;
 
@@ -605,7 +524,7 @@ process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
     struct dp_packet pkt;
     struct flow flow;
 
-    error = ofputil_decode_packet_in(&pi, oh);
+    error = ofputil_decode_packet_in(oh, true, &pi, NULL, &buffer_id, NULL);
     if (error) {
         VLOG_WARN_RL(&rl, "failed to decode packet-in: %s",
                      ofperr_to_string(error));
@@ -619,7 +538,7 @@ process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
         return;
     }
 
-    /* Extract flow data from 'opi' into 'flow'. */
+    /* Extract flow data from 'pi' into 'flow'. */
     dp_packet_use_const(&pkt, pi.packet, pi.packet_len);
     flow_extract(&pkt, &flow);
     flow.in_port.ofp_port = pi.flow_metadata.flow.in_port.ofp_port;
@@ -643,8 +562,8 @@ process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
     }
 
     /* Prepare packet_out in case we need one. */
-    po.buffer_id = pi.buffer_id;
-    if (po.buffer_id == UINT32_MAX) {
+    po.buffer_id = buffer_id;
+    if (buffer_id == UINT32_MAX) {
         po.packet = dp_packet_data(&pkt);
         po.packet_len = dp_packet_size(&pkt);
     } else {
@@ -664,7 +583,7 @@ process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
             .table_id = 0xff,
             .command = OFPFC_ADD,
             .idle_timeout = sw->max_idle,
-            .buffer_id = pi.buffer_id,
+            .buffer_id = buffer_id,
             .out_port = OFPP_NONE,
             .ofpacts = ofpacts.data,
             .ofpacts_len = ofpacts.size,
@@ -677,13 +596,13 @@ process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
         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) {
+        if (buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
             queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
         }
     } 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) {
+        if (buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
             queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
         }
     }