~ubuntu-branches/ubuntu/raring/ibutils/raring-proposed

« back to all changes in this revision

Viewing changes to ibmgtsim/src/sim.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Benoit Mortier
  • Date: 2010-01-11 22:22:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100111222200-53kum2et5nh13rv3
Tags: upstream-1.2-OFED-1.4.2
ImportĀ upstreamĀ versionĀ 1.2-OFED-1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004 Mellanox Technologies LTD. All rights reserved.
 
3
 *
 
4
 * This software is available to you under a choice of one of two
 
5
 * licenses.  You may choose to be licensed under the terms of the GNU
 
6
 * General Public License (GPL) Version 2, available from the file
 
7
 * COPYING in the main directory of this source tree, or the
 
8
 * OpenIB.org BSD license below:
 
9
 *
 
10
 *     Redistribution and use in source and binary forms, with or
 
11
 *     without modification, are permitted provided that the following
 
12
 *     conditions are met:
 
13
 *
 
14
 *      - Redistributions of source code must retain the above
 
15
 *        copyright notice, this list of conditions and the following
 
16
 *        disclaimer.
 
17
 *
 
18
 *      - Redistributions in binary form must reproduce the above
 
19
 *        copyright notice, this list of conditions and the following
 
20
 *        disclaimer in the documentation and/or other materials
 
21
 *        provided with the distribution.
 
22
 *
 
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
24
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
25
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
26
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 
27
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
28
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
29
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
30
 * SOFTWARE.
 
31
 *
 
32
 * $Id$
 
33
 */
 
34
 
 
35
#include "git_version.h"
 
36
#include "sim.h"
 
37
#include "msgmgr.h"
 
38
#include "sma.h"
 
39
#include "vsa.h"
 
40
#include "pma.h"
 
41
#include <getopt.h>
 
42
#include <inttypes.h>
 
43
 
 
44
//////////////////////////////////////////////////////////////
 
45
//
 
46
// CLASS  IBMgtSim
 
47
//
 
48
 
 
49
char *IBMgtSim::getSimulatorDir()
 
50
{
 
51
  static char *ibmgtSimDir = NULL;
 
52
  static char *defaultIbmgtSimDir = "/tmp/ibmgtsim";
 
53
 
 
54
  if (ibmgtSimDir == NULL)
 
55
  {
 
56
    ibmgtSimDir = getenv("IBMGTSIM_DIR");
 
57
    if (ibmgtSimDir == NULL) {
 
58
      printf("-W- Environment variable: IBMGTSIM_DIR does not exist.\n");
 
59
      printf("    Please create one used by the simulator.\n");
 
60
      printf("    Using /tmp/ibmgtsim as default.\n");
 
61
      ibmgtSimDir = defaultIbmgtSimDir;
 
62
    }
 
63
  }
 
64
  return ibmgtSimDir;
 
65
}
 
66
 
 
67
/* allocate guids to the nodes */
 
68
int
 
69
IBMgtSim::allocateFabricNodeGuids()
 
70
{
 
71
  FILE* dumpFile;
 
72
 
 
73
  MSGREG(errMsg1, 'F', "Fail to open guid dump file:$", "server");
 
74
  string dumpFileName(getSimulatorDir());
 
75
  dumpFileName += "/ibmgtsim.guids.txt";
 
76
  dumpFile = fopen(dumpFileName.c_str(), "w");
 
77
  if (! dumpFile)
 
78
  {
 
79
    MSGSND(errMsg1, dumpFileName);
 
80
    exit(1);
 
81
  }
 
82
 
 
83
  MSGREG(msg1, 'I', "Assigning Guids ...", "server");
 
84
  MSGSND(msg1);
 
85
 
 
86
  uint64_t curGuid = 0x0002c90000000000ULL;
 
87
 
 
88
  /* simply go over all nodes and allocate guids */
 
89
  for (map_str_pnode::iterator nI = pFabric->NodeByName.begin();
 
90
       nI != pFabric->NodeByName.end();
 
91
       nI++)
 
92
  {
 
93
    IBNode *pNode = (*nI).second;
 
94
    pNode->guid_set(++curGuid);
 
95
    fprintf(dumpFile, "NODE   %s 0x%016" PRIx64 "\n",
 
96
            pNode->name.c_str(), pNode->guid_get());
 
97
 
 
98
    /* go over all ports of the node and assign guid */
 
99
    for (unsigned int pn = 1; pn <= pNode->numPorts; pn++)
 
100
    {
 
101
      IBPort *pPort = pNode->getPort(pn);
 
102
      if (! pPort) continue;
 
103
 
 
104
      if (pNode->type == IB_SW_NODE)
 
105
        pPort->guid_set(curGuid);
 
106
      else
 
107
      {
 
108
        pPort->guid_set(++curGuid);
 
109
        fprintf(dumpFile, "PORT   %s 0x%016" PRIx64 "\n",
 
110
                pPort->getName().c_str(), pPort->guid_get());
 
111
      }
 
112
 
 
113
    }
 
114
 
 
115
    /* assign system guids by first one */
 
116
    IBSystem *pSystem = pNode->p_system;
 
117
    if (pSystem && (pSystem->guid_get() == 0))
 
118
    {
 
119
      pSystem->guid_set(++curGuid);
 
120
      fprintf(dumpFile, "SYSTEM %s 0x%016" PRIx64 "\n",
 
121
              pSystem->name.c_str(), pSystem->guid_get());
 
122
    }
 
123
 
 
124
  } // all nodes
 
125
  fclose(dumpFile);
 
126
 
 
127
  return 0;
 
128
}
 
