pktgen.c 94 KB

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