pktgen.c 80 KB

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