Add support for OFPFC_MODIFY Flow Mod command.
[openvswitch] / switch / datapath.c
index e91fc99825d794347717bf68ab32f49f13d55628..24617efbf2a20506505251fb1728e802fea295c8 100644 (file)
@@ -117,7 +117,7 @@ struct datapath {
     /* Remote connections. */
     struct remote *controller;  /* Connection to controller. */
     struct list remotes;        /* All connections (including controller). */
-    struct vconn *listen_vconn;
+    struct pvconn *listen_pvconn;
 
     time_t last_timeout;
 
@@ -212,7 +212,7 @@ dp_new(struct datapath **dp_, uint64_t dpid, struct rconn *rconn)
     dp->last_timeout = time_now();
     list_init(&dp->remotes);
     dp->controller = remote_create(dp, rconn);
-    dp->listen_vconn = NULL;
+    dp->listen_pvconn = NULL;
     dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
     dp->chain = chain_create();
     if (!dp->chain) {
@@ -277,10 +277,10 @@ dp_add_port(struct datapath *dp, const char *name)
 }
 
 void
-dp_add_listen_vconn(struct datapath *dp, struct vconn *listen_vconn)
+dp_add_listen_pvconn(struct datapath *dp, struct pvconn *listen_pvconn)
 {
-    assert(!dp->listen_vconn);
-    dp->listen_vconn = listen_vconn;
+    assert(!dp->listen_pvconn);
+    dp->listen_pvconn = listen_pvconn;
 }
 
 void
@@ -341,12 +341,12 @@ dp_run(struct datapath *dp)
     LIST_FOR_EACH_SAFE (r, rn, struct remote, node, &dp->remotes) {
         remote_run(dp, r);
     }
-    if (dp->listen_vconn) {
+    if (dp->listen_pvconn) {
         for (;;) {
             struct vconn *new_vconn;
             int retval;
 
-            retval = vconn_accept(dp->listen_vconn, &new_vconn);
+            retval = pvconn_accept(dp->listen_pvconn, OFP_VERSION, &new_vconn);
             if (retval) {
                 if (retval != EAGAIN) {
                     VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
@@ -479,8 +479,8 @@ dp_wait(struct datapath *dp)
     LIST_FOR_EACH (r, struct remote, node, &dp->remotes) {
         remote_wait(r);
     }
-    if (dp->listen_vconn) {
-        vconn_accept_wait(dp->listen_vconn);
+    if (dp->listen_pvconn) {
+        pvconn_wait(dp->listen_pvconn);
     }
 }
 
@@ -672,14 +672,11 @@ dp_send_features_reply(struct datapath *dp, const struct sender *sender)
 
     ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
                                sender, &buffer);
-    ofr->datapath_id    = htonll(dp->id); 
-    ofr->n_exact        = htonl(2 * TABLE_HASH_MAX_FLOWS);
-    ofr->n_compression  = 0;         /* Not supported */
-    ofr->n_general      = htonl(TABLE_LINEAR_MAX_FLOWS);
-    ofr->buffer_mb      = htonl(UINT32_MAX);
-    ofr->n_buffers      = htonl(N_PKT_BUFFERS);
-    ofr->capabilities   = htonl(OFP_SUPPORTED_CAPABILITIES);
-    ofr->actions        = htonl(OFP_SUPPORTED_ACTIONS);
+    ofr->datapath_id  = htonll(dp->id); 
+    ofr->n_tables     = dp->chain->n_tables;
+    ofr->n_buffers    = htonl(N_PKT_BUFFERS);
+    ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
+    ofr->actions      = htonl(OFP_SUPPORTED_ACTIONS);
     LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
         struct ofp_phy_port *opp = buffer_put_uninit(buffer, sizeof *opp);
         memset(opp, 0, sizeof *opp);
@@ -796,12 +793,11 @@ send_flow_expired(struct datapath *dp, struct sw_flow *flow,
 
 void
 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
-        uint16_t type, uint16_t code, const uint8_t *data, size_t len)
+                  uint16_t type, uint16_t code, const void *data, size_t len)
 {
     struct buffer *buffer;
     struct ofp_error_msg *oem;
-    oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR_MSG, 
-                              sender, &buffer);
+    oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR, sender, &buffer);
     oem->type = htons(type);
     oem->code = htons(code);
     memcpy(oem->data, data, len);
@@ -813,7 +809,7 @@ fill_flow_stats(struct buffer *buffer, struct sw_flow *flow,
                 int table_idx, time_t now)
 {
     struct ofp_flow_stats *ofs;
-    int length = sizeof *ofs + sizeof *ofs->actions * flow->n_actions;
+    int length = sizeof *ofs + sizeof *ofs->actions * flow->sf_acts->n_actions;
     ofs = buffer_put_uninit(buffer, length);
     ofs->length          = htons(length);
     ofs->table_id        = table_idx;
@@ -837,8 +833,8 @@ fill_flow_stats(struct buffer *buffer, struct sw_flow *flow,
     memset(ofs->pad2, 0, sizeof ofs->pad2);
     ofs->packet_count    = htonll(flow->packet_count);
     ofs->byte_count      = htonll(flow->byte_count);
-    memcpy(ofs->actions, flow->actions,
-           sizeof *ofs->actions * flow->n_actions);
+    memcpy(ofs->actions, flow->sf_acts->actions,
+           sizeof *ofs->actions * flow->sf_acts->n_actions);
 }
 
 \f
@@ -870,7 +866,8 @@ int run_flow_through_tables(struct datapath *dp, struct buffer *buffer,
     if (flow != NULL) {
         flow_used(flow, buffer);
         execute_actions(dp, buffer, port_no(dp, p),
-                        &key, flow->actions, flow->n_actions, false);
+                        &key, flow->sf_acts->actions, 
+                        flow->sf_acts->n_actions, false);
         return 0;
     } else {
         return -ESRCH;
@@ -1182,10 +1179,11 @@ add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
     flow->idle_timeout = ntohs(ofm->idle_timeout);
     flow->hard_timeout = ntohs(ofm->hard_timeout);
     flow->used = flow->created = time_now();
-    flow->n_actions = n_actions;
+    flow->sf_acts->n_actions = n_actions;
     flow->byte_count = 0;
     flow->packet_count = 0;
-    memcpy(flow->actions, ofm->actions, n_actions * sizeof *flow->actions);
+    memcpy(flow->sf_acts->actions, ofm->actions, 
+                n_actions * sizeof *flow->sf_acts->actions);
 
     /* Act. */
     error = chain_insert(dp->chain, flow);
@@ -1216,6 +1214,55 @@ error:
     return error;
 }
 
+static int
+mod_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
+{
+    int error = -ENOMEM;
+    int n_actions;
+    int i;
+    struct sw_flow_key key;
+
+
+    /* To prevent loops, make sure there's no action to send to the
+     * OFP_TABLE virtual port.
+     */
+    n_actions = (ntohs(ofm->header.length) - sizeof *ofm) 
+            / sizeof *ofm->actions;
+    for (i=0; i<n_actions; i++) {
+        const struct ofp_action *a = &ofm->actions[i];
+
+        if (a->type == htons(OFPAT_OUTPUT)
+                    && (a->arg.output.port == htons(OFPP_TABLE)
+                        || a->arg.output.port == htons(OFPP_NONE)
+                        || a->arg.output.port == ofm->match.in_port)) {
+            /* xxx Send fancy new error message? */
+            goto error;
+        }
+    }
+
+    flow_extract_match(&key, &ofm->match);
+    chain_modify(dp->chain, &key, ofm->actions, n_actions);
+
+    if (ntohl(ofm->buffer_id) != UINT32_MAX) {
+        struct buffer *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
+        if (buffer) {
+            struct sw_flow_key skb_key;
+            uint16_t in_port = ntohs(ofm->match.in_port);
+            flow_extract(buffer, in_port, &skb_key.flow);
+            execute_actions(dp, buffer, in_port, &skb_key,
+                            ofm->actions, n_actions, false);
+        } else {
+            error = -ESRCH; 
+        }
+    }
+    return error;
+
+error:
+    if (ntohl(ofm->buffer_id) != (uint32_t) -1)
+        discard_buffer(ntohl(ofm->buffer_id));
+    return error;
+}
+
 static int
 recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
           const void *msg)
@@ -1225,6 +1272,8 @@ recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
 
     if (command == OFPFC_ADD) {
         return add_flow(dp, ofm);
+    } else if (command == OFPFC_MODIFY) {
+        return mod_flow(dp, ofm);
     }  else if (command == OFPFC_DELETE) {
         struct sw_flow_key key;
         flow_extract_match(&key, &ofm->match);
@@ -1388,6 +1437,7 @@ static int table_stats_dump(struct datapath *dp, void *state,
         dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
         strncpy(ots->name, stats.name, sizeof ots->name);
         ots->table_id = i;
+        ots->wildcards = htonl(stats.wildcards);
         memset(ots->pad, 0, sizeof ots->pad);
         ots->max_entries = htonl(stats.max_flows);
         ots->active_count = htonl(stats.n_flows);
@@ -1577,6 +1627,8 @@ recv_stats_request(struct datapath *dp, const struct sender *sender,
 
     type = ntohs(rq->type);
     if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
+        dp_send_error_msg(dp, sender, OFPET_BAD_REQUEST, OFPBRC_BAD_STAT,
+                          rq, rq_len);
         VLOG_WARN_RL(&rl, "received stats request of unknown type %d", type);
         return -EINVAL;
     }
@@ -1679,21 +1731,23 @@ fwd_control_input(struct datapath *dp, const struct sender *sender,
         },
     };
 
-    const struct openflow_packet *pkt;
     struct ofp_header *oh;
 
     oh = (struct ofp_header *) msg;
-    assert(oh->version == OFP_VERSION);
-    if (oh->type >= ARRAY_SIZE(packets) || ntohs(oh->length) > length)
+    if (ntohs(oh->length) > length)
         return -EINVAL;
 
-    pkt = &packets[oh->type];
-    if (!pkt->handler)
-        return -ENOSYS;
-    if (length < pkt->min_size)
-        return -EFAULT;
-
-    return pkt->handler(dp, sender, msg);
+    if (oh->type < ARRAY_SIZE(packets)) {
+        const struct openflow_packet *pkt = &packets[oh->type];
+        if (pkt->handler) {
+            if (length < pkt->min_size)
+                return -EFAULT;
+            return pkt->handler(dp, sender, msg);
+        }
+    }
+    dp_send_error_msg(dp, sender, OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE,
+                      msg, length);
+    return -EINVAL;
 }
 \f
 /* Packet buffering. */