-- Copyright (C) 1984  by Xerox Corporation. All rights reserved. 
-- RealForwarder.mesa, HGM, 13-Oct-84 16:23:55

DIRECTORY
  Heap USING [systemZone],
  Inline USING [LowHalf],
  Process USING [Detach, SetPriority, SetTimeout, MsecToTicks],
  Runtime USING [IsBound],
  System USING [Pulses, GetClockPulses, PulsesToMicroseconds],
  
  Buffer USING [
    AccessHandle, BuffersLeft, GetBuffer, MakePool,
    PupBuffer, ReturnBuffer, QueueInitialize],
  CommFlags USING [doStats],
  Driver USING [Network, GetDeviceChain],
  ForwarderDefs USING [
    Counters, SetupForwarderThings, ForwarderStats, PrintBadPup,
    statPupGatewayPacketMs, statGateInfoReplies,
    statRoutingTableChanges, statGateInfoBC, statGateLowOnBuffers,
    statGarbageSourceOrDest, statNoRouteToNet, statTooManyHops,
    forwarderStatsRequest],
  PupDefs,
  PupRouterDefs USING [
    BuildErrorPup, EnumerateRoutingTable, GetRoutingTableEntry, maxHop,
    PupGateInfo, PupGatewaySee, RejectPupWithBadChecksum, RoutingTableEntry,
    SetBadPupProc, SetPupChecksum],
  PupTypes USING [
    PupNetID, PupHostID, PupErrorCode, allNets, allHosts, gatewaySoc,
    maxDataWordsPerRoutingPup],
  RoutingFudges USING [],
  Stats USING [StatBump, StatIncr];

