1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//
// Copyright 2010 Ettus Research LLC
//
// This program 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 of the License, or
// (at your option) any later version.
//
// This program 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, see <http://www.gnu.org/licenses/>.
//
#include <uhd/transport/udp_zero_copy.hpp>
#include <uhd/utils/assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/asio.hpp>
#include <boost/format.hpp>
#include <iostream>
using namespace uhd::transport;
/***********************************************************************
* Constants
**********************************************************************/
static const size_t MIN_SOCK_BUFF_SIZE = size_t(100e3);
static const size_t MAX_DGRAM_SIZE = 2048; //assume max size on send and recv
static const double RECV_TIMEOUT = 0.1; // 100 ms
/***********************************************************************
* Managed receive buffer implementation for udp zero-copy asio:
**********************************************************************/
class managed_recv_buffer_impl : public managed_recv_buffer{
public:
managed_recv_buffer_impl(const boost::asio::const_buffer &buff) : _buff(buff){
/* NOP */
}
~managed_recv_buffer_impl(void){
delete [] this->cast<const boost::uint8_t *>();
}
private:
const boost::asio::const_buffer &get(void) const{
return _buff;
}
const boost::asio::const_buffer _buff;
};
/***********************************************************************
* Managed send buffer implementation for udp zero-copy asio:
**********************************************************************/
class managed_send_buffer_impl : public managed_send_buffer{
public:
managed_send_buffer_impl(
const boost::asio::mutable_buffer &buff,
boost::asio::ip::udp::socket *socket
) : _buff(buff), _socket(socket){
/* NOP */
}
~managed_send_buffer_impl(void){
/* NOP */
}
void commit(size_t num_bytes){
_socket->send(boost::asio::buffer(_buff, num_bytes));
}
private:
const boost::asio::mutable_buffer &get(void) const{
return _buff;
}
const boost::asio::mutable_buffer _buff;
boost::asio::ip::udp::socket *_socket;
};
/***********************************************************************
* Zero Copy UDP implementation with ASIO:
* This is the portable zero copy implementation for systems
* where a faster, platform specific solution is not available.
* However, it is not a true zero copy implementation as each
* send and recv requires a copy operation to/from userspace.
**********************************************************************/
class udp_zero_copy_impl : public udp_zero_copy{
public:
typedef boost::shared_ptr<udp_zero_copy_impl> sptr;
//structors
udp_zero_copy_impl(const std::string &addr, const std::string &port);
~udp_zero_copy_impl(void);
//send/recv
managed_recv_buffer::sptr get_recv_buff(void);
managed_send_buffer::sptr get_send_buff(void);
//manage buffer
template <typename Opt> size_t get_buff_size(void){
Opt option;
_socket->get_option(option);
return option.value();
}
template <typename Opt> size_t resize_buff(size_t num_bytes){
Opt option(num_bytes);
_socket->set_option(option);
return get_buff_size<Opt>();
}
private:
boost::asio::ip::udp::socket *_socket;
boost::asio::io_service _io_service;
//send and recv buffer memory (allocated once)
boost::uint8_t _send_mem[MIN_SOCK_BUFF_SIZE];
managed_send_buffer::sptr _send_buff;
};
udp_zero_copy_impl::udp_zero_copy_impl(const std::string &addr, const std::string &port){
//std::cout << boost::format("Creating udp transport for %s %s") % addr % port << std::endl;
// resolve the address
boost::asio::ip::udp::resolver resolver(_io_service);
boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), addr, port);
boost::asio::ip::udp::endpoint receiver_endpoint = *resolver.resolve(query);
// create, open, and connect the socket
_socket = new boost::asio::ip::udp::socket(_io_service);
_socket->open(boost::asio::ip::udp::v4());
_socket->connect(receiver_endpoint);
// create the managed send buff (just once)
_send_buff = managed_send_buffer::sptr(new managed_send_buffer_impl(
boost::asio::buffer(_send_mem, MIN_SOCK_BUFF_SIZE), _socket
));
// set recv timeout
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = size_t(RECV_TIMEOUT*1e6);
UHD_ASSERT_THROW(setsockopt(
_socket->native(),
SOL_SOCKET, SO_RCVTIMEO,
(const char *)&tv, sizeof(timeval)
) == 0);
}
udp_zero_copy_impl::~udp_zero_copy_impl(void){
delete _socket;
}
managed_recv_buffer::sptr udp_zero_copy_impl::get_recv_buff(void){
//allocate memory
boost::uint8_t *recv_mem = new boost::uint8_t[MAX_DGRAM_SIZE];
//call recv() with timeout option
size_t num_bytes = _socket->receive(boost::asio::buffer(recv_mem, MAX_DGRAM_SIZE));
//create a new managed buffer to house the data
return managed_recv_buffer::sptr(
new managed_recv_buffer_impl(boost::asio::buffer(recv_mem, num_bytes))
);
}
managed_send_buffer::sptr udp_zero_copy_impl::get_send_buff(void){
return _send_buff; //FIXME there is only ever one send buff, we assume that the caller doesnt hang onto these
}
/***********************************************************************
* UDP zero copy make function
**********************************************************************/
template<typename Opt> static inline void resize_buff_helper(
udp_zero_copy_impl::sptr udp_trans,
size_t target_size,
const std::string &name
){
//resize the buffer if size was provided
if (target_size > 0){
size_t actual_size = udp_trans->resize_buff<Opt>(target_size);
if (target_size != actual_size) std::cout << boost::format(
"Target %s buffer size: %d\n"
"Actual %s byffer size: %d"
) % name % target_size % name % actual_size << std::endl;
}
//otherwise, ensure that the buffer is at least the minimum size
else if (udp_trans->get_buff_size<Opt>() < MIN_SOCK_BUFF_SIZE){
resize_buff_helper<Opt>(udp_trans, MIN_SOCK_BUFF_SIZE, name);
}
}
udp_zero_copy::sptr udp_zero_copy::make(
const std::string &addr,
const std::string &port,
size_t recv_buff_size,
size_t send_buff_size
){
udp_zero_copy_impl::sptr udp_trans(new udp_zero_copy_impl(addr, port));
//call the helper to resize send and recv buffers
resize_buff_helper<boost::asio::socket_base::receive_buffer_size>(udp_trans, recv_buff_size, "recv");
resize_buff_helper<boost::asio::socket_base::send_buffer_size> (udp_trans, send_buff_size, "send");
return udp_trans;
}
|