pktgen.c 91 KB

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