pktgen_sample01_simple.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. # Flow variation random source port between min and max
  33. UDP_SRC_MIN=9
  34. UDP_SRC_MAX=109
  35. # General cleanup everything since last run
  36. # (especially important if other threads were configured by other scripts)
  37. [ -z "$APPEND" ] && pg_ctrl "reset"
  38. # Add remove all other devices and add_device $DEV to thread 0
  39. thread=0
  40. [ -z "$APPEND" ] && pg_thread $thread "rem_device_all"
  41. pg_thread $thread "add_device" $DEV
  42. # How many packets to send (zero means indefinitely)
  43. pg_set $DEV "count $COUNT"
  44. # Reduce alloc cost by sending same SKB many times
  45. # - this obviously affects the randomness within the packet
  46. pg_set $DEV "clone_skb $CLONE_SKB"
  47. # Set packet size
  48. pg_set $DEV "pkt_size $PKT_SIZE"
  49. # Delay between packets (zero means max speed)
  50. pg_set $DEV "delay $DELAY"
  51. # Flag example disabling timestamping
  52. pg_set $DEV "flag NO_TIMESTAMP"
  53. # Destination
  54. pg_set $DEV "dst_mac $DST_MAC"
  55. pg_set $DEV "dst${IP6}_min $DST_MIN"
  56. pg_set $DEV "dst${IP6}_max $DST_MAX"
  57. if [ -n "$DST_PORT" ]; then
  58. # Single destination port or random port range
  59. pg_set $DEV "flag UDPDST_RND"
  60. pg_set $DEV "udp_dst_min $UDP_DST_MIN"
  61. pg_set $DEV "udp_dst_max $UDP_DST_MAX"
  62. fi
  63. [ ! -z "$UDP_CSUM" ] && pg_set $dev "flag UDPCSUM"
  64. # Setup random UDP port src range
  65. pg_set $DEV "flag UDPSRC_RND"
  66. pg_set $DEV "udp_src_min $UDP_SRC_MIN"
  67. pg_set $DEV "udp_src_max $UDP_SRC_MAX"
  68. # Run if user hits control-c
  69. function print_result() {
  70. # Print results
  71. echo "Result device: $DEV"
  72. cat /proc/net/pktgen/$DEV
  73. }
  74. # trap keyboard interrupt (Ctrl-C)
  75. trap true SIGINT
  76. if [ -z "$APPEND" ]; then
  77. # start_run
  78. echo "Running... ctrl^C to stop" >&2
  79. pg_ctrl "start"
  80. echo "Done" >&2
  81. print_result
  82. else
  83. echo "Append mode: config done. Do more or use 'pg_ctrl start' to run"
  84. fi