summaryrefslogtreecommitdiff
path: root/virtio_user.c
blob: 4e73aa3a5f97c9553dfa3def20260ac869a5b8e1 (plain)
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
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
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
/*
 * Copyright 2013-2014 6WIND S.A.
 *
 * 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 6WIND S.A. 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.
 */

#define VIRTIO_NET_PMD_VERSION "1.2"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/io.h>
#include <assert.h>

#include <linux/virtio_net.h>
#include <linux/virtio_pci.h>
#include <linux/virtio_ring.h>

#include <rte_ethdev.h>
#include <rte_malloc.h>
#include <rte_memzone.h>
#include <rte_version.h>

#define DPDK_VERSION_BEFORE(x,y) RTE_VERSION <  RTE_VERSION_NUM(x,y,0,0)
#define DPDK_VERSION_SINCE(x,y)  RTE_VERSION >= RTE_VERSION_NUM(x,y,0,0)

/* We never use MSI-X, so the offset is never 24. */
#define VIRTIO_PCI_CONFIG_OFFT	20

#define VIRTIO_USERMAP_MAX_QP	128

#define VIRTIO_MAX_MAC_ADDRS 1

/* If multiqueue is provided by host, then we suppport it. */
#ifndef VIRTIO_NET_F_MQ
#define VIRTIO_NET_F_MQ 22      /* Device supports Receive Flow Steering */
#define VIRTIO_NET_CTRL_MQ   4
#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000

struct virtio_net_config_full {
	uint8_t mac[6];
	uint16_t  status;
	uint16_t max_virtqueue_pairs;
} __attribute__((packed));
#else
#define virtio_net_config_full virtio_net_config
#endif

/* Comment the following define in case of out of order store / load. */
#define WEAK_BARRIERS

#ifdef WEAK_BARRIERS
#define virtio_mb()  __asm__ __volatile__("":::"memory")
#define virtio_wmb() __asm__ __volatile__("":::"memory")
#define virtio_rmb() __asm__ __volatile__("":::"memory")
#else
#define virtio_mb()  rte_mb()
#define virtio_wmb() rte_wmb()
#define virtio_rmb() rte_rmb()
#endif

enum {
	VIRTIO_QUEUE_RECV,
	VIRTIO_QUEUE_XMIT,
	VIRTIO_QUEUE_CTRL,

	VIRTIO_QUEUE_MAX
};

struct virtio_net_adapter;

struct virtio_tx_head {
	struct virtio_pmd_net_hdr* header;
	phys_addr_t hdr_addr;
};

struct virtio_stats {
	uint64_t ibytes;
	uint64_t ipackets;
	uint64_t ierrors;
	uint64_t ibadlen;
	uint64_t obytes;
	uint64_t opackets;
};

struct virtio_net_vring {
	struct vring vr;
	unsigned int head;
	unsigned int num_added;
	uint16_t last_used;
	unsigned int queue_idx;
	unsigned int freeslots;
	struct virtio_net_adapter* adapter;
	void** user;
	uint8_t port_id;
	struct rte_mempool* mp;
	unsigned int qsize;
	unsigned int qmask;
	struct virtio_tx_head* tx_hdrs;
	struct virtio_stats stats;
	unsigned long size;
	void* mapped_addr;
	size_t mapped_size;
};

struct virtio_net_adapter {
	uint16_t ioaddr;
	uint32_t iolen;

	struct virtio_net_vring qps[VIRTIO_USERMAP_MAX_QP][2];
	struct virtio_net_vring cq;
	uint32_t qp_num;
	uint32_t cur_qp;
	uint32_t cq_present;

	unsigned int features;
	int header_size;
	int header_offset;

	int device_fd;
	uint32_t mtu;

	struct virtio_net_config_full conf;
	struct ether_addr mac[VIRTIO_MAX_MAC_ADDRS];

	struct rte_pci_addr pci_addr;
};

struct virtio_pmd_net_hdr {
	union {
		struct virtio_net_hdr hdr;
		struct virtio_net_hdr_mrg_rxbuf mhdr;
	};
};

/* Uncomment features when implemented. */
static unsigned int features_array[] = {
	/* VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, */
	/* VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_GUEST_TSO4, */
	/* VIRTIO_NET_F_HOST_TSO6, VIRTIO_NET_F_GUEST_TSO6, */
	/* VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_ECN, */
	/* VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_GUEST_UFO, */
	/* VIRTIO_NET_F_GSO, */
	VIRTIO_NET_F_MAC, VIRTIO_NET_F_MRG_RXBUF,
	VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
	VIRTIO_NET_F_MQ
};

#define VIRTIO_MAX_CTRL_DATA 128

struct virtio_pmd_ctrl {
	struct virtio_net_ctrl_hdr hdr;
	uint8_t data[VIRTIO_MAX_CTRL_DATA];
};

#if DPDK_VERSION_BEFORE(1,3)
/* until dpdk-1.3, queue structs are igb ones */
typedef struct igb_tx_queue rte_txq_t;
typedef struct igb_rx_queue rte_rxq_t;
/* until dpdk-1.3, bit-fields vlan_macip are flat in rte_pktmbuf */
#define RTE_MBUF_VLAN_MAC_IP(m) (m)->pkt.vlan_tci
#else /* since dpdk-1.3 */
typedef void rte_txq_t;
typedef void rte_rxq_t;
#define RTE_MBUF_VLAN_MAC_IP(m) (m)->pkt.vlan_macip.f.vlan_tci
#endif

/* This part is taken from ixgbe and igb pmd drivers. */
static inline struct rte_mbuf*
rte_rxmbuf_alloc(struct rte_mempool* mp)
{
	struct rte_mbuf *m;
	m = __rte_mbuf_raw_alloc(mp);
	__rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
	return m;
}

#define RTE_MBUF_DATA_DMA_ADDR(mb) \
	((uint64_t)(mb)->pkt.data - \
	 (uint64_t)(mb)->buf_addr) + \
	(mb)->buf_physaddr

static inline int
virtio_has_feature(const struct virtio_net_adapter* adapter,
		   unsigned long feature)
{
	return !!(adapter->features & (1 << feature));
}

