pktgen.c 93 KB

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