commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r10984 - gnuradio/branches/developers/eb/vrt/vrt/lib


From: eb
Subject: [Commit-gnuradio] r10984 - gnuradio/branches/developers/eb/vrt/vrt/lib
Date: Thu, 7 May 2009 01:59:53 -0600 (MDT)

Author: eb
Date: 2009-05-07 01:59:53 -0600 (Thu, 07 May 2009)
New Revision: 10984

Added:
   gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.cc
   gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.h
Log:
copy of eth_buffer with juha's patch applied

Copied: gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.cc (from 
rev 10980, gnuradio/branches/developers/eb/vrt/usrp2/host/lib/eth_buffer.cc)
===================================================================
--- gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.cc             
                (rev 0)
+++ gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.cc     
2009-05-07 07:59:53 UTC (rev 10984)
@@ -0,0 +1,274 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "eth_buffer.h"
+#include "ethernet.h"
+#include <usrp2/data_handler.h>
+#include <linux/if_packet.h>
+#include <sys/socket.h>
+#include <sys/mman.h>
+#include <sys/poll.h>
+#include <iostream>
+#include <cmath>
+#include <errno.h>
+#include <stdexcept>
+#include <string.h>
+
+
+#define ETH_BUFFER_DEBUG      0 // define to 0 or 1
+#if ETH_BUFFER_DEBUG
+#define DEBUG_LOG(x) ::write(2, (x), 1)
+#else
+#define DEBUG_LOG(X)
+#endif
+
+#define DEFAULT_MEM_SIZE   25e6 // ~0.25s @ 100 MB/s
+#define MAX_MEM_SIZE     1000e6 // ~10.00s @ 100 MB/s. 
+#define MAX_SLAB_SIZE    131072 // 128 KB (FIXME fish out of /proc/slabinfo)
+#define MAX_PKT_SIZE       1512 // we don't do jumbo frames
+
+namespace usrp2 {
+
+  eth_buffer::eth_buffer(size_t rx_bufsize)
+    : d_fd(0), d_using_tpring(false), d_buflen(0), d_buf(0), d_frame_nr(0),
+      d_frame_size(0), d_head(0), d_ring(0), d_ethernet(new ethernet())
+  {
+    if (rx_bufsize == 0)
+      d_buflen = (size_t)DEFAULT_MEM_SIZE;
+    else
+      d_buflen = std::min((size_t)MAX_MEM_SIZE, rx_bufsize);
+       
+    memset(d_mac, 0, sizeof(d_mac));
+  }
+
+  eth_buffer::~eth_buffer()
+  {
+    close();
+  }
+  
+  bool 
+  eth_buffer::open(const std::string &ifname, int protocol)
+  {
+    if (!d_ethernet->open(ifname, protocol)) {
+      std::cerr << "eth_buffer: unable to open interface " 
+               << ifname << std::endl;
+      return false;
+    }
+
+    d_fd = d_ethernet->fd();
+    memcpy(d_mac, d_ethernet->mac(), sizeof(d_mac));
+    
+    struct tpacket_req req;
+    size_t page_size = getpagesize();
+
+    // Calculate minimum power-of-two aligned size for frames
+    req.tp_frame_size =
+      (unsigned int)rint(pow(2, 
ceil(log2(TPACKET_ALIGN(TPACKET_HDRLEN)+TPACKET_ALIGN(MAX_PKT_SIZE)))));
+    d_frame_size = req.tp_frame_size;
+
+    // Calculate minimum contiguous pages needed to enclose a frame
+    int npages = (page_size > req.tp_frame_size) ? 1 : 
((req.tp_frame_size+page_size-1)/page_size);
+    req.tp_block_size = page_size << (int)ceil(log2(npages));
+
+    // Calculate number of blocks
+    req.tp_block_nr = (int)(d_buflen/req.tp_block_size);
+                              
+
+    // Recalculate buffer length
+    d_buflen = req.tp_block_nr*req.tp_block_size;
+
+    // Finally, calculate total number of frames.  Since frames, blocks,
+    // and pages are all power-of-two aligned, frames are contiguous
+    req.tp_frame_nr = d_buflen/req.tp_frame_size;
+    d_frame_nr = req.tp_frame_nr;
+
+#if 0
+    if (ETH_BUFFER_DEBUG)
+      std::cerr << "eth_buffer:" 
+               << " frame_size=" << req.tp_frame_size
+               << " block_size=" << req.tp_block_size
+                << " block_nr=" << req.tp_block_nr
+               << " frame_nr=" << req.tp_frame_nr
+               << " buflen=" << d_buflen
+               << std::endl;
+#endif
+
+    // Try to get kernel shared memory buffer    
+    if (setsockopt(d_fd, SOL_PACKET, PACKET_RX_RING, (void *)&req, 
sizeof(req))) {
+      perror("eth_buffer: setsockopt");
+      d_using_tpring = false;
+      if (!(d_buf = (uint8_t *)malloc(d_buflen))) {
+        std::cerr << "eth_buffer: failed to allocate packet memory" << 
std::endl;
+       return false;
+      }
+      
+      std::cerr << "eth_buffer: using malloc'd memory for buffer" << std::endl;
+    }
+    else {
+      d_using_tpring = true;
+      void *p = mmap(0, d_buflen, PROT_READ|PROT_WRITE, MAP_SHARED, d_fd, 0);
+      if (p == MAP_FAILED){
+        perror("eth_buffer: mmap");
+       return false;
+      }
+      d_buf = (uint8_t *) p;
+
+      if (ETH_BUFFER_DEBUG)
+        std::cerr << "eth_buffer: using kernel shared mem for buffer" << 
std::endl;
+    }
+
+    // Initialize our pointers into the packet ring
+    d_ring = std::vector<uint8_t *>(req.tp_frame_nr);
+    for (unsigned int i=0; i < req.tp_frame_nr; i++) {
+      d_ring[i] = (uint8_t *)(d_buf+i*req.tp_frame_size);
+    }
+
+    // If not using kernel ring, instantiate select/read thread here
+
+    return true;
+  }
+
+  bool
+  eth_buffer::close()
+  {
+    // if we have background thread, stop it here
+
+    if (!d_using_tpring && d_buf)
+       free(d_buf);
+       
+    return d_ethernet->close();
+  }
+
+  bool 
+  eth_buffer::attach_pktfilter(pktfilter *pf)
+  {
+    return d_ethernet->attach_pktfilter(pf);
+  }
+
+  inline bool
+  eth_buffer::frame_available()
+  {
+    return (((tpacket_hdr *)d_ring[d_head])->tp_status != TP_STATUS_KERNEL);
+  }
+  
+  eth_buffer::result
+  eth_buffer::rx_frames(data_handler *f, int timeout_in_ms)
+  {
+    DEBUG_LOG("\n");
+      
+    while (!frame_available()) {
+      if (timeout_in_ms == 0) {
+        DEBUG_LOG("w");
+        return EB_WOULD_BLOCK;
+      }
+      
+      struct pollfd pfd;
+      pfd.fd = d_fd;
+      pfd.revents = 0;
+      pfd.events = POLLIN;
+
+      DEBUG_LOG("P");
+
+      int pres = poll(&pfd, 1, timeout_in_ms);
+      if (pres == -1) {
+        perror("poll");
+       return EB_ERROR;
+      }
+
+      if (pres == 0) {
+        DEBUG_LOG("t");
+       return EB_TIMED_OUT;
+      }
+    }
+
+    // Iterate through available packets
+    while (frame_available()) {
+      // Get start of ethernet frame and length
+      tpacket_hdr *hdr = (tpacket_hdr *)d_ring[d_head];
+      void *base = (uint8_t *)hdr+hdr->tp_mac;
+      size_t len = hdr->tp_len;
+      
+      // FYI, (base % 4 == 2) Not what we want given the current FPGA
+      // code.  This means that our uint32_t samples are not 4-byte
+      // aligned.  We'll have to deal with it downstream.
+
+      if (0)
+       fprintf(stderr, "eth_buffer: base = %p  tp_mac = %3d  tp_net = %3d\n",
+               base, hdr->tp_mac, hdr->tp_net);
+
+      // Invoke data handler
+      data_handler::result r = (*f)(base, len);
+      if (!(r & data_handler::KEEP))
+        hdr->tp_status = TP_STATUS_KERNEL; // mark it free
+
+      inc_head();
+
+      if (r & data_handler::DONE)
+        break;
+    }
+
+    DEBUG_LOG("|");
+    return EB_OK;
+  }
+
+  eth_buffer::result
+  eth_buffer::tx_frame(const void *base, size_t len, int flags)
+  {
+    DEBUG_LOG("T");
+
+    if (flags & EF_DONTWAIT)    // FIXME: implement flags
+      throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
+
+    int res = d_ethernet->write_packet(base, len);
+    if (res < 0 || (unsigned int)res != len)
+      return EB_ERROR;
+
+    return EB_OK;
+  }
+
+  eth_buffer::result
+  eth_buffer::tx_framev(const eth_iovec *iov, int iovcnt, int flags)
+  {
+    DEBUG_LOG("T");
+
+    if (flags & EF_DONTWAIT)    // FIXME: implement flags
+      throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
+
+    int res = d_ethernet->write_packetv(iov, iovcnt);
+    if (res < 0)
+      return EB_ERROR;
+
+    return EB_OK;
+  }
+
+  void
+  eth_buffer::release_frame(void *base)
+  {
+    // Get d_frame_size aligned header
+    tpacket_hdr *hdr = (tpacket_hdr *)((intptr_t)base & ~(d_frame_size-1));
+    hdr->tp_status = TP_STATUS_KERNEL; // mark it free
+  }
+  
+} // namespace usrp2

