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[ -z "$COUNT" ] && COUNT="100000" # Zero means indefinitely
18
19# Base Config
20DELAY="0"        # Zero means max speed
21[ -z "$CLONE_SKB" ] && CLONE_SKB="0"
22
23# Flow variation random source port between min and max
24UDP_SRC_MIN=9
25UDP_SRC_MAX=109
26
27# (example of setting default params in your script)
28if [ -z "$DEST_IP" ]; then
29    [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
30fi
31[ -z "$DST_MAC" ] && DST_MAC="90:e2:ba:ff:ff:ff"
32if [ -n "$DEST_IP" ]; then
33    validate_addr${IP6} $DEST_IP
34    read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
35fi
36if [ -n "$DST_PORT" ]; then
37    read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
38    validate_ports $UDP_DST_MIN $UDP_DST_MAX
39fi
40
41# General cleanup everything since last run
42pg_ctrl "reset"
43
44# Threads are specified with parameter -t value in $THREADS
45for ((thread = $F_THREAD; thread <= $L_THREAD; thread++)); do
46    # The device name is extended with @name, using thread number to
47    # make then unique, but any name will do.
48    dev=${DEV}@${thread}
49
50    # Add remove all other devices and add_device $dev to thread
51    pg_thread $thread "rem_device_all"
52    pg_thread $thread "add_device" $dev
53
54    # Notice config queue to map to cpu (mirrors smp_processor_id())
55    # It is beneficial to map IRQ /proc/irq/*/smp_affinity 1:1 to CPU number
56    pg_set $dev "flag QUEUE_MAP_CPU"
57
58    # Base config of dev
59    pg_set $dev "count $COUNT"
60    pg_set $dev "clone_skb $CLONE_SKB"
61    pg_set $dev "pkt_size $PKT_SIZE"
62    pg_set $dev "delay $DELAY"
63
64    # Flag example disabling timestamping
65    pg_set $dev "flag NO_TIMESTAMP"
66
67    # Destination
68    pg_set $dev "dst_mac $DST_MAC"
69    pg_set $dev "dst${IP6}_min $DST_MIN"
70    pg_set $dev "dst${IP6}_max $DST_MAX"
71
72    if [ -n "$DST_PORT" ]; then
73	# Single destination port or random port range
74	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"
77    fi
78
79    # Setup random UDP port src range
80    pg_set $dev "flag UDPSRC_RND"
81    pg_set $dev "udp_src_min $UDP_SRC_MIN"
82    pg_set $dev "udp_src_max $UDP_SRC_MAX"
83done
84
85# start_run
86echo "Running... ctrl^C to stop" >&2
87pg_ctrl "start"
88echo "Done" >&2
89
90# Print results
91for ((thread = $F_THREAD; thread <= $L_THREAD; thread++)); do
92    dev=${DEV}@${thread}
93    echo "Device: $dev"
94    cat /proc/net/pktgen/$dev | grep -A2 "Result:"
95done
96