pktgen.c 93 KB

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