pktgen.c 91 KB

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