Repository navigation
Expand file tree
/
Copy pathnetmap_ptnet.c
More file actions
1602 lines (1359 loc) · 40.8 KB
/
Copy pathnetmap_ptnet.c
File metadata and controls
1602 lines (1359 loc) · 40.8 KB
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
/*
* Netmap passthrough interface driver for Linux
* Copyright(c) 2015 Vincenzo Maffione <v.maffione@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
* The full GNU General Public License is included in this distribution in
* the file called "COPYING".
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>
#include <linux/in.h>
#include <linux/interrupt.h>
#include <linux/virtio_net.h>
#include <bsd_glue.h>
#include <net/netmap.h>
#include <dev/netmap/netmap_kern.h>
#include <net/netmap_virt.h>
#include <dev/netmap/netmap_mem2.h>
extern int ptnet_vnet_hdr;
static bool ptnet_gso = true;
module_param(ptnet_gso, bool, 0644);
/* Enable to debug RX-side hangs */
//#define HANGCTRL
#if 0 /* Switch to 1 to enable per-packet logs. */
#define DBG D
#else
#define DBG ND
#endif
#define PTNET_DRV_NAME "ptnet"
struct ptnet_info;
/* Per-ring data structure. */
struct ptnet_queue {
struct ptnet_info *pi;
struct ptnet_ring *ptring;
int kring_id;
u8* __iomem kick;
/* MSI-X interrupt data structures. */
char msix_name[64];
cpumask_var_t msix_affinity_mask;
};
struct ptnet_rx_queue {
struct ptnet_queue q;
struct napi_struct napi;
struct page *rx_pool;
int rx_pool_num;
#ifdef HANGCTRL
#define HANG_INTVAL_MS 3000
struct timer_list hang_timer;
#endif
};
/* Per-adapter data structure. */
struct ptnet_info {
struct net_device *netdev;
struct pci_dev *pdev;
/* Mirrors PTFEAT register content. */
uint32_t ptfeatures;
unsigned int vnet_hdr_len;
/* Access to device memory. */
int bars;
u8* __iomem ioaddr;
#ifndef PTNET_CSB_ALLOC
u8* __iomem csbaddr;
#endif /* !PTNET_CSB_ALLOC */
/* MSI-X interrupt data structures. */
#ifdef NETMAP_LINUX_HAVE_PCI_ENABLE_MSIX
struct msix_entry *msix_entries;
#endif
int num_rings;
int num_tx_rings;
struct ptnet_queue **queues;
struct ptnet_queue **rxqueues;
/* CSB memory to be used for producer/consumer state
* synchronization. */
struct ptnet_csb *csb;
int min_tx_slots;
/* Pass-through netmap adapter used by netmap. */
struct netmap_pt_guest_adapter *ptna;
};
#ifndef NAPI_POLL_WEIGHT
#define NAPI_POLL_WEIGHT 64
#endif
#ifdef HANGCTRL
static void
hang_tmr_callback(unsigned long arg)
{
struct ptnet_rx_queue *prq = (struct ptnet_rx_queue *)arg;
struct ptnet_info *pi = prq->q.pi;
struct netmap_adapter *na = &pi->ptna->dr.up;
struct netmap_kring *kring = na->rx_rings + prq->q.kring_id;
struct netmap_ring *ring = kring->ring;
pr_info("PTNET HANG RX#%d: hwc %u h %u c %u hwt %u t %u"
" rx.guest_need_kick %u\n",
kring->ring_id, kring->nr_hwcur, ring->head, ring->cur,
kring->nr_hwtail, ring->tail, prq->q.ptring->guest_need_kick);
if (mod_timer(&prq->hang_timer,
jiffies + msecs_to_jiffies(HANG_INTVAL_MS))) {
pr_err("%s: mod_timer() failed\n", __func__);
}
}
#endif
static inline void
ptnet_sync_tail(struct ptnet_ring *ptring, struct netmap_kring *kring)
{
struct netmap_ring *ring = kring->ring;
/* Update hwcur and hwtail as known by the host. */
ptnetmap_guest_read_kring_csb(ptring, kring);
/* nm_sync_finalize */
ring->tail = kring->rtail = kring->nr_hwtail;
}
static inline int
ptnet_tx_slots(struct netmap_ring *ring)
{
int space = (int)ring->tail - ring->head;
if (space < 0) {
space += ring->num_slots;
}
return space;
}
struct xmit_copy_args {
struct netmap_adapter *na;
struct netmap_ring *ring;
unsigned int head;
unsigned int lim;
struct netmap_slot *slot;
void *nmbuf;
int nmbuf_bytes;
};
static inline void
ptnet_copy_to_ring(struct xmit_copy_args *a,
void *skbdata, unsigned int skbdata_len)
{
for (;;) {
int copy = min(skbdata_len,
NETMAP_BUF_SIZE(a->na) - a->nmbuf_bytes);
memcpy(a->nmbuf, skbdata, copy);
skbdata += copy;
skbdata_len -= copy;
a->nmbuf += copy;
a->nmbuf_bytes += copy;
if (likely(!skbdata_len)) {
break;
}
a->slot->len = a->nmbuf_bytes;
a->slot->flags = NS_MOREFRAG;
a->head = nm_next(a->head, a->lim);
a->slot = &a->ring->slot[a->head];
a->nmbuf = NMB(a->na, a->slot);
a->nmbuf_bytes = 0;
}
}
#ifdef NETMAP_LINUX_HAVE_XMIT_MORE
#define XMIT_MORE(skb) skb->xmit_more
#else
#define XMIT_MORE(skb) false
#endif
static netdev_tx_t
ptnet_start_xmit(struct sk_buff *skb, struct net_device *netdev)
{
struct ptnet_info *pi = netdev_priv(netdev);
int nfrags = skb_shinfo(skb)->nr_frags;
int queue_idx = skb_get_queue_mapping(skb);
struct ptnet_queue *pq = pi->queues[queue_idx];
struct ptnet_ring *ptring = pq->ptring;
struct netmap_kring *kring;
struct xmit_copy_args a;
int f;
a.na = &pi->ptna->dr.up;
kring = &a.na->tx_rings[queue_idx];
a.ring = kring->ring;
a.lim = kring->nkr_num_slots - 1;
DBG("TX skb len=%d", skb->len);
/* Update hwcur and hwtail (completed TX slots) as known by the host,
* by reading from CSB. */
ptnet_sync_tail(ptring, kring);
if (unlikely(ptnet_tx_slots(a.ring) < pi->min_tx_slots)) {
ND(1, "TX ring unexpected overflow, requeuing");
return NETDEV_TX_BUSY;
}
/* Grab the next available TX slot. */
a.head = a.ring->head;
a.slot = &a.ring->slot[a.head];
a.nmbuf = NMB(a.na, a.slot);
a.nmbuf_bytes = 0;
/* First step: Setup the virtio-net header at the beginning of th
* first slot. */
if (pi->vnet_hdr_len) {
struct virtio_net_hdr_mrg_rxbuf *vh = a.nmbuf;
if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
vh->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
vh->hdr.csum_start = skb_checksum_start_offset(skb);
vh->hdr.csum_offset = skb->csum_offset;
} else {
vh->hdr.flags = 0;
vh->hdr.csum_start = vh->hdr.csum_offset = 0;
}
if (skb_is_gso(skb)) {
vh->hdr.hdr_len = skb_headlen(skb);
vh->hdr.gso_size = skb_shinfo(skb)->gso_size;
if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
vh->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
#ifdef NETMAP_LINUX_HAVE_UFO
} else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
vh->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
#endif /* NETMAP_LINUX_HAVE_UFO */
} else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
vh->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
}
if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) {
vh->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
}
} else {
vh->hdr.hdr_len = vh->hdr.gso_size = 0;
vh->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
}
vh->num_buffers = 0; /* unused */
ND(1, "%s: vnet hdr: flags %x csum_start %u csum_ofs %u hdr_len = "
"%u gso_size %u gso_type %x", __func__, vh->hdr.flags,
vh->hdr.csum_start, vh->hdr.csum_offset, vh->hdr.hdr_len,
vh->hdr.gso_size, vh->hdr.gso_type);
a.nmbuf += sizeof(*vh);
a.nmbuf_bytes += sizeof(*vh);
}
/* Second step: Copy in the linear part of the sk_buff. */
ptnet_copy_to_ring(&a, skb->data, skb_headlen(skb));
/* Third step: Copy in the sk_buffs frags. */
for (f = 0; f < nfrags; f++) {
const struct skb_frag_struct *frag;
frag = &skb_shinfo(skb)->frags[f];
ptnet_copy_to_ring(&a, skb_frag_address(frag),
skb_frag_size(frag));
}
/* Prepare the last slot. */
a.slot->len = a.nmbuf_bytes;
a.slot->flags = 0;
a.ring->head = a.ring->cur = nm_next(a.head, a.lim);
if (skb_shinfo(skb)->nr_frags) {
ND(1, "TX frags #%u lfsz %u tsz %d gso_segs %d gso_size %d", skb_shinfo(skb)->nr_frags,
skb_frag_size(&skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags-1]),
(int)skb->len, skb_shinfo(skb)->gso_segs, skb_shinfo(skb)->gso_size);
}
BUG_ON(a.ring->slot[a.head].flags & NS_MOREFRAG);
/* nm_txsync_prologue */
kring->rcur = a.ring->cur;
kring->rhead = a.ring->head;
if (!XMIT_MORE(skb)) {
/* Tell the host to process the new packets, updating cur and
* head in the CSB. */
ptnetmap_guest_write_kring_csb(ptring, kring->rcur,
kring->rhead);
}
/* Ask for a kick from a guest to the host if needed. */
if (NM_ACCESS_ONCE(ptring->host_need_kick)) {
ptring->sync_flags = NAF_FORCE_RECLAIM;
iowrite32(0, pq->kick);
}
/* No more TX slots for further transmissions. We have to stop the
* qdisc layer and enable notifications. */
if (ptnet_tx_slots(a.ring) < pi->min_tx_slots) {
netif_stop_subqueue(netdev, pq->kring_id);
ptring->guest_need_kick = 1;
/* Double check. */
ptnet_sync_tail(ptring, kring);
if (unlikely(ptnet_tx_slots(a.ring) >= pi->min_tx_slots)) {
/* More TX space came in the meanwhile. */
netif_start_subqueue(netdev, pq->kring_id);
ptring->guest_need_kick = 0;
}
}
pi->netdev->stats.tx_bytes += skb->len;
pi->netdev->stats.tx_packets ++;
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
}
/*
* ptnet_get_stats - Get System Network Statistics
*
* Returns the address of the device statistics structure.
*/
static struct net_device_stats *
ptnet_get_stats(struct net_device *netdev)
{
return &netdev->stats;
}
/*
* ptnet_change_mtu - Change the Maximum Transfer Unit
* @new_mtu: new value for maximum frame size
*
* Returns 0 on success, negative on failure
*/
static int
ptnet_change_mtu(struct net_device *netdev, int new_mtu)
{
pr_info("%s: %s changing MTU from %d to %d\n",
__func__, netdev->name, netdev->mtu, new_mtu);
netdev->mtu = new_mtu;
return 0;
}
/*
* ptnet_tx_intr - Interrupt handler for TX queues
* @data: pointer to a network interface device structure
*/
static irqreturn_t
ptnet_tx_intr(int irq, void *data)
{
struct ptnet_queue *pq = data;
struct net_device *netdev = pq->pi->netdev;
if (netmap_tx_irq(netdev, pq->kring_id)) {
return IRQ_HANDLED;
}
/* Just wake up the qdisc layer, it will flush pending transmissions,
* with the side effect of reclaiming completed TX slots. */
netif_wake_subqueue(netdev, pq->kring_id);
return IRQ_HANDLED;
}
static inline void
ptnet_napi_schedule(struct ptnet_queue *pq)
{
struct ptnet_rx_queue *prq = (struct ptnet_rx_queue *)pq;
/* Disable RX interrupts and schedule NAPI. */
if (likely(napi_schedule_prep(&prq->napi))) {
/* It's good thing to reset rx.guest_need_kick as soon as
* possible. */
pq->ptring->guest_need_kick = 0;
__napi_schedule(&prq->napi);
} else {
/* NAPI is already scheduled and we are ok with it. */
pq->ptring->guest_need_kick = 1;
}
}
/*
* ptnet_rx_intr - Interrupt handler for RX queues
* @data: pointer to a network interface device structure
*/
static irqreturn_t
ptnet_rx_intr(int irq, void *data)
{
ptnet_napi_schedule((struct ptnet_queue *)data);
return IRQ_HANDLED;
}
static struct page *
ptnet_alloc_page(struct ptnet_rx_queue *prq)
{
struct page *p = prq->rx_pool;
if (p) {
prq->rx_pool = (struct page *)(p->private);
prq->rx_pool_num--;
p->private = (unsigned long)NULL;
return p;
}
return alloc_page(GFP_ATOMIC);
}
static inline void
ptnet_rx_pool_refill(struct ptnet_rx_queue *prq)
{
while (prq->rx_pool_num < 2 * MAX_SKB_FRAGS) {
struct page *p = alloc_page(GFP_ATOMIC);
if (!p) {
break;
}
p->private = (unsigned long)(prq->rx_pool);
prq->rx_pool = p;
prq->rx_pool_num ++;
}
}
/*
* ptnet_rx_poll - NAPI RX polling callback
*/
static int
ptnet_rx_poll(struct napi_struct *napi, int budget)
{
struct ptnet_rx_queue *prq = container_of(napi, struct ptnet_rx_queue,
napi);
struct ptnet_queue *pq = (struct ptnet_queue *)prq;
struct ptnet_ring *ptring = pq->ptring;
struct ptnet_info *pi = pq->pi;
struct netmap_adapter *na = &pi->ptna->dr.up;
struct netmap_kring *kring = &na->rx_rings[pq->kring_id];
struct netmap_ring *ring = kring->ring;
unsigned int const lim = kring->nkr_num_slots - 1;
bool have_vnet_hdr = pi->vnet_hdr_len;
unsigned int head = ring->head;
int work_done = 0;
int nm_irq;
nm_irq = netmap_rx_irq(pi->netdev, pq->kring_id, &work_done);
if (nm_irq != NM_IRQ_PASS) {
if (nm_irq == NM_IRQ_COMPLETED) {
napi_complete(napi);
return 1;
} else {
return budget;
}
}
#ifdef HANGCTRL
del_timer(&prq->hang_timer);
#endif
/* Update hwtail, rtail, tail and hwcur to what is known from the host,
* reading from CSB. */
ptnet_sync_tail(ptring, kring);
kring->nr_kflags &= ~NKR_PENDINTR;
/* Import completed RX slots. */
while (work_done < budget && head != ring->tail) {
struct virtio_net_hdr_mrg_rxbuf *vh;
struct netmap_slot *slot;
struct sk_buff *skb;
unsigned int first = head;
struct page *skbpage = NULL;
int skbdata_avail = 0;
void *skbdata = NULL;
int nmbuf_len;
void *nmbuf;
int copy;
int nns = 0;
slot = &ring->slot[head];
nmbuf = NMB(na, slot);
nmbuf_len = slot->len;
vh = nmbuf;
if (likely(have_vnet_hdr)) {
ND(1, "%s: vnet hdr: flags %x csum_start %u "
"csum_ofs %u hdr_len = %u gso_size %u "
"gso_type %x", __func__, vh->hdr.flags,
vh->hdr.csum_start, vh->hdr.csum_offset,
vh->hdr.hdr_len, vh->hdr.gso_size,
vh->hdr.gso_type);
nmbuf += sizeof(*vh);
nmbuf_len -= sizeof(*vh);
}
#ifdef NETMAP_LINUX_HAVE_NAPI_ALLOC_SKB
skb = napi_alloc_skb(napi, nmbuf_len);
#elif defined(NETMAP_LINUX_HAVE_ALLOC_SKB_IP_ALIGN)
skb = netdev_alloc_skb_ip_align(pi->netdev, nmbuf_len);
#else
skb = netdev_alloc_skb(pi->netdev, nmbuf_len);
#endif
if (unlikely(!skb)) {
pr_err("%s: skb allocation failed\n",
__func__);
break;
}
memcpy(skb_put(skb, nmbuf_len), nmbuf, nmbuf_len);
while (slot->flags & NS_MOREFRAG) {
head = nm_next(head, lim);
nns++;
if (unlikely(head == ring->tail)) {
ND(1, "Warning: truncated packet, retrying");
dev_kfree_skb_any(skb);
work_done ++;
pi->netdev->stats.rx_frame_errors ++;
/* Reset head to the beginning of the
* current packet. */
head = first;
goto out_of_slots;
}
slot = &ring->slot[head];
nmbuf = NMB(na, slot);
nmbuf_len = slot->len;
do {
if (!skbdata_avail) {
if (skbpage) {
ND(1, "add f #%u fsz %lu tsz %d", skb_shinfo(skb)->nr_frags,
PAGE_SIZE - skbdata_avail, (int)skb->len);
skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
skbpage, 0, PAGE_SIZE - skbdata_avail
#ifdef NETMAP_LINUX_SKB_ADD_RX_FRAG_6ARGS
, PAGE_SIZE
#endif
);
}
skbpage = ptnet_alloc_page(prq);
if (unlikely(!skbpage)) {
pr_err("%s: pntet_alloc_page() failed\n",
__func__);
break;
}
skbdata = page_address(skbpage);
skbdata_avail = PAGE_SIZE;
}
copy = min(nmbuf_len, skbdata_avail);
memcpy(skbdata, nmbuf, copy);
nmbuf += copy;
nmbuf_len -= copy;
skbdata += copy;
skbdata_avail -= copy;
} while (nmbuf_len);
}
nns++;
if (skbpage) {
skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
skbpage, 0, PAGE_SIZE - skbdata_avail
#ifdef NETMAP_LINUX_SKB_ADD_RX_FRAG_6ARGS
, PAGE_SIZE
#endif
);
ND(1, "RX frags #%u lfsz %lu tsz %d nns %d",
skb_shinfo(skb)->nr_frags,
PAGE_SIZE - skbdata_avail, (int)skb->len, nns);
}
head = nm_next(head, lim);
DBG("RX SKB len=%d", skb->len);
pi->netdev->stats.rx_bytes += skb->len;
pi->netdev->stats.rx_packets ++;
if (likely(have_vnet_hdr && (vh->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))) {
if (unlikely(!skb_partial_csum_set(skb, vh->hdr.csum_start,
vh->hdr.csum_offset))) {
dev_kfree_skb_any(skb);
work_done ++;
pi->netdev->stats.rx_frame_errors ++;
continue;
}
} else if (have_vnet_hdr && (vh->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)) {
skb->ip_summed = CHECKSUM_UNNECESSARY;
}
skb->protocol = eth_type_trans(skb, pi->netdev);
if (likely(have_vnet_hdr && vh->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE)) {
switch (vh->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
case VIRTIO_NET_HDR_GSO_TCPV4:
skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
break;
#ifdef NETMAP_LINUX_HAVE_UFO
case VIRTIO_NET_HDR_GSO_UDP:
skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
break;
#endif /* NETMAP_LINUX_HAVE_UFO */
case VIRTIO_NET_HDR_GSO_TCPV6:
skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
break;
}
if (vh->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) {
skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
}
skb_shinfo(skb)->gso_size = vh->hdr.gso_size;
skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
skb_shinfo(skb)->gso_segs = 0;
}
/*
* We should always use napi_gro_receive() in place of
* netif_receive_skb(). However, currently we have an
* issue (probably due this driver and/or netmap) such
* that when virtio-net header is not null using
* napi_gro_receive() causes sometimes reordering
* of two consecutive sk_buffs. This reordering usually
* causes the network stack to issue a TCP DUPACK, and
* so a retransmission on the sender side. This bug
* seems to disappear if we use netif_receive_skb(skb)
* here.
*
* I've also noticed that when the reordering happens,
* i.e.
*
* skb1, skb2 --> napi_gro_receive() --> skb2, skb1
*
* then it is always true that
*
* (skb1->len == MTU+14) && (skb2->len > skb1->len)
* && skb_is_gso(skb1) == 0 && skb_is_gso(skb2) != 0
*
* where usually MTU == 1500.
*/
if (have_vnet_hdr && vh->hdr.flags) {
netif_receive_skb(skb);
} else {
napi_gro_receive(napi, skb);
}
work_done ++;
}
out_of_slots:
if (work_done < budget) {
/* Budget was not fully consumed, since we have no more
* completed RX slots. We can enable notifications and
* exit polling mode. */
ptring->guest_need_kick = 1;
#ifdef NETMAP_LINUX_HAVE_NAPI_COMPLETE_DONE
napi_complete_done(napi, work_done);
#else
napi_complete(napi);
#endif
/* Double check for more completed RX slots. */
ptnet_sync_tail(ptring, kring);
if (head != ring->tail) {
/* If there is more work to do, disable notifications
* and reschedule. */
ptnet_napi_schedule(pq);
}
#ifdef HANGCTRL
if (mod_timer(&prq->hang_timer,
jiffies + msecs_to_jiffies(HANG_INTVAL_MS))) {
pr_err("%s: mod_timer failed\n", __func__);
}
#endif
}
if (work_done) {
/* Tell the host (through the CSB) about the updated ring->cur and
* ring->head (RX buffer refill).
*/
ring->head = ring->cur = head;
kring->rcur = ring->cur;
kring->rhead = ring->head;
ptnetmap_guest_write_kring_csb(ptring, kring->rcur,
kring->rhead);
/* Kick the host if needed. */
if (NM_ACCESS_ONCE(ptring->host_need_kick)) {
ptring->sync_flags = NAF_FORCE_READ;
iowrite32(0, pq->kick);
}
}
ptnet_rx_pool_refill(prq);
return work_done;
}
#ifdef CONFIG_NET_POLL_CONTROLLER
/* Polling 'interrupt' - used by things like netconsole to send skbs
* without having to re-enable interrupts. It's not called while
* the interrupt routine is executing.
*/
static void
ptnet_netpoll(struct net_device *netdev)
{
struct ptnet_info *pi = netdev_priv(netdev);
struct netmap_adapter *na = &pi->ptna->dr.up;
int i;
for (i = 0; i < na->num_rx_rings; i++) {
ptnet_napi_schedule(pi->rxqueues[i]);
}
}
#endif
unsigned int
ptnet_get_irq_vector(struct ptnet_info *pi, unsigned int i)
{
#ifdef NETMAP_LINUX_HAVE_PCI_ENABLE_MSIX
return pi->msix_entries[i].vector;
#else
return pci_irq_vector(pi->pdev, i);
#endif
}
static int
ptnet_irqs_init(struct ptnet_info *pi)
{
int ret = 0;
int i;
/* Allocate the MSI-X interrupt vectors we need. */
#ifdef NETMAP_LINUX_HAVE_PCI_ENABLE_MSIX
pi->msix_entries = kzalloc(sizeof(*pi->msix_entries) * pi->num_rings,
GFP_KERNEL);
if (!pi->msix_entries) {
pr_err("%s: Failed to allocate msix entires\n", __func__);
return -ENOMEM;
}
for (i=0; i<pi->num_rings; i++) {
pi->msix_entries[i].entry = i;
}
ret = pci_enable_msix(pi->pdev, pi->msix_entries, pi->num_rings);
if (ret == 0) { /* ok */
ret = pi->num_rings;
}
#else
ret = pci_alloc_irq_vectors(pi->pdev, pi->num_rings, pi->num_rings,
PCI_IRQ_MSIX);
#endif
if (ret != pi->num_rings) {
pr_err("%s: Failed to enable msix vectors (%d)\n",
__func__, ret);
goto err_alloc;
}
for (i=0; i<pi->num_rings; i++) {
struct ptnet_queue *pq = pi->queues[i];
memset(&pq->msix_affinity_mask, 0, sizeof(pq->msix_affinity_mask));
if (!alloc_cpumask_var(&pq->msix_affinity_mask, GFP_KERNEL)) {
pr_err("%s: Failed to alloc cpumask var\n", __func__);
goto err_masks;
}
}
for (i=0; i<pi->num_rings; i++) {
struct ptnet_queue *pq = pi->queues[i];
irq_handler_t handler = (i < pi->num_tx_rings) ?
ptnet_tx_intr : ptnet_rx_intr;
unsigned int vector = ptnet_get_irq_vector(pi, i);
snprintf(pq->msix_name, sizeof(pq->msix_name),
"%s-%d", pi->netdev->name, i);
ret = request_irq(vector, handler, 0, pq->msix_name, pq);
if (ret) {
pr_err("%s: Unable to allocate interrupt (%d)\n",
__func__, ret);
goto err_irqs;
}
pr_info("%s: IRQ for ring #%d --> %u, handler %p\n",
__func__, i, vector, handler);
}
return 0;
err_irqs:
for (; i>=0; i--) {
free_irq(ptnet_get_irq_vector(pi, i), pi->queues[i]);
}
i = pi->num_rings-1;
err_masks:
for (; i>=0; i--) {
free_cpumask_var(pi->queues[i]->msix_affinity_mask);
}
err_alloc:
#ifdef NETMAP_LINUX_HAVE_PCI_ENABLE_MSIX
kfree(pi->msix_entries);
#endif
return ret;
}
static void
ptnet_irqs_fini(struct ptnet_info *pi)
{
int i;
for (i=0; i<pi->num_rings; i++) {
struct ptnet_queue *pq = pi->queues[i];
free_irq(ptnet_get_irq_vector(pi, i), pq);
if (pq->msix_affinity_mask) {
free_cpumask_var(pq->msix_affinity_mask);
}
}
#ifdef NETMAP_LINUX_HAVE_PCI_ENABLE_MSIX
pci_disable_msix(pi->pdev);
kfree(pi->msix_entries);
#else
pci_free_irq_vectors(pi->pdev);
#endif
}
static int ptnet_nm_register(struct netmap_adapter *na, int onoff);
/*
* ptnet_open - Called when a network interface is made active
*
* Returns 0 on success, negative value on failure.
* The open entry point is called when a network interface is made
* active by the system (IFF_UP). */
static int
ptnet_open(struct net_device *netdev)
{
struct ptnet_info *pi = netdev_priv(netdev);
struct netmap_adapter *na_dr = &pi->ptna->dr.up;
struct netmap_adapter *na_nm = &pi->ptna->hwup.up;
int ret;
int i;
netmap_update_config(na_dr);
ret = netmap_mem_finalize(na_dr->nm_mem, na_dr);
if (ret) {
pr_err("%s: netmap_mem_finalize() failed\n", __func__);
goto err_mem_finalize;
}
if (pi->ptna->backend_regifs == 0) {
ret = ptnet_nm_krings_create(na_nm);
if (ret) {
pr_err("%s: ptnet_nm_krings_create() failed\n",
__func__);
goto err_mem_finalize;
}
ret = netmap_mem_rings_create(na_dr);
if (ret) {
pr_err("%s: netmap_mem_rings_create() failed\n",
__func__);
goto err_rings_create;
}
ret = netmap_mem_get_lut(na_dr->nm_mem, &na_dr->na_lut);
if (ret) {
pr_err("%s: netmap_mem_get_lut() failed\n", __func__);
goto err_get_lut;
}
}
ret = ptnet_nm_register(na_dr, 1 /* on */);
if (ret) {
goto err_register;
}
{
unsigned int nm_buf_size = NETMAP_BUF_SIZE(na_dr);
BUG_ON(nm_buf_size == 0);
pi->min_tx_slots = 65536 / nm_buf_size + 2;
pr_info("%s: min_tx_slots = %u\n", __func__, pi->min_tx_slots);
}
netif_tx_start_all_queues(netdev);
for (i = 0; i < na_dr->num_rx_rings; i++){
struct ptnet_rx_queue *prq = (struct ptnet_rx_queue *)
pi->rxqueues[i];
#ifdef HANGCTRL
setup_timer(&prq->hang_timer, &hang_tmr_callback,
(unsigned long)prq);
if (mod_timer(&prq->hang_timer,
jiffies + msecs_to_jiffies(HANG_INTVAL_MS))) {
pr_err("%s: mod_timer failed\n", __func__);
}
#endif
prq->rx_pool = NULL;
prq->rx_pool_num = 0;
napi_enable(&prq->napi);
/* There may be pending packets received in netmap mode while
* the interface was down. Schedule NAPI to flush packets that
* are pending in the RX ring. We won't receive further
* interrupts until the pending ones will be processed. */
pr_info("%s: Schedule NAPI to flush RX ring #%d\n",
__func__, i);
ptnet_napi_schedule(&prq->q);
}
return 0;
err_register:
memset(&na_dr->na_lut, 0, sizeof(na_dr->na_lut));
err_get_lut:
netmap_mem_rings_delete(na_dr);
err_rings_create:
ptnet_nm_krings_delete(na_nm);
err_mem_finalize:
return -ret;
}
/*
* ptnet_close - Disables a network interface
*
* Returns 0, this is not allowed to fail.
* The close entry point is called when an interface is de-activated
* by the OS.
*/
static int
ptnet_close(struct net_device *netdev)
{
struct ptnet_info *pi = netdev_priv(netdev);
struct netmap_adapter *na_dr = &pi->ptna->dr.up;
struct netmap_adapter *na_nm = &pi->ptna->hwup.up;
int i;
netif_tx_stop_all_queues(netdev);
for (i = 0; i < na_dr->num_rx_rings; i++){
struct ptnet_rx_queue *prq = (struct ptnet_rx_queue *)
pi->rxqueues[i];
#ifdef HANGCTRL
del_timer(&prq->hang_timer);
#endif
/* Stop napi. */
napi_disable(&prq->napi);
/* Free RX pool. */
while (prq->rx_pool) {
struct page *p = prq->rx_pool;
prq->rx_pool = (struct page *)(p->private);
p->private = (unsigned long)NULL;
put_page(p);