pktgen.c 88 KB

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