pktgen.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845
  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(KERN_WARNING "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(KERN_WARNING "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(KERN_WARNING "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "pktgen: VLAN turned on\n");
  1348. if (debug && pkt_dev->nr_labels)
  1349. printk(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "pktgen: SVLAN turned on\n");
  1398. if (debug && pkt_dev->nr_labels)
  1399. printk(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
  1540. name, (unsigned long)count);
  1541. if (!t) {
  1542. printk(KERN_ERR "pktgen: ERROR: No thread\n");
  1543. ret = -EINVAL;
  1544. goto out;
  1545. }
  1546. pg_result = &(t->result[0]);
  1547. if (!strcmp(name, "add_device")) {
  1548. char f[32];
  1549. memset(f, 0, 32);
  1550. len = strn_len(&user_buffer[i], sizeof(f) - 1);
  1551. if (len < 0) {
  1552. ret = len;
  1553. goto out;
  1554. }
  1555. if (copy_from_user(f, &user_buffer[i], len))
  1556. return -EFAULT;
  1557. i += len;
  1558. mutex_lock(&pktgen_thread_lock);
  1559. pktgen_add_device(t, f);
  1560. mutex_unlock(&pktgen_thread_lock);
  1561. ret = count;
  1562. sprintf(pg_result, "OK: add_device=%s", f);
  1563. goto out;
  1564. }
  1565. if (!strcmp(name, "rem_device_all")) {
  1566. mutex_lock(&pktgen_thread_lock);
  1567. t->control |= T_REMDEVALL;
  1568. mutex_unlock(&pktgen_thread_lock);
  1569. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  1570. ret = count;
  1571. sprintf(pg_result, "OK: rem_device_all");
  1572. goto out;
  1573. }
  1574. if (!strcmp(name, "max_before_softirq")) {
  1575. len = num_arg(&user_buffer[i], 10, &value);
  1576. mutex_lock(&pktgen_thread_lock);
  1577. t->max_before_softirq = value;
  1578. mutex_unlock(&pktgen_thread_lock);
  1579. ret = count;
  1580. sprintf(pg_result, "OK: max_before_softirq=%lu", value);
  1581. goto out;
  1582. }
  1583. ret = -EINVAL;
  1584. out:
  1585. return ret;
  1586. }
  1587. static int pktgen_thread_open(struct inode *inode, struct file *file)
  1588. {
  1589. return single_open(file, pktgen_thread_show, PDE(inode)->data);
  1590. }
  1591. static const struct file_operations pktgen_thread_fops = {
  1592. .owner = THIS_MODULE,
  1593. .open = pktgen_thread_open,
  1594. .read = seq_read,
  1595. .llseek = seq_lseek,
  1596. .write = pktgen_thread_write,
  1597. .release = single_release,
  1598. };
  1599. /* Think find or remove for NN */
  1600. static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
  1601. {
  1602. struct pktgen_thread *t;
  1603. struct pktgen_dev *pkt_dev = NULL;
  1604. list_for_each_entry(t, &pktgen_threads, th_list) {
  1605. pkt_dev = pktgen_find_dev(t, ifname);
  1606. if (pkt_dev) {
  1607. if (remove) {
  1608. if_lock(t);
  1609. pkt_dev->removal_mark = 1;
  1610. t->control |= T_REMDEV;
  1611. if_unlock(t);
  1612. }
  1613. break;
  1614. }
  1615. }
  1616. return pkt_dev;
  1617. }
  1618. /*
  1619. * mark a device for removal
  1620. */
  1621. static void pktgen_mark_device(const char *ifname)
  1622. {
  1623. struct pktgen_dev *pkt_dev = NULL;
  1624. const int max_tries = 10, msec_per_try = 125;
  1625. int i = 0;
  1626. mutex_lock(&pktgen_thread_lock);
  1627. pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
  1628. while (1) {
  1629. pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
  1630. if (pkt_dev == NULL)
  1631. break; /* success */
  1632. mutex_unlock(&pktgen_thread_lock);
  1633. pr_debug("pktgen: pktgen_mark_device waiting for %s "
  1634. "to disappear....\n", ifname);
  1635. schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
  1636. mutex_lock(&pktgen_thread_lock);
  1637. if (++i >= max_tries) {
  1638. printk(KERN_ERR "pktgen_mark_device: timed out after "
  1639. "waiting %d msec for device %s to be removed\n",
  1640. msec_per_try * i, ifname);
  1641. break;
  1642. }
  1643. }
  1644. mutex_unlock(&pktgen_thread_lock);
  1645. }
  1646. static void pktgen_change_name(struct net_device *dev)
  1647. {
  1648. struct pktgen_thread *t;
  1649. list_for_each_entry(t, &pktgen_threads, th_list) {
  1650. struct pktgen_dev *pkt_dev;
  1651. list_for_each_entry(pkt_dev, &t->if_list, list) {
  1652. if (pkt_dev->odev != dev)
  1653. continue;
  1654. remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
  1655. pkt_dev->entry = create_proc_entry(dev->name, 0600,
  1656. pg_proc_dir);
  1657. if (!pkt_dev->entry)
  1658. printk(KERN_ERR "pktgen: can't move proc "
  1659. " entry for '%s'\n", dev->name);
  1660. break;
  1661. }
  1662. }
  1663. }
  1664. static int pktgen_device_event(struct notifier_block *unused,
  1665. unsigned long event, void *ptr)
  1666. {
  1667. struct net_device *dev = ptr;
  1668. /* It is OK that we do not hold the group lock right now,
  1669. * as we run under the RTNL lock.
  1670. */
  1671. switch (event) {
  1672. case NETDEV_CHANGENAME:
  1673. pktgen_change_name(dev);
  1674. break;
  1675. case NETDEV_UNREGISTER:
  1676. pktgen_mark_device(dev->name);
  1677. break;
  1678. }
  1679. return NOTIFY_DONE;
  1680. }
  1681. /* Associate pktgen_dev with a device. */
  1682. static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
  1683. {
  1684. struct net_device *odev;
  1685. int err;
  1686. /* Clean old setups */
  1687. if (pkt_dev->odev) {
  1688. dev_put(pkt_dev->odev);
  1689. pkt_dev->odev = NULL;
  1690. }
  1691. odev = dev_get_by_name(ifname);
  1692. if (!odev) {
  1693. printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
  1694. return -ENODEV;
  1695. }
  1696. if (odev->type != ARPHRD_ETHER) {
  1697. printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
  1698. err = -EINVAL;
  1699. } else if (!netif_running(odev)) {
  1700. printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
  1701. err = -ENETDOWN;
  1702. } else {
  1703. pkt_dev->odev = odev;
  1704. return 0;
  1705. }
  1706. dev_put(odev);
  1707. return err;
  1708. }
  1709. /* Read pkt_dev from the interface and set up internal pktgen_dev
  1710. * structure to have the right information to create/send packets
  1711. */
  1712. static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
  1713. {
  1714. if (!pkt_dev->odev) {
  1715. printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
  1716. "setup_inject.\n");
  1717. sprintf(pkt_dev->result,
  1718. "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
  1719. return;
  1720. }
  1721. /* Default to the interface's mac if not explicitly set. */
  1722. if (is_zero_ether_addr(pkt_dev->src_mac))
  1723. memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
  1724. /* Set up Dest MAC */
  1725. memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
  1726. /* Set up pkt size */
  1727. pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
  1728. if (pkt_dev->flags & F_IPV6) {
  1729. /*
  1730. * Skip this automatic address setting until locks or functions
  1731. * gets exported
  1732. */
  1733. #ifdef NOTNOW
  1734. int i, set = 0, err = 1;
  1735. struct inet6_dev *idev;
  1736. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  1737. if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
  1738. set = 1;
  1739. break;
  1740. }
  1741. if (!set) {
  1742. /*
  1743. * Use linklevel address if unconfigured.
  1744. *
  1745. * use ipv6_get_lladdr if/when it's get exported
  1746. */
  1747. rcu_read_lock();
  1748. if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
  1749. struct inet6_ifaddr *ifp;
  1750. read_lock_bh(&idev->lock);
  1751. for (ifp = idev->addr_list; ifp;
  1752. ifp = ifp->if_next) {
  1753. if (ifp->scope == IFA_LINK
  1754. && !(ifp->
  1755. flags & IFA_F_TENTATIVE)) {
  1756. ipv6_addr_copy(&pkt_dev->
  1757. cur_in6_saddr,
  1758. &ifp->addr);
  1759. err = 0;
  1760. break;
  1761. }
  1762. }
  1763. read_unlock_bh(&idev->lock);
  1764. }
  1765. rcu_read_unlock();
  1766. if (err)
  1767. printk(KERN_ERR "pktgen: ERROR: IPv6 link "
  1768. "address not availble.\n");
  1769. }
  1770. #endif
  1771. } else {
  1772. pkt_dev->saddr_min = 0;
  1773. pkt_dev->saddr_max = 0;
  1774. if (strlen(pkt_dev->src_min) == 0) {
  1775. struct in_device *in_dev;
  1776. rcu_read_lock();
  1777. in_dev = __in_dev_get_rcu(pkt_dev->odev);
  1778. if (in_dev) {
  1779. if (in_dev->ifa_list) {
  1780. pkt_dev->saddr_min =
  1781. in_dev->ifa_list->ifa_address;
  1782. pkt_dev->saddr_max = pkt_dev->saddr_min;
  1783. }
  1784. }
  1785. rcu_read_unlock();
  1786. } else {
  1787. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1788. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1789. }
  1790. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1791. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1792. }
  1793. /* Initialize current values. */
  1794. pkt_dev->cur_dst_mac_offset = 0;
  1795. pkt_dev->cur_src_mac_offset = 0;
  1796. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1797. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1798. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1799. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1800. pkt_dev->nflows = 0;
  1801. }
  1802. static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
  1803. {
  1804. __u64 start;
  1805. __u64 now;
  1806. start = now = getCurUs();
  1807. printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
  1808. while (now < spin_until_us) {
  1809. /* TODO: optimize sleeping behavior */
  1810. if (spin_until_us - now > jiffies_to_usecs(1) + 1)
  1811. schedule_timeout_interruptible(1);
  1812. else if (spin_until_us - now > 100) {
  1813. do_softirq();
  1814. if (!pkt_dev->running)
  1815. return;
  1816. if (need_resched())
  1817. schedule();
  1818. }
  1819. now = getCurUs();
  1820. }
  1821. pkt_dev->idle_acc += now - start;
  1822. }
  1823. static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
  1824. {
  1825. pkt_dev->pkt_overhead = 0;
  1826. pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
  1827. pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
  1828. pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
  1829. }
  1830. static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
  1831. {
  1832. if (pkt_dev->flows[flow].flags & F_INIT)
  1833. return 1;
  1834. else
  1835. return 0;
  1836. }
  1837. static inline int f_pick(struct pktgen_dev *pkt_dev)
  1838. {
  1839. int flow = pkt_dev->curfl;
  1840. if (pkt_dev->flags & F_FLOW_SEQ) {
  1841. if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
  1842. /* reset time */
  1843. pkt_dev->flows[flow].count = 0;
  1844. pkt_dev->curfl += 1;
  1845. if (pkt_dev->curfl >= pkt_dev->cflows)
  1846. pkt_dev->curfl = 0; /*reset */
  1847. }
  1848. } else {
  1849. flow = random32() % pkt_dev->cflows;
  1850. if (pkt_dev->flows[flow].count > pkt_dev->lflow)
  1851. pkt_dev->flows[flow].count = 0;
  1852. }
  1853. return pkt_dev->curfl;
  1854. }
  1855. #ifdef CONFIG_XFRM
  1856. /* If there was already an IPSEC SA, we keep it as is, else
  1857. * we go look for it ...
  1858. */
  1859. static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
  1860. {
  1861. struct xfrm_state *x = pkt_dev->flows[flow].x;
  1862. if (!x) {
  1863. /*slow path: we dont already have xfrm_state*/
  1864. x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
  1865. (xfrm_address_t *)&pkt_dev->cur_saddr,
  1866. AF_INET,
  1867. pkt_dev->ipsmode,
  1868. pkt_dev->ipsproto, 0);
  1869. if (x) {
  1870. pkt_dev->flows[flow].x = x;
  1871. set_pkt_overhead(pkt_dev);
  1872. pkt_dev->pkt_overhead+=x->props.header_len;
  1873. }
  1874. }
  1875. }
  1876. #endif
  1877. /* Increment/randomize headers according to flags and current values
  1878. * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  1879. */
  1880. static void mod_cur_headers(struct pktgen_dev *pkt_dev)
  1881. {
  1882. __u32 imn;
  1883. __u32 imx;
  1884. int flow = 0;
  1885. if (pkt_dev->cflows)
  1886. flow = f_pick(pkt_dev);
  1887. /* Deal with source MAC */
  1888. if (pkt_dev->src_mac_count > 1) {
  1889. __u32 mc;
  1890. __u32 tmp;
  1891. if (pkt_dev->flags & F_MACSRC_RND)
  1892. mc = random32() % pkt_dev->src_mac_count;
  1893. else {
  1894. mc = pkt_dev->cur_src_mac_offset++;
  1895. if (pkt_dev->cur_src_mac_offset >
  1896. pkt_dev->src_mac_count)
  1897. pkt_dev->cur_src_mac_offset = 0;
  1898. }
  1899. tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
  1900. pkt_dev->hh[11] = tmp;
  1901. tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  1902. pkt_dev->hh[10] = tmp;
  1903. tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  1904. pkt_dev->hh[9] = tmp;
  1905. tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  1906. pkt_dev->hh[8] = tmp;
  1907. tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
  1908. pkt_dev->hh[7] = tmp;
  1909. }
  1910. /* Deal with Destination MAC */
  1911. if (pkt_dev->dst_mac_count > 1) {
  1912. __u32 mc;
  1913. __u32 tmp;
  1914. if (pkt_dev->flags & F_MACDST_RND)
  1915. mc = random32() % pkt_dev->dst_mac_count;
  1916. else {
  1917. mc = pkt_dev->cur_dst_mac_offset++;
  1918. if (pkt_dev->cur_dst_mac_offset >
  1919. pkt_dev->dst_mac_count) {
  1920. pkt_dev->cur_dst_mac_offset = 0;
  1921. }
  1922. }
  1923. tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
  1924. pkt_dev->hh[5] = tmp;
  1925. tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  1926. pkt_dev->hh[4] = tmp;
  1927. tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  1928. pkt_dev->hh[3] = tmp;
  1929. tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  1930. pkt_dev->hh[2] = tmp;
  1931. tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
  1932. pkt_dev->hh[1] = tmp;
  1933. }
  1934. if (pkt_dev->flags & F_MPLS_RND) {
  1935. unsigned i;
  1936. for (i = 0; i < pkt_dev->nr_labels; i++)
  1937. if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
  1938. pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
  1939. ((__force __be32)random32() &
  1940. htonl(0x000fffff));
  1941. }
  1942. if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
  1943. pkt_dev->vlan_id = random32() & (4096-1);
  1944. }
  1945. if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
  1946. pkt_dev->svlan_id = random32() & (4096 - 1);
  1947. }
  1948. if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
  1949. if (pkt_dev->flags & F_UDPSRC_RND)
  1950. pkt_dev->cur_udp_src = random32() %
  1951. (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
  1952. + pkt_dev->udp_src_min;
  1953. else {
  1954. pkt_dev->cur_udp_src++;
  1955. if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
  1956. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1957. }
  1958. }
  1959. if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
  1960. if (pkt_dev->flags & F_UDPDST_RND) {
  1961. pkt_dev->cur_udp_dst = random32() %
  1962. (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
  1963. + pkt_dev->udp_dst_min;
  1964. } else {
  1965. pkt_dev->cur_udp_dst++;
  1966. if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
  1967. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1968. }
  1969. }
  1970. if (!(pkt_dev->flags & F_IPV6)) {
  1971. if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
  1972. ntohl(pkt_dev->
  1973. saddr_max))) {
  1974. __u32 t;
  1975. if (pkt_dev->flags & F_IPSRC_RND)
  1976. t = random32() % (imx - imn) + imn;
  1977. else {
  1978. t = ntohl(pkt_dev->cur_saddr);
  1979. t++;
  1980. if (t > imx) {
  1981. t = imn;
  1982. }
  1983. }
  1984. pkt_dev->cur_saddr = htonl(t);
  1985. }
  1986. if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
  1987. pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
  1988. } else {
  1989. imn = ntohl(pkt_dev->daddr_min);
  1990. imx = ntohl(pkt_dev->daddr_max);
  1991. if (imn < imx) {
  1992. __u32 t;
  1993. __be32 s;
  1994. if (pkt_dev->flags & F_IPDST_RND) {
  1995. t = random32() % (imx - imn) + imn;
  1996. s = htonl(t);
  1997. while (LOOPBACK(s) || MULTICAST(s)
  1998. || BADCLASS(s) || ZERONET(s)
  1999. || LOCAL_MCAST(s)) {
  2000. t = random32() % (imx - imn) + imn;
  2001. s = htonl(t);
  2002. }
  2003. pkt_dev->cur_daddr = s;
  2004. } else {
  2005. t = ntohl(pkt_dev->cur_daddr);
  2006. t++;
  2007. if (t > imx) {
  2008. t = imn;
  2009. }
  2010. pkt_dev->cur_daddr = htonl(t);
  2011. }
  2012. }
  2013. if (pkt_dev->cflows) {
  2014. pkt_dev->flows[flow].flags |= F_INIT;
  2015. pkt_dev->flows[flow].cur_daddr =
  2016. pkt_dev->cur_daddr;
  2017. #ifdef CONFIG_XFRM
  2018. if (pkt_dev->flags & F_IPSEC_ON)
  2019. get_ipsec_sa(pkt_dev, flow);
  2020. #endif
  2021. pkt_dev->nflows++;
  2022. }
  2023. }
  2024. } else { /* IPV6 * */
  2025. if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
  2026. pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
  2027. pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
  2028. pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
  2029. else {
  2030. int i;
  2031. /* Only random destinations yet */
  2032. for (i = 0; i < 4; i++) {
  2033. pkt_dev->cur_in6_daddr.s6_addr32[i] =
  2034. (((__force __be32)random32() |
  2035. pkt_dev->min_in6_daddr.s6_addr32[i]) &
  2036. pkt_dev->max_in6_daddr.s6_addr32[i]);
  2037. }
  2038. }
  2039. }
  2040. if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
  2041. __u32 t;
  2042. if (pkt_dev->flags & F_TXSIZE_RND) {
  2043. t = random32() %
  2044. (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
  2045. + pkt_dev->min_pkt_size;
  2046. } else {
  2047. t = pkt_dev->cur_pkt_size + 1;
  2048. if (t > pkt_dev->max_pkt_size)
  2049. t = pkt_dev->min_pkt_size;
  2050. }
  2051. pkt_dev->cur_pkt_size = t;
  2052. }
  2053. pkt_dev->flows[flow].count++;
  2054. }
  2055. #ifdef CONFIG_XFRM
  2056. static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
  2057. {
  2058. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2059. int err = 0;
  2060. struct iphdr *iph;
  2061. if (!x)
  2062. return 0;
  2063. /* XXX: we dont support tunnel mode for now until
  2064. * we resolve the dst issue */
  2065. if (x->props.mode != XFRM_MODE_TRANSPORT)
  2066. return 0;
  2067. spin_lock(&x->lock);
  2068. iph = ip_hdr(skb);
  2069. err = x->mode->output(x, skb);
  2070. if (err)
  2071. goto error;
  2072. err = x->type->output(x, skb);
  2073. if (err)
  2074. goto error;
  2075. x->curlft.bytes +=skb->len;
  2076. x->curlft.packets++;
  2077. spin_unlock(&x->lock);
  2078. error:
  2079. spin_unlock(&x->lock);
  2080. return err;
  2081. }
  2082. static inline void free_SAs(struct pktgen_dev *pkt_dev)
  2083. {
  2084. if (pkt_dev->cflows) {
  2085. /* let go of the SAs if we have them */
  2086. int i = 0;
  2087. for (; i < pkt_dev->nflows; i++){
  2088. struct xfrm_state *x = pkt_dev->flows[i].x;
  2089. if (x) {
  2090. xfrm_state_put(x);
  2091. pkt_dev->flows[i].x = NULL;
  2092. }
  2093. }
  2094. }
  2095. }
  2096. static inline int process_ipsec(struct pktgen_dev *pkt_dev,
  2097. struct sk_buff *skb, __be16 protocol)
  2098. {
  2099. if (pkt_dev->flags & F_IPSEC_ON) {
  2100. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2101. int nhead = 0;
  2102. if (x) {
  2103. int ret;
  2104. __u8 *eth;
  2105. nhead = x->props.header_len - skb_headroom(skb);
  2106. if (nhead >0) {
  2107. ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
  2108. if (ret < 0) {
  2109. printk(KERN_ERR "Error expanding "
  2110. "ipsec packet %d\n",ret);
  2111. return 0;
  2112. }
  2113. }
  2114. /* ipsec is not expecting ll header */
  2115. skb_pull(skb, ETH_HLEN);
  2116. ret = pktgen_output_ipsec(skb, pkt_dev);
  2117. if (ret) {
  2118. printk(KERN_ERR "Error creating ipsec "
  2119. "packet %d\n",ret);
  2120. kfree_skb(skb);
  2121. return 0;
  2122. }
  2123. /* restore ll */
  2124. eth = (__u8 *) skb_push(skb, ETH_HLEN);
  2125. memcpy(eth, pkt_dev->hh, 12);
  2126. *(u16 *) & eth[12] = protocol;
  2127. }
  2128. }
  2129. return 1;
  2130. }
  2131. #endif
  2132. static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
  2133. {
  2134. unsigned i;
  2135. for (i = 0; i < pkt_dev->nr_labels; i++) {
  2136. *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
  2137. }
  2138. mpls--;
  2139. *mpls |= MPLS_STACK_BOTTOM;
  2140. }
  2141. static inline __be16 build_tci(unsigned int id, unsigned int cfi,
  2142. unsigned int prio)
  2143. {
  2144. return htons(id | (cfi << 12) | (prio << 13));
  2145. }
  2146. static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
  2147. struct pktgen_dev *pkt_dev)
  2148. {
  2149. struct sk_buff *skb = NULL;
  2150. __u8 *eth;
  2151. struct udphdr *udph;
  2152. int datalen, iplen;
  2153. struct iphdr *iph;
  2154. struct pktgen_hdr *pgh = NULL;
  2155. __be16 protocol = htons(ETH_P_IP);
  2156. __be32 *mpls;
  2157. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2158. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2159. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2160. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2161. if (pkt_dev->nr_labels)
  2162. protocol = htons(ETH_P_MPLS_UC);
  2163. if (pkt_dev->vlan_id != 0xffff)
  2164. protocol = htons(ETH_P_8021Q);
  2165. /* Update any of the values, used when we're incrementing various
  2166. * fields.
  2167. */
  2168. mod_cur_headers(pkt_dev);
  2169. datalen = (odev->hard_header_len + 16) & ~0xf;
  2170. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
  2171. pkt_dev->pkt_overhead, GFP_ATOMIC);
  2172. if (!skb) {
  2173. sprintf(pkt_dev->result, "No memory");
  2174. return NULL;
  2175. }
  2176. skb_reserve(skb, datalen);
  2177. /* Reserve for ethernet and IP header */
  2178. eth = (__u8 *) skb_push(skb, 14);
  2179. mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
  2180. if (pkt_dev->nr_labels)
  2181. mpls_push(mpls, pkt_dev);
  2182. if (pkt_dev->vlan_id != 0xffff) {
  2183. if (pkt_dev->svlan_id != 0xffff) {
  2184. svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2185. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2186. pkt_dev->svlan_cfi,
  2187. pkt_dev->svlan_p);
  2188. svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2189. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2190. }
  2191. vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2192. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2193. pkt_dev->vlan_cfi,
  2194. pkt_dev->vlan_p);
  2195. vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2196. *vlan_encapsulated_proto = htons(ETH_P_IP);
  2197. }
  2198. skb->network_header = skb->tail;
  2199. skb->transport_header = skb->network_header + sizeof(struct iphdr);
  2200. skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
  2201. iph = ip_hdr(skb);
  2202. udph = udp_hdr(skb);
  2203. memcpy(eth, pkt_dev->hh, 12);
  2204. *(__be16 *) & eth[12] = protocol;
  2205. /* Eth + IPh + UDPh + mpls */
  2206. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
  2207. pkt_dev->pkt_overhead;
  2208. if (datalen < sizeof(struct pktgen_hdr))
  2209. datalen = sizeof(struct pktgen_hdr);
  2210. udph->source = htons(pkt_dev->cur_udp_src);
  2211. udph->dest = htons(pkt_dev->cur_udp_dst);
  2212. udph->len = htons(datalen + 8); /* DATA + udphdr */
  2213. udph->check = 0; /* No checksum */
  2214. iph->ihl = 5;
  2215. iph->version = 4;
  2216. iph->ttl = 32;
  2217. iph->tos = pkt_dev->tos;
  2218. iph->protocol = IPPROTO_UDP; /* UDP */
  2219. iph->saddr = pkt_dev->cur_saddr;
  2220. iph->daddr = pkt_dev->cur_daddr;
  2221. iph->frag_off = 0;
  2222. iplen = 20 + 8 + datalen;
  2223. iph->tot_len = htons(iplen);
  2224. iph->check = 0;
  2225. iph->check = ip_fast_csum((void *)iph, iph->ihl);
  2226. skb->protocol = protocol;
  2227. skb->mac_header = (skb->network_header - ETH_HLEN -
  2228. pkt_dev->pkt_overhead);
  2229. skb->dev = odev;
  2230. skb->pkt_type = PACKET_HOST;
  2231. if (pkt_dev->nfrags <= 0)
  2232. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  2233. else {
  2234. int frags = pkt_dev->nfrags;
  2235. int i;
  2236. pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
  2237. if (frags > MAX_SKB_FRAGS)
  2238. frags = MAX_SKB_FRAGS;
  2239. if (datalen > frags * PAGE_SIZE) {
  2240. skb_put(skb, datalen - frags * PAGE_SIZE);
  2241. datalen = frags * PAGE_SIZE;
  2242. }
  2243. i = 0;
  2244. while (datalen > 0) {
  2245. struct page *page = alloc_pages(GFP_KERNEL, 0);
  2246. skb_shinfo(skb)->frags[i].page = page;
  2247. skb_shinfo(skb)->frags[i].page_offset = 0;
  2248. skb_shinfo(skb)->frags[i].size =
  2249. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  2250. datalen -= skb_shinfo(skb)->frags[i].size;
  2251. skb->len += skb_shinfo(skb)->frags[i].size;
  2252. skb->data_len += skb_shinfo(skb)->frags[i].size;
  2253. i++;
  2254. skb_shinfo(skb)->nr_frags = i;
  2255. }
  2256. while (i < frags) {
  2257. int rem;
  2258. if (i == 0)
  2259. break;
  2260. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  2261. if (rem == 0)
  2262. break;
  2263. skb_shinfo(skb)->frags[i - 1].size -= rem;
  2264. skb_shinfo(skb)->frags[i] =
  2265. skb_shinfo(skb)->frags[i - 1];
  2266. get_page(skb_shinfo(skb)->frags[i].page);
  2267. skb_shinfo(skb)->frags[i].page =
  2268. skb_shinfo(skb)->frags[i - 1].page;
  2269. skb_shinfo(skb)->frags[i].page_offset +=
  2270. skb_shinfo(skb)->frags[i - 1].size;
  2271. skb_shinfo(skb)->frags[i].size = rem;
  2272. i++;
  2273. skb_shinfo(skb)->nr_frags = i;
  2274. }
  2275. }
  2276. /* Stamp the time, and sequence number, convert them to network byte order */
  2277. if (pgh) {
  2278. struct timeval timestamp;
  2279. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2280. pgh->seq_num = htonl(pkt_dev->seq_num);
  2281. do_gettimeofday(&timestamp);
  2282. pgh->tv_sec = htonl(timestamp.tv_sec);
  2283. pgh->tv_usec = htonl(timestamp.tv_usec);
  2284. }
  2285. #ifdef CONFIG_XFRM
  2286. if (!process_ipsec(pkt_dev, skb, protocol))
  2287. return NULL;
  2288. #endif
  2289. return skb;
  2290. }
  2291. /*
  2292. * scan_ip6, fmt_ip taken from dietlibc-0.21
  2293. * Author Felix von Leitner <felix-dietlibc@fefe.de>
  2294. *
  2295. * Slightly modified for kernel.
  2296. * Should be candidate for net/ipv4/utils.c
  2297. * --ro
  2298. */
  2299. static unsigned int scan_ip6(const char *s, char ip[16])
  2300. {
  2301. unsigned int i;
  2302. unsigned int len = 0;
  2303. unsigned long u;
  2304. char suffix[16];
  2305. unsigned int prefixlen = 0;
  2306. unsigned int suffixlen = 0;
  2307. __be32 tmp;
  2308. for (i = 0; i < 16; i++)
  2309. ip[i] = 0;
  2310. for (;;) {
  2311. if (*s == ':') {
  2312. len++;
  2313. if (s[1] == ':') { /* Found "::", skip to part 2 */
  2314. s += 2;
  2315. len++;
  2316. break;
  2317. }
  2318. s++;
  2319. }
  2320. {
  2321. char *tmp;
  2322. u = simple_strtoul(s, &tmp, 16);
  2323. i = tmp - s;
  2324. }
  2325. if (!i)
  2326. return 0;
  2327. if (prefixlen == 12 && s[i] == '.') {
  2328. /* the last 4 bytes may be written as IPv4 address */
  2329. tmp = in_aton(s);
  2330. memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
  2331. return i + len;
  2332. }
  2333. ip[prefixlen++] = (u >> 8);
  2334. ip[prefixlen++] = (u & 255);
  2335. s += i;
  2336. len += i;
  2337. if (prefixlen == 16)
  2338. return len;
  2339. }
  2340. /* part 2, after "::" */
  2341. for (;;) {
  2342. if (*s == ':') {
  2343. if (suffixlen == 0)
  2344. break;
  2345. s++;
  2346. len++;
  2347. } else if (suffixlen != 0)
  2348. break;
  2349. {
  2350. char *tmp;
  2351. u = simple_strtol(s, &tmp, 16);
  2352. i = tmp - s;
  2353. }
  2354. if (!i) {
  2355. if (*s)
  2356. len--;
  2357. break;
  2358. }
  2359. if (suffixlen + prefixlen <= 12 && s[i] == '.') {
  2360. tmp = in_aton(s);
  2361. memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
  2362. sizeof(tmp));
  2363. suffixlen += 4;
  2364. len += strlen(s);
  2365. break;
  2366. }
  2367. suffix[suffixlen++] = (u >> 8);
  2368. suffix[suffixlen++] = (u & 255);
  2369. s += i;
  2370. len += i;
  2371. if (prefixlen + suffixlen == 16)
  2372. break;
  2373. }
  2374. for (i = 0; i < suffixlen; i++)
  2375. ip[16 - suffixlen + i] = suffix[i];
  2376. return len;
  2377. }
  2378. static char tohex(char hexdigit)
  2379. {
  2380. return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
  2381. }
  2382. static int fmt_xlong(char *s, unsigned int i)
  2383. {
  2384. char *bak = s;
  2385. *s = tohex((i >> 12) & 0xf);
  2386. if (s != bak || *s != '0')
  2387. ++s;
  2388. *s = tohex((i >> 8) & 0xf);
  2389. if (s != bak || *s != '0')
  2390. ++s;
  2391. *s = tohex((i >> 4) & 0xf);
  2392. if (s != bak || *s != '0')
  2393. ++s;
  2394. *s = tohex(i & 0xf);
  2395. return s - bak + 1;
  2396. }
  2397. static unsigned int fmt_ip6(char *s, const char ip[16])
  2398. {
  2399. unsigned int len;
  2400. unsigned int i;
  2401. unsigned int temp;
  2402. unsigned int compressing;
  2403. int j;
  2404. len = 0;
  2405. compressing = 0;
  2406. for (j = 0; j < 16; j += 2) {
  2407. #ifdef V4MAPPEDPREFIX
  2408. if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
  2409. inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
  2410. temp = strlen(s);
  2411. return len + temp;
  2412. }
  2413. #endif
  2414. temp = ((unsigned long)(unsigned char)ip[j] << 8) +
  2415. (unsigned long)(unsigned char)ip[j + 1];
  2416. if (temp == 0) {
  2417. if (!compressing) {
  2418. compressing = 1;
  2419. if (j == 0) {
  2420. *s++ = ':';
  2421. ++len;
  2422. }
  2423. }
  2424. } else {
  2425. if (compressing) {
  2426. compressing = 0;
  2427. *s++ = ':';
  2428. ++len;
  2429. }
  2430. i = fmt_xlong(s, temp);
  2431. len += i;
  2432. s += i;
  2433. if (j < 14) {
  2434. *s++ = ':';
  2435. ++len;
  2436. }
  2437. }
  2438. }
  2439. if (compressing) {
  2440. *s++ = ':';
  2441. ++len;
  2442. }
  2443. *s = 0;
  2444. return len;
  2445. }
  2446. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  2447. struct pktgen_dev *pkt_dev)
  2448. {
  2449. struct sk_buff *skb = NULL;
  2450. __u8 *eth;
  2451. struct udphdr *udph;
  2452. int datalen;
  2453. struct ipv6hdr *iph;
  2454. struct pktgen_hdr *pgh = NULL;
  2455. __be16 protocol = htons(ETH_P_IPV6);
  2456. __be32 *mpls;
  2457. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2458. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2459. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2460. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2461. if (pkt_dev->nr_labels)
  2462. protocol = htons(ETH_P_MPLS_UC);
  2463. if (pkt_dev->vlan_id != 0xffff)
  2464. protocol = htons(ETH_P_8021Q);
  2465. /* Update any of the values, used when we're incrementing various
  2466. * fields.
  2467. */
  2468. mod_cur_headers(pkt_dev);
  2469. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
  2470. pkt_dev->pkt_overhead, GFP_ATOMIC);
  2471. if (!skb) {
  2472. sprintf(pkt_dev->result, "No memory");
  2473. return NULL;
  2474. }
  2475. skb_reserve(skb, 16);
  2476. /* Reserve for ethernet and IP header */
  2477. eth = (__u8 *) skb_push(skb, 14);
  2478. mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
  2479. if (pkt_dev->nr_labels)
  2480. mpls_push(mpls, pkt_dev);
  2481. if (pkt_dev->vlan_id != 0xffff) {
  2482. if (pkt_dev->svlan_id != 0xffff) {
  2483. svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2484. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2485. pkt_dev->svlan_cfi,
  2486. pkt_dev->svlan_p);
  2487. svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2488. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2489. }
  2490. vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2491. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2492. pkt_dev->vlan_cfi,
  2493. pkt_dev->vlan_p);
  2494. vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2495. *vlan_encapsulated_proto = htons(ETH_P_IPV6);
  2496. }
  2497. skb->network_header = skb->tail;
  2498. skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
  2499. skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
  2500. iph = ipv6_hdr(skb);
  2501. udph = udp_hdr(skb);
  2502. memcpy(eth, pkt_dev->hh, 12);
  2503. *(__be16 *) & eth[12] = protocol;
  2504. /* Eth + IPh + UDPh + mpls */
  2505. datalen = pkt_dev->cur_pkt_size - 14 -
  2506. sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
  2507. pkt_dev->pkt_overhead;
  2508. if (datalen < sizeof(struct pktgen_hdr)) {
  2509. datalen = sizeof(struct pktgen_hdr);
  2510. if (net_ratelimit())
  2511. printk(KERN_INFO "pktgen: increased datalen to %d\n",
  2512. datalen);
  2513. }
  2514. udph->source = htons(pkt_dev->cur_udp_src);
  2515. udph->dest = htons(pkt_dev->cur_udp_dst);
  2516. udph->len = htons(datalen + sizeof(struct udphdr));
  2517. udph->check = 0; /* No checksum */
  2518. *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
  2519. if (pkt_dev->traffic_class) {
  2520. /* Version + traffic class + flow (0) */
  2521. *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
  2522. }
  2523. iph->hop_limit = 32;
  2524. iph->payload_len = htons(sizeof(struct udphdr) + datalen);
  2525. iph->nexthdr = IPPROTO_UDP;
  2526. ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
  2527. ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
  2528. skb->mac_header = (skb->network_header - ETH_HLEN -
  2529. pkt_dev->pkt_overhead);
  2530. skb->protocol = protocol;
  2531. skb->dev = odev;
  2532. skb->pkt_type = PACKET_HOST;
  2533. if (pkt_dev->nfrags <= 0)
  2534. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  2535. else {
  2536. int frags = pkt_dev->nfrags;
  2537. int i;
  2538. pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
  2539. if (frags > MAX_SKB_FRAGS)
  2540. frags = MAX_SKB_FRAGS;
  2541. if (datalen > frags * PAGE_SIZE) {
  2542. skb_put(skb, datalen - frags * PAGE_SIZE);
  2543. datalen = frags * PAGE_SIZE;
  2544. }
  2545. i = 0;
  2546. while (datalen > 0) {
  2547. struct page *page = alloc_pages(GFP_KERNEL, 0);
  2548. skb_shinfo(skb)->frags[i].page = page;
  2549. skb_shinfo(skb)->frags[i].page_offset = 0;
  2550. skb_shinfo(skb)->frags[i].size =
  2551. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  2552. datalen -= skb_shinfo(skb)->frags[i].size;
  2553. skb->len += skb_shinfo(skb)->frags[i].size;
  2554. skb->data_len += skb_shinfo(skb)->frags[i].size;
  2555. i++;
  2556. skb_shinfo(skb)->nr_frags = i;
  2557. }
  2558. while (i < frags) {
  2559. int rem;
  2560. if (i == 0)
  2561. break;
  2562. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  2563. if (rem == 0)
  2564. break;
  2565. skb_shinfo(skb)->frags[i - 1].size -= rem;
  2566. skb_shinfo(skb)->frags[i] =
  2567. skb_shinfo(skb)->frags[i - 1];
  2568. get_page(skb_shinfo(skb)->frags[i].page);
  2569. skb_shinfo(skb)->frags[i].page =
  2570. skb_shinfo(skb)->frags[i - 1].page;
  2571. skb_shinfo(skb)->frags[i].page_offset +=
  2572. skb_shinfo(skb)->frags[i - 1].size;
  2573. skb_shinfo(skb)->frags[i].size = rem;
  2574. i++;
  2575. skb_shinfo(skb)->nr_frags = i;
  2576. }
  2577. }
  2578. /* Stamp the time, and sequence number, convert them to network byte order */
  2579. /* should we update cloned packets too ? */
  2580. if (pgh) {
  2581. struct timeval timestamp;
  2582. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2583. pgh->seq_num = htonl(pkt_dev->seq_num);
  2584. do_gettimeofday(&timestamp);
  2585. pgh->tv_sec = htonl(timestamp.tv_sec);
  2586. pgh->tv_usec = htonl(timestamp.tv_usec);
  2587. }
  2588. /* pkt_dev->seq_num++; FF: you really mean this? */
  2589. return skb;
  2590. }
  2591. static inline struct sk_buff *fill_packet(struct net_device *odev,
  2592. struct pktgen_dev *pkt_dev)
  2593. {
  2594. if (pkt_dev->flags & F_IPV6)
  2595. return fill_packet_ipv6(odev, pkt_dev);
  2596. else
  2597. return fill_packet_ipv4(odev, pkt_dev);
  2598. }
  2599. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  2600. {
  2601. pkt_dev->seq_num = 1;
  2602. pkt_dev->idle_acc = 0;
  2603. pkt_dev->sofar = 0;
  2604. pkt_dev->tx_bytes = 0;
  2605. pkt_dev->errors = 0;
  2606. }
  2607. /* Set up structure for sending pkts, clear counters */
  2608. static void pktgen_run(struct pktgen_thread *t)
  2609. {
  2610. struct pktgen_dev *pkt_dev;
  2611. int started = 0;
  2612. pr_debug("pktgen: entering pktgen_run. %p\n", t);
  2613. if_lock(t);
  2614. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2615. /*
  2616. * setup odev and create initial packet.
  2617. */
  2618. pktgen_setup_inject(pkt_dev);
  2619. if (pkt_dev->odev) {
  2620. pktgen_clear_counters(pkt_dev);
  2621. pkt_dev->running = 1; /* Cranke yeself! */
  2622. pkt_dev->skb = NULL;
  2623. pkt_dev->started_at = getCurUs();
  2624. pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
  2625. pkt_dev->next_tx_ns = 0;
  2626. set_pkt_overhead(pkt_dev);
  2627. strcpy(pkt_dev->result, "Starting");
  2628. started++;
  2629. } else
  2630. strcpy(pkt_dev->result, "Error starting");
  2631. }
  2632. if_unlock(t);
  2633. if (started)
  2634. t->control &= ~(T_STOP);
  2635. }
  2636. static void pktgen_stop_all_threads_ifs(void)
  2637. {
  2638. struct pktgen_thread *t;
  2639. pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
  2640. mutex_lock(&pktgen_thread_lock);
  2641. list_for_each_entry(t, &pktgen_threads, th_list)
  2642. t->control |= T_STOP;
  2643. mutex_unlock(&pktgen_thread_lock);
  2644. }
  2645. static int thread_is_running(struct pktgen_thread *t)
  2646. {
  2647. struct pktgen_dev *pkt_dev;
  2648. int res = 0;
  2649. list_for_each_entry(pkt_dev, &t->if_list, list)
  2650. if (pkt_dev->running) {
  2651. res = 1;
  2652. break;
  2653. }
  2654. return res;
  2655. }
  2656. static int pktgen_wait_thread_run(struct pktgen_thread *t)
  2657. {
  2658. if_lock(t);
  2659. while (thread_is_running(t)) {
  2660. if_unlock(t);
  2661. msleep_interruptible(100);
  2662. if (signal_pending(current))
  2663. goto signal;
  2664. if_lock(t);
  2665. }
  2666. if_unlock(t);
  2667. return 1;
  2668. signal:
  2669. return 0;
  2670. }
  2671. static int pktgen_wait_all_threads_run(void)
  2672. {
  2673. struct pktgen_thread *t;
  2674. int sig = 1;
  2675. mutex_lock(&pktgen_thread_lock);
  2676. list_for_each_entry(t, &pktgen_threads, th_list) {
  2677. sig = pktgen_wait_thread_run(t);
  2678. if (sig == 0)
  2679. break;
  2680. }
  2681. if (sig == 0)
  2682. list_for_each_entry(t, &pktgen_threads, th_list)
  2683. t->control |= (T_STOP);
  2684. mutex_unlock(&pktgen_thread_lock);
  2685. return sig;
  2686. }
  2687. static void pktgen_run_all_threads(void)
  2688. {
  2689. struct pktgen_thread *t;
  2690. pr_debug("pktgen: entering pktgen_run_all_threads.\n");
  2691. mutex_lock(&pktgen_thread_lock);
  2692. list_for_each_entry(t, &pktgen_threads, th_list)
  2693. t->control |= (T_RUN);
  2694. mutex_unlock(&pktgen_thread_lock);
  2695. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  2696. pktgen_wait_all_threads_run();
  2697. }
  2698. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2699. {
  2700. __u64 total_us, bps, mbps, pps, idle;
  2701. char *p = pkt_dev->result;
  2702. total_us = pkt_dev->stopped_at - pkt_dev->started_at;
  2703. idle = pkt_dev->idle_acc;
  2704. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2705. (unsigned long long)total_us,
  2706. (unsigned long long)(total_us - idle),
  2707. (unsigned long long)idle,
  2708. (unsigned long long)pkt_dev->sofar,
  2709. pkt_dev->cur_pkt_size, nr_frags);
  2710. pps = pkt_dev->sofar * USEC_PER_SEC;
  2711. while ((total_us >> 32) != 0) {
  2712. pps >>= 1;
  2713. total_us >>= 1;
  2714. }
  2715. do_div(pps, total_us);
  2716. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2717. mbps = bps;
  2718. do_div(mbps, 1000000);
  2719. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2720. (unsigned long long)pps,
  2721. (unsigned long long)mbps,
  2722. (unsigned long long)bps,
  2723. (unsigned long long)pkt_dev->errors);
  2724. }
  2725. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2726. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2727. {
  2728. int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
  2729. if (!pkt_dev->running) {
  2730. printk(KERN_WARNING "pktgen: interface: %s is already "
  2731. "stopped\n", pkt_dev->odev->name);
  2732. return -EINVAL;
  2733. }
  2734. pkt_dev->stopped_at = getCurUs();
  2735. pkt_dev->running = 0;
  2736. show_results(pkt_dev, nr_frags);
  2737. return 0;
  2738. }
  2739. static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
  2740. {
  2741. struct pktgen_dev *pkt_dev, *best = NULL;
  2742. if_lock(t);
  2743. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2744. if (!pkt_dev->running)
  2745. continue;
  2746. if (best == NULL)
  2747. best = pkt_dev;
  2748. else if (pkt_dev->next_tx_us < best->next_tx_us)
  2749. best = pkt_dev;
  2750. }
  2751. if_unlock(t);
  2752. return best;
  2753. }
  2754. static void pktgen_stop(struct pktgen_thread *t)
  2755. {
  2756. struct pktgen_dev *pkt_dev;
  2757. pr_debug("pktgen: entering pktgen_stop\n");
  2758. if_lock(t);
  2759. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2760. pktgen_stop_device(pkt_dev);
  2761. if (pkt_dev->skb)
  2762. kfree_skb(pkt_dev->skb);
  2763. pkt_dev->skb = NULL;
  2764. }
  2765. if_unlock(t);
  2766. }
  2767. /*
  2768. * one of our devices needs to be removed - find it
  2769. * and remove it
  2770. */
  2771. static void pktgen_rem_one_if(struct pktgen_thread *t)
  2772. {
  2773. struct list_head *q, *n;
  2774. struct pktgen_dev *cur;
  2775. pr_debug("pktgen: entering pktgen_rem_one_if\n");
  2776. if_lock(t);
  2777. list_for_each_safe(q, n, &t->if_list) {
  2778. cur = list_entry(q, struct pktgen_dev, list);
  2779. if (!cur->removal_mark)
  2780. continue;
  2781. if (cur->skb)
  2782. kfree_skb(cur->skb);
  2783. cur->skb = NULL;
  2784. pktgen_remove_device(t, cur);
  2785. break;
  2786. }
  2787. if_unlock(t);
  2788. }
  2789. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2790. {
  2791. struct list_head *q, *n;
  2792. struct pktgen_dev *cur;
  2793. /* Remove all devices, free mem */
  2794. pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
  2795. if_lock(t);
  2796. list_for_each_safe(q, n, &t->if_list) {
  2797. cur = list_entry(q, struct pktgen_dev, list);
  2798. if (cur->skb)
  2799. kfree_skb(cur->skb);
  2800. cur->skb = NULL;
  2801. pktgen_remove_device(t, cur);
  2802. }
  2803. if_unlock(t);
  2804. }
  2805. static void pktgen_rem_thread(struct pktgen_thread *t)
  2806. {
  2807. /* Remove from the thread list */
  2808. remove_proc_entry(t->tsk->comm, pg_proc_dir);
  2809. mutex_lock(&pktgen_thread_lock);
  2810. list_del(&t->th_list);
  2811. mutex_unlock(&pktgen_thread_lock);
  2812. }
  2813. static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2814. {
  2815. struct net_device *odev = NULL;
  2816. __u64 idle_start = 0;
  2817. int ret;
  2818. odev = pkt_dev->odev;
  2819. if (pkt_dev->delay_us || pkt_dev->delay_ns) {
  2820. u64 now;
  2821. now = getCurUs();
  2822. if (now < pkt_dev->next_tx_us)
  2823. spin(pkt_dev, pkt_dev->next_tx_us);
  2824. /* This is max DELAY, this has special meaning of
  2825. * "never transmit"
  2826. */
  2827. if (pkt_dev->delay_us == 0x7FFFFFFF) {
  2828. pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
  2829. pkt_dev->next_tx_ns = pkt_dev->delay_ns;
  2830. goto out;
  2831. }
  2832. }
  2833. if ((netif_queue_stopped(odev) ||
  2834. netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) ||
  2835. need_resched()) {
  2836. idle_start = getCurUs();
  2837. if (!netif_running(odev)) {
  2838. pktgen_stop_device(pkt_dev);
  2839. if (pkt_dev->skb)
  2840. kfree_skb(pkt_dev->skb);
  2841. pkt_dev->skb = NULL;
  2842. goto out;
  2843. }
  2844. if (need_resched())
  2845. schedule();
  2846. pkt_dev->idle_acc += getCurUs() - idle_start;
  2847. if (netif_queue_stopped(odev) ||
  2848. netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
  2849. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2850. pkt_dev->next_tx_ns = 0;
  2851. goto out; /* Try the next interface */
  2852. }
  2853. }
  2854. if (pkt_dev->last_ok || !pkt_dev->skb) {
  2855. if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
  2856. || (!pkt_dev->skb)) {
  2857. /* build a new pkt */
  2858. if (pkt_dev->skb)
  2859. kfree_skb(pkt_dev->skb);
  2860. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2861. if (pkt_dev->skb == NULL) {
  2862. printk(KERN_ERR "pktgen: ERROR: couldn't "
  2863. "allocate skb in fill_packet.\n");
  2864. schedule();
  2865. pkt_dev->clone_count--; /* back out increment, OOM */
  2866. goto out;
  2867. }
  2868. pkt_dev->allocated_skbs++;
  2869. pkt_dev->clone_count = 0; /* reset counter */
  2870. }
  2871. }
  2872. netif_tx_lock_bh(odev);
  2873. if (!netif_queue_stopped(odev) &&
  2874. !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) {
  2875. atomic_inc(&(pkt_dev->skb->users));
  2876. retry_now:
  2877. ret = odev->hard_start_xmit(pkt_dev->skb, odev);
  2878. if (likely(ret == NETDEV_TX_OK)) {
  2879. pkt_dev->last_ok = 1;
  2880. pkt_dev->sofar++;
  2881. pkt_dev->seq_num++;
  2882. pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
  2883. } else if (ret == NETDEV_TX_LOCKED
  2884. && (odev->features & NETIF_F_LLTX)) {
  2885. cpu_relax();
  2886. goto retry_now;
  2887. } else { /* Retry it next time */
  2888. atomic_dec(&(pkt_dev->skb->users));
  2889. if (debug && net_ratelimit())
  2890. printk(KERN_INFO "pktgen: Hard xmit error\n");
  2891. pkt_dev->errors++;
  2892. pkt_dev->last_ok = 0;
  2893. }
  2894. pkt_dev->next_tx_us = getCurUs();
  2895. pkt_dev->next_tx_ns = 0;
  2896. pkt_dev->next_tx_us += pkt_dev->delay_us;
  2897. pkt_dev->next_tx_ns += pkt_dev->delay_ns;
  2898. if (pkt_dev->next_tx_ns > 1000) {
  2899. pkt_dev->next_tx_us++;
  2900. pkt_dev->next_tx_ns -= 1000;
  2901. }
  2902. }
  2903. else { /* Retry it next time */
  2904. pkt_dev->last_ok = 0;
  2905. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2906. pkt_dev->next_tx_ns = 0;
  2907. }
  2908. netif_tx_unlock_bh(odev);
  2909. /* If pkt_dev->count is zero, then run forever */
  2910. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  2911. if (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2912. idle_start = getCurUs();
  2913. while (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2914. if (signal_pending(current)) {
  2915. break;
  2916. }
  2917. schedule();
  2918. }
  2919. pkt_dev->idle_acc += getCurUs() - idle_start;
  2920. }
  2921. /* Done with this */
  2922. pktgen_stop_device(pkt_dev);
  2923. if (pkt_dev->skb)
  2924. kfree_skb(pkt_dev->skb);
  2925. pkt_dev->skb = NULL;
  2926. }
  2927. out:;
  2928. }
  2929. /*
  2930. * Main loop of the thread goes here
  2931. */
  2932. static int pktgen_thread_worker(void *arg)
  2933. {
  2934. DEFINE_WAIT(wait);
  2935. struct pktgen_thread *t = arg;
  2936. struct pktgen_dev *pkt_dev = NULL;
  2937. int cpu = t->cpu;
  2938. u32 max_before_softirq;
  2939. u32 tx_since_softirq = 0;
  2940. BUG_ON(smp_processor_id() != cpu);
  2941. init_waitqueue_head(&t->queue);
  2942. t->pid = current->pid;
  2943. pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
  2944. max_before_softirq = t->max_before_softirq;
  2945. set_current_state(TASK_INTERRUPTIBLE);
  2946. set_freezable();
  2947. while (!kthread_should_stop()) {
  2948. pkt_dev = next_to_run(t);
  2949. if (!pkt_dev &&
  2950. (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
  2951. == 0) {
  2952. prepare_to_wait(&(t->queue), &wait,
  2953. TASK_INTERRUPTIBLE);
  2954. schedule_timeout(HZ / 10);
  2955. finish_wait(&(t->queue), &wait);
  2956. }
  2957. __set_current_state(TASK_RUNNING);
  2958. if (pkt_dev) {
  2959. pktgen_xmit(pkt_dev);
  2960. /*
  2961. * We like to stay RUNNING but must also give
  2962. * others fair share.
  2963. */
  2964. tx_since_softirq += pkt_dev->last_ok;
  2965. if (tx_since_softirq > max_before_softirq) {
  2966. if (local_softirq_pending())
  2967. do_softirq();
  2968. tx_since_softirq = 0;
  2969. }
  2970. }
  2971. if (t->control & T_STOP) {
  2972. pktgen_stop(t);
  2973. t->control &= ~(T_STOP);
  2974. }
  2975. if (t->control & T_RUN) {
  2976. pktgen_run(t);
  2977. t->control &= ~(T_RUN);
  2978. }
  2979. if (t->control & T_REMDEVALL) {
  2980. pktgen_rem_all_ifs(t);
  2981. t->control &= ~(T_REMDEVALL);
  2982. }
  2983. if (t->control & T_REMDEV) {
  2984. pktgen_rem_one_if(t);
  2985. t->control &= ~(T_REMDEV);
  2986. }
  2987. try_to_freeze();
  2988. set_current_state(TASK_INTERRUPTIBLE);
  2989. }
  2990. pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
  2991. pktgen_stop(t);
  2992. pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
  2993. pktgen_rem_all_ifs(t);
  2994. pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
  2995. pktgen_rem_thread(t);
  2996. return 0;
  2997. }
  2998. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  2999. const char *ifname)
  3000. {
  3001. struct pktgen_dev *p, *pkt_dev = NULL;
  3002. if_lock(t);
  3003. list_for_each_entry(p, &t->if_list, list)
  3004. if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
  3005. pkt_dev = p;
  3006. break;
  3007. }
  3008. if_unlock(t);
  3009. pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
  3010. return pkt_dev;
  3011. }
  3012. /*
  3013. * Adds a dev at front of if_list.
  3014. */
  3015. static int add_dev_to_thread(struct pktgen_thread *t,
  3016. struct pktgen_dev *pkt_dev)
  3017. {
  3018. int rv = 0;
  3019. if_lock(t);
  3020. if (pkt_dev->pg_thread) {
  3021. printk(KERN_ERR "pktgen: ERROR: already assigned "
  3022. "to a thread.\n");
  3023. rv = -EBUSY;
  3024. goto out;
  3025. }
  3026. list_add(&pkt_dev->list, &t->if_list);
  3027. pkt_dev->pg_thread = t;
  3028. pkt_dev->running = 0;
  3029. out:
  3030. if_unlock(t);
  3031. return rv;
  3032. }
  3033. /* Called under thread lock */
  3034. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
  3035. {
  3036. struct pktgen_dev *pkt_dev;
  3037. int err;
  3038. /* We don't allow a device to be on several threads */
  3039. pkt_dev = __pktgen_NN_threads(ifname, FIND);
  3040. if (pkt_dev) {
  3041. printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
  3042. return -EBUSY;
  3043. }
  3044. pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
  3045. if (!pkt_dev)
  3046. return -ENOMEM;
  3047. pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
  3048. if (pkt_dev->flows == NULL) {
  3049. kfree(pkt_dev);
  3050. return -ENOMEM;
  3051. }
  3052. memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
  3053. pkt_dev->removal_mark = 0;
  3054. pkt_dev->min_pkt_size = ETH_ZLEN;
  3055. pkt_dev->max_pkt_size = ETH_ZLEN;
  3056. pkt_dev->nfrags = 0;
  3057. pkt_dev->clone_skb = pg_clone_skb_d;
  3058. pkt_dev->delay_us = pg_delay_d / 1000;
  3059. pkt_dev->delay_ns = pg_delay_d % 1000;
  3060. pkt_dev->count = pg_count_d;
  3061. pkt_dev->sofar = 0;
  3062. pkt_dev->udp_src_min = 9; /* sink port */
  3063. pkt_dev->udp_src_max = 9;
  3064. pkt_dev->udp_dst_min = 9;
  3065. pkt_dev->udp_dst_max = 9;
  3066. pkt_dev->vlan_p = 0;
  3067. pkt_dev->vlan_cfi = 0;
  3068. pkt_dev->vlan_id = 0xffff;
  3069. pkt_dev->svlan_p = 0;
  3070. pkt_dev->svlan_cfi = 0;
  3071. pkt_dev->svlan_id = 0xffff;
  3072. err = pktgen_setup_dev(pkt_dev, ifname);
  3073. if (err)
  3074. goto out1;
  3075. pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
  3076. if (!pkt_dev->entry) {
  3077. printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
  3078. PG_PROC_DIR, ifname);
  3079. err = -EINVAL;
  3080. goto out2;
  3081. }
  3082. pkt_dev->entry->proc_fops = &pktgen_if_fops;
  3083. pkt_dev->entry->data = pkt_dev;
  3084. #ifdef CONFIG_XFRM
  3085. pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
  3086. pkt_dev->ipsproto = IPPROTO_ESP;
  3087. #endif
  3088. return add_dev_to_thread(t, pkt_dev);
  3089. out2:
  3090. dev_put(pkt_dev->odev);
  3091. out1:
  3092. #ifdef CONFIG_XFRM
  3093. free_SAs(pkt_dev);
  3094. #endif
  3095. if (pkt_dev->flows)
  3096. vfree(pkt_dev->flows);
  3097. kfree(pkt_dev);
  3098. return err;
  3099. }
  3100. static int __init pktgen_create_thread(int cpu)
  3101. {
  3102. struct pktgen_thread *t;
  3103. struct proc_dir_entry *pe;
  3104. struct task_struct *p;
  3105. t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
  3106. if (!t) {
  3107. printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
  3108. "create new thread.\n");
  3109. return -ENOMEM;
  3110. }
  3111. spin_lock_init(&t->if_lock);
  3112. t->cpu = cpu;
  3113. INIT_LIST_HEAD(&t->if_list);
  3114. list_add_tail(&t->th_list, &pktgen_threads);
  3115. p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
  3116. if (IS_ERR(p)) {
  3117. printk(KERN_ERR "pktgen: kernel_thread() failed "
  3118. "for cpu %d\n", t->cpu);
  3119. list_del(&t->th_list);
  3120. kfree(t);
  3121. return PTR_ERR(p);
  3122. }
  3123. kthread_bind(p, cpu);
  3124. t->tsk = p;
  3125. pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
  3126. if (!pe) {
  3127. printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
  3128. PG_PROC_DIR, t->tsk->comm);
  3129. kthread_stop(p);
  3130. list_del(&t->th_list);
  3131. kfree(t);
  3132. return -EINVAL;
  3133. }
  3134. pe->proc_fops = &pktgen_thread_fops;
  3135. pe->data = t;
  3136. wake_up_process(p);
  3137. return 0;
  3138. }
  3139. /*
  3140. * Removes a device from the thread if_list.
  3141. */
  3142. static void _rem_dev_from_if_list(struct pktgen_thread *t,
  3143. struct pktgen_dev *pkt_dev)
  3144. {
  3145. struct list_head *q, *n;
  3146. struct pktgen_dev *p;
  3147. list_for_each_safe(q, n, &t->if_list) {
  3148. p = list_entry(q, struct pktgen_dev, list);
  3149. if (p == pkt_dev)
  3150. list_del(&p->list);
  3151. }
  3152. }
  3153. static int pktgen_remove_device(struct pktgen_thread *t,
  3154. struct pktgen_dev *pkt_dev)
  3155. {
  3156. pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
  3157. if (pkt_dev->running) {
  3158. printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
  3159. "running interface, stopping it now.\n");
  3160. pktgen_stop_device(pkt_dev);
  3161. }
  3162. /* Dis-associate from the interface */
  3163. if (pkt_dev->odev) {
  3164. dev_put(pkt_dev->odev);
  3165. pkt_dev->odev = NULL;
  3166. }
  3167. /* And update the thread if_list */
  3168. _rem_dev_from_if_list(t, pkt_dev);
  3169. if (pkt_dev->entry)
  3170. remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
  3171. #ifdef CONFIG_XFRM
  3172. free_SAs(pkt_dev);
  3173. #endif
  3174. if (pkt_dev->flows)
  3175. vfree(pkt_dev->flows);
  3176. kfree(pkt_dev);
  3177. return 0;
  3178. }
  3179. static int __init pg_init(void)
  3180. {
  3181. int cpu;
  3182. struct proc_dir_entry *pe;
  3183. printk(KERN_INFO "%s", version);
  3184. pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
  3185. if (!pg_proc_dir)
  3186. return -ENODEV;
  3187. pg_proc_dir->owner = THIS_MODULE;
  3188. pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
  3189. if (pe == NULL) {
  3190. printk(KERN_ERR "pktgen: ERROR: cannot create %s "
  3191. "procfs entry.\n", PGCTRL);
  3192. proc_net_remove(PG_PROC_DIR);
  3193. return -EINVAL;
  3194. }
  3195. pe->proc_fops = &pktgen_fops;
  3196. pe->data = NULL;
  3197. /* Register us to receive netdevice events */
  3198. register_netdevice_notifier(&pktgen_notifier_block);
  3199. for_each_online_cpu(cpu) {
  3200. int err;
  3201. err = pktgen_create_thread(cpu);
  3202. if (err)
  3203. printk(KERN_WARNING "pktgen: WARNING: Cannot create "
  3204. "thread for cpu %d (%d)\n", cpu, err);
  3205. }
  3206. if (list_empty(&pktgen_threads)) {
  3207. printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
  3208. "all threads\n");
  3209. unregister_netdevice_notifier(&pktgen_notifier_block);
  3210. remove_proc_entry(PGCTRL, pg_proc_dir);
  3211. proc_net_remove(PG_PROC_DIR);
  3212. return -ENODEV;
  3213. }
  3214. return 0;
  3215. }
  3216. static void __exit pg_cleanup(void)
  3217. {
  3218. struct pktgen_thread *t;
  3219. struct list_head *q, *n;
  3220. wait_queue_head_t queue;
  3221. init_waitqueue_head(&queue);
  3222. /* Stop all interfaces & threads */
  3223. list_for_each_safe(q, n, &pktgen_threads) {
  3224. t = list_entry(q, struct pktgen_thread, th_list);
  3225. kthread_stop(t->tsk);
  3226. kfree(t);
  3227. }
  3228. /* Un-register us from receiving netdevice events */
  3229. unregister_netdevice_notifier(&pktgen_notifier_block);
  3230. /* Clean up proc file system */
  3231. remove_proc_entry(PGCTRL, pg_proc_dir);
  3232. proc_net_remove(PG_PROC_DIR);
  3233. }
  3234. module_init(pg_init);
  3235. module_exit(pg_cleanup);
  3236. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
  3237. MODULE_DESCRIPTION("Packet Generator tool");
  3238. MODULE_LICENSE("GPL");
  3239. module_param(pg_count_d, int, 0);
  3240. module_param(pg_delay_d, int, 0);
  3241. module_param(pg_clone_skb_d, int, 0);
  3242. module_param(debug, int, 0);