#!/bin/bash
#bond3tool new [ dev bondX ] [ slave1 ethx ] [slave2 ethx] [slave3 ethx] [ip x.x.x.x] [netmask x.x.x.x] [gateway x.x.x.x]
#bond3tool del [ dev bondX ]
#bond3tool latency  [ dev bondX ] [ ip x.x.x.x ] [ interval x ] [ rtt x ] [ rtt_dif x ]
#bond3tool set [ dev bondX ] [ cycle_time 100 ] [ switch_cycle 2 ] [ delay_cycle 1 ] [ gateway x.x.x.x ]
#bond3tool switch [ dev bondX ] [ ip 192.168.10.1 ] [ old_duty_line ethX1 ] duty_line ethX2
#bond3tool config [ dev bondX ]
#bond3tool show [ dev bondX ] [ interval n ] [ link 1|2|3|1,2 ] [ duty_line eth0 ] [ backup_line eth0 ] [ ip x.x.x.x ] [time] [bandwidth]

queue_len=900000
time_flag=0
bandwidth_flag=0
time_title="4 8 12"
bandwidth_title="5 9 13"
slave1_title="2 3"
slave2_title="6 7"
slave3_title="10 11"
pcap_demo_path="/usr/bin/bond3tool_pcap_demo"
default_name="bond3+"

function usage() {
echo " 
bond3tool V1.2
usage:
	bond3tool new [ dev bondX ] [ slave1 ethx ] [slave2 ethx] [slave3 ethx] [ip x.x.x.x] [netmask x.x.x.x] [gateway x.x.x.x]
	bond3tool del [ dev bondX ]
	bond3tool latency  [ dev bondX ] [ ip x.x.x.x ] [ interval x ] [ rtt x ] [ rtt_dif x ]
	bond3tool set [ dev bondX ] [ cycle_time 100 ] [ switch_cycle 2 ] [ delay_cycle 1 ] [ gateway x.x.x.x ]
	bond3tool switch [ dev bondX ] [ ip 192.168.10.1 ] [ old_duty_line ethX1 ] duty_line ethX2
	bond3tool config [ dev bondX ]
	bond3tool show [ dev bondX ] [ interval n ] [ link 1|2|3|1,2 ] [ duty_line eth0 ] [ backup_line eth0 ] [ ip x.x.x.x ] [time] [bandwidth]
"
}

function bond3_judge_parameter_exist(){	
	temp=`cat $2|grep $1`
	if [ "$temp" != "" ]; then
	        echo 1
	else
	        echo 0
	fi
}

function bond3_sysctl_file_check() {
        path=/etc/sysctl.conf
        i=` bond3_judge_parameter_exist $1 $path`
        if [ $i == "1" ]; then
                sed -i '/'$1'*/c '$1' = '$2' '$3' '$4'' $path
        else
                cat<<EOF>> $path
$1 = $2
EOF
        fi
}


function bond3_sysctl() {
	
	sysctl kernel.shmall=429496729600 > /dev/null

	
	sysctl net.ipv4.udp_mem="26214400 32768000 39321600" > /dev/null
	sysctl net.core.rmem_max=335544320 > /dev/null
	sysctl net.core.wmem_max=335544320 > /dev/null
	sysctl net.core.rmem_default=200000000 > /dev/null
	sysctl net.core.wmem_default=200000000 > /dev/null

	
	sysctl net.core.netdev_max_backlog=200000 > /dev/null
	sysctl net.ipv4.ipfrag_high_thresh=1819430400 > /dev/null
	sysctl net.ipv4.ipfrag_low_thresh=1614572800 > /dev/null

#	bond3_sysctl_file_check kernel.shmall 429496729600
#	bond3_sysctl_file_check net.ipv4.udp_mem 26214400 32768000 39321600
#	bond3_sysctl_file_check net.core.rmem_max 335544320
#	bond3_sysctl_file_check net.core.wmem_max 335544320
#	bond3_sysctl_file_check net.core.rmem_default 200000000
#	bond3_sysctl_file_check net.core.wmem_default 200000000
#	bond3_sysctl_file_check net.core.netdev_max_backlog 200000
#	bond3_sysctl_file_check net.ipv4.ipfrag_high_thresh 1819430400
#	bond3_sysctl_file_check net.ipv4.ipfrag_low_thresh 1614572800
}

