pktgen.c 84 KB

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