StateSensor.cc 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// StateSensor.cc

#include "lib.h"
#include "StateSensor.h"

using namespace std;

StateSensor::StateSensor()
  : state(INITIAL)
{
  logWrite(SENSOR, "State change to INITIAL");
}

StateSensor::~StateSensor()
{
}

18
int StateSensor::getState(void) const
19 20 21 22
{
  return state;
}

23 24 25 26 27
bool StateSensor::isSaturated(void) const
{
  return saturated;
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
void StateSensor::localSend(PacketInfo * packet)
{
  if (packet->tcp->syn && state == INITIAL)
  {
    state = AFTER_SYN;
    logWrite(SENSOR, "State change to AFTER_SYN");
  }
  else if (packet->tcp->syn)
  {
    logWrite(ERROR, "Sent a SYN packet out of order");
  }
  else if (! packet->tcp->syn && state == AFTER_SYN_ACK)
  {
    state = BEFORE_ESTABLISHED;
    logWrite(SENSOR, "State change to BEFORE_ESTABLISHED");
  }
  else if (state == BEFORE_ESTABLISHED)
  {
    state = ESTABLISHED;
    logWrite(SENSOR, "State change to ESTABLISHED");
  }
49
  calculateSaturated(packet);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
}

void StateSensor::localAck(PacketInfo * packet)
{
  if (packet->tcp->syn && packet->tcp->ack && state == AFTER_SYN)
  {
    state = AFTER_SYN_ACK;
    logWrite(SENSOR, "State change to AFTER_SYN_ACK");
  }
  else if (packet->tcp->syn && packet->tcp->ack)
  {
    logWrite(ERROR, "Received a SYNACK packet out of order");
  }
  else if (state == BEFORE_ESTABLISHED)
  {
    state = ESTABLISHED;
    logWrite(SENSOR, "State change to ESTABLISHED");
  }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  calculateSaturated(packet);
}

void StateSensor::calculateSaturated(PacketInfo * packet)
{
  unsigned int snd_cwnd = packet->kernel->tcpi_snd_cwnd;
  unsigned int snd_ssthresh = packet->kernel->tcpi_snd_ssthresh;
  unsigned int window = (static_cast<unsigned int>(htons(packet->tcp->window))
    << packet->kernel->tcpi_rcv_wscale);
  unsigned int unacked = packet->kernel->tcpi_unacked * 1448;
  logWrite(SENSOR, "stateEstablished=%d,bufferFull=%d", state == ESTABLISHED,
           packet->bufferFull);
  logWrite(SENSOR, "snd_cwnd=%u,snd_ssthresh=%u,window=%u,unacked=%u",
           snd_cwnd, snd_ssthresh, window, unacked);
  saturated = (state == ESTABLISHED
               && packet->bufferFull
               // and *not* in slow start
               && !(snd_cwnd < snd_ssthresh || window <= unacked));
86
}