pktgen.c 94 KB

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