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