flash_faults.c 17.7 KB
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
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */


/****************************************************
 *
 * fault injection support for flash memory system
 *
 * Author:  John Chapin   2/13/96
 * Revised: Dan Teodosiu  09/09/96
 *
 ****************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

#include "sim_error.h"
#include "tcl_init.h"
#include "cpu_interface.h"

#ifdef USE_FLASHLITE

#include "mipsy_interface.h"

/**** Tcl dispatch table *****/

/* Router tcl interface:
 *
 * router connections k         # return connections for this router, as
 *                              # { { router port } M ... }
 * router queuelen k p l        # return queue length at port on given lane
 * router packet k p l n        # return packet n on lane l for port p of k
 * router drop k p l n          # drop specified packet
 * router insert k p l xport PKT_rep_as_returned_by_router_packet
 * router severlink k p bhole   # sever specified link (black-hole)
 * router blockNode k           # block specified node
 * router powerfail k           # powerfail router
 * router reroute (top|bottom|left|right) # redo routing w/o using bad link
 *
 * router watch k on|off        # raise annotation when packet enqueued
 *
 * annotation set router k {    # set annotation on router k
 *    whatever
 * }
 */

/*
 * magic route nodenum destnum nextport	# backdoor program MAGIC routing table bin
 */


static int cmdConnections(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdQueuelen(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdPacket(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdDrop(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdInsertPKT(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdInsertVP(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdSeverlink(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdBlockNode(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdPowerfail(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdWatch(Tcl_Interp *interp, int argc, char *argv[]);
static int cmdReroute(Tcl_Interp *interp, int argc, char *argv[]);

static tclcmd routerCmds[] = {
  {"connections",3, cmdConnections," connections routerNum"},
  {"queuelen",   5, cmdQueuelen,   " queuelen routerNum portNum laneNum"},
  {"packet",     6, cmdPacket,     " packet routerNum portNum laneNum pktNum"},
  {"drop",       6, cmdDrop,       " drop routerNum portNum laneNum pktNum"},
  {"insertPKT",  43,cmdInsertPKT,  " insertPKT routerNum portNum lane <PKT>"},
  {"insertVP",   15,cmdInsertVP,   " insertVP routerNum portNum lane <VP>"},
  {"severlink",  5, cmdSeverlink,  " severlink routerNum portNum bhole"},
  {"blockNode",  3, cmdBlockNode,  " blockNode nodeNum"},
  {"powerfail",  3, cmdPowerfail,  " powerfail routerNum"},
  {"watch",      4, cmdWatch,      " watch routerNum on|off"},
  {"reroute",    3, cmdReroute,    " reroute top|bottom|left|right"},
  { NULL,        0, NULL,          NULL}
};

static int cmdMRoute(Tcl_Interp *interp, int argc, char *argv[]);

static tclcmd magicCmds[] = {
  {"route",      5, cmdMRoute,     " route nodenum destnum nextport"},
  { NULL,        0, NULL,          NULL}
};

int LinkSymbolToRetrieveFlashFaultInit;

void FlashFaultInit(Tcl_Interp* interp)
{
   Tcl_CreateCommand(interp,
                     "router",
                     DispatchCmd,
                     (ClientData)routerCmds,
                     NULL);
   Tcl_CreateCommand(interp,
                     "magic",
                     DispatchCmd,
                     (ClientData)magicCmds,
                     NULL);
}

static int 
cmdConnections(Tcl_Interp *interp, int argc, char *argv[])
{
   char* routerName = argv[2];
   int routerNum, portNum, result, pn, r, done, code;
   char buf[64];

   if ((code = Tcl_GetInt(interp, routerName, &routerNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad router num \"", routerName, "\"", NULL);
      return code;
   }

   for (portNum = 0, done = 0; !done; portNum++) {
     switch (QueryPort(routerNum, portNum, &result)) {
     case ROUTERTCL_NONE:
       /* not connected */
       Tcl_AppendElement(interp, "");
       break;
     case ROUTERTCL_ROUTER:
       /* connected to router: find out which port */
       for (pn = 0; ; pn++) {
	 if ((code = QueryPort(result, pn, &r)) == ROUTERTCL_ROUTER &&
	     r == routerNum)
	   break;
	 ASSERT(code == ROUTERTCL_NONE ||  /* port not connected */
		code == ROUTERTCL_MAGIC || /* MAGIC port */
		code == ROUTERTCL_ROUTER); /* port connected to other router */
       }
       sprintf(buf, "%d %d", result, pn);
       Tcl_AppendElement(interp, buf);
       break;
     case ROUTERTCL_MAGIC:
       /* connected to magic */
       Tcl_AppendElement(interp, "M");
       break;
     case ROUTERTCL_BADPORT:
       /* done all ports */
       done = 1;
       break;
     case ROUTERTCL_BADROUTER:
       Tcl_AppendResult(interp, "router ", routerName, " out of range", NULL);
       return TCL_ERROR;
     default:
       Tcl_AppendResult(interp, "unexpected return code from QueryPort", NULL);
       return TCL_ERROR;
     }
   }
   return TCL_OK;
}

static int 
cmdQueuelen(Tcl_Interp *interp, int argc, char *argv[])
{
   int routerNum, portNum, laneNum, code;
   char* routerName = argv[2];
   char* portName = argv[3];
   char* laneName = argv[4];
   char buf[20];
   int result;

   if ((code = Tcl_GetInt(interp, routerName, &routerNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad router num \"", routerName, "\"", NULL);
      return code;
   }

   if ((code = Tcl_GetInt(interp, portName, &portNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad port num \"", portName, "\"", NULL);
      return code;
   }

   if ((code = Tcl_GetInt(interp, laneName, &laneNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad lane num \"", laneName, "\"", NULL);
      return code;
   }

   switch (QueueLength(routerNum, portNum, laneNum, &result)) {
   case ROUTERTCL_OK:
      sprintf(buf, "%d", result);
      Tcl_AppendResult(interp, buf, NULL);
      return TCL_OK;
   case ROUTERTCL_BADPORT:
      Tcl_AppendResult(interp, "port num ", portName, " out of range", NULL);
      return TCL_ERROR;
   case ROUTERTCL_BADROUTER:
      Tcl_AppendResult(interp, "router num ", portName, " out of range", NULL);
      return TCL_ERROR;
   case ROUTERTCL_BADLANE:
      Tcl_AppendResult(interp, "lane num ", laneName, " out of range", NULL);
      return TCL_ERROR;
   default:
      Tcl_AppendResult(interp, "unexpected return code from QueueLength in router queuelen", NULL);
      return TCL_ERROR;
   }
}

static int 
cmdDrop(Tcl_Interp *interp, int argc, char *argv[])
{
   int routerNum, portNum, laneNum, packetNum, code;
   char* routerName = argv[2];
   char* portName = argv[3];
   char* laneName = argv[4];
   char* packetName = argv[5];

   if ((code = Tcl_GetInt(interp, routerName, &routerNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad router num \"", routerName, "\"", NULL);
      return code;
   }

   if ((code = Tcl_GetInt(interp, portName, &portNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad port num \"", portName, "\"", NULL);
      return code;
   }

   if ((code = Tcl_GetInt(interp, laneName, &laneNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad lane num \"", laneName, "\"", NULL);
      return code;
   }

   if ((code = Tcl_GetInt(interp, packetName, &packetNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad packet num \"", packetName, "\"", NULL);
      return code;
   }

   switch (DropPacket(routerNum, portNum, laneNum, packetNum)) {
   case ROUTERTCL_OK:
      return TCL_OK;
   case ROUTERTCL_BADPORT:
      Tcl_AppendResult(interp, "port num ", portName, " out of range", NULL);
      return TCL_ERROR;
   case ROUTERTCL_BADROUTER:
      Tcl_AppendResult(interp, "router num ", routerName, " out of range", NULL);
      return TCL_ERROR;
   case ROUTERTCL_BADLANE:
      Tcl_AppendResult(interp, "lane num ", laneName, " out of range", NULL);
      return TCL_ERROR;
   case ROUTERTCL_BADPACKET:
      Tcl_AppendResult(interp, "packet num ", packetName, " out of range", NULL);
      return TCL_ERROR;
   default:
      Tcl_AppendResult(interp, "unexpected return code from QueueLength in router queuelen", NULL);
      return TCL_ERROR;
   }
}

static int
get_arg(Tcl_Interp* interp, char* arg_string, int* arg_val, char* msg)
{
  int code;
  if ((code = Tcl_GetInt(interp, arg_string, arg_val)) != TCL_OK)
    Tcl_AppendResult(interp, msg, "\"", arg_string, "\"", NULL);
  return code;
}

static int 
cmdInsertPKT(Tcl_Interp *interp, int argc, char *argv[])
{
  int          rn=0, pn=0, ln=0;
  RouterPacket rp;
  int*         hp = (int*)&rp.h;
  int*         dp = (int*)rp.data;
  int*         ep = (int*)&rp.errbits;
  int          code, i;

  if ((code = get_arg(interp, argv[2], &rn,         "bad router")) != TCL_OK ||
      (code = get_arg(interp, argv[3], &pn,         "bad port"))   != TCL_OK ||
      (code = get_arg(interp, argv[4], &ln,         "bad lane"))   != TCL_OK ||

      (code = get_arg(interp, argv[6], hp+0,        "bad h0"))     != TCL_OK ||
      (code = get_arg(interp, argv[7], hp+1,        "bad h1"))     != TCL_OK ||
      (code = get_arg(interp, argv[8], hp+2,        "bad h2"))     != TCL_OK ||
      (code = get_arg(interp, argv[9], hp+3,        "bad h3"))     != TCL_OK ||
      (code = get_arg(interp, argv[10],ep,          "bad errbits"))!= TCL_OK)
    return code;
  if (strcmp(argv[5], "PKT") == 0) {
    rp.is_vp   = 0;
    rp.datalen = 16;
  } else {
    Tcl_AppendResult(interp, "bad packet type \"", argv[5], "\"", NULL);
    return TCL_ERROR;
  }
  for (i = 0; i < rp.datalen*sizeof(LL)/sizeof(unsigned); i++)
    if ((code = get_arg(interp, argv[11+i], dp+i,   "bad data")    != TCL_OK))
      return code;

  if (InsertPacket(rn, pn, ln, &rp) == 1) return TCL_OK;
  Tcl_AppendResult(interp, "cannot insert packet", NULL);
  return TCL_ERROR;
}

static int 
cmdInsertVP(Tcl_Interp *interp, int argc, char *argv[])
{
  int          rn=0, pn=0, ln=0;
  RouterPacket rp;
  int*         hp = (int*)&rp.h;
  int*         dp = (int*)rp.data;
  int*         ep = (int*)&rp.errbits;
  int          code, i;

  if ((code = get_arg(interp, argv[2], &rn,         "bad router")) != TCL_OK ||
      (code = get_arg(interp, argv[3], &pn,         "bad port"))   != TCL_OK ||
      (code = get_arg(interp, argv[4], &ln,         "bad lane"))   != TCL_OK ||

      (code = get_arg(interp, argv[6], hp+0,        "bad h0"))     != TCL_OK ||
      (code = get_arg(interp, argv[7], hp+1,        "bad h1"))     != TCL_OK ||
      (code = get_arg(interp, argv[8], hp+2,        "bad h2"))     != TCL_OK ||
      (code = get_arg(interp, argv[9], hp+3,        "bad h3"))     != TCL_OK ||
      (code = get_arg(interp, argv[10],ep,          "bad errbits"))!= TCL_OK)
    return code;
  if (strcmp(argv[5], "VP") == 0) {
    rp.is_vp   = 1;
    rp.datalen = 1;
  } else {
    Tcl_AppendResult(interp, "bad packet type \"", argv[5], "\"", NULL);
    return TCL_ERROR;
  }
  for (i = 0; i < (rp.datalen+1)*sizeof(LL)/sizeof(unsigned); i++)
    if ((code = get_arg(interp, argv[11+i], dp+i,   "bad data")    != TCL_OK))
      return code;

  if (InsertPacket(rn, pn, ln, &rp) == 1) return TCL_OK;
  Tcl_AppendResult(interp, "cannot insert packet", NULL);
  return TCL_ERROR;
}

static int
cmdSeverlink(Tcl_Interp *interp, int argc, char *argv[])
{
  char* routerName = argv[2];
  char* portName   = argv[3];
  char* bholeStr   = argv[4];
  int   routerNum, portNum, bhole;
  int   code;

  if ((code = Tcl_GetInt(interp, routerName, &routerNum)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad router num \"", routerName, "\"", NULL);
    return code;
  }
  if ((code = Tcl_GetInt(interp, portName, &portNum)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad port num \"", portName, "\"", NULL);
    return code;
  }
  if ((code = Tcl_GetInt(interp, bholeStr, &bhole)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad blackhole arg \"", bholeStr, "\"", NULL);
    return code;
  }

#ifndef SOLO
  if (SeverLink(routerNum, portNum, bhole) != 1) {
    Tcl_AppendResult(interp, "couldn't sever link", NULL);
    return TCL_ERROR;
  }
#endif
  return TCL_OK;
}

static int
cmdBlockNode(Tcl_Interp *interp, int argc, char *argv[])
{
  char* nodeName = argv[2];
  int   nodeNum;
  int   code;

  if ((code = Tcl_GetInt(interp, nodeName, &nodeNum)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad node num \"", nodeName, "\"", NULL);
    return code;
  }

  if (BlockNode(nodeNum) != 1) {
    Tcl_AppendResult(interp, "couldn't block node", NULL);
    return TCL_ERROR;
  }
  return TCL_OK;
}

static int 
cmdReroute(Tcl_Interp *interp, int argc, char *argv[])
{
   char *badLink = argv[2];
   if (!strcmp(badLink, "top")) {
      BadLinkReroute(LINK_TOP);
   } else if (!strcmp(badLink, "bottom")) {
      BadLinkReroute(LINK_BOTTOM);
   } else if (!strcmp(badLink, "left")) {
      BadLinkReroute(LINK_LEFT);
   } else if (!strcmp(badLink, "right")) {
      BadLinkReroute(LINK_RIGHT);
   } else {
      Tcl_AppendResult(interp, "link \"", badLink, "\"must be top|bottom|left|right", NULL);
      return TCL_ERROR;
   }
   return TCL_OK;
}

static int 
cmdPowerfail(Tcl_Interp *interp, int argc, char *argv[])
{
   Tcl_AppendResult(interp, "router powerfail unimplemented", NULL);
   return TCL_ERROR;
}

static int 
cmdPacket(Tcl_Interp *interp, int argc, char *argv[])
{
   char*        routerName = argv[2];
   char*        portName   = argv[3];
   char*        laneName   = argv[4];
   char*        packetName = argv[5];

   int          routerNum, portNum, laneNum, packetNum;
   RouterPacket rp;
   unsigned*    pp;
   int          i, code;
   static char  buf[512];
   unsigned     dlen;

   if ((code = Tcl_GetInt(interp, routerName, &routerNum)) != TCL_OK) {
     Tcl_AppendResult(interp, "bad router num \"", routerName, "\"", NULL);
     return code;
   }
   if ((code = Tcl_GetInt(interp, portName, &portNum)) != TCL_OK) {
     Tcl_AppendResult(interp, "bad port num \"", portName, "\"", NULL);
     return code;
   }
   if ((code = Tcl_GetInt(interp, laneName, &laneNum)) != TCL_OK) {
     Tcl_AppendResult(interp, "bad lane num \"", laneName, "\"", NULL);
     return code;
   }
   if ((code = Tcl_GetInt(interp, packetName, &packetNum)) != TCL_OK) {
     Tcl_AppendResult(interp, "bad packet num \"", packetName, "\"", NULL);
     return code;
   }

#ifndef SOLO
   /* get all the information on this packet */
   GetPacketInfo(routerNum, portNum, laneNum, packetNum, &rp);

   /* Shrink-wrap it, then off you go */
   Tcl_AppendElement(interp, rp.is_vp ? "VP" : "PKT");

   pp = (unsigned*)&rp.h; /* 2 dwords, that's 4 words */
   for (i = 0; i < sizeof(rp.h)/sizeof(unsigned); i++) {
     sprintf(buf, "0x%08x", *(pp+i));        Tcl_AppendElement(interp, buf);
   }
   {
     sprintf(buf, "0x%08x", rp.errbits);     Tcl_AppendElement(interp, buf);
   }
   pp = (unsigned*)rp.data;
   dlen = rp.datalen + (rp.is_vp ? 1 : 0);
   for (i = 0; i < dlen*sizeof(LL)/sizeof(unsigned); i++) {
     sprintf(buf, "0x%08x", *(pp+i));        Tcl_AppendElement(interp, buf);
   }
#endif

   return TCL_OK;
}

static int 
cmdWatch(Tcl_Interp *interp, int argc, char *argv[])
{
   int routerNum, val, code;
   char* routerName = argv[2];
   char* valName = argv[3];

   if ((code = Tcl_GetInt(interp, routerName, &routerNum)) != TCL_OK) {
      Tcl_AppendResult(interp, "bad router num \"", routerName, "\"", NULL);
      return code;
   }

   if (!strcmp(valName, "off")) {
      val = 0;
   } else if (!strcmp(valName, "on")) {
      val = 1;
   } else {
      Tcl_AppendResult(interp, "bad setting \"", valName, "\"", NULL);
      return TCL_ERROR;
   }
      
   switch (WatchRouter(routerNum, val)) {
   case ROUTERTCL_OK:
      return TCL_OK;
   case ROUTERTCL_BADROUTER:
      Tcl_AppendResult(interp, "router num ", routerName, " out of range", NULL);
      return TCL_ERROR;
   default:
      Tcl_AppendResult(interp, "unexpected return code from WatchRouter in router watch", NULL);
      return TCL_ERROR;
   }
}

/* note: the following is called when a new packet is received by
 * a router for which watch has been turned on. Lacking a better way
 * of passing arguments to the annotation body, this will first set
 * the following variables:
 *   router_ann_routerNum
 *   router_ann_portNum
 *   router_ann_lane
 */
void 
NotifyPacketArrived(int routerNum, int portNum, int lane)
{
  char buf[32];

  sprintf(buf, "%d", routerNum);
  Tcl_SetVar(TCLInterp, "router_ann_routerNum", buf, TCL_GLOBAL_ONLY);

  sprintf(buf, "%d", portNum);
  Tcl_SetVar(TCLInterp, "router_ann_portNum", buf, TCL_GLOBAL_ONLY);

  sprintf(buf, "%d",lane);
  Tcl_SetVar(TCLInterp, "router_ann_lane", buf, TCL_GLOBAL_ONLY);

  AnnExec(AnnFindInt("router", routerNum));
}

static int
cmdMRoute(Tcl_Interp *interp, int argc, char *argv[])
{
  char* nodeName = argv[2];
  int   nodeNum;
  char* destName = argv[3];
  int   destNum;
  char* portName = argv[4];
  int   portNum;
  int   code;

  if ((code = Tcl_GetInt(interp, nodeName, &nodeNum)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad node num \"", nodeName, "\"", NULL);
    return code;
  }

  if ((code = Tcl_GetInt(interp, destName, &destNum)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad dest num \"", destName, "\"", NULL);
    return code;
  }

  if ((code = Tcl_GetInt(interp, portName, &portNum)) != TCL_OK) {
    Tcl_AppendResult(interp, "bad port num \"", portName, "\"", NULL);
    return code;
  }

  if (SetMAGICRouteBin(nodeNum, destNum, portNum) != 1) {
    Tcl_AppendResult(interp, "couldn't set MAGIC route table bin", NULL);
    return TCL_ERROR;
  }
  return TCL_OK;
}


#else /* USE_FLASHLITE */

int LinkSymbolToRetrieveFlashFaultInit = 0;

void FlashFaultInit(Tcl_Interp* interp) {}

#endif /* USE_FLASHLITE */