pktgen.c 75 KB

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