pktgen.c 93 KB

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