forked from luigirizzo/netmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackmap.c
More file actions
1402 lines (1249 loc) · 35.3 KB
/
stackmap.c
File metadata and controls
1402 lines (1249 loc) · 35.3 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) 2017 NetApp. Inc.
* Copyright (C) 2017 NEC Europe Ltd.
* Copyright (C) 2017 Michio Honda
* 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.
*/
/*
* common headers
*/
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/param.h> /* defines used in kernel.h */
#include <sys/kernel.h> /* types used in module initialization */
#include <sys/conf.h> /* cdevsw struct, UID, GID */
#include <sys/sockio.h>
#include <sys/socketvar.h> /* struct socket */
#include <sys/malloc.h>
#include <sys/poll.h>
#include <sys/rwlock.h>
#include <sys/socket.h> /* sockaddrs */
#include <sys/selinfo.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/bpf.h> /* BIOCIMMEDIATE */
#include <net/ethernet.h> /* struct ether_header */
#include <netinet/in.h> /* IPPROTO_UDP */
#include <machine/bus.h> /* bus_dmamap_* */
#include <sys/endian.h>
#include <sys/refcount.h>
#elif defined(linux)
#include <bsd_glue.h>
#endif
#include <net/netmap.h>
#include <dev/netmap/netmap_kern.h>
#include <dev/netmap/netmap_mem2.h>
#include <dev/netmap/netmap_bdg.h>
#ifdef WITH_STACK
int stackmap_no_runtocomp = 0;
int stackmap_host_batch = 1;
int stackmap_verbose = 0;
#ifdef linux
EXPORT_SYMBOL(stackmap_verbose);
#endif
static int stackmap_extra = 2048;
SYSBEGIN(vars_stack);
SYSCTL_DECL(_dev_netmap);
SYSCTL_INT(_dev_netmap, OID_AUTO, stackmap_no_runtocomp, CTLFLAG_RW, &stackmap_no_runtocomp, 0 , "");
SYSCTL_INT(_dev_netmap, OID_AUTO, stackmap_host_batch, CTLFLAG_RW, &stackmap_host_batch, 0 , "");
SYSCTL_INT(_dev_netmap, OID_AUTO, stackmap_verbose, CTLFLAG_RW, &stackmap_verbose, 0 , "");
SYSCTL_INT(_dev_netmap, OID_AUTO, stackmap_extra, CTLFLAG_RW, &stackmap_extra, 0 , "");
SYSEND;
static inline struct netmap_adapter *
stmp_na(const struct netmap_adapter *slave)
{
const struct netmap_vp_adapter *vpna;
if (unlikely(!slave))
return NULL;
vpna = (const struct netmap_vp_adapter *)slave;
return &netmap_bdg_port(vpna->na_bdg, 0)->up;
}
static inline int
stmp_is_host(struct netmap_adapter *na)
{
return na->nm_register == NULL;
}
/* nm_notify() for NIC RX.
* Deliver interrupts to the same ring index of master if possible
*/
static int
stmp_intr_notify(struct netmap_kring *kring, int flags)
{
struct netmap_adapter *hwna = kring->na, *vpna, *mna;
enum txrx t = kring->tx ? NR_TX : NR_RX;
vpna = (struct netmap_adapter *)hwna->na_private;
if (unlikely(!vpna))
return NM_IRQ_COMPLETED;
/* just wakeup the client on the master */
mna = stmp_na(vpna);
if (likely(mna)) {
u_int me = kring - NMR(hwna, t), last;
struct netmap_kring *mk;
if (stackmap_no_runtocomp)
return netmap_bwrap_intr_notify(kring, flags);
last = nma_get_nrings(mna, t);
mk = &NMR(mna, t)[last > me ? me : me % last];
mk->nm_notify(mk, 0);
}
return NM_IRQ_COMPLETED;
}
/*
* We need to form lists using scb and buf_idx, because they
* can be very long due to ofo packets that have been queued
*/
#define STACKMAP_FD_HOST (NM_BDG_MAXPORTS*NM_BDG_MAXRINGS-1)
struct stmp_bdg_q {
uint32_t bq_head;
uint32_t bq_tail;
};
struct stmp_fwd {
uint16_t nfds;
uint16_t npkts;
struct stmp_bdg_q fde[NM_BDG_MAXPORTS * NM_BDG_MAXRINGS
+ NM_BDG_BATCH_MAX]; /* XXX */
uint32_t tmp[NM_BDG_BATCH_MAX];
uint32_t fds[NM_BDG_BATCH_MAX/2]; // max fd index
};
#define STACKMAP_FT_NULL 0 // invalid buf index
struct stmp_extra_slot {
struct netmap_slot slot;
uint16_t prev;
uint16_t next;
};
struct stmp_extra_pool {
u_int num;
struct stmp_extra_slot *slots;
uint32_t free;
uint32_t free_tail;
uint32_t busy;
uint32_t busy_tail;
};
#define NM_EXT_NULL ((uint16_t)~0)
void
stmp_extra_dequeue(struct netmap_kring *kring, struct netmap_slot *slot)
{
struct netmap_ring *ring = kring->ring;
struct stmp_extra_pool *pool = kring->extra;
struct stmp_extra_slot *slots, *extra;
u_int pos;
if (unlikely(!pool)) {
RD(1, "kring->extra has gone");
return;
} else if (unlikely(!pool->num)) {
RD(1, "extra slots have gone");
return;
}
slots = pool->slots;
/* nothing to do if I am on the ring */
if ((uintptr_t)slot >= (uintptr_t)ring->slot &&
(uintptr_t)slot < (uintptr_t)(ring->slot + kring->nkr_num_slots)) {
return;
} else if (!(likely((uintptr_t)slot >= (uintptr_t)slots) &&
likely((uintptr_t)slot < (uintptr_t)(slots + pool->num)))) {
D("WARNING: invalid slot");
return;
}
extra = (struct stmp_extra_slot *)slot;
pos = extra - slots;
/* remove from busy list (offset has been modified to indicate prev) */
if (extra->next == NM_EXT_NULL)
pool->busy_tail = extra->prev; // might be NM_EXT_NULL
else
slots[extra->next].prev = extra->prev; // might be NM_EXT_NULL
if (extra->prev == NM_EXT_NULL)
pool->busy = extra->next; // might be NM_EXT_NULL
else
slots[extra->prev].next = extra->next; // might be NM_EXT_NULL
/* append to free list */
extra->next = NM_EXT_NULL;
if (unlikely(pool->free == NM_EXT_NULL))
pool->free = pos;
else
slots[pool->free_tail].next = pos;
extra->prev = pool->free_tail; // can be NM_EXT_NULL
pool->free_tail = pos;
}
int
stmp_extra_enqueue(struct netmap_kring *kring, struct netmap_slot *slot)
{
struct netmap_adapter *na = kring->na;
struct stmp_extra_pool *pool = kring->extra;
struct stmp_extra_slot *slots = pool->slots, *extra;
uint32_t tmp;
u_int pos;
struct stmp_cb *scb;
if (pool->free_tail == NM_EXT_NULL)
return EBUSY;
pos = pool->free_tail;
extra = &slots[pos];
/* remove from free list */
pool->free_tail = extra->prev;
if (unlikely(pool->free_tail == NM_EXT_NULL)) // I was the last one
pool->free = NM_EXT_NULL;
else // not the last one
slots[extra->prev].next = NM_EXT_NULL;
/* apend to busy list */
extra->next = NM_EXT_NULL;
if (pool->busy == NM_EXT_NULL) {
pool->busy = pos;
} else
slots[pool->busy_tail].next = pos;
extra->prev = pool->busy_tail;
pool->busy_tail = pos;
scb = NMCB_BUF(NMB(na, slot));
tmp = extra->slot.buf_idx; // backup
extra->slot = *slot;
slot->buf_idx = tmp;
slot->flags |= NS_BUF_CHANGED;
slot->len = slot->offset = slot->next = 0;
slot->fd = 0;
scbw(scb, kring, &extra->slot);
return 0;
}
static inline struct stmp_fwd *
stmp_get_fwd(struct netmap_kring *kring)
{
return (struct stmp_fwd *)kring->nkr_ft;
}
void
stmp_add_fdtable(struct stmp_cb *scb, struct netmap_kring *kring)
{
struct netmap_slot *slot = scb_slot(scb);
struct stmp_fwd *ft;
uint32_t fd = slot->fd;
struct stmp_bdg_q *fde;
int i;
ft = stmp_get_fwd(kring);
i = slot->buf_idx;
scb->next = STACKMAP_FT_NULL;
fde = ft->fde + fd;
if (fde->bq_head == STACKMAP_FT_NULL) {
fde->bq_head = fde->bq_tail = i;
ft->fds[ft->nfds++] = fd;
} else {
struct netmap_slot s = { fde->bq_tail };
struct stmp_cb *prev = NMCB_BUF(NMB(kring->na, &s));
prev->next = fde->bq_tail = i;
}
ft->npkts++;
}
/* TX:
* 1. sort packets by socket with forming send buffer (in-order iteration)
* 2. do tcp processing on each socket (out-of-order iteration)
* We must take into account MOREFRAGS.
* We do not support INDIRECT as packet movement is done by swapping
* We thus overwrite ptr field (8 byte width) in a slot to store a
* socket (4 byte), next buf index (2 byte).
* The rest of 2 bytes may be used to store the number of frags
* (1 byte) and destination port (1 byte).
*/
struct stmp_sk_adapter *
stmp_ska_from_fd(struct netmap_adapter *na, int fd)
{
struct netmap_stack_adapter *sna = (struct netmap_stack_adapter *)na;
if (unlikely(fd >= sna->sk_adapters_max))
return NULL;
return sna->sk_adapters[fd];
}
/* Differ from nm_kr_space() due to different meaning of the lease */
static inline uint32_t
stmp_kr_rxspace(struct netmap_kring *k)
{
int busy = k->nr_hwtail - k->nkr_hwlease;
if (busy < 0)
busy += k->nkr_num_slots;
return k->nkr_num_slots - 1 - busy;
}
/* bdg_{r,w}lock() must be held */
static void
stmp_bdg_flush(struct netmap_kring *kring)
{
struct netmap_adapter *na = kring->na, *rxna;
struct nm_bridge *b = ((struct netmap_vp_adapter *)na)->na_bdg;
struct stmp_fwd *ft;
u_int lim_rx, howmany;
u_int dst_nr, nrings;
struct netmap_kring *rxkring;
int j, want, nonfree_num = 0;
uint32_t *nonfree;
if (netmap_bdg_rlock(b, na)){
return;
}
ft = stmp_get_fwd(kring);
nonfree = ft->tmp;
if (stmp_is_host(na)) {
want = kring->rhead - kring->nr_hwcur;
if (want < 0)
want += kring->nkr_num_slots;
} else {
want = ft->npkts;
}
/* XXX perhaps this is handled later? */
if (unlikely(netmap_bdg_active_ports(b) < 3)) {
RD(1, "only 1 or 2 active ports");
goto runlock;
}
/* Now, we know how many packets go to the receiver */
if (na == stmp_na(na) || stmp_is_host(na)) {
rxna = &netmap_bdg_port(b, 1)->up; /* XXX */
} else {
rxna = stmp_na(na);
}
if (unlikely(!nm_netmap_on(rxna))) {
panic("receiver na off");
}
dst_nr = kring - NMR(kring->na, NR_TX); // XXX cannot rely on ring_id
nrings = nma_get_nrings(rxna, NR_RX);
if (dst_nr >= nrings)
dst_nr = dst_nr % nrings;
rxkring = NMR(rxna, NR_RX) + dst_nr;
lim_rx = rxkring->nkr_num_slots - 1;
j = rxkring->nr_hwtail;
/* under lock */
mtx_lock(&rxkring->q_lock);
if (unlikely(rxkring->nkr_stopped)) {
mtx_unlock(&rxkring->q_lock);
goto runlock;
}
howmany = stmp_kr_rxspace(rxkring);
if (howmany < want) { // try to reclaim completed buffers
u_int i = rxkring->nkr_hwlease, n = 0;
for (; i != rxkring->nr_hwtail; i = nm_next(i, lim_rx), n++) {
struct netmap_slot *slot = &rxkring->ring->slot[i];
struct stmp_cb *scb = NMCB_BUF(NMB(rxna, slot));
if (stmp_cb_valid(scb) &&
stmp_cb_rstate(scb) != SCB_M_NOREF)
break;
}
howmany += n;
rxkring->nkr_hwlease = i;
} else if (likely(want < howmany)) {
howmany = want;
}
if (stmp_is_host(na)) { // don't touch buffers
u_int k = kring->nr_hwcur, lim_tx = kring->nkr_num_slots - 1;
while (howmany--) {
struct netmap_slot *ts, *rs, tmp;
ts = &kring->ring->slot[k];
__builtin_prefetch(ts);
rs = &rxkring->ring->slot[j];
__builtin_prefetch(rs);
tmp = *rs;
*rs = *ts;
*ts = tmp;
ts->flags |= NS_BUF_CHANGED;
rs->flags |= NS_BUF_CHANGED;
k = nm_next(k, lim_tx);
j = nm_next(j, lim_rx);
}
} else {
int n, sent = 0;
for (n = 0; n < ft->nfds && howmany;) {
int fd = ft->fds[n];
struct stmp_bdg_q *bq = ft->fde + fd;
uint32_t next = bq->bq_head;
do {
struct netmap_slot tmp, *ts, *rs;
struct stmp_cb *scb;
rs = &rxkring->ring->slot[j];
__builtin_prefetch(rs);
tmp.buf_idx = next;
scb = NMCB_BUF(NMB(na, &tmp));
next = scb->next;
if (unlikely(!stmp_cb_valid(scb))) {
D("invalid scb %p next %u", scb, next);
goto skip;
}
ts = scb_slot(scb);
if (unlikely(ts == NULL)) {
D("null ts %p next %u", ts, next);
goto skip;
}
if (stmp_cb_rstate(scb) == SCB_M_TXREF) {
nonfree[nonfree_num++] = j;
}
scbw(scb, rxkring, rs);
tmp = *rs;
*rs = *ts;
*ts = tmp;
ts->len = ts->offset = 0;
ts->fd = 0;
ts->flags |= NS_BUF_CHANGED;
rs->flags |= NS_BUF_CHANGED;
skip:
j = nm_next(j, lim_rx);
sent++;
} while (next != STACKMAP_FT_NULL && --howmany);
if (likely(next == STACKMAP_FT_NULL))
n++;
bq->bq_head = next; // no NULL if howmany has run out
}
ft->nfds -= n;
ft->npkts -= sent;
memmove(ft->fds, ft->fds + n, sizeof(ft->fds[0]) * ft->nfds);
}
rxkring->nr_hwtail = j;
mtx_unlock(&rxkring->q_lock);
rxkring->nm_notify(rxkring, 0);
rxkring->nkr_hwlease = rxkring->nr_hwcur;
/* swap out packets still referred by the stack */
for (j = 0; j < nonfree_num; j++) {
struct netmap_slot *slot = &rxkring->ring->slot[nonfree[j]];
if (unlikely(stmp_extra_enqueue(rxkring, slot))) {
/* Don't reclaim on/after this postion */
u_long nm_i = slot - rxkring->ring->slot;
rxkring->nkr_hwlease = nm_i;
break;
}
}
runlock:
netmap_bdg_runlock(b);
return;
}
/* Form fdtable to be flushed */
static int
stmp_bdg_preflush(struct netmap_kring *kring)
{
struct netmap_adapter *na = kring->na;
int k = kring->nr_hwcur;
u_int lim_tx = kring->nkr_num_slots - 1;
const int rhead = kring->rhead;
int tx = 0;
struct stmp_fwd *ft = stmp_get_fwd(kring);
if (na == stmp_na(na))
tx = 1;
else if (stmp_is_host(na))
kring->nkr_hwlease = rhead; // skip loop below
//if (ft->npkts) {
//stmp_bdg_flush(kring);
//}
for (k = kring->nkr_hwlease; k != rhead; k = nm_next(k, lim_tx)) {
struct netmap_slot *slot = &kring->ring->slot[k];
struct stmp_cb *scb;
char *nmb = NMB(na, slot);
int error;
__builtin_prefetch(nmb);
if (unlikely(slot->len == 0)) {
continue;
}
scb = NMCB_BUF(nmb);
scbw(scb, kring, slot);
error = tx ? nm_os_stmp_send(kring, slot) :
nm_os_stmp_recv(kring, slot);
if (unlikely(error)) {
/* We stop processing on -EAGAIN(TX) which occurs due
* to misbehaviong user e.g., invalid fd.
*/
if (error == -EBUSY)
k = nm_next(k, lim_tx);
break;
}
}
kring->nkr_hwlease = k; // next position to throw into the stack
stmp_bdg_flush(kring);
if (ft->npkts) { // we have leftover, cannot report k
int j;
/* try to reclaim buffers on txring */
for (j = kring->nr_hwcur; j != k; j = nm_next(j, lim_tx)) {
struct netmap_slot *slot = &kring->ring->slot[j];
struct stmp_cb *scb;
if (unlikely(!slot->len))
continue;
scb = NMCB_BUF(NMB(na, slot));
/* scb can be invalid due to new buffer swap-ed in */
if (stmp_cb_valid(scb) &&
stmp_cb_rstate(scb) != SCB_M_NOREF)
break;
}
k = j;
}
return k;
}
static int
stmp_rxsync(struct netmap_kring *kring, int flags)
{
struct netmap_stack_adapter *sna =
(struct netmap_stack_adapter *)kring->na;
struct nm_bridge *b = sna->up.na_bdg;
int i, err;
register_t intr;
/* TODO scan only necessary ports */
err = netmap_vp_rxsync(kring, flags); // reclaim buffers released
if (err)
return err;
if (stackmap_no_runtocomp)
return 0;
intr = intr_disable(); // emulate software interrupt context
for_bdg_ports(i, b) {
struct netmap_vp_adapter *vpna = netmap_bdg_port(b, i);
struct netmap_adapter *na = &vpna->up;
struct netmap_adapter *hwna;
u_int first, stride, last, i;
if (netmap_bdg_idx(vpna) == netmap_bdg_idx(&sna->up))
continue;
else if (stmp_is_host(na))
continue;
/* We assume the same number of hwna with vpna
* (see netmap_bwrap_attach()) */
hwna = ((struct netmap_bwrap_adapter *)vpna)->hwna;
/* hw ring(s) to scan */
first = kring->na->num_rx_rings > 1 ? kring->ring_id : 0;
stride = kring->na->num_rx_rings;
last = na->num_rx_rings;
for (i = first; i < last; i += stride) {
struct netmap_kring *hwk, *bk, *hk;
hwk = &NMR(hwna, NR_RX)[i];
bk = &NMR(na, NR_TX)[i];
hk = &NMR(hwna, NR_RX)[last];
if (hwna->na_flags & NAF_HOST_MQ)
hk += i;
/*
* bdg_flush has been put off because we do not want
* it to run in bdg_config context with bridge wlock
* held. Thus, if we have some packets originated by
* this NIC ring, just drain it without NIC's rxsync.
*/
if (stmp_get_fwd(bk)->npkts > 0) {
stmp_bdg_flush(bk);
} else {
netmap_bwrap_intr_notify(hwk, 0);
if (stackmap_host_batch) {
netmap_bwrap_intr_notify(hk, 0);
}
}
}
}
intr_restore(intr);
return netmap_vp_rxsync(kring, flags);
}
static int
stmp_txsync(struct netmap_kring *kring, int flags)
{
struct netmap_adapter *na = kring->na;
u_int const head = kring->rhead;
u_int done;
if (unlikely(((struct netmap_vp_adapter *)na)->na_bdg == NULL)) {
done = head;
return 0;
}
done = stmp_bdg_preflush(kring);
kring->nr_hwcur = done;
kring->nr_hwtail = nm_prev(done, kring->nkr_num_slots - 1);
return 0;
}
static int
nombq_rxsync(struct netmap_kring *kring, int flags)
{
(void)kring;
(void)flags;
return 0;
}
static int
nombq(struct netmap_adapter *na, struct mbuf *m)
{
struct netmap_kring *kring;
struct netmap_slot *hslot;
u_int head, nm_i, lim, len = MBUF_LEN(m);
/* host ring */
nm_i = curcpu % nm_num_host_rings(na, NR_RX);
kring = &NMR(na, NR_RX)[nma_get_nrings(na, NR_RX) + nm_i];
head = kring->rhead;
lim = kring->nkr_num_slots - 1;
nm_i = kring->nr_hwtail;
/* check space */
if (unlikely(nm_i == nm_prev(kring->nr_hwcur, lim))) {
RD(1, "kring full");
m_freem(m);
return EBUSY;
} else if (unlikely(!nm_netmap_on(na))) {
m_freem(m);
return ENXIO;
}
hslot = &kring->ring->slot[nm_i];
m_copydata(m, 0, len, (char *)NMB(na, hslot) + na->virt_hdr_len);
hslot->len = len;
kring->nr_hwtail = nm_next(nm_i, lim);
nm_i = kring->nr_hwcur;
if (likely(nm_i != head)) {
kring->nr_hwcur = head;
}
if (!stackmap_host_batch) {
netmap_bwrap_intr_notify(kring, 0);
}
/* as if netmap_transmit + rxsync_from_host done */
m_freem(m);
return 0;
}
#ifdef __FreeBSD__
/* FreeBSD doesn't have protocol header offsets filled */
static inline void
__mbuf_proto_headers(struct mbuf *m)
{
uint16_t ethertype;
ethertype = ntohs(*(uint16_t *)(m->m_data + 12));
if (MBUF_NETWORK_OFFSET(m) > 0)
return;
m->m_pkthdr.l2hlen = sizeof(struct ether_header);
m->m_pkthdr.l3hlen = sizeof(struct nm_iphdr);
}
#else
#define __mbuf_proto_headers(m)
#endif /* __FreeBSD__ */
static void
csum_transmit(struct netmap_adapter *na, struct mbuf *m)
{
if (nm_os_mbuf_has_offld(m)) {
struct nm_iphdr *iph;
char *th;
uint16_t *check;
__mbuf_proto_headers(m);
iph = (struct nm_iphdr *)MBUF_NETWORK_HEADER(m);
KASSERT(iph != NULL, ("NULL iph"));
th = MBUF_TRANSPORT_HEADER(m);
KASSERT(th != NULL, ("NULL th"));
th = MBUF_TRANSPORT_HEADER(m);
if (iph->protocol == IPPROTO_UDP) {
check = &((struct nm_udphdr *)th)->check;
} else if (likely(iph->protocol == IPPROTO_TCP)) {
check = &((struct nm_tcphdr *)th)->check;
} else {
panic("bad proto %u w/ offld", iph->protocol);
}
/* With ethtool -K eth1 tx-checksum-ip-generic on, we
* see HWCSUM/IP6CSUM in dev and ip_sum PARTIAL on m.
*/
*check = 0;
nm_os_csum_tcpudp_ipv4(iph, th,
MBUF_LEN(m) - MBUF_TRANSPORT_OFFSET(m), check);
//m->ip_summed = 0;
//m->m_pkthdr.csum_flags = CSUM_TSO; // XXX
}
nombq(na, m);
}
int
stmp_transmit(struct ifnet *ifp, struct mbuf *m)
{
struct netmap_adapter *na = NA(ifp);
struct stmp_cb *scb = NULL;
struct netmap_slot *slot;
char *nmb;
int mismatch;
struct mbuf *md = m;
#ifdef linux
/* txsync-ing TX packets are always frags */
if (!MBUF_NONLINEAR(m)) {
csum_transmit(na, m);
return 0;
}
scb = NMCB_EXT(m, 0, NETMAP_BUF_SIZE(na));
#else
/* M_EXT or multiple mbufs (i.e., chain) */
if ((m->m_flags & M_EXT)) { // not TCP case
scb = NMCB_EXT(m, 0, NETMAP_BUF_SIZE(na));
}
if (!scb || !stmp_cb_valid(scb)) { // TCP case
if (MBUF_NONLINEAR(m) && (m->m_next->m_flags & M_EXT)) {
scb = NMCB_EXT(m->m_next, 0, NETMAP_BUF_SIZE(na));
}
md = m->m_next;
}
if (!scb || !stmp_cb_valid(scb)) {
csum_transmit(na, m);
return 0;
}
#endif /* linux */
if (unlikely(stmp_cb_rstate(scb) != SCB_M_STACK) ||
/* FreeBSD ARP reply recycles the request mbuf */
unlikely(scb_kring(scb) &&
scb_kring(scb)->na->na_private == na->na_private)) {
MBUF_LINEARIZE(m); // XXX
csum_transmit(na, m);
return 0;
}
/* Valid scb, txsync-ing packet. */
slot = scb_slot(scb);
if (unlikely(stmp_cb_rstate(scb) == SCB_M_QUEUED)) {
/* originated by netmap but has been queued in either extra
* or txring slot. The backend might drop this packet.
*/
#ifdef linux
struct stmp_cb *scb2;
int i, n = MBUF_CLUSTERS(m);
for (i = 0; i < n; i++) {
scb2 = NMCB_EXT(m, i, NETMAP_BUF_SIZE(na));
stmp_cb_wstate(scb2, SCB_M_NOREF);
}
#else
/* To be done */
#endif /* linux */
slot->len = 0; // XXX
MBUF_LINEARIZE(m);
csum_transmit(na, m);
return 0;
}
nmb = NMB(na, slot);
/* bring protocol headers in */
mismatch = MBUF_HEADLEN(m) - (int)slot->offset;
ND("MBUF_HEADLEN(m) %d MBUF_HEADLEN(nd) %d m->m_len %d"
"m->m_pkthdr.len %d m->m_pkthdr.l2hlen %d "
"m->m_pkthdr.l3hlen %d m->m_pkthdr.l4hlen %d ethtype 0x%x "
"slot->len %u slot->offset %u virt %u offld %d mismatch %d",
MBUF_HEADLEN(m), MBUF_HEADLEN(md), m->m_len, m->m_pkthdr.len,
m->m_pkthdr.l2hlen, m->m_pkthdr.l3hlen, m->m_pkthdr.l4hlen,
ntohs(*(uint16_t *)(m->m_data + 12)), slot->len, slot->offset,
na->virt_hdr_len, nm_os_mbuf_has_offld(m), mismatch);
if (!mismatch) {
/* Length has already been validated */
memcpy(nmb + na->virt_hdr_len, MBUF_DATA(m), slot->offset);
} else {
m_copydata(m, 0, MBUF_LEN(m), nmb + na->virt_hdr_len);
slot->len += mismatch;
}
if (nm_os_mbuf_has_offld(m)) {
struct nm_iphdr *iph;
struct nm_tcphdr *tcph;
uint16_t *check;
int len, v = na->virt_hdr_len;
__mbuf_proto_headers(m);
iph = (struct nm_iphdr *)(nmb + v + MBUF_NETWORK_OFFSET(m));
tcph = (struct nm_tcphdr *)(nmb + v + MBUF_TRANSPORT_OFFSET(m));
check = &tcph->check;
*check = 0;
len = slot->len - v - MBUF_TRANSPORT_OFFSET(m);
nm_os_csum_tcpudp_ipv4(iph, tcph, len, check);
}
stmp_add_fdtable(scb, scb_kring(scb));
/* We don't know when the stack actually releases the data;
* it might holds reference via clone.
*/
stmp_cb_wstate(scb, SCB_M_TXREF);
#ifdef linux
/* for FreeBSD mbuf comes from our code */
nm_set_mbuf_data_destructor(m, &scb->ui,
nm_os_stmp_mbuf_data_destructor);
#endif /* linux */
m_freem(m);
return 0;
}
static void
stmp_extra_free(struct netmap_adapter *na)
{
enum txrx t;
for_rx_tx(t) {
int i;
for (i = 0; i < netmap_real_rings(na, t); i++) {
struct netmap_kring *kring = &NMR(na, t)[i];
struct stmp_extra_pool *extra;
uint32_t j;
if (!kring->extra)
continue;
extra = kring->extra;
j = extra->busy;
while (j != NM_EXT_NULL) {
struct stmp_extra_slot *es = &extra->slots[j];
struct stmp_cb *scb
= NMCB_BUF(NMB(na, &es->slot));
stmp_cb_set_gone(scb);
j = es->next;
}
kring->extra = NULL;
extra->num = 0;
if (extra->slots)
nm_os_free(extra->slots);
nm_os_free(extra);
}
}
}
static int
stmp_extra_alloc(struct netmap_adapter *na)
{
enum txrx t;
for_rx_tx(t) {
int i;
/* XXX probably we don't need extra on host rings */
for (i = 0; i < netmap_real_rings(na, t); i++) {
struct netmap_kring *kring = &NMR(na, t)[i];
struct stmp_extra_pool *pool;
struct stmp_extra_slot *extra_slots = NULL;
u_int want = stackmap_extra, n, j, next;
pool = nm_os_malloc(sizeof(*kring->extra));
if (!pool)
break;
kring->extra = pool;
n = netmap_extra_alloc(na, &next, want);
if (n < want)
D("allocated only %u bufs", n);
kring->extra->num = n;
if (n) {
extra_slots = nm_os_malloc(sizeof(*extra_slots)
* n);
if (!extra_slots)
break;
}
for (j = 0; j < n; j++) {
struct stmp_extra_slot *exs;
struct netmap_slot tmp = {.buf_idx = next};
exs = &extra_slots[j];
exs->slot.buf_idx = next;
exs->slot.len = 0;
exs->prev = j == 0 ? NM_EXT_NULL : j - 1;
exs->next = j + 1 == n ? NM_EXT_NULL : j + 1;
next = *(uint32_t *)NMB(na, &tmp);
}
pool->free = 0;
pool->free_tail = n - 1;
pool->busy = pool->busy_tail = NM_EXT_NULL;
pool->slots = extra_slots;
}
/* rollaback on error */
if (i < netmap_real_rings(na, t)) {
stmp_extra_free(na);
return ENOMEM;
}
}
return 0;
}
/* Create extra buffers and mbuf pool */
#define for_each_kring_n(_i, _k, _karr, _n) \
for (_k=_karr, _i = 0; _i < _n; (_k)++, (_i)++)
#define for_each_tx_kring(_i, _k, _na) \
for_each_kring_n(_i, _k, (_na)->tx_rings, (_na)->num_tx_rings)
static int
stmp_mbufpool_alloc(struct netmap_adapter *na)
{
struct netmap_kring *kring;
int r, error = 0;
for_each_tx_kring(r, kring, na) {
kring->tx_pool = NULL;
}
for_each_tx_kring(r, kring, na) {
kring->tx_pool =
nm_os_malloc(na->num_tx_desc *
sizeof(struct mbuf *));
if (!kring->tx_pool) {
D("tx_pool allocation failed");
error = ENOMEM;
break;
}
bzero(kring->tx_pool, na->num_tx_desc * sizeof(struct mbuf *));
kring->tx_pool[0] = nm_os_malloc(sizeof(struct mbuf));
if (!kring->tx_pool[0]) {
error = ENOMEM;
break;
}
bzero(kring->tx_pool[0], sizeof(struct mbuf));
}
if (error) {
for_each_tx_kring(r, kring, na) {
if (kring->tx_pool == NULL)
continue;
if (kring->tx_pool[0])
nm_os_free(kring->tx_pool[0]);
nm_os_free(kring->tx_pool);
kring->tx_pool = NULL;
}
}
return error;
}
static void
stmp_mbufpool_free(struct netmap_adapter *na)
{
struct netmap_kring *kring;
int r;
for_each_tx_kring(r, kring, na) {
if (kring->tx_pool == NULL)
continue;
if (kring->tx_pool[0])
nm_os_free(kring->tx_pool[0]);
nm_os_free(kring->tx_pool);
kring->tx_pool = NULL;
}
}
/* Stackmap extends default bwrap_reg() and bwrap_attach() */
static int
stmp_bwrap_reg(struct netmap_adapter *na, int onoff)
{