pktgen.c 92 KB

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