pktgen.c 76 KB

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