pktgen.c 82 KB

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