xref: /openbmc/linux/samples/pktgen/pktgen_sample01_simple.sh (revision f6d73b12ca9fd3b1c29a6a725cd751b972c740cf)
16f094797SJesper Dangaard Brouer#!/bin/bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
36f094797SJesper Dangaard Brouer#
46f094797SJesper Dangaard Brouer# Simple example:
56f094797SJesper Dangaard Brouer#  * pktgen sending with single thread and single interface
66f094797SJesper Dangaard Brouer#  * flow variation via random UDP source port
76f094797SJesper Dangaard Brouer#
86f094797SJesper Dangaard Brouerbasedir=`dirname $0`
96f094797SJesper Dangaard Brouersource ${basedir}/functions.sh
106f094797SJesper Dangaard Brouerroot_check_run_with_sudo "$@"
116f094797SJesper Dangaard Brouer
126f094797SJesper Dangaard Brouer# Parameter parsing via include
136f094797SJesper Dangaard Brouer# - go look in parameters.sh to see which setting are avail
146f094797SJesper Dangaard Brouer# - required param is the interface "-i" stored in $DEV
156f094797SJesper Dangaard Brouersource ${basedir}/parameters.sh
16a27ac539SJ.J. Martzki
17a27ac539SJ.J. Martzki# Trap EXIT first
18a27ac539SJ.J. Martzkitrap_exit
19a27ac539SJ.J. Martzki
206f094797SJesper Dangaard Brouer#
216f094797SJesper Dangaard Brouer# Set some default params, if they didn't get set
220f06a678SMartin KaFai Lauif [ -z "$DEST_IP" ]; then
230f06a678SMartin KaFai Lau    [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
240f06a678SMartin KaFai Laufi
256f094797SJesper Dangaard Brouer[ -z "$CLONE_SKB" ] && CLONE_SKB="0"
266f094797SJesper Dangaard Brouer# Example enforce param "-m" for dst_mac
276f094797SJesper Dangaard Brouer[ -z "$DST_MAC" ] && usage && err 2 "Must specify -m dst_mac"
2869137ea6STariq Toukan[ -z "$COUNT" ]   && COUNT="100000" # Zero means indefinitely
2940f843eeSDaniel T. Leeif [ -n "$DEST_IP" ]; then
3040f843eeSDaniel T. Lee    validate_addr${IP6} $DEST_IP
3140f843eeSDaniel T. Lee    read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
3240f843eeSDaniel T. Leefi
336e32a74aSDaniel T. Leeif [ -n "$DST_PORT" ]; then
34723d2904SDaniel T. Lee    read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
35723d2904SDaniel T. Lee    validate_ports $UDP_DST_MIN $UDP_DST_MAX
366e32a74aSDaniel T. Leefi
376f094797SJesper Dangaard Brouer
386f094797SJesper Dangaard Brouer# Flow variation random source port between min and max
39723d2904SDaniel T. LeeUDP_SRC_MIN=9
40723d2904SDaniel T. LeeUDP_SRC_MAX=109
416f094797SJesper Dangaard Brouer
426f094797SJesper Dangaard Brouer# General cleanup everything since last run
436f094797SJesper Dangaard Brouer# (especially important if other threads were configured by other scripts)
44c8fd4852SIgor Russkikh[ -z "$APPEND" ] && pg_ctrl "reset"
456f094797SJesper Dangaard Brouer
466f094797SJesper Dangaard Brouer# Add remove all other devices and add_device $DEV to thread 0
476f094797SJesper Dangaard Brouerthread=0
48c8fd4852SIgor Russkikh[ -z "$APPEND" ] && pg_thread $thread "rem_device_all"
496f094797SJesper Dangaard Brouerpg_thread $thread "add_device" $DEV
506f094797SJesper Dangaard Brouer
516f094797SJesper Dangaard Brouer# How many packets to send (zero means indefinitely)
526f094797SJesper Dangaard Brouerpg_set $DEV "count $COUNT"
536f094797SJesper Dangaard Brouer
546f094797SJesper Dangaard Brouer# Reduce alloc cost by sending same SKB many times
556f094797SJesper Dangaard Brouer# - this obviously affects the randomness within the packet
566f094797SJesper Dangaard Brouerpg_set $DEV "clone_skb $CLONE_SKB"
576f094797SJesper Dangaard Brouer
586f094797SJesper Dangaard Brouer# Set packet size
596f094797SJesper Dangaard Brouerpg_set $DEV "pkt_size $PKT_SIZE"
606f094797SJesper Dangaard Brouer
616f094797SJesper Dangaard Brouer# Delay between packets (zero means max speed)
626f094797SJesper Dangaard Brouerpg_set $DEV "delay $DELAY"
636f094797SJesper Dangaard Brouer
646f094797SJesper Dangaard Brouer# Flag example disabling timestamping
656f094797SJesper Dangaard Brouerpg_set $DEV "flag NO_TIMESTAMP"
666f094797SJesper Dangaard Brouer
676f094797SJesper Dangaard Brouer# Destination
686f094797SJesper Dangaard Brouerpg_set $DEV "dst_mac $DST_MAC"
6940f843eeSDaniel T. Leepg_set $DEV "dst${IP6}_min $DST_MIN"
7040f843eeSDaniel T. Leepg_set $DEV "dst${IP6}_max $DST_MAX"
716f094797SJesper Dangaard Brouer
726e32a74aSDaniel T. Leeif [ -n "$DST_PORT" ]; then
736e32a74aSDaniel T. Lee    # Single destination port or random port range
746e32a74aSDaniel T. Lee    pg_set $DEV "flag UDPDST_RND"
75723d2904SDaniel T. Lee    pg_set $DEV "udp_dst_min $UDP_DST_MIN"
76723d2904SDaniel T. Lee    pg_set $DEV "udp_dst_max $UDP_DST_MAX"
776e32a74aSDaniel T. Leefi
786e32a74aSDaniel T. Lee
79*4ae7a3c3SWei Fang[ ! -z "$UDP_CSUM" ] && pg_set $DEV "flag UDPCSUM"
80460a9aa2SLorenzo Bianconi
816f094797SJesper Dangaard Brouer# Setup random UDP port src range
826f094797SJesper Dangaard Brouerpg_set $DEV "flag UDPSRC_RND"
83723d2904SDaniel T. Leepg_set $DEV "udp_src_min $UDP_SRC_MIN"
84723d2904SDaniel T. Leepg_set $DEV "udp_src_max $UDP_SRC_MAX"
856f094797SJesper Dangaard Brouer
866c882bdcSJuhee Kang# Run if user hits control-c
876c882bdcSJuhee Kangfunction print_result() {
886c882bdcSJuhee Kang    # Print results
896c882bdcSJuhee Kang    echo "Result device: $DEV"
906c882bdcSJuhee Kang    cat /proc/net/pktgen/$DEV
916c882bdcSJuhee Kang}
926c882bdcSJuhee Kang# trap keyboard interrupt (Ctrl-C)
936c882bdcSJuhee Kangtrap true SIGINT
946c882bdcSJuhee Kang
95c8fd4852SIgor Russkikhif [ -z "$APPEND" ]; then
966f094797SJesper Dangaard Brouer    # start_run
976f094797SJesper Dangaard Brouer    echo "Running... ctrl^C to stop" >&2
986f094797SJesper Dangaard Brouer    pg_ctrl "start"
996f094797SJesper Dangaard Brouer    echo "Done" >&2
1006f094797SJesper Dangaard Brouer
1016c882bdcSJuhee Kang    print_result
102c8fd4852SIgor Russkikhelse
103c8fd4852SIgor Russkikh    echo "Append mode: config done. Do more or use 'pg_ctrl start' to run"
104c8fd4852SIgor Russkikhfi