UdpMinDelaySensor.cc 3.13 KB
Newer Older
1 2
/*
 * Copyright (c) 2006 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23
 */

24
#include "UdpMinDelaySensor.h"
25
#include "CommandOutput.h"
26

27 28 29
using namespace std;

UdpMinDelaySensor::UdpMinDelaySensor(UdpPacketSensor const *udpPacketSensorVal)
30 31 32 33 34 35 36 37 38 39 40
	: minDelay(ULONG_LONG_MAX),
	packetHistory(udpPacketSensorVal)
{

}

UdpMinDelaySensor::~UdpMinDelaySensor()
{

}

41
unsigned long long UdpMinDelaySensor::getMinDelay() const
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
	return minDelay;
}	

void UdpMinDelaySensor::localSend(PacketInfo *packet)
{
	// Do nothing.
	sendValid = true;
	ackValid = false;

}


void UdpMinDelaySensor::localAck(PacketInfo *packet)
{
	 // This is a re-ordered ACK or an incorrect packet - don't do anything
	// with it - just return.
59 60 61
	// If this packet is ACKing a packet that we lost due to libpcap send loss,
	// dont use this packet timestamp for RTT calculations.
	if( packetHistory->isAckValid() == false || packetHistory->isAckFake() == true )
62 63 64 65 66 67 68 69 70 71 72 73 74 75
	{
		ackValid = false;
		sendValid = false;
		return;
	}
	ackValid = true;
	sendValid = false;

	unsigned short int seqNum = *(unsigned short int *)(packet->payload + 1);

	unsigned long long oneWayDelay;
	bool eventFlag = false;

	vector<UdpPacketInfo >::iterator vecIterator;
76 77
	vector<UdpPacketInfo > ackedPackets = packetHistory->getAckedPackets();
	vecIterator = find_if(ackedPackets.begin(), ackedPackets.end(), bind2nd(equalSeqNum(), seqNum));
78 79

	unsigned long long timeStamp = packet->packetTime.toMicroseconds();
80 81

	oneWayDelay = (timeStamp - (*vecIterator).timeStamp)/2;
82 83 84

	// Calculate the delay for the maximum sized packet.

85
	oneWayDelay = ( oneWayDelay ) * 1518 / ( (*vecIterator).packetSize);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

	// Set this as the new minimum one way delay.
	if(oneWayDelay < minDelay)
	{
		eventFlag = true;
		minDelay = oneWayDelay;
	}

	// We should not be calculating the minimum delay based on the
	// redundant ACKs - because we cannot exactly calculate their
	// RTT values, from just the receiver timestamps.

	// Send an event message to the monitor to change the value of minimum one way delay.
	if(eventFlag == true)
	{
101
		ostringstream messageBuffer;
102
		messageBuffer << "delay="<<(minDelay)/1000;
103
		global::output->eventMessage(messageBuffer.str(), packet->elab, CommandOutput::FORWARD_PATH);
104
		global::output->eventMessage(messageBuffer.str(), packet->elab, CommandOutput::BACKWARD_PATH);
105 106 107 108
		logWrite(SENSOR,"VALUE::New Min delay = %llu",minDelay);
	}
	logWrite(SENSOR,"MIND:TIME=%llu,MIND=%llu",timeStamp,minDelay);
}