pktgen.c 87 KB

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