static inline void
virtio_set_feature(struct virtio_net_adapter* adapter)
{
	outl(adapter->features, adapter->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
}

static inline void
virtio_negociate_feature(struct virtio_net_adapter* adapter)
{
	unsigned int features = inl(adapter->ioaddr + VIRTIO_PCI_HOST_FEATURES);
	unsigned int i;
	adapter->features = 0;
	for (i = 0; i < sizeof(features_array) / sizeof(features_array[0]); i++) {
		if (features & (1 << features_array[i])) {
			adapter->features |= (1 << features_array[i]);
		}
	}

	/* Transport feature negociation. */
	/* FIXME: INDIRECT not supported yet
	if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
		adapter->features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
	}
	*/
}

static inline void
virtio_read_conf(uint16_t addr, int offset, void* data, int len)
{
	int i;
	uint8_t* cfg = (uint8_t*)data;
	for (i = 0; i < len; i++) {
		cfg[i] = inb(addr + offset + i);
	}
}

static inline void
virtio_ring_doorbell(struct virtio_net_vring* vq)
{
	virtio_wmb();
	vq->vr.avail->idx += vq->num_added;
	vq->num_added = 0;

	virtio_mb();
	outw(vq->queue_idx, vq->adapter->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
}

static const struct rte_memzone *
ring_dma_zone_reserve(const char *z_name, uint32_t ring_size, int socket_id)
{
	const struct rte_memzone *mz;

	mz = rte_memzone_lookup(z_name);
	if (mz)
		return mz;

	return rte_memzone_reserve_aligned(z_name, (uint64_t) ring_size,
			socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
}

static int
virtio_alloc_queue(struct virtio_net_adapter* dev,
		   struct virtio_net_vring* queue,
		   int index, int rsize,
		   char* mem_name, int socket_id)
{
	uint16_t qsize;
	const struct rte_memzone *mz;
	unsigned long size;
	int psz = sysconf(_SC_PAGE_SIZE), i;

	outw(index, dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
	qsize = inw(dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);

	queue->adapter = dev;

	if (inl(dev->ioaddr + VIRTIO_PCI_QUEUE_PFN) != 0) {
		rte_panic(PCI_PRI_FMT": this virtio-net device is already used by "
			  "another driver\n",
			  dev->pci_addr.domain,
			  dev->pci_addr.bus,
			  dev->pci_addr.devid,
			  dev->pci_addr.function);
	}

	if (rsize && qsize != rsize) {
		RTE_LOG(ERR, PMD, "%s(): number of RX descriptors per"
			" queue doesn't match: (%u) != %d\n",
			__func__, qsize, (int)rsize);
		return -EINVAL;
	}

	/* No interrupt. */
	outw(VIRTIO_MSI_NO_VECTOR, dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);

	size = vring_size(qsize, VIRTIO_PCI_VRING_ALIGN);
	if (size & (psz - 1)) {
		size += psz;
		size &= ~(psz - 1);
	}

	mz = ring_dma_zone_reserve(mem_name, size, socket_id);

	if (mz == NULL) {
		RTE_LOG(ERR, PMD, "%s: Cannot allocate queue %s\n",
				__func__, mem_name);
		return -ENOMEM;
	}

	outl(mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
	     dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);

	memset(mz->addr, 0, size);
	vring_init(&queue->vr, qsize, mz->addr, VIRTIO_PCI_VRING_ALIGN);

	queue->size = size;

	queue->head = 0;
	queue->last_used = queue->vr.used->idx;
	queue->freeslots = queue->vr.num;
	queue->num_added = 0;
	queue->queue_idx = index;
	queue->user = NULL;
	queue->qsize = qsize;
	assert(!(qsize & (qsize - 1)));
	queue->qmask = qsize - 1;
	queue->mapped_addr = NULL;
	queue->mapped_size = 0;

	for (i = 0; i < qsize - 1; i++) {
		queue->vr.desc[i].next = i + 1;
	}

	/* This is actually a FIFO, not a ring. Set to 0 for sanity purpose. */
	queue->vr.desc[qsize - 1].next = 0;

	/* Be sure to not be bothered with interrupts. */
	queue->vr.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;

	return 0;
}

static void
virtio_dev_infos_get(struct rte_eth_dev *dev,
		     struct rte_eth_dev_info *dev_info)
{
	const struct virtio_net_adapter* priv;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	dev_info->max_rx_queues = priv->qp_num;
	dev_info->max_tx_queues = priv->qp_num;
	dev_info->min_rx_bufsize = 2048; /* cf BSIZEPACKET in SRRCTL register */
	dev_info->max_rx_pktlen = 16384; /* includes CRC, cf MAXFRS register */
	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
}

static int
virtio_alloc_rxq(struct virtio_net_vring* rq, unsigned int num)
{
	unsigned int head = rq->head, avail;
	unsigned int num_alloc = 0;
	unsigned int i;
	uint32_t hdr_offt = rq->adapter->header_offset;

	for (i = head; num_alloc < num; i = rq->vr.desc[i].next, num_alloc++) {
		struct rte_mbuf* pkt = rte_rxmbuf_alloc(rq->mp);
		unsigned long phyaddr;
		unsigned int buf_len;
		if (!pkt) {
			break;
		}
		phyaddr = (pkt->buf_physaddr + hdr_offt);
		buf_len = (pkt->buf_len - hdr_offt);
		rq->vr.desc[i].flags = VRING_DESC_F_WRITE;
		rq->vr.desc[i].addr = phyaddr;
		rq->vr.desc[i].len = buf_len;
		rq->user[i] = (void*)pkt;
		rq->freeslots--;
		head = i;

		avail = (rq->vr.avail->idx + rq->num_added) & rq->qmask;
		rq->num_added++;
		rq->vr.avail->ring[avail] = head;
	}

	rq->head = i;

	if (num_alloc) {
		virtio_ring_doorbell(rq);
	}
	return num_alloc;
}

static uint16_t
virtio_recv_buf(rte_rxq_t *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
{
	unsigned int len;
	struct virtio_pmd_net_hdr* ptr;
	uint16_t completed = 0;
	struct virtio_net_vring* rq = (struct virtio_net_vring*)rxq;
	uint32_t hdr_offt = rq->adapter->header_offset;
	uint32_t hdr_sz = rq->adapter->header_size;

	while (rq->last_used != rq->vr.used->idx) {
		struct rte_mbuf *buf, *cur_seg;
		uint32_t index;
		unsigned int idx, i, n_frag;

		if (completed >= nb_pkts) {
			break;
		}

		virtio_rmb();
		/* Power of two by specifications. */
		index = rq->last_used & rq->qmask;
		idx = rq->vr.used->ring[index].id;
		len = (rq->vr.used->ring[index].len - hdr_sz);
		i = idx;

		buf = (struct rte_mbuf*)rq->user[i];
		buf->pkt.data = ((char *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
		ptr = (struct virtio_pmd_net_hdr*)
			((char*)buf->buf_addr + hdr_offt);

		if (hdr_sz == sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
			n_frag = ptr->mhdr.num_buffers;
			/* FIXME: What does it mean when 0 ? */
			n_frag = n_frag ? n_frag : 1;
		} else {
			n_frag = 1;
		}

		/* Gather split buffers. */
		rq->vr.desc[i].next = rq->head;
		rq->head = idx;
		rq->last_used++;
		rq->freeslots++;

		buf->pkt.nb_segs = n_frag;
		buf->pkt.next = NULL;
		buf->pkt.pkt_len = len;
		buf->pkt.data_len = len;
		buf->pkt.in_port = rq->port_id;
		buf->ol_flags = 0; /* RSS - FDIR // Error */
		RTE_MBUF_VLAN_MAC_IP(buf) = 0;

		cur_seg = buf;
		rx_pkts[completed] = buf;

		while (n_frag > 1) {

			if (rq->vr.used->idx == rq->last_used) {
				rq->stats.ibadlen++;
				rte_pktmbuf_free(rx_pkts[completed]);
				goto error_frag;
			}

			index = rq->last_used & rq->qmask;
			idx = rq->vr.used->ring[index].id;
			len = rq->vr.used->ring[index].len;

			i = idx;
			buf = (struct rte_mbuf*)rq->user[i];
			buf->pkt.data = ((char *)buf->buf_addr + hdr_offt);

			buf->pkt.next = NULL;
			buf->pkt.data_len = len;

			/* New tail */
			cur_seg->pkt.next = buf;
			cur_seg = buf;

			/* Update head pkt_len */
			rx_pkts[completed]->pkt.pkt_len += len;

			rq->vr.desc[i].next = rq->head;
			rq->head = idx;

			rq->last_used++;
			rq->freeslots++;

			n_frag--;
		}
		rq->stats.ibytes += rx_pkts[completed]->pkt.pkt_len;
		rq->stats.ipackets++;
		completed++;
	}

error_frag:

	if (rq->freeslots) {
		virtio_alloc_rxq(rq, rq->freeslots);
	}
	return completed;
}

static int
virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
			   uint16_t queue_idx,
			   uint16_t nb_desc,
			   unsigned int socket_id,
			   const struct rte_eth_rxconf *rx_conf,
			   struct rte_mempool *mp)
{

	struct virtio_net_adapter* priv;
	char qname[32];
	struct virtio_net_vring* rq;
	unsigned int vr_idx = 2 * queue_idx + VIRTIO_QUEUE_RECV;
	char ring_name[RTE_MEMZONE_NAMESIZE];
	int diag;
	(void)rx_conf;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;
	rq = &priv->qps[queue_idx][VIRTIO_QUEUE_RECV];

	snprintf(qname, sizeof(qname), "input.%d", queue_idx);

	snprintf(ring_name, sizeof(ring_name), "%s_input_%d_%d",
		 dev->driver->pci_drv.name, dev->data->port_id,
		 queue_idx);

	diag = virtio_alloc_queue(priv, rq, vr_idx, nb_desc,
				  ring_name, socket_id);
	if (diag) {
		return diag;
	}

	rq->user = rte_zmalloc(qname, nb_desc * sizeof(void*), CACHE_LINE_SIZE);
	if (rq->user == NULL) {
		return -ENOMEM;
	}

	rq->mp = mp;
	if (!virtio_alloc_rxq(rq, rq->freeslots)) {
		rte_free(rq->user);
		return -ENOMEM;
	}
	rq->tx_hdrs = NULL;

	memset(&rq->stats, 0, sizeof(struct virtio_stats));

	dev->data->rx_queues[queue_idx] = (void*)rq;
	dev->rx_pkt_burst = virtio_recv_buf;
	rq->port_id = dev->data->port_id;

	return 0;
}

int virtio_tx_complete(struct virtio_net_vring* tq)
{
	int num_freed = 0;

	while (tq->last_used != tq->vr.used->idx) {
		unsigned int idx, i;
		uint32_t index;

		virtio_rmb();
		index = tq->last_used & tq->qmask;
		idx = tq->vr.used->ring[index].id;
		i = idx;

		while (tq->vr.desc[i].flags & VRING_DESC_F_NEXT) {
			if (tq->user[i]) {
				rte_pktmbuf_free_seg((struct rte_mbuf*)tq->user[i]);
				tq->user[i] = NULL;
			}
			i = tq->vr.desc[i].next;

			num_freed++;
		}
		if (tq->user[i]) {
			rte_pktmbuf_free_seg((struct rte_mbuf*)tq->user[i]);
			tq->user[i] = NULL;
		}

		tq->vr.desc[i].next = tq->head;
		tq->head = idx;

		tq->last_used++;
		num_freed++;
	}

	tq->freeslots += num_freed;
	return num_freed;
}

static inline int
virtio_send_packet(struct virtio_net_vring* tq, struct rte_mbuf* mbuf)
{
	unsigned int head, prev, i, avail;
	unsigned int nseg = mbuf->pkt.nb_segs;
	struct rte_mbuf* cur = mbuf;
	unsigned int hdr_len;
	unsigned long hdr_phys;
	uint32_t len = mbuf->pkt.pkt_len;

	/* No need to send empty packets. */
	if (len == 0) {
		return 1;
	}

	/* Check if there are slots available. */
	if (tq->freeslots < nseg + 1) {
		return -1;
	}

	head  = tq->head;

	/* FIXME: No CSUM of GS offloading for the moment. */
	hdr_phys = tq->tx_hdrs[tq->qsize].hdr_addr;

	/* First we fill in the VIRTIO header. */
	hdr_len = tq->adapter->header_size;

	tq->vr.desc[head].flags = VRING_DESC_F_NEXT;
	tq->vr.desc[head].addr = hdr_phys;
	tq->vr.desc[head].len = hdr_len;
	tq->user[head] = NULL;

	for (i = tq->vr.desc[head].next; nseg > 0; nseg--, cur = cur->pkt.next) {
		if (!cur->pkt.data_len) {
			continue;
		}
		tq->vr.desc[i].flags = VRING_DESC_F_NEXT;
		tq->vr.desc[i].addr = RTE_MBUF_DATA_DMA_ADDR(cur);
		tq->vr.desc[i].len = cur->pkt.data_len;
		tq->user[i] = cur;
		prev = i;
		i = tq->vr.desc[i].next;
		tq->freeslots--;
	}
	tq->vr.desc[prev].flags &= ~VRING_DESC_F_NEXT;


	avail = (tq->vr.avail->idx + tq->num_added) & tq->qmask;
	tq->vr.avail->ring[avail] = head;

	tq->head = i;

	tq->num_added++;
	tq->freeslots--;

	tq->stats.obytes += len;
	tq->stats.opackets++;

	return tq->num_added;
}

/* Exported Tx function after Tx Queue setup. */
static uint16_t
eth_tx_burst(rte_txq_t *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
{
	struct virtio_net_vring* tq = (struct virtio_net_vring*)txq;
	int pkt_index = 0;

	virtio_tx_complete(tq);

	while (pkt_index < nb_pkts) {
		int ret = virtio_send_packet(tq, tx_pkts[pkt_index]);
		if (ret <= 0) {
			if(!virtio_tx_complete(tq)) {
				break;
			}
		} else {
			++pkt_index;
		}
	}

	if (pkt_index) {
		virtio_ring_doorbell(tq);
	}

	return pkt_index;
}

static int
virtio_alloc_txq(struct virtio_net_vring* tq, int sz)
{
	int psz = sysconf(_SC_PAGE_SIZE);
	int strsz = sizeof(struct virtio_pmd_net_hdr);
	int hdrppg = psz / strsz;
	int pgnum = (sz + (hdrppg>>1)) / hdrppg;
	int i, j;
	void* addr;

	addr = mmap(NULL, psz*pgnum, PROT_READ|PROT_WRITE,
		    MAP_ANONYMOUS|MAP_SHARED, -1, 0);

	if(addr == MAP_FAILED) {
		perror("mmap");
		return-1;
	}

	tq->tx_hdrs = (struct virtio_tx_head*)
			rte_zmalloc("virtio-tx-headers",
				    sz * sizeof(struct virtio_tx_head), 0);
	if (tq->tx_hdrs == NULL) {
		munmap(addr, psz * pgnum);
		return -ENOMEM;
	}

	/* Let's unmap it. */
	tq->mapped_addr = addr;
	tq->mapped_size = psz * pgnum;

	for (i = 0; i < pgnum; i++) {
		uint8_t* ptr = (uint8_t*)addr + i*psz;
		struct virtio_pmd_net_hdr* hdr =
			(struct virtio_pmd_net_hdr*)ptr;
		phys_addr_t physaddr = rte_mem_virt2phy(ptr);
		for (j = i*hdrppg; j < sz && j < (i+1)*hdrppg; j++) {
			tq->tx_hdrs[j].header = &hdr[j];
			tq->tx_hdrs[j].hdr_addr = physaddr +
						  strsz * (j - (i * hdrppg));
		}
	}
	/* Reserve a NULL header for NO OP on hypervisor side. */
	memset(tq->tx_hdrs[sz - 1].header, 0,
	       sizeof(*tq->tx_hdrs[sz - 1].header));
	return 0;
}

static int
virtio_dev_tx_queue_setup(struct rte_eth_dev* dev,
			   uint16_t queue_idx,
			   uint16_t nb_desc,
			   unsigned int socket_id,
			   const struct rte_eth_txconf* tx_conf)
{
	struct virtio_net_adapter* priv;
	char qname[32];
	struct virtio_net_vring* tq;
	unsigned int vr_idx = 2 * queue_idx + VIRTIO_QUEUE_XMIT;
	char ring_name[RTE_MEMZONE_NAMESIZE];
	int diag;
	(void)tx_conf;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;
	tq = &priv->qps[queue_idx][VIRTIO_QUEUE_XMIT];

	snprintf(qname, sizeof(qname), "input.%d", queue_idx);

	snprintf(ring_name, sizeof(ring_name), "%s_output_%d_%d",
		 dev->driver->pci_drv.name, dev->data->port_id,
		 queue_idx);

	diag = virtio_alloc_queue(priv, tq, vr_idx, nb_desc,
				  ring_name, socket_id);
	if (diag) {
		return diag;
	}

	tq->user = rte_zmalloc(qname, nb_desc * sizeof(void*), CACHE_LINE_SIZE);
	if (tq->user == NULL) {
		return -ENOMEM;
	}

	tq->mp = NULL;
	if (virtio_alloc_txq(tq, tq->qsize + 1)) {
		rte_free(tq->user);
		return -ENOMEM;
	}

	memset(&tq->stats, 0, sizeof(struct virtio_stats));

	dev->data->tx_queues[queue_idx] = (void*)tq;
	dev->tx_pkt_burst = eth_tx_burst;
	tq->port_id = dev->data->port_id;

	return 0;
}

static int
virtio_send_command(struct virtio_net_vring* vq, struct virtio_pmd_ctrl* ctrl,
		    int* dlen, int pkt_num)
{
	unsigned int head = vq->head, i, avail;
	virtio_net_ctrl_ack status = ~0;
	int k, sum = 0;

	if (!vq->adapter->cq_present) {
		RTE_LOG(ERR, PMD, "%s(): Control queue is "
			"not supported by this device\n", __func__);
		return -1;
	}

	if (vq->freeslots < (unsigned int)pkt_num + 2 || pkt_num < 1) {
		return -1;
	}

	/*
	 * Format is enforced in QEMU code:
	 * - one Tx packet for header
	 * - at least one Tx packet per argument
	 * - one Rx packet for ACK.
	 */
	vq->vr.desc[head].flags = VRING_DESC_F_NEXT;
	vq->vr.desc[head].addr = rte_mem_virt2phy(&ctrl->hdr);
	vq->vr.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
	vq->freeslots--;
	i = vq->vr.desc[head].next;

	for (k = 0; k < pkt_num; k++) {
		vq->vr.desc[i].flags = VRING_DESC_F_NEXT;
		vq->vr.desc[i].addr = rte_mem_virt2phy(&ctrl->data[sum]);
		vq->vr.desc[i].len = dlen[k];
		sum += dlen[k];
		vq->freeslots--;
		i = vq->vr.desc[i].next;
	}

	vq->vr.desc[i].flags = VRING_DESC_F_WRITE;
	vq->vr.desc[i].addr = rte_mem_virt2phy(&status);
	vq->vr.desc[i].len = sizeof(status);
	vq->freeslots--;

	avail = (vq->vr.avail->idx + vq->num_added) & vq->qmask;
	vq->vr.avail->ring[avail] = head;

	vq->head = vq->vr.desc[i].next;
	vq->num_added++;

	virtio_ring_doorbell(vq);

	while (vq->last_used == vq->vr.used->idx) {
		usleep(100);
	}

	while (vq->last_used != vq->vr.used->idx) {
		unsigned int idx, i;
		uint32_t index;

		virtio_rmb();
		index = vq->last_used & vq->qmask;
		idx = vq->vr.used->ring[index].id;
		i = idx;

		while (vq->vr.desc[i].flags & VRING_DESC_F_NEXT) {
			i = vq->vr.desc[i].next;
			vq->freeslots++;
		}

		vq->vr.desc[i].next = vq->head;
		vq->head = idx;

		vq->last_used++;
		vq->freeslots++;
	}

	return status;
}

static void
virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	int status;
	struct virtio_pmd_ctrl ctrl;
	int dlen[1];

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
	ctrl.data[0] = 1;
	dlen[0] = 1;
	status = virtio_send_command(&priv->cq, &ctrl, dlen, 1);

	if (status) {
		RTE_LOG(ERR, PMD, "%s(): Error while sending "
			"rx control command\n", __func__);
	}
}

static void
virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	int status;
	struct virtio_pmd_ctrl ctrl;
	int dlen[1];

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
	ctrl.data[0] = 0;
	dlen[0] = 1;
	status = virtio_send_command(&priv->cq, &ctrl, dlen, 1);

	if (status) {
		RTE_LOG(ERR, PMD, "%s(): Error while sending "
			"rx control command\n", __func__);
	}
}

static void
virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	int status;
	struct virtio_pmd_ctrl ctrl;
	int dlen[1];

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
	ctrl.data[0] = 1;
	dlen[0] = 1;
	status = virtio_send_command(&priv->cq, &ctrl, dlen, 1);

	if (status) {
		RTE_LOG(ERR, PMD, "%s(): Error while sending "
			"rx control command\n", __func__);
	}
}

static void
virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	int status;
	struct virtio_pmd_ctrl ctrl;
	int dlen[1];

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
	ctrl.data[0] = 0;
	dlen[0] = 1;
	status = virtio_send_command(&priv->cq, &ctrl, dlen, 1);

	if (status) {
		RTE_LOG(ERR, PMD, "%s(): Error while sending "
			"rx control command\n", __func__);
	}
}

/* Check link and update status. */
static int
virtio_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
{
	const struct virtio_net_adapter* priv;
	struct rte_eth_link *link;
	int old_link_status;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;
	link = &dev->data->dev_link;
	old_link_status = link->link_status;
	link->link_duplex = ETH_LINK_FULL_DUPLEX;

	if (!virtio_has_feature(priv, VIRTIO_NET_F_STATUS)) {
		link->link_status = 1;
		link->link_speed = ETH_LINK_SPEED_100;
	} else {
		uint16_t status;
		virtio_read_conf(priv->ioaddr + VIRTIO_PCI_CONFIG_OFFT,
				 offsetof(struct virtio_net_config_full,
					  status),
				 &status, sizeof(status));
		if (status & VIRTIO_NET_S_LINK_UP) {
			link->link_status = 1;
			link->link_speed = ETH_LINK_SPEED_100;
		} else {
			link->link_status = 0;
			link->link_speed = 0;
		}
	}

	(void)wait_to_complete;
	if (link->link_status == old_link_status)
		return -1;
	return 0;
}

/*
 * Doesn't actually stop the device.
 */
static void
virtio_dev_stop(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	uint8_t status;
	uint16_t index;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;
	/* Set status to 0, but this will mean we have to re-init dev. */
	outb(0,  priv->ioaddr + VIRTIO_PCI_STATUS);

#if DPDK_VERSION_BEFORE(1,3)
	/* Set queues to 0 to release them. */
	for (index = 0; index < priv->qp_num * 2; index++) {
		unsigned int i;
		outw(index, priv->ioaddr + VIRTIO_PCI_QUEUE_SEL);
		outl(0, priv->ioaddr + VIRTIO_PCI_QUEUE_PFN);
		for (i = 0 ; i < priv->qps[VIRTIO_QUEUE_RECV][index>>1].qsize; i++) {
			rte_pktmbuf_free(priv->qps[VIRTIO_QUEUE_RECV][index>>1].user[i]);
		}
		rte_free(priv->qps[VIRTIO_QUEUE_RECV][index>>1].user);
		for (i = 0 ; i < priv->qps[VIRTIO_QUEUE_XMIT][index>>1].qsize; i++) {
			if (priv->qps[VIRTIO_QUEUE_XMIT][index>>1].user[i]) {
				rte_pktmbuf_free_seg(priv->qps[VIRTIO_QUEUE_XMIT][index>>1].user[i]);
			}
		}
		rte_free(priv->qps[VIRTIO_QUEUE_XMIT][index>>1].user);
		rte_free(priv->qps[VIRTIO_QUEUE_XMIT][index>>1].tx_hdrs);
		munmap(priv->qps[VIRTIO_QUEUE_XMIT][index>>1].mapped_addr,
		       priv->qps[VIRTIO_QUEUE_XMIT][index>>1].mapped_size);
	}
#endif
	index = priv->qp_num * 2;
	if (priv->cq_present) {
		outw(index, priv->ioaddr + VIRTIO_PCI_QUEUE_SEL);
		outl(0, priv->ioaddr + VIRTIO_PCI_QUEUE_PFN);
	}

	/* Re-register driver afterwards. Sleep between changing of status. */
	sleep(1);

	status = inb(priv->ioaddr + VIRTIO_PCI_STATUS);
	status |= VIRTIO_CONFIG_S_DRIVER;
	outb(status,  priv->ioaddr + VIRTIO_PCI_STATUS);

	priv->features = 0;
	virtio_negociate_feature(priv);
	virtio_set_feature(priv);
}

/* Start device and configure RSS if enabled. */
static int
virtio_dev_start(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	priv = (struct virtio_net_adapter*)dev->data->dev_private;
	uint8_t status;
	char cqname[RTE_MEMZONE_NAMESIZE];
	uint32_t queue_id = priv->qp_num * 2;
	struct virtio_pmd_ctrl ctrl;

	priv->mtu = ETHER_MAX_LEN;
	if (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf) {
		RTE_LOG(WARNING, PMD, "%s(): RSS cannot be configured "
			"for VirtI/O net devices\n", __func__);
	}
	snprintf(cqname, sizeof(cqname), "%s_control_%d_%d",
		 dev->driver->pci_drv.name, dev->data->port_id,
		 queue_id);

	priv->cq_present = 0;
	/* Allocate control queue. */
	if (!virtio_alloc_queue(priv, &priv->cq, queue_id, 0, cqname, 0)) {
		priv->cq_present = 1;
	}

	/* This feature, if set on the host, is mandatory on the guest. */
	if (virtio_has_feature(priv, VIRTIO_NET_F_MAC)) {
		int k;
		for (k = 0; k < 6; k++) {
			outb(priv->mac[0].addr_bytes[k],
			     priv->ioaddr + VIRTIO_PCI_CONFIG_OFFT + k);
		}
	}

	/* Now we can finalize the status. */
	status = inb(priv->ioaddr + VIRTIO_PCI_STATUS);
	status |= VIRTIO_CONFIG_S_DRIVER_OK;
	outb(status,  priv->ioaddr + VIRTIO_PCI_STATUS);

	/* Negociate multiqueue if possible. */
	if (priv->cur_qp > 1) {
		int dlen[1];
		int ret;
		ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
		memcpy(ctrl.data, &priv->cur_qp, sizeof(uint16_t));
		dlen[0] = sizeof(uint16_t);
		ret = virtio_send_command(&priv->cq, &ctrl, dlen, 1);

		if (ret) {
			rte_panic("Multiqueue configured but send command "
				  "failed, this is too late now...\n");
		}
	}
	return 0;
}

static void
virtio_dev_close(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	uint16_t index;
	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	/* Set status to 0, but this will mean we have to re-init dev. */
	outb(0,  priv->ioaddr + VIRTIO_PCI_STATUS);

	/* Set queues to 0 to avoid inifinite loop in kernel. */
	for (index = 0; index < 2 * priv->qp_num; index++) {
		outw(index, priv->ioaddr + VIRTIO_PCI_QUEUE_SEL);
		outl(0, priv->ioaddr + VIRTIO_PCI_QUEUE_PFN);
	}

	index = priv->qp_num * 2;
	if (priv->cq_present) {
		outw(index, priv->ioaddr + VIRTIO_PCI_QUEUE_SEL);
		outl(0, priv->ioaddr + VIRTIO_PCI_QUEUE_PFN);
	}
}

#if DPDK_VERSION_SINCE(1,3)
static int
#else
static void
#endif
virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
{
	(void)dev;
	(void)vlan_id;
	(void)on;
	RTE_LOG(WARNING, PMD, "%s(): No VLAN enabled for device\n",
			__func__);
#if DPDK_VERSION_SINCE(1,3)
	return -1;
#endif
}

#if DPDK_VERSION_BEFORE(1,3)
/* Allocate array of pointers to register queues */
static int queue_array_alloc(struct rte_eth_dev *dev,
			     uint16_t nb_rx_q, uint16_t nb_tx_q)
{
	void *rqs, *tqs;
	uint16_t n_rq;

	rqs = dev->data->rx_queues;
	n_rq = dev->data->nb_rx_queues;

	tqs = dev->data->tx_queues;

	if (rqs == NULL) {
		rqs = malloc(sizeof(void*) * nb_rx_q);
	} else if (n_rq != nb_rx_q) {
		rqs = realloc(rqs, sizeof(void*) * nb_rx_q);
	}
	dev->data->nb_rx_queues = nb_rx_q;
	dev->data->rx_queues = rqs;

	if (tqs == NULL) {
		tqs = malloc(sizeof(void*) * nb_tx_q);
	} else if (n_rq != nb_rx_q) {
		tqs = realloc(tqs, sizeof(void*) * nb_tx_q);
	}
	dev->data->nb_tx_queues = nb_tx_q;
	dev->data->tx_queues = tqs;

	if (rqs == NULL || tqs == NULL) {
		free(rqs);
		free(tqs);
		RTE_LOG(ERR, PMD, "%s(): port_id=%d: allocation of array "
			  "for queues failed\n",
			  __func__, dev->data->port_id);
		return -ENOMEM;
	}

	return 0;
}

static int
virtio_dev_configure(struct rte_eth_dev *dev,
		     uint16_t nb_rx_q, uint16_t nb_tx_q)
#else
static int
virtio_dev_configure(struct rte_eth_dev *dev)
#endif
{
	struct virtio_net_adapter* priv;

#if DPDK_VERSION_BEFORE(1,3)
	int ret;
	ret = queue_array_alloc(dev, nb_rx_q, nb_tx_q);
	if (ret) {
		return ret;
	}
#endif

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	memset(priv->qps, 0, sizeof(priv->qps));
	if (dev->data->nb_rx_queues != dev->data->nb_tx_queues) {
		RTE_LOG(ERR, PMD, "%s(): Queues must be allocated pair-wise: "
			"%d %d\n", __func__, dev->data->nb_rx_queues,
					     dev->data->nb_tx_queues);
		return -1;
	}
	priv->cur_qp = dev->data->nb_rx_queues;

	/* FIXME: This should be supported actually. */
	if (dev->data->dev_conf.rxmode.hw_vlan_filter) {
		RTE_LOG(WARNING, PMD, "%s(): VLAN offloading is "
			"not supported right now\n", __func__);
	}

	if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_SIGNATURE) {
		RTE_LOG(WARNING, PMD, "%s(): Flow Director mode signature"
			" not supported right now\n", __func__);
	} else if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT) {
		RTE_LOG(WARNING, PMD, "%s(): Flow Director perfect"
			" not supported right now\n", __func__);
	}


	return 0;
}

#if DPDK_VERSION_SINCE(1,3)
static void
virtio_dev_rx_queue_release(rte_rxq_t* rxq)
{
	struct virtio_net_vring* rq = (struct virtio_net_vring*)rxq;
	unsigned int i;

	outw(rq->queue_idx, rq->adapter->ioaddr + VIRTIO_PCI_QUEUE_SEL);
	outl(0, rq->adapter->ioaddr + VIRTIO_PCI_QUEUE_PFN);

	for (i = 0 ; i < rq->qsize; i++) {
		rte_pktmbuf_free(rq->user[i]);
	}
	rte_free(rq->user);
}

static void
virtio_dev_tx_queue_release(rte_txq_t* txq)
{
	struct virtio_net_vring* tq = (struct virtio_net_vring*)txq;
	unsigned int i;

	outw(tq->queue_idx, tq->adapter->ioaddr + VIRTIO_PCI_QUEUE_SEL);
	outl(0, tq->adapter->ioaddr + VIRTIO_PCI_QUEUE_PFN);

	for (i = 0 ; i < tq->qsize; i++) {
		if (tq->user[i]) {
			rte_pktmbuf_free_seg(tq->user[i]);
		}
	}
	rte_free(tq->user);
	rte_free(tq->tx_hdrs);
	munmap(tq->mapped_addr, tq->mapped_size);
}
#endif

/* Get statistics. All statistics are not available compared to IXGBE. */
static void
virtio_dev_stats_get(struct rte_eth_dev *dev,
		      struct rte_eth_stats *stats)
{
	const struct virtio_net_adapter* priv;
	unsigned int i;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;


	stats->opackets = 0;
	stats->obytes = 0;

	stats->ipackets = 0;
	stats->ibytes = 0;
	stats->ierrors = 0;
	/* No data for these ones */
	stats->oerrors = 0;
	stats->rx_nombuf = 0;
	stats->fdirmatch = 0;
	stats->fdirmiss = 0;

	for (i = 0; i < priv->cur_qp; i++) {
		stats->opackets +=
			priv->qps[i][VIRTIO_QUEUE_XMIT].stats.opackets;
		stats->obytes   +=
			priv->qps[i][VIRTIO_QUEUE_XMIT].stats.obytes;
		stats->ipackets +=
			priv->qps[i][VIRTIO_QUEUE_RECV].stats.ipackets;
		stats->ibytes   +=
			priv->qps[i][VIRTIO_QUEUE_RECV].stats.ibytes;
		stats->ierrors  +=
			priv->qps[i][VIRTIO_QUEUE_RECV].stats.ierrors +
			priv->qps[i][VIRTIO_QUEUE_RECV].stats.ibadlen;
	}
}

static void
virtio_dev_stats_reset(struct rte_eth_dev *dev)
{
	struct virtio_net_adapter* priv;
	unsigned int i;

	priv = (struct virtio_net_adapter*)dev->data->dev_private;

	for (i = 0; i < priv->cur_qp; i++) {
		priv->qps[i][VIRTIO_QUEUE_XMIT].stats.opackets = 0;
		priv->qps[i][VIRTIO_QUEUE_XMIT].stats.obytes = 0;
		priv->qps[i][VIRTIO_QUEUE_RECV].stats.ipackets = 0;
		priv->qps[i][VIRTIO_QUEUE_RECV].stats.ibytes = 0;
		priv->qps[i][VIRTIO_QUEUE_RECV].stats.ibadlen = 0;
		priv->qps[i][VIRTIO_QUEUE_RECV].stats.ierrors = 0;
	}
}

static struct eth_dev_ops virtio_eth_dev_ops = {
	.dev_configure        = virtio_dev_configure,
	.dev_start            = virtio_dev_start,
	.dev_stop             = virtio_dev_stop,
	.dev_close            = virtio_dev_close,
	.promiscuous_enable   = virtio_dev_promiscuous_enable,
	.promiscuous_disable  = virtio_dev_promiscuous_disable,
	.allmulticast_enable  = virtio_dev_allmulticast_enable,
	.allmulticast_disable = virtio_dev_allmulticast_disable,
	.link_update          = virtio_dev_link_update,
	.stats_get            = virtio_dev_stats_get,
	.stats_reset          = virtio_dev_stats_reset,
	.dev_infos_get        = virtio_dev_infos_get,
	.vlan_filter_set      = virtio_vlan_filter_set,
	.rx_queue_setup       = virtio_dev_rx_queue_setup,
	.tx_queue_setup       = virtio_dev_tx_queue_setup,
#if DPDK_VERSION_SINCE(1,3)
	.rx_queue_release     = virtio_dev_rx_queue_release,
	.tx_queue_release     = virtio_dev_tx_queue_release,
#endif
	.dev_led_on           = NULL,
	.dev_led_off          = NULL,
	.flow_ctrl_set        = NULL,
	.mac_addr_remove      = NULL,
	.mac_addr_add         = NULL,
	.fdir_add_signature_filter    = NULL,
	.fdir_update_signature_filter = NULL,
	.fdir_remove_signature_filter = NULL,
	.fdir_infos_get               = NULL,
	.fdir_add_perfect_filter      = NULL,
	.fdir_update_perfect_filter   = NULL,
	.fdir_remove_perfect_filter   = NULL,
	.fdir_set_masks               = NULL,
};

static int
eth_virtio_dev_init(struct eth_driver *eth_drv, struct rte_eth_dev *eth_dev)
{
	struct virtio_net_adapter* priv;
	FILE* fp;
	char* line = NULL;
	char pci_id[128];
	int found = 0;
	size_t linesz;
	struct virtio_net_config_full *config;
	uint8_t status;
	uint32_t conf_sz = sizeof(config->mac);
	(void)eth_drv;

	priv = (struct virtio_net_adapter*)eth_dev->data->dev_private;
	eth_dev->dev_ops = &virtio_eth_dev_ops;

	priv->pci_addr = eth_dev->pci_dev->addr;

	snprintf(pci_id, sizeof(pci_id), "%04x:%02x:%02x.%d",
		 eth_dev->pci_dev->addr.domain,
		 eth_dev->pci_dev->addr.bus,
		 eth_dev->pci_dev->addr.devid,
		 eth_dev->pci_dev->addr.function);

	fp = fopen("/proc/ioports", "r");
	if (fp == NULL) {
		perror("fopen");
		return -1;
	}

	while (getdelim(&line, &linesz, '\n', fp) > 0) {
		char* ptr = line;
		char* left;
		int n;

		n = strcspn(ptr, ":");
		ptr[n]= 0;
		left = &ptr[n+1];

		while (*left && isspace(*left)) {
			left++;
		}
		if (!strncmp(left, pci_id, strlen(pci_id))) {
			uint16_t start, end;
			int len;
			found = 1;

			while (*ptr && isspace(*ptr)) {
				ptr++;
			}
			sscanf(ptr, "%04hx-%04hx", &start, &end);

			len = end - start + 1;
			priv->ioaddr = start;
			priv->iolen = len;
			break;
		}
	}
	free(line);
	fclose(fp);

	if (!found) {
		return -1;
	}

	/* Register as a driver. */
	status = inb(priv->ioaddr + VIRTIO_PCI_STATUS);
	status |= VIRTIO_CONFIG_S_DRIVER;
	outb(status,  priv->ioaddr + VIRTIO_PCI_STATUS);

	priv->features = 0;
	virtio_negociate_feature(priv);
	virtio_set_feature(priv);

	if (virtio_has_feature(priv, VIRTIO_NET_F_MRG_RXBUF)) {
		priv->header_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
	} else {
		priv->header_size = sizeof(struct virtio_net_hdr);
	}
	priv->header_offset = RTE_PKTMBUF_HEADROOM - priv->header_size;

	config = &priv->conf;
	if (virtio_has_feature(priv, VIRTIO_NET_F_STATUS)) {
		conf_sz += sizeof(config->status);
	} else {
		config->status = 0;
	}
	if (virtio_has_feature(priv, VIRTIO_NET_F_MQ)) {
		conf_sz += sizeof(config->max_virtqueue_pairs);
	} else {
		config->max_virtqueue_pairs = 1;
	}

	virtio_read_conf(priv->ioaddr + VIRTIO_PCI_CONFIG_OFFT,
			 0, (uint8_t*)config, conf_sz);

	eth_dev->data->mac_addrs = priv->mac;

	memcpy(eth_dev->data->mac_addrs->addr_bytes, config->mac, 6);

	priv->qp_num = priv->conf.max_virtqueue_pairs;
	priv->cur_qp = 0;
	return 0;
}

#define VIRTIO_VENDOR_PCI_ID 0x1af4
#define VIRTIO_DEV_ID_START  0x1000
#define VIRTIO_DEV_ID_END    0x103f

static struct rte_pci_id
	pci_id_virtio_net_map[VIRTIO_DEV_ID_END-VIRTIO_DEV_ID_START+2];

static struct eth_driver rte_virtio_net_pmd = {
	{
		.name = "rte_virtio_net_pmd",
		.id_table = pci_id_virtio_net_map,
		.drv_flags = RTE_PCI_DRV_FORCE_UNBIND,
	},
	.eth_dev_init = eth_virtio_dev_init,
	.dev_private_size = sizeof(struct virtio_net_adapter),
};

/* Shared object initializer */
void __attribute__((constructor))
virtio_net_pmd_init(void)
{
	uint16_t i;

	printf("librte_pmd_virtio version " VIRTIO_NET_PMD_VERSION "\n"
	       "\tCopyright 2013-2014 6WIND S.A. provided without warranty.\n");

	/* First thing is to acquire privileges to access ALL ioports. */
	int ret = iopl(3);

	/* We can't be sure we can print anything here. Return on error. */
	if (ret) {
		return;
	}

	/* According to hardcoded values in virtio_pci.h */
	for (i = VIRTIO_DEV_ID_START; i <= VIRTIO_DEV_ID_END; i++) {
		pci_id_virtio_net_map[i - VIRTIO_DEV_ID_START] =
			(struct rte_pci_id){
			  .vendor_id = VIRTIO_VENDOR_PCI_ID,
			  .device_id = i,
			  .subsystem_vendor_id = PCI_ANY_ID,
			  .subsystem_device_id = VIRTIO_ID_NET };
	}
	pci_id_virtio_net_map[i - VIRTIO_DEV_ID_START] =
			(struct rte_pci_id){ .vendor_id = 0 };
	rte_eth_driver_register(&rte_virtio_net_pmd);
}