pktgen.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  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 "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. inline static 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 spinlock_t _thread_lock = SPIN_LOCK_UNLOCKED;
  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. current->state = TASK_INTERRUPTIBLE;
  1235. schedule_timeout(HZ/8); /* Propagate thread->control */
  1236. ret = count;
  1237. sprintf(pg_result, "OK: rem_device_all");
  1238. goto out;
  1239. }
  1240. if (!strcmp(name, "max_before_softirq")) {
  1241. len = num_arg(&user_buffer[i], 10, &value);
  1242. thread_lock();
  1243. t->max_before_softirq = value;
  1244. thread_unlock();
  1245. ret = count;
  1246. sprintf(pg_result, "OK: max_before_softirq=%lu", value);
  1247. goto out;
  1248. }
  1249. ret = -EINVAL;
  1250. out:
  1251. return ret;
  1252. }
  1253. static int create_proc_dir(void)
  1254. {
  1255. int len;
  1256. /* does proc_dir already exists */
  1257. len = strlen(PG_PROC_DIR);
  1258. for (pg_proc_dir = proc_net->subdir; pg_proc_dir; pg_proc_dir=pg_proc_dir->next) {
  1259. if ((pg_proc_dir->namelen == len) &&
  1260. (! memcmp(pg_proc_dir->name, PG_PROC_DIR, len)))
  1261. break;
  1262. }
  1263. if (!pg_proc_dir)
  1264. pg_proc_dir = create_proc_entry(PG_PROC_DIR, S_IFDIR, proc_net);
  1265. if (!pg_proc_dir)
  1266. return -ENODEV;
  1267. return 0;
  1268. }
  1269. static int remove_proc_dir(void)
  1270. {
  1271. remove_proc_entry(PG_PROC_DIR, proc_net);
  1272. return 0;
  1273. }
  1274. /* Think find or remove for NN */
  1275. static struct pktgen_dev *__pktgen_NN_threads(const char* ifname, int remove)
  1276. {
  1277. struct pktgen_thread *t;
  1278. struct pktgen_dev *pkt_dev = NULL;
  1279. t = pktgen_threads;
  1280. while (t) {
  1281. pkt_dev = pktgen_find_dev(t, ifname);
  1282. if (pkt_dev) {
  1283. if(remove) {
  1284. if_lock(t);
  1285. pktgen_remove_device(t, pkt_dev);
  1286. if_unlock(t);
  1287. }
  1288. break;
  1289. }
  1290. t = t->next;
  1291. }
  1292. return pkt_dev;
  1293. }
  1294. static struct pktgen_dev *pktgen_NN_threads(const char* ifname, int remove)
  1295. {
  1296. struct pktgen_dev *pkt_dev = NULL;
  1297. thread_lock();
  1298. pkt_dev = __pktgen_NN_threads(ifname, remove);
  1299. thread_unlock();
  1300. return pkt_dev;
  1301. }
  1302. static int pktgen_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  1303. {
  1304. struct net_device *dev = (struct net_device *)(ptr);
  1305. /* It is OK that we do not hold the group lock right now,
  1306. * as we run under the RTNL lock.
  1307. */
  1308. switch (event) {
  1309. case NETDEV_CHANGEADDR:
  1310. case NETDEV_GOING_DOWN:
  1311. case NETDEV_DOWN:
  1312. case NETDEV_UP:
  1313. /* Ignore for now */
  1314. break;
  1315. case NETDEV_UNREGISTER:
  1316. pktgen_NN_threads(dev->name, REMOVE);
  1317. break;
  1318. };
  1319. return NOTIFY_DONE;
  1320. }
  1321. /* Associate pktgen_dev with a device. */
  1322. static struct net_device* pktgen_setup_dev(struct pktgen_dev *pkt_dev) {
  1323. struct net_device *odev;
  1324. /* Clean old setups */
  1325. if (pkt_dev->odev) {
  1326. dev_put(pkt_dev->odev);
  1327. pkt_dev->odev = NULL;
  1328. }
  1329. odev = dev_get_by_name(pkt_dev->ifname);
  1330. if (!odev) {
  1331. printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
  1332. goto out;
  1333. }
  1334. if (odev->type != ARPHRD_ETHER) {
  1335. printk("pktgen: not an ethernet device: \"%s\"\n", pkt_dev->ifname);
  1336. goto out_put;
  1337. }
  1338. if (!netif_running(odev)) {
  1339. printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
  1340. goto out_put;
  1341. }
  1342. pkt_dev->odev = odev;
  1343. return pkt_dev->odev;
  1344. out_put:
  1345. dev_put(odev);
  1346. out:
  1347. return NULL;
  1348. }
  1349. /* Read pkt_dev from the interface and set up internal pktgen_dev
  1350. * structure to have the right information to create/send packets
  1351. */
  1352. static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
  1353. {
  1354. /* Try once more, just in case it works now. */
  1355. if (!pkt_dev->odev)
  1356. pktgen_setup_dev(pkt_dev);
  1357. if (!pkt_dev->odev) {
  1358. printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
  1359. sprintf(pkt_dev->result, "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
  1360. return;
  1361. }
  1362. /* Default to the interface's mac if not explicitly set. */
  1363. if ((pkt_dev->src_mac[0] == 0) &&
  1364. (pkt_dev->src_mac[1] == 0) &&
  1365. (pkt_dev->src_mac[2] == 0) &&
  1366. (pkt_dev->src_mac[3] == 0) &&
  1367. (pkt_dev->src_mac[4] == 0) &&
  1368. (pkt_dev->src_mac[5] == 0)) {
  1369. memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, 6);
  1370. }
  1371. /* Set up Dest MAC */
  1372. memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, 6);
  1373. /* Set up pkt size */
  1374. pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
  1375. if(pkt_dev->flags & F_IPV6) {
  1376. /*
  1377. * Skip this automatic address setting until locks or functions
  1378. * gets exported
  1379. */
  1380. #ifdef NOTNOW
  1381. int i, set = 0, err=1;
  1382. struct inet6_dev *idev;
  1383. for(i=0; i< IN6_ADDR_HSIZE; i++)
  1384. if(pkt_dev->cur_in6_saddr.s6_addr[i]) {
  1385. set = 1;
  1386. break;
  1387. }
  1388. if(!set) {
  1389. /*
  1390. * Use linklevel address if unconfigured.
  1391. *
  1392. * use ipv6_get_lladdr if/when it's get exported
  1393. */
  1394. read_lock(&addrconf_lock);
  1395. if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
  1396. struct inet6_ifaddr *ifp;
  1397. read_lock_bh(&idev->lock);
  1398. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  1399. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  1400. ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &ifp->addr);
  1401. err = 0;
  1402. break;
  1403. }
  1404. }
  1405. read_unlock_bh(&idev->lock);
  1406. }
  1407. read_unlock(&addrconf_lock);
  1408. if(err) printk("pktgen: ERROR: IPv6 link address not availble.\n");
  1409. }
  1410. #endif
  1411. }
  1412. else {
  1413. pkt_dev->saddr_min = 0;
  1414. pkt_dev->saddr_max = 0;
  1415. if (strlen(pkt_dev->src_min) == 0) {
  1416. struct in_device *in_dev;
  1417. rcu_read_lock();
  1418. in_dev = __in_dev_get(pkt_dev->odev);
  1419. if (in_dev) {
  1420. if (in_dev->ifa_list) {
  1421. pkt_dev->saddr_min = in_dev->ifa_list->ifa_address;
  1422. pkt_dev->saddr_max = pkt_dev->saddr_min;
  1423. }
  1424. __in_dev_put(in_dev);
  1425. }
  1426. rcu_read_unlock();
  1427. }
  1428. else {
  1429. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1430. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1431. }
  1432. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1433. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1434. }
  1435. /* Initialize current values. */
  1436. pkt_dev->cur_dst_mac_offset = 0;
  1437. pkt_dev->cur_src_mac_offset = 0;
  1438. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1439. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1440. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1441. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1442. pkt_dev->nflows = 0;
  1443. }
  1444. static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
  1445. {
  1446. __u64 start;
  1447. __u64 now;
  1448. start = now = getCurUs();
  1449. printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
  1450. while (now < spin_until_us) {
  1451. /* TODO: optimise sleeping behavior */
  1452. if (spin_until_us - now > (1000000/HZ)+1) {
  1453. current->state = TASK_INTERRUPTIBLE;
  1454. schedule_timeout(1);
  1455. } else if (spin_until_us - now > 100) {
  1456. do_softirq();
  1457. if (!pkt_dev->running)
  1458. return;
  1459. if (need_resched())
  1460. schedule();
  1461. }
  1462. now = getCurUs();
  1463. }
  1464. pkt_dev->idle_acc += now - start;
  1465. }
  1466. /* Increment/randomize headers according to flags and current values
  1467. * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  1468. */
  1469. static void mod_cur_headers(struct pktgen_dev *pkt_dev) {
  1470. __u32 imn;
  1471. __u32 imx;
  1472. int flow = 0;
  1473. if(pkt_dev->cflows) {
  1474. flow = pktgen_random() % pkt_dev->cflows;
  1475. if (pkt_dev->flows[flow].count > pkt_dev->lflow)
  1476. pkt_dev->flows[flow].count = 0;
  1477. }
  1478. /* Deal with source MAC */
  1479. if (pkt_dev->src_mac_count > 1) {
  1480. __u32 mc;
  1481. __u32 tmp;
  1482. if (pkt_dev->flags & F_MACSRC_RND)
  1483. mc = pktgen_random() % (pkt_dev->src_mac_count);
  1484. else {
  1485. mc = pkt_dev->cur_src_mac_offset++;
  1486. if (pkt_dev->cur_src_mac_offset > pkt_dev->src_mac_count)
  1487. pkt_dev->cur_src_mac_offset = 0;
  1488. }
  1489. tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
  1490. pkt_dev->hh[11] = tmp;
  1491. tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  1492. pkt_dev->hh[10] = tmp;
  1493. tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  1494. pkt_dev->hh[9] = tmp;
  1495. tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  1496. pkt_dev->hh[8] = tmp;
  1497. tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
  1498. pkt_dev->hh[7] = tmp;
  1499. }
  1500. /* Deal with Destination MAC */
  1501. if (pkt_dev->dst_mac_count > 1) {
  1502. __u32 mc;
  1503. __u32 tmp;
  1504. if (pkt_dev->flags & F_MACDST_RND)
  1505. mc = pktgen_random() % (pkt_dev->dst_mac_count);
  1506. else {
  1507. mc = pkt_dev->cur_dst_mac_offset++;
  1508. if (pkt_dev->cur_dst_mac_offset > pkt_dev->dst_mac_count) {
  1509. pkt_dev->cur_dst_mac_offset = 0;
  1510. }
  1511. }
  1512. tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
  1513. pkt_dev->hh[5] = tmp;
  1514. tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  1515. pkt_dev->hh[4] = tmp;
  1516. tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  1517. pkt_dev->hh[3] = tmp;
  1518. tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  1519. pkt_dev->hh[2] = tmp;
  1520. tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
  1521. pkt_dev->hh[1] = tmp;
  1522. }
  1523. if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
  1524. if (pkt_dev->flags & F_UDPSRC_RND)
  1525. pkt_dev->cur_udp_src = ((pktgen_random() % (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) + pkt_dev->udp_src_min);
  1526. else {
  1527. pkt_dev->cur_udp_src++;
  1528. if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
  1529. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1530. }
  1531. }
  1532. if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
  1533. if (pkt_dev->flags & F_UDPDST_RND) {
  1534. pkt_dev->cur_udp_dst = ((pktgen_random() % (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) + pkt_dev->udp_dst_min);
  1535. }
  1536. else {
  1537. pkt_dev->cur_udp_dst++;
  1538. if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
  1539. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1540. }
  1541. }
  1542. if (!(pkt_dev->flags & F_IPV6)) {
  1543. if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = ntohl(pkt_dev->saddr_max))) {
  1544. __u32 t;
  1545. if (pkt_dev->flags & F_IPSRC_RND)
  1546. t = ((pktgen_random() % (imx - imn)) + imn);
  1547. else {
  1548. t = ntohl(pkt_dev->cur_saddr);
  1549. t++;
  1550. if (t > imx) {
  1551. t = imn;
  1552. }
  1553. }
  1554. pkt_dev->cur_saddr = htonl(t);
  1555. }
  1556. if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
  1557. pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
  1558. } else {
  1559. if ((imn = ntohl(pkt_dev->daddr_min)) < (imx = ntohl(pkt_dev->daddr_max))) {
  1560. __u32 t;
  1561. if (pkt_dev->flags & F_IPDST_RND) {
  1562. t = ((pktgen_random() % (imx - imn)) + imn);
  1563. t = htonl(t);
  1564. while( LOOPBACK(t) || MULTICAST(t) || BADCLASS(t) || ZERONET(t) || LOCAL_MCAST(t) ) {
  1565. t = ((pktgen_random() % (imx - imn)) + imn);
  1566. t = htonl(t);
  1567. }
  1568. pkt_dev->cur_daddr = t;
  1569. }
  1570. else {
  1571. t = ntohl(pkt_dev->cur_daddr);
  1572. t++;
  1573. if (t > imx) {
  1574. t = imn;
  1575. }
  1576. pkt_dev->cur_daddr = htonl(t);
  1577. }
  1578. }
  1579. if(pkt_dev->cflows) {
  1580. pkt_dev->flows[flow].cur_daddr = pkt_dev->cur_daddr;
  1581. pkt_dev->nflows++;
  1582. }
  1583. }
  1584. }
  1585. else /* IPV6 * */
  1586. {
  1587. if(pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
  1588. pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
  1589. pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
  1590. pkt_dev->min_in6_daddr.s6_addr32[3] == 0);
  1591. else {
  1592. int i;
  1593. /* Only random destinations yet */
  1594. for(i=0; i < 4; i++) {
  1595. pkt_dev->cur_in6_daddr.s6_addr32[i] =
  1596. ((pktgen_random() |
  1597. pkt_dev->min_in6_daddr.s6_addr32[i]) &
  1598. pkt_dev->max_in6_daddr.s6_addr32[i]);
  1599. }
  1600. }
  1601. }
  1602. if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
  1603. __u32 t;
  1604. if (pkt_dev->flags & F_TXSIZE_RND) {
  1605. t = ((pktgen_random() % (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
  1606. + pkt_dev->min_pkt_size);
  1607. }
  1608. else {
  1609. t = pkt_dev->cur_pkt_size + 1;
  1610. if (t > pkt_dev->max_pkt_size)
  1611. t = pkt_dev->min_pkt_size;
  1612. }
  1613. pkt_dev->cur_pkt_size = t;
  1614. }
  1615. pkt_dev->flows[flow].count++;
  1616. }
  1617. static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
  1618. struct pktgen_dev *pkt_dev)
  1619. {
  1620. struct sk_buff *skb = NULL;
  1621. __u8 *eth;
  1622. struct udphdr *udph;
  1623. int datalen, iplen;
  1624. struct iphdr *iph;
  1625. struct pktgen_hdr *pgh = NULL;
  1626. /* Update any of the values, used when we're incrementing various
  1627. * fields.
  1628. */
  1629. mod_cur_headers(pkt_dev);
  1630. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
  1631. if (!skb) {
  1632. sprintf(pkt_dev->result, "No memory");
  1633. return NULL;
  1634. }
  1635. skb_reserve(skb, 16);
  1636. /* Reserve for ethernet and IP header */
  1637. eth = (__u8 *) skb_push(skb, 14);
  1638. iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
  1639. udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
  1640. memcpy(eth, pkt_dev->hh, 12);
  1641. *(u16*)&eth[12] = __constant_htons(ETH_P_IP);
  1642. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
  1643. if (datalen < sizeof(struct pktgen_hdr))
  1644. datalen = sizeof(struct pktgen_hdr);
  1645. udph->source = htons(pkt_dev->cur_udp_src);
  1646. udph->dest = htons(pkt_dev->cur_udp_dst);
  1647. udph->len = htons(datalen + 8); /* DATA + udphdr */
  1648. udph->check = 0; /* No checksum */
  1649. iph->ihl = 5;
  1650. iph->version = 4;
  1651. iph->ttl = 32;
  1652. iph->tos = 0;
  1653. iph->protocol = IPPROTO_UDP; /* UDP */
  1654. iph->saddr = pkt_dev->cur_saddr;
  1655. iph->daddr = pkt_dev->cur_daddr;
  1656. iph->frag_off = 0;
  1657. iplen = 20 + 8 + datalen;
  1658. iph->tot_len = htons(iplen);
  1659. iph->check = 0;
  1660. iph->check = ip_fast_csum((void *) iph, iph->ihl);
  1661. skb->protocol = __constant_htons(ETH_P_IP);
  1662. skb->mac.raw = ((u8 *)iph) - 14;
  1663. skb->dev = odev;
  1664. skb->pkt_type = PACKET_HOST;
  1665. if (pkt_dev->nfrags <= 0)
  1666. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  1667. else {
  1668. int frags = pkt_dev->nfrags;
  1669. int i;
  1670. pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
  1671. if (frags > MAX_SKB_FRAGS)
  1672. frags = MAX_SKB_FRAGS;
  1673. if (datalen > frags*PAGE_SIZE) {
  1674. skb_put(skb, datalen-frags*PAGE_SIZE);
  1675. datalen = frags*PAGE_SIZE;
  1676. }
  1677. i = 0;
  1678. while (datalen > 0) {
  1679. struct page *page = alloc_pages(GFP_KERNEL, 0);
  1680. skb_shinfo(skb)->frags[i].page = page;
  1681. skb_shinfo(skb)->frags[i].page_offset = 0;
  1682. skb_shinfo(skb)->frags[i].size =
  1683. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  1684. datalen -= skb_shinfo(skb)->frags[i].size;
  1685. skb->len += skb_shinfo(skb)->frags[i].size;
  1686. skb->data_len += skb_shinfo(skb)->frags[i].size;
  1687. i++;
  1688. skb_shinfo(skb)->nr_frags = i;
  1689. }
  1690. while (i < frags) {
  1691. int rem;
  1692. if (i == 0)
  1693. break;
  1694. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  1695. if (rem == 0)
  1696. break;
  1697. skb_shinfo(skb)->frags[i - 1].size -= rem;
  1698. skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
  1699. get_page(skb_shinfo(skb)->frags[i].page);
  1700. skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
  1701. skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
  1702. skb_shinfo(skb)->frags[i].size = rem;
  1703. i++;
  1704. skb_shinfo(skb)->nr_frags = i;
  1705. }
  1706. }
  1707. /* Stamp the time, and sequence number, convert them to network byte order */
  1708. if (pgh) {
  1709. struct timeval timestamp;
  1710. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  1711. pgh->seq_num = htonl(pkt_dev->seq_num);
  1712. do_gettimeofday(&timestamp);
  1713. pgh->tv_sec = htonl(timestamp.tv_sec);
  1714. pgh->tv_usec = htonl(timestamp.tv_usec);
  1715. }
  1716. pkt_dev->seq_num++;
  1717. return skb;
  1718. }
  1719. /*
  1720. * scan_ip6, fmt_ip taken from dietlibc-0.21
  1721. * Author Felix von Leitner <felix-dietlibc@fefe.de>
  1722. *
  1723. * Slightly modified for kernel.
  1724. * Should be candidate for net/ipv4/utils.c
  1725. * --ro
  1726. */
  1727. static unsigned int scan_ip6(const char *s,char ip[16])
  1728. {
  1729. unsigned int i;
  1730. unsigned int len=0;
  1731. unsigned long u;
  1732. char suffix[16];
  1733. unsigned int prefixlen=0;
  1734. unsigned int suffixlen=0;
  1735. __u32 tmp;
  1736. for (i=0; i<16; i++) ip[i]=0;
  1737. for (;;) {
  1738. if (*s == ':') {
  1739. len++;
  1740. if (s[1] == ':') { /* Found "::", skip to part 2 */
  1741. s+=2;
  1742. len++;
  1743. break;
  1744. }
  1745. s++;
  1746. }
  1747. {
  1748. char *tmp;
  1749. u=simple_strtoul(s,&tmp,16);
  1750. i=tmp-s;
  1751. }
  1752. if (!i) return 0;
  1753. if (prefixlen==12 && s[i]=='.') {
  1754. /* the last 4 bytes may be written as IPv4 address */
  1755. tmp = in_aton(s);
  1756. memcpy((struct in_addr*)(ip+12), &tmp, sizeof(tmp));
  1757. return i+len;
  1758. }
  1759. ip[prefixlen++] = (u >> 8);
  1760. ip[prefixlen++] = (u & 255);
  1761. s += i; len += i;
  1762. if (prefixlen==16)
  1763. return len;
  1764. }
  1765. /* part 2, after "::" */
  1766. for (;;) {
  1767. if (*s == ':') {
  1768. if (suffixlen==0)
  1769. break;
  1770. s++;
  1771. len++;
  1772. } else if (suffixlen!=0)
  1773. break;
  1774. {
  1775. char *tmp;
  1776. u=simple_strtol(s,&tmp,16);
  1777. i=tmp-s;
  1778. }
  1779. if (!i) {
  1780. if (*s) len--;
  1781. break;
  1782. }
  1783. if (suffixlen+prefixlen<=12 && s[i]=='.') {
  1784. tmp = in_aton(s);
  1785. memcpy((struct in_addr*)(suffix+suffixlen), &tmp, sizeof(tmp));
  1786. suffixlen+=4;
  1787. len+=strlen(s);
  1788. break;
  1789. }
  1790. suffix[suffixlen++] = (u >> 8);
  1791. suffix[suffixlen++] = (u & 255);
  1792. s += i; len += i;
  1793. if (prefixlen+suffixlen==16)
  1794. break;
  1795. }
  1796. for (i=0; i<suffixlen; i++)
  1797. ip[16-suffixlen+i] = suffix[i];
  1798. return len;
  1799. }
  1800. static char tohex(char hexdigit) {
  1801. return hexdigit>9?hexdigit+'a'-10:hexdigit+'0';
  1802. }
  1803. static int fmt_xlong(char* s,unsigned int i) {
  1804. char* bak=s;
  1805. *s=tohex((i>>12)&0xf); if (s!=bak || *s!='0') ++s;
  1806. *s=tohex((i>>8)&0xf); if (s!=bak || *s!='0') ++s;
  1807. *s=tohex((i>>4)&0xf); if (s!=bak || *s!='0') ++s;
  1808. *s=tohex(i&0xf);
  1809. return s-bak+1;
  1810. }
  1811. static unsigned int fmt_ip6(char *s,const char ip[16]) {
  1812. unsigned int len;
  1813. unsigned int i;
  1814. unsigned int temp;
  1815. unsigned int compressing;
  1816. int j;
  1817. len = 0; compressing = 0;
  1818. for (j=0; j<16; j+=2) {
  1819. #ifdef V4MAPPEDPREFIX
  1820. if (j==12 && !memcmp(ip,V4mappedprefix,12)) {
  1821. inet_ntoa_r(*(struct in_addr*)(ip+12),s);
  1822. temp=strlen(s);
  1823. return len+temp;
  1824. }
  1825. #endif
  1826. temp = ((unsigned long) (unsigned char) ip[j] << 8) +
  1827. (unsigned long) (unsigned char) ip[j+1];
  1828. if (temp == 0) {
  1829. if (!compressing) {
  1830. compressing=1;
  1831. if (j==0) {
  1832. *s++=':'; ++len;
  1833. }
  1834. }
  1835. } else {
  1836. if (compressing) {
  1837. compressing=0;
  1838. *s++=':'; ++len;
  1839. }
  1840. i = fmt_xlong(s,temp); len += i; s += i;
  1841. if (j<14) {
  1842. *s++ = ':';
  1843. ++len;
  1844. }
  1845. }
  1846. }
  1847. if (compressing) {
  1848. *s++=':'; ++len;
  1849. }
  1850. *s=0;
  1851. return len;
  1852. }
  1853. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  1854. struct pktgen_dev *pkt_dev)
  1855. {
  1856. struct sk_buff *skb = NULL;
  1857. __u8 *eth;
  1858. struct udphdr *udph;
  1859. int datalen;
  1860. struct ipv6hdr *iph;
  1861. struct pktgen_hdr *pgh = NULL;
  1862. /* Update any of the values, used when we're incrementing various
  1863. * fields.
  1864. */
  1865. mod_cur_headers(pkt_dev);
  1866. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
  1867. if (!skb) {
  1868. sprintf(pkt_dev->result, "No memory");
  1869. return NULL;
  1870. }
  1871. skb_reserve(skb, 16);
  1872. /* Reserve for ethernet and IP header */
  1873. eth = (__u8 *) skb_push(skb, 14);
  1874. iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
  1875. udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
  1876. memcpy(eth, pkt_dev->hh, 12);
  1877. *(u16*)&eth[12] = __constant_htons(ETH_P_IPV6);
  1878. datalen = pkt_dev->cur_pkt_size-14-
  1879. sizeof(struct ipv6hdr)-sizeof(struct udphdr); /* Eth + IPh + UDPh */
  1880. if (datalen < sizeof(struct pktgen_hdr)) {
  1881. datalen = sizeof(struct pktgen_hdr);
  1882. if (net_ratelimit())
  1883. printk(KERN_INFO "pktgen: increased datalen to %d\n", datalen);
  1884. }
  1885. udph->source = htons(pkt_dev->cur_udp_src);
  1886. udph->dest = htons(pkt_dev->cur_udp_dst);
  1887. udph->len = htons(datalen + sizeof(struct udphdr));
  1888. udph->check = 0; /* No checksum */
  1889. *(u32*)iph = __constant_htonl(0x60000000); /* Version + flow */
  1890. iph->hop_limit = 32;
  1891. iph->payload_len = htons(sizeof(struct udphdr) + datalen);
  1892. iph->nexthdr = IPPROTO_UDP;
  1893. ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
  1894. ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
  1895. skb->mac.raw = ((u8 *)iph) - 14;
  1896. skb->protocol = __constant_htons(ETH_P_IPV6);
  1897. skb->dev = odev;
  1898. skb->pkt_type = PACKET_HOST;
  1899. if (pkt_dev->nfrags <= 0)
  1900. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  1901. else {
  1902. int frags = pkt_dev->nfrags;
  1903. int i;
  1904. pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
  1905. if (frags > MAX_SKB_FRAGS)
  1906. frags = MAX_SKB_FRAGS;
  1907. if (datalen > frags*PAGE_SIZE) {
  1908. skb_put(skb, datalen-frags*PAGE_SIZE);
  1909. datalen = frags*PAGE_SIZE;
  1910. }
  1911. i = 0;
  1912. while (datalen > 0) {
  1913. struct page *page = alloc_pages(GFP_KERNEL, 0);
  1914. skb_shinfo(skb)->frags[i].page = page;
  1915. skb_shinfo(skb)->frags[i].page_offset = 0;
  1916. skb_shinfo(skb)->frags[i].size =
  1917. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  1918. datalen -= skb_shinfo(skb)->frags[i].size;
  1919. skb->len += skb_shinfo(skb)->frags[i].size;
  1920. skb->data_len += skb_shinfo(skb)->frags[i].size;
  1921. i++;
  1922. skb_shinfo(skb)->nr_frags = i;
  1923. }
  1924. while (i < frags) {
  1925. int rem;
  1926. if (i == 0)
  1927. break;
  1928. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  1929. if (rem == 0)
  1930. break;
  1931. skb_shinfo(skb)->frags[i - 1].size -= rem;
  1932. skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
  1933. get_page(skb_shinfo(skb)->frags[i].page);
  1934. skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
  1935. skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
  1936. skb_shinfo(skb)->frags[i].size = rem;
  1937. i++;
  1938. skb_shinfo(skb)->nr_frags = i;
  1939. }
  1940. }
  1941. /* Stamp the time, and sequence number, convert them to network byte order */
  1942. /* should we update cloned packets too ? */
  1943. if (pgh) {
  1944. struct timeval timestamp;
  1945. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  1946. pgh->seq_num = htonl(pkt_dev->seq_num);
  1947. do_gettimeofday(&timestamp);
  1948. pgh->tv_sec = htonl(timestamp.tv_sec);
  1949. pgh->tv_usec = htonl(timestamp.tv_usec);
  1950. }
  1951. pkt_dev->seq_num++;
  1952. return skb;
  1953. }
  1954. static inline struct sk_buff *fill_packet(struct net_device *odev,
  1955. struct pktgen_dev *pkt_dev)
  1956. {
  1957. if(pkt_dev->flags & F_IPV6)
  1958. return fill_packet_ipv6(odev, pkt_dev);
  1959. else
  1960. return fill_packet_ipv4(odev, pkt_dev);
  1961. }
  1962. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  1963. {
  1964. pkt_dev->seq_num = 1;
  1965. pkt_dev->idle_acc = 0;
  1966. pkt_dev->sofar = 0;
  1967. pkt_dev->tx_bytes = 0;
  1968. pkt_dev->errors = 0;
  1969. }
  1970. /* Set up structure for sending pkts, clear counters */
  1971. static void pktgen_run(struct pktgen_thread *t)
  1972. {
  1973. struct pktgen_dev *pkt_dev = NULL;
  1974. int started = 0;
  1975. PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
  1976. if_lock(t);
  1977. for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
  1978. /*
  1979. * setup odev and create initial packet.
  1980. */
  1981. pktgen_setup_inject(pkt_dev);
  1982. if(pkt_dev->odev) {
  1983. pktgen_clear_counters(pkt_dev);
  1984. pkt_dev->running = 1; /* Cranke yeself! */
  1985. pkt_dev->skb = NULL;
  1986. pkt_dev->started_at = getCurUs();
  1987. pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
  1988. pkt_dev->next_tx_ns = 0;
  1989. strcpy(pkt_dev->result, "Starting");
  1990. started++;
  1991. }
  1992. else
  1993. strcpy(pkt_dev->result, "Error starting");
  1994. }
  1995. if_unlock(t);
  1996. if(started) t->control &= ~(T_STOP);
  1997. }
  1998. static void pktgen_stop_all_threads_ifs(void)
  1999. {
  2000. struct pktgen_thread *t = pktgen_threads;
  2001. PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads.\n"));
  2002. thread_lock();
  2003. while(t) {
  2004. pktgen_stop(t);
  2005. t = t->next;
  2006. }
  2007. thread_unlock();
  2008. }
  2009. static int thread_is_running(struct pktgen_thread *t )
  2010. {
  2011. struct pktgen_dev *next;
  2012. int res = 0;
  2013. for(next=t->if_list; next; next=next->next) {
  2014. if(next->running) {
  2015. res = 1;
  2016. break;
  2017. }
  2018. }
  2019. return res;
  2020. }
  2021. static int pktgen_wait_thread_run(struct pktgen_thread *t )
  2022. {
  2023. if_lock(t);
  2024. while(thread_is_running(t)) {
  2025. if_unlock(t);
  2026. msleep_interruptible(100);
  2027. if (signal_pending(current))
  2028. goto signal;
  2029. if_lock(t);
  2030. }
  2031. if_unlock(t);
  2032. return 1;
  2033. signal:
  2034. return 0;
  2035. }
  2036. static int pktgen_wait_all_threads_run(void)
  2037. {
  2038. struct pktgen_thread *t = pktgen_threads;
  2039. int sig = 1;
  2040. while (t) {
  2041. sig = pktgen_wait_thread_run(t);
  2042. if( sig == 0 ) break;
  2043. thread_lock();
  2044. t=t->next;
  2045. thread_unlock();
  2046. }
  2047. if(sig == 0) {
  2048. thread_lock();
  2049. while (t) {
  2050. t->control |= (T_STOP);
  2051. t=t->next;
  2052. }
  2053. thread_unlock();
  2054. }
  2055. return sig;
  2056. }
  2057. static void pktgen_run_all_threads(void)
  2058. {
  2059. struct pktgen_thread *t = pktgen_threads;
  2060. PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
  2061. thread_lock();
  2062. while(t) {
  2063. t->control |= (T_RUN);
  2064. t = t->next;
  2065. }
  2066. thread_unlock();
  2067. current->state = TASK_INTERRUPTIBLE;
  2068. schedule_timeout(HZ/8); /* Propagate thread->control */
  2069. pktgen_wait_all_threads_run();
  2070. }
  2071. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2072. {
  2073. __u64 total_us, bps, mbps, pps, idle;
  2074. char *p = pkt_dev->result;
  2075. total_us = pkt_dev->stopped_at - pkt_dev->started_at;
  2076. idle = pkt_dev->idle_acc;
  2077. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2078. (unsigned long long) total_us,
  2079. (unsigned long long)(total_us - idle),
  2080. (unsigned long long) idle,
  2081. (unsigned long long) pkt_dev->sofar,
  2082. pkt_dev->cur_pkt_size, nr_frags);
  2083. pps = pkt_dev->sofar * USEC_PER_SEC;
  2084. while ((total_us >> 32) != 0) {
  2085. pps >>= 1;
  2086. total_us >>= 1;
  2087. }
  2088. do_div(pps, total_us);
  2089. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2090. mbps = bps;
  2091. do_div(mbps, 1000000);
  2092. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2093. (unsigned long long) pps,
  2094. (unsigned long long) mbps,
  2095. (unsigned long long) bps,
  2096. (unsigned long long) pkt_dev->errors);
  2097. }
  2098. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2099. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2100. {
  2101. if (!pkt_dev->running) {
  2102. printk("pktgen: interface: %s is already stopped\n", pkt_dev->ifname);
  2103. return -EINVAL;
  2104. }
  2105. pkt_dev->stopped_at = getCurUs();
  2106. pkt_dev->running = 0;
  2107. show_results(pkt_dev, skb_shinfo(pkt_dev->skb)->nr_frags);
  2108. if (pkt_dev->skb)
  2109. kfree_skb(pkt_dev->skb);
  2110. pkt_dev->skb = NULL;
  2111. return 0;
  2112. }
  2113. static struct pktgen_dev *next_to_run(struct pktgen_thread *t )
  2114. {
  2115. struct pktgen_dev *next, *best = NULL;
  2116. if_lock(t);
  2117. for(next=t->if_list; next ; next=next->next) {
  2118. if(!next->running) continue;
  2119. if(best == NULL) best=next;
  2120. else if ( next->next_tx_us < best->next_tx_us)
  2121. best = next;
  2122. }
  2123. if_unlock(t);
  2124. return best;
  2125. }
  2126. static void pktgen_stop(struct pktgen_thread *t) {
  2127. struct pktgen_dev *next = NULL;
  2128. PG_DEBUG(printk("pktgen: entering pktgen_stop.\n"));
  2129. if_lock(t);
  2130. for(next=t->if_list; next; next=next->next)
  2131. pktgen_stop_device(next);
  2132. if_unlock(t);
  2133. }
  2134. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2135. {
  2136. struct pktgen_dev *cur, *next = NULL;
  2137. /* Remove all devices, free mem */
  2138. if_lock(t);
  2139. for(cur=t->if_list; cur; cur=next) {
  2140. next = cur->next;
  2141. pktgen_remove_device(t, cur);
  2142. }
  2143. if_unlock(t);
  2144. }
  2145. static void pktgen_rem_thread(struct pktgen_thread *t)
  2146. {
  2147. /* Remove from the thread list */
  2148. struct pktgen_thread *tmp = pktgen_threads;
  2149. if (strlen(t->fname))
  2150. remove_proc_entry(t->fname, NULL);
  2151. thread_lock();
  2152. if (tmp == t)
  2153. pktgen_threads = tmp->next;
  2154. else {
  2155. while (tmp) {
  2156. if (tmp->next == t) {
  2157. tmp->next = t->next;
  2158. t->next = NULL;
  2159. break;
  2160. }
  2161. tmp = tmp->next;
  2162. }
  2163. }
  2164. thread_unlock();
  2165. }
  2166. static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2167. {
  2168. struct net_device *odev = NULL;
  2169. __u64 idle_start = 0;
  2170. int ret;
  2171. odev = pkt_dev->odev;
  2172. if (pkt_dev->delay_us || pkt_dev->delay_ns) {
  2173. u64 now;
  2174. now = getCurUs();
  2175. if (now < pkt_dev->next_tx_us)
  2176. spin(pkt_dev, pkt_dev->next_tx_us);
  2177. /* This is max DELAY, this has special meaning of
  2178. * "never transmit"
  2179. */
  2180. if (pkt_dev->delay_us == 0x7FFFFFFF) {
  2181. pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
  2182. pkt_dev->next_tx_ns = pkt_dev->delay_ns;
  2183. goto out;
  2184. }
  2185. }
  2186. if (netif_queue_stopped(odev) || need_resched()) {
  2187. idle_start = getCurUs();
  2188. if (!netif_running(odev)) {
  2189. pktgen_stop_device(pkt_dev);
  2190. goto out;
  2191. }
  2192. if (need_resched())
  2193. schedule();
  2194. pkt_dev->idle_acc += getCurUs() - idle_start;
  2195. if (netif_queue_stopped(odev)) {
  2196. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2197. pkt_dev->next_tx_ns = 0;
  2198. goto out; /* Try the next interface */
  2199. }
  2200. }
  2201. if (pkt_dev->last_ok || !pkt_dev->skb) {
  2202. if ((++pkt_dev->clone_count >= pkt_dev->clone_skb ) || (!pkt_dev->skb)) {
  2203. /* build a new pkt */
  2204. if (pkt_dev->skb)
  2205. kfree_skb(pkt_dev->skb);
  2206. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2207. if (pkt_dev->skb == NULL) {
  2208. printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
  2209. schedule();
  2210. pkt_dev->clone_count--; /* back out increment, OOM */
  2211. goto out;
  2212. }
  2213. pkt_dev->allocated_skbs++;
  2214. pkt_dev->clone_count = 0; /* reset counter */
  2215. }
  2216. }
  2217. spin_lock_bh(&odev->xmit_lock);
  2218. if (!netif_queue_stopped(odev)) {
  2219. atomic_inc(&(pkt_dev->skb->users));
  2220. retry_now:
  2221. ret = odev->hard_start_xmit(pkt_dev->skb, odev);
  2222. if (likely(ret == NETDEV_TX_OK)) {
  2223. pkt_dev->last_ok = 1;
  2224. pkt_dev->sofar++;
  2225. pkt_dev->seq_num++;
  2226. pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
  2227. } else if (ret == NETDEV_TX_LOCKED
  2228. && (odev->features & NETIF_F_LLTX)) {
  2229. cpu_relax();
  2230. goto retry_now;
  2231. } else { /* Retry it next time */
  2232. atomic_dec(&(pkt_dev->skb->users));
  2233. if (debug && net_ratelimit())
  2234. printk(KERN_INFO "pktgen: Hard xmit error\n");
  2235. pkt_dev->errors++;
  2236. pkt_dev->last_ok = 0;
  2237. }
  2238. pkt_dev->next_tx_us = getCurUs();
  2239. pkt_dev->next_tx_ns = 0;
  2240. pkt_dev->next_tx_us += pkt_dev->delay_us;
  2241. pkt_dev->next_tx_ns += pkt_dev->delay_ns;
  2242. if (pkt_dev->next_tx_ns > 1000) {
  2243. pkt_dev->next_tx_us++;
  2244. pkt_dev->next_tx_ns -= 1000;
  2245. }
  2246. }
  2247. else { /* Retry it next time */
  2248. pkt_dev->last_ok = 0;
  2249. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2250. pkt_dev->next_tx_ns = 0;
  2251. }
  2252. spin_unlock_bh(&odev->xmit_lock);
  2253. /* If pkt_dev->count is zero, then run forever */
  2254. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  2255. if (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2256. idle_start = getCurUs();
  2257. while (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2258. if (signal_pending(current)) {
  2259. break;
  2260. }
  2261. schedule();
  2262. }
  2263. pkt_dev->idle_acc += getCurUs() - idle_start;
  2264. }
  2265. /* Done with this */
  2266. pktgen_stop_device(pkt_dev);
  2267. }
  2268. out:;
  2269. }
  2270. /*
  2271. * Main loop of the thread goes here
  2272. */
  2273. static void pktgen_thread_worker(struct pktgen_thread *t)
  2274. {
  2275. DEFINE_WAIT(wait);
  2276. struct pktgen_dev *pkt_dev = NULL;
  2277. int cpu = t->cpu;
  2278. sigset_t tmpsig;
  2279. u32 max_before_softirq;
  2280. u32 tx_since_softirq = 0;
  2281. daemonize("pktgen/%d", cpu);
  2282. /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
  2283. spin_lock_irq(&current->sighand->siglock);
  2284. tmpsig = current->blocked;
  2285. siginitsetinv(&current->blocked,
  2286. sigmask(SIGKILL) |
  2287. sigmask(SIGSTOP)|
  2288. sigmask(SIGTERM));
  2289. recalc_sigpending();
  2290. spin_unlock_irq(&current->sighand->siglock);
  2291. /* Migrate to the right CPU */
  2292. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  2293. if (smp_processor_id() != cpu)
  2294. BUG();
  2295. init_waitqueue_head(&t->queue);
  2296. t->control &= ~(T_TERMINATE);
  2297. t->control &= ~(T_RUN);
  2298. t->control &= ~(T_STOP);
  2299. t->control &= ~(T_REMDEV);
  2300. t->pid = current->pid;
  2301. PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
  2302. max_before_softirq = t->max_before_softirq;
  2303. __set_current_state(TASK_INTERRUPTIBLE);
  2304. mb();
  2305. while (1) {
  2306. __set_current_state(TASK_RUNNING);
  2307. /*
  2308. * Get next dev to xmit -- if any.
  2309. */
  2310. pkt_dev = next_to_run(t);
  2311. if (pkt_dev) {
  2312. pktgen_xmit(pkt_dev);
  2313. /*
  2314. * We like to stay RUNNING but must also give
  2315. * others fair share.
  2316. */
  2317. tx_since_softirq += pkt_dev->last_ok;
  2318. if (tx_since_softirq > max_before_softirq) {
  2319. if (local_softirq_pending())
  2320. do_softirq();
  2321. tx_since_softirq = 0;
  2322. }
  2323. } else {
  2324. prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
  2325. schedule_timeout(HZ/10);
  2326. finish_wait(&(t->queue), &wait);
  2327. }
  2328. /*
  2329. * Back from sleep, either due to the timeout or signal.
  2330. * We check if we have any "posted" work for us.
  2331. */
  2332. if (t->control & T_TERMINATE || signal_pending(current))
  2333. /* we received a request to terminate ourself */
  2334. break;
  2335. if(t->control & T_STOP) {
  2336. pktgen_stop(t);
  2337. t->control &= ~(T_STOP);
  2338. }
  2339. if(t->control & T_RUN) {
  2340. pktgen_run(t);
  2341. t->control &= ~(T_RUN);
  2342. }
  2343. if(t->control & T_REMDEV) {
  2344. pktgen_rem_all_ifs(t);
  2345. t->control &= ~(T_REMDEV);
  2346. }
  2347. if (need_resched())
  2348. schedule();
  2349. }
  2350. PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
  2351. pktgen_stop(t);
  2352. PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
  2353. pktgen_rem_all_ifs(t);
  2354. PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
  2355. pktgen_rem_thread(t);
  2356. }
  2357. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, const char* ifname)
  2358. {
  2359. struct pktgen_dev *pkt_dev = NULL;
  2360. if_lock(t);
  2361. for(pkt_dev=t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
  2362. if (strcmp(pkt_dev->ifname, ifname) == 0) {
  2363. break;
  2364. }
  2365. }
  2366. if_unlock(t);
  2367. PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname,pkt_dev));
  2368. return pkt_dev;
  2369. }
  2370. /*
  2371. * Adds a dev at front of if_list.
  2372. */
  2373. static int add_dev_to_thread(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
  2374. {
  2375. int rv = 0;
  2376. if_lock(t);
  2377. if (pkt_dev->pg_thread) {
  2378. printk("pktgen: ERROR: already assigned to a thread.\n");
  2379. rv = -EBUSY;
  2380. goto out;
  2381. }
  2382. pkt_dev->next =t->if_list; t->if_list=pkt_dev;
  2383. pkt_dev->pg_thread = t;
  2384. pkt_dev->running = 0;
  2385. out:
  2386. if_unlock(t);
  2387. return rv;
  2388. }
  2389. /* Called under thread lock */
  2390. static int pktgen_add_device(struct pktgen_thread *t, const char* ifname)
  2391. {
  2392. struct pktgen_dev *pkt_dev;
  2393. /* We don't allow a device to be on several threads */
  2394. if( (pkt_dev = __pktgen_NN_threads(ifname, FIND)) == NULL) {
  2395. pkt_dev = kmalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
  2396. if (!pkt_dev)
  2397. return -ENOMEM;
  2398. memset(pkt_dev, 0, sizeof(struct pktgen_dev));
  2399. pkt_dev->flows = vmalloc(MAX_CFLOWS*sizeof(struct flow_state));
  2400. if (pkt_dev->flows == NULL) {
  2401. kfree(pkt_dev);
  2402. return -ENOMEM;
  2403. }
  2404. memset(pkt_dev->flows, 0, MAX_CFLOWS*sizeof(struct flow_state));
  2405. pkt_dev->min_pkt_size = ETH_ZLEN;
  2406. pkt_dev->max_pkt_size = ETH_ZLEN;
  2407. pkt_dev->nfrags = 0;
  2408. pkt_dev->clone_skb = pg_clone_skb_d;
  2409. pkt_dev->delay_us = pg_delay_d / 1000;
  2410. pkt_dev->delay_ns = pg_delay_d % 1000;
  2411. pkt_dev->count = pg_count_d;
  2412. pkt_dev->sofar = 0;
  2413. pkt_dev->udp_src_min = 9; /* sink port */
  2414. pkt_dev->udp_src_max = 9;
  2415. pkt_dev->udp_dst_min = 9;
  2416. pkt_dev->udp_dst_max = 9;
  2417. strncpy(pkt_dev->ifname, ifname, 31);
  2418. sprintf(pkt_dev->fname, "net/%s/%s", PG_PROC_DIR, ifname);
  2419. if (! pktgen_setup_dev(pkt_dev)) {
  2420. printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
  2421. if (pkt_dev->flows)
  2422. vfree(pkt_dev->flows);
  2423. kfree(pkt_dev);
  2424. return -ENODEV;
  2425. }
  2426. pkt_dev->proc_ent = create_proc_entry(pkt_dev->fname, 0600, NULL);
  2427. if (!pkt_dev->proc_ent) {
  2428. printk("pktgen: cannot create %s procfs entry.\n", pkt_dev->fname);
  2429. if (pkt_dev->flows)
  2430. vfree(pkt_dev->flows);
  2431. kfree(pkt_dev);
  2432. return -EINVAL;
  2433. }
  2434. pkt_dev->proc_ent->read_proc = proc_if_read;
  2435. pkt_dev->proc_ent->write_proc = proc_if_write;
  2436. pkt_dev->proc_ent->data = (void*)(pkt_dev);
  2437. pkt_dev->proc_ent->owner = THIS_MODULE;
  2438. return add_dev_to_thread(t, pkt_dev);
  2439. }
  2440. else {
  2441. printk("pktgen: ERROR: interface already used.\n");
  2442. return -EBUSY;
  2443. }
  2444. }
  2445. static struct pktgen_thread *pktgen_find_thread(const char* name)
  2446. {
  2447. struct pktgen_thread *t = NULL;
  2448. thread_lock();
  2449. t = pktgen_threads;
  2450. while (t) {
  2451. if (strcmp(t->name, name) == 0)
  2452. break;
  2453. t = t->next;
  2454. }
  2455. thread_unlock();
  2456. return t;
  2457. }
  2458. static int pktgen_create_thread(const char* name, int cpu)
  2459. {
  2460. struct pktgen_thread *t = NULL;
  2461. if (strlen(name) > 31) {
  2462. printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
  2463. return -EINVAL;
  2464. }
  2465. if (pktgen_find_thread(name)) {
  2466. printk("pktgen: ERROR: thread: %s already exists\n", name);
  2467. return -EINVAL;
  2468. }
  2469. t = (struct pktgen_thread*)(kmalloc(sizeof(struct pktgen_thread), GFP_KERNEL));
  2470. if (!t) {
  2471. printk("pktgen: ERROR: out of memory, can't create new thread.\n");
  2472. return -ENOMEM;
  2473. }
  2474. memset(t, 0, sizeof(struct pktgen_thread));
  2475. strcpy(t->name, name);
  2476. spin_lock_init(&t->if_lock);
  2477. t->cpu = cpu;
  2478. sprintf(t->fname, "net/%s/%s", PG_PROC_DIR, t->name);
  2479. t->proc_ent = create_proc_entry(t->fname, 0600, NULL);
  2480. if (!t->proc_ent) {
  2481. printk("pktgen: cannot create %s procfs entry.\n", t->fname);
  2482. kfree(t);
  2483. return -EINVAL;
  2484. }
  2485. t->proc_ent->read_proc = proc_thread_read;
  2486. t->proc_ent->write_proc = proc_thread_write;
  2487. t->proc_ent->data = (void*)(t);
  2488. t->proc_ent->owner = THIS_MODULE;
  2489. t->next = pktgen_threads;
  2490. pktgen_threads = t;
  2491. if (kernel_thread((void *) pktgen_thread_worker, (void *) t,
  2492. CLONE_FS | CLONE_FILES | CLONE_SIGHAND) < 0)
  2493. printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
  2494. return 0;
  2495. }
  2496. /*
  2497. * Removes a device from the thread if_list.
  2498. */
  2499. static void _rem_dev_from_if_list(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
  2500. {
  2501. struct pktgen_dev *i, *prev = NULL;
  2502. i = t->if_list;
  2503. while(i) {
  2504. if(i == pkt_dev) {
  2505. if(prev) prev->next = i->next;
  2506. else t->if_list = NULL;
  2507. break;
  2508. }
  2509. prev = i;
  2510. i=i->next;
  2511. }
  2512. }
  2513. static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
  2514. {
  2515. PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
  2516. if (pkt_dev->running) {
  2517. printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
  2518. pktgen_stop_device(pkt_dev);
  2519. }
  2520. /* Dis-associate from the interface */
  2521. if (pkt_dev->odev) {
  2522. dev_put(pkt_dev->odev);
  2523. pkt_dev->odev = NULL;
  2524. }
  2525. /* And update the thread if_list */
  2526. _rem_dev_from_if_list(t, pkt_dev);
  2527. /* Clean up proc file system */
  2528. if (strlen(pkt_dev->fname))
  2529. remove_proc_entry(pkt_dev->fname, NULL);
  2530. if (pkt_dev->flows)
  2531. vfree(pkt_dev->flows);
  2532. kfree(pkt_dev);
  2533. return 0;
  2534. }
  2535. static int __init pg_init(void)
  2536. {
  2537. int cpu;
  2538. printk(version);
  2539. module_fname[0] = 0;
  2540. create_proc_dir();
  2541. sprintf(module_fname, "net/%s/pgctrl", PG_PROC_DIR);
  2542. module_proc_ent = create_proc_entry(module_fname, 0600, NULL);
  2543. if (!module_proc_ent) {
  2544. printk("pktgen: ERROR: cannot create %s procfs entry.\n", module_fname);
  2545. return -EINVAL;
  2546. }
  2547. module_proc_ent->proc_fops = &pktgen_fops;
  2548. module_proc_ent->data = NULL;
  2549. /* Register us to receive netdevice events */
  2550. register_netdevice_notifier(&pktgen_notifier_block);
  2551. for (cpu = 0; cpu < NR_CPUS ; cpu++) {
  2552. char buf[30];
  2553. if (!cpu_online(cpu))
  2554. continue;
  2555. sprintf(buf, "kpktgend_%i", cpu);
  2556. pktgen_create_thread(buf, cpu);
  2557. }
  2558. return 0;
  2559. }
  2560. static void __exit pg_cleanup(void)
  2561. {
  2562. wait_queue_head_t queue;
  2563. init_waitqueue_head(&queue);
  2564. /* Stop all interfaces & threads */
  2565. while (pktgen_threads) {
  2566. struct pktgen_thread *t = pktgen_threads;
  2567. pktgen_threads->control |= (T_TERMINATE);
  2568. wait_event_interruptible_timeout(queue, (t != pktgen_threads), HZ);
  2569. }
  2570. /* Un-register us from receiving netdevice events */
  2571. unregister_netdevice_notifier(&pktgen_notifier_block);
  2572. /* Clean up proc file system */
  2573. remove_proc_entry(module_fname, NULL);
  2574. remove_proc_dir();
  2575. }
  2576. module_init(pg_init);
  2577. module_exit(pg_cleanup);
  2578. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
  2579. MODULE_DESCRIPTION("Packet Generator tool");
  2580. MODULE_LICENSE("GPL");
  2581. module_param(pg_count_d, int, 0);
  2582. module_param(pg_delay_d, int, 0);
  2583. module_param(pg_clone_skb_d, int, 0);
  2584. module_param(debug, int, 0);