pktgen.c 93 KB

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