function show_link_list() {
	name=$default_name
	flag=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
	mode=0
	read -a title1 <<< "------------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- ---------------"
	read -a title2 <<< "Remote_IP Active_Line SLAVE_1 STATE LAST_ACTIVE BANDWIDTH SLAVE_2 STATE LAST_ACTIVE BANDWIDTH SLAVE_3 STATE LAST_ACTIVE BANDWIDTH"


	while [ $# -gt 0 ]; do
		case $1 in
			dev) name=$2; shift;shift;;
			duty_line) duty_line=$2; shift;shift;;
			backup_line) backup_line=$2; shift;shift;;
			ip) ip=$2; shift;shift;;
			type) type=$2; shift;shift;;
			link) list=$2; shift;shift;;
			interval) interval=$2; shift;shift;;
			[0-9]*) flag[$1]=1; shift;;
			time) time_flag=1; shift;;
			bandwidth) bandwidth_flag=1; shift;;
			*)  shift;;
		esac
	done

	if [ "$time_flag" != "1" ]; then
		flag[4]=1
		flag[8]=1
		flag[12]=1
	fi

	if [ "$bandwidth_flag" != "1" ]; then
		flag[5]=1
		flag[9]=1
		flag[13]=1
	fi
	
	mode=$((${flag[2]}*100))
	mode=$(($mode+${flag[6]}*10))
	mode=$(($mode+${flag[10]}))
	
	case $mode in
		1)flag[12]=1;flag[13]=1; ;;
		10)flag[6]=0 ;flag[7]=0 ;;
		11)flag[8]=1 ;flag[9]=1 ;flag[12]=1;flag[13]=1;;
		100)flag[2]=0 ;flag[3]=0 ;;
		101)flag[2]=0 ;flag[3]=0 ;flag[12]=1;flag[13]=1;;
		110)flag[2]=0 ;flag[3]=0 ;flag[6]=0 ;flag[7]=0 ;;
		111)return;;
		*);;
	esac

	for((i=0;i<=13;i++))
	do
		if [ "${flag[$i]}" != "1" ]; then
	        echo -n ${title1[$i]}
	    fi
	done
	echo

	printf "%-18s" ${title2[0]}
	for((i=1;i<=13;i++))
	do
		if [ "${flag[$i]}" != "1" ]; then
	        printf "%-15s" ${title2[$i]}
	    fi
	done
	echo

	for((i=0;i<=13;i++))
	do
		if [ "${flag[$i]}" != "1" ]; then
	        echo -n ${title1[$i]}
	    fi
	done
	echo

	cat /proc/net/bonding_debug/${name}_link_state_table | while read line
	do
		if [[ $line =~ "---------" ]]; then
			continue
		fi
		if [[ $line =~ "Remote IP" ]]; then
			continue
		fi
		
		IFS=' '
		read -a strarr <<< "$line"

		i_ip=${strarr[0]}
		i_duty_line=${strarr[1]}
		
		i_slave1_name=${strarr[2]}
		i_slave1_state=${strarr[3]}
		i_slave1_last_active=${strarr[4]}
		
		i_slave2_name=${strarr[6]}
		i_slave2_state=${strarr[7]}
		i_slave2_last_active=${strarr[8]}	
		
		i_slave3_name=${strarr[10]}
		i_slave3_state=${strarr[11]}
		i_slave3_last_active=${strarr[12]}			

		
		if [ "$ip" ] && [[ ! ${i_ip} =~ $ip ]]; then
			continue
		fi

		if [ "$duty_line" ] && [[ $duty_line != ${i_duty_line} ]]; then
			continue
		fi
		
		if [ "$backup_line" ] && [[ ${i_duty_line} == $backup_line ]]; then
			continue
		fi
		
		if [ "$backup_line" ] && [[ ${i_slave1_name} != ${backup_line} ]] \
		&& [[ ${i_slave2_name} != ${backup_line} ]] && [[ ${i_slave3_name} != ${backup_line} ]]; then
			continue
		fi
		
		if [ "$type" ] && [ ! "${i_slave2_state}" ] ; then
			continue
		fi
		
		if [ "$type" ] && [ ${i_slave1_state} == ${i_slave2_state} ] ; then
			continue
		fi

		case $mode in
		0);;
		1)
			if [ "$i_slave3_name" != "" ]; then
				continue
			fi
			;;
		10)
			if [ "$i_slave2_name" != "" ] && [ "$i_slave3_name" == "" ]; then
				continue
			fi
			;;
		11)
			if [ "$i_slave2_name" != "" ]; then
				continue
			fi
			;;
		100)
			if [ "$i_slave2_name" == "" ]; then
				continue
			fi
			;;
		101)
			if [ "$i_slave2_name" == "" ]; then
				continue
			fi

			if [ "$i_slave3_name" != "" ]; then
				continue
			fi
			;;
		110)
			if [ "$i_slave3_name" == "" ]; then
				continue
			fi
			;;
		esac

		printf "%-18s" ${strarr[0]}
		for((i=1;i<=13;i++))
		do
			if [ "${flag[$i]}" != "1" ]; then
		        printf "%-15s" ${strarr[$i]}
		    fi
		done
		echo
	
	done

	for((i=0;i<=13;i++))
	do
		if [ "${flag[$i]}" != "1" ]; then
	        echo -n ${title1[$i]}
	    fi
	done
	echo
}

