# # Common parameter parsing for pktgen scripts # proc usage { echo "" echo "Usage: $0 [-vx] -i ethX" echo " -i : (\$DEV) output interface/device (required)" echo " -s : (\$PKT_SIZE) packet size" echo " -d : (\$DEST_IP) destination IP" echo " -m : (\$DST_MAC) destination MAC-addr" echo " -t : (\$THREADS) threads to start" echo " -c : (\$SKB_CLONE) SKB clones send before alloc new SKB" echo " -b : (\$BURST) HW level bursting of SKBs" echo " -v : (\$VERBOSE) verbose" echo " -x : (\$DEBUG) debug" echo " -6 : (\$IP6) IPv6" echo "" } ## --- Parse command line arguments / parameters --- ## echo "Commandline options:" while getopts "s:i:d:m:t:c:b:vxh6" option { matchstr $option { i { # interface export DEV=$OPTARG info "Output device set to: DEV=$DEV" } s { export PKT_SIZE=$OPTARG info "Packet size set to: PKT_SIZE=$PKT_SIZE bytes" } d { # destination IP export DEST_IP=$OPTARG info "Destination IP set to: DEST_IP=$DEST_IP" } m { # MAC export DST_MAC=$OPTARG info "Destination MAC set to: DST_MAC=$DST_MAC" } t { export THREADS=$OPTARG export CPU_THREADS=$OPTARG let "CPU_THREADS -= 1" info "Number of threads to start: $THREADS (0 to $CPU_THREADS)" } c { export CLONE_SKB=$OPTARG info "CLONE_SKB=$CLONE_SKB" } b { export BURST=$OPTARG info "SKB bursting: BURST=$BURST" } v { export VERBOSE=yes info "Verbose mode: VERBOSE=$VERBOSE" } x { export DEBUG=yes info "Debug mode: DEBUG=$DEBUG" } 6 { export IP6=6 info "IP6: IP6=$IP6" } h|?|* { usage; err 2 "[ERROR] Unknown parameters!!!" } } } shift $( $OPTIND - 1 ) if test -z $PKT_SIZE { # NIC adds 4 bytes CRC export PKT_SIZE=60 info "Default packet size set to: set to: $PKT_SIZE bytes" } if test -z $THREADS { # Zero CPU threads means one thread, because CPU numbers are zero indexed export CPU_THREADS=0 export THREADS=1 } if test -z $DEV { usage err 2 "Please specify output device" } if test -z $DST_MAC { warn "Missing destination MAC address" } if test -z $DEST_IP { warn "Missing destination IP address" } if test ! -d /proc/net/pktgen { info "Loading kernel module: pktgen" modprobe pktgen } (CommandList children: [ (FuncDef name: usage body: (BraceGroup children: [ (C {(echo)} {(DQ )}) (C {(echo)} {(DQ ("Usage: ") ($ VSub_Number "$0") (" [-vx] -i ethX"))}) (C {(echo)} { (DQ (" -i : (") (EscapedLiteralPart token:) ("DEV) output interface/device (required)") ) } ) (C {(echo)} { (DQ (" -s : (") (EscapedLiteralPart token:) ("PKT_SIZE) packet size") ) } ) (C {(echo)} { (DQ (" -d : (") (EscapedLiteralPart token:) ("DEST_IP) destination IP") ) } ) (C {(echo)} { (DQ (" -m : (") (EscapedLiteralPart token:) ("DST_MAC) destination MAC-addr") ) } ) (C {(echo)} { (DQ (" -t : (") (EscapedLiteralPart token:) ("THREADS) threads to start") ) } ) (C {(echo)} { (DQ (" -c : (") (EscapedLiteralPart token:) ("SKB_CLONE) SKB clones send before alloc new SKB") ) } ) (C {(echo)} { (DQ (" -b : (") (EscapedLiteralPart token:) ("BURST) HW level bursting of SKBs") ) } ) (C {(echo)} { (DQ (" -v : (") (EscapedLiteralPart token:) ("VERBOSE) verbose") ) } ) (C {(echo)} { (DQ (" -x : (") (EscapedLiteralPart token:) ("DEBUG) debug") ) } ) (C {(echo)} { (DQ (" -6 : (") (EscapedLiteralPart token:) ("IP6) IPv6")) } ) (C {(echo)} {(DQ )}) ] spids: [16] ) spids: [10 15] ) (While cond: [ (Sentence child: (C {(getopts)} {(DQ ("s:i:d:m:t:c:b:vxh6"))} {(option)}) terminator: ) ] body: (DoGroup children: [ (Case to_match: {($ VSub_Name "$option")} arms: [ (case_arm pat_list: [{(i)}] action: [ (C {(export)} {(Lit_VarLike "DEV=") ($ VSub_Name "$OPTARG")}) (C {(info)} {(DQ ("Output device set to: DEV=") ($ VSub_Name "$DEV"))}) ] spids: [159 160 180 -1] ) (case_arm pat_list: [{(s)}] action: [ (C {(export)} {(Lit_VarLike "PKT_SIZE=") ($ VSub_Name "$OPTARG")}) (C {(info)} {(DQ ("Packet size set to: PKT_SIZE=") ($ VSub_Name "$PKT_SIZE") (" bytes"))} ) ] spids: [183 184 202 -1] ) (case_arm pat_list: [{(d)}] action: [ (C {(export)} {(Lit_VarLike "DEST_IP=") ($ VSub_Name "$OPTARG")}) (C {(info)} {(DQ ("Destination IP set to: DEST_IP=") ($ VSub_Name "$DEST_IP"))}) ] spids: [205 206 226 -1] ) (case_arm pat_list: [{(m)}] action: [ (C {(export)} {(Lit_VarLike "DST_MAC=") ($ VSub_Name "$OPTARG")}) (C {(info)} {(DQ ("Destination MAC set to: DST_MAC=") ($ VSub_Name "$DST_MAC"))}) ] spids: [229 230 250 -1] ) (case_arm pat_list: [{(t)}] action: [ (C {(export)} {(Lit_VarLike "THREADS=") ($ VSub_Name "$OPTARG")}) (C {(export)} {(Lit_VarLike "CPU_THREADS=") ($ VSub_Name "$OPTARG")}) (C {(let)} {(DQ ("CPU_THREADS -= 1"))}) (C {(info)} { (DQ ("Number of threads to start: ") ($ VSub_Name "$THREADS") (" (0 to ") ($ VSub_Name "$CPU_THREADS") (")") ) } ) ] spids: [253 254 287 -1] ) (case_arm pat_list: [{(c)}] action: [ (C {(export)} {(Lit_VarLike "CLONE_SKB=") ($ VSub_Name "$OPTARG")}) (C {(info)} {(DQ ("CLONE_SKB=") ($ VSub_Name "$CLONE_SKB"))}) ] spids: [290 291 308 -1] ) (case_arm pat_list: [{(b)}] action: [ (C {(export)} {(Lit_VarLike "BURST=") ($ VSub_Name "$OPTARG")}) (C {(info)} {(DQ ("SKB bursting: BURST=") ($ VSub_Name "$BURST"))}) ] spids: [311 312 329 -1] ) (case_arm pat_list: [{(v)}] action: [ (C {(export)} {(Lit_VarLike "VERBOSE=") (yes)}) (C {(info)} {(DQ ("Verbose mode: VERBOSE=") ($ VSub_Name "$VERBOSE"))}) ] spids: [332 333 350 -1] ) (case_arm pat_list: [{(x)}] action: [ (C {(export)} {(Lit_VarLike "DEBUG=") (yes)}) (C {(info)} {(DQ ("Debug mode: DEBUG=") ($ VSub_Name "$DEBUG"))}) ] spids: [353 354 371 -1] ) (case_arm pat_list: [{(6)}] action: [ (C {(export)} {(Lit_VarLike "IP6=") (6)}) (C {(info)} {(DQ ("IP6: IP6=") ($ VSub_Name "$IP6"))}) ] spids: [374 375 392 -1] ) (case_arm pat_list: [{(h)} {(Lit_Other "?")} {(Lit_Other "*")}] action: [ (Sentence child:(C {(usage)}) terminator:) (C {(err)} {(2)} {(DQ ("[ERROR] Unknown parameters!!!"))}) ] spids: [395 400 -1 416] ) ] spids: [152 156 416] ) ] spids: [149 418] ) ) (C {(shift)} { (ArithSubPart anode: (ArithBinary op_id: Arith_Minus left: (ArithWord w:{($ VSub_Name "$OPTIND")}) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [422 431] ) } ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$PKT_SIZE"))} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(export)} {(Lit_VarLike "PKT_SIZE=") (60)}) (C {(info)} {(DQ ("Default packet size set to: set to: ") ($ VSub_Name "$PKT_SIZE") (" bytes"))} ) ] spids: [-1 447] ) ] spids: [-1 468] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$THREADS"))} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(export)} {(Lit_VarLike "CPU_THREADS=") (0)}) (C {(export)} {(Lit_VarLike "THREADS=") (1)}) ] spids: [-1 484] ) ] spids: [-1 502] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$DEV"))} {(Lit_Other "]")}) terminator: ) ] action: [(C {(usage)}) (C {(err)} {(2)} {(DQ ("Please specify output device"))})] spids: [-1 518] ) ] spids: [-1 532] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$DST_MAC"))} {(Lit_Other "]")}) terminator: ) ] action: [(C {(warn)} {(DQ ("Missing destination MAC address"))})] spids: [-1 548] ) ] spids: [-1 557] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$DEST_IP"))} {(Lit_Other "]")}) terminator: ) ] action: [(C {(warn)} {(DQ ("Missing destination IP address"))})] spids: [-1 573] ) ] spids: [-1 582] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(KW_Bang "!")} {(-d)} {(/proc/net/pktgen)} {(Lit_Other "]")}) terminator: ) ] action: [(C {(info)} {(DQ ("Loading kernel module: pktgen"))}) (C {(modprobe)} {(pktgen)})] spids: [-1 598] ) ] spids: [-1 612] ) ] )