pktgen.c 90 KB

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