3
* Copyright 2008,2009 Free Software Foundation, Inc.
5
* This file is part of GNU Radio
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)
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.
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.
26
#include "eth_buffer.h"
28
#include <usrp2/data_handler.h>
29
#include <linux/if_packet.h>
30
#include <sys/socket.h>
41
#define ETH_BUFFER_DEBUG 0 // define to 0 or 1
43
#define DEBUG_LOG(x) ::write(2, (x), 1)
48
#define DEFAULT_MEM_SIZE 25e6 // ~0.25s @ 100 MB/s
49
#define MAX_MEM_SIZE 1000e6 // ~10.00s @ 100 MB/s.
50
#define MAX_SLAB_SIZE 131072 // 128 KB (FIXME fish out of /proc/slabinfo)
51
#define MAX_PKT_SIZE 1512 // we don't do jumbo frames
55
eth_buffer::eth_buffer(size_t rx_bufsize)
56
: d_fd(0), d_using_tpring(false), d_buflen(0), d_buf(0), d_frame_nr(0),
57
d_frame_size(0), d_head(0), d_ring(0), d_ethernet(new ethernet())
60
d_buflen = (size_t)DEFAULT_MEM_SIZE;
62
d_buflen = std::min((size_t)MAX_MEM_SIZE, rx_bufsize);
64
memset(d_mac, 0, sizeof(d_mac));
67
eth_buffer::~eth_buffer()
73
eth_buffer::open(const std::string &ifname, int protocol)
75
if (!d_ethernet->open(ifname, protocol)) {
76
std::cerr << "eth_buffer: unable to open interface "
77
<< ifname << std::endl;
81
d_fd = d_ethernet->fd();
82
memcpy(d_mac, d_ethernet->mac(), sizeof(d_mac));
84
struct tpacket_req req;
85
size_t page_size = getpagesize();
87
// Calculate minimum power-of-two aligned size for frames
89
(unsigned int)rint(pow(2, ceil(log2(TPACKET_ALIGN(TPACKET_HDRLEN)+TPACKET_ALIGN(MAX_PKT_SIZE)))));
90
d_frame_size = req.tp_frame_size;
92
// Calculate minimum contiguous pages needed to enclose a frame
93
int npages = (page_size > req.tp_frame_size) ? 1 : ((req.tp_frame_size+page_size-1)/page_size);
94
req.tp_block_size = page_size << (int)ceil(log2(npages));
96
// Calculate number of blocks
97
req.tp_block_nr = (int)(d_buflen/req.tp_block_size);
100
// Recalculate buffer length
101
d_buflen = req.tp_block_nr*req.tp_block_size;
103
// Finally, calculate total number of frames. Since frames, blocks,
104
// and pages are all power-of-two aligned, frames are contiguous
105
req.tp_frame_nr = d_buflen/req.tp_frame_size;
106
d_frame_nr = req.tp_frame_nr;
109
if (ETH_BUFFER_DEBUG)
110
std::cerr << "eth_buffer:"
111
<< " frame_size=" << req.tp_frame_size
112
<< " block_size=" << req.tp_block_size
113
<< " block_nr=" << req.tp_block_nr
114
<< " frame_nr=" << req.tp_frame_nr
115
<< " buflen=" << d_buflen
119
// Try to get kernel shared memory buffer
120
if (setsockopt(d_fd, SOL_PACKET, PACKET_RX_RING, (void *)&req, sizeof(req))) {
121
perror("eth_buffer: setsockopt");
122
d_using_tpring = false;
123
if (!(d_buf = (uint8_t *)malloc(d_buflen))) {
124
std::cerr << "eth_buffer: failed to allocate packet memory" << std::endl;
128
std::cerr << "eth_buffer: using malloc'd memory for buffer" << std::endl;
131
d_using_tpring = true;
132
void *p = mmap(0, d_buflen, PROT_READ|PROT_WRITE, MAP_SHARED, d_fd, 0);
133
if (p == MAP_FAILED){
134
perror("eth_buffer: mmap");
137
d_buf = (uint8_t *) p;
139
if (ETH_BUFFER_DEBUG)
140
std::cerr << "eth_buffer: using kernel shared mem for buffer" << std::endl;
143
// Initialize our pointers into the packet ring
144
d_ring = std::vector<uint8_t *>(req.tp_frame_nr);
145
for (unsigned int i=0; i < req.tp_frame_nr; i++) {
146
d_ring[i] = (uint8_t *)(d_buf+i*req.tp_frame_size);
149
// If not using kernel ring, instantiate select/read thread here
157
// if we have background thread, stop it here
159
if (!d_using_tpring && d_buf)
162
return d_ethernet->close();
166
eth_buffer::attach_pktfilter(pktfilter *pf)
168
return d_ethernet->attach_pktfilter(pf);
172
eth_buffer::frame_available()
174
return (((tpacket_hdr *)d_ring[d_head])->tp_status != TP_STATUS_KERNEL);
178
eth_buffer::rx_frames(data_handler *f, int timeout_in_ms)
182
while (!frame_available()) {
183
if (timeout_in_ms == 0) {
185
return EB_WOULD_BLOCK;
195
int pres = poll(&pfd, 1, timeout_in_ms);
207
// Iterate through available packets
208
while (frame_available()) {
209
// Get start of ethernet frame and length
210
tpacket_hdr *hdr = (tpacket_hdr *)d_ring[d_head];
211
void *base = (uint8_t *)hdr+hdr->tp_mac;
212
size_t len = hdr->tp_len;
214
// FYI, (base % 4 == 2) Not what we want given the current FPGA
215
// code. This means that our uint32_t samples are not 4-byte
216
// aligned. We'll have to deal with it downstream.
219
fprintf(stderr, "eth_buffer: base = %p tp_mac = %3d tp_net = %3d\n",
220
base, hdr->tp_mac, hdr->tp_net);
222
// Invoke data handler
223
data_handler::result r = (*f)(base, len);
224
if (!(r & data_handler::KEEP))
225
hdr->tp_status = TP_STATUS_KERNEL; // mark it free
229
if (r & data_handler::DONE)
238
eth_buffer::tx_frame(const void *base, size_t len, int flags)
242
if (flags & EF_DONTWAIT) // FIXME: implement flags
243
throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
245
int res = d_ethernet->write_packet(base, len);
246
if (res < 0 || (unsigned int)res != len)
253
eth_buffer::tx_framev(const eth_iovec *iov, int iovcnt, int flags)
257
if (flags & EF_DONTWAIT) // FIXME: implement flags
258
throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
260
int res = d_ethernet->write_packetv(iov, iovcnt);
268
eth_buffer::release_frame(void *base)
270
// Get d_frame_size aligned header
271
tpacket_hdr *hdr = (tpacket_hdr *)((intptr_t)base & ~(d_frame_size-1));
272
hdr->tp_status = TP_STATUS_KERNEL; // mark it free