pktgen.c 93 KB

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