pktgen.c 93 KB

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