Copied: gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.h (from 
rev 10980, gnuradio/branches/developers/eb/vrt/usrp2/host/lib/eth_buffer.h)
===================================================================
--- gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.h              
                (rev 0)
+++ gnuradio/branches/developers/eb/vrt/vrt/lib/socket_rx_buffer.h      
2009-05-07 07:59:53 UTC (rev 10984)
@@ -0,0 +1,198 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef INCLUDED_USRP2_ETH_BUFFER_H
+#define INCLUDED_USRP2_ETH_BUFFER_H
+
+#include "pktfilter.h"
+#include <eth_common.h>
+#include <boost/utility.hpp>
+#include <vector>
+#include <memory>
+#include <stdint.h>
+
+namespace usrp2 {
+
+  class ethernet;
+  class data_handler;
+
+  /*!
+   * \brief high-performance interface to send and receive raw
+   * ethernet frames with out-of-order retirement of received frames.
+   *
+   * On many systems it should be possible to implement this on top of libpcap
+   *
+   * \internal
+   */
+  class eth_buffer : boost::noncopyable 
+  {
+    
+    int                  d_fd;                 // socket file descriptor
+    uint8_t      d_mac[6];             // our mac address
+    bool          d_using_tpring;       // using kernel mapped packet ring
+    size_t        d_buflen;             // length of our buffer
+    uint8_t      *d_buf;                // packet ring
+    unsigned int  d_frame_nr;           // max frames on ring
+    size_t        d_frame_size;         // frame storage size
+    unsigned int  d_head;               // pointer to next frame
+
+    std::vector<uint8_t *>  d_ring;     // pointers into buffer
+    std::auto_ptr<ethernet> d_ethernet; // our underlying interface
+  
+    bool frame_available();
+
+    void inc_head()
+    {
+      if (d_head + 1 >= d_frame_nr)
+       d_head = 0;
+      else
+       d_head = d_head + 1;
+    }
+
+
+  public:
+
+    enum result {
+      EB_OK,           //< everything's fine
+      EB_ERROR,                //< A non-recoverable error occurred
+      EB_WOULD_BLOCK,  //< A timeout of 0 was specified and nothing was ready
+      EB_TIMED_OUT,    //< The timeout expired before anything was ready
+    };
+
+    static const unsigned int MAX_PKTLEN = 1512;
+    static const unsigned int MIN_PKTLEN = 64;
+
+    /*!
+     * \param rx_bufsize is a hint as to the number of bytes of memory
+     * to allocate for received ethernet frames (0 -> reasonable default)
+     */
+    eth_buffer(size_t rx_bufsize = 0);
+    ~eth_buffer();
+    
+    /*!
+     * \brief open the specified interface
+     *
+     * \param ifname ethernet interface name, e.g., "eth0"
+     * \param protocol is the ethertype protocol number in network order.
+     *    Use 0 to receive all protocols.
+     */
+    bool open(const std::string &ifname, int protocol);
+
+    /*!
+     * \brief close the interface
+     */
+    bool close();
+
+    /*!
+     * \brief attach packet filter to socket to restrict which packets read 
sees.
+     * \param pf       the packet filter
+     */
+    bool attach_pktfilter(pktfilter *pf);
+
+    /*!
+     * \brief return 6 byte string containing our MAC address
+     */
+    const uint8_t *mac() const { return d_mac; }
+
+    /*!
+     * \brief Call \p f for each frame in the receive buffer.
+     * \param f is the frame data handler
+     * \param timeout (in ms) controls behavior when there are no frames to 
read
+     *
+     * If \p timeout is 0, rx_frames will not wait for frames if none are 
+     * available, and f will not be invoked.  If \p timeout is -1 (the 
+     * default), rx_frames will block indefinitely until frames are 
+     * available.  If \p timeout is positive, it indicates the number of
+     * milliseconds to wait for a frame to become available.  Once the
+     * timeout has expired, rx_frames will return, f never having been 
+     * invoked.
+     *
+     * \p f will be called on each ethernet frame that is available.
+     * \p f returns a bit mask with one of the following set or cleared:
+     * 
+     * data_handler::KEEP  - hold onto the frame and present it again during 
+     *                       the next call to rx_frames, otherwise discard it
+     *
+     * data_handler::DONE -  return from rx_frames now even though more frames
+     *                       might be available; otherwise continue if more 
+     *                       frames are ready.
+     *
+     * The idea of holding onto a frame for the next iteration allows
+     * the caller to scan the received packet stream for particular
+     * classes of frames (such as command replies) leaving the rest
+     * intact.  On the next call all kept frames, followed by any new
+     * frames received, will be presented in order to \p f.  
+     * See usrp2.cc for an example of the pattern.
+     *
+     * \returns EB_OK if at least one frame was received
+     * \returns EB_WOULD_BLOCK if \p timeout is 0 and the call would have 
blocked
+     * \returns EB_TIMED_OUT if timeout occurred
+     * \returns EB_ERROR if there was an unrecoverable error.
+     */
+    result rx_frames(data_handler *f, int timeout=-1);
+
+    /*
+     * \brief Release frame from buffer
+     *
+     * Call to release a frame previously held by a data_handler::KEEP.
+     * The pointer may be offset from the base of the frame up to its length.
+     */
+    void release_frame(void *p);
+
+    /*
+     * \brief Write an ethernet frame to the interface.
+     *
+     * \param base points to the beginning of the frame (the 14-byte ethernet 
header).
+     * \param len is the length of the frame in bytes.
+     * \param flags is 0 or the bitwise-or of values from eth_flags
+     *
+     * The frame must begin with a 14-byte ethernet header.
+     *
+     * \returns EB_OK if the frame was successfully enqueued.
+     * \returns EB_WOULD_BLOCK if flags contains EF_DONT_WAIT and the call 
would have blocked.
+     * \returns EB_ERROR if there was an unrecoverable error.
+     */
+    result tx_frame(const void *base, size_t len, int flags=0);
+
+    /*
+     * \brief Write an ethernet frame to the interface using scatter/gather.
+     *
+     * \param iov points to an array of iovec structs
+     * \param iovcnt is the number of entries
+     * \param flags is 0 or the bitwise-or of values from eth_flags
+     *
+     * The frame must begin with a 14-byte ethernet header.
+     *
+     * \returns EB_OK if the frame was successfully enqueued.
+     * \returns EB_WOULD_BLOCK if flags contains EF_DONT_WAIT and the call 
would have blocked.
+     * \returns EB_ERROR if there was an unrecoverable error.
+     */
+    result tx_framev(const eth_iovec *iov, int iovcnt, int flags=0);
+
+    /*
+     * \brief Returns maximum possible number of frames in buffer
+     */
+    unsigned int max_frames() const { return d_frame_nr; }
+
+  };
+
+};  // namespace usrp2
+
+#endif /* INCLUDED_USRP2_ETH_BUFFER_H */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]