pktgen_sample01_simple.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Simple example:
  5. # * pktgen sending with single thread and single interface
  6. # * flow variation via random UDP source port
  7. #
  8. basedir=`dirname $0`
  9. source ${basedir}/functions.sh
  10. root_check_run_with_sudo "$@"
  11. # Parameter parsing via include
  12. # - go look in parameters.sh to see which setting are avail
  13. # - required param is the interface "-i" stored in $DEV
  14. source ${basedir}/parameters.sh
  15. #
  16. # Set some default params, if they didn't get set
  17. if [ -z "$DEST_IP" ]; then
  18. [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
  19. fi
  20. [ -z "$CLONE_SKB" ] && CLONE_SKB="0"
  21. # Example enforce param "-m" for dst_mac
  22. [ -z "$DST_MAC" ] && usage && err 2 "Must specify -m dst_mac"
  23. [ -z "$COUNT" ] && COUNT="100000" # Zero means indefinitely
  24. if [ -n "$DEST_IP" ]; then
  25. validate_addr${IP6} $DEST_IP
  26. read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
  27. fi
  28. if [ -n "$DST_PORT" ]; then
  29. read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
  30. validate_ports $UDP_DST_MIN $UDP_DST_MAX
  31. fi
  32. # Base Config
  33. DELAY="0" # Zero means max speed
  34. # Flow variation random source port between min and max
  35. UDP_SRC_MIN=9
  36. UDP_SRC_MAX=109
  37. # General cleanup everything since last run
  38. # (especially important if other threads were configured by other scripts)
  39. pg_ctrl "reset"
  40. # Add remove all other devices and add_device $DEV to thread 0
  41. thread=0
  42. pg_thread $thread "rem_device_all"
  43. pg_thread $thread "add_device" $DEV
  44. # How many packets to send (zero means indefinitely)
  45. pg_set $DEV "count $COUNT"
  46. # Reduce alloc cost by sending same SKB many times
  47. # - this obviously affects the randomness within the packet
  48. pg_set $DEV "clone_skb $CLONE_SKB"
  49. # Set packet size
  50. pg_set $DEV "pkt_size $PKT_SIZE"
  51. # Delay between packets (zero means max speed)
  52. pg_set $DEV "delay $DELAY"
  53. # Flag example disabling timestamping
  54. pg_set $DEV "flag NO_TIMESTAMP"
  55. # Destination
  56. pg_set $DEV "dst_mac $DST_MAC"
  57. pg_set $DEV "dst${IP6}_min $DST_MIN"
  58. pg_set $DEV "dst${IP6}_max $DST_MAX"
  59. if [ -n "$DST_PORT" ]; then
  60. # Single destination port or random port range
  61. pg_set $DEV "flag UDPDST_RND"
  62. pg_set $DEV "udp_dst_min $UDP_DST_MIN"
  63. pg_set $DEV "udp_dst_max $UDP_DST_MAX"
  64. fi
  65. # Setup random UDP port src range
  66. pg_set $DEV "flag UDPSRC_RND"
  67. pg_set $DEV "udp_src_min $UDP_SRC_MIN"
  68. pg_set $DEV "udp_src_max $UDP_SRC_MAX"
  69. # start_run
  70. echo "Running... ctrl^C to stop" >&2
  71. pg_ctrl "start"
  72. echo "Done" >&2
  73. # Print results
  74. echo "Result device: $DEV"
  75. cat /proc/net/pktgen/$DEV