pktgen.c 90 KB

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