pktgen.c 86 KB

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