Codebase list ibutils / 7ff97930-aa68-4caf-aa62-7d9a855df24e/main ibdm / ibdm / ibdm.i
7ff97930-aa68-4caf-aa62-7d9a855df24e/main

Tree @7ff97930-aa68-4caf-aa62-7d9a855df24e/main (Download .tar.gz)

ibdm.i @7ff97930-aa68-4caf-aa62-7d9a855df24e/mainraw · history · blame

   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
/*
 * Copyright (c) 2004-2010 Mellanox Technologies LTD. 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.
 *
 */


/*
 * IB Fabric Data Model (and Utilities)
 *
 * Data Model Interface File for TCL SWIG
 *
 */

%title "IB Fabric Data Model - TCL Extention"

%module ibdm
%{
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sstream>
#include "Fabric.h"
#include "SubnMgt.h"
#include "CredLoops.h"
#include "TraceRoute.h"
#include "TopoMatch.h"
#include "Congestion.h"

# if __WORDSIZE == 64
#  define __PRI64_PREFIX   "l"
#  define __PRIPTR_PREFIX  "l"
# else
#  define __PRI64_PREFIX   "ll"
#  define __PRIPTR_PREFIX
# endif
#ifndef PRIx64
# define PRIx64         __PRI64_PREFIX "x"
#endif
%}

