pktgen.c 98 KB

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