104 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# Simple example:5# * pktgen sending with single thread and single interface6# * flow variation via random UDP source port7#8basedir=`dirname $0`9source ${basedir}/functions.sh10root_check_run_with_sudo "$@"11 12# Parameter parsing via include13# - go look in parameters.sh to see which setting are avail14# - required param is the interface "-i" stored in $DEV15source ${basedir}/parameters.sh16 17# Trap EXIT first18trap_exit19 20#21# Set some default params, if they didn't get set22if [ -z "$DEST_IP" ]; then23 [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"24fi25[ -z "$CLONE_SKB" ] && CLONE_SKB="0"26# Example enforce param "-m" for dst_mac27[ -z "$DST_MAC" ] && usage && err 2 "Must specify -m dst_mac"28[ -z "$COUNT" ] && COUNT="100000" # Zero means indefinitely29if [ -n "$DEST_IP" ]; then30 validate_addr${IP6} $DEST_IP31 read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)32fi33if [ -n "$DST_PORT" ]; then34 read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)35 validate_ports $UDP_DST_MIN $UDP_DST_MAX36fi37 38# Flow variation random source port between min and max39UDP_SRC_MIN=940UDP_SRC_MAX=10941 42# General cleanup everything since last run43# (especially important if other threads were configured by other scripts)44[ -z "$APPEND" ] && pg_ctrl "reset"45 46# Add remove all other devices and add_device $DEV to thread 047thread=048[ -z "$APPEND" ] && pg_thread $thread "rem_device_all"49pg_thread $thread "add_device" $DEV50 51# How many packets to send (zero means indefinitely)52pg_set $DEV "count $COUNT"53 54# Reduce alloc cost by sending same SKB many times55# - this obviously affects the randomness within the packet56pg_set $DEV "clone_skb $CLONE_SKB"57 58# Set packet size59pg_set $DEV "pkt_size $PKT_SIZE"60 61# Delay between packets (zero means max speed)62pg_set $DEV "delay $DELAY"63 64# Flag example disabling timestamping65pg_set $DEV "flag NO_TIMESTAMP"66 67# Destination68pg_set $DEV "dst_mac $DST_MAC"69pg_set $DEV "dst${IP6}_min $DST_MIN"70pg_set $DEV "dst${IP6}_max $DST_MAX"71 72if [ -n "$DST_PORT" ]; then73 # Single destination port or random port range74 pg_set $DEV "flag UDPDST_RND"75 pg_set $DEV "udp_dst_min $UDP_DST_MIN"76 pg_set $DEV "udp_dst_max $UDP_DST_MAX"77fi78 79[ ! -z "$UDP_CSUM" ] && pg_set $DEV "flag UDPCSUM"80 81# Setup random UDP port src range82pg_set $DEV "flag UDPSRC_RND"83pg_set $DEV "udp_src_min $UDP_SRC_MIN"84pg_set $DEV "udp_src_max $UDP_SRC_MAX"85 86# Run if user hits control-c87function print_result() {88 # Print results89 echo "Result device: $DEV"90 cat /proc/net/pktgen/$DEV91}92# trap keyboard interrupt (Ctrl-C)93trap true SIGINT94 95if [ -z "$APPEND" ]; then96 # start_run97 echo "Running... ctrl^C to stop" >&298 pg_ctrl "start"99 echo "Done" >&2100 101 print_result102else103 echo "Append mode: config done. Do more or use 'pg_ctrl start' to run"104fi