+ return error != OFPROTO_POSTPONE;
+}
+\f
+/* Asynchronous operations. */
+
+/* Creates and returns a new ofopgroup that is not associated with any
+ * OpenFlow connection.
+ *
+ * The caller should add operations to the returned group with
+ * ofoperation_create() and then submit it with ofopgroup_submit(). */
+static struct ofopgroup *
+ofopgroup_create_unattached(struct ofproto *ofproto)
+{
+ struct ofopgroup *group = xzalloc(sizeof *group);
+ group->ofproto = ofproto;
+ list_init(&group->ofproto_node);
+ list_init(&group->ops);
+ list_init(&group->ofconn_node);
+ return group;
+}
+
+/* Creates and returns a new ofopgroup for 'ofproto'.
+ *
+ * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
+ * connection. The 'request' and 'buffer_id' arguments are ignored.
+ *
+ * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
+ * If the ofopgroup eventually fails, then the error reply will include
+ * 'request'. If the ofopgroup eventually succeeds, then the packet with
+ * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
+ *
+ * The caller should add operations to the returned group with
+ * ofoperation_create() and then submit it with ofopgroup_submit(). */
+static struct ofopgroup *
+ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
+ const struct ofp_header *request, uint32_t buffer_id)
+{
+ struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
+ if (ofconn) {
+ size_t request_len = ntohs(request->length);
+
+ assert(ofconn_get_ofproto(ofconn) == ofproto);
+
+ ofconn_add_opgroup(ofconn, &group->ofconn_node);
+ group->ofconn = ofconn;
+ group->request = xmemdup(request, MIN(request_len, 64));
+ group->buffer_id = buffer_id;
+ }
+ return group;
+}
+
+/* Submits 'group' for processing.
+ *
+ * If 'group' contains no operations (e.g. none were ever added, or all of the
+ * ones that were added completed synchronously), then it is destroyed
+ * immediately. Otherwise it is added to the ofproto's list of pending
+ * groups. */
+static void
+ofopgroup_submit(struct ofopgroup *group)
+{
+ if (list_is_empty(&group->ops)) {
+ ofopgroup_destroy(group);
+ } else {
+ list_push_back(&group->ofproto->pending, &group->ofproto_node);
+ group->ofproto->n_pending++;
+ }
+}
+
+static void
+ofopgroup_destroy(struct ofopgroup *group)
+{
+ assert(list_is_empty(&group->ops));
+ if (!list_is_empty(&group->ofproto_node)) {
+ assert(group->ofproto->n_pending > 0);
+ group->ofproto->n_pending--;
+ list_remove(&group->ofproto_node);
+ }
+ if (!list_is_empty(&group->ofconn_node)) {
+ list_remove(&group->ofconn_node);
+ if (group->error) {
+ ofconn_send_error(group->ofconn, group->request, group->error);
+ }
+ connmgr_retry(group->ofproto->connmgr);
+ }
+ free(group->request);
+ free(group);
+}
+
+/* Initiates a new operation on 'rule', of the specified 'type', within
+ * 'group'. Prior to calling, 'rule' must not have any pending operation. */
+static void
+ofoperation_create(struct ofopgroup *group, struct rule *rule,
+ enum ofoperation_type type)
+{
+ struct ofoperation *op;
+
+ assert(!rule->pending);
+
+ op = rule->pending = xzalloc(sizeof *op);
+ op->group = group;
+ list_push_back(&group->ops, &op->group_node);
+ op->rule = rule;
+ op->type = type;
+ op->status = -1;
+ op->flow_cookie = rule->flow_cookie;
+
+ if (type == OFOPERATION_DELETE) {
+ hmap_insert(&op->group->ofproto->deletions, &op->hmap_node,
+ cls_rule_hash(&rule->cr, rule->table_id));
+ }
+}
+
+static void
+ofoperation_destroy(struct ofoperation *op)
+{
+ struct ofopgroup *group = op->group;
+
+ if (op->rule) {
+ op->rule->pending = NULL;
+ }
+ if (op->type == OFOPERATION_DELETE) {
+ hmap_remove(&group->ofproto->deletions, &op->hmap_node);
+ }
+ list_remove(&op->group_node);
+ free(op->actions);
+ free(op);
+
+ if (list_is_empty(&group->ops) && !list_is_empty(&group->ofproto_node)) {
+ ofopgroup_destroy(group);
+ }
+}
+
+/* Indicates that 'op' completed with status 'error', which is either 0 to
+ * indicate success or an OpenFlow error code (constructed with
+ * e.g. ofp_mkerr()).
+ *
+ * If 'error' is 0, indicating success, the operation will be committed
+ * permanently to the flow table. There is one interesting subcase:
+ *
+ * - If 'op' is an "add flow" operation that is replacing an existing rule in
+ * the flow table (the "victim" rule) by a new one, then the caller must
+ * have uninitialized any derived state in the victim rule, as in step 5 in
+ * the "Life Cycle" in ofproto/ofproto-provider.h. ofoperation_complete()
+ * performs steps 6 and 7 for the victim rule, most notably by calling its
+ * ->rule_dealloc() function.
+ *
+ * If 'error' is nonzero, then generally the operation will be rolled back:
+ *
+ * - If 'op' is an "add flow" operation, ofproto removes the new rule or
+ * restores the original rule. The caller must have uninitialized any
+ * derived state in the new rule, as in step 5 of in the "Life Cycle" in
+ * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
+ * and 7 for the new rule, calling its ->rule_dealloc() function.
+ *
+ * - If 'op' is a "modify flow" operation, ofproto restores the original
+ * actions.
+ *
+ * - 'op' must not be a "delete flow" operation. Removing a rule is not
+ * allowed to fail. It must always succeed.
+ *
+ * Please see the large comment in ofproto/ofproto-provider.h titled
+ * "Asynchronous Operation Support" for more information. */
+void
+ofoperation_complete(struct ofoperation *op, int error)
+{
+ struct ofopgroup *group = op->group;
+ struct rule *rule = op->rule;
+ struct classifier *table = &rule->ofproto->tables[rule->table_id];
+
+ assert(rule->pending == op);
+ assert(op->status < 0);
+ assert(error >= 0);
+
+ if (!error
+ && !group->error
+ && op->type != OFOPERATION_DELETE
+ && group->ofconn
+ && group->buffer_id != UINT32_MAX
+ && list_is_singleton(&op->group_node)) {
+ struct ofpbuf *packet;
+ uint16_t in_port;
+
+ error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
+ &packet, &in_port);
+ if (packet) {
+ assert(!error);
+ error = rule_execute(rule, in_port, packet);
+ }
+ }
+ if (!group->error) {
+ group->error = error;
+ }
+
+ switch (op->type) {
+ case OFOPERATION_ADD:
+ if (!error) {
+ if (op->victim) {
+ ofproto_rule_destroy__(op->victim);
+ }
+ } else {
+ if (op->victim) {
+ classifier_replace(table, &op->victim->cr);
+ op->victim = NULL;
+ } else {
+ classifier_remove(table, &rule->cr);
+ }
+ ofproto_rule_destroy__(rule);
+ }
+ op->victim = NULL;
+ break;
+
+ case OFOPERATION_DELETE:
+ assert(!error);
+ ofproto_rule_destroy__(rule);
+ op->rule = NULL;
+ break;
+
+ case OFOPERATION_MODIFY:
+ if (!error) {
+ rule->modified = time_msec();
+ } else {
+ free(rule->actions);
+ rule->actions = op->actions;
+ rule->n_actions = op->n_actions;
+ op->actions = NULL;
+ }
+ break;
+
+ default:
+ NOT_REACHED();
+ }
+ ofoperation_destroy(op);
+}
+
+struct rule *
+ofoperation_get_victim(struct ofoperation *op)
+{
+ assert(op->type == OFOPERATION_ADD);
+ return op->victim;