pktgen.c 98 KB

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