129
 
 
130
/* initialize simulator nodes */
 
131
int IBMgtSim::populateFabricNodes()
 
132
{
 
133
 
 
134
  MSGREG(msg1, 'I', "Populating Fabric Nodes ...", "server");
 
135
  MSGSND(msg1);
 
136
 
 
137
  /* simply go over all nodes and instantiate the sim node */
 
138
  for (map_str_pnode::iterator nI = pFabric->NodeByName.begin();
 
139
       nI != pFabric->NodeByName.end();
 
140
       nI++)
 
141
  {
 
142
    IBNode *pNode = (*nI).second;
 
143
    IBMSNode *pSimNode = new IBMSNode(this, pNode);
 
144
    ibmsSetIBNodeSimNode(pNode, pSimNode);
 
145
 
 
146
    list_uint16 smClassList;
 
147
    smClassList.push_back(0x1);
 
148
    smClassList.push_back(0x81);
 
149
    IBMSSma *pSma = new IBMSSma(pSimNode, smClassList);
 
150
    if (! pSma)
 
151
    {
 
152
      MSGREG(err1, 'F', "Fail to allocate SMA for Node:$", "server");
 
153
      MSGSND(err1, pNode->name);
 
154
      exit(1);
 
155
    }
 
156
 
 
157
    list_uint16 vsClassList;
 
158
    vsClassList.push_back(0x9);
 
159
    vsClassList.push_back(0x10);
 
160
    IBMSVendorSpecific *pVsa = new IBMSVendorSpecific(pSimNode, vsClassList);
 
161
    if (! pVsa)
 
162
    {
 
163
      MSGREG(err2, 'F', "Fail to allocate VSA for Node:$", "server");
 
164
      MSGSND(err2, pNode->name);
 
165
      exit(1);
 
166
    }
 
167
 
 
168
    IBMSPma *pPma = new IBMSPma(pSimNode, 0x04);
 
169
    if (! pPma)
 
170
    {
 
171
        MSGREG(err3, 'F', "Fail to allocate PMA for Node:$", "server");
 
172
        MSGSND(err3, pNode->name);
 
173
        exit(1);
 
174
    }
 
175
  }
 
176
 
 
177
  return 0;
 
178
}
 
179
 
 
180
/* Initialize the fabric server and dispatcher */
 
181
int IBMgtSim::init(string topoFileName, int serverPortNum, int numWorkers)
 
182
{
 
183
  /* parse the given fabric */
 
184
  pFabric = new IBFabric;
 
185
 
 
186
  if (pFabric->parseTopology(topoFileName)) {
 
187
    cout << "-E- Fail to parse topology file:" << topoFileName << endl;
 
188
    exit(1);
 
189
  }
 
190
 
 
191
  /* allocate guids to the nodes */
 
192
  allocateFabricNodeGuids();
 
193
 
 
194
  /* initialize simulator nodes */
 
195
  populateFabricNodes();
 
196
 
 
197
  /* try to generate the server on the given port or next ones... */
 
198
  int trys = 0;
 
199
  do {
 
200
    pServer = new IBMSServer(this, serverPortNum++);
 
201
 
 
202
    if (pServer->isAlive())
 
203
      break;
 
204
    else
 
205
      delete pServer;
 
206
 
 
207
  } while (trys++ < 10);
 
208
 
 
209
  if (!pServer->isAlive())
 
210
    return 1;
 
211
 
 
212
  pDispatcher = new IBMSDispatcher(numWorkers, 50, 10);
 
213
 
 
214
  return 0;
 
215
}
 
