ofproto: Inline trivial functions.
[sliver-openvswitch.git] / lib / ofpbuf.c
index 56521b7..1f4b61d 100644 (file)
 #include "netdev-dpdk.h"
 #include "util.h"
 
+static void
+ofpbuf_init__(struct ofpbuf *b, size_t allocated, enum ofpbuf_source source)
+{
+    b->allocated = allocated;
+    b->source = source;
+    b->frame = NULL;
+    b->l2_5_ofs = b->l3_ofs = b->l4_ofs = UINT16_MAX;
+    list_poison(&b->list_node);
+}
+
 static void
 ofpbuf_use__(struct ofpbuf *b, void *base, size_t allocated,
              enum ofpbuf_source source)
@@ -30,11 +40,7 @@ ofpbuf_use__(struct ofpbuf *b, void *base, size_t allocated,
     ofpbuf_set_data(b, base);
     ofpbuf_set_size(b, 0);
 
-    b->allocated = allocated;
-    b->source = source;
-    b->l2 = NULL;
-    b->l2_5_ofs = b->l3_ofs = b->l4_ofs = UINT16_MAX;
-    list_poison(&b->list_node);
+    ofpbuf_init__(b, allocated, source);
 }
 
 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
@@ -101,6 +107,18 @@ ofpbuf_use_const(struct ofpbuf *b, const void *data, size_t size)
     ofpbuf_set_size(b, size);
 }
 
+/* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
+ * memory starting at 'base'.  DPDK allocated ofpbuf and *data is allocated
+ * from one continous memory region, so in memory data start right after
+ * ofpbuf.  Therefore there is special method to free this type of
+ * buffer.  ofpbuf base, data and size are initialized by dpdk rcv() so no
+ * need to initialize those fields. */
+void
+ofpbuf_init_dpdk(struct ofpbuf *b, size_t allocated)
+{
+    ofpbuf_init__(b, allocated, OFPBUF_DPDK);
+}
+
 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
  * bytes. */
 void
@@ -117,9 +135,7 @@ ofpbuf_uninit(struct ofpbuf *b)
         if (b->source == OFPBUF_MALLOC) {
             free(ofpbuf_base(b));
         }
-        if (b->source == OFPBUF_DPDK) {
-            free_dpdk_buf(b);
-        }
+        ovs_assert(b->source != OFPBUF_DPDK);
     }
 }
 
@@ -170,10 +186,11 @@ ofpbuf_clone_with_headroom(const struct ofpbuf *buffer, size_t headroom)
     new_buffer = ofpbuf_clone_data_with_headroom(ofpbuf_data(buffer),
                                                  ofpbuf_size(buffer),
                                                  headroom);
-    if (buffer->l2) {
-        uintptr_t data_delta = (char *)ofpbuf_data(new_buffer) - (char *)ofpbuf_data(buffer);
+    if (buffer->frame) {
+        uintptr_t data_delta
+            = (char *)ofpbuf_data(new_buffer) - (char *)ofpbuf_data(buffer);
 
-        new_buffer->l2 = (char *) buffer->l2 + data_delta;
+        new_buffer->frame = (char *) buffer->frame + data_delta;
     }
     new_buffer->l2_5_ofs = buffer->l2_5_ofs;
     new_buffer->l3_ofs = buffer->l3_ofs;
@@ -258,12 +275,12 @@ ofpbuf_resize__(struct ofpbuf *b, size_t new_headroom, size_t new_tailroom)
 
     new_data = (char *) new_base + new_headroom;
     if (ofpbuf_data(b) != new_data) {
-        uintptr_t data_delta = (char *) new_data - (char *) ofpbuf_data(b);
+        if (b->frame) {
+            uintptr_t data_delta = (char *) new_data - (char *) ofpbuf_data(b);
 
-        ofpbuf_set_data(b, new_data);
-        if (b->l2) {
-            b->l2 = (char *) b->l2 + data_delta;
+            b->frame = (char *) b->frame + data_delta;
         }
+        ofpbuf_set_data(b, new_data);
     }
 }
 
@@ -520,12 +537,12 @@ ofpbuf_resize_l2_5(struct ofpbuf *b, int increment)
         ofpbuf_pull(b, -increment);
     }
 
-    b->l2 = ofpbuf_data(b);
+    b->frame = ofpbuf_data(b);
     /* Adjust layer offsets after l2_5. */
     ofpbuf_adjust_layer_offset(&b->l3_ofs, increment);
     ofpbuf_adjust_layer_offset(&b->l4_ofs, increment);
 
-    return b->l2;
+    return b->frame;
 }
 
 /* Adjust the size of the l2 portion of the ofpbuf, updating the l2
@@ -536,5 +553,5 @@ ofpbuf_resize_l2(struct ofpbuf *b, int increment)
 {
     ofpbuf_resize_l2_5(b, increment);
     ofpbuf_adjust_layer_offset(&b->l2_5_ofs, increment);
-    return b->l2;
+    return b->frame;
 }