RealForwarder: MONITOR
  IMPORTS
    Heap, Inline, Process, Runtime, System, Stats, ForwarderDefs, PupRouterDefs,
    Driver, PupDefs, Buffer
  EXPORTS Buffer, ForwarderDefs, PupRouterDefs, RoutingFudges
  SHARES Buffer =
  BEGIN OPEN Stats, PupRouterDefs, PupDefs, Buffer, PupTypes;

  -- EXPORTed TYPEs
  Network: PUBLIC TYPE = Driver.Network;

  tellEverybody: BOOLEAN ← TRUE;  -- debugging flag to avoid poluting the world
  doStats: BOOLEAN = TRUE;

  packets: LONG POINTER TO ForwarderDefs.Counters;
  bytes: LONG POINTER TO ForwarderDefs.Counters;
  bad: LONG POINTER TO ForwarderDefs.Counters;
  nets: CARDINAL;
  stop: BOOLEAN ← FALSE;
  pupGateSoc: PupSocket;
  pool: Buffer.AccessHandle ← Buffer.MakePool[send: 5, receive: 0];
  extraHops: CARDINAL ← 0;


  GetPointerToPupGateStats: PUBLIC PROCEDURE
    RETURNS [
      packets, bytes: LONG POINTER TO ForwarderDefs.Counters, nets: CARDINAL] =
    BEGIN RETURN[RealForwarder.packets, RealForwarder.bytes, RealForwarder.nets]; END;

  GetPointerToBadPupStats: PUBLIC PROCEDURE
    RETURNS [
      bad: LONG POINTER TO ForwarderDefs.Counters] =
    BEGIN RETURN[RealForwarder.bad]; END;

  SetPupFudge: PUBLIC PROCEDURE [hops: CARDINAL] =
    BEGIN
    extraHops ← hops;
    END;

  PupForwarderOn: PUBLIC PROCEDURE =
    BEGIN
    network: Network ← Driver.GetDeviceChain[];
    IF doStats THEN
      BEGIN
      finger: Network;
      size: CARDINAL;
      nets ← 1;  -- discard
      FOR finger ← network, finger.next UNTIL finger = NIL DO
        nets ← nets + 1; ENDLOOP;
      nets ← nets + 2;  -- Allocate room for more drivers.
      size ← nets*nets;
      packets ← Heap.systemZone.NEW[ForwarderDefs.Counters[size]];
      bytes ← Heap.systemZone.NEW[ForwarderDefs.Counters[size]];
      FOR i: CARDINAL IN [0..size) DO packets[i] ← bytes[i] ← 0; ENDLOOP;
      bad ← Heap.systemZone.NEW[ForwarderDefs.Counters[nets]];
      FOR i: CARDINAL IN [0..nets) DO bad[i] ← 0; ENDLOOP;
      END;
    Process.Detach[FORK PupForwarderOn2[]];
    END;

  PupForwarderOn2: ENTRY PROCEDURE =
    BEGIN
    network: Network ← Driver.GetDeviceChain[];
    world: PupAddress ← [[0], [0], gatewaySoc];
    spin: CONDITION;
    Process.SetTimeout[@spin, Process.MsecToTicks[100]];
    -- As a hack, we can run without knowing our network number at startup time.
    -- Wait here to be sure we don't polute things if we don't know it yet.
    Process.Detach[FORK LookAtBadPups[]];
    Process.Detach[FORK TransferForwarderStats[]];
    UNTIL network.pupNetNumber # 0 DO IF stop THEN RETURN; WAIT spin; ENDLOOP;
    Process.Detach[FORK PupTalk[]];
    pupGateSoc ← PupSocketMake[gatewaySoc, world, veryLongWait];
    Process.Detach[FORK PupListen[]];
    END;

  PupForwarderOff: PUBLIC ENTRY PROCEDURE =
    BEGIN
    spin: CONDITION;
    Process.SetTimeout[@spin, Process.MsecToTicks[100]];
    stop ← TRUE;
    THROUGH [0..5) DO
      -- 5 cycles of 100 ms each
      NOTIFY talker; WAIT spin; ENDLOOP;
    END;

  PupListen: PUBLIC PROCEDURE =
    BEGIN
    b: PupBuffer;
    DO
      b ← pupGateSoc.get[];
      IF b = NIL THEN LOOP;
      SELECT b.pup.pupType FROM
        gatewayRequest =>
          BEGIN
          IF ~tellEverybody AND b.pup.dest.host = allHosts THEN
            BEGIN  -- don't answer broadcast requests yet
            Buffer.ReturnBuffer[b];
            END
          ELSE
            BEGIN
            SwapPupSourceAndDest[b];
            SendPupRoutingPacket[b, FALSE];
            IF doStats THEN StatIncr[ForwarderDefs.statGateInfoReplies];
            END
          END;
        gatewayInfo =>
          BEGIN  -- RoutingTable packet from another Gateway
	  pulses: System.Pulses ← System.GetClockPulses[];
	  ms: LONG CARDINAL;
          IF doStats AND ~CommFlags.doStats THEN StatIncr[statPupGatewayPacketsRecv];
          IF b.pup.pupTransportControl = 0 AND PupGatewaySee[b] THEN
            BEGIN
            KickTalker[];
            IF doStats THEN StatIncr[ForwarderDefs.statRoutingTableChanges];
            END;
          Buffer.ReturnBuffer[b];
          pulses ← System.Pulses[System.GetClockPulses[] - pulses];
	  ms ← System.PulsesToMicroseconds[pulses]/1000;
          IF doStats THEN Stats.StatBump[ForwarderDefs.statPupGatewayPacketMs, Inline.LowHalf[ms]];
          END;
        ForwarderDefs.forwarderStatsRequest => PutStats[b];
        ENDCASE => Buffer.ReturnBuffer[b];
      ENDLOOP;
    END;

  talker: CONDITION;

  KickTalker: PUBLIC ENTRY PROCEDURE = BEGIN NOTIFY talker; END;

  PupTalk: PUBLIC PROCEDURE =
    BEGIN
    Pause: ENTRY PROCEDURE = INLINE  -- Else hang ML if GetBuffer is unlucky
      BEGIN
      WAIT talker;
      WAIT pause;  -- Don't broadcast too often
      END;
    pause: CONDITION;
    b: PupBuffer;
    i: CARDINAL ← 0;
    Process.SetTimeout[@talker, Process.MsecToTicks[28000]];
    Process.SetTimeout[@pause, Process.MsecToTicks[2000]];
    DO
      Pause[];
      IF tellEverybody THEN
        BEGIN
        i ← i + 1;
        b ← Buffer.GetBuffer[pup, pool, send];
        b.pup.pupID ← [0, i];
        b.pup.dest ← [allNets, allHosts, gatewaySoc];
        b.pup.source ← [, , gatewaySoc];
        SendPupRoutingPacket[b, TRUE];
        IF doStats THEN StatIncr[ForwarderDefs.statGateInfoBC];
        END;
      ENDLOOP;
    END;

  SendPupRoutingPacket: PROCEDURE [b: PupBuffer, kick: BOOLEAN] =
    BEGIN
    packetNumber: CARDINAL ← 1;
    data: LONG POINTER TO PupGateInfo ← LOOPHOLE[@b.pup.pupWords[0]];
    AddOne: PROCEDURE [rte: RoutingTableEntry] =
      BEGIN
      IF rte.net = 0 OR rte.network = NIL THEN RETURN;
      IF n = maxDataWordsPerRoutingPup/SIZE[PupGateInfo] THEN
        BEGIN
        b2: PupBuffer ← Buffer.GetBuffer[pup, pool, send, fullBuffer, FALSE];
        IF b2 # NIL THEN
          BEGIN
          b2.pup.pupID ← b.pup.pupID;
          b2.pup.source ← b.pup.source;
          b2.pup.dest ← b.pup.dest;
          b2.pup.pupType ← b.pup.pupType;
          SetPupContentsWords[b, n*SIZE[PupGateInfo]];
          PupRouterSendThis[b];
          b ← b2;
 	  packetNumber ← packetNumber + 1;
          IF kick THEN b.pup.pupID.a ← packetNumber;
          END;
        data ← LOOPHOLE[@b.pup.pupWords[0]];
        n ← 0;
        END;
      IF rte.hop = 0 THEN
        data↑ ← [
          net: rte.net, viaNet: rte.net, viaHost: [rte.network.pupHostNumber],
          hop: 0 + extraHops]
      ELSE
        data↑ ← [
          net: rte.net, viaNet: [rte.network.pupNetNumber], viaHost: rte.route,
          hop: rte.hop + extraHops];
      IF stop THEN data.hop ← maxHop + 1;
      data ← data + SIZE[PupGateInfo];
      n ← n + 1;
      END;
    n: CARDINAL ← 0;
    b.pup.pupType ← gatewayInfo;
    IF kick THEN b.pup.pupID.a ← packetNumber;
    EnumerateRoutingTable[AddOne];
    SetPupContentsWords[b, n*SIZE[PupGateInfo]];
    PupRouterSendThis[b];
    END;

  magicOne: INTEGER = -9;  -- offset for pupTransportControl
  magicTwo: CARDINAL = 1;

  DoForwardPupBuffer: PUBLIC PROCEDURE [b: PupBuffer] =
    BEGIN
    fromNetwork: Network ← b.network;
    toNetwork: Network;
    sourceNet: PupNetID ← b.pup.source.net;
    destNet: PupNetID ← b.pup.dest.net;
    length: CARDINAL = b.pup.pupLength;
    route: PupHostID;
    rte: RoutingTableEntry;
    old: WORD;
    errorCode: PupTypes.PupErrorCode;
    -- Allow directed broadcasts, but avoid nonsense and loops
    IF sourceNet = 0 OR destNet = 0 OR b.pup.source.host = allHosts
      OR (destNet = fromNetwork.pupNetNumber AND b.pup.dest.host = allHosts) THEN
      BEGIN  -- Don't forward this Pup
      Buffer.ReturnBuffer[b];
      IF doStats THEN StatIncr[statPupNotForwarded];
      IF doStats THEN StatIncr[ForwarderDefs.statGarbageSourceOrDest];
      RETURN;
      END;
    old ← (@b.pup.pupWords[0] + magicOne)↑;
    IF b.pup.pupTransportControl = maxHop*20B THEN
      BEGIN
      DoErrorPup[b, eightHopsPupErrorCode, "Discarded by 16th Gateway"];
      IF doStats THEN BumpPupStats[fromNetwork.index, 0, 0];
      IF doStats THEN StatIncr[ForwarderDefs.statTooManyHops];
      RETURN;
      END;
    b.pup.pupTransportControl ← b.pup.pupTransportControl + 20B;
    rte ← GetRoutingTableEntry[destNet];
    IF rte = NIL OR rte.hop > maxHop OR (toNetwork ← rte.network) = NIL THEN
      BEGIN  -- don't know how to get there
      DoErrorPup[b, cantGetTherePupErrorCode, "No route to that Net"];
      IF doStats THEN BumpPupStats[fromNetwork.index, 0, 0];
      IF doStats THEN StatIncr[ForwarderDefs.statNoRouteToNet];
      RETURN;
      END;
