pktgen.c 87 KB

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