1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Multiqueue: Using pktgen threads for sending on multiple CPUs
5#  * adding devices to kernel threads
6#  * notice the naming scheme for keeping device names unique
7#  * nameing scheme: dev@thread_number
8#  * flow variation via random UDP source port
9#
10basedir=`dirname $0`
11source ${basedir}/functions.sh
12root_check_run_with_sudo "$@"
13#
14# Required param: -i dev in $DEV
15source ${basedir}/parameters.sh
16
17# Trap EXIT first
18trap_exit
19
20[ -z "$COUNT" ] && COUNT="100000" # Zero means indefinitely
21
22# Base Config
23[ -z "$CLONE_SKB" ] && CLONE_SKB="0"
24
25# Flow variation random source port between min and max
26UDP_SRC_MIN=9
27UDP_SRC_MAX=109
28
29# (example of setting default params in your script)
30if [ -z "$DEST_IP" ]; then
31    [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
32fi
33[ -z "$DST_MAC" ] && DST_MAC="90:e2:ba:ff:ff:ff"
34if [ -n "$DEST_IP" ]; then
35    validate_addr${IP6} $DEST_IP
36    read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
37fi
38if [ -n "$DST_PORT" ]; then
39    read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
40    validate_ports $UDP_DST_MIN $UDP_DST_MAX
41fi
42
43# General cleanup everything since last run
44[ -z "$APPEND" ] && pg_ctrl "reset"
45
46# Threads are specified with parameter -t value in $THREADS
47for ((thread = $F_THREAD; thread <= $L_THREAD; thread++)); do
48    # The device name is extended with @name, using thread number to
49    # make then unique, but any name will do.
50    dev=${DEV}@${thread}
51
52    # Add remove all other devices and add_device $dev to thread
53    [ -z "$APPEND" ] && pg_thread $thread "rem_device_all"
54    pg_thread $thread "add_device" $dev
55
56    # Notice config queue to map to cpu (mirrors smp_processor_id())
57    # It is beneficial to map IRQ /proc/irq/*/smp_affinity 1:1 to CPU number
58    pg_set $dev "flag QUEUE_MAP_CPU"
59
60    # Base config of dev
61    pg_set $dev "count $COUNT"
62    pg_set $dev "clone_skb $CLONE_SKB"
63    pg_set $dev "pkt_size $PKT_SIZE"
64    pg_set $dev "delay $DELAY"
65
66    # Flag example disabling timestamping
67    pg_set $dev "flag NO_TIMESTAMP"
68
69    # Destination
70    pg_set $dev "dst_mac $DST_MAC"
71    pg_set $dev "dst${IP6}_min $DST_MIN"
72    pg_set $dev "dst${IP6}_max $DST_MAX"
73
74    if [ -n "$DST_PORT" ]; then
75	# Single destination port or random port range
76	pg_set $dev "flag UDPDST_RND"
77	pg_set $dev "udp_dst_min $UDP_DST_MIN"
78	pg_set $dev "udp_dst_max $UDP_DST_MAX"
79    fi
80
81    [ ! -z "$UDP_CSUM" ] && pg_set $dev "flag UDPCSUM"
82
83    # Setup random UDP port src range
84    pg_set $dev "flag UDPSRC_RND"
85    pg_set $dev "udp_src_min $UDP_SRC_MIN"
86    pg_set $dev "udp_src_max $UDP_SRC_MAX"
87done
88
89# Run if user hits control-c
90function print_result() {
91    # Print results
92    for ((thread = $F_THREAD; thread <= $L_THREAD; thread++)); do
93        dev=${DEV}@${thread}
94        echo "Device: $dev"
95        cat /proc/net/pktgen/$dev | grep -A2 "Result:"
96    done
97}
98# trap keyboard interrupt (Ctrl-C)
99trap true SIGINT
100
101if [ -z "$APPEND" ]; then
102    # start_run
103    echo "Running... ctrl^C to stop" >&2
104    pg_ctrl "start"
105    echo "Done" >&2
106
107    print_result
108else
109    echo "Append mode: config done. Do more or use 'pg_ctrl start' to run"
110fi
111