pktgen.c 82 KB

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