pktgen.c 93 KB

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