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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
|
/* $NetBSD: if_xennet.c,v 1.1.2.1 2004/05/22 15:58:29 he Exp $ */
/*
*
* Copyright (c) 2004 Christian Limpach.
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christian Limpach.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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>
__KERNEL_RCSID(0, "$NetBSD: if_xennet.c,v 1.1.2.1 2004/05/22 15:58:29 he Exp $");
#include "opt_inet.h"
#include "rnd.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#if NRND > 0
#include <sys/rnd.h>
#endif
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#ifdef mediacode
#include <net/if_media.h>
#endif
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#endif
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/nfsmount.h>
#include <nfs/nfsdiskless.h>
#include "bpfilter.h"
#if NBPFILTER > 0
#include <net/bpf.h>
#include <net/bpfdesc.h>
#endif
#include <uvm/uvm_extern.h>
#include <uvm/uvm_page.h>
#include <machine/xen.h>
#include <machine/hypervisor.h>
#include <machine/evtchn.h>
#include <machine/ctrl_if.h>
#include <machine/if_xennetvar.h>
#ifdef DEBUG
#define XENNET_DEBUG
#endif
#if defined(XENNET_DEBUG) && !defined(DEBUG)
#define DEBUG
#endif
/* #define XENNET_DEBUG_DUMP */
#ifdef XENNET_DEBUG
#define XEDB_FOLLOW 0x01
#define XEDB_INIT 0x02
#define XEDB_EVENT 0x04
#define XEDB_MBUF 0x08
#define XEDB_MEM 0x10
int xennet_debug = 0x0;
void printk(char *, ...);
#define DPRINTF(x) if (xennet_debug) printk x;
#define DPRINTFN(n,x) if (xennet_debug & (n)) printk x;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
#define PRINTF(x) printf x;
#ifdef XENNET_DEBUG_DUMP
static void xennet_hex_dump(unsigned char *, size_t, char *, int);
#endif
int xennet_match (struct device *, struct cfdata *, void *);
void xennet_attach (struct device *, struct device *, void *);
static void xennet_ctrlif_rx(ctrl_msg_t *, unsigned long);
static int xennet_driver_count_connected(void);
static void xennet_driver_status_change(netif_fe_driver_status_t *);
static void xennet_interface_status_change(netif_fe_interface_status_t *);
static void xennet_tx_mbuf_free(struct mbuf *, caddr_t, size_t, void *);
static void xennet_rx_mbuf_free(struct mbuf *, caddr_t, size_t, void *);
static int xen_network_handler(void *);
static void network_tx_buf_gc(struct xennet_softc *);
static void network_alloc_rx_buffers(struct xennet_softc *);
static void network_alloc_tx_buffers(struct xennet_softc *);
void xennet_init(struct xennet_softc *);
void xennet_reset(struct xennet_softc *);
#ifdef mediacode
static int xennet_mediachange (struct ifnet *);
static void xennet_mediastatus(struct ifnet *, struct ifmediareq *);
#endif
CFATTACH_DECL(xennet, sizeof(struct xennet_softc),
xennet_match, xennet_attach, NULL, NULL);
#define TX_MAX_ENTRIES (NETIF_TX_RING_SIZE - 2)
#define RX_MAX_ENTRIES (NETIF_RX_RING_SIZE - 2)
#define TX_ENTRIES 128
#define RX_ENTRIES 128
static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
/** Network interface info. */
struct xennet_ctrl {
/** Number of interfaces. */
int xc_interfaces;
/** Number of connected interfaces. */
int xc_connected;
/** Error code. */
int xc_err;
/** Driver status. */
int xc_up;
cfprint_t xc_cfprint;
struct device *xc_parent;
};
static struct xennet_ctrl netctrl = { -1, 0, 0 };
#ifdef mediacode
static int xennet_media[] = {
IFM_ETHER|IFM_AUTO,
};
static int nxennet_media = (sizeof(xennet_media)/sizeof(xennet_media[0]));
#endif
static int
xennet_wait_for_interfaces(void)
{
while (netctrl.xc_interfaces != netctrl.xc_connected)
HYPERVISOR_yield();
return 0;
}
int
xennet_scan(struct device *self, struct xennet_attach_args *xneta,
cfprint_t print)
{
ctrl_msg_t cmsg;
netif_fe_driver_status_t st;
if ((xen_start_info.flags & SIF_INITDOMAIN) ||
(xen_start_info.flags & SIF_NET_BE_DOMAIN))
return 0;
netctrl.xc_parent = self;
netctrl.xc_cfprint = print;
printf("Initialising Xen virtual ethernet frontend driver.\n");
(void)ctrl_if_register_receiver(CMSG_NETIF_FE, xennet_ctrlif_rx,
CALLBACK_IN_BLOCKING_CONTEXT);
/* Send a driver-UP notification to the domain controller. */
cmsg.type = CMSG_NETIF_FE;
cmsg.subtype = CMSG_NETIF_FE_DRIVER_STATUS;
cmsg.length = sizeof(netif_fe_driver_status_t);
st.status = NETIF_DRIVER_STATUS_UP;
st.max_handle = 0;
memcpy(cmsg.msg, &st, sizeof(st));
ctrl_if_send_message_block(&cmsg, NULL, 0, 0);
return 0;
}
void
xennet_scan_finish(struct device *parent)
{
int err;
err = xennet_wait_for_interfaces();
if (err)
ctrl_if_unregister_receiver(CMSG_NETIF_FE, xennet_ctrlif_rx);
}
int
xennet_match(struct device *parent, struct cfdata *match, void *aux)
{
struct xennet_attach_args *xa = (struct xennet_attach_args *)aux;
if (strcmp(xa->xa_device, "xennet") == 0)
return 1;
return 0;
}
void
xennet_attach(struct device *parent, struct device *self, void *aux)
{
struct xennet_attach_args *xneta = (struct xennet_attach_args *)aux;
struct xennet_softc *sc = (struct xennet_softc *)self;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int idx;
aprint_normal(": Xen Virtual Network Interface\n");
sc->sc_ifno = xneta->xa_handle;
/* Initialize ifnet structure. */
memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
ifp->if_softc = sc;
ifp->if_start = xennet_start;
ifp->if_ioctl = xennet_ioctl;
ifp->if_watchdog = xennet_watchdog;
ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS;
#ifdef mediacode
ifmedia_init(&sc->sc_media, 0, xennet_mediachange,
xennet_mediastatus);
for (idx = 0; idx < nxennet_media; idx++)
ifmedia_add(&sc->sc_media, xennet_media[idx], 0, NULL);
ifmedia_set(&sc->sc_media, xennet_media[0]);
#endif
for (idx = 0; idx < NETIF_TX_RING_SIZE; idx++)
sc->sc_tx_bufa[idx].xb_next = idx + 1;
for (idx = 0; idx < NETIF_RX_RING_SIZE; idx++)
sc->sc_rx_bufa[idx].xb_next = idx + 1;
}
static struct xennet_softc *
find_device(int handle)
{
struct device *dv;
struct xennet_softc *xs = NULL;
for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
if (dv->dv_cfattach == NULL ||
dv->dv_cfattach->ca_attach != xennet_attach)
continue;
xs = (struct xennet_softc *)dv;
if (xs->sc_ifno == handle)
break;
}
return dv ? xs : NULL;
}
static void
xennet_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
{
int respond = 1;
DPRINTFN(XEDB_EVENT, ("> ctrlif_rx=%d\n", msg->subtype));
switch (msg->subtype) {
case CMSG_NETIF_FE_INTERFACE_STATUS:
if (msg->length != sizeof(netif_fe_interface_status_t))
goto error;
xennet_interface_status_change(
(netif_fe_interface_status_t *)&msg->msg[0]);
break;
case CMSG_NETIF_FE_DRIVER_STATUS:
if (msg->length != sizeof(netif_fe_driver_status_t))
goto error;
xennet_driver_status_change(
(netif_fe_driver_status_t *)&msg->msg[0]);
break;
error:
default:
msg->length = 0;
break;
}
if (respond)
ctrl_if_send_response(msg);
}
static void
xennet_driver_status_change(netif_fe_driver_status_t *status)
{
DPRINTFN(XEDB_EVENT, ("xennet_driver_status_change(%d)\n",
status->status));
netctrl.xc_up = status->status;
xennet_driver_count_connected();
}
static int
xennet_driver_count_connected(void)
{
struct device *dv;
struct xennet_softc *xs = NULL;
netctrl.xc_interfaces = netctrl.xc_connected = 0;
for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
if (dv->dv_cfattach == NULL ||
dv->dv_cfattach->ca_attach != xennet_attach)
continue;
xs = (struct xennet_softc *)dv;
netctrl.xc_interfaces++;
if (xs->sc_backend_state == BEST_CONNECTED)
netctrl.xc_connected++;
}
return netctrl.xc_connected;
}
static void
xennet_interface_status_change(netif_fe_interface_status_t *status)
{
ctrl_msg_t cmsg;
netif_fe_interface_connect_t up;
struct xennet_softc *sc;
struct ifnet *ifp;
struct xennet_attach_args xneta;
DPRINTFN(XEDB_EVENT, ("xennet_interface_status_change(%d,%d,%02x:%02x:%02x:%02x:%02x:%02x)\n",
status->status,
status->handle,
status->mac[0], status->mac[1], status->mac[2],
status->mac[3], status->mac[4], status->mac[5]));
sc = find_device(status->handle);
if (sc == NULL) {
xneta.xa_device = "xennet";
xneta.xa_handle = status->handle;
config_found(netctrl.xc_parent, &xneta, netctrl.xc_cfprint);
sc = find_device(status->handle);
if (sc == NULL) {
printf("Status change: invalid netif handle %u\n",
status->handle);
return;
}
}
ifp = &sc->sc_ethercom.ec_if;
DPRINTFN(XEDB_EVENT, ("xennet_interface_status_change(%d,%p,%02x:%02x:%02x:%02x:%02x:%02x)\n",
status->handle, sc,
status->mac[0], status->mac[1], status->mac[2],
status->mac[3], status->mac[4], status->mac[5]));
switch (status->status) {
case NETIF_INTERFACE_STATUS_CLOSED:
printf("Unexpected netif-CLOSED message in state %d\n",
sc->sc_backend_state);
break;
case NETIF_INTERFACE_STATUS_DISCONNECTED:
#if 0
if (sc->sc_backend_state != BEST_CLOSED) {
printk("Unexpected netif-DISCONNECTED message"
" in state %d\n", sc->sc_backend_state);
printk("Attempting to reconnect network interface\n");
/* Begin interface recovery.
*
* NB. Whilst we're recovering, we turn the
* carrier state off. We take measures to
* ensure that this device isn't used for
* anything. We also stop the queue for this
* device. Various different approaches
* (e.g. continuing to buffer packets) have
* been tested but don't appear to improve the
* overall impact on TCP connections.
*
* TODO: (MAW) Change the Xend<->Guest
* protocol so that a recovery is initiated by
* a special "RESET" message - disconnect
* could just mean we're not allowed to use
* this interface any more.
*/
/* Stop old i/f to prevent errors whilst we
* rebuild the state. */
spin_lock_irq(&np->tx_lock);
spin_lock(&np->rx_lock);
netif_stop_queue(dev);
np->backend_state = BEST_DISCONNECTED;
spin_unlock(&np->rx_lock);
spin_unlock_irq(&np->tx_lock);
/* Free resources. */
free_irq(np->irq, dev);
unbind_evtchn_from_irq(np->evtchn);
free_page((unsigned long)np->tx);
free_page((unsigned long)np->rx);
}
#endif
if (sc->sc_backend_state == BEST_CLOSED) {
/* Move from CLOSED to DISCONNECTED state. */
sc->sc_tx = (netif_tx_interface_t *)
uvm_km_valloc_align(kernel_map, PAGE_SIZE, PAGE_SIZE);
if (sc->sc_tx == NULL)
panic("netif: no tx va");
sc->sc_rx = (netif_rx_interface_t *)
uvm_km_valloc_align(kernel_map, PAGE_SIZE, PAGE_SIZE);
if (sc->sc_rx == NULL)
panic("netif: no rx va");
sc->sc_pg_tx = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
if (sc->sc_pg_tx == NULL) {
panic("netif: no tx pages");
}
pmap_kenter_pa((vaddr_t)sc->sc_tx, VM_PAGE_TO_PHYS(sc->sc_pg_tx),
VM_PROT_READ | VM_PROT_WRITE);
sc->sc_pg_rx = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
if (sc->sc_pg_rx == NULL) {
panic("netif: no rx pages");
}
pmap_kenter_pa((vaddr_t)sc->sc_rx, VM_PAGE_TO_PHYS(sc->sc_pg_rx),
VM_PROT_READ | VM_PROT_WRITE);
sc->sc_backend_state = BEST_DISCONNECTED;
}
/* Construct an interface-CONNECT message for the
* domain controller. */
cmsg.type = CMSG_NETIF_FE;
cmsg.subtype = CMSG_NETIF_FE_INTERFACE_CONNECT;
cmsg.length = sizeof(netif_fe_interface_connect_t);
up.handle = status->handle;
up.tx_shmem_frame = xpmap_ptom(VM_PAGE_TO_PHYS(sc->sc_pg_tx)) >> PAGE_SHIFT;
up.rx_shmem_frame = xpmap_ptom(VM_PAGE_TO_PHYS(sc->sc_pg_rx)) >> PAGE_SHIFT;
memcpy(cmsg.msg, &up, sizeof(up));
/* Tell the controller to bring up the interface. */
ctrl_if_send_message_block(&cmsg, NULL, 0, 0);
break;
case NETIF_INTERFACE_STATUS_CONNECTED:
if (sc->sc_backend_state == BEST_CLOSED) {
printf("Unexpected netif-CONNECTED message"
" in state %d\n", sc->sc_backend_state);
break;
}
memcpy(sc->sc_enaddr, status->mac, ETHER_ADDR_LEN);
#if 0
if (xen_start_info.flags & SIF_PRIVILEGED) {
/* XXX for domain-0 change out ethernet address to be
* different than the physical address since arp
* replies from other domains will report the physical
* address.
*/
if (sc->sc_enaddr[0] != 0xaa)
sc->sc_enaddr[0] = 0xaa;
else
sc->sc_enaddr[0] = 0xab;
}
#endif
/* Recovery procedure: */
/* Step 1: Reinitialise variables. */
sc->sc_rx_resp_cons = sc->sc_tx_resp_cons = /* sc->sc_tx_full = */ 0;
sc->sc_rx->event = sc->sc_tx->event = 1;
/* Step 2: Rebuild the RX and TX ring contents. */
network_alloc_rx_buffers(sc);
SLIST_INIT(&sc->sc_tx_bufs);
network_alloc_tx_buffers(sc);
/* Step 3: All public and private state should now be
* sane. Get ready to start sending and receiving
* packets and give the driver domain a kick because
* we've probably just requeued some packets.
*/
sc->sc_backend_state = BEST_CONNECTED;
__insn_barrier();
hypervisor_notify_via_evtchn(status->evtchn);
network_tx_buf_gc(sc);
if_attach(ifp);
ether_ifattach(ifp, sc->sc_enaddr);
sc->sc_evtchn = status->evtchn;
sc->sc_irq = bind_evtchn_to_irq(sc->sc_evtchn);
event_set_handler(sc->sc_irq, &xen_network_handler, sc, IPL_NET);
hypervisor_enable_irq(sc->sc_irq);
xennet_driver_count_connected();
aprint_normal("%s: MAC address %s\n", sc->sc_dev.dv_xname,
ether_sprintf(sc->sc_enaddr));
#if NRND > 0
rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
RND_TYPE_NET, 0);
#endif
break;
default:
printf("Status change to unknown value %d\n",
status->status);
break;
}
DPRINTFN(XEDB_EVENT, ("xennet_interface_status_change()\n"));
}
static void
xennet_tx_mbuf_free(struct mbuf *m, caddr_t buf, size_t size, void *arg)
{
struct xennet_txbuf *txbuf = (struct xennet_txbuf *)arg;
DPRINTFN(XEDB_MBUF, ("xennet_tx_mbuf_free %p pa %p\n", txbuf,
(void *)txbuf->xt_pa));
SLIST_INSERT_HEAD(&txbuf->xt_sc->sc_tx_bufs, txbuf, xt_next);
pool_cache_put(&mbpool_cache, m);
}
static void
xennet_rx_push_buffer(struct xennet_softc *sc, int id)
{
NETIF_RING_IDX ringidx;
int nr_pfns;
ringidx = sc->sc_rx->req_prod;
nr_pfns = 0;
DPRINTFN(XEDB_MEM, ("readding page va %p pa %p ma %p/%p to rx_ring "
"at %d with id %d\n",
(void *)sc->sc_rx_bufa[id].xb_rx.xbrx_va,
(void *)sc->sc_rx_bufa[id].xb_rx.xbrx_pa,
(void *)(PTE_BASE[x86_btop
(sc->sc_rx_bufa[id].xb_rx.xbrx_va)] &
PG_FRAME),
(void *)xpmap_ptom(sc->sc_rx_bufa[id].xb_rx.xbrx_pa),
ringidx, id));
sc->sc_rx->ring[MASK_NETIF_RX_IDX(ringidx)].req.id = id;
rx_pfn_array[nr_pfns] = xpmap_ptom(sc->sc_rx_bufa[id].xb_rx.xbrx_pa)
>> PAGE_SHIFT;
/* Remove this page from pseudo phys map before
* passing back to Xen. */
xpmap_phys_to_machine_mapping[(sc->sc_rx_bufa[id].xb_rx.xbrx_pa - XPMAP_OFFSET) >> PAGE_SHIFT] =
INVALID_P2M_ENTRY;
rx_mcl[nr_pfns].op = __HYPERVISOR_update_va_mapping;
rx_mcl[nr_pfns].args[0] = sc->sc_rx_bufa[id].xb_rx.xbrx_va;
rx_mcl[nr_pfns].args[1] = 0;
rx_mcl[nr_pfns].args[2] = 0;
nr_pfns++;
sc->sc_rx_bufs_to_notify++;
ringidx++;
/*
* We may have allocated buffers which have entries
* outstanding in the page update queue -- make sure we flush
* those first!
*/
xpq_flush_queue();
/* After all PTEs have been zapped we blow away stale TLB entries. */
rx_mcl[nr_pfns-1].args[2] = UVMF_TLB_FLUSH|UVMF_LOCAL;
/* Give away a batch of pages. */
rx_mcl[nr_pfns].op = __HYPERVISOR_dom_mem_op;
rx_mcl[nr_pfns].args[0] = MEMOP_decrease_reservation;
rx_mcl[nr_pfns].args[1] = (unsigned long)rx_pfn_array;
rx_mcl[nr_pfns].args[2] = (unsigned long)nr_pfns;
rx_mcl[nr_pfns].args[3] = 0;
rx_mcl[nr_pfns].args[4] = DOMID_SELF;
/* Zap PTEs and give away pages in one big multicall. */
(void)HYPERVISOR_multicall(rx_mcl, nr_pfns+1);
/* Check return status of HYPERVISOR_dom_mem_op(). */
if ( rx_mcl[nr_pfns].result != nr_pfns )
panic("Unable to reduce memory reservation\n");
/* Above is a suitable barrier to ensure backend will see requests. */
sc->sc_rx->req_prod = ringidx;
}
static void
xennet_rx_mbuf_free(struct mbuf *m, caddr_t buf, size_t size, void *arg)
{
union xennet_bufarray *xb = (union xennet_bufarray *)arg;
struct xennet_softc *sc = xb->xb_rx.xbrx_sc;
int id = (xb - sc->sc_rx_bufa);
DPRINTFN(XEDB_MBUF, ("xennet_rx_mbuf_free id %d, mbuf %p, buf %p, "
"size %d\n", id, m, buf, size));
xennet_rx_push_buffer(sc, id);
pool_cache_put(&mbpool_cache, m);
}
static int
xen_network_handler(void *arg)
{
struct xennet_softc *sc = arg;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
netif_rx_response_t *rx;
paddr_t pa;
NETIF_RING_IDX ringidx;
mmu_update_t *mmu = rx_mmu;
multicall_entry_t *mcl = rx_mcl;
struct mbuf *m;
network_tx_buf_gc(sc);
#if NRND > 0
rnd_add_uint32(&sc->sc_rnd_source, sc->sc_rx_resp_cons);
#endif
again:
for (ringidx = sc->sc_rx_resp_cons;
ringidx != sc->sc_rx->resp_prod;
ringidx++) {
rx = &sc->sc_rx->ring[MASK_NETIF_RX_IDX(ringidx)].resp;
if (rx->status < 0)
panic("rx->status < 0");
/* XXXcl check rx->status for error */
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
printf("xennet: rx no mbuf\n");
break;
}
pa = sc->sc_rx_bufa[rx->id].xb_rx.xbrx_pa;
DPRINTFN(XEDB_EVENT, ("rx event %d for id %d, size %d, "
"status %d, ma %08lx, pa %08lx\n", ringidx,
rx->id, rx->status, rx->status, rx->addr, pa));
/* Remap the page. */
mmu->ptr = (rx->addr & PG_FRAME) | MMU_MACHPHYS_UPDATE;
mmu->val = (pa - XPMAP_OFFSET) >> PAGE_SHIFT;
mmu++;
mcl->op = __HYPERVISOR_update_va_mapping;
mcl->args[0] = sc->sc_rx_bufa[rx->id].xb_rx.xbrx_va;
mcl->args[1] = (rx->addr & PG_FRAME) | PG_V|PG_KW;
mcl->args[2] = UVMF_TLB_FLUSH|UVMF_LOCAL; // 0;
mcl++;
xpmap_phys_to_machine_mapping
[(pa - XPMAP_OFFSET) >> PAGE_SHIFT] =
rx->addr >> PAGE_SHIFT;
/* Do all the remapping work, and M->P updates, in one
* big hypercall. */
if ((mcl - rx_mcl) != 0) {
mcl->op = __HYPERVISOR_mmu_update;
mcl->args[0] = (unsigned long)rx_mmu;
mcl->args[1] = mmu - rx_mmu;
mcl->args[2] = 0;
mcl++;
(void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
}
if (0)
printf("page mapped at va %08lx -> %08x/%08lx\n",
sc->sc_rx_bufa[rx->id].xb_rx.xbrx_va,
PTE_BASE[x86_btop(sc->sc_rx_bufa[rx->id].xb_rx.xbrx_va)],
rx->addr);
mmu = rx_mmu;
mcl = rx_mcl;
DPRINTFN(XEDB_MBUF, ("rx packet mbuf %p va %p pa %p/%p "
"ma %p\n", m,
(void *)sc->sc_rx_bufa[rx->id].xb_rx.xbrx_va,
(void *)(xpmap_mtop(PTE_BASE[x86_btop
(sc->sc_rx_bufa[rx->id].xb_rx.xbrx_va)] & PG_FRAME)), (void *)pa,
(void *)(PTE_BASE[x86_btop
(sc->sc_rx_bufa[rx->id].xb_rx.xbrx_va)] & PG_FRAME)));
m->m_len = m->m_pkthdr.len = rx->status;
m->m_pkthdr.rcvif = ifp;
if (sc->sc_rx->req_prod != sc->sc_rx->resp_prod) {
MEXTADD(m, (void *)(sc->sc_rx_bufa[rx->id].xb_rx.
xbrx_va + (rx->addr & PAGE_MASK)), rx->status, M_DEVBUF,
xennet_rx_mbuf_free,
&sc->sc_rx_bufa[rx->id]);
} else {
/*
* This was our last receive buffer, allocate
* memory, copy data and push the receive
* buffer back to the hypervisor.
*/
MEXTMALLOC(m, rx->status, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
printf("xennet: rx no mbuf 2\n");
m_free(m);
break;
}
memcpy(m->m_data, (void *)(sc->sc_rx_bufa[rx->id].
xb_rx.xbrx_va + (rx->addr & PAGE_MASK)), rx->status);
xennet_rx_push_buffer(sc, rx->id);
}
#ifdef XENNET_DEBUG_DUMP
xennet_hex_dump(mtod(m, u_char *), m->m_pkthdr.len, "r", rx->id);
#endif
#if NBPFILTER > 0
/*
* Pass packet to bpf if there is a listener.
*/
if (ifp->if_bpf)
bpf_mtap(ifp->if_bpf, m);
#endif
ifp->if_ipackets++;
/* Pass the packet up. */
(*ifp->if_input)(ifp, m);
}
sc->sc_rx_resp_cons = ringidx;
sc->sc_rx->event = sc->sc_rx_resp_cons + 1;
if (sc->sc_rx->resp_prod != ringidx)
goto again;
return 0;
}
static inline int
get_bufarray_entry(union xennet_bufarray *a)
{
int idx;
idx = a[0].xb_next;
a[0].xb_next = a[idx].xb_next;
return idx;
}
static inline void
put_bufarray_entry(union xennet_bufarray *a, int idx)
{
a[idx].xb_next = a[0].xb_next;
a[0].xb_next = idx;
}
static void
network_tx_buf_gc(struct xennet_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
NETIF_RING_IDX idx, prod;
do {
prod = sc->sc_tx->resp_prod;
for (idx = sc->sc_tx_resp_cons; idx != prod; idx++) {
DPRINTFN(XEDB_EVENT, ("tx event at pos %d, status: "
"%d, id: %d, mbuf %p, buf %p\n", idx,
sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].resp.status,
sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].resp.id,
sc->sc_tx_bufa[sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].resp.id].xb_tx.xbtx_m,
mtod(sc->sc_tx_bufa[sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].resp.id].xb_tx.xbtx_m, void *)));
m_freem(sc->sc_tx_bufa[sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].resp.id].xb_tx.xbtx_m);
put_bufarray_entry(sc->sc_tx_bufa,
sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].resp.id);
sc->sc_tx_entries--; /* atomic */
}
sc->sc_tx_resp_cons = prod;
/*
* Set a new event, then check for race with update of
* tx_cons.
*/
sc->sc_tx->event = /* atomic */
prod + (sc->sc_tx_entries >> 1) + 1;
__insn_barrier();
} while (prod != sc->sc_tx->resp_prod);
if (sc->sc_tx->resp_prod == sc->sc_tx->req_prod)
ifp->if_timer = 0;
/* KDASSERT(sc->sc_net_idx->tx_req_prod == */
/* TX_RING_ADD(sc->sc_net_idx->tx_resp_prod, sc->sc_tx_entries)); */
}
static void
network_alloc_rx_buffers(struct xennet_softc *sc)
{
vaddr_t rxpages, va;
paddr_t pa;
struct vm_page *pg;
int id, nr_pfns;
NETIF_RING_IDX ringidx;
int s;
ringidx = sc->sc_rx->req_prod;
if ((ringidx - sc->sc_rx_resp_cons) > (RX_MAX_ENTRIES / 2))
return;
nr_pfns = 0;
rxpages = uvm_km_valloc_align(kernel_map, RX_ENTRIES * PAGE_SIZE,
PAGE_SIZE);
s = splnet();
for (va = rxpages; va < rxpages + RX_ENTRIES * PAGE_SIZE;
va += PAGE_SIZE) {
pg = uvm_pagealloc(NULL, 0, NULL, 0);
if (pg == NULL)
panic("network_alloc_rx_buffers: no pages");
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
VM_PROT_READ | VM_PROT_WRITE);
id = get_bufarray_entry(sc->sc_rx_bufa);
sc->sc_rx_bufa[id].xb_rx.xbrx_va = va;
sc->sc_rx_bufa[id].xb_rx.xbrx_sc = sc;
pa = VM_PAGE_TO_PHYS(pg);
DPRINTFN(XEDB_MEM, ("adding page va %p pa %p/%p "
"ma %p/%p to rx_ring at %d with id %d\n", (void *)va,
(void *)(VM_PAGE_TO_PHYS(pg) & PG_FRAME), (void *)xpmap_mtop(PTE_BASE[x86_btop(va)]),
(void *)(PTE_BASE[x86_btop(va)] & PG_FRAME),
(void *)xpmap_ptom(VM_PAGE_TO_PHYS(pg)),
ringidx, id));
sc->sc_rx_bufa[id].xb_rx.xbrx_pa = pa;
sc->sc_rx->ring[MASK_NETIF_RX_IDX(ringidx)].req.id = id;
rx_pfn_array[nr_pfns] = xpmap_ptom(pa) >> PAGE_SHIFT;
/* Remove this page from pseudo phys map before
* passing back to Xen. */
xpmap_phys_to_machine_mapping[(pa - XPMAP_OFFSET) >> PAGE_SHIFT] =
INVALID_P2M_ENTRY;
rx_mcl[nr_pfns].op = __HYPERVISOR_update_va_mapping;
rx_mcl[nr_pfns].args[0] = va;
rx_mcl[nr_pfns].args[1] = 0;
rx_mcl[nr_pfns].args[2] = 0;
nr_pfns++;
sc->sc_rx_bufs_to_notify++;
ringidx++;
if ((ringidx - sc->sc_rx_resp_cons) == RX_MAX_ENTRIES)
break;
}
if (nr_pfns == 0) {
splx(s);
return;
}
/*
* We may have allocated buffers which have entries
* outstanding in the page update queue -- make sure we flush
* those first!
*/
xpq_flush_queue();
/* After all PTEs have been zapped we blow away stale TLB entries. */
rx_mcl[nr_pfns-1].args[2] = UVMF_TLB_FLUSH|UVMF_LOCAL;
/* Give away a batch of pages. */
rx_mcl[nr_pfns].op = __HYPERVISOR_dom_mem_op;
rx_mcl[nr_pfns].args[0] = MEMOP_decrease_reservation;
rx_mcl[nr_pfns].args[1] = (unsigned long)rx_pfn_array;
rx_mcl[nr_pfns].args[2] = (unsigned long)nr_pfns;
rx_mcl[nr_pfns].args[3] = 0;
rx_mcl[nr_pfns].args[4] = DOMID_SELF;
/* Zap PTEs and give away pages in one big multicall. */
(void)HYPERVISOR_multicall(rx_mcl, nr_pfns+1);
/* Check return status of HYPERVISOR_dom_mem_op(). */
if (rx_mcl[nr_pfns].result != nr_pfns)
panic("Unable to reduce memory reservation\n");
/* Above is a suitable barrier to ensure backend will see requests. */
sc->sc_rx->req_prod = ringidx;
splx(s);
}
static void
network_alloc_tx_buffers(struct xennet_softc *sc)
{
vaddr_t txpages, va;
struct vm_page *pg;
struct xennet_txbuf *txbuf;
int i;
txpages = uvm_km_valloc_align(kernel_map,
(TX_ENTRIES / TXBUF_PER_PAGE) * PAGE_SIZE, PAGE_SIZE);
for (va = txpages;
va < txpages + (TX_ENTRIES / TXBUF_PER_PAGE) * PAGE_SIZE;
va += PAGE_SIZE) {
pg = uvm_pagealloc(NULL, 0, NULL, 0);
if (pg == NULL)
panic("network_alloc_tx_buffers: no pages");
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
VM_PROT_READ | VM_PROT_WRITE);
for (i = 0; i < TXBUF_PER_PAGE; i++) {
txbuf = (struct xennet_txbuf *)
(va + i * (PAGE_SIZE / TXBUF_PER_PAGE));
txbuf->xt_sc = sc;
txbuf->xt_pa = VM_PAGE_TO_PHYS(pg) +
i * (PAGE_SIZE / TXBUF_PER_PAGE) +
sizeof(struct xennet_txbuf);
SLIST_INSERT_HEAD(&sc->sc_tx_bufs, txbuf, xt_next);
}
}
}
/*
* Called at splnet.
*/
void
xennet_start(struct ifnet *ifp)
{
struct xennet_softc *sc = ifp->if_softc;
struct mbuf *m, *new_m;
struct xennet_txbuf *txbuf;
netif_tx_request_t *txreq;
NETIF_RING_IDX idx;
paddr_t pa;
int bufid;
DPRINTFN(XEDB_FOLLOW, ("%s: xennet_start()\n", sc->sc_dev.dv_xname));
#ifdef DIAGNOSTIC
IFQ_POLL(&ifp->if_snd, m);
if (m == 0)
panic("%s: No packet to start", sc->sc_dev.dv_xname);
#endif
#if NRND > 0
rnd_add_uint32(&sc->sc_rnd_source, sc->sc_tx->req_prod);
#endif
if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
return;
idx = sc->sc_tx->req_prod;
while (/*CONSTCOND*/1) {
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL)
break;
switch (m->m_flags & (M_EXT|M_EXT_CLUSTER)) {
case M_EXT|M_EXT_CLUSTER:
pa = m->m_ext.ext_paddr +
(m->m_data - m->m_ext.ext_buf);
break;
default:
case 0:
pa = m->m_paddr + M_BUFOFFSET(m) +
(m->m_data - M_BUFADDR(m));
break;
}
if (m->m_pkthdr.len != m->m_len ||
(pa ^ (pa + m->m_pkthdr.len)) & PG_FRAME) {
txbuf = SLIST_FIRST(&sc->sc_tx_bufs);
if (txbuf == NULL) {
// printf("xennet: no tx bufs\n");
break;
}
MGETHDR(new_m, M_DONTWAIT, MT_DATA);
if (new_m == NULL) {
printf("xennet: no mbuf\n");
break;
}
SLIST_REMOVE_HEAD(&sc->sc_tx_bufs, xt_next);
IFQ_DEQUEUE(&ifp->if_snd, m);
KASSERT(m->m_flags & M_PKTHDR);
M_COPY_PKTHDR(new_m, m);
m_copydata(m, 0, m->m_pkthdr.len, txbuf->xt_buf);
MEXTADD(new_m, txbuf->xt_buf, m->m_pkthdr.len,
M_DEVBUF, xennet_tx_mbuf_free, txbuf);
new_m->m_ext.ext_paddr = txbuf->xt_pa;
new_m->m_len = new_m->m_pkthdr.len = m->m_pkthdr.len;
m_freem(m);
m = new_m;
pa = m->m_ext.ext_paddr +
(m->m_data - m->m_ext.ext_buf);
} else
IFQ_DEQUEUE(&ifp->if_snd, m);
bufid = get_bufarray_entry(sc->sc_tx_bufa);
sc->sc_tx_bufa[bufid].xb_tx.xbtx_m = m;
DPRINTFN(XEDB_MBUF, ("xennet_start id %d, mbuf %p, buf %p/%p, "
"size %d\n", bufid, m, mtod(m, void *),
(void *)pa, m->m_pkthdr.len));
#ifdef XENNET_DEBUG_DUMP
xennet_hex_dump(mtod(m, u_char *), m->m_pkthdr.len, "s", bufid);
#endif
txreq = &sc->sc_tx->ring[MASK_NETIF_TX_IDX(idx)].req;
txreq->id = bufid;
txreq->addr = xpmap_ptom(pa);
txreq->size = m->m_pkthdr.len;
__insn_barrier();
idx++;
sc->sc_tx->req_prod = idx;
sc->sc_tx_entries++; /* XXX atomic */
#ifdef XENNET_DEBUG
DPRINTFN(XEDB_MEM, ("packet addr %p/%p, physical %p/%p, "
"m_paddr %p, len %d/%d\n", M_BUFADDR(m), mtod(m, void *),
|