pktgen.c 93 KB

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