pktgen.c 97 KB

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