#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2010 OpenWrt.org

START=18

#set command line in uboot
#dpdk_bootargs=isolcpus=2-3 rcu_nocbs=2-3 nohz_full=2-3

local sdk_so="libipoque_pace2.so"
local sdk_so_version=`uci -c /etc/profile.d  get profile.@global[0].dpi_sdk_version`
local sdk_path="/tmp/database/database/$sdk_so"

board_info()
{
	model_name=$(cat /tmp/devinfo/model_name | sed 's/ /_/g')
}
board_info

cpulist_to_mask()
{
	cmask=0
	for clist in ${1//,/ }
	do
		start=${clist%%-*}
		end=${clist##*-}
		if [ "$start" -eq "$end" ];
		then
			cmask=$(($cmask | (1<<$start)))
		else
			for cpu in `seq $start $end`
			do
				cmask=$(($cmask | (1<<$cpu)))
			done
		fi
	done
	echo -n $cmask
}

system_setup()
{
	cat /proc/mounts | grep -q hugetlbfs || \
		$(mkdir -p /mnt/huge; mount -t hugetlbfs nodev /mnt/huge)
	echo 1024 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
	
	isolcpus=$(cat /sys/devices/system/cpu/isolated)
	[ -n "$isolcpus" ] && {
		clear_mask=$(cpulist_to_mask $isolcpus) #Isolate CPU1 to CPU3 from IRQs
		for i in /proc/irq/*/smp_affinity
		do
		  echo $(printf %x $(( 0x$(cat $i) & ~$clear_mask ))) > $i
		done
		
		oncpus=$(cat /sys/devices/system/cpu/online)
		online_mask=$(cpulist_to_mask $oncpus)
		kernel_mask=$(printf %x $(($online_mask ^ $clear_mask)))
		echo ${kernel_mask} > /sys/bus/workqueue/devices/writeback/cpumask
	}
}

system_destroy()
{
	isolcpus=$(cat /sys/devices/system/cpu/isolated)
	[ -n "$isolcpus" ] && {
		clear_mask=$(cpulist_to_mask $isolcpus) #Isolate CPU1 to CPU3 from IRQs
		for i in /proc/irq/*/smp_affinity
		do
		  echo $(printf %x $(( 0x$(cat $i) | $clear_mask ))) > $i
		done
		
		oncpus=$(cat /sys/devices/system/cpu/online)
		kernel_mask=$(printf %x $(cpulist_to_mask $oncpus))
		echo ${kernel_mask} > /sys/bus/workqueue/devices/writeback/cpumask
	}
	echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
	umount /mnt/huge
}

start() {
	local ct_max=`uci -c /etc/profile.d get profile.@system_params[0].nf_conntrack_max`
	local ipstat_entries_max=`uci -q get natfwd_ipstat.setting.ipstat_entries_max`
	local nat_ipstat_max=`uci -c /etc/profile.d get profile.@upnp[0].upnp_mapping_max`
	local ipsec_tunnel_max=`uci -c /etc/profile.d get profile.@ipsec[0].tunnel_limits`
	local openvpn_servlist_max=`uci -c /etc/profile.d get profile.@openvpn[0].server_list_max`
	local openvpn_clilist_max=`uci -c /etc/profile.d get profile.@openvpn[0].client_list_max`
	local openvpn_tunnel_max=110
	local sslvpn_tunnel_max=`uci -c /etc/profile.d get profile.@sslvpn[0].tunnel_max`
	local crypto_symsession_max=4096
	local used_tx_queue_num=`uci -c /etc/profile.d get profile.@dpdk_para[0].used_tx_queue_num`
	local switch_eth_port_id=`uci -c /etc/profile.d get profile.@dpdk_para[0].switch_eth_port_id`

	openvpn_tunnel_max=$((10*$openvpn_servlist_max+$openvpn_clilist_max))
	# (1) for every lcore, every tunnel has 4 sessions when updating.
	crypto_symsession_max=$((4*$openvpn_tunnel_max + 4*$ipsec_tunnel_max + 4*$sslvpn_tunnel_max))
	
	# (2) there are 3 lcores, 
	crypto_symsession_max=$((3*$crypto_symsession_max))

	system_setup
	case ${model_name} in
		"ER8411" | "ER8411C-M2" | "G611")
			# ethernet
			insmod uio_pdrv_genirq.ko of_id="generic-uio"
			insmod musdk_cma.ko
			insmod mv_dmax2_uio.ko
			# crypto
			insmod crypto_safexcel.ko rings=0,0 # kernel => dpdk
			insmod mv_sam_uio.ko
			# dpdk ko
			insmod rte_kni.ko kthread_mode="multiple" carrier="on"
			# kctrl
			insmod kctrl.ko dev_names="kern0,kern1" bridge_dev="inf" msg_dev="kern0" offload_at_pkts=0 l_ipstat_entries_max=${ipstat_entries_max}
			
			#ethtool -K eth1 receive-hashing on
			#ethtool -X eth1 weight 1 1 0 0

			#���/׼�� ������SDK����
			[ -f "$sdk_path" ] && {
				ln -sf $sdk_path /usr/lib/$sdk_so.$sdk_so_version
			}

			if [ -e /sys/class/net/dpdk3 ];then
				# hw loop mode
				natfwd --proc-type=primary --vdev=net_mvpp2,iface=dpdk0,iface=dpdk1,iface=dpdk2,iface=dpdk3 --vdev=crypto_mvsam,max_nb_sessions=${crypto_symsession_max} --vdev=crypto_openssl -c e --master-lcore 1 --log-level user1:1 -- -P -p f --ring 1 --master-ring r0 --config="(t0,0,1,0),(2,0,1,0),(2,1,2,0),(2,2,3,0),(0,0,2,2),(0,1,3,2),(3,0,2,1),(3,1,3,1),(1,0,2,3),(1,1,3,3)" --devmap "(0,2,inf),(1,3,kern1)" --txd 2048 --rxd 2048 --hash-entry-num ${ct_max} --ipstat-entries-max ${ipstat_entries_max} --nat-ipstat-index-max ${nat_ipstat_max} --ipsec-tunnel-max ${ipsec_tunnel_max} --crypto-symsess-max ${crypto_symsession_max} --used-tx-queue-num ${used_tx_queue_num} --switch-eth-port-id ${switch_eth_port_id} &
			else
				# kni mode
				natfwd --vdev=net_mvpp2,iface=dpdk0,iface=dpdk1 --vdev=crypto_mvsam,max_nb_sessions=${crypto_symsession_max} --vdev=crypto_openssl -c c --master-lcore 2 --log-level user1:1 -- -P -p 3 --kni 3 --config="(k0,0,2,0),(0,0,2,k0),(0,1,3,k0),(k1,0,2,1),(k1,1,3,1),(1,0,2,k1),(1,1,3,k1)" --devmap "(0,k0,kern0),(1,k1,kern1)" --txd 2048 --rxd 2048 --hash-entry-num ${ct_max} --ipstat-entries-max ${ipstat_entries_max} --nat-ipstat-index-max ${nat_ipstat_max} --ipsec-tunnel-max ${ipsec_tunnel_max} --crypto-symsess-max ${crypto_symsession_max} --used-tx-queue-num ${used_tx_queue_num} --switch-eth-port-id ${switch_eth_port_id}&
				while ! ifconfig kern0 up; do sleep 1; done
				while ! ifconfig kern1 up; do sleep 1; done
				ifconfig kern0 promisc
				ifconfig kern1 promisc
				echo 3 > /sys/class/net/kern0/queues/rx-0/rps_cpus
				echo 3 > /sys/class/net/kern1/queues/rx-0/rps_cpus	
			fi
			# disable oom-killer
			echo -1000 > /proc/$(pidof natfwd)/oom_score_adj
		;;
		*)
		;;
	esac
}

stop() {
	killall natfwd
	case ${model_name} in
		"ER8411" | "ER8411C-M2" | "G611")
			# kctrl
			rmmod kctrl.ko
			# dpdk ko
			rmmod rte_kni.ko
			# crypto
			rmmod mv_sam_uio.ko
			rmmod crypto_safexcel.ko rings=0,0
			insmod crypto_safexcel.ko # dpdk => kernel
			# ethernet
			rmmod mv_dmax2_uio.ko
			rmmod musdk_cma.ko
			rmmod uio_pdrv_genirq.ko
			
			#ethtool -K eth1 receive-hashing off
			#ethtool -X eth1 weight 1 1 1 1
		;;
		*)
		;;
	esac
	system_destroy
}
