pktgen.c 91 KB

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