pktgen.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724
  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. ret = pktgen_add_device(t, f);
  1505. mutex_unlock(&pktgen_thread_lock);
  1506. if (!ret) {
  1507. ret = count;
  1508. sprintf(pg_result, "OK: add_device=%s", f);
  1509. } else
  1510. sprintf(pg_result, "ERROR: can not add device %s", f);
  1511. goto out;
  1512. }
  1513. if (!strcmp(name, "rem_device_all")) {
  1514. mutex_lock(&pktgen_thread_lock);
  1515. t->control |= T_REMDEVALL;
  1516. mutex_unlock(&pktgen_thread_lock);
  1517. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  1518. ret = count;
  1519. sprintf(pg_result, "OK: rem_device_all");
  1520. goto out;
  1521. }
  1522. if (!strcmp(name, "max_before_softirq")) {
  1523. sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
  1524. ret = count;
  1525. goto out;
  1526. }
  1527. ret = -EINVAL;
  1528. out:
  1529. return ret;
  1530. }
  1531. static int pktgen_thread_open(struct inode *inode, struct file *file)
  1532. {
  1533. return single_open(file, pktgen_thread_show, PDE(inode)->data);
  1534. }
  1535. static const struct file_operations pktgen_thread_fops = {
  1536. .owner = THIS_MODULE,
  1537. .open = pktgen_thread_open,
  1538. .read = seq_read,
  1539. .llseek = seq_lseek,
  1540. .write = pktgen_thread_write,
  1541. .release = single_release,
  1542. };
  1543. /* Think find or remove for NN */
  1544. static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
  1545. const char *ifname, int remove)
  1546. {
  1547. struct pktgen_thread *t;
  1548. struct pktgen_dev *pkt_dev = NULL;
  1549. bool exact = (remove == FIND);
  1550. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  1551. pkt_dev = pktgen_find_dev(t, ifname, exact);
  1552. if (pkt_dev) {
  1553. if (remove) {
  1554. if_lock(t);
  1555. pkt_dev->removal_mark = 1;
  1556. t->control |= T_REMDEV;
  1557. if_unlock(t);
  1558. }
  1559. break;
  1560. }
  1561. }
  1562. return pkt_dev;
  1563. }
  1564. /*
  1565. * mark a device for removal
  1566. */
  1567. static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
  1568. {
  1569. struct pktgen_dev *pkt_dev = NULL;
  1570. const int max_tries = 10, msec_per_try = 125;
  1571. int i = 0;
  1572. mutex_lock(&pktgen_thread_lock);
  1573. pr_debug("%s: marking %s for removal\n", __func__, ifname);
  1574. while (1) {
  1575. pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
  1576. if (pkt_dev == NULL)
  1577. break; /* success */
  1578. mutex_unlock(&pktgen_thread_lock);
  1579. pr_debug("%s: waiting for %s to disappear....\n",
  1580. __func__, ifname);
  1581. schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
  1582. mutex_lock(&pktgen_thread_lock);
  1583. if (++i >= max_tries) {
  1584. pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
  1585. __func__, msec_per_try * i, ifname);
  1586. break;
  1587. }
  1588. }
  1589. mutex_unlock(&pktgen_thread_lock);
  1590. }
  1591. static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
  1592. {
  1593. struct pktgen_thread *t;
  1594. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  1595. struct pktgen_dev *pkt_dev;
  1596. list_for_each_entry(pkt_dev, &t->if_list, list) {
  1597. if (pkt_dev->odev != dev)
  1598. continue;
  1599. remove_proc_entry(pkt_dev->entry->name, pn->proc_dir);
  1600. pkt_dev->entry = proc_create_data(dev->name, 0600,
  1601. pn->proc_dir,
  1602. &pktgen_if_fops,
  1603. pkt_dev);
  1604. if (!pkt_dev->entry)
  1605. pr_err("can't move proc entry for '%s'\n",
  1606. dev->name);
  1607. break;
  1608. }
  1609. }
  1610. }
  1611. static int pktgen_device_event(struct notifier_block *unused,
  1612. unsigned long event, void *ptr)
  1613. {
  1614. struct net_device *dev = ptr;
  1615. struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
  1616. if (pn->pktgen_exiting)
  1617. return NOTIFY_DONE;
  1618. /* It is OK that we do not hold the group lock right now,
  1619. * as we run under the RTNL lock.
  1620. */
  1621. switch (event) {
  1622. case NETDEV_CHANGENAME:
  1623. pktgen_change_name(pn, dev);
  1624. break;
  1625. case NETDEV_UNREGISTER:
  1626. pktgen_mark_device(pn, dev->name);
  1627. break;
  1628. }
  1629. return NOTIFY_DONE;
  1630. }
  1631. static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
  1632. struct pktgen_dev *pkt_dev,
  1633. const char *ifname)
  1634. {
  1635. char b[IFNAMSIZ+5];
  1636. int i;
  1637. for (i = 0; ifname[i] != '@'; i++) {
  1638. if (i == IFNAMSIZ)
  1639. break;
  1640. b[i] = ifname[i];
  1641. }
  1642. b[i] = 0;
  1643. return dev_get_by_name(pn->net, b);
  1644. }
  1645. /* Associate pktgen_dev with a device. */
  1646. static int pktgen_setup_dev(const struct pktgen_net *pn,
  1647. struct pktgen_dev *pkt_dev, const char *ifname)
  1648. {
  1649. struct net_device *odev;
  1650. int err;
  1651. /* Clean old setups */
  1652. if (pkt_dev->odev) {
  1653. dev_put(pkt_dev->odev);
  1654. pkt_dev->odev = NULL;
  1655. }
  1656. odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
  1657. if (!odev) {
  1658. pr_err("no such netdevice: \"%s\"\n", ifname);
  1659. return -ENODEV;
  1660. }
  1661. if (odev->type != ARPHRD_ETHER) {
  1662. pr_err("not an ethernet device: \"%s\"\n", ifname);
  1663. err = -EINVAL;
  1664. } else if (!netif_running(odev)) {
  1665. pr_err("device is down: \"%s\"\n", ifname);
  1666. err = -ENETDOWN;
  1667. } else {
  1668. pkt_dev->odev = odev;
  1669. return 0;
  1670. }
  1671. dev_put(odev);
  1672. return err;
  1673. }
  1674. /* Read pkt_dev from the interface and set up internal pktgen_dev
  1675. * structure to have the right information to create/send packets
  1676. */
  1677. static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
  1678. {
  1679. int ntxq;
  1680. if (!pkt_dev->odev) {
  1681. pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
  1682. sprintf(pkt_dev->result,
  1683. "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
  1684. return;
  1685. }
  1686. /* make sure that we don't pick a non-existing transmit queue */
  1687. ntxq = pkt_dev->odev->real_num_tx_queues;
  1688. if (ntxq <= pkt_dev->queue_map_min) {
  1689. pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
  1690. pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
  1691. pkt_dev->odevname);
  1692. pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
  1693. }
  1694. if (pkt_dev->queue_map_max >= ntxq) {
  1695. pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
  1696. pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
  1697. pkt_dev->odevname);
  1698. pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
  1699. }
  1700. /* Default to the interface's mac if not explicitly set. */
  1701. if (is_zero_ether_addr(pkt_dev->src_mac))
  1702. memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
  1703. /* Set up Dest MAC */
  1704. memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
  1705. if (pkt_dev->flags & F_IPV6) {
  1706. int i, set = 0, err = 1;
  1707. struct inet6_dev *idev;
  1708. if (pkt_dev->min_pkt_size == 0) {
  1709. pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
  1710. + sizeof(struct udphdr)
  1711. + sizeof(struct pktgen_hdr)
  1712. + pkt_dev->pkt_overhead;
  1713. }
  1714. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  1715. if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
  1716. set = 1;
  1717. break;
  1718. }
  1719. if (!set) {
  1720. /*
  1721. * Use linklevel address if unconfigured.
  1722. *
  1723. * use ipv6_get_lladdr if/when it's get exported
  1724. */
  1725. rcu_read_lock();
  1726. idev = __in6_dev_get(pkt_dev->odev);
  1727. if (idev) {
  1728. struct inet6_ifaddr *ifp;
  1729. read_lock_bh(&idev->lock);
  1730. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  1731. if ((ifp->scope & IFA_LINK) &&
  1732. !(ifp->flags & IFA_F_TENTATIVE)) {
  1733. pkt_dev->cur_in6_saddr = ifp->addr;
  1734. err = 0;
  1735. break;
  1736. }
  1737. }
  1738. read_unlock_bh(&idev->lock);
  1739. }
  1740. rcu_read_unlock();
  1741. if (err)
  1742. pr_err("ERROR: IPv6 link address not available\n");
  1743. }
  1744. } else {
  1745. if (pkt_dev->min_pkt_size == 0) {
  1746. pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
  1747. + sizeof(struct udphdr)
  1748. + sizeof(struct pktgen_hdr)
  1749. + pkt_dev->pkt_overhead;
  1750. }
  1751. pkt_dev->saddr_min = 0;
  1752. pkt_dev->saddr_max = 0;
  1753. if (strlen(pkt_dev->src_min) == 0) {
  1754. struct in_device *in_dev;
  1755. rcu_read_lock();
  1756. in_dev = __in_dev_get_rcu(pkt_dev->odev);
  1757. if (in_dev) {
  1758. if (in_dev->ifa_list) {
  1759. pkt_dev->saddr_min =
  1760. in_dev->ifa_list->ifa_address;
  1761. pkt_dev->saddr_max = pkt_dev->saddr_min;
  1762. }
  1763. }
  1764. rcu_read_unlock();
  1765. } else {
  1766. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1767. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1768. }
  1769. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1770. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1771. }
  1772. /* Initialize current values. */
  1773. pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
  1774. if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
  1775. pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
  1776. pkt_dev->cur_dst_mac_offset = 0;
  1777. pkt_dev->cur_src_mac_offset = 0;
  1778. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1779. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1780. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1781. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1782. pkt_dev->nflows = 0;
  1783. }
  1784. static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
  1785. {
  1786. ktime_t start_time, end_time;
  1787. s64 remaining;
  1788. struct hrtimer_sleeper t;
  1789. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1790. hrtimer_set_expires(&t.timer, spin_until);
  1791. remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
  1792. if (remaining <= 0) {
  1793. pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
  1794. return;
  1795. }
  1796. start_time = ktime_get();
  1797. if (remaining < 100000) {
  1798. /* for small delays (<100us), just loop until limit is reached */
  1799. do {
  1800. end_time = ktime_get();
  1801. } while (ktime_compare(end_time, spin_until) < 0);
  1802. } else {
  1803. /* see do_nanosleep */
  1804. hrtimer_init_sleeper(&t, current);
  1805. do {
  1806. set_current_state(TASK_INTERRUPTIBLE);
  1807. hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
  1808. if (!hrtimer_active(&t.timer))
  1809. t.task = NULL;
  1810. if (likely(t.task))
  1811. schedule();
  1812. hrtimer_cancel(&t.timer);
  1813. } while (t.task && pkt_dev->running && !signal_pending(current));
  1814. __set_current_state(TASK_RUNNING);
  1815. end_time = ktime_get();
  1816. }
  1817. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
  1818. pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
  1819. }
  1820. static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
  1821. {
  1822. pkt_dev->pkt_overhead = 0;
  1823. pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
  1824. pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
  1825. pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
  1826. }
  1827. static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
  1828. {
  1829. return !!(pkt_dev->flows[flow].flags & F_INIT);
  1830. }
  1831. static inline int f_pick(struct pktgen_dev *pkt_dev)
  1832. {
  1833. int flow = pkt_dev->curfl;
  1834. if (pkt_dev->flags & F_FLOW_SEQ) {
  1835. if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
  1836. /* reset time */
  1837. pkt_dev->flows[flow].count = 0;
  1838. pkt_dev->flows[flow].flags = 0;
  1839. pkt_dev->curfl += 1;
  1840. if (pkt_dev->curfl >= pkt_dev->cflows)
  1841. pkt_dev->curfl = 0; /*reset */
  1842. }
  1843. } else {
  1844. flow = random32() % pkt_dev->cflows;
  1845. pkt_dev->curfl = flow;
  1846. if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
  1847. pkt_dev->flows[flow].count = 0;
  1848. pkt_dev->flows[flow].flags = 0;
  1849. }
  1850. }
  1851. return pkt_dev->curfl;
  1852. }
  1853. #ifdef CONFIG_XFRM
  1854. /* If there was already an IPSEC SA, we keep it as is, else
  1855. * we go look for it ...
  1856. */
  1857. #define DUMMY_MARK 0
  1858. static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
  1859. {
  1860. struct xfrm_state *x = pkt_dev->flows[flow].x;
  1861. struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
  1862. if (!x) {
  1863. /*slow path: we dont already have xfrm_state*/
  1864. x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
  1865. (xfrm_address_t *)&pkt_dev->cur_daddr,
  1866. (xfrm_address_t *)&pkt_dev->cur_saddr,
  1867. AF_INET,
  1868. pkt_dev->ipsmode,
  1869. pkt_dev->ipsproto, 0);
  1870. if (x) {
  1871. pkt_dev->flows[flow].x = x;
  1872. set_pkt_overhead(pkt_dev);
  1873. pkt_dev->pkt_overhead += x->props.header_len;
  1874. }
  1875. }
  1876. }
  1877. #endif
  1878. static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
  1879. {
  1880. if (pkt_dev->flags & F_QUEUE_MAP_CPU)
  1881. pkt_dev->cur_queue_map = smp_processor_id();
  1882. else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
  1883. __u16 t;
  1884. if (pkt_dev->flags & F_QUEUE_MAP_RND) {
  1885. t = random32() %
  1886. (pkt_dev->queue_map_max -
  1887. pkt_dev->queue_map_min + 1)
  1888. + pkt_dev->queue_map_min;
  1889. } else {
  1890. t = pkt_dev->cur_queue_map + 1;
  1891. if (t > pkt_dev->queue_map_max)
  1892. t = pkt_dev->queue_map_min;
  1893. }
  1894. pkt_dev->cur_queue_map = t;
  1895. }
  1896. pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
  1897. }
  1898. /* Increment/randomize headers according to flags and current values
  1899. * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  1900. */
  1901. static void mod_cur_headers(struct pktgen_dev *pkt_dev)
  1902. {
  1903. __u32 imn;
  1904. __u32 imx;
  1905. int flow = 0;
  1906. if (pkt_dev->cflows)
  1907. flow = f_pick(pkt_dev);
  1908. /* Deal with source MAC */
  1909. if (pkt_dev->src_mac_count > 1) {
  1910. __u32 mc;
  1911. __u32 tmp;
  1912. if (pkt_dev->flags & F_MACSRC_RND)
  1913. mc = random32() % pkt_dev->src_mac_count;
  1914. else {
  1915. mc = pkt_dev->cur_src_mac_offset++;
  1916. if (pkt_dev->cur_src_mac_offset >=
  1917. pkt_dev->src_mac_count)
  1918. pkt_dev->cur_src_mac_offset = 0;
  1919. }
  1920. tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
  1921. pkt_dev->hh[11] = tmp;
  1922. tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  1923. pkt_dev->hh[10] = tmp;
  1924. tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  1925. pkt_dev->hh[9] = tmp;
  1926. tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  1927. pkt_dev->hh[8] = tmp;
  1928. tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
  1929. pkt_dev->hh[7] = tmp;
  1930. }
  1931. /* Deal with Destination MAC */
  1932. if (pkt_dev->dst_mac_count > 1) {
  1933. __u32 mc;
  1934. __u32 tmp;
  1935. if (pkt_dev->flags & F_MACDST_RND)
  1936. mc = random32() % pkt_dev->dst_mac_count;
  1937. else {
  1938. mc = pkt_dev->cur_dst_mac_offset++;
  1939. if (pkt_dev->cur_dst_mac_offset >=
  1940. pkt_dev->dst_mac_count) {
  1941. pkt_dev->cur_dst_mac_offset = 0;
  1942. }
  1943. }
  1944. tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
  1945. pkt_dev->hh[5] = tmp;
  1946. tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  1947. pkt_dev->hh[4] = tmp;
  1948. tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  1949. pkt_dev->hh[3] = tmp;
  1950. tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  1951. pkt_dev->hh[2] = tmp;
  1952. tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
  1953. pkt_dev->hh[1] = tmp;
  1954. }
  1955. if (pkt_dev->flags & F_MPLS_RND) {
  1956. unsigned int i;
  1957. for (i = 0; i < pkt_dev->nr_labels; i++)
  1958. if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
  1959. pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
  1960. ((__force __be32)random32() &
  1961. htonl(0x000fffff));
  1962. }
  1963. if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
  1964. pkt_dev->vlan_id = random32() & (4096-1);
  1965. }
  1966. if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
  1967. pkt_dev->svlan_id = random32() & (4096 - 1);
  1968. }
  1969. if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
  1970. if (pkt_dev->flags & F_UDPSRC_RND)
  1971. pkt_dev->cur_udp_src = random32() %
  1972. (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
  1973. + pkt_dev->udp_src_min;
  1974. else {
  1975. pkt_dev->cur_udp_src++;
  1976. if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
  1977. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1978. }
  1979. }
  1980. if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
  1981. if (pkt_dev->flags & F_UDPDST_RND) {
  1982. pkt_dev->cur_udp_dst = random32() %
  1983. (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
  1984. + pkt_dev->udp_dst_min;
  1985. } else {
  1986. pkt_dev->cur_udp_dst++;
  1987. if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
  1988. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1989. }
  1990. }
  1991. if (!(pkt_dev->flags & F_IPV6)) {
  1992. imn = ntohl(pkt_dev->saddr_min);
  1993. imx = ntohl(pkt_dev->saddr_max);
  1994. if (imn < imx) {
  1995. __u32 t;
  1996. if (pkt_dev->flags & F_IPSRC_RND)
  1997. t = random32() % (imx - imn) + imn;
  1998. else {
  1999. t = ntohl(pkt_dev->cur_saddr);
  2000. t++;
  2001. if (t > imx)
  2002. t = imn;
  2003. }
  2004. pkt_dev->cur_saddr = htonl(t);
  2005. }
  2006. if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
  2007. pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
  2008. } else {
  2009. imn = ntohl(pkt_dev->daddr_min);
  2010. imx = ntohl(pkt_dev->daddr_max);
  2011. if (imn < imx) {
  2012. __u32 t;
  2013. __be32 s;
  2014. if (pkt_dev->flags & F_IPDST_RND) {
  2015. t = random32() % (imx - imn) + imn;
  2016. s = htonl(t);
  2017. while (ipv4_is_loopback(s) ||
  2018. ipv4_is_multicast(s) ||
  2019. ipv4_is_lbcast(s) ||
  2020. ipv4_is_zeronet(s) ||
  2021. ipv4_is_local_multicast(s)) {
  2022. t = random32() % (imx - imn) + imn;
  2023. s = htonl(t);
  2024. }
  2025. pkt_dev->cur_daddr = s;
  2026. } else {
  2027. t = ntohl(pkt_dev->cur_daddr);
  2028. t++;
  2029. if (t > imx) {
  2030. t = imn;
  2031. }
  2032. pkt_dev->cur_daddr = htonl(t);
  2033. }
  2034. }
  2035. if (pkt_dev->cflows) {
  2036. pkt_dev->flows[flow].flags |= F_INIT;
  2037. pkt_dev->flows[flow].cur_daddr =
  2038. pkt_dev->cur_daddr;
  2039. #ifdef CONFIG_XFRM
  2040. if (pkt_dev->flags & F_IPSEC_ON)
  2041. get_ipsec_sa(pkt_dev, flow);
  2042. #endif
  2043. pkt_dev->nflows++;
  2044. }
  2045. }
  2046. } else { /* IPV6 * */
  2047. if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
  2048. int i;
  2049. /* Only random destinations yet */
  2050. for (i = 0; i < 4; i++) {
  2051. pkt_dev->cur_in6_daddr.s6_addr32[i] =
  2052. (((__force __be32)random32() |
  2053. pkt_dev->min_in6_daddr.s6_addr32[i]) &
  2054. pkt_dev->max_in6_daddr.s6_addr32[i]);
  2055. }
  2056. }
  2057. }
  2058. if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
  2059. __u32 t;
  2060. if (pkt_dev->flags & F_TXSIZE_RND) {
  2061. t = random32() %
  2062. (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
  2063. + pkt_dev->min_pkt_size;
  2064. } else {
  2065. t = pkt_dev->cur_pkt_size + 1;
  2066. if (t > pkt_dev->max_pkt_size)
  2067. t = pkt_dev->min_pkt_size;
  2068. }
  2069. pkt_dev->cur_pkt_size = t;
  2070. }
  2071. set_cur_queue_map(pkt_dev);
  2072. pkt_dev->flows[flow].count++;
  2073. }
  2074. #ifdef CONFIG_XFRM
  2075. static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
  2076. {
  2077. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2078. int err = 0;
  2079. if (!x)
  2080. return 0;
  2081. /* XXX: we dont support tunnel mode for now until
  2082. * we resolve the dst issue */
  2083. if (x->props.mode != XFRM_MODE_TRANSPORT)
  2084. return 0;
  2085. spin_lock(&x->lock);
  2086. err = x->outer_mode->output(x, skb);
  2087. if (err)
  2088. goto error;
  2089. err = x->type->output(x, skb);
  2090. if (err)
  2091. goto error;
  2092. x->curlft.bytes += skb->len;
  2093. x->curlft.packets++;
  2094. error:
  2095. spin_unlock(&x->lock);
  2096. return err;
  2097. }
  2098. static void free_SAs(struct pktgen_dev *pkt_dev)
  2099. {
  2100. if (pkt_dev->cflows) {
  2101. /* let go of the SAs if we have them */
  2102. int i;
  2103. for (i = 0; i < pkt_dev->cflows; i++) {
  2104. struct xfrm_state *x = pkt_dev->flows[i].x;
  2105. if (x) {
  2106. xfrm_state_put(x);
  2107. pkt_dev->flows[i].x = NULL;
  2108. }
  2109. }
  2110. }
  2111. }
  2112. static int process_ipsec(struct pktgen_dev *pkt_dev,
  2113. struct sk_buff *skb, __be16 protocol)
  2114. {
  2115. if (pkt_dev->flags & F_IPSEC_ON) {
  2116. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2117. int nhead = 0;
  2118. if (x) {
  2119. int ret;
  2120. __u8 *eth;
  2121. nhead = x->props.header_len - skb_headroom(skb);
  2122. if (nhead > 0) {
  2123. ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
  2124. if (ret < 0) {
  2125. pr_err("Error expanding ipsec packet %d\n",
  2126. ret);
  2127. goto err;
  2128. }
  2129. }
  2130. /* ipsec is not expecting ll header */
  2131. skb_pull(skb, ETH_HLEN);
  2132. ret = pktgen_output_ipsec(skb, pkt_dev);
  2133. if (ret) {
  2134. pr_err("Error creating ipsec packet %d\n", ret);
  2135. goto err;
  2136. }
  2137. /* restore ll */
  2138. eth = (__u8 *) skb_push(skb, ETH_HLEN);
  2139. memcpy(eth, pkt_dev->hh, 12);
  2140. *(u16 *) &eth[12] = protocol;
  2141. }
  2142. }
  2143. return 1;
  2144. err:
  2145. kfree_skb(skb);
  2146. return 0;
  2147. }
  2148. #endif
  2149. static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
  2150. {
  2151. unsigned int i;
  2152. for (i = 0; i < pkt_dev->nr_labels; i++)
  2153. *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
  2154. mpls--;
  2155. *mpls |= MPLS_STACK_BOTTOM;
  2156. }
  2157. static inline __be16 build_tci(unsigned int id, unsigned int cfi,
  2158. unsigned int prio)
  2159. {
  2160. return htons(id | (cfi << 12) | (prio << 13));
  2161. }
  2162. static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
  2163. int datalen)
  2164. {
  2165. struct timeval timestamp;
  2166. struct pktgen_hdr *pgh;
  2167. pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
  2168. datalen -= sizeof(*pgh);
  2169. if (pkt_dev->nfrags <= 0) {
  2170. memset(skb_put(skb, datalen), 0, datalen);
  2171. } else {
  2172. int frags = pkt_dev->nfrags;
  2173. int i, len;
  2174. int frag_len;
  2175. if (frags > MAX_SKB_FRAGS)
  2176. frags = MAX_SKB_FRAGS;
  2177. len = datalen - frags * PAGE_SIZE;
  2178. if (len > 0) {
  2179. memset(skb_put(skb, len), 0, len);
  2180. datalen = frags * PAGE_SIZE;
  2181. }
  2182. i = 0;
  2183. frag_len = (datalen/frags) < PAGE_SIZE ?
  2184. (datalen/frags) : PAGE_SIZE;
  2185. while (datalen > 0) {
  2186. if (unlikely(!pkt_dev->page)) {
  2187. int node = numa_node_id();
  2188. if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
  2189. node = pkt_dev->node;
  2190. pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2191. if (!pkt_dev->page)
  2192. break;
  2193. }
  2194. get_page(pkt_dev->page);
  2195. skb_frag_set_page(skb, i, pkt_dev->page);
  2196. skb_shinfo(skb)->frags[i].page_offset = 0;
  2197. /*last fragment, fill rest of data*/
  2198. if (i == (frags - 1))
  2199. skb_frag_size_set(&skb_shinfo(skb)->frags[i],
  2200. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
  2201. else
  2202. skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
  2203. datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2204. skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2205. skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2206. i++;
  2207. skb_shinfo(skb)->nr_frags = i;
  2208. }
  2209. }
  2210. /* Stamp the time, and sequence number,
  2211. * convert them to network byte order
  2212. */
  2213. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2214. pgh->seq_num = htonl(pkt_dev->seq_num);
  2215. do_gettimeofday(&timestamp);
  2216. pgh->tv_sec = htonl(timestamp.tv_sec);
  2217. pgh->tv_usec = htonl(timestamp.tv_usec);
  2218. }
  2219. static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
  2220. struct pktgen_dev *pkt_dev)
  2221. {
  2222. struct sk_buff *skb = NULL;
  2223. __u8 *eth;
  2224. struct udphdr *udph;
  2225. int datalen, iplen;
  2226. struct iphdr *iph;
  2227. __be16 protocol = htons(ETH_P_IP);
  2228. __be32 *mpls;
  2229. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2230. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2231. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2232. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2233. u16 queue_map;
  2234. if (pkt_dev->nr_labels)
  2235. protocol = htons(ETH_P_MPLS_UC);
  2236. if (pkt_dev->vlan_id != 0xffff)
  2237. protocol = htons(ETH_P_8021Q);
  2238. /* Update any of the values, used when we're incrementing various
  2239. * fields.
  2240. */
  2241. mod_cur_headers(pkt_dev);
  2242. queue_map = pkt_dev->cur_queue_map;
  2243. datalen = (odev->hard_header_len + 16) & ~0xf;
  2244. if (pkt_dev->flags & F_NODE) {
  2245. int node;
  2246. if (pkt_dev->node >= 0)
  2247. node = pkt_dev->node;
  2248. else
  2249. node = numa_node_id();
  2250. skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
  2251. + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
  2252. if (likely(skb)) {
  2253. skb_reserve(skb, NET_SKB_PAD);
  2254. skb->dev = odev;
  2255. }
  2256. }
  2257. else
  2258. skb = __netdev_alloc_skb(odev,
  2259. pkt_dev->cur_pkt_size + 64
  2260. + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
  2261. if (!skb) {
  2262. sprintf(pkt_dev->result, "No memory");
  2263. return NULL;
  2264. }
  2265. prefetchw(skb->data);
  2266. skb_reserve(skb, datalen);
  2267. /* Reserve for ethernet and IP header */
  2268. eth = (__u8 *) skb_push(skb, 14);
  2269. mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
  2270. if (pkt_dev->nr_labels)
  2271. mpls_push(mpls, pkt_dev);
  2272. if (pkt_dev->vlan_id != 0xffff) {
  2273. if (pkt_dev->svlan_id != 0xffff) {
  2274. svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2275. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2276. pkt_dev->svlan_cfi,
  2277. pkt_dev->svlan_p);
  2278. svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2279. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2280. }
  2281. vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2282. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2283. pkt_dev->vlan_cfi,
  2284. pkt_dev->vlan_p);
  2285. vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2286. *vlan_encapsulated_proto = htons(ETH_P_IP);
  2287. }
  2288. skb->network_header = skb->tail;
  2289. skb->transport_header = skb->network_header + sizeof(struct iphdr);
  2290. skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
  2291. skb_set_queue_mapping(skb, queue_map);
  2292. skb->priority = pkt_dev->skb_priority;
  2293. iph = ip_hdr(skb);
  2294. udph = udp_hdr(skb);
  2295. memcpy(eth, pkt_dev->hh, 12);
  2296. *(__be16 *) & eth[12] = protocol;
  2297. /* Eth + IPh + UDPh + mpls */
  2298. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
  2299. pkt_dev->pkt_overhead;
  2300. if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
  2301. datalen = sizeof(struct pktgen_hdr);
  2302. udph->source = htons(pkt_dev->cur_udp_src);
  2303. udph->dest = htons(pkt_dev->cur_udp_dst);
  2304. udph->len = htons(datalen + 8); /* DATA + udphdr */
  2305. udph->check = 0; /* No checksum */
  2306. iph->ihl = 5;
  2307. iph->version = 4;
  2308. iph->ttl = 32;
  2309. iph->tos = pkt_dev->tos;
  2310. iph->protocol = IPPROTO_UDP; /* UDP */
  2311. iph->saddr = pkt_dev->cur_saddr;
  2312. iph->daddr = pkt_dev->cur_daddr;
  2313. iph->id = htons(pkt_dev->ip_id);
  2314. pkt_dev->ip_id++;
  2315. iph->frag_off = 0;
  2316. iplen = 20 + 8 + datalen;
  2317. iph->tot_len = htons(iplen);
  2318. iph->check = 0;
  2319. iph->check = ip_fast_csum((void *)iph, iph->ihl);
  2320. skb->protocol = protocol;
  2321. skb->mac_header = (skb->network_header - ETH_HLEN -
  2322. pkt_dev->pkt_overhead);
  2323. skb->dev = odev;
  2324. skb->pkt_type = PACKET_HOST;
  2325. pktgen_finalize_skb(pkt_dev, skb, datalen);
  2326. #ifdef CONFIG_XFRM
  2327. if (!process_ipsec(pkt_dev, skb, protocol))
  2328. return NULL;
  2329. #endif
  2330. return skb;
  2331. }
  2332. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  2333. struct pktgen_dev *pkt_dev)
  2334. {
  2335. struct sk_buff *skb = NULL;
  2336. __u8 *eth;
  2337. struct udphdr *udph;
  2338. int datalen;
  2339. struct ipv6hdr *iph;
  2340. __be16 protocol = htons(ETH_P_IPV6);
  2341. __be32 *mpls;
  2342. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2343. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2344. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2345. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2346. u16 queue_map;
  2347. if (pkt_dev->nr_labels)
  2348. protocol = htons(ETH_P_MPLS_UC);
  2349. if (pkt_dev->vlan_id != 0xffff)
  2350. protocol = htons(ETH_P_8021Q);
  2351. /* Update any of the values, used when we're incrementing various
  2352. * fields.
  2353. */
  2354. mod_cur_headers(pkt_dev);
  2355. queue_map = pkt_dev->cur_queue_map;
  2356. skb = __netdev_alloc_skb(odev,
  2357. pkt_dev->cur_pkt_size + 64
  2358. + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
  2359. if (!skb) {
  2360. sprintf(pkt_dev->result, "No memory");
  2361. return NULL;
  2362. }
  2363. prefetchw(skb->data);
  2364. skb_reserve(skb, 16);
  2365. /* Reserve for ethernet and IP header */
  2366. eth = (__u8 *) skb_push(skb, 14);
  2367. mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
  2368. if (pkt_dev->nr_labels)
  2369. mpls_push(mpls, pkt_dev);
  2370. if (pkt_dev->vlan_id != 0xffff) {
  2371. if (pkt_dev->svlan_id != 0xffff) {
  2372. svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2373. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2374. pkt_dev->svlan_cfi,
  2375. pkt_dev->svlan_p);
  2376. svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2377. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2378. }
  2379. vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2380. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2381. pkt_dev->vlan_cfi,
  2382. pkt_dev->vlan_p);
  2383. vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2384. *vlan_encapsulated_proto = htons(ETH_P_IPV6);
  2385. }
  2386. skb->network_header = skb->tail;
  2387. skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
  2388. skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
  2389. skb_set_queue_mapping(skb, queue_map);
  2390. skb->priority = pkt_dev->skb_priority;
  2391. iph = ipv6_hdr(skb);
  2392. udph = udp_hdr(skb);
  2393. memcpy(eth, pkt_dev->hh, 12);
  2394. *(__be16 *) &eth[12] = protocol;
  2395. /* Eth + IPh + UDPh + mpls */
  2396. datalen = pkt_dev->cur_pkt_size - 14 -
  2397. sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
  2398. pkt_dev->pkt_overhead;
  2399. if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
  2400. datalen = sizeof(struct pktgen_hdr);
  2401. net_info_ratelimited("increased datalen to %d\n", datalen);
  2402. }
  2403. udph->source = htons(pkt_dev->cur_udp_src);
  2404. udph->dest = htons(pkt_dev->cur_udp_dst);
  2405. udph->len = htons(datalen + sizeof(struct udphdr));
  2406. udph->check = 0; /* No checksum */
  2407. *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
  2408. if (pkt_dev->traffic_class) {
  2409. /* Version + traffic class + flow (0) */
  2410. *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
  2411. }
  2412. iph->hop_limit = 32;
  2413. iph->payload_len = htons(sizeof(struct udphdr) + datalen);
  2414. iph->nexthdr = IPPROTO_UDP;
  2415. iph->daddr = pkt_dev->cur_in6_daddr;
  2416. iph->saddr = pkt_dev->cur_in6_saddr;
  2417. skb->mac_header = (skb->network_header - ETH_HLEN -
  2418. pkt_dev->pkt_overhead);
  2419. skb->protocol = protocol;
  2420. skb->dev = odev;
  2421. skb->pkt_type = PACKET_HOST;
  2422. pktgen_finalize_skb(pkt_dev, skb, datalen);
  2423. return skb;
  2424. }
  2425. static struct sk_buff *fill_packet(struct net_device *odev,
  2426. struct pktgen_dev *pkt_dev)
  2427. {
  2428. if (pkt_dev->flags & F_IPV6)
  2429. return fill_packet_ipv6(odev, pkt_dev);
  2430. else
  2431. return fill_packet_ipv4(odev, pkt_dev);
  2432. }
  2433. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  2434. {
  2435. pkt_dev->seq_num = 1;
  2436. pkt_dev->idle_acc = 0;
  2437. pkt_dev->sofar = 0;
  2438. pkt_dev->tx_bytes = 0;
  2439. pkt_dev->errors = 0;
  2440. }
  2441. /* Set up structure for sending pkts, clear counters */
  2442. static void pktgen_run(struct pktgen_thread *t)
  2443. {
  2444. struct pktgen_dev *pkt_dev;
  2445. int started = 0;
  2446. func_enter();
  2447. if_lock(t);
  2448. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2449. /*
  2450. * setup odev and create initial packet.
  2451. */
  2452. pktgen_setup_inject(pkt_dev);
  2453. if (pkt_dev->odev) {
  2454. pktgen_clear_counters(pkt_dev);
  2455. pkt_dev->running = 1; /* Cranke yeself! */
  2456. pkt_dev->skb = NULL;
  2457. pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
  2458. set_pkt_overhead(pkt_dev);
  2459. strcpy(pkt_dev->result, "Starting");
  2460. started++;
  2461. } else
  2462. strcpy(pkt_dev->result, "Error starting");
  2463. }
  2464. if_unlock(t);
  2465. if (started)
  2466. t->control &= ~(T_STOP);
  2467. }
  2468. static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
  2469. {
  2470. struct pktgen_thread *t;
  2471. func_enter();
  2472. mutex_lock(&pktgen_thread_lock);
  2473. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2474. t->control |= T_STOP;
  2475. mutex_unlock(&pktgen_thread_lock);
  2476. }
  2477. static int thread_is_running(const struct pktgen_thread *t)
  2478. {
  2479. const struct pktgen_dev *pkt_dev;
  2480. list_for_each_entry(pkt_dev, &t->if_list, list)
  2481. if (pkt_dev->running)
  2482. return 1;
  2483. return 0;
  2484. }
  2485. static int pktgen_wait_thread_run(struct pktgen_thread *t)
  2486. {
  2487. if_lock(t);
  2488. while (thread_is_running(t)) {
  2489. if_unlock(t);
  2490. msleep_interruptible(100);
  2491. if (signal_pending(current))
  2492. goto signal;
  2493. if_lock(t);
  2494. }
  2495. if_unlock(t);
  2496. return 1;
  2497. signal:
  2498. return 0;
  2499. }
  2500. static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
  2501. {
  2502. struct pktgen_thread *t;
  2503. int sig = 1;
  2504. mutex_lock(&pktgen_thread_lock);
  2505. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  2506. sig = pktgen_wait_thread_run(t);
  2507. if (sig == 0)
  2508. break;
  2509. }
  2510. if (sig == 0)
  2511. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2512. t->control |= (T_STOP);
  2513. mutex_unlock(&pktgen_thread_lock);
  2514. return sig;
  2515. }
  2516. static void pktgen_run_all_threads(struct pktgen_net *pn)
  2517. {
  2518. struct pktgen_thread *t;
  2519. func_enter();
  2520. mutex_lock(&pktgen_thread_lock);
  2521. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2522. t->control |= (T_RUN);
  2523. mutex_unlock(&pktgen_thread_lock);
  2524. /* Propagate thread->control */
  2525. schedule_timeout_interruptible(msecs_to_jiffies(125));
  2526. pktgen_wait_all_threads_run(pn);
  2527. }
  2528. static void pktgen_reset_all_threads(struct pktgen_net *pn)
  2529. {
  2530. struct pktgen_thread *t;
  2531. func_enter();
  2532. mutex_lock(&pktgen_thread_lock);
  2533. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2534. t->control |= (T_REMDEVALL);
  2535. mutex_unlock(&pktgen_thread_lock);
  2536. /* Propagate thread->control */
  2537. schedule_timeout_interruptible(msecs_to_jiffies(125));
  2538. pktgen_wait_all_threads_run(pn);
  2539. }
  2540. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2541. {
  2542. __u64 bps, mbps, pps;
  2543. char *p = pkt_dev->result;
  2544. ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
  2545. pkt_dev->started_at);
  2546. ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
  2547. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2548. (unsigned long long)ktime_to_us(elapsed),
  2549. (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
  2550. (unsigned long long)ktime_to_us(idle),
  2551. (unsigned long long)pkt_dev->sofar,
  2552. pkt_dev->cur_pkt_size, nr_frags);
  2553. pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
  2554. ktime_to_ns(elapsed));
  2555. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2556. mbps = bps;
  2557. do_div(mbps, 1000000);
  2558. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2559. (unsigned long long)pps,
  2560. (unsigned long long)mbps,
  2561. (unsigned long long)bps,
  2562. (unsigned long long)pkt_dev->errors);
  2563. }
  2564. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2565. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2566. {
  2567. int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
  2568. if (!pkt_dev->running) {
  2569. pr_warning("interface: %s is already stopped\n",
  2570. pkt_dev->odevname);
  2571. return -EINVAL;
  2572. }
  2573. kfree_skb(pkt_dev->skb);
  2574. pkt_dev->skb = NULL;
  2575. pkt_dev->stopped_at = ktime_get();
  2576. pkt_dev->running = 0;
  2577. show_results(pkt_dev, nr_frags);
  2578. return 0;
  2579. }
  2580. static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
  2581. {
  2582. struct pktgen_dev *pkt_dev, *best = NULL;
  2583. if_lock(t);
  2584. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2585. if (!pkt_dev->running)
  2586. continue;
  2587. if (best == NULL)
  2588. best = pkt_dev;
  2589. else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
  2590. best = pkt_dev;
  2591. }
  2592. if_unlock(t);
  2593. return best;
  2594. }
  2595. static void pktgen_stop(struct pktgen_thread *t)
  2596. {
  2597. struct pktgen_dev *pkt_dev;
  2598. func_enter();
  2599. if_lock(t);
  2600. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2601. pktgen_stop_device(pkt_dev);
  2602. }
  2603. if_unlock(t);
  2604. }
  2605. /*
  2606. * one of our devices needs to be removed - find it
  2607. * and remove it
  2608. */
  2609. static void pktgen_rem_one_if(struct pktgen_thread *t)
  2610. {
  2611. struct list_head *q, *n;
  2612. struct pktgen_dev *cur;
  2613. func_enter();
  2614. if_lock(t);
  2615. list_for_each_safe(q, n, &t->if_list) {
  2616. cur = list_entry(q, struct pktgen_dev, list);
  2617. if (!cur->removal_mark)
  2618. continue;
  2619. kfree_skb(cur->skb);
  2620. cur->skb = NULL;
  2621. pktgen_remove_device(t, cur);
  2622. break;
  2623. }
  2624. if_unlock(t);
  2625. }
  2626. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2627. {
  2628. struct list_head *q, *n;
  2629. struct pktgen_dev *cur;
  2630. func_enter();
  2631. /* Remove all devices, free mem */
  2632. if_lock(t);
  2633. list_for_each_safe(q, n, &t->if_list) {
  2634. cur = list_entry(q, struct pktgen_dev, list);
  2635. kfree_skb(cur->skb);
  2636. cur->skb = NULL;
  2637. pktgen_remove_device(t, cur);
  2638. }
  2639. if_unlock(t);
  2640. }
  2641. static void pktgen_rem_thread(struct pktgen_thread *t)
  2642. {
  2643. /* Remove from the thread list */
  2644. remove_proc_entry(t->tsk->comm, t->net->proc_dir);
  2645. }
  2646. static void pktgen_resched(struct pktgen_dev *pkt_dev)
  2647. {
  2648. ktime_t idle_start = ktime_get();
  2649. schedule();
  2650. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
  2651. }
  2652. static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
  2653. {
  2654. ktime_t idle_start = ktime_get();
  2655. while (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2656. if (signal_pending(current))
  2657. break;
  2658. if (need_resched())
  2659. pktgen_resched(pkt_dev);
  2660. else
  2661. cpu_relax();
  2662. }
  2663. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
  2664. }
  2665. static void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2666. {
  2667. struct net_device *odev = pkt_dev->odev;
  2668. netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
  2669. = odev->netdev_ops->ndo_start_xmit;
  2670. struct netdev_queue *txq;
  2671. u16 queue_map;
  2672. int ret;
  2673. /* If device is offline, then don't send */
  2674. if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
  2675. pktgen_stop_device(pkt_dev);
  2676. return;
  2677. }
  2678. /* This is max DELAY, this has special meaning of
  2679. * "never transmit"
  2680. */
  2681. if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
  2682. pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
  2683. return;
  2684. }
  2685. /* If no skb or clone count exhausted then get new one */
  2686. if (!pkt_dev->skb || (pkt_dev->last_ok &&
  2687. ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
  2688. /* build a new pkt */
  2689. kfree_skb(pkt_dev->skb);
  2690. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2691. if (pkt_dev->skb == NULL) {
  2692. pr_err("ERROR: couldn't allocate skb in fill_packet\n");
  2693. schedule();
  2694. pkt_dev->clone_count--; /* back out increment, OOM */
  2695. return;
  2696. }
  2697. pkt_dev->last_pkt_size = pkt_dev->skb->len;
  2698. pkt_dev->allocated_skbs++;
  2699. pkt_dev->clone_count = 0; /* reset counter */
  2700. }
  2701. if (pkt_dev->delay && pkt_dev->last_ok)
  2702. spin(pkt_dev, pkt_dev->next_tx);
  2703. queue_map = skb_get_queue_mapping(pkt_dev->skb);
  2704. txq = netdev_get_tx_queue(odev, queue_map);
  2705. __netif_tx_lock_bh(txq);
  2706. if (unlikely(netif_xmit_frozen_or_stopped(txq))) {
  2707. ret = NETDEV_TX_BUSY;
  2708. pkt_dev->last_ok = 0;
  2709. goto unlock;
  2710. }
  2711. atomic_inc(&(pkt_dev->skb->users));
  2712. ret = (*xmit)(pkt_dev->skb, odev);
  2713. switch (ret) {
  2714. case NETDEV_TX_OK:
  2715. txq_trans_update(txq);
  2716. pkt_dev->last_ok = 1;
  2717. pkt_dev->sofar++;
  2718. pkt_dev->seq_num++;
  2719. pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
  2720. break;
  2721. case NET_XMIT_DROP:
  2722. case NET_XMIT_CN:
  2723. case NET_XMIT_POLICED:
  2724. /* skb has been consumed */
  2725. pkt_dev->errors++;
  2726. break;
  2727. default: /* Drivers are not supposed to return other values! */
  2728. net_info_ratelimited("%s xmit error: %d\n",
  2729. pkt_dev->odevname, ret);
  2730. pkt_dev->errors++;
  2731. /* fallthru */
  2732. case NETDEV_TX_LOCKED:
  2733. case NETDEV_TX_BUSY:
  2734. /* Retry it next time */
  2735. atomic_dec(&(pkt_dev->skb->users));
  2736. pkt_dev->last_ok = 0;
  2737. }
  2738. unlock:
  2739. __netif_tx_unlock_bh(txq);
  2740. /* If pkt_dev->count is zero, then run forever */
  2741. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  2742. pktgen_wait_for_skb(pkt_dev);
  2743. /* Done with this */
  2744. pktgen_stop_device(pkt_dev);
  2745. }
  2746. }
  2747. /*
  2748. * Main loop of the thread goes here
  2749. */
  2750. static int pktgen_thread_worker(void *arg)
  2751. {
  2752. DEFINE_WAIT(wait);
  2753. struct pktgen_thread *t = arg;
  2754. struct pktgen_dev *pkt_dev = NULL;
  2755. int cpu = t->cpu;
  2756. BUG_ON(smp_processor_id() != cpu);
  2757. init_waitqueue_head(&t->queue);
  2758. complete(&t->start_done);
  2759. pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
  2760. set_current_state(TASK_INTERRUPTIBLE);
  2761. set_freezable();
  2762. while (!kthread_should_stop()) {
  2763. pkt_dev = next_to_run(t);
  2764. if (unlikely(!pkt_dev && t->control == 0)) {
  2765. if (t->net->pktgen_exiting)
  2766. break;
  2767. wait_event_interruptible_timeout(t->queue,
  2768. t->control != 0,
  2769. HZ/10);
  2770. try_to_freeze();
  2771. continue;
  2772. }
  2773. __set_current_state(TASK_RUNNING);
  2774. if (likely(pkt_dev)) {
  2775. pktgen_xmit(pkt_dev);
  2776. if (need_resched())
  2777. pktgen_resched(pkt_dev);
  2778. else
  2779. cpu_relax();
  2780. }
  2781. if (t->control & T_STOP) {
  2782. pktgen_stop(t);
  2783. t->control &= ~(T_STOP);
  2784. }
  2785. if (t->control & T_RUN) {
  2786. pktgen_run(t);
  2787. t->control &= ~(T_RUN);
  2788. }
  2789. if (t->control & T_REMDEVALL) {
  2790. pktgen_rem_all_ifs(t);
  2791. t->control &= ~(T_REMDEVALL);
  2792. }
  2793. if (t->control & T_REMDEV) {
  2794. pktgen_rem_one_if(t);
  2795. t->control &= ~(T_REMDEV);
  2796. }
  2797. try_to_freeze();
  2798. set_current_state(TASK_INTERRUPTIBLE);
  2799. }
  2800. pr_debug("%s stopping all device\n", t->tsk->comm);
  2801. pktgen_stop(t);
  2802. pr_debug("%s removing all device\n", t->tsk->comm);
  2803. pktgen_rem_all_ifs(t);
  2804. pr_debug("%s removing thread\n", t->tsk->comm);
  2805. pktgen_rem_thread(t);
  2806. /* Wait for kthread_stop */
  2807. while (!kthread_should_stop()) {
  2808. set_current_state(TASK_INTERRUPTIBLE);
  2809. schedule();
  2810. }
  2811. __set_current_state(TASK_RUNNING);
  2812. return 0;
  2813. }
  2814. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  2815. const char *ifname, bool exact)
  2816. {
  2817. struct pktgen_dev *p, *pkt_dev = NULL;
  2818. size_t len = strlen(ifname);
  2819. if_lock(t);
  2820. list_for_each_entry(p, &t->if_list, list)
  2821. if (strncmp(p->odevname, ifname, len) == 0) {
  2822. if (p->odevname[len]) {
  2823. if (exact || p->odevname[len] != '@')
  2824. continue;
  2825. }
  2826. pkt_dev = p;
  2827. break;
  2828. }
  2829. if_unlock(t);
  2830. pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
  2831. return pkt_dev;
  2832. }
  2833. /*
  2834. * Adds a dev at front of if_list.
  2835. */
  2836. static int add_dev_to_thread(struct pktgen_thread *t,
  2837. struct pktgen_dev *pkt_dev)
  2838. {
  2839. int rv = 0;
  2840. if_lock(t);
  2841. if (pkt_dev->pg_thread) {
  2842. pr_err("ERROR: already assigned to a thread\n");
  2843. rv = -EBUSY;
  2844. goto out;
  2845. }
  2846. list_add(&pkt_dev->list, &t->if_list);
  2847. pkt_dev->pg_thread = t;
  2848. pkt_dev->running = 0;
  2849. out:
  2850. if_unlock(t);
  2851. return rv;
  2852. }
  2853. /* Called under thread lock */
  2854. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
  2855. {
  2856. struct pktgen_dev *pkt_dev;
  2857. int err;
  2858. int node = cpu_to_node(t->cpu);
  2859. /* We don't allow a device to be on several threads */
  2860. pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
  2861. if (pkt_dev) {
  2862. pr_err("ERROR: interface already used\n");
  2863. return -EBUSY;
  2864. }
  2865. pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
  2866. if (!pkt_dev)
  2867. return -ENOMEM;
  2868. strcpy(pkt_dev->odevname, ifname);
  2869. pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
  2870. node);
  2871. if (pkt_dev->flows == NULL) {
  2872. kfree(pkt_dev);
  2873. return -ENOMEM;
  2874. }
  2875. pkt_dev->removal_mark = 0;
  2876. pkt_dev->nfrags = 0;
  2877. pkt_dev->delay = pg_delay_d;
  2878. pkt_dev->count = pg_count_d;
  2879. pkt_dev->sofar = 0;
  2880. pkt_dev->udp_src_min = 9; /* sink port */
  2881. pkt_dev->udp_src_max = 9;
  2882. pkt_dev->udp_dst_min = 9;
  2883. pkt_dev->udp_dst_max = 9;
  2884. pkt_dev->vlan_p = 0;
  2885. pkt_dev->vlan_cfi = 0;
  2886. pkt_dev->vlan_id = 0xffff;
  2887. pkt_dev->svlan_p = 0;
  2888. pkt_dev->svlan_cfi = 0;
  2889. pkt_dev->svlan_id = 0xffff;
  2890. pkt_dev->node = -1;
  2891. err = pktgen_setup_dev(t->net, pkt_dev, ifname);
  2892. if (err)
  2893. goto out1;
  2894. if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
  2895. pkt_dev->clone_skb = pg_clone_skb_d;
  2896. pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
  2897. &pktgen_if_fops, pkt_dev);
  2898. if (!pkt_dev->entry) {
  2899. pr_err("cannot create %s/%s procfs entry\n",
  2900. PG_PROC_DIR, ifname);
  2901. err = -EINVAL;
  2902. goto out2;
  2903. }
  2904. #ifdef CONFIG_XFRM
  2905. pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
  2906. pkt_dev->ipsproto = IPPROTO_ESP;
  2907. #endif
  2908. return add_dev_to_thread(t, pkt_dev);
  2909. out2:
  2910. dev_put(pkt_dev->odev);
  2911. out1:
  2912. #ifdef CONFIG_XFRM
  2913. free_SAs(pkt_dev);
  2914. #endif
  2915. vfree(pkt_dev->flows);
  2916. kfree(pkt_dev);
  2917. return err;
  2918. }
  2919. static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
  2920. {
  2921. struct pktgen_thread *t;
  2922. struct proc_dir_entry *pe;
  2923. struct task_struct *p;
  2924. t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
  2925. cpu_to_node(cpu));
  2926. if (!t) {
  2927. pr_err("ERROR: out of memory, can't create new thread\n");
  2928. return -ENOMEM;
  2929. }
  2930. spin_lock_init(&t->if_lock);
  2931. t->cpu = cpu;
  2932. INIT_LIST_HEAD(&t->if_list);
  2933. list_add_tail(&t->th_list, &pn->pktgen_threads);
  2934. init_completion(&t->start_done);
  2935. p = kthread_create_on_node(pktgen_thread_worker,
  2936. t,
  2937. cpu_to_node(cpu),
  2938. "kpktgend_%d", cpu);
  2939. if (IS_ERR(p)) {
  2940. pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
  2941. list_del(&t->th_list);
  2942. kfree(t);
  2943. return PTR_ERR(p);
  2944. }
  2945. kthread_bind(p, cpu);
  2946. t->tsk = p;
  2947. pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
  2948. &pktgen_thread_fops, t);
  2949. if (!pe) {
  2950. pr_err("cannot create %s/%s procfs entry\n",
  2951. PG_PROC_DIR, t->tsk->comm);
  2952. kthread_stop(p);
  2953. list_del(&t->th_list);
  2954. kfree(t);
  2955. return -EINVAL;
  2956. }
  2957. t->net = pn;
  2958. wake_up_process(p);
  2959. wait_for_completion(&t->start_done);
  2960. return 0;
  2961. }
  2962. /*
  2963. * Removes a device from the thread if_list.
  2964. */
  2965. static void _rem_dev_from_if_list(struct pktgen_thread *t,
  2966. struct pktgen_dev *pkt_dev)
  2967. {
  2968. struct list_head *q, *n;
  2969. struct pktgen_dev *p;
  2970. list_for_each_safe(q, n, &t->if_list) {
  2971. p = list_entry(q, struct pktgen_dev, list);
  2972. if (p == pkt_dev)
  2973. list_del(&p->list);
  2974. }
  2975. }
  2976. static int pktgen_remove_device(struct pktgen_thread *t,
  2977. struct pktgen_dev *pkt_dev)
  2978. {
  2979. struct pktgen_net *pn = t->net;
  2980. pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
  2981. if (pkt_dev->running) {
  2982. pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
  2983. pktgen_stop_device(pkt_dev);
  2984. }
  2985. /* Dis-associate from the interface */
  2986. if (pkt_dev->odev) {
  2987. dev_put(pkt_dev->odev);
  2988. pkt_dev->odev = NULL;
  2989. }
  2990. /* And update the thread if_list */
  2991. _rem_dev_from_if_list(t, pkt_dev);
  2992. if (pkt_dev->entry)
  2993. remove_proc_entry(pkt_dev->entry->name, pn->proc_dir);
  2994. #ifdef CONFIG_XFRM
  2995. free_SAs(pkt_dev);
  2996. #endif
  2997. vfree(pkt_dev->flows);
  2998. if (pkt_dev->page)
  2999. put_page(pkt_dev->page);
  3000. kfree(pkt_dev);
  3001. return 0;
  3002. }
  3003. static int __net_init pg_net_init(struct net *net)
  3004. {
  3005. struct pktgen_net *pn = net_generic(net, pg_net_id);
  3006. struct proc_dir_entry *pe;
  3007. int cpu, ret = 0;
  3008. pn->net = net;
  3009. INIT_LIST_HEAD(&pn->pktgen_threads);
  3010. pn->pktgen_exiting = false;
  3011. pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
  3012. if (!pn->proc_dir) {
  3013. pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
  3014. return -ENODEV;
  3015. }
  3016. pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
  3017. if (pe == NULL) {
  3018. pr_err("cannot create %s procfs entry\n", PGCTRL);
  3019. ret = -EINVAL;
  3020. goto remove;
  3021. }
  3022. for_each_online_cpu(cpu) {
  3023. int err;
  3024. err = pktgen_create_thread(cpu, pn);
  3025. if (err)
  3026. pr_warn("Cannot create thread for cpu %d (%d)\n",
  3027. cpu, err);
  3028. }
  3029. if (list_empty(&pn->pktgen_threads)) {
  3030. pr_err("Initialization failed for all threads\n");
  3031. ret = -ENODEV;
  3032. goto remove_entry;
  3033. }
  3034. return 0;
  3035. remove_entry:
  3036. remove_proc_entry(PGCTRL, pn->proc_dir);
  3037. remove:
  3038. remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
  3039. return ret;
  3040. }
  3041. static void __net_exit pg_net_exit(struct net *net)
  3042. {
  3043. struct pktgen_net *pn = net_generic(net, pg_net_id);
  3044. struct pktgen_thread *t;
  3045. struct list_head *q, *n;
  3046. LIST_HEAD(list);
  3047. /* Stop all interfaces & threads */
  3048. pn->pktgen_exiting = true;
  3049. mutex_lock(&pktgen_thread_lock);
  3050. list_splice_init(&pn->pktgen_threads, &list);
  3051. mutex_unlock(&pktgen_thread_lock);
  3052. list_for_each_safe(q, n, &list) {
  3053. t = list_entry(q, struct pktgen_thread, th_list);
  3054. list_del(&t->th_list);
  3055. kthread_stop(t->tsk);
  3056. kfree(t);
  3057. }
  3058. remove_proc_entry(PGCTRL, pn->proc_dir);
  3059. remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
  3060. }
  3061. static struct pernet_operations pg_net_ops = {
  3062. .init = pg_net_init,
  3063. .exit = pg_net_exit,
  3064. .id = &pg_net_id,
  3065. .size = sizeof(struct pktgen_net),
  3066. };
  3067. static int __init pg_init(void)
  3068. {
  3069. int ret = 0;
  3070. pr_info("%s", version);
  3071. ret = register_pernet_subsys(&pg_net_ops);
  3072. if (ret)
  3073. return ret;
  3074. ret = register_netdevice_notifier(&pktgen_notifier_block);
  3075. if (ret)
  3076. unregister_pernet_subsys(&pg_net_ops);
  3077. return ret;
  3078. }
  3079. static void __exit pg_cleanup(void)
  3080. {
  3081. unregister_netdevice_notifier(&pktgen_notifier_block);
  3082. unregister_pernet_subsys(&pg_net_ops);
  3083. }
  3084. module_init(pg_init);
  3085. module_exit(pg_cleanup);
  3086. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
  3087. MODULE_DESCRIPTION("Packet Generator tool");
  3088. MODULE_LICENSE("GPL");
  3089. MODULE_VERSION(VERSION);
  3090. module_param(pg_count_d, int, 0);
  3091. MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
  3092. module_param(pg_delay_d, int, 0);
  3093. MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
  3094. module_param(pg_clone_skb_d, int, 0);
  3095. MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
  3096. module_param(debug, int, 0);
  3097. MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");