pktgen.c 94 KB

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