pktgen.c 90 KB

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