pktgen.c 86 KB

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