#ifndef __included_dpdk_replication_h__
#define __included_dpdk_replication_h__
#include <vnet/devices/dpdk/dpdk.h>

/*
 * vlib_dpdk_clone_buffer - clone a buffer 
 * for port mirroring, lawful intercept, etc.
 * rte_pktmbuf_clone (...) requires that the forwarding path
 * not touch any of the cloned data. The hope is that we'll
 * figure out how to relax that restriction. 
 * 
 * For the moment, copy packet data.
 */

static inline vlib_buffer_t *
vlib_dpdk_clone_buffer (vlib_main_t * vm, vlib_buffer_t * b)
{
  u32 new_buffers_needed = 1;
  unsigned socket_id = rte_socket_id();
  struct rte_mempool *rmp = vm->buffer_main->pktmbuf_pools[socket_id];
  struct rte_mbuf *rte_mbufs[5];
  vlib_buffer_free_list_t * fl;
  vlib_buffer_t * rv;
  u8 * copy_src, * copy_dst;
  vlib_buffer_t *src_buf, *dst_buf;

  fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);

  if (PREDICT_FALSE(b->flags & VLIB_BUFFER_NEXT_PRESENT))
    {
      vlib_buffer_t *tmp = b;
      int i;

      while (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
        {
          new_buffers_needed ++;
          tmp = vlib_get_buffer (vm, tmp->next_buffer);
        }

      /* Should never happen... */
      if (PREDICT_FALSE(new_buffers_needed > ARRAY_LEN(rte_mbufs)))
        {
          clib_warning ("need %d buffers", new_buffers_needed);
          return 0;
        }
      
      if (rte_mempool_get_bulk (rmp, (void **)rte_mbufs, 
                                new_buffers_needed) < 0)
        return 0;

      src_buf = b;
      rv = dst_buf = (vlib_buffer_t *)(rte_mbufs[0] + 1);
      vlib_buffer_init_for_free_list (dst_buf, fl);
      copy_src = b->data + src_buf->current_data;
      copy_dst = dst_buf->data + src_buf->current_data;

      for (i = 0; i < new_buffers_needed; i++)
        {
          memcpy (copy_src, copy_dst, src_buf->current_length);
          dst_buf->current_data = src_buf->current_data;
          dst_buf->current_length = src_buf->current_length;
          dst_buf->flags = src_buf->flags;

          if (i == 0)
            {
              dst_buf->total_length_not_including_first_buffer = 
                src_buf->total_length_not_including_first_buffer;
              vnet_buffer(dst_buf)->sw_if_index[VLIB_RX] =
                vnet_buffer(src_buf)->sw_if_index[VLIB_RX];
              vnet_buffer(dst_buf)->sw_if_index[VLIB_TX] =
                vnet_buffer(src_buf)->sw_if_index[VLIB_TX];
              vnet_buffer(dst_buf)->l2 = vnet_buffer(b)->l2;
            }

          if (i < new_buffers_needed - 1)
            {
              src_buf = vlib_get_buffer (vm, src_buf->next_buffer);
              dst_buf = (vlib_buffer_t *)(rte_mbufs[i+1] + 1);
              vlib_buffer_init_for_free_list (dst_buf, fl);
              copy_src = src_buf->data;
              copy_dst = dst_buf->data;
            }
        }
      return rv;
    }

  if (rte_mempool_get_bulk (rmp, (void **)rte_mbufs, 1) < 0)
    return 0;

  rv = (vlib_buffer_t *)(rte_mbufs[0] + 1);
  vlib_buffer_init_for_free_list (rv, fl);

  memcpy(rv->data + b->current_data, b->data + b->current_data, 
         b->current_length);
  rv->current_data = b->current_data;
  rv->current_length = b->current_length;
  vnet_buffer(rv)->sw_if_index[VLIB_RX] =
    vnet_buffer(b)->sw_if_index[VLIB_RX];
  vnet_buffer(rv)->sw_if_index[VLIB_TX] =
    vnet_buffer(b)->sw_if_index[VLIB_TX];
  vnet_buffer(rv)->l2 = vnet_buffer(b)->l2;

  return (rv);
}


#endif /* __included_dpdk_replication_h__ */
