1#!/bin/bash
2#
3# Multiqueue: Using pktgen threads for sending on multiple CPUs
4#  * adding devices to kernel threads which are in the same NUMA node
5#  * bound devices queue's irq affinity to the threads, 1:1 mapping
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# Base Config
18DELAY="0"        # Zero means max speed
19[ -z "$COUNT" ]     && COUNT="20000000"   # Zero means indefinitely
20[ -z "$CLONE_SKB" ] && CLONE_SKB="0"
21
22# Flow variation random source port between min and max
23UDP_MIN=9
24UDP_MAX=109
25
26node=`get_iface_node $DEV`
27irq_array=(`get_iface_irqs $DEV`)
28cpu_array=(`get_node_cpus $node`)
29
30[ $THREADS -gt ${#irq_array[*]} -o $THREADS -gt ${#cpu_array[*]}  ] && \
31	err 1 "Thread number $THREADS exceeds: min (${#irq_array[*]},${#cpu_array[*]})"
32
33# (example of setting default params in your script)
34if [ -z "$DEST_IP" ]; then
35    [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
36fi
37[ -z "$DST_MAC" ] && DST_MAC="90:e2:ba:ff:ff:ff"
38if [ -n "$DST_PORT" ]; then
39    read -r DST_MIN DST_MAX <<< $(parse_ports $DST_PORT)
40    validate_ports $DST_MIN $DST_MAX
41fi
42
43# General cleanup everything since last run
44pg_ctrl "reset"
45
46# Threads are specified with parameter -t value in $THREADS
47for ((i = 0; i < $THREADS; i++)); do
48    # The device name is extended with @name, using thread number to
49    # make then unique, but any name will do.
50    # Set the queue's irq affinity to this $thread (processor)
51    # if '-f' is designated, offset cpu id
52    thread=${cpu_array[$((i+F_THREAD))]}
53    dev=${DEV}@${thread}
54    echo $thread > /proc/irq/${irq_array[$i]}/smp_affinity_list
55    info "irq ${irq_array[$i]} is set affinity to `cat /proc/irq/${irq_array[$i]}/smp_affinity_list`"
56
57    # Add remove all other devices and add_device $dev to thread
58    pg_thread $thread "rem_device_all"
59    pg_thread $thread "add_device" $dev
60
61    # select queue and bind the queue and $dev in 1:1 relationship
62    queue_num=$i
63    info "queue number is $queue_num"
64    pg_set $dev "queue_map_min $queue_num"
65    pg_set $dev "queue_map_max $queue_num"
66
67    # Notice config queue to map to cpu (mirrors smp_processor_id())
68    # It is beneficial to map IRQ /proc/irq/*/smp_affinity 1:1 to CPU number
69    pg_set $dev "flag QUEUE_MAP_CPU"
70
71    # Base config of dev
72    pg_set $dev "count $COUNT"
73    pg_set $dev "clone_skb $CLONE_SKB"
74    pg_set $dev "pkt_size $PKT_SIZE"
75    pg_set $dev "delay $DELAY"
76
77    # Flag example disabling timestamping
78    pg_set $dev "flag NO_TIMESTAMP"
79
80    # Destination
81    pg_set $dev "dst_mac $DST_MAC"
82    pg_set $dev "dst$IP6 $DEST_IP"
83
84    if [ -n "$DST_PORT" ]; then
85	# Single destination port or random port range
86	pg_set $dev "flag UDPDST_RND"
87	pg_set $dev "udp_dst_min $DST_MIN"
88	pg_set $dev "udp_dst_max $DST_MAX"
89    fi
90
91    # Setup random UDP port src range
92    pg_set $dev "flag UDPSRC_RND"
93    pg_set $dev "udp_src_min $UDP_MIN"
94    pg_set $dev "udp_src_max $UDP_MAX"
95done
96
97# start_run
98echo "Running... ctrl^C to stop" >&2
99pg_ctrl "start"
100echo "Done" >&2
101
102# Print results
103for ((i = 0; i < $THREADS; i++)); do
104    thread=${cpu_array[$((i+F_THREAD))]}
105    dev=${DEV}@${thread}
106    echo "Device: $dev"
107    cat /proc/net/pktgen/$dev | grep -A2 "Result:"
108done
109