Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Simple example:
0005 #  * pktgen sending with single thread and single interface
0006 #  * flow variation via random UDP source port
0007 #
0008 basedir=`dirname $0`
0009 source ${basedir}/functions.sh
0010 root_check_run_with_sudo "$@"
0011 
0012 # Parameter parsing via include
0013 # - go look in parameters.sh to see which setting are avail
0014 # - required param is the interface "-i" stored in $DEV
0015 source ${basedir}/parameters.sh
0016 #
0017 # Set some default params, if they didn't get set
0018 if [ -z "$DEST_IP" ]; then
0019     [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
0020 fi
0021 [ -z "$CLONE_SKB" ] && CLONE_SKB="0"
0022 # Example enforce param "-m" for dst_mac
0023 [ -z "$DST_MAC" ] && usage && err 2 "Must specify -m dst_mac"
0024 [ -z "$COUNT" ]   && COUNT="100000" # Zero means indefinitely
0025 if [ -n "$DEST_IP" ]; then
0026     validate_addr${IP6} $DEST_IP
0027     read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
0028 fi
0029 if [ -n "$DST_PORT" ]; then
0030     read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
0031     validate_ports $UDP_DST_MIN $UDP_DST_MAX
0032 fi
0033 
0034 # Flow variation random source port between min and max
0035 UDP_SRC_MIN=9
0036 UDP_SRC_MAX=109
0037 
0038 # General cleanup everything since last run
0039 # (especially important if other threads were configured by other scripts)
0040 [ -z "$APPEND" ] && pg_ctrl "reset"
0041 
0042 # Add remove all other devices and add_device $DEV to thread 0
0043 thread=0
0044 [ -z "$APPEND" ] && pg_thread $thread "rem_device_all"
0045 pg_thread $thread "add_device" $DEV
0046 
0047 # How many packets to send (zero means indefinitely)
0048 pg_set $DEV "count $COUNT"
0049 
0050 # Reduce alloc cost by sending same SKB many times
0051 # - this obviously affects the randomness within the packet
0052 pg_set $DEV "clone_skb $CLONE_SKB"
0053 
0054 # Set packet size
0055 pg_set $DEV "pkt_size $PKT_SIZE"
0056 
0057 # Delay between packets (zero means max speed)
0058 pg_set $DEV "delay $DELAY"
0059 
0060 # Flag example disabling timestamping
0061 pg_set $DEV "flag NO_TIMESTAMP"
0062 
0063 # Destination
0064 pg_set $DEV "dst_mac $DST_MAC"
0065 pg_set $DEV "dst${IP6}_min $DST_MIN"
0066 pg_set $DEV "dst${IP6}_max $DST_MAX"
0067 
0068 if [ -n "$DST_PORT" ]; then
0069     # Single destination port or random port range
0070     pg_set $DEV "flag UDPDST_RND"
0071     pg_set $DEV "udp_dst_min $UDP_DST_MIN"
0072     pg_set $DEV "udp_dst_max $UDP_DST_MAX"
0073 fi
0074 
0075 [ ! -z "$UDP_CSUM" ] && pg_set $dev "flag UDPCSUM"
0076 
0077 # Setup random UDP port src range
0078 pg_set $DEV "flag UDPSRC_RND"
0079 pg_set $DEV "udp_src_min $UDP_SRC_MIN"
0080 pg_set $DEV "udp_src_max $UDP_SRC_MAX"
0081 
0082 # Run if user hits control-c
0083 function print_result() {
0084     # Print results
0085     echo "Result device: $DEV"
0086     cat /proc/net/pktgen/$DEV
0087 }
0088 # trap keyboard interrupt (Ctrl-C)
0089 trap true SIGINT
0090 
0091 if [ -z "$APPEND" ]; then
0092     # start_run
0093     echo "Running... ctrl^C to stop" >&2
0094     pg_ctrl "start"
0095     echo "Done" >&2
0096 
0097     print_result
0098 else
0099     echo "Append mode: config done. Do more or use 'pg_ctrl start' to run"
0100 fi