pktgen.c 97 KB

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