pktgen.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ====================================
  3. HOWTO for the linux packet generator
  4. ====================================
  5. Enable CONFIG_NET_PKTGEN to compile and build pktgen either in-kernel
  6. or as a module. A module is preferred; modprobe pktgen if needed. Once
  7. running, pktgen creates a thread for each CPU with affinity to that CPU.
  8. Monitoring and controlling is done via /proc. It is easiest to select a
  9. suitable sample script and configure that.
  10. On a dual CPU::
  11. ps aux | grep pkt
  12. root 129 0.3 0.0 0 0 ? SW 2003 523:20 [kpktgend_0]
  13. root 130 0.3 0.0 0 0 ? SW 2003 509:50 [kpktgend_1]
  14. For monitoring and control pktgen creates::
  15. /proc/net/pktgen/pgctrl
  16. /proc/net/pktgen/kpktgend_X
  17. /proc/net/pktgen/ethX
  18. Tuning NIC for max performance
  19. ==============================
  20. The default NIC settings are (likely) not tuned for pktgen's artificial
  21. overload type of benchmarking, as this could hurt the normal use-case.
  22. Specifically increasing the TX ring buffer in the NIC::
  23. # ethtool -G ethX tx 1024
  24. A larger TX ring can improve pktgen's performance, while it can hurt
  25. in the general case, 1) because the TX ring buffer might get larger
  26. than the CPU's L1/L2 cache, 2) because it allows more queueing in the
  27. NIC HW layer (which is bad for bufferbloat).
  28. One should hesitate to conclude that packets/descriptors in the HW
  29. TX ring cause delay. Drivers usually delay cleaning up the
  30. ring-buffers for various performance reasons, and packets stalling
  31. the TX ring might just be waiting for cleanup.
  32. This cleanup issue is specifically the case for the driver ixgbe
  33. (Intel 82599 chip). This driver (ixgbe) combines TX+RX ring cleanups,
  34. and the cleanup interval is affected by the ethtool --coalesce setting
  35. of parameter "rx-usecs".
  36. For ixgbe use e.g. "30" resulting in approx 33K interrupts/sec (1/30*10^6)::
  37. # ethtool -C ethX rx-usecs 30
  38. Kernel threads
  39. ==============
  40. Pktgen creates a thread for each CPU with affinity to that CPU.
  41. Which is controlled through procfile /proc/net/pktgen/kpktgend_X.
  42. Example: /proc/net/pktgen/kpktgend_0::
  43. Running:
  44. Stopped: eth4@0
  45. Result: OK: add_device=eth4@0
  46. Most important are the devices assigned to the thread.
  47. The two basic thread commands are:
  48. * add_device DEVICE@NAME -- adds a single device
  49. * rem_device_all -- remove all associated devices
  50. When adding a device to a thread, a corresponding procfile is created
  51. which is used for configuring this device. Thus, device names need to
  52. be unique.
  53. To support adding the same device to multiple threads, which is useful
  54. with multi queue NICs, the device naming scheme is extended with "@":
  55. device@something
  56. The part after "@" can be anything, but it is custom to use the thread
  57. number.
  58. Viewing devices
  59. ===============
  60. The Params section holds configured information. The Current section
  61. holds running statistics. The Result is printed after a run or after
  62. interruption. Example::
  63. /proc/net/pktgen/eth4@0
  64. Params: count 100000 min_pkt_size: 60 max_pkt_size: 60
  65. frags: 0 delay: 0 clone_skb: 64 ifname: eth4@0
  66. flows: 0 flowlen: 0
  67. queue_map_min: 0 queue_map_max: 0
  68. dst_min: 192.168.81.2 dst_max:
  69. src_min: src_max:
  70. src_mac: 90:e2:ba:0a:56:b4 dst_mac: 00:1b:21:3c:9d:f8
  71. udp_src_min: 9 udp_src_max: 109 udp_dst_min: 9 udp_dst_max: 9
  72. src_mac_count: 0 dst_mac_count: 0
  73. Flags: UDPSRC_RND NO_TIMESTAMP QUEUE_MAP_CPU
  74. Current:
  75. pkts-sofar: 100000 errors: 0
  76. started: 623913381008us stopped: 623913396439us idle: 25us
  77. seq_num: 100001 cur_dst_mac_offset: 0 cur_src_mac_offset: 0
  78. cur_saddr: 192.168.8.3 cur_daddr: 192.168.81.2
  79. cur_udp_dst: 9 cur_udp_src: 42
  80. cur_queue_map: 0
  81. flows: 0
  82. Result: OK: 15430(c15405+d25) usec, 100000 (60byte,0frags)
  83. 6480562pps 3110Mb/sec (3110669760bps) errors: 0
  84. Configuring devices
  85. ===================
  86. This is done via the /proc interface, and most easily done via pgset
  87. as defined in the sample scripts.
  88. You need to specify PGDEV environment variable to use functions from sample
  89. scripts, i.e.::
  90. export PGDEV=/proc/net/pktgen/eth4@0
  91. source samples/pktgen/functions.sh
  92. Examples::
  93. pg_ctrl start starts injection.
  94. pg_ctrl stop aborts injection. Also, ^C aborts generator.
  95. pgset "clone_skb 1" sets the number of copies of the same packet
  96. pgset "clone_skb 0" use single SKB for all transmits
  97. pgset "burst 8" uses xmit_more API to queue 8 copies of the same
  98. packet and update HW tx queue tail pointer once.
  99. "burst 1" is the default
  100. pgset "pkt_size 9014" sets packet size to 9014
  101. pgset "frags 5" packet will consist of 5 fragments
  102. pgset "count 200000" sets number of packets to send, set to zero
  103. for continuous sends until explicitly stopped.
  104. pgset "delay 5000" adds delay to hard_start_xmit(). nanoseconds
  105. pgset "dst 10.0.0.1" sets IP destination address
  106. (BEWARE! This generator is very aggressive!)
  107. pgset "dst_min 10.0.0.1" Same as dst
  108. pgset "dst_max 10.0.0.254" Set the maximum destination IP.
  109. pgset "src_min 10.0.0.1" Set the minimum (or only) source IP.
  110. pgset "src_max 10.0.0.254" Set the maximum source IP.
  111. pgset "dst6 fec0::1" IPV6 destination address
  112. pgset "src6 fec0::2" IPV6 source address
  113. pgset "dstmac 00:00:00:00:00:00" sets MAC destination address
  114. pgset "srcmac 00:00:00:00:00:00" sets MAC source address
  115. pgset "queue_map_min 0" Sets the min value of tx queue interval
  116. pgset "queue_map_max 7" Sets the max value of tx queue interval, for multiqueue devices
  117. To select queue 1 of a given device,
  118. use queue_map_min=1 and queue_map_max=1
  119. pgset "src_mac_count 1" Sets the number of MACs we'll range through.
  120. The 'minimum' MAC is what you set with srcmac.
  121. pgset "dst_mac_count 1" Sets the number of MACs we'll range through.
  122. The 'minimum' MAC is what you set with dstmac.
  123. pgset "flag [name]" Set a flag to determine behaviour. Current flags
  124. are: IPSRC_RND # IP source is random (between min/max)
  125. IPDST_RND # IP destination is random
  126. UDPSRC_RND, UDPDST_RND,
  127. MACSRC_RND, MACDST_RND
  128. TXSIZE_RND, IPV6,
  129. MPLS_RND, VID_RND, SVID_RND
  130. FLOW_SEQ,
  131. QUEUE_MAP_RND # queue map random
  132. QUEUE_MAP_CPU # queue map mirrors smp_processor_id()
  133. UDPCSUM,
  134. IPSEC # IPsec encapsulation (needs CONFIG_XFRM)
  135. NODE_ALLOC # node specific memory allocation
  136. NO_TIMESTAMP # disable timestamping
  137. SHARED # enable shared SKB
  138. pgset 'flag ![name]' Clear a flag to determine behaviour.
  139. Note that you might need to use single quote in
  140. interactive mode, so that your shell wouldn't expand
  141. the specified flag as a history command.
  142. pgset "spi [SPI_VALUE]" Set specific SA used to transform packet.
  143. pgset "udp_src_min 9" set UDP source port min, If < udp_src_max, then
  144. cycle through the port range.
  145. pgset "udp_src_max 9" set UDP source port max.
  146. pgset "udp_dst_min 9" set UDP destination port min, If < udp_dst_max, then
  147. cycle through the port range.
  148. pgset "udp_dst_max 9" set UDP destination port max.
  149. pgset "mpls 0001000a,0002000a,0000000a" set MPLS labels (in this example
  150. outer label=16,middle label=32,
  151. inner label=0 (IPv4 NULL)) Note that
  152. there must be no spaces between the
  153. arguments. Leading zeros are required.
  154. Do not set the bottom of stack bit,
  155. that's done automatically. If you do
  156. set the bottom of stack bit, that
  157. indicates that you want to randomly
  158. generate that address and the flag
  159. MPLS_RND will be turned on. You
  160. can have any mix of random and fixed
  161. labels in the label stack.
  162. pgset "mpls 0" turn off mpls (or any invalid argument works too!)
  163. pgset "vlan_id 77" set VLAN ID 0-4095
  164. pgset "vlan_p 3" set priority bit 0-7 (default 0)
  165. pgset "vlan_cfi 0" set canonical format identifier 0-1 (default 0)
  166. pgset "svlan_id 22" set SVLAN ID 0-4095
  167. pgset "svlan_p 3" set priority bit 0-7 (default 0)
  168. pgset "svlan_cfi 0" set canonical format identifier 0-1 (default 0)
  169. pgset "vlan_id 9999" > 4095 remove vlan and svlan tags
  170. pgset "svlan 9999" > 4095 remove svlan tag
  171. pgset "tos XX" set former IPv4 TOS field (e.g. "tos 28" for AF11 no ECN, default 00)
  172. pgset "traffic_class XX" set former IPv6 TRAFFIC CLASS (e.g. "traffic_class B8" for EF no ECN, default 00)
  173. pgset "rate 300M" set rate to 300 Mb/s
  174. pgset "ratep 1000000" set rate to 1Mpps
  175. pgset "xmit_mode netif_receive" RX inject into stack netif_receive_skb()
  176. Works with "burst" but not with "clone_skb".
  177. Default xmit_mode is "start_xmit".
  178. Sample scripts
  179. ==============
  180. A collection of tutorial scripts and helpers for pktgen is in the
  181. samples/pktgen directory. The helper parameters.sh file support easy
  182. and consistent parameter parsing across the sample scripts.
  183. Usage example and help::
  184. ./pktgen_sample01_simple.sh -i eth4 -m 00:1B:21:3C:9D:F8 -d 192.168.8.2
  185. Usage:::
  186. ./pktgen_sample01_simple.sh [-vx] -i ethX
  187. -i : ($DEV) output interface/device (required)
  188. -s : ($PKT_SIZE) packet size
  189. -d : ($DEST_IP) destination IP. CIDR (e.g. 198.18.0.0/15) is also allowed
  190. -m : ($DST_MAC) destination MAC-addr
  191. -p : ($DST_PORT) destination PORT range (e.g. 433-444) is also allowed
  192. -t : ($THREADS) threads to start
  193. -f : ($F_THREAD) index of first thread (zero indexed CPU number)
  194. -c : ($SKB_CLONE) SKB clones send before alloc new SKB
  195. -n : ($COUNT) num messages to send per thread, 0 means indefinitely
  196. -b : ($BURST) HW level bursting of SKBs
  197. -v : ($VERBOSE) verbose
  198. -x : ($DEBUG) debug
  199. -6 : ($IP6) IPv6
  200. -w : ($DELAY) Tx Delay value (ns)
  201. -a : ($APPEND) Script will not reset generator's state, but will append its config
  202. The global variables being set are also listed. E.g. the required
  203. interface/device parameter "-i" sets variable $DEV. Copy the
  204. pktgen_sampleXX scripts and modify them to fit your own needs.
  205. Interrupt affinity
  206. ===================
  207. Note that when adding devices to a specific CPU it is a good idea to
  208. also assign /proc/irq/XX/smp_affinity so that the TX interrupts are bound
  209. to the same CPU. This reduces cache bouncing when freeing skbs.
  210. Plus using the device flag QUEUE_MAP_CPU, which maps the SKBs TX queue
  211. to the running threads CPU (directly from smp_processor_id()).
  212. Enable IPsec
  213. ============
  214. Default IPsec transformation with ESP encapsulation plus transport mode
  215. can be enabled by simply setting::
  216. pgset "flag IPSEC"
  217. pgset "flows 1"
  218. To avoid breaking existing testbed scripts for using AH type and tunnel mode,
  219. you can use "pgset spi SPI_VALUE" to specify which transformation mode
  220. to employ.
  221. Disable shared SKB
  222. ==================
  223. By default, SKBs sent by pktgen are shared (user count > 1).
  224. To test with non-shared SKBs, remove the "SHARED" flag by simply setting::
  225. pg_set "flag !SHARED"
  226. However, if the "clone_skb" or "burst" parameters are configured, the skb
  227. still needs to be held by pktgen for further access. Hence the skb must be
  228. shared.
  229. Current commands and configuration options
  230. ==========================================
  231. **Pgcontrol commands**::
  232. start
  233. stop
  234. reset
  235. **Thread commands**::
  236. add_device
  237. rem_device_all
  238. **Device commands**::
  239. count
  240. clone_skb
  241. burst
  242. debug
  243. frags
  244. delay
  245. src_mac_count
  246. dst_mac_count
  247. pkt_size
  248. min_pkt_size
  249. max_pkt_size
  250. queue_map_min
  251. queue_map_max
  252. skb_priority
  253. tos (ipv4)
  254. traffic_class (ipv6)
  255. mpls
  256. udp_src_min
  257. udp_src_max
  258. udp_dst_min
  259. udp_dst_max
  260. node
  261. flag
  262. IPSRC_RND
  263. IPDST_RND
  264. UDPSRC_RND
  265. UDPDST_RND
  266. MACSRC_RND
  267. MACDST_RND
  268. TXSIZE_RND
  269. IPV6
  270. MPLS_RND
  271. VID_RND
  272. SVID_RND
  273. FLOW_SEQ
  274. QUEUE_MAP_RND
  275. QUEUE_MAP_CPU
  276. UDPCSUM
  277. IPSEC
  278. NODE_ALLOC
  279. NO_TIMESTAMP
  280. SHARED
  281. spi (ipsec)
  282. dst_min
  283. dst_max
  284. src_min
  285. src_max
  286. dst_mac
  287. src_mac
  288. clear_counters
  289. src6
  290. dst6
  291. dst6_max
  292. dst6_min
  293. flows
  294. flowlen
  295. rate
  296. ratep
  297. xmit_mode <start_xmit|netif_receive>
  298. vlan_cfi
  299. vlan_id
  300. vlan_p
  301. svlan_cfi
  302. svlan_id
  303. svlan_p
  304. References:
  305. - ftp://robur.slu.se/pub/Linux/net-development/pktgen-testing/
  306. - ftp://robur.slu.se/pub/Linux/net-development/pktgen-testing/examples/
  307. Paper from Linux-Kongress in Erlangen 2004.
  308. - ftp://robur.slu.se/pub/Linux/net-development/pktgen-testing/pktgen_paper.pdf
  309. Thanks to:
  310. Grant Grundler for testing on IA-64 and parisc, Harald Welte, Lennert Buytenhek
  311. Stephen Hemminger, Andi Kleen, Dave Miller and many others.
  312. Good luck with the linux net-development.