pktgen.c 92 KB

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