IF FALSE THEN
    IF BuffersLeft[NIL] < 3 THEN
      BEGIN
      IF doStats THEN StatIncr[statPupNotForwarded];
      Buffer.ReturnBuffer[b];
      IF doStats THEN BumpPupStats[fromNetwork.index, 0, 0];
      IF doStats THEN StatIncr[ForwarderDefs.statGateLowOnBuffers];
      RETURN;
      END;
    b.network ← toNetwork;
    UpdatePupChecksum[b, magicTwo, old];
    IF (route ← rte.route) = 0 THEN route ← b.pup.dest.host;
    errorCode ← toNetwork.encapsulateAndForwardPup[b, route];
    SELECT errorCode FROM
      noErrorPupErrorCode =>
        BEGIN
        IF doStats THEN
          BEGIN
          StatIncr[statPupForwarded];
          BumpPupStats[fromNetwork.index, toNetwork.index, length];
          END;
        END;
      ENDCASE =>
        BEGIN
        IF errorCode = cantGetTherePupErrorCode AND rte.hop # 0 THEN
          rte.hop ← maxHop + 1;
        DoErrorPup[b, errorCode, NIL];
        IF doStats THEN BumpPupStats[fromNetwork.index, 0, 0];
        END;
    END;

  -- This is the routine that defines the layout of the statistics counters.
  -- Note that a dest of 0 is used for discard.

  BumpPupStats: PROCEDURE [sourceIndex, destIndex, length: CARDINAL] =
    BEGIN
    index: CARDINAL = sourceIndex + destIndex*nets;
    packets[index] ← packets[index] + 1;
    bytes[index] ← bytes[index] + length;
    END;

  DoErrorPup: PROCEDURE [
    b: PupBuffer, code: PupTypes.PupErrorCode, text: LONG STRING] =
    BEGIN
    destNet: PupNetID;
    toNetwork: Network;
    route: PupHostID;
    rte: RoutingTableEntry;
    IF doStats THEN StatIncr[statPupNotForwarded];
    IF text = NIL THEN
      SELECT code FROM
        connectionLimitPupErrorCode =>
          BEGIN
          text ← "Gateway Output Queue connection limit exceeded";
          code ← gatewayResourceLimitsPupErrorCode;
          END;
        gatewayResourceLimitsPupErrorCode => text ← "Gateway Output Queue full";
        cantGetTherePupErrorCode => text ← "No route to host";
        ENDCASE;
    IF ~PupRouterDefs.BuildErrorPup[b, code, text] THEN RETURN;
    destNet ← b.pup.dest.net;
    b.pup.source.socket ← [0, 0];
    rte ← GetRoutingTableEntry[destNet];
    IF rte = NIL OR rte.hop > maxHop OR (toNetwork ← rte.network) = NIL THEN
      BEGIN Buffer.ReturnBuffer[b]; RETURN; END;
    b.network ← toNetwork;
    IF (route ← rte.route) = 0 THEN route ← b.pup.dest.host;
    SetPupChecksum[b];
    toNetwork.encapsulateAndSendPup[b, route];
    END;

  UpdatePupChecksum: PUBLIC PROCEDURE [
    b: PupBuffer, offset: INTEGER, oldValue: WORD] =
    BEGIN
    len: CARDINAL ← (b.pup.pupLength - 1)/2;
    checksumLoc: LONG POINTER ← @b.pup.pupLength + len;
    diff: WORD;
    IF checksumLoc↑ = 177777B THEN RETURN;
    diff ← OnesSub[
      (@b.pup.pupWords[0] + (offset + (magicOne - magicTwo)))↑, oldValue];
    checksumLoc↑ ← OnesAdd[checksumLoc↑, LeftCycle[diff, len - offset]];
    END;

  OnesAdd: PROCEDURE [a, b: CARDINAL] RETURNS [c: CARDINAL] = INLINE
    BEGIN c ← a + b; IF c < a THEN c ← c + 1; IF c = 177777B THEN c ← 0; END;

  OnesSub: PROCEDURE [a, b: CARDINAL] RETURNS [c: CARDINAL] = INLINE
    BEGIN
    c ← a + (-b - 1);
    IF c < a THEN c ← c + 1;
    IF c = 177777B THEN c ← 0;
    END;

  LeftCycle: PROCEDURE [a, b: CARDINAL] RETURNS [c: CARDINAL] = INLINE
    BEGIN
    c ← a;
    THROUGH [0..(b MOD 16)) DO
      IF c < 100000B THEN c ← c*2 ELSE c ← c*2 + 1; ENDLOOP;
    END;

  badPupArrived: CONDITION;
  badPupQueue: QueueObject;

  PutBadPup: ENTRY PROCEDURE [b: PupBuffer] =
    BEGIN
    network: Network ← b.network;
    index: CARDINAL ← network.index;
    bad[index] ← bad[index] + 1;
    IF badPupQueue.length > 5 THEN
      BEGIN PupRouterDefs.RejectPupWithBadChecksum[b]; RETURN; END;
    EnqueuePup[@badPupQueue, b];
    NOTIFY badPupArrived;
    END;

  GetBadPup: ENTRY PROCEDURE RETURNS [b: PupBuffer] =
    BEGIN
    IF badPupQueue.length = 0 THEN WAIT badPupArrived;
    b ← DequeuePup[@badPupQueue];
    END;

  LookAtBadPups: PROCEDURE =
    BEGIN
    IF ~Runtime.IsBound[LOOPHOLE[ForwarderDefs.PrintBadPup]] THEN RETURN;
    Process.SetPriority[1];
    PupRouterDefs.SetBadPupProc[PutBadPup];
    DO ForwarderDefs.PrintBadPup[GetBadPup[]]; ENDLOOP;
    END;

  -- UGH, we can't just call somebody because we don't want to clutter up core with stuff that isn't normally needed, so we go through this horrible process switch to change priorities.

  statsArrived: CONDITION;
  statsQueue: QueueObject;

  PutStats: ENTRY PROCEDURE [b: PupBuffer] =
    BEGIN
    IF statsQueue.length > 5 THEN BEGIN Buffer.ReturnBuffer[b]; RETURN; END;
    EnqueuePup[@statsQueue, b];
    NOTIFY statsArrived;
    END;

  GetStats: ENTRY PROCEDURE RETURNS [b: PupBuffer] =
    BEGIN
    WHILE statsQueue.length = 0 DO WAIT statsArrived; ENDLOOP;
    b ← DequeuePup[@statsQueue];
    END;

  TransferForwarderStats: PROCEDURE =
    BEGIN
    Process.SetPriority[1];
    DO ForwarderDefs.ForwarderStats[GetStats[]]; ENDLOOP;
    END;

  -- Initialization

  ForwarderDefs.SetupForwarderThings[];
  Buffer.QueueInitialize[@badPupQueue];
  Process.SetTimeout[@badPupArrived, Process.MsecToTicks[60000]];
  Buffer.QueueInitialize[@statsQueue];
  Process.SetTimeout[@statsArrived, Process.MsecToTicks[60000]];
  END.