forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.cc
371 lines (341 loc) · 13 KB
/
net.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* 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 <boost/asio/ip/address_v4.hpp>
#include <boost/algorithm/string.hpp>
#include "net.hh"
#include <utility>
#include "toeplitz.hh"
using std::move;
ipv4_addr::ipv4_addr(const std::string &addr) {
std::vector<std::string> items;
boost::split(items, addr, boost::is_any_of(":"));
if (items.size() == 1) {
ip = boost::asio::ip::address_v4::from_string(addr).to_ulong();
port = 0;
} else if (items.size() == 2) {
ip = boost::asio::ip::address_v4::from_string(items[0]).to_ulong();
port = std::stoul(items[1]);
} else {
throw std::invalid_argument("invalid format: " + addr);
}
}
ipv4_addr::ipv4_addr(const std::string &addr, uint16_t port_) : ip(boost::asio::ip::address_v4::from_string(addr).to_ulong()), port(port_) {}
namespace net {
inline
bool qp::poll_tx() {
if (_tx_packetq.size() < 16) {
// refill send queue from upper layers
uint32_t work;
do {
work = 0;
for (auto&& pr : _pkt_providers) {
auto p = pr();
if (p) {
work++;
_tx_packetq.push_back(std::move(p.value()));
if (_tx_packetq.size() == 128) {
break;
}
}
}
} while (work && _tx_packetq.size() < 128);
}
if (!_tx_packetq.empty()) {
_stats.tx.good.update_pkts_bunch(send(_tx_packetq));
return true;
}
return false;
}
qp::qp(bool register_copy_stats,
const std::string stats_plugin_name, uint8_t qid)
: _tx_poller(reactor::poller::simple([this] { return poll_tx(); }))
, _stats_plugin_name(stats_plugin_name)
, _queue_name(std::string("queue") + std::to_string(qid))
, _collectd_regs({
//
// Packets rate: DERIVE:0:u
//
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "if_packets", _queue_name)
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.rx.good.packets)
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.tx.good.packets)
),
//
// Bytes rate: DERIVE:0:U
//
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "if_octets", _queue_name)
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.rx.good.bytes)
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.tx.good.bytes)
),
//
// Queue length: GAUGE:0:U
//
// Tx
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "queue_length", "tx-packet-queue")
, scollectd::make_typed(scollectd::data_type::GAUGE
, std::bind(&decltype(_tx_packetq)::size, &_tx_packetq))
),
//
// Linearization counter: DERIVE:0:U
//
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "total_operations", "xmit-linearized")
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.tx.linearized)
),
//
// Number of packets in last bunch: GAUGE:0:U
//
// Tx
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "requests", "tx-packet-queue-last-bunch")
, scollectd::make_typed(scollectd::data_type::GAUGE
, _stats.tx.good.last_bunch)
),
// Rx
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "requests", "rx-packet-queue-last-bunch")
, scollectd::make_typed(scollectd::data_type::GAUGE
, _stats.rx.good.last_bunch)
),
//
// Fragments rate: DERIVE:0:U
//
// Tx
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "total_operations", "tx-frags")
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.tx.good.nr_frags)
),
// Rx
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "total_operations", "rx-frags")
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.rx.good.nr_frags)
),
})
{
if (register_copy_stats) {
//
// Non-zero-copy data bytes rate: DERIVE:0:u
//
_collectd_regs.push_back(
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "if_octets", _queue_name + " Copy Bytes")
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.rx.good.copy_bytes)
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.tx.good.copy_bytes)
));
//
// Non-zero-copy data fragments rate: DERIVE:0:u
//
// Tx
_collectd_regs.push_back(
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "total_operations", "tx-frags-copy")
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.tx.good.copy_frags)
));
// Rx
_collectd_regs.push_back(
scollectd::add_polled_metric(scollectd::type_instance_id(
_stats_plugin_name
, scollectd::per_cpu_plugin_instance
, "total_operations", "rx-frags-copy")
, scollectd::make_typed(scollectd::data_type::DERIVE
, _stats.rx.good.copy_frags)
));
}
}
qp::~qp() {
}
void qp::configure_proxies(const std::map<unsigned, float>& cpu_weights) {
assert(!cpu_weights.empty());
if ((cpu_weights.size() == 1 && cpu_weights.begin()->first == engine().cpu_id())) {
// special case queue sending to self only, to avoid requiring a hash value
return;
}
register_packet_provider([this] {
std::experimental::optional<packet> p;
if (!_proxy_packetq.empty()) {
p = std::move(_proxy_packetq.front());
_proxy_packetq.pop_front();
}
return p;
});
build_sw_reta(cpu_weights);
}
void qp::build_sw_reta(const std::map<unsigned, float>& cpu_weights) {
float total_weight = 0;
for (auto&& x : cpu_weights) {
total_weight += x.second;
}
float accum = 0;
unsigned idx = 0;
std::array<uint8_t, 128> reta;
for (auto&& entry : cpu_weights) {
auto cpu = entry.first;
auto weight = entry.second;
accum += weight;
while (idx < (accum / total_weight * reta.size() - 0.5)) {
reta[idx++] = cpu;
}
}
_sw_reta = reta;
}
subscription<packet>
device::receive(std::function<future<> (packet)> next_packet) {
auto sub = _queues[engine().cpu_id()]->_rx_stream.listen(std::move(next_packet));
_queues[engine().cpu_id()]->rx_start();
return std::move(sub);
}
void device::set_local_queue(std::unique_ptr<qp> dev) {
assert(!_queues[engine().cpu_id()]);
_queues[engine().cpu_id()] = dev.get();
engine().at_destroy([dev = std::move(dev)] {});
}
l3_protocol::l3_protocol(interface* netif, eth_protocol_num proto_num, packet_provider_type func)
: _netif(netif), _proto_num(proto_num) {
_netif->register_packet_provider(std::move(func));
}
subscription<packet, ethernet_address> l3_protocol::receive(
std::function<future<> (packet p, ethernet_address from)> rx_fn,
std::function<bool (forward_hash&, packet&, size_t)> forward) {
return _netif->register_l3(_proto_num, std::move(rx_fn), std::move(forward));
};
interface::interface(std::shared_ptr<device> dev)
: _dev(dev)
, _rx(_dev->receive([this] (packet p) { return dispatch_packet(std::move(p)); }))
, _hw_address(_dev->hw_address())
, _hw_features(_dev->hw_features()) {
dev->local_queue().register_packet_provider([this, idx = 0u] () mutable {
std::experimental::optional<packet> p;
for (size_t i = 0; i < _pkt_providers.size(); i++) {
auto l3p = _pkt_providers[idx++]();
if (idx == _pkt_providers.size())
idx = 0;
if (l3p) {
auto l3pv = std::move(l3p.value());
auto eh = l3pv.p.prepend_header<eth_hdr>();
eh->dst_mac = l3pv.to;
eh->src_mac = _hw_address;
eh->eth_proto = uint16_t(l3pv.proto_num);
*eh = hton(*eh);
p = std::move(l3pv.p);
return p;
}
}
return p;
});
}
subscription<packet, ethernet_address>
interface::register_l3(eth_protocol_num proto_num,
std::function<future<> (packet p, ethernet_address from)> next,
std::function<bool (forward_hash&, packet& p, size_t)> forward) {
auto i = _proto_map.emplace(std::piecewise_construct, std::make_tuple(uint16_t(proto_num)), std::forward_as_tuple(std::move(forward)));
assert(i.second);
l3_rx_stream& l3_rx = i.first->second;
return l3_rx.packet_stream.listen(std::move(next));
}
unsigned interface::hash2cpu(uint32_t hash) {
return _dev->hash2cpu(hash);
}
uint16_t interface::hw_queues_count() {
return _dev->hw_queues_count();
}
const rss_key_type& interface::rss_key() const {
return _dev->rss_key();
}
void interface::forward(unsigned cpuid, packet p) {
static __thread unsigned queue_depth;
if (queue_depth < 1000) {
queue_depth++;
auto src_cpu = engine().cpu_id();
smp::submit_to(cpuid, [this, p = std::move(p), src_cpu]() mutable {
_dev->l2receive(p.free_on_cpu(src_cpu));
}).then([] {
queue_depth--;
});
}
}
future<> interface::dispatch_packet(packet p) {
auto eh = p.get_header<eth_hdr>();
if (eh) {
auto i = _proto_map.find(ntoh(eh->eth_proto));
if (i != _proto_map.end()) {
l3_rx_stream& l3 = i->second;
auto fw = _dev->forward_dst(engine().cpu_id(), [&p, &l3, this] () {
auto hwrss = p.rss_hash();
if (hwrss) {
return hwrss.value();
} else {
forward_hash data;
if (l3.forward(data, p, sizeof(eth_hdr))) {
return toeplitz_hash(rss_key(), data);
}
return 0u;
}
});
if (fw != engine().cpu_id()) {
forward(fw, std::move(p));
} else {
auto h = ntoh(*eh);
auto from = h.src_mac;
p.trim_front(sizeof(*eh));
// avoid chaining, since queue lenth is unlimited
// drop instead.
if (l3.ready.available()) {
l3.ready = l3.packet_stream.produce(std::move(p), from);
}
}
}
}
return make_ready_future<>();
}
}