# Copyright (c) 2016, Noa Zilberman
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the project, the copyright holder nor the names of its
#  contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#Run application level jitter test
#Minimum latency test
OUTPUT_FILE = out.txt
PIN_CPU = 2
RUN_RT = 1
SLEEP_DELAY = 60
SAMPLES = 1000000
ITERATIONS=10
IP_ADDRESS = 192.168.100.1
IP_LOCAL = 192.168.100.2
IP_NET = 192.168.100.0
IF_NAME = eth1
EXIST = $(shell ip route show|grep $(IP_NET) | wc -l)
USE_DAG =0
DAG_OUTPUT = capture/dag.dat
SKIP=10000
RECORDS=$$(( $(SAMPLES) - $(SKIP) ))
RUNS=$$(($(TOTAL_SAMPLES) / $(SAMPLES)))

#all: test_emu

all:
	for i in `seq 1 $(ITERATIONS)`; do\
                ${MAKE} start_dag;\
                ${MAKE} test_emu I=$$i;\
                ${MAKE} stop_dag;\
                ${MAKE} analyse_dag I=$$i;\
        done
	./parse.sh $(DAG_OUTPUT) $(OUTPUT_FILE)


test_remote:
	@echo " #################################################"
	@echo " Starting icmp rtt test	                 	 "
	@echo " #################################################"
	sleep $(SLEEP_DELAY)
	sysctl -w net.ipv4.ping_group_range="0 0"
	ifconfig $(IF_NAME) $(IP_LOCAL) up
	$(PIN_PATH)/pininit
	$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT) "./source/icmp_test -n $(SAMPLES) -c $(PIN_CPU) -f $(OUTPUT_FILE) -i $(IP_ADDRESS)"
	$(PIN_PATH)/pinreset
	@echo " #################################################"
	@echo " Finished application+OS ping test                "
	@echo " #################################################"

test_emu:
	@echo " #################################################"
	@echo " Starting icmp rtt test                           "
	@echo " #################################################"
	ifconfig $(IF_NAME) $(IP_LOCAL) up
	@echo "$(EXIST)"
ifeq  ($(EXIST),0)
	ip route add $(IP_NET)/24 dev $(IF_NAME)
else
	ip route chg $(IP_NET)/24 dev $(IF_NAME)
endif	#route add -net $(IP_NET)  netmask 255.255.255.0 dev $(IF_NAME)
	arp -s $(IP_ADDRESS)  00:0f:53:0d:d1:51 -i $(IF_NAME)
	sleep $(SLEEP_DELAY)
	sysctl -w net.ipv4.ping_group_range="0 0"
	$(PIN_PATH)/pininit
	$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT) "./source/icmp_test -n $(SAMPLES) -c $(PIN_CPU) -f $(I)_$(OUTPUT_FILE) -i $(IP_ADDRESS)"
	$(PIN_PATH)/pinreset
	@echo " #################################################"
	@echo " Finished application+OS ping test                "
	@echo " #################################################"


test_local:
	@echo " #################################################"
	@echo " Starting icmp rtt test                           "
	@echo " #################################################"
	sleep $(SLEEP_DELAY)
	ifconfig $(IF_NAME) $(IP_LOCAL) up
	sysctl -w net.ipv4.ping_group_range="0 0"
	$(PIN_PATH)/pininit
	$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT) "./source/icmp_test -n $(SAMPLES) -c $(PIN_CPU) -f local_$(OUTPUT_FILE) -i $(IP_LOCAL)"
	$(PIN_PATH)/pinreset
	@echo " #################################################"
	@echo " Finished application+OS ping test                "
	@echo " #################################################"

start_dag:
	@echo "Starting DAG Capture..."
ifeq ($(USE_DAG),1)
	dagload ;\
	$(CAMIO_PATH)/dag_capture/bin/dag_capture & echo $$! > capture.PID
endif

stop_dag:
	@echo "Stopping DAG Capture..."
ifeq ($(USE_DAG), 1)
	sleep 1
	kill `cat capture.PID`
	rm capture.PID
	sleep 5
endif

analyse_dag:
ifeq ($(USE_DAG),1)
	$(CAMIO_PATH)/dag_analyse_join/bin/dag_analyse -i /tmp/dag_cap_B -I /tmp/dag_cap_A -o $(DAG_OUTPUT)$(I) -f $(SKIP) -F $(SKIP) -l $(RECORDS) -c
endif


clean:
	rm -f *$(OUTPUT_FILE) 
	rm -f source/*.o source/icmp_test
build:
	gcc source/icmp_test.c -o source/icmp_test
