pktgen.c 95 KB

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