-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
1799 lines (1510 loc) · 45.5 KB
/
main.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
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2017 Intel Corporation
*/
#include <arpa/inet.h>
#include <getopt.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/virtio_net.h>
#include <linux/virtio_ring.h>
#include <signal.h>
#include <stdint.h>
#include <sys/eventfd.h>
#include <sys/param.h>
#include <unistd.h>
#include <rte_cycles.h>
#include <rte_ethdev.h>
#include <rte_log.h>
#include <rte_string_fns.h>
#include <rte_malloc.h>
#include <rte_net.h>
#include <rte_vhost.h>
#include <rte_ip.h>
#include <rte_tcp.h>
#include <rte_pause.h>
#include "ioat.h"
#include "main.h"
#ifndef MAX_QUEUES
#define MAX_QUEUES 128
#endif
#define NUM_MBUFS_DEFAULT 0x24000
/* the maximum number of external ports supported */
#define MAX_SUP_PORTS 1
#define MBUF_CACHE_SIZE 128
#define MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
#define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
#define BURST_RX_WAIT_US 15 /* Defines how long we wait between retries on RX */
#define BURST_RX_RETRIES 4 /* Number of retries on RX. */
#define JUMBO_FRAME_MAX_SIZE 0x2600
#define MAX_MTU (JUMBO_FRAME_MAX_SIZE - (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN))
/* State of virtio device. */
#define DEVICE_MAC_LEARNING 0
#define DEVICE_RX 1
#define DEVICE_SAFE_REMOVE 2
/* Configurable number of RX/TX ring descriptors */
#define RTE_TEST_RX_DESC_DEFAULT 1024
#define RTE_TEST_TX_DESC_DEFAULT 512
#define INVALID_PORT_ID 0xFF
/* number of mbufs in all pools - if specified on command-line. */
static int total_num_mbufs = NUM_MBUFS_DEFAULT;
/* mask of enabled ports */
static uint32_t enabled_port_mask = 0;
/* Promiscuous mode */
static uint32_t promiscuous;
/* number of devices/queues to support*/
static uint32_t num_queues = 0;
static uint32_t num_devices;
static struct rte_mempool *mbuf_pool;
static int mergeable;
/* Enable VM2VM communications. If this is disabled then the MAC address compare is skipped. */
typedef enum {
VM2VM_DISABLED = 0,
VM2VM_SOFTWARE = 1,
VM2VM_HARDWARE = 2,
VM2VM_LAST
} vm2vm_type;
static vm2vm_type vm2vm_mode = VM2VM_SOFTWARE;
/* Enable stats. */
static uint32_t enable_stats = 0;
/* Enable retries on RX. */
static uint32_t enable_retry = 1;
/* Disable TX checksum offload */
static uint32_t enable_tx_csum;
/* Disable TSO offload */
static uint32_t enable_tso;
static int client_mode;
static int builtin_net_driver;
static int async_vhost_driver;
static char *dma_type;
/* Specify timeout (in useconds) between retries on RX. */
static uint32_t burst_rx_delay_time = BURST_RX_WAIT_US;
/* Specify the number of retries on RX. */
static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
/* Socket file paths. Can be set by user */
static char *socket_files;
static int nb_sockets;
/* empty VMDq configuration structure. Filled in programmatically */
static struct rte_eth_conf vmdq_conf_default = {
.rxmode = {
.mq_mode = RTE_ETH_MQ_RX_VMDQ_ONLY,
.split_hdr_size = 0,
/*
* VLAN strip is necessary for 1G NIC such as I350,
* this fixes bug of ipv4 forwarding in guest can't
* forward packets from one virtio dev to another virtio dev.
*/
.offloads = RTE_ETH_RX_OFFLOAD_VLAN_STRIP,
},
.txmode = {
.mq_mode = RTE_ETH_MQ_TX_NONE,
.offloads = (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
RTE_ETH_TX_OFFLOAD_TCP_TSO),
},
.rx_adv_conf = {
/*
* should be overridden separately in code with
* appropriate values
*/
.vmdq_rx_conf = {
.nb_queue_pools = RTE_ETH_8_POOLS,
.enable_default_pool = 0,
.default_pool = 0,
.nb_pool_maps = 0,
.pool_map = {{0, 0},},
},
},
};
static unsigned lcore_ids[RTE_MAX_LCORE];
static uint16_t ports[RTE_MAX_ETHPORTS];
static unsigned num_ports = 0; /**< The number of ports specified in command line */
static uint16_t num_pf_queues, num_vmdq_queues;
static uint16_t vmdq_pool_base, vmdq_queue_base;
static uint16_t queues_per_pool;
const uint16_t vlan_tags[] = {
1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015,
1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031,
1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039,
1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047,
1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063,
};
/* ethernet addresses of ports */
static struct rte_ether_addr vmdq_ports_eth_addr[RTE_MAX_ETHPORTS];
static struct vhost_dev_tailq_list vhost_dev_list =
TAILQ_HEAD_INITIALIZER(vhost_dev_list);
static struct lcore_info lcore_info[RTE_MAX_LCORE];
/* Used for queueing bursts of TX packets. */
struct mbuf_table {
unsigned len;
unsigned txq_id;
struct rte_mbuf *m_table[MAX_PKT_BURST];
};
struct vhost_bufftable {
uint32_t len;
uint64_t pre_tsc;
struct rte_mbuf *m_table[MAX_PKT_BURST];
};
/* TX queue for each data core. */
struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
/*
* Vhost TX buffer for each data core.
* Every data core maintains a TX buffer for every vhost device,
* which is used for batch pkts enqueue for higher performance.
*/
struct vhost_bufftable *vhost_txbuff[RTE_MAX_LCORE * MAX_VHOST_DEVICE];
#define MBUF_TABLE_DRAIN_TSC ((rte_get_tsc_hz() + US_PER_S - 1) \
/ US_PER_S * BURST_TX_DRAIN_US)
static inline int
open_dma(const char *value)
{
if (dma_type != NULL && strncmp(dma_type, "ioat", 4) == 0)
return open_ioat(value);
return -1;
}
/*
* Builds up the correct configuration for VMDQ VLAN pool map
* according to the pool & queue limits.
*/
static inline int
get_eth_conf(struct rte_eth_conf *eth_conf, uint32_t num_devices)
{
struct rte_eth_vmdq_rx_conf conf;
struct rte_eth_vmdq_rx_conf *def_conf =
&vmdq_conf_default.rx_adv_conf.vmdq_rx_conf;
unsigned i;
memset(&conf, 0, sizeof(conf));
conf.nb_queue_pools = (enum rte_eth_nb_pools)num_devices;
conf.nb_pool_maps = num_devices;
conf.enable_loop_back = def_conf->enable_loop_back;
conf.rx_mode = def_conf->rx_mode;
for (i = 0; i < conf.nb_pool_maps; i++) {
conf.pool_map[i].vlan_id = vlan_tags[ i ];
conf.pool_map[i].pools = (1UL << i);
}
(void)(rte_memcpy(eth_conf, &vmdq_conf_default, sizeof(*eth_conf)));
(void)(rte_memcpy(ð_conf->rx_adv_conf.vmdq_rx_conf, &conf,
sizeof(eth_conf->rx_adv_conf.vmdq_rx_conf)));
return 0;
}
/*
* Initialises a given port using global settings and with the rx buffers
* coming from the mbuf_pool passed as parameter
*/
static inline int
port_init(uint16_t port)
{
struct rte_eth_dev_info dev_info;
struct rte_eth_conf port_conf;
struct rte_eth_rxconf *rxconf;
struct rte_eth_txconf *txconf;
int16_t rx_rings, tx_rings;
uint16_t rx_ring_size, tx_ring_size;
int retval;
uint16_t q;
/* The max pool number from dev_info will be used to validate the pool number specified in cmd line */
retval = rte_eth_dev_info_get(port, &dev_info);
if (retval != 0) {
RTE_LOG(ERR, VHOST_PORT,
"Error during getting device (port %u) info: %s\n",
port, strerror(-retval));
return retval;
}
if (dev_info.max_vmdq_pools == 0) {
RTE_LOG(ERR, VHOST_PORT, "Failed to get VMDq info.\n");
return -1;
}
rxconf = &dev_info.default_rxconf;
txconf = &dev_info.default_txconf;
rxconf->rx_drop_en = 1;
/*configure the number of supported virtio devices based on VMDQ limits */
num_devices = dev_info.max_vmdq_pools;
rx_ring_size = RTE_TEST_RX_DESC_DEFAULT;
tx_ring_size = RTE_TEST_TX_DESC_DEFAULT;
tx_rings = (uint16_t)rte_lcore_count();
if (mergeable) {
if (dev_info.max_mtu != UINT16_MAX && dev_info.max_rx_pktlen > dev_info.max_mtu)
vmdq_conf_default.rxmode.mtu = dev_info.max_mtu;
else
vmdq_conf_default.rxmode.mtu = MAX_MTU;
}
/* Get port configuration. */
retval = get_eth_conf(&port_conf, num_devices);
if (retval < 0)
return retval;
/* NIC queues are divided into pf queues and vmdq queues. */
num_pf_queues = dev_info.max_rx_queues - dev_info.vmdq_queue_num;
queues_per_pool = dev_info.vmdq_queue_num / dev_info.max_vmdq_pools;
num_vmdq_queues = num_devices * queues_per_pool;
num_queues = num_pf_queues + num_vmdq_queues;
vmdq_queue_base = dev_info.vmdq_queue_base;
vmdq_pool_base = dev_info.vmdq_pool_base;
printf("pf queue num: %u, configured vmdq pool num: %u, each vmdq pool has %u queues\n",
num_pf_queues, num_devices, queues_per_pool);
if (!rte_eth_dev_is_valid_port(port))
return -1;
rx_rings = (uint16_t)dev_info.max_rx_queues;
if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
port_conf.txmode.offloads |=
RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
/* Configure ethernet device. */
retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
if (retval != 0) {
RTE_LOG(ERR, VHOST_PORT, "Failed to configure port %u: %s.\n",
port, strerror(-retval));
return retval;
}
retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &rx_ring_size,
&tx_ring_size);
if (retval != 0) {
RTE_LOG(ERR, VHOST_PORT, "Failed to adjust number of descriptors "
"for port %u: %s.\n", port, strerror(-retval));
return retval;
}
if (rx_ring_size > RTE_TEST_RX_DESC_DEFAULT) {
RTE_LOG(ERR, VHOST_PORT, "Mbuf pool has an insufficient size "
"for Rx queues on port %u.\n", port);
return -1;
}
/* Setup the queues. */
rxconf->offloads = port_conf.rxmode.offloads;
for (q = 0; q < rx_rings; q ++) {
retval = rte_eth_rx_queue_setup(port, q, rx_ring_size,
rte_eth_dev_socket_id(port),
rxconf,
mbuf_pool);
if (retval < 0) {
RTE_LOG(ERR, VHOST_PORT,
"Failed to setup rx queue %u of port %u: %s.\n",
q, port, strerror(-retval));
return retval;
}
}
txconf->offloads = port_conf.txmode.offloads;
for (q = 0; q < tx_rings; q ++) {
retval = rte_eth_tx_queue_setup(port, q, tx_ring_size,
rte_eth_dev_socket_id(port),
txconf);
if (retval < 0) {
RTE_LOG(ERR, VHOST_PORT,
"Failed to setup tx queue %u of port %u: %s.\n",
q, port, strerror(-retval));
return retval;
}
}
/* Start the device. */
retval = rte_eth_dev_start(port);
if (retval < 0) {
RTE_LOG(ERR, VHOST_PORT, "Failed to start port %u: %s\n",
port, strerror(-retval));
return retval;
}
if (promiscuous) {
retval = rte_eth_promiscuous_enable(port);
if (retval != 0) {
RTE_LOG(ERR, VHOST_PORT,
"Failed to enable promiscuous mode on port %u: %s\n",
port, rte_strerror(-retval));
return retval;
}
}
retval = rte_eth_macaddr_get(port, &vmdq_ports_eth_addr[port]);
if (retval < 0) {
RTE_LOG(ERR, VHOST_PORT,
"Failed to get MAC address on port %u: %s\n",
port, rte_strerror(-retval));
return retval;
}
RTE_LOG(INFO, VHOST_PORT, "Max virtio devices supported: %u\n", num_devices);
RTE_LOG(INFO, VHOST_PORT, "Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
port, RTE_ETHER_ADDR_BYTES(&vmdq_ports_eth_addr[port]));
return 0;
}
/*
* Set socket file path.
*/
static int
us_vhost_parse_socket_path(const char *q_arg)
{
char *old;
/* parse number string */
if (strnlen(q_arg, PATH_MAX) == PATH_MAX)
return -1;
old = socket_files;
socket_files = realloc(socket_files, PATH_MAX * (nb_sockets + 1));
if (socket_files == NULL) {
free(old);
return -1;
}
strlcpy(socket_files + nb_sockets * PATH_MAX, q_arg, PATH_MAX);
nb_sockets++;
return 0;
}
/*
* Parse the portmask provided at run time.
*/
static int
parse_portmask(const char *portmask)
{
char *end = NULL;
unsigned long pm;
errno = 0;
/* parse hexadecimal string */
pm = strtoul(portmask, &end, 16);
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
return 0;
return pm;
}
/*
* Parse num options at run time.
*/
static int
parse_num_opt(const char *q_arg, uint32_t max_valid_value)
{
char *end = NULL;
unsigned long num;
errno = 0;
/* parse unsigned int string */
num = strtoul(q_arg, &end, 10);
if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
return -1;
if (num > max_valid_value)
return -1;
return num;
}
/*
* Display usage
*/
static void
us_vhost_usage(const char *prgname)
{
RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
" --vm2vm [0|1|2]\n"
" --rx_retry [0|1] --mergeable [0|1] --stats [0-N]\n"
" --socket-file <path>\n"
" --nb-devices ND\n"
" -p PORTMASK: Set mask for ports to be used by application\n"
" --vm2vm [0|1|2]: disable/software(default)/hardware vm2vm comms\n"
" --rx-retry [0|1]: disable/enable(default) retries on Rx. Enable retry if destination queue is full\n"
" --rx-retry-delay [0-N]: timeout(in usecond) between retries on RX. This makes effect only if retries on rx enabled\n"
" --rx-retry-num [0-N]: the number of retries on rx. This makes effect only if retries on rx enabled\n"
" --mergeable [0|1]: disable(default)/enable RX mergeable buffers\n"
" --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
" --socket-file: The path of the socket file.\n"
" --tx-csum [0|1] disable/enable TX checksum offload.\n"
" --tso [0|1] disable/enable TCP segment offload.\n"
" --client register a vhost-user socket as client mode.\n"
" --dma-type register dma type for your vhost async driver. For example \"ioat\" for now.\n"
" --dmas register dma channel for specific vhost device.\n"
" --total-num-mbufs [0-N] set the number of mbufs to be allocated in mbuf pools, the default value is 147456.\n",
prgname);
}
enum {
#define OPT_VM2VM "vm2vm"
OPT_VM2VM_NUM = 256,
#define OPT_RX_RETRY "rx-retry"
OPT_RX_RETRY_NUM,
#define OPT_RX_RETRY_DELAY "rx-retry-delay"
OPT_RX_RETRY_DELAY_NUM,
#define OPT_RX_RETRY_NUMB "rx-retry-num"
OPT_RX_RETRY_NUMB_NUM,
#define OPT_MERGEABLE "mergeable"
OPT_MERGEABLE_NUM,
#define OPT_STATS "stats"
OPT_STATS_NUM,
#define OPT_SOCKET_FILE "socket-file"
OPT_SOCKET_FILE_NUM,
#define OPT_TX_CSUM "tx-csum"
OPT_TX_CSUM_NUM,
#define OPT_TSO "tso"
OPT_TSO_NUM,
#define OPT_CLIENT "client"
OPT_CLIENT_NUM,
#define OPT_BUILTIN_NET_DRIVER "builtin-net-driver"
OPT_BUILTIN_NET_DRIVER_NUM,
#define OPT_DMA_TYPE "dma-type"
OPT_DMA_TYPE_NUM,
#define OPT_DMAS "dmas"
OPT_DMAS_NUM,
#define OPT_NUM_MBUFS "total-num-mbufs"
OPT_NUM_MBUFS_NUM,
};
/*
* Parse the arguments given in the command line of the application.
*/
static int
us_vhost_parse_args(int argc, char **argv)
{
int opt, ret;
int option_index;
unsigned i;
const char *prgname = argv[0];
static struct option long_option[] = {
{OPT_VM2VM, required_argument,
NULL, OPT_VM2VM_NUM},
{OPT_RX_RETRY, required_argument,
NULL, OPT_RX_RETRY_NUM},
{OPT_RX_RETRY_DELAY, required_argument,
NULL, OPT_RX_RETRY_DELAY_NUM},
{OPT_RX_RETRY_NUMB, required_argument,
NULL, OPT_RX_RETRY_NUMB_NUM},
{OPT_MERGEABLE, required_argument,
NULL, OPT_MERGEABLE_NUM},
{OPT_STATS, required_argument,
NULL, OPT_STATS_NUM},
{OPT_SOCKET_FILE, required_argument,
NULL, OPT_SOCKET_FILE_NUM},
{OPT_TX_CSUM, required_argument,
NULL, OPT_TX_CSUM_NUM},
{OPT_TSO, required_argument,
NULL, OPT_TSO_NUM},
{OPT_CLIENT, no_argument,
NULL, OPT_CLIENT_NUM},
{OPT_BUILTIN_NET_DRIVER, no_argument,
NULL, OPT_BUILTIN_NET_DRIVER_NUM},
{OPT_DMA_TYPE, required_argument,
NULL, OPT_DMA_TYPE_NUM},
{OPT_DMAS, required_argument,
NULL, OPT_DMAS_NUM},
{OPT_NUM_MBUFS, required_argument,
NULL, OPT_NUM_MBUFS_NUM},
{NULL, 0, 0, 0},
};
/* Parse command line */
while ((opt = getopt_long(argc, argv, "p:P",
long_option, &option_index)) != EOF) {
switch (opt) {
/* Portmask */
case 'p':
enabled_port_mask = parse_portmask(optarg);
if (enabled_port_mask == 0) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid portmask\n");
us_vhost_usage(prgname);
return -1;
}
break;
case 'P':
promiscuous = 1;
vmdq_conf_default.rx_adv_conf.vmdq_rx_conf.rx_mode =
RTE_ETH_VMDQ_ACCEPT_BROADCAST |
RTE_ETH_VMDQ_ACCEPT_MULTICAST;
break;
case OPT_VM2VM_NUM:
ret = parse_num_opt(optarg, (VM2VM_LAST - 1));
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid argument for "
"vm2vm [0|1|2]\n");
us_vhost_usage(prgname);
return -1;
}
vm2vm_mode = (vm2vm_type)ret;
break;
case OPT_RX_RETRY_NUM:
ret = parse_num_opt(optarg, 1);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for rx-retry [0|1]\n");
us_vhost_usage(prgname);
return -1;
}
enable_retry = ret;
break;
case OPT_TX_CSUM_NUM:
ret = parse_num_opt(optarg, 1);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for tx-csum [0|1]\n");
us_vhost_usage(prgname);
return -1;
}
enable_tx_csum = ret;
break;
case OPT_TSO_NUM:
ret = parse_num_opt(optarg, 1);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for tso [0|1]\n");
us_vhost_usage(prgname);
return -1;
}
enable_tso = ret;
break;
case OPT_RX_RETRY_DELAY_NUM:
ret = parse_num_opt(optarg, INT32_MAX);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for rx-retry-delay [0-N]\n");
us_vhost_usage(prgname);
return -1;
}
burst_rx_delay_time = ret;
break;
case OPT_RX_RETRY_NUMB_NUM:
ret = parse_num_opt(optarg, INT32_MAX);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for rx-retry-num [0-N]\n");
us_vhost_usage(prgname);
return -1;
}
burst_rx_retry_num = ret;
break;
case OPT_MERGEABLE_NUM:
ret = parse_num_opt(optarg, 1);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for mergeable [0|1]\n");
us_vhost_usage(prgname);
return -1;
}
mergeable = !!ret;
break;
case OPT_STATS_NUM:
ret = parse_num_opt(optarg, INT32_MAX);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid argument for stats [0..N]\n");
us_vhost_usage(prgname);
return -1;
}
enable_stats = ret;
break;
/* Set socket file path. */
case OPT_SOCKET_FILE_NUM:
if (us_vhost_parse_socket_path(optarg) == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid argument for socket name (Max %d characters)\n",
PATH_MAX);
us_vhost_usage(prgname);
return -1;
}
break;
case OPT_DMA_TYPE_NUM:
dma_type = optarg;
break;
case OPT_DMAS_NUM:
if (open_dma(optarg) == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Wrong DMA args\n");
us_vhost_usage(prgname);
return -1;
}
async_vhost_driver = 1;
break;
case OPT_NUM_MBUFS_NUM:
ret = parse_num_opt(optarg, INT32_MAX);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid argument for total-num-mbufs [0..N]\n");
us_vhost_usage(prgname);
return -1;
}
if (total_num_mbufs < ret)
total_num_mbufs = ret;
break;
case OPT_CLIENT_NUM:
client_mode = 1;
break;
case OPT_BUILTIN_NET_DRIVER_NUM:
builtin_net_driver = 1;
break;
/* Invalid option - print options. */
default:
us_vhost_usage(prgname);
return -1;
}
}
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
if (enabled_port_mask & (1 << i))
ports[num_ports++] = i;
}
if ((num_ports == 0) || (num_ports > MAX_SUP_PORTS)) {
RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
"but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
return -1;
}
return 0;
}
/*
* Update the global var NUM_PORTS and array PORTS according to system ports number
* and return valid ports number
*/
static unsigned check_ports_num(unsigned nb_ports)
{
unsigned valid_num_ports = num_ports;
unsigned portid;
if (num_ports > nb_ports) {
RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) exceeds total system port number(%u)\n",
num_ports, nb_ports);
num_ports = nb_ports;
}
for (portid = 0; portid < num_ports; portid ++) {
if (!rte_eth_dev_is_valid_port(ports[portid])) {
RTE_LOG(INFO, VHOST_PORT,
"\nSpecified port ID(%u) is not valid\n",
ports[portid]);
ports[portid] = INVALID_PORT_ID;
valid_num_ports--;
}
}
return valid_num_ports;
}
static __rte_always_inline struct vhost_dev *
find_vhost_dev(struct rte_ether_addr *mac)
{
struct vhost_dev *vdev;
TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
if (vdev->ready == DEVICE_RX &&
rte_is_same_ether_addr(mac, &vdev->mac_address))
return vdev;
}
return NULL;
}
/*
* This function learns the MAC address of the device and registers this along with a
* vlan tag to a VMDQ.
*/
static int
link_vmdq(struct vhost_dev *vdev, struct rte_mbuf *m)
{
struct rte_ether_hdr *pkt_hdr;
int i, ret;
/* Learn MAC address of guest device from packet */
pkt_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
if (find_vhost_dev(&pkt_hdr->src_addr)) {
RTE_LOG(ERR, VHOST_DATA,
"(%d) device is using a registered MAC!\n",
vdev->vid);
return -1;
}
for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
vdev->mac_address.addr_bytes[i] =
pkt_hdr->src_addr.addr_bytes[i];
/* vlan_tag currently uses the device_id. */
vdev->vlan_tag = vlan_tags[vdev->vid];
/* Print out VMDQ registration info. */
RTE_LOG(INFO, VHOST_DATA,
"(%d) mac " RTE_ETHER_ADDR_PRT_FMT " and vlan %d registered\n",
vdev->vid, RTE_ETHER_ADDR_BYTES(&vdev->mac_address),
vdev->vlan_tag);
/* Register the MAC address. */
ret = rte_eth_dev_mac_addr_add(ports[0], &vdev->mac_address,
(uint32_t)vdev->vid + vmdq_pool_base);
if (ret)
RTE_LOG(ERR, VHOST_DATA,
"(%d) failed to add device MAC address to VMDQ\n",
vdev->vid);
rte_eth_dev_set_vlan_strip_on_queue(ports[0], vdev->vmdq_rx_q, 1);
/* Set device as ready for RX. */
vdev->ready = DEVICE_RX;
return 0;
}
/*
* Removes MAC address and vlan tag from VMDQ. Ensures that nothing is adding buffers to the RX
* queue before disabling RX on the device.
*/
static inline void
unlink_vmdq(struct vhost_dev *vdev)
{
unsigned i = 0;
unsigned rx_count;
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
if (vdev->ready == DEVICE_RX) {
/*clear MAC and VLAN settings*/
rte_eth_dev_mac_addr_remove(ports[0], &vdev->mac_address);
for (i = 0; i < 6; i++)
vdev->mac_address.addr_bytes[i] = 0;
vdev->vlan_tag = 0;
/*Clear out the receive buffers*/
rx_count = rte_eth_rx_burst(ports[0],
(uint16_t)vdev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
while (rx_count) {
for (i = 0; i < rx_count; i++)
rte_pktmbuf_free(pkts_burst[i]);
rx_count = rte_eth_rx_burst(ports[0],
(uint16_t)vdev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
}
vdev->ready = DEVICE_MAC_LEARNING;
}
}
static inline void
free_pkts(struct rte_mbuf **pkts, uint16_t n)
{
while (n--)
rte_pktmbuf_free(pkts[n]);
}
static __rte_always_inline void
complete_async_pkts(struct vhost_dev *vdev)
{
struct rte_mbuf *p_cpl[MAX_PKT_BURST];
uint16_t complete_count;
complete_count = rte_vhost_poll_enqueue_completed(vdev->vid,
VIRTIO_RXQ, p_cpl, MAX_PKT_BURST);
if (complete_count) {
free_pkts(p_cpl, complete_count);
__atomic_sub_fetch(&vdev->pkts_inflight, complete_count, __ATOMIC_SEQ_CST);
}
}
static __rte_always_inline void
sync_virtio_xmit(struct vhost_dev *dst_vdev, struct vhost_dev *src_vdev,
struct rte_mbuf *m)
{
uint16_t ret;
if (builtin_net_driver) {
ret = vs_enqueue_pkts(dst_vdev, VIRTIO_RXQ, &m, 1);
} else {
ret = rte_vhost_enqueue_burst(dst_vdev->vid, VIRTIO_RXQ, &m, 1);
}
if (enable_stats) {
__atomic_add_fetch(&dst_vdev->stats.rx_total_atomic, 1,
__ATOMIC_SEQ_CST);
__atomic_add_fetch(&dst_vdev->stats.rx_atomic, ret,
__ATOMIC_SEQ_CST);
src_vdev->stats.tx_total++;
src_vdev->stats.tx += ret;
}
}
static __rte_always_inline void
drain_vhost(struct vhost_dev *vdev)
{
uint16_t ret;
uint32_t buff_idx = rte_lcore_id() * MAX_VHOST_DEVICE + vdev->vid;
uint16_t nr_xmit = vhost_txbuff[buff_idx]->len;
struct rte_mbuf **m = vhost_txbuff[buff_idx]->m_table;
if (builtin_net_driver) {
ret = vs_enqueue_pkts(vdev, VIRTIO_RXQ, m, nr_xmit);
} else if (async_vhost_driver) {
uint16_t enqueue_fail = 0;
complete_async_pkts(vdev);
ret = rte_vhost_submit_enqueue_burst(vdev->vid, VIRTIO_RXQ, m, nr_xmit);
__atomic_add_fetch(&vdev->pkts_inflight, ret, __ATOMIC_SEQ_CST);
enqueue_fail = nr_xmit - ret;
if (enqueue_fail)
free_pkts(&m[ret], nr_xmit - ret);
} else {
ret = rte_vhost_enqueue_burst(vdev->vid, VIRTIO_RXQ,
m, nr_xmit);
}
if (enable_stats) {
__atomic_add_fetch(&vdev->stats.rx_total_atomic, nr_xmit,
__ATOMIC_SEQ_CST);
__atomic_add_fetch(&vdev->stats.rx_atomic, ret,
__ATOMIC_SEQ_CST);
}
if (!async_vhost_driver)
free_pkts(m, nr_xmit);
}
static __rte_always_inline void
drain_vhost_table(void)
{
uint16_t lcore_id = rte_lcore_id();
struct vhost_bufftable *vhost_txq;
struct vhost_dev *vdev;
uint64_t cur_tsc;
TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
if (unlikely(vdev->remove == 1))
continue;
vhost_txq = vhost_txbuff[lcore_id * MAX_VHOST_DEVICE
+ vdev->vid];
cur_tsc = rte_rdtsc();
if (unlikely(cur_tsc - vhost_txq->pre_tsc
> MBUF_TABLE_DRAIN_TSC)) {
RTE_LOG_DP(DEBUG, VHOST_DATA,
"Vhost TX queue drained after timeout with burst size %u\n",
vhost_txq->len);
drain_vhost(vdev);
vhost_txq->len = 0;
vhost_txq->pre_tsc = cur_tsc;
}
}
}
/*
* Check if the packet destination MAC address is for a local device. If so then put
* the packet on that devices RX queue. If not then return.
*/
static __rte_always_inline int
virtio_tx_local(struct vhost_dev *vdev, struct rte_mbuf *m)
{
struct rte_ether_hdr *pkt_hdr;
struct vhost_dev *dst_vdev;
struct vhost_bufftable *vhost_txq;
uint16_t lcore_id = rte_lcore_id();
pkt_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
dst_vdev = find_vhost_dev(&pkt_hdr->dst_addr);
if (!dst_vdev)
return -1;
if (vdev->vid == dst_vdev->vid) {
RTE_LOG_DP(DEBUG, VHOST_DATA,
"(%d) TX: src and dst MAC is same. Dropping packet.\n",
vdev->vid);
return 0;
}
RTE_LOG_DP(DEBUG, VHOST_DATA,
"(%d) TX: MAC address is local\n", dst_vdev->vid);
if (unlikely(dst_vdev->remove)) {
RTE_LOG_DP(DEBUG, VHOST_DATA,
"(%d) device is marked for removal\n", dst_vdev->vid);
return 0;
}
vhost_txq = vhost_txbuff[lcore_id * MAX_VHOST_DEVICE + dst_vdev->vid];