^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # Simple example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # * pktgen sending with single thread and single interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # * flow variation via random UDP source port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) basedir=`dirname $0`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) source ${basedir}/functions.sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) root_check_run_with_sudo "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # Parameter parsing via include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # - go look in parameters.sh to see which setting are avail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # - required param is the interface "-i" stored in $DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) source ${basedir}/parameters.sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) # Set some default params, if they didn't get set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if [ -z "$DEST_IP" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) [ -z "$CLONE_SKB" ] && CLONE_SKB="0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # Example enforce param "-m" for dst_mac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) [ -z "$DST_MAC" ] && usage && err 2 "Must specify -m dst_mac"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) [ -z "$COUNT" ] && COUNT="100000" # Zero means indefinitely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if [ -n "$DEST_IP" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) validate_addr${IP6} $DEST_IP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if [ -n "$DST_PORT" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) validate_ports $UDP_DST_MIN $UDP_DST_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) # Base Config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) DELAY="0" # Zero means max speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) # Flow variation random source port between min and max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) UDP_SRC_MIN=9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) UDP_SRC_MAX=109
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) # General cleanup everything since last run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # (especially important if other threads were configured by other scripts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pg_ctrl "reset"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) # Add remove all other devices and add_device $DEV to thread 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) thread=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) pg_thread $thread "rem_device_all"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pg_thread $thread "add_device" $DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # How many packets to send (zero means indefinitely)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) pg_set $DEV "count $COUNT"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) # Reduce alloc cost by sending same SKB many times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) # - this obviously affects the randomness within the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pg_set $DEV "clone_skb $CLONE_SKB"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) # Set packet size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) pg_set $DEV "pkt_size $PKT_SIZE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) # Delay between packets (zero means max speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pg_set $DEV "delay $DELAY"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) # Flag example disabling timestamping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) pg_set $DEV "flag NO_TIMESTAMP"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) # Destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pg_set $DEV "dst_mac $DST_MAC"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pg_set $DEV "dst${IP6}_min $DST_MIN"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pg_set $DEV "dst${IP6}_max $DST_MAX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if [ -n "$DST_PORT" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) # Single destination port or random port range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pg_set $DEV "flag UDPDST_RND"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) pg_set $DEV "udp_dst_min $UDP_DST_MIN"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) pg_set $DEV "udp_dst_max $UDP_DST_MAX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) # Setup random UDP port src range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pg_set $DEV "flag UDPSRC_RND"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) pg_set $DEV "udp_src_min $UDP_SRC_MIN"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pg_set $DEV "udp_src_max $UDP_SRC_MAX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) # start_run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) echo "Running... ctrl^C to stop" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pg_ctrl "start"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) echo "Done" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # Print results
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) echo "Result device: $DEV"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cat /proc/net/pktgen/$DEV