forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.cc
228 lines (190 loc) · 6.26 KB
/
udp.cc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#include "ip.hh"
#include "stack.hh"
namespace seastar {
using namespace net;
namespace net {
namespace ipv4_udp_impl {
static inline
ipv4_addr
to_ipv4_addr(ipv4_address a, uint16_t port) {
return {a.ip, port};
}
class native_datagram : public udp_datagram_impl {
private:
ipv4_addr _src;
ipv4_addr _dst;
packet _p;
public:
native_datagram(ipv4_address src, ipv4_address dst, packet p)
: _p(std::move(p)) {
udp_hdr* hdr = _p.get_header<udp_hdr>();
auto h = ntoh(*hdr);
_p.trim_front(sizeof(*hdr));
_src = to_ipv4_addr(src, h.src_port);
_dst = to_ipv4_addr(dst, h.dst_port);
}
virtual ipv4_addr get_src() override {
return _src;
};
virtual ipv4_addr get_dst() override {
return _dst;
};
virtual uint16_t get_dst_port() override {
return _dst.port;
}
virtual packet& get_data() override {
return _p;
}
};
class native_channel : public udp_channel_impl {
private:
ipv4_udp& _proto;
ipv4_udp::registration _reg;
bool _closed;
lw_shared_ptr<udp_channel_state> _state;
public:
native_channel(ipv4_udp &proto, ipv4_udp::registration reg, lw_shared_ptr<udp_channel_state> state)
: _proto(proto)
, _reg(reg)
, _closed(false)
, _state(state)
{
}
~native_channel()
{
if (!_closed)
close();
}
virtual future<udp_datagram> receive() override {
return _state->_queue.pop_eventually();
}
virtual future<> send(ipv4_addr dst, const char* msg) override {
return send(dst, packet::from_static_data(msg, strlen(msg)));
}
virtual future<> send(ipv4_addr dst, packet p) override {
auto len = p.len();
return _state->wait_for_send_buffer(len).then([this, dst, p = std::move(p), len] () mutable {
p = packet(std::move(p), make_deleter([s = _state, len] { s->complete_send(len); }));
_proto.send(_reg.port(), dst, std::move(p));
});
}
virtual bool is_closed() const {
return _closed;
}
virtual void close() override {
_state->_queue.abort(std::make_exception_ptr(std::system_error(EBADF, std::system_category())));
_reg.unregister();
_state->_queue.abort(std::make_exception_ptr(std::system_error(EPIPE, std::system_category())));
_closed = true;
}
};
} /* namespace ipv4_udp_impl */
using namespace net::ipv4_udp_impl;
const int ipv4_udp::default_queue_size = 1024;
ipv4_udp::ipv4_udp(ipv4& inet)
: _inet(inet)
{
_inet.register_packet_provider([this] {
std::experimental::optional<ipv4_traits::l4packet> l4p;
if (!_packetq.empty()) {
l4p = std::move(_packetq.front());
_packetq.pop_front();
}
return l4p;
});
}
bool ipv4_udp::forward(forward_hash& out_hash_data, packet& p, size_t off)
{
auto uh = p.get_header<udp_hdr>(off);
if (!uh) {
out_hash_data.push_back(uh->src_port);
out_hash_data.push_back(uh->dst_port);
}
return true;
}
void ipv4_udp::received(packet p, ipv4_address from, ipv4_address to)
{
udp_datagram dgram(std::make_unique<native_datagram>(from, to, std::move(p)));
auto chan_it = _channels.find(dgram.get_dst_port());
if (chan_it != _channels.end()) {
auto chan = chan_it->second;
chan->_queue.push(std::move(dgram));
}
}
void ipv4_udp::send(uint16_t src_port, ipv4_addr dst, packet &&p)
{
auto src = _inet.host_address();
auto hdr = p.prepend_header<udp_hdr>();
hdr->src_port = src_port;
hdr->dst_port = dst.port;
hdr->len = p.len();
*hdr = hton(*hdr);
offload_info oi;
checksummer csum;
ipv4_traits::udp_pseudo_header_checksum(csum, src, dst, p.len());
bool needs_frag = ipv4::needs_frag(p, ip_protocol_num::udp, _inet.hw_features());
if (_inet.hw_features().tx_csum_l4_offload && !needs_frag) {
hdr->cksum = ~csum.get();
oi.needs_csum = true;
} else {
csum.sum(p);
hdr->cksum = csum.get();
oi.needs_csum = false;
}
oi.protocol = ip_protocol_num::udp;
p.set_offload_info(oi);
_inet.get_l2_dst_address(dst).then([this, dst, p = std::move(p)] (ethernet_address e_dst) mutable {
_packetq.emplace_back(ipv4_traits::l4packet{dst, std::move(p), e_dst, ip_protocol_num::udp});
});
}
uint16_t ipv4_udp::next_port(uint16_t port) {
return (port + 1) == 0 ? min_anonymous_port : port + 1;
}
udp_channel
ipv4_udp::make_channel(ipv4_addr addr) {
if (!is_ip_unspecified(addr)) {
throw std::runtime_error("Binding to specific IP not supported yet");
}
uint16_t bind_port;
if (!is_port_unspecified(addr)) {
if (_channels.count(addr.port)) {
throw std::runtime_error("Address already in use");
}
bind_port = addr.port;
} else {
auto starting_port = _next_anonymous_port;
while (_channels.count(_next_anonymous_port)) {
_next_anonymous_port = next_port(_next_anonymous_port);
if (starting_port == _next_anonymous_port) {
throw std::runtime_error("No free port");
}
}
bind_port = _next_anonymous_port;
_next_anonymous_port = next_port(_next_anonymous_port);
}
auto chan_state = make_lw_shared<udp_channel_state>(_queue_size);
_channels[bind_port] = chan_state;
return udp_channel(std::make_unique<native_channel>(*this, registration(*this, bind_port), chan_state));
}
} /* namespace net */
}