#!/bin/bash # # marsiod startup marsio zerocopy driver daemon # # chkconfig: 235 10 10 # description: marsio zero copy driver . /etc/rc.d/init.d/functions . /etc/mrenv.conf if [ -z "$DPDK_ROOT" ]; then DPDK_ROOT="/opt/iiesoft/dpdk" fi if [ -z "$MARSIO_ROOT" ]; then MARSIO_ROOT="/opt/iiesoft/marsio" fi if [ -z "$HUGEPAGE_NUM_2M" ]; then HUGEPAGE_NUM_2M=4096 fi if [ -z "$PCI_ADDR_IXGBE" ]; then PCI_ADDR_IXGBE="" fi if [ -z "$PCI_ADDR_IGB" ]; then PCI_ADDR_IGB="" fi MODULE_PATH=$DPDK_ROOT/lib/modules/$(uname -r)/extra/dpdk DAEMOH_PATH=$MARSIO_ROOT/bin/ KNI_MODULE=rte_kni.ko UIO_MODULE=igb_uio.ko NR_HUGEPAGE_FILE_2M=/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages NR_HUGEPAGE_FILE_1G=/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages HUGEPAGE_PATH_2M=/mnt/.huge_2M HUGEPAGE_PATH_1G=/mnt/.huge_1G # Execuate File path DAEMON=$DAEMOH_PATH/zcpd NICTOOL=$DPDK_ROOT/sbin/dpdk_nic_bind DAEMON_START=$MARSIO_ROOT/bin/mrstart DAEMON_STOP=$MARSIO_ROOT/bin/mrstop function module_unload_uio() { lsmod | grep -s igb_uio > /dev/null 2>&1 [ $? -eq 0 ] && rmmod igb_uio > /dev/null 2>&1 || return 0 [ ! $? -eq 0 ] && return 1 return 0 } function module_unload_kni() { lsmod | grep -s rte_kni > /dev/null 2>&1 [ $? -eq 0 ] && rmmod rte_kni > /dev/null 2>&1 || return 0 [ ! $? -eq 0 ] && return 1 return 0 } function module_load_kni() { if [ ! -e $MODULE_PATH/$KNI_MODULE ]; then echo -n "$KNI_MODULE does not existed. Check DPDK is installed or not. " return 1 fi module_unload_kni insmod $MODULE_PATH/$KNI_MODULE > /dev/null 2>&1 [ ! $? -eq 0 ] && echo -n "$KNI_MODULE load failure." && return 1 return 0 } function module_load_uio() { if [ ! -e $MODULE_PATH/$UIO_MODULE ]; then echo -n "$UIO_MODULE does not existed. Check DPDK is installed or not. " return 1 fi module_unload_uio modprobe uio &> /dev/null insmod $MODULE_PATH/$UIO_MODULE [ ! $? -eq 0 ] && echo -n "$UIO_MODULE load failure." && return 1 return 0 } function huge_release_1G() { cat /proc/mounts | grep "$HUGEPAGE_PATH_1G" > /dev/null grep -s "$HUGEPAGE_PATH_1G" /proc/mounts > /dev/null if [ $? -eq 0 ]; then umount $HUGEPAGE_PATH_1G > /dev/null [ ! $? -eq 0 ] && echo "umount 1G hugepage failure, maybe in use" && return 1 fi if [ -d $HUGEPAGE_PATH_1G ] ; then rm -R $HUGEPAGE_PATH_1G [ ! $? $HUGEPAGE_PATH_2M ] && echo "Cannot delete 2M hugepage" && return 1 fi return 0 } function huge_release_2M() { cat /proc/mounts | grep "$HUGEPAGE_PATH_2M" > /dev/null grep -s "$HUGEPAGE_PATH_2M" /proc/mounts > /dev/null if [ $? -eq 0 ]; then umount $HUGEPAGE_PATH_2M > /dev/null [ ! $? -eq 0 ] && echo "umount 2M hugepage failure, maybe in use" && return 1 fi if [ -d $HUGEPAGE_PATH_2M ] ; then rm -R $HUGEPAGE_PATH_2M [ ! $? -eq 0 ] && echo "Cannot delete 2M hugepage" && return 1 fi if [ -e $NR_HUGEPAGE_FILE_2M ]; then echo -n 0 > $NR_HUGEPAGE_FILE_2M fi return 0 } function huge_alloc_1G() { if [ ! -e $NR_HUGEPAGE_FILE_1G ]; then return 1 fi SIZE_1G=$(cat $NR_HUGEPAGE_FILE_1G) if [ ! $SIZE_1G -gt 0 ]; then echo -e "\tHugepage 1G has $SIZE_1G nr_hugepages. Please set \"hugepagesz=1G hugepages=4\" in /boot/grub/grub.conf." return 1 fi mkdir -p $HUGEPAGE_PATH_1G mount -t hugetlbfs nodev -o pagesize=1G $HUGEPAGE_PATH_1G [ $? -eq 0 ] && return 0 return 1 } function huge_alloc_2M() { if [ ! -e $NR_HUGEPAGE_FILE_2M ]; then return 1 fi SIZE_2M=$(cat $NR_HUGEPAGE_FILE_2M) [ $SIZE_2M -eq 0 ] && echo -n $HUGEPAGE_NUM_2M > $NR_HUGEPAGE_FILE_2M mkdir -p $HUGEPAGE_PATH_2M mount -t hugetlbfs nodev $HUGEPAGE_PATH_2M [ $? -eq 0 ] && return 0 return 1 } function huge_alloc() { huge_release_1G huge_release_2M huge_alloc_1G || huge_alloc_2M return $? } function huge_release() { huge_release_1G && huge_release_2M return $? } function nic_unbind_ixgbe() { OLDIFS=$IFS; IFS=, for ITER_PCI_ADDR in $PCI_ADDR_IXGBE; do $NICTOOL --force -b ixgbe $ITER_PCI_ADDR [ ! $? -eq 0 ] && return 1 done IFS=$OLDIFS return 0 } function nic_unbind_igb() { OLDIFS=$IFS; IFS=, for ITER_PCI_ADDR in $PCI_ADDR_IGB; do $NICTOOL --force -b igb $ITER_PCI_ADDR [ ! $? -eq 0 ] && return 1 done IFS=$OLDIFS return 0 } function nic_bind_ixgbe() { OLDIFS=$IFS; IFS=, for ITER_PCI_ADDR in $PCI_ADDR_IXGBE; do $NICTOOL --force -b igb_uio $ITER_PCI_ADDR [ ! $? -eq 0 ] && return 1 done IFS=$OLDIFS return 0 } function nic_bind_igb() { OLDIFS=$IFS; IFS=, for ITER_PCI_ADDR in $PCI_ADDR_IGB; do $NICTOOL --force -b igb_uio $ITER_PCI_ADDR [ ! $? -eq 0 ] && return 1 done IFS=$OLDIFS return 0 } function marsiod_stop() { $DAEMON_STOP > /dev/null return 0 } function marsiod_start() { echo 0 > /proc/sys/kernel/randomize_va_space $DAEMON_START & > /dev/null return 0 } function start() { # Require Step, if failure, all step should be stop. action $"Loading IGB_UIO Module:" module_load_uio [ ! $? -eq 0 ] && return 1 action $"Loading Hugepage memory:" huge_alloc [ ! $? -eq 0 ] && return 1 action $"Binding IXGBE NIC Device with IGB_UIO:" nic_bind_ixgbe [ ! $? -eq 0 ] && return 1 action $"Binding IGB NIC Device with IGB_UIO:" nic_bind_igb [ ! $? -eq 0 ] && return 1 action $"Starting Marsio ZC Daemon:" marsiod_start [ ! $? -eq 0 ] && return 1 return 0 } function stop() { action $"Stoping Marsio ZC Daemon:" marsiod_stop [ ! $? -eq 0 ] && return 1 action $"Unloading Hugepage memory:" huge_release [ ! $? -eq 0 ] && return 1 action $"Unbind IXGBE UIO Device:" nic_unbind_ixgbe [ ! $? -eq 0 ] && return 1 action $"Unbind IGB UIO Device:" nic_unbind_igb [ ! $? -eq 0 ] && return 1 action $"Unloading IGB_UIO Module:" module_unload_uio [ ! $? -eq 0 ] && return 1 return 0 } function restart() { stop start $* } action=$1 shift case $action in start) start $* ;; stop) stop ;; restart) restart $* ;; *) echo "Usage: service $0 {start|stop|restart}" exit 1 esac exit $?