forked from luigirizzo/netmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetmap_pt.c
More file actions
1453 lines (1222 loc) · 40 KB
/
netmap_pt.c
File metadata and controls
1453 lines (1222 loc) · 40 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
/*
* Copyright (C) 2015 Stefano Garzarella
* Copyright (C) 2016 Vincenzo Maffione
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
/*
* common headers
*/
#if defined(__FreeBSD__)
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/selinfo.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <machine/bus.h>
//#define usleep_range(_1, _2)
#define usleep_range(_1, _2) \
pause_sbt("ptnetmap-sleep", SBT_1US * _1, SBT_1US * 1, C_ABSOLUTE)
#elif defined(linux)
#include <bsd_glue.h>
#endif
#include <net/netmap.h>
#include <dev/netmap/netmap_kern.h>
#include <net/netmap_virt.h>
#include <dev/netmap/netmap_mem2.h>
#ifdef WITH_PTNETMAP_HOST
/* RX cycle without receive any packets */
#define PTN_RX_DRY_CYCLES_MAX 10
/* Limit Batch TX to half ring.
* Currently disabled, since it does not manage NS_MOREFRAG, which
* results in random drops in the VALE txsync. */
//#define PTN_TX_BATCH_LIM(_n) ((_n >> 1))
//#define BUSY_WAIT
#define NETMAP_PT_DEBUG /* Enables communication debugging. */
#ifdef NETMAP_PT_DEBUG
#define DBG(x) x
#else
#define DBG(x)
#endif
#undef RATE
//#define RATE /* Enables communication statistics. */
#ifdef RATE
#define IFRATE(x) x
struct rate_batch_stats {
unsigned long sync;
unsigned long sync_dry;
unsigned long pkt;
};
struct rate_stats {
unsigned long gtxk; /* Guest --> Host Tx kicks. */
unsigned long grxk; /* Guest --> Host Rx kicks. */
unsigned long htxk; /* Host --> Guest Tx kicks. */
unsigned long hrxk; /* Host --> Guest Rx Kicks. */
unsigned long btxwu; /* Backend Tx wake-up. */
unsigned long brxwu; /* Backend Rx wake-up. */
struct rate_batch_stats txbs;
struct rate_batch_stats rxbs;
};
struct rate_context {
struct timer_list timer;
struct rate_stats new;
struct rate_stats old;
};
#define RATE_PERIOD 2
static void
rate_callback(unsigned long arg)
{
struct rate_context * ctx = (struct rate_context *)arg;
struct rate_stats cur = ctx->new;
struct rate_batch_stats *txbs = &cur.txbs;
struct rate_batch_stats *rxbs = &cur.rxbs;
struct rate_batch_stats *txbs_old = &ctx->old.txbs;
struct rate_batch_stats *rxbs_old = &ctx->old.rxbs;
uint64_t tx_batch, rx_batch;
unsigned long txpkts, rxpkts;
unsigned long gtxk, grxk;
int r;
txpkts = txbs->pkt - txbs_old->pkt;
rxpkts = rxbs->pkt - rxbs_old->pkt;
tx_batch = ((txbs->sync - txbs_old->sync) > 0) ?
txpkts / (txbs->sync - txbs_old->sync): 0;
rx_batch = ((rxbs->sync - rxbs_old->sync) > 0) ?
rxpkts / (rxbs->sync - rxbs_old->sync): 0;
/* Fix-up gtxk and grxk estimates. */
gtxk = (cur.gtxk - ctx->old.gtxk) - (cur.btxwu - ctx->old.btxwu);
grxk = (cur.grxk - ctx->old.grxk) - (cur.brxwu - ctx->old.brxwu);
printk("txpkts = %lu Hz\n", txpkts/RATE_PERIOD);
printk("gtxk = %lu Hz\n", gtxk/RATE_PERIOD);
printk("htxk = %lu Hz\n", (cur.htxk - ctx->old.htxk)/RATE_PERIOD);
printk("btxw = %lu Hz\n", (cur.btxwu - ctx->old.btxwu)/RATE_PERIOD);
printk("rxpkts = %lu Hz\n", rxpkts/RATE_PERIOD);
printk("grxk = %lu Hz\n", grxk/RATE_PERIOD);
printk("hrxk = %lu Hz\n", (cur.hrxk - ctx->old.hrxk)/RATE_PERIOD);
printk("brxw = %lu Hz\n", (cur.brxwu - ctx->old.brxwu)/RATE_PERIOD);
printk("txbatch = %llu avg\n", tx_batch);
printk("rxbatch = %llu avg\n", rx_batch);
printk("\n");
ctx->old = cur;
r = mod_timer(&ctx->timer, jiffies +
msecs_to_jiffies(RATE_PERIOD * 1000));
if (unlikely(r))
D("[ptnetmap] Error: mod_timer()\n");
}
static void
rate_batch_stats_update(struct rate_batch_stats *bf, uint32_t pre_tail,
uint32_t act_tail, uint32_t num_slots)
{
int n = (int)act_tail - pre_tail;
if (n) {
if (n < 0)
n += num_slots;
bf->sync++;
bf->pkt += n;
} else {
bf->sync_dry++;
}
}
#else /* !RATE */
#define IFRATE(x)
#endif /* RATE */
struct ptnetmap_state {
/* Kthreads. */
struct nm_kthread **kthreads;
/* Shared memory with the guest (TX/RX) */
struct ptnet_ring __user *ptrings;
bool stopped;
/* Netmap adapter wrapping the backend. */
struct netmap_pt_host_adapter *pth_na;
IFRATE(struct rate_context rate_ctx;)
};
static inline void
ptnetmap_kring_dump(const char *title, const struct netmap_kring *kring)
{
RD(1, "%s - name: %s hwcur: %d hwtail: %d rhead: %d rcur: %d \
rtail: %d head: %d cur: %d tail: %d",
title, kring->name, kring->nr_hwcur,
kring->nr_hwtail, kring->rhead, kring->rcur, kring->rtail,
kring->ring->head, kring->ring->cur, kring->ring->tail);
}
/*
* TX functions to set/get and to handle host/guest kick.
*/
/* Enable or disable guest --> host kicks. */
static inline void
ptring_kick_enable(struct ptnet_ring __user *ptring, uint32_t val)
{
CSB_WRITE(ptring, host_need_kick, val);
}
/* Are guest interrupt enabled or disabled? */
static inline uint32_t
ptring_intr_enabled(struct ptnet_ring __user *ptring)
{
uint32_t v;
CSB_READ(ptring, guest_need_kick, v);
return v;
}
/* Enable or disable guest interrupts. */
static inline void
ptring_intr_enable(struct ptnet_ring __user *ptring, uint32_t val)
{
CSB_WRITE(ptring, guest_need_kick, val);
}
/* Handle TX events: from the guest or from the backend */
static void
ptnetmap_tx_handler(void *data)
{
struct netmap_kring *kring = data;
struct netmap_pt_host_adapter *pth_na =
(struct netmap_pt_host_adapter *)kring->na->na_private;
struct ptnetmap_state *ptns = pth_na->ptns;
struct ptnet_ring __user *ptring;
struct netmap_ring shadow_ring; /* shadow copy of the netmap_ring */
bool more_txspace = false;
struct nm_kthread *kth;
uint32_t num_slots;
int batch;
IFRATE(uint32_t pre_tail);
if (unlikely(!ptns)) {
D("ERROR ptnetmap state is NULL");
return;
}
if (unlikely(ptns->stopped)) {
RD(1, "backend netmap is being stopped");
return;
}
if (unlikely(nm_kr_tryget(kring, 1, NULL))) {
D("ERROR nm_kr_tryget()");
return;
}
/* This is a guess, to be fixed in the rate callback. */
IFRATE(ptns->rate_ctx.new.gtxk++);
/* Get TX ptring pointer from the CSB. */
ptring = ptns->ptrings + kring->ring_id;
kth = ptns->kthreads[kring->ring_id];
num_slots = kring->nkr_num_slots;
shadow_ring.head = kring->rhead;
shadow_ring.cur = kring->rcur;
/* Disable guest --> host notifications. */
ptring_kick_enable(ptring, 0);
/* Copy the guest kring pointers from the CSB */
ptnetmap_host_read_kring_csb(ptring, &shadow_ring, num_slots);
for (;;) {
/* If guest moves ahead too fast, let's cut the move so
* that we don't exceed our batch limit. */
batch = shadow_ring.head - kring->nr_hwcur;
if (batch < 0)
batch += num_slots;
#ifdef PTN_TX_BATCH_LIM
if (batch > PTN_TX_BATCH_LIM(num_slots)) {
uint32_t head_lim = kring->nr_hwcur + PTN_TX_BATCH_LIM(num_slots);
if (head_lim >= num_slots)
head_lim -= num_slots;
ND(1, "batch: %d head: %d head_lim: %d", batch, shadow_ring.head,
head_lim);
shadow_ring.head = head_lim;
batch = PTN_TX_BATCH_LIM(num_slots);
}
#endif /* PTN_TX_BATCH_LIM */
if (nm_kr_txspace(kring) <= (num_slots >> 1)) {
shadow_ring.flags |= NAF_FORCE_RECLAIM;
}
/* Netmap prologue */
shadow_ring.tail = kring->rtail;
if (unlikely(nm_txsync_prologue(kring, &shadow_ring) >= num_slots)) {
/* Reinit ring and enable notifications. */
netmap_ring_reinit(kring);
ptring_kick_enable(ptring, 1);
break;
}
if (unlikely(netmap_verbose & NM_VERB_TXSYNC)) {
ptnetmap_kring_dump("pre txsync", kring);
}
IFRATE(pre_tail = kring->rtail);
if (unlikely(kring->nm_sync(kring, shadow_ring.flags))) {
/* Reenable notifications. */
ptring_kick_enable(ptring, 1);
D("ERROR txsync()");
break;
}
/*
* Finalize
* Copy host hwcur and hwtail into the CSB for the guest sync(), and
* do the nm_sync_finalize.
*/
ptnetmap_host_write_kring_csb(ptring, kring->nr_hwcur,
kring->nr_hwtail);
if (kring->rtail != kring->nr_hwtail) {
/* Some more room available in the parent adapter. */
kring->rtail = kring->nr_hwtail;
more_txspace = true;
}
IFRATE(rate_batch_stats_update(&ptns->rate_ctx.new.txbs, pre_tail,
kring->rtail, num_slots));
if (unlikely(netmap_verbose & NM_VERB_TXSYNC)) {
ptnetmap_kring_dump("post txsync", kring);
}
#ifndef BUSY_WAIT
/* Interrupt the guest if needed. */
if (more_txspace && ptring_intr_enabled(ptring)) {
/* Disable guest kick to avoid sending unnecessary kicks */
ptring_intr_enable(ptring, 0);
nm_os_kthread_send_irq(kth);
IFRATE(ptns->rate_ctx.new.htxk++);
more_txspace = false;
}
#endif
/* Read CSB to see if there is more work to do. */
ptnetmap_host_read_kring_csb(ptring, &shadow_ring, num_slots);
#ifndef BUSY_WAIT
if (shadow_ring.head == kring->rhead) {
/*
* No more packets to transmit. We enable notifications and
* go to sleep, waiting for a kick from the guest when new
* new slots are ready for transmission.
*/
usleep_range(1,1);
/* Reenable notifications. */
ptring_kick_enable(ptring, 1);
/* Doublecheck. */
ptnetmap_host_read_kring_csb(ptring, &shadow_ring, num_slots);
if (shadow_ring.head != kring->rhead) {
/* We won the race condition, there are more packets to
* transmit. Disable notifications and do another cycle */
ptring_kick_enable(ptring, 0);
continue;
}
break;
}
if (nm_kr_txempty(kring)) {
/* No more available TX slots. We stop waiting for a notification
* from the backend (netmap_tx_irq). */
ND(1, "TX ring");
break;
}
#endif
if (unlikely(ptns->stopped)) {
D("backend netmap is being stopped");
break;
}
}
nm_kr_put(kring);
if (more_txspace && ptring_intr_enabled(ptring)) {
ptring_intr_enable(ptring, 0);
nm_os_kthread_send_irq(kth);
IFRATE(ptns->rate_ctx.new.htxk++);
}
}
/*
* We need RX kicks from the guest when (tail == head-1), where we wait
* for the guest to refill.
*/
#ifndef BUSY_WAIT
static inline int
ptnetmap_norxslots(struct netmap_kring *kring, uint32_t g_head)
{
return (NM_ACCESS_ONCE(kring->nr_hwtail) == nm_prev(g_head,
kring->nkr_num_slots - 1));
}
#endif /* !BUSY_WAIT */
/* Handle RX events: from the guest or from the backend */
static void
ptnetmap_rx_handler(void *data)
{
struct netmap_kring *kring = data;
struct netmap_pt_host_adapter *pth_na =
(struct netmap_pt_host_adapter *)kring->na->na_private;
struct ptnetmap_state *ptns = pth_na->ptns;
struct ptnet_ring __user *ptring;
struct netmap_ring shadow_ring; /* shadow copy of the netmap_ring */
struct nm_kthread *kth;
uint32_t num_slots;
int dry_cycles = 0;
bool some_recvd = false;
IFRATE(uint32_t pre_tail);
if (unlikely(!ptns || !ptns->pth_na)) {
D("ERROR ptnetmap state %p, ptnetmap host adapter %p", ptns,
ptns ? ptns->pth_na : NULL);
return;
}
if (unlikely(ptns->stopped)) {
RD(1, "backend netmap is being stopped");
return;
}
if (unlikely(nm_kr_tryget(kring, 1, NULL))) {
D("ERROR nm_kr_tryget()");
return;
}
/* This is a guess, to be fixed in the rate callback. */
IFRATE(ptns->rate_ctx.new.grxk++);
/* Get RX ptring pointer from the CSB. */
ptring = ptns->ptrings + (pth_na->up.num_tx_rings + kring->ring_id);
kth = ptns->kthreads[pth_na->up.num_tx_rings + kring->ring_id];
num_slots = kring->nkr_num_slots;
shadow_ring.head = kring->rhead;
shadow_ring.cur = kring->rcur;
/* Disable notifications. */
ptring_kick_enable(ptring, 0);
/* Copy the guest kring pointers from the CSB */
ptnetmap_host_read_kring_csb(ptring, &shadow_ring, num_slots);
for (;;) {
uint32_t hwtail;
/* Netmap prologue */
shadow_ring.tail = kring->rtail;
if (unlikely(nm_rxsync_prologue(kring, &shadow_ring) >= num_slots)) {
/* Reinit ring and enable notifications. */
netmap_ring_reinit(kring);
ptring_kick_enable(ptring, 1);
break;
}
if (unlikely(netmap_verbose & NM_VERB_RXSYNC)) {
ptnetmap_kring_dump("pre rxsync", kring);
}
IFRATE(pre_tail = kring->rtail);
if (unlikely(kring->nm_sync(kring, shadow_ring.flags))) {
/* Reenable notifications. */
ptring_kick_enable(ptring, 1);
D("ERROR rxsync()");
break;
}
/*
* Finalize
* Copy host hwcur and hwtail into the CSB for the guest sync()
*/
hwtail = NM_ACCESS_ONCE(kring->nr_hwtail);
ptnetmap_host_write_kring_csb(ptring, kring->nr_hwcur, hwtail);
if (kring->rtail != hwtail) {
kring->rtail = hwtail;
some_recvd = true;
dry_cycles = 0;
} else {
dry_cycles++;
}
IFRATE(rate_batch_stats_update(&ptns->rate_ctx.new.rxbs, pre_tail,
kring->rtail, num_slots));
if (unlikely(netmap_verbose & NM_VERB_RXSYNC)) {
ptnetmap_kring_dump("post rxsync", kring);
}
#ifndef BUSY_WAIT
/* Interrupt the guest if needed. */
if (some_recvd && ptring_intr_enabled(ptring)) {
/* Disable guest kick to avoid sending unnecessary kicks */
ptring_intr_enable(ptring, 0);
nm_os_kthread_send_irq(kth);
IFRATE(ptns->rate_ctx.new.hrxk++);
some_recvd = false;
}
#endif
/* Read CSB to see if there is more work to do. */
ptnetmap_host_read_kring_csb(ptring, &shadow_ring, num_slots);
#ifndef BUSY_WAIT
if (ptnetmap_norxslots(kring, shadow_ring.head)) {
/*
* No more slots available for reception. We enable notification and
* go to sleep, waiting for a kick from the guest when new receive
* slots are available.
*/
usleep_range(1,1);
/* Reenable notifications. */
ptring_kick_enable(ptring, 1);
/* Doublecheck. */
ptnetmap_host_read_kring_csb(ptring, &shadow_ring, num_slots);
if (!ptnetmap_norxslots(kring, shadow_ring.head)) {
/* We won the race condition, more slots are available. Disable
* notifications and do another cycle. */
ptring_kick_enable(ptring, 0);
continue;
}
break;
}
hwtail = NM_ACCESS_ONCE(kring->nr_hwtail);
if (unlikely(hwtail == kring->rhead ||
dry_cycles >= PTN_RX_DRY_CYCLES_MAX)) {
/* No more packets to be read from the backend. We stop and
* wait for a notification from the backend (netmap_rx_irq). */
ND(1, "nr_hwtail: %d rhead: %d dry_cycles: %d",
hwtail, kring->rhead, dry_cycles);
break;
}
#endif
if (unlikely(ptns->stopped)) {
D("backend netmap is being stopped");
break;
}
}
nm_kr_put(kring);
/* Interrupt the guest if needed. */
if (some_recvd && ptring_intr_enabled(ptring)) {
ptring_intr_enable(ptring, 0);
nm_os_kthread_send_irq(kth);
IFRATE(ptns->rate_ctx.new.hrxk++);
}
}
#ifdef NETMAP_PT_DEBUG
static void
ptnetmap_print_configuration(struct ptnetmap_cfg *cfg)
{
int k;
D("ptnetmap configuration:");
D(" CSB ptrings @%p, num_rings=%u, cfgtype %08x", cfg->ptrings,
cfg->num_rings, cfg->cfgtype);
for (k = 0; k < cfg->num_rings; k++) {
switch (cfg->cfgtype) {
case PTNETMAP_CFGTYPE_QEMU: {
struct ptnetmap_cfgentry_qemu *e =
(struct ptnetmap_cfgentry_qemu *)(cfg+1) + k;
D(" ring #%d: ioeventfd=%lu, irqfd=%lu", k,
(unsigned long)e->ioeventfd,
(unsigned long)e->irqfd);
break;
}
case PTNETMAP_CFGTYPE_BHYVE:
{
struct ptnetmap_cfgentry_bhyve *e =
(struct ptnetmap_cfgentry_bhyve *)(cfg+1) + k;
D(" ring #%d: wchan=%lu, ioctl_fd=%lu, "
"ioctl_cmd=%lu, msix_msg_data=%lu, msix_addr=%lu",
k, (unsigned long)e->wchan,
(unsigned long)e->ioctl_fd,
(unsigned long)e->ioctl_cmd,
(unsigned long)e->ioctl_data.msg_data,
(unsigned long)e->ioctl_data.addr);
break;
}
}
}
}
#endif /* NETMAP_PT_DEBUG */
/* Copy actual state of the host ring into the CSB for the guest init */
static int
ptnetmap_kring_snapshot(struct netmap_kring *kring, struct ptnet_ring __user *ptring)
{
if(CSB_WRITE(ptring, head, kring->rhead))
goto err;
if(CSB_WRITE(ptring, cur, kring->rcur))
goto err;
if(CSB_WRITE(ptring, hwcur, kring->nr_hwcur))
goto err;
if(CSB_WRITE(ptring, hwtail, NM_ACCESS_ONCE(kring->nr_hwtail)))
goto err;
DBG(ptnetmap_kring_dump("ptnetmap_kring_snapshot", kring);)
return 0;
err:
return EFAULT;
}
static struct netmap_kring *
ptnetmap_kring(struct netmap_pt_host_adapter *pth_na, int k)
{
if (k < pth_na->up.num_tx_rings) {
return pth_na->up.tx_rings + k;
}
return pth_na->up.rx_rings + k - pth_na->up.num_tx_rings;
}
static int
ptnetmap_krings_snapshot(struct netmap_pt_host_adapter *pth_na)
{
struct ptnetmap_state *ptns = pth_na->ptns;
struct netmap_kring *kring;
unsigned int num_rings;
int err = 0, k;
num_rings = pth_na->up.num_tx_rings +
pth_na->up.num_rx_rings;
for (k = 0; k < num_rings; k++) {
kring = ptnetmap_kring(pth_na, k);
err |= ptnetmap_kring_snapshot(kring, ptns->ptrings + k);
}
return err;
}
/*
* Functions to create, start and stop the kthreads
*/
static int
ptnetmap_create_kthreads(struct netmap_pt_host_adapter *pth_na,
struct ptnetmap_cfg *cfg)
{
struct ptnetmap_state *ptns = pth_na->ptns;
struct nm_kthread_cfg nmk_cfg;
unsigned int num_rings;
uint8_t *cfg_entries = (uint8_t *)(cfg + 1);
int k;
num_rings = pth_na->up.num_tx_rings +
pth_na->up.num_rx_rings;
for (k = 0; k < num_rings; k++) {
nmk_cfg.attach_user = 1; /* attach kthread to user process */
nmk_cfg.worker_private = ptnetmap_kring(pth_na, k);
nmk_cfg.type = k;
if (k < pth_na->up.num_tx_rings) {
nmk_cfg.worker_fn = ptnetmap_tx_handler;
} else {
nmk_cfg.worker_fn = ptnetmap_rx_handler;
}
ptns->kthreads[k] = nm_os_kthread_create(&nmk_cfg,
cfg->cfgtype, cfg_entries + k * cfg->entry_size);
if (ptns->kthreads[k] == NULL) {
goto err;
}
}
return 0;
err:
for (k = 0; k < num_rings; k++) {
if (ptns->kthreads[k]) {
nm_os_kthread_delete(ptns->kthreads[k]);
ptns->kthreads[k] = NULL;
}
}
return EFAULT;
}
static int
ptnetmap_start_kthreads(struct netmap_pt_host_adapter *pth_na)
{
struct ptnetmap_state *ptns = pth_na->ptns;
int num_rings;
int error;
int k;
if (!ptns) {
D("BUG ptns is NULL");
return EFAULT;
}
ptns->stopped = false;
num_rings = ptns->pth_na->up.num_tx_rings +
ptns->pth_na->up.num_rx_rings;
for (k = 0; k < num_rings; k++) {
//nm_os_kthread_set_affinity(ptns->kthreads[k], xxx);
error = nm_os_kthread_start(ptns->kthreads[k]);
if (error) {
return error;
}
}
return 0;
}
static void
ptnetmap_stop_kthreads(struct netmap_pt_host_adapter *pth_na)
{
struct ptnetmap_state *ptns = pth_na->ptns;
int num_rings;
int k;
if (!ptns) {
/* Nothing to do. */
return;
}
ptns->stopped = true;
num_rings = ptns->pth_na->up.num_tx_rings +
ptns->pth_na->up.num_rx_rings;
for (k = 0; k < num_rings; k++) {
nm_os_kthread_stop(ptns->kthreads[k]);
}
}
static struct ptnetmap_cfg *
ptnetmap_read_cfg(struct nmreq *nmr)
{
uintptr_t *nmr_ptncfg = (uintptr_t *)&nmr->nr_arg1;
struct ptnetmap_cfg *cfg;
struct ptnetmap_cfg tmp;
size_t cfglen;
if (copyin((const void *)*nmr_ptncfg, &tmp, sizeof(tmp))) {
D("Partial copyin() failed");
return NULL;
}
cfglen = sizeof(tmp) + tmp.num_rings * tmp.entry_size;
cfg = nm_os_malloc(cfglen);
if (!cfg) {
return NULL;
}
if (copyin((const void *)*nmr_ptncfg, cfg, cfglen)) {
D("Full copyin() failed");
nm_os_free(cfg);
return NULL;
}
return cfg;
}
static int nm_unused_notify(struct netmap_kring *, int);
static int nm_pt_host_notify(struct netmap_kring *, int);
/* Create ptnetmap state and switch parent adapter to ptnetmap mode. */
static int
ptnetmap_create(struct netmap_pt_host_adapter *pth_na,
struct ptnetmap_cfg *cfg)
{
struct ptnetmap_state *ptns;
unsigned int num_rings;
int ret, i;
/* Check if ptnetmap state is already there. */
if (pth_na->ptns) {
D("ERROR adapter %p already in ptnetmap mode", pth_na->parent);
return EINVAL;
}
num_rings = pth_na->up.num_tx_rings + pth_na->up.num_rx_rings;
if (num_rings != cfg->num_rings) {
D("ERROR configuration mismatch, expected %u rings, found %u",
num_rings, cfg->num_rings);
return EINVAL;
}
ptns = nm_os_malloc(sizeof(*ptns) + num_rings * sizeof(*ptns->kthreads));
if (!ptns) {
return ENOMEM;
}
ptns->kthreads = (struct nm_kthread **)(ptns + 1);
ptns->stopped = true;
/* Cross-link data structures. */
pth_na->ptns = ptns;
ptns->pth_na = pth_na;
/* Store the CSB address provided by the hypervisor. */
ptns->ptrings = cfg->ptrings;
DBG(ptnetmap_print_configuration(cfg));
/* Create kthreads */
if ((ret = ptnetmap_create_kthreads(pth_na, cfg))) {
D("ERROR ptnetmap_create_kthreads()");
goto err;
}
/* Copy krings state into the CSB for the guest initialization */
if ((ret = ptnetmap_krings_snapshot(pth_na))) {
D("ERROR ptnetmap_krings_snapshot()");
goto err;
}
/* Overwrite parent nm_notify krings callback. */
pth_na->parent->na_private = pth_na;
pth_na->parent_nm_notify = pth_na->parent->nm_notify;
pth_na->parent->nm_notify = nm_unused_notify;
for (i = 0; i < pth_na->parent->num_rx_rings; i++) {
pth_na->up.rx_rings[i].save_notify =
pth_na->up.rx_rings[i].nm_notify;
pth_na->up.rx_rings[i].nm_notify = nm_pt_host_notify;
}
for (i = 0; i < pth_na->parent->num_tx_rings; i++) {
pth_na->up.tx_rings[i].save_notify =
pth_na->up.tx_rings[i].nm_notify;
pth_na->up.tx_rings[i].nm_notify = nm_pt_host_notify;
}
#ifdef RATE
memset(&ptns->rate_ctx, 0, sizeof(ptns->rate_ctx));
setup_timer(&ptns->rate_ctx.timer, &rate_callback,
(unsigned long)&ptns->rate_ctx);
if (mod_timer(&ptns->rate_ctx.timer, jiffies + msecs_to_jiffies(1500)))
D("[ptn] Error: mod_timer()\n");
#endif
DBG(D("[%s] ptnetmap configuration DONE", pth_na->up.name));
return 0;
err:
pth_na->ptns = NULL;
nm_os_free(ptns);
return ret;
}
/* Switch parent adapter back to normal mode and destroy
* ptnetmap state. */
static void
ptnetmap_delete(struct netmap_pt_host_adapter *pth_na)
{
struct ptnetmap_state *ptns = pth_na->ptns;
int num_rings;
int i;
if (!ptns) {
/* Nothing to do. */
return;
}
/* Restore parent adapter callbacks. */
pth_na->parent->nm_notify = pth_na->parent_nm_notify;
pth_na->parent->na_private = NULL;
for (i = 0; i < pth_na->parent->num_rx_rings; i++) {
pth_na->up.rx_rings[i].nm_notify =
pth_na->up.rx_rings[i].save_notify;
pth_na->up.rx_rings[i].save_notify = NULL;
}
for (i = 0; i < pth_na->parent->num_tx_rings; i++) {
pth_na->up.tx_rings[i].nm_notify =
pth_na->up.tx_rings[i].save_notify;
pth_na->up.tx_rings[i].save_notify = NULL;
}
/* Delete kthreads. */
num_rings = ptns->pth_na->up.num_tx_rings +
ptns->pth_na->up.num_rx_rings;
for (i = 0; i < num_rings; i++) {
nm_os_kthread_delete(ptns->kthreads[i]);
ptns->kthreads[i] = NULL;
}
IFRATE(del_timer(&ptns->rate_ctx.timer));
nm_os_free(ptns);
pth_na->ptns = NULL;
DBG(D("[%s] ptnetmap deleted", pth_na->up.name));
}
/*
* Called by netmap_ioctl().
* Operation is indicated in nmr->nr_cmd.
*
* Called without NMG_LOCK.
*/
int
ptnetmap_ctl(struct nmreq *nmr, struct netmap_adapter *na)
{
struct netmap_pt_host_adapter *pth_na;
struct ptnetmap_cfg *cfg;
char *name;
int cmd, error = 0;
name = nmr->nr_name;
cmd = nmr->nr_cmd;
DBG(D("name: %s", name));
if (!nm_ptnetmap_host_on(na)) {
D("ERROR Netmap adapter %p is not a ptnetmap host adapter", na);
error = ENXIO;
goto done;
}
pth_na = (struct netmap_pt_host_adapter *)na;
NMG_LOCK();
switch (cmd) {
case NETMAP_PT_HOST_CREATE:
/* Read hypervisor configuration from userspace. */
cfg = ptnetmap_read_cfg(nmr);
if (!cfg)
break;
/* Create ptnetmap state (kthreads, ...) and switch parent
* adapter to ptnetmap mode. */
error = ptnetmap_create(pth_na, cfg);
nm_os_free(cfg);
if (error)
break;
/* Start kthreads. */
error = ptnetmap_start_kthreads(pth_na);
if (error)
ptnetmap_delete(pth_na);
break;
case NETMAP_PT_HOST_DELETE:
/* Stop kthreads. */
ptnetmap_stop_kthreads(pth_na);
/* Switch parent adapter back to normal mode and destroy
* ptnetmap state (kthreads, ...). */
ptnetmap_delete(pth_na);
break;
default:
D("ERROR invalid cmd (nmr->nr_cmd) (0x%x)", cmd);
error = EINVAL;
break;
}
NMG_UNLOCK();
done:
return error;
}
/* nm_notify callbacks for ptnetmap */
static int
nm_pt_host_notify(struct netmap_kring *kring, int flags)
{
struct netmap_adapter *na = kring->na;
struct netmap_pt_host_adapter *pth_na =
(struct netmap_pt_host_adapter *)na->na_private;
struct ptnetmap_state *ptns;
int k;
/* First check that the passthrough port is not being destroyed. */
if (unlikely(!pth_na)) {
return NM_IRQ_COMPLETED;
}
ptns = pth_na->ptns;
if (unlikely(!ptns || ptns->stopped)) {
return NM_IRQ_COMPLETED;
}
k = kring->ring_id;
/* Notify kthreads (wake up if needed) */
if (kring->tx == NR_TX) {
ND(1, "TX backend irq");
IFRATE(ptns->rate_ctx.new.btxwu++);
} else {
k += pth_na->up.num_tx_rings;
ND(1, "RX backend irq");
IFRATE(ptns->rate_ctx.new.brxwu++);
}
nm_os_kthread_wakeup_worker(ptns->kthreads[k]);
return NM_IRQ_COMPLETED;
}