pktgen.c 97 KB

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