pktgen_sample01_simple.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # Trap EXIT first
  16. trap_exit
  17. #
  18. # Set some default params, if they didn't get set
  19. if [ -z "$DEST_IP" ]; then
  20. [ -z "$IP6" ] && DEST_IP="198.18.0.42" || DEST_IP="FD00::1"
  21. fi
  22. [ -z "$CLONE_SKB" ] && CLONE_SKB="0"
  23. # Example enforce param "-m" for dst_mac
  24. [ -z "$DST_MAC" ] && usage && err 2 "Must specify -m dst_mac"
  25. [ -z "$COUNT" ] && COUNT="100000" # Zero means indefinitely
  26. if [ -n "$DEST_IP" ]; then
  27. validate_addr${IP6} $DEST_IP
  28. read -r DST_MIN DST_MAX <<< $(parse_addr${IP6} $DEST_IP)
  29. fi
  30. if [ -n "$DST_PORT" ]; then
  31. read -r UDP_DST_MIN UDP_DST_MAX <<< $(parse_ports $DST_PORT)
  32. validate_ports $UDP_DST_MIN $UDP_DST_MAX
  33. fi
  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. [ -z "$APPEND" ] && pg_ctrl "reset"
  40. # Add remove all other devices and add_device $DEV to thread 0
  41. thread=0
  42. [ -z "$APPEND" ] && 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. [ ! -z "$UDP_CSUM" ] && pg_set $DEV "flag UDPCSUM"
  66. # Setup random UDP port src range
  67. pg_set $DEV "flag UDPSRC_RND"
  68. pg_set $DEV "udp_src_min $UDP_SRC_MIN"
  69. pg_set $DEV "udp_src_max $UDP_SRC_MAX"
  70. # Run if user hits control-c
  71. function print_result() {
  72. # Print results
  73. echo "Result device: $DEV"
  74. cat /proc/net/pktgen/$DEV
  75. }
  76. # trap keyboard interrupt (Ctrl-C)
  77. trap true SIGINT
  78. if [ -z "$APPEND" ]; then
  79. # start_run
  80. echo "Running... ctrl^C to stop" >&2
  81. pg_ctrl "start"
  82. echo "Done" >&2
  83. print_result
  84. else
  85. echo "Append mode: config done. Do more or use 'pg_ctrl start' to run"
  86. fi