diff mbox series

[ovs-dev,RFC,v6,07/11] dp-packet: Handle multi-seg mubfs in shift() func.

Message ID 1526491963-45108-8-git-send-email-tiago.lam@intel.com
State Superseded
Delegated to: Ian Stokes
Headers show
Series Support multi-segment mbufs | expand

Commit Message

Lam, Tiago May 16, 2018, 5:32 p.m. UTC
In its current implementation dp_packet_shift() is also unaware of
multi-seg mbufs (that holds data in memory non-contiguously) and assumes
that data exists contiguously in memory, memmove'ing data to perform the
shift.

To add support for multi-seg mbuds a new set of functions was
introduced, dp_packet_mbuf_shift() and dp_packet_mbuf_write(). These
functions are used by dp_packet_shift(), when handling multi-seg mbufs,
to shift and write data within a chain of mbufs.

Signed-off-by: Tiago Lam <tiago.lam@intel.com>
---
 lib/dp-packet.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/dp-packet.c b/lib/dp-packet.c
index eefde37..1a666f2 100644
--- a/lib/dp-packet.c
+++ b/lib/dp-packet.c
@@ -294,13 +294,106 @@  dp_packet_prealloc_headroom(struct dp_packet *b, size_t size)
     }
 }
 
+/* XXX: Caller must provide the correct mbuf where the offset is located. It
+   must also guarantee that 'ofs' is within bounds */
+static void
+dp_packet_mbuf_write(const struct rte_mbuf *mbuf, uint16_t ofs, uint32_t len,
+                     const void *data)
+{
+    while (mbuf) {
+        if (len == 0) {
+            break;
+        }
+
+        char *dst_addr = ((char *) mbuf->buf_addr) + mbuf->data_off + ofs;
+        uint16_t data_len = rte_pktmbuf_mtod(mbuf, char *) + mbuf->buf_len -
+            dst_addr;
+
+        int len_copy = MIN(len, data_len);
+        memcpy(dst_addr, data, len_copy);
+
+        data = ((char *) data) + len_copy;
+        len -= len_copy;
+        ofs = 0;
+
+        mbuf = mbuf->next;
+    }
+}
+
+static void
+dp_packet_mbuf_shift_(struct rte_mbuf *dbuf, uint16_t dst_ofs,
+                      struct rte_mbuf *sbuf, uint16_t src_ofs, int len)
+{
+    char rd[len];
+
+    const char *wd = rte_pktmbuf_read(sbuf, src_ofs, len, rd);
+    if (!wd) {
+        /* XXX: Log WARN here. */
+        return;
+    }
+
+    dp_packet_mbuf_write(dbuf, dst_ofs, len, wd);
+}
+
+/* XXX: Delta must fall within the bounds of an mbuf */
+static void
+dp_packet_mbuf_shift(struct dp_packet *b, int delta)
+{
+    struct rte_mbuf *sbuf;
+    struct rte_mbuf *dbuf;
+    uint16_t src_ofs;
+    uint16_t dst_ofs;
+
+    struct rte_mbuf *mbuf = CONST_CAST(struct rte_mbuf *, &b->mbuf);
+    dbuf = mbuf;
+    sbuf = mbuf;
+
+    /* Set the destination address to copy to */
+    if (delta < 0) {
+        dst_ofs = delta;
+        src_ofs = 0;
+    } else {
+        /* Set the destination address to copy to */
+        dst_ofs = delta;
+        src_ofs = 0;
+    }
+
+    struct rte_mbuf *tmbuf = rte_pktmbuf_lastseg(mbuf);
+    if (dbuf != tmbuf) {
+        tmbuf->data_len += delta;
+    }
+
+    /* Shift data from src mbuf and offset to dst mbuf and offset */
+    dp_packet_mbuf_shift_(dbuf, dst_ofs, sbuf, src_ofs,
+        rte_pktmbuf_pkt_len(sbuf));
+
+    dp_packet_set_data(b, rte_pktmbuf_mtod_offset(dbuf, char *, dst_ofs));
+}
+
 /* 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'). */
+ * byte to move one byte backward (from 'p' to 'p-1').
+ * Note for DPBUF_DPDK(XXX): The shift can only move within the bounds of an
+ * mbuf since it assumes the source and destination offsets will be on the
+ * same mbuf. */
 void
 dp_packet_shift(struct dp_packet *b, int delta)
 {
+#ifdef DPDK_NETDEV
+     if (b->source == DPBUF_DPDK) {
+        ovs_assert(delta > 0 ? delta <= dp_packet_tailroom(b)
+                   : delta < 0 ? -delta <= dp_packet_headroom(b)
+                   : true);
+
+
+        if (delta != 0) {
+            dp_packet_mbuf_shift(b, delta);
+        }
+
+        return;
+    }
+#endif
     ovs_assert(delta > 0 ? delta <= dp_packet_tailroom(b)
                : delta < 0 ? -delta <= dp_packet_headroom(b)
                : true);