pktgen.c 79 KB

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