-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
netfront.c
2344 lines (1954 loc) · 54.7 KB
/
netfront.c
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
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2004-2006 Kip Macy
* Copyright (c) 2015 Wei Liu <wei.liu2@citrix.com>
* 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/param.h>
#include <sys/sockio.h>
#include <sys/limits.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_media.h>
#include <net/bpf.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <netinet/tcp.h>
#include <netinet/tcp_lro.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <sys/bus.h>
#include <xen/xen-os.h>
#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
#include <xen/gnttab.h>
#include <xen/interface/memory.h>
#include <xen/interface/io/netif.h>
#include <xen/xenbus/xenbusvar.h>
#include "xenbus_if.h"
/* Features supported by all backends. TSO and LRO can be negotiated */
#define XN_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
#define NET_TX_RING_SIZE __CONST_RING_SIZE(netif_tx, PAGE_SIZE)
#define NET_RX_RING_SIZE __CONST_RING_SIZE(netif_rx, PAGE_SIZE)
#define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
/*
* Should the driver do LRO on the RX end
* this can be toggled on the fly, but the
* interface must be reset (down/up) for it
* to take effect.
*/
static int xn_enable_lro = 1;
TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro);
/*
* Number of pairs of queues.
*/
static unsigned long xn_num_queues = 4;
TUNABLE_ULONG("hw.xn.num_queues", &xn_num_queues);
/**
* \brief The maximum allowed data fragments in a single transmit
* request.
*
* This limit is imposed by the backend driver. We assume here that
* we are dealing with a Linux driver domain and have set our limit
* to mirror the Linux MAX_SKB_FRAGS constant.
*/
#define MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2)
#define RX_COPY_THRESHOLD 256
#define net_ratelimit() 0
struct netfront_rxq;
struct netfront_txq;
struct netfront_info;
struct netfront_rx_info;
static void xn_txeof(struct netfront_txq *);
static void xn_rxeof(struct netfront_rxq *);
static void xn_alloc_rx_buffers(struct netfront_rxq *);
static void xn_alloc_rx_buffers_callout(void *arg);
static void xn_release_rx_bufs(struct netfront_rxq *);
static void xn_release_tx_bufs(struct netfront_txq *);
static void xn_rxq_intr(struct netfront_rxq *);
static void xn_txq_intr(struct netfront_txq *);
static void xn_intr(void *);
static inline int xn_count_frags(struct mbuf *m);
static int xn_assemble_tx_request(struct netfront_txq *, struct mbuf *);
static int xn_ioctl(struct ifnet *, u_long, caddr_t);
static void xn_ifinit_locked(struct netfront_info *);
static void xn_ifinit(void *);
static void xn_stop(struct netfront_info *);
static void xn_query_features(struct netfront_info *np);
static int xn_configure_features(struct netfront_info *np);
static void netif_free(struct netfront_info *info);
static int netfront_detach(device_t dev);
static int xn_txq_mq_start_locked(struct netfront_txq *, struct mbuf *);
static int xn_txq_mq_start(struct ifnet *, struct mbuf *);
static int talk_to_backend(device_t dev, struct netfront_info *info);
static int create_netdev(device_t dev);
static void netif_disconnect_backend(struct netfront_info *info);
static int setup_device(device_t dev, struct netfront_info *info,
unsigned long);
static int xn_ifmedia_upd(struct ifnet *ifp);
static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
static int xn_connect(struct netfront_info *);
static void xn_kick_rings(struct netfront_info *);
static int xn_get_responses(struct netfront_rxq *,
struct netfront_rx_info *, RING_IDX, RING_IDX *,
struct mbuf **);
#define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT)
#define INVALID_P2M_ENTRY (~0UL)
#define XN_QUEUE_NAME_LEN 8 /* xn{t,r}x_%u, allow for two digits */
struct netfront_rxq {
struct netfront_info *info;
u_int id;
char name[XN_QUEUE_NAME_LEN];
struct mtx lock;
int ring_ref;
netif_rx_front_ring_t ring;
xen_intr_handle_t xen_intr_handle;
grant_ref_t gref_head;
grant_ref_t grant_ref[NET_RX_RING_SIZE + 1];
struct mbuf *mbufs[NET_RX_RING_SIZE + 1];
struct lro_ctrl lro;
struct callout rx_refill;
};
struct netfront_txq {
struct netfront_info *info;
u_int id;
char name[XN_QUEUE_NAME_LEN];
struct mtx lock;
int ring_ref;
netif_tx_front_ring_t ring;
xen_intr_handle_t xen_intr_handle;
grant_ref_t gref_head;
grant_ref_t grant_ref[NET_TX_RING_SIZE + 1];
struct mbuf *mbufs[NET_TX_RING_SIZE + 1];
int mbufs_cnt;
struct buf_ring *br;
struct taskqueue *tq;
struct task defrtask;
bool full;
};
struct netfront_info {
struct ifnet *xn_ifp;
struct mtx sc_lock;
u_int num_queues;
struct netfront_rxq *rxq;
struct netfront_txq *txq;
u_int carrier;
u_int maxfrags;
device_t xbdev;
uint8_t mac[ETHER_ADDR_LEN];
int xn_if_flags;
struct ifmedia sc_media;
bool xn_reset;
};
struct netfront_rx_info {
struct netif_rx_response rx;
struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
};
#define XN_RX_LOCK(_q) mtx_lock(&(_q)->lock)
#define XN_RX_UNLOCK(_q) mtx_unlock(&(_q)->lock)
#define XN_TX_LOCK(_q) mtx_lock(&(_q)->lock)
#define XN_TX_TRYLOCK(_q) mtx_trylock(&(_q)->lock)
#define XN_TX_UNLOCK(_q) mtx_unlock(&(_q)->lock)
#define XN_LOCK(_sc) mtx_lock(&(_sc)->sc_lock);
#define XN_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_lock);
#define XN_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_lock, MA_OWNED);
#define XN_RX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED);
#define XN_TX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED);
#define netfront_carrier_on(netif) ((netif)->carrier = 1)
#define netfront_carrier_off(netif) ((netif)->carrier = 0)
#define netfront_carrier_ok(netif) ((netif)->carrier)
/* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
static inline void
add_id_to_freelist(struct mbuf **list, uintptr_t id)
{
KASSERT(id != 0,
("%s: the head item (0) must always be free.", __func__));
list[id] = list[0];
list[0] = (struct mbuf *)id;
}
static inline unsigned short
get_id_from_freelist(struct mbuf **list)
{
uintptr_t id;
id = (uintptr_t)list[0];
KASSERT(id != 0,
("%s: the head item (0) must always remain free.", __func__));
list[0] = list[id];
return (id);
}
static inline int
xn_rxidx(RING_IDX idx)
{
return idx & (NET_RX_RING_SIZE - 1);
}
static inline struct mbuf *
xn_get_rx_mbuf(struct netfront_rxq *rxq, RING_IDX ri)
{
int i;
struct mbuf *m;
i = xn_rxidx(ri);
m = rxq->mbufs[i];
rxq->mbufs[i] = NULL;
return (m);
}
static inline grant_ref_t
xn_get_rx_ref(struct netfront_rxq *rxq, RING_IDX ri)
{
int i = xn_rxidx(ri);
grant_ref_t ref = rxq->grant_ref[i];
KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n"));
rxq->grant_ref[i] = GRANT_REF_INVALID;
return (ref);
}
#define IPRINTK(fmt, args...) \
printf("[XEN] " fmt, ##args)
#ifdef INVARIANTS
#define WPRINTK(fmt, args...) \
printf("[XEN] " fmt, ##args)
#else
#define WPRINTK(fmt, args...)
#endif
#ifdef DEBUG
#define DPRINTK(fmt, args...) \
printf("[XEN] %s: " fmt, __func__, ##args)
#else
#define DPRINTK(fmt, args...)
#endif
/**
* Read the 'mac' node at the given device's node in the store, and parse that
* as colon-separated octets, placing result the given mac array. mac must be
* a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
* Return 0 on success, or errno on error.
*/
static int
xen_net_read_mac(device_t dev, uint8_t mac[])
{
int error, i;
char *s, *e, *macstr;
const char *path;
path = xenbus_get_node(dev);
error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
if (error == ENOENT) {
/*
* Deal with missing mac XenStore nodes on devices with
* HVM emulation (the 'ioemu' configuration attribute)
* enabled.
*
* The HVM emulator may execute in a stub device model
* domain which lacks the permission, only given to Dom0,
* to update the guest's XenStore tree. For this reason,
* the HVM emulator doesn't even attempt to write the
* front-side mac node, even when operating in Dom0.
* However, there should always be a mac listed in the
* backend tree. Fallback to this version if our query
* of the front side XenStore location doesn't find
* anything.
*/
path = xenbus_get_otherend_path(dev);
error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
}
if (error != 0) {
xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
return (error);
}
s = macstr;
for (i = 0; i < ETHER_ADDR_LEN; i++) {
mac[i] = strtoul(s, &e, 16);
if (s == e || (e[0] != ':' && e[0] != 0)) {
free(macstr, M_XENBUS);
return (ENOENT);
}
s = &e[1];
}
free(macstr, M_XENBUS);
return (0);
}
/**
* Entry point to this code when a new device is created. Allocate the basic
* structures and the ring buffers for communication with the backend, and
* inform the backend of the appropriate details for those. Switch to
* Connected state.
*/
static int
netfront_probe(device_t dev)
{
if (xen_hvm_domain() && xen_disable_pv_nics != 0)
return (ENXIO);
if (!strcmp(xenbus_get_type(dev), "vif")) {
device_set_desc(dev, "Virtual Network Interface");
return (0);
}
return (ENXIO);
}
static int
netfront_attach(device_t dev)
{
int err;
err = create_netdev(dev);
if (err != 0) {
xenbus_dev_fatal(dev, err, "creating netdev");
return (err);
}
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "enable_lro", CTLFLAG_RW,
&xn_enable_lro, 0, "Large Receive Offload");
SYSCTL_ADD_ULONG(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "num_queues", CTLFLAG_RD,
&xn_num_queues, "Number of pairs of queues");
return (0);
}
static int
netfront_suspend(device_t dev)
{
struct netfront_info *np = device_get_softc(dev);
u_int i;
for (i = 0; i < np->num_queues; i++) {
XN_RX_LOCK(&np->rxq[i]);
XN_TX_LOCK(&np->txq[i]);
}
netfront_carrier_off(np);
for (i = 0; i < np->num_queues; i++) {
XN_RX_UNLOCK(&np->rxq[i]);
XN_TX_UNLOCK(&np->txq[i]);
}
return (0);
}
/**
* We are reconnecting to the backend, due to a suspend/resume, or a backend
* driver restart. We tear down our netif structure and recreate it, but
* leave the device-layer structures intact so that this is transparent to the
* rest of the kernel.
*/
static int
netfront_resume(device_t dev)
{
struct netfront_info *info = device_get_softc(dev);
u_int i;
if (xen_suspend_cancelled) {
for (i = 0; i < info->num_queues; i++) {
XN_RX_LOCK(&info->rxq[i]);
XN_TX_LOCK(&info->txq[i]);
}
netfront_carrier_on(info);
for (i = 0; i < info->num_queues; i++) {
XN_RX_UNLOCK(&info->rxq[i]);
XN_TX_UNLOCK(&info->txq[i]);
}
return (0);
}
netif_disconnect_backend(info);
return (0);
}
static int
write_queue_xenstore_keys(device_t dev,
struct netfront_rxq *rxq,
struct netfront_txq *txq,
struct xs_transaction *xst, bool hierarchy)
{
int err;
const char *message;
const char *node = xenbus_get_node(dev);
char *path;
size_t path_size;
KASSERT(rxq->id == txq->id, ("Mismatch between RX and TX queue ids"));
/* Split event channel support is not yet there. */
KASSERT(rxq->xen_intr_handle == txq->xen_intr_handle,
("Split event channels are not supported"));
if (hierarchy) {
path_size = strlen(node) + 10;
path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO);
snprintf(path, path_size, "%s/queue-%u", node, rxq->id);
} else {
path_size = strlen(node) + 1;
path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO);
snprintf(path, path_size, "%s", node);
}
err = xs_printf(*xst, path, "tx-ring-ref","%u", txq->ring_ref);
if (err != 0) {
message = "writing tx ring-ref";
goto error;
}
err = xs_printf(*xst, path, "rx-ring-ref","%u", rxq->ring_ref);
if (err != 0) {
message = "writing rx ring-ref";
goto error;
}
err = xs_printf(*xst, path, "event-channel", "%u",
xen_intr_port(rxq->xen_intr_handle));
if (err != 0) {
message = "writing event-channel";
goto error;
}
free(path, M_DEVBUF);
return (0);
error:
free(path, M_DEVBUF);
xenbus_dev_fatal(dev, err, "%s", message);
return (err);
}
/* Common code used when first setting up, and when resuming. */
static int
talk_to_backend(device_t dev, struct netfront_info *info)
{
const char *message;
struct xs_transaction xst;
const char *node = xenbus_get_node(dev);
int err;
unsigned long num_queues, max_queues = 0;
unsigned int i;
err = xen_net_read_mac(dev, info->mac);
if (err != 0) {
xenbus_dev_fatal(dev, err, "parsing %s/mac", node);
goto out;
}
err = xs_scanf(XST_NIL, xenbus_get_otherend_path(info->xbdev),
"multi-queue-max-queues", NULL, "%lu", &max_queues);
if (err != 0)
max_queues = 1;
num_queues = xn_num_queues;
if (num_queues > max_queues)
num_queues = max_queues;
err = setup_device(dev, info, num_queues);
if (err != 0)
goto out;
again:
err = xs_transaction_start(&xst);
if (err != 0) {
xenbus_dev_fatal(dev, err, "starting transaction");
goto free;
}
if (info->num_queues == 1) {
err = write_queue_xenstore_keys(dev, &info->rxq[0],
&info->txq[0], &xst, false);
if (err != 0)
goto abort_transaction_no_def_error;
} else {
err = xs_printf(xst, node, "multi-queue-num-queues",
"%u", info->num_queues);
if (err != 0) {
message = "writing multi-queue-num-queues";
goto abort_transaction;
}
for (i = 0; i < info->num_queues; i++) {
err = write_queue_xenstore_keys(dev, &info->rxq[i],
&info->txq[i], &xst, true);
if (err != 0)
goto abort_transaction_no_def_error;
}
}
err = xs_printf(xst, node, "request-rx-copy", "%u", 1);
if (err != 0) {
message = "writing request-rx-copy";
goto abort_transaction;
}
err = xs_printf(xst, node, "feature-rx-notify", "%d", 1);
if (err != 0) {
message = "writing feature-rx-notify";
goto abort_transaction;
}
err = xs_printf(xst, node, "feature-sg", "%d", 1);
if (err != 0) {
message = "writing feature-sg";
goto abort_transaction;
}
if ((info->xn_ifp->if_capenable & IFCAP_LRO) != 0) {
err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1);
if (err != 0) {
message = "writing feature-gso-tcpv4";
goto abort_transaction;
}
}
if ((info->xn_ifp->if_capenable & IFCAP_RXCSUM) == 0) {
err = xs_printf(xst, node, "feature-no-csum-offload", "%d", 1);
if (err != 0) {
message = "writing feature-no-csum-offload";
goto abort_transaction;
}
}
err = xs_transaction_end(xst, 0);
if (err != 0) {
if (err == EAGAIN)
goto again;
xenbus_dev_fatal(dev, err, "completing transaction");
goto free;
}
return 0;
abort_transaction:
xenbus_dev_fatal(dev, err, "%s", message);
abort_transaction_no_def_error:
xs_transaction_end(xst, 1);
free:
netif_free(info);
out:
return (err);
}
static void
xn_rxq_intr(struct netfront_rxq *rxq)
{
XN_RX_LOCK(rxq);
xn_rxeof(rxq);
XN_RX_UNLOCK(rxq);
}
static void
xn_txq_start(struct netfront_txq *txq)
{
struct netfront_info *np = txq->info;
struct ifnet *ifp = np->xn_ifp;
XN_TX_LOCK_ASSERT(txq);
if (!drbr_empty(ifp, txq->br))
xn_txq_mq_start_locked(txq, NULL);
}
static void
xn_txq_intr(struct netfront_txq *txq)
{
XN_TX_LOCK(txq);
if (RING_HAS_UNCONSUMED_RESPONSES(&txq->ring))
xn_txeof(txq);
xn_txq_start(txq);
XN_TX_UNLOCK(txq);
}
static void
xn_txq_tq_deferred(void *xtxq, int pending)
{
struct netfront_txq *txq = xtxq;
XN_TX_LOCK(txq);
xn_txq_start(txq);
XN_TX_UNLOCK(txq);
}
static void
disconnect_rxq(struct netfront_rxq *rxq)
{
xn_release_rx_bufs(rxq);
gnttab_free_grant_references(rxq->gref_head);
gnttab_end_foreign_access(rxq->ring_ref, NULL);
/*
* No split event channel support at the moment, handle will
* be unbound in tx. So no need to call xen_intr_unbind here,
* but we do want to reset the handler to 0.
*/
rxq->xen_intr_handle = 0;
}
static void
destroy_rxq(struct netfront_rxq *rxq)
{
callout_drain(&rxq->rx_refill);
free(rxq->ring.sring, M_DEVBUF);
}
static void
destroy_rxqs(struct netfront_info *np)
{
int i;
for (i = 0; i < np->num_queues; i++)
destroy_rxq(&np->rxq[i]);
free(np->rxq, M_DEVBUF);
np->rxq = NULL;
}
static int
setup_rxqs(device_t dev, struct netfront_info *info,
unsigned long num_queues)
{
int q, i;
int error;
netif_rx_sring_t *rxs;
struct netfront_rxq *rxq;
info->rxq = malloc(sizeof(struct netfront_rxq) * num_queues,
M_DEVBUF, M_WAITOK|M_ZERO);
for (q = 0; q < num_queues; q++) {
rxq = &info->rxq[q];
rxq->id = q;
rxq->info = info;
rxq->ring_ref = GRANT_REF_INVALID;
rxq->ring.sring = NULL;
snprintf(rxq->name, XN_QUEUE_NAME_LEN, "xnrx_%u", q);
mtx_init(&rxq->lock, rxq->name, "netfront receive lock",
MTX_DEF);
for (i = 0; i <= NET_RX_RING_SIZE; i++) {
rxq->mbufs[i] = NULL;
rxq->grant_ref[i] = GRANT_REF_INVALID;
}
/* Start resources allocation */
if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
&rxq->gref_head) != 0) {
device_printf(dev, "allocating rx gref");
error = ENOMEM;
goto fail;
}
rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF,
M_WAITOK|M_ZERO);
SHARED_RING_INIT(rxs);
FRONT_RING_INIT(&rxq->ring, rxs, PAGE_SIZE);
error = xenbus_grant_ring(dev, virt_to_mfn(rxs),
&rxq->ring_ref);
if (error != 0) {
device_printf(dev, "granting rx ring page");
goto fail_grant_ring;
}
callout_init(&rxq->rx_refill, 1);
}
return (0);
fail_grant_ring:
gnttab_free_grant_references(rxq->gref_head);
free(rxq->ring.sring, M_DEVBUF);
fail:
for (; q >= 0; q--) {
disconnect_rxq(&info->rxq[q]);
destroy_rxq(&info->rxq[q]);
}
free(info->rxq, M_DEVBUF);
return (error);
}
static void
disconnect_txq(struct netfront_txq *txq)
{
xn_release_tx_bufs(txq);
gnttab_free_grant_references(txq->gref_head);
gnttab_end_foreign_access(txq->ring_ref, NULL);
xen_intr_unbind(&txq->xen_intr_handle);
}
static void
destroy_txq(struct netfront_txq *txq)
{
free(txq->ring.sring, M_DEVBUF);
buf_ring_free(txq->br, M_DEVBUF);
taskqueue_drain_all(txq->tq);
taskqueue_free(txq->tq);
}
static void
destroy_txqs(struct netfront_info *np)
{
int i;
for (i = 0; i < np->num_queues; i++)
destroy_txq(&np->txq[i]);
free(np->txq, M_DEVBUF);
np->txq = NULL;
}
static int
setup_txqs(device_t dev, struct netfront_info *info,
unsigned long num_queues)
{
int q, i;
int error;
netif_tx_sring_t *txs;
struct netfront_txq *txq;
info->txq = malloc(sizeof(struct netfront_txq) * num_queues,
M_DEVBUF, M_WAITOK|M_ZERO);
for (q = 0; q < num_queues; q++) {
txq = &info->txq[q];
txq->id = q;
txq->info = info;
txq->ring_ref = GRANT_REF_INVALID;
txq->ring.sring = NULL;
snprintf(txq->name, XN_QUEUE_NAME_LEN, "xntx_%u", q);
mtx_init(&txq->lock, txq->name, "netfront transmit lock",
MTX_DEF);
for (i = 0; i <= NET_TX_RING_SIZE; i++) {
txq->mbufs[i] = (void *) ((u_long) i+1);
txq->grant_ref[i] = GRANT_REF_INVALID;
}
txq->mbufs[NET_TX_RING_SIZE] = (void *)0;
/* Start resources allocation. */
if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
&txq->gref_head) != 0) {
device_printf(dev, "failed to allocate tx grant refs\n");
error = ENOMEM;
goto fail;
}
txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF,
M_WAITOK|M_ZERO);
SHARED_RING_INIT(txs);
FRONT_RING_INIT(&txq->ring, txs, PAGE_SIZE);
error = xenbus_grant_ring(dev, virt_to_mfn(txs),
&txq->ring_ref);
if (error != 0) {
device_printf(dev, "failed to grant tx ring\n");
goto fail_grant_ring;
}
txq->br = buf_ring_alloc(NET_TX_RING_SIZE, M_DEVBUF,
M_WAITOK, &txq->lock);
TASK_INIT(&txq->defrtask, 0, xn_txq_tq_deferred, txq);
txq->tq = taskqueue_create(txq->name, M_WAITOK,
taskqueue_thread_enqueue, &txq->tq);
error = taskqueue_start_threads(&txq->tq, 1, PI_NET,
"%s txq %d", device_get_nameunit(dev), txq->id);
if (error != 0) {
device_printf(dev, "failed to start tx taskq %d\n",
txq->id);
goto fail_start_thread;
}
error = xen_intr_alloc_and_bind_local_port(dev,
xenbus_get_otherend_id(dev), /* filter */ NULL, xn_intr,
&info->txq[q], INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY,
&txq->xen_intr_handle);
if (error != 0) {
device_printf(dev, "xen_intr_alloc_and_bind_local_port failed\n");
goto fail_bind_port;
}
}
return (0);
fail_bind_port:
taskqueue_drain_all(txq->tq);
fail_start_thread:
buf_ring_free(txq->br, M_DEVBUF);
taskqueue_free(txq->tq);
gnttab_end_foreign_access(txq->ring_ref, NULL);
fail_grant_ring:
gnttab_free_grant_references(txq->gref_head);
free(txq->ring.sring, M_DEVBUF);
fail:
for (; q >= 0; q--) {
disconnect_txq(&info->txq[q]);
destroy_txq(&info->txq[q]);
}
free(info->txq, M_DEVBUF);
return (error);
}
static int
setup_device(device_t dev, struct netfront_info *info,
unsigned long num_queues)
{
int error;
int q;
if (info->txq)
destroy_txqs(info);
if (info->rxq)
destroy_rxqs(info);
info->num_queues = 0;
error = setup_rxqs(dev, info, num_queues);
if (error != 0)
goto out;
error = setup_txqs(dev, info, num_queues);
if (error != 0)
goto out;
info->num_queues = num_queues;
/* No split event channel at the moment. */
for (q = 0; q < num_queues; q++)
info->rxq[q].xen_intr_handle = info->txq[q].xen_intr_handle;
return (0);
out:
KASSERT(error != 0, ("Error path taken without providing an error code"));
return (error);
}
#ifdef INET
/**
* If this interface has an ipv4 address, send an arp for it. This
* helps to get the network going again after migrating hosts.
*/
static void
netfront_send_fake_arp(device_t dev, struct netfront_info *info)
{
struct ifnet *ifp;
struct ifaddr *ifa;
ifp = info->xn_ifp;
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
if (ifa->ifa_addr->sa_family == AF_INET) {
arp_ifinit(ifp, ifa);
}
}
}
#endif
/**
* Callback received when the backend's state changes.
*/
static void
netfront_backend_changed(device_t dev, XenbusState newstate)
{
struct netfront_info *sc = device_get_softc(dev);
DPRINTK("newstate=%d\n", newstate);
CURVNET_SET(sc->xn_ifp->if_vnet);
switch (newstate) {
case XenbusStateInitialising:
case XenbusStateInitialised:
case XenbusStateUnknown:
case XenbusStateReconfigured:
case XenbusStateReconfiguring:
break;
case XenbusStateInitWait:
if (xenbus_get_state(dev) != XenbusStateInitialising)
break;
if (xn_connect(sc) != 0)
break;
/* Switch to connected state before kicking the rings. */
xenbus_set_state(sc->xbdev, XenbusStateConnected);
xn_kick_rings(sc);
break;
case XenbusStateClosing:
xenbus_set_state(dev, XenbusStateClosed);
break;
case XenbusStateClosed:
if (sc->xn_reset) {
netif_disconnect_backend(sc);
xenbus_set_state(dev, XenbusStateInitialising);
sc->xn_reset = false;
}
break;
case XenbusStateConnected:
#ifdef INET
netfront_send_fake_arp(dev, sc);
#endif
break;
}
CURVNET_RESTORE();