216
 
 
217
//////////////////////////////////////////////////////////////
 
218
//
 
219
static char IBMgtSimUsage[] =
 
220
"Usage: ibmgtsim [-vh] -t <topology file> [-p <server port>][-w <num workers>]";
 
221
 
 
222
void
 
223
show_usage() {
 
224
  cout << IBMgtSimUsage << endl;
 
225
}
 
226
 
 
227
void
 
228
show_help() {
 
229
  cout << "HELP" << endl;
 
230
}
 
231
 
 
232
#ifndef IBMGTSIM_CODE_VERSION
 
233
#define IBMGTSIM_CODE_VERSION "undefined"
 
234
#endif
 
235
const char * ibmsSourceVersion = IBMGTSIM_CODE_VERSION ;
 
236
 
 
237
//////////////////////////////////////////////////////////////
 
238
//
 
239
// MAIN
 
240
//
 
241
 
 
242
#ifdef BUILD_STANDALONE_SIM
 
243
int main(int argc, char **argv)
 
244
{
 
245
  /*
 
246
   * Parsing of Command Line
 
247
   */
 
248
 
 
249
  string TopoFile = string("");
 
250
  int numWorkers = 5;
 
251
  int serverPortNum = 42561;
 
252
  int verbosity = MsgDefault;
 
253
 
 
254
  char next_option;
 
255
  const char * const short_option = "vht:w:p:";
 
256
  /*
 
257
         In the array below, the 2nd parameter specified the number
 
258
         of arguments as follows:
 
259
         0: no arguments
 
260
         1: argument
 
261
         2: optional
 
262
  */
 
263
  const option long_option[] =
 
264
         {
 
265
                {       "verbose",           0, NULL,   'v'},
 
266
                {       "help",              0, NULL,   'h'},
 
267
                {       "port-num",          1, NULL,   'p'},
 
268
                {       "workers",       1,     NULL,   'w'},
 
269
                {       "topology",          1, NULL,   't'},
 
270
                {       NULL,           0,      NULL,    0 }    /* Required at the end of the array */
 
271
         };
 
272
 
 
273
  printf("-----------------------------------------------------\n\n");
 
274
  printf("    Mellanox Technologies IB Management Simulator\n");
 
275
  printf("   -----------------------------------------------\n\n");
 
276
 
 
277
  do
 
278
  {
 
279
         next_option = getopt_long(argc, argv, short_option, long_option, NULL);
 
280
         switch(next_option)
 
281
         {
 
282
         case 'v':
 
283
                /*
 
284
                  Verbose Mode
 
285
                */
 
286
      verbosity = MsgShowAll;
 
287
                printf(" Verbose Mode\n");
 
288
                break;
 
289
 
 
290
         case 'p':
 
291
                /*
 
292
                  Specific Server Port
 
293
                */
 
294
                serverPortNum = atoi(optarg);
 
295
                printf(" Using Port:%u\n", serverPortNum);
 
296
                break;
 
297
 
 
298
         case 'w':
 
299
                /*
 
300
                  Specifies number of worker threads
 
301
                */
 
302
                numWorkers = atoi(optarg);
 
303
      printf(" Initializing %u mad dispatcher threads.\n", numWorkers);
 
304
                break;
 
305
 
 
306
         case 't':
 
307
                /*
 
308
                  Specifies Subnet Cabling file
 
309
                */
 
310
      TopoFile = string(optarg);
 
311
      printf(" Using topology file:%s.\n", TopoFile.c_str());
 
312
                break;
 
313
 
 
314
         case 'h':
 
315
                show_help();
 
316
                return 0;
 
317
                break;
 
318
 
 
319
         case -1:
 
320
                break; /* done with option */
 
321
         default: /* something wrong */
 
322
                show_usage();
 
323
                exit(1);
 
324
         }
 
325
  }
 
326
  while(next_option != -1);
 
327
 
 
328
  if (!TopoFile.size()) {
 
329
         printf("-E- Missing some mandatory arguments.\n");
 
330
         show_usage();
 
331
         exit(1);
 
332
  }
 
333
 
 
334
  /* initialize the logger */
 
335
  msgMgr(verbosity, &cout);
 
336
 
 
337
  IBMgtSim sim;
 
338
 
 
339
  sim.init(TopoFile, serverPortNum, numWorkers);
 
340
 
 
341
  while (1)
 
342
    sleep(1000);
 
343
 
 
344
  return 0;
 
345
}
 
346
 
 
347
#endif