105a14d5eSJesper Dangaard Brouer#!/bin/bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
305a14d5eSJesper Dangaard Brouer#
405a14d5eSJesper Dangaard Brouer# Benchmark script:
505a14d5eSJesper Dangaard Brouer#  - developed for benchmarking ingress qdisc path
605a14d5eSJesper Dangaard Brouer#
705a14d5eSJesper Dangaard Brouer# Script for injecting packets into RX path of the stack with pktgen
805a14d5eSJesper Dangaard Brouer# "xmit_mode netif_receive".  With an invalid dst_mac this will only
905a14d5eSJesper Dangaard Brouer# measure the ingress code path as packets gets dropped in ip_rcv().
1005a14d5eSJesper Dangaard Brouer#
1105a14d5eSJesper Dangaard Brouer# This script don't really need any hardware.  It benchmarks software
1205a14d5eSJesper Dangaard Brouer# RX path just after NIC driver level.  With bursting is also
1305a14d5eSJesper Dangaard Brouer# "removes" the SKB alloc/free overhead.
1405a14d5eSJesper Dangaard Brouer#
1505a14d5eSJesper Dangaard Brouer# Setup scenarios for measuring ingress qdisc (with invalid dst_mac):
1605a14d5eSJesper Dangaard Brouer# ------------------------------------------------------------------
1705a14d5eSJesper Dangaard Brouer# (1) no ingress (uses static_key_false(&ingress_needed))
1805a14d5eSJesper Dangaard Brouer#
1905a14d5eSJesper Dangaard Brouer# (2) ingress on other dev (change ingress_needed and calls
2005a14d5eSJesper Dangaard Brouer#     handle_ing() but exit early)
2105a14d5eSJesper Dangaard Brouer#
2205a14d5eSJesper Dangaard Brouer#  config:  tc qdisc add dev $SOMEDEV handle ffff: ingress
2305a14d5eSJesper Dangaard Brouer#
2405a14d5eSJesper Dangaard Brouer# (3) ingress on this dev, handle_ing() -> tc_classify()
2505a14d5eSJesper Dangaard Brouer#
2605a14d5eSJesper Dangaard Brouer#  config:  tc qdisc add dev $DEV handle ffff: ingress
2705a14d5eSJesper Dangaard Brouer#
2805a14d5eSJesper Dangaard Brouer# (4) ingress on this dev + drop at u32 classifier/action.
2905a14d5eSJesper Dangaard Brouer#
3005a14d5eSJesper Dangaard Brouerbasedir=`dirname $0`
3105a14d5eSJesper Dangaard Brouersource ${basedir}/functions.sh
3205a14d5eSJesper Dangaard Brouerroot_check_run_with_sudo "$@"
3305a14d5eSJesper Dangaard Brouer
3405a14d5eSJesper Dangaard Brouer# Parameter parsing via include
3505a14d5eSJesper Dangaard Brouersource ${basedir}/parameters.sh
3605a14d5eSJesper Dangaard Brouer# Using invalid DST_MAC will cause the packets to get dropped in
3705a14d5eSJesper Dangaard Brouer# ip_rcv() which is part of the test
380f06a678SMartin KaFai Lauif [ -z "$DEST_IP" ]; then
390f06a678SMartin KaFai Lau    [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
400f06a678SMartin KaFai Laufi
4105a14d5eSJesper Dangaard Brouer[ -z "$DST_MAC" ] && DST_MAC="90:e2:ba:ff:ff:ff"
4205a14d5eSJesper Dangaard Brouer[ -z "$BURST" ] && BURST=1024
4369137ea6STariq Toukan[ -z "$COUNT" ] && COUNT="10000000" # Zero means indefinitely
4440f843eeSDaniel T. Leeif [ -n "$DEST_IP" ]; then
4540f843eeSDaniel T. Lee    validate_addr${IP6} $DEST_IP
4640f843eeSDaniel T. Lee    read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
4740f843eeSDaniel T. Leefi
486e32a74aSDaniel T. Leeif [ -n "$DST_PORT" ]; then
49723d2904SDaniel T. Lee    read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
50723d2904SDaniel T. Lee    validate_ports $UDP_DST_MIN $UDP_DST_MAX
516e32a74aSDaniel T. Leefi
5205a14d5eSJesper Dangaard Brouer
5305a14d5eSJesper Dangaard Brouer# General cleanup everything since last run
5405a14d5eSJesper Dangaard Brouerpg_ctrl "reset"
5505a14d5eSJesper Dangaard Brouer
5605a14d5eSJesper Dangaard Brouer# Threads are specified with parameter -t value in $THREADS
57e0e16672STariq Toukanfor ((thread = $F_THREAD; thread <= $L_THREAD; thread++)); do
5805a14d5eSJesper Dangaard Brouer    # The device name is extended with @name, using thread number to
5905a14d5eSJesper Dangaard Brouer    # make then unique, but any name will do.
6005a14d5eSJesper Dangaard Brouer    dev=${DEV}@${thread}
6105a14d5eSJesper Dangaard Brouer
6205a14d5eSJesper Dangaard Brouer    # Add remove all other devices and add_device $dev to thread
6305a14d5eSJesper Dangaard Brouer    pg_thread $thread "rem_device_all"
6405a14d5eSJesper Dangaard Brouer    pg_thread $thread "add_device" $dev
6505a14d5eSJesper Dangaard Brouer
6605a14d5eSJesper Dangaard Brouer    # Base config of dev
6705a14d5eSJesper Dangaard Brouer    pg_set $dev "flag QUEUE_MAP_CPU"
6805a14d5eSJesper Dangaard Brouer    pg_set $dev "count $COUNT"
6905a14d5eSJesper Dangaard Brouer    pg_set $dev "pkt_size $PKT_SIZE"
7005a14d5eSJesper Dangaard Brouer    pg_set $dev "delay $DELAY"
7105a14d5eSJesper Dangaard Brouer    pg_set $dev "flag NO_TIMESTAMP"
7205a14d5eSJesper Dangaard Brouer
7305a14d5eSJesper Dangaard Brouer    # Destination
7405a14d5eSJesper Dangaard Brouer    pg_set $dev "dst_mac $DST_MAC"
7540f843eeSDaniel T. Lee    pg_set $dev "dst${IP6}_min $DST_MIN"
7640f843eeSDaniel T. Lee    pg_set $dev "dst${IP6}_max $DST_MAX"
7705a14d5eSJesper Dangaard Brouer
786e32a74aSDaniel T. Lee    if [ -n "$DST_PORT" ]; then
796e32a74aSDaniel T. Lee	# Single destination port or random port range
806e32a74aSDaniel T. Lee	pg_set $dev "flag UDPDST_RND"
81723d2904SDaniel T. Lee	pg_set $dev "udp_dst_min $UDP_DST_MIN"
82723d2904SDaniel T. Lee	pg_set $dev "udp_dst_max $UDP_DST_MAX"
836e32a74aSDaniel T. Lee    fi
846e32a74aSDaniel T. Lee
8505a14d5eSJesper Dangaard Brouer    # Inject packet into RX path of stack
8605a14d5eSJesper Dangaard Brouer    pg_set $dev "xmit_mode netif_receive"
8705a14d5eSJesper Dangaard Brouer
8805a14d5eSJesper Dangaard Brouer    # Burst allow us to avoid measuring SKB alloc/free overhead
8905a14d5eSJesper Dangaard Brouer    pg_set $dev "burst $BURST"
9005a14d5eSJesper Dangaard Brouerdone
9105a14d5eSJesper Dangaard Brouer
92*6c882bdcSJuhee Kang# Run if user hits control-c
93*6c882bdcSJuhee Kangfunction print_result() {
9405a14d5eSJesper Dangaard Brouer    # Print results
95e0e16672STariq Toukan    for ((thread = $F_THREAD; thread <= $L_THREAD; thread++)); do
9605a14d5eSJesper Dangaard Brouer        dev=${DEV}@${thread}
9705a14d5eSJesper Dangaard Brouer        echo "Device: $dev"
9805a14d5eSJesper Dangaard Brouer        cat /proc/net/pktgen/$dev | grep -A2 "Result:"
9905a14d5eSJesper Dangaard Brouer    done
100*6c882bdcSJuhee Kang}
101*6c882bdcSJuhee Kang# trap keyboard interrupt (Ctrl-C)
102*6c882bdcSJuhee Kangtrap true SIGINT
103*6c882bdcSJuhee Kang
104*6c882bdcSJuhee Kang# start_run
105*6c882bdcSJuhee Kangecho "Running... ctrl^C to stop" >&2
106*6c882bdcSJuhee Kangpg_ctrl "start"
107*6c882bdcSJuhee Kangecho "Done" >&2
108*6c882bdcSJuhee Kang
109*6c882bdcSJuhee Kangprint_result
110