Statistics
| Branch: | Tag: | Revision:

root / usrp2 / host / lib / eth_buffer.cc @ ed236703

History | View | Annotate | Download (7.3 kB)

1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2008,2009 Free Software Foundation, Inc.
4
 * 
5
 * This file is part of GNU Radio
6
 * 
7
 * GNU Radio is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3, or (at your option)
10
 * any later version.
11
 * 
12
 * GNU Radio is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 * 
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
#ifdef HAVE_CONFIG_H
23
#include <config.h>
24
#endif
25
26
#include "eth_buffer.h"
27
#include "ethernet.h"
28
#include <usrp2/data_handler.h>
29
#include <linux/if_packet.h>
30
#include <sys/socket.h>
31
#include <sys/mman.h>
32
#include <sys/poll.h>
33
#include <iostream>
34
#include <cmath>
35
#include <errno.h>
36
#include <stdexcept>
37
#include <string.h>
38
39
40
#define ETH_BUFFER_DEBUG      0 // define to 0 or 1
41
#if ETH_BUFFER_DEBUG
42
#define DEBUG_LOG(x) ::write(2, (x), 1)
43
#else
44
#define DEBUG_LOG(X)
45
#endif
46
47
#define DEFAULT_MEM_SIZE   25e6 // ~0.25s @ 100 MB/s
48
#define MAX_MEM_SIZE     1000e6 // ~10.00s @ 100 MB/s. 
49
#define MAX_SLAB_SIZE    131072 // 128 KB (FIXME fish out of /proc/slabinfo)
50
#define MAX_PKT_SIZE       1512 // we don't do jumbo frames
51
52
namespace usrp2 {
53
54
  eth_buffer::eth_buffer(size_t rx_bufsize)
55
    : d_fd(0), d_using_tpring(false), d_buflen(0), d_buf(0), d_frame_nr(0),
56
      d_frame_size(0), d_head(0), d_ring(0), d_ethernet(new ethernet())
57
  {
58
    if (rx_bufsize == 0)
59
      d_buflen = (size_t)DEFAULT_MEM_SIZE;
60
    else
61
      d_buflen = std::min((size_t)MAX_MEM_SIZE, rx_bufsize);
62
        
63
    memset(d_mac, 0, sizeof(d_mac));
64
  }
65
66
  eth_buffer::~eth_buffer()
67
  {
68
    close();
69
  }
70
  
71
  bool 
72
  eth_buffer::open(const std::string &ifname, int protocol)
73
  {
74
    if (!d_ethernet->open(ifname, protocol)) {
75
      std::cerr << "eth_buffer: unable to open interface " 
76
                << ifname << std::endl;
77
      return false;
78
    }
79
80
    d_fd = d_ethernet->fd();
81
    memcpy(d_mac, d_ethernet->mac(), sizeof(d_mac));
82
    
83
    struct tpacket_req req;
84
    size_t page_size = getpagesize();
85
86
    // Calculate minimum power-of-two aligned size for frames
87
    req.tp_frame_size =
88
      (unsigned int)rint(pow(2, ceil(log2(TPACKET_ALIGN(TPACKET_HDRLEN)+TPACKET_ALIGN(MAX_PKT_SIZE)))));
89
    d_frame_size = req.tp_frame_size;
90
91
    // Calculate minimum contiguous pages needed to enclose a frame
92
    int npages = (page_size > req.tp_frame_size) ? 1 : ((req.tp_frame_size+page_size-1)/page_size);
93
    req.tp_block_size = page_size << (int)ceil(log2(npages));
94
95
    // Calculate number of blocks
96
    req.tp_block_nr = (int)(d_buflen/req.tp_block_size);
97
                               
98
99
    // Recalculate buffer length
100
    d_buflen = req.tp_block_nr*req.tp_block_size;
101
102
    // Finally, calculate total number of frames.  Since frames, blocks,
103
    // and pages are all power-of-two aligned, frames are contiguous
104
    req.tp_frame_nr = d_buflen/req.tp_frame_size;
105
    d_frame_nr = req.tp_frame_nr;
106
107
#if 0
108
    if (ETH_BUFFER_DEBUG)
109
      std::cerr << "eth_buffer:" 
110
                << " frame_size=" << req.tp_frame_size
111
                << " block_size=" << req.tp_block_size
112
                << " block_nr=" << req.tp_block_nr
113
                << " frame_nr=" << req.tp_frame_nr
114
                << " buflen=" << d_buflen
115
                << std::endl;
116
#endif
117
118
    // Try to get kernel shared memory buffer    
119
    if (setsockopt(d_fd, SOL_PACKET, PACKET_RX_RING, (void *)&req, sizeof(req))) {
120
      perror("eth_buffer: setsockopt");
121
      d_using_tpring = false;
122
      if (!(d_buf = (uint8_t *)malloc(d_buflen))) {
123
        std::cerr << "eth_buffer: failed to allocate packet memory" << std::endl;
124
        return false;
125
      }
126
      
127
      std::cerr << "eth_buffer: using malloc'd memory for buffer" << std::endl;
128
    }
129
    else {
130
      d_using_tpring = true;
131
      void *p = mmap(0, d_buflen, PROT_READ|PROT_WRITE, MAP_SHARED, d_fd, 0);
132
      if (p == MAP_FAILED){
133
        perror("eth_buffer: mmap");
134
        return false;
135
      }
136
      d_buf = (uint8_t *) p;
137
138
      if (ETH_BUFFER_DEBUG)
139
        std::cerr << "eth_buffer: using kernel shared mem for buffer" << std::endl;
140
    }
141
142
    // Initialize our pointers into the packet ring
143
    d_ring = std::vector<uint8_t *>(req.tp_frame_nr);
144
    for (unsigned int i=0; i < req.tp_frame_nr; i++) {
145
      d_ring[i] = (uint8_t *)(d_buf+i*req.tp_frame_size);
146
    }
147
148
    // If not using kernel ring, instantiate select/read thread here
149
150
    return true;
151
  }
152
153
  bool
154
  eth_buffer::close()
155
  {
156
    // if we have background thread, stop it here
157
158
    if (!d_using_tpring && d_buf)
159
        free(d_buf);
160
        
161
    return d_ethernet->close();
162
  }
163
164
  bool 
165
  eth_buffer::attach_pktfilter(pktfilter *pf)
166
  {
167
    return d_ethernet->attach_pktfilter(pf);
168
  }
169
170
  inline bool
171
  eth_buffer::frame_available()
172
  {
173
    return (((tpacket_hdr *)d_ring[d_head])->tp_status != TP_STATUS_KERNEL);
174
  }
175
  
176
  eth_buffer::result
177
  eth_buffer::rx_frames(data_handler *f, int timeout_in_ms)
178
  {
179
    DEBUG_LOG("\n");
180
      
181
    while (!frame_available()) {
182
      if (timeout_in_ms == 0) {
183
        DEBUG_LOG("w");
184
        return EB_WOULD_BLOCK;
185
      }
186
      
187
      struct pollfd pfd;
188
      pfd.fd = d_fd;
189
      pfd.revents = 0;
190
      pfd.events = POLLIN;
191
192
      DEBUG_LOG("P");
193
194
      int pres = poll(&pfd, 1, timeout_in_ms);
195
      if (pres == -1) {
196
        perror("poll");
197
        return EB_ERROR;
198
      }
199
200
      if (pres == 0) {
201
        DEBUG_LOG("t");
202
        return EB_TIMED_OUT;
203
      }
204
    }
205
206
    // Iterate through available packets
207
    while (frame_available()) {
208
      // Get start of ethernet frame and length
209
      tpacket_hdr *hdr = (tpacket_hdr *)d_ring[d_head];
210
      void *base = (uint8_t *)hdr+hdr->tp_mac;
211
      size_t len = hdr->tp_len;
212
      
213
      // FYI, (base % 4 == 2) Not what we want given the current FPGA
214
      // code.  This means that our uint32_t samples are not 4-byte
215
      // aligned.  We'll have to deal with it downstream.
216
217
      if (0)
218
        fprintf(stderr, "eth_buffer: base = %p  tp_mac = %3d  tp_net = %3d\n",
219
                base, hdr->tp_mac, hdr->tp_net);
220
221
      // Invoke data handler
222
      data_handler::result r = (*f)(base, len);
223
      if (!(r & data_handler::KEEP))
224
        hdr->tp_status = TP_STATUS_KERNEL; // mark it free
225
226
      inc_head();
227
228
      if (r & data_handler::DONE)
229
        break;
230
    }
231
232
    DEBUG_LOG("|");
233
    return EB_OK;
234
  }
235
236
  eth_buffer::result
237
  eth_buffer::tx_frame(const void *base, size_t len, int flags)
238
  {
239
    DEBUG_LOG("T");
240
241
    if (flags & EF_DONTWAIT)    // FIXME: implement flags
242
      throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
243
244
    int res = d_ethernet->write_packet(base, len);
245
    if (res < 0 || (unsigned int)res != len)
246
      return EB_ERROR;
247
248
    return EB_OK;
249
  }
250
251
  eth_buffer::result
252
  eth_buffer::tx_framev(const eth_iovec *iov, int iovcnt, int flags)
253
  {
254
    DEBUG_LOG("T");
255
256
    if (flags & EF_DONTWAIT)    // FIXME: implement flags
257
      throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
258
259
    int res = d_ethernet->write_packetv(iov, iovcnt);
260
    if (res < 0)
261
      return EB_ERROR;
262
263
    return EB_OK;
264
  }
265
266
  void
267
  eth_buffer::release_frame(void *base)
268
  {
269
    // Get d_frame_size aligned header
270
    tpacket_hdr *hdr = (tpacket_hdr *)((intptr_t)base & ~(d_frame_size-1));
271
    hdr->tp_status = TP_STATUS_KERNEL; // mark it free
272
  }
273
  
274
} // namespace usrp2