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
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
|
/*
* Copyright (c) 2004 Topspin Communications. All rights reserved.
* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) 2004 Voltaire, Inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - 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.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "ipoib.h"
static int ipoib_resolvemulti(struct ifnet *, struct sockaddr **,
struct sockaddr *);
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <linux/if_arp.h> /* For ARPHRD_xxx */
#include <linux/if_vlan.h>
#include <net/ip.h>
#include <net/ipv6.h>
MODULE_AUTHOR("Roland Dreier");
MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
MODULE_LICENSE("Dual BSD/GPL");
int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
int ipoib_debug_level = 1;
module_param_named(debug_level, ipoib_debug_level, int, 0644);
MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
#endif
struct ipoib_path_iter {
struct ipoib_dev_priv *priv;
struct ipoib_path path;
};
static const u8 ipv4_bcast_addr[] = {
0x00, 0xff, 0xff, 0xff,
0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
};
struct workqueue_struct *ipoib_workqueue;
struct ib_sa_client ipoib_sa_client;
static void ipoib_add_one(struct ib_device *device);
static void ipoib_remove_one(struct ib_device *device);
static void ipoib_start(struct ifnet *dev);
static int ipoib_output(struct ifnet *ifp, struct mbuf *m,
const struct sockaddr *dst, struct route *ro);
static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
static void ipoib_input(struct ifnet *ifp, struct mbuf *m);
#define IPOIB_MTAP(_ifp, _m) \
do { \
if (bpf_peers_present((_ifp)->if_bpf)) { \
M_ASSERTVALID(_m); \
ipoib_mtap_mb((_ifp), (_m)); \
} \
} while (0)
/*
* This is for clients that have an ipoib_header in the mbuf.
*/
static void
ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb)
{
struct ipoib_header *ih;
struct ether_header eh;
ih = mtod(mb, struct ipoib_header *);
eh.ether_type = ih->proto;
bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN);
bzero(&eh.ether_shost, ETHER_ADDR_LEN);
mb->m_data += sizeof(struct ipoib_header);
mb->m_len -= sizeof(struct ipoib_header);
bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
mb->m_data -= sizeof(struct ipoib_header);
mb->m_len += sizeof(struct ipoib_header);
}
void
ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto)
{
struct ether_header eh;
eh.ether_type = proto;
bzero(&eh.ether_shost, ETHER_ADDR_LEN);
bzero(&eh.ether_dhost, ETHER_ADDR_LEN);
bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
}
static struct ib_client ipoib_client = {
.name = "ipoib",
.add = ipoib_add_one,
.remove = ipoib_remove_one
};
int
ipoib_open(struct ipoib_dev_priv *priv)
{
struct ifnet *dev = priv->dev;
ipoib_dbg(priv, "bringing up interface\n");
set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
if (ipoib_pkey_dev_delay_open(priv))
return 0;
if (ipoib_ib_dev_open(priv))
goto err_disable;
if (ipoib_ib_dev_up(priv))
goto err_stop;
if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
struct ipoib_dev_priv *cpriv;
/* Bring up any child interfaces too */
mutex_lock(&priv->vlan_mutex);
list_for_each_entry(cpriv, &priv->child_intfs, list)
if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
ipoib_open(cpriv);
mutex_unlock(&priv->vlan_mutex);
}
dev->if_drv_flags |= IFF_DRV_RUNNING;
dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
return 0;
err_stop:
ipoib_ib_dev_stop(priv, 1);
err_disable:
clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
return -EINVAL;
}
static void
ipoib_init(void *arg)
{
struct ifnet *dev;
struct ipoib_dev_priv *priv;
priv = arg;
dev = priv->dev;
if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
ipoib_open(priv);
queue_work(ipoib_workqueue, &priv->flush_light);
}
static int
ipoib_stop(struct ipoib_dev_priv *priv)
{
struct ifnet *dev = priv->dev;
ipoib_dbg(priv, "stopping interface\n");
clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
ipoib_ib_dev_down(priv, 0);
ipoib_ib_dev_stop(priv, 0);
if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
struct ipoib_dev_priv *cpriv;
/* Bring down any child interfaces too */
mutex_lock(&priv->vlan_mutex);
list_for_each_entry(cpriv, &priv->child_intfs, list)
if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0)
ipoib_stop(cpriv);
mutex_unlock(&priv->vlan_mutex);
}
return 0;
}
int
ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu)
{
struct ifnet *dev = priv->dev;
/* dev->if_mtu > 2K ==> connected mode */
if (ipoib_cm_admin_enabled(priv)) {
if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
return -EINVAL;
if (new_mtu > priv->mcast_mtu)
ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
priv->mcast_mtu);
dev->if_mtu = new_mtu;
return 0;
}
if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
return -EINVAL;
priv->admin_mtu = new_mtu;
dev->if_mtu = min(priv->mcast_mtu, priv->admin_mtu);
queue_work(ipoib_workqueue, &priv->flush_light);
return 0;
}
static int
ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
struct ipoib_dev_priv *priv = ifp->if_softc;
struct ifaddr *ifa = (struct ifaddr *) data;
struct ifreq *ifr = (struct ifreq *) data;
int error = 0;
switch (command) {
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
error = -ipoib_open(priv);
} else
if (ifp->if_drv_flags & IFF_DRV_RUNNING)
ipoib_stop(priv);
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
if (ifp->if_drv_flags & IFF_DRV_RUNNING)
queue_work(ipoib_workqueue, &priv->restart_task);
break;
case SIOCSIFADDR:
ifp->if_flags |= IFF_UP;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
ifp->if_init(ifp->if_softc); /* before arpwhohas */
arp_ifinit(ifp, ifa);
break;
#endif
default:
ifp->if_init(ifp->if_softc);
break;
}
break;
case SIOCGIFADDR:
{
struct sockaddr *sa;
sa = (struct sockaddr *) & ifr->ifr_data;
bcopy(IF_LLADDR(ifp),
(caddr_t) sa->sa_data, INFINIBAND_ALEN);
}
break;
case SIOCSIFMTU:
/*
* Set the interface MTU.
*/
error = -ipoib_change_mtu(priv, ifr->ifr_mtu);
break;
default:
error = EINVAL;
break;
}
return (error);
}
static struct ipoib_path *
__path_find(struct ipoib_dev_priv *priv, void *gid)
{
struct rb_node *n = priv->path_tree.rb_node;
struct ipoib_path *path;
int ret;
while (n) {
path = rb_entry(n, struct ipoib_path, rb_node);
ret = memcmp(gid, path->pathrec.dgid.raw,
sizeof (union ib_gid));
if (ret < 0)
n = n->rb_left;
else if (ret > 0)
n = n->rb_right;
else
return path;
}
return NULL;
}
static int
__path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
{
struct rb_node **n = &priv->path_tree.rb_node;
struct rb_node *pn = NULL;
struct ipoib_path *tpath;
int ret;
while (*n) {
pn = *n;
tpath = rb_entry(pn, struct ipoib_path, rb_node);
ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
sizeof (union ib_gid));
if (ret < 0)
n = &pn->rb_left;
else if (ret > 0)
n = &pn->rb_right;
else
return -EEXIST;
}
rb_link_node(&path->rb_node, pn, n);
rb_insert_color(&path->rb_node, &priv->path_tree);
list_add_tail(&path->list, &priv->path_list);
return 0;
}
void
ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
{
_IF_DRAIN(&path->queue);
if (path->ah)
ipoib_put_ah(path->ah);
if (ipoib_cm_get(path))
ipoib_cm_destroy_tx(ipoib_cm_get(path));
kfree(path);
}
#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
struct ipoib_path_iter *
ipoib_path_iter_init(struct ipoib_dev_priv *priv)
{
struct ipoib_path_iter *iter;
iter = kmalloc(sizeof *iter, GFP_KERNEL);
if (!iter)
return NULL;
iter->priv = priv;
memset(iter->path.pathrec.dgid.raw, 0, 16);
if (ipoib_path_iter_next(iter)) {
kfree(iter);
return NULL;
}
return iter;
}
int
ipoib_path_iter_next(struct ipoib_path_iter *iter)
{
struct ipoib_dev_priv *priv = iter->priv;
struct rb_node *n;
struct ipoib_path *path;
int ret = 1;
spin_lock_irq(&priv->lock);
n = rb_first(&priv->path_tree);
while (n) {
path = rb_entry(n, struct ipoib_path, rb_node);
if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
sizeof (union ib_gid)) < 0) {
iter->path = *path;
ret = 0;
break;
}
n = rb_next(n);
}
spin_unlock_irq(&priv->lock);
return ret;
}
void
ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
{
*path = iter->path;
}
#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
void
ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
{
struct ipoib_path *path, *tp;
spin_lock_irq(&priv->lock);
list_for_each_entry_safe(path, tp, &priv->path_list, list) {
ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
be16_to_cpu(path->pathrec.dlid),
path->pathrec.dgid.raw, ":");
path->valid = 0;
}
spin_unlock_irq(&priv->lock);
}
void
ipoib_flush_paths(struct ipoib_dev_priv *priv)
{
struct ipoib_path *path, *tp;
LIST_HEAD(remove_list);
unsigned long flags;
spin_lock_irqsave(&priv->lock, flags);
list_splice_init(&priv->path_list, &remove_list);
list_for_each_entry(path, &remove_list, list)
rb_erase(&path->rb_node, &priv->path_tree);
list_for_each_entry_safe(path, tp, &remove_list, list) {
if (path->query)
ib_sa_cancel_query(path->query_id, path->query);
spin_unlock_irqrestore(&priv->lock, flags);
wait_for_completion(&path->done);
ipoib_path_free(priv, path);
spin_lock_irqsave(&priv->lock, flags);
}
spin_unlock_irqrestore(&priv->lock, flags);
}
static void
path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
{
struct ipoib_path *path = path_ptr;
struct ipoib_dev_priv *priv = path->priv;
struct ifnet *dev = priv->dev;
struct ipoib_ah *ah = NULL;
struct ipoib_ah *old_ah = NULL;
struct ifqueue mbqueue;
struct mbuf *mb;
unsigned long flags;
if (!status)
ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
else
ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
status, path->pathrec.dgid.raw, ":");
bzero(&mbqueue, sizeof(mbqueue));
if (!status) {
struct ib_ah_attr av;
if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
ah = ipoib_create_ah(priv, priv->pd, &av);
}
spin_lock_irqsave(&priv->lock, flags);
if (ah) {
path->pathrec = *pathrec;
old_ah = path->ah;
path->ah = ah;
ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
for (;;) {
_IF_DEQUEUE(&path->queue, mb);
if (mb == NULL)
break;
_IF_ENQUEUE(&mbqueue, mb);
}
#ifdef CONFIG_INFINIBAND_IPOIB_CM
if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
#endif
path->valid = 1;
}
path->query = NULL;
complete(&path->done);
spin_unlock_irqrestore(&priv->lock, flags);
if (old_ah)
ipoib_put_ah(old_ah);
for (;;) {
_IF_DEQUEUE(&mbqueue, mb);
if (mb == NULL)
break;
mb->m_pkthdr.rcvif = dev;
if (dev->if_transmit(dev, mb))
ipoib_warn(priv, "dev_queue_xmit failed "
"to requeue packet\n");
}
}
static struct ipoib_path *
path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
{
struct ipoib_path *path;
if (!priv->broadcast)
return NULL;
path = kzalloc(sizeof *path, GFP_ATOMIC);
if (!path)
return NULL;
path->priv = priv;
bzero(&path->queue, sizeof(path->queue));
#ifdef CONFIG_INFINIBAND_IPOIB_CM
memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
#endif
memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
path->pathrec.sgid = priv->local_gid;
path->pathrec.pkey = cpu_to_be16(priv->pkey);
path->pathrec.numb_path = 1;
path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
return path;
}
static int
path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
{
struct ifnet *dev = priv->dev;
ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
struct ib_sa_path_rec p_rec;
p_rec = path->pathrec;
p_rec.mtu_selector = IB_SA_GT;
switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) {
case 512:
p_rec.mtu = IB_MTU_256;
break;
case 1024:
p_rec.mtu = IB_MTU_512;
break;
case 2048:
p_rec.mtu = IB_MTU_1024;
break;
case 4096:
p_rec.mtu = IB_MTU_2048;
break;
default:
/* Wildcard everything */
comp_mask = 0;
p_rec.mtu = 0;
p_rec.mtu_selector = 0;
}
ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
p_rec.dgid.raw, ":",
comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
init_completion(&path->done);
path->query_id =
ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
&p_rec, comp_mask |
IB_SA_PATH_REC_DGID |
IB_SA_PATH_REC_SGID |
IB_SA_PATH_REC_NUMB_PATH |
IB_SA_PATH_REC_TRAFFIC_CLASS |
IB_SA_PATH_REC_PKEY,
1000, GFP_ATOMIC,
path_rec_completion,
path, &path->query);
if (path->query_id < 0) {
ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
path->query = NULL;
complete(&path->done);
return path->query_id;
}
return 0;
}
static void
ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
{
struct ipoib_path *path;
path = __path_find(priv, eh->hwaddr + 4);
if (!path || !path->valid) {
int new_path = 0;
if (!path) {
path = path_rec_create(priv, eh->hwaddr);
new_path = 1;
}
if (path) {
_IF_ENQUEUE(&path->queue, mb);
if (!path->query && path_rec_start(priv, path)) {
spin_unlock_irqrestore(&priv->lock, flags);
if (new_path)
ipoib_path_free(priv, path);
return;
} else
__path_add(priv, path);
} else {
++priv->dev->if_oerrors;
m_freem(mb);
}
return;
}
if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
ipoib_cm_send(priv, mb, ipoib_cm_get(path));
} else if (path->ah) {
ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
} else if ((path->query || !path_rec_start(priv, path)) &&
path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
_IF_ENQUEUE(&path->queue, mb);
} else {
++priv->dev->if_oerrors;
m_freem(mb);
}
}
static int
ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
{
struct ipoib_header *eh;
eh = mtod(mb, struct ipoib_header *);
if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
/* Add in the P_Key for multicast*/
eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
eh->hwaddr[9] = priv->pkey & 0xff;
ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
} else
ipoib_unicast_send(mb, priv, eh);
return 0;
}
static void
_ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
{
struct mbuf *mb;
if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
IFF_DRV_RUNNING)
return;
spin_lock(&priv->lock);
while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
(dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
if (mb == NULL)
break;
IPOIB_MTAP(dev, mb);
ipoib_send_one(priv, mb);
}
spin_unlock(&priv->lock);
}
static void
ipoib_start(struct ifnet *dev)
{
_ipoib_start(dev, dev->if_softc);
}
static void
ipoib_vlan_start(struct ifnet *dev)
{
struct ipoib_dev_priv *priv;
struct mbuf *mb;
priv = VLAN_COOKIE(dev);
if (priv != NULL)
return _ipoib_start(dev, priv);
while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
if (mb == NULL)
break;
m_freem(mb);
dev->if_oerrors++;
}
}
int
ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
{
/* Allocate RX/TX "rings" to hold queued mbs */
priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
GFP_KERNEL);
if (!priv->rx_ring) {
printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
ca->name, ipoib_recvq_size);
goto out;
}
priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
if (!priv->tx_ring) {
printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
ca->name, ipoib_sendq_size);
goto out_rx_ring_cleanup;
}
memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
if (ipoib_ib_dev_init(priv, ca, port))
goto out_tx_ring_cleanup;
return 0;
out_tx_ring_cleanup:
kfree(priv->tx_ring);
out_rx_ring_cleanup:
kfree(priv->rx_ring);
out:
return -ENOMEM;
}
static void
ipoib_detach(struct ipoib_dev_priv *priv)
{
struct ifnet *dev;
dev = priv->dev;
if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
bpfdetach(dev);
if_detach(dev);
if_free(dev);
} else
VLAN_SETCOOKIE(priv->dev, NULL);
free(priv, M_TEMP);
}
void
ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
{
struct ipoib_dev_priv *cpriv, *tcpriv;
/* Delete any child interfaces first */
list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
ipoib_dev_cleanup(cpriv);
ipoib_detach(cpriv);
}
ipoib_ib_dev_cleanup(priv);
kfree(priv->rx_ring);
kfree(priv->tx_ring);
priv->rx_ring = NULL;
priv->tx_ring = NULL;
}
static volatile int ipoib_unit;
static struct ipoib_dev_priv *
ipoib_priv_alloc(void)
{
struct ipoib_dev_priv *priv;
priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
spin_lock_init(&priv->lock);
mutex_init(&priv->vlan_mutex);
INIT_LIST_HEAD(&priv->path_list);
INIT_LIST_HEAD(&priv->child_intfs);
INIT_LIST_HEAD(&priv->dead_ahs);
INIT_LIST_HEAD(&priv->multicast_list);
INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
return (priv);
}
struct ipoib_dev_priv *
ipoib_intf_alloc(const char *name)
{
struct ipoib_dev_priv *priv;
struct sockaddr_dl *sdl;
struct ifnet *dev;
priv = ipoib_priv_alloc();
dev = priv->dev = if_alloc(IFT_INFINIBAND);
if (!dev) {
free(priv, M_TEMP);
return NULL;
}
dev->if_softc = priv;
if_initname(dev, name, atomic_fetchadd_int(&ipoib_unit, 1));
dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
dev->if_addrlen = INFINIBAND_ALEN;
dev->if_hdrlen = IPOIB_HEADER_LEN;
if_attach(dev);
dev->if_init = ipoib_init;
dev->if_ioctl = ipoib_ioctl;
dev->if_start = ipoib_start;
dev->if_output = ipoib_output;
dev->if_input = ipoib_input;
dev->if_resolvemulti = ipoib_resolvemulti;
if_initbaudrate(dev, IF_Gbps(10));
dev->if_broadcastaddr = priv->broadcastaddr;
dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
sdl->sdl_type = IFT_INFINIBAND;
sdl->sdl_alen = dev->if_addrlen;
priv->dev = dev;
if_link_state_change(dev, LINK_STATE_DOWN);
bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
return dev->if_softc;
}
int
ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
{
struct ib_device_attr *device_attr;
int result = -ENOMEM;
device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
if (!device_attr) {
printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
hca->name, sizeof *device_attr);
return result;
}
result = ib_query_device(hca, device_attr);
if (result) {
printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
hca->name, result);
kfree(device_attr);
return result;
}
priv->hca_caps = device_attr->device_cap_flags;
kfree(device_attr);
priv->dev->if_hwassist = 0;
priv->dev->if_capabilities = 0;
#ifndef CONFIG_INFINIBAND_IPOIB_CM
if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
set_bit(IPOIB_FLAG_CSUM, &priv->flags);
priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
}
#if 0
if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
priv->dev->if_capabilities |= IFCAP_TSO4;
priv->dev->if_hwassist |= CSUM_TSO;
}
#endif
#endif
priv->dev->if_capabilities |=
IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
priv->dev->if_capenable = priv->dev->if_capabilities;
return 0;
}
static struct ifnet *
ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
{
struct ipoib_dev_priv *priv;
struct ib_port_attr attr;
int result = -ENOMEM;
priv = ipoib_intf_alloc(format);
if (!priv)
goto alloc_mem_failed;
if (!ib_query_port(hca, port, &attr))
priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
else {
printk(KERN_WARNING "%s: ib_query_port %d failed\n",
hca->name, port);
goto device_init_failed;
}
/* MTU will be reset when mcast join happens */
priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
result = ib_query_pkey(hca, port, 0, &priv->pkey);
if (result) {
printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
hca->name, port, result);
goto device_init_failed;
}
if (ipoib_set_dev_features(priv, hca))
goto device_init_failed;
/*
* Set the full membership bit, so that we join the right
* broadcast group, etc.
*/
priv->pkey |= 0x8000;
priv->broadcastaddr[8] = priv->pkey >> 8;
priv->broadcastaddr[9] = priv->pkey & 0xff;
result = ib_query_gid(hca, port, 0, &priv->local_gid);
if (result) {
printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
hca->name, port, result);
goto device_init_failed;
}
memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
result = ipoib_dev_init(priv, hca, port);
if (result < 0) {
printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
hca->name, port, result);
goto device_init_failed;
}
if (ipoib_cm_admin_enabled(priv))
priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
INIT_IB_EVENT_HANDLER(&priv->event_handler,
priv->ca, ipoib_event);
result = ib_register_event_handler(&priv->event_handler);
if (result < 0) {
printk(KERN_WARNING "%s: ib_register_event_handler failed for "
"port %d (ret = %d)\n",
hca->name, port, result);
goto event_failed;
}
if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
return priv->dev;
event_failed:
ipoib_dev_cleanup(priv);
device_init_failed:
ipoib_detach(priv);
alloc_mem_failed:
return ERR_PTR(result);
}
static void
ipoib_add_one(struct ib_device *device)
{
struct list_head *dev_list;
struct ifnet *dev;
struct ipoib_dev_priv *priv;
int s, e, p;
if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
return;
dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
if (!dev_list)
return;
INIT_LIST_HEAD(dev_list);
if (device->node_type == RDMA_NODE_IB_SWITCH) {
s = 0;
e = 0;
} else {
s = 1;
e = device->phys_port_cnt;
}
for (p = s; p <= e; ++p) {
if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
continue;
dev = ipoib_add_port("ib", device, p);
if (!IS_ERR(dev)) {
priv = dev->if_softc;
list_add_tail(&priv->list, dev_list);
}
}
ib_set_client_data(device, &ipoib_client, dev_list);
}
static void
ipoib_remove_one(struct ib_device *device)
{
struct ipoib_dev_priv *priv, *tmp;
struct list_head *dev_list;
if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
return;
dev_list = ib_get_client_data(device, &ipoib_client);
list_for_each_entry_safe(priv, tmp, dev_list, list) {
if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
continue;
ipoib_stop(priv);
ib_unregister_event_handler(&priv->event_handler);
/* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
flush_workqueue(ipoib_workqueue);
ipoib_dev_cleanup(priv);
ipoib_detach(priv);
}
kfree(dev_list);
}
static void
ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
{
struct ipoib_dev_priv *parent;
struct ipoib_dev_priv *priv;
struct ifnet *dev;
uint16_t pkey;
int error;
if (ifp->if_type != IFT_INFINIBAND)
return;
dev = VLAN_DEVAT(ifp, vtag);
if (dev == NULL)
return;
priv = NULL;
error = 0;
parent = ifp->if_softc;
/* We only support 15 bits of pkey. */
if (vtag & 0x8000)
return;
pkey = vtag | 0x8000; /* Set full membership bit. */
if (pkey == parent->pkey)
return;
/* Check for dups */
mutex_lock(&parent->vlan_mutex);
list_for_each_entry(priv, &parent->child_intfs, list) {
if (priv->pkey == pkey) {
priv = NULL;
error = EBUSY;
goto out;
}
}
priv = ipoib_priv_alloc();
priv->dev = dev;
priv->max_ib_mtu = parent->max_ib_mtu;
priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
error = ipoib_set_dev_features(priv, parent->ca);
if (error)
goto out;
priv->pkey = pkey;
priv->broadcastaddr[8] = pkey >> 8;
priv->broadcastaddr[9] = pkey & 0xff;
dev->if_broadcastaddr = priv->broadcastaddr;
error = ipoib_dev_init(priv, parent->ca, parent->port);
if (error)
goto out;
priv->parent = parent->dev;
list_add_tail(&priv->list, &parent->child_intfs);
VLAN_SETCOOKIE(dev, priv);
dev->if_start = ipoib_vlan_start;
dev->if_drv_flags &= ~IFF_DRV_RUNNING;
dev->if_hdrlen = IPOIB_HEADER_LEN;
if (ifp->if_drv_flags & IFF_DRV_RUNNING)
ipoib_open(priv);
mutex_unlock(&parent->vlan_mutex);
return;
out:
mutex_unlock(&parent->vlan_mutex);
if (priv)
free(priv, M_TEMP);
if (error)
ipoib_warn(parent,
"failed to initialize subinterface: device %s, port %d vtag 0x%X",
parent->ca->name, parent->port, vtag);
return;
}
static void
ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
{
struct ipoib_dev_priv *parent;
struct ipoib_dev_priv *priv;
struct ifnet *dev;
uint16_t pkey;
if (ifp->if_type != IFT_INFINIBAND)
return;
dev = VLAN_DEVAT(ifp, vtag);
if (dev)
VLAN_SETCOOKIE(dev, NULL);
pkey = vtag | 0x8000;
parent = ifp->if_softc;
mutex_lock(&parent->vlan_mutex);
list_for_each_entry(priv, &parent->child_intfs, list) {
if (priv->pkey == pkey) {
ipoib_dev_cleanup(priv);
list_del(&priv->list);
break;
}
}
mutex_unlock(&parent->vlan_mutex);
}
eventhandler_tag ipoib_vlan_attach;
eventhandler_tag ipoib_vlan_detach;
static int __init
ipoib_init_module(void)
{
int ret;
ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
IPOIB_MIN_QUEUE_SIZE));
#ifdef CONFIG_INFINIBAND_IPOIB_CM
ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
#endif
ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
/*
* We create our own workqueue mainly because we want to be
* able to flush it when devices are being removed. We can't
* use schedule_work()/flush_scheduled_work() because both
* unregister_netdev() and linkwatch_event take the rtnl lock,
* so flush_scheduled_work() can deadlock during device
* removal.
*/
ipoib_workqueue = create_singlethread_workqueue("ipoib");
if (!ipoib_workqueue) {
ret = -ENOMEM;
goto err_fs;
}
ib_sa_register_client(&ipoib_sa_client);
ret = ib_register_client(&ipoib_client);
if (ret)
goto err_sa;
return 0;
err_sa:
ib_sa_unregister_client(&ipoib_sa_client);
destroy_workqueue(ipoib_workqueue);
err_fs:
return ret;
}
static void __exit
ipoib_cleanup_module(void)
{
EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
ib_unregister_client(&ipoib_client);
ib_sa_unregister_client(&ipoib_sa_client);
destroy_workqueue(ipoib_workqueue);
}
/*
* Infiniband output routine.
*/
static int
ipoib_output(struct ifnet *ifp, struct mbuf *m,
const struct sockaddr *dst, struct route *ro)
{
u_char edst[INFINIBAND_ALEN];
struct llentry *lle = NULL;
struct rtentry *rt0 = NULL;
struct ipoib_header *eh;
int error = 0;
short type;
if (ro != NULL) {
if (!(m->m_flags & (M_BCAST | M_MCAST)))
lle = ro->ro_lle;
rt0 = ro->ro_rt;
}
#ifdef MAC
error = mac_ifnet_check_transmit(ifp, m);
if (error)
goto bad;
#endif
M_PROFILE(m);
if (ifp->if_flags & IFF_MONITOR) {
error = ENETDOWN;
goto bad;
}
if (!((ifp->if_flags & IFF_UP) &&
(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
error = ENETDOWN;
goto bad;
}
switch (dst->sa_family) {
#ifdef INET
case AF_INET:
if (lle != NULL && (lle->la_flags & LLE_VALID))
memcpy(edst, &lle->ll_addr.mac8, sizeof(edst));
else if (m->m_flags & M_MCAST)
ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
else
error = arpresolve(ifp, rt0, m, dst, edst, &lle);
if (error)
return (error == EWOULDBLOCK ? 0 : error);
type = htons(ETHERTYPE_IP);
break;
case AF_ARP:
{
struct arphdr *ah;
ah = mtod(m, struct arphdr *);
ah->ar_hrd = htons(ARPHRD_INFINIBAND);
switch(ntohs(ah->ar_op)) {
case ARPOP_REVREQUEST:
case ARPOP_REVREPLY:
type = htons(ETHERTYPE_REVARP);
break;
case ARPOP_REQUEST:
case ARPOP_REPLY:
default:
type = htons(ETHERTYPE_ARP);
break;
}
if (m->m_flags & M_BCAST)
bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
else
bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
}
break;
#endif
#ifdef INET6
case AF_INET6:
if (lle != NULL && (lle->la_flags & LLE_VALID))
memcpy(edst, &lle->ll_addr.mac8, sizeof(edst));
else if (m->m_flags & M_MCAST)
ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
else
error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
if (error)
return error;
type = htons(ETHERTYPE_IPV6);
break;
#endif
default:
if_printf(ifp, "can't handle af%d\n", dst->sa_family);
error = EAFNOSUPPORT;
goto bad;
}
/*
* Add local net header. If no space in first mbuf,
* allocate another.
*/
M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
if (m == NULL) {
error = ENOBUFS;
goto bad;
}
eh = mtod(m, struct ipoib_header *);
(void)memcpy(&eh->proto, &type, sizeof(eh->proto));
(void)memcpy(&eh->hwaddr, edst, sizeof (edst));
/*
* Queue message on interface, update output statistics if
* successful, and start output if interface not yet active.
*/
return ((ifp->if_transmit)(ifp, m));
bad:
if (m != NULL)
m_freem(m);
return (error);
}
/*
* Upper layer processing for a received Infiniband packet.
*/
void
ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
{
int isr;
#ifdef MAC
/*
* Tag the mbuf with an appropriate MAC label before any other
* consumers can get to it.
*/
mac_ifnet_create_mbuf(ifp, m);
#endif
/* Allow monitor mode to claim this frame, after stats are updated. */
if (ifp->if_flags & IFF_MONITOR) {
if_printf(ifp, "discard frame at IFF_MONITOR\n");
m_freem(m);
return;
}
/*
* Dispatch frame to upper layer.
*/
switch (proto) {
#ifdef INET
case ETHERTYPE_IP:
isr = NETISR_IP;
break;
case ETHERTYPE_ARP:
if (ifp->if_flags & IFF_NOARP) {
/* Discard packet if ARP is disabled on interface */
m_freem(m);
return;
}
isr = NETISR_ARP;
break;
#endif
#ifdef INET6
case ETHERTYPE_IPV6:
isr = NETISR_IPV6;
break;
#endif
default:
goto discard;
}
netisr_dispatch(isr, m);
return;
discard:
m_freem(m);
}
/*
* Process a received Infiniband packet.
*/
static void
ipoib_input(struct ifnet *ifp, struct mbuf *m)
{
struct ipoib_header *eh;
if ((ifp->if_flags & IFF_UP) == 0) {
m_freem(m);
return;
}
CURVNET_SET_QUIET(ifp->if_vnet);
/* Let BPF have it before we strip the header. */
IPOIB_MTAP(ifp, m);
eh = mtod(m, struct ipoib_header *);
/*
* Reset layer specific mbuf flags to avoid confusing upper layers.
* Strip off Infiniband header.
*/
m->m_flags &= ~M_VLANTAG;
m_clrprotoflags(m);
m_adj(m, IPOIB_HEADER_LEN);
if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
ifp->if_addrlen) == 0)
m->m_flags |= M_BCAST;
else
m->m_flags |= M_MCAST;
ifp->if_imcasts++;
}
ipoib_demux(ifp, m, ntohs(eh->proto));
CURVNET_RESTORE();
}
static int
ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
struct sockaddr *sa)
{
struct sockaddr_dl *sdl;
#ifdef INET
struct sockaddr_in *sin;
#endif
#ifdef INET6
struct sockaddr_in6 *sin6;
#endif
u_char *e_addr;
switch(sa->sa_family) {
case AF_LINK:
/*
* No mapping needed. Just check that it's a valid MC address.
*/
sdl = (struct sockaddr_dl *)sa;
e_addr = LLADDR(sdl);
if (!IPOIB_IS_MULTICAST(e_addr))
return EADDRNOTAVAIL;
*llsa = 0;
return 0;
#ifdef INET
case AF_INET:
sin = (struct sockaddr_in *)sa;
if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
return EADDRNOTAVAIL;
sdl = malloc(sizeof *sdl, M_IFMADDR,
M_NOWAIT|M_ZERO);
if (sdl == NULL)
return ENOMEM;
sdl->sdl_len = sizeof *sdl;
sdl->sdl_family = AF_LINK;
sdl->sdl_index = ifp->if_index;
sdl->sdl_type = IFT_INFINIBAND;
sdl->sdl_alen = INFINIBAND_ALEN;
e_addr = LLADDR(sdl);
ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
e_addr);
*llsa = (struct sockaddr *)sdl;
return 0;
#endif
#ifdef INET6
case AF_INET6:
sin6 = (struct sockaddr_in6 *)sa;
/*
* An IP6 address of 0 means listen to all
* of the multicast address used for IP6.
* This has no meaning in ipoib.
*/
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
return EADDRNOTAVAIL;
if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
return EADDRNOTAVAIL;
sdl = malloc(sizeof *sdl, M_IFMADDR,
M_NOWAIT|M_ZERO);
if (sdl == NULL)
return (ENOMEM);
sdl->sdl_len = sizeof *sdl;
sdl->sdl_family = AF_LINK;
sdl->sdl_index = ifp->if_index;
sdl->sdl_type = IFT_INFINIBAND;
sdl->sdl_alen = INFINIBAND_ALEN;
e_addr = LLADDR(sdl);
ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
*llsa = (struct sockaddr *)sdl;
return 0;
#endif
default:
return EAFNOSUPPORT;
}
}
module_init(ipoib_init_module);
module_exit(ipoib_cleanup_module);
|