function bond3_show_link() {
	name=$default_name
	args=$@
	while [ $# -gt 0 ]; do
		case $1 in
			dev) name=$2; shift;;
			duty_line) duty_line=$2; shift;;
			backup_line) backup_line=$2; shift;;
			ip) ip=$2; shift;;
			type) type=$2; shift;;
			link) list=$2; shift;;
			interval) interval=$2; shift;;
			time) time_flag=1;;
			bandwidth) bandwidth_flag=1;;
			*)  usage; exit;;
		esac
		shift
	done
	
	if [ ! -f "/proc/net/bonding/$name" ]; then
		echo "device $name does not exist!"
		return
	fi
	
	slaves=`cat /proc/net/bonding/$name | grep "Slave Interface" | wc -l`
	
	if [[ slaves -eq 0 ]]; then
		echo "bond device has no slaves!"
		return
	fi
	
	if [[ $slaves -eq 1 ]]; then
		if [[ "$list" == "1,2" ]] || [[ "$list" == "2,1" ]] || [[ "$list" == "2" ]] || [[ "$list" == "3" ]]; then
			echo "bond device doesn't have slave 2!"
			return
		fi
	fi
	
	if [[ $slaves -eq 2 ]] && [[ $list == 3 ]]; then
		echo "bond device doesn't have slave 3!"
		return
	fi

	if [[ "$list" == "3,1" ]] || [[ "$list" == "1,3" ]]; then
		show_link_list $args $slave2_title 
		return
	fi
	
	if [[ "$list" == "3,2" ]] || [[ "$list" == "2,3" ]]; then
		show_link_list $args $slave1_title 
		return
	fi
	
	if [[ "$list" == "1,2" ]] || [[ "$list" == "2,1" ]]; then
		show_link_list $args $slave3_title
		return
	fi	

	if [[ "$list" == "1,2,3" ]] || [[ "$list" == "2,1,3" ]] || [[ "$list" == "3,1,2" ]] || [[ "$list" == "3,2,1" ]] || [[ "$list" == "1,3,2" ]] || [[ "$list" == "2,3,1" ]]; then
		show_link_list $args
		return
	fi
	
	while [ 1 ]
	do
		if [[ $list == 1 ]]; then
			show_link_list $args $slave3_title $slave2_title
		elif [[ $list == 2 ]] ; then
			show_link_list $args $slave1_title $slave3_title
		elif [[ $list == 3 ]]; then
			show_link_list $args $slave1_title $slave2_title
		elif [ "$list" == "" ]; then	
			if  [[ $slaves -eq 1 ]]; then
				show_link_list $args $slave3_title $slave2_title
			elif [[ $slaves -eq 2 ]]; then
				show_link_list $args $slave3_title
			else
				show_link_list $args
			fi
		else
			echo "show link parameter error"
		fi
		
		if [ $interval ]; then
			sleep $interval
		else
			break
		fi
	done
}

