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