pktgen.c 92 KB

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