pktgen.c 93 KB

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