pktgen.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958
  1. /*
  2. * Authors:
  3. * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
  4. * Uppsala University and
  5. * Swedish University of Agricultural Sciences
  6. *
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. * Ben Greear <greearb@candelatech.com>
  9. * Jens Låås <jens.laas@data.slu.se>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. *
  17. * A tool for loading the network with preconfigurated packets.
  18. * The tool is implemented as a linux module. Parameters are output
  19. * device, delay (to hard_xmit), number of packets, and whether
  20. * to use multiple SKBs or just the same one.
  21. * pktgen uses the installed interface's output routine.
  22. *
  23. * Additional hacking by:
  24. *
  25. * Jens.Laas@data.slu.se
  26. * Improved by ANK. 010120.
  27. * Improved by ANK even more. 010212.
  28. * MAC address typo fixed. 010417 --ro
  29. * Integrated. 020301 --DaveM
  30. * Added multiskb option 020301 --DaveM
  31. * Scaling of results. 020417--sigurdur@linpro.no
  32. * Significant re-work of the module:
  33. * * Convert to threaded model to more efficiently be able to transmit
  34. * and receive on multiple interfaces at once.
  35. * * Converted many counters to __u64 to allow longer runs.
  36. * * Allow configuration of ranges, like min/max IP address, MACs,
  37. * and UDP-ports, for both source and destination, and can
  38. * set to use a random distribution or sequentially walk the range.
  39. * * Can now change most values after starting.
  40. * * Place 12-byte packet in UDP payload with magic number,
  41. * sequence number, and timestamp.
  42. * * Add receiver code that detects dropped pkts, re-ordered pkts, and
  43. * latencies (with micro-second) precision.
  44. * * Add IOCTL interface to easily get counters & configuration.
  45. * --Ben Greear <greearb@candelatech.com>
  46. *
  47. * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
  48. * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
  49. * as a "fastpath" with a configurable number of clones after alloc's.
  50. * clone_skb=0 means all packets are allocated this also means ranges time
  51. * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
  52. * clones.
  53. *
  54. * Also moved to /proc/net/pktgen/
  55. * --ro
  56. *
  57. * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
  58. * mistakes. Also merged in DaveM's patch in the -pre6 patch.
  59. * --Ben Greear <greearb@candelatech.com>
  60. *
  61. * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
  62. *
  63. *
  64. * 021124 Finished major redesign and rewrite for new functionality.
  65. * See Documentation/networking/pktgen.txt for how to use this.
  66. *
  67. * The new operation:
  68. * For each CPU one thread/process is created at start. This process checks
  69. * for running devices in the if_list and sends packets until count is 0 it
  70. * also the thread checks the thread->control which is used for inter-process
  71. * communication. controlling process "posts" operations to the threads this
  72. * way.
  73. * The if_list is RCU protected, and the if_lock remains to protect updating
  74. * of if_list, from "add_device" as it invoked from userspace (via proc write).
  75. *
  76. * By design there should only be *one* "controlling" process. In practice
  77. * multiple write accesses gives unpredictable result. Understood by "write"
  78. * to /proc gives result code thats should be read be the "writer".
  79. * For practical use this should be no problem.
  80. *
  81. * Note when adding devices to a specific CPU there good idea to also assign
  82. * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
  83. * --ro
  84. *
  85. * Fix refcount off by one if first packet fails, potential null deref,
  86. * memleak 030710- KJP
  87. *
  88. * First "ranges" functionality for ipv6 030726 --ro
  89. *
  90. * Included flow support. 030802 ANK.
  91. *
  92. * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
  93. *
  94. * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
  95. * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
  96. *
  97. * New xmit() return, do_div and misc clean up by Stephen Hemminger
  98. * <shemminger@osdl.org> 040923
  99. *
  100. * Randy Dunlap fixed u64 printk compiler warning
  101. *
  102. * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
  103. * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
  104. *
  105. * Corrections from Nikolai Malykh (nmalykh@bilim.com)
  106. * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
  107. *
  108. * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
  109. * 050103
  110. *
  111. * MPLS support by Steven Whitehouse <steve@chygwyn.com>
  112. *
  113. * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
  114. *
  115. * Fixed src_mac command to set source mac of packet to value specified in
  116. * command by Adit Ranadive <adit.262@gmail.com>
  117. *
  118. */
  119. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  120. #include <linux/sys.h>
  121. #include <linux/types.h>
  122. #include <linux/module.h>
  123. #include <linux/moduleparam.h>
  124. #include <linux/kernel.h>
  125. #include <linux/mutex.h>
  126. #include <linux/sched.h>
  127. #include <linux/slab.h>
  128. #include <linux/vmalloc.h>
  129. #include <linux/unistd.h>
  130. #include <linux/string.h>
  131. #include <linux/ptrace.h>
  132. #include <linux/errno.h>
  133. #include <linux/ioport.h>
  134. #include <linux/interrupt.h>
  135. #include <linux/capability.h>
  136. #include <linux/hrtimer.h>
  137. #include <linux/freezer.h>
  138. #include <linux/delay.h>
  139. #include <linux/timer.h>
  140. #include <linux/list.h>
  141. #include <linux/init.h>
  142. #include <linux/skbuff.h>
  143. #include <linux/netdevice.h>
  144. #include <linux/inet.h>
  145. #include <linux/inetdevice.h>
  146. #include <linux/rtnetlink.h>
  147. #include <linux/if_arp.h>
  148. #include <linux/if_vlan.h>
  149. #include <linux/in.h>
  150. #include <linux/ip.h>
  151. #include <linux/ipv6.h>
  152. #include <linux/udp.h>
  153. #include <linux/proc_fs.h>
  154. #include <linux/seq_file.h>
  155. #include <linux/wait.h>
  156. #include <linux/etherdevice.h>
  157. #include <linux/kthread.h>
  158. #include <linux/prefetch.h>
  159. #include <net/net_namespace.h>
  160. #include <net/checksum.h>
  161. #include <net/ipv6.h>
  162. #include <net/udp.h>
  163. #include <net/ip6_checksum.h>
  164. #include <net/addrconf.h>
  165. #ifdef CONFIG_XFRM
  166. #include <net/xfrm.h>
  167. #endif
  168. #include <net/netns/generic.h>
  169. #include <asm/byteorder.h>
  170. #include <linux/rcupdate.h>
  171. #include <linux/bitops.h>
  172. #include <linux/io.h>
  173. #include <linux/timex.h>
  174. #include <linux/uaccess.h>
  175. #include <asm/dma.h>
  176. #include <asm/div64.h> /* do_div */
  177. #define VERSION "2.75"
  178. #define IP_NAME_SZ 32
  179. #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
  180. #define MPLS_STACK_BOTTOM htonl(0x00000100)
  181. #define func_enter() pr_debug("entering %s\n", __func__);
  182. #define PKT_FLAGS \
  183. pf(IPV6) /* Interface in IPV6 Mode */ \
  184. pf(IPSRC_RND) /* IP-Src Random */ \
  185. pf(IPDST_RND) /* IP-Dst Random */ \
  186. pf(TXSIZE_RND) /* Transmit size is random */ \
  187. pf(UDPSRC_RND) /* UDP-Src Random */ \
  188. pf(UDPDST_RND) /* UDP-Dst Random */ \
  189. pf(UDPCSUM) /* Include UDP checksum */ \
  190. pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
  191. pf(MPLS_RND) /* Random MPLS labels */ \
  192. pf(QUEUE_MAP_RND) /* queue map Random */ \
  193. pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
  194. pf(FLOW_SEQ) /* Sequential flows */ \
  195. pf(IPSEC) /* ipsec on for flows */ \
  196. pf(MACSRC_RND) /* MAC-Src Random */ \
  197. pf(MACDST_RND) /* MAC-Dst Random */ \
  198. pf(VID_RND) /* Random VLAN ID */ \
  199. pf(SVID_RND) /* Random SVLAN ID */ \
  200. pf(NODE) /* Node memory alloc*/ \
  201. #define pf(flag) flag##_SHIFT,
  202. enum pkt_flags {
  203. PKT_FLAGS
  204. };
  205. #undef pf
  206. /* Device flag bits */
  207. #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
  208. PKT_FLAGS
  209. #undef pf
  210. #define pf(flag) __stringify(flag),
  211. static char *pkt_flag_names[] = {
  212. PKT_FLAGS
  213. };
  214. #undef pf
  215. #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
  216. /* Thread control flag bits */
  217. #define T_STOP (1<<0) /* Stop run */
  218. #define T_RUN (1<<1) /* Start run */
  219. #define T_REMDEVALL (1<<2) /* Remove all devs */
  220. #define T_REMDEV (1<<3) /* Remove one dev */
  221. /* Xmit modes */
  222. #define M_START_XMIT 0 /* Default normal TX */
  223. #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
  224. #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
  225. /* If lock -- protects updating of if_list */
  226. #define if_lock(t) mutex_lock(&(t->if_lock));
  227. #define if_unlock(t) mutex_unlock(&(t->if_lock));
  228. /* Used to help with determining the pkts on receive */
  229. #define PKTGEN_MAGIC 0xbe9be955
  230. #define PG_PROC_DIR "pktgen"
  231. #define PGCTRL "pgctrl"
  232. #define MAX_CFLOWS 65536
  233. #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
  234. #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
  235. struct flow_state {
  236. __be32 cur_daddr;
  237. int count;
  238. #ifdef CONFIG_XFRM
  239. struct xfrm_state *x;
  240. #endif
  241. __u32 flags;
  242. };
  243. /* flow flag bits */
  244. #define F_INIT (1<<0) /* flow has been initialized */
  245. struct pktgen_dev {
  246. /*
  247. * Try to keep frequent/infrequent used vars. separated.
  248. */
  249. struct proc_dir_entry *entry; /* proc file */
  250. struct pktgen_thread *pg_thread;/* the owner */
  251. struct list_head list; /* chaining in the thread's run-queue */
  252. struct rcu_head rcu; /* freed by RCU */
  253. int running; /* if false, the test will stop */
  254. /* If min != max, then we will either do a linear iteration, or
  255. * we will do a random selection from within the range.
  256. */
  257. __u32 flags;
  258. int xmit_mode;
  259. int min_pkt_size;
  260. int max_pkt_size;
  261. int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
  262. int nfrags;
  263. int removal_mark; /* non-zero => the device is marked for
  264. * removal by worker thread */
  265. struct page *page;
  266. u64 delay; /* nano-seconds */
  267. __u64 count; /* Default No packets to send */
  268. __u64 sofar; /* How many pkts we've sent so far */
  269. __u64 tx_bytes; /* How many bytes we've transmitted */
  270. __u64 errors; /* Errors when trying to transmit, */
  271. /* runtime counters relating to clone_skb */
  272. __u32 clone_count;
  273. int last_ok; /* Was last skb sent?
  274. * Or a failed transmit of some sort?
  275. * This will keep sequence numbers in order
  276. */
  277. ktime_t next_tx;
  278. ktime_t started_at;
  279. ktime_t stopped_at;
  280. u64 idle_acc; /* nano-seconds */
  281. __u32 seq_num;
  282. int clone_skb; /*
  283. * Use multiple SKBs during packet gen.
  284. * If this number is greater than 1, then
  285. * that many copies of the same packet will be
  286. * sent before a new packet is allocated.
  287. * If you want to send 1024 identical packets
  288. * before creating a new packet,
  289. * set clone_skb to 1024.
  290. */
  291. char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  292. char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  293. char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  294. char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  295. struct in6_addr in6_saddr;
  296. struct in6_addr in6_daddr;
  297. struct in6_addr cur_in6_daddr;
  298. struct in6_addr cur_in6_saddr;
  299. /* For ranges */
  300. struct in6_addr min_in6_daddr;
  301. struct in6_addr max_in6_daddr;
  302. struct in6_addr min_in6_saddr;
  303. struct in6_addr max_in6_saddr;
  304. /* If we're doing ranges, random or incremental, then this
  305. * defines the min/max for those ranges.
  306. */
  307. __be32 saddr_min; /* inclusive, source IP address */
  308. __be32 saddr_max; /* exclusive, source IP address */
  309. __be32 daddr_min; /* inclusive, dest IP address */
  310. __be32 daddr_max; /* exclusive, dest IP address */
  311. __u16 udp_src_min; /* inclusive, source UDP port */
  312. __u16 udp_src_max; /* exclusive, source UDP port */
  313. __u16 udp_dst_min; /* inclusive, dest UDP port */
  314. __u16 udp_dst_max; /* exclusive, dest UDP port */
  315. /* DSCP + ECN */
  316. __u8 tos; /* six MSB of (former) IPv4 TOS
  317. are for dscp codepoint */
  318. __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
  319. (see RFC 3260, sec. 4) */
  320. /* MPLS */
  321. unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
  322. __be32 labels[MAX_MPLS_LABELS];
  323. /* VLAN/SVLAN (802.1Q/Q-in-Q) */
  324. __u8 vlan_p;
  325. __u8 vlan_cfi;
  326. __u16 vlan_id; /* 0xffff means no vlan tag */
  327. __u8 svlan_p;
  328. __u8 svlan_cfi;
  329. __u16 svlan_id; /* 0xffff means no svlan tag */
  330. __u32 src_mac_count; /* How many MACs to iterate through */
  331. __u32 dst_mac_count; /* How many MACs to iterate through */
  332. unsigned char dst_mac[ETH_ALEN];
  333. unsigned char src_mac[ETH_ALEN];
  334. __u32 cur_dst_mac_offset;
  335. __u32 cur_src_mac_offset;
  336. __be32 cur_saddr;
  337. __be32 cur_daddr;
  338. __u16 ip_id;
  339. __u16 cur_udp_dst;
  340. __u16 cur_udp_src;
  341. __u16 cur_queue_map;
  342. __u32 cur_pkt_size;
  343. __u32 last_pkt_size;
  344. __u8 hh[14];
  345. /* = {
  346. 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
  347. We fill in SRC address later
  348. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  349. 0x08, 0x00
  350. };
  351. */
  352. __u16 pad; /* pad out the hh struct to an even 16 bytes */
  353. struct sk_buff *skb; /* skb we are to transmit next, used for when we
  354. * are transmitting the same one multiple times
  355. */
  356. struct net_device *odev; /* The out-going device.
  357. * Note that the device should have it's
  358. * pg_info pointer pointing back to this
  359. * device.
  360. * Set when the user specifies the out-going
  361. * device name (not when the inject is
  362. * started as it used to do.)
  363. */
  364. char odevname[32];
  365. struct flow_state *flows;
  366. unsigned int cflows; /* Concurrent flows (config) */
  367. unsigned int lflow; /* Flow length (config) */
  368. unsigned int nflows; /* accumulated flows (stats) */
  369. unsigned int curfl; /* current sequenced flow (state)*/
  370. u16 queue_map_min;
  371. u16 queue_map_max;
  372. __u32 skb_priority; /* skb priority field */
  373. unsigned int burst; /* number of duplicated packets to burst */
  374. int node; /* Memory node */
  375. #ifdef CONFIG_XFRM
  376. __u8 ipsmode; /* IPSEC mode (config) */
  377. __u8 ipsproto; /* IPSEC type (config) */
  378. __u32 spi;
  379. struct xfrm_dst xdst;
  380. struct dst_ops dstops;
  381. #endif
  382. char result[512];
  383. };
  384. struct pktgen_hdr {
  385. __be32 pgh_magic;
  386. __be32 seq_num;
  387. __be32 tv_sec;
  388. __be32 tv_usec;
  389. };
  390. static unsigned int pg_net_id __read_mostly;
  391. struct pktgen_net {
  392. struct net *net;
  393. struct proc_dir_entry *proc_dir;
  394. struct list_head pktgen_threads;
  395. bool pktgen_exiting;
  396. };
  397. struct pktgen_thread {
  398. struct mutex if_lock; /* for list of devices */
  399. struct list_head if_list; /* All device here */
  400. struct list_head th_list;
  401. struct task_struct *tsk;
  402. char result[512];
  403. /* Field for thread to receive "posted" events terminate,
  404. stop ifs etc. */
  405. u32 control;
  406. int cpu;
  407. wait_queue_head_t queue;
  408. struct completion start_done;
  409. struct pktgen_net *net;
  410. };
  411. #define REMOVE 1
  412. #define FIND 0
  413. static const char version[] =
  414. "Packet Generator for packet performance testing. "
  415. "Version: " VERSION "\n";
  416. static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
  417. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
  418. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  419. const char *ifname, bool exact);
  420. static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
  421. static void pktgen_run_all_threads(struct pktgen_net *pn);
  422. static void pktgen_reset_all_threads(struct pktgen_net *pn);
  423. static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
  424. static void pktgen_stop(struct pktgen_thread *t);
  425. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
  426. /* Module parameters, defaults. */
  427. static int pg_count_d __read_mostly = 1000;
  428. static int pg_delay_d __read_mostly;
  429. static int pg_clone_skb_d __read_mostly;
  430. static int debug __read_mostly;
  431. static DEFINE_MUTEX(pktgen_thread_lock);
  432. static struct notifier_block pktgen_notifier_block = {
  433. .notifier_call = pktgen_device_event,
  434. };
  435. /*
  436. * /proc handling functions
  437. *
  438. */
  439. static int pgctrl_show(struct seq_file *seq, void *v)
  440. {
  441. seq_puts(seq, version);
  442. return 0;
  443. }
  444. static ssize_t pgctrl_write(struct file *file, const char __user *buf,
  445. size_t count, loff_t *ppos)
  446. {
  447. char data[128];
  448. struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
  449. if (!capable(CAP_NET_ADMIN))
  450. return -EPERM;
  451. if (count == 0)
  452. return -EINVAL;
  453. if (count > sizeof(data))
  454. count = sizeof(data);
  455. if (copy_from_user(data, buf, count))
  456. return -EFAULT;
  457. data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
  458. if (!strcmp(data, "stop"))
  459. pktgen_stop_all_threads_ifs(pn);
  460. else if (!strcmp(data, "start"))
  461. pktgen_run_all_threads(pn);
  462. else if (!strcmp(data, "reset"))
  463. pktgen_reset_all_threads(pn);
  464. else
  465. return -EINVAL;
  466. return count;
  467. }
  468. static int pgctrl_open(struct inode *inode, struct file *file)
  469. {
  470. return single_open(file, pgctrl_show, PDE_DATA(inode));
  471. }
  472. static const struct file_operations pktgen_fops = {
  473. .open = pgctrl_open,
  474. .read = seq_read,
  475. .llseek = seq_lseek,
  476. .write = pgctrl_write,
  477. .release = single_release,
  478. };
  479. static int pktgen_if_show(struct seq_file *seq, void *v)
  480. {
  481. const struct pktgen_dev *pkt_dev = seq->private;
  482. ktime_t stopped;
  483. unsigned int i;
  484. u64 idle;
  485. seq_printf(seq,
  486. "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
  487. (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
  488. pkt_dev->max_pkt_size);
  489. seq_printf(seq,
  490. " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
  491. pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
  492. pkt_dev->clone_skb, pkt_dev->odevname);
  493. seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
  494. pkt_dev->lflow);
  495. seq_printf(seq,
  496. " queue_map_min: %u queue_map_max: %u\n",
  497. pkt_dev->queue_map_min,
  498. pkt_dev->queue_map_max);
  499. if (pkt_dev->skb_priority)
  500. seq_printf(seq, " skb_priority: %u\n",
  501. pkt_dev->skb_priority);
  502. if (pkt_dev->flags & F_IPV6) {
  503. seq_printf(seq,
  504. " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
  505. " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
  506. &pkt_dev->in6_saddr,
  507. &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
  508. &pkt_dev->in6_daddr,
  509. &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
  510. } else {
  511. seq_printf(seq,
  512. " dst_min: %s dst_max: %s\n",
  513. pkt_dev->dst_min, pkt_dev->dst_max);
  514. seq_printf(seq,
  515. " src_min: %s src_max: %s\n",
  516. pkt_dev->src_min, pkt_dev->src_max);
  517. }
  518. seq_puts(seq, " src_mac: ");
  519. seq_printf(seq, "%pM ",
  520. is_zero_ether_addr(pkt_dev->src_mac) ?
  521. pkt_dev->odev->dev_addr : pkt_dev->src_mac);
  522. seq_puts(seq, "dst_mac: ");
  523. seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
  524. seq_printf(seq,
  525. " udp_src_min: %d udp_src_max: %d"
  526. " udp_dst_min: %d udp_dst_max: %d\n",
  527. pkt_dev->udp_src_min, pkt_dev->udp_src_max,
  528. pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
  529. seq_printf(seq,
  530. " src_mac_count: %d dst_mac_count: %d\n",
  531. pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
  532. if (pkt_dev->nr_labels) {
  533. seq_puts(seq, " mpls: ");
  534. for (i = 0; i < pkt_dev->nr_labels; i++)
  535. seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
  536. i == pkt_dev->nr_labels-1 ? "\n" : ", ");
  537. }
  538. if (pkt_dev->vlan_id != 0xffff)
  539. seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
  540. pkt_dev->vlan_id, pkt_dev->vlan_p,
  541. pkt_dev->vlan_cfi);
  542. if (pkt_dev->svlan_id != 0xffff)
  543. seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
  544. pkt_dev->svlan_id, pkt_dev->svlan_p,
  545. pkt_dev->svlan_cfi);
  546. if (pkt_dev->tos)
  547. seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
  548. if (pkt_dev->traffic_class)
  549. seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
  550. if (pkt_dev->burst > 1)
  551. seq_printf(seq, " burst: %d\n", pkt_dev->burst);
  552. if (pkt_dev->node >= 0)
  553. seq_printf(seq, " node: %d\n", pkt_dev->node);
  554. if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
  555. seq_puts(seq, " xmit_mode: netif_receive\n");
  556. else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
  557. seq_puts(seq, " xmit_mode: xmit_queue\n");
  558. seq_puts(seq, " Flags: ");
  559. for (i = 0; i < NR_PKT_FLAGS; i++) {
  560. if (i == F_FLOW_SEQ)
  561. if (!pkt_dev->cflows)
  562. continue;
  563. if (pkt_dev->flags & (1 << i))
  564. seq_printf(seq, "%s ", pkt_flag_names[i]);
  565. else if (i == F_FLOW_SEQ)
  566. seq_puts(seq, "FLOW_RND ");
  567. #ifdef CONFIG_XFRM
  568. if (i == F_IPSEC && pkt_dev->spi)
  569. seq_printf(seq, "spi:%u", pkt_dev->spi);
  570. #endif
  571. }
  572. seq_puts(seq, "\n");
  573. /* not really stopped, more like last-running-at */
  574. stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
  575. idle = pkt_dev->idle_acc;
  576. do_div(idle, NSEC_PER_USEC);
  577. seq_printf(seq,
  578. "Current:\n pkts-sofar: %llu errors: %llu\n",
  579. (unsigned long long)pkt_dev->sofar,
  580. (unsigned long long)pkt_dev->errors);
  581. seq_printf(seq,
  582. " started: %lluus stopped: %lluus idle: %lluus\n",
  583. (unsigned long long) ktime_to_us(pkt_dev->started_at),
  584. (unsigned long long) ktime_to_us(stopped),
  585. (unsigned long long) idle);
  586. seq_printf(seq,
  587. " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
  588. pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
  589. pkt_dev->cur_src_mac_offset);
  590. if (pkt_dev->flags & F_IPV6) {
  591. seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
  592. &pkt_dev->cur_in6_saddr,
  593. &pkt_dev->cur_in6_daddr);
  594. } else
  595. seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
  596. &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
  597. seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
  598. pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
  599. seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
  600. seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
  601. if (pkt_dev->result[0])
  602. seq_printf(seq, "Result: %s\n", pkt_dev->result);
  603. else
  604. seq_puts(seq, "Result: Idle\n");
  605. return 0;
  606. }
  607. static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
  608. __u32 *num)
  609. {
  610. int i = 0;
  611. *num = 0;
  612. for (; i < maxlen; i++) {
  613. int value;
  614. char c;
  615. *num <<= 4;
  616. if (get_user(c, &user_buffer[i]))
  617. return -EFAULT;
  618. value = hex_to_bin(c);
  619. if (value >= 0)
  620. *num |= value;
  621. else
  622. break;
  623. }
  624. return i;
  625. }
  626. static int count_trail_chars(const char __user * user_buffer,
  627. unsigned int maxlen)
  628. {
  629. int i;
  630. for (i = 0; i < maxlen; i++) {
  631. char c;
  632. if (get_user(c, &user_buffer[i]))
  633. return -EFAULT;
  634. switch (c) {
  635. case '\"':
  636. case '\n':
  637. case '\r':
  638. case '\t':
  639. case ' ':
  640. case '=':
  641. break;
  642. default:
  643. goto done;
  644. }
  645. }
  646. done:
  647. return i;
  648. }
  649. static long num_arg(const char __user *user_buffer, unsigned long maxlen,
  650. unsigned long *num)
  651. {
  652. int i;
  653. *num = 0;
  654. for (i = 0; i < maxlen; i++) {
  655. char c;
  656. if (get_user(c, &user_buffer[i]))
  657. return -EFAULT;
  658. if ((c >= '0') && (c <= '9')) {
  659. *num *= 10;
  660. *num += c - '0';
  661. } else
  662. break;
  663. }
  664. return i;
  665. }
  666. static int strn_len(const char __user * user_buffer, unsigned int maxlen)
  667. {
  668. int i;
  669. for (i = 0; i < maxlen; i++) {
  670. char c;
  671. if (get_user(c, &user_buffer[i]))
  672. return -EFAULT;
  673. switch (c) {
  674. case '\"':
  675. case '\n':
  676. case '\r':
  677. case '\t':
  678. case ' ':
  679. goto done_str;
  680. default:
  681. break;
  682. }
  683. }
  684. done_str:
  685. return i;
  686. }
  687. static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
  688. {
  689. unsigned int n = 0;
  690. char c;
  691. ssize_t i = 0;
  692. int len;
  693. pkt_dev->nr_labels = 0;
  694. do {
  695. __u32 tmp;
  696. len = hex32_arg(&buffer[i], 8, &tmp);
  697. if (len <= 0)
  698. return len;
  699. pkt_dev->labels[n] = htonl(tmp);
  700. if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
  701. pkt_dev->flags |= F_MPLS_RND;
  702. i += len;
  703. if (get_user(c, &buffer[i]))
  704. return -EFAULT;
  705. i++;
  706. n++;
  707. if (n >= MAX_MPLS_LABELS)
  708. return -E2BIG;
  709. } while (c == ',');
  710. pkt_dev->nr_labels = n;
  711. return i;
  712. }
  713. static ssize_t pktgen_if_write(struct file *file,
  714. const char __user * user_buffer, size_t count,
  715. loff_t * offset)
  716. {
  717. struct seq_file *seq = file->private_data;
  718. struct pktgen_dev *pkt_dev = seq->private;
  719. int i, max, len;
  720. char name[16], valstr[32];
  721. unsigned long value = 0;
  722. char *pg_result = NULL;
  723. int tmp = 0;
  724. char buf[128];
  725. pg_result = &(pkt_dev->result[0]);
  726. if (count < 1) {
  727. pr_warn("wrong command format\n");
  728. return -EINVAL;
  729. }
  730. max = count;
  731. tmp = count_trail_chars(user_buffer, max);
  732. if (tmp < 0) {
  733. pr_warn("illegal format\n");
  734. return tmp;
  735. }
  736. i = tmp;
  737. /* Read variable name */
  738. len = strn_len(&user_buffer[i], sizeof(name) - 1);
  739. if (len < 0)
  740. return len;
  741. memset(name, 0, sizeof(name));
  742. if (copy_from_user(name, &user_buffer[i], len))
  743. return -EFAULT;
  744. i += len;
  745. max = count - i;
  746. len = count_trail_chars(&user_buffer[i], max);
  747. if (len < 0)
  748. return len;
  749. i += len;
  750. if (debug) {
  751. size_t copy = min_t(size_t, count, 1023);
  752. char tb[copy + 1];
  753. if (copy_from_user(tb, user_buffer, copy))
  754. return -EFAULT;
  755. tb[copy] = 0;
  756. pr_debug("%s,%lu buffer -:%s:-\n",
  757. name, (unsigned long)count, tb);
  758. }
  759. if (!strcmp(name, "min_pkt_size")) {
  760. len = num_arg(&user_buffer[i], 10, &value);
  761. if (len < 0)
  762. return len;
  763. i += len;
  764. if (value < 14 + 20 + 8)
  765. value = 14 + 20 + 8;
  766. if (value != pkt_dev->min_pkt_size) {
  767. pkt_dev->min_pkt_size = value;
  768. pkt_dev->cur_pkt_size = value;
  769. }
  770. sprintf(pg_result, "OK: min_pkt_size=%u",
  771. pkt_dev->min_pkt_size);
  772. return count;
  773. }
  774. if (!strcmp(name, "max_pkt_size")) {
  775. len = num_arg(&user_buffer[i], 10, &value);
  776. if (len < 0)
  777. return len;
  778. i += len;
  779. if (value < 14 + 20 + 8)
  780. value = 14 + 20 + 8;
  781. if (value != pkt_dev->max_pkt_size) {
  782. pkt_dev->max_pkt_size = value;
  783. pkt_dev->cur_pkt_size = value;
  784. }
  785. sprintf(pg_result, "OK: max_pkt_size=%u",
  786. pkt_dev->max_pkt_size);
  787. return count;
  788. }
  789. /* Shortcut for min = max */
  790. if (!strcmp(name, "pkt_size")) {
  791. len = num_arg(&user_buffer[i], 10, &value);
  792. if (len < 0)
  793. return len;
  794. i += len;
  795. if (value < 14 + 20 + 8)
  796. value = 14 + 20 + 8;
  797. if (value != pkt_dev->min_pkt_size) {
  798. pkt_dev->min_pkt_size = value;
  799. pkt_dev->max_pkt_size = value;
  800. pkt_dev->cur_pkt_size = value;
  801. }
  802. sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
  803. return count;
  804. }
  805. if (!strcmp(name, "debug")) {
  806. len = num_arg(&user_buffer[i], 10, &value);
  807. if (len < 0)
  808. return len;
  809. i += len;
  810. debug = value;
  811. sprintf(pg_result, "OK: debug=%u", debug);
  812. return count;
  813. }
  814. if (!strcmp(name, "frags")) {
  815. len = num_arg(&user_buffer[i], 10, &value);
  816. if (len < 0)
  817. return len;
  818. i += len;
  819. pkt_dev->nfrags = value;
  820. sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
  821. return count;
  822. }
  823. if (!strcmp(name, "delay")) {
  824. len = num_arg(&user_buffer[i], 10, &value);
  825. if (len < 0)
  826. return len;
  827. i += len;
  828. if (value == 0x7FFFFFFF)
  829. pkt_dev->delay = ULLONG_MAX;
  830. else
  831. pkt_dev->delay = (u64)value;
  832. sprintf(pg_result, "OK: delay=%llu",
  833. (unsigned long long) pkt_dev->delay);
  834. return count;
  835. }
  836. if (!strcmp(name, "rate")) {
  837. len = num_arg(&user_buffer[i], 10, &value);
  838. if (len < 0)
  839. return len;
  840. i += len;
  841. if (!value)
  842. return len;
  843. pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
  844. if (debug)
  845. pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
  846. sprintf(pg_result, "OK: rate=%lu", value);
  847. return count;
  848. }
  849. if (!strcmp(name, "ratep")) {
  850. len = num_arg(&user_buffer[i], 10, &value);
  851. if (len < 0)
  852. return len;
  853. i += len;
  854. if (!value)
  855. return len;
  856. pkt_dev->delay = NSEC_PER_SEC/value;
  857. if (debug)
  858. pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
  859. sprintf(pg_result, "OK: rate=%lu", value);
  860. return count;
  861. }
  862. if (!strcmp(name, "udp_src_min")) {
  863. len = num_arg(&user_buffer[i], 10, &value);
  864. if (len < 0)
  865. return len;
  866. i += len;
  867. if (value != pkt_dev->udp_src_min) {
  868. pkt_dev->udp_src_min = value;
  869. pkt_dev->cur_udp_src = value;
  870. }
  871. sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
  872. return count;
  873. }
  874. if (!strcmp(name, "udp_dst_min")) {
  875. len = num_arg(&user_buffer[i], 10, &value);
  876. if (len < 0)
  877. return len;
  878. i += len;
  879. if (value != pkt_dev->udp_dst_min) {
  880. pkt_dev->udp_dst_min = value;
  881. pkt_dev->cur_udp_dst = value;
  882. }
  883. sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
  884. return count;
  885. }
  886. if (!strcmp(name, "udp_src_max")) {
  887. len = num_arg(&user_buffer[i], 10, &value);
  888. if (len < 0)
  889. return len;
  890. i += len;
  891. if (value != pkt_dev->udp_src_max) {
  892. pkt_dev->udp_src_max = value;
  893. pkt_dev->cur_udp_src = value;
  894. }
  895. sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
  896. return count;
  897. }
  898. if (!strcmp(name, "udp_dst_max")) {
  899. len = num_arg(&user_buffer[i], 10, &value);
  900. if (len < 0)
  901. return len;
  902. i += len;
  903. if (value != pkt_dev->udp_dst_max) {
  904. pkt_dev->udp_dst_max = value;
  905. pkt_dev->cur_udp_dst = value;
  906. }
  907. sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
  908. return count;
  909. }
  910. if (!strcmp(name, "clone_skb")) {
  911. len = num_arg(&user_buffer[i], 10, &value);
  912. if (len < 0)
  913. return len;
  914. if ((value > 0) &&
  915. ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
  916. !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
  917. return -ENOTSUPP;
  918. i += len;
  919. pkt_dev->clone_skb = value;
  920. sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
  921. return count;
  922. }
  923. if (!strcmp(name, "count")) {
  924. len = num_arg(&user_buffer[i], 10, &value);
  925. if (len < 0)
  926. return len;
  927. i += len;
  928. pkt_dev->count = value;
  929. sprintf(pg_result, "OK: count=%llu",
  930. (unsigned long long)pkt_dev->count);
  931. return count;
  932. }
  933. if (!strcmp(name, "src_mac_count")) {
  934. len = num_arg(&user_buffer[i], 10, &value);
  935. if (len < 0)
  936. return len;
  937. i += len;
  938. if (pkt_dev->src_mac_count != value) {
  939. pkt_dev->src_mac_count = value;
  940. pkt_dev->cur_src_mac_offset = 0;
  941. }
  942. sprintf(pg_result, "OK: src_mac_count=%d",
  943. pkt_dev->src_mac_count);
  944. return count;
  945. }
  946. if (!strcmp(name, "dst_mac_count")) {
  947. len = num_arg(&user_buffer[i], 10, &value);
  948. if (len < 0)
  949. return len;
  950. i += len;
  951. if (pkt_dev->dst_mac_count != value) {
  952. pkt_dev->dst_mac_count = value;
  953. pkt_dev->cur_dst_mac_offset = 0;
  954. }
  955. sprintf(pg_result, "OK: dst_mac_count=%d",
  956. pkt_dev->dst_mac_count);
  957. return count;
  958. }
  959. if (!strcmp(name, "burst")) {
  960. len = num_arg(&user_buffer[i], 10, &value);
  961. if (len < 0)
  962. return len;
  963. i += len;
  964. if ((value > 1) &&
  965. ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
  966. ((pkt_dev->xmit_mode == M_START_XMIT) &&
  967. (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
  968. return -ENOTSUPP;
  969. pkt_dev->burst = value < 1 ? 1 : value;
  970. sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
  971. return count;
  972. }
  973. if (!strcmp(name, "node")) {
  974. len = num_arg(&user_buffer[i], 10, &value);
  975. if (len < 0)
  976. return len;
  977. i += len;
  978. if (node_possible(value)) {
  979. pkt_dev->node = value;
  980. sprintf(pg_result, "OK: node=%d", pkt_dev->node);
  981. if (pkt_dev->page) {
  982. put_page(pkt_dev->page);
  983. pkt_dev->page = NULL;
  984. }
  985. }
  986. else
  987. sprintf(pg_result, "ERROR: node not possible");
  988. return count;
  989. }
  990. if (!strcmp(name, "xmit_mode")) {
  991. char f[32];
  992. memset(f, 0, 32);
  993. len = strn_len(&user_buffer[i], sizeof(f) - 1);
  994. if (len < 0)
  995. return len;
  996. if (copy_from_user(f, &user_buffer[i], len))
  997. return -EFAULT;
  998. i += len;
  999. if (strcmp(f, "start_xmit") == 0) {
  1000. pkt_dev->xmit_mode = M_START_XMIT;
  1001. } else if (strcmp(f, "netif_receive") == 0) {
  1002. /* clone_skb set earlier, not supported in this mode */
  1003. if (pkt_dev->clone_skb > 0)
  1004. return -ENOTSUPP;
  1005. pkt_dev->xmit_mode = M_NETIF_RECEIVE;
  1006. /* make sure new packet is allocated every time
  1007. * pktgen_xmit() is called
  1008. */
  1009. pkt_dev->last_ok = 1;
  1010. /* override clone_skb if user passed default value
  1011. * at module loading time
  1012. */
  1013. pkt_dev->clone_skb = 0;
  1014. } else if (strcmp(f, "queue_xmit") == 0) {
  1015. pkt_dev->xmit_mode = M_QUEUE_XMIT;
  1016. pkt_dev->last_ok = 1;
  1017. } else {
  1018. sprintf(pg_result,
  1019. "xmit_mode -:%s:- unknown\nAvailable modes: %s",
  1020. f, "start_xmit, netif_receive\n");
  1021. return count;
  1022. }
  1023. sprintf(pg_result, "OK: xmit_mode=%s", f);
  1024. return count;
  1025. }
  1026. if (!strcmp(name, "flag")) {
  1027. char f[32];
  1028. memset(f, 0, 32);
  1029. len = strn_len(&user_buffer[i], sizeof(f) - 1);
  1030. if (len < 0)
  1031. return len;
  1032. if (copy_from_user(f, &user_buffer[i], len))
  1033. return -EFAULT;
  1034. i += len;
  1035. if (strcmp(f, "IPSRC_RND") == 0)
  1036. pkt_dev->flags |= F_IPSRC_RND;
  1037. else if (strcmp(f, "!IPSRC_RND") == 0)
  1038. pkt_dev->flags &= ~F_IPSRC_RND;
  1039. else if (strcmp(f, "TXSIZE_RND") == 0)
  1040. pkt_dev->flags |= F_TXSIZE_RND;
  1041. else if (strcmp(f, "!TXSIZE_RND") == 0)
  1042. pkt_dev->flags &= ~F_TXSIZE_RND;
  1043. else if (strcmp(f, "IPDST_RND") == 0)
  1044. pkt_dev->flags |= F_IPDST_RND;
  1045. else if (strcmp(f, "!IPDST_RND") == 0)
  1046. pkt_dev->flags &= ~F_IPDST_RND;
  1047. else if (strcmp(f, "UDPSRC_RND") == 0)
  1048. pkt_dev->flags |= F_UDPSRC_RND;
  1049. else if (strcmp(f, "!UDPSRC_RND") == 0)
  1050. pkt_dev->flags &= ~F_UDPSRC_RND;
  1051. else if (strcmp(f, "UDPDST_RND") == 0)
  1052. pkt_dev->flags |= F_UDPDST_RND;
  1053. else if (strcmp(f, "!UDPDST_RND") == 0)
  1054. pkt_dev->flags &= ~F_UDPDST_RND;
  1055. else if (strcmp(f, "MACSRC_RND") == 0)
  1056. pkt_dev->flags |= F_MACSRC_RND;
  1057. else if (strcmp(f, "!MACSRC_RND") == 0)
  1058. pkt_dev->flags &= ~F_MACSRC_RND;
  1059. else if (strcmp(f, "MACDST_RND") == 0)
  1060. pkt_dev->flags |= F_MACDST_RND;
  1061. else if (strcmp(f, "!MACDST_RND") == 0)
  1062. pkt_dev->flags &= ~F_MACDST_RND;
  1063. else if (strcmp(f, "MPLS_RND") == 0)
  1064. pkt_dev->flags |= F_MPLS_RND;
  1065. else if (strcmp(f, "!MPLS_RND") == 0)
  1066. pkt_dev->flags &= ~F_MPLS_RND;
  1067. else if (strcmp(f, "VID_RND") == 0)
  1068. pkt_dev->flags |= F_VID_RND;
  1069. else if (strcmp(f, "!VID_RND") == 0)
  1070. pkt_dev->flags &= ~F_VID_RND;
  1071. else if (strcmp(f, "SVID_RND") == 0)
  1072. pkt_dev->flags |= F_SVID_RND;
  1073. else if (strcmp(f, "!SVID_RND") == 0)
  1074. pkt_dev->flags &= ~F_SVID_RND;
  1075. else if (strcmp(f, "FLOW_SEQ") == 0 || strcmp(f, "!FLOW_RND") == 0)
  1076. pkt_dev->flags |= F_FLOW_SEQ;
  1077. else if (strcmp(f, "FLOW_RND") == 0 || strcmp(f, "!FLOW_SEQ") == 0)
  1078. pkt_dev->flags &= ~F_FLOW_SEQ;
  1079. else if (strcmp(f, "QUEUE_MAP_RND") == 0)
  1080. pkt_dev->flags |= F_QUEUE_MAP_RND;
  1081. else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
  1082. pkt_dev->flags &= ~F_QUEUE_MAP_RND;
  1083. else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
  1084. pkt_dev->flags |= F_QUEUE_MAP_CPU;
  1085. else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
  1086. pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
  1087. #ifdef CONFIG_XFRM
  1088. else if (strcmp(f, "IPSEC") == 0)
  1089. pkt_dev->flags |= F_IPSEC;
  1090. else if (strcmp(f, "!IPSEC") == 0)
  1091. pkt_dev->flags &= ~F_IPSEC;
  1092. #endif
  1093. else if (strcmp(f, "!IPV6") == 0)
  1094. pkt_dev->flags &= ~F_IPV6;
  1095. else if (strcmp(f, "NODE_ALLOC") == 0)
  1096. pkt_dev->flags |= F_NODE;
  1097. else if (strcmp(f, "!NODE_ALLOC") == 0)
  1098. pkt_dev->flags &= ~F_NODE;
  1099. else if (strcmp(f, "UDPCSUM") == 0)
  1100. pkt_dev->flags |= F_UDPCSUM;
  1101. else if (strcmp(f, "!UDPCSUM") == 0)
  1102. pkt_dev->flags &= ~F_UDPCSUM;
  1103. else if (strcmp(f, "NO_TIMESTAMP") == 0)
  1104. pkt_dev->flags |= F_NO_TIMESTAMP;
  1105. else if (strcmp(f, "!NO_TIMESTAMP") == 0)
  1106. pkt_dev->flags &= ~F_NO_TIMESTAMP;
  1107. else {
  1108. sprintf(pg_result,
  1109. "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
  1110. f,
  1111. "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
  1112. "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
  1113. "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
  1114. "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
  1115. "NO_TIMESTAMP, "
  1116. #ifdef CONFIG_XFRM
  1117. "IPSEC, "
  1118. #endif
  1119. "NODE_ALLOC\n");
  1120. return count;
  1121. }
  1122. sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
  1123. return count;
  1124. }
  1125. if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
  1126. len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
  1127. if (len < 0)
  1128. return len;
  1129. if (copy_from_user(buf, &user_buffer[i], len))
  1130. return -EFAULT;
  1131. buf[len] = 0;
  1132. if (strcmp(buf, pkt_dev->dst_min) != 0) {
  1133. memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
  1134. strncpy(pkt_dev->dst_min, buf, len);
  1135. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1136. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1137. }
  1138. if (debug)
  1139. pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
  1140. i += len;
  1141. sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
  1142. return count;
  1143. }
  1144. if (!strcmp(name, "dst_max")) {
  1145. len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
  1146. if (len < 0)
  1147. return len;
  1148. if (copy_from_user(buf, &user_buffer[i], len))
  1149. return -EFAULT;
  1150. buf[len] = 0;
  1151. if (strcmp(buf, pkt_dev->dst_max) != 0) {
  1152. memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
  1153. strncpy(pkt_dev->dst_max, buf, len);
  1154. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1155. pkt_dev->cur_daddr = pkt_dev->daddr_max;
  1156. }
  1157. if (debug)
  1158. pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
  1159. i += len;
  1160. sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
  1161. return count;
  1162. }
  1163. if (!strcmp(name, "dst6")) {
  1164. len = strn_len(&user_buffer[i], sizeof(buf) - 1);
  1165. if (len < 0)
  1166. return len;
  1167. pkt_dev->flags |= F_IPV6;
  1168. if (copy_from_user(buf, &user_buffer[i], len))
  1169. return -EFAULT;
  1170. buf[len] = 0;
  1171. in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
  1172. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
  1173. pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
  1174. if (debug)
  1175. pr_debug("dst6 set to: %s\n", buf);
  1176. i += len;
  1177. sprintf(pg_result, "OK: dst6=%s", buf);
  1178. return count;
  1179. }
  1180. if (!strcmp(name, "dst6_min")) {
  1181. len = strn_len(&user_buffer[i], sizeof(buf) - 1);
  1182. if (len < 0)
  1183. return len;
  1184. pkt_dev->flags |= F_IPV6;
  1185. if (copy_from_user(buf, &user_buffer[i], len))
  1186. return -EFAULT;
  1187. buf[len] = 0;
  1188. in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
  1189. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
  1190. pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
  1191. if (debug)
  1192. pr_debug("dst6_min set to: %s\n", buf);
  1193. i += len;
  1194. sprintf(pg_result, "OK: dst6_min=%s", buf);
  1195. return count;
  1196. }
  1197. if (!strcmp(name, "dst6_max")) {
  1198. len = strn_len(&user_buffer[i], sizeof(buf) - 1);
  1199. if (len < 0)
  1200. return len;
  1201. pkt_dev->flags |= F_IPV6;
  1202. if (copy_from_user(buf, &user_buffer[i], len))
  1203. return -EFAULT;
  1204. buf[len] = 0;
  1205. in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
  1206. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
  1207. if (debug)
  1208. pr_debug("dst6_max set to: %s\n", buf);
  1209. i += len;
  1210. sprintf(pg_result, "OK: dst6_max=%s", buf);
  1211. return count;
  1212. }
  1213. if (!strcmp(name, "src6")) {
  1214. len = strn_len(&user_buffer[i], sizeof(buf) - 1);
  1215. if (len < 0)
  1216. return len;
  1217. pkt_dev->flags |= F_IPV6;
  1218. if (copy_from_user(buf, &user_buffer[i], len))
  1219. return -EFAULT;
  1220. buf[len] = 0;
  1221. in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
  1222. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
  1223. pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
  1224. if (debug)
  1225. pr_debug("src6 set to: %s\n", buf);
  1226. i += len;
  1227. sprintf(pg_result, "OK: src6=%s", buf);
  1228. return count;
  1229. }
  1230. if (!strcmp(name, "src_min")) {
  1231. len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
  1232. if (len < 0)
  1233. return len;
  1234. if (copy_from_user(buf, &user_buffer[i], len))
  1235. return -EFAULT;
  1236. buf[len] = 0;
  1237. if (strcmp(buf, pkt_dev->src_min) != 0) {
  1238. memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
  1239. strncpy(pkt_dev->src_min, buf, len);
  1240. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1241. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1242. }
  1243. if (debug)
  1244. pr_debug("src_min set to: %s\n", pkt_dev->src_min);
  1245. i += len;
  1246. sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
  1247. return count;
  1248. }
  1249. if (!strcmp(name, "src_max")) {
  1250. len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
  1251. if (len < 0)
  1252. return len;
  1253. if (copy_from_user(buf, &user_buffer[i], len))
  1254. return -EFAULT;
  1255. buf[len] = 0;
  1256. if (strcmp(buf, pkt_dev->src_max) != 0) {
  1257. memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
  1258. strncpy(pkt_dev->src_max, buf, len);
  1259. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1260. pkt_dev->cur_saddr = pkt_dev->saddr_max;
  1261. }
  1262. if (debug)
  1263. pr_debug("src_max set to: %s\n", pkt_dev->src_max);
  1264. i += len;
  1265. sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
  1266. return count;
  1267. }
  1268. if (!strcmp(name, "dst_mac")) {
  1269. len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
  1270. if (len < 0)
  1271. return len;
  1272. memset(valstr, 0, sizeof(valstr));
  1273. if (copy_from_user(valstr, &user_buffer[i], len))
  1274. return -EFAULT;
  1275. if (!mac_pton(valstr, pkt_dev->dst_mac))
  1276. return -EINVAL;
  1277. /* Set up Dest MAC */
  1278. ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
  1279. sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
  1280. return count;
  1281. }
  1282. if (!strcmp(name, "src_mac")) {
  1283. len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
  1284. if (len < 0)
  1285. return len;
  1286. memset(valstr, 0, sizeof(valstr));
  1287. if (copy_from_user(valstr, &user_buffer[i], len))
  1288. return -EFAULT;
  1289. if (!mac_pton(valstr, pkt_dev->src_mac))
  1290. return -EINVAL;
  1291. /* Set up Src MAC */
  1292. ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
  1293. sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
  1294. return count;
  1295. }
  1296. if (!strcmp(name, "clear_counters")) {
  1297. pktgen_clear_counters(pkt_dev);
  1298. sprintf(pg_result, "OK: Clearing counters.\n");
  1299. return count;
  1300. }
  1301. if (!strcmp(name, "flows")) {
  1302. len = num_arg(&user_buffer[i], 10, &value);
  1303. if (len < 0)
  1304. return len;
  1305. i += len;
  1306. if (value > MAX_CFLOWS)
  1307. value = MAX_CFLOWS;
  1308. pkt_dev->cflows = value;
  1309. sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
  1310. return count;
  1311. }
  1312. #ifdef CONFIG_XFRM
  1313. if (!strcmp(name, "spi")) {
  1314. len = num_arg(&user_buffer[i], 10, &value);
  1315. if (len < 0)
  1316. return len;
  1317. i += len;
  1318. pkt_dev->spi = value;
  1319. sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
  1320. return count;
  1321. }
  1322. #endif
  1323. if (!strcmp(name, "flowlen")) {
  1324. len = num_arg(&user_buffer[i], 10, &value);
  1325. if (len < 0)
  1326. return len;
  1327. i += len;
  1328. pkt_dev->lflow = value;
  1329. sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
  1330. return count;
  1331. }
  1332. if (!strcmp(name, "queue_map_min")) {
  1333. len = num_arg(&user_buffer[i], 5, &value);
  1334. if (len < 0)
  1335. return len;
  1336. i += len;
  1337. pkt_dev->queue_map_min = value;
  1338. sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
  1339. return count;
  1340. }
  1341. if (!strcmp(name, "queue_map_max")) {
  1342. len = num_arg(&user_buffer[i], 5, &value);
  1343. if (len < 0)
  1344. return len;
  1345. i += len;
  1346. pkt_dev->queue_map_max = value;
  1347. sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
  1348. return count;
  1349. }
  1350. if (!strcmp(name, "mpls")) {
  1351. unsigned int n, cnt;
  1352. len = get_labels(&user_buffer[i], pkt_dev);
  1353. if (len < 0)
  1354. return len;
  1355. i += len;
  1356. cnt = sprintf(pg_result, "OK: mpls=");
  1357. for (n = 0; n < pkt_dev->nr_labels; n++)
  1358. cnt += sprintf(pg_result + cnt,
  1359. "%08x%s", ntohl(pkt_dev->labels[n]),
  1360. n == pkt_dev->nr_labels-1 ? "" : ",");
  1361. if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
  1362. pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
  1363. pkt_dev->svlan_id = 0xffff;
  1364. if (debug)
  1365. pr_debug("VLAN/SVLAN auto turned off\n");
  1366. }
  1367. return count;
  1368. }
  1369. if (!strcmp(name, "vlan_id")) {
  1370. len = num_arg(&user_buffer[i], 4, &value);
  1371. if (len < 0)
  1372. return len;
  1373. i += len;
  1374. if (value <= 4095) {
  1375. pkt_dev->vlan_id = value; /* turn on VLAN */
  1376. if (debug)
  1377. pr_debug("VLAN turned on\n");
  1378. if (debug && pkt_dev->nr_labels)
  1379. pr_debug("MPLS auto turned off\n");
  1380. pkt_dev->nr_labels = 0; /* turn off MPLS */
  1381. sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
  1382. } else {
  1383. pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
  1384. pkt_dev->svlan_id = 0xffff;
  1385. if (debug)
  1386. pr_debug("VLAN/SVLAN turned off\n");
  1387. }
  1388. return count;
  1389. }
  1390. if (!strcmp(name, "vlan_p")) {
  1391. len = num_arg(&user_buffer[i], 1, &value);
  1392. if (len < 0)
  1393. return len;
  1394. i += len;
  1395. if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
  1396. pkt_dev->vlan_p = value;
  1397. sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
  1398. } else {
  1399. sprintf(pg_result, "ERROR: vlan_p must be 0-7");
  1400. }
  1401. return count;
  1402. }
  1403. if (!strcmp(name, "vlan_cfi")) {
  1404. len = num_arg(&user_buffer[i], 1, &value);
  1405. if (len < 0)
  1406. return len;
  1407. i += len;
  1408. if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
  1409. pkt_dev->vlan_cfi = value;
  1410. sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
  1411. } else {
  1412. sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
  1413. }
  1414. return count;
  1415. }
  1416. if (!strcmp(name, "svlan_id")) {
  1417. len = num_arg(&user_buffer[i], 4, &value);
  1418. if (len < 0)
  1419. return len;
  1420. i += len;
  1421. if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
  1422. pkt_dev->svlan_id = value; /* turn on SVLAN */
  1423. if (debug)
  1424. pr_debug("SVLAN turned on\n");
  1425. if (debug && pkt_dev->nr_labels)
  1426. pr_debug("MPLS auto turned off\n");
  1427. pkt_dev->nr_labels = 0; /* turn off MPLS */
  1428. sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
  1429. } else {
  1430. pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
  1431. pkt_dev->svlan_id = 0xffff;
  1432. if (debug)
  1433. pr_debug("VLAN/SVLAN turned off\n");
  1434. }
  1435. return count;
  1436. }
  1437. if (!strcmp(name, "svlan_p")) {
  1438. len = num_arg(&user_buffer[i], 1, &value);
  1439. if (len < 0)
  1440. return len;
  1441. i += len;
  1442. if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
  1443. pkt_dev->svlan_p = value;
  1444. sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
  1445. } else {
  1446. sprintf(pg_result, "ERROR: svlan_p must be 0-7");
  1447. }
  1448. return count;
  1449. }
  1450. if (!strcmp(name, "svlan_cfi")) {
  1451. len = num_arg(&user_buffer[i], 1, &value);
  1452. if (len < 0)
  1453. return len;
  1454. i += len;
  1455. if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
  1456. pkt_dev->svlan_cfi = value;
  1457. sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
  1458. } else {
  1459. sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
  1460. }
  1461. return count;
  1462. }
  1463. if (!strcmp(name, "tos")) {
  1464. __u32 tmp_value = 0;
  1465. len = hex32_arg(&user_buffer[i], 2, &tmp_value);
  1466. if (len < 0)
  1467. return len;
  1468. i += len;
  1469. if (len == 2) {
  1470. pkt_dev->tos = tmp_value;
  1471. sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
  1472. } else {
  1473. sprintf(pg_result, "ERROR: tos must be 00-ff");
  1474. }
  1475. return count;
  1476. }
  1477. if (!strcmp(name, "traffic_class")) {
  1478. __u32 tmp_value = 0;
  1479. len = hex32_arg(&user_buffer[i], 2, &tmp_value);
  1480. if (len < 0)
  1481. return len;
  1482. i += len;
  1483. if (len == 2) {
  1484. pkt_dev->traffic_class = tmp_value;
  1485. sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
  1486. } else {
  1487. sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
  1488. }
  1489. return count;
  1490. }
  1491. if (!strcmp(name, "skb_priority")) {
  1492. len = num_arg(&user_buffer[i], 9, &value);
  1493. if (len < 0)
  1494. return len;
  1495. i += len;
  1496. pkt_dev->skb_priority = value;
  1497. sprintf(pg_result, "OK: skb_priority=%i",
  1498. pkt_dev->skb_priority);
  1499. return count;
  1500. }
  1501. sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
  1502. return -EINVAL;
  1503. }
  1504. static int pktgen_if_open(struct inode *inode, struct file *file)
  1505. {
  1506. return single_open(file, pktgen_if_show, PDE_DATA(inode));
  1507. }
  1508. static const struct file_operations pktgen_if_fops = {
  1509. .open = pktgen_if_open,
  1510. .read = seq_read,
  1511. .llseek = seq_lseek,
  1512. .write = pktgen_if_write,
  1513. .release = single_release,
  1514. };
  1515. static int pktgen_thread_show(struct seq_file *seq, void *v)
  1516. {
  1517. struct pktgen_thread *t = seq->private;
  1518. const struct pktgen_dev *pkt_dev;
  1519. BUG_ON(!t);
  1520. seq_puts(seq, "Running: ");
  1521. rcu_read_lock();
  1522. list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
  1523. if (pkt_dev->running)
  1524. seq_printf(seq, "%s ", pkt_dev->odevname);
  1525. seq_puts(seq, "\nStopped: ");
  1526. list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
  1527. if (!pkt_dev->running)
  1528. seq_printf(seq, "%s ", pkt_dev->odevname);
  1529. if (t->result[0])
  1530. seq_printf(seq, "\nResult: %s\n", t->result);
  1531. else
  1532. seq_puts(seq, "\nResult: NA\n");
  1533. rcu_read_unlock();
  1534. return 0;
  1535. }
  1536. static ssize_t pktgen_thread_write(struct file *file,
  1537. const char __user * user_buffer,
  1538. size_t count, loff_t * offset)
  1539. {
  1540. struct seq_file *seq = file->private_data;
  1541. struct pktgen_thread *t = seq->private;
  1542. int i, max, len, ret;
  1543. char name[40];
  1544. char *pg_result;
  1545. if (count < 1) {
  1546. // sprintf(pg_result, "Wrong command format");
  1547. return -EINVAL;
  1548. }
  1549. max = count;
  1550. len = count_trail_chars(user_buffer, max);
  1551. if (len < 0)
  1552. return len;
  1553. i = len;
  1554. /* Read variable name */
  1555. len = strn_len(&user_buffer[i], sizeof(name) - 1);
  1556. if (len < 0)
  1557. return len;
  1558. memset(name, 0, sizeof(name));
  1559. if (copy_from_user(name, &user_buffer[i], len))
  1560. return -EFAULT;
  1561. i += len;
  1562. max = count - i;
  1563. len = count_trail_chars(&user_buffer[i], max);
  1564. if (len < 0)
  1565. return len;
  1566. i += len;
  1567. if (debug)
  1568. pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
  1569. if (!t) {
  1570. pr_err("ERROR: No thread\n");
  1571. ret = -EINVAL;
  1572. goto out;
  1573. }
  1574. pg_result = &(t->result[0]);
  1575. if (!strcmp(name, "add_device")) {
  1576. char f[32];
  1577. memset(f, 0, 32);
  1578. len = strn_len(&user_buffer[i], sizeof(f) - 1);
  1579. if (len < 0) {
  1580. ret = len;
  1581. goto out;
  1582. }
  1583. if (copy_from_user(f, &user_buffer[i], len))
  1584. return -EFAULT;
  1585. i += len;
  1586. mutex_lock(&pktgen_thread_lock);
  1587. ret = pktgen_add_device(t, f);
  1588. mutex_unlock(&pktgen_thread_lock);
  1589. if (!ret) {
  1590. ret = count;
  1591. sprintf(pg_result, "OK: add_device=%s", f);
  1592. } else
  1593. sprintf(pg_result, "ERROR: can not add device %s", f);
  1594. goto out;
  1595. }
  1596. if (!strcmp(name, "rem_device_all")) {
  1597. mutex_lock(&pktgen_thread_lock);
  1598. t->control |= T_REMDEVALL;
  1599. mutex_unlock(&pktgen_thread_lock);
  1600. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  1601. ret = count;
  1602. sprintf(pg_result, "OK: rem_device_all");
  1603. goto out;
  1604. }
  1605. if (!strcmp(name, "max_before_softirq")) {
  1606. sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
  1607. ret = count;
  1608. goto out;
  1609. }
  1610. ret = -EINVAL;
  1611. out:
  1612. return ret;
  1613. }
  1614. static int pktgen_thread_open(struct inode *inode, struct file *file)
  1615. {
  1616. return single_open(file, pktgen_thread_show, PDE_DATA(inode));
  1617. }
  1618. static const struct file_operations pktgen_thread_fops = {
  1619. .open = pktgen_thread_open,
  1620. .read = seq_read,
  1621. .llseek = seq_lseek,
  1622. .write = pktgen_thread_write,
  1623. .release = single_release,
  1624. };
  1625. /* Think find or remove for NN */
  1626. static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
  1627. const char *ifname, int remove)
  1628. {
  1629. struct pktgen_thread *t;
  1630. struct pktgen_dev *pkt_dev = NULL;
  1631. bool exact = (remove == FIND);
  1632. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  1633. pkt_dev = pktgen_find_dev(t, ifname, exact);
  1634. if (pkt_dev) {
  1635. if (remove) {
  1636. pkt_dev->removal_mark = 1;
  1637. t->control |= T_REMDEV;
  1638. }
  1639. break;
  1640. }
  1641. }
  1642. return pkt_dev;
  1643. }
  1644. /*
  1645. * mark a device for removal
  1646. */
  1647. static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
  1648. {
  1649. struct pktgen_dev *pkt_dev = NULL;
  1650. const int max_tries = 10, msec_per_try = 125;
  1651. int i = 0;
  1652. mutex_lock(&pktgen_thread_lock);
  1653. pr_debug("%s: marking %s for removal\n", __func__, ifname);
  1654. while (1) {
  1655. pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
  1656. if (pkt_dev == NULL)
  1657. break; /* success */
  1658. mutex_unlock(&pktgen_thread_lock);
  1659. pr_debug("%s: waiting for %s to disappear....\n",
  1660. __func__, ifname);
  1661. schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
  1662. mutex_lock(&pktgen_thread_lock);
  1663. if (++i >= max_tries) {
  1664. pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
  1665. __func__, msec_per_try * i, ifname);
  1666. break;
  1667. }
  1668. }
  1669. mutex_unlock(&pktgen_thread_lock);
  1670. }
  1671. static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
  1672. {
  1673. struct pktgen_thread *t;
  1674. mutex_lock(&pktgen_thread_lock);
  1675. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  1676. struct pktgen_dev *pkt_dev;
  1677. if_lock(t);
  1678. list_for_each_entry(pkt_dev, &t->if_list, list) {
  1679. if (pkt_dev->odev != dev)
  1680. continue;
  1681. proc_remove(pkt_dev->entry);
  1682. pkt_dev->entry = proc_create_data(dev->name, 0600,
  1683. pn->proc_dir,
  1684. &pktgen_if_fops,
  1685. pkt_dev);
  1686. if (!pkt_dev->entry)
  1687. pr_err("can't move proc entry for '%s'\n",
  1688. dev->name);
  1689. break;
  1690. }
  1691. if_unlock(t);
  1692. }
  1693. mutex_unlock(&pktgen_thread_lock);
  1694. }
  1695. static int pktgen_device_event(struct notifier_block *unused,
  1696. unsigned long event, void *ptr)
  1697. {
  1698. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1699. struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
  1700. if (pn->pktgen_exiting)
  1701. return NOTIFY_DONE;
  1702. /* It is OK that we do not hold the group lock right now,
  1703. * as we run under the RTNL lock.
  1704. */
  1705. switch (event) {
  1706. case NETDEV_CHANGENAME:
  1707. pktgen_change_name(pn, dev);
  1708. break;
  1709. case NETDEV_UNREGISTER:
  1710. pktgen_mark_device(pn, dev->name);
  1711. break;
  1712. }
  1713. return NOTIFY_DONE;
  1714. }
  1715. static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
  1716. struct pktgen_dev *pkt_dev,
  1717. const char *ifname)
  1718. {
  1719. char b[IFNAMSIZ+5];
  1720. int i;
  1721. for (i = 0; ifname[i] != '@'; i++) {
  1722. if (i == IFNAMSIZ)
  1723. break;
  1724. b[i] = ifname[i];
  1725. }
  1726. b[i] = 0;
  1727. return dev_get_by_name(pn->net, b);
  1728. }
  1729. /* Associate pktgen_dev with a device. */
  1730. static int pktgen_setup_dev(const struct pktgen_net *pn,
  1731. struct pktgen_dev *pkt_dev, const char *ifname)
  1732. {
  1733. struct net_device *odev;
  1734. int err;
  1735. /* Clean old setups */
  1736. if (pkt_dev->odev) {
  1737. dev_put(pkt_dev->odev);
  1738. pkt_dev->odev = NULL;
  1739. }
  1740. odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
  1741. if (!odev) {
  1742. pr_err("no such netdevice: \"%s\"\n", ifname);
  1743. return -ENODEV;
  1744. }
  1745. if (odev->type != ARPHRD_ETHER) {
  1746. pr_err("not an ethernet device: \"%s\"\n", ifname);
  1747. err = -EINVAL;
  1748. } else if (!netif_running(odev)) {
  1749. pr_err("device is down: \"%s\"\n", ifname);
  1750. err = -ENETDOWN;
  1751. } else {
  1752. pkt_dev->odev = odev;
  1753. return 0;
  1754. }
  1755. dev_put(odev);
  1756. return err;
  1757. }
  1758. /* Read pkt_dev from the interface and set up internal pktgen_dev
  1759. * structure to have the right information to create/send packets
  1760. */
  1761. static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
  1762. {
  1763. int ntxq;
  1764. if (!pkt_dev->odev) {
  1765. pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
  1766. sprintf(pkt_dev->result,
  1767. "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
  1768. return;
  1769. }
  1770. /* make sure that we don't pick a non-existing transmit queue */
  1771. ntxq = pkt_dev->odev->real_num_tx_queues;
  1772. if (ntxq <= pkt_dev->queue_map_min) {
  1773. pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
  1774. pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
  1775. pkt_dev->odevname);
  1776. pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
  1777. }
  1778. if (pkt_dev->queue_map_max >= ntxq) {
  1779. pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
  1780. pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
  1781. pkt_dev->odevname);
  1782. pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
  1783. }
  1784. /* Default to the interface's mac if not explicitly set. */
  1785. if (is_zero_ether_addr(pkt_dev->src_mac))
  1786. ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
  1787. /* Set up Dest MAC */
  1788. ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
  1789. if (pkt_dev->flags & F_IPV6) {
  1790. int i, set = 0, err = 1;
  1791. struct inet6_dev *idev;
  1792. if (pkt_dev->min_pkt_size == 0) {
  1793. pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
  1794. + sizeof(struct udphdr)
  1795. + sizeof(struct pktgen_hdr)
  1796. + pkt_dev->pkt_overhead;
  1797. }
  1798. for (i = 0; i < sizeof(struct in6_addr); i++)
  1799. if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
  1800. set = 1;
  1801. break;
  1802. }
  1803. if (!set) {
  1804. /*
  1805. * Use linklevel address if unconfigured.
  1806. *
  1807. * use ipv6_get_lladdr if/when it's get exported
  1808. */
  1809. rcu_read_lock();
  1810. idev = __in6_dev_get(pkt_dev->odev);
  1811. if (idev) {
  1812. struct inet6_ifaddr *ifp;
  1813. read_lock_bh(&idev->lock);
  1814. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  1815. if ((ifp->scope & IFA_LINK) &&
  1816. !(ifp->flags & IFA_F_TENTATIVE)) {
  1817. pkt_dev->cur_in6_saddr = ifp->addr;
  1818. err = 0;
  1819. break;
  1820. }
  1821. }
  1822. read_unlock_bh(&idev->lock);
  1823. }
  1824. rcu_read_unlock();
  1825. if (err)
  1826. pr_err("ERROR: IPv6 link address not available\n");
  1827. }
  1828. } else {
  1829. if (pkt_dev->min_pkt_size == 0) {
  1830. pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
  1831. + sizeof(struct udphdr)
  1832. + sizeof(struct pktgen_hdr)
  1833. + pkt_dev->pkt_overhead;
  1834. }
  1835. pkt_dev->saddr_min = 0;
  1836. pkt_dev->saddr_max = 0;
  1837. if (strlen(pkt_dev->src_min) == 0) {
  1838. struct in_device *in_dev;
  1839. rcu_read_lock();
  1840. in_dev = __in_dev_get_rcu(pkt_dev->odev);
  1841. if (in_dev) {
  1842. if (in_dev->ifa_list) {
  1843. pkt_dev->saddr_min =
  1844. in_dev->ifa_list->ifa_address;
  1845. pkt_dev->saddr_max = pkt_dev->saddr_min;
  1846. }
  1847. }
  1848. rcu_read_unlock();
  1849. } else {
  1850. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1851. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1852. }
  1853. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1854. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1855. }
  1856. /* Initialize current values. */
  1857. pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
  1858. if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
  1859. pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
  1860. pkt_dev->cur_dst_mac_offset = 0;
  1861. pkt_dev->cur_src_mac_offset = 0;
  1862. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1863. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1864. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1865. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1866. pkt_dev->nflows = 0;
  1867. }
  1868. static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
  1869. {
  1870. ktime_t start_time, end_time;
  1871. s64 remaining;
  1872. struct hrtimer_sleeper t;
  1873. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1874. hrtimer_set_expires(&t.timer, spin_until);
  1875. remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
  1876. if (remaining <= 0)
  1877. goto out;
  1878. start_time = ktime_get();
  1879. if (remaining < 100000) {
  1880. /* for small delays (<100us), just loop until limit is reached */
  1881. do {
  1882. end_time = ktime_get();
  1883. } while (ktime_compare(end_time, spin_until) < 0);
  1884. } else {
  1885. /* see do_nanosleep */
  1886. hrtimer_init_sleeper(&t, current);
  1887. do {
  1888. set_current_state(TASK_INTERRUPTIBLE);
  1889. hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
  1890. if (likely(t.task))
  1891. schedule();
  1892. hrtimer_cancel(&t.timer);
  1893. } while (t.task && pkt_dev->running && !signal_pending(current));
  1894. __set_current_state(TASK_RUNNING);
  1895. end_time = ktime_get();
  1896. }
  1897. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
  1898. out:
  1899. pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
  1900. destroy_hrtimer_on_stack(&t.timer);
  1901. }
  1902. static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
  1903. {
  1904. pkt_dev->pkt_overhead = 0;
  1905. pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
  1906. pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
  1907. pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
  1908. }
  1909. static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
  1910. {
  1911. return !!(pkt_dev->flows[flow].flags & F_INIT);
  1912. }
  1913. static inline int f_pick(struct pktgen_dev *pkt_dev)
  1914. {
  1915. int flow = pkt_dev->curfl;
  1916. if (pkt_dev->flags & F_FLOW_SEQ) {
  1917. if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
  1918. /* reset time */
  1919. pkt_dev->flows[flow].count = 0;
  1920. pkt_dev->flows[flow].flags = 0;
  1921. pkt_dev->curfl += 1;
  1922. if (pkt_dev->curfl >= pkt_dev->cflows)
  1923. pkt_dev->curfl = 0; /*reset */
  1924. }
  1925. } else {
  1926. flow = prandom_u32() % pkt_dev->cflows;
  1927. pkt_dev->curfl = flow;
  1928. if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
  1929. pkt_dev->flows[flow].count = 0;
  1930. pkt_dev->flows[flow].flags = 0;
  1931. }
  1932. }
  1933. return pkt_dev->curfl;
  1934. }
  1935. #ifdef CONFIG_XFRM
  1936. /* If there was already an IPSEC SA, we keep it as is, else
  1937. * we go look for it ...
  1938. */
  1939. #define DUMMY_MARK 0
  1940. static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
  1941. {
  1942. struct xfrm_state *x = pkt_dev->flows[flow].x;
  1943. struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
  1944. if (!x) {
  1945. if (pkt_dev->spi) {
  1946. /* We need as quick as possible to find the right SA
  1947. * Searching with minimum criteria to archieve this.
  1948. */
  1949. x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
  1950. } else {
  1951. /* slow path: we dont already have xfrm_state */
  1952. x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
  1953. (xfrm_address_t *)&pkt_dev->cur_daddr,
  1954. (xfrm_address_t *)&pkt_dev->cur_saddr,
  1955. AF_INET,
  1956. pkt_dev->ipsmode,
  1957. pkt_dev->ipsproto, 0);
  1958. }
  1959. if (x) {
  1960. pkt_dev->flows[flow].x = x;
  1961. set_pkt_overhead(pkt_dev);
  1962. pkt_dev->pkt_overhead += x->props.header_len;
  1963. }
  1964. }
  1965. }
  1966. #endif
  1967. static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
  1968. {
  1969. if (pkt_dev->flags & F_QUEUE_MAP_CPU)
  1970. pkt_dev->cur_queue_map = smp_processor_id();
  1971. else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
  1972. __u16 t;
  1973. if (pkt_dev->flags & F_QUEUE_MAP_RND) {
  1974. t = prandom_u32() %
  1975. (pkt_dev->queue_map_max -
  1976. pkt_dev->queue_map_min + 1)
  1977. + pkt_dev->queue_map_min;
  1978. } else {
  1979. t = pkt_dev->cur_queue_map + 1;
  1980. if (t > pkt_dev->queue_map_max)
  1981. t = pkt_dev->queue_map_min;
  1982. }
  1983. pkt_dev->cur_queue_map = t;
  1984. }
  1985. pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
  1986. }
  1987. /* Increment/randomize headers according to flags and current values
  1988. * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  1989. */
  1990. static void mod_cur_headers(struct pktgen_dev *pkt_dev)
  1991. {
  1992. __u32 imn;
  1993. __u32 imx;
  1994. int flow = 0;
  1995. if (pkt_dev->cflows)
  1996. flow = f_pick(pkt_dev);
  1997. /* Deal with source MAC */
  1998. if (pkt_dev->src_mac_count > 1) {
  1999. __u32 mc;
  2000. __u32 tmp;
  2001. if (pkt_dev->flags & F_MACSRC_RND)
  2002. mc = prandom_u32() % pkt_dev->src_mac_count;
  2003. else {
  2004. mc = pkt_dev->cur_src_mac_offset++;
  2005. if (pkt_dev->cur_src_mac_offset >=
  2006. pkt_dev->src_mac_count)
  2007. pkt_dev->cur_src_mac_offset = 0;
  2008. }
  2009. tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
  2010. pkt_dev->hh[11] = tmp;
  2011. tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  2012. pkt_dev->hh[10] = tmp;
  2013. tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  2014. pkt_dev->hh[9] = tmp;
  2015. tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  2016. pkt_dev->hh[8] = tmp;
  2017. tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
  2018. pkt_dev->hh[7] = tmp;
  2019. }
  2020. /* Deal with Destination MAC */
  2021. if (pkt_dev->dst_mac_count > 1) {
  2022. __u32 mc;
  2023. __u32 tmp;
  2024. if (pkt_dev->flags & F_MACDST_RND)
  2025. mc = prandom_u32() % pkt_dev->dst_mac_count;
  2026. else {
  2027. mc = pkt_dev->cur_dst_mac_offset++;
  2028. if (pkt_dev->cur_dst_mac_offset >=
  2029. pkt_dev->dst_mac_count) {
  2030. pkt_dev->cur_dst_mac_offset = 0;
  2031. }
  2032. }
  2033. tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
  2034. pkt_dev->hh[5] = tmp;
  2035. tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  2036. pkt_dev->hh[4] = tmp;
  2037. tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  2038. pkt_dev->hh[3] = tmp;
  2039. tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  2040. pkt_dev->hh[2] = tmp;
  2041. tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
  2042. pkt_dev->hh[1] = tmp;
  2043. }
  2044. if (pkt_dev->flags & F_MPLS_RND) {
  2045. unsigned int i;
  2046. for (i = 0; i < pkt_dev->nr_labels; i++)
  2047. if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
  2048. pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
  2049. ((__force __be32)prandom_u32() &
  2050. htonl(0x000fffff));
  2051. }
  2052. if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
  2053. pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
  2054. }
  2055. if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
  2056. pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
  2057. }
  2058. if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
  2059. if (pkt_dev->flags & F_UDPSRC_RND)
  2060. pkt_dev->cur_udp_src = prandom_u32() %
  2061. (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
  2062. + pkt_dev->udp_src_min;
  2063. else {
  2064. pkt_dev->cur_udp_src++;
  2065. if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
  2066. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  2067. }
  2068. }
  2069. if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
  2070. if (pkt_dev->flags & F_UDPDST_RND) {
  2071. pkt_dev->cur_udp_dst = prandom_u32() %
  2072. (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
  2073. + pkt_dev->udp_dst_min;
  2074. } else {
  2075. pkt_dev->cur_udp_dst++;
  2076. if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
  2077. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  2078. }
  2079. }
  2080. if (!(pkt_dev->flags & F_IPV6)) {
  2081. imn = ntohl(pkt_dev->saddr_min);
  2082. imx = ntohl(pkt_dev->saddr_max);
  2083. if (imn < imx) {
  2084. __u32 t;
  2085. if (pkt_dev->flags & F_IPSRC_RND)
  2086. t = prandom_u32() % (imx - imn) + imn;
  2087. else {
  2088. t = ntohl(pkt_dev->cur_saddr);
  2089. t++;
  2090. if (t > imx)
  2091. t = imn;
  2092. }
  2093. pkt_dev->cur_saddr = htonl(t);
  2094. }
  2095. if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
  2096. pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
  2097. } else {
  2098. imn = ntohl(pkt_dev->daddr_min);
  2099. imx = ntohl(pkt_dev->daddr_max);
  2100. if (imn < imx) {
  2101. __u32 t;
  2102. __be32 s;
  2103. if (pkt_dev->flags & F_IPDST_RND) {
  2104. do {
  2105. t = prandom_u32() %
  2106. (imx - imn) + imn;
  2107. s = htonl(t);
  2108. } while (ipv4_is_loopback(s) ||
  2109. ipv4_is_multicast(s) ||
  2110. ipv4_is_lbcast(s) ||
  2111. ipv4_is_zeronet(s) ||
  2112. ipv4_is_local_multicast(s));
  2113. pkt_dev->cur_daddr = s;
  2114. } else {
  2115. t = ntohl(pkt_dev->cur_daddr);
  2116. t++;
  2117. if (t > imx) {
  2118. t = imn;
  2119. }
  2120. pkt_dev->cur_daddr = htonl(t);
  2121. }
  2122. }
  2123. if (pkt_dev->cflows) {
  2124. pkt_dev->flows[flow].flags |= F_INIT;
  2125. pkt_dev->flows[flow].cur_daddr =
  2126. pkt_dev->cur_daddr;
  2127. #ifdef CONFIG_XFRM
  2128. if (pkt_dev->flags & F_IPSEC)
  2129. get_ipsec_sa(pkt_dev, flow);
  2130. #endif
  2131. pkt_dev->nflows++;
  2132. }
  2133. }
  2134. } else { /* IPV6 * */
  2135. if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
  2136. int i;
  2137. /* Only random destinations yet */
  2138. for (i = 0; i < 4; i++) {
  2139. pkt_dev->cur_in6_daddr.s6_addr32[i] =
  2140. (((__force __be32)prandom_u32() |
  2141. pkt_dev->min_in6_daddr.s6_addr32[i]) &
  2142. pkt_dev->max_in6_daddr.s6_addr32[i]);
  2143. }
  2144. }
  2145. }
  2146. if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
  2147. __u32 t;
  2148. if (pkt_dev->flags & F_TXSIZE_RND) {
  2149. t = prandom_u32() %
  2150. (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
  2151. + pkt_dev->min_pkt_size;
  2152. } else {
  2153. t = pkt_dev->cur_pkt_size + 1;
  2154. if (t > pkt_dev->max_pkt_size)
  2155. t = pkt_dev->min_pkt_size;
  2156. }
  2157. pkt_dev->cur_pkt_size = t;
  2158. }
  2159. set_cur_queue_map(pkt_dev);
  2160. pkt_dev->flows[flow].count++;
  2161. }
  2162. #ifdef CONFIG_XFRM
  2163. static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
  2164. [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
  2165. };
  2166. static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
  2167. {
  2168. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2169. int err = 0;
  2170. struct net *net = dev_net(pkt_dev->odev);
  2171. if (!x)
  2172. return 0;
  2173. /* XXX: we dont support tunnel mode for now until
  2174. * we resolve the dst issue */
  2175. if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
  2176. return 0;
  2177. /* But when user specify an valid SPI, transformation
  2178. * supports both transport/tunnel mode + ESP/AH type.
  2179. */
  2180. if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
  2181. skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
  2182. rcu_read_lock_bh();
  2183. err = x->outer_mode->output(x, skb);
  2184. rcu_read_unlock_bh();
  2185. if (err) {
  2186. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
  2187. goto error;
  2188. }
  2189. err = x->type->output(x, skb);
  2190. if (err) {
  2191. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  2192. goto error;
  2193. }
  2194. spin_lock_bh(&x->lock);
  2195. x->curlft.bytes += skb->len;
  2196. x->curlft.packets++;
  2197. spin_unlock_bh(&x->lock);
  2198. error:
  2199. return err;
  2200. }
  2201. static void free_SAs(struct pktgen_dev *pkt_dev)
  2202. {
  2203. if (pkt_dev->cflows) {
  2204. /* let go of the SAs if we have them */
  2205. int i;
  2206. for (i = 0; i < pkt_dev->cflows; i++) {
  2207. struct xfrm_state *x = pkt_dev->flows[i].x;
  2208. if (x) {
  2209. xfrm_state_put(x);
  2210. pkt_dev->flows[i].x = NULL;
  2211. }
  2212. }
  2213. }
  2214. }
  2215. static int process_ipsec(struct pktgen_dev *pkt_dev,
  2216. struct sk_buff *skb, __be16 protocol)
  2217. {
  2218. if (pkt_dev->flags & F_IPSEC) {
  2219. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2220. int nhead = 0;
  2221. if (x) {
  2222. struct ethhdr *eth;
  2223. struct iphdr *iph;
  2224. int ret;
  2225. nhead = x->props.header_len - skb_headroom(skb);
  2226. if (nhead > 0) {
  2227. ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
  2228. if (ret < 0) {
  2229. pr_err("Error expanding ipsec packet %d\n",
  2230. ret);
  2231. goto err;
  2232. }
  2233. }
  2234. /* ipsec is not expecting ll header */
  2235. skb_pull(skb, ETH_HLEN);
  2236. ret = pktgen_output_ipsec(skb, pkt_dev);
  2237. if (ret) {
  2238. pr_err("Error creating ipsec packet %d\n", ret);
  2239. goto err;
  2240. }
  2241. /* restore ll */
  2242. eth = skb_push(skb, ETH_HLEN);
  2243. memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
  2244. eth->h_proto = protocol;
  2245. /* Update IPv4 header len as well as checksum value */
  2246. iph = ip_hdr(skb);
  2247. iph->tot_len = htons(skb->len - ETH_HLEN);
  2248. ip_send_check(iph);
  2249. }
  2250. }
  2251. return 1;
  2252. err:
  2253. kfree_skb(skb);
  2254. return 0;
  2255. }
  2256. #endif
  2257. static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
  2258. {
  2259. unsigned int i;
  2260. for (i = 0; i < pkt_dev->nr_labels; i++)
  2261. *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
  2262. mpls--;
  2263. *mpls |= MPLS_STACK_BOTTOM;
  2264. }
  2265. static inline __be16 build_tci(unsigned int id, unsigned int cfi,
  2266. unsigned int prio)
  2267. {
  2268. return htons(id | (cfi << 12) | (prio << 13));
  2269. }
  2270. static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
  2271. int datalen)
  2272. {
  2273. struct timespec64 timestamp;
  2274. struct pktgen_hdr *pgh;
  2275. pgh = skb_put(skb, sizeof(*pgh));
  2276. datalen -= sizeof(*pgh);
  2277. if (pkt_dev->nfrags <= 0) {
  2278. skb_put_zero(skb, datalen);
  2279. } else {
  2280. int frags = pkt_dev->nfrags;
  2281. int i, len;
  2282. int frag_len;
  2283. if (frags > MAX_SKB_FRAGS)
  2284. frags = MAX_SKB_FRAGS;
  2285. len = datalen - frags * PAGE_SIZE;
  2286. if (len > 0) {
  2287. skb_put_zero(skb, len);
  2288. datalen = frags * PAGE_SIZE;
  2289. }
  2290. i = 0;
  2291. frag_len = (datalen/frags) < PAGE_SIZE ?
  2292. (datalen/frags) : PAGE_SIZE;
  2293. while (datalen > 0) {
  2294. if (unlikely(!pkt_dev->page)) {
  2295. int node = numa_node_id();
  2296. if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
  2297. node = pkt_dev->node;
  2298. pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2299. if (!pkt_dev->page)
  2300. break;
  2301. }
  2302. get_page(pkt_dev->page);
  2303. skb_frag_set_page(skb, i, pkt_dev->page);
  2304. skb_shinfo(skb)->frags[i].page_offset = 0;
  2305. /*last fragment, fill rest of data*/
  2306. if (i == (frags - 1))
  2307. skb_frag_size_set(&skb_shinfo(skb)->frags[i],
  2308. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
  2309. else
  2310. skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
  2311. datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2312. skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2313. skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2314. i++;
  2315. skb_shinfo(skb)->nr_frags = i;
  2316. }
  2317. }
  2318. /* Stamp the time, and sequence number,
  2319. * convert them to network byte order
  2320. */
  2321. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2322. pgh->seq_num = htonl(pkt_dev->seq_num);
  2323. if (pkt_dev->flags & F_NO_TIMESTAMP) {
  2324. pgh->tv_sec = 0;
  2325. pgh->tv_usec = 0;
  2326. } else {
  2327. /*
  2328. * pgh->tv_sec wraps in y2106 when interpreted as unsigned
  2329. * as done by wireshark, or y2038 when interpreted as signed.
  2330. * This is probably harmless, but if anyone wants to improve
  2331. * it, we could introduce a variant that puts 64-bit nanoseconds
  2332. * into the respective header bytes.
  2333. * This would also be slightly faster to read.
  2334. */
  2335. ktime_get_real_ts64(&timestamp);
  2336. pgh->tv_sec = htonl(timestamp.tv_sec);
  2337. pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
  2338. }
  2339. }
  2340. static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
  2341. struct pktgen_dev *pkt_dev)
  2342. {
  2343. unsigned int extralen = LL_RESERVED_SPACE(dev);
  2344. struct sk_buff *skb = NULL;
  2345. unsigned int size;
  2346. size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
  2347. if (pkt_dev->flags & F_NODE) {
  2348. int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
  2349. skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
  2350. if (likely(skb)) {
  2351. skb_reserve(skb, NET_SKB_PAD);
  2352. skb->dev = dev;
  2353. }
  2354. } else {
  2355. skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
  2356. }
  2357. /* the caller pre-fetches from skb->data and reserves for the mac hdr */
  2358. if (likely(skb))
  2359. skb_reserve(skb, extralen - 16);
  2360. return skb;
  2361. }
  2362. static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
  2363. struct pktgen_dev *pkt_dev)
  2364. {
  2365. struct sk_buff *skb = NULL;
  2366. __u8 *eth;
  2367. struct udphdr *udph;
  2368. int datalen, iplen;
  2369. struct iphdr *iph;
  2370. __be16 protocol = htons(ETH_P_IP);
  2371. __be32 *mpls;
  2372. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2373. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2374. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2375. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2376. u16 queue_map;
  2377. if (pkt_dev->nr_labels)
  2378. protocol = htons(ETH_P_MPLS_UC);
  2379. if (pkt_dev->vlan_id != 0xffff)
  2380. protocol = htons(ETH_P_8021Q);
  2381. /* Update any of the values, used when we're incrementing various
  2382. * fields.
  2383. */
  2384. mod_cur_headers(pkt_dev);
  2385. queue_map = pkt_dev->cur_queue_map;
  2386. skb = pktgen_alloc_skb(odev, pkt_dev);
  2387. if (!skb) {
  2388. sprintf(pkt_dev->result, "No memory");
  2389. return NULL;
  2390. }
  2391. prefetchw(skb->data);
  2392. skb_reserve(skb, 16);
  2393. /* Reserve for ethernet and IP header */
  2394. eth = skb_push(skb, 14);
  2395. mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
  2396. if (pkt_dev->nr_labels)
  2397. mpls_push(mpls, pkt_dev);
  2398. if (pkt_dev->vlan_id != 0xffff) {
  2399. if (pkt_dev->svlan_id != 0xffff) {
  2400. svlan_tci = skb_put(skb, sizeof(__be16));
  2401. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2402. pkt_dev->svlan_cfi,
  2403. pkt_dev->svlan_p);
  2404. svlan_encapsulated_proto = skb_put(skb,
  2405. sizeof(__be16));
  2406. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2407. }
  2408. vlan_tci = skb_put(skb, sizeof(__be16));
  2409. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2410. pkt_dev->vlan_cfi,
  2411. pkt_dev->vlan_p);
  2412. vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
  2413. *vlan_encapsulated_proto = htons(ETH_P_IP);
  2414. }
  2415. skb_reset_mac_header(skb);
  2416. skb_set_network_header(skb, skb->len);
  2417. iph = skb_put(skb, sizeof(struct iphdr));
  2418. skb_set_transport_header(skb, skb->len);
  2419. udph = skb_put(skb, sizeof(struct udphdr));
  2420. skb_set_queue_mapping(skb, queue_map);
  2421. skb->priority = pkt_dev->skb_priority;
  2422. memcpy(eth, pkt_dev->hh, 12);
  2423. *(__be16 *) & eth[12] = protocol;
  2424. /* Eth + IPh + UDPh + mpls */
  2425. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
  2426. pkt_dev->pkt_overhead;
  2427. if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
  2428. datalen = sizeof(struct pktgen_hdr);
  2429. udph->source = htons(pkt_dev->cur_udp_src);
  2430. udph->dest = htons(pkt_dev->cur_udp_dst);
  2431. udph->len = htons(datalen + 8); /* DATA + udphdr */
  2432. udph->check = 0;
  2433. iph->ihl = 5;
  2434. iph->version = 4;
  2435. iph->ttl = 32;
  2436. iph->tos = pkt_dev->tos;
  2437. iph->protocol = IPPROTO_UDP; /* UDP */
  2438. iph->saddr = pkt_dev->cur_saddr;
  2439. iph->daddr = pkt_dev->cur_daddr;
  2440. iph->id = htons(pkt_dev->ip_id);
  2441. pkt_dev->ip_id++;
  2442. iph->frag_off = 0;
  2443. iplen = 20 + 8 + datalen;
  2444. iph->tot_len = htons(iplen);
  2445. ip_send_check(iph);
  2446. skb->protocol = protocol;
  2447. skb->dev = odev;
  2448. skb->pkt_type = PACKET_HOST;
  2449. pktgen_finalize_skb(pkt_dev, skb, datalen);
  2450. if (!(pkt_dev->flags & F_UDPCSUM)) {
  2451. skb->ip_summed = CHECKSUM_NONE;
  2452. } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
  2453. skb->ip_summed = CHECKSUM_PARTIAL;
  2454. skb->csum = 0;
  2455. udp4_hwcsum(skb, iph->saddr, iph->daddr);
  2456. } else {
  2457. __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
  2458. /* add protocol-dependent pseudo-header */
  2459. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  2460. datalen + 8, IPPROTO_UDP, csum);
  2461. if (udph->check == 0)
  2462. udph->check = CSUM_MANGLED_0;
  2463. }
  2464. #ifdef CONFIG_XFRM
  2465. if (!process_ipsec(pkt_dev, skb, protocol))
  2466. return NULL;
  2467. #endif
  2468. return skb;
  2469. }
  2470. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  2471. struct pktgen_dev *pkt_dev)
  2472. {
  2473. struct sk_buff *skb = NULL;
  2474. __u8 *eth;
  2475. struct udphdr *udph;
  2476. int datalen, udplen;
  2477. struct ipv6hdr *iph;
  2478. __be16 protocol = htons(ETH_P_IPV6);
  2479. __be32 *mpls;
  2480. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2481. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2482. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2483. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2484. u16 queue_map;
  2485. if (pkt_dev->nr_labels)
  2486. protocol = htons(ETH_P_MPLS_UC);
  2487. if (pkt_dev->vlan_id != 0xffff)
  2488. protocol = htons(ETH_P_8021Q);
  2489. /* Update any of the values, used when we're incrementing various
  2490. * fields.
  2491. */
  2492. mod_cur_headers(pkt_dev);
  2493. queue_map = pkt_dev->cur_queue_map;
  2494. skb = pktgen_alloc_skb(odev, pkt_dev);
  2495. if (!skb) {
  2496. sprintf(pkt_dev->result, "No memory");
  2497. return NULL;
  2498. }
  2499. prefetchw(skb->data);
  2500. skb_reserve(skb, 16);
  2501. /* Reserve for ethernet and IP header */
  2502. eth = skb_push(skb, 14);
  2503. mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
  2504. if (pkt_dev->nr_labels)
  2505. mpls_push(mpls, pkt_dev);
  2506. if (pkt_dev->vlan_id != 0xffff) {
  2507. if (pkt_dev->svlan_id != 0xffff) {
  2508. svlan_tci = skb_put(skb, sizeof(__be16));
  2509. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2510. pkt_dev->svlan_cfi,
  2511. pkt_dev->svlan_p);
  2512. svlan_encapsulated_proto = skb_put(skb,
  2513. sizeof(__be16));
  2514. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2515. }
  2516. vlan_tci = skb_put(skb, sizeof(__be16));
  2517. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2518. pkt_dev->vlan_cfi,
  2519. pkt_dev->vlan_p);
  2520. vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
  2521. *vlan_encapsulated_proto = htons(ETH_P_IPV6);
  2522. }
  2523. skb_reset_mac_header(skb);
  2524. skb_set_network_header(skb, skb->len);
  2525. iph = skb_put(skb, sizeof(struct ipv6hdr));
  2526. skb_set_transport_header(skb, skb->len);
  2527. udph = skb_put(skb, sizeof(struct udphdr));
  2528. skb_set_queue_mapping(skb, queue_map);
  2529. skb->priority = pkt_dev->skb_priority;
  2530. memcpy(eth, pkt_dev->hh, 12);
  2531. *(__be16 *) &eth[12] = protocol;
  2532. /* Eth + IPh + UDPh + mpls */
  2533. datalen = pkt_dev->cur_pkt_size - 14 -
  2534. sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
  2535. pkt_dev->pkt_overhead;
  2536. if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
  2537. datalen = sizeof(struct pktgen_hdr);
  2538. net_info_ratelimited("increased datalen to %d\n", datalen);
  2539. }
  2540. udplen = datalen + sizeof(struct udphdr);
  2541. udph->source = htons(pkt_dev->cur_udp_src);
  2542. udph->dest = htons(pkt_dev->cur_udp_dst);
  2543. udph->len = htons(udplen);
  2544. udph->check = 0;
  2545. *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
  2546. if (pkt_dev->traffic_class) {
  2547. /* Version + traffic class + flow (0) */
  2548. *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
  2549. }
  2550. iph->hop_limit = 32;
  2551. iph->payload_len = htons(udplen);
  2552. iph->nexthdr = IPPROTO_UDP;
  2553. iph->daddr = pkt_dev->cur_in6_daddr;
  2554. iph->saddr = pkt_dev->cur_in6_saddr;
  2555. skb->protocol = protocol;
  2556. skb->dev = odev;
  2557. skb->pkt_type = PACKET_HOST;
  2558. pktgen_finalize_skb(pkt_dev, skb, datalen);
  2559. if (!(pkt_dev->flags & F_UDPCSUM)) {
  2560. skb->ip_summed = CHECKSUM_NONE;
  2561. } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
  2562. skb->ip_summed = CHECKSUM_PARTIAL;
  2563. skb->csum_start = skb_transport_header(skb) - skb->head;
  2564. skb->csum_offset = offsetof(struct udphdr, check);
  2565. udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
  2566. } else {
  2567. __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
  2568. /* add protocol-dependent pseudo-header */
  2569. udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
  2570. if (udph->check == 0)
  2571. udph->check = CSUM_MANGLED_0;
  2572. }
  2573. return skb;
  2574. }
  2575. static struct sk_buff *fill_packet(struct net_device *odev,
  2576. struct pktgen_dev *pkt_dev)
  2577. {
  2578. if (pkt_dev->flags & F_IPV6)
  2579. return fill_packet_ipv6(odev, pkt_dev);
  2580. else
  2581. return fill_packet_ipv4(odev, pkt_dev);
  2582. }
  2583. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  2584. {
  2585. pkt_dev->seq_num = 1;
  2586. pkt_dev->idle_acc = 0;
  2587. pkt_dev->sofar = 0;
  2588. pkt_dev->tx_bytes = 0;
  2589. pkt_dev->errors = 0;
  2590. }
  2591. /* Set up structure for sending pkts, clear counters */
  2592. static void pktgen_run(struct pktgen_thread *t)
  2593. {
  2594. struct pktgen_dev *pkt_dev;
  2595. int started = 0;
  2596. func_enter();
  2597. rcu_read_lock();
  2598. list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
  2599. /*
  2600. * setup odev and create initial packet.
  2601. */
  2602. pktgen_setup_inject(pkt_dev);
  2603. if (pkt_dev->odev) {
  2604. pktgen_clear_counters(pkt_dev);
  2605. pkt_dev->skb = NULL;
  2606. pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
  2607. set_pkt_overhead(pkt_dev);
  2608. strcpy(pkt_dev->result, "Starting");
  2609. pkt_dev->running = 1; /* Cranke yeself! */
  2610. started++;
  2611. } else
  2612. strcpy(pkt_dev->result, "Error starting");
  2613. }
  2614. rcu_read_unlock();
  2615. if (started)
  2616. t->control &= ~(T_STOP);
  2617. }
  2618. static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
  2619. {
  2620. struct pktgen_thread *t;
  2621. func_enter();
  2622. mutex_lock(&pktgen_thread_lock);
  2623. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2624. t->control |= T_STOP;
  2625. mutex_unlock(&pktgen_thread_lock);
  2626. }
  2627. static int thread_is_running(const struct pktgen_thread *t)
  2628. {
  2629. const struct pktgen_dev *pkt_dev;
  2630. rcu_read_lock();
  2631. list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
  2632. if (pkt_dev->running) {
  2633. rcu_read_unlock();
  2634. return 1;
  2635. }
  2636. rcu_read_unlock();
  2637. return 0;
  2638. }
  2639. static int pktgen_wait_thread_run(struct pktgen_thread *t)
  2640. {
  2641. while (thread_is_running(t)) {
  2642. msleep_interruptible(100);
  2643. if (signal_pending(current))
  2644. goto signal;
  2645. }
  2646. return 1;
  2647. signal:
  2648. return 0;
  2649. }
  2650. static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
  2651. {
  2652. struct pktgen_thread *t;
  2653. int sig = 1;
  2654. mutex_lock(&pktgen_thread_lock);
  2655. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  2656. sig = pktgen_wait_thread_run(t);
  2657. if (sig == 0)
  2658. break;
  2659. }
  2660. if (sig == 0)
  2661. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2662. t->control |= (T_STOP);
  2663. mutex_unlock(&pktgen_thread_lock);
  2664. return sig;
  2665. }
  2666. static void pktgen_run_all_threads(struct pktgen_net *pn)
  2667. {
  2668. struct pktgen_thread *t;
  2669. func_enter();
  2670. mutex_lock(&pktgen_thread_lock);
  2671. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2672. t->control |= (T_RUN);
  2673. mutex_unlock(&pktgen_thread_lock);
  2674. /* Propagate thread->control */
  2675. schedule_timeout_interruptible(msecs_to_jiffies(125));
  2676. pktgen_wait_all_threads_run(pn);
  2677. }
  2678. static void pktgen_reset_all_threads(struct pktgen_net *pn)
  2679. {
  2680. struct pktgen_thread *t;
  2681. func_enter();
  2682. mutex_lock(&pktgen_thread_lock);
  2683. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2684. t->control |= (T_REMDEVALL);
  2685. mutex_unlock(&pktgen_thread_lock);
  2686. /* Propagate thread->control */
  2687. schedule_timeout_interruptible(msecs_to_jiffies(125));
  2688. pktgen_wait_all_threads_run(pn);
  2689. }
  2690. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2691. {
  2692. __u64 bps, mbps, pps;
  2693. char *p = pkt_dev->result;
  2694. ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
  2695. pkt_dev->started_at);
  2696. ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
  2697. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2698. (unsigned long long)ktime_to_us(elapsed),
  2699. (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
  2700. (unsigned long long)ktime_to_us(idle),
  2701. (unsigned long long)pkt_dev->sofar,
  2702. pkt_dev->cur_pkt_size, nr_frags);
  2703. pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
  2704. ktime_to_ns(elapsed));
  2705. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2706. mbps = bps;
  2707. do_div(mbps, 1000000);
  2708. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2709. (unsigned long long)pps,
  2710. (unsigned long long)mbps,
  2711. (unsigned long long)bps,
  2712. (unsigned long long)pkt_dev->errors);
  2713. }
  2714. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2715. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2716. {
  2717. int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
  2718. if (!pkt_dev->running) {
  2719. pr_warn("interface: %s is already stopped\n",
  2720. pkt_dev->odevname);
  2721. return -EINVAL;
  2722. }
  2723. pkt_dev->running = 0;
  2724. kfree_skb(pkt_dev->skb);
  2725. pkt_dev->skb = NULL;
  2726. pkt_dev->stopped_at = ktime_get();
  2727. show_results(pkt_dev, nr_frags);
  2728. return 0;
  2729. }
  2730. static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
  2731. {
  2732. struct pktgen_dev *pkt_dev, *best = NULL;
  2733. rcu_read_lock();
  2734. list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
  2735. if (!pkt_dev->running)
  2736. continue;
  2737. if (best == NULL)
  2738. best = pkt_dev;
  2739. else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
  2740. best = pkt_dev;
  2741. }
  2742. rcu_read_unlock();
  2743. return best;
  2744. }
  2745. static void pktgen_stop(struct pktgen_thread *t)
  2746. {
  2747. struct pktgen_dev *pkt_dev;
  2748. func_enter();
  2749. rcu_read_lock();
  2750. list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
  2751. pktgen_stop_device(pkt_dev);
  2752. }
  2753. rcu_read_unlock();
  2754. }
  2755. /*
  2756. * one of our devices needs to be removed - find it
  2757. * and remove it
  2758. */
  2759. static void pktgen_rem_one_if(struct pktgen_thread *t)
  2760. {
  2761. struct list_head *q, *n;
  2762. struct pktgen_dev *cur;
  2763. func_enter();
  2764. list_for_each_safe(q, n, &t->if_list) {
  2765. cur = list_entry(q, struct pktgen_dev, list);
  2766. if (!cur->removal_mark)
  2767. continue;
  2768. kfree_skb(cur->skb);
  2769. cur->skb = NULL;
  2770. pktgen_remove_device(t, cur);
  2771. break;
  2772. }
  2773. }
  2774. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2775. {
  2776. struct list_head *q, *n;
  2777. struct pktgen_dev *cur;
  2778. func_enter();
  2779. /* Remove all devices, free mem */
  2780. list_for_each_safe(q, n, &t->if_list) {
  2781. cur = list_entry(q, struct pktgen_dev, list);
  2782. kfree_skb(cur->skb);
  2783. cur->skb = NULL;
  2784. pktgen_remove_device(t, cur);
  2785. }
  2786. }
  2787. static void pktgen_rem_thread(struct pktgen_thread *t)
  2788. {
  2789. /* Remove from the thread list */
  2790. remove_proc_entry(t->tsk->comm, t->net->proc_dir);
  2791. }
  2792. static void pktgen_resched(struct pktgen_dev *pkt_dev)
  2793. {
  2794. ktime_t idle_start = ktime_get();
  2795. schedule();
  2796. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
  2797. }
  2798. static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
  2799. {
  2800. ktime_t idle_start = ktime_get();
  2801. while (refcount_read(&(pkt_dev->skb->users)) != 1) {
  2802. if (signal_pending(current))
  2803. break;
  2804. if (need_resched())
  2805. pktgen_resched(pkt_dev);
  2806. else
  2807. cpu_relax();
  2808. }
  2809. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
  2810. }
  2811. static void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2812. {
  2813. unsigned int burst = READ_ONCE(pkt_dev->burst);
  2814. struct net_device *odev = pkt_dev->odev;
  2815. struct netdev_queue *txq;
  2816. struct sk_buff *skb;
  2817. int ret;
  2818. /* If device is offline, then don't send */
  2819. if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
  2820. pktgen_stop_device(pkt_dev);
  2821. return;
  2822. }
  2823. /* This is max DELAY, this has special meaning of
  2824. * "never transmit"
  2825. */
  2826. if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
  2827. pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
  2828. return;
  2829. }
  2830. /* If no skb or clone count exhausted then get new one */
  2831. if (!pkt_dev->skb || (pkt_dev->last_ok &&
  2832. ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
  2833. /* build a new pkt */
  2834. kfree_skb(pkt_dev->skb);
  2835. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2836. if (pkt_dev->skb == NULL) {
  2837. pr_err("ERROR: couldn't allocate skb in fill_packet\n");
  2838. schedule();
  2839. pkt_dev->clone_count--; /* back out increment, OOM */
  2840. return;
  2841. }
  2842. pkt_dev->last_pkt_size = pkt_dev->skb->len;
  2843. pkt_dev->clone_count = 0; /* reset counter */
  2844. }
  2845. if (pkt_dev->delay && pkt_dev->last_ok)
  2846. spin(pkt_dev, pkt_dev->next_tx);
  2847. if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
  2848. skb = pkt_dev->skb;
  2849. skb->protocol = eth_type_trans(skb, skb->dev);
  2850. refcount_add(burst, &skb->users);
  2851. local_bh_disable();
  2852. do {
  2853. ret = netif_receive_skb(skb);
  2854. if (ret == NET_RX_DROP)
  2855. pkt_dev->errors++;
  2856. pkt_dev->sofar++;
  2857. pkt_dev->seq_num++;
  2858. if (refcount_read(&skb->users) != burst) {
  2859. /* skb was queued by rps/rfs or taps,
  2860. * so cannot reuse this skb
  2861. */
  2862. WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
  2863. /* get out of the loop and wait
  2864. * until skb is consumed
  2865. */
  2866. break;
  2867. }
  2868. /* skb was 'freed' by stack, so clean few
  2869. * bits and reuse it
  2870. */
  2871. skb_reset_tc(skb);
  2872. } while (--burst > 0);
  2873. goto out; /* Skips xmit_mode M_START_XMIT */
  2874. } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
  2875. local_bh_disable();
  2876. refcount_inc(&pkt_dev->skb->users);
  2877. ret = dev_queue_xmit(pkt_dev->skb);
  2878. switch (ret) {
  2879. case NET_XMIT_SUCCESS:
  2880. pkt_dev->sofar++;
  2881. pkt_dev->seq_num++;
  2882. pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
  2883. break;
  2884. case NET_XMIT_DROP:
  2885. case NET_XMIT_CN:
  2886. /* These are all valid return codes for a qdisc but
  2887. * indicate packets are being dropped or will likely
  2888. * be dropped soon.
  2889. */
  2890. case NETDEV_TX_BUSY:
  2891. /* qdisc may call dev_hard_start_xmit directly in cases
  2892. * where no queues exist e.g. loopback device, virtual
  2893. * devices, etc. In this case we need to handle
  2894. * NETDEV_TX_ codes.
  2895. */
  2896. default:
  2897. pkt_dev->errors++;
  2898. net_info_ratelimited("%s xmit error: %d\n",
  2899. pkt_dev->odevname, ret);
  2900. break;
  2901. }
  2902. goto out;
  2903. }
  2904. txq = skb_get_tx_queue(odev, pkt_dev->skb);
  2905. local_bh_disable();
  2906. HARD_TX_LOCK(odev, txq, smp_processor_id());
  2907. if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
  2908. ret = NETDEV_TX_BUSY;
  2909. pkt_dev->last_ok = 0;
  2910. goto unlock;
  2911. }
  2912. refcount_add(burst, &pkt_dev->skb->users);
  2913. xmit_more:
  2914. ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
  2915. switch (ret) {
  2916. case NETDEV_TX_OK:
  2917. pkt_dev->last_ok = 1;
  2918. pkt_dev->sofar++;
  2919. pkt_dev->seq_num++;
  2920. pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
  2921. if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
  2922. goto xmit_more;
  2923. break;
  2924. case NET_XMIT_DROP:
  2925. case NET_XMIT_CN:
  2926. /* skb has been consumed */
  2927. pkt_dev->errors++;
  2928. break;
  2929. default: /* Drivers are not supposed to return other values! */
  2930. net_info_ratelimited("%s xmit error: %d\n",
  2931. pkt_dev->odevname, ret);
  2932. pkt_dev->errors++;
  2933. /* fallthru */
  2934. case NETDEV_TX_BUSY:
  2935. /* Retry it next time */
  2936. refcount_dec(&(pkt_dev->skb->users));
  2937. pkt_dev->last_ok = 0;
  2938. }
  2939. if (unlikely(burst))
  2940. WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
  2941. unlock:
  2942. HARD_TX_UNLOCK(odev, txq);
  2943. out:
  2944. local_bh_enable();
  2945. /* If pkt_dev->count is zero, then run forever */
  2946. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  2947. pktgen_wait_for_skb(pkt_dev);
  2948. /* Done with this */
  2949. pktgen_stop_device(pkt_dev);
  2950. }
  2951. }
  2952. /*
  2953. * Main loop of the thread goes here
  2954. */
  2955. static int pktgen_thread_worker(void *arg)
  2956. {
  2957. DEFINE_WAIT(wait);
  2958. struct pktgen_thread *t = arg;
  2959. struct pktgen_dev *pkt_dev = NULL;
  2960. int cpu = t->cpu;
  2961. BUG_ON(smp_processor_id() != cpu);
  2962. init_waitqueue_head(&t->queue);
  2963. complete(&t->start_done);
  2964. pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
  2965. set_freezable();
  2966. while (!kthread_should_stop()) {
  2967. pkt_dev = next_to_run(t);
  2968. if (unlikely(!pkt_dev && t->control == 0)) {
  2969. if (t->net->pktgen_exiting)
  2970. break;
  2971. wait_event_interruptible_timeout(t->queue,
  2972. t->control != 0,
  2973. HZ/10);
  2974. try_to_freeze();
  2975. continue;
  2976. }
  2977. if (likely(pkt_dev)) {
  2978. pktgen_xmit(pkt_dev);
  2979. if (need_resched())
  2980. pktgen_resched(pkt_dev);
  2981. else
  2982. cpu_relax();
  2983. }
  2984. if (t->control & T_STOP) {
  2985. pktgen_stop(t);
  2986. t->control &= ~(T_STOP);
  2987. }
  2988. if (t->control & T_RUN) {
  2989. pktgen_run(t);
  2990. t->control &= ~(T_RUN);
  2991. }
  2992. if (t->control & T_REMDEVALL) {
  2993. pktgen_rem_all_ifs(t);
  2994. t->control &= ~(T_REMDEVALL);
  2995. }
  2996. if (t->control & T_REMDEV) {
  2997. pktgen_rem_one_if(t);
  2998. t->control &= ~(T_REMDEV);
  2999. }
  3000. try_to_freeze();
  3001. }
  3002. pr_debug("%s stopping all device\n", t->tsk->comm);
  3003. pktgen_stop(t);
  3004. pr_debug("%s removing all device\n", t->tsk->comm);
  3005. pktgen_rem_all_ifs(t);
  3006. pr_debug("%s removing thread\n", t->tsk->comm);
  3007. pktgen_rem_thread(t);
  3008. return 0;
  3009. }
  3010. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  3011. const char *ifname, bool exact)
  3012. {
  3013. struct pktgen_dev *p, *pkt_dev = NULL;
  3014. size_t len = strlen(ifname);
  3015. rcu_read_lock();
  3016. list_for_each_entry_rcu(p, &t->if_list, list)
  3017. if (strncmp(p->odevname, ifname, len) == 0) {
  3018. if (p->odevname[len]) {
  3019. if (exact || p->odevname[len] != '@')
  3020. continue;
  3021. }
  3022. pkt_dev = p;
  3023. break;
  3024. }
  3025. rcu_read_unlock();
  3026. pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
  3027. return pkt_dev;
  3028. }
  3029. /*
  3030. * Adds a dev at front of if_list.
  3031. */
  3032. static int add_dev_to_thread(struct pktgen_thread *t,
  3033. struct pktgen_dev *pkt_dev)
  3034. {
  3035. int rv = 0;
  3036. /* This function cannot be called concurrently, as its called
  3037. * under pktgen_thread_lock mutex, but it can run from
  3038. * userspace on another CPU than the kthread. The if_lock()
  3039. * is used here to sync with concurrent instances of
  3040. * _rem_dev_from_if_list() invoked via kthread, which is also
  3041. * updating the if_list */
  3042. if_lock(t);
  3043. if (pkt_dev->pg_thread) {
  3044. pr_err("ERROR: already assigned to a thread\n");
  3045. rv = -EBUSY;
  3046. goto out;
  3047. }
  3048. pkt_dev->running = 0;
  3049. pkt_dev->pg_thread = t;
  3050. list_add_rcu(&pkt_dev->list, &t->if_list);
  3051. out:
  3052. if_unlock(t);
  3053. return rv;
  3054. }
  3055. /* Called under thread lock */
  3056. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
  3057. {
  3058. struct pktgen_dev *pkt_dev;
  3059. int err;
  3060. int node = cpu_to_node(t->cpu);
  3061. /* We don't allow a device to be on several threads */
  3062. pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
  3063. if (pkt_dev) {
  3064. pr_err("ERROR: interface already used\n");
  3065. return -EBUSY;
  3066. }
  3067. pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
  3068. if (!pkt_dev)
  3069. return -ENOMEM;
  3070. strcpy(pkt_dev->odevname, ifname);
  3071. pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
  3072. node);
  3073. if (pkt_dev->flows == NULL) {
  3074. kfree(pkt_dev);
  3075. return -ENOMEM;
  3076. }
  3077. pkt_dev->removal_mark = 0;
  3078. pkt_dev->nfrags = 0;
  3079. pkt_dev->delay = pg_delay_d;
  3080. pkt_dev->count = pg_count_d;
  3081. pkt_dev->sofar = 0;
  3082. pkt_dev->udp_src_min = 9; /* sink port */
  3083. pkt_dev->udp_src_max = 9;
  3084. pkt_dev->udp_dst_min = 9;
  3085. pkt_dev->udp_dst_max = 9;
  3086. pkt_dev->vlan_p = 0;
  3087. pkt_dev->vlan_cfi = 0;
  3088. pkt_dev->vlan_id = 0xffff;
  3089. pkt_dev->svlan_p = 0;
  3090. pkt_dev->svlan_cfi = 0;
  3091. pkt_dev->svlan_id = 0xffff;
  3092. pkt_dev->burst = 1;
  3093. pkt_dev->node = -1;
  3094. err = pktgen_setup_dev(t->net, pkt_dev, ifname);
  3095. if (err)
  3096. goto out1;
  3097. if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
  3098. pkt_dev->clone_skb = pg_clone_skb_d;
  3099. pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
  3100. &pktgen_if_fops, pkt_dev);
  3101. if (!pkt_dev->entry) {
  3102. pr_err("cannot create %s/%s procfs entry\n",
  3103. PG_PROC_DIR, ifname);
  3104. err = -EINVAL;
  3105. goto out2;
  3106. }
  3107. #ifdef CONFIG_XFRM
  3108. pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
  3109. pkt_dev->ipsproto = IPPROTO_ESP;
  3110. /* xfrm tunnel mode needs additional dst to extract outter
  3111. * ip header protocol/ttl/id field, here creat a phony one.
  3112. * instead of looking for a valid rt, which definitely hurting
  3113. * performance under such circumstance.
  3114. */
  3115. pkt_dev->dstops.family = AF_INET;
  3116. pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
  3117. dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
  3118. pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
  3119. pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
  3120. #endif
  3121. return add_dev_to_thread(t, pkt_dev);
  3122. out2:
  3123. dev_put(pkt_dev->odev);
  3124. out1:
  3125. #ifdef CONFIG_XFRM
  3126. free_SAs(pkt_dev);
  3127. #endif
  3128. vfree(pkt_dev->flows);
  3129. kfree(pkt_dev);
  3130. return err;
  3131. }
  3132. static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
  3133. {
  3134. struct pktgen_thread *t;
  3135. struct proc_dir_entry *pe;
  3136. struct task_struct *p;
  3137. t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
  3138. cpu_to_node(cpu));
  3139. if (!t) {
  3140. pr_err("ERROR: out of memory, can't create new thread\n");
  3141. return -ENOMEM;
  3142. }
  3143. mutex_init(&t->if_lock);
  3144. t->cpu = cpu;
  3145. INIT_LIST_HEAD(&t->if_list);
  3146. list_add_tail(&t->th_list, &pn->pktgen_threads);
  3147. init_completion(&t->start_done);
  3148. p = kthread_create_on_node(pktgen_thread_worker,
  3149. t,
  3150. cpu_to_node(cpu),
  3151. "kpktgend_%d", cpu);
  3152. if (IS_ERR(p)) {
  3153. pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
  3154. list_del(&t->th_list);
  3155. kfree(t);
  3156. return PTR_ERR(p);
  3157. }
  3158. kthread_bind(p, cpu);
  3159. t->tsk = p;
  3160. pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
  3161. &pktgen_thread_fops, t);
  3162. if (!pe) {
  3163. pr_err("cannot create %s/%s procfs entry\n",
  3164. PG_PROC_DIR, t->tsk->comm);
  3165. kthread_stop(p);
  3166. list_del(&t->th_list);
  3167. kfree(t);
  3168. return -EINVAL;
  3169. }
  3170. t->net = pn;
  3171. get_task_struct(p);
  3172. wake_up_process(p);
  3173. wait_for_completion(&t->start_done);
  3174. return 0;
  3175. }
  3176. /*
  3177. * Removes a device from the thread if_list.
  3178. */
  3179. static void _rem_dev_from_if_list(struct pktgen_thread *t,
  3180. struct pktgen_dev *pkt_dev)
  3181. {
  3182. struct list_head *q, *n;
  3183. struct pktgen_dev *p;
  3184. if_lock(t);
  3185. list_for_each_safe(q, n, &t->if_list) {
  3186. p = list_entry(q, struct pktgen_dev, list);
  3187. if (p == pkt_dev)
  3188. list_del_rcu(&p->list);
  3189. }
  3190. if_unlock(t);
  3191. }
  3192. static int pktgen_remove_device(struct pktgen_thread *t,
  3193. struct pktgen_dev *pkt_dev)
  3194. {
  3195. pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
  3196. if (pkt_dev->running) {
  3197. pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
  3198. pktgen_stop_device(pkt_dev);
  3199. }
  3200. /* Dis-associate from the interface */
  3201. if (pkt_dev->odev) {
  3202. dev_put(pkt_dev->odev);
  3203. pkt_dev->odev = NULL;
  3204. }
  3205. /* Remove proc before if_list entry, because add_device uses
  3206. * list to determine if interface already exist, avoid race
  3207. * with proc_create_data() */
  3208. proc_remove(pkt_dev->entry);
  3209. /* And update the thread if_list */
  3210. _rem_dev_from_if_list(t, pkt_dev);
  3211. #ifdef CONFIG_XFRM
  3212. free_SAs(pkt_dev);
  3213. #endif
  3214. vfree(pkt_dev->flows);
  3215. if (pkt_dev->page)
  3216. put_page(pkt_dev->page);
  3217. kfree_rcu(pkt_dev, rcu);
  3218. return 0;
  3219. }
  3220. static int __net_init pg_net_init(struct net *net)
  3221. {
  3222. struct pktgen_net *pn = net_generic(net, pg_net_id);
  3223. struct proc_dir_entry *pe;
  3224. int cpu, ret = 0;
  3225. pn->net = net;
  3226. INIT_LIST_HEAD(&pn->pktgen_threads);
  3227. pn->pktgen_exiting = false;
  3228. pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
  3229. if (!pn->proc_dir) {
  3230. pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
  3231. return -ENODEV;
  3232. }
  3233. pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
  3234. if (pe == NULL) {
  3235. pr_err("cannot create %s procfs entry\n", PGCTRL);
  3236. ret = -EINVAL;
  3237. goto remove;
  3238. }
  3239. for_each_online_cpu(cpu) {
  3240. int err;
  3241. err = pktgen_create_thread(cpu, pn);
  3242. if (err)
  3243. pr_warn("Cannot create thread for cpu %d (%d)\n",
  3244. cpu, err);
  3245. }
  3246. if (list_empty(&pn->pktgen_threads)) {
  3247. pr_err("Initialization failed for all threads\n");
  3248. ret = -ENODEV;
  3249. goto remove_entry;
  3250. }
  3251. return 0;
  3252. remove_entry:
  3253. remove_proc_entry(PGCTRL, pn->proc_dir);
  3254. remove:
  3255. remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
  3256. return ret;
  3257. }
  3258. static void __net_exit pg_net_exit(struct net *net)
  3259. {
  3260. struct pktgen_net *pn = net_generic(net, pg_net_id);
  3261. struct pktgen_thread *t;
  3262. struct list_head *q, *n;
  3263. LIST_HEAD(list);
  3264. /* Stop all interfaces & threads */
  3265. pn->pktgen_exiting = true;
  3266. mutex_lock(&pktgen_thread_lock);
  3267. list_splice_init(&pn->pktgen_threads, &list);
  3268. mutex_unlock(&pktgen_thread_lock);
  3269. list_for_each_safe(q, n, &list) {
  3270. t = list_entry(q, struct pktgen_thread, th_list);
  3271. list_del(&t->th_list);
  3272. kthread_stop(t->tsk);
  3273. put_task_struct(t->tsk);
  3274. kfree(t);
  3275. }
  3276. remove_proc_entry(PGCTRL, pn->proc_dir);
  3277. remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
  3278. }
  3279. static struct pernet_operations pg_net_ops = {
  3280. .init = pg_net_init,
  3281. .exit = pg_net_exit,
  3282. .id = &pg_net_id,
  3283. .size = sizeof(struct pktgen_net),
  3284. };
  3285. static int __init pg_init(void)
  3286. {
  3287. int ret = 0;
  3288. pr_info("%s", version);
  3289. ret = register_pernet_subsys(&pg_net_ops);
  3290. if (ret)
  3291. return ret;
  3292. ret = register_netdevice_notifier(&pktgen_notifier_block);
  3293. if (ret)
  3294. unregister_pernet_subsys(&pg_net_ops);
  3295. return ret;
  3296. }
  3297. static void __exit pg_cleanup(void)
  3298. {
  3299. unregister_netdevice_notifier(&pktgen_notifier_block);
  3300. unregister_pernet_subsys(&pg_net_ops);
  3301. /* Don't need rcu_barrier() due to use of kfree_rcu() */
  3302. }
  3303. module_init(pg_init);
  3304. module_exit(pg_cleanup);
  3305. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
  3306. MODULE_DESCRIPTION("Packet Generator tool");
  3307. MODULE_LICENSE("GPL");
  3308. MODULE_VERSION(VERSION);
  3309. module_param(pg_count_d, int, 0);
  3310. MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
  3311. module_param(pg_delay_d, int, 0);
  3312. MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
  3313. module_param(pg_clone_skb_d, int, 0);
  3314. MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
  3315. module_param(debug, int, 0);
  3316. MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");