pktgen.c 94 KB

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