pktgen.c 93 KB

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