pktgen.c 94 KB

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