forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpdk.cc
2306 lines (1947 loc) · 79.3 KB
/
dpdk.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*/
#ifdef HAVE_DPDK
#include <cinttypes>
#include "core/posix.hh"
#include "core/vla.hh"
#include "virtio-interface.hh"
#include "core/reactor.hh"
#include "core/stream.hh"
#include "core/circular_buffer.hh"
#include "core/align.hh"
#include "core/sstring.hh"
#include "core/memory.hh"
#include "core/metrics.hh"
#include "util/function_input_iterator.hh"
#include "util/transform_iterator.hh"
#include <atomic>
#include <vector>
#include <queue>
#include <experimental/optional>
#include <boost/preprocessor.hpp>
#include "ip.hh"
#include "const.hh"
#include "core/dpdk_rte.hh"
#include "dpdk.hh"
#include "toeplitz.hh"
#include <getopt.h>
#include <malloc.h>
#include <cinttypes>
#include <rte_config.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_pci.h>
#include <rte_ethdev.h>
#include <rte_cycles.h>
#include <rte_memzone.h>
#if RTE_VERSION <= RTE_VERSION_NUM(2,0,0,16)
static
inline
char*
rte_mbuf_to_baddr(rte_mbuf* mbuf) {
return reinterpret_cast<char*>(RTE_MBUF_TO_BADDR(mbuf));
}
void* as_cookie(struct rte_pktmbuf_pool_private& p) {
return reinterpret_cast<void*>(uint64_t(p.mbuf_data_room_size));
};
#else
void* as_cookie(struct rte_pktmbuf_pool_private& p) {
return &p;
};
#endif
#ifndef MARKER
typedef void *MARKER[0]; /**< generic marker for a point in a structure */
#endif
using namespace net;
namespace dpdk {
/******************* Net device related constatns *****************************/
static constexpr uint16_t default_ring_size = 512;
//
// We need 2 times the ring size of buffers because of the way PMDs
// refill the ring.
//
static constexpr uint16_t mbufs_per_queue_rx = 2 * default_ring_size;
static constexpr uint16_t rx_gc_thresh = 64;
//
// No need to keep more descriptors in the air than can be sent in a single
// rte_eth_tx_burst() call.
//
static constexpr uint16_t mbufs_per_queue_tx = 2 * default_ring_size;
static constexpr uint16_t mbuf_cache_size = 512;
static constexpr uint16_t mbuf_overhead =
sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM;
//
// We'll allocate 2K data buffers for an inline case because this would require
// a single page per mbuf. If we used 4K data buffers here it would require 2
// pages for a single buffer (due to "mbuf_overhead") and this is a much more
// demanding memory constraint.
//
static constexpr size_t inline_mbuf_data_size = 2048;
//
// Size of the data buffer in the non-inline case.
//
// We may want to change (increase) this value in future, while the
// inline_mbuf_data_size value will unlikely change due to reasons described
// above.
//
static constexpr size_t mbuf_data_size = 2048;
// (INLINE_MBUF_DATA_SIZE(2K)*32 = 64K = Max TSO/LRO size) + 1 mbuf for headers
static constexpr uint8_t max_frags = 32 + 1;
//
// Intel's 40G NIC HW limit for a number of fragments in an xmit segment.
//
// See Chapter 8.4.1 "Transmit Packet in System Memory" of the xl710 devices
// spec. for more details.
//
static constexpr uint8_t i40e_max_xmit_segment_frags = 8;
//
// VMWare's virtual NIC limit for a number of fragments in an xmit segment.
//
// see drivers/net/vmxnet3/base/vmxnet3_defs.h VMXNET3_MAX_TXD_PER_PKT
//
static constexpr uint8_t vmxnet3_max_xmit_segment_frags = 16;
static constexpr uint16_t inline_mbuf_size =
inline_mbuf_data_size + mbuf_overhead;
uint32_t qp_mempool_obj_size(bool hugetlbfs_membackend)
{
uint32_t mp_size = 0;
struct rte_mempool_objsz mp_obj_sz = {};
//
// We will align each size to huge page size because DPDK allocates
// physically contiguous memory region for each pool object.
//
// Rx
if (hugetlbfs_membackend) {
mp_size +=
align_up(rte_mempool_calc_obj_size(mbuf_overhead, 0, &mp_obj_sz)+
sizeof(struct rte_pktmbuf_pool_private),
memory::huge_page_size);
} else {
mp_size +=
align_up(rte_mempool_calc_obj_size(inline_mbuf_size, 0, &mp_obj_sz)+
sizeof(struct rte_pktmbuf_pool_private),
memory::huge_page_size);
}
//Tx
std::memset(&mp_obj_sz, 0, sizeof(mp_obj_sz));
mp_size += align_up(rte_mempool_calc_obj_size(inline_mbuf_size, 0,
&mp_obj_sz)+
sizeof(struct rte_pktmbuf_pool_private),
memory::huge_page_size);
return mp_size;
}
static constexpr const char* pktmbuf_pool_name = "dpdk_pktmbuf_pool";
/*
* When doing reads from the NIC queues, use this batch size
*/
static constexpr uint8_t packet_read_size = 32;
/******************************************************************************/
struct port_stats {
port_stats() : rx{}, tx{} {}
struct {
struct {
uint64_t mcast; // number of received multicast packets
uint64_t pause_xon; // number of received PAUSE XON frames
uint64_t pause_xoff; // number of received PAUSE XOFF frames
} good;
struct {
uint64_t dropped; // missed packets (e.g. full FIFO)
uint64_t crc; // packets with CRC error
uint64_t len; // packets with a bad length
uint64_t total; // total number of erroneous received packets
} bad;
} rx;
struct {
struct {
uint64_t pause_xon; // number of sent PAUSE XON frames
uint64_t pause_xoff; // number of sent PAUSE XOFF frames
} good;
struct {
uint64_t total; // total number of failed transmitted packets
} bad;
} tx;
};
#define XSTATS_ID_LIST \
(rx_multicast_packets) \
(rx_xon_packets) \
(rx_xoff_packets) \
(rx_crc_errors) \
(rx_length_errors) \
(rx_undersize_errors) \
(rx_oversize_errors) \
(tx_xon_packets) \
(tx_xoff_packets)
class dpdk_xstats {
public:
dpdk_xstats(uint8_t port_id)
: _port_id(port_id)
{
}
~dpdk_xstats()
{
if (_xstats)
delete _xstats;
if (_xstat_names)
delete _xstat_names;
}
enum xstat_id {
BOOST_PP_SEQ_ENUM(XSTATS_ID_LIST)
};
void start() {
_len = rte_eth_xstats_get_names(_port_id, NULL, 0);
_xstats = new rte_eth_xstat[_len];
_xstat_names = new rte_eth_xstat_name[_len];
update_xstats();
update_xstat_names();
update_offsets();
}
void update_xstats() {
auto len = rte_eth_xstats_get(_port_id, _xstats, _len);
assert(len == _len);
}
uint64_t get_value(const xstat_id id) {
auto off = _offsets[static_cast<int>(id)];
return _xstats[off].value;
}
private:
uint8_t _port_id;
int _len;
struct rte_eth_xstat *_xstats = nullptr;
struct rte_eth_xstat_name *_xstat_names = nullptr;
int _offsets[BOOST_PP_SEQ_SIZE(XSTATS_ID_LIST)];
static const sstring id_to_str(const xstat_id id) {
#define ENUM_TO_STR(r, data, elem) \
if (id == elem) \
return BOOST_PP_STRINGIZE(elem);
BOOST_PP_SEQ_FOR_EACH(ENUM_TO_STR, ~, XSTATS_ID_LIST)
return "";
}
int get_offset_by_name(const xstat_id id, const int len) {
for (int i = 0; i < len; i++) {
if (id_to_str(id) == _xstat_names[i].name)
return i;
}
return -1;
}
void update_xstat_names() {
auto len = rte_eth_xstats_get_names(_port_id, _xstat_names, _len);
assert(len == _len);
}
void update_offsets() {
#define FIND_OFFSET(r, data, elem) \
_offsets[static_cast<int>(elem)] = \
get_offset_by_name(elem, _len);
BOOST_PP_SEQ_FOR_EACH(FIND_OFFSET, ~, XSTATS_ID_LIST)
}
};
class dpdk_device : public device {
uint8_t _port_idx;
uint16_t _num_queues;
net::hw_features _hw_features;
uint8_t _queues_ready = 0;
unsigned _home_cpu;
bool _use_lro;
bool _enable_fc;
std::vector<uint8_t> _redir_table;
rss_key_type _rss_key;
port_stats _stats;
timer<> _stats_collector;
const std::string _stats_plugin_name;
const std::string _stats_plugin_inst;
seastar::metrics::metric_groups _metrics;
bool _is_i40e_device = false;
bool _is_vmxnet3_device = false;
dpdk_xstats _xstats;
public:
rte_eth_dev_info _dev_info = {};
promise<> _link_ready_promise;
private:
/**
* Port initialization consists of 3 main stages:
* 1) General port initialization which ends with a call to
* rte_eth_dev_configure() where we request the needed number of Rx and
* Tx queues.
* 2) Individual queues initialization. This is done in the constructor of
* dpdk_qp class. In particular the memory pools for queues are allocated
* in this stage.
* 3) The final stage of the initialization which starts with the call of
* rte_eth_dev_start() after which the port becomes fully functional. We
* will also wait for a link to get up in this stage.
*/
/**
* First stage of the port initialization.
*
* @return 0 in case of success and an appropriate error code in case of an
* error.
*/
int init_port_start();
/**
* The final stage of a port initialization.
* @note Must be called *after* all queues from stage (2) have been
* initialized.
*/
void init_port_fini();
/**
* Check the link status of out port in up to 9s, and print them finally.
*/
void check_port_link_status();
/**
* Configures the HW Flow Control
*/
void set_hw_flow_control();
public:
dpdk_device(uint8_t port_idx, uint16_t num_queues, bool use_lro,
bool enable_fc)
: _port_idx(port_idx)
, _num_queues(num_queues)
, _home_cpu(engine().cpu_id())
, _use_lro(use_lro)
, _enable_fc(enable_fc)
, _stats_plugin_name("network")
, _stats_plugin_inst(std::string("port") + std::to_string(_port_idx))
, _xstats(port_idx)
{
/* now initialise the port we will use */
int ret = init_port_start();
if (ret != 0) {
rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n", _port_idx);
}
/* need to defer initialize xstats since NIC specific xstat entries
show up only after port initization */
_xstats.start();
_stats_collector.set_callback([&] {
rte_eth_stats rte_stats = {};
int rc = rte_eth_stats_get(_port_idx, &rte_stats);
if (rc) {
printf("Failed to get port statistics: %s\n", strerror(rc));
}
_stats.rx.good.mcast =
_xstats.get_value(dpdk_xstats::xstat_id::rx_multicast_packets);
_stats.rx.good.pause_xon =
_xstats.get_value(dpdk_xstats::xstat_id::rx_xon_packets);
_stats.rx.good.pause_xoff =
_xstats.get_value(dpdk_xstats::xstat_id::rx_xoff_packets);
_stats.rx.bad.crc =
_xstats.get_value(dpdk_xstats::xstat_id::rx_crc_errors);
_stats.rx.bad.len =
_xstats.get_value(dpdk_xstats::xstat_id::rx_length_errors) +
_xstats.get_value(dpdk_xstats::xstat_id::rx_undersize_errors) +
_xstats.get_value(dpdk_xstats::xstat_id::rx_oversize_errors);
_stats.rx.bad.total = rte_stats.ierrors;
_stats.tx.good.pause_xon =
_xstats.get_value(dpdk_xstats::xstat_id::tx_xon_packets);
_stats.tx.good.pause_xoff =
_xstats.get_value(dpdk_xstats::xstat_id::tx_xoff_packets);
_stats.tx.bad.total = rte_stats.oerrors;
});
// Register port statistics pollers
namespace sm = seastar::metrics;
_metrics.add_group(_stats_plugin_name, {
// Rx Good
sm::make_derive("rx_multicast", _stats.rx.good.mcast,
sm::description("Counts a number of received multicast packets."), {sm::shard_label(_stats_plugin_inst)}),
// Rx Errors
sm::make_derive("rx_crc_errors", _stats.rx.bad.crc,
sm::description("Counts a number of received packets with a bad CRC value. "
"A non-zero value of this metric usually indicates a HW problem, e.g. a bad cable."), {sm::shard_label(_stats_plugin_inst)}),
sm::make_derive("rx_dropped", _stats.rx.bad.dropped,
sm::description("Counts a number of dropped received packets. "
"A non-zero value of this counter indicated the overflow of ingress HW buffers. "
"This usually happens because of a rate of a sender on the other side of the link is higher than we can process as a receiver."), {sm::shard_label(_stats_plugin_inst)}),
sm::make_derive("rx_bad_length_errors", _stats.rx.bad.len,
sm::description("Counts a number of received packets with a bad length value. "
"A non-zero value of this metric usually indicates a HW issue: e.g. bad cable."), {sm::shard_label(_stats_plugin_inst)}),
// Coupled counters:
// Good
sm::make_derive("rx_pause_xon", _stats.rx.good.pause_xon,
sm::description("Counts a number of received PAUSE XON frames (PAUSE frame with a quanta of zero). "
"When PAUSE XON frame is received our port may resume sending L2 frames. "
"PAUSE XON frames are sent to resume sending that was previously paused with a PAUSE XOFF frame. If ingress "
"buffer falls below the low watermark threshold before the timeout configured in the original PAUSE XOFF frame the receiver may decide to send PAUSE XON frame. "
"A non-zero value of this metric may mean that our sender is bursty and that the spikes overwhelm the receiver on the other side of the link."), {sm::shard_label(_stats_plugin_inst)}),
sm::make_derive("tx_pause_xon", _stats.tx.good.pause_xon,
sm::description("Counts a number of sent PAUSE XON frames (L2 flow control frames). "
"A non-zero value of this metric indicates that our ingress path doesn't keep up with the rate of a sender on the other side of the link. "
"Note that if a sender port respects PAUSE frames this will prevent it from sending from ALL its egress queues because L2 flow control is defined "
"on a per-link resolution."), {sm::shard_label(_stats_plugin_inst)}),
sm::make_derive("rx_pause_xoff", _stats.rx.good.pause_xoff,
sm::description("Counts a number of received PAUSE XOFF frames. "
"A non-zero value of this metric indicates that our egress overwhelms the receiver on the other side of the link and it has to send PAUSE frames to make us stop sending. "
"Note that if our port respects PAUSE frames a reception of a PAUSE XOFF frame will cause ALL egress queues of this port to stop sending."), {sm::shard_label(_stats_plugin_inst)}),
sm::make_derive("tx_pause_xoff", _stats.tx.good.pause_xoff,
sm::description("Counts a number of sent PAUSE XOFF frames. "
"A non-zero value of this metric indicates that our ingress path (SW and HW) doesn't keep up with the rate of a sender on the other side of the link and as a result "
"our ingress HW buffers overflow."), {sm::shard_label(_stats_plugin_inst)}),
// Errors
sm::make_derive("rx_errors", _stats.rx.bad.total,
sm::description("Counts the total number of ingress errors: CRC errors, bad length errors, etc."), {sm::shard_label(_stats_plugin_inst)}),
sm::make_derive("tx_errors", _stats.tx.bad.total,
sm::description("Counts a total number of egress errors. A non-zero value usually indicated a problem with a HW or a SW driver."), {sm::shard_label(_stats_plugin_inst)}),
});
}
~dpdk_device() {
_stats_collector.cancel();
}
ethernet_address hw_address() override {
struct ether_addr mac;
rte_eth_macaddr_get(_port_idx, &mac);
return mac.addr_bytes;
}
net::hw_features hw_features() override {
return _hw_features;
}
net::hw_features& hw_features_ref() { return _hw_features; }
const rte_eth_rxconf* def_rx_conf() const {
return &_dev_info.default_rxconf;
}
const rte_eth_txconf* def_tx_conf() const {
return &_dev_info.default_txconf;
}
/**
* Set the RSS table in the device and store it in the internal vector.
*/
void set_rss_table();
virtual uint16_t hw_queues_count() override { return _num_queues; }
virtual future<> link_ready() { return _link_ready_promise.get_future(); }
virtual std::unique_ptr<qp> init_local_queue(boost::program_options::variables_map opts, uint16_t qid) override;
virtual unsigned hash2qid(uint32_t hash) override {
assert(_redir_table.size());
return _redir_table[hash & (_redir_table.size() - 1)];
}
uint8_t port_idx() { return _port_idx; }
bool is_i40e_device() const {
return _is_i40e_device;
}
bool is_vmxnet3_device() const {
return _is_vmxnet3_device;
}
virtual const rss_key_type& rss_key() const override { return _rss_key; }
};
template <bool HugetlbfsMemBackend>
class dpdk_qp : public net::qp {
class tx_buf_factory;
class tx_buf {
friend class dpdk_qp;
public:
static tx_buf* me(rte_mbuf* mbuf) {
return reinterpret_cast<tx_buf*>(mbuf);
}
private:
/**
* Checks if the original packet of a given cluster should be linearized
* due to HW limitations.
*
* @param head head of a cluster to check
*
* @return TRUE if a packet should be linearized.
*/
static bool i40e_should_linearize(rte_mbuf *head) {
bool is_tso = head->ol_flags & PKT_TX_TCP_SEG;
// For a non-TSO case: number of fragments should not exceed 8
if (!is_tso){
return head->nb_segs > i40e_max_xmit_segment_frags;
}
//
// For a TSO case each MSS window should not include more than 8
// fragments including headers.
//
// Calculate the number of frags containing headers.
//
// Note: we support neither VLAN nor tunneling thus headers size
// accounting is super simple.
//
size_t headers_size = head->l2_len + head->l3_len + head->l4_len;
unsigned hdr_frags = 0;
size_t cur_payload_len = 0;
rte_mbuf *cur_seg = head;
while (cur_seg && cur_payload_len < headers_size) {
cur_payload_len += cur_seg->data_len;
cur_seg = cur_seg->next;
hdr_frags++;
}
//
// Header fragments will be used for each TSO segment, thus the
// maximum number of data segments will be 8 minus the number of
// header fragments.
//
// It's unclear from the spec how the first TSO segment is treated
// if the last fragment with headers contains some data bytes:
// whether this fragment will be accounted as a single fragment or
// as two separate fragments. We prefer to play it safe and assume
// that this fragment will be accounted as two separate fragments.
//
size_t max_win_size = i40e_max_xmit_segment_frags - hdr_frags;
if (head->nb_segs <= max_win_size) {
return false;
}
// Get the data (without headers) part of the first data fragment
size_t prev_frag_data = cur_payload_len - headers_size;
auto mss = head->tso_segsz;
while (cur_seg) {
unsigned frags_in_seg = 0;
size_t cur_seg_size = 0;
if (prev_frag_data) {
cur_seg_size = prev_frag_data;
frags_in_seg++;
prev_frag_data = 0;
}
while (cur_seg_size < mss && cur_seg) {
cur_seg_size += cur_seg->data_len;
cur_seg = cur_seg->next;
frags_in_seg++;
if (frags_in_seg > max_win_size) {
return true;
}
}
if (cur_seg_size > mss) {
prev_frag_data = cur_seg_size - mss;
}
}
return false;
}
/**
* Sets the offload info in the head buffer of an rte_mbufs cluster.
*
* @param p an original packet the cluster is built for
* @param qp QP handle
* @param head a head of an rte_mbufs cluster
*/
static void set_cluster_offload_info(const packet& p, const dpdk_qp& qp, rte_mbuf* head) {
// Handle TCP checksum offload
auto oi = p.offload_info();
if (oi.needs_ip_csum) {
head->ol_flags |= PKT_TX_IP_CKSUM;
// TODO: Take a VLAN header into an account here
head->l2_len = sizeof(struct ether_hdr);
head->l3_len = oi.ip_hdr_len;
}
if (qp.port().hw_features().tx_csum_l4_offload) {
if (oi.protocol == ip_protocol_num::tcp) {
head->ol_flags |= PKT_TX_TCP_CKSUM;
// TODO: Take a VLAN header into an account here
head->l2_len = sizeof(struct ether_hdr);
head->l3_len = oi.ip_hdr_len;
if (oi.tso_seg_size) {
assert(oi.needs_ip_csum);
head->ol_flags |= PKT_TX_TCP_SEG;
head->l4_len = oi.tcp_hdr_len;
head->tso_segsz = oi.tso_seg_size;
}
} else if (oi.protocol == ip_protocol_num::udp) {
head->ol_flags |= PKT_TX_UDP_CKSUM;
// TODO: Take a VLAN header into an account here
head->l2_len = sizeof(struct ether_hdr);
head->l3_len = oi.ip_hdr_len;
}
}
}
/**
* Creates a tx_buf cluster representing a given packet in a "zero-copy"
* way.
*
* @param p packet to translate
* @param qp dpdk_qp handle
*
* @return the HEAD tx_buf of the cluster or nullptr in case of a
* failure
*/
static tx_buf* from_packet_zc(packet&& p, dpdk_qp& qp) {
// Too fragmented - linearize
if (p.nr_frags() > max_frags) {
p.linearize();
++qp._stats.tx.linearized;
}
build_mbuf_cluster:
rte_mbuf *head = nullptr, *last_seg = nullptr;
unsigned nsegs = 0;
//
// Create a HEAD of the fragmented packet: check if frag0 has to be
// copied and if yes - send it in a copy way
//
if (!check_frag0(p)) {
if (!copy_one_frag(qp, p.frag(0), head, last_seg, nsegs)) {
return nullptr;
}
} else if (!translate_one_frag(qp, p.frag(0), head, last_seg, nsegs)) {
return nullptr;
}
unsigned total_nsegs = nsegs;
for (unsigned i = 1; i < p.nr_frags(); i++) {
rte_mbuf *h = nullptr, *new_last_seg = nullptr;
if (!translate_one_frag(qp, p.frag(i), h, new_last_seg, nsegs)) {
me(head)->recycle();
return nullptr;
}
total_nsegs += nsegs;
// Attach a new buffers' chain to the packet chain
last_seg->next = h;
last_seg = new_last_seg;
}
// Update the HEAD buffer with the packet info
head->pkt_len = p.len();
head->nb_segs = total_nsegs;
set_cluster_offload_info(p, qp, head);
//
// If a packet hasn't been linearized already and the resulting
// cluster requires the linearisation due to HW limitation:
//
// - Recycle the cluster.
// - Linearize the packet.
// - Build the cluster once again
//
if (head->nb_segs > max_frags ||
(p.nr_frags() > 1 && qp.port().is_i40e_device() && i40e_should_linearize(head)) ||
(p.nr_frags() > vmxnet3_max_xmit_segment_frags && qp.port().is_vmxnet3_device())) {
me(head)->recycle();
p.linearize();
++qp._stats.tx.linearized;
goto build_mbuf_cluster;
}
me(last_seg)->set_packet(std::move(p));
return me(head);
}
/**
* Copy the contents of the "packet" into the given cluster of
* rte_mbuf's.
*
* @note Size of the cluster has to be big enough to accommodate all the
* contents of the given packet.
*
* @param p packet to copy
* @param head head of the rte_mbuf's cluster
*/
static void copy_packet_to_cluster(const packet& p, rte_mbuf* head) {
rte_mbuf* cur_seg = head;
size_t cur_seg_offset = 0;
unsigned cur_frag_idx = 0;
size_t cur_frag_offset = 0;
while (true) {
size_t to_copy = std::min(p.frag(cur_frag_idx).size - cur_frag_offset,
inline_mbuf_data_size - cur_seg_offset);
memcpy(rte_pktmbuf_mtod_offset(cur_seg, void*, cur_seg_offset),
p.frag(cur_frag_idx).base + cur_frag_offset, to_copy);
cur_frag_offset += to_copy;
cur_seg_offset += to_copy;
if (cur_frag_offset >= p.frag(cur_frag_idx).size) {
++cur_frag_idx;
if (cur_frag_idx >= p.nr_frags()) {
//
// We are done - set the data size of the last segment
// of the cluster.
//
cur_seg->data_len = cur_seg_offset;
break;
}
cur_frag_offset = 0;
}
if (cur_seg_offset >= inline_mbuf_data_size) {
cur_seg->data_len = inline_mbuf_data_size;
cur_seg = cur_seg->next;
cur_seg_offset = 0;
// FIXME: assert in a fast-path - remove!!!
assert(cur_seg);
}
}
}
/**
* Creates a tx_buf cluster representing a given packet in a "copy" way.
*
* @param p packet to translate
* @param qp dpdk_qp handle
*
* @return the HEAD tx_buf of the cluster or nullptr in case of a
* failure
*/
static tx_buf* from_packet_copy(packet&& p, dpdk_qp& qp) {
// sanity
if (!p.len()) {
return nullptr;
}
/*
* Here we are going to use the fact that the inline data size is a
* power of two.
*
* We will first try to allocate the cluster and only if we are
* successful - we will go and copy the data.
*/
auto aligned_len = align_up((size_t)p.len(), inline_mbuf_data_size);
unsigned nsegs = aligned_len / inline_mbuf_data_size;
rte_mbuf *head = nullptr, *last_seg = nullptr;
tx_buf* buf = qp.get_tx_buf();
if (!buf) {
return nullptr;
}
head = buf->rte_mbuf_p();
last_seg = head;
for (unsigned i = 1; i < nsegs; i++) {
buf = qp.get_tx_buf();
if (!buf) {
me(head)->recycle();
return nullptr;
}
last_seg->next = buf->rte_mbuf_p();
last_seg = last_seg->next;
}
//
// If we've got here means that we have succeeded already!
// We only need to copy the data and set the head buffer with the
// relevant info.
//
head->pkt_len = p.len();
head->nb_segs = nsegs;
copy_packet_to_cluster(p, head);
set_cluster_offload_info(p, qp, head);
return me(head);
}
/**
* Zero-copy handling of a single net::fragment.
*
* @param do_one_buf Functor responsible for a single rte_mbuf
* handling
* @param qp dpdk_qp handle (in)
* @param frag Fragment to copy (in)
* @param head Head of the cluster (out)
* @param last_seg Last segment of the cluster (out)
* @param nsegs Number of segments in the cluster (out)
*
* @return TRUE in case of success
*/
template <class DoOneBufFunc>
static bool do_one_frag(DoOneBufFunc do_one_buf, dpdk_qp& qp,
fragment& frag, rte_mbuf*& head,
rte_mbuf*& last_seg, unsigned& nsegs) {
size_t len, left_to_set = frag.size;
char* base = frag.base;
rte_mbuf* m;
// TODO: assert() in a fast path! Remove me ASAP!
assert(frag.size);
// Create a HEAD of mbufs' cluster and set the first bytes into it
len = do_one_buf(qp, head, base, left_to_set);
if (!len) {
return false;
}
left_to_set -= len;
base += len;
nsegs = 1;
//
// Set the rest of the data into the new mbufs and chain them to
// the cluster.
//
rte_mbuf* prev_seg = head;
while (left_to_set) {
len = do_one_buf(qp, m, base, left_to_set);
if (!len) {
me(head)->recycle();
return false;
}
left_to_set -= len;
base += len;
nsegs++;
prev_seg->next = m;
prev_seg = m;
}
// Return the last mbuf in the cluster
last_seg = prev_seg;
return true;
}
/**
* Zero-copy handling of a single net::fragment.
*
* @param qp dpdk_qp handle (in)
* @param frag Fragment to copy (in)
* @param head Head of the cluster (out)
* @param last_seg Last segment of the cluster (out)
* @param nsegs Number of segments in the cluster (out)
*
* @return TRUE in case of success
*/
static bool translate_one_frag(dpdk_qp& qp, fragment& frag,
rte_mbuf*& head, rte_mbuf*& last_seg,
unsigned& nsegs) {
return do_one_frag(set_one_data_buf, qp, frag, head,
last_seg, nsegs);
}
/**
* Copies one net::fragment into the cluster of rte_mbuf's.
*
* @param qp dpdk_qp handle (in)
* @param frag Fragment to copy (in)
* @param head Head of the cluster (out)
* @param last_seg Last segment of the cluster (out)
* @param nsegs Number of segments in the cluster (out)
*
* We return the "last_seg" to avoid traversing the cluster in order to get
* it.
*
* @return TRUE in case of success
*/
static bool copy_one_frag(dpdk_qp& qp, fragment& frag,
rte_mbuf*& head, rte_mbuf*& last_seg,
unsigned& nsegs) {
return do_one_frag(copy_one_data_buf, qp, frag, head,
last_seg, nsegs);
}
/**
* Allocates a single rte_mbuf and sets it to point to a given data
* buffer.
*
* @param qp dpdk_qp handle (in)
* @param m New allocated rte_mbuf (out)
* @param va virtual address of a data buffer (in)
* @param buf_len length of the data to copy (in)
*
* @return The actual number of bytes that has been set in the mbuf
*/
static size_t set_one_data_buf(
dpdk_qp& qp, rte_mbuf*& m, char* va, size_t buf_len) {
static constexpr size_t max_frag_len = 15 * 1024; // 15K
using namespace memory;
translation tr = translate(va, buf_len);
//
// Currently we break a buffer on a 15K boundary because 82599
// devices have a 15.5K limitation on a maximum single fragment
// size.
//
phys_addr_t pa = tr.addr;
if (!tr.size) {
return copy_one_data_buf(qp, m, va, buf_len);
}
tx_buf* buf = qp.get_tx_buf();
if (!buf) {
return 0;
}
size_t len = std::min(tr.size, max_frag_len);
buf->set_zc_info(va, pa, len);
m = buf->rte_mbuf_p();
return len;
}
/**
* Allocates a single rte_mbuf and copies a given data into it.
*
* @param qp dpdk_qp handle (in)
* @param m New allocated rte_mbuf (out)
* @param data Data to copy from (in)
* @param buf_len length of the data to copy (in)
*
* @return The actual number of bytes that has been copied
*/
static size_t copy_one_data_buf(
dpdk_qp& qp, rte_mbuf*& m, char* data, size_t buf_len)
{
tx_buf* buf = qp.get_tx_buf();
if (!buf) {
return 0;
}