%{
  /* GLOBALS */
  static char ibdm_tcl_error_msg[1024];
  static int  ibdm_tcl_error;
  static vector< IBFabric *> ibdm_fabrics;
  static IBLinkWidth UnknownLinkWidth = IB_UNKNOWN_LINK_WIDTH;
  static IBLinkSpeed UnknownLinkSpeed = IB_UNKNOWN_LINK_SPEED;
  static IBLinkWidth DefaultLinkWidth = IB_LINK_WIDTH_4X;
  static IBLinkSpeed DefaultLinkSpeed = IB_LINK_SPEED_2_5;

  /*
	  MAPPING IBDM OBJECTS TO TCL and BACK:
	  The idea is that we have specifc rules for naming
	  Node, Port, System and SystemPort for a specific Fabric.

	  All Fabrics are stored by id in a global vector.

	  So the object names will follow:
	  <type>:<fabricIdx>/<name>

  */

  /* Given a fabric pointer return its idx (starting w 1) or 0 */
  int ibdmGetFabricIdxByPtr(IBFabric *p_fabric) {
	 /* go over all fabrics and find it's index: */
	 for (unsigned int i = 0; i < ibdm_fabrics.size(); i++) {
		if (ibdm_fabrics[i] == p_fabric) {
		  return(i+1);
		}
	 }
	 return(0);
  }

  /* Given a fabric idx return it's pointer */
  /* Note the index is 1-N and thus we need to -1 it before access */
  IBFabric *ibdmGetFabricPtrByIdx(unsigned int idx) {
	 if ((idx > ibdm_fabrics.size()) || (idx < 1)) {
		return NULL;
	 }
	 return ibdm_fabrics[idx - 1];
  }

  /*
	 we provide our own constructor such that all IBFabrics are
	 registered in the global vector;
  */
  IBFabric *new_IBFabric(void) {
	 IBFabric *p_fabric = new IBFabric();
    unsigned int i;
	 if (p_fabric) {
      /* look for an open index in the vector of fabrics */
      for (i = 0; i < ibdm_fabrics.size(); i++)
      {
        if (ibdm_fabrics[i] == NULL)
        {
          ibdm_fabrics[i] = p_fabric;
          return p_fabric;
        }
      }
      ibdm_fabrics.push_back(p_fabric);
	 }
	 return p_fabric;
  }

  /*
	 we provide our own destructor such that the deleted fabric is
    de-registered from the global fabrics vector
  */
  void delete_IBFabric(IBFabric *p_fabric) {
    int idx = ibdmGetFabricIdxByPtr(p_fabric);
    if (! idx) {
      printf("ERROR: Fabric idx:%p does not exist in the global vector!\n",
             p_fabric);
    } else {
      ibdm_fabrics[idx-1] = NULL;
    }
    delete p_fabric;
  }

  /* Given the Object Pointer and Type provide it's TCL name */
  int ibdmGetObjTclNameByPtr(Tcl_Obj *objPtr, void *ptr, char *type) {
	 char tclName[128];
	 char name[128];
	 IBFabric *p_fabric;
	 string uiType;

	 if (!strcmp(type, "IBNode *")) {
		IBNode *p_node = (IBNode *)ptr;
		p_fabric = p_node->p_fabric;
		sprintf(name, ":%s", p_node->name.c_str());
		uiType = "node";
	 } else if (!strcmp(type, "IBPort *")) {
		IBPort *p_port = (IBPort *)ptr;
		sprintf(name,":%s/%u", p_port->p_node->name.c_str(), p_port->num);
		p_fabric = p_port->p_node->p_fabric;
		uiType = "port";
	 } else if (!strcmp(type, "IBSystem *")) {
		IBSystem *p_system = (IBSystem *)ptr;
		sprintf(name, ":%s", p_system->name.c_str());
		uiType = "system";
		p_fabric = p_system->p_fabric;
	 } else if (!strcmp(type, "IBSysPort *")) {
		IBSysPort *p_sysPort = (IBSysPort *)ptr;
		sprintf(name, ":%s:%s",  p_sysPort->p_system->name.c_str(),
				  p_sysPort->name.c_str());
		uiType = "sysport";
		p_fabric = p_sysPort->p_system->p_fabric;
	 } else if (!strcmp(type, "IBFabric *")) {
		p_fabric = (IBFabric *)ptr;
		uiType = "fabric";
		name[0] = '\0';
	 } else {
		sprintf(tclName, "-E- Unrecognized Object Type:%s", type);
		Tcl_SetStringObj(objPtr, tclName, -1);
		return TCL_ERROR;
	 }

	 /* get the fabric index */
	 int idx = ibdmGetFabricIdxByPtr(p_fabric);
	 if (idx == 0) {
		Tcl_SetStringObj(objPtr, "-E- Fail to find fabric by ptr", -1);
		return TCL_ERROR;
	 }

	 sprintf(tclName, "%s:%u%s", uiType.c_str(), idx, name);
	 Tcl_SetStringObj(objPtr, tclName, -1);
	 return TCL_OK;
  }

  /* Given the Object TCL Name Get it's pointer */
  int ibdmGetObjPtrByTclName(Tcl_Obj *objPtr, void **ptr) {
	 /* we need to parse the name and get the type etc. */
	 char buf[256];
	 char *type, *name=0, *fabIdxStr;
	 char *colonIdx, *slashIdx;
	 int fabricIdx;
	 *ptr = NULL;

	 strcpy(buf, Tcl_GetStringFromObj(objPtr,0));

	 /* the format is always: <type>:<idx>[:<name>] */

	 /* first separate the type */
	 colonIdx = index(buf,':');
	 if (!colonIdx) {
		printf("-E- Bad formatted (no :) ibdm object:%s\n", buf);
		return TCL_ERROR;
	 }
	 *colonIdx = '\0';

	 type = buf;
	 fabIdxStr = ++colonIdx;

	 /* now separate the fabric section if tyep is not fabric */
	 if (strcmp(type, "fabric")) {
		slashIdx = index(fabIdxStr,':');
		if (!slashIdx) {
		  printf( "-E- Bad formatted ibdm fabric object:%s\n",
					 Tcl_GetStringFromObj(objPtr,0));
		  return TCL_ERROR;
		}
		*slashIdx = '\0';
		name = ++slashIdx;
	 }

	 /* Ok so now get the fabic pointer */
	 fabricIdx = atoi(fabIdxStr);

	 IBFabric *p_fabric = ibdmGetFabricPtrByIdx(fabricIdx);
	 if (! p_fabric) {
		*ptr = NULL;
		return TCL_ERROR;
	 }

	 if (!strcmp(type, "fabric")) {
		*ptr = p_fabric;
	 } else if (!strcmp(type, "node")) {
		IBNode *p_node = p_fabric->getNode(string(name));
		if (! p_node) {
		  printf("-E- Fail to get node:%s\n", name);
		  return TCL_ERROR;
		}
		*ptr = p_node;
	 } else if (!strcmp(type, "port")) {
		slashIdx = rindex(name,'/');
		if (!slashIdx) {
		  printf("-E- Bad formatted ibdm node object:%s\n",
					Tcl_GetStringFromObj(objPtr,0));
		  return TCL_ERROR;
		}
		*slashIdx = '\0';
		int portNum = atoi(++slashIdx);
		IBNode *p_node = p_fabric->getNode(string(name));
		if (! p_node) {
		  printf("-E- Fail to get node:%s\n", name);
		  return TCL_ERROR;
		}
		IBPort *p_port = p_node->getPort(portNum);
		if (! p_port) {
		  printf("-E- Fail to get node:%s port:%u\n",
					 name, portNum);
		  return TCL_ERROR;
		}
		*ptr = p_port;
	 } else if (!strcmp(type, "system")) {
		IBSystem *p_system = p_fabric->getSystem(string(name));
		if (! p_system) {
		  printf("-E- Fail to get system:%s\n", name);
		  return TCL_ERROR;
		}
		*ptr = p_system;
	 } else if (!strcmp(type, "sysport")) {
		/* the format of system port is:  <type>:<idx>:<sys>:<port> */
		colonIdx = index(name,':');
		if (!colonIdx) {
		  printf("-E- Bad formatted ibdm sysport object:%s\n",
					Tcl_GetStringFromObj(objPtr,0) );
		  return TCL_ERROR;
		}
		*colonIdx = '\0';
		IBSystem *p_system = p_fabric->getSystem(string(name));
		if (! p_system) {
		  printf("-E- Fail to get system:%s\n", name);
		  return TCL_ERROR;
		}
		IBSysPort *p_sysPort = p_system->getSysPort(string(++colonIdx));
		if (! p_sysPort) {
		  printf("-E- Fail to get system:%s port:%s\n", name, colonIdx);
		  return TCL_ERROR;
		}
		*ptr = p_sysPort;
	 } else {
		printf("-E- Unrecognized Object Type:%s\n", type);
		return TCL_ERROR;
	 }
	 return TCL_OK;
  }

  int ibdmReportNonUpDownCa2CaPaths(IBFabric *p_fabric, list_pnode rootNodes) {
    map_pnode_int nodesRank;
    if (SubnRankFabricNodesByRootNodes(p_fabric, rootNodes, nodesRank))
    {
      printf("-E- fail to rank the fabric by the given root nodes.\n");
      return(1);
    }
    return( SubnReportNonUpDownCa2CaPaths(p_fabric, nodesRank));
  }

  int ibdmFatTreeRoute(IBFabric *p_fabric, list_pnode rootNodes) {
    map_pnode_int nodesRank;
    if (SubnRankFabricNodesByRootNodes(p_fabric, rootNodes, nodesRank))
    {
      printf("-E- fail to rank the fabric by the given root nodes.\n");
      return(1);
    }
    return( SubnMgtFatTreeRoute(p_fabric));
  }

  int ibdmCheckFabricMCGrpsForCreditLoopPotential(IBFabric *p_fabric, list_pnode rootNodes) {
    map_pnode_int nodesRank;
    if (SubnRankFabricNodesByRootNodes(p_fabric, rootNodes, nodesRank))
    {
      printf("-E- fail to rank the fabric by the given root nodes.\n");
      return(1);
    }
    return( SubnMgtCheckFabricMCGrpsForCreditLoopPotential(p_fabric, nodesRank));
  }

  int ibdmRankFabricByRoots(IBFabric *p_fabric, list_pnode rootNodes) {
    map_pnode_int nodesRank;
    if (SubnRankFabricNodesByRootNodes(p_fabric, rootNodes, nodesRank))
    {
      printf("-E- fail to rank the fabric by the given root nodes.\n");
      return(1);
    }
    return(0);
  }

%}


