-
Notifications
You must be signed in to change notification settings - Fork 15
/
virtio_worker.c
2312 lines (2053 loc) · 65.7 KB
/
virtio_worker.c
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
/*
* BSD LICENSE
*
* Copyright(c) 2016-2017 Netronome.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Netronome nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "virtio_worker.h"
#define __MODULE__ "virtio_worker"
#include "log.h"
#include <rte_version.h>
#if RTE_VERSION_NUM(17, 5, 0, 0) <= RTE_VERSION
#include <rte_vhost.h>
#else
#include <rte_virtio_net.h>
#endif
#include "rte_errno.h"
#include "rte_mbuf.h"
#include "dpdk_eal.h"
#include "rte_ethdev.h"
#include "cpuinfo.h"
#include <stdbool.h>
#include <pthread.h>
#include <string.h>
#include <bsd/string.h>
#include <stdint.h>
#include <unistd.h>
#include <sched.h>
#include <sys/time.h>
#include <assert.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_tcp.h>
#include <rte_udp.h>
#include <rte_sctp.h>
#include <rte_arp.h>
#include <rte_jhash.h>
#include <rte_spinlock.h>
#include <rte_cycles.h>
#if RTE_VERSION_NUM(16, 7, 0, 0) > RTE_VERSION
#include <numaif.h>
#endif
#if RTE_VERSION_NUM(21, 00, 0, 0) <= RTE_VERSION
#ifdef RTE_NET_BOND
#define RTE_LIBRTE_PMD_BOND
#endif
#endif
#ifdef RTE_LIBRTE_PMD_BOND
#include <rte_eth_bond.h>
#endif
#include "virtio_forwarder_compat.h"
static worker_thread_t worker_threads[MAX_CPUS];
static uint64_t worker_core_bitmap;
static vio_vf_relay_t virtio_vf_relays[MAX_RELAYS];
static relay_prev_counters_t relay_prev_counters[MAX_RELAYS];
vio_vf_relay_t * get_relay_from_id(unsigned id) {
if (id >= MAX_RELAYS) {
log_error("Invalid relay ID passed");
return NULL;
}
return &virtio_vf_relays[id];
}
static bool have_worker_on_node(int node)
{
cpuinfo_t *c = get_cpuinfo();
for (int i=0; i<MAX_CPUS; ++i) {
if ((int)c->cpus[i].numanode != node)
continue;
if ((1ULL<<i) & worker_core_bitmap)
return true;
}
return false;
}
/*
* Get the least busy worker CPU based on the number of relays it is servicing.
* VF-to-virtio is assumed approx 20% more CPU intensive.
* Does not account for packet rates.
*/
static int naive_get_idlest_worker(int node)
{
int min, min_index;
int cpu_workers[MAX_CPUS] = {0};
bool worker_on_node = false;
cpuinfo_t *c = get_cpuinfo();
for (unsigned w=0; w<MAX_RELAYS; ++w) {
vio_vf_relay_t *relay = &virtio_vf_relays[w];
if ((relay->dpdk.state == DPDK_READY ||
relay->dpdk.state == DPDK_ADDED) &&
relay->dpdk.vf2vio_cpu >= 0)
cpu_workers[relay->dpdk.vf2vio_cpu] += 12;
if (relay->vio.state == VIRTIO_READY &&
relay->vio.vio2vf_cpu >= 0)
cpu_workers[relay->vio.vio2vf_cpu] += 10;
}
worker_on_node = have_worker_on_node(node);
min = MAX_RELAYS * 100;
min_index = -1;
for (unsigned cpu=0; cpu<MAX_CPUS; ++cpu) {
if (node != SOCKET_ID_ANY && worker_on_node) {
if ((int)c->cpus[cpu].numanode != node)
continue;
}
if (((1ULL<<cpu) & worker_core_bitmap) == 0)
continue;
worker_thread_t *worker = &worker_threads[cpu];
if (worker->initialized) {
if (cpu_workers[cpu] < min || min_index == -1) {
min = cpu_workers[cpu];
min_index = cpu;
}
}
}
return min_index;
}
static void find_vf2virtio_cpu(vio_vf_relay_t *relay)
{
int idlest_cpu;
int conf_cpu = g_vio_worker_conf.relay_cpus[relay->id].vf2vio_cpu;
if (conf_cpu != -1) {
relay->dpdk.vf2vio_cpu = conf_cpu;
log_debug("Using CPU %u for relay %u vf2virtio",
relay->dpdk.vf2vio_cpu, relay->id);
return;
}
if (relay->dpdk.vf2vio_cpu != -1) {
worker_threads[relay->dpdk.vf2vio_cpu].need_update = true;
relay->dpdk.vf2vio_cpu = -1;
}
idlest_cpu = naive_get_idlest_worker(relay->vio.mempool_socket_id);
assert(idlest_cpu >= 0);
relay->dpdk.vf2vio_cpu = idlest_cpu;
log_debug("Found CPU %u for relay %u vf2virtio",
relay->dpdk.vf2vio_cpu, relay->id);
}
/**
* Log a warning message if the UIO driver appears to be set up incorrectly on
* @a pci_dbdf).
*/
static void check_uio_driver_setup(const char *pci_dbdf)
{
char buf[64];
char linkname[128];
int len;
/* Attempt to find device driver associated with PCI device. */
snprintf(buf, 64, "/sys/bus/pci/devices/%s/driver", pci_dbdf);
if ((len=readlink(buf, linkname, 128)) > 0) {
if (len==128)
len = 127;
linkname[len] = 0;
char *s = strrchr(linkname, '/');
if (s && (len - (s - linkname) > 1)) {
s += 1;
if (strstr(s, "uio") == 0 && strstr(s, "vfio") == 0)
log_warning("PCI '%s' should be assigned to a UIO driver for use with virtio-forwarder, but is assigned to '%s'",
pci_dbdf, s);
else
log_warning("PCI '%s' seems to be assigned to a UIO driver ('%s')",
pci_dbdf, s);
}
} else
log_warning("PCI '%s' does not seem to be assigned to any UIO driver",
pci_dbdf);
}
/**
* Return true if two PCI address strings represent the same address.
*
* Currently this just uses strcmp(). This should work in cases of interest to
* vRouter (and wherever it was working before, as this is what the original
* code did).
*
* In the future, we should probably compare the address components
* numerically.
*/
static bool pci_dbdf_equal(const char *dbdf1, const char *dbdf2)
{
return strcmp(dbdf1, dbdf2) == 0;
}
/**
* Return true if a conditional VF add should be treated a no-op, rather than
* an error.
*/
static bool
conditional_add_ok(vio_vf_relay_t *relay, const char *pci_dbdf,
unsigned virtio_id)
{
return (relay->dpdk.state == DPDK_READY ||
relay->dpdk.state == DPDK_ADDED)
&& relay->id == virtio_id
&& pci_dbdf_equal(relay->dpdk.pci_dbdf, pci_dbdf);
}
static void get_tx_conf(const struct rte_eth_dev_info *dev_info,
struct rte_eth_txconf *tx_conf)
{
*tx_conf = dev_info->default_txconf;
#if RTE_VERSION_NUM(22, 03, 0, 0) <= RTE_VERSION
uint64_t desired_offloads;
desired_offloads = RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
RTE_ETH_TX_OFFLOAD_TCP_CKSUM;
if (g_vio_worker_conf.enable_tso)
desired_offloads |= (RTE_ETH_TX_OFFLOAD_TCP_TSO |
RTE_ETH_TX_OFFLOAD_UDP_TSO);
tx_conf->offloads = dev_info->tx_offload_capa & desired_offloads;
#elif RTE_VERSION_NUM(17, 11, 0, 0) <= RTE_VERSION
uint64_t desired_offloads;
desired_offloads = DEV_TX_OFFLOAD_IPV4_CKSUM |
DEV_TX_OFFLOAD_UDP_CKSUM |
DEV_TX_OFFLOAD_TCP_CKSUM;
if (g_vio_worker_conf.enable_tso)
desired_offloads |= (DEV_TX_OFFLOAD_TCP_TSO |
DEV_TX_OFFLOAD_UDP_TSO);
tx_conf->offloads = dev_info->tx_offload_capa & desired_offloads;
#endif
#if RTE_VERSION_NUM(18, 8, 0, 0) > RTE_VERSION
tx_conf->txq_flags &= ~ETH_TXQ_FLAGS_NOOFFLOADS;
#endif
}
static void get_rx_conf(const struct rte_eth_dev_info *dev_info,
struct rte_eth_rxconf *rx_conf)
{
*rx_conf = dev_info->default_rxconf;
#if RTE_VERSION_NUM(22, 03, 0, 0) <= RTE_VERSION
uint64_t desired_offloads;
desired_offloads = RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
RTE_ETH_RX_OFFLOAD_TCP_CKSUM;
rx_conf->offloads = dev_info->rx_offload_capa & desired_offloads;
#elif RTE_VERSION_NUM(17, 11, 0, 0) <= RTE_VERSION
uint64_t desired_offloads;
desired_offloads = DEV_RX_OFFLOAD_IPV4_CKSUM |
DEV_RX_OFFLOAD_UDP_CKSUM |
DEV_RX_OFFLOAD_TCP_CKSUM;
rx_conf->offloads = dev_info->rx_offload_capa & desired_offloads;
#endif
}
static int stop_close_remove_dev(dpdk_port_t port_id,
struct rte_eth_dev_info dev_info,
const char err_api[])
{
int err;
#if RTE_VERSION_NUM(20, 11, 0, 0) <= RTE_VERSION
err = rte_eth_dev_stop(port_id);
if (err != 0) {
log_error("%s: can't stop port %u", err_api, port_id);
return err;
}
err = rte_eth_dev_close(port_id);
if (err != 0) {
log_error("%s: can't close port %u", err_api, port_id);
return err;
}
#else
rte_eth_dev_stop(port_id);
rte_eth_dev_close(port_id);
#endif
#if RTE_VERSION_NUM(18, 11, 0, 0) <= RTE_VERSION
err = rte_dev_remove(dev_info.device);
#endif
return err;
}
static int cleanup_eth_dev(dpdk_port_t port_id)
{
int err;
static const char err_api[] = "rte_dev_remove";
struct rte_eth_dev_info dev_info;
#if RTE_VERSION_NUM(19, 11, 0, 0) <= RTE_VERSION
err = rte_eth_dev_info_get(port_id, &dev_info);
if (err != 0) {
log_error("%s: can't get port %u the dev_info", err_api,
port_id);
return err;
}
err = stop_close_remove_dev(port_id, dev_info, err_api);
#elif RTE_VERSION_NUM(18, 11, 0, 0) <= RTE_VERSION
rte_eth_dev_info_get(port_id, &dev_info);
err = stop_close_remove_dev(port_id, dev_info, err_api);
#else
const char rte_detach_api[] = "rte_eth_dev_detach";
char detach_dbdf[RTE_ETH_NAME_MAX_LEN];
err = rte_eth_dev_detach(port_id, detach_dbdf);
#endif
if (err != 0) {
log_warning("%s(%hhu) failed with error %i", err_api,
port_id, err);
}
return err;
}
static int start_eth_dev(dpdk_port_t port_id)
{
int err;
err = rte_eth_dev_start(port_id);
if (err != 0)
log_error("rte_eth_dev_start(%hhu) failed with error %i",
port_id, err);
return err;
}
static struct rte_mempool *alloc_mempool(unsigned virtio_id, int socket_id,
unsigned n)
{
char buf[32];
/* Alternate between these names to allow check before migration. */
snprintf(buf, 32, "mempool_%u", virtio_id);
if (rte_mempool_lookup(buf))
snprintf(buf, 32, "mempoolx_%u", virtio_id);
return rte_pktmbuf_pool_create(buf, n, 32-1, 0,
(g_vio_worker_conf.use_jumbo ||
g_vio_worker_conf.enable_tso) ?
JUMBO_MBUF_SIZE : DEFAULT_MBUF_SIZE,
socket_id);
}
static int __attribute__((unused))
migrate_mempool(vio_vf_relay_t *relay, int newnode, unsigned num_pktmbufs)
{
#if RTE_VERSION_NUM(16, 7, 0, 0) <= RTE_VERSION
uint8_t port_id = relay->dpdk.dpdk_port;
struct rte_mempool *new_pool;
/* Try to allocate a new mempool. */
new_pool = alloc_mempool(relay->id, newnode, num_pktmbufs);
if (!new_pool) {
log_error("Could not alloc mempool for virtio %u on socket %d! Previous configuration will be retained",
relay->id, newnode);
return -1;
}
/**
* On success, stop the VF associated with this relay and reconfigure.
* See http://dpdk.org/doc/api/rte__ethdev_8h.html
*/
assert(relay->dpdk.state != DPDK_READY);
assert(relay->dpdk.rx_pkts_avail == 0);
if (relay->dpdk.state == DPDK_ADDED) {
log_debug("Previously setup VF requires update. Stopping VF...");
rte_eth_dev_stop(port_id);
}
/* Free old mempool. rte_mempool docs state that no other cores should
* use the mempool while it is being freed. */
while (rte_spinlock_trylock(&relay->vio.sl) == 0);
while (rte_spinlock_trylock(&relay->dpdk.sl) == 0);
log_debug("Migrating mempool for relay %u...", relay->id);
if (relay->vio.mempool)
rte_mempool_free(relay->vio.mempool);
relay->vio.mempool = new_pool;
relay->vio.mempool_socket_id = newnode;
__sync_synchronize();
rte_spinlock_unlock(&relay->dpdk.sl);
rte_spinlock_unlock(&relay->vio.sl);
/* Reconfigure VF if it was previously configured. */
if (relay->dpdk.state == DPDK_ADDED) {
int err;
struct rte_eth_rxconf rx_conf;
struct rte_eth_txconf tx_conf;
struct rte_eth_dev_info dev_info;
log_info("Updating VF %s with the new NUMA configuration...",
relay->dpdk.pci_dbdf);
/* Reconfigure queues. */
#if RTE_VERSION_NUM(19, 11, 0, 0) <= RTE_VERSION
static const char err_api[] = "migrate_mempool";
err = rte_eth_dev_info_get(port_id, &dev_info);
if (err != 0) {
log_error("%s: can't get port %u the dev_info", err_api,
port_id);
return err;
}
#else
rte_eth_dev_info_get(relay->dpdk.dpdk_port, &dev_info);
#endif
get_tx_conf(&dev_info, &tx_conf);
err = rte_eth_tx_queue_setup(port_id, 0, 1024,
relay->vio.mempool_socket_id, &tx_conf);
if (err != 0) {
log_error("rte_eth_tx_queue_setup(%hhu, 0, 1024) failed with error %i",
port_id, err);
rte_eth_dev_close(port_id);
return -1;
}
get_rx_conf(&dev_info, &rx_conf);
err = rte_eth_rx_queue_setup(port_id, 0, 1024,
relay->vio.mempool_socket_id, &rx_conf,
relay->vio.mempool);
if (err != 0) {
log_error("rte_eth_rx_queue_setup(%hhu, 0, 1024) failed with error %i",
port_id, err);
rte_eth_dev_close(port_id);
return -1;
}
log_info("Successfully re-configured VF %s for relay %u",
relay->dpdk.pci_dbdf, relay->id);
}
return 0;
#else
log_warning("migrate_mempool(relay %u, dest node %d, pktmbufs %u) not supported for this version of DPDK",
relay->id, newnode, num_pktmbufs);
return 1;
#endif
}
static int dev_queue_configure(const char *name, dpdk_port_t port_id,
unsigned virtio_id, vio_vf_relay_t *relay, bool is_bond)
{
int err;
struct rte_eth_conf eth_conf = {0};
struct rte_eth_rxconf rx_conf;
struct rte_eth_txconf tx_conf;
struct rte_eth_dev_info dev_info;
log_info("Adding DPDK port %hhu ('%s') to virtio ('%u')",
port_id, name, virtio_id);
/* Configure per-port offloads. */
#if RTE_VERSION_NUM(19, 11, 0, 0) <= RTE_VERSION
static const char err_api[] = "migrate_mempool";
err = rte_eth_dev_info_get(port_id, &dev_info);
if (err != 0) {
log_error("%s: can't get port %u the dev_info", err_api,
port_id);
return err;
}
#elif RTE_VERSION_NUM(18, 8, 0, 0) <= RTE_VERSION
rte_eth_dev_info_get(port_id, &dev_info);
#endif
#if RTE_VERSION_NUM(18, 8, 0, 0) <= RTE_VERSION
#if RTE_VERSION_NUM(21, 11, 0, 0) > RTE_VERSION
if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_JUMBO_FRAME)
eth_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
#endif
#if RTE_VERSION_NUM(22, 03, 0, 0) <= RTE_VERSION
if (dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_CHECKSUM)
eth_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_CHECKSUM;
if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM)
eth_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_IPV4_CKSUM;
if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_UDP_CKSUM)
eth_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_UDP_CKSUM;
if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_TCP_CKSUM)
eth_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_TCP_CKSUM;
if (g_vio_worker_conf.enable_tso) {
if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_TCP_TSO)
eth_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_TCP_TSO;
if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_UDP_TSO)
eth_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_UDP_TSO;
}
#else
if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_CHECKSUM)
eth_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
eth_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM)
eth_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM)
eth_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
if (g_vio_worker_conf.enable_tso) {
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO)
eth_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_TSO;
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_TSO)
eth_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_TSO;
}
#endif
#else
eth_conf.rxmode.jumbo_frame = 1;
eth_conf.rxmode.hw_ip_checksum = 1;
#endif
#if RTE_VERSION_NUM(21, 11, 0, 0) > RTE_VERSION
eth_conf.rxmode.max_rx_pkt_len = relay->use_jumbo ? JUMBO_MBUF_SIZE :
DEFAULT_MBUF_SIZE;
#else
eth_conf.rxmode.mtu = relay->use_jumbo ? JUMBO_MBUF_SIZE :
DEFAULT_MBUF_SIZE;
#endif
err = rte_eth_dev_configure(port_id, 1, 1, ð_conf);
if (err != 0) {
log_error("rte_eth_dev_configure(%hhu, 1, 1) failed with error %i",
port_id, err);
return 4;
}
if (!is_bond) {
err = rte_eth_dev_set_mtu(port_id, relay->use_jumbo ?
JUMBO_IP_MTU : DEFAULT_IP_MTU);
if (err != 0) {
log_error("rte_eth_dev_set_mtu failed with error %i",
err);
return 4;
}
}
if (!relay->vio.mempool) {
struct rte_mempool *mpool;
mpool = alloc_mempool(relay->id, relay->vio.mempool_socket_id,
NUM_PKTMBUF_POOL-1);
if (!mpool) {
log_error("Could not alloc mempool for virtio %u on socket %u! rte_errno = %d (%s).",
relay->id, relay->vio.mempool_socket_id,
rte_errno, rte_strerror(rte_errno));
return -1;
} else {
relay->vio.mempool = mpool;
}
}
/*
* Per <http://dpdk.org/doc/api/rte__ethdev_8h.html>, we must setup the
* TX queue before setting up the RX queue.
*/
#if RTE_VERSION_NUM(19, 11, 0, 0) <= RTE_VERSION
err = rte_eth_dev_info_get(port_id, &dev_info);
if (err != 0) {
log_error("%s: can't get port %u the dev_info", err_api,
port_id);
return err;
}
#else
rte_eth_dev_info_get(port_id, &dev_info);
#endif
get_tx_conf(&dev_info, &tx_conf);
err = rte_eth_tx_queue_setup(port_id, 0, 1024,
relay->vio.mempool_socket_id, &tx_conf);
if (err != 0) {
log_error("rte_eth_tx_queue_setup(%hhu, 0, 1024) failed with error %i",
port_id, err);
return 6;
}
get_rx_conf(&dev_info, &rx_conf);
err = rte_eth_rx_queue_setup(port_id, 0, 1024,
relay->vio.mempool_socket_id, &rx_conf,
relay->vio.mempool);
if (err != 0) {
log_error("rte_eth_rx_queue_setup(%hhu, 0, 1024) failed with error %i",
port_id, err);
return 5;
}
return 0;
}
static int init_vf(const char *pci_dbdf, dpdk_port_t *port_id,
unsigned virtio_id, vio_vf_relay_t *relay)
{
int err;
#if RTE_VERSION_NUM(18, 8, 0, 0) <= RTE_VERSION
const char rte_attach_api[] = "rte_dev_probe";
err = rte_dev_probe(pci_dbdf);
#else
const char rte_attach_api[] = "rte_eth_dev_attach";
err = rte_eth_dev_attach(pci_dbdf, port_id);
#endif
if (err != 0) {
/* err is always -1 on error, so no useful additional info.
* Print it anyway for consistency with other error messages. */
log_error("%s('%s') failed with error %i", rte_attach_api,
pci_dbdf, err);
check_uio_driver_setup(pci_dbdf);
return 2;
}
#if RTE_VERSION_NUM(18, 8, 0, 0) <= RTE_VERSION
struct rte_dev_iterator it;
RTE_ETH_FOREACH_MATCHING_DEV(*port_id, pci_dbdf, &it) {
#endif
err = dev_queue_configure(pci_dbdf, *port_id, virtio_id, relay, false);
if (err) {
#if RTE_VERSION_NUM(18, 8, 0, 0) <= RTE_VERSION
rte_eth_iterator_cleanup(&it);
#endif
cleanup_eth_dev(*port_id);
return err;
}
rte_eth_promiscuous_enable(*port_id);
#if RTE_VERSION_NUM(18, 8, 0, 0) <= RTE_VERSION
/* Enforce a single match - there should only be one. */
rte_eth_iterator_cleanup(&it);
break;
}
#endif
return 0;
}
static int configure_dev_with_virtio(const char *pci_dbdf, dpdk_port_t port_id,
unsigned virtio_id, vio_vf_relay_t *relay,
bool is_bond, unsigned num_slaves)
{
int dpdk_state;
if (relay->vio.state == VIRTIO_READY) {
log_debug("Starting device for relay %u", virtio_id);
if (start_eth_dev(port_id) != 0)
return 1;
dpdk_state = DPDK_READY;
} else {
log_debug("Not starting device for relay %u since virtio not connected",
virtio_id);
rte_eth_dev_stop(port_id); /* stop the VF explicitly in case it was still running from a previous process. */
dpdk_state = DPDK_ADDED;
}
/* Success */
log_info("Added dpdk port %hhu to relay", port_id);
relay->vio.lm_pending = false;
relay->dpdk.dpdk_port = port_id;
strlcpy(relay->dpdk.pci_dbdf, pci_dbdf, 20);
find_vf2virtio_cpu(relay);
relay->dpdk.state = dpdk_state;
relay->dpdk.is_bond = is_bond;
relay->dpdk.num_slaves = num_slaves;
__sync_synchronize();
worker_threads[relay->dpdk.vf2vio_cpu].need_update = true;
return 0;
}
int
virtio_forwarder_add_vf2(const char *pci_dbdf, unsigned virtio_id, bool conditional)
{
#ifdef VIRTIO_ECHO
log_warning("No effect adding dpdk interface '%s' to relay %u in VIRTIO_ECHO mode!",
pci_dbdf, virtio_id);
return 0;
#else
dpdk_port_t port_id;
int err;
log_debug("Got virtio_forwarder_add_vf2('%s', %u, conditional=%s)",
pci_dbdf, virtio_id, conditional ? "true" : "false");
if (virtio_id >= MAX_RELAYS) {
log_error("Tried to add PCI '%s' to invalid virtio ID %u! (valid range is 0..%u)",
pci_dbdf, virtio_id, MAX_RELAYS - 1);
return 1;
}
vio_vf_relay_t *relay= &virtio_vf_relays[virtio_id];
if (relay->dpdk.state != DPDK_UNINIT) {
/* VF already active */
if (conditional && conditional_add_ok(relay, pci_dbdf, virtio_id)) {
log_info("No action required; VF was already properly configured");
return 0;
} else {
log_error("Tried to add DPDK port to already initialized entity!");
return 8;
}
}
/* New VF */
err = init_vf(pci_dbdf, &port_id, virtio_id, relay);
if (err)
return err;
/* Successfully added VF. */
err = configure_dev_with_virtio(pci_dbdf, port_id, virtio_id, relay,
false, 0);
if (err)
cleanup_eth_dev(port_id);
return err;
#endif /* VIRTIO_ECHO */
}
int virtio_forwarder_add_vf(const char *pci_dbdf, unsigned virtio_id)
{
/* Original code used unconditional adds/removes. */
return virtio_forwarder_add_vf2(pci_dbdf, virtio_id, false);
}
static void
format_slave_dbdfs(char slave_dbdfs[MAX_NUM_BOND_SLAVES][RTE_ETH_NAME_MAX_LEN],
unsigned num_slaves, char *p)
{
size_t idx = 0;
for (unsigned i=0; i<num_slaves; i++) {
strcpy(&p[idx], slave_dbdfs[i]);
idx += strlen(slave_dbdfs[i]);
strcpy(&p[idx], " ");
idx ++;
}
}
int virtio_forwarder_bond_add(char slave_dbdfs[MAX_NUM_BOND_SLAVES][RTE_ETH_NAME_MAX_LEN],
unsigned num_slaves, const char *name, uint8_t mode,
unsigned virtio_id)
{
char p[RTE_ETH_NAME_MAX_LEN * MAX_NUM_BOND_SLAVES];
format_slave_dbdfs(slave_dbdfs, num_slaves, p);
log_debug("Got virtio_forwarder_bond_add(<%s>, %u, %s, %u, %u)", p,
num_slaves, name, mode, virtio_id);
#ifdef RTE_LIBRTE_PMD_BOND
dpdk_port_t port_id, slave_port_ids[MAX_NUM_BOND_SLAVES];
int err, rc, socket_id;
if (virtio_id >= MAX_RELAYS) {
log_error("Tried to add bond '%s' to invalid virtio ID %u! (valid range is 0..%u)",
name, virtio_id, MAX_RELAYS - 1);
return 1;
}
vio_vf_relay_t *relay= &virtio_vf_relays[virtio_id];
if (relay->dpdk.state != DPDK_UNINIT) {
/* VF/bond already active on virtio instance. */
log_error("Tried to add DPDK port to already initialized entity!");
return 8;
}
/* XXX: DPDK defines SOCKET_ID_ANY as -1, but the bonding API accepts
* an unsigned... Until they fix it use 0 for unspecified sockets. */
socket_id = relay->vio.mempool_socket_id == SOCKET_ID_ANY ?
0 : relay->vio.mempool_socket_id;
err = rte_eth_bond_create(name, mode, socket_id);
if (err < 0) {
log_error("rte_eth_bond_create(%s, %u, %d) failed with error %d",
name, mode, socket_id, err);
return 2;
} else {
port_id = err;
}
#if RTE_VERSION_NUM(16, 7, 0, 0) <= RTE_VERSION
/* Error if a slave is already attached to DPDK. */
for (unsigned i=0; i<num_slaves; ++i) {
dpdk_port_t tmp;
if (rte_eth_dev_get_port_by_name(slave_dbdfs[i], &tmp) == 0) {
log_warning("The specified bond slave ('%s') is already in use with port id %u.",
slave_dbdfs[i], tmp);
rc = 3;
goto error_bond_deconfigure;
}
}
#endif
/* XXX: Bonds require more memory than ordinary VFs. At this point, we
* cannot rely on any DPDK info in the relay struct, and must therefore
* assume that the old memory [if allocated] is insufficient. */
if (migrate_mempool(relay, socket_id, num_slaves*NUM_PKTMBUF_POOL-1))
log_warning("Bond memory allocation failed. Active-active implementations may fail due to insufficient resources");
/* Configure bond. */
err = dev_queue_configure(name, port_id, virtio_id, relay, true);
if (err) {
log_error("Bond configuration failed. Tearing down...");
rc = 4;
goto error_bond_deconfigure;
}
/* Add slaves to bond. XXX: The queue configure which takes place in
* init_vf may be redundant, as it is done in any case when the bond
* is started - check whether dev attach is adequate here. */
for (unsigned i=0; i<num_slaves; ++i) {
/* Setup slave interface. */
err = init_vf(slave_dbdfs[i], &slave_port_ids[i], virtio_id, relay);
if (err) {
rc = 5;
goto error_slaves_deconfigure;
}
/* Add slave to bond. */
rte_eth_bond_slave_add(port_id, slave_port_ids[i]);
}
/* Successfully configured bond. */
err = configure_dev_with_virtio(name, port_id, virtio_id, relay, true,
num_slaves);
if (err) {
rc = 6;
goto error_slaves_deconfigure;
}
return 0;
error_slaves_deconfigure:
/* Deconfigure the other slaves that may have been initialized. */
for (unsigned i=0; i<num_slaves; ++i)
cleanup_eth_dev(slave_port_ids[i]);
error_bond_deconfigure:
err = rte_eth_bond_free(name);
if (err)
log_warning("During error recovery, rte_eth_bond_free(%s) failed with error %i",
name, err);
return rc;
#else
log_warning("Bonding not supported for this version of DPDK");
return -1;
#endif
}
static int stop_vm2vf_thread(vio_vf_relay_t *relay)
{
if (relay->dpdk.vf2vio_cpu >= 0) {
unsigned retries = 0;
relay->dpdk.state = DPDK_REMOVING1;
while (relay->dpdk.state != DPDK_UNINIT && retries++ < 20)
usleep(50000);
if (relay->dpdk.state != DPDK_UNINIT) {
log_error("Timeout waiting for DPDK port %hhu ('%s') to be released from relay thread!",
relay->dpdk.dpdk_port, relay->dpdk.pci_dbdf);
return 5;
}
}
return 0;
}
static int detach_slaves(vio_vf_relay_t *relay __attribute__((unused)))
{
#ifdef RTE_LIBRTE_PMD_BOND
dpdk_port_t port_id = relay->dpdk.dpdk_port;
dpdk_port_t slaves[MAX_NUM_BOND_SLAVES] = {0};
dpdk_port_t n_slaves;
int err;
if (!relay->dpdk.is_bond) {
log_warning("detach_slaves called on non-bond packet relay!");
return 0;
}
err = rte_eth_bond_slaves_get(port_id, slaves, MAX_NUM_BOND_SLAVES) ;
if (err < 0) {
log_error("Error retrieving slave information for bond %s",
relay->dpdk.pci_dbdf);
return 1;
} else {
n_slaves = err;
}
for (unsigned i=0; i<n_slaves; ++i) {
if (rte_eth_bond_slave_remove(port_id, slaves[i]))
log_warning("Error removing slave %u from device %s",
i, relay->dpdk.pci_dbdf);
}
for (unsigned i=0; i<n_slaves; ++i)
cleanup_eth_dev(slaves[i]);
#endif
return 0;
}
static int detach_device(vio_vf_relay_t *relay)
{
dpdk_port_t port_id = relay->dpdk.dpdk_port;
char *pci_dbdf = relay->dpdk.pci_dbdf;
int err, tmpidx;
err = stop_vm2vf_thread(relay);
if (err)
return err;
/* Update workers. */
tmpidx = relay->dpdk.vf2vio_cpu;
relay->dpdk.vf2vio_cpu = -1;
__sync_synchronize();
worker_threads[tmpidx].need_update = true;
/* Detach VF. */
log_debug("Stopping PCI '%s' device (port %hhu)", pci_dbdf, port_id);
if (relay->dpdk.is_bond) {
detach_slaves(relay);
rte_eth_dev_stop(port_id);
rte_eth_dev_close(port_id);
#ifdef RTE_LIBRTE_PMD_BOND
err = rte_eth_bond_free(relay->dpdk.pci_dbdf);
#endif
} else {
err = cleanup_eth_dev(port_id);
}
if (err == 0) {
log_debug("Removed PCI '%s' device as port %hhu",
pci_dbdf, port_id);
} else {
return 2;
}
log_info("removing DPDK port %hhu ('%s') from virtio %u", port_id,
pci_dbdf, relay->id);
relay->dpdk.dpdk_port = -1;
relay->dpdk.pci_dbdf[0] = 0;
relay->dpdk.is_bond = false;
relay->dpdk.num_slaves = 0;
#if RTE_VERSION_NUM(16, 7, 0, 0) <= RTE_VERSION
if (relay->vio.state == VIRTIO_UNINIT) {
rte_mempool_free(relay->vio.mempool);
relay->vio.mempool = NULL;
}
#endif
log_info("Removed dpdk port %hhu from virtio", port_id);
return err;
}
int
virtio_forwarder_remove_vf2(const char *pci_dbdf, unsigned virtio_id,
bool conditional)
{
#ifndef VIRTIO_ECHO
int err = 0;
log_debug("Got virtio_forwarder_remove_vf2('%s', %u, conditional=%s)",
pci_dbdf, virtio_id, conditional ? "true" : "false");
if (virtio_id >= MAX_RELAYS) {
log_error("Tried to remove PCI '%s' from invalid virtio ID %u!",
pci_dbdf, virtio_id);
return 1;
}
vio_vf_relay_t *relay = &virtio_vf_relays[virtio_id];
if (relay->dpdk.state == DPDK_READY || relay->dpdk.state == DPDK_ADDED) {
if (pci_dbdf_equal(pci_dbdf, relay->dpdk.pci_dbdf)) {
err = detach_device(relay);
if (err)
return err;
} else {
log_error("Tried to remove DPDK with non-matching PCI addr!");
return 3;
}
} else if (conditional) {
log_info("No action required; VF was already removed");
return 0;
} else {
log_error("Tried to remove DPDK port from un-initialized entity!");
return 4;
}
#else
log_warning("No effect adding dpdk interface '%s' to relay %u in VIRTIO_ECHO mode!",
pci_dbdf, virtio_id);
#endif
return err;
}
int virtio_forwarder_remove_vf(const char *pci_dbdf, unsigned virtio_id)
{