meta-flow: Correctly set destination MAC in mf_set_flow_value().
[sliver-openvswitch.git] / ofproto / pktbuf.c
index b04eb59..7cc96a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "timeval.h"
 #include "util.h"
 #include "vconn.h"
-
-#define THIS_MODULE VLM_pktbuf
 #include "vlog.h"
 
+VLOG_DEFINE_THIS_MODULE(pktbuf);
+
+COVERAGE_DEFINE(pktbuf_buffer_unknown);
+COVERAGE_DEFINE(pktbuf_null_cookie);
+COVERAGE_DEFINE(pktbuf_retrieved);
+COVERAGE_DEFINE(pktbuf_reuse_error);
+
 /* Buffers are identified by a 32-bit opaque ID.  We divide the ID
  * into a buffer number (low bits) and a cookie (high bits).  The buffer number
  * is an index into an array of buffers.  The cookie distinguishes between
@@ -87,8 +92,9 @@ make_id(unsigned int buffer_idx, unsigned int cookie)
 }
 
 /* Attempts to allocate an OpenFlow packet buffer id within 'pb'.  The packet
- * buffer will store a copy of 'buffer' and the port number 'in_port', which
- * should be the datapath port number on which 'buffer' was received.
+ * buffer will store a copy of 'buffer_size' bytes in 'buffer' and the port
+ * number 'in_port', which should be the OpenFlow port number on which 'buffer'
+ * was received.
  *
  * If successful, returns the packet buffer id (a number other than
  * UINT32_MAX).  pktbuf_retrieve() can later be used to retrieve the buffer and
@@ -97,7 +103,8 @@ make_id(unsigned int buffer_idx, unsigned int cookie)
  *
  * The caller retains ownership of 'buffer'. */
 uint32_t
-pktbuf_save(struct pktbuf *pb, struct ofpbuf *buffer, uint16_t in_port)
+pktbuf_save(struct pktbuf *pb, const void *buffer, size_t buffer_size,
+            uint16_t in_port)
 {
     struct packet *p = &pb->packets[pb->buffer_idx];
     pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
@@ -112,7 +119,10 @@ pktbuf_save(struct pktbuf *pb, struct ofpbuf *buffer, uint16_t in_port)
     if (++p->cookie >= COOKIE_MAX) {
         p->cookie = 0;
     }
-    p->buffer = ofpbuf_clone(buffer);
+    p->buffer = ofpbuf_clone_data_with_headroom(buffer, buffer_size,
+                                                sizeof(struct ofp_packet_in));
+
+
     p->timeout = time_msec() + OVERWRITE_MSECS;
     p->in_port = in_port;
     return make_id(p - pb->packets, p->cookie);
@@ -154,6 +164,11 @@ pktbuf_get_null(void)
  * identifies a "null" packet buffer (created with pktbuf_get_null()), stores
  * NULL in '*bufferp' and UINT16_max in '*in_port'.
  *
+ * 'in_port' may be NULL if the input port is not of interest.
+ *
+ * A returned packet will have at least sizeof(struct ofp_packet_in) bytes of
+ * headroom.
+ *
  * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
 int
 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
@@ -163,6 +178,11 @@ pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
     struct packet *p;
     int error;
 
+    if (id == UINT32_MAX) {
+        error = 0;
+        goto error;
+    }
+
     if (!pb) {
         VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
                      "without buffers");
@@ -174,7 +194,9 @@ pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
         struct ofpbuf *buffer = p->buffer;
         if (buffer) {
             *bufferp = buffer;
-            *in_port = p->in_port;
+            if (in_port) {
+                *in_port = p->in_port;
+            }
             p->buffer = NULL;
             COVERAGE_INC(pktbuf_retrieved);
             return 0;
@@ -194,8 +216,11 @@ pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
                      "if the switch was recently in fail-open mode)", id);
         error = 0;
     }
+error:
     *bufferp = NULL;
-    *in_port = UINT16_MAX;
+    if (in_port) {
+        *in_port = UINT16_MAX;
+    }
     return error;
 }