//
// exception handling wrapper based on the MsgMgr interfaces
//

// it assumes we do not send the messages to stderr
%except(tcl8) {
  ibdm_tcl_error = 0;
  $function;
  if (ibdm_tcl_error) {
	 Tcl_SetStringObj(Tcl_GetObjResult(interp), ibdm_tcl_error_msg, -1);
	 return TCL_ERROR;
  }
}

//
// TYPE MAPS:
//
%include typemaps.i

// Convert a TCL Object to C++ world.
%typemap(tcl8,in) IBFabric *, IBNode *, IBSystem *, IBPort *, IBSysPort * {

  void *ptr;
  if (ibdmGetObjPtrByTclName($source, &ptr) != TCL_OK) {
	 char err[128];
	 sprintf(err, "-E- fail to find ibdm obj by id:%s",Tcl_GetString($source) );
	 // Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
  }

  $target = ($type)ptr;
}

// Convert C++ pointer to TCL
%typemap(tcl8,out) IBFabric *, IBNode *, IBSystem *, IBPort *, IBSysPort * {
  if ($source)
	 ibdmGetObjTclNameByPtr($target, $source, "$type");
}

%typemap(tcl8,check)  IBFabric *, IBNode *, IBSystem *, IBPort *, IBSysPort * {
  /* the format is always: <type>:<idx>[:<name>] */

  // get the type from the given source
  char buf[128];
  strcpy(buf, Tcl_GetStringFromObj($source,0));
  char *colonIdx = index(buf,':');
  if (!colonIdx) {
	 char err[128];
	 sprintf(err, "-E- Bad formatted ibdm object:%s", buf);
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
  }
  *colonIdx = '\0';

  if (!strcmp("$basetype", "IBFabric ")) {
	if (strcmp(buf, "fabric")) {
	 char err[256];
	 sprintf(err, "-E- basetype is $basetype but received obj of type %s", buf);
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
	}
  } else if (!strcmp("$basetype", "IBSystem ")) {
	if (strcmp(buf, "system")) {
	 char err[256];
	 sprintf(err, "-E- basetype is $basetype but received obj of type %s", buf);
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
	}
  } else if (!strcmp("$basetype", "IBSysPort ")) {
	if (strcmp(buf, "sysport")) {
	 char err[256];
	 sprintf(err, "-E- basetype is $basetype but received obj of type %s", buf);
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
	}
  } else if (!strcmp("$basetype", "IBNode ")) {
	if (strcmp(buf, "node")) {
	 char err[256];
	 sprintf(err, "-E- basetype is $basetype but received obj of type %s", buf);
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
	}
  } else if (!strcmp("$basetype", "IBPort ")) {
	if (strcmp(buf, "port")) {
	 char err[256];
	 sprintf(err, "-E- basetype is $basetype but received obj of type %s", buf);
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
	}
  } else {
	 char err[256];
	 sprintf(err, "-E- basetype '$basetype' is unknown");
	 Tcl_SetStringObj(tcl_result, err, strlen(err));
	 return TCL_ERROR;
  }
}

// representing vec_pport in TCL
%typemap(tcl8, out) vec_pport * {
  Tcl_Obj *p_tclObj;

  for (unsigned int i = 0; i < $source->size(); i++) {
	 IBPort *p_port = (*$source)[i];
	 if (p_port) {
		p_tclObj = Tcl_NewObj();
		if (ibdmGetObjTclNameByPtr(p_tclObj, p_port, "IBPort *")
			 != TCL_OK) {
		  printf("-E- Fail to map Port Object (a Vector element)\n");
		} else {
		  Tcl_AppendElement(interp, Tcl_GetString(p_tclObj));
		}
		Tcl_DecrRefCount(p_tclObj);
	 }
  }
}

%typemap(tcl8, out) vec_vec_byte * {
  for (unsigned int i = 0; i < $source->size(); i++) {
	 Tcl_AppendResult(interp,"{", NULL);
	 for (unsigned int j = 0; j < (*$source)[i].size(); j++) {
		char buf[32];
		sprintf(buf,"%u ", (*$source)[i][j]);
		Tcl_AppendResult(interp, buf, NULL);
	 }
	 Tcl_AppendResult(interp,"} ", NULL);
  }
}

%typemap(tcl8, out) vec_byte * {
  for (unsigned int i = 0; i < $source->size(); i++) {
	 char buf[32];
	 sprintf(buf,"%u ", (*$source)[i]);
	 Tcl_AppendResult(interp, buf, NULL);
  }
}

