pktgen.c 76 KB

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