pktgen.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  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. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
  1582. if (!skb) {
  1583. sprintf(pkt_dev->result, "No memory");
  1584. return NULL;
  1585. }
  1586. skb_reserve(skb, 16);
  1587. /* Reserve for ethernet and IP header */
  1588. eth = (__u8 *) skb_push(skb, 14);
  1589. iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
  1590. udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
  1591. memcpy(eth, pkt_dev->hh, 12);
  1592. *(u16*)&eth[12] = __constant_htons(ETH_P_IP);
  1593. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
  1594. if (datalen < sizeof(struct pktgen_hdr))
  1595. datalen = sizeof(struct pktgen_hdr);
  1596. udph->source = htons(pkt_dev->cur_udp_src);
  1597. udph->dest = htons(pkt_dev->cur_udp_dst);
  1598. udph->len = htons(datalen + 8); /* DATA + udphdr */
  1599. udph->check = 0; /* No checksum */
  1600. iph->ihl = 5;
  1601. iph->version = 4;
  1602. iph->ttl = 32;
  1603. iph->tos = 0;
  1604. iph->protocol = IPPROTO_UDP; /* UDP */
  1605. iph->saddr = pkt_dev->cur_saddr;
  1606. iph->daddr = pkt_dev->cur_daddr;
  1607. iph->frag_off = 0;
  1608. iplen = 20 + 8 + datalen;
  1609. iph->tot_len = htons(iplen);
  1610. iph->check = 0;
  1611. iph->check = ip_fast_csum((void *) iph, iph->ihl);
  1612. skb->protocol = __constant_htons(ETH_P_IP);
  1613. skb->mac.raw = ((u8 *)iph) - 14;
  1614. skb->dev = odev;
  1615. skb->pkt_type = PACKET_HOST;
  1616. if (pkt_dev->nfrags <= 0)
  1617. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  1618. else {
  1619. int frags = pkt_dev->nfrags;
  1620. int i;
  1621. pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
  1622. if (frags > MAX_SKB_FRAGS)
  1623. frags = MAX_SKB_FRAGS;
  1624. if (datalen > frags*PAGE_SIZE) {
  1625. skb_put(skb, datalen-frags*PAGE_SIZE);
  1626. datalen = frags*PAGE_SIZE;
  1627. }
  1628. i = 0;
  1629. while (datalen > 0) {
  1630. struct page *page = alloc_pages(GFP_KERNEL, 0);
  1631. skb_shinfo(skb)->frags[i].page = page;
  1632. skb_shinfo(skb)->frags[i].page_offset = 0;
  1633. skb_shinfo(skb)->frags[i].size =
  1634. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  1635. datalen -= skb_shinfo(skb)->frags[i].size;
  1636. skb->len += skb_shinfo(skb)->frags[i].size;
  1637. skb->data_len += skb_shinfo(skb)->frags[i].size;
  1638. i++;
  1639. skb_shinfo(skb)->nr_frags = i;
  1640. }
  1641. while (i < frags) {
  1642. int rem;
  1643. if (i == 0)
  1644. break;
  1645. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  1646. if (rem == 0)
  1647. break;
  1648. skb_shinfo(skb)->frags[i - 1].size -= rem;
  1649. skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
  1650. get_page(skb_shinfo(skb)->frags[i].page);
  1651. skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
  1652. skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
  1653. skb_shinfo(skb)->frags[i].size = rem;
  1654. i++;
  1655. skb_shinfo(skb)->nr_frags = i;
  1656. }
  1657. }
  1658. /* Stamp the time, and sequence number, convert them to network byte order */
  1659. if (pgh) {
  1660. struct timeval timestamp;
  1661. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  1662. pgh->seq_num = htonl(pkt_dev->seq_num);
  1663. do_gettimeofday(&timestamp);
  1664. pgh->tv_sec = htonl(timestamp.tv_sec);
  1665. pgh->tv_usec = htonl(timestamp.tv_usec);
  1666. }
  1667. pkt_dev->seq_num++;
  1668. return skb;
  1669. }
  1670. /*
  1671. * scan_ip6, fmt_ip taken from dietlibc-0.21
  1672. * Author Felix von Leitner <felix-dietlibc@fefe.de>
  1673. *
  1674. * Slightly modified for kernel.
  1675. * Should be candidate for net/ipv4/utils.c
  1676. * --ro
  1677. */
  1678. static unsigned int scan_ip6(const char *s,char ip[16])
  1679. {
  1680. unsigned int i;
  1681. unsigned int len=0;
  1682. unsigned long u;
  1683. char suffix[16];
  1684. unsigned int prefixlen=0;
  1685. unsigned int suffixlen=0;
  1686. __u32 tmp;
  1687. for (i=0; i<16; i++) ip[i]=0;
  1688. for (;;) {
  1689. if (*s == ':') {
  1690. len++;
  1691. if (s[1] == ':') { /* Found "::", skip to part 2 */
  1692. s+=2;
  1693. len++;
  1694. break;
  1695. }
  1696. s++;
  1697. }
  1698. {
  1699. char *tmp;
  1700. u=simple_strtoul(s,&tmp,16);
  1701. i=tmp-s;
  1702. }
  1703. if (!i) return 0;
  1704. if (prefixlen==12 && s[i]=='.') {
  1705. /* the last 4 bytes may be written as IPv4 address */
  1706. tmp = in_aton(s);
  1707. memcpy((struct in_addr*)(ip+12), &tmp, sizeof(tmp));
  1708. return i+len;
  1709. }
  1710. ip[prefixlen++] = (u >> 8);
  1711. ip[prefixlen++] = (u & 255);
  1712. s += i; len += i;
  1713. if (prefixlen==16)
  1714. return len;
  1715. }
  1716. /* part 2, after "::" */
  1717. for (;;) {
  1718. if (*s == ':') {
  1719. if (suffixlen==0)
  1720. break;
  1721. s++;
  1722. len++;
  1723. } else if (suffixlen!=0)
  1724. break;
  1725. {
  1726. char *tmp;
  1727. u=simple_strtol(s,&tmp,16);
  1728. i=tmp-s;
  1729. }
  1730. if (!i) {
  1731. if (*s) len--;
  1732. break;
  1733. }
  1734. if (suffixlen+prefixlen<=12 && s[i]=='.') {
  1735. tmp = in_aton(s);
  1736. memcpy((struct in_addr*)(suffix+suffixlen), &tmp, sizeof(tmp));
  1737. suffixlen+=4;
  1738. len+=strlen(s);
  1739. break;
  1740. }
  1741. suffix[suffixlen++] = (u >> 8);
  1742. suffix[suffixlen++] = (u & 255);
  1743. s += i; len += i;
  1744. if (prefixlen+suffixlen==16)
  1745. break;
  1746. }
  1747. for (i=0; i<suffixlen; i++)
  1748. ip[16-suffixlen+i] = suffix[i];
  1749. return len;
  1750. }
  1751. static char tohex(char hexdigit) {
  1752. return hexdigit>9?hexdigit+'a'-10:hexdigit+'0';
  1753. }
  1754. static int fmt_xlong(char* s,unsigned int i) {
  1755. char* bak=s;
  1756. *s=tohex((i>>12)&0xf); if (s!=bak || *s!='0') ++s;
  1757. *s=tohex((i>>8)&0xf); if (s!=bak || *s!='0') ++s;
  1758. *s=tohex((i>>4)&0xf); if (s!=bak || *s!='0') ++s;
  1759. *s=tohex(i&0xf);
  1760. return s-bak+1;
  1761. }
  1762. static unsigned int fmt_ip6(char *s,const char ip[16]) {
  1763. unsigned int len;
  1764. unsigned int i;
  1765. unsigned int temp;
  1766. unsigned int compressing;
  1767. int j;
  1768. len = 0; compressing = 0;
  1769. for (j=0; j<16; j+=2) {
  1770. #ifdef V4MAPPEDPREFIX
  1771. if (j==12 && !memcmp(ip,V4mappedprefix,12)) {
  1772. inet_ntoa_r(*(struct in_addr*)(ip+12),s);
  1773. temp=strlen(s);
  1774. return len+temp;
  1775. }
  1776. #endif
  1777. temp = ((unsigned long) (unsigned char) ip[j] << 8) +
  1778. (unsigned long) (unsigned char) ip[j+1];
  1779. if (temp == 0) {
  1780. if (!compressing) {
  1781. compressing=1;
  1782. if (j==0) {
  1783. *s++=':'; ++len;
  1784. }
  1785. }
  1786. } else {
  1787. if (compressing) {
  1788. compressing=0;
  1789. *s++=':'; ++len;
  1790. }
  1791. i = fmt_xlong(s,temp); len += i; s += i;
  1792. if (j<14) {
  1793. *s++ = ':';
  1794. ++len;
  1795. }
  1796. }
  1797. }
  1798. if (compressing) {
  1799. *s++=':'; ++len;
  1800. }
  1801. *s=0;
  1802. return len;
  1803. }
  1804. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  1805. struct pktgen_dev *pkt_dev)
  1806. {
  1807. struct sk_buff *skb = NULL;
  1808. __u8 *eth;
  1809. struct udphdr *udph;
  1810. int datalen;
  1811. struct ipv6hdr *iph;
  1812. struct pktgen_hdr *pgh = NULL;
  1813. /* Update any of the values, used when we're incrementing various
  1814. * fields.
  1815. */
  1816. mod_cur_headers(pkt_dev);
  1817. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
  1818. if (!skb) {
  1819. sprintf(pkt_dev->result, "No memory");
  1820. return NULL;
  1821. }
  1822. skb_reserve(skb, 16);
  1823. /* Reserve for ethernet and IP header */
  1824. eth = (__u8 *) skb_push(skb, 14);
  1825. iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
  1826. udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
  1827. memcpy(eth, pkt_dev->hh, 12);
  1828. *(u16*)&eth[12] = __constant_htons(ETH_P_IPV6);
  1829. datalen = pkt_dev->cur_pkt_size-14-
  1830. sizeof(struct ipv6hdr)-sizeof(struct udphdr); /* Eth + IPh + UDPh */
  1831. if (datalen < sizeof(struct pktgen_hdr)) {
  1832. datalen = sizeof(struct pktgen_hdr);
  1833. if (net_ratelimit())
  1834. printk(KERN_INFO "pktgen: increased datalen to %d\n", datalen);
  1835. }
  1836. udph->source = htons(pkt_dev->cur_udp_src);
  1837. udph->dest = htons(pkt_dev->cur_udp_dst);
  1838. udph->len = htons(datalen + sizeof(struct udphdr));
  1839. udph->check = 0; /* No checksum */
  1840. *(u32*)iph = __constant_htonl(0x60000000); /* Version + flow */
  1841. iph->hop_limit = 32;
  1842. iph->payload_len = htons(sizeof(struct udphdr) + datalen);
  1843. iph->nexthdr = IPPROTO_UDP;
  1844. ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
  1845. ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
  1846. skb->mac.raw = ((u8 *)iph) - 14;
  1847. skb->protocol = __constant_htons(ETH_P_IPV6);
  1848. skb->dev = odev;
  1849. skb->pkt_type = PACKET_HOST;
  1850. if (pkt_dev->nfrags <= 0)
  1851. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  1852. else {
  1853. int frags = pkt_dev->nfrags;
  1854. int i;
  1855. pgh = (struct pktgen_hdr*)(((char*)(udph)) + 8);
  1856. if (frags > MAX_SKB_FRAGS)
  1857. frags = MAX_SKB_FRAGS;
  1858. if (datalen > frags*PAGE_SIZE) {
  1859. skb_put(skb, datalen-frags*PAGE_SIZE);
  1860. datalen = frags*PAGE_SIZE;
  1861. }
  1862. i = 0;
  1863. while (datalen > 0) {
  1864. struct page *page = alloc_pages(GFP_KERNEL, 0);
  1865. skb_shinfo(skb)->frags[i].page = page;
  1866. skb_shinfo(skb)->frags[i].page_offset = 0;
  1867. skb_shinfo(skb)->frags[i].size =
  1868. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  1869. datalen -= skb_shinfo(skb)->frags[i].size;
  1870. skb->len += skb_shinfo(skb)->frags[i].size;
  1871. skb->data_len += skb_shinfo(skb)->frags[i].size;
  1872. i++;
  1873. skb_shinfo(skb)->nr_frags = i;
  1874. }
  1875. while (i < frags) {
  1876. int rem;
  1877. if (i == 0)
  1878. break;
  1879. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  1880. if (rem == 0)
  1881. break;
  1882. skb_shinfo(skb)->frags[i - 1].size -= rem;
  1883. skb_shinfo(skb)->frags[i] = skb_shinfo(skb)->frags[i - 1];
  1884. get_page(skb_shinfo(skb)->frags[i].page);
  1885. skb_shinfo(skb)->frags[i].page = skb_shinfo(skb)->frags[i - 1].page;
  1886. skb_shinfo(skb)->frags[i].page_offset += skb_shinfo(skb)->frags[i - 1].size;
  1887. skb_shinfo(skb)->frags[i].size = rem;
  1888. i++;
  1889. skb_shinfo(skb)->nr_frags = i;
  1890. }
  1891. }
  1892. /* Stamp the time, and sequence number, convert them to network byte order */
  1893. /* should we update cloned packets too ? */
  1894. if (pgh) {
  1895. struct timeval timestamp;
  1896. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  1897. pgh->seq_num = htonl(pkt_dev->seq_num);
  1898. do_gettimeofday(&timestamp);
  1899. pgh->tv_sec = htonl(timestamp.tv_sec);
  1900. pgh->tv_usec = htonl(timestamp.tv_usec);
  1901. }
  1902. pkt_dev->seq_num++;
  1903. return skb;
  1904. }
  1905. static inline struct sk_buff *fill_packet(struct net_device *odev,
  1906. struct pktgen_dev *pkt_dev)
  1907. {
  1908. if(pkt_dev->flags & F_IPV6)
  1909. return fill_packet_ipv6(odev, pkt_dev);
  1910. else
  1911. return fill_packet_ipv4(odev, pkt_dev);
  1912. }
  1913. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  1914. {
  1915. pkt_dev->seq_num = 1;
  1916. pkt_dev->idle_acc = 0;
  1917. pkt_dev->sofar = 0;
  1918. pkt_dev->tx_bytes = 0;
  1919. pkt_dev->errors = 0;
  1920. }
  1921. /* Set up structure for sending pkts, clear counters */
  1922. static void pktgen_run(struct pktgen_thread *t)
  1923. {
  1924. struct pktgen_dev *pkt_dev = NULL;
  1925. int started = 0;
  1926. PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
  1927. if_lock(t);
  1928. for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
  1929. /*
  1930. * setup odev and create initial packet.
  1931. */
  1932. pktgen_setup_inject(pkt_dev);
  1933. if(pkt_dev->odev) {
  1934. pktgen_clear_counters(pkt_dev);
  1935. pkt_dev->running = 1; /* Cranke yeself! */
  1936. pkt_dev->skb = NULL;
  1937. pkt_dev->started_at = getCurUs();
  1938. pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
  1939. pkt_dev->next_tx_ns = 0;
  1940. strcpy(pkt_dev->result, "Starting");
  1941. started++;
  1942. }
  1943. else
  1944. strcpy(pkt_dev->result, "Error starting");
  1945. }
  1946. if_unlock(t);
  1947. if(started) t->control &= ~(T_STOP);
  1948. }
  1949. static void pktgen_stop_all_threads_ifs(void)
  1950. {
  1951. struct pktgen_thread *t = pktgen_threads;
  1952. PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads.\n"));
  1953. thread_lock();
  1954. while(t) {
  1955. pktgen_stop(t);
  1956. t = t->next;
  1957. }
  1958. thread_unlock();
  1959. }
  1960. static int thread_is_running(struct pktgen_thread *t )
  1961. {
  1962. struct pktgen_dev *next;
  1963. int res = 0;
  1964. for(next=t->if_list; next; next=next->next) {
  1965. if(next->running) {
  1966. res = 1;
  1967. break;
  1968. }
  1969. }
  1970. return res;
  1971. }
  1972. static int pktgen_wait_thread_run(struct pktgen_thread *t )
  1973. {
  1974. if_lock(t);
  1975. while(thread_is_running(t)) {
  1976. if_unlock(t);
  1977. msleep_interruptible(100);
  1978. if (signal_pending(current))
  1979. goto signal;
  1980. if_lock(t);
  1981. }
  1982. if_unlock(t);
  1983. return 1;
  1984. signal:
  1985. return 0;
  1986. }
  1987. static int pktgen_wait_all_threads_run(void)
  1988. {
  1989. struct pktgen_thread *t = pktgen_threads;
  1990. int sig = 1;
  1991. while (t) {
  1992. sig = pktgen_wait_thread_run(t);
  1993. if( sig == 0 ) break;
  1994. thread_lock();
  1995. t=t->next;
  1996. thread_unlock();
  1997. }
  1998. if(sig == 0) {
  1999. thread_lock();
  2000. while (t) {
  2001. t->control |= (T_STOP);
  2002. t=t->next;
  2003. }
  2004. thread_unlock();
  2005. }
  2006. return sig;
  2007. }
  2008. static void pktgen_run_all_threads(void)
  2009. {
  2010. struct pktgen_thread *t = pktgen_threads;
  2011. PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
  2012. thread_lock();
  2013. while(t) {
  2014. t->control |= (T_RUN);
  2015. t = t->next;
  2016. }
  2017. thread_unlock();
  2018. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  2019. pktgen_wait_all_threads_run();
  2020. }
  2021. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2022. {
  2023. __u64 total_us, bps, mbps, pps, idle;
  2024. char *p = pkt_dev->result;
  2025. total_us = pkt_dev->stopped_at - pkt_dev->started_at;
  2026. idle = pkt_dev->idle_acc;
  2027. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2028. (unsigned long long) total_us,
  2029. (unsigned long long)(total_us - idle),
  2030. (unsigned long long) idle,
  2031. (unsigned long long) pkt_dev->sofar,
  2032. pkt_dev->cur_pkt_size, nr_frags);
  2033. pps = pkt_dev->sofar * USEC_PER_SEC;
  2034. while ((total_us >> 32) != 0) {
  2035. pps >>= 1;
  2036. total_us >>= 1;
  2037. }
  2038. do_div(pps, total_us);
  2039. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2040. mbps = bps;
  2041. do_div(mbps, 1000000);
  2042. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2043. (unsigned long long) pps,
  2044. (unsigned long long) mbps,
  2045. (unsigned long long) bps,
  2046. (unsigned long long) pkt_dev->errors);
  2047. }
  2048. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2049. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2050. {
  2051. if (!pkt_dev->running) {
  2052. printk("pktgen: interface: %s is already stopped\n", pkt_dev->ifname);
  2053. return -EINVAL;
  2054. }
  2055. pkt_dev->stopped_at = getCurUs();
  2056. pkt_dev->running = 0;
  2057. show_results(pkt_dev, skb_shinfo(pkt_dev->skb)->nr_frags);
  2058. if (pkt_dev->skb)
  2059. kfree_skb(pkt_dev->skb);
  2060. pkt_dev->skb = NULL;
  2061. return 0;
  2062. }
  2063. static struct pktgen_dev *next_to_run(struct pktgen_thread *t )
  2064. {
  2065. struct pktgen_dev *next, *best = NULL;
  2066. if_lock(t);
  2067. for(next=t->if_list; next ; next=next->next) {
  2068. if(!next->running) continue;
  2069. if(best == NULL) best=next;
  2070. else if ( next->next_tx_us < best->next_tx_us)
  2071. best = next;
  2072. }
  2073. if_unlock(t);
  2074. return best;
  2075. }
  2076. static void pktgen_stop(struct pktgen_thread *t) {
  2077. struct pktgen_dev *next = NULL;
  2078. PG_DEBUG(printk("pktgen: entering pktgen_stop.\n"));
  2079. if_lock(t);
  2080. for(next=t->if_list; next; next=next->next)
  2081. pktgen_stop_device(next);
  2082. if_unlock(t);
  2083. }
  2084. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2085. {
  2086. struct pktgen_dev *cur, *next = NULL;
  2087. /* Remove all devices, free mem */
  2088. if_lock(t);
  2089. for(cur=t->if_list; cur; cur=next) {
  2090. next = cur->next;
  2091. pktgen_remove_device(t, cur);
  2092. }
  2093. if_unlock(t);
  2094. }
  2095. static void pktgen_rem_thread(struct pktgen_thread *t)
  2096. {
  2097. /* Remove from the thread list */
  2098. struct pktgen_thread *tmp = pktgen_threads;
  2099. remove_proc_entry(t->name, pg_proc_dir);
  2100. thread_lock();
  2101. if (tmp == t)
  2102. pktgen_threads = tmp->next;
  2103. else {
  2104. while (tmp) {
  2105. if (tmp->next == t) {
  2106. tmp->next = t->next;
  2107. t->next = NULL;
  2108. break;
  2109. }
  2110. tmp = tmp->next;
  2111. }
  2112. }
  2113. thread_unlock();
  2114. }
  2115. static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2116. {
  2117. struct net_device *odev = NULL;
  2118. __u64 idle_start = 0;
  2119. int ret;
  2120. odev = pkt_dev->odev;
  2121. if (pkt_dev->delay_us || pkt_dev->delay_ns) {
  2122. u64 now;
  2123. now = getCurUs();
  2124. if (now < pkt_dev->next_tx_us)
  2125. spin(pkt_dev, pkt_dev->next_tx_us);
  2126. /* This is max DELAY, this has special meaning of
  2127. * "never transmit"
  2128. */
  2129. if (pkt_dev->delay_us == 0x7FFFFFFF) {
  2130. pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
  2131. pkt_dev->next_tx_ns = pkt_dev->delay_ns;
  2132. goto out;
  2133. }
  2134. }
  2135. if (netif_queue_stopped(odev) || need_resched()) {
  2136. idle_start = getCurUs();
  2137. if (!netif_running(odev)) {
  2138. pktgen_stop_device(pkt_dev);
  2139. goto out;
  2140. }
  2141. if (need_resched())
  2142. schedule();
  2143. pkt_dev->idle_acc += getCurUs() - idle_start;
  2144. if (netif_queue_stopped(odev)) {
  2145. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2146. pkt_dev->next_tx_ns = 0;
  2147. goto out; /* Try the next interface */
  2148. }
  2149. }
  2150. if (pkt_dev->last_ok || !pkt_dev->skb) {
  2151. if ((++pkt_dev->clone_count >= pkt_dev->clone_skb ) || (!pkt_dev->skb)) {
  2152. /* build a new pkt */
  2153. if (pkt_dev->skb)
  2154. kfree_skb(pkt_dev->skb);
  2155. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2156. if (pkt_dev->skb == NULL) {
  2157. printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
  2158. schedule();
  2159. pkt_dev->clone_count--; /* back out increment, OOM */
  2160. goto out;
  2161. }
  2162. pkt_dev->allocated_skbs++;
  2163. pkt_dev->clone_count = 0; /* reset counter */
  2164. }
  2165. }
  2166. spin_lock_bh(&odev->xmit_lock);
  2167. if (!netif_queue_stopped(odev)) {
  2168. atomic_inc(&(pkt_dev->skb->users));
  2169. retry_now:
  2170. ret = odev->hard_start_xmit(pkt_dev->skb, odev);
  2171. if (likely(ret == NETDEV_TX_OK)) {
  2172. pkt_dev->last_ok = 1;
  2173. pkt_dev->sofar++;
  2174. pkt_dev->seq_num++;
  2175. pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
  2176. } else if (ret == NETDEV_TX_LOCKED
  2177. && (odev->features & NETIF_F_LLTX)) {
  2178. cpu_relax();
  2179. goto retry_now;
  2180. } else { /* Retry it next time */
  2181. atomic_dec(&(pkt_dev->skb->users));
  2182. if (debug && net_ratelimit())
  2183. printk(KERN_INFO "pktgen: Hard xmit error\n");
  2184. pkt_dev->errors++;
  2185. pkt_dev->last_ok = 0;
  2186. }
  2187. pkt_dev->next_tx_us = getCurUs();
  2188. pkt_dev->next_tx_ns = 0;
  2189. pkt_dev->next_tx_us += pkt_dev->delay_us;
  2190. pkt_dev->next_tx_ns += pkt_dev->delay_ns;
  2191. if (pkt_dev->next_tx_ns > 1000) {
  2192. pkt_dev->next_tx_us++;
  2193. pkt_dev->next_tx_ns -= 1000;
  2194. }
  2195. }
  2196. else { /* Retry it next time */
  2197. pkt_dev->last_ok = 0;
  2198. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2199. pkt_dev->next_tx_ns = 0;
  2200. }
  2201. spin_unlock_bh(&odev->xmit_lock);
  2202. /* If pkt_dev->count is zero, then run forever */
  2203. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  2204. if (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2205. idle_start = getCurUs();
  2206. while (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2207. if (signal_pending(current)) {
  2208. break;
  2209. }
  2210. schedule();
  2211. }
  2212. pkt_dev->idle_acc += getCurUs() - idle_start;
  2213. }
  2214. /* Done with this */
  2215. pktgen_stop_device(pkt_dev);
  2216. }
  2217. out:;
  2218. }
  2219. /*
  2220. * Main loop of the thread goes here
  2221. */
  2222. static void pktgen_thread_worker(struct pktgen_thread *t)
  2223. {
  2224. DEFINE_WAIT(wait);
  2225. struct pktgen_dev *pkt_dev = NULL;
  2226. int cpu = t->cpu;
  2227. sigset_t tmpsig;
  2228. u32 max_before_softirq;
  2229. u32 tx_since_softirq = 0;
  2230. daemonize("pktgen/%d", cpu);
  2231. /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
  2232. spin_lock_irq(&current->sighand->siglock);
  2233. tmpsig = current->blocked;
  2234. siginitsetinv(&current->blocked,
  2235. sigmask(SIGKILL) |
  2236. sigmask(SIGSTOP)|
  2237. sigmask(SIGTERM));
  2238. recalc_sigpending();
  2239. spin_unlock_irq(&current->sighand->siglock);
  2240. /* Migrate to the right CPU */
  2241. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  2242. if (smp_processor_id() != cpu)
  2243. BUG();
  2244. init_waitqueue_head(&t->queue);
  2245. t->control &= ~(T_TERMINATE);
  2246. t->control &= ~(T_RUN);
  2247. t->control &= ~(T_STOP);
  2248. t->control &= ~(T_REMDEV);
  2249. t->pid = current->pid;
  2250. PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
  2251. max_before_softirq = t->max_before_softirq;
  2252. __set_current_state(TASK_INTERRUPTIBLE);
  2253. mb();
  2254. while (1) {
  2255. __set_current_state(TASK_RUNNING);
  2256. /*
  2257. * Get next dev to xmit -- if any.
  2258. */
  2259. pkt_dev = next_to_run(t);
  2260. if (pkt_dev) {
  2261. pktgen_xmit(pkt_dev);
  2262. /*
  2263. * We like to stay RUNNING but must also give
  2264. * others fair share.
  2265. */
  2266. tx_since_softirq += pkt_dev->last_ok;
  2267. if (tx_since_softirq > max_before_softirq) {
  2268. if (local_softirq_pending())
  2269. do_softirq();
  2270. tx_since_softirq = 0;
  2271. }
  2272. } else {
  2273. prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
  2274. schedule_timeout(HZ/10);
  2275. finish_wait(&(t->queue), &wait);
  2276. }
  2277. /*
  2278. * Back from sleep, either due to the timeout or signal.
  2279. * We check if we have any "posted" work for us.
  2280. */
  2281. if (t->control & T_TERMINATE || signal_pending(current))
  2282. /* we received a request to terminate ourself */
  2283. break;
  2284. if(t->control & T_STOP) {
  2285. pktgen_stop(t);
  2286. t->control &= ~(T_STOP);
  2287. }
  2288. if(t->control & T_RUN) {
  2289. pktgen_run(t);
  2290. t->control &= ~(T_RUN);
  2291. }
  2292. if(t->control & T_REMDEV) {
  2293. pktgen_rem_all_ifs(t);
  2294. t->control &= ~(T_REMDEV);
  2295. }
  2296. if (need_resched())
  2297. schedule();
  2298. }
  2299. PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
  2300. pktgen_stop(t);
  2301. PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
  2302. pktgen_rem_all_ifs(t);
  2303. PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
  2304. pktgen_rem_thread(t);
  2305. }
  2306. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, const char* ifname)
  2307. {
  2308. struct pktgen_dev *pkt_dev = NULL;
  2309. if_lock(t);
  2310. for(pkt_dev=t->if_list; pkt_dev; pkt_dev = pkt_dev->next ) {
  2311. if (strncmp(pkt_dev->ifname, ifname, IFNAMSIZ) == 0) {
  2312. break;
  2313. }
  2314. }
  2315. if_unlock(t);
  2316. PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname,pkt_dev));
  2317. return pkt_dev;
  2318. }
  2319. /*
  2320. * Adds a dev at front of if_list.
  2321. */
  2322. static int add_dev_to_thread(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
  2323. {
  2324. int rv = 0;
  2325. if_lock(t);
  2326. if (pkt_dev->pg_thread) {
  2327. printk("pktgen: ERROR: already assigned to a thread.\n");
  2328. rv = -EBUSY;
  2329. goto out;
  2330. }
  2331. pkt_dev->next =t->if_list; t->if_list=pkt_dev;
  2332. pkt_dev->pg_thread = t;
  2333. pkt_dev->running = 0;
  2334. out:
  2335. if_unlock(t);
  2336. return rv;
  2337. }
  2338. /* Called under thread lock */
  2339. static int pktgen_add_device(struct pktgen_thread *t, const char* ifname)
  2340. {
  2341. struct pktgen_dev *pkt_dev;
  2342. struct proc_dir_entry *pe;
  2343. /* We don't allow a device to be on several threads */
  2344. pkt_dev = __pktgen_NN_threads(ifname, FIND);
  2345. if (pkt_dev) {
  2346. printk("pktgen: ERROR: interface already used.\n");
  2347. return -EBUSY;
  2348. }
  2349. pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
  2350. if (!pkt_dev)
  2351. return -ENOMEM;
  2352. pkt_dev->flows = vmalloc(MAX_CFLOWS*sizeof(struct flow_state));
  2353. if (pkt_dev->flows == NULL) {
  2354. kfree(pkt_dev);
  2355. return -ENOMEM;
  2356. }
  2357. memset(pkt_dev->flows, 0, MAX_CFLOWS*sizeof(struct flow_state));
  2358. pkt_dev->min_pkt_size = ETH_ZLEN;
  2359. pkt_dev->max_pkt_size = ETH_ZLEN;
  2360. pkt_dev->nfrags = 0;
  2361. pkt_dev->clone_skb = pg_clone_skb_d;
  2362. pkt_dev->delay_us = pg_delay_d / 1000;
  2363. pkt_dev->delay_ns = pg_delay_d % 1000;
  2364. pkt_dev->count = pg_count_d;
  2365. pkt_dev->sofar = 0;
  2366. pkt_dev->udp_src_min = 9; /* sink port */
  2367. pkt_dev->udp_src_max = 9;
  2368. pkt_dev->udp_dst_min = 9;
  2369. pkt_dev->udp_dst_max = 9;
  2370. strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
  2371. if (! pktgen_setup_dev(pkt_dev)) {
  2372. printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
  2373. if (pkt_dev->flows)
  2374. vfree(pkt_dev->flows);
  2375. kfree(pkt_dev);
  2376. return -ENODEV;
  2377. }
  2378. pe = create_proc_entry(ifname, 0600, pg_proc_dir);
  2379. if (!pe) {
  2380. printk("pktgen: cannot create %s/%s procfs entry.\n",
  2381. PG_PROC_DIR, ifname);
  2382. if (pkt_dev->flows)
  2383. vfree(pkt_dev->flows);
  2384. kfree(pkt_dev);
  2385. return -EINVAL;
  2386. }
  2387. pe->proc_fops = &pktgen_if_fops;
  2388. pe->data = pkt_dev;
  2389. return add_dev_to_thread(t, pkt_dev);
  2390. }
  2391. static struct pktgen_thread * __init pktgen_find_thread(const char* name)
  2392. {
  2393. struct pktgen_thread *t = NULL;
  2394. thread_lock();
  2395. t = pktgen_threads;
  2396. while (t) {
  2397. if (strcmp(t->name, name) == 0)
  2398. break;
  2399. t = t->next;
  2400. }
  2401. thread_unlock();
  2402. return t;
  2403. }
  2404. static int __init pktgen_create_thread(const char* name, int cpu)
  2405. {
  2406. struct pktgen_thread *t = NULL;
  2407. struct proc_dir_entry *pe;
  2408. if (strlen(name) > 31) {
  2409. printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
  2410. return -EINVAL;
  2411. }
  2412. if (pktgen_find_thread(name)) {
  2413. printk("pktgen: ERROR: thread: %s already exists\n", name);
  2414. return -EINVAL;
  2415. }
  2416. t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
  2417. if (!t) {
  2418. printk("pktgen: ERROR: out of memory, can't create new thread.\n");
  2419. return -ENOMEM;
  2420. }
  2421. strcpy(t->name, name);
  2422. spin_lock_init(&t->if_lock);
  2423. t->cpu = cpu;
  2424. pe = create_proc_entry(t->name, 0600, pg_proc_dir);
  2425. if (!pe) {
  2426. printk("pktgen: cannot create %s/%s procfs entry.\n",
  2427. PG_PROC_DIR, t->name);
  2428. kfree(t);
  2429. return -EINVAL;
  2430. }
  2431. pe->proc_fops = &pktgen_thread_fops;
  2432. pe->data = t;
  2433. t->next = pktgen_threads;
  2434. pktgen_threads = t;
  2435. if (kernel_thread((void *) pktgen_thread_worker, (void *) t,
  2436. CLONE_FS | CLONE_FILES | CLONE_SIGHAND) < 0)
  2437. printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
  2438. return 0;
  2439. }
  2440. /*
  2441. * Removes a device from the thread if_list.
  2442. */
  2443. static void _rem_dev_from_if_list(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
  2444. {
  2445. struct pktgen_dev *i, *prev = NULL;
  2446. i = t->if_list;
  2447. while(i) {
  2448. if(i == pkt_dev) {
  2449. if(prev) prev->next = i->next;
  2450. else t->if_list = NULL;
  2451. break;
  2452. }
  2453. prev = i;
  2454. i=i->next;
  2455. }
  2456. }
  2457. static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *pkt_dev)
  2458. {
  2459. PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
  2460. if (pkt_dev->running) {
  2461. printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
  2462. pktgen_stop_device(pkt_dev);
  2463. }
  2464. /* Dis-associate from the interface */
  2465. if (pkt_dev->odev) {
  2466. dev_put(pkt_dev->odev);
  2467. pkt_dev->odev = NULL;
  2468. }
  2469. /* And update the thread if_list */
  2470. _rem_dev_from_if_list(t, pkt_dev);
  2471. /* Clean up proc file system */
  2472. remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
  2473. if (pkt_dev->flows)
  2474. vfree(pkt_dev->flows);
  2475. kfree(pkt_dev);
  2476. return 0;
  2477. }
  2478. static int __init pg_init(void)
  2479. {
  2480. int cpu;
  2481. struct proc_dir_entry *pe;
  2482. printk(version);
  2483. pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
  2484. if (!pg_proc_dir)
  2485. return -ENODEV;
  2486. pg_proc_dir->owner = THIS_MODULE;
  2487. pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
  2488. if (pe == NULL) {
  2489. printk("pktgen: ERROR: cannot create %s procfs entry.\n", PGCTRL);
  2490. proc_net_remove(PG_PROC_DIR);
  2491. return -EINVAL;
  2492. }
  2493. pe->proc_fops = &pktgen_fops;
  2494. pe->data = NULL;
  2495. /* Register us to receive netdevice events */
  2496. register_netdevice_notifier(&pktgen_notifier_block);
  2497. for_each_online_cpu(cpu) {
  2498. char buf[30];
  2499. sprintf(buf, "kpktgend_%i", cpu);
  2500. pktgen_create_thread(buf, cpu);
  2501. }
  2502. return 0;
  2503. }
  2504. static void __exit pg_cleanup(void)
  2505. {
  2506. wait_queue_head_t queue;
  2507. init_waitqueue_head(&queue);
  2508. /* Stop all interfaces & threads */
  2509. while (pktgen_threads) {
  2510. struct pktgen_thread *t = pktgen_threads;
  2511. pktgen_threads->control |= (T_TERMINATE);
  2512. wait_event_interruptible_timeout(queue, (t != pktgen_threads), HZ);
  2513. }
  2514. /* Un-register us from receiving netdevice events */
  2515. unregister_netdevice_notifier(&pktgen_notifier_block);
  2516. /* Clean up proc file system */
  2517. remove_proc_entry(PGCTRL, pg_proc_dir);
  2518. proc_net_remove(PG_PROC_DIR);
  2519. }
  2520. module_init(pg_init);
  2521. module_exit(pg_cleanup);
  2522. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
  2523. MODULE_DESCRIPTION("Packet Generator tool");
  2524. MODULE_LICENSE("GPL");
  2525. module_param(pg_count_d, int, 0);
  2526. module_param(pg_delay_d, int, 0);
  2527. module_param(pg_clone_skb_d, int, 0);
  2528. module_param(debug, int, 0);