pktgen.c 93 KB

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