%description:
Tests Ethernet communication via switch.
All hosts use EtherMAC implementation with external queue.
Simulation has pairs of hosts that communicate with each other via only one switch.
Both hosts in pair have same ethernet configurations.
Both hosts in pair are source and sink, too.

variations:
 - speed: 10Mbps / 100Mbps / 1000Mbps

checks:
 - the idle state of rx is less than or equals to 2% in all hosts
 - the utilization state of rx is more than or equals to 98% in all hosts

%#--------------------------------------------------------------------------------------------------------------
%testprog: opp_run

%#--------------------------------------------------------------------------------------------------------------
%file: test.ned
import ned.DatarateChannel;
import inet.node.ethernet.EtherHost;
import inet.node.ethernet.EtherSwitch;

module EtherHostQ extends EtherHost
{
    parameters:
        queueType = "DropTailQueue";
}


//
// Sample Ethernet LAN: hosts connected in pairs
//
network SpeedTest
{
    types:
        channel C10 extends DatarateChannel
        {
            delay = 0s;
            datarate = 10Mbps;
        }
        channel C100 extends DatarateChannel
        {
            delay = 0s;
            datarate = 100Mbps;
        }
        channel C1000 extends DatarateChannel
        {
            delay = 0s;
            datarate = 1000Mbps;
        }
    submodules:
        host_10_F_A: EtherHostQ {
            parameters:
                @display("p=70,70");
        }
        host_10_F_B: EtherHostQ {
            parameters:
                @display("p=210,70");
        }
        host_100_F_A: EtherHostQ {
            parameters:
                @display("p=70,140");
        }
        host_100_F_B: EtherHostQ {
            parameters:
                @display("p=210,140");
        }
        host_1000_F_A: EtherHostQ {
            parameters:
                @display("p=70,210");
        }
        host_1000_F_B: EtherHostQ {
            parameters:
                @display("p=210,210");
        }
        switch: EtherSwitch {
            parameters:
                @display("p=140,140");
            gates:
                ethg[6];
        }
    connections:
        switch.ethg[0] <--> C10   <--> host_10_F_A.ethg;
        switch.ethg[1] <--> C10   <--> host_10_F_B.ethg;
        switch.ethg[2] <--> C100  <--> host_100_F_A.ethg;
        switch.ethg[3] <--> C100  <--> host_100_F_B.ethg;
        switch.ethg[4] <--> C1000 <--> host_1000_F_A.ethg;
        switch.ethg[5] <--> C1000 <--> host_1000_F_B.ethg;
}

%#--------------------------------------------------------------------------------------------------------------
%inifile: omnetpp.ini
[General]
sim-time-limit = 5s

tkenv-plugin-path = ../../../etc/plugins
#record-eventlog = true
**.vector-recording = false

network = SpeedTest

*.host_10_F_A.cli.destAddress = "host_10_F_B"
*.host_10_F_B.cli.destAddress = "host_10_F_A"

*.host_100_F_A.cli.destAddress = "host_100_F_B"
*.host_100_F_B.cli.destAddress = "host_100_F_A"

*.host_1000_F_A.cli.destAddress = "host_1000_F_B"
*.host_1000_F_B.cli.destAddress = "host_1000_F_A"


**.cli.reqLength = 1250B       # 10.000 bit
**.cli.respLength = 1250B      # 10.000 bit
*.host_*_*.cli.startTime = 0s

*.host_*_F_*.mac.duplexMode = true

*.host_1000_F_*.cli.sendInterval = 0.005ms       # 10.000 / speed [ / 2 when halfduplex]
*.host_100_F_*.cli.sendInterval  = 0.05ms        # 10.000 / speed [ / 2 when halfduplex]
*.host_10_F_*.cli.sendInterval   = 0.5ms            # 10.000 / speed [ / 2 when halfduplex]

**.mac.address = "auto"

%#--------------------------------------------------------------------------------------------------------------
%postrun-command: Rscript check.r
%#--------------------------------------------------------------------------------------------------------------
%file: check.r
#!/usr/bin/env Rscript

options(echo=FALSE)
options(width=160)
library("omnetpp", warn.conflicts=FALSE)

#TEST parameters
scafile <- 'results/General-#0.sca'
linecount <- 12
idlelimit <- 2.0
usedlimit <- 98.0

# begin TEST:

idle <- loadDataset(scafile, add(type='scalar', select='name("rx channel idle *")'))
used <- loadDataset(scafile, add(type='scalar', select='name("rx channel utilization *")'))

cat("\nOMNETPP TEST RESULT: ")

if(length(idle$scalars$value) == linecount & max(idle$scalars$value) <= idlelimit)
{
    cat("IDLE OK\n")
} else {
    cat("IDLE BAD:\n")
    print(idle$scalars[idle$scalars$value > idlelimit,])
}

cat("\nOMNETPP TEST RESULT: ")

if(length(used$scalars$value) == linecount & min(used$scalars$value) >= usedlimit)
{
    cat("USED OK\n")
} else {
    cat("USED BAD:\n")
    print(used$scalars[used$scalars$value < usedlimit,])
}

cat("\n")

%#--------------------------------------------------------------------------------------------------------------
%contains: postrun-command(1).out

OMNETPP TEST RESULT: IDLE OK

OMNETPP TEST RESULT: USED OK

%#--------------------------------------------------------------------------------------------------------------
