Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / host / lib / eth_buffer.cc
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 #include <cstdio>
39
40
41 #define ETH_BUFFER_DEBUG      0 // define to 0 or 1
42 #if ETH_BUFFER_DEBUG
43 #define DEBUG_LOG(x) ::write(2, (x), 1)
44 #else
45 #define DEBUG_LOG(X)
46 #endif
47
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
52
53 namespace usrp2 {
54
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())
58   {
59     if (rx_bufsize == 0)
60       d_buflen = (size_t)DEFAULT_MEM_SIZE;
61     else
62       d_buflen = std::min((size_t)MAX_MEM_SIZE, rx_bufsize);
63         
64     memset(d_mac, 0, sizeof(d_mac));
65   }
66
67   eth_buffer::~eth_buffer()
68   {
69     close();
70   }
71   
72   bool 
73   eth_buffer::open(const std::string &ifname, int protocol)
74   {
75     if (!d_ethernet->open(ifname, protocol)) {
76       std::cerr << "eth_buffer: unable to open interface " 
77                 << ifname << std::endl;
78       return false;
79     }
80
81     d_fd = d_ethernet->fd();
82     memcpy(d_mac, d_ethernet->mac(), sizeof(d_mac));
83     
84     struct tpacket_req req;
85     size_t page_size = getpagesize();
86
87     // Calculate minimum power-of-two aligned size for frames
88     req.tp_frame_size =
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;
91
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));
95
96     // Calculate number of blocks
97     req.tp_block_nr = (int)(d_buflen/req.tp_block_size);
98                                
99
100     // Recalculate buffer length
101     d_buflen = req.tp_block_nr*req.tp_block_size;
102
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;
107
108 #if 0
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
116                 << std::endl;
117 #endif
118
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;
125         return false;
126       }
127       
128       std::cerr << "eth_buffer: using malloc'd memory for buffer" << std::endl;
129     }
130     else {
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");
135         return false;
136       }
137       d_buf = (uint8_t *) p;
138
139       if (ETH_BUFFER_DEBUG)
140         std::cerr << "eth_buffer: using kernel shared mem for buffer" << std::endl;
141     }
142
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);
147     }
148
149     // If not using kernel ring, instantiate select/read thread here
150
151     return true;
152   }
153
154   bool
155   eth_buffer::close()
156   {
157     // if we have background thread, stop it here
158
159     if (!d_using_tpring && d_buf)
160         free(d_buf);
161         
162     return d_ethernet->close();
163   }
164
165   bool 
166   eth_buffer::attach_pktfilter(pktfilter *pf)
167   {
168     return d_ethernet->attach_pktfilter(pf);
169   }
170
171   inline bool
172   eth_buffer::frame_available()
173   {
174     return (((tpacket_hdr *)d_ring[d_head])->tp_status != TP_STATUS_KERNEL);
175   }
176   
177   eth_buffer::result
178   eth_buffer::rx_frames(data_handler *f, int timeout_in_ms)
179   {
180     DEBUG_LOG("\n");
181       
182     while (!frame_available()) {
183       if (timeout_in_ms == 0) {
184         DEBUG_LOG("w");
185         return EB_WOULD_BLOCK;
186       }
187       
188       struct pollfd pfd;
189       pfd.fd = d_fd;
190       pfd.revents = 0;
191       pfd.events = POLLIN;
192
193       DEBUG_LOG("P");
194
195       int pres = poll(&pfd, 1, timeout_in_ms);
196       if (pres == -1) {
197         perror("poll");
198         return EB_ERROR;
199       }
200
201       if (pres == 0) {
202         DEBUG_LOG("t");
203         return EB_TIMED_OUT;
204       }
205     }
206
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;
213       
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.
217
218       if (0)
219         fprintf(stderr, "eth_buffer: base = %p  tp_mac = %3d  tp_net = %3d\n",
220                 base, hdr->tp_mac, hdr->tp_net);
221
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
226
227       inc_head();
228
229       if (r & data_handler::DONE)
230         break;
231     }
232
233     DEBUG_LOG("|");
234     return EB_OK;
235   }
236
237   eth_buffer::result
238   eth_buffer::tx_frame(const void *base, size_t len, int flags)
239   {
240     DEBUG_LOG("T");
241
242     if (flags & EF_DONTWAIT)    // FIXME: implement flags
243       throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
244
245     int res = d_ethernet->write_packet(base, len);
246     if (res < 0 || (unsigned int)res != len)
247       return EB_ERROR;
248
249     return EB_OK;
250   }
251
252   eth_buffer::result
253   eth_buffer::tx_framev(const eth_iovec *iov, int iovcnt, int flags)
254   {
255     DEBUG_LOG("T");
256
257     if (flags & EF_DONTWAIT)    // FIXME: implement flags
258       throw std::runtime_error("tx_frame: EF_DONTWAIT not implemented");
259
260     int res = d_ethernet->write_packetv(iov, iovcnt);
261     if (res < 0)
262       return EB_ERROR;
263
264     return EB_OK;
265   }
266
267   void
268   eth_buffer::release_frame(void *base)
269   {
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
273   }
274   
275 } // namespace usrp2