pktgen.c 92 KB

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