+static enum ofperr
+vconn_bundle_reply_validate(struct ofpbuf *reply,
+ struct ofputil_bundle_ctrl_msg *request,
+ void (*error_reporter)(const struct ofp_header *))
+{
+ const struct ofp_header *oh;
+ enum ofptype type;
+ enum ofperr error;
+ struct ofputil_bundle_ctrl_msg rbc;
+
+ oh = reply->data;
+ error = ofptype_decode(&type, oh);
+ if (error) {
+ return error;
+ }
+
+ if (type == OFPTYPE_ERROR) {
+ error_reporter(oh);
+ return ofperr_decode_msg(oh, NULL);
+ }
+ if (type != OFPTYPE_BUNDLE_CONTROL) {
+ return OFPERR_OFPBRC_BAD_TYPE;
+ }
+
+ error = ofputil_decode_bundle_ctrl(oh, &rbc);
+ if (error) {
+ return error;
+ }
+
+ if (rbc.bundle_id != request->bundle_id) {
+ return OFPERR_OFPBFC_BAD_ID;
+ }
+
+ if (rbc.type != request->type + 1) {
+ return OFPERR_OFPBFC_BAD_TYPE;
+ }
+
+ return 0;
+}
+
+/* Send bundle control message 'bc' of 'type' via 'vconn', and wait for either
+ * an error or the corresponding bundle control message response.
+ *
+ * 'error_reporter' is called for any error responses received, which may be
+ * also regarding earlier OpenFlow messages than this bundle control message.
+ *
+ * Returns errno value, or 0 when successful. */
+static int
+vconn_bundle_control_transact(struct vconn *vconn,
+ struct ofputil_bundle_ctrl_msg *bc,
+ uint16_t type,
+ void (*error_reporter)(const struct ofp_header *))
+{
+ struct ofpbuf *request, *reply;
+ int error;
+ enum ofperr ofperr;
+
+ bc->type = type;
+ request = ofputil_encode_bundle_ctrl_request(vconn->version, bc);
+ ofpmsg_update_length(request);
+ error = vconn_transact__(vconn, request, &reply, error_reporter);
+ if (error) {
+ return error;
+ }
+
+ ofperr = vconn_bundle_reply_validate(reply, bc, error_reporter);
+ if (ofperr) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "Bundle %s failed (%s).",
+ type == OFPBCT_OPEN_REQUEST ? "open"
+ : type == OFPBCT_CLOSE_REQUEST ? "close"
+ : type == OFPBCT_COMMIT_REQUEST ? "commit"
+ : type == OFPBCT_DISCARD_REQUEST ? "discard"
+ : "control message",
+ ofperr_to_string(ofperr));
+ }
+ ofpbuf_delete(reply);
+
+ return ofperr ? EPROTO : 0;
+}
+
+/* Checks if error responses can be received on 'vconn'. */
+static void
+vconn_recv_error(struct vconn *vconn,
+ void (*error_reporter)(const struct ofp_header *))
+{
+ int error;
+
+ do {
+ struct ofpbuf *reply;
+
+ error = vconn_recv(vconn, &reply);
+ if (!error) {
+ const struct ofp_header *oh;
+ enum ofptype type;
+ enum ofperr ofperr;
+
+ oh = reply->data;
+ ofperr = ofptype_decode(&type, oh);
+ if (!ofperr && type == OFPTYPE_ERROR) {
+ error_reporter(oh);
+ } else {
+ VLOG_DBG_RL(&bad_ofmsg_rl,
+ "%s: received unexpected reply with xid %08"PRIx32,
+ vconn->name, ntohl(oh->xid));
+ }
+ ofpbuf_delete(reply);
+ }
+ } while (!error);
+}
+
+static int
+vconn_bundle_add_msg(struct vconn *vconn, struct ofputil_bundle_ctrl_msg *bc,
+ struct ofpbuf *msg,
+ void (*error_reporter)(const struct ofp_header *))
+{
+ struct ofputil_bundle_add_msg bam;
+ struct ofpbuf *request;
+ int error;
+
+ bam.bundle_id = bc->bundle_id;
+ bam.flags = bc->flags;
+ bam.msg = msg->data;
+
+ request = ofputil_encode_bundle_add(vconn->version, &bam);
+ ofpmsg_update_length(request);
+
+ error = vconn_send_block(vconn, request);
+ if (!error) {
+ /* Check for an error return, so that the socket buffer does not become
+ * full of errors. */
+ vconn_recv_error(vconn, error_reporter);
+ }
+ return error;
+}
+
+int
+vconn_bundle_transact(struct vconn *vconn, struct ovs_list *requests,
+ uint16_t flags,
+ void (*error_reporter)(const struct ofp_header *))
+{
+ struct ofputil_bundle_ctrl_msg bc;
+ struct ofpbuf *request;
+ int error;
+
+ memset(&bc, 0, sizeof bc);
+ bc.flags = flags;
+ error = vconn_bundle_control_transact(vconn, &bc, OFPBCT_OPEN_REQUEST,
+ error_reporter);
+ if (error) {
+ return error;
+ }
+
+ LIST_FOR_EACH (request, list_node, requests) {
+ error = vconn_bundle_add_msg(vconn, &bc, request, error_reporter);
+ if (error) {
+ break;
+ }
+ }
+
+ if (!error) {
+ error = vconn_bundle_control_transact(vconn, &bc,
+ OFPBCT_COMMIT_REQUEST,
+ error_reporter);
+ } else {
+ /* Do not overwrite the error code from vconn_bundle_add_msg().
+ * Any error in discard should be either reported or logged, so it
+ * should not get lost. */
+ vconn_bundle_control_transact(vconn, &bc, OFPBCT_DISCARD_REQUEST,
+ error_reporter);
+ }
+ return error;
+}
+