// representing map_str_psysport in TCL
%typemap(tcl8, out) map_str_psysport * {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  map_str_psysport::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I).second, "IBSysPort *") != TCL_OK) {
		printf("-E- Fail to map SysPort Object (a Vector element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "%s %s", (*I).first.c_str(), Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// representing map_str_psys in TCL
%typemap(tcl8, out) map_str_psys * {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  map_str_psys::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I).second, "IBSystem *") != TCL_OK) {
		printf("-E- Fail to map System Object (a Vector element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "%s %s", (*I).first.c_str(), Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// representing map_str_pnode in TCL
%typemap(tcl8, out) map_str_pnode * {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  map_str_pnode::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I).second, "IBNode *") != TCL_OK) {
		printf("-E- Fail to map Node Object (a Vector element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "%s %s", (*I).first.c_str(), Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// representing map_guid_pport in TCL
%typemap(tcl8, out) map_guid_pport * {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  map_guid_pport::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I).second, "IBPort *") != TCL_OK) {
		printf("-E- Fail to map Port Object (a guid map element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "0x%016" PRIx64 " %s",
              (*I).first, Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// representing map_guid_pnode in TCL
%typemap(tcl8, out) map_guid_pnode * {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  map_guid_pnode::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I).second, "IBNode *") != TCL_OK) {
		printf("-E- Fail to map Node Object (a guid map element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "0x%016" PRIx64 " %s",
              (*I).first, Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// representing map_guid_psystem in TCL
%typemap(tcl8, out) map_guid_psys * {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  map_guid_psys::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I).second, "IBSystem *") != TCL_OK) {
		printf("-E- Fail to map System Object (a guid map element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "0x%016" PRIx64 " %s",
              (*I).first, Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// cast a Tcl String object to string
%typemap(tcl8,in) string * {
  int len;
  static string $target_tmp;
  $target_tmp = string(Tcl_GetStringFromObj($source,&len));
  $target = &$target_tmp;
}

// NOTE FOR SOME REASON THE RETURN TYPE IS TAKEN NOT AS A POINTER
// BUT PASSED AS ONE ?????

// return a char * from a string:
%typemap(tcl8,out) string, string * {
	char ezTmp[1024];
	strcpy(ezTmp, $source->c_str());
	Tcl_SetStringObj(tcl_result, ezTmp, strlen(ezTmp));
}
%{
#define new_string string
%}
// return a char * from a string that was allocated previously
// so we need to delete it
%typemap(tcl8,out) new_string, new_string * {
	char ezTmp[1024];
	strcpy(ezTmp, $source->c_str());
	Tcl_SetStringObj(tcl_result, ezTmp, strlen(ezTmp));
   delete $source;
}

%typemap (tcl8, ignore) char **p_out_str (char *p_c) {
  $target = &p_c;
}

%typemap (tcl8, argout) char **p_out_str {
  if (*$source) {
     Tcl_SetStringObj($target,*$source,strlen(*$source));
      free(*$source);
  } else {
     Tcl_SetStringObj($target,"",-1);
  }
}

/*
  TCL Type Maps for Standard Integer Types
*/

%typemap(tcl8,in) uint64_t *(uint64_t temp) {
  temp = strtoull(Tcl_GetStringFromObj($source,NULL), NULL,16);
  $target = &temp;
}

%typemap(tcl8,out) uint64_t {
  char buff[20];
  sprintf(buff, "0x%016" PRIx64, *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,out) uint64_t * {
  char buff[20];
  sprintf(buff, "0x%016" PRIx64, *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%{
#define new_uint64_t uint64_t
%}
%typemap(tcl8,out) new_uint64_t *, new_uint64_t {
  char buff[20];
  /* new_uint64_t tcl8 out */
  sprintf(buff, "0x%016" PRIx64, *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
  delete $source;
}

%typemap(tcl8,in) uint32_t * (uint32_t temp){
  temp = strtoul(Tcl_GetStringFromObj($source,NULL), NULL, 0);
  $target = &temp;
}

%typemap(tcl8,out) uint32_t * {
  char buff[20];
  sprintf(buff, "%u", *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,in) uint16_t * (uint16_t temp) {
  temp = strtoul(Tcl_GetStringFromObj($source,NULL), NULL, 0);
  $target = &temp;
}

%typemap(tcl8,out) uint16_t * {
  char buff[20];
  sprintf(buff, "%u", *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,in) uint8_t * (uint8_t temp) {
  temp = strtoul(Tcl_GetStringFromObj($source,NULL), NULL, 0);
  $target = &temp;
}

%typemap(tcl8,out) uint8_t * {
  char buff[20];
  sprintf(buff, "%u", *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,in) ib_net64_t *(uint64_t temp) {
  temp = cl_hton64(strtoull(Tcl_GetStringFromObj($source,NULL), NULL, 16));
  $target = &temp;
}

%typemap(tcl8,out) ib_net64_t * {
  char buff[20];
  sprintf(buff, "0x%016" PRIx64, cl_ntoh64(*$source));
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,in) ib_net32_t *(ib_net32_t temp) {
  temp = cl_hton32(strtoul(Tcl_GetStringFromObj($source,NULL), NULL, 0));
  $target = &temp;
}

%typemap(tcl8,out) ib_net32_t * {
  char buff[20];
  sprintf(buff, "%u", cl_ntoh32(*$source));
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,in) ib_net16_t * (ib_net16_t temp) {
  temp = cl_hton16(strtoul(Tcl_GetStringFromObj($source,NULL), NULL, 0));
  $target = &temp;
}

%typemap(tcl8,out) ib_net16_t * {
  char buff[20];
  sprintf(buff, "%u", cl_hton16(*$source));
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,argout) uint64_t *OUTPUT {
  char buff[20];
  sprintf(buff, "0x%016" PRIx64, *$source);
  Tcl_SetStringObj($target,buff,strlen(buff));
}

%typemap(tcl8,ignore) uint64_t *OUTPUT(uint64_t temp) {
  $target = &temp;
}


%typemap(tcl8,argout) ostringstream & {
  Tcl_SetStringObj($target, (char*)$source->str().c_str(),
                   $source->str().size() + 1);
}

%typemap(tcl8,ignore) ostringstream &(ostringstream tempStream) {
  $target = &tempStream;
}

%typemap(tcl8,out) boolean_t * {
  if (*$source) {
	 Tcl_SetStringObj($target,"TRUE", 4);
  } else {
	 Tcl_SetStringObj($target,"FALSE", 5);
  }
}

%typemap(tcl8,in) boolean_t *(boolean_t temp) {
  if (strcmp(Tcl_GetStringFromObj($source,NULL), "TRUE")) {
	 temp = FALSE;
  } else {
	 temp = TRUE;
  }
  $target = &temp;
}

%typemap(tcl8,out) IBLinkWidth * {
  Tcl_SetStringObj($target,width2char(*$source), -1);
}

%typemap(tcl8,in) IBLinkWidth * (IBLinkWidth temp1) {
  temp1 = char2width(Tcl_GetStringFromObj($source,NULL));
  $target = &temp1;
}

%typemap(tcl8,out) IBLinkSpeed * {
  Tcl_SetStringObj($target,speed2char(*$source), -1);
}

%typemap(tcl8,in) IBLinkSpeed * (IBLinkSpeed temp2) {
  temp2 = char2speed(Tcl_GetStringFromObj($source,NULL));
  $target = &temp2;
}
%include typemaps.i

%typemap(tcl8,out) list_pnode *,  list_pnode {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  list_pnode::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;

  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I), "IBNode *") != TCL_OK) {
		printf("-E- Fail to map Node Object (a guid map element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "%s", Tcl_GetString(p_tclObj));
		Tcl_AppendElement(interp, buf);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}

// given a list of node names - generate the list of nodes
%typemap(tcl8,in) list_pnode * (list_pnode tmpNodeList) {
#if TCL_MINOR_VERSION > 3
  const char **sub_lists;
#else
  char **sub_lists;
#endif
  int num_sub_lists;
  int idx;

  /* we will use the TCL split list to split into elements */
  if (Tcl_SplitList(interp,
                    Tcl_GetStringFromObj($source,0),
                    &num_sub_lists, &sub_lists) != TCL_OK) {
    printf("-E- Bad formatted list :%s\n",
           Tcl_GetStringFromObj($source,0));
    return TCL_ERROR;
  }

  for (idx = 0; (idx < num_sub_lists); idx++)
  {
    /* we need to double copy since TCL 8.4 requires split res to be const */
    Tcl_Obj *p_tclObj;
    void *ptr;
    char buf[128];
    strcpy(buf, sub_lists[idx]);

    if (strncmp("node:", buf, 5)) {
      printf("-E- Bad formatted node (%u) object:%s\n", idx, buf);
      return TCL_ERROR;
    }

	 p_tclObj = Tcl_NewObj();
    Tcl_SetStringObj(p_tclObj, buf, -1);
    if (ibdmGetObjPtrByTclName(p_tclObj, &ptr) != TCL_OK) {
      printf("-E- fail to find ibdm obj by id:%s", buf );
      Tcl_DecrRefCount(p_tclObj);
      return TCL_ERROR;
    }
    Tcl_DecrRefCount(p_tclObj);
    tmpNodeList.push_back((IBNode *)ptr);
  }

  $target = &tmpNodeList;
}

//
// INTERFACE DEFINITION (~copy of h file)
//

%section "IBDM Constants"
/* These constants are provided by IBDM: */

typedef enum {IB_UNKNOWN_NODE_TYPE, IB_CA_NODE, IB_SW_NODE} IBNodeType;
/* Node Types */

%subsection "Log Verbosity Flags",before,pre
/* To be or'ed and used as the value of FabricUtilsVerboseLevel */
%readonly
#define FABU_LOG_NONE    0x0
#define FABU_LOG_ERROR   0x1
#define FABU_LOG_INFO    0x2
#define FABU_LOG_VERBOSE 0x4
%readwrite

%section "IBDM Globals"
int FabricUtilsVerboseLevel;
/* Log level: set to FABU_LOG* values  */

int ibdmUseInternalLog();
/* instruct ibdm to use intrernal buffer for log */
int ibdmUseCoutLog();
/* use stdout for log */
%new char *ibdmGetAndClearInternalLog();
/* obtain log messages from internal log and clear it */

%section "IBDM Objects",pre
/* This section decribes the various object types exposed by IBDM. */
%text %{

  IBDM exposes some of its internal objects. The objects
  identifiers returned by the various function calls are formatted
  according to the following rules:
  Fabric: fabric:<idx>
  System: system:<fab idx>:<sys name>
  SysPort: sysport:<fab idx>:<sys name>:<port name>
  Node: node:<fab idx>:<node name>
  Port: port:<fab idx>:<node name>/<port num>

  IBDM Objects are standard Swig-Tcl objects.
  As such they have two flavors for their usage: Variables, Objects.

  Variables/Pointers:
     For each object attribute a "get" and "set" methods are provided.
	  The format of the methods is: <class>_<attribute>_<get|set>.
     The "set" method is only available for read/write attributes.

	  Example:
     set nodes [ibdm_get_nodes]
     set node  [lindex $nodes 0]
     IBNode_numPorts_get $node

  Objects:
     Given an object pointer one can convert it to a Tcl "Object"
	  using the following command:
     <class> <obj_name> -this <obj pointer>

     Once declared the <obj-name> can be used in conjunction to
     with the standard "configure" and "cget" commands.

	  Example (following the previous one):
     IBFabric VaTech -this $fabric
	  VaTech cget -NodeByName

     To delete an object symbol (and enable its mapping to another
     pointer) use:
     rename <obj name> ""
     for example:
     rename VaTech ""
%}

//
// IB Port class.
// This is not the "End Node" but the physical port of
// a node.
//
class IBPort {
 public:
  IBPort    * p_remotePort; // Port connected on the other side of link
  IBSysPort * p_sysPort;    // The system port (if any) connected to
  IBNode    * p_node;       // The node the port is part of.
  int			      num;            // Physical ports are identified by number.
  unsigned int	   base_lid;       // The base lid assigned to the port.
  IBLinkWidth     width;          // The link width of the port
  IBLinkSpeed     speed;          // The link speed of the port
  unsigned int    counter1;       // a generic value to be used by various algorithms

  IBPort(IBNode *p_nodePtr, int number);
  // constructor

  new_uint64_t guid_get();
  void guid_set(uint64_t guid);

  new_string getName();

  void connect (IBPort *p_otherPort,
                IBLinkWidth w = DefaultLinkWidth,
                IBLinkSpeed s = DefaultLinkSpeed);
  // connect the port to another node port

  int disconnect();
  // disconnect the port. Return 0 if successful

};

//
// IB Node class
//
class IBNode {
 public:
  string			   name;      // Name of the node (instance name of the chip)
  IBNodeType		type;      // Either a CA or SW
  uint32_t        devId;     // The device ID of the node
  uint32_t        revId;     // The device revision Id.
  uint32_t        vendId;    // The device Vendor ID.
  string          attributes;// Comma-sep string of arbitrary attributes k=v
  uint8_t         rank;      // The rank of the node in the tree
%readonly
  IBSystem	     *p_system; // What system we belong to
  IBFabric	     *p_fabric; // What fabric we belong to.
  unsigned int	   numPorts;  // Number of physical ports
  vec_pport		   Ports;     // Vector of all the ports
  vec_vec_byte		MinHopsTable; // Table describing minimal hop count through
                                // each port to each target lid
  vec_byte        LFT;          // The LFT of this node (for switches only)
%readwrite
  // void            *p_appData1;  // Application Private Data #1
  // void            *p_appData2;  // Application Private Data #2

  new_uint64_t guid_get();
  void guid_set(uint64_t guid);

  IBNode(string n,
			IBFabric *p_fab,
			IBSystem *p_sys,
			IBNodeType t, int np);
  // Constractor

  ~IBNode();

  inline IBPort *makePort (unsigned int num);
  // create a new port by name if required to

  inline IBPort *getPort(unsigned int num);
  // get a port by number num = 1..N:

  void setHops (IBPort *p_port, unsigned int lid, int hops);
  // Set the min hop for the given port (* is all) lid pair

  int getHops (IBPort *p_port, unsigned int lid);
  // Get the min number of hops defined for the given port or all

  IBPort *getFirstMinHopPort(unsigned int lid);
  // Scan the node ports and find the first port
  // with min hop to the lid

  void setLFTPortForLid (unsigned int lid, unsigned int portNum);
  // Set the Linear Forwarding Table:

  int getLFTPortForLid (unsigned int lid);
  // Get the LFT for a given lid

  void repHopTable();
  // dump out the min hop table of the node

}; // Class IBNode

//
// System Port Class
// The System Port is a front pannel entity.
//
class IBSysPort {
 public:
  string			   name;              // The front pannel name of the port
  IBSysPort	*p_remoteSysPort;  // If connected the other side sys port
  IBSystem	*p_system;         // System it benongs to
  IBPort	   *p_nodePort;       // The node port it connects to.

  IBSysPort(string n, IBSystem *p_sys);
  // Constructor

  ~IBSysPort();

  void connect (IBSysPort *p_otherSysPort,
                IBLinkWidth width = UnknownLinkWidth,
                IBLinkSpeed speed = UnknownLinkSpeed);
  // connect two SysPorts

  int disconnect();
  // disconnect the SysPort (and ports). Return 0 if successful

};

//
// IB System Class
// This is normally derived into a system specific class
//
class IBSystem {
 public:
  string			   name;       // the "host" name of the system
  string			   type;       // what is the type i.e. Cougar, Buffalo etc
  IBFabric        *p_fabric;  // fabric belongs to
%readonly
  map_str_pnode NodeByName;   // Provide the node pointer by its name
  map_str_psysport PortByName;// A map provising pointer to the SysPort by name
%readwrite

  IBSystem(string n, IBFabric *p_fab, string t);
  // Constractor

  ~IBSystem();

  new_uint64_t guid_get();
  void guid_set(uint64_t guid);

  IBSysPort *makeSysPort (string pName);
  // make sure we got the port defined (so define it if not)

  IBPort *getSysPortNodePortByName (string sysPortName);
  // get the node port for the given sys port by name

  IBSysPort *getSysPort(string name);
  // Get a Sys Port by name
};

//
// IB Fabric Class
// The entire fabric
//
class IBFabric {
 public:
%readonly
  map_str_pnode NodeByName;   // Provide the node pointer by its name
  map_str_psys  SystemByName; // Provide the system pointer by its name
  vec_pport     PortByLid;    // Pointer to the Port by its lid
  map_guid_pnode NodeByGuid;   // Provides the node by guid
  map_guid_psys  SystemByGuid; // Provides the system by guid
  map_guid_pport PortByGuid;   // Provides the port by guid
%readwrite
  unsigned int  minLid;       // Track min lid used.
  unsigned int  maxLid;       // Track max lid used.
  unsigned int  lmc;          // LMC value used

  // IBFabric() {maxLid = 0;};

  // we need to have our own destructor to take care of the
  // fabrics vector cleanup

  // ~IBFabric();

  IBNode *makeNode (string n,
						  IBSystem *p_sys,
						  IBNodeType type,
						  unsigned int numPorts);
  // get the node by its name (create one of does not exist)

  IBNode *getNode (string name);
  // get the node by its name

  list_pnode *getNodesByType (IBNodeType type);
  // return the list of node pointers matching the required type

  IBSystem *makeGenericSystem (string name);
  // crate a new generic system - basically an empty contaner for nodes...

  IBSystem *makeSystem (string name, string type);
  // crate a new system - the type must have a registed factory.

  IBSystem *getSystem(string name);
  // Get system by name

  IBSystem *getSystemByGuid(uint64_t guid);
  // get the system by its guid
  IBNode *getNodeByGuid(uint64_t guid);
  // get the node by its guid
  IBPort *getPortByGuid(uint64_t guid);
  // get the port by its guid

  void addCable (string t1, string n1, string p1,
					  string t2, string n2, string p2,
                 IBLinkWidth width = DefaultLinkWidth,
                 IBLinkSpeed speed = DefaultLinkSpeed
                 );

  // Add a cable connection

  int parseCables (string fn);
  // Parse the cables file and build the fabric

  int parseTopology (string fn);
  // Parse Topology File

  int addLink(string type1, int numPorts1, uint64_t sysGuid1,
				  uint64_t nodeGuid1,  uint64_t portGuid1,
				  int vend1, int devId1, int rev1, string desc1,
				  int hcaIdx1, int lid1, int portNum1,
				  string type2, int numPorts2, uint64_t sysGuid2,
				  uint64_t nodeGuid2,  uint64_t portGuid2,
				  int vend2, int devId2, int rev2, string desc2,
				  int hcaIdx2, int lid2, int portNum2,
              IBLinkWidth width = DefaultLinkWidth,
              IBLinkSpeed speed = DefaultLinkSpeed);
  // Add a link into the fabric - this will create system
  // and nodes as required.

  int parseSubnetLinks (string fn);
  // Parse the OpenSM subnet.lst file and build the fabric from it.

  int parseFdbFile(string fn);
  // Parse OpenSM FDB dump file

  int parseMCFdbFile(string fn);
  // Parse an OpenSM MCFDBs file and set the MFT table accordingly

  int parsePSLFile(string fn);
  // Parse Path to SL mapping file

  int parseSLVLFile(string fn);
  // Parse SLVL tables file

  inline void setLidPort (unsigned int lid, IBPort *p_port);
  // set a lid port

  inline IBPort *getPortByLid (unsigned int lid);
  // get a port by lid

  int dumpTopology(const char *fileName, const char *ibnlDir);
  // write out a topology file and IBNLs into given directory

  int dumpNameMap(const char *fileName);
  // write out node names map file (guid, lid and name for each node)

  int setNodeGuidsByNameMapFile(const char *fileName);
  // parse a name to guid and LID map file and update node GUIDs

};

/* we use our own version of the constructor */
IBFabric*  new_IBFabric();
/* we use our own version of the destructor */
void delete_IBFabric(IBFabric *p_fabric);

%section "IBDM Functions",pre
/* IBDM functions */
%text %{
This section provide the details about the functions IBDM exposes.
The order follows the expected order in a regular IBDM flow.
They all return 0 on succes.
%}

%subsection "Subnet Utilities",before,pre
%text %{

  Subnet Utilities:

  The file holds a set of utilities to be run on the subnet to mimic OpenSM
  initialization and analyze the results:

  Assign Lids: SubnMgtAssignLids
  Init min hop tables: SubnMgtCalcMinHopTables
  Perform Enhanced LMC aware routing: SubnMgtOsmEnhancedRoute
  Perform standard routing: SubnMgtOsmRoute
  Perform Fat Tree specialized routing: SubnMgtFatTreeRoute
  Verify all CA to CA routes: SubnMgtVerifyAllCaToCaRoutes

%}

%name(ibdmAssignLids)
 int SubnMgtAssignLids (IBPort *p_smNodePort, unsigned int lmc = 0);
// Assign lids

%name(ibdmCalcMinHopTables)
 int SubnMgtCalcMinHopTables (IBFabric *p_fabric);
// Calculate the minhop table for the switches

%name(ibdmCalcUpDnMinHopTbls)
 int SubnMgtCalcUpDnMinHopTblsByRootNodesRex(IBFabric *p_fabric, const char *rootNodesNameRex);
// Fill in the FDB tables in a Up Down routing.
// Start the tree from the given nodes by regular expression

%name (ibdmOsmRoute)
 int SubnMgtOsmRoute(IBFabric *p_fabric);
// Fill in the FDB tables in an OpesnSM style routing
// which is switch based, uses number of routes per port
// profiling and treat LMC assigned lids sequentialy
// Rely on running the SubnMgtCalcMinHopTables beforehand

%name(ibdmEnhancedRoute)
 int SubnMgtOsmEnhancedRoute(IBFabric *p_fabric);
// Fill in the FDB tables in an OpesnSM style routing
// which is switch based, uses number of routes per port
// profiling and treat LMC assigned lids sequentialy.
// Also it will favor runing through a new system or node
// on top of the port profile.
// Rely on running the SubnMgtCalcMinHopTables beforehand

int ibdmFatTreeRoute(IBFabric *p_fabric, list_pnode rootNodes);
// Perform Fat Tree specific routing by assigning a single LID to
// each root node port a single LID to route through.

%name(ibdmFatTreeAnalysis) int FatTreeAnalysis(IBFabric *p_fabric);
// Performs FatTree structural analysis

%name(ibdmFatTreeRouteByPermutation) int FatTreeRouteByPermutation(IBFabric *p_fabric, const char* srcs, const char* dsts);
// Performs optimal permutation routing in FatTree

%name(ibdmVerifyCAtoCARoutes)
 int SubnMgtVerifyAllCaToCaRoutes(IBFabric *p_fabric);
// Verify point to point connectivity

%name(ibdmVerifyAllPaths)
 int SubnMgtVerifyAllRoutes(IBFabric *p_fabric);
// Verify all paths

%name(ibdmAnalyzeLoops)
 int CrdLoopAnalyze(IBFabric *p_fabric);
// Analyze the Fabric for Credit Loops

%name(ibdmSetCreditLoopAnalysisMode)
int CredLoopMode(int include_switch_to_switch_paths, int include_multicast);
// Set the analysis mode of ibdmAnalyzeLoops

%name(ibdmFindSymmetricalTreeRoots)
list_pnode SubnMgtFindTreeRootNodes(IBFabric *p_fabric);
// Analyze the fabric to find its root nodes assuming it is
// a pure tree (keeping all levels in place).

%name(ibdmFindRootNodesByMinHop)
list_pnode SubnMgtFindRootNodesByMinHop(IBFabric *p_fabric);
// Analyze the fabric to find its root nodes using statistical methods
// on the profiles of min hops to CAs

int ibdmRankFabricByRoots(IBFabric *p_fabric, list_pnode rootNodes);
// Just rank the fabric according to the given nodes list

int ibdmReportNonUpDownCa2CaPaths(IBFabric *p_fabric, list_pnode rootNodes);
// Find any routes that exist in the FDB's from CA to CA and do not adhare to
// the up/down rules. Report any crossing of the path. Use the given list fo nodes
// as roots of the tree.

%name(ibdmReportCA2CAPathsThroughSWPort)
int SubnReportCA2CAPathsThroughSWPort(IBPort *p_port);
// Report all CA 2 CA Paths giong through the given port

%name(ibdmCheckMulticastGroups)
int SubnMgtCheckFabricMCGrps(IBFabric *p_fabric);
// Check all multicast groups :
// 1. all switches holding it are connected
// 2. No loops (i.e. a single BFS with no returns).

int ibdmCheckFabricMCGrpsForCreditLoopPotential(
  IBFabric *p_fabric, list_pnode rootNodes);
// Check all multicast groups do not have credit loop potential

%name(ibdmLinkCoverageAnalysis)
int LinkCoverageAnalysis(IBFabric *p_fabric, list_pnode rootNodes);
// Provide sets of port pairs to run BW check from in a way that is
// full bandwidth. Reide in LinkCover.cpp

%subsection "Tracing Utilities",before,pre

%name(ibdmTraceDRPathRoute)
int TraceDRPathRoute (IBPort *p_smNodePort, list_int drPathPortNums);
// Trace a direct route from the given SM node port

%name(ibdmTraceRouteByMinHops)
int TraceRouteByMinHops (IBFabric *p_fabric,
  unsigned int slid , unsigned int dlid);
// Trace a route from slid to dlid by Min Hop

%typemap(tcl8,in) list_pnode_arg_name*(list_pnode tmp) {
	$target = &tmp;
}

%typemap(tcl8,argout) list_pnode_arg_name* {
  // build a TCL list out of the Objec ID's of the ibdm objects in it.
  list_pnode::const_iterator I = $source->begin();
  Tcl_Obj *p_tclObj;
  Tcl_SetVar(interp, Tcl_GetString($arg),"",0);
  while (I != $source->end()) {
	 p_tclObj = Tcl_NewObj();
	 if (ibdmGetObjTclNameByPtr(p_tclObj, (*I), "IBNode *") != TCL_OK) {
		printf("-E- Fail to map Node Object (a guid map element)\n");
	 } else {
		char buf[128];
		sprintf(buf, "%s", Tcl_GetString(p_tclObj));
		Tcl_SetVar(interp, Tcl_GetString($arg), buf,
					  TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
	 }
	 Tcl_DecrRefCount(p_tclObj);
	 I++;
  }
}
%{
#define list_pnode_arg_name list_pnode
%}

%typemap(tcl8,in) unsigned_int_arg_name*(unsigned int tmp) {
	$target = &tmp;
}

%typemap(tcl8,argout) unsigned_int_arg_name* {
   char buf[16];
	sprintf(buf, "%u", tmp);
   Tcl_SetVar(interp, Tcl_GetString($arg), buf, 0);
}
%{
#define unsigned_int_arg_name unsigned int
%}

%name(ibdmTraceRouteByLFT)
int TraceRouteByLFT (IBFabric *p_fabric,
							unsigned int slid , unsigned int dlid,
							unsigned_int_arg_name *hops,
							list_pnode_arg_name *p_nodesList);
// Trace a route from slid to dlid by LFT

%subsection "Topology Matching Utilities",before,pre

%apply char **p_out_str {char **p_report_str};

%name(ibdmMatchFabrics)
int TopoMatchFabrics(
  IBFabric *p_spec_fabric,       // The specification fabric
  IBFabric *p_discovered_fabric, // The discovered fabric
  char     *anchorNodeName,      // The system to be the anchor point
  int       anchorPortNum,       // The port number of the anchor port
  uint64_t  anchorPortGuid,      // Discovered Guid of the anchor port
  char **p_report_str            // Diagnostic output.
  );
// Error if fabrics deffer. And provide it as result.

%name(ibdmBuildMergedFabric)
int
TopoMergeDiscAndSpecFabrics(
  IBFabric  *p_spec_fabric,       // The specification fabric
  IBFabric  *p_discovered_fabric, // The discovered fabric
  IBFabric  *p_merged_fabric);    // Output merged fabric (allocated internaly)
// Build a merged fabric from a matched discovered and spec fabrics.
// NOTE: you have to run ibdmMatchFabrics before calling this routine.

%name(ibdmMatchFabricsFromEdge)
int
TopoMatchFabricsFromEdge(
  IBFabric *p_sFabric,            // The specification fabric
  IBFabric *p_dFabric,            // The discovered fabric
  char **p_report_str             // Diagnostic output.
  );
// Match the fabrics going from the hosts inwards. Report mismatches.

%subsection "Congestion Analysis Utilities",before,pre

%name(ibdmCongInit) int CongInit(IBFabric *p_fabric);
// Initialize a fabric for congestion analysis

%name(ibdmCongCleanup) int CongCleanup(IBFabric *p_fabric);
// Cleanup congestion analysis data and free memory

%name(ibdmCongClear) int CongZero(IBFabric *p_fabric);
// Clear the congestion analysis path trace. Does not affect max paths

%name(ibdmCongTrace)
int CongTrackPath(IBFabric *p_fabric, uint16_t srcLid, uint16_t dstLid);
// Trace the path from source to destination tracking the visited links

%name(ibdmCongReport) int CongReport(IBFabric *p_fabric, ostringstream &out);
// Report the max path count and histogram

%name(ibdmCongDump) int CongDump(IBFabric *p_fabric, ostringstream &out);
// provide detailed dump of the link usage

//
// FIX OF SWIG TO SUPPORT NAME ALTERNATE MANGLING
//
%{
#include "swig_alternate_mangling.cpp"
%}

///////////////////////////////////////////////////////////////////////////////
extern char * ibdmSourceVersion;

//
// INIT CODE
//
%init %{

  /* mixing declarations .... */
  {
	 // Register the objects for alternate mangling
    SWIG_AlternateObjMangling["_IBFabric_p"] = &ibdmGetObjTclNameByPtr;
    SWIG_AlternateNameToObj  ["_IBFabric_p"] = &ibdmGetObjPtrByTclName;

    SWIG_AlternateObjMangling["_IBSystem_p"] = &ibdmGetObjTclNameByPtr;
    SWIG_AlternateNameToObj  ["_IBSystem_p"] = &ibdmGetObjPtrByTclName;

    SWIG_AlternateObjMangling["_IBSysPort_p"] = &ibdmGetObjTclNameByPtr;
    SWIG_AlternateNameToObj  ["_IBSysPort_p"] = &ibdmGetObjPtrByTclName;

    SWIG_AlternateObjMangling["_IBNode_p"] = &ibdmGetObjTclNameByPtr;
    SWIG_AlternateNameToObj  ["_IBNode_p"] = &ibdmGetObjPtrByTclName;

    SWIG_AlternateObjMangling["_IBPort_p"] = &ibdmGetObjTclNameByPtr;
    SWIG_AlternateNameToObj  ["_IBPort_p"] = &ibdmGetObjPtrByTclName;
  }

%}