dpif-netdev: Add DPDK netdev.
[sliver-openvswitch.git] / lib / ofpbuf.c
index 82940eb..3de8ca5 100644 (file)
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "dynamic-string.h"
+#include "netdev-dpdk.h"
 #include "util.h"
 
 static void
@@ -110,8 +111,13 @@ ofpbuf_init(struct ofpbuf *b, size_t size)
 void
 ofpbuf_uninit(struct ofpbuf *b)
 {
-    if (b && b->source == OFPBUF_MALLOC) {
-        free(b->base);
+    if (b) {
+        if (b->source == OFPBUF_MALLOC) {
+            free(b->base);
+        }
+        if (b->source == OFPBUF_DPDK) {
+            free_dpdk_buf(b);
+        }
     }
 }
 
@@ -265,6 +271,9 @@ ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom)
     new_allocated = new_headroom + b->size + new_tailroom;
 
     switch (b->source) {
+    case OFPBUF_DPDK:
+        OVS_NOT_REACHED();
+
     case OFPBUF_MALLOC:
         if (new_headroom == ofpbuf_headroom(b)) {
             new_base = xrealloc(b->base, new_allocated);
@@ -343,6 +352,8 @@ ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size)
 void
 ofpbuf_trim(struct ofpbuf *b)
 {
+    ovs_assert(b->source != OFPBUF_DPDK);
+
     if (b->source == OFPBUF_MALLOC
         && (ofpbuf_headroom(b) || ofpbuf_tailroom(b))) {
         ofpbuf_resize__(b, 0, 0);
@@ -359,6 +370,24 @@ ofpbuf_padto(struct ofpbuf *b, size_t length)
     }
 }
 
+/* Shifts all of the data within the allocated space in 'b' by 'delta' bytes.
+ * For example, a 'delta' of 1 would cause each byte of data to move one byte
+ * forward (from address 'p' to 'p+1'), and a 'delta' of -1 would cause each
+ * byte to move one byte backward (from 'p' to 'p-1'). */
+void
+ofpbuf_shift(struct ofpbuf *b, int delta)
+{
+    ovs_assert(delta > 0 ? delta <= ofpbuf_tailroom(b)
+               : delta < 0 ? -delta <= ofpbuf_headroom(b)
+               : true);
+
+    if (delta != 0) {
+        char *dst = (char *) b->data + delta;
+        memmove(dst, b->data, b->size);
+        b->data = dst;
+    }
+}
+
 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
  * copying its data if necessary.  Returns a pointer to the first byte of the
  * new data, which is left uninitialized. */
@@ -544,6 +573,8 @@ void *
 ofpbuf_steal_data(struct ofpbuf *b)
 {
     void *p;
+    ovs_assert(b->source != OFPBUF_DPDK);
+
     if (b->source == OFPBUF_MALLOC && b->data == b->base) {
         p = b->data;
     } else {