pktgen.c 93 KB

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