pktgen.c 95 KB

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