pktgen.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697
  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 struct sk_buff *fill_packet_ipv4(struct net_device *odev,
  1985. struct pktgen_dev *pkt_dev)
  1986. {
  1987. struct sk_buff *skb = NULL;
  1988. __u8 *eth;
  1989. struct udphdr *udph;
  1990. int datalen, iplen;
  1991. struct iphdr *iph;
  1992. struct pktgen_hdr *pgh = NULL;
  1993. __be16 protocol = __constant_htons(ETH_P_IP);
  1994. __be32 *mpls;
  1995. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  1996. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  1997. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  1998. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  1999. if (pkt_dev->nr_labels)
  2000. protocol = __constant_htons(ETH_P_MPLS_UC);
  2001. if (pkt_dev->vlan_id != 0xffff)
  2002. protocol = __constant_htons(ETH_P_8021Q);
  2003. /* Update any of the values, used when we're incrementing various
  2004. * fields.
  2005. */
  2006. mod_cur_headers(pkt_dev);
  2007. datalen = (odev->hard_header_len + 16) & ~0xf;
  2008. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
  2009. pkt_dev->nr_labels*sizeof(u32) +
  2010. VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
  2011. GFP_ATOMIC);
  2012. if (!skb) {
  2013. sprintf(pkt_dev->result, "No memory");
  2014. return NULL;
  2015. }
  2016. skb_reserve(skb, datalen);
  2017. /* Reserve for ethernet and IP header */
  2018. eth = (__u8 *) skb_push(skb, 14);
  2019. mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
  2020. if (pkt_dev->nr_labels)
  2021. mpls_push(mpls, pkt_dev);
  2022. if (pkt_dev->vlan_id != 0xffff) {
  2023. if(pkt_dev->svlan_id != 0xffff) {
  2024. svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2025. *svlan_tci = htons(pkt_dev->svlan_id);
  2026. *svlan_tci |= pkt_dev->svlan_p << 5;
  2027. *svlan_tci |= pkt_dev->svlan_cfi << 4;
  2028. svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2029. *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
  2030. }
  2031. vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2032. *vlan_tci = htons(pkt_dev->vlan_id);
  2033. *vlan_tci |= pkt_dev->vlan_p << 5;
  2034. *vlan_tci |= pkt_dev->vlan_cfi << 4;
  2035. vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2036. *vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
  2037. }
  2038. iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
  2039. udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
  2040. memcpy(eth, pkt_dev->hh, 12);
  2041. *(u16 *) & eth[12] = protocol;
  2042. /* Eth + IPh + UDPh + mpls */
  2043. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
  2044. pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
  2045. if (datalen < sizeof(struct pktgen_hdr))
  2046. datalen = sizeof(struct pktgen_hdr);
  2047. udph->source = htons(pkt_dev->cur_udp_src);
  2048. udph->dest = htons(pkt_dev->cur_udp_dst);
  2049. udph->len = htons(datalen + 8); /* DATA + udphdr */
  2050. udph->check = 0; /* No checksum */
  2051. iph->ihl = 5;
  2052. iph->version = 4;
  2053. iph->ttl = 32;
  2054. iph->tos = pkt_dev->tos;
  2055. iph->protocol = IPPROTO_UDP; /* UDP */
  2056. iph->saddr = pkt_dev->cur_saddr;
  2057. iph->daddr = pkt_dev->cur_daddr;
  2058. iph->frag_off = 0;
  2059. iplen = 20 + 8 + datalen;
  2060. iph->tot_len = htons(iplen);
  2061. iph->check = 0;
  2062. iph->check = ip_fast_csum((void *)iph, iph->ihl);
  2063. skb->protocol = protocol;
  2064. skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
  2065. VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
  2066. skb->dev = odev;
  2067. skb->pkt_type = PACKET_HOST;
  2068. skb->nh.iph = iph;
  2069. skb->h.uh = udph;
  2070. if (pkt_dev->nfrags <= 0)
  2071. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  2072. else {
  2073. int frags = pkt_dev->nfrags;
  2074. int i;
  2075. pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
  2076. if (frags > MAX_SKB_FRAGS)
  2077. frags = MAX_SKB_FRAGS;
  2078. if (datalen > frags * PAGE_SIZE) {
  2079. skb_put(skb, datalen - frags * PAGE_SIZE);
  2080. datalen = frags * PAGE_SIZE;
  2081. }
  2082. i = 0;
  2083. while (datalen > 0) {
  2084. struct page *page = alloc_pages(GFP_KERNEL, 0);
  2085. skb_shinfo(skb)->frags[i].page = page;
  2086. skb_shinfo(skb)->frags[i].page_offset = 0;
  2087. skb_shinfo(skb)->frags[i].size =
  2088. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  2089. datalen -= skb_shinfo(skb)->frags[i].size;
  2090. skb->len += skb_shinfo(skb)->frags[i].size;
  2091. skb->data_len += skb_shinfo(skb)->frags[i].size;
  2092. i++;
  2093. skb_shinfo(skb)->nr_frags = i;
  2094. }
  2095. while (i < frags) {
  2096. int rem;
  2097. if (i == 0)
  2098. break;
  2099. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  2100. if (rem == 0)
  2101. break;
  2102. skb_shinfo(skb)->frags[i - 1].size -= rem;
  2103. skb_shinfo(skb)->frags[i] =
  2104. skb_shinfo(skb)->frags[i - 1];
  2105. get_page(skb_shinfo(skb)->frags[i].page);
  2106. skb_shinfo(skb)->frags[i].page =
  2107. skb_shinfo(skb)->frags[i - 1].page;
  2108. skb_shinfo(skb)->frags[i].page_offset +=
  2109. skb_shinfo(skb)->frags[i - 1].size;
  2110. skb_shinfo(skb)->frags[i].size = rem;
  2111. i++;
  2112. skb_shinfo(skb)->nr_frags = i;
  2113. }
  2114. }
  2115. /* Stamp the time, and sequence number, convert them to network byte order */
  2116. if (pgh) {
  2117. struct timeval timestamp;
  2118. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2119. pgh->seq_num = htonl(pkt_dev->seq_num);
  2120. do_gettimeofday(&timestamp);
  2121. pgh->tv_sec = htonl(timestamp.tv_sec);
  2122. pgh->tv_usec = htonl(timestamp.tv_usec);
  2123. }
  2124. return skb;
  2125. }
  2126. /*
  2127. * scan_ip6, fmt_ip taken from dietlibc-0.21
  2128. * Author Felix von Leitner <felix-dietlibc@fefe.de>
  2129. *
  2130. * Slightly modified for kernel.
  2131. * Should be candidate for net/ipv4/utils.c
  2132. * --ro
  2133. */
  2134. static unsigned int scan_ip6(const char *s, char ip[16])
  2135. {
  2136. unsigned int i;
  2137. unsigned int len = 0;
  2138. unsigned long u;
  2139. char suffix[16];
  2140. unsigned int prefixlen = 0;
  2141. unsigned int suffixlen = 0;
  2142. __u32 tmp;
  2143. for (i = 0; i < 16; i++)
  2144. ip[i] = 0;
  2145. for (;;) {
  2146. if (*s == ':') {
  2147. len++;
  2148. if (s[1] == ':') { /* Found "::", skip to part 2 */
  2149. s += 2;
  2150. len++;
  2151. break;
  2152. }
  2153. s++;
  2154. }
  2155. {
  2156. char *tmp;
  2157. u = simple_strtoul(s, &tmp, 16);
  2158. i = tmp - s;
  2159. }
  2160. if (!i)
  2161. return 0;
  2162. if (prefixlen == 12 && s[i] == '.') {
  2163. /* the last 4 bytes may be written as IPv4 address */
  2164. tmp = in_aton(s);
  2165. memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
  2166. return i + len;
  2167. }
  2168. ip[prefixlen++] = (u >> 8);
  2169. ip[prefixlen++] = (u & 255);
  2170. s += i;
  2171. len += i;
  2172. if (prefixlen == 16)
  2173. return len;
  2174. }
  2175. /* part 2, after "::" */
  2176. for (;;) {
  2177. if (*s == ':') {
  2178. if (suffixlen == 0)
  2179. break;
  2180. s++;
  2181. len++;
  2182. } else if (suffixlen != 0)
  2183. break;
  2184. {
  2185. char *tmp;
  2186. u = simple_strtol(s, &tmp, 16);
  2187. i = tmp - s;
  2188. }
  2189. if (!i) {
  2190. if (*s)
  2191. len--;
  2192. break;
  2193. }
  2194. if (suffixlen + prefixlen <= 12 && s[i] == '.') {
  2195. tmp = in_aton(s);
  2196. memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
  2197. sizeof(tmp));
  2198. suffixlen += 4;
  2199. len += strlen(s);
  2200. break;
  2201. }
  2202. suffix[suffixlen++] = (u >> 8);
  2203. suffix[suffixlen++] = (u & 255);
  2204. s += i;
  2205. len += i;
  2206. if (prefixlen + suffixlen == 16)
  2207. break;
  2208. }
  2209. for (i = 0; i < suffixlen; i++)
  2210. ip[16 - suffixlen + i] = suffix[i];
  2211. return len;
  2212. }
  2213. static char tohex(char hexdigit)
  2214. {
  2215. return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
  2216. }
  2217. static int fmt_xlong(char *s, unsigned int i)
  2218. {
  2219. char *bak = s;
  2220. *s = tohex((i >> 12) & 0xf);
  2221. if (s != bak || *s != '0')
  2222. ++s;
  2223. *s = tohex((i >> 8) & 0xf);
  2224. if (s != bak || *s != '0')
  2225. ++s;
  2226. *s = tohex((i >> 4) & 0xf);
  2227. if (s != bak || *s != '0')
  2228. ++s;
  2229. *s = tohex(i & 0xf);
  2230. return s - bak + 1;
  2231. }
  2232. static unsigned int fmt_ip6(char *s, const char ip[16])
  2233. {
  2234. unsigned int len;
  2235. unsigned int i;
  2236. unsigned int temp;
  2237. unsigned int compressing;
  2238. int j;
  2239. len = 0;
  2240. compressing = 0;
  2241. for (j = 0; j < 16; j += 2) {
  2242. #ifdef V4MAPPEDPREFIX
  2243. if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
  2244. inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
  2245. temp = strlen(s);
  2246. return len + temp;
  2247. }
  2248. #endif
  2249. temp = ((unsigned long)(unsigned char)ip[j] << 8) +
  2250. (unsigned long)(unsigned char)ip[j + 1];
  2251. if (temp == 0) {
  2252. if (!compressing) {
  2253. compressing = 1;
  2254. if (j == 0) {
  2255. *s++ = ':';
  2256. ++len;
  2257. }
  2258. }
  2259. } else {
  2260. if (compressing) {
  2261. compressing = 0;
  2262. *s++ = ':';
  2263. ++len;
  2264. }
  2265. i = fmt_xlong(s, temp);
  2266. len += i;
  2267. s += i;
  2268. if (j < 14) {
  2269. *s++ = ':';
  2270. ++len;
  2271. }
  2272. }
  2273. }
  2274. if (compressing) {
  2275. *s++ = ':';
  2276. ++len;
  2277. }
  2278. *s = 0;
  2279. return len;
  2280. }
  2281. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  2282. struct pktgen_dev *pkt_dev)
  2283. {
  2284. struct sk_buff *skb = NULL;
  2285. __u8 *eth;
  2286. struct udphdr *udph;
  2287. int datalen;
  2288. struct ipv6hdr *iph;
  2289. struct pktgen_hdr *pgh = NULL;
  2290. __be16 protocol = __constant_htons(ETH_P_IPV6);
  2291. __be32 *mpls;
  2292. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2293. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2294. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2295. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2296. if (pkt_dev->nr_labels)
  2297. protocol = __constant_htons(ETH_P_MPLS_UC);
  2298. if (pkt_dev->vlan_id != 0xffff)
  2299. protocol = __constant_htons(ETH_P_8021Q);
  2300. /* Update any of the values, used when we're incrementing various
  2301. * fields.
  2302. */
  2303. mod_cur_headers(pkt_dev);
  2304. skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
  2305. pkt_dev->nr_labels*sizeof(u32) +
  2306. VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
  2307. GFP_ATOMIC);
  2308. if (!skb) {
  2309. sprintf(pkt_dev->result, "No memory");
  2310. return NULL;
  2311. }
  2312. skb_reserve(skb, 16);
  2313. /* Reserve for ethernet and IP header */
  2314. eth = (__u8 *) skb_push(skb, 14);
  2315. mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
  2316. if (pkt_dev->nr_labels)
  2317. mpls_push(mpls, pkt_dev);
  2318. if (pkt_dev->vlan_id != 0xffff) {
  2319. if(pkt_dev->svlan_id != 0xffff) {
  2320. svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2321. *svlan_tci = htons(pkt_dev->svlan_id);
  2322. *svlan_tci |= pkt_dev->svlan_p << 5;
  2323. *svlan_tci |= pkt_dev->svlan_cfi << 4;
  2324. svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2325. *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
  2326. }
  2327. vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
  2328. *vlan_tci = htons(pkt_dev->vlan_id);
  2329. *vlan_tci |= pkt_dev->vlan_p << 5;
  2330. *vlan_tci |= pkt_dev->vlan_cfi << 4;
  2331. vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
  2332. *vlan_encapsulated_proto = __constant_htons(ETH_P_IPV6);
  2333. }
  2334. iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
  2335. udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
  2336. memcpy(eth, pkt_dev->hh, 12);
  2337. *(u16 *) & eth[12] = protocol;
  2338. /* Eth + IPh + UDPh + mpls */
  2339. datalen = pkt_dev->cur_pkt_size - 14 -
  2340. sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
  2341. pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
  2342. if (datalen < sizeof(struct pktgen_hdr)) {
  2343. datalen = sizeof(struct pktgen_hdr);
  2344. if (net_ratelimit())
  2345. printk(KERN_INFO "pktgen: increased datalen to %d\n",
  2346. datalen);
  2347. }
  2348. udph->source = htons(pkt_dev->cur_udp_src);
  2349. udph->dest = htons(pkt_dev->cur_udp_dst);
  2350. udph->len = htons(datalen + sizeof(struct udphdr));
  2351. udph->check = 0; /* No checksum */
  2352. *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
  2353. if (pkt_dev->traffic_class) {
  2354. /* Version + traffic class + flow (0) */
  2355. *(u32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
  2356. }
  2357. iph->hop_limit = 32;
  2358. iph->payload_len = htons(sizeof(struct udphdr) + datalen);
  2359. iph->nexthdr = IPPROTO_UDP;
  2360. ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
  2361. ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
  2362. skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
  2363. VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
  2364. skb->protocol = protocol;
  2365. skb->dev = odev;
  2366. skb->pkt_type = PACKET_HOST;
  2367. skb->nh.ipv6h = iph;
  2368. skb->h.uh = udph;
  2369. if (pkt_dev->nfrags <= 0)
  2370. pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
  2371. else {
  2372. int frags = pkt_dev->nfrags;
  2373. int i;
  2374. pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
  2375. if (frags > MAX_SKB_FRAGS)
  2376. frags = MAX_SKB_FRAGS;
  2377. if (datalen > frags * PAGE_SIZE) {
  2378. skb_put(skb, datalen - frags * PAGE_SIZE);
  2379. datalen = frags * PAGE_SIZE;
  2380. }
  2381. i = 0;
  2382. while (datalen > 0) {
  2383. struct page *page = alloc_pages(GFP_KERNEL, 0);
  2384. skb_shinfo(skb)->frags[i].page = page;
  2385. skb_shinfo(skb)->frags[i].page_offset = 0;
  2386. skb_shinfo(skb)->frags[i].size =
  2387. (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
  2388. datalen -= skb_shinfo(skb)->frags[i].size;
  2389. skb->len += skb_shinfo(skb)->frags[i].size;
  2390. skb->data_len += skb_shinfo(skb)->frags[i].size;
  2391. i++;
  2392. skb_shinfo(skb)->nr_frags = i;
  2393. }
  2394. while (i < frags) {
  2395. int rem;
  2396. if (i == 0)
  2397. break;
  2398. rem = skb_shinfo(skb)->frags[i - 1].size / 2;
  2399. if (rem == 0)
  2400. break;
  2401. skb_shinfo(skb)->frags[i - 1].size -= rem;
  2402. skb_shinfo(skb)->frags[i] =
  2403. skb_shinfo(skb)->frags[i - 1];
  2404. get_page(skb_shinfo(skb)->frags[i].page);
  2405. skb_shinfo(skb)->frags[i].page =
  2406. skb_shinfo(skb)->frags[i - 1].page;
  2407. skb_shinfo(skb)->frags[i].page_offset +=
  2408. skb_shinfo(skb)->frags[i - 1].size;
  2409. skb_shinfo(skb)->frags[i].size = rem;
  2410. i++;
  2411. skb_shinfo(skb)->nr_frags = i;
  2412. }
  2413. }
  2414. /* Stamp the time, and sequence number, convert them to network byte order */
  2415. /* should we update cloned packets too ? */
  2416. if (pgh) {
  2417. struct timeval timestamp;
  2418. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2419. pgh->seq_num = htonl(pkt_dev->seq_num);
  2420. do_gettimeofday(&timestamp);
  2421. pgh->tv_sec = htonl(timestamp.tv_sec);
  2422. pgh->tv_usec = htonl(timestamp.tv_usec);
  2423. }
  2424. /* pkt_dev->seq_num++; FF: you really mean this? */
  2425. return skb;
  2426. }
  2427. static inline struct sk_buff *fill_packet(struct net_device *odev,
  2428. struct pktgen_dev *pkt_dev)
  2429. {
  2430. if (pkt_dev->flags & F_IPV6)
  2431. return fill_packet_ipv6(odev, pkt_dev);
  2432. else
  2433. return fill_packet_ipv4(odev, pkt_dev);
  2434. }
  2435. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  2436. {
  2437. pkt_dev->seq_num = 1;
  2438. pkt_dev->idle_acc = 0;
  2439. pkt_dev->sofar = 0;
  2440. pkt_dev->tx_bytes = 0;
  2441. pkt_dev->errors = 0;
  2442. }
  2443. /* Set up structure for sending pkts, clear counters */
  2444. static void pktgen_run(struct pktgen_thread *t)
  2445. {
  2446. struct pktgen_dev *pkt_dev;
  2447. int started = 0;
  2448. PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
  2449. if_lock(t);
  2450. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2451. /*
  2452. * setup odev and create initial packet.
  2453. */
  2454. pktgen_setup_inject(pkt_dev);
  2455. if (pkt_dev->odev) {
  2456. pktgen_clear_counters(pkt_dev);
  2457. pkt_dev->running = 1; /* Cranke yeself! */
  2458. pkt_dev->skb = NULL;
  2459. pkt_dev->started_at = getCurUs();
  2460. pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
  2461. pkt_dev->next_tx_ns = 0;
  2462. strcpy(pkt_dev->result, "Starting");
  2463. started++;
  2464. } else
  2465. strcpy(pkt_dev->result, "Error starting");
  2466. }
  2467. if_unlock(t);
  2468. if (started)
  2469. t->control &= ~(T_STOP);
  2470. }
  2471. static void pktgen_stop_all_threads_ifs(void)
  2472. {
  2473. struct pktgen_thread *t;
  2474. PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
  2475. mutex_lock(&pktgen_thread_lock);
  2476. list_for_each_entry(t, &pktgen_threads, th_list)
  2477. t->control |= T_STOP;
  2478. mutex_unlock(&pktgen_thread_lock);
  2479. }
  2480. static int thread_is_running(struct pktgen_thread *t)
  2481. {
  2482. struct pktgen_dev *pkt_dev;
  2483. int res = 0;
  2484. list_for_each_entry(pkt_dev, &t->if_list, list)
  2485. if (pkt_dev->running) {
  2486. res = 1;
  2487. break;
  2488. }
  2489. return res;
  2490. }
  2491. static int pktgen_wait_thread_run(struct pktgen_thread *t)
  2492. {
  2493. if_lock(t);
  2494. while (thread_is_running(t)) {
  2495. if_unlock(t);
  2496. msleep_interruptible(100);
  2497. if (signal_pending(current))
  2498. goto signal;
  2499. if_lock(t);
  2500. }
  2501. if_unlock(t);
  2502. return 1;
  2503. signal:
  2504. return 0;
  2505. }
  2506. static int pktgen_wait_all_threads_run(void)
  2507. {
  2508. struct pktgen_thread *t;
  2509. int sig = 1;
  2510. mutex_lock(&pktgen_thread_lock);
  2511. list_for_each_entry(t, &pktgen_threads, th_list) {
  2512. sig = pktgen_wait_thread_run(t);
  2513. if (sig == 0)
  2514. break;
  2515. }
  2516. if (sig == 0)
  2517. list_for_each_entry(t, &pktgen_threads, th_list)
  2518. t->control |= (T_STOP);
  2519. mutex_unlock(&pktgen_thread_lock);
  2520. return sig;
  2521. }
  2522. static void pktgen_run_all_threads(void)
  2523. {
  2524. struct pktgen_thread *t;
  2525. PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
  2526. mutex_lock(&pktgen_thread_lock);
  2527. list_for_each_entry(t, &pktgen_threads, th_list)
  2528. t->control |= (T_RUN);
  2529. mutex_unlock(&pktgen_thread_lock);
  2530. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  2531. pktgen_wait_all_threads_run();
  2532. }
  2533. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2534. {
  2535. __u64 total_us, bps, mbps, pps, idle;
  2536. char *p = pkt_dev->result;
  2537. total_us = pkt_dev->stopped_at - pkt_dev->started_at;
  2538. idle = pkt_dev->idle_acc;
  2539. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2540. (unsigned long long)total_us,
  2541. (unsigned long long)(total_us - idle),
  2542. (unsigned long long)idle,
  2543. (unsigned long long)pkt_dev->sofar,
  2544. pkt_dev->cur_pkt_size, nr_frags);
  2545. pps = pkt_dev->sofar * USEC_PER_SEC;
  2546. while ((total_us >> 32) != 0) {
  2547. pps >>= 1;
  2548. total_us >>= 1;
  2549. }
  2550. do_div(pps, total_us);
  2551. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2552. mbps = bps;
  2553. do_div(mbps, 1000000);
  2554. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2555. (unsigned long long)pps,
  2556. (unsigned long long)mbps,
  2557. (unsigned long long)bps,
  2558. (unsigned long long)pkt_dev->errors);
  2559. }
  2560. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2561. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2562. {
  2563. int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
  2564. if (!pkt_dev->running) {
  2565. printk("pktgen: interface: %s is already stopped\n",
  2566. pkt_dev->ifname);
  2567. return -EINVAL;
  2568. }
  2569. pkt_dev->stopped_at = getCurUs();
  2570. pkt_dev->running = 0;
  2571. show_results(pkt_dev, nr_frags);
  2572. return 0;
  2573. }
  2574. static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
  2575. {
  2576. struct pktgen_dev *pkt_dev, *best = NULL;
  2577. if_lock(t);
  2578. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2579. if (!pkt_dev->running)
  2580. continue;
  2581. if (best == NULL)
  2582. best = pkt_dev;
  2583. else if (pkt_dev->next_tx_us < best->next_tx_us)
  2584. best = pkt_dev;
  2585. }
  2586. if_unlock(t);
  2587. return best;
  2588. }
  2589. static void pktgen_stop(struct pktgen_thread *t)
  2590. {
  2591. struct pktgen_dev *pkt_dev;
  2592. PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
  2593. if_lock(t);
  2594. list_for_each_entry(pkt_dev, &t->if_list, list) {
  2595. pktgen_stop_device(pkt_dev);
  2596. if (pkt_dev->skb)
  2597. kfree_skb(pkt_dev->skb);
  2598. pkt_dev->skb = NULL;
  2599. }
  2600. if_unlock(t);
  2601. }
  2602. /*
  2603. * one of our devices needs to be removed - find it
  2604. * and remove it
  2605. */
  2606. static void pktgen_rem_one_if(struct pktgen_thread *t)
  2607. {
  2608. struct list_head *q, *n;
  2609. struct pktgen_dev *cur;
  2610. PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
  2611. if_lock(t);
  2612. list_for_each_safe(q, n, &t->if_list) {
  2613. cur = list_entry(q, struct pktgen_dev, list);
  2614. if (!cur->removal_mark)
  2615. continue;
  2616. if (cur->skb)
  2617. kfree_skb(cur->skb);
  2618. cur->skb = NULL;
  2619. pktgen_remove_device(t, cur);
  2620. break;
  2621. }
  2622. if_unlock(t);
  2623. }
  2624. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2625. {
  2626. struct list_head *q, *n;
  2627. struct pktgen_dev *cur;
  2628. /* Remove all devices, free mem */
  2629. PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
  2630. if_lock(t);
  2631. list_for_each_safe(q, n, &t->if_list) {
  2632. cur = list_entry(q, struct pktgen_dev, list);
  2633. if (cur->skb)
  2634. kfree_skb(cur->skb);
  2635. cur->skb = NULL;
  2636. pktgen_remove_device(t, cur);
  2637. }
  2638. if_unlock(t);
  2639. }
  2640. static void pktgen_rem_thread(struct pktgen_thread *t)
  2641. {
  2642. /* Remove from the thread list */
  2643. remove_proc_entry(t->name, pg_proc_dir);
  2644. mutex_lock(&pktgen_thread_lock);
  2645. list_del(&t->th_list);
  2646. mutex_unlock(&pktgen_thread_lock);
  2647. }
  2648. static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2649. {
  2650. struct net_device *odev = NULL;
  2651. __u64 idle_start = 0;
  2652. int ret;
  2653. odev = pkt_dev->odev;
  2654. if (pkt_dev->delay_us || pkt_dev->delay_ns) {
  2655. u64 now;
  2656. now = getCurUs();
  2657. if (now < pkt_dev->next_tx_us)
  2658. spin(pkt_dev, pkt_dev->next_tx_us);
  2659. /* This is max DELAY, this has special meaning of
  2660. * "never transmit"
  2661. */
  2662. if (pkt_dev->delay_us == 0x7FFFFFFF) {
  2663. pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
  2664. pkt_dev->next_tx_ns = pkt_dev->delay_ns;
  2665. goto out;
  2666. }
  2667. }
  2668. if (netif_queue_stopped(odev) || need_resched()) {
  2669. idle_start = getCurUs();
  2670. if (!netif_running(odev)) {
  2671. pktgen_stop_device(pkt_dev);
  2672. if (pkt_dev->skb)
  2673. kfree_skb(pkt_dev->skb);
  2674. pkt_dev->skb = NULL;
  2675. goto out;
  2676. }
  2677. if (need_resched())
  2678. schedule();
  2679. pkt_dev->idle_acc += getCurUs() - idle_start;
  2680. if (netif_queue_stopped(odev)) {
  2681. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2682. pkt_dev->next_tx_ns = 0;
  2683. goto out; /* Try the next interface */
  2684. }
  2685. }
  2686. if (pkt_dev->last_ok || !pkt_dev->skb) {
  2687. if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
  2688. || (!pkt_dev->skb)) {
  2689. /* build a new pkt */
  2690. if (pkt_dev->skb)
  2691. kfree_skb(pkt_dev->skb);
  2692. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2693. if (pkt_dev->skb == NULL) {
  2694. printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
  2695. schedule();
  2696. pkt_dev->clone_count--; /* back out increment, OOM */
  2697. goto out;
  2698. }
  2699. pkt_dev->allocated_skbs++;
  2700. pkt_dev->clone_count = 0; /* reset counter */
  2701. }
  2702. }
  2703. netif_tx_lock_bh(odev);
  2704. if (!netif_queue_stopped(odev)) {
  2705. atomic_inc(&(pkt_dev->skb->users));
  2706. retry_now:
  2707. ret = odev->hard_start_xmit(pkt_dev->skb, odev);
  2708. if (likely(ret == NETDEV_TX_OK)) {
  2709. pkt_dev->last_ok = 1;
  2710. pkt_dev->sofar++;
  2711. pkt_dev->seq_num++;
  2712. pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
  2713. } else if (ret == NETDEV_TX_LOCKED
  2714. && (odev->features & NETIF_F_LLTX)) {
  2715. cpu_relax();
  2716. goto retry_now;
  2717. } else { /* Retry it next time */
  2718. atomic_dec(&(pkt_dev->skb->users));
  2719. if (debug && net_ratelimit())
  2720. printk(KERN_INFO "pktgen: Hard xmit error\n");
  2721. pkt_dev->errors++;
  2722. pkt_dev->last_ok = 0;
  2723. }
  2724. pkt_dev->next_tx_us = getCurUs();
  2725. pkt_dev->next_tx_ns = 0;
  2726. pkt_dev->next_tx_us += pkt_dev->delay_us;
  2727. pkt_dev->next_tx_ns += pkt_dev->delay_ns;
  2728. if (pkt_dev->next_tx_ns > 1000) {
  2729. pkt_dev->next_tx_us++;
  2730. pkt_dev->next_tx_ns -= 1000;
  2731. }
  2732. }
  2733. else { /* Retry it next time */
  2734. pkt_dev->last_ok = 0;
  2735. pkt_dev->next_tx_us = getCurUs(); /* TODO */
  2736. pkt_dev->next_tx_ns = 0;
  2737. }
  2738. netif_tx_unlock_bh(odev);
  2739. /* If pkt_dev->count is zero, then run forever */
  2740. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  2741. if (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2742. idle_start = getCurUs();
  2743. while (atomic_read(&(pkt_dev->skb->users)) != 1) {
  2744. if (signal_pending(current)) {
  2745. break;
  2746. }
  2747. schedule();
  2748. }
  2749. pkt_dev->idle_acc += getCurUs() - idle_start;
  2750. }
  2751. /* Done with this */
  2752. pktgen_stop_device(pkt_dev);
  2753. if (pkt_dev->skb)
  2754. kfree_skb(pkt_dev->skb);
  2755. pkt_dev->skb = NULL;
  2756. }
  2757. out:;
  2758. }
  2759. /*
  2760. * Main loop of the thread goes here
  2761. */
  2762. static void pktgen_thread_worker(struct pktgen_thread *t)
  2763. {
  2764. DEFINE_WAIT(wait);
  2765. struct pktgen_dev *pkt_dev = NULL;
  2766. int cpu = t->cpu;
  2767. sigset_t tmpsig;
  2768. u32 max_before_softirq;
  2769. u32 tx_since_softirq = 0;
  2770. daemonize("pktgen/%d", cpu);
  2771. /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
  2772. spin_lock_irq(&current->sighand->siglock);
  2773. tmpsig = current->blocked;
  2774. siginitsetinv(&current->blocked,
  2775. sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
  2776. recalc_sigpending();
  2777. spin_unlock_irq(&current->sighand->siglock);
  2778. /* Migrate to the right CPU */
  2779. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  2780. if (smp_processor_id() != cpu)
  2781. BUG();
  2782. init_waitqueue_head(&t->queue);
  2783. t->control &= ~(T_TERMINATE);
  2784. t->control &= ~(T_RUN);
  2785. t->control &= ~(T_STOP);
  2786. t->control &= ~(T_REMDEVALL);
  2787. t->control &= ~(T_REMDEV);
  2788. t->pid = current->pid;
  2789. PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
  2790. max_before_softirq = t->max_before_softirq;
  2791. __set_current_state(TASK_INTERRUPTIBLE);
  2792. mb();
  2793. while (1) {
  2794. __set_current_state(TASK_RUNNING);
  2795. /*
  2796. * Get next dev to xmit -- if any.
  2797. */
  2798. pkt_dev = next_to_run(t);
  2799. if (pkt_dev) {
  2800. pktgen_xmit(pkt_dev);
  2801. /*
  2802. * We like to stay RUNNING but must also give
  2803. * others fair share.
  2804. */
  2805. tx_since_softirq += pkt_dev->last_ok;
  2806. if (tx_since_softirq > max_before_softirq) {
  2807. if (local_softirq_pending())
  2808. do_softirq();
  2809. tx_since_softirq = 0;
  2810. }
  2811. } else {
  2812. prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
  2813. schedule_timeout(HZ / 10);
  2814. finish_wait(&(t->queue), &wait);
  2815. }
  2816. /*
  2817. * Back from sleep, either due to the timeout or signal.
  2818. * We check if we have any "posted" work for us.
  2819. */
  2820. if (t->control & T_TERMINATE || signal_pending(current))
  2821. /* we received a request to terminate ourself */
  2822. break;
  2823. if (t->control & T_STOP) {
  2824. pktgen_stop(t);
  2825. t->control &= ~(T_STOP);
  2826. }
  2827. if (t->control & T_RUN) {
  2828. pktgen_run(t);
  2829. t->control &= ~(T_RUN);
  2830. }
  2831. if (t->control & T_REMDEVALL) {
  2832. pktgen_rem_all_ifs(t);
  2833. t->control &= ~(T_REMDEVALL);
  2834. }
  2835. if (t->control & T_REMDEV) {
  2836. pktgen_rem_one_if(t);
  2837. t->control &= ~(T_REMDEV);
  2838. }
  2839. if (need_resched())
  2840. schedule();
  2841. }
  2842. PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
  2843. pktgen_stop(t);
  2844. PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
  2845. pktgen_rem_all_ifs(t);
  2846. PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
  2847. pktgen_rem_thread(t);
  2848. t->removed = 1;
  2849. }
  2850. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  2851. const char *ifname)
  2852. {
  2853. struct pktgen_dev *p, *pkt_dev = NULL;
  2854. if_lock(t);
  2855. list_for_each_entry(p, &t->if_list, list)
  2856. if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
  2857. pkt_dev = p;
  2858. break;
  2859. }
  2860. if_unlock(t);
  2861. PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
  2862. return pkt_dev;
  2863. }
  2864. /*
  2865. * Adds a dev at front of if_list.
  2866. */
  2867. static int add_dev_to_thread(struct pktgen_thread *t,
  2868. struct pktgen_dev *pkt_dev)
  2869. {
  2870. int rv = 0;
  2871. if_lock(t);
  2872. if (pkt_dev->pg_thread) {
  2873. printk("pktgen: ERROR: already assigned to a thread.\n");
  2874. rv = -EBUSY;
  2875. goto out;
  2876. }
  2877. list_add(&pkt_dev->list, &t->if_list);
  2878. pkt_dev->pg_thread = t;
  2879. pkt_dev->running = 0;
  2880. out:
  2881. if_unlock(t);
  2882. return rv;
  2883. }
  2884. /* Called under thread lock */
  2885. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
  2886. {
  2887. struct pktgen_dev *pkt_dev;
  2888. struct proc_dir_entry *pe;
  2889. /* We don't allow a device to be on several threads */
  2890. pkt_dev = __pktgen_NN_threads(ifname, FIND);
  2891. if (pkt_dev) {
  2892. printk("pktgen: ERROR: interface already used.\n");
  2893. return -EBUSY;
  2894. }
  2895. pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
  2896. if (!pkt_dev)
  2897. return -ENOMEM;
  2898. pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
  2899. if (pkt_dev->flows == NULL) {
  2900. kfree(pkt_dev);
  2901. return -ENOMEM;
  2902. }
  2903. memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
  2904. pkt_dev->removal_mark = 0;
  2905. pkt_dev->min_pkt_size = ETH_ZLEN;
  2906. pkt_dev->max_pkt_size = ETH_ZLEN;
  2907. pkt_dev->nfrags = 0;
  2908. pkt_dev->clone_skb = pg_clone_skb_d;
  2909. pkt_dev->delay_us = pg_delay_d / 1000;
  2910. pkt_dev->delay_ns = pg_delay_d % 1000;
  2911. pkt_dev->count = pg_count_d;
  2912. pkt_dev->sofar = 0;
  2913. pkt_dev->udp_src_min = 9; /* sink port */
  2914. pkt_dev->udp_src_max = 9;
  2915. pkt_dev->udp_dst_min = 9;
  2916. pkt_dev->udp_dst_max = 9;
  2917. pkt_dev->vlan_p = 0;
  2918. pkt_dev->vlan_cfi = 0;
  2919. pkt_dev->vlan_id = 0xffff;
  2920. pkt_dev->svlan_p = 0;
  2921. pkt_dev->svlan_cfi = 0;
  2922. pkt_dev->svlan_id = 0xffff;
  2923. strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
  2924. if (!pktgen_setup_dev(pkt_dev)) {
  2925. printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
  2926. if (pkt_dev->flows)
  2927. vfree(pkt_dev->flows);
  2928. kfree(pkt_dev);
  2929. return -ENODEV;
  2930. }
  2931. pe = create_proc_entry(ifname, 0600, pg_proc_dir);
  2932. if (!pe) {
  2933. printk("pktgen: cannot create %s/%s procfs entry.\n",
  2934. PG_PROC_DIR, ifname);
  2935. if (pkt_dev->flows)
  2936. vfree(pkt_dev->flows);
  2937. kfree(pkt_dev);
  2938. return -EINVAL;
  2939. }
  2940. pe->proc_fops = &pktgen_if_fops;
  2941. pe->data = pkt_dev;
  2942. return add_dev_to_thread(t, pkt_dev);
  2943. }
  2944. static struct pktgen_thread *__init pktgen_find_thread(const char *name)
  2945. {
  2946. struct pktgen_thread *t;
  2947. mutex_lock(&pktgen_thread_lock);
  2948. list_for_each_entry(t, &pktgen_threads, th_list)
  2949. if (strcmp(t->name, name) == 0) {
  2950. mutex_unlock(&pktgen_thread_lock);
  2951. return t;
  2952. }
  2953. mutex_unlock(&pktgen_thread_lock);
  2954. return NULL;
  2955. }
  2956. static int __init pktgen_create_thread(const char *name, int cpu)
  2957. {
  2958. int err;
  2959. struct pktgen_thread *t = NULL;
  2960. struct proc_dir_entry *pe;
  2961. if (strlen(name) > 31) {
  2962. printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
  2963. return -EINVAL;
  2964. }
  2965. if (pktgen_find_thread(name)) {
  2966. printk("pktgen: ERROR: thread: %s already exists\n", name);
  2967. return -EINVAL;
  2968. }
  2969. t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
  2970. if (!t) {
  2971. printk("pktgen: ERROR: out of memory, can't create new thread.\n");
  2972. return -ENOMEM;
  2973. }
  2974. strcpy(t->name, name);
  2975. spin_lock_init(&t->if_lock);
  2976. t->cpu = cpu;
  2977. pe = create_proc_entry(t->name, 0600, pg_proc_dir);
  2978. if (!pe) {
  2979. printk("pktgen: cannot create %s/%s procfs entry.\n",
  2980. PG_PROC_DIR, t->name);
  2981. kfree(t);
  2982. return -EINVAL;
  2983. }
  2984. pe->proc_fops = &pktgen_thread_fops;
  2985. pe->data = t;
  2986. INIT_LIST_HEAD(&t->if_list);
  2987. list_add_tail(&t->th_list, &pktgen_threads);
  2988. t->removed = 0;
  2989. err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
  2990. CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  2991. if (err < 0) {
  2992. printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
  2993. remove_proc_entry(t->name, pg_proc_dir);
  2994. list_del(&t->th_list);
  2995. kfree(t);
  2996. return err;
  2997. }
  2998. return 0;
  2999. }
  3000. /*
  3001. * Removes a device from the thread if_list.
  3002. */
  3003. static void _rem_dev_from_if_list(struct pktgen_thread *t,
  3004. struct pktgen_dev *pkt_dev)
  3005. {
  3006. struct list_head *q, *n;
  3007. struct pktgen_dev *p;
  3008. list_for_each_safe(q, n, &t->if_list) {
  3009. p = list_entry(q, struct pktgen_dev, list);
  3010. if (p == pkt_dev)
  3011. list_del(&p->list);
  3012. }
  3013. }
  3014. static int pktgen_remove_device(struct pktgen_thread *t,
  3015. struct pktgen_dev *pkt_dev)
  3016. {
  3017. PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
  3018. if (pkt_dev->running) {
  3019. printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
  3020. pktgen_stop_device(pkt_dev);
  3021. }
  3022. /* Dis-associate from the interface */
  3023. if (pkt_dev->odev) {
  3024. dev_put(pkt_dev->odev);
  3025. pkt_dev->odev = NULL;
  3026. }
  3027. /* And update the thread if_list */
  3028. _rem_dev_from_if_list(t, pkt_dev);
  3029. /* Clean up proc file system */
  3030. remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
  3031. if (pkt_dev->flows)
  3032. vfree(pkt_dev->flows);
  3033. kfree(pkt_dev);
  3034. return 0;
  3035. }
  3036. static int __init pg_init(void)
  3037. {
  3038. int cpu;
  3039. struct proc_dir_entry *pe;
  3040. printk(version);
  3041. pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
  3042. if (!pg_proc_dir)
  3043. return -ENODEV;
  3044. pg_proc_dir->owner = THIS_MODULE;
  3045. pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
  3046. if (pe == NULL) {
  3047. printk("pktgen: ERROR: cannot create %s procfs entry.\n",
  3048. PGCTRL);
  3049. proc_net_remove(PG_PROC_DIR);
  3050. return -EINVAL;
  3051. }
  3052. pe->proc_fops = &pktgen_fops;
  3053. pe->data = NULL;
  3054. /* Register us to receive netdevice events */
  3055. register_netdevice_notifier(&pktgen_notifier_block);
  3056. for_each_online_cpu(cpu) {
  3057. int err;
  3058. char buf[30];
  3059. sprintf(buf, "kpktgend_%i", cpu);
  3060. err = pktgen_create_thread(buf, cpu);
  3061. if (err)
  3062. printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
  3063. cpu, err);
  3064. }
  3065. if (list_empty(&pktgen_threads)) {
  3066. printk("pktgen: ERROR: Initialization failed for all threads\n");
  3067. unregister_netdevice_notifier(&pktgen_notifier_block);
  3068. remove_proc_entry(PGCTRL, pg_proc_dir);
  3069. proc_net_remove(PG_PROC_DIR);
  3070. return -ENODEV;
  3071. }
  3072. return 0;
  3073. }
  3074. static void __exit pg_cleanup(void)
  3075. {
  3076. struct pktgen_thread *t;
  3077. struct list_head *q, *n;
  3078. wait_queue_head_t queue;
  3079. init_waitqueue_head(&queue);
  3080. /* Stop all interfaces & threads */
  3081. list_for_each_safe(q, n, &pktgen_threads) {
  3082. t = list_entry(q, struct pktgen_thread, th_list);
  3083. t->control |= (T_TERMINATE);
  3084. wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
  3085. }
  3086. /* Un-register us from receiving netdevice events */
  3087. unregister_netdevice_notifier(&pktgen_notifier_block);
  3088. /* Clean up proc file system */
  3089. remove_proc_entry(PGCTRL, pg_proc_dir);
  3090. proc_net_remove(PG_PROC_DIR);
  3091. }
  3092. module_init(pg_init);
  3093. module_exit(pg_cleanup);
  3094. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
  3095. MODULE_DESCRIPTION("Packet Generator tool");
  3096. MODULE_LICENSE("GPL");
  3097. module_param(pg_count_d, int, 0);
  3098. module_param(pg_delay_d, int, 0);
  3099. module_param(pg_clone_skb_d, int, 0);
  3100. module_param(debug, int, 0);