pktgen.c 93 KB

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