[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ErrorModel Question in NS2



>   I the following to define the error model 
> 
>     set loss_module [new ErrorModel] 
>     $loss_module set rate_ 10 
> 
>     which does not make any errors. 
> 
>    What I don't understand is: 
> 
>     o Do I need to insert the loss module to my TCP agent? HOW? 
>     o Any specific sequence? 

Following are scripts I wrote to insert/remove/enable/disable loss modules
into SimpleLinks for Simulator and SessionSim. There loss modules are
inserted right after the queue. Hope they'll be useful.

You'll need some refinements. E.g., you may want to put error modules as
parameter to insert-loss instead of specifying a rate, and my ErrorModel
is my special one you'll need to change it.

- Haobo

#
# insert an ErrorModel between the queue and the link
# a pkt still go thru the queue and may be dropped there, then the 
# loss introduced by the ErrorModel is purely the effect of the link
#
# Must be inserted *RIGHT AFTER* the deqT_ (if present) or queue_, because
# nam can only visualize a packet drop if and only if it is on the link or 
# in the queue
#
SimpleLink instproc add-loss { ns em } {
	$self instvar queue_ head_ link_ isLossy_ deqT_ lossModule_

	# set em between a queue and a DelayLink
	$em onlink

	if [info exists deqT_] {
		$em target [$deqT_ target]
		$deqT_ target $em
	} else {
		$em target [$queue_ target]
		$queue_ target $em
	}

	set isLossy_ 1
	set lossModule_ $em
}

SimpleLink instproc is-lossy {} {
	$self instvar isLossy_ lossModule_
	if [info exists isLossy_] {
		return [$lossModule_ enabled?]
	} else {
		return 0
	}
}

SimpleLink instproc has-loss-module {} {
	$self instvar isLossy_
	if [info exists isLossy_] {
		return 1
	} else {
		return 0
	}
}

SimpleLink instproc enable-loss {} {
	$self instvar isLossy_ lossModule_

	if ![info exists isLossy_] {
		return
	}
	$lossModule_ enable
}

SimpleLink instproc change-loss-rate { rate } {
	$self instvar lossModule_
	if [info exists lossModule_] {
		$lossModule_ set rate_ $rate
	}
}

SimpleLink instproc disable-loss {} {
	$self instvar isLossy_ lossModule_

	if ![info exists isLossy_] {
		return
	}
	$lossModule_ disable
}

SimpleLink instproc remove-loss {} {
	$self instvar isLossy_ queue_ head_ deqT_

	if ![info exists isLossy_] {
		return;
	}

	if [info exists deqT_] {
		set em [$deqT_ target]
		$deqT_ target [$em target]
	} else {
		set em [$queue_ target]
		$queue_ target [$em target]
	}

	set drp [$em drop-target]

	[Simulator instance] instvar alltrace_

	if [info exists alltrace_] {
		set pos [lsearch -glob $alltrace_ $drp]
		if {$pos == -1} {
			$self instvar fromNode_ toNode_
			error "No drop trace $drp for lossy link ([$fromNode_ id]:[$toNode_ id]), all trace = $alltrace_"
		} else {
			set alltrace_ [lreplace $alltrace_ $pos $pos]
			puts "Drop trace object $drp deleted"
		}
		delete $drp
	}

	delete $em
	unset isLossy_
}

#
# following is the correct separation between link and simulator
# simulator should set everything which is related to its variables,
# then pass everything else to link
#
Simulator instproc insert-loss { n1 n2 rate } {
	$self instvar myLossyRanVar_ nullAgent_ 
	$self instvar link_ 

	# if the link already has a loss module, enable it
	set lnk $link_([$n1 id]:[$n2 id])
	if [$lnk has-loss-module] {
		$lnk enable-loss
#		puts "Link ([$n1 id]:[$n2 id]) loss enabled"
		return
	}

	if ![info exists myLossyRanVar_] {
		set myLossyRanVar_ [new RandomVariable/Uniform]
		$myLossyRanVar_ set min_ 0.0
		$myLossyRanVar_ set max_ 1.0
	}
	set em [new ErrorModel/RandomSRMErrorModel pkt]
	$em ranvar $myLossyRanVar_
	$em set rate_ $rate ;# 10% loss => rate = 0.1
	$em drop-target $nullAgent_

	set trace [$self get-ns-traceall]
	if {$trace != ""} {
		set drpT [$self create-trace Drop $trace $n1 $n2]
		set trace [$self get-nam-traceall]
		if {$trace != ""} {
			$drpT namattach $trace
		}
		$drpT target [$em drop-target]
		$em drop-target $drpT
	} else {
		set trace [$self get-nam-traceall]
		if {$trace != ""} {
			set drpT [$self create-trace Drop $trace $n1 $n2
nam]
			$drpT target [$em drop-target]
			$em drop-target $drpT
		}
	}

	$lnk add-loss $self $em
	puts "Lossy link ([$n1 id]:[$n2 id]) added"
}

# should eventually rename to diable instead of remove
Simulator instproc remove-loss { n1 n2 } {
	$self instvar link_
#	$link_([$n1 id]:[$n2 id]) remove-loss
	$link_([$n1 id]:[$n2 id]) disable-loss
	puts "Link ([$n1 id]:[$n2 id]) no longer lossy"
}

Simulator instproc add-duplex-lossy-link { n1 n2 rate } {
	$self insert-loss $n1 $n2 $rate
	$self insert-loss $n2 $n1 $rate
}

Simulator instproc remove-duplex-lossy-link { n1 n2 } {
	$self remove-loss $n1 $n2
	$self remove-loss $n2 $n1
}

#
# change all duplex link to lossy link
#
Simulator instproc duplex-lossy-link { n1 n2 bw delay type {rate 0.1} } {
	$ns duplex-link $n1 $n2 $bw $delay $type

	# we may add some ranvar and ratio here so only some percentage of 
	# the links are lossy, and these lossy links are chosen randomly
	$self add-duplex-lossy-link $n1 $n2 $rate
}

Simulator instproc duplex-lossy-link-of-interfaces { n1 n2 bw delay type
{rate 0.1} } {
	$ns duplex-lossy-link-of-interfaces $n1 $n2 $bw $delay $type
	$self add-duplex-lossy-link $n1 $n2 $rate
}

Simulator instproc is-lossy { src dst } {
	$self instvar link_
	return [$link_([$src id]:[$dst id]) is-lossy]
}

SessionSim instproc insert-loss { n1 n2 rate} {
	$self instvar loss_ link_
	$self instvar myLossyRanVar_ nullAgent_ 

	set from [$n1 id]
	set to [$n2 id]

	# if the link already has a loss module, enable it
	if [info exists loss_($from:$to)] {
		$loss_($from:$to) enable
#		puts "Link ([$n1 id]:[$n2 id]) loss enabled"
		return
	}

	if ![info exists myLossyRanVar_] {
		set myLossyRanVar_ [new RandomVariable/Uniform]
		$myLossyRanVar_ set min_ 0.0
		$myLossyRanVar_ set max_ 1.0
	}
	set em [new ErrorModel/RandomSRMErrorModel pkt]
	$em ranvar $myLossyRanVar_
	$em set rate_ $rate ;# 10% loss => rate = 0.1
	$em drop-target $nullAgent_

	set trace [$self get-nam-traceall]
	if {$trace != ""} {
		set drpT [$self create-trace Drop $trace $n1 $n2 nam]
		$drpT target [$em drop-target]
		$em drop-target $drpT
	}

	if [info exists link_($from:$to)] {
		set loss_($from:$to) $em
	}
	puts "Lossy link ([$n1 id]:[$n2 id]) added"

}

SessionSim instproc is-lossy { src dst } {
	$self instvar loss_

	set from [$src id]
	set to [$dst id]
	return [info exists loss_($from:$to)]
}

# should eventually rename to diable instead of remove
SessionSim instproc remove-loss { n1 n2 } {
	$self instvar loss_

	set from [$n1 id]
	set to [$n2 id]
	if [info exists loss_($from:$to)] {
		$loss_($from:$to) disable
	}
	puts "Link ([$n1 id]:[$n2 id]) no longer lossy"
}