pktgen.c 91 KB

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