pktgen.c 82 KB

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