forked from luigirizzo/netmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetmap_linux.c
More file actions
2227 lines (1903 loc) · 53 KB
/
netmap_linux.c
File metadata and controls
2227 lines (1903 loc) · 53 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) 2013-2014 Universita` di Pisa. 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.
*/
#include "bsd_glue.h"
#include <linux/file.h> /* fget(int fd) */
#include <net/netmap.h>
#include <dev/netmap/netmap_kern.h>
#include <net/netmap_virt.h>
#include <dev/netmap/netmap_mem2.h>
#include <linux/rtnetlink.h>
#include <linux/nsproxy.h>
#include <net/pkt_sched.h>
#include <net/sch_generic.h>
#include "netmap_linux_config.h"
void *
nm_os_malloc(size_t size)
{
return kmalloc(size, GFP_ATOMIC | __GFP_ZERO);
}
void *
nm_os_realloc(void *addr, size_t new_size, size_t old_size)
{
(void)old_size;
return krealloc(addr, new_size, GFP_ATOMIC | __GFP_ZERO);
}
void
nm_os_free(void *addr){
kfree(addr);
}
void
nm_os_selinfo_init(NM_SELINFO_T *si)
{
init_waitqueue_head(si);
}
void
nm_os_selinfo_uninit(NM_SELINFO_T *si)
{
}
void
nm_os_ifnet_lock(void)
{
rtnl_lock();
}
void
nm_os_ifnet_unlock(void)
{
rtnl_unlock();
}
void
nm_os_get_module(void)
{
__module_get(THIS_MODULE);
}
void
nm_os_put_module(void)
{
module_put(THIS_MODULE);
}
/* Register for a notification on device removal */
static int
linux_netmap_notifier_cb(struct notifier_block *b,
unsigned long val, void *v)
{
struct ifnet *ifp = netdev_notifier_info_to_dev(v);
/* linux calls us while holding rtnl_lock() */
switch (val) {
case NETDEV_REGISTER:
netmap_undo_zombie(ifp);
break;
case NETDEV_UNREGISTER:
netmap_make_zombie(ifp);
break;
case NETDEV_GOING_DOWN:
netmap_disable_all_rings(ifp);
break;
case NETDEV_UP:
netmap_enable_all_rings(ifp);
break;
default:
/* we don't care */
break;
}
return NOTIFY_OK;
}
static struct notifier_block linux_netmap_netdev_notifier = {
.notifier_call = linux_netmap_notifier_cb,
};
static int nm_os_ifnet_registered;
int
nm_os_ifnet_init(void)
{
int error = NM_REG_NETDEV_NOTIF(&linux_netmap_netdev_notifier);
if (!error)
nm_os_ifnet_registered = 1;
return error;
}
void
nm_os_ifnet_fini(void)
{
if (nm_os_ifnet_registered) {
NM_UNREG_NETDEV_NOTIF(&linux_netmap_netdev_notifier);
nm_os_ifnet_registered = 0;
}
}
#ifdef NETMAP_LINUX_HAVE_IOMMU
#include <linux/iommu.h>
/* #################### IOMMU ################## */
/*
* Returns the IOMMU domain id that the device belongs to.
*/
int nm_iommu_group_id(struct device *dev)
{
struct iommu_group *grp;
int id;
if (!dev)
return 0;
grp = iommu_group_get(dev);
if (!grp)
return 0;
id = iommu_group_id(grp);
return id;
}
#else /* ! HAVE_IOMMU */
int nm_iommu_group_id(struct device *dev)
{
return 0;
}
#endif /* HAVE_IOMMU */
/* #################### VALE OFFLOADINGS SUPPORT ################## */
/* Compute and return a raw checksum over (data, len), using 'cur_sum'
* as initial value. Both 'cur_sum' and the return value are in host
* byte order.
*/
rawsum_t
nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum)
{
return csum_partial(data, len, cur_sum);
}
/* Compute an IPv4 header checksum, where 'data' points to the IPv4 header,
* and 'len' is the IPv4 header length. Return value is in network byte
* order.
*/
uint16_t
nm_os_csum_ipv4(struct nm_iphdr *iph)
{
return ip_compute_csum((void*)iph, sizeof(struct nm_iphdr));
}
/* Compute and insert a TCP/UDP checksum over IPv4: 'iph' points to the IPv4
* header, 'data' points to the TCP/UDP header, 'datalen' is the lenght of
* TCP/UDP header + payload.
*/
void
nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
size_t datalen, uint16_t *check)
{
*check = csum_tcpudp_magic(iph->saddr, iph->daddr,
datalen, iph->protocol,
csum_partial(data, datalen, 0));
}
/* Compute and insert a TCP/UDP checksum over IPv6: 'ip6h' points to the IPv6
* header, 'data' points to the TCP/UDP header, 'datalen' is the lenght of
* TCP/UDP header + payload.
*/
void
nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
size_t datalen, uint16_t *check)
{
*check = csum_ipv6_magic((void *)&ip6h->saddr, (void*)&ip6h->daddr,
datalen, ip6h->nexthdr,
csum_partial(data, datalen, 0));
}
uint16_t
nm_os_csum_fold(rawsum_t cur_sum)
{
return csum_fold(cur_sum);
}
/* on linux we send up one packet at a time */
void *
nm_os_send_up(struct ifnet *ifp, struct mbuf *m, struct mbuf *prev)
{
(void)ifp;
(void)prev;
m->priority = NM_MAGIC_PRIORITY_RX; /* do not reinject to netmap */
netif_rx(m);
return NULL;
}
int
nm_os_mbuf_has_offld(struct mbuf *m)
{
return m->ip_summed == CHECKSUM_PARTIAL || skb_is_gso(m);
}
#ifdef WITH_GENERIC
/* ####################### MITIGATION SUPPORT ###################### */
/*
* The generic driver calls netmap once per received packet.
* This is inefficient so we implement a mitigation mechanism,
* as follows:
* - the first packet on an idle receiver triggers a notification
* and starts a timer;
* - subsequent incoming packets do not cause a notification
* until the timer expires;
* - when the timer expires and there are pending packets,
* a notification is sent up and the timer is restarted.
*/
static NETMAP_LINUX_TIMER_RTYPE
generic_timer_handler(struct hrtimer *t)
{
struct nm_generic_mit *mit =
container_of(t, struct nm_generic_mit, mit_timer);
u_int work_done;
if (!mit->mit_pending) {
return HRTIMER_NORESTART;
}
/* Some work arrived while the timer was counting down:
* Reset the pending work flag, restart the timer and send
* a notification.
*/
mit->mit_pending = 0;
/* below is a variation of netmap_generic_irq XXX revise */
if (nm_netmap_on(mit->mit_na)) {
netmap_common_irq(mit->mit_na, mit->mit_ring_idx, &work_done);
generic_rate(0, 0, 0, 0, 0, 1);
}
nm_os_mitigation_restart(mit);
return HRTIMER_RESTART;
}
void
nm_os_mitigation_init(struct nm_generic_mit *mit, int idx,
struct netmap_adapter *na)
{
hrtimer_init(&mit->mit_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
mit->mit_timer.function = &generic_timer_handler;
mit->mit_pending = 0;
mit->mit_ring_idx = idx;
mit->mit_na = na;
}
void
nm_os_mitigation_start(struct nm_generic_mit *mit)
{
hrtimer_start(&mit->mit_timer, ktime_set(0, netmap_generic_mit), HRTIMER_MODE_REL);
}
void
nm_os_mitigation_restart(struct nm_generic_mit *mit)
{
hrtimer_forward_now(&mit->mit_timer, ktime_set(0, netmap_generic_mit));
}
int
nm_os_mitigation_active(struct nm_generic_mit *mit)
{
return hrtimer_active(&mit->mit_timer);
}
void
nm_os_mitigation_cleanup(struct nm_generic_mit *mit)
{
hrtimer_cancel(&mit->mit_timer);
}
/* #################### GENERIC ADAPTER SUPPORT ################### */
/*
* This handler is registered within the attached net_device
* in the Linux RX subsystem, so that every mbuf passed up by
* the driver can be stolen to the network stack.
* Stolen packets are put in a queue where the
* generic_netmap_rxsync() callback can extract them.
* Packets that comes from netmap_txsync_to_host() are not
* stolen.
*/
#ifdef NETMAP_LINUX_HAVE_RX_REGISTER
enum {
NM_RX_HANDLER_STOLEN,
NM_RX_HANDLER_PASS,
};
static inline int
linux_generic_rx_handler_common(struct mbuf *m)
{
int stolen;
/* If we were called by NM_SEND_UP(), we want to pass the mbuf
to network stack. We detect this situation looking at the
priority field. */
if (m->priority == NM_MAGIC_PRIORITY_RX) {
return NM_RX_HANDLER_PASS;
}
/* When we intercept a sk_buff coming from the driver, it happens that
skb->data points to the IP header, e.g. the ethernet header has
already been pulled. Since we want the netmap rings to contain the
full ethernet header, we push it back, so that the RX ring reader
can see it. */
skb_push(m, ETH_HLEN);
/* Possibly steal the mbuf and notify the pollers for a new RX
* packet. */
stolen = generic_rx_handler(m->dev, m);
if (stolen) {
return NM_RX_HANDLER_STOLEN;
}
skb_pull(m, ETH_HLEN);
return NM_RX_HANDLER_PASS;
}
#ifdef NETMAP_LINUX_HAVE_RX_HANDLER_RESULT
static rx_handler_result_t
linux_generic_rx_handler(struct mbuf **pm)
{
int ret = linux_generic_rx_handler_common(*pm);
return likely(ret == NM_RX_HANDLER_STOLEN) ? RX_HANDLER_CONSUMED :
RX_HANDLER_PASS;
}
#else /* ! HAVE_RX_HANDLER_RESULT */
static struct sk_buff *
linux_generic_rx_handler(struct mbuf *m)
{
int ret = linux_generic_rx_handler_common(m);
return likely(ret == NM_RX_HANDLER_STOLEN) ? NULL : m;
}
#endif /* HAVE_RX_HANDLER_RESULT */
#endif /* HAVE_RX_REGISTER */
/* Ask the Linux RX subsystem to intercept (or stop intercepting)
* the packets incoming from the interface attached to 'na'.
*/
int
nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept)
{
#ifndef NETMAP_LINUX_HAVE_RX_REGISTER
#warning "Packet reception with emulated (generic) mode not supported for this kernel version"
return 0;
#else /* HAVE_RX_REGISTER */
struct netmap_adapter *na = &gna->up.up;
struct ifnet *ifp = netmap_generic_getifp(gna);
if (intercept) {
return -netdev_rx_handler_register(ifp,
&linux_generic_rx_handler, na);
} else {
netdev_rx_handler_unregister(ifp);
return 0;
}
#endif /* HAVE_RX_REGISTER */
}
#ifdef NETMAP_LINUX_SELECT_QUEUE
static u16
generic_ndo_select_queue(struct ifnet *ifp, struct mbuf *m
#if NETMAP_LINUX_SELECT_QUEUE >= 3
, void *accel_priv
#if NETMAP_LINUX_SELECT_QUEUE >= 4
, select_queue_fallback_t fallback
#endif /* >= 4 */
#endif /* >= 3 */
)
{
return skb_get_queue_mapping(m); // actually 0 on 2.6.23 and before
}
#endif /* SELECT_QUEUE */
/* Replacement for the driver ndo_start_xmit() method.
* When this function is invoked because of the dev_queue_xmit() call
* in generic_xmit_frame() (e.g. because of a txsync on the NIC), we have
* to call the original ndo_start_xmit() method.
* In all the other cases (e.g. when the TX request comes from the network
* stack) we intercept the packet and put it into the RX ring associated
* to the host stack.
*/
static netdev_tx_t
generic_ndo_start_xmit(struct mbuf *m, struct ifnet *ifp)
{
struct netmap_generic_adapter *gna =
(struct netmap_generic_adapter *)NA(ifp);
if (likely(m->priority == NM_MAGIC_PRIORITY_TX)) {
/* Reset priority, so that generic_netmap_tx_clean()
* knows that it can reclaim this mbuf. */
m->priority = 0;
return gna->save_start_xmit(m, ifp); /* To the driver. */
}
/* To a netmap RX ring. */
return linux_netmap_start_xmit(m, ifp);
}
struct nm_generic_qdisc {
unsigned int qidx;
unsigned int limit;
};
static int
generic_qdisc_init(struct Qdisc *qdisc, struct nlattr *opt)
{
struct nm_generic_qdisc *priv = NULL;
/* Kernel < 2.6.39, do not have qdisc->limit, so we will
* always use our priv->limit, for simplicity. */
priv = qdisc_priv(qdisc);
priv->qidx = 0;
priv->limit = 1024; /* This is going to be overridden. */
if (opt) {
struct nm_generic_qdisc *qdiscopt = nla_data(opt);
if (nla_len(opt) < sizeof(*qdiscopt)) {
D("Invalid netlink attribute");
return EINVAL;
}
priv->qidx = qdiscopt->qidx;
priv->limit = qdiscopt->limit;
D("Qdisc #%d initialized with max_len = %u", priv->qidx,
priv->limit);
}
/* Qdisc bypassing is not an option for now.
qdisc->flags |= TCQ_F_CAN_BYPASS; */
return 0;
}
static int
generic_qdisc_enqueue(struct mbuf *m, struct Qdisc *qdisc
#ifdef NETMAP_LINUX_HAVE_QDISC_ENQUEUE_TOFREE
, struct mbuf **to_free
#endif
)
{
struct nm_generic_qdisc *priv = qdisc_priv(qdisc);
if (unlikely(qdisc_qlen(qdisc) >= priv->limit)) {
RD(5, "dropping mbuf");
return qdisc_drop(m, qdisc
#ifdef NETMAP_LINUX_HAVE_QDISC_ENQUEUE_TOFREE
, to_free
#endif
);
/* or qdisc_reshape_fail() ? */
}
ND(5, "Enqueuing mbuf, len %u", qdisc_qlen(qdisc));
return qdisc_enqueue_tail(m, qdisc);
}
static struct mbuf *
generic_qdisc_dequeue(struct Qdisc *qdisc)
{
struct mbuf *m = qdisc_dequeue_head(qdisc);
if (!m) {
return NULL;
}
if (unlikely(m->priority == NM_MAGIC_PRIORITY_TXQE)) {
/* nm_os_generic_xmit_frame() asked us an event on this mbuf.
* We have to set the priority to the normal TX token, so that
* generic_ndo_start_xmit can pass it to the driver. */
m->priority = NM_MAGIC_PRIORITY_TX;
ND(5, "Event met, notify %p", m);
netmap_generic_irq(NA(qdisc_dev(qdisc)),
skb_get_queue_mapping(m), NULL);
}
ND(5, "Dequeuing mbuf, len %u", qdisc_qlen(qdisc));
return m;
}
static struct Qdisc_ops
generic_qdisc_ops __read_mostly = {
.id = "netmap_generic",
.priv_size = sizeof(struct nm_generic_qdisc),
.init = generic_qdisc_init,
.reset = qdisc_reset_queue,
.change = generic_qdisc_init,
.enqueue = generic_qdisc_enqueue,
.dequeue = generic_qdisc_dequeue,
.dump = NULL,
.owner = THIS_MODULE,
};
static int
nm_os_catch_qdisc(struct netmap_generic_adapter *gna, int intercept)
{
struct netmap_adapter *na = &gna->up.up;
struct ifnet *ifp = netmap_generic_getifp(gna);
struct nm_generic_qdisc *qdiscopt = NULL;
struct Qdisc *fqdisc = NULL;
struct nlattr *nla = NULL;
struct netdev_queue *txq;
unsigned int i;
if (!gna->txqdisc) {
return 0;
}
if (intercept) {
nla = kmalloc(nla_attr_size(sizeof(*qdiscopt)),
GFP_KERNEL);
if (!nla) {
D("Failed to allocate netlink attribute");
return ENOMEM;
}
nla->nla_type = RTM_NEWQDISC;
nla->nla_len = nla_attr_size(sizeof(*qdiscopt));
qdiscopt = (struct nm_generic_qdisc *)nla_data(nla);
memset(qdiscopt, 0, sizeof(*qdiscopt));
qdiscopt->limit = na->num_tx_desc;
}
if (ifp->flags & IFF_UP) {
dev_deactivate(ifp);
}
/* Replace the current qdiscs with our own. */
for (i = 0; i < ifp->real_num_tx_queues; i++) {
struct Qdisc *nqdisc = NULL;
struct Qdisc *oqdisc;
int err;
txq = netdev_get_tx_queue(ifp, i);
if (intercept) {
/* This takes a refcount to netmap module, alloc the
* qdisc and calls the init() op with NULL netlink
* attribute. */
nqdisc = qdisc_create_dflt(
#ifndef NETMAP_LINUX_QDISC_CREATE_DFLT_3ARGS
ifp,
#endif /* NETMAP_LINUX_QDISC_CREATE_DFLT_3ARGS */
txq, &generic_qdisc_ops,
TC_H_UNSPEC);
if (!nqdisc) {
D("Failed to create qdisc");
goto qdisc_create;
}
fqdisc = fqdisc ?: nqdisc;
/* Call the change() op passing a valid netlink
* attribute. This is used to set the queue idx. */
qdiscopt->qidx = i;
err = nqdisc->ops->change(nqdisc, nla);
if (err) {
D("Failed to init qdisc");
goto qdisc_create;
}
}
oqdisc = dev_graft_qdisc(txq, nqdisc);
/* We can call this also with
* odisc == &noop_qdisc, since the noop
* qdisc has the TCQ_F_BUILTIN flag set,
* and so qdisc_destroy will skip it. */
qdisc_destroy(oqdisc);
}
kfree(nla);
if (ifp->qdisc) {
qdisc_destroy(ifp->qdisc);
}
if (intercept) {
atomic_inc(&fqdisc->refcnt);
ifp->qdisc = fqdisc;
} else {
ifp->qdisc = &noop_qdisc;
}
if (ifp->flags & IFF_UP) {
dev_activate(ifp);
}
return 0;
qdisc_create:
if (nla) {
kfree(nla);
}
nm_os_catch_qdisc(gna, 0);
return -1;
}
/* Must be called under rtnl. */
int
nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
{
struct netmap_adapter *na = &gna->up.up;
struct ifnet *ifp = netmap_generic_getifp(gna);
int err;
err = nm_os_catch_qdisc(gna, intercept);
if (err) {
return err;
}
if (intercept) {
/*
* Save the old pointer to the netdev_ops,
* create an updated netdev ops replacing the
* ndo_select_queue() and ndo_start_xmit() methods
* with our custom ones, and make the driver use it.
*/
na->if_transmit = (void *)ifp->netdev_ops;
/* Save a redundant copy of ndo_start_xmit(). */
gna->save_start_xmit = ifp->netdev_ops->ndo_start_xmit;
gna->generic_ndo = *ifp->netdev_ops; /* Copy all */
gna->generic_ndo.ndo_start_xmit = &generic_ndo_start_xmit;
#ifndef NETMAP_LINUX_SELECT_QUEUE
D("No packet steering support");
#else
gna->generic_ndo.ndo_select_queue = &generic_ndo_select_queue;
#endif
ifp->netdev_ops = &gna->generic_ndo;
} else {
/* Restore the original netdev_ops. */
ifp->netdev_ops = (void *)na->if_transmit;
}
return 0;
}
/* Transmit routine used by generic_netmap_txsync(). Returns 0 on success
and -1 on error (which may be packet drops or other errors). */
int
nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
{
struct mbuf *m = a->m;
struct ifnet *ifp = a->ifp;
u_int len = a->len;
netdev_tx_t ret;
/* We know that the driver needs to prepend ifp->needed_headroom bytes
* to each packet to be transmitted. We then reset the mbuf pointers
* to the correct initial state:
* ___________________________________________
* ^ ^ ^
* | | |
* head data end
* tail
*
* which correspond to an empty buffer with exactly
* ifp->needed_headroom bytes between head and data.
*/
m->len = 0;
m->data = m->head + ifp->needed_headroom;
skb_reset_tail_pointer(m);
skb_reset_mac_header(m);
/* Initialize the header pointers assuming this is an IPv4 packet.
* This is useful to make netmap interact well with TC when
* netmap_generic_txqdisc == 0. */
skb_set_network_header(m, 14);
skb_set_transport_header(m, 34);
m->protocol = htons(ETH_P_IP);
m->pkt_type = PACKET_HOST;
/* Copy a netmap buffer into the mbuf.
* TODO Support the slot flags (NS_MOREFRAG, NS_INDIRECT). */
skb_copy_to_linear_data(m, a->addr, len); // skb_store_bits(m, 0, addr, len);
skb_put(m, len);
/* Hold a reference on this, we are going to recycle mbufs as
* much as possible. */
NM_ATOMIC_INC(&m->users);
/* On linux m->dev is not reliable, since it can be changed by the
* ndo_start_xmit() callback. This happens, for instance, with veth
* and bridge drivers. For this reason, the nm_os_generic_xmit_frame()
* implementation for linux stores a copy of m->dev into the
* destructor_arg field. */
m->dev = ifp;
skb_shinfo(m)->destructor_arg = m->dev;
/* Tell generic_ndo_start_xmit() to pass this mbuf to the driver. */
skb_set_queue_mapping(m, a->ring_nr);
m->priority = a->qevent ? NM_MAGIC_PRIORITY_TXQE : NM_MAGIC_PRIORITY_TX;
ret = dev_queue_xmit(m);
if (unlikely(ret != NET_XMIT_SUCCESS)) {
/* Reset priority, so that generic_netmap_tx_clean() can
* reclaim this mbuf. */
m->priority = 0;
/* Qdisc queue is full (this cannot happen with
* the netmap-aware qdisc, see exaplanation in
* netmap_generic_txsync), or qdisc is being
* deactivated. In the latter case dev_queue_xmit()
* does not call the enqueue method and returns
* NET_XMIT_DROP.
* If there is no carrier, the generic qdisc is
* not yet active (is pending in the qdisc_sleeping
* field), and so the temporary noop qdisc enqueue
* method will drop the packet and return NET_XMIT_CN.
*/
RD(3, "Warning: dev_queue_xmit() is dropping [%d]", ret);
return -1;
}
return 0;
}
void
nm_os_generic_set_features(struct netmap_generic_adapter *gna)
{
gna->rxsg = 1; /* Supported through skb_copy_bits(). */
gna->txqdisc = netmap_generic_txqdisc;
}
#endif /* WITH_GENERIC */
/* Use ethtool to find the current NIC rings lengths, so that the netmap
rings can have the same lengths. */
int
nm_os_generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
{
int error = EOPNOTSUPP;
#ifdef NETMAP_LINUX_HAVE_GET_RINGPARAM
struct ethtool_ringparam rp;
if (ifp->ethtool_ops && ifp->ethtool_ops->get_ringparam) {
ifp->ethtool_ops->get_ringparam(ifp, &rp);
*tx = rp.tx_pending ? rp.tx_pending : rp.tx_max_pending;
*rx = rp.rx_pending ? rp.rx_pending : rp.rx_max_pending;
if (*rx < 3) {
D("Invalid RX ring size %u, using default", *rx);
*rx = netmap_generic_ringsize;
}
if (*tx < 3) {
D("Invalid TX ring size %u, using default", *tx);
*tx = netmap_generic_ringsize;
}
error = 0;
}
#endif /* HAVE_GET_RINGPARAM */
return error;
}
/* Fills in the output arguments with the number of hardware TX/RX queues. */
void
nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
{
#ifdef NETMAP_LINUX_HAVE_SET_CHANNELS
struct ethtool_channels ch;
memset(&ch, 0, sizeof(ch));
if (ifp->ethtool_ops && ifp->ethtool_ops->get_channels) {
ifp->ethtool_ops->get_channels(ifp, &ch);
*txq = ch.tx_count ? ch.tx_count : ch.combined_count;
*rxq = ch.rx_count ? ch.rx_count : ch.combined_count;
} else
#endif /* HAVE_SET_CHANNELS */
{
*txq = ifp->real_num_tx_queues;
#if defined(NETMAP_LINUX_HAVE_REAL_NUM_RX_QUEUES)
*rxq = ifp->real_num_rx_queues;
#else
*rxq = 1;
#endif /* HAVE_REAL_NUM_RX_QUEUES */
}
}
int
netmap_linux_config(struct netmap_adapter *na,
u_int *txr, u_int *txd, u_int *rxr, u_int *rxd)
{
struct ifnet *ifp = na->ifp;
int error = 0;
rtnl_lock();
if (ifp == NULL) {
D("zombie adapter");
error = ENXIO;
goto out;
}
error = nm_os_generic_find_num_desc(ifp, txd, rxd);
if (error)
goto out;
nm_os_generic_find_num_queues(ifp, txr, rxr);
out:
rtnl_unlock();
return error;
}
/* ######################## FILE OPERATIONS ####################### */
struct net_device *
ifunit_ref(const char *name)
{
#ifndef NETMAP_LINUX_HAVE_INIT_NET
return dev_get_by_name(name);
#else
void *ns = &init_net;
#ifdef CONFIG_NET_NS
ns = current->nsproxy->net_ns;
#endif
return dev_get_by_name(ns, name);
#endif
}
void if_ref(struct net_device *ifp)
{
dev_hold(ifp);
}
void if_rele(struct net_device *ifp)
{
dev_put(ifp);
}
struct nm_linux_selrecord_t {
struct file *file;
struct poll_table_struct *pwait;
};
/*
* Remap linux arguments into the FreeBSD call.
* - pwait is the poll table, passed as 'dev';
* If pwait == NULL someone else already woke up before. We can report
* events but they are filtered upstream.
* If pwait != NULL, then pwait->key contains the list of events.
* - events is computed from pwait as above.
* - file is passed as 'td';
*/
static u_int
linux_netmap_poll(struct file * file, struct poll_table_struct *pwait)
{
#ifdef NETMAP_LINUX_PWAIT_KEY
int events = pwait ? pwait->NETMAP_LINUX_PWAIT_KEY : \
POLLIN | POLLOUT | POLLERR;
#else
int events = POLLIN | POLLOUT; /* XXX maybe... */
#endif /* PWAIT_KEY */
struct nm_linux_selrecord_t sr = {
.file = file,
.pwait = pwait
};
struct netmap_priv_d *priv = file->private_data;
return netmap_poll(priv, events, &sr);
}
static int
linux_netmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
struct netmap_priv_d *priv = vma->vm_private_data;
struct netmap_adapter *na = priv->np_na;
struct page *page;
unsigned long off = (vma->vm_pgoff + vmf->pgoff) << PAGE_SHIFT;
unsigned long pa, pfn;
pa = netmap_mem_ofstophys(na->nm_mem, off);
ND("fault off %lx -> phys addr %lx", off, pa);
if (pa == 0)
return VM_FAULT_SIGBUS;
pfn = pa >> PAGE_SHIFT;
if (!pfn_valid(pfn))
return VM_FAULT_SIGBUS;
page = pfn_to_page(pfn);
get_page(page);
vmf->page = page;
return 0;
}
static struct vm_operations_struct linux_netmap_mmap_ops = {
.fault = linux_netmap_fault,
};
static int
linux_netmap_mmap(struct file *f, struct vm_area_struct *vma)
{
int error = 0;
unsigned long off;
u_int memsize, memflags;
struct netmap_priv_d *priv = f->private_data;
struct netmap_adapter *na = priv->np_na;
/*
* vma->vm_start: start of mapping user address space
* vma->vm_end: end of the mapping user address space
* vma->vm_pfoff: offset of first page in the device
*/
if (priv->np_nifp == NULL) {
return -EINVAL;
}
mb();
/* check that [off, off + vsize) is within our memory */
error = netmap_mem_get_info(na->nm_mem, &memsize, &memflags, NULL);
ND("get_info returned %d", error);
if (error)
return -error;
off = vma->vm_pgoff << PAGE_SHIFT;
ND("off %lx size %lx memsize %x", off,
(vma->vm_end - vma->vm_start), memsize);
if (off + (vma->vm_end - vma->vm_start) > memsize)
return -EINVAL;
if (memflags & NETMAP_MEM_IO) {
vm_ooffset_t pa;
/* the underlying memory is contiguous */
pa = netmap_mem_ofstophys(na->nm_mem, 0);
if (pa == 0)
return -EINVAL;
return remap_pfn_range(vma, vma->vm_start,
pa >> PAGE_SHIFT,
vma->vm_end - vma->vm_start,
vma->vm_page_prot);
} else {
/* non contiguous memory, we serve
* page faults as they come
*/
vma->vm_private_data = priv;
vma->vm_ops = &linux_netmap_mmap_ops;
}
return 0;
}
/*
* This one is probably already protected by the netif lock XXX