forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxenfront.cc
458 lines (361 loc) · 12.5 KB
/
xenfront.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* 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/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include "core/posix.hh"
#include "core/vla.hh"
#include "core/reactor.hh"
#include "core/future-util.hh"
#include "core/stream.hh"
#include "core/circular_buffer.hh"
#include "core/align.hh"
#include <atomic>
#include <list>
#include <queue>
#include <fcntl.h>
#include <linux/if_tun.h>
#include "ip.hh"
#include "net/native-stack.hh"
#include <xen/xen.h>
#include <xen/memory.h>
#include <xen/sys/gntalloc.h>
#include "core/xen/xenstore.hh"
#include "core/xen/evtchn.hh"
#include "core/metrics.hh"
#include "xenfront.hh"
#include <unordered_set>
using namespace net;
namespace xen {
using phys = uint64_t;
class xenfront_device : public device {
public:
xenstore* _xenstore = xenstore::instance();
private:
net::hw_features _hw_features;
ethernet_address _hw_address;
std::string _device_str;
public:
bool _userspace;
public:
xenfront_device(boost::program_options::variables_map opts, bool userspace)
: _hw_address(net::parse_ethernet_address(_xenstore->read(path("mac"))))
, _device_str("device/vif/" + std::to_string(opts["vif"].as<unsigned>()))
, _userspace(userspace) {
_hw_features.rx_csum_offload = true;
_hw_features.tx_csum_l4_offload = true;
}
std::string path(std::string s) { return _device_str + "/" + s; }
ethernet_address hw_address() override {
return _hw_address;
}
net::hw_features hw_features() override {
return _hw_features;
}
virtual std::unique_ptr<qp> init_local_queue(boost::program_options::variables_map opts, uint16_t qid) override;
};
class xenfront_qp : public net::qp {
private:
xenfront_device* _dev;
unsigned _otherend;
std::string _backend;
gntalloc *_gntalloc;
evtchn *_evtchn;
port _tx_evtchn;
port _rx_evtchn;
front_ring<tx> _tx_ring;
front_ring<rx> _rx_ring;
grant_head *_tx_refs;
grant_head *_rx_refs;
std::unordered_map<std::string, int> _features;
static std::unordered_map<std::string, std::string> _supported_features;
port bind_tx_evtchn(bool split);
port bind_rx_evtchn(bool split);
future<> alloc_rx_references();
future<> handle_tx_completions();
future<> queue_rx_packet();
void alloc_one_rx_reference(unsigned id);
std::string path(std::string s) { return _dev->path(s); }
public:
explicit xenfront_qp(xenfront_device* dev, boost::program_options::variables_map opts);
~xenfront_qp();
virtual void rx_start() override;
virtual future<> send(packet p) override;
void inc_rx_error_count() { ++_stats.rx.bad.total; }
};
std::unordered_map<std::string, std::string>
xenfront_qp::_supported_features = {
{ "feature-split-event-channels", "feature-split-event-channels" },
{ "feature-rx-copy", "request-rx-copy" }
};
void
xenfront_qp::rx_start() {
keep_doing([this] {
return _rx_evtchn.pending().then([this] {
return queue_rx_packet();
});
});
}
future<>
xenfront_qp::send(packet _p) {
uint32_t frag = 0;
// There doesn't seem to be a way to tell xen, when using the userspace
// drivers, to map a particular page. Therefore, the only alternative
// here is to copy. All pages shared must come from the gntalloc mmap.
//
// A better solution could be to change the packet allocation path to
// use a pre-determined page for data.
//
// In-kernel should be fine
if (_p.nr_frags() > 1) {
_stats.tx.good.update_copy_stats(_p.nr_frags(), _p.len());
}
// FIXME: negotiate and use scatter/gather
_p.linearize();
++_stats.tx.linearized;
return _tx_ring.entries.has_room().then([this, p = std::move(_p), frag] () mutable {
auto req_prod = _tx_ring._sring->req_prod;
auto f = p.frag(frag);
auto ref = _tx_refs->new_ref(f.base, f.size);
unsigned idx = _tx_ring.entries.get_index();
assert(!_tx_ring.entries[idx]);
_tx_ring.entries[idx] = ref;
auto req = &_tx_ring._sring->_ring[idx].req;
req->gref = ref.xen_id;
req->offset = 0;
req->flags = {};
if (p.offload_info().protocol != ip_protocol_num::unused) {
req->flags.csum_blank = true;
req->flags.data_validated = true;
} else {
req->flags.data_validated = true;
}
req->id = idx;
req->size = f.size;
_tx_ring.req_prod_pvt = idx;
_tx_ring._sring->req_prod = req_prod + 1;
_tx_ring._sring->req_event++;
if ((frag + 1) == p.nr_frags()) {
_tx_evtchn.notify();
}
_stats.tx.good.update_frags_stats(1, f.size);
return make_ready_future<>();
});
// FIXME: Don't forget to clear all grant refs when frontend closes. Or is it automatic?
}
#define rmb() asm volatile("lfence":::"memory");
#define wmb() asm volatile("":::"memory");
template <typename T>
future<> front_ring<T>::entries::has_room() {
return _available.wait();
}
template <typename T>
void front_ring<T>::entries::free_index(unsigned id) {
_available.signal();
}
template <typename T>
unsigned front_ring<T>::entries::get_index() {
return front_ring<T>::idx(_next_idx++);
}
template <typename T>
void front_ring<T>::process_ring(std::function<bool (gntref &entry, T& el)> func, grant_head *refs)
{
auto prod = _sring->rsp_prod;
rmb();
for (unsigned i = rsp_cons; i != prod; i++) {
auto el = _sring->_ring[idx(i)];
if (el.rsp.status < 0) {
dump("Packet error", el.rsp);
_dev.inc_rx_error_count();
continue;
}
auto& entry = entries[i];
if (!func(entry, el)) {
continue;
}
assert(entry.xen_id >= 0);
refs->free_ref(entry);
entries.free_index(i);
prod = _sring->rsp_prod;
}
rsp_cons = prod;
_sring->rsp_event = prod + 1;
}
future<> xenfront_qp::queue_rx_packet()
{
uint64_t bunch = 0;
uint64_t bytes = 0;
_rx_ring.process_ring([this, &bunch, &bytes] (gntref &entry, rx &rx) mutable {
packet p(static_cast<char *>(entry.page) + rx.rsp.offset, rx.rsp.status);
_dev->l2receive(std::move(p));
bytes += rx.rsp.status;
bunch++;
return true;
}, _rx_refs);
_stats.rx.good.update_pkts_bunch(bunch);
//
// Our XEN implementation only supports packets with a single fragment
// at the moment.
//
_stats.rx.good.update_frags_stats(bunch, bytes);
return make_ready_future<>();
}
void xenfront_qp::alloc_one_rx_reference(unsigned index) {
_rx_ring.entries[index] = _rx_refs->new_ref();
// This is how the backend knows where to put data.
auto req = &_rx_ring._sring->_ring[index].req;
req->id = index;
req->gref = _rx_ring.entries[index].xen_id;
}
future<> xenfront_qp::alloc_rx_references() {
return _rx_ring.entries.has_room().then([this] () {
unsigned i = _rx_ring.entries.get_index();
auto req_prod = _rx_ring.req_prod_pvt;
alloc_one_rx_reference(i);
++req_prod;
_rx_ring.req_prod_pvt = req_prod;
wmb();
_rx_ring._sring->req_prod = req_prod;
/* ready */
_rx_evtchn.notify();
});
}
future<> xenfront_qp::handle_tx_completions() {
_tx_ring.process_ring([this] (gntref &entry, tx &tx) {
if (tx.rsp.status == 1) {
return false;
}
if (tx.rsp.status != 0) {
_tx_ring.dump("TX positive packet error", tx.rsp);
return false;
}
return true;
}, _tx_refs);
return make_ready_future<>();
}
port xenfront_qp::bind_tx_evtchn(bool split) {
return _evtchn->bind();
}
port xenfront_qp::bind_rx_evtchn(bool split) {
if (split) {
return _evtchn->bind();
}
return _evtchn->bind(_tx_evtchn.number());
}
xenfront_qp::xenfront_qp(xenfront_device* dev, boost::program_options::variables_map opts)
: qp(true), _dev(dev)
, _otherend(_dev->_xenstore->read<int>(path("backend-id")))
, _backend(_dev->_xenstore->read(path("backend")))
, _gntalloc(gntalloc::instance(_dev->_userspace, _otherend))
, _evtchn(evtchn::instance(_dev->_userspace, _otherend))
, _tx_ring(_gntalloc->alloc_ref(), *this)
, _rx_ring(_gntalloc->alloc_ref(), *this)
, _tx_refs(_gntalloc->alloc_ref(front_ring<tx>::nr_ents))
, _rx_refs(_gntalloc->alloc_ref(front_ring<rx>::nr_ents)) {
auto all_features = _dev->_xenstore->ls(_backend);
for (auto&& feat : all_features) {
if (feat.compare(0, 8, "feature-") == 0) {
auto val = _dev->_xenstore->read<int>(_backend + "/" + feat);
try {
auto key = _supported_features.at(feat);
_features[key] = val;
} catch (const std::out_of_range& oor) {
_features[feat] = 0;
}
}
}
if (!opts["split-event-channels"].as<bool>()) {
_features["feature-split-event-channels"] = 0;
}
bool split = _features["feature-split-event-channels"];
_tx_evtchn = bind_tx_evtchn(split);
_rx_evtchn = bind_rx_evtchn(split);
{
auto t = xenstore::xenstore_transaction();
for (auto&& f: _features) {
_dev->_xenstore->write(path(f.first), f.second, t);
}
if (split) {
_dev->_xenstore->write<int>(path("event-channel-tx"), _tx_evtchn.number(), t);
_dev->_xenstore->write<int>(path("event-channel-rx"), _rx_evtchn.number(), t);
} else {
_dev->_xenstore->write<int>(path("event-channel"), _rx_evtchn.number(), t);
}
_dev->_xenstore->write<int>(path("tx-ring-ref"), _tx_ring.ref, t);
_dev->_xenstore->write<int>(path("rx-ring-ref"), _rx_ring.ref, t);
_dev->_xenstore->write<int>(path("state"), 4, t);
}
// Register Rx error statistics
namespace sm = seastar::metrics;
_metrics.add_group("network", {
sm::make_derive(_queue_name + "_rx_errors", _stats.rx.bad.total,
sm::description("Counts a total number of Rx packets with errors."))
});
keep_doing([this] {
return alloc_rx_references();
});
_rx_evtchn.umask();
keep_doing([this] () {
return _tx_evtchn.pending().then([this] {
handle_tx_completions();
});
});
_tx_evtchn.umask();
}
xenfront_qp::~xenfront_qp() {
{
auto t = xenstore::xenstore_transaction();
for (auto& f: _features) {
_dev->_xenstore->remove(path(f.first), t);
}
_dev->_xenstore->remove(path("event-channel-tx"), t);
_dev->_xenstore->remove(path("event-channel-rx"), t);
_dev->_xenstore->remove(path("event-channel"), t);
_dev->_xenstore->remove(path("tx-ring-ref"), t);
_dev->_xenstore->remove(path("rx-ring-ref"), t);
_dev->_xenstore->write<int>(path("state"), 6, t);
}
_dev->_xenstore->write<int>(path("state"), 1);
}
boost::program_options::options_description
get_xenfront_net_options_description() {
boost::program_options::options_description opts(
"xenfront net options");
opts.add_options()
("vif",
boost::program_options::value<unsigned>()->default_value(0),
"vif number to hijack")
("split-event-channels",
boost::program_options::value<bool>()->default_value(true),
"Split event channel support")
;
return opts;
}
std::unique_ptr<qp> xenfront_device::init_local_queue(boost::program_options::variables_map opts, uint16_t qid) {
assert(!qid);
return std::make_unique<xenfront_qp>(this, opts);
}
std::unique_ptr<net::device> create_xenfront_net_device(boost::program_options::variables_map opts, bool userspace) {
static bool called = false;
assert(!called);
called = true;
return std::make_unique<xenfront_device>(opts, userspace);
}
}