ofp-util: Prepare Packet Out encoder for other Open Flow versions
[sliver-openvswitch.git] / lib / ofp-util.c
index 36f1c8a..910e63a 100644 (file)
@@ -1911,6 +1911,26 @@ ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
     return msg;
 }
 
+static void
+ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
+                                struct cls_rule *rule,
+                                struct ofpbuf *b)
+{
+    pin->packet = b->data;
+    pin->packet_len = b->size;
+
+    pin->fmd.in_port = rule->flow.in_port;
+
+    pin->fmd.tun_id = rule->flow.tun_id;
+    pin->fmd.tun_id_mask = rule->wc.tun_id_mask;
+
+    pin->fmd.metadata = rule->flow.metadata;
+    pin->fmd.metadata_mask = rule->wc.metadata_mask;
+
+    memcpy(pin->fmd.regs, rule->flow.regs, sizeof pin->fmd.regs);
+    memcpy(pin->fmd.reg_masks, rule->wc.reg_masks, sizeof pin->fmd.reg_masks);
+}
+
 enum ofperr
 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
                          const struct ofp_header *oh)
@@ -1922,7 +1942,29 @@ ofputil_decode_packet_in(struct ofputil_packet_in *pin,
 
     ofpbuf_use_const(&b, oh, ntohs(oh->length));
     raw = ofpraw_pull_assert(&b);
-    if (raw == OFPRAW_OFPT10_PACKET_IN) {
+    if (raw == OFPRAW_OFPT12_PACKET_IN) {
+        const struct ofp12_packet_in *opi;
+        struct cls_rule rule;
+        int error;
+
+        opi = ofpbuf_pull(&b, sizeof *opi);
+        error = oxm_pull_match_loose(&b, 0, &rule);
+        if (error) {
+            return error;
+        }
+
+        if (!ofpbuf_try_pull(&b, 2)) {
+            return OFPERR_OFPBRC_BAD_LEN;
+        }
+
+        pin->reason = opi->reason;
+        pin->table_id = opi->table_id;
+
+        pin->buffer_id = ntohl(opi->buffer_id);
+        pin->total_len = ntohs(opi->total_len);
+
+        ofputil_decode_packet_in_finish(pin, &rule, &b);
+    } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
         const struct ofp_packet_in *opi;
 
         opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
@@ -1950,26 +1992,14 @@ ofputil_decode_packet_in(struct ofputil_packet_in *pin,
             return OFPERR_OFPBRC_BAD_LEN;
         }
 
-        pin->packet = b.data;
-        pin->packet_len = b.size;
         pin->reason = npi->reason;
         pin->table_id = npi->table_id;
         pin->cookie = npi->cookie;
 
-        pin->fmd.in_port = rule.flow.in_port;
-
-        pin->fmd.tun_id = rule.flow.tun_id;
-        pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
-
-        pin->fmd.metadata = rule.flow.metadata;
-        pin->fmd.metadata_mask = rule.wc.metadata_mask;
-
-        memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
-        memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
-               sizeof pin->fmd.reg_masks);
-
         pin->buffer_id = ntohl(npi->buffer_id);
         pin->total_len = ntohs(npi->total_len);
+
+        ofputil_decode_packet_in_finish(pin, &rule, &b);
     } else {
         NOT_REACHED();
     }
@@ -3013,10 +3043,10 @@ ofputil_append_flow_update(const struct ofputil_flow_update *update,
 }
 \f
 struct ofpbuf *
-ofputil_encode_packet_out(const struct ofputil_packet_out *po)
+ofputil_encode_packet_out(const struct ofputil_packet_out *po,
+                          enum ofputil_protocol protocol)
 {
-    struct ofp_packet_out *opo;
-    size_t actions_ofs;
+    enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
     struct ofpbuf *msg;
     size_t size;
 
@@ -3025,15 +3055,28 @@ ofputil_encode_packet_out(const struct ofputil_packet_out *po)
         size += po->packet_len;
     }
 
-    msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
-    ofpbuf_put_zeros(msg, sizeof *opo);
-    actions_ofs = msg->size;
-    ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
+    switch (ofp_version) {
+    case OFP10_VERSION: {
+        struct ofp_packet_out *opo;
+        size_t actions_ofs;
+
+        msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
+        ofpbuf_put_zeros(msg, sizeof *opo);
+        actions_ofs = msg->size;
+        ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
+
+        opo = msg->l3;
+        opo->buffer_id = htonl(po->buffer_id);
+        opo->in_port = htons(po->in_port);
+        opo->actions_len = htons(msg->size - actions_ofs);
+        break;
+    }
 
-    opo = msg->l3;
-    opo->buffer_id = htonl(po->buffer_id);
-    opo->in_port = htons(po->in_port);
-    opo->actions_len = htons(msg->size - actions_ofs);
+    case OFP11_VERSION:
+    case OFP12_VERSION:
+    default:
+        NOT_REACHED();
+    }
 
     if (po->buffer_id == UINT32_MAX) {
         ofpbuf_put(msg, po->packet, po->packet_len);