pktgen.c 91 KB

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