function bond3_config_show() {
        name=$1

        if [ ! -f "/proc/net/bonding/$name" ]; then
                echo "device $name does not exist!"
                return
        fi

        slave1=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==1 {print $3}'`
        slave2=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==2 {print $3}'`
        slave3=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==3 {print $3}'`

        ip=`ip address show dev $name | grep inet | grep -v inet6 | awk '{print $2}' | awk -F '/' '{print $1}'`
        cycle_time=`cat /sys/devices/virtual/net/$name/bonding/cycle_time`
        switch_cycle=`cat /sys/devices/virtual/net/$name/bonding/switch_cycle`
        delay_cycle=`cat /sys/devices/virtual/net/$name/bonding/delay_cycle`
        gateway_ip=`cat /sys/devices/virtual/net/$name/bonding/gateway_ip`
        gateway_mac_line1=`cat /sys/devices/virtual/net/$name/bonding/gateway_mac | tail -n +1 | head -n 1`
        gateway_mac_line2=`cat /sys/devices/virtual/net/$name/bonding/gateway_mac | tail -n +2 | head -n 1`
        gateway_mac_line3=`cat /sys/devices/virtual/net/$name/bonding/gateway_mac | tail -n +3 | head -n 1`

		if [ -f /sys/devices/virtual/net/$name/bonding/enhanced_bond3 ] 
		then 
				enhanced=`cat /sys/devices/virtual/net/$name/bonding/enhanced_bond3`
		    	
		else
		   		cat /proc/net/bonding/$name
		   		exit
		fi
        
        echo -------------------------------------------------------
        echo
        echo name: $name
        echo
        echo slaves:
        if [ "$slave1" ]; then
                echo "    slave1: $slave1"
        fi
        if [ "$slave2" ]; then
                echo "    slave2: $slave2"
        fi
        if [ "$slave3" ]; then
                echo "    slave3: $slave3"
        fi
        echo
        echo enhanced: $enhanced
        echo
        echo ip: $ip
        echo
        echo cycle_time: $cycle_time
        echo
        echo switch_cycle: $switch_cycle
        echo
        echo delay_cycle: $delay_cycle
        echo
        echo gateway_ip: $gateway_ip
        echo
    	echo "gateway_mac: $gateway_mac_line1"
		echo "             $gateway_mac_line2"
		echo "             $gateway_mac_line3"
        echo
        echo -------------------------------------------------------
}

function bond3_config_link() {
        read -a name <<< `ls /proc/net/bonding`
        while [ $# -gt 0 ]; do
                case $1 in
                        "") shift;;
                        dev) name=$2; bond3_config_show $name; exit;;
                        *)  usage; exit;;
                esac
                shift
        done

        i=0
        while [ 1 ]
        do
                if [ "${name[$i]}" == "" ]; then
                        break
                fi
                if [ -f /sys/devices/virtual/net/$name/bonding/enhanced_bond3 ]
				then
						bond3_config_show ${name[$i]}
						let i+=1
                fi
        done
}
function bond3_switch_duty_line() {
	name=$default_name
	
	while [ $# -gt 0 ]; do
#		echo "processing option: $1"
		case $1 in
			dev) name=$2; shift;;
			ip) ip=$2; shift;;
			old_duty_line) old_duty_line=$2; shift;;
			duty_line) duty_line=$2; shift;;
			*)  usage; exit;;
		esac
		shift
	done
	
	if [[ -z duty_line ]]; then
		usage; exit;
	fi
	
	if [ "$ip" ]; then
		
		echo $ip $duty_line > /sys/devices/virtual/net/$name/bonding/switch_duty_line 2>&1
		if [ $? -ne 0 ]; then
			echo switch failed, please check parameters[ ip:$ip duty_line:$duty_line ]
		else
			echo "Success to switch duty line to $duty_line with option ip = $ip"
		fi
		
	elif [ "$old_duty_line" ]; then
		echo $old_duty_line $duty_line > /sys/devices/virtual/net/$name/bonding/switch_duty_line 2>&1
		if [ $? -ne 0 ]; then
			echo switch failed, please check parameters[ old_duty_line:$old_duty_line duty_line:$duty_line ]
		else
			echo "Success to switch duty line from $old_duty_line to $duty_line"
		fi
	else

		echo $duty_line > /sys/devices/virtual/net/$name/bonding/switch_duty_line 2>&1
		if [ $? -ne 0 ]; then
			echo switch failed, please check parameters[ duty_line:$duty_line ]
		else
			echo "Success to switch duty line to $duty_line"
		fi
		
	fi
}

function bond3_set_link() {
	name=$default_name
	
	while [ $# -gt 0 ]; do
#		echo "processing option: $1"
		case $1 in
			dev) name=$2; shift;;
			cycle_time) cycle_time=$2; shift;;
			switch_cycle) switch_cycle=$2; shift;;
			delay_cycle) delay_cycle=$2; shift;;
			gateway) gateway=$2; shift;;
			*)  usage; exit;;
		esac
		shift
	done
	
	if [ "$cycle_time" ]; then
		echo "set cycle_time to $cycle_time"
		echo $cycle_time > /sys/devices/virtual/net/$name/bonding/cycle_time
	fi

	if [ "$delay_cycle" ]; then
		echo "set delay_cycle to $delay_cycle"
		echo $delay_cycle > /sys/devices/virtual/net/$name/bonding/delay_cycle
	fi
	
	if [ "$switch_cycle" ]; then
		echo "set switch_cycle to $switch_cycle"
		echo $switch_cycle > /sys/devices/virtual/net/$name/bonding/switch_cycle
	fi
	
	if [ "$gateway" ]; then
		echo "set gateway to $gateway"
		echo $gateway > /sys/devices/virtual/net/$name/bonding/gateway_ip
	fi	
}

function bond3_slave_txqueuelen() {
        path=/etc/rc.d/rc.local
        i=`bond3_judge_parameter_exist $1 $path`
        if [ $i == "1" ]; then
                sed -i '/'$1'*/c ifconfig '$1' txqueuelen 900000' $path
        else
                cat<<EOF>> $path
ifconfig $1 txqueuelen 900000
EOF
        fi
}

function bond3_del_link() {
	name=$default_name
	force_flag=0	
#			force) force_flag=1; shift;;
	while [ $# -gt 0 ]; do
		case $1 in
			dev) name=$2; shift;;
			*)  usage; exit;;
		esac
		shift
	done
	
	if [ ! -n $name ]; then
		exit
	fi
	ls /etc/sysconfig/network-scripts/ifcfg-$name 1>/dev/null 2>/dev/null

	if [ $? != 0 ];then
		echo "dev $name is not exist"
		exit
	fi
	slave1=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==1 {print $3}'`
    	slave2=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==2 {print $3}'`
    	slave3=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==3 {print $3}'`
	
#	if [ "$force_flag" == "1" ]; then
#		rm -f /etc/sysconfig/network-scripts/ifcfg-$name
#		rm -f /etc/sysconfig/network-scripts/ifcfg-$slave1
#		rm -f /etc/sysconfig/network-scripts/ifcfg-$slave2
#		rm -f /etc/sysconfig/network-scripts/ifcfg-$slave3
#		return	
#	fi
	rm -f /etc/sysconfig/network-scripts/ifcfg-$name
	rm -f /etc/sysconfig/network-scripts/ifcfg-$slave1
	rm -f /etc/sysconfig/network-scripts/ifcfg-$slave2
	rm -f /etc/sysconfig/network-scripts/ifcfg-$slave3
	ip link del dev $name
	echo "success to clean configuration file of $name!"
}

function bond3_new_link() {
	name=$default_name
	ip=""
	netmask="255.255.255.0"
	gateway="0.0.0.0"

	while [ $# -gt 0 ]; do
#		echo "processing option: $1"
		case $1 in
			dev) name=$2; shift;;
			ip) ip=$2; shift;;
			netmask) netmask=$2; shift;;
			gateway) gateway=$2; shift;;
			slave1) slave1=$2; shift ;;
			slave2) slave2=$2; shift ;;
			slave3) slave3=$2; shift ;;
			*)  usage; exit;;
		esac
		shift
	done

rm -rf  /etc/sysconfig/network-scripts/ifcfg-$name 2>/dev/null

	cat<<EOF>> /etc/sysconfig/network-scripts/ifcfg-$name
NAME=$name
DEVICE=$name
ONBOOT=yes
TYPE=Bond
BONDING_MASTER=yes
NETMASK=$netmask
GATEWAY=$gateway
BONDING_OPTS="mode=3 miimon=100"
EOF
	if [ "$ip" != "" ]; then
		cat<<EOF>> /etc/sysconfig/network-scripts/ifcfg-$name
IPADDR=$ip
EOF
	fi
	
	if [ $? -eq 0 ]; then
		#add slaves to master
		if [ "$slave1" ]; then
			if [ `ifconfig $slave1 |grep "ether"| awk '{print $2}'` ]; then
				slave1HDaddr=` ifconfig $slave1 |grep "ether"| awk '{print $2}' `
				rm -f /etc/sysconfig/network-scripts/ifcfg-$slave1
				cat<<EOF>> /etc/sysconfig/network-scripts/ifcfg-$slave1
TYPE=Ethernet
NAME=$slave1
DEVICE=$slave1
MASTER=$name
SLAVE=yes
EOF
				ifconfig $slave1 txqueuelen 900000
				#bond3_slave_txqueuelen $slave1
			else
				echo $slave1 not exit!
			fi

		fi
		if [ "$slave2" ]; then
			if [ `ifconfig $slave2 |grep "ether"| awk '{print $2}'` ]; then
				slave2HDaddr=` ifconfig $slave2 |grep "ether"| awk '{print $2}' `
				rm -f /etc/sysconfig/network-scripts/ifcfg-$slave2
				cat<<EOF>> /etc/sysconfig/network-scripts/ifcfg-$slave2
TYPE=Ethernet
NAME=$slave2
DEVICE=$slave2
MASTER=$name
SLAVE=yes
EOF
				ifconfig $slave2 txqueuelen 900000
				#bond3_slave_txqueuelen $slave2
			else
				echo $slave2 not exit!
			fi
		fi
		if [ "$slave3" ]; then
			if [ `ifconfig $slave3 |grep "ether"| awk '{print $2}'` ]; then
				slave3HDaddr=` ifconfig $slave3 |grep "ether"| awk '{print $2}' `
				rm -f /etc/sysconfig/network-scripts/ifcfg-$slave3
				cat<<EOF>> /etc/sysconfig/network-scripts/ifcfg-$slave3
TYPE=Ethernet
NAME=$slave3
DEVICE=$slave3
MASTER=$name
SLAVE=yes
EOF
				ifconfig $slave3 txqueuelen 900000
				#bond3_slave_txqueuelen $slave3
			else
				echo $slave3 not exit!
			fi
		fi

		
		#enable the bond device
		rmmod bonding

		echo "options bonding gateway_ip=$gateway" > /etc/modprobe.d/bonding.conf

		systemctl restart NetworkManager
				
		echo "success to create $name!"
	fi
}

function check_mod() {
	name=""
	if [ "$1" == "" ]; then
		name=$default_name
	else
		name=$1
	fi	

	if [ ! -f "/proc/net/bonding/$name" ]; then
		echo file /proc/net/bonding/$name not exist !
		return 1
	fi
	
	if [ `cat /proc/net/bonding/$name|grep Bonding|grep Mode:|awk 'NR==1 {print $3}'` != "fault-tolerance" ]; then
		echo "-----------------------------------------------------------"
		echo "/proc/net/bonding/$name :"
		cat /proc/net/bonding/$name
		echo
		echo "Error !! "
		echo "Bonding Mode is not fault-tolerance (broadcast)"
		echo
	fi
} 


function bond3_latency_link() {
	name=$default_name
	interval=1
	rtt=1000
	rtt_dif=100
	while [ $# -gt 0 ]; do
		case $1 in
			dev) name=$2; shift;;
			ip) ip=$2; shift;;
			interval) interval=$2; shift;;
			rtt) rtt=$2; shift;;
			rtt_dif) rtt_dif=$2; shift;;
			*)  usage; exit;;
		esac
		shift
	done

	cat /proc/net/bonding/$name >/dev/null
	if [ $? != 0 ]; then
			echo dev $name no found
			exit
	fi

	slave_num=`cat /proc/net/bonding/$name | grep "Slave Interface" | wc -l`
	slave1=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==1 {print $3}'`
    slave2=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==2 {print $3}'`
    slave3=` cat /proc/net/bonding/$name | grep "Slave Interface" | awk 'NR==3 {print $3}'`

	if [ "$ip" ]; then
		$pcap_demo_path $slave_num $interval $rtt $rtt_dif $name $ip $slave1 $slave2 $slave3
		exit
	fi
	$pcap_demo_path $slave_num $interval $rtt $rtt_dif $name
	
}

function main() {
	if [ $# -lt 1 ]; then
		usage
		exit
	fi

#	bond3_sysctl
	
	func=$1
	shift

	if [ "$func" != "new" ]; then
		if [ "$func" != "config" ]; then
			check_mod $2
		fi
	fi

	case $func in
		new) bond3_new_link $@;;
		del) bond3_del_link $@;;
		set) bond3_set_link $@;;
		switch) bond3_switch_duty_line $@;;
		config) bond3_config_link $@;;
		show) bond3_show_link $@;;
		latency) bond3_latency_link $@;;
		*) usage ;;
	esac
}

main $@
