pktgen.c 87 KB

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