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
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/machsystm.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/kmem.h>
#include <sys/strsun.h>
#include <sys/callb.h>
#include <sys/sdt.h>
#include <sys/mach_descrip.h>
#include <sys/mdeg.h>
#include <net/if.h>
#include <sys/vsw.h>
#include <sys/vio_mailbox.h>
#include <sys/vio_common.h>
#include <sys/vnet_common.h>
#include <sys/vnet_mailbox.h>
#include <sys/vio_util.h>
/*
* This file contains the implementation of TxDring data transfer mode of VIO
* Protocol in vsw. The functions in this file are invoked from vsw_ldc.c
* after TxDring mode is negotiated with the peer during attribute phase of
* handshake. This file contains functions that setup the transmit and receive
* descriptor rings, and associated resources in TxDring mode. It also contains
* the transmit and receive data processing functions that are invoked in
* TxDring mode.
*/
/* Functions exported to vsw_ldc.c */
vio_dring_reg_msg_t *vsw_create_tx_dring_info(vsw_ldc_t *);
int vsw_setup_tx_dring(vsw_ldc_t *ldcp, dring_info_t *dp);
void vsw_destroy_tx_dring(vsw_ldc_t *ldcp);
dring_info_t *vsw_map_rx_dring(vsw_ldc_t *ldcp, void *pkt);
void vsw_unmap_rx_dring(vsw_ldc_t *ldcp);
int vsw_dringsend(vsw_ldc_t *, mblk_t *);
void vsw_ldc_msg_worker(void *arg);
void vsw_stop_msg_thread(vsw_ldc_t *ldcp);
void vsw_process_dringdata(void *, void *);
int vsw_send_msg(vsw_ldc_t *, void *, int, boolean_t);
int vsw_reclaim_dring(dring_info_t *dp, int start);
int vsw_dring_find_free_desc(dring_info_t *, vsw_private_desc_t **, int *);
/* Internal functions */
static int vsw_init_multipools(vsw_ldc_t *ldcp, vsw_t *vswp);
static dring_info_t *vsw_create_tx_dring(vsw_ldc_t *);
/* Functions imported from vsw_ldc.c */
extern void vsw_process_pkt(void *);
extern void vsw_destroy_rxpools(void *);
extern dring_info_t *vsw_map_dring_cmn(vsw_ldc_t *ldcp,
vio_dring_reg_msg_t *dring_pkt);
extern void vsw_process_conn_evt(vsw_ldc_t *, uint16_t);
extern mblk_t *vsw_vlan_frame_pretag(void *arg, int type, mblk_t *mp);
/* Tunables */
extern int vsw_wretries;
extern int vsw_recv_delay;
extern int vsw_recv_retries;
extern boolean_t vsw_jumbo_rxpools;
extern uint32_t vsw_chain_len;
extern uint32_t vsw_num_descriptors;
extern uint32_t vsw_mblk_size1;
extern uint32_t vsw_mblk_size2;
extern uint32_t vsw_mblk_size3;
extern uint32_t vsw_mblk_size4;
extern uint32_t vsw_num_mblks1;
extern uint32_t vsw_num_mblks2;
extern uint32_t vsw_num_mblks3;
extern uint32_t vsw_num_mblks4;
#define VSW_NUM_VMPOOLS 3 /* number of vio mblk pools */
#define SND_DRING_NACK(ldcp, pkt) \
pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
pkt->tag.vio_sid = ldcp->local_session; \
(void) vsw_send_msg(ldcp, (void *)pkt, \
sizeof (vio_dring_msg_t), B_TRUE);
vio_dring_reg_msg_t *
vsw_create_tx_dring_info(vsw_ldc_t *ldcp)
{
vio_dring_reg_msg_t *mp;
dring_info_t *dp;
vsw_t *vswp = ldcp->ldc_vswp;
D1(vswp, "%s enter\n", __func__);
/*
* If we can't create a dring, obviously no point sending
* a message.
*/
if ((dp = vsw_create_tx_dring(ldcp)) == NULL)
return (NULL);
mp = kmem_zalloc(sizeof (vio_dring_reg_msg_t), KM_SLEEP);
mp->tag.vio_msgtype = VIO_TYPE_CTRL;
mp->tag.vio_subtype = VIO_SUBTYPE_INFO;
mp->tag.vio_subtype_env = VIO_DRING_REG;
mp->tag.vio_sid = ldcp->local_session;
/* payload */
mp->num_descriptors = dp->num_descriptors;
mp->descriptor_size = dp->descriptor_size;
mp->options = dp->options;
mp->ncookies = dp->dring_ncookies;
bcopy(&dp->dring_cookie[0], &mp->cookie[0], sizeof (ldc_mem_cookie_t));
mp->dring_ident = 0;
D1(vswp, "%s exit\n", __func__);
return (mp);
}
/*
* Allocate transmit resources for the channel. The resources consist of a
* transmit descriptor ring and an associated transmit buffer area.
*/
static dring_info_t *
vsw_create_tx_dring(vsw_ldc_t *ldcp)
{
vsw_t *vswp = ldcp->ldc_vswp;
ldc_mem_info_t minfo;
dring_info_t *dp;
dp = (dring_info_t *)kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
ldcp->lane_out.dringp = dp;
/* create public section of ring */
if ((ldc_mem_dring_create(vsw_num_descriptors,
sizeof (vnet_public_desc_t), &dp->dring_handle)) != 0) {
DERR(vswp, "vsw_create_tx_dring(%lld): ldc dring create "
"failed", ldcp->ldc_id);
goto fail;
}
ASSERT(dp->dring_handle != NULL);
/*
* Get the base address of the public section of the ring.
*/
if ((ldc_mem_dring_info(dp->dring_handle, &minfo)) != 0) {
DERR(vswp, "vsw_create_tx_dring(%lld): dring info failed\n",
ldcp->ldc_id);
goto fail;
} else {
ASSERT(minfo.vaddr != 0);
dp->pub_addr = minfo.vaddr;
}
dp->num_descriptors = vsw_num_descriptors;
dp->descriptor_size = sizeof (vnet_public_desc_t);
dp->options = VIO_TX_DRING;
dp->dring_ncookies = 1; /* guaranteed by ldc */
/*
* create private portion of ring
*/
dp->priv_addr = (vsw_private_desc_t *)kmem_zalloc(
(sizeof (vsw_private_desc_t) * vsw_num_descriptors), KM_SLEEP);
if (vsw_setup_tx_dring(ldcp, dp)) {
DERR(vswp, "%s: unable to setup ring", __func__);
goto fail;
}
/* bind dring to the channel */
if ((ldc_mem_dring_bind(ldcp->ldc_handle, dp->dring_handle,
LDC_DIRECT_MAP | LDC_SHADOW_MAP, LDC_MEM_RW,
&dp->dring_cookie[0], &dp->dring_ncookies)) != 0) {
DERR(vswp, "vsw_create_tx_dring: unable to bind to channel "
"%lld", ldcp->ldc_id);
goto fail;
}
/* haven't used any descriptors yet */
dp->end_idx = 0;
dp->last_ack_recv = -1;
dp->restart_reqd = B_TRUE;
return (dp);
fail:
vsw_destroy_tx_dring(ldcp);
return (NULL);
}
/*
* Setup the descriptors in the tx dring.
* Returns 0 on success, 1 on failure.
*/
int
vsw_setup_tx_dring(vsw_ldc_t *ldcp, dring_info_t *dp)
{
vnet_public_desc_t *pub_addr = NULL;
vsw_private_desc_t *priv_addr = NULL;
vsw_t *vswp = ldcp->ldc_vswp;
uint64_t *tmpp;
uint64_t offset = 0;
uint32_t ncookies = 0;
static char *name = "vsw_setup_ring";
int i, j, nc, rv;
size_t data_sz;
void *data_addr;
priv_addr = dp->priv_addr;
pub_addr = dp->pub_addr;
/* public section may be null but private should never be */
ASSERT(priv_addr != NULL);
/*
* Allocate the region of memory which will be used to hold
* the data the descriptors will refer to.
*/
data_sz = vswp->max_frame_size + VNET_IPALIGN + VNET_LDCALIGN;
/*
* In order to ensure that the number of ldc cookies per descriptor is
* limited to be within the default MAX_COOKIES (2), we take the steps
* outlined below:
*
* Align the entire data buffer area to 8K and carve out per descriptor
* data buffers starting from this 8K aligned base address.
*
* We round up the mtu specified to be a multiple of 2K or 4K.
* For sizes up to 12K we round up the size to the next 2K.
* For sizes > 12K we round up to the next 4K (otherwise sizes such as
* 14K could end up needing 3 cookies, with the buffer spread across
* 3 8K pages: 8K+6K, 2K+8K+2K, 6K+8K, ...).
*/
if (data_sz <= VNET_12K) {
data_sz = VNET_ROUNDUP_2K(data_sz);
} else {
data_sz = VNET_ROUNDUP_4K(data_sz);
}
dp->desc_data_sz = data_sz;
/* allocate extra 8K bytes for alignment */
dp->data_sz = (vsw_num_descriptors * data_sz) + VNET_8K;
data_addr = kmem_alloc(dp->data_sz, KM_SLEEP);
dp->data_addr = data_addr;
D2(vswp, "%s: allocated %lld bytes at 0x%llx\n", name,
dp->data_sz, dp->data_addr);
/* align the starting address of the data area to 8K */
data_addr = (void *)VNET_ROUNDUP_8K((uintptr_t)data_addr);
tmpp = (uint64_t *)data_addr;
offset = dp->desc_data_sz/sizeof (tmpp);
/*
* Initialise some of the private and public (if they exist)
* descriptor fields.
*/
for (i = 0; i < vsw_num_descriptors; i++) {
mutex_init(&priv_addr->dstate_lock, NULL, MUTEX_DRIVER, NULL);
if ((ldc_mem_alloc_handle(ldcp->ldc_handle,
&priv_addr->memhandle)) != 0) {
DERR(vswp, "%s: alloc mem handle failed", name);
goto fail;
}
priv_addr->datap = (void *)tmpp;
rv = ldc_mem_bind_handle(priv_addr->memhandle,
(caddr_t)priv_addr->datap, dp->desc_data_sz,
LDC_SHADOW_MAP, LDC_MEM_R|LDC_MEM_W,
&(priv_addr->memcookie[0]), &ncookies);
if (rv != 0) {
DERR(vswp, "%s(%lld): ldc_mem_bind_handle failed "
"(rv %d)", name, ldcp->ldc_id, rv);
goto fail;
}
priv_addr->bound = 1;
D2(vswp, "%s: %d: memcookie 0 : addr 0x%llx : size 0x%llx",
name, i, priv_addr->memcookie[0].addr,
priv_addr->memcookie[0].size);
if (ncookies >= (uint32_t)(VSW_MAX_COOKIES + 1)) {
DERR(vswp, "%s(%lld) ldc_mem_bind_handle returned "
"invalid num of cookies (%d) for size 0x%llx",
name, ldcp->ldc_id, ncookies, VSW_RING_EL_DATA_SZ);
goto fail;
} else {
for (j = 1; j < ncookies; j++) {
rv = ldc_mem_nextcookie(priv_addr->memhandle,
&(priv_addr->memcookie[j]));
if (rv != 0) {
DERR(vswp, "%s: ldc_mem_nextcookie "
"failed rv (%d)", name, rv);
goto fail;
}
D3(vswp, "%s: memcookie %d : addr 0x%llx : "
"size 0x%llx", name, j,
priv_addr->memcookie[j].addr,
priv_addr->memcookie[j].size);
}
}
priv_addr->ncookies = ncookies;
priv_addr->dstate = VIO_DESC_FREE;
if (pub_addr != NULL) {
/* link pub and private sides */
priv_addr->descp = pub_addr;
pub_addr->ncookies = priv_addr->ncookies;
for (nc = 0; nc < pub_addr->ncookies; nc++) {
bcopy(&priv_addr->memcookie[nc],
&pub_addr->memcookie[nc],
sizeof (ldc_mem_cookie_t));
}
pub_addr->hdr.dstate = VIO_DESC_FREE;
pub_addr++;
}
/*
* move to next element in the dring and the next
* position in the data buffer.
*/
priv_addr++;
tmpp += offset;
}
return (0);
fail:
/* return failure; caller will cleanup */
return (1);
}
/*
* Free transmit resources for the channel.
*/
void
vsw_destroy_tx_dring(vsw_ldc_t *ldcp)
{
vsw_private_desc_t *paddr = NULL;
int i;
lane_t *lp = &ldcp->lane_out;
dring_info_t *dp;
dp = lp->dringp;
if (dp == NULL) {
return;
}
mutex_enter(&dp->dlock);
if (dp->priv_addr != NULL) {
/*
* First unbind and free the memory handles
* stored in each descriptor within the ring.
*/
for (i = 0; i < vsw_num_descriptors; i++) {
paddr = (vsw_private_desc_t *)dp->priv_addr + i;
if (paddr->memhandle != 0) {
if (paddr->bound == 1) {
if (ldc_mem_unbind_handle(
paddr->memhandle) != 0) {
DERR(NULL, "error "
"unbinding handle for "
"ring 0x%llx at pos %d",
dp, i);
continue;
}
paddr->bound = 0;
}
if (ldc_mem_free_handle(
paddr->memhandle) != 0) {
DERR(NULL, "error freeing "
"handle for ring 0x%llx "
"at pos %d", dp, i);
continue;
}
paddr->memhandle = 0;
}
mutex_destroy(&paddr->dstate_lock);
}
kmem_free(dp->priv_addr,
(sizeof (vsw_private_desc_t) * vsw_num_descriptors));
}
/*
* Now unbind and destroy the ring itself.
*/
if (dp->dring_handle != 0) {
(void) ldc_mem_dring_unbind(dp->dring_handle);
(void) ldc_mem_dring_destroy(dp->dring_handle);
}
if (dp->data_addr != NULL) {
kmem_free(dp->data_addr, dp->data_sz);
}
mutex_exit(&dp->dlock);
mutex_destroy(&dp->dlock);
mutex_destroy(&dp->restart_lock);
kmem_free(dp, sizeof (dring_info_t));
lp->dringp = NULL;
}
/*
* Map the transmit descriptor ring exported
* by the peer, as our receive descriptor ring.
*/
dring_info_t *
vsw_map_rx_dring(vsw_ldc_t *ldcp, void *pkt)
{
int rv;
dring_info_t *dp;
vio_dring_reg_msg_t *dring_pkt = pkt;
vsw_t *vswp = ldcp->ldc_vswp;
dp = vsw_map_dring_cmn(ldcp, dring_pkt);
if (dp == NULL) {
return (NULL);
}
/* TxDring mode specific initializations */
dp->end_idx = 0;
ldcp->lane_in.dringp = dp;
/* Allocate pools of receive mblks */
rv = vsw_init_multipools(ldcp, vswp);
if (rv != 0) {
/*
* We do not return failure if receive mblk pools can't
* be allocated, instead allocb(9F) will be used to
* dynamically allocate buffers during receive.
*/
DWARN(vswp, "%s: unable to create free mblk pools for"
" channel %ld (rv %d)", __func__, ldcp->ldc_id, rv);
}
return (dp);
}
/*
* Unmap the receive descriptor ring.
*/
void
vsw_unmap_rx_dring(vsw_ldc_t *ldcp)
{
vio_mblk_pool_t *fvmp = NULL;
vsw_t *vswp = ldcp->ldc_vswp;
lane_t *lp = &ldcp->lane_in;
dring_info_t *dp;
if ((dp = lp->dringp) == NULL) {
return;
}
/*
* If we can't destroy all the rx pools for this channel,
* dispatch a task to retry and clean up those rx pools. Note
* that we don't need to wait for the task to complete. If the
* vsw device itself gets detached (vsw_detach()), it will wait
* for the task to complete implicitly in ddi_taskq_destroy().
*/
vio_destroy_multipools(&ldcp->vmp, &fvmp);
if (fvmp != NULL) {
(void) ddi_taskq_dispatch(vswp->rxp_taskq,
vsw_destroy_rxpools, fvmp, DDI_SLEEP);
}
if (dp->dring_handle != 0) {
(void) ldc_mem_dring_unmap(dp->dring_handle);
}
kmem_free(dp, sizeof (dring_info_t));
lp->dringp = NULL;
}
static int
vsw_init_multipools(vsw_ldc_t *ldcp, vsw_t *vswp)
{
size_t data_sz;
int rv;
uint32_t sz1 = 0;
uint32_t sz2 = 0;
uint32_t sz3 = 0;
uint32_t sz4 = 0;
/*
* We round up the mtu specified to be a multiple of 2K to limit the
* number of rx buffer pools created for a given mtu.
*/
data_sz = vswp->max_frame_size + VNET_IPALIGN + VNET_LDCALIGN;
data_sz = VNET_ROUNDUP_2K(data_sz);
/*
* If pool sizes are specified, use them. Note that the presence of
* the first tunable will be used as a hint.
*/
if (vsw_mblk_size1 != 0) {
sz1 = vsw_mblk_size1;
sz2 = vsw_mblk_size2;
sz3 = vsw_mblk_size3;
sz4 = vsw_mblk_size4;
if (sz4 == 0) { /* need 3 pools */
ldcp->max_rxpool_size = sz3;
rv = vio_init_multipools(&ldcp->vmp,
VSW_NUM_VMPOOLS, sz1, sz2, sz3,
vsw_num_mblks1, vsw_num_mblks2, vsw_num_mblks3);
} else {
ldcp->max_rxpool_size = sz4;
rv = vio_init_multipools(&ldcp->vmp,
VSW_NUM_VMPOOLS + 1, sz1, sz2, sz3, sz4,
vsw_num_mblks1, vsw_num_mblks2, vsw_num_mblks3,
vsw_num_mblks4);
}
return (rv);
}
/*
* Pool sizes are not specified. We select the pool sizes based on the
* mtu if vnet_jumbo_rxpools is enabled.
*/
if (vsw_jumbo_rxpools == B_FALSE || data_sz == VNET_2K) {
/*
* Receive buffer pool allocation based on mtu is disabled.
* Use the default mechanism of standard size pool allocation.
*/
sz1 = VSW_MBLK_SZ_128;
sz2 = VSW_MBLK_SZ_256;
sz3 = VSW_MBLK_SZ_2048;
ldcp->max_rxpool_size = sz3;
rv = vio_init_multipools(&ldcp->vmp, VSW_NUM_VMPOOLS,
sz1, sz2, sz3,
vsw_num_mblks1, vsw_num_mblks2, vsw_num_mblks3);
return (rv);
}
switch (data_sz) {
case VNET_4K:
sz1 = VSW_MBLK_SZ_128;
sz2 = VSW_MBLK_SZ_256;
sz3 = VSW_MBLK_SZ_2048;
sz4 = sz3 << 1; /* 4K */
ldcp->max_rxpool_size = sz4;
rv = vio_init_multipools(&ldcp->vmp, VSW_NUM_VMPOOLS + 1,
sz1, sz2, sz3, sz4,
vsw_num_mblks1, vsw_num_mblks2, vsw_num_mblks3,
vsw_num_mblks4);
break;
default: /* data_sz: 4K+ to 16K */
sz1 = VSW_MBLK_SZ_256;
sz2 = VSW_MBLK_SZ_2048;
sz3 = data_sz >> 1; /* Jumbo-size/2 */
sz4 = data_sz; /* Jumbo-size */
ldcp->max_rxpool_size = sz4;
rv = vio_init_multipools(&ldcp->vmp, VSW_NUM_VMPOOLS + 1,
sz1, sz2, sz3, sz4,
vsw_num_mblks1, vsw_num_mblks2, vsw_num_mblks3,
vsw_num_mblks4);
break;
}
return (rv);
}
/*
* Generic routine to send message out over ldc channel.
*
* It is possible that when we attempt to write over the ldc channel
* that we get notified that it has been reset. Depending on the value
* of the handle_reset flag we either handle that event here or simply
* notify the caller that the channel was reset.
*/
int
vsw_send_msg(vsw_ldc_t *ldcp, void *msgp, int size, boolean_t handle_reset)
{
int rv;
size_t msglen = size;
vio_msg_tag_t *tag = (vio_msg_tag_t *)msgp;
vsw_t *vswp = ldcp->ldc_vswp;
vio_dring_msg_t *dmsg;
vio_raw_data_msg_t *rmsg;
vnet_ibnd_desc_t *imsg;
boolean_t data_msg = B_FALSE;
int retries = vsw_wretries;
D1(vswp, "vsw_send_msg (%lld) enter : sending %d bytes",
ldcp->ldc_id, size);
D2(vswp, "send_msg: type 0x%llx", tag->vio_msgtype);
D2(vswp, "send_msg: stype 0x%llx", tag->vio_subtype);
D2(vswp, "send_msg: senv 0x%llx", tag->vio_subtype_env);
mutex_enter(&ldcp->ldc_txlock);
if (tag->vio_subtype == VIO_SUBTYPE_INFO) {
if (tag->vio_subtype_env == VIO_DRING_DATA) {
dmsg = (vio_dring_msg_t *)tag;
dmsg->seq_num = ldcp->lane_out.seq_num;
data_msg = B_TRUE;
} else if (tag->vio_subtype_env == VIO_PKT_DATA) {
rmsg = (vio_raw_data_msg_t *)tag;
rmsg->seq_num = ldcp->lane_out.seq_num;
data_msg = B_TRUE;
} else if (tag->vio_subtype_env == VIO_DESC_DATA) {
imsg = (vnet_ibnd_desc_t *)tag;
imsg->hdr.seq_num = ldcp->lane_out.seq_num;
data_msg = B_TRUE;
}
}
do {
msglen = size;
rv = ldc_write(ldcp->ldc_handle, (caddr_t)msgp, &msglen);
} while (rv == EWOULDBLOCK && --retries > 0);
if (rv == 0 && data_msg == B_TRUE) {
ldcp->lane_out.seq_num++;
}
if ((rv != 0) || (msglen != size)) {
DERR(vswp, "vsw_send_msg:ldc_write failed: chan(%lld) rv(%d) "
"size (%d) msglen(%d)\n", ldcp->ldc_id, rv, size, msglen);
ldcp->ldc_stats.oerrors++;
}
mutex_exit(&ldcp->ldc_txlock);
/*
* If channel has been reset we either handle it here or
* simply report back that it has been reset and let caller
* decide what to do.
*/
if (rv == ECONNRESET) {
DWARN(vswp, "%s (%lld) channel reset", __func__, ldcp->ldc_id);
if (handle_reset) {
vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
}
}
return (rv);
}
/*
* A per LDC worker thread to process ldc messages. This thread is woken up by
* the LDC interrupt handler to process LDC packets and receive data.
*/
void
vsw_ldc_msg_worker(void *arg)
{
callb_cpr_t cprinfo;
vsw_ldc_t *ldcp = (vsw_ldc_t *)arg;
vsw_t *vswp = ldcp->ldc_vswp;
D1(vswp, "%s(%lld):enter\n", __func__, ldcp->ldc_id);
CALLB_CPR_INIT(&cprinfo, &ldcp->msg_thr_lock, callb_generic_cpr,
"vsw_msg_thread");
mutex_enter(&ldcp->msg_thr_lock);
while (!(ldcp->msg_thr_flags & VSW_WTHR_STOP)) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
/*
* Wait until the data is received or a stop
* request is received.
*/
while (!(ldcp->msg_thr_flags &
(VSW_WTHR_DATARCVD | VSW_WTHR_STOP))) {
cv_wait(&ldcp->msg_thr_cv, &ldcp->msg_thr_lock);
}
CALLB_CPR_SAFE_END(&cprinfo, &ldcp->msg_thr_lock)
/*
* First process the stop request.
*/
if (ldcp->msg_thr_flags & VSW_WTHR_STOP) {
D2(vswp, "%s(%lld):Rx thread stopped\n",
__func__, ldcp->ldc_id);
break;
}
ldcp->msg_thr_flags &= ~VSW_WTHR_DATARCVD;
mutex_exit(&ldcp->msg_thr_lock);
D1(vswp, "%s(%lld):calling vsw_process_pkt\n",
__func__, ldcp->ldc_id);
mutex_enter(&ldcp->ldc_cblock);
vsw_process_pkt(ldcp);
mutex_exit(&ldcp->ldc_cblock);
mutex_enter(&ldcp->msg_thr_lock);
}
/*
* Update the run status and wakeup the thread that
* has sent the stop request.
*/
ldcp->msg_thr_flags &= ~VSW_WTHR_STOP;
ldcp->msg_thread = NULL;
CALLB_CPR_EXIT(&cprinfo);
D1(vswp, "%s(%lld):exit\n", __func__, ldcp->ldc_id);
thread_exit();
}
/* Co-ordinate with msg processing thread to stop it */
void
vsw_stop_msg_thread(vsw_ldc_t *ldcp)
{
kt_did_t tid = 0;
vsw_t *vswp = ldcp->ldc_vswp;
D1(vswp, "%s(%lld):enter\n", __func__, ldcp->ldc_id);
/*
* Send a stop request by setting the stop flag and
* wait until the msg process thread stops.
*/
mutex_enter(&ldcp->msg_thr_lock);
if (ldcp->msg_thread != NULL) {
tid = ldcp->msg_thread->t_did;
ldcp->msg_thr_flags |= VSW_WTHR_STOP;
cv_signal(&ldcp->msg_thr_cv);
}
mutex_exit(&ldcp->msg_thr_lock);
if (tid != 0) {
thread_join(tid);
}
D1(vswp, "%s(%lld):exit\n", __func__, ldcp->ldc_id);
}
/*
* Send packet out via descriptor ring to a logical device.
*/
int
vsw_dringsend(vsw_ldc_t *ldcp, mblk_t *mp)
{
vio_dring_msg_t dring_pkt;
dring_info_t *dp = NULL;
vsw_private_desc_t *priv_desc = NULL;
vnet_public_desc_t *pub = NULL;
vsw_t *vswp = ldcp->ldc_vswp;
mblk_t *bp;
size_t n, size;
caddr_t bufp;
int idx;
int status = LDC_TX_SUCCESS;
struct ether_header *ehp = (struct ether_header *)mp->b_rptr;
lane_t *lp = &ldcp->lane_out;
D1(vswp, "%s(%lld): enter\n", __func__, ldcp->ldc_id);
/* TODO: make test a macro */
if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == 0)) {
DWARN(vswp, "%s(%lld) status(%d) lstate(0x%llx), dropping "
"packet\n", __func__, ldcp->ldc_id, ldcp->ldc_status,
ldcp->lane_out.lstate);
ldcp->ldc_stats.oerrors++;
return (LDC_TX_FAILURE);
}
if ((dp = ldcp->lane_out.dringp) == NULL) {
DERR(vswp, "%s(%lld): no dring for outbound lane on"
" channel %d", __func__, ldcp->ldc_id, ldcp->ldc_id);
ldcp->ldc_stats.oerrors++;
return (LDC_TX_FAILURE);
}
size = msgsize(mp);
if (size > (size_t)lp->mtu) {
DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
ldcp->ldc_id, size);
ldcp->ldc_stats.oerrors++;
return (LDC_TX_FAILURE);
}
/*
* Find a free descriptor
*
* Note: for the moment we are assuming that we will only
* have one dring going from the switch to each of its
* peers. This may change in the future.
*/
if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
D2(vswp, "%s(%lld): no descriptor available for ring "
"at 0x%llx", __func__, ldcp->ldc_id, dp);
/* nothing more we can do */
status = LDC_TX_NORESOURCES;
ldcp->ldc_stats.tx_no_desc++;
goto vsw_dringsend_free_exit;
} else {
D2(vswp, "%s(%lld): free private descriptor found at pos %ld "
"addr 0x%llx\n", __func__, ldcp->ldc_id, idx, priv_desc);
}
/* copy data into the descriptor */
bufp = priv_desc->datap;
bufp += VNET_IPALIGN;
for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
n = MBLKL(bp);
bcopy(bp->b_rptr, bufp, n);
bufp += n;
}
priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
pub = priv_desc->descp;
pub->nbytes = priv_desc->datalen;
/* update statistics */
if (IS_BROADCAST(ehp))
ldcp->ldc_stats.brdcstxmt++;
else if (IS_MULTICAST(ehp))
ldcp->ldc_stats.multixmt++;
ldcp->ldc_stats.opackets++;
ldcp->ldc_stats.obytes += priv_desc->datalen;
mutex_enter(&priv_desc->dstate_lock);
pub->hdr.dstate = VIO_DESC_READY;
mutex_exit(&priv_desc->dstate_lock);
/*
* Determine whether or not we need to send a message to our
* peer prompting them to read our newly updated descriptor(s).
*/
mutex_enter(&dp->restart_lock);
if (dp->restart_reqd) {
dp->restart_reqd = B_FALSE;
ldcp->ldc_stats.dring_data_msgs_sent++;
mutex_exit(&dp->restart_lock);
/*
* Send a vio_dring_msg to peer to prompt them to read
* the updated descriptor ring.
*/
dring_pkt.tag.vio_msgtype = VIO_TYPE_DATA;
dring_pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
dring_pkt.tag.vio_subtype_env = VIO_DRING_DATA;
dring_pkt.tag.vio_sid = ldcp->local_session;
/* Note - for now using first ring */
dring_pkt.dring_ident = dp->ident;
/*
* If last_ack_recv is -1 then we know we've not
* received any ack's yet, so this must be the first
* msg sent, so set the start to the begining of the ring.
*/
mutex_enter(&dp->dlock);
if (dp->last_ack_recv == -1) {
dring_pkt.start_idx = 0;
} else {
dring_pkt.start_idx =
(dp->last_ack_recv + 1) % dp->num_descriptors;
}
dring_pkt.end_idx = -1;
mutex_exit(&dp->dlock);
D3(vswp, "%s(%lld): dring 0x%llx : ident 0x%llx\n", __func__,
ldcp->ldc_id, dp, dring_pkt.dring_ident);
D3(vswp, "%s(%lld): start %lld : end %lld :\n",
__func__, ldcp->ldc_id, dring_pkt.start_idx,
dring_pkt.end_idx);
(void) vsw_send_msg(ldcp, (void *)&dring_pkt,
sizeof (vio_dring_msg_t), B_TRUE);
return (status);
} else {
mutex_exit(&dp->restart_lock);
D2(vswp, "%s(%lld): updating descp %d", __func__,
ldcp->ldc_id, idx);
}
vsw_dringsend_free_exit:
D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
return (status);
}
/*
* Searches the private section of a ring for a free descriptor,
* starting at the location of the last free descriptor found
* previously.
*
* Returns 0 if free descriptor is available, and updates state
* of private descriptor to VIO_DESC_READY, otherwise returns 1.
*
* FUTURE: might need to return contiguous range of descriptors
* as dring info msg assumes all will be contiguous.
*/
int
vsw_dring_find_free_desc(dring_info_t *dringp,
vsw_private_desc_t **priv_p, int *idx)
{
vsw_private_desc_t *addr = NULL;
int num = vsw_num_descriptors;
int ret = 1;
D1(NULL, "%s enter\n", __func__);
ASSERT(dringp->priv_addr != NULL);
D2(NULL, "%s: searching ring, dringp 0x%llx : start pos %lld",
__func__, dringp, dringp->end_idx);
addr = (vsw_private_desc_t *)dringp->priv_addr + dringp->end_idx;
mutex_enter(&addr->dstate_lock);
if (addr->dstate == VIO_DESC_FREE) {
addr->dstate = VIO_DESC_READY;
*priv_p = addr;
*idx = dringp->end_idx;
dringp->end_idx = (dringp->end_idx + 1) % num;
ret = 0;
}
mutex_exit(&addr->dstate_lock);
/* ring full */
if (ret == 1) {
D2(NULL, "%s: no desp free: started at %d", __func__,
dringp->end_idx);
}
D1(NULL, "%s: exit\n", __func__);
return (ret);
}
/* vsw_reclaim_dring -- reclaim descriptors */
int
vsw_reclaim_dring(dring_info_t *dp, int start)
{
int i, j, len;
vsw_private_desc_t *priv_addr;
vnet_public_desc_t *pub_addr;
pub_addr = (vnet_public_desc_t *)dp->pub_addr;
priv_addr = (vsw_private_desc_t *)dp->priv_addr;
len = dp->num_descriptors;
D2(NULL, "%s: start index %ld\n", __func__, start);
j = 0;
for (i = start; j < len; i = (i + 1) % len, j++) {
pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
mutex_enter(&priv_addr->dstate_lock);
if (pub_addr->hdr.dstate != VIO_DESC_DONE) {
mutex_exit(&priv_addr->dstate_lock);
break;
}
pub_addr->hdr.dstate = VIO_DESC_FREE;
priv_addr->dstate = VIO_DESC_FREE;
/* clear all the fields */
priv_addr->datalen = 0;
pub_addr->hdr.ack = 0;
mutex_exit(&priv_addr->dstate_lock);
D3(NULL, "claiming descp:%d pub state:0x%llx priv state 0x%llx",
i, pub_addr->hdr.dstate, priv_addr->dstate);
}
return (j);
}
void
vsw_process_dringdata(void *arg, void *dpkt)
{
vsw_ldc_t *ldcp = arg;
vio_dring_msg_t *dring_pkt;
vnet_public_desc_t desc, *pub_addr = NULL;
vsw_private_desc_t *priv_addr = NULL;
dring_info_t *dp = NULL;
vsw_t *vswp = ldcp->ldc_vswp;
mblk_t *mp = NULL;
vio_mblk_t *vmp = NULL;
mblk_t *bp = NULL;
mblk_t *bpt = NULL;
size_t nbytes = 0;
uint64_t chain = 0;
uint64_t len;
uint32_t pos, start;
uint32_t range_start, range_end;
int32_t end, num, cnt = 0;
int i, rv, rng_rv = 0, msg_rv = 0;
boolean_t prev_desc_ack = B_FALSE;
int read_attempts = 0;
struct ether_header *ehp;
lane_t *lp = &ldcp->lane_out;
D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
/*
* We know this is a data/dring packet so
* cast it into the correct structure.
*/
dring_pkt = (vio_dring_msg_t *)dpkt;
/*
* Switch on the vio_subtype. If its INFO then we need to
* process the data. If its an ACK we need to make sure
* it makes sense (i.e did we send an earlier data/info),
* and if its a NACK then we maybe attempt a retry.
*/
switch (dring_pkt->tag.vio_subtype) {
case VIO_SUBTYPE_INFO:
D2(vswp, "%s(%lld): VIO_SUBTYPE_INFO", __func__, ldcp->ldc_id);
dp = ldcp->lane_in.dringp;
if (dp->ident != dring_pkt->dring_ident) {
DERR(vswp, "%s(%lld): unable to find dring from "
"ident 0x%llx", __func__, ldcp->ldc_id,
dring_pkt->dring_ident);
SND_DRING_NACK(ldcp, dring_pkt);
return;
}
ldcp->ldc_stats.dring_data_msgs_rcvd++;
start = pos = dring_pkt->start_idx;
end = dring_pkt->end_idx;
len = dp->num_descriptors;
range_start = range_end = pos;
D2(vswp, "%s(%lld): start index %ld : end %ld\n",
__func__, ldcp->ldc_id, start, end);
if (end == -1) {
num = -1;
} else if (end >= 0) {
num = end >= pos ? end - pos + 1: (len - pos + 1) + end;
/* basic sanity check */
if (end > len) {
DERR(vswp, "%s(%lld): endpoint %lld outside "
"ring length %lld", __func__,
ldcp->ldc_id, end, len);
SND_DRING_NACK(ldcp, dring_pkt);
return;
}
} else {
DERR(vswp, "%s(%lld): invalid endpoint %lld",
__func__, ldcp->ldc_id, end);
SND_DRING_NACK(ldcp, dring_pkt);
return;
}
while (cnt != num) {
vsw_recheck_desc:
pub_addr = (vnet_public_desc_t *)dp->pub_addr + pos;
if ((rng_rv = vnet_dring_entry_copy(pub_addr,
&desc, dp->dring_mtype, dp->dring_handle,
pos, pos)) != 0) {
DERR(vswp, "%s(%lld): unable to copy "
"descriptor at pos %d: err %d",
__func__, pos, ldcp->ldc_id, rng_rv);
ldcp->ldc_stats.ierrors++;
break;
}
/*
* When given a bounded range of descriptors
* to process, its an error to hit a descriptor
* which is not ready. In the non-bounded case
* (end_idx == -1) this simply indicates we have
* reached the end of the current active range.
*/
if (desc.hdr.dstate != VIO_DESC_READY) {
/* unbound - no error */
if (end == -1) {
if (read_attempts == vsw_recv_retries)
break;
delay(drv_usectohz(vsw_recv_delay));
read_attempts++;
goto vsw_recheck_desc;
}
/* bounded - error - so NACK back */
DERR(vswp, "%s(%lld): descriptor not READY "
"(%d)", __func__, ldcp->ldc_id,
desc.hdr.dstate);
SND_DRING_NACK(ldcp, dring_pkt);
return;
}
DTRACE_PROBE1(read_attempts, int, read_attempts);
range_end = pos;
/*
* If we ACK'd the previous descriptor then now
* record the new range start position for later
* ACK's.
*/
if (prev_desc_ack) {
range_start = pos;
D2(vswp, "%s(%lld): updating range start to be "
"%d", __func__, ldcp->ldc_id, range_start);
prev_desc_ack = B_FALSE;
}
D2(vswp, "%s(%lld): processing desc %lld at pos"
" 0x%llx : dstate 0x%lx : datalen 0x%lx",
__func__, ldcp->ldc_id, pos, &desc,
desc.hdr.dstate, desc.nbytes);
if ((desc.nbytes < ETHERMIN) ||
(desc.nbytes > lp->mtu)) {
/* invalid size; drop the packet */
ldcp->ldc_stats.ierrors++;
goto vsw_process_desc_done;
}
/*
* Ensure that we ask ldc for an aligned
* number of bytes. Data is padded to align on 8
* byte boundary, desc.nbytes is actual data length,
* i.e. minus that padding.
*/
nbytes = (desc.nbytes + VNET_IPALIGN + 7) & ~7;
if (nbytes > ldcp->max_rxpool_size) {
mp = allocb(desc.nbytes + VNET_IPALIGN + 8,
BPRI_MED);
vmp = NULL;
} else {
vmp = vio_multipool_allocb(&ldcp->vmp, nbytes);
if (vmp == NULL) {
ldcp->ldc_stats.rx_vio_allocb_fail++;
/*
* No free receive buffers available,
* so fallback onto allocb(9F). Make
* sure that we get a data buffer which
* is a multiple of 8 as this is
* required by ldc_mem_copy.
*/
DTRACE_PROBE(allocb);
mp = allocb(desc.nbytes +
VNET_IPALIGN + 8, BPRI_MED);
} else {
mp = vmp->mp;
}
}
if (mp == NULL) {
DERR(vswp, "%s(%ld): allocb failed",
__func__, ldcp->ldc_id);
rng_rv = vnet_dring_entry_set_dstate(pub_addr,
dp->dring_mtype, dp->dring_handle, pos, pos,
VIO_DESC_DONE);
ldcp->ldc_stats.ierrors++;
ldcp->ldc_stats.rx_allocb_fail++;
break;
}
rv = ldc_mem_copy(ldcp->ldc_handle,
(caddr_t)mp->b_rptr, 0, &nbytes,
desc.memcookie, desc.ncookies, LDC_COPY_IN);
if (rv != 0) {
DERR(vswp, "%s(%d): unable to copy in data "
"from %d cookies in desc %d (rv %d)",
__func__, ldcp->ldc_id, desc.ncookies,
pos, rv);
freemsg(mp);
rng_rv = vnet_dring_entry_set_dstate(pub_addr,
dp->dring_mtype, dp->dring_handle, pos, pos,
VIO_DESC_DONE);
ldcp->ldc_stats.ierrors++;
break;
} else {
D2(vswp, "%s(%d): copied in %ld bytes"
" using %d cookies", __func__,
ldcp->ldc_id, nbytes, desc.ncookies);
}
/* adjust the read pointer to skip over the padding */
mp->b_rptr += VNET_IPALIGN;
/* point to the actual end of data */
mp->b_wptr = mp->b_rptr + desc.nbytes;
if (vmp != NULL) {
vmp->state = VIO_MBLK_HAS_DATA;
}
/* update statistics */
ehp = (struct ether_header *)mp->b_rptr;
if (IS_BROADCAST(ehp))
ldcp->ldc_stats.brdcstrcv++;
else if (IS_MULTICAST(ehp))
ldcp->ldc_stats.multircv++;
ldcp->ldc_stats.ipackets++;
ldcp->ldc_stats.rbytes += desc.nbytes;
/*
* IPALIGN space can be used for VLAN_TAG
*/
(void) vsw_vlan_frame_pretag(ldcp->ldc_port,
VSW_VNETPORT, mp);
/* build a chain of received packets */
if (bp == NULL) {
/* first pkt */
bp = mp;
bp->b_next = bp->b_prev = NULL;
bpt = bp;
chain = 1;
} else {
mp->b_next = mp->b_prev = NULL;
bpt->b_next = mp;
bpt = mp;
chain++;
}
vsw_process_desc_done:
/* mark we are finished with this descriptor */
if ((rng_rv = vnet_dring_entry_set_dstate(pub_addr,
dp->dring_mtype, dp->dring_handle, pos, pos,
VIO_DESC_DONE)) != 0) {
DERR(vswp, "%s(%lld): unable to update "
"dstate at pos %d: err %d",
__func__, pos, ldcp->ldc_id, rng_rv);
ldcp->ldc_stats.ierrors++;
break;
}
/*
* Send an ACK back to peer if requested.
*/
if (desc.hdr.ack) {
dring_pkt->start_idx = range_start;
dring_pkt->end_idx = range_end;
DERR(vswp, "%s(%lld): processed %d %d, ACK"
" requested", __func__, ldcp->ldc_id,
dring_pkt->start_idx, dring_pkt->end_idx);
dring_pkt->dring_process_state = VIO_DP_ACTIVE;
dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
dring_pkt->tag.vio_sid = ldcp->local_session;
msg_rv = vsw_send_msg(ldcp, (void *)dring_pkt,
sizeof (vio_dring_msg_t), B_FALSE);
/*
* Check if ACK was successfully sent. If not
* we break and deal with that below.
*/
if (msg_rv != 0)
break;
prev_desc_ack = B_TRUE;
range_start = pos;
}
/* next descriptor */
pos = (pos + 1) % len;
cnt++;
/*
* Break out of loop here and stop processing to
* allow some other network device (or disk) to
* get access to the cpu.
*/
if (chain > vsw_chain_len) {
D3(vswp, "%s(%lld): switching chain of %d "
"msgs", __func__, ldcp->ldc_id, chain);
break;
}
}
/* send the chain of packets to be switched */
if (bp != NULL) {
DTRACE_PROBE1(vsw_rcv_msgs, int, chain);
D3(vswp, "%s(%lld): switching chain of %d msgs",
__func__, ldcp->ldc_id, chain);
vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
ldcp->ldc_port, NULL);
}
/*
* If when we encountered an error when attempting to
* access an imported dring, initiate a connection reset.
*/
if (rng_rv != 0) {
vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
break;
}
/*
* If when we attempted to send the ACK we found that the
* channel had been reset then now handle this.
*/
if (msg_rv == ECONNRESET) {
vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
break;
}
DTRACE_PROBE1(msg_cnt, int, cnt);
/*
* We are now finished so ACK back with the state
* set to STOPPING so our peer knows we are finished
*/
dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
dring_pkt->tag.vio_sid = ldcp->local_session;
dring_pkt->dring_process_state = VIO_DP_STOPPED;
DTRACE_PROBE(stop_process_sent);
/*
* We have not processed any more descriptors beyond
* the last one we ACK'd.
*/
if (prev_desc_ack)
range_start = range_end;
dring_pkt->start_idx = range_start;
dring_pkt->end_idx = range_end;
D2(vswp, "%s(%lld) processed : %d : %d, now stopping",
__func__, ldcp->ldc_id, dring_pkt->start_idx,
dring_pkt->end_idx);
(void) vsw_send_msg(ldcp, (void *)dring_pkt,
sizeof (vio_dring_msg_t), B_TRUE);
ldcp->ldc_stats.dring_data_acks_sent++;
ldcp->ldc_stats.dring_stopped_acks_sent++;
break;
case VIO_SUBTYPE_ACK:
D2(vswp, "%s(%lld): VIO_SUBTYPE_ACK", __func__, ldcp->ldc_id);
/*
* Verify that the relevant descriptors are all
* marked as DONE
*/
dp = ldcp->lane_out.dringp;
if (dp->ident != dring_pkt->dring_ident) {
DERR(vswp, "%s: unknown ident in ACK", __func__);
return;
}
start = end = 0;
start = dring_pkt->start_idx;
end = dring_pkt->end_idx;
len = dp->num_descriptors;
mutex_enter(&dp->dlock);
dp->last_ack_recv = end;
ldcp->ldc_stats.dring_data_acks_rcvd++;
mutex_exit(&dp->dlock);
(void) vsw_reclaim_dring(dp, start);
/*
* If our peer is stopping processing descriptors then
* we check to make sure it has processed all the descriptors
* we have updated. If not then we send it a new message
* to prompt it to restart.
*/
if (dring_pkt->dring_process_state == VIO_DP_STOPPED) {
DTRACE_PROBE(stop_process_recv);
D2(vswp, "%s(%lld): got stopping msg : %d : %d",
__func__, ldcp->ldc_id, dring_pkt->start_idx,
dring_pkt->end_idx);
/*
* Check next descriptor in public section of ring.
* If its marked as READY then we need to prompt our
* peer to start processing the ring again.
*/
i = (end + 1) % len;
pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
/*
* Hold the restart lock across all of this to
* make sure that its not possible for us to
* decide that a msg needs to be sent in the future
* but the sending code having already checked is
* about to exit.
*/
mutex_enter(&dp->restart_lock);
ldcp->ldc_stats.dring_stopped_acks_rcvd++;
mutex_enter(&priv_addr->dstate_lock);
if (pub_addr->hdr.dstate == VIO_DESC_READY) {
mutex_exit(&priv_addr->dstate_lock);
dring_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
dring_pkt->tag.vio_sid = ldcp->local_session;
dring_pkt->start_idx = (end + 1) % len;
dring_pkt->end_idx = -1;
D2(vswp, "%s(%lld) : sending restart msg:"
" %d : %d", __func__, ldcp->ldc_id,
dring_pkt->start_idx, dring_pkt->end_idx);
msg_rv = vsw_send_msg(ldcp, (void *)dring_pkt,
sizeof (vio_dring_msg_t), B_FALSE);
ldcp->ldc_stats.dring_data_msgs_sent++;
} else {
mutex_exit(&priv_addr->dstate_lock);
dp->restart_reqd = B_TRUE;
}
mutex_exit(&dp->restart_lock);
}
if (msg_rv == ECONNRESET)
vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
break;
case VIO_SUBTYPE_NACK:
DWARN(vswp, "%s(%lld): VIO_SUBTYPE_NACK",
__func__, ldcp->ldc_id);
/*
* Something is badly wrong if we are getting NACK's
* for our data pkts. So reset the channel.
*/
vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
break;
default:
DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
ldcp->ldc_id, dring_pkt->tag.vio_subtype);
}
D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
}
|