pktgen.c 84 KB

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