pktgen.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Authors:
  4. * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
  5. * Uppsala University and
  6. * Swedish University of Agricultural Sciences
  7. *
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. * Ben Greear <greearb@candelatech.com>
  10. * Jens Låås <jens.laas@data.slu.se>
  11. *
  12. * A tool for loading the network with preconfigurated packets.
  13. * The tool is implemented as a linux module. Parameters are output
  14. * device, delay (to hard_xmit), number of packets, and whether
  15. * to use multiple SKBs or just the same one.
  16. * pktgen uses the installed interface's output routine.
  17. *
  18. * Additional hacking by:
  19. *
  20. * Jens.Laas@data.slu.se
  21. * Improved by ANK. 010120.
  22. * Improved by ANK even more. 010212.
  23. * MAC address typo fixed. 010417 --ro
  24. * Integrated. 020301 --DaveM
  25. * Added multiskb option 020301 --DaveM
  26. * Scaling of results. 020417--sigurdur@linpro.no
  27. * Significant re-work of the module:
  28. * * Convert to threaded model to more efficiently be able to transmit
  29. * and receive on multiple interfaces at once.
  30. * * Converted many counters to __u64 to allow longer runs.
  31. * * Allow configuration of ranges, like min/max IP address, MACs,
  32. * and UDP-ports, for both source and destination, and can
  33. * set to use a random distribution or sequentially walk the range.
  34. * * Can now change most values after starting.
  35. * * Place 12-byte packet in UDP payload with magic number,
  36. * sequence number, and timestamp.
  37. * * Add receiver code that detects dropped pkts, re-ordered pkts, and
  38. * latencies (with micro-second) precision.
  39. * * Add IOCTL interface to easily get counters & configuration.
  40. * --Ben Greear <greearb@candelatech.com>
  41. *
  42. * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
  43. * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
  44. * as a "fastpath" with a configurable number of clones after alloc's.
  45. * clone_skb=0 means all packets are allocated this also means ranges time
  46. * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
  47. * clones.
  48. *
  49. * Also moved to /proc/net/pktgen/
  50. * --ro
  51. *
  52. * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
  53. * mistakes. Also merged in DaveM's patch in the -pre6 patch.
  54. * --Ben Greear <greearb@candelatech.com>
  55. *
  56. * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
  57. *
  58. * 021124 Finished major redesign and rewrite for new functionality.
  59. * See Documentation/networking/pktgen.rst for how to use this.
  60. *
  61. * The new operation:
  62. * For each CPU one thread/process is created at start. This process checks
  63. * for running devices in the if_list and sends packets until count is 0 it
  64. * also the thread checks the thread->control which is used for inter-process
  65. * communication. controlling process "posts" operations to the threads this
  66. * way.
  67. * The if_list is RCU protected, and the if_lock remains to protect updating
  68. * of if_list, from "add_device" as it invoked from userspace (via proc write).
  69. *
  70. * By design there should only be *one* "controlling" process. In practice
  71. * multiple write accesses gives unpredictable result. Understood by "write"
  72. * to /proc gives result code that should be read be the "writer".
  73. * For practical use this should be no problem.
  74. *
  75. * Note when adding devices to a specific CPU there good idea to also assign
  76. * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
  77. * --ro
  78. *
  79. * Fix refcount off by one if first packet fails, potential null deref,
  80. * memleak 030710- KJP
  81. *
  82. * First "ranges" functionality for ipv6 030726 --ro
  83. *
  84. * Included flow support. 030802 ANK.
  85. *
  86. * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
  87. *
  88. * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
  89. * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
  90. *
  91. * New xmit() return, do_div and misc clean up by Stephen Hemminger
  92. * <shemminger@osdl.org> 040923
  93. *
  94. * Randy Dunlap fixed u64 printk compiler warning
  95. *
  96. * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
  97. * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
  98. *
  99. * Corrections from Nikolai Malykh (nmalykh@bilim.com)
  100. * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
  101. *
  102. * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
  103. * 050103
  104. *
  105. * MPLS support by Steven Whitehouse <steve@chygwyn.com>
  106. *
  107. * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
  108. *
  109. * Fixed src_mac command to set source mac of packet to value specified in
  110. * command by Adit Ranadive <adit.262@gmail.com>
  111. */
  112. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  113. #include <linux/sys.h>
  114. #include <linux/types.h>
  115. #include <linux/minmax.h>
  116. #include <linux/module.h>
  117. #include <linux/moduleparam.h>
  118. #include <linux/kernel.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/hex.h>
  128. #include <linux/ioport.h>
  129. #include <linux/interrupt.h>
  130. #include <linux/capability.h>
  131. #include <linux/hrtimer.h>
  132. #include <linux/freezer.h>
  133. #include <linux/delay.h>
  134. #include <linux/timer.h>
  135. #include <linux/list.h>
  136. #include <linux/init.h>
  137. #include <linux/skbuff.h>
  138. #include <linux/netdevice.h>
  139. #include <linux/inet.h>
  140. #include <linux/inetdevice.h>
  141. #include <linux/rtnetlink.h>
  142. #include <linux/if_arp.h>
  143. #include <linux/if_vlan.h>
  144. #include <linux/in.h>
  145. #include <linux/ip.h>
  146. #include <linux/ipv6.h>
  147. #include <linux/udp.h>
  148. #include <linux/proc_fs.h>
  149. #include <linux/seq_file.h>
  150. #include <linux/wait.h>
  151. #include <linux/etherdevice.h>
  152. #include <linux/kthread.h>
  153. #include <linux/prefetch.h>
  154. #include <linux/mmzone.h>
  155. #include <net/net_namespace.h>
  156. #include <net/checksum.h>
  157. #include <net/ipv6.h>
  158. #include <net/udp.h>
  159. #include <net/ip6_checksum.h>
  160. #include <net/addrconf.h>
  161. #include <net/xfrm.h>
  162. #include <net/netns/generic.h>
  163. #include <asm/byteorder.h>
  164. #include <linux/rcupdate.h>
  165. #include <linux/bitops.h>
  166. #include <linux/io.h>
  167. #include <linux/timex.h>
  168. #include <linux/uaccess.h>
  169. #include <asm/dma.h>
  170. #include <asm/div64.h> /* do_div */
  171. #define VERSION "2.75"
  172. #define IP_NAME_SZ 32
  173. #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
  174. #define MPLS_STACK_BOTTOM htonl(0x00000100)
  175. /* Max number of internet mix entries that can be specified in imix_weights. */
  176. #define MAX_IMIX_ENTRIES 20
  177. #define IMIX_PRECISION 100 /* Precision of IMIX distribution */
  178. #define func_enter() pr_debug("entering %s\n", __func__)
  179. #define PKT_FLAGS \
  180. pf(IPV6) /* Interface in IPV6 Mode */ \
  181. pf(IPSRC_RND) /* IP-Src Random */ \
  182. pf(IPDST_RND) /* IP-Dst Random */ \
  183. pf(TXSIZE_RND) /* Transmit size is random */ \
  184. pf(UDPSRC_RND) /* UDP-Src Random */ \
  185. pf(UDPDST_RND) /* UDP-Dst Random */ \
  186. pf(UDPCSUM) /* Include UDP checksum */ \
  187. pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
  188. pf(MPLS_RND) /* Random MPLS labels */ \
  189. pf(QUEUE_MAP_RND) /* queue map Random */ \
  190. pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
  191. pf(FLOW_SEQ) /* Sequential flows */ \
  192. pf(IPSEC) /* ipsec on for flows */ \
  193. pf(MACSRC_RND) /* MAC-Src Random */ \
  194. pf(MACDST_RND) /* MAC-Dst Random */ \
  195. pf(VID_RND) /* Random VLAN ID */ \
  196. pf(SVID_RND) /* Random SVLAN ID */ \
  197. pf(NODE) /* Node memory alloc*/ \
  198. pf(SHARED) /* Shared SKB */ \
  199. #define pf(flag) flag##_SHIFT,
  200. enum pkt_flags {
  201. PKT_FLAGS
  202. };
  203. #undef pf
  204. /* Device flag bits */
  205. #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
  206. PKT_FLAGS
  207. #undef pf
  208. #define pf(flag) __stringify(flag),
  209. static char *pkt_flag_names[] = {
  210. PKT_FLAGS
  211. };
  212. #undef pf
  213. #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
  214. /* Thread control flag bits */
  215. #define T_STOP (1<<0) /* Stop run */
  216. #define T_RUN (1<<1) /* Start run */
  217. #define T_REMDEVALL (1<<2) /* Remove all devs */
  218. #define T_REMDEV (1<<3) /* Remove one dev */
  219. /* Xmit modes */
  220. #define M_START_XMIT 0 /* Default normal TX */
  221. #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
  222. #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
  223. /* If lock -- protects updating of if_list */
  224. #define if_lock(t) mutex_lock(&(t->if_lock))
  225. #define if_unlock(t) mutex_unlock(&(t->if_lock))
  226. /* Used to help with determining the pkts on receive */
  227. #define PKTGEN_MAGIC 0xbe9be955
  228. #define PG_PROC_DIR "pktgen"
  229. #define PGCTRL "pgctrl"
  230. #define MAX_CFLOWS 65536
  231. #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
  232. #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
  233. struct imix_pkt {
  234. u64 size;
  235. u64 weight;
  236. u64 count_so_far;
  237. };
  238. struct flow_state {
  239. __be32 cur_daddr;
  240. int count;
  241. #ifdef CONFIG_XFRM
  242. struct xfrm_state *x;
  243. #endif
  244. __u32 flags;
  245. };
  246. /* flow flag bits */
  247. #define F_INIT (1<<0) /* flow has been initialized */
  248. struct pktgen_dev {
  249. /*
  250. * Try to keep frequent/infrequent used vars. separated.
  251. */
  252. struct proc_dir_entry *entry; /* proc file */
  253. struct pktgen_thread *pg_thread;/* the owner */
  254. struct list_head list; /* chaining in the thread's run-queue */
  255. struct rcu_head rcu; /* freed by RCU */
  256. int running; /* if false, the test will stop */
  257. /* If min != max, then we will either do a linear iteration, or
  258. * we will do a random selection from within the range.
  259. */
  260. __u32 flags;
  261. int xmit_mode;
  262. int min_pkt_size;
  263. int max_pkt_size;
  264. int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
  265. int nfrags;
  266. int removal_mark; /* non-zero => the device is marked for
  267. * removal by worker thread
  268. */
  269. struct page *page;
  270. u64 delay; /* nano-seconds */
  271. __u64 count; /* Default No packets to send */
  272. __u64 sofar; /* How many pkts we've sent so far */
  273. __u64 tx_bytes; /* How many bytes we've transmitted */
  274. __u64 errors; /* Errors when trying to transmit, */
  275. /* runtime counters relating to clone_skb */
  276. __u32 clone_count;
  277. int last_ok; /* Was last skb sent?
  278. * Or a failed transmit of some sort?
  279. * This will keep sequence numbers in order
  280. */
  281. ktime_t next_tx;
  282. ktime_t started_at;
  283. ktime_t stopped_at;
  284. u64 idle_acc; /* nano-seconds */
  285. __u32 seq_num;
  286. int clone_skb; /*
  287. * Use multiple SKBs during packet gen.
  288. * If this number is greater than 1, then
  289. * that many copies of the same packet will be
  290. * sent before a new packet is allocated.
  291. * If you want to send 1024 identical packets
  292. * before creating a new packet,
  293. * set clone_skb to 1024.
  294. */
  295. char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  296. char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  297. char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  298. char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
  299. struct in6_addr in6_saddr;
  300. struct in6_addr in6_daddr;
  301. struct in6_addr cur_in6_daddr;
  302. struct in6_addr cur_in6_saddr;
  303. /* For ranges */
  304. struct in6_addr min_in6_daddr;
  305. struct in6_addr max_in6_daddr;
  306. struct in6_addr min_in6_saddr;
  307. struct in6_addr max_in6_saddr;
  308. /* If we're doing ranges, random or incremental, then this
  309. * defines the min/max for those ranges.
  310. */
  311. __be32 saddr_min; /* inclusive, source IP address */
  312. __be32 saddr_max; /* exclusive, source IP address */
  313. __be32 daddr_min; /* inclusive, dest IP address */
  314. __be32 daddr_max; /* exclusive, dest IP address */
  315. __u16 udp_src_min; /* inclusive, source UDP port */
  316. __u16 udp_src_max; /* exclusive, source UDP port */
  317. __u16 udp_dst_min; /* inclusive, dest UDP port */
  318. __u16 udp_dst_max; /* exclusive, dest UDP port */
  319. /* DSCP + ECN */
  320. __u8 tos; /* six MSB of (former) IPv4 TOS
  321. * are for dscp codepoint
  322. */
  323. __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
  324. * (see RFC 3260, sec. 4)
  325. */
  326. /* IMIX */
  327. unsigned int n_imix_entries;
  328. struct imix_pkt imix_entries[MAX_IMIX_ENTRIES];
  329. /* Maps 0-IMIX_PRECISION range to imix_entry based on probability*/
  330. __u8 imix_distribution[IMIX_PRECISION];
  331. /* MPLS */
  332. unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
  333. __be32 labels[MAX_MPLS_LABELS];
  334. /* VLAN/SVLAN (802.1Q/Q-in-Q) */
  335. __u8 vlan_p;
  336. __u8 vlan_cfi;
  337. __u16 vlan_id; /* 0xffff means no vlan tag */
  338. __u8 svlan_p;
  339. __u8 svlan_cfi;
  340. __u16 svlan_id; /* 0xffff means no svlan tag */
  341. __u32 src_mac_count; /* How many MACs to iterate through */
  342. __u32 dst_mac_count; /* How many MACs to iterate through */
  343. unsigned char dst_mac[ETH_ALEN];
  344. unsigned char src_mac[ETH_ALEN];
  345. __u32 cur_dst_mac_offset;
  346. __u32 cur_src_mac_offset;
  347. __be32 cur_saddr;
  348. __be32 cur_daddr;
  349. __u16 ip_id;
  350. __u16 cur_udp_dst;
  351. __u16 cur_udp_src;
  352. __u16 cur_queue_map;
  353. __u32 cur_pkt_size;
  354. __u32 last_pkt_size;
  355. __u8 hh[14];
  356. /* = {
  357. * 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
  358. *
  359. * We fill in SRC address later
  360. * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  361. * 0x08, 0x00
  362. * };
  363. */
  364. __u16 pad; /* pad out the hh struct to an even 16 bytes */
  365. struct sk_buff *skb; /* skb we are to transmit next, used for when we
  366. * are transmitting the same one multiple times
  367. */
  368. struct net_device *odev; /* The out-going device.
  369. * Note that the device should have it's
  370. * pg_info pointer pointing back to this
  371. * device.
  372. * Set when the user specifies the out-going
  373. * device name (not when the inject is
  374. * started as it used to do.)
  375. */
  376. netdevice_tracker dev_tracker;
  377. char odevname[32];
  378. struct flow_state *flows;
  379. unsigned int cflows; /* Concurrent flows (config) */
  380. unsigned int lflow; /* Flow length (config) */
  381. unsigned int nflows; /* accumulated flows (stats) */
  382. unsigned int curfl; /* current sequenced flow (state)*/
  383. u16 queue_map_min;
  384. u16 queue_map_max;
  385. __u32 skb_priority; /* skb priority field */
  386. unsigned int burst; /* number of duplicated packets to burst */
  387. int node; /* Memory node */
  388. #ifdef CONFIG_XFRM
  389. __u8 ipsmode; /* IPSEC mode (config) */
  390. __u8 ipsproto; /* IPSEC type (config) */
  391. __u32 spi;
  392. struct xfrm_dst xdst;
  393. struct dst_ops dstops;
  394. #endif
  395. char result[512];
  396. };
  397. struct pktgen_hdr {
  398. __be32 pgh_magic;
  399. __be32 seq_num;
  400. __be32 tv_sec;
  401. __be32 tv_usec;
  402. };
  403. static unsigned int pg_net_id __read_mostly;
  404. struct pktgen_net {
  405. struct net *net;
  406. struct proc_dir_entry *proc_dir;
  407. struct list_head pktgen_threads;
  408. bool pktgen_exiting;
  409. };
  410. struct pktgen_thread {
  411. struct mutex if_lock; /* for list of devices */
  412. struct list_head if_list; /* All device here */
  413. struct list_head th_list;
  414. struct task_struct *tsk;
  415. char result[512];
  416. /* Field for thread to receive "posted" events terminate,
  417. * stop ifs etc.
  418. */
  419. u32 control;
  420. int cpu;
  421. wait_queue_head_t queue;
  422. struct completion start_done;
  423. struct pktgen_net *net;
  424. };
  425. #define REMOVE 1
  426. #define FIND 0
  427. static const char version[] =
  428. "Packet Generator for packet performance testing. Version: " VERSION "\n";
  429. static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
  430. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
  431. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  432. const char *ifname, bool exact);
  433. static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
  434. static void pktgen_run_all_threads(struct pktgen_net *pn);
  435. static void pktgen_reset_all_threads(struct pktgen_net *pn);
  436. static void pktgen_stop_all_threads(struct pktgen_net *pn);
  437. static void pktgen_stop(struct pktgen_thread *t);
  438. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
  439. static void fill_imix_distribution(struct pktgen_dev *pkt_dev);
  440. /* Module parameters, defaults. */
  441. static int pg_count_d __read_mostly = 1000;
  442. static int pg_delay_d __read_mostly;
  443. static int pg_clone_skb_d __read_mostly;
  444. static int debug __read_mostly;
  445. static DEFINE_MUTEX(pktgen_thread_lock);
  446. static struct notifier_block pktgen_notifier_block = {
  447. .notifier_call = pktgen_device_event,
  448. };
  449. /*
  450. * /proc handling functions
  451. *
  452. */
  453. static int pgctrl_show(struct seq_file *seq, void *v)
  454. {
  455. seq_puts(seq, version);
  456. return 0;
  457. }
  458. static ssize_t pgctrl_write(struct file *file, const char __user *buf,
  459. size_t count, loff_t *ppos)
  460. {
  461. char data[128];
  462. size_t max;
  463. struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
  464. if (!capable(CAP_NET_ADMIN))
  465. return -EPERM;
  466. if (count < 1)
  467. return -EINVAL;
  468. max = min(count, sizeof(data) - 1);
  469. if (copy_from_user(data, buf, max))
  470. return -EFAULT;
  471. if (data[max - 1] == '\n')
  472. data[max - 1] = 0; /* strip trailing '\n', terminate string */
  473. else
  474. data[max] = 0; /* terminate string */
  475. if (!strcmp(data, "stop"))
  476. pktgen_stop_all_threads(pn);
  477. else if (!strcmp(data, "start"))
  478. pktgen_run_all_threads(pn);
  479. else if (!strcmp(data, "reset"))
  480. pktgen_reset_all_threads(pn);
  481. else
  482. return -EINVAL;
  483. return count;
  484. }
  485. static int pgctrl_open(struct inode *inode, struct file *file)
  486. {
  487. return single_open(file, pgctrl_show, pde_data(inode));
  488. }
  489. static const struct proc_ops pktgen_proc_ops = {
  490. .proc_open = pgctrl_open,
  491. .proc_read = seq_read,
  492. .proc_lseek = seq_lseek,
  493. .proc_write = pgctrl_write,
  494. .proc_release = single_release,
  495. };
  496. static int pktgen_if_show(struct seq_file *seq, void *v)
  497. {
  498. const struct pktgen_dev *pkt_dev = seq->private;
  499. ktime_t stopped;
  500. unsigned int i;
  501. u64 idle;
  502. seq_printf(seq,
  503. "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
  504. (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
  505. pkt_dev->max_pkt_size);
  506. if (pkt_dev->n_imix_entries > 0) {
  507. seq_puts(seq, " imix_weights: ");
  508. for (i = 0; i < pkt_dev->n_imix_entries; i++) {
  509. seq_printf(seq, "%llu,%llu ",
  510. pkt_dev->imix_entries[i].size,
  511. pkt_dev->imix_entries[i].weight);
  512. }
  513. seq_puts(seq, "\n");
  514. }
  515. seq_printf(seq,
  516. " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
  517. pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
  518. pkt_dev->clone_skb, pkt_dev->odevname);
  519. seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
  520. pkt_dev->lflow);
  521. seq_printf(seq,
  522. " queue_map_min: %u queue_map_max: %u\n",
  523. pkt_dev->queue_map_min,
  524. pkt_dev->queue_map_max);
  525. if (pkt_dev->skb_priority)
  526. seq_printf(seq, " skb_priority: %u\n",
  527. pkt_dev->skb_priority);
  528. if (pkt_dev->flags & F_IPV6) {
  529. seq_printf(seq,
  530. " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
  531. " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
  532. &pkt_dev->in6_saddr,
  533. &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
  534. &pkt_dev->in6_daddr,
  535. &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
  536. } else {
  537. seq_printf(seq,
  538. " dst_min: %s dst_max: %s\n",
  539. pkt_dev->dst_min, pkt_dev->dst_max);
  540. seq_printf(seq,
  541. " src_min: %s src_max: %s\n",
  542. pkt_dev->src_min, pkt_dev->src_max);
  543. }
  544. seq_puts(seq, " src_mac: ");
  545. seq_printf(seq, "%pM ",
  546. is_zero_ether_addr(pkt_dev->src_mac) ?
  547. pkt_dev->odev->dev_addr : pkt_dev->src_mac);
  548. seq_puts(seq, "dst_mac: ");
  549. seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
  550. seq_printf(seq,
  551. " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
  552. pkt_dev->udp_src_min, pkt_dev->udp_src_max,
  553. pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
  554. seq_printf(seq,
  555. " src_mac_count: %d dst_mac_count: %d\n",
  556. pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
  557. if (pkt_dev->nr_labels) {
  558. seq_puts(seq, " mpls: ");
  559. for (i = 0; i < pkt_dev->nr_labels; i++)
  560. seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
  561. i == pkt_dev->nr_labels-1 ? "\n" : ", ");
  562. }
  563. if (pkt_dev->vlan_id != 0xffff)
  564. seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
  565. pkt_dev->vlan_id, pkt_dev->vlan_p,
  566. pkt_dev->vlan_cfi);
  567. if (pkt_dev->svlan_id != 0xffff)
  568. seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
  569. pkt_dev->svlan_id, pkt_dev->svlan_p,
  570. pkt_dev->svlan_cfi);
  571. if (pkt_dev->tos)
  572. seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
  573. if (pkt_dev->traffic_class)
  574. seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
  575. if (pkt_dev->burst > 1)
  576. seq_printf(seq, " burst: %d\n", pkt_dev->burst);
  577. if (pkt_dev->node >= 0)
  578. seq_printf(seq, " node: %d\n", pkt_dev->node);
  579. if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
  580. seq_puts(seq, " xmit_mode: netif_receive\n");
  581. else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
  582. seq_puts(seq, " xmit_mode: xmit_queue\n");
  583. seq_puts(seq, " Flags: ");
  584. for (i = 0; i < NR_PKT_FLAGS; i++) {
  585. if (i == FLOW_SEQ_SHIFT)
  586. if (!pkt_dev->cflows)
  587. continue;
  588. if (pkt_dev->flags & (1 << i)) {
  589. seq_printf(seq, "%s ", pkt_flag_names[i]);
  590. #ifdef CONFIG_XFRM
  591. if (i == IPSEC_SHIFT && pkt_dev->spi)
  592. seq_printf(seq, "spi:%u ", pkt_dev->spi);
  593. #endif
  594. } else if (i == FLOW_SEQ_SHIFT) {
  595. seq_puts(seq, "FLOW_RND ");
  596. }
  597. }
  598. seq_puts(seq, "\n");
  599. /* not really stopped, more like last-running-at */
  600. stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
  601. idle = pkt_dev->idle_acc;
  602. do_div(idle, NSEC_PER_USEC);
  603. seq_printf(seq,
  604. "Current:\n pkts-sofar: %llu errors: %llu\n",
  605. (unsigned long long)pkt_dev->sofar,
  606. (unsigned long long)pkt_dev->errors);
  607. if (pkt_dev->n_imix_entries > 0) {
  608. int i;
  609. seq_puts(seq, " imix_size_counts: ");
  610. for (i = 0; i < pkt_dev->n_imix_entries; i++) {
  611. seq_printf(seq, "%llu,%llu ",
  612. pkt_dev->imix_entries[i].size,
  613. pkt_dev->imix_entries[i].count_so_far);
  614. }
  615. seq_puts(seq, "\n");
  616. }
  617. seq_printf(seq,
  618. " started: %lluus stopped: %lluus idle: %lluus\n",
  619. (unsigned long long) ktime_to_us(pkt_dev->started_at),
  620. (unsigned long long) ktime_to_us(stopped),
  621. (unsigned long long) idle);
  622. seq_printf(seq,
  623. " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
  624. pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
  625. pkt_dev->cur_src_mac_offset);
  626. if (pkt_dev->flags & F_IPV6) {
  627. seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
  628. &pkt_dev->cur_in6_saddr,
  629. &pkt_dev->cur_in6_daddr);
  630. } else
  631. seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
  632. &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
  633. seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
  634. pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
  635. seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
  636. seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
  637. if (pkt_dev->result[0])
  638. seq_printf(seq, "Result: %s\n", pkt_dev->result);
  639. else
  640. seq_puts(seq, "Result: Idle\n");
  641. return 0;
  642. }
  643. static ssize_t hex32_arg(const char __user *user_buffer, size_t maxlen,
  644. __u32 *num)
  645. {
  646. size_t i = 0;
  647. *num = 0;
  648. for (; i < maxlen; i++) {
  649. int value;
  650. char c;
  651. if (get_user(c, &user_buffer[i]))
  652. return -EFAULT;
  653. value = hex_to_bin(c);
  654. if (value >= 0) {
  655. *num <<= 4;
  656. *num |= value;
  657. } else {
  658. break;
  659. }
  660. }
  661. return i;
  662. }
  663. static ssize_t count_trail_chars(const char __user *user_buffer, size_t maxlen)
  664. {
  665. size_t i;
  666. for (i = 0; i < maxlen; i++) {
  667. char c;
  668. if (get_user(c, &user_buffer[i]))
  669. return -EFAULT;
  670. switch (c) {
  671. case '\"':
  672. case '\n':
  673. case '\r':
  674. case '\t':
  675. case ' ':
  676. case '=':
  677. break;
  678. default:
  679. goto done;
  680. }
  681. }
  682. done:
  683. return i;
  684. }
  685. static ssize_t num_arg(const char __user *user_buffer, size_t maxlen,
  686. unsigned long *num)
  687. {
  688. size_t i;
  689. *num = 0;
  690. for (i = 0; i < maxlen; i++) {
  691. char c;
  692. if (get_user(c, &user_buffer[i]))
  693. return -EFAULT;
  694. if ((c >= '0') && (c <= '9')) {
  695. *num *= 10;
  696. *num += c - '0';
  697. } else
  698. break;
  699. }
  700. return i;
  701. }
  702. static ssize_t strn_len(const char __user *user_buffer, size_t maxlen)
  703. {
  704. size_t i;
  705. for (i = 0; i < maxlen; i++) {
  706. char c;
  707. if (get_user(c, &user_buffer[i]))
  708. return -EFAULT;
  709. switch (c) {
  710. case '\"':
  711. case '\n':
  712. case '\r':
  713. case '\t':
  714. case ' ':
  715. case '=':
  716. goto done_str;
  717. default:
  718. break;
  719. }
  720. }
  721. done_str:
  722. return i;
  723. }
  724. /* Parses imix entries from user buffer.
  725. * The user buffer should consist of imix entries separated by spaces
  726. * where each entry consists of size and weight delimited by commas.
  727. * "size1,weight_1 size2,weight_2 ... size_n,weight_n" for example.
  728. */
  729. static ssize_t get_imix_entries(const char __user *buffer,
  730. size_t maxlen,
  731. struct pktgen_dev *pkt_dev)
  732. {
  733. size_t i = 0, max;
  734. ssize_t len;
  735. char c;
  736. pkt_dev->n_imix_entries = 0;
  737. do {
  738. unsigned long weight;
  739. unsigned long size;
  740. if (pkt_dev->n_imix_entries >= MAX_IMIX_ENTRIES)
  741. return -E2BIG;
  742. if (i >= maxlen)
  743. return -EINVAL;
  744. max = min(10, maxlen - i);
  745. len = num_arg(&buffer[i], max, &size);
  746. if (len < 0)
  747. return len;
  748. i += len;
  749. if (i >= maxlen)
  750. return -EINVAL;
  751. if (get_user(c, &buffer[i]))
  752. return -EFAULT;
  753. /* Check for comma between size_i and weight_i */
  754. if (c != ',')
  755. return -EINVAL;
  756. i++;
  757. if (i >= maxlen)
  758. return -EINVAL;
  759. if (size < 14 + 20 + 8)
  760. size = 14 + 20 + 8;
  761. max = min(10, maxlen - i);
  762. len = num_arg(&buffer[i], max, &weight);
  763. if (len < 0)
  764. return len;
  765. if (weight <= 0)
  766. return -EINVAL;
  767. pkt_dev->imix_entries[pkt_dev->n_imix_entries].size = size;
  768. pkt_dev->imix_entries[pkt_dev->n_imix_entries].weight = weight;
  769. i += len;
  770. pkt_dev->n_imix_entries++;
  771. if (i >= maxlen)
  772. break;
  773. if (get_user(c, &buffer[i]))
  774. return -EFAULT;
  775. i++;
  776. } while (c == ' ');
  777. return i;
  778. }
  779. static ssize_t get_labels(const char __user *buffer,
  780. size_t maxlen, struct pktgen_dev *pkt_dev)
  781. {
  782. unsigned int n = 0;
  783. size_t i = 0, max;
  784. ssize_t len;
  785. char c;
  786. pkt_dev->nr_labels = 0;
  787. do {
  788. __u32 tmp;
  789. if (n >= MAX_MPLS_LABELS)
  790. return -E2BIG;
  791. if (i >= maxlen)
  792. return -EINVAL;
  793. max = min(8, maxlen - i);
  794. len = hex32_arg(&buffer[i], max, &tmp);
  795. if (len < 0)
  796. return len;
  797. /* return empty list in case of invalid input or zero value */
  798. if (len == 0 || tmp == 0)
  799. return maxlen;
  800. pkt_dev->labels[n] = htonl(tmp);
  801. if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
  802. pkt_dev->flags |= F_MPLS_RND;
  803. i += len;
  804. n++;
  805. if (i >= maxlen)
  806. break;
  807. if (get_user(c, &buffer[i]))
  808. return -EFAULT;
  809. i++;
  810. } while (c == ',');
  811. pkt_dev->nr_labels = n;
  812. return i;
  813. }
  814. static __u32 pktgen_read_flag(const char *f, bool *disable)
  815. {
  816. __u32 i;
  817. if (f[0] == '!') {
  818. *disable = true;
  819. f++;
  820. }
  821. for (i = 0; i < NR_PKT_FLAGS; i++) {
  822. if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
  823. continue;
  824. /* allow only disabling ipv6 flag */
  825. if (!*disable && i == IPV6_SHIFT)
  826. continue;
  827. if (strcmp(f, pkt_flag_names[i]) == 0)
  828. return 1 << i;
  829. }
  830. if (strcmp(f, "FLOW_RND") == 0) {
  831. *disable = !*disable;
  832. return F_FLOW_SEQ;
  833. }
  834. return 0;
  835. }
  836. static ssize_t pktgen_if_write(struct file *file,
  837. const char __user *user_buffer, size_t count,
  838. loff_t *offset)
  839. {
  840. struct seq_file *seq = file->private_data;
  841. struct pktgen_dev *pkt_dev = seq->private;
  842. size_t i, max;
  843. ssize_t len;
  844. char name[16], valstr[32];
  845. unsigned long value = 0;
  846. char *pg_result = NULL;
  847. char buf[128];
  848. pg_result = &(pkt_dev->result[0]);
  849. if (count < 1) {
  850. pr_warn("wrong command format\n");
  851. return -EINVAL;
  852. }
  853. max = count;
  854. len = count_trail_chars(user_buffer, max);
  855. if (len < 0) {
  856. pr_warn("illegal format\n");
  857. return len;
  858. }
  859. i = len;
  860. /* Read variable name */
  861. max = min(sizeof(name) - 1, count - i);
  862. len = strn_len(&user_buffer[i], max);
  863. if (len < 0)
  864. return len;
  865. memset(name, 0, sizeof(name));
  866. if (copy_from_user(name, &user_buffer[i], len))
  867. return -EFAULT;
  868. i += len;
  869. max = count - i;
  870. len = count_trail_chars(&user_buffer[i], max);
  871. if (len < 0)
  872. return len;
  873. i += len;
  874. if (debug) {
  875. size_t copy = min_t(size_t, count + 1, 1024);
  876. char *tp = strndup_user(user_buffer, copy);
  877. if (IS_ERR(tp))
  878. return PTR_ERR(tp);
  879. pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
  880. kfree(tp);
  881. }
  882. if (!strcmp(name, "min_pkt_size")) {
  883. max = min(10, count - i);
  884. len = num_arg(&user_buffer[i], max, &value);
  885. if (len < 0)
  886. return len;
  887. if (value < 14 + 20 + 8)
  888. value = 14 + 20 + 8;
  889. if (value != pkt_dev->min_pkt_size) {
  890. pkt_dev->min_pkt_size = value;
  891. pkt_dev->cur_pkt_size = value;
  892. }
  893. sprintf(pg_result, "OK: min_pkt_size=%d",
  894. pkt_dev->min_pkt_size);
  895. return count;
  896. }
  897. if (!strcmp(name, "max_pkt_size")) {
  898. max = min(10, count - i);
  899. len = num_arg(&user_buffer[i], max, &value);
  900. if (len < 0)
  901. return len;
  902. if (value < 14 + 20 + 8)
  903. value = 14 + 20 + 8;
  904. if (value != pkt_dev->max_pkt_size) {
  905. pkt_dev->max_pkt_size = value;
  906. pkt_dev->cur_pkt_size = value;
  907. }
  908. sprintf(pg_result, "OK: max_pkt_size=%d",
  909. pkt_dev->max_pkt_size);
  910. return count;
  911. }
  912. /* Shortcut for min = max */
  913. if (!strcmp(name, "pkt_size")) {
  914. max = min(10, count - i);
  915. len = num_arg(&user_buffer[i], max, &value);
  916. if (len < 0)
  917. return len;
  918. if (value < 14 + 20 + 8)
  919. value = 14 + 20 + 8;
  920. if (value != pkt_dev->min_pkt_size) {
  921. pkt_dev->min_pkt_size = value;
  922. pkt_dev->max_pkt_size = value;
  923. pkt_dev->cur_pkt_size = value;
  924. }
  925. sprintf(pg_result, "OK: pkt_size=%d", pkt_dev->min_pkt_size);
  926. return count;
  927. }
  928. if (!strcmp(name, "imix_weights")) {
  929. if (pkt_dev->clone_skb > 0)
  930. return -EINVAL;
  931. max = count - i;
  932. len = get_imix_entries(&user_buffer[i], max, pkt_dev);
  933. if (len < 0)
  934. return len;
  935. fill_imix_distribution(pkt_dev);
  936. return count;
  937. }
  938. if (!strcmp(name, "debug")) {
  939. max = min(10, count - i);
  940. len = num_arg(&user_buffer[i], max, &value);
  941. if (len < 0)
  942. return len;
  943. debug = value;
  944. sprintf(pg_result, "OK: debug=%u", debug);
  945. return count;
  946. }
  947. if (!strcmp(name, "frags")) {
  948. max = min(10, count - i);
  949. len = num_arg(&user_buffer[i], max, &value);
  950. if (len < 0)
  951. return len;
  952. pkt_dev->nfrags = value;
  953. sprintf(pg_result, "OK: frags=%d", pkt_dev->nfrags);
  954. return count;
  955. }
  956. if (!strcmp(name, "delay")) {
  957. max = min(10, count - i);
  958. len = num_arg(&user_buffer[i], max, &value);
  959. if (len < 0)
  960. return len;
  961. if (value == 0x7FFFFFFF)
  962. pkt_dev->delay = ULLONG_MAX;
  963. else
  964. pkt_dev->delay = (u64)value;
  965. sprintf(pg_result, "OK: delay=%llu",
  966. (unsigned long long) pkt_dev->delay);
  967. return count;
  968. }
  969. if (!strcmp(name, "rate")) {
  970. max = min(10, count - i);
  971. len = num_arg(&user_buffer[i], max, &value);
  972. if (len < 0)
  973. return len;
  974. if (!value)
  975. return -EINVAL;
  976. pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
  977. if (debug)
  978. pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
  979. sprintf(pg_result, "OK: rate=%lu", value);
  980. return count;
  981. }
  982. if (!strcmp(name, "ratep")) {
  983. max = min(10, count - i);
  984. len = num_arg(&user_buffer[i], max, &value);
  985. if (len < 0)
  986. return len;
  987. if (!value)
  988. return -EINVAL;
  989. pkt_dev->delay = NSEC_PER_SEC/value;
  990. if (debug)
  991. pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
  992. sprintf(pg_result, "OK: rate=%lu", value);
  993. return count;
  994. }
  995. if (!strcmp(name, "udp_src_min")) {
  996. max = min(10, count - i);
  997. len = num_arg(&user_buffer[i], max, &value);
  998. if (len < 0)
  999. return len;
  1000. if (value != pkt_dev->udp_src_min) {
  1001. pkt_dev->udp_src_min = value;
  1002. pkt_dev->cur_udp_src = value;
  1003. }
  1004. sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
  1005. return count;
  1006. }
  1007. if (!strcmp(name, "udp_dst_min")) {
  1008. max = min(10, count - i);
  1009. len = num_arg(&user_buffer[i], max, &value);
  1010. if (len < 0)
  1011. return len;
  1012. if (value != pkt_dev->udp_dst_min) {
  1013. pkt_dev->udp_dst_min = value;
  1014. pkt_dev->cur_udp_dst = value;
  1015. }
  1016. sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
  1017. return count;
  1018. }
  1019. if (!strcmp(name, "udp_src_max")) {
  1020. max = min(10, count - i);
  1021. len = num_arg(&user_buffer[i], max, &value);
  1022. if (len < 0)
  1023. return len;
  1024. if (value != pkt_dev->udp_src_max) {
  1025. pkt_dev->udp_src_max = value;
  1026. pkt_dev->cur_udp_src = value;
  1027. }
  1028. sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
  1029. return count;
  1030. }
  1031. if (!strcmp(name, "udp_dst_max")) {
  1032. max = min(10, count - i);
  1033. len = num_arg(&user_buffer[i], max, &value);
  1034. if (len < 0)
  1035. return len;
  1036. if (value != pkt_dev->udp_dst_max) {
  1037. pkt_dev->udp_dst_max = value;
  1038. pkt_dev->cur_udp_dst = value;
  1039. }
  1040. sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
  1041. return count;
  1042. }
  1043. if (!strcmp(name, "clone_skb")) {
  1044. max = min(10, count - i);
  1045. len = num_arg(&user_buffer[i], max, &value);
  1046. if (len < 0)
  1047. return len;
  1048. /* clone_skb is not supported for netif_receive xmit_mode and
  1049. * IMIX mode.
  1050. */
  1051. if ((value > 0) &&
  1052. ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
  1053. !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
  1054. return -EOPNOTSUPP;
  1055. if (value > 0 && (pkt_dev->n_imix_entries > 0 ||
  1056. !(pkt_dev->flags & F_SHARED)))
  1057. return -EINVAL;
  1058. pkt_dev->clone_skb = value;
  1059. sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
  1060. return count;
  1061. }
  1062. if (!strcmp(name, "count")) {
  1063. max = min(10, count - i);
  1064. len = num_arg(&user_buffer[i], max, &value);
  1065. if (len < 0)
  1066. return len;
  1067. pkt_dev->count = value;
  1068. sprintf(pg_result, "OK: count=%llu",
  1069. (unsigned long long)pkt_dev->count);
  1070. return count;
  1071. }
  1072. if (!strcmp(name, "src_mac_count")) {
  1073. max = min(10, count - i);
  1074. len = num_arg(&user_buffer[i], max, &value);
  1075. if (len < 0)
  1076. return len;
  1077. if (pkt_dev->src_mac_count != value) {
  1078. pkt_dev->src_mac_count = value;
  1079. pkt_dev->cur_src_mac_offset = 0;
  1080. }
  1081. sprintf(pg_result, "OK: src_mac_count=%d",
  1082. pkt_dev->src_mac_count);
  1083. return count;
  1084. }
  1085. if (!strcmp(name, "dst_mac_count")) {
  1086. max = min(10, count - i);
  1087. len = num_arg(&user_buffer[i], max, &value);
  1088. if (len < 0)
  1089. return len;
  1090. if (pkt_dev->dst_mac_count != value) {
  1091. pkt_dev->dst_mac_count = value;
  1092. pkt_dev->cur_dst_mac_offset = 0;
  1093. }
  1094. sprintf(pg_result, "OK: dst_mac_count=%d",
  1095. pkt_dev->dst_mac_count);
  1096. return count;
  1097. }
  1098. if (!strcmp(name, "burst")) {
  1099. max = min(10, count - i);
  1100. len = num_arg(&user_buffer[i], max, &value);
  1101. if (len < 0)
  1102. return len;
  1103. if ((value > 1) &&
  1104. ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
  1105. ((pkt_dev->xmit_mode == M_START_XMIT) &&
  1106. (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
  1107. return -EOPNOTSUPP;
  1108. if (value > 1 && !(pkt_dev->flags & F_SHARED))
  1109. return -EINVAL;
  1110. pkt_dev->burst = value < 1 ? 1 : value;
  1111. sprintf(pg_result, "OK: burst=%u", pkt_dev->burst);
  1112. return count;
  1113. }
  1114. if (!strcmp(name, "node")) {
  1115. max = min(10, count - i);
  1116. len = num_arg(&user_buffer[i], max, &value);
  1117. if (len < 0)
  1118. return len;
  1119. if (node_possible(value)) {
  1120. pkt_dev->node = value;
  1121. sprintf(pg_result, "OK: node=%d", pkt_dev->node);
  1122. if (pkt_dev->page) {
  1123. put_page(pkt_dev->page);
  1124. pkt_dev->page = NULL;
  1125. }
  1126. } else {
  1127. sprintf(pg_result, "ERROR: node not possible");
  1128. }
  1129. return count;
  1130. }
  1131. if (!strcmp(name, "xmit_mode")) {
  1132. char f[32];
  1133. max = min(sizeof(f) - 1, count - i);
  1134. len = strn_len(&user_buffer[i], max);
  1135. if (len < 0)
  1136. return len;
  1137. memset(f, 0, sizeof(f));
  1138. if (copy_from_user(f, &user_buffer[i], len))
  1139. return -EFAULT;
  1140. if (strcmp(f, "start_xmit") == 0) {
  1141. pkt_dev->xmit_mode = M_START_XMIT;
  1142. } else if (strcmp(f, "netif_receive") == 0) {
  1143. /* clone_skb set earlier, not supported in this mode */
  1144. if (pkt_dev->clone_skb > 0)
  1145. return -EOPNOTSUPP;
  1146. pkt_dev->xmit_mode = M_NETIF_RECEIVE;
  1147. /* make sure new packet is allocated every time
  1148. * pktgen_xmit() is called
  1149. */
  1150. pkt_dev->last_ok = 1;
  1151. } else if (strcmp(f, "queue_xmit") == 0) {
  1152. pkt_dev->xmit_mode = M_QUEUE_XMIT;
  1153. pkt_dev->last_ok = 1;
  1154. } else {
  1155. sprintf(pg_result,
  1156. "xmit_mode -:%s:- unknown\nAvailable modes: %s",
  1157. f, "start_xmit, netif_receive\n");
  1158. return count;
  1159. }
  1160. sprintf(pg_result, "OK: xmit_mode=%s", f);
  1161. return count;
  1162. }
  1163. if (!strcmp(name, "flag")) {
  1164. bool disable = false;
  1165. __u32 flag;
  1166. char f[32];
  1167. char *end;
  1168. max = min(sizeof(f) - 1, count - i);
  1169. len = strn_len(&user_buffer[i], max);
  1170. if (len < 0)
  1171. return len;
  1172. memset(f, 0, 32);
  1173. if (copy_from_user(f, &user_buffer[i], len))
  1174. return -EFAULT;
  1175. flag = pktgen_read_flag(f, &disable);
  1176. if (flag) {
  1177. if (disable) {
  1178. /* If "clone_skb", or "burst" parameters are
  1179. * configured, it means that the skb still
  1180. * needs to be referenced by the pktgen, so
  1181. * the skb must be shared.
  1182. */
  1183. if (flag == F_SHARED && (pkt_dev->clone_skb ||
  1184. pkt_dev->burst > 1))
  1185. return -EINVAL;
  1186. pkt_dev->flags &= ~flag;
  1187. } else {
  1188. pkt_dev->flags |= flag;
  1189. }
  1190. sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
  1191. return count;
  1192. }
  1193. /* Unknown flag */
  1194. end = pkt_dev->result + sizeof(pkt_dev->result);
  1195. pg_result += sprintf(pg_result,
  1196. "Flag -:%s:- unknown\n"
  1197. "Available flags, (prepend ! to un-set flag):\n", f);
  1198. for (int n = 0; n < NR_PKT_FLAGS && pg_result < end; n++) {
  1199. if (!IS_ENABLED(CONFIG_XFRM) && n == IPSEC_SHIFT)
  1200. continue;
  1201. pg_result += snprintf(pg_result, end - pg_result,
  1202. "%s, ", pkt_flag_names[n]);
  1203. }
  1204. if (!WARN_ON_ONCE(pg_result >= end)) {
  1205. /* Remove the comma and whitespace at the end */
  1206. *(pg_result - 2) = '\0';
  1207. }
  1208. return count;
  1209. }
  1210. if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
  1211. max = min(sizeof(pkt_dev->dst_min) - 1, count - i);
  1212. len = strn_len(&user_buffer[i], max);
  1213. if (len < 0)
  1214. return len;
  1215. if (copy_from_user(buf, &user_buffer[i], len))
  1216. return -EFAULT;
  1217. buf[len] = 0;
  1218. if (strcmp(buf, pkt_dev->dst_min) != 0) {
  1219. strscpy_pad(pkt_dev->dst_min, buf);
  1220. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1221. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1222. }
  1223. if (debug)
  1224. pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
  1225. sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
  1226. return count;
  1227. }
  1228. if (!strcmp(name, "dst_max")) {
  1229. max = min(sizeof(pkt_dev->dst_max) - 1, count - i);
  1230. len = strn_len(&user_buffer[i], max);
  1231. if (len < 0)
  1232. return len;
  1233. if (copy_from_user(buf, &user_buffer[i], len))
  1234. return -EFAULT;
  1235. buf[len] = 0;
  1236. if (strcmp(buf, pkt_dev->dst_max) != 0) {
  1237. strscpy_pad(pkt_dev->dst_max, buf);
  1238. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1239. pkt_dev->cur_daddr = pkt_dev->daddr_max;
  1240. }
  1241. if (debug)
  1242. pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
  1243. sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
  1244. return count;
  1245. }
  1246. if (!strcmp(name, "dst6")) {
  1247. max = min(sizeof(buf) - 1, count - i);
  1248. len = strn_len(&user_buffer[i], max);
  1249. if (len < 0)
  1250. return len;
  1251. pkt_dev->flags |= F_IPV6;
  1252. if (copy_from_user(buf, &user_buffer[i], len))
  1253. return -EFAULT;
  1254. buf[len] = 0;
  1255. in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
  1256. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
  1257. pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
  1258. if (debug)
  1259. pr_debug("dst6 set to: %s\n", buf);
  1260. sprintf(pg_result, "OK: dst6=%s", buf);
  1261. return count;
  1262. }
  1263. if (!strcmp(name, "dst6_min")) {
  1264. max = min(sizeof(buf) - 1, count - i);
  1265. len = strn_len(&user_buffer[i], max);
  1266. if (len < 0)
  1267. return len;
  1268. pkt_dev->flags |= F_IPV6;
  1269. if (copy_from_user(buf, &user_buffer[i], len))
  1270. return -EFAULT;
  1271. buf[len] = 0;
  1272. in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
  1273. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
  1274. pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
  1275. if (debug)
  1276. pr_debug("dst6_min set to: %s\n", buf);
  1277. sprintf(pg_result, "OK: dst6_min=%s", buf);
  1278. return count;
  1279. }
  1280. if (!strcmp(name, "dst6_max")) {
  1281. max = min(sizeof(buf) - 1, count - i);
  1282. len = strn_len(&user_buffer[i], max);
  1283. if (len < 0)
  1284. return len;
  1285. pkt_dev->flags |= F_IPV6;
  1286. if (copy_from_user(buf, &user_buffer[i], len))
  1287. return -EFAULT;
  1288. buf[len] = 0;
  1289. in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
  1290. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
  1291. if (debug)
  1292. pr_debug("dst6_max set to: %s\n", buf);
  1293. sprintf(pg_result, "OK: dst6_max=%s", buf);
  1294. return count;
  1295. }
  1296. if (!strcmp(name, "src6")) {
  1297. max = min(sizeof(buf) - 1, count - i);
  1298. len = strn_len(&user_buffer[i], max);
  1299. if (len < 0)
  1300. return len;
  1301. pkt_dev->flags |= F_IPV6;
  1302. if (copy_from_user(buf, &user_buffer[i], len))
  1303. return -EFAULT;
  1304. buf[len] = 0;
  1305. in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
  1306. snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
  1307. pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
  1308. if (debug)
  1309. pr_debug("src6 set to: %s\n", buf);
  1310. sprintf(pg_result, "OK: src6=%s", buf);
  1311. return count;
  1312. }
  1313. if (!strcmp(name, "src_min")) {
  1314. max = min(sizeof(pkt_dev->src_min) - 1, count - i);
  1315. len = strn_len(&user_buffer[i], max);
  1316. if (len < 0)
  1317. return len;
  1318. if (copy_from_user(buf, &user_buffer[i], len))
  1319. return -EFAULT;
  1320. buf[len] = 0;
  1321. if (strcmp(buf, pkt_dev->src_min) != 0) {
  1322. strscpy_pad(pkt_dev->src_min, buf);
  1323. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1324. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1325. }
  1326. if (debug)
  1327. pr_debug("src_min set to: %s\n", pkt_dev->src_min);
  1328. sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
  1329. return count;
  1330. }
  1331. if (!strcmp(name, "src_max")) {
  1332. max = min(sizeof(pkt_dev->src_max) - 1, count - i);
  1333. len = strn_len(&user_buffer[i], max);
  1334. if (len < 0)
  1335. return len;
  1336. if (copy_from_user(buf, &user_buffer[i], len))
  1337. return -EFAULT;
  1338. buf[len] = 0;
  1339. if (strcmp(buf, pkt_dev->src_max) != 0) {
  1340. strscpy_pad(pkt_dev->src_max, buf);
  1341. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1342. pkt_dev->cur_saddr = pkt_dev->saddr_max;
  1343. }
  1344. if (debug)
  1345. pr_debug("src_max set to: %s\n", pkt_dev->src_max);
  1346. sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
  1347. return count;
  1348. }
  1349. if (!strcmp(name, "dst_mac")) {
  1350. max = min(sizeof(valstr) - 1, count - i);
  1351. len = strn_len(&user_buffer[i], max);
  1352. if (len < 0)
  1353. return len;
  1354. memset(valstr, 0, sizeof(valstr));
  1355. if (copy_from_user(valstr, &user_buffer[i], len))
  1356. return -EFAULT;
  1357. if (!mac_pton(valstr, pkt_dev->dst_mac))
  1358. return -EINVAL;
  1359. /* Set up Dest MAC */
  1360. ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
  1361. sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
  1362. return count;
  1363. }
  1364. if (!strcmp(name, "src_mac")) {
  1365. max = min(sizeof(valstr) - 1, count - i);
  1366. len = strn_len(&user_buffer[i], max);
  1367. if (len < 0)
  1368. return len;
  1369. memset(valstr, 0, sizeof(valstr));
  1370. if (copy_from_user(valstr, &user_buffer[i], len))
  1371. return -EFAULT;
  1372. if (!mac_pton(valstr, pkt_dev->src_mac))
  1373. return -EINVAL;
  1374. /* Set up Src MAC */
  1375. ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
  1376. sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
  1377. return count;
  1378. }
  1379. if (!strcmp(name, "clear_counters")) {
  1380. pktgen_clear_counters(pkt_dev);
  1381. sprintf(pg_result, "OK: Clearing counters.\n");
  1382. return count;
  1383. }
  1384. if (!strcmp(name, "flows")) {
  1385. max = min(10, count - i);
  1386. len = num_arg(&user_buffer[i], max, &value);
  1387. if (len < 0)
  1388. return len;
  1389. if (value > MAX_CFLOWS)
  1390. value = MAX_CFLOWS;
  1391. pkt_dev->cflows = value;
  1392. sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
  1393. return count;
  1394. }
  1395. #ifdef CONFIG_XFRM
  1396. if (!strcmp(name, "spi")) {
  1397. max = min(10, count - i);
  1398. len = num_arg(&user_buffer[i], max, &value);
  1399. if (len < 0)
  1400. return len;
  1401. pkt_dev->spi = value;
  1402. sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
  1403. return count;
  1404. }
  1405. #endif
  1406. if (!strcmp(name, "flowlen")) {
  1407. max = min(10, count - i);
  1408. len = num_arg(&user_buffer[i], max, &value);
  1409. if (len < 0)
  1410. return len;
  1411. pkt_dev->lflow = value;
  1412. sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
  1413. return count;
  1414. }
  1415. if (!strcmp(name, "queue_map_min")) {
  1416. max = min(5, count - i);
  1417. len = num_arg(&user_buffer[i], max, &value);
  1418. if (len < 0)
  1419. return len;
  1420. pkt_dev->queue_map_min = value;
  1421. sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
  1422. return count;
  1423. }
  1424. if (!strcmp(name, "queue_map_max")) {
  1425. max = min(5, count - i);
  1426. len = num_arg(&user_buffer[i], max, &value);
  1427. if (len < 0)
  1428. return len;
  1429. pkt_dev->queue_map_max = value;
  1430. sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
  1431. return count;
  1432. }
  1433. if (!strcmp(name, "mpls")) {
  1434. unsigned int n, cnt;
  1435. max = count - i;
  1436. len = get_labels(&user_buffer[i], max, pkt_dev);
  1437. if (len < 0)
  1438. return len;
  1439. cnt = sprintf(pg_result, "OK: mpls=");
  1440. for (n = 0; n < pkt_dev->nr_labels; n++)
  1441. cnt += sprintf(pg_result + cnt,
  1442. "%08x%s", ntohl(pkt_dev->labels[n]),
  1443. n == pkt_dev->nr_labels-1 ? "" : ",");
  1444. if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
  1445. pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
  1446. pkt_dev->svlan_id = 0xffff;
  1447. if (debug)
  1448. pr_debug("VLAN/SVLAN auto turned off\n");
  1449. }
  1450. return count;
  1451. }
  1452. if (!strcmp(name, "vlan_id")) {
  1453. max = min(4, count - i);
  1454. len = num_arg(&user_buffer[i], max, &value);
  1455. if (len < 0)
  1456. return len;
  1457. if (value <= 4095) {
  1458. pkt_dev->vlan_id = value; /* turn on VLAN */
  1459. if (debug)
  1460. pr_debug("VLAN turned on\n");
  1461. if (debug && pkt_dev->nr_labels)
  1462. pr_debug("MPLS auto turned off\n");
  1463. pkt_dev->nr_labels = 0; /* turn off MPLS */
  1464. sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
  1465. } else {
  1466. pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
  1467. pkt_dev->svlan_id = 0xffff;
  1468. if (debug)
  1469. pr_debug("VLAN/SVLAN turned off\n");
  1470. }
  1471. return count;
  1472. }
  1473. if (!strcmp(name, "vlan_p")) {
  1474. max = min(1, count - i);
  1475. len = num_arg(&user_buffer[i], max, &value);
  1476. if (len < 0)
  1477. return len;
  1478. if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
  1479. pkt_dev->vlan_p = value;
  1480. sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
  1481. } else {
  1482. sprintf(pg_result, "ERROR: vlan_p must be 0-7");
  1483. }
  1484. return count;
  1485. }
  1486. if (!strcmp(name, "vlan_cfi")) {
  1487. max = min(1, count - i);
  1488. len = num_arg(&user_buffer[i], max, &value);
  1489. if (len < 0)
  1490. return len;
  1491. if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
  1492. pkt_dev->vlan_cfi = value;
  1493. sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
  1494. } else {
  1495. sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
  1496. }
  1497. return count;
  1498. }
  1499. if (!strcmp(name, "svlan_id")) {
  1500. max = min(4, count - i);
  1501. len = num_arg(&user_buffer[i], max, &value);
  1502. if (len < 0)
  1503. return len;
  1504. if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
  1505. pkt_dev->svlan_id = value; /* turn on SVLAN */
  1506. if (debug)
  1507. pr_debug("SVLAN turned on\n");
  1508. if (debug && pkt_dev->nr_labels)
  1509. pr_debug("MPLS auto turned off\n");
  1510. pkt_dev->nr_labels = 0; /* turn off MPLS */
  1511. sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
  1512. } else {
  1513. pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
  1514. pkt_dev->svlan_id = 0xffff;
  1515. if (debug)
  1516. pr_debug("VLAN/SVLAN turned off\n");
  1517. }
  1518. return count;
  1519. }
  1520. if (!strcmp(name, "svlan_p")) {
  1521. max = min(1, count - i);
  1522. len = num_arg(&user_buffer[i], max, &value);
  1523. if (len < 0)
  1524. return len;
  1525. if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
  1526. pkt_dev->svlan_p = value;
  1527. sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
  1528. } else {
  1529. sprintf(pg_result, "ERROR: svlan_p must be 0-7");
  1530. }
  1531. return count;
  1532. }
  1533. if (!strcmp(name, "svlan_cfi")) {
  1534. max = min(1, count - i);
  1535. len = num_arg(&user_buffer[i], max, &value);
  1536. if (len < 0)
  1537. return len;
  1538. if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
  1539. pkt_dev->svlan_cfi = value;
  1540. sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
  1541. } else {
  1542. sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
  1543. }
  1544. return count;
  1545. }
  1546. if (!strcmp(name, "tos")) {
  1547. __u32 tmp_value;
  1548. max = min(2, count - i);
  1549. len = hex32_arg(&user_buffer[i], max, &tmp_value);
  1550. if (len < 0)
  1551. return len;
  1552. if (len == 2) {
  1553. pkt_dev->tos = tmp_value;
  1554. sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
  1555. } else {
  1556. sprintf(pg_result, "ERROR: tos must be 00-ff");
  1557. }
  1558. return count;
  1559. }
  1560. if (!strcmp(name, "traffic_class")) {
  1561. __u32 tmp_value;
  1562. max = min(2, count - i);
  1563. len = hex32_arg(&user_buffer[i], max, &tmp_value);
  1564. if (len < 0)
  1565. return len;
  1566. if (len == 2) {
  1567. pkt_dev->traffic_class = tmp_value;
  1568. sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
  1569. } else {
  1570. sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
  1571. }
  1572. return count;
  1573. }
  1574. if (!strcmp(name, "skb_priority")) {
  1575. max = min(9, count - i);
  1576. len = num_arg(&user_buffer[i], max, &value);
  1577. if (len < 0)
  1578. return len;
  1579. pkt_dev->skb_priority = value;
  1580. sprintf(pg_result, "OK: skb_priority=%i",
  1581. pkt_dev->skb_priority);
  1582. return count;
  1583. }
  1584. sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
  1585. return -EINVAL;
  1586. }
  1587. static int pktgen_if_open(struct inode *inode, struct file *file)
  1588. {
  1589. return single_open(file, pktgen_if_show, pde_data(inode));
  1590. }
  1591. static const struct proc_ops pktgen_if_proc_ops = {
  1592. .proc_open = pktgen_if_open,
  1593. .proc_read = seq_read,
  1594. .proc_lseek = seq_lseek,
  1595. .proc_write = pktgen_if_write,
  1596. .proc_release = single_release,
  1597. };
  1598. static int pktgen_thread_show(struct seq_file *seq, void *v)
  1599. {
  1600. struct pktgen_thread *t = seq->private;
  1601. const struct pktgen_dev *pkt_dev;
  1602. BUG_ON(!t);
  1603. seq_puts(seq, "Running: ");
  1604. rcu_read_lock();
  1605. list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
  1606. if (pkt_dev->running)
  1607. seq_printf(seq, "%s ", pkt_dev->odevname);
  1608. seq_puts(seq, "\nStopped: ");
  1609. list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
  1610. if (!pkt_dev->running)
  1611. seq_printf(seq, "%s ", pkt_dev->odevname);
  1612. if (t->result[0])
  1613. seq_printf(seq, "\nResult: %s\n", t->result);
  1614. else
  1615. seq_puts(seq, "\nResult: NA\n");
  1616. rcu_read_unlock();
  1617. return 0;
  1618. }
  1619. static ssize_t pktgen_thread_write(struct file *file,
  1620. const char __user *user_buffer,
  1621. size_t count, loff_t *offset)
  1622. {
  1623. struct seq_file *seq = file->private_data;
  1624. struct pktgen_thread *t = seq->private;
  1625. size_t i, max;
  1626. ssize_t len, ret;
  1627. char name[40];
  1628. char *pg_result;
  1629. if (count < 1) {
  1630. // sprintf(pg_result, "Wrong command format");
  1631. return -EINVAL;
  1632. }
  1633. max = count;
  1634. len = count_trail_chars(user_buffer, max);
  1635. if (len < 0)
  1636. return len;
  1637. i = len;
  1638. /* Read variable name */
  1639. max = min(sizeof(name) - 1, count - i);
  1640. len = strn_len(&user_buffer[i], max);
  1641. if (len < 0)
  1642. return len;
  1643. memset(name, 0, sizeof(name));
  1644. if (copy_from_user(name, &user_buffer[i], len))
  1645. return -EFAULT;
  1646. i += len;
  1647. max = count - i;
  1648. len = count_trail_chars(&user_buffer[i], max);
  1649. if (len < 0)
  1650. return len;
  1651. i += len;
  1652. if (debug)
  1653. pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
  1654. if (!t) {
  1655. pr_err("ERROR: No thread\n");
  1656. ret = -EINVAL;
  1657. goto out;
  1658. }
  1659. pg_result = &(t->result[0]);
  1660. if (!strcmp(name, "add_device")) {
  1661. char f[32];
  1662. memset(f, 0, 32);
  1663. max = min(sizeof(f) - 1, count - i);
  1664. len = strn_len(&user_buffer[i], max);
  1665. if (len < 0) {
  1666. ret = len;
  1667. goto out;
  1668. }
  1669. if (copy_from_user(f, &user_buffer[i], len))
  1670. return -EFAULT;
  1671. mutex_lock(&pktgen_thread_lock);
  1672. ret = pktgen_add_device(t, f);
  1673. mutex_unlock(&pktgen_thread_lock);
  1674. if (!ret) {
  1675. ret = count;
  1676. sprintf(pg_result, "OK: add_device=%s", f);
  1677. } else
  1678. sprintf(pg_result, "ERROR: can not add device %s", f);
  1679. goto out;
  1680. }
  1681. if (!strcmp(name, "rem_device_all")) {
  1682. mutex_lock(&pktgen_thread_lock);
  1683. t->control |= T_REMDEVALL;
  1684. mutex_unlock(&pktgen_thread_lock);
  1685. schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
  1686. ret = count;
  1687. sprintf(pg_result, "OK: rem_device_all");
  1688. goto out;
  1689. }
  1690. if (!strcmp(name, "max_before_softirq")) {
  1691. sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
  1692. ret = count;
  1693. goto out;
  1694. }
  1695. ret = -EINVAL;
  1696. out:
  1697. return ret;
  1698. }
  1699. static int pktgen_thread_open(struct inode *inode, struct file *file)
  1700. {
  1701. return single_open(file, pktgen_thread_show, pde_data(inode));
  1702. }
  1703. static const struct proc_ops pktgen_thread_proc_ops = {
  1704. .proc_open = pktgen_thread_open,
  1705. .proc_read = seq_read,
  1706. .proc_lseek = seq_lseek,
  1707. .proc_write = pktgen_thread_write,
  1708. .proc_release = single_release,
  1709. };
  1710. /* Think find or remove for NN */
  1711. static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
  1712. const char *ifname, int remove)
  1713. {
  1714. struct pktgen_thread *t;
  1715. struct pktgen_dev *pkt_dev = NULL;
  1716. bool exact = (remove == FIND);
  1717. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  1718. pkt_dev = pktgen_find_dev(t, ifname, exact);
  1719. if (pkt_dev) {
  1720. if (remove) {
  1721. pkt_dev->removal_mark = 1;
  1722. t->control |= T_REMDEV;
  1723. }
  1724. break;
  1725. }
  1726. }
  1727. return pkt_dev;
  1728. }
  1729. /*
  1730. * mark a device for removal
  1731. */
  1732. static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
  1733. {
  1734. struct pktgen_dev *pkt_dev = NULL;
  1735. const int max_tries = 10, msec_per_try = 125;
  1736. int i = 0;
  1737. mutex_lock(&pktgen_thread_lock);
  1738. pr_debug("%s: marking %s for removal\n", __func__, ifname);
  1739. while (1) {
  1740. pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
  1741. if (pkt_dev == NULL)
  1742. break; /* success */
  1743. mutex_unlock(&pktgen_thread_lock);
  1744. pr_debug("%s: waiting for %s to disappear....\n",
  1745. __func__, ifname);
  1746. schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
  1747. mutex_lock(&pktgen_thread_lock);
  1748. if (++i >= max_tries) {
  1749. pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
  1750. __func__, msec_per_try * i, ifname);
  1751. break;
  1752. }
  1753. }
  1754. mutex_unlock(&pktgen_thread_lock);
  1755. }
  1756. static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
  1757. {
  1758. struct pktgen_thread *t;
  1759. mutex_lock(&pktgen_thread_lock);
  1760. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  1761. struct pktgen_dev *pkt_dev;
  1762. if_lock(t);
  1763. list_for_each_entry(pkt_dev, &t->if_list, list) {
  1764. if (pkt_dev->odev != dev)
  1765. continue;
  1766. proc_remove(pkt_dev->entry);
  1767. pkt_dev->entry = proc_create_data(dev->name, 0600,
  1768. pn->proc_dir,
  1769. &pktgen_if_proc_ops,
  1770. pkt_dev);
  1771. if (!pkt_dev->entry)
  1772. pr_err("can't move proc entry for '%s'\n",
  1773. dev->name);
  1774. break;
  1775. }
  1776. if_unlock(t);
  1777. }
  1778. mutex_unlock(&pktgen_thread_lock);
  1779. }
  1780. static int pktgen_device_event(struct notifier_block *unused,
  1781. unsigned long event, void *ptr)
  1782. {
  1783. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1784. struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
  1785. if (pn->pktgen_exiting)
  1786. return NOTIFY_DONE;
  1787. /* It is OK that we do not hold the group lock right now,
  1788. * as we run under the RTNL lock.
  1789. */
  1790. switch (event) {
  1791. case NETDEV_CHANGENAME:
  1792. pktgen_change_name(pn, dev);
  1793. break;
  1794. case NETDEV_UNREGISTER:
  1795. pktgen_mark_device(pn, dev->name);
  1796. break;
  1797. }
  1798. return NOTIFY_DONE;
  1799. }
  1800. static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
  1801. struct pktgen_dev *pkt_dev,
  1802. const char *ifname)
  1803. {
  1804. char b[IFNAMSIZ+5];
  1805. int i;
  1806. for (i = 0; ifname[i] != '@'; i++) {
  1807. if (i == IFNAMSIZ)
  1808. break;
  1809. b[i] = ifname[i];
  1810. }
  1811. b[i] = 0;
  1812. return dev_get_by_name(pn->net, b);
  1813. }
  1814. /* Associate pktgen_dev with a device. */
  1815. static int pktgen_setup_dev(const struct pktgen_net *pn,
  1816. struct pktgen_dev *pkt_dev, const char *ifname)
  1817. {
  1818. struct net_device *odev;
  1819. int err;
  1820. /* Clean old setups */
  1821. if (pkt_dev->odev) {
  1822. netdev_put(pkt_dev->odev, &pkt_dev->dev_tracker);
  1823. pkt_dev->odev = NULL;
  1824. }
  1825. odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
  1826. if (!odev) {
  1827. pr_err("no such netdevice: \"%s\"\n", ifname);
  1828. return -ENODEV;
  1829. }
  1830. if (odev->type != ARPHRD_ETHER && odev->type != ARPHRD_LOOPBACK) {
  1831. pr_err("not an ethernet or loopback device: \"%s\"\n", ifname);
  1832. err = -EINVAL;
  1833. } else if (!netif_running(odev)) {
  1834. pr_err("device is down: \"%s\"\n", ifname);
  1835. err = -ENETDOWN;
  1836. } else {
  1837. pkt_dev->odev = odev;
  1838. netdev_tracker_alloc(odev, &pkt_dev->dev_tracker, GFP_KERNEL);
  1839. return 0;
  1840. }
  1841. dev_put(odev);
  1842. return err;
  1843. }
  1844. /* Read pkt_dev from the interface and set up internal pktgen_dev
  1845. * structure to have the right information to create/send packets
  1846. */
  1847. static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
  1848. {
  1849. int ntxq;
  1850. if (!pkt_dev->odev) {
  1851. pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
  1852. sprintf(pkt_dev->result,
  1853. "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
  1854. return;
  1855. }
  1856. /* make sure that we don't pick a non-existing transmit queue */
  1857. ntxq = pkt_dev->odev->real_num_tx_queues;
  1858. if (ntxq <= pkt_dev->queue_map_min) {
  1859. pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
  1860. pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
  1861. pkt_dev->odevname);
  1862. pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
  1863. }
  1864. if (pkt_dev->queue_map_max >= ntxq) {
  1865. pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
  1866. pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
  1867. pkt_dev->odevname);
  1868. pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
  1869. }
  1870. /* Default to the interface's mac if not explicitly set. */
  1871. if (is_zero_ether_addr(pkt_dev->src_mac))
  1872. ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
  1873. /* Set up Dest MAC */
  1874. ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
  1875. if (pkt_dev->flags & F_IPV6) {
  1876. int i, set = 0, err = 1;
  1877. struct inet6_dev *idev;
  1878. if (pkt_dev->min_pkt_size == 0) {
  1879. pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
  1880. + sizeof(struct udphdr)
  1881. + sizeof(struct pktgen_hdr)
  1882. + pkt_dev->pkt_overhead;
  1883. }
  1884. for (i = 0; i < sizeof(struct in6_addr); i++)
  1885. if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
  1886. set = 1;
  1887. break;
  1888. }
  1889. if (!set) {
  1890. /*
  1891. * Use linklevel address if unconfigured.
  1892. *
  1893. * use ipv6_get_lladdr if/when it's get exported
  1894. */
  1895. rcu_read_lock();
  1896. idev = __in6_dev_get(pkt_dev->odev);
  1897. if (idev) {
  1898. struct inet6_ifaddr *ifp;
  1899. read_lock_bh(&idev->lock);
  1900. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  1901. if ((ifp->scope & IFA_LINK) &&
  1902. !(ifp->flags & IFA_F_TENTATIVE)) {
  1903. pkt_dev->cur_in6_saddr = ifp->addr;
  1904. err = 0;
  1905. break;
  1906. }
  1907. }
  1908. read_unlock_bh(&idev->lock);
  1909. }
  1910. rcu_read_unlock();
  1911. if (err)
  1912. pr_err("ERROR: IPv6 link address not available\n");
  1913. }
  1914. } else {
  1915. if (pkt_dev->min_pkt_size == 0) {
  1916. pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
  1917. + sizeof(struct udphdr)
  1918. + sizeof(struct pktgen_hdr)
  1919. + pkt_dev->pkt_overhead;
  1920. }
  1921. pkt_dev->saddr_min = 0;
  1922. pkt_dev->saddr_max = 0;
  1923. if (strlen(pkt_dev->src_min) == 0) {
  1924. struct in_device *in_dev;
  1925. rcu_read_lock();
  1926. in_dev = __in_dev_get_rcu(pkt_dev->odev);
  1927. if (in_dev) {
  1928. const struct in_ifaddr *ifa;
  1929. ifa = rcu_dereference(in_dev->ifa_list);
  1930. if (ifa) {
  1931. pkt_dev->saddr_min = ifa->ifa_address;
  1932. pkt_dev->saddr_max = pkt_dev->saddr_min;
  1933. }
  1934. }
  1935. rcu_read_unlock();
  1936. } else {
  1937. pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
  1938. pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
  1939. }
  1940. pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
  1941. pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
  1942. }
  1943. /* Initialize current values. */
  1944. pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
  1945. if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
  1946. pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
  1947. pkt_dev->cur_dst_mac_offset = 0;
  1948. pkt_dev->cur_src_mac_offset = 0;
  1949. pkt_dev->cur_saddr = pkt_dev->saddr_min;
  1950. pkt_dev->cur_daddr = pkt_dev->daddr_min;
  1951. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  1952. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  1953. pkt_dev->nflows = 0;
  1954. }
  1955. static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
  1956. {
  1957. ktime_t start_time, end_time;
  1958. s64 remaining;
  1959. struct hrtimer_sleeper t;
  1960. hrtimer_setup_sleeper_on_stack(&t, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1961. hrtimer_set_expires(&t.timer, spin_until);
  1962. remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
  1963. if (remaining <= 0)
  1964. goto out;
  1965. start_time = ktime_get();
  1966. if (remaining < 100000) {
  1967. /* for small delays (<100us), just loop until limit is reached */
  1968. do {
  1969. end_time = ktime_get();
  1970. } while (ktime_compare(end_time, spin_until) < 0);
  1971. } else {
  1972. do {
  1973. set_current_state(TASK_INTERRUPTIBLE);
  1974. hrtimer_sleeper_start_expires(&t, HRTIMER_MODE_ABS);
  1975. if (likely(t.task))
  1976. schedule();
  1977. hrtimer_cancel(&t.timer);
  1978. } while (t.task && pkt_dev->running && !signal_pending(current));
  1979. __set_current_state(TASK_RUNNING);
  1980. end_time = ktime_get();
  1981. }
  1982. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
  1983. out:
  1984. pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
  1985. destroy_hrtimer_on_stack(&t.timer);
  1986. }
  1987. static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
  1988. {
  1989. pkt_dev->pkt_overhead = 0;
  1990. pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
  1991. pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
  1992. pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
  1993. }
  1994. static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
  1995. {
  1996. return !!(pkt_dev->flows[flow].flags & F_INIT);
  1997. }
  1998. static inline int f_pick(struct pktgen_dev *pkt_dev)
  1999. {
  2000. int flow = pkt_dev->curfl;
  2001. if (pkt_dev->flags & F_FLOW_SEQ) {
  2002. if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
  2003. /* reset time */
  2004. pkt_dev->flows[flow].count = 0;
  2005. pkt_dev->flows[flow].flags = 0;
  2006. pkt_dev->curfl += 1;
  2007. if (pkt_dev->curfl >= pkt_dev->cflows)
  2008. pkt_dev->curfl = 0; /*reset */
  2009. }
  2010. } else {
  2011. flow = get_random_u32_below(pkt_dev->cflows);
  2012. pkt_dev->curfl = flow;
  2013. if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
  2014. pkt_dev->flows[flow].count = 0;
  2015. pkt_dev->flows[flow].flags = 0;
  2016. }
  2017. }
  2018. return pkt_dev->curfl;
  2019. }
  2020. /* If there was already an IPSEC SA, we keep it as is, else
  2021. * we go look for it ...
  2022. */
  2023. #define DUMMY_MARK 0
  2024. static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
  2025. {
  2026. #ifdef CONFIG_XFRM
  2027. struct xfrm_state *x = pkt_dev->flows[flow].x;
  2028. struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
  2029. if (!x) {
  2030. if (pkt_dev->spi) {
  2031. /* We need as quick as possible to find the right SA
  2032. * Searching with minimum criteria to achieve, this.
  2033. */
  2034. x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
  2035. } else {
  2036. /* slow path: we don't already have xfrm_state */
  2037. x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
  2038. (xfrm_address_t *)&pkt_dev->cur_daddr,
  2039. (xfrm_address_t *)&pkt_dev->cur_saddr,
  2040. AF_INET,
  2041. pkt_dev->ipsmode,
  2042. pkt_dev->ipsproto, 0);
  2043. }
  2044. if (x) {
  2045. pkt_dev->flows[flow].x = x;
  2046. set_pkt_overhead(pkt_dev);
  2047. pkt_dev->pkt_overhead += x->props.header_len;
  2048. }
  2049. }
  2050. #endif
  2051. }
  2052. static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
  2053. {
  2054. if (pkt_dev->flags & F_QUEUE_MAP_CPU)
  2055. pkt_dev->cur_queue_map = smp_processor_id();
  2056. else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
  2057. __u16 t;
  2058. if (pkt_dev->flags & F_QUEUE_MAP_RND) {
  2059. t = get_random_u32_inclusive(pkt_dev->queue_map_min,
  2060. pkt_dev->queue_map_max);
  2061. } else {
  2062. t = pkt_dev->cur_queue_map + 1;
  2063. if (t > pkt_dev->queue_map_max)
  2064. t = pkt_dev->queue_map_min;
  2065. }
  2066. pkt_dev->cur_queue_map = t;
  2067. }
  2068. pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
  2069. }
  2070. /* Increment/randomize headers according to flags and current values
  2071. * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
  2072. */
  2073. static void mod_cur_headers(struct pktgen_dev *pkt_dev)
  2074. {
  2075. __u32 imn;
  2076. __u32 imx;
  2077. int flow = 0;
  2078. if (pkt_dev->cflows)
  2079. flow = f_pick(pkt_dev);
  2080. /* Deal with source MAC */
  2081. if (pkt_dev->src_mac_count > 1) {
  2082. __u32 mc;
  2083. __u32 tmp;
  2084. if (pkt_dev->flags & F_MACSRC_RND)
  2085. mc = get_random_u32_below(pkt_dev->src_mac_count);
  2086. else {
  2087. mc = pkt_dev->cur_src_mac_offset++;
  2088. if (pkt_dev->cur_src_mac_offset >=
  2089. pkt_dev->src_mac_count)
  2090. pkt_dev->cur_src_mac_offset = 0;
  2091. }
  2092. tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
  2093. pkt_dev->hh[11] = tmp;
  2094. tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  2095. pkt_dev->hh[10] = tmp;
  2096. tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  2097. pkt_dev->hh[9] = tmp;
  2098. tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  2099. pkt_dev->hh[8] = tmp;
  2100. tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
  2101. pkt_dev->hh[7] = tmp;
  2102. }
  2103. /* Deal with Destination MAC */
  2104. if (pkt_dev->dst_mac_count > 1) {
  2105. __u32 mc;
  2106. __u32 tmp;
  2107. if (pkt_dev->flags & F_MACDST_RND)
  2108. mc = get_random_u32_below(pkt_dev->dst_mac_count);
  2109. else {
  2110. mc = pkt_dev->cur_dst_mac_offset++;
  2111. if (pkt_dev->cur_dst_mac_offset >=
  2112. pkt_dev->dst_mac_count) {
  2113. pkt_dev->cur_dst_mac_offset = 0;
  2114. }
  2115. }
  2116. tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
  2117. pkt_dev->hh[5] = tmp;
  2118. tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
  2119. pkt_dev->hh[4] = tmp;
  2120. tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
  2121. pkt_dev->hh[3] = tmp;
  2122. tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
  2123. pkt_dev->hh[2] = tmp;
  2124. tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
  2125. pkt_dev->hh[1] = tmp;
  2126. }
  2127. if (pkt_dev->flags & F_MPLS_RND) {
  2128. unsigned int i;
  2129. for (i = 0; i < pkt_dev->nr_labels; i++)
  2130. if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
  2131. pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
  2132. ((__force __be32)get_random_u32() &
  2133. htonl(0x000fffff));
  2134. }
  2135. if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
  2136. pkt_dev->vlan_id = get_random_u32_below(4096);
  2137. }
  2138. if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
  2139. pkt_dev->svlan_id = get_random_u32_below(4096);
  2140. }
  2141. if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
  2142. if (pkt_dev->flags & F_UDPSRC_RND)
  2143. pkt_dev->cur_udp_src = get_random_u32_inclusive(pkt_dev->udp_src_min,
  2144. pkt_dev->udp_src_max - 1);
  2145. else {
  2146. pkt_dev->cur_udp_src++;
  2147. if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
  2148. pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
  2149. }
  2150. }
  2151. if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
  2152. if (pkt_dev->flags & F_UDPDST_RND) {
  2153. pkt_dev->cur_udp_dst = get_random_u32_inclusive(pkt_dev->udp_dst_min,
  2154. pkt_dev->udp_dst_max - 1);
  2155. } else {
  2156. pkt_dev->cur_udp_dst++;
  2157. if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
  2158. pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
  2159. }
  2160. }
  2161. if (!(pkt_dev->flags & F_IPV6)) {
  2162. imn = ntohl(pkt_dev->saddr_min);
  2163. imx = ntohl(pkt_dev->saddr_max);
  2164. if (imn < imx) {
  2165. __u32 t;
  2166. if (pkt_dev->flags & F_IPSRC_RND)
  2167. t = get_random_u32_inclusive(imn, imx - 1);
  2168. else {
  2169. t = ntohl(pkt_dev->cur_saddr);
  2170. t++;
  2171. if (t > imx)
  2172. t = imn;
  2173. }
  2174. pkt_dev->cur_saddr = htonl(t);
  2175. }
  2176. if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
  2177. pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
  2178. } else {
  2179. imn = ntohl(pkt_dev->daddr_min);
  2180. imx = ntohl(pkt_dev->daddr_max);
  2181. if (imn < imx) {
  2182. __u32 t;
  2183. __be32 s;
  2184. if (pkt_dev->flags & F_IPDST_RND) {
  2185. do {
  2186. t = get_random_u32_inclusive(imn, imx - 1);
  2187. s = htonl(t);
  2188. } while (ipv4_is_loopback(s) ||
  2189. ipv4_is_multicast(s) ||
  2190. ipv4_is_lbcast(s) ||
  2191. ipv4_is_zeronet(s) ||
  2192. ipv4_is_local_multicast(s));
  2193. pkt_dev->cur_daddr = s;
  2194. } else {
  2195. t = ntohl(pkt_dev->cur_daddr);
  2196. t++;
  2197. if (t > imx) {
  2198. t = imn;
  2199. }
  2200. pkt_dev->cur_daddr = htonl(t);
  2201. }
  2202. }
  2203. if (pkt_dev->cflows) {
  2204. pkt_dev->flows[flow].flags |= F_INIT;
  2205. pkt_dev->flows[flow].cur_daddr =
  2206. pkt_dev->cur_daddr;
  2207. if (pkt_dev->flags & F_IPSEC)
  2208. get_ipsec_sa(pkt_dev, flow);
  2209. pkt_dev->nflows++;
  2210. }
  2211. }
  2212. } else { /* IPV6 * */
  2213. if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
  2214. int i;
  2215. /* Only random destinations yet */
  2216. for (i = 0; i < 4; i++) {
  2217. pkt_dev->cur_in6_daddr.s6_addr32[i] =
  2218. (((__force __be32)get_random_u32() |
  2219. pkt_dev->min_in6_daddr.s6_addr32[i]) &
  2220. pkt_dev->max_in6_daddr.s6_addr32[i]);
  2221. }
  2222. }
  2223. }
  2224. if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
  2225. __u32 t;
  2226. if (pkt_dev->flags & F_TXSIZE_RND) {
  2227. t = get_random_u32_inclusive(pkt_dev->min_pkt_size,
  2228. pkt_dev->max_pkt_size - 1);
  2229. } else {
  2230. t = pkt_dev->cur_pkt_size + 1;
  2231. if (t > pkt_dev->max_pkt_size)
  2232. t = pkt_dev->min_pkt_size;
  2233. }
  2234. pkt_dev->cur_pkt_size = t;
  2235. } else if (pkt_dev->n_imix_entries > 0) {
  2236. struct imix_pkt *entry;
  2237. __u32 t = get_random_u32_below(IMIX_PRECISION);
  2238. __u8 entry_index = pkt_dev->imix_distribution[t];
  2239. entry = &pkt_dev->imix_entries[entry_index];
  2240. entry->count_so_far++;
  2241. pkt_dev->cur_pkt_size = entry->size;
  2242. }
  2243. set_cur_queue_map(pkt_dev);
  2244. pkt_dev->flows[flow].count++;
  2245. }
  2246. static void fill_imix_distribution(struct pktgen_dev *pkt_dev)
  2247. {
  2248. int cumulative_probabilites[MAX_IMIX_ENTRIES];
  2249. int j = 0;
  2250. __u64 cumulative_prob = 0;
  2251. __u64 total_weight = 0;
  2252. int i = 0;
  2253. for (i = 0; i < pkt_dev->n_imix_entries; i++)
  2254. total_weight += pkt_dev->imix_entries[i].weight;
  2255. /* Fill cumulative_probabilites with sum of normalized probabilities */
  2256. for (i = 0; i < pkt_dev->n_imix_entries - 1; i++) {
  2257. cumulative_prob += div64_u64(pkt_dev->imix_entries[i].weight *
  2258. IMIX_PRECISION,
  2259. total_weight);
  2260. cumulative_probabilites[i] = cumulative_prob;
  2261. }
  2262. cumulative_probabilites[pkt_dev->n_imix_entries - 1] = 100;
  2263. for (i = 0; i < IMIX_PRECISION; i++) {
  2264. if (i == cumulative_probabilites[j])
  2265. j++;
  2266. pkt_dev->imix_distribution[i] = j;
  2267. }
  2268. }
  2269. #ifdef CONFIG_XFRM
  2270. static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
  2271. [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
  2272. };
  2273. static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
  2274. {
  2275. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2276. int err = 0;
  2277. struct net *net = dev_net(pkt_dev->odev);
  2278. if (!x)
  2279. return 0;
  2280. /* XXX: we dont support tunnel mode for now until
  2281. * we resolve the dst issue
  2282. */
  2283. if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
  2284. return 0;
  2285. /* But when user specify an valid SPI, transformation
  2286. * supports both transport/tunnel mode + ESP/AH type.
  2287. */
  2288. if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
  2289. skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
  2290. rcu_read_lock_bh();
  2291. err = pktgen_xfrm_outer_mode_output(x, skb);
  2292. rcu_read_unlock_bh();
  2293. if (err) {
  2294. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
  2295. goto error;
  2296. }
  2297. err = x->type->output(x, skb);
  2298. if (err) {
  2299. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  2300. goto error;
  2301. }
  2302. spin_lock_bh(&x->lock);
  2303. x->curlft.bytes += skb->len;
  2304. x->curlft.packets++;
  2305. spin_unlock_bh(&x->lock);
  2306. error:
  2307. return err;
  2308. }
  2309. static void free_SAs(struct pktgen_dev *pkt_dev)
  2310. {
  2311. if (pkt_dev->cflows) {
  2312. /* let go of the SAs if we have them */
  2313. int i;
  2314. for (i = 0; i < pkt_dev->cflows; i++) {
  2315. struct xfrm_state *x = pkt_dev->flows[i].x;
  2316. if (x) {
  2317. xfrm_state_put(x);
  2318. pkt_dev->flows[i].x = NULL;
  2319. }
  2320. }
  2321. }
  2322. }
  2323. static int process_ipsec(struct pktgen_dev *pkt_dev,
  2324. struct sk_buff *skb, __be16 protocol)
  2325. {
  2326. if (pkt_dev->flags & F_IPSEC) {
  2327. struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
  2328. int nhead = 0;
  2329. if (x) {
  2330. struct ethhdr *eth;
  2331. struct iphdr *iph;
  2332. int ret;
  2333. nhead = x->props.header_len - skb_headroom(skb);
  2334. if (nhead > 0) {
  2335. ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
  2336. if (ret < 0) {
  2337. pr_err("Error expanding ipsec packet %d\n",
  2338. ret);
  2339. goto err;
  2340. }
  2341. }
  2342. /* ipsec is not expecting ll header */
  2343. skb_pull(skb, ETH_HLEN);
  2344. ret = pktgen_output_ipsec(skb, pkt_dev);
  2345. if (ret) {
  2346. pr_err("Error creating ipsec packet %d\n", ret);
  2347. goto err;
  2348. }
  2349. /* restore ll */
  2350. eth = skb_push(skb, ETH_HLEN);
  2351. memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
  2352. eth->h_proto = protocol;
  2353. /* Update IPv4 header len as well as checksum value */
  2354. iph = ip_hdr(skb);
  2355. iph->tot_len = htons(skb->len - ETH_HLEN);
  2356. ip_send_check(iph);
  2357. }
  2358. }
  2359. return 1;
  2360. err:
  2361. kfree_skb(skb);
  2362. return 0;
  2363. }
  2364. #endif
  2365. static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
  2366. {
  2367. unsigned int i;
  2368. for (i = 0; i < pkt_dev->nr_labels; i++)
  2369. *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
  2370. mpls--;
  2371. *mpls |= MPLS_STACK_BOTTOM;
  2372. }
  2373. static inline __be16 build_tci(unsigned int id, unsigned int cfi,
  2374. unsigned int prio)
  2375. {
  2376. return htons(id | (cfi << 12) | (prio << 13));
  2377. }
  2378. static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
  2379. int datalen)
  2380. {
  2381. struct timespec64 timestamp;
  2382. struct pktgen_hdr *pgh;
  2383. pgh = skb_put(skb, sizeof(*pgh));
  2384. datalen -= sizeof(*pgh);
  2385. if (pkt_dev->nfrags <= 0) {
  2386. skb_put_zero(skb, datalen);
  2387. } else {
  2388. int frags = pkt_dev->nfrags;
  2389. int i, len;
  2390. int frag_len;
  2391. if (frags > MAX_SKB_FRAGS)
  2392. frags = MAX_SKB_FRAGS;
  2393. len = datalen - frags * PAGE_SIZE;
  2394. if (len > 0) {
  2395. skb_put_zero(skb, len);
  2396. datalen = frags * PAGE_SIZE;
  2397. }
  2398. i = 0;
  2399. frag_len = min_t(int, datalen / frags, PAGE_SIZE);
  2400. while (datalen > 0) {
  2401. if (unlikely(!pkt_dev->page)) {
  2402. int node = numa_node_id();
  2403. if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
  2404. node = pkt_dev->node;
  2405. pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2406. if (!pkt_dev->page)
  2407. break;
  2408. }
  2409. get_page(pkt_dev->page);
  2410. /*last fragment, fill rest of data*/
  2411. if (i == (frags - 1))
  2412. skb_frag_fill_page_desc(&skb_shinfo(skb)->frags[i],
  2413. pkt_dev->page, 0,
  2414. min(datalen, PAGE_SIZE));
  2415. else
  2416. skb_frag_fill_page_desc(&skb_shinfo(skb)->frags[i],
  2417. pkt_dev->page, 0, frag_len);
  2418. datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2419. skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2420. skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2421. i++;
  2422. skb_shinfo(skb)->nr_frags = i;
  2423. }
  2424. }
  2425. /* Stamp the time, and sequence number,
  2426. * convert them to network byte order
  2427. */
  2428. pgh->pgh_magic = htonl(PKTGEN_MAGIC);
  2429. pgh->seq_num = htonl(pkt_dev->seq_num);
  2430. if (pkt_dev->flags & F_NO_TIMESTAMP) {
  2431. pgh->tv_sec = 0;
  2432. pgh->tv_usec = 0;
  2433. } else {
  2434. /*
  2435. * pgh->tv_sec wraps in y2106 when interpreted as unsigned
  2436. * as done by wireshark, or y2038 when interpreted as signed.
  2437. * This is probably harmless, but if anyone wants to improve
  2438. * it, we could introduce a variant that puts 64-bit nanoseconds
  2439. * into the respective header bytes.
  2440. * This would also be slightly faster to read.
  2441. */
  2442. ktime_get_real_ts64(&timestamp);
  2443. pgh->tv_sec = htonl(timestamp.tv_sec);
  2444. pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
  2445. }
  2446. }
  2447. static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
  2448. struct pktgen_dev *pkt_dev)
  2449. {
  2450. unsigned int extralen = LL_RESERVED_SPACE(dev);
  2451. struct sk_buff *skb = NULL;
  2452. unsigned int size;
  2453. size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
  2454. if (pkt_dev->flags & F_NODE) {
  2455. int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
  2456. skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
  2457. if (likely(skb)) {
  2458. skb_reserve(skb, NET_SKB_PAD);
  2459. skb->dev = dev;
  2460. }
  2461. } else {
  2462. skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
  2463. }
  2464. /* the caller pre-fetches from skb->data and reserves for the mac hdr */
  2465. if (likely(skb))
  2466. skb_reserve(skb, extralen - 16);
  2467. return skb;
  2468. }
  2469. static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
  2470. struct pktgen_dev *pkt_dev)
  2471. {
  2472. struct sk_buff *skb = NULL;
  2473. __u8 *eth;
  2474. struct udphdr *udph;
  2475. int datalen, iplen;
  2476. struct iphdr *iph;
  2477. __be16 protocol = htons(ETH_P_IP);
  2478. __be32 *mpls;
  2479. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2480. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2481. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2482. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2483. u16 queue_map;
  2484. if (pkt_dev->nr_labels)
  2485. protocol = htons(ETH_P_MPLS_UC);
  2486. if (pkt_dev->vlan_id != 0xffff)
  2487. protocol = htons(ETH_P_8021Q);
  2488. /* Update any of the values, used when we're incrementing various
  2489. * fields.
  2490. */
  2491. mod_cur_headers(pkt_dev);
  2492. queue_map = pkt_dev->cur_queue_map;
  2493. skb = pktgen_alloc_skb(odev, pkt_dev);
  2494. if (!skb) {
  2495. sprintf(pkt_dev->result, "No memory");
  2496. return NULL;
  2497. }
  2498. prefetchw(skb->data);
  2499. skb_reserve(skb, 16);
  2500. /* Reserve for ethernet and IP header */
  2501. eth = skb_push(skb, 14);
  2502. mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
  2503. if (pkt_dev->nr_labels)
  2504. mpls_push(mpls, pkt_dev);
  2505. if (pkt_dev->vlan_id != 0xffff) {
  2506. if (pkt_dev->svlan_id != 0xffff) {
  2507. svlan_tci = skb_put(skb, sizeof(__be16));
  2508. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2509. pkt_dev->svlan_cfi,
  2510. pkt_dev->svlan_p);
  2511. svlan_encapsulated_proto = skb_put(skb,
  2512. sizeof(__be16));
  2513. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2514. }
  2515. vlan_tci = skb_put(skb, sizeof(__be16));
  2516. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2517. pkt_dev->vlan_cfi,
  2518. pkt_dev->vlan_p);
  2519. vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
  2520. *vlan_encapsulated_proto = htons(ETH_P_IP);
  2521. }
  2522. skb_reset_mac_header(skb);
  2523. skb_set_network_header(skb, skb->len);
  2524. iph = skb_put(skb, sizeof(struct iphdr));
  2525. skb_set_transport_header(skb, skb->len);
  2526. udph = skb_put(skb, sizeof(struct udphdr));
  2527. skb_set_queue_mapping(skb, queue_map);
  2528. skb->priority = pkt_dev->skb_priority;
  2529. memcpy(eth, pkt_dev->hh, 12);
  2530. *(__be16 *)&eth[12] = protocol;
  2531. /* Eth + IPh + UDPh + mpls */
  2532. datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
  2533. pkt_dev->pkt_overhead;
  2534. if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
  2535. datalen = sizeof(struct pktgen_hdr);
  2536. udph->source = htons(pkt_dev->cur_udp_src);
  2537. udph->dest = htons(pkt_dev->cur_udp_dst);
  2538. udph->len = htons(datalen + 8); /* DATA + udphdr */
  2539. udph->check = 0;
  2540. iph->ihl = 5;
  2541. iph->version = 4;
  2542. iph->ttl = 32;
  2543. iph->tos = pkt_dev->tos;
  2544. iph->protocol = IPPROTO_UDP; /* UDP */
  2545. iph->saddr = pkt_dev->cur_saddr;
  2546. iph->daddr = pkt_dev->cur_daddr;
  2547. iph->id = htons(pkt_dev->ip_id);
  2548. pkt_dev->ip_id++;
  2549. iph->frag_off = 0;
  2550. iplen = 20 + 8 + datalen;
  2551. iph->tot_len = htons(iplen);
  2552. ip_send_check(iph);
  2553. skb->protocol = protocol;
  2554. skb->dev = odev;
  2555. skb->pkt_type = PACKET_HOST;
  2556. pktgen_finalize_skb(pkt_dev, skb, datalen);
  2557. if (!(pkt_dev->flags & F_UDPCSUM)) {
  2558. skb->ip_summed = CHECKSUM_NONE;
  2559. } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
  2560. skb->ip_summed = CHECKSUM_PARTIAL;
  2561. skb->csum = 0;
  2562. udp4_hwcsum(skb, iph->saddr, iph->daddr);
  2563. } else {
  2564. __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
  2565. /* add protocol-dependent pseudo-header */
  2566. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  2567. datalen + 8, IPPROTO_UDP, csum);
  2568. if (udph->check == 0)
  2569. udph->check = CSUM_MANGLED_0;
  2570. }
  2571. #ifdef CONFIG_XFRM
  2572. if (!process_ipsec(pkt_dev, skb, protocol))
  2573. return NULL;
  2574. #endif
  2575. return skb;
  2576. }
  2577. static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
  2578. struct pktgen_dev *pkt_dev)
  2579. {
  2580. struct sk_buff *skb = NULL;
  2581. __u8 *eth;
  2582. struct udphdr *udph;
  2583. int datalen, udplen;
  2584. struct ipv6hdr *iph;
  2585. __be16 protocol = htons(ETH_P_IPV6);
  2586. __be32 *mpls;
  2587. __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
  2588. __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
  2589. __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
  2590. __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
  2591. u16 queue_map;
  2592. if (pkt_dev->nr_labels)
  2593. protocol = htons(ETH_P_MPLS_UC);
  2594. if (pkt_dev->vlan_id != 0xffff)
  2595. protocol = htons(ETH_P_8021Q);
  2596. /* Update any of the values, used when we're incrementing various
  2597. * fields.
  2598. */
  2599. mod_cur_headers(pkt_dev);
  2600. queue_map = pkt_dev->cur_queue_map;
  2601. skb = pktgen_alloc_skb(odev, pkt_dev);
  2602. if (!skb) {
  2603. sprintf(pkt_dev->result, "No memory");
  2604. return NULL;
  2605. }
  2606. prefetchw(skb->data);
  2607. skb_reserve(skb, 16);
  2608. /* Reserve for ethernet and IP header */
  2609. eth = skb_push(skb, 14);
  2610. mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
  2611. if (pkt_dev->nr_labels)
  2612. mpls_push(mpls, pkt_dev);
  2613. if (pkt_dev->vlan_id != 0xffff) {
  2614. if (pkt_dev->svlan_id != 0xffff) {
  2615. svlan_tci = skb_put(skb, sizeof(__be16));
  2616. *svlan_tci = build_tci(pkt_dev->svlan_id,
  2617. pkt_dev->svlan_cfi,
  2618. pkt_dev->svlan_p);
  2619. svlan_encapsulated_proto = skb_put(skb,
  2620. sizeof(__be16));
  2621. *svlan_encapsulated_proto = htons(ETH_P_8021Q);
  2622. }
  2623. vlan_tci = skb_put(skb, sizeof(__be16));
  2624. *vlan_tci = build_tci(pkt_dev->vlan_id,
  2625. pkt_dev->vlan_cfi,
  2626. pkt_dev->vlan_p);
  2627. vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
  2628. *vlan_encapsulated_proto = htons(ETH_P_IPV6);
  2629. }
  2630. skb_reset_mac_header(skb);
  2631. skb_set_network_header(skb, skb->len);
  2632. iph = skb_put(skb, sizeof(struct ipv6hdr));
  2633. skb_set_transport_header(skb, skb->len);
  2634. udph = skb_put(skb, sizeof(struct udphdr));
  2635. skb_set_queue_mapping(skb, queue_map);
  2636. skb->priority = pkt_dev->skb_priority;
  2637. memcpy(eth, pkt_dev->hh, 12);
  2638. *(__be16 *) &eth[12] = protocol;
  2639. /* Eth + IPh + UDPh + mpls */
  2640. datalen = pkt_dev->cur_pkt_size - 14 -
  2641. sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
  2642. pkt_dev->pkt_overhead;
  2643. if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
  2644. datalen = sizeof(struct pktgen_hdr);
  2645. net_info_ratelimited("increased datalen to %d\n", datalen);
  2646. }
  2647. udplen = datalen + sizeof(struct udphdr);
  2648. udph->source = htons(pkt_dev->cur_udp_src);
  2649. udph->dest = htons(pkt_dev->cur_udp_dst);
  2650. udph->len = htons(udplen);
  2651. udph->check = 0;
  2652. *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
  2653. if (pkt_dev->traffic_class) {
  2654. /* Version + traffic class + flow (0) */
  2655. *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
  2656. }
  2657. iph->hop_limit = 32;
  2658. iph->payload_len = htons(udplen);
  2659. iph->nexthdr = IPPROTO_UDP;
  2660. iph->daddr = pkt_dev->cur_in6_daddr;
  2661. iph->saddr = pkt_dev->cur_in6_saddr;
  2662. skb->protocol = protocol;
  2663. skb->dev = odev;
  2664. skb->pkt_type = PACKET_HOST;
  2665. pktgen_finalize_skb(pkt_dev, skb, datalen);
  2666. if (!(pkt_dev->flags & F_UDPCSUM)) {
  2667. skb->ip_summed = CHECKSUM_NONE;
  2668. } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
  2669. skb->ip_summed = CHECKSUM_PARTIAL;
  2670. skb->csum_start = skb_transport_header(skb) - skb->head;
  2671. skb->csum_offset = offsetof(struct udphdr, check);
  2672. udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
  2673. } else {
  2674. __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
  2675. /* add protocol-dependent pseudo-header */
  2676. udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
  2677. if (udph->check == 0)
  2678. udph->check = CSUM_MANGLED_0;
  2679. }
  2680. return skb;
  2681. }
  2682. static struct sk_buff *fill_packet(struct net_device *odev,
  2683. struct pktgen_dev *pkt_dev)
  2684. {
  2685. if (pkt_dev->flags & F_IPV6)
  2686. return fill_packet_ipv6(odev, pkt_dev);
  2687. else
  2688. return fill_packet_ipv4(odev, pkt_dev);
  2689. }
  2690. static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
  2691. {
  2692. pkt_dev->seq_num = 1;
  2693. pkt_dev->idle_acc = 0;
  2694. pkt_dev->sofar = 0;
  2695. pkt_dev->tx_bytes = 0;
  2696. pkt_dev->errors = 0;
  2697. }
  2698. /* Set up structure for sending pkts, clear counters */
  2699. static void pktgen_run(struct pktgen_thread *t)
  2700. {
  2701. struct pktgen_dev *pkt_dev;
  2702. int started = 0;
  2703. func_enter();
  2704. rcu_read_lock();
  2705. list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
  2706. /*
  2707. * setup odev and create initial packet.
  2708. */
  2709. pktgen_setup_inject(pkt_dev);
  2710. if (pkt_dev->odev) {
  2711. pktgen_clear_counters(pkt_dev);
  2712. pkt_dev->skb = NULL;
  2713. pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
  2714. set_pkt_overhead(pkt_dev);
  2715. strscpy(pkt_dev->result, "Starting");
  2716. pkt_dev->running = 1; /* Cranke yeself! */
  2717. started++;
  2718. } else
  2719. strscpy(pkt_dev->result, "Error starting");
  2720. }
  2721. rcu_read_unlock();
  2722. if (started)
  2723. t->control &= ~(T_STOP);
  2724. }
  2725. static void pktgen_handle_all_threads(struct pktgen_net *pn, u32 flags)
  2726. {
  2727. struct pktgen_thread *t;
  2728. mutex_lock(&pktgen_thread_lock);
  2729. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2730. t->control |= (flags);
  2731. mutex_unlock(&pktgen_thread_lock);
  2732. }
  2733. static void pktgen_stop_all_threads(struct pktgen_net *pn)
  2734. {
  2735. func_enter();
  2736. pktgen_handle_all_threads(pn, T_STOP);
  2737. }
  2738. static int thread_is_running(const struct pktgen_thread *t)
  2739. {
  2740. const struct pktgen_dev *pkt_dev;
  2741. rcu_read_lock();
  2742. list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
  2743. if (pkt_dev->running) {
  2744. rcu_read_unlock();
  2745. return 1;
  2746. }
  2747. rcu_read_unlock();
  2748. return 0;
  2749. }
  2750. static int pktgen_wait_thread_run(struct pktgen_thread *t)
  2751. {
  2752. while (thread_is_running(t)) {
  2753. /* note: 't' will still be around even after the unlock/lock
  2754. * cycle because pktgen_thread threads are only cleared at
  2755. * net exit
  2756. */
  2757. mutex_unlock(&pktgen_thread_lock);
  2758. msleep_interruptible(100);
  2759. mutex_lock(&pktgen_thread_lock);
  2760. if (signal_pending(current))
  2761. goto signal;
  2762. }
  2763. return 1;
  2764. signal:
  2765. return 0;
  2766. }
  2767. static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
  2768. {
  2769. struct pktgen_thread *t;
  2770. int sig = 1;
  2771. /* prevent from racing with rmmod */
  2772. if (!try_module_get(THIS_MODULE))
  2773. return sig;
  2774. mutex_lock(&pktgen_thread_lock);
  2775. list_for_each_entry(t, &pn->pktgen_threads, th_list) {
  2776. sig = pktgen_wait_thread_run(t);
  2777. if (sig == 0)
  2778. break;
  2779. }
  2780. if (sig == 0)
  2781. list_for_each_entry(t, &pn->pktgen_threads, th_list)
  2782. t->control |= (T_STOP);
  2783. mutex_unlock(&pktgen_thread_lock);
  2784. module_put(THIS_MODULE);
  2785. return sig;
  2786. }
  2787. static void pktgen_run_all_threads(struct pktgen_net *pn)
  2788. {
  2789. func_enter();
  2790. pktgen_handle_all_threads(pn, T_RUN);
  2791. /* Propagate thread->control */
  2792. schedule_timeout_interruptible(msecs_to_jiffies(125));
  2793. pktgen_wait_all_threads_run(pn);
  2794. }
  2795. static void pktgen_reset_all_threads(struct pktgen_net *pn)
  2796. {
  2797. func_enter();
  2798. pktgen_handle_all_threads(pn, T_REMDEVALL);
  2799. /* Propagate thread->control */
  2800. schedule_timeout_interruptible(msecs_to_jiffies(125));
  2801. pktgen_wait_all_threads_run(pn);
  2802. }
  2803. static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
  2804. {
  2805. __u64 bps, mbps, pps;
  2806. char *p = pkt_dev->result;
  2807. ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
  2808. pkt_dev->started_at);
  2809. ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
  2810. p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
  2811. (unsigned long long)ktime_to_us(elapsed),
  2812. (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
  2813. (unsigned long long)ktime_to_us(idle),
  2814. (unsigned long long)pkt_dev->sofar,
  2815. pkt_dev->cur_pkt_size, nr_frags);
  2816. pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
  2817. ktime_to_ns(elapsed));
  2818. if (pkt_dev->n_imix_entries > 0) {
  2819. int i;
  2820. struct imix_pkt *entry;
  2821. bps = 0;
  2822. for (i = 0; i < pkt_dev->n_imix_entries; i++) {
  2823. entry = &pkt_dev->imix_entries[i];
  2824. bps += entry->size * entry->count_so_far;
  2825. }
  2826. bps = div64_u64(bps * 8 * NSEC_PER_SEC, ktime_to_ns(elapsed));
  2827. } else {
  2828. bps = pps * 8 * pkt_dev->cur_pkt_size;
  2829. }
  2830. mbps = bps;
  2831. do_div(mbps, 1000000);
  2832. p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
  2833. (unsigned long long)pps,
  2834. (unsigned long long)mbps,
  2835. (unsigned long long)bps,
  2836. (unsigned long long)pkt_dev->errors);
  2837. }
  2838. /* Set stopped-at timer, remove from running list, do counters & statistics */
  2839. static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
  2840. {
  2841. int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
  2842. if (!pkt_dev->running) {
  2843. pr_warn("interface: %s is already stopped\n",
  2844. pkt_dev->odevname);
  2845. return -EINVAL;
  2846. }
  2847. pkt_dev->running = 0;
  2848. kfree_skb(pkt_dev->skb);
  2849. pkt_dev->skb = NULL;
  2850. pkt_dev->stopped_at = ktime_get();
  2851. show_results(pkt_dev, nr_frags);
  2852. return 0;
  2853. }
  2854. static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
  2855. {
  2856. struct pktgen_dev *pkt_dev, *best = NULL;
  2857. rcu_read_lock();
  2858. list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
  2859. if (!pkt_dev->running)
  2860. continue;
  2861. if (best == NULL)
  2862. best = pkt_dev;
  2863. else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
  2864. best = pkt_dev;
  2865. }
  2866. rcu_read_unlock();
  2867. return best;
  2868. }
  2869. static void pktgen_stop(struct pktgen_thread *t)
  2870. {
  2871. struct pktgen_dev *pkt_dev;
  2872. func_enter();
  2873. rcu_read_lock();
  2874. list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
  2875. pktgen_stop_device(pkt_dev);
  2876. }
  2877. rcu_read_unlock();
  2878. }
  2879. /*
  2880. * one of our devices needs to be removed - find it
  2881. * and remove it
  2882. */
  2883. static void pktgen_rem_one_if(struct pktgen_thread *t)
  2884. {
  2885. struct list_head *q, *n;
  2886. struct pktgen_dev *cur;
  2887. func_enter();
  2888. list_for_each_safe(q, n, &t->if_list) {
  2889. cur = list_entry(q, struct pktgen_dev, list);
  2890. if (!cur->removal_mark)
  2891. continue;
  2892. kfree_skb(cur->skb);
  2893. cur->skb = NULL;
  2894. pktgen_remove_device(t, cur);
  2895. break;
  2896. }
  2897. }
  2898. static void pktgen_rem_all_ifs(struct pktgen_thread *t)
  2899. {
  2900. struct list_head *q, *n;
  2901. struct pktgen_dev *cur;
  2902. func_enter();
  2903. /* Remove all devices, free mem */
  2904. list_for_each_safe(q, n, &t->if_list) {
  2905. cur = list_entry(q, struct pktgen_dev, list);
  2906. kfree_skb(cur->skb);
  2907. cur->skb = NULL;
  2908. pktgen_remove_device(t, cur);
  2909. }
  2910. }
  2911. static void pktgen_rem_thread(struct pktgen_thread *t)
  2912. {
  2913. /* Remove from the thread list */
  2914. remove_proc_entry(t->tsk->comm, t->net->proc_dir);
  2915. }
  2916. static void pktgen_resched(struct pktgen_dev *pkt_dev)
  2917. {
  2918. ktime_t idle_start = ktime_get();
  2919. schedule();
  2920. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
  2921. }
  2922. static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
  2923. {
  2924. ktime_t idle_start = ktime_get();
  2925. while (refcount_read(&(pkt_dev->skb->users)) != 1) {
  2926. if (signal_pending(current))
  2927. break;
  2928. if (need_resched())
  2929. pktgen_resched(pkt_dev);
  2930. else
  2931. cpu_relax();
  2932. }
  2933. pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
  2934. }
  2935. static void pktgen_xmit(struct pktgen_dev *pkt_dev)
  2936. {
  2937. bool skb_shared = !!(READ_ONCE(pkt_dev->flags) & F_SHARED);
  2938. struct net_device *odev = pkt_dev->odev;
  2939. struct netdev_queue *txq;
  2940. unsigned int burst = 1;
  2941. struct sk_buff *skb;
  2942. int clone_skb = 0;
  2943. int ret;
  2944. /* If 'skb_shared' is false, the read of possible
  2945. * new values (if any) for 'burst' and 'clone_skb' will be skipped to
  2946. * prevent some concurrent changes from slipping in. And the stabilized
  2947. * config will be read in during the next run of pktgen_xmit.
  2948. */
  2949. if (skb_shared) {
  2950. burst = READ_ONCE(pkt_dev->burst);
  2951. clone_skb = READ_ONCE(pkt_dev->clone_skb);
  2952. }
  2953. /* If device is offline, then don't send */
  2954. if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
  2955. pktgen_stop_device(pkt_dev);
  2956. return;
  2957. }
  2958. /* This is max DELAY, this has special meaning of
  2959. * "never transmit"
  2960. */
  2961. if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
  2962. pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
  2963. return;
  2964. }
  2965. /* If no skb or clone count exhausted then get new one */
  2966. if (!pkt_dev->skb || (pkt_dev->last_ok &&
  2967. ++pkt_dev->clone_count >= clone_skb)) {
  2968. /* build a new pkt */
  2969. kfree_skb(pkt_dev->skb);
  2970. pkt_dev->skb = fill_packet(odev, pkt_dev);
  2971. if (pkt_dev->skb == NULL) {
  2972. pr_err("ERROR: couldn't allocate skb in fill_packet\n");
  2973. schedule();
  2974. pkt_dev->clone_count--; /* back out increment, OOM */
  2975. return;
  2976. }
  2977. pkt_dev->last_pkt_size = pkt_dev->skb->len;
  2978. pkt_dev->clone_count = 0; /* reset counter */
  2979. }
  2980. if (pkt_dev->delay && pkt_dev->last_ok)
  2981. spin(pkt_dev, pkt_dev->next_tx);
  2982. if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
  2983. skb = pkt_dev->skb;
  2984. skb->protocol = eth_type_trans(skb, skb->dev);
  2985. if (skb_shared)
  2986. refcount_add(burst, &skb->users);
  2987. local_bh_disable();
  2988. do {
  2989. ret = netif_receive_skb(skb);
  2990. if (ret == NET_RX_DROP)
  2991. pkt_dev->errors++;
  2992. pkt_dev->sofar++;
  2993. pkt_dev->seq_num++;
  2994. if (unlikely(!skb_shared)) {
  2995. pkt_dev->skb = NULL;
  2996. break;
  2997. }
  2998. if (refcount_read(&skb->users) != burst) {
  2999. /* skb was queued by rps/rfs or taps,
  3000. * so cannot reuse this skb
  3001. */
  3002. WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
  3003. /* get out of the loop and wait
  3004. * until skb is consumed
  3005. */
  3006. break;
  3007. }
  3008. /* skb was 'freed' by stack, so clean few
  3009. * bits and reuse it
  3010. */
  3011. skb_reset_redirect(skb);
  3012. } while (--burst > 0);
  3013. goto out; /* Skips xmit_mode M_START_XMIT */
  3014. } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
  3015. local_bh_disable();
  3016. if (skb_shared)
  3017. refcount_inc(&pkt_dev->skb->users);
  3018. ret = dev_queue_xmit(pkt_dev->skb);
  3019. if (!skb_shared && dev_xmit_complete(ret))
  3020. pkt_dev->skb = NULL;
  3021. switch (ret) {
  3022. case NET_XMIT_SUCCESS:
  3023. pkt_dev->sofar++;
  3024. pkt_dev->seq_num++;
  3025. pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
  3026. break;
  3027. case NET_XMIT_DROP:
  3028. case NET_XMIT_CN:
  3029. /* These are all valid return codes for a qdisc but
  3030. * indicate packets are being dropped or will likely
  3031. * be dropped soon.
  3032. */
  3033. case NETDEV_TX_BUSY:
  3034. /* qdisc may call dev_hard_start_xmit directly in cases
  3035. * where no queues exist e.g. loopback device, virtual
  3036. * devices, etc. In this case we need to handle
  3037. * NETDEV_TX_ codes.
  3038. */
  3039. default:
  3040. pkt_dev->errors++;
  3041. net_info_ratelimited("%s xmit error: %d\n",
  3042. pkt_dev->odevname, ret);
  3043. break;
  3044. }
  3045. goto out;
  3046. }
  3047. txq = skb_get_tx_queue(odev, pkt_dev->skb);
  3048. local_bh_disable();
  3049. HARD_TX_LOCK(odev, txq, smp_processor_id());
  3050. if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
  3051. pkt_dev->last_ok = 0;
  3052. goto unlock;
  3053. }
  3054. if (skb_shared)
  3055. refcount_add(burst, &pkt_dev->skb->users);
  3056. xmit_more:
  3057. ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
  3058. if (!skb_shared && dev_xmit_complete(ret))
  3059. pkt_dev->skb = NULL;
  3060. switch (ret) {
  3061. case NETDEV_TX_OK:
  3062. pkt_dev->last_ok = 1;
  3063. pkt_dev->sofar++;
  3064. pkt_dev->seq_num++;
  3065. pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
  3066. if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
  3067. goto xmit_more;
  3068. break;
  3069. case NET_XMIT_DROP:
  3070. case NET_XMIT_CN:
  3071. /* skb has been consumed */
  3072. pkt_dev->errors++;
  3073. break;
  3074. default: /* Drivers are not supposed to return other values! */
  3075. net_info_ratelimited("%s xmit error: %d\n",
  3076. pkt_dev->odevname, ret);
  3077. pkt_dev->errors++;
  3078. fallthrough;
  3079. case NETDEV_TX_BUSY:
  3080. /* Retry it next time */
  3081. if (skb_shared)
  3082. refcount_dec(&pkt_dev->skb->users);
  3083. pkt_dev->last_ok = 0;
  3084. }
  3085. if (unlikely(burst))
  3086. WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
  3087. unlock:
  3088. HARD_TX_UNLOCK(odev, txq);
  3089. out:
  3090. local_bh_enable();
  3091. /* If pkt_dev->count is zero, then run forever */
  3092. if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
  3093. if (pkt_dev->skb)
  3094. pktgen_wait_for_skb(pkt_dev);
  3095. /* Done with this */
  3096. pktgen_stop_device(pkt_dev);
  3097. }
  3098. }
  3099. /*
  3100. * Main loop of the thread goes here
  3101. */
  3102. static int pktgen_thread_worker(void *arg)
  3103. {
  3104. struct pktgen_thread *t = arg;
  3105. struct pktgen_dev *pkt_dev = NULL;
  3106. int cpu = t->cpu;
  3107. WARN_ON_ONCE(smp_processor_id() != cpu);
  3108. init_waitqueue_head(&t->queue);
  3109. complete(&t->start_done);
  3110. pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
  3111. set_freezable();
  3112. while (!kthread_should_stop()) {
  3113. pkt_dev = next_to_run(t);
  3114. if (unlikely(!pkt_dev && t->control == 0)) {
  3115. if (t->net->pktgen_exiting)
  3116. break;
  3117. wait_event_freezable_timeout(t->queue,
  3118. t->control != 0, HZ / 10);
  3119. continue;
  3120. }
  3121. if (likely(pkt_dev)) {
  3122. pktgen_xmit(pkt_dev);
  3123. if (need_resched())
  3124. pktgen_resched(pkt_dev);
  3125. else
  3126. cpu_relax();
  3127. }
  3128. if (t->control & T_STOP) {
  3129. pktgen_stop(t);
  3130. t->control &= ~(T_STOP);
  3131. }
  3132. if (t->control & T_RUN) {
  3133. pktgen_run(t);
  3134. t->control &= ~(T_RUN);
  3135. }
  3136. if (t->control & T_REMDEVALL) {
  3137. pktgen_rem_all_ifs(t);
  3138. t->control &= ~(T_REMDEVALL);
  3139. }
  3140. if (t->control & T_REMDEV) {
  3141. pktgen_rem_one_if(t);
  3142. t->control &= ~(T_REMDEV);
  3143. }
  3144. try_to_freeze();
  3145. }
  3146. pr_debug("%s stopping all device\n", t->tsk->comm);
  3147. pktgen_stop(t);
  3148. pr_debug("%s removing all device\n", t->tsk->comm);
  3149. pktgen_rem_all_ifs(t);
  3150. pr_debug("%s removing thread\n", t->tsk->comm);
  3151. pktgen_rem_thread(t);
  3152. return 0;
  3153. }
  3154. static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
  3155. const char *ifname, bool exact)
  3156. {
  3157. struct pktgen_dev *p, *pkt_dev = NULL;
  3158. size_t len = strlen(ifname);
  3159. rcu_read_lock();
  3160. list_for_each_entry_rcu(p, &t->if_list, list)
  3161. if (strncmp(p->odevname, ifname, len) == 0) {
  3162. if (p->odevname[len]) {
  3163. if (exact || p->odevname[len] != '@')
  3164. continue;
  3165. }
  3166. pkt_dev = p;
  3167. break;
  3168. }
  3169. rcu_read_unlock();
  3170. pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
  3171. return pkt_dev;
  3172. }
  3173. /*
  3174. * Adds a dev at front of if_list.
  3175. */
  3176. static int add_dev_to_thread(struct pktgen_thread *t,
  3177. struct pktgen_dev *pkt_dev)
  3178. {
  3179. int rv = 0;
  3180. /* This function cannot be called concurrently, as its called
  3181. * under pktgen_thread_lock mutex, but it can run from
  3182. * userspace on another CPU than the kthread. The if_lock()
  3183. * is used here to sync with concurrent instances of
  3184. * _rem_dev_from_if_list() invoked via kthread, which is also
  3185. * updating the if_list
  3186. */
  3187. if_lock(t);
  3188. if (pkt_dev->pg_thread) {
  3189. pr_err("ERROR: already assigned to a thread\n");
  3190. rv = -EBUSY;
  3191. goto out;
  3192. }
  3193. pkt_dev->running = 0;
  3194. pkt_dev->pg_thread = t;
  3195. list_add_rcu(&pkt_dev->list, &t->if_list);
  3196. out:
  3197. if_unlock(t);
  3198. return rv;
  3199. }
  3200. /* Called under thread lock */
  3201. static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
  3202. {
  3203. struct pktgen_dev *pkt_dev;
  3204. int err;
  3205. int node = cpu_to_node(t->cpu);
  3206. /* We don't allow a device to be on several threads */
  3207. pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
  3208. if (pkt_dev) {
  3209. pr_err("ERROR: interface already used\n");
  3210. return -EBUSY;
  3211. }
  3212. pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
  3213. if (!pkt_dev)
  3214. return -ENOMEM;
  3215. strscpy(pkt_dev->odevname, ifname);
  3216. pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
  3217. sizeof(struct flow_state)),
  3218. node);
  3219. if (pkt_dev->flows == NULL) {
  3220. kfree(pkt_dev);
  3221. return -ENOMEM;
  3222. }
  3223. pkt_dev->removal_mark = 0;
  3224. pkt_dev->nfrags = 0;
  3225. pkt_dev->delay = pg_delay_d;
  3226. pkt_dev->count = pg_count_d;
  3227. pkt_dev->sofar = 0;
  3228. pkt_dev->udp_src_min = 9; /* sink port */
  3229. pkt_dev->udp_src_max = 9;
  3230. pkt_dev->udp_dst_min = 9;
  3231. pkt_dev->udp_dst_max = 9;
  3232. pkt_dev->vlan_p = 0;
  3233. pkt_dev->vlan_cfi = 0;
  3234. pkt_dev->vlan_id = 0xffff;
  3235. pkt_dev->svlan_p = 0;
  3236. pkt_dev->svlan_cfi = 0;
  3237. pkt_dev->svlan_id = 0xffff;
  3238. pkt_dev->burst = 1;
  3239. pkt_dev->node = NUMA_NO_NODE;
  3240. pkt_dev->flags = F_SHARED; /* SKB shared by default */
  3241. err = pktgen_setup_dev(t->net, pkt_dev, ifname);
  3242. if (err)
  3243. goto out1;
  3244. if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
  3245. pkt_dev->clone_skb = pg_clone_skb_d;
  3246. pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
  3247. &pktgen_if_proc_ops, pkt_dev);
  3248. if (!pkt_dev->entry) {
  3249. pr_err("cannot create %s/%s procfs entry\n",
  3250. PG_PROC_DIR, ifname);
  3251. err = -EINVAL;
  3252. goto out2;
  3253. }
  3254. #ifdef CONFIG_XFRM
  3255. pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
  3256. pkt_dev->ipsproto = IPPROTO_ESP;
  3257. /* xfrm tunnel mode needs additional dst to extract outer
  3258. * ip header protocol/ttl/id field, here create a phony one.
  3259. * instead of looking for a valid rt, which definitely hurting
  3260. * performance under such circumstance.
  3261. */
  3262. pkt_dev->dstops.family = AF_INET;
  3263. pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
  3264. dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
  3265. pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
  3266. pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
  3267. #endif
  3268. return add_dev_to_thread(t, pkt_dev);
  3269. out2:
  3270. netdev_put(pkt_dev->odev, &pkt_dev->dev_tracker);
  3271. out1:
  3272. #ifdef CONFIG_XFRM
  3273. free_SAs(pkt_dev);
  3274. #endif
  3275. vfree(pkt_dev->flows);
  3276. kfree(pkt_dev);
  3277. return err;
  3278. }
  3279. static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
  3280. {
  3281. struct pktgen_thread *t;
  3282. struct proc_dir_entry *pe;
  3283. struct task_struct *p;
  3284. t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
  3285. cpu_to_node(cpu));
  3286. if (!t) {
  3287. pr_err("ERROR: out of memory, can't create new thread\n");
  3288. return -ENOMEM;
  3289. }
  3290. mutex_init(&t->if_lock);
  3291. t->cpu = cpu;
  3292. INIT_LIST_HEAD(&t->if_list);
  3293. list_add_tail(&t->th_list, &pn->pktgen_threads);
  3294. init_completion(&t->start_done);
  3295. p = kthread_create_on_cpu(pktgen_thread_worker, t, cpu, "kpktgend_%d");
  3296. if (IS_ERR(p)) {
  3297. pr_err("kthread_create_on_node() failed for cpu %d\n", t->cpu);
  3298. list_del(&t->th_list);
  3299. kfree(t);
  3300. return PTR_ERR(p);
  3301. }
  3302. t->tsk = p;
  3303. pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
  3304. &pktgen_thread_proc_ops, t);
  3305. if (!pe) {
  3306. pr_err("cannot create %s/%s procfs entry\n",
  3307. PG_PROC_DIR, t->tsk->comm);
  3308. kthread_stop(p);
  3309. list_del(&t->th_list);
  3310. kfree(t);
  3311. return -EINVAL;
  3312. }
  3313. t->net = pn;
  3314. get_task_struct(p);
  3315. wake_up_process(p);
  3316. wait_for_completion(&t->start_done);
  3317. return 0;
  3318. }
  3319. /*
  3320. * Removes a device from the thread if_list.
  3321. */
  3322. static void _rem_dev_from_if_list(struct pktgen_thread *t,
  3323. struct pktgen_dev *pkt_dev)
  3324. {
  3325. struct list_head *q, *n;
  3326. struct pktgen_dev *p;
  3327. if_lock(t);
  3328. list_for_each_safe(q, n, &t->if_list) {
  3329. p = list_entry(q, struct pktgen_dev, list);
  3330. if (p == pkt_dev)
  3331. list_del_rcu(&p->list);
  3332. }
  3333. if_unlock(t);
  3334. }
  3335. static int pktgen_remove_device(struct pktgen_thread *t,
  3336. struct pktgen_dev *pkt_dev)
  3337. {
  3338. pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
  3339. if (pkt_dev->running) {
  3340. pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
  3341. pktgen_stop_device(pkt_dev);
  3342. }
  3343. /* Dis-associate from the interface */
  3344. if (pkt_dev->odev) {
  3345. netdev_put(pkt_dev->odev, &pkt_dev->dev_tracker);
  3346. pkt_dev->odev = NULL;
  3347. }
  3348. /* Remove proc before if_list entry, because add_device uses
  3349. * list to determine if interface already exist, avoid race
  3350. * with proc_create_data()
  3351. */
  3352. proc_remove(pkt_dev->entry);
  3353. /* And update the thread if_list */
  3354. _rem_dev_from_if_list(t, pkt_dev);
  3355. #ifdef CONFIG_XFRM
  3356. free_SAs(pkt_dev);
  3357. #endif
  3358. vfree(pkt_dev->flows);
  3359. if (pkt_dev->page)
  3360. put_page(pkt_dev->page);
  3361. kfree_rcu(pkt_dev, rcu);
  3362. return 0;
  3363. }
  3364. static int __net_init pg_net_init(struct net *net)
  3365. {
  3366. struct pktgen_net *pn = net_generic(net, pg_net_id);
  3367. struct proc_dir_entry *pe;
  3368. int cpu, ret = 0;
  3369. pn->net = net;
  3370. INIT_LIST_HEAD(&pn->pktgen_threads);
  3371. pn->pktgen_exiting = false;
  3372. pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
  3373. if (!pn->proc_dir) {
  3374. pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
  3375. return -ENODEV;
  3376. }
  3377. pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_proc_ops);
  3378. if (pe == NULL) {
  3379. pr_err("cannot create %s procfs entry\n", PGCTRL);
  3380. ret = -EINVAL;
  3381. goto remove;
  3382. }
  3383. cpus_read_lock();
  3384. for_each_online_cpu(cpu) {
  3385. int err;
  3386. err = pktgen_create_thread(cpu, pn);
  3387. if (err)
  3388. pr_warn("Cannot create thread for cpu %d (%d)\n",
  3389. cpu, err);
  3390. }
  3391. cpus_read_unlock();
  3392. if (list_empty(&pn->pktgen_threads)) {
  3393. pr_err("Initialization failed for all threads\n");
  3394. ret = -ENODEV;
  3395. goto remove_entry;
  3396. }
  3397. return 0;
  3398. remove_entry:
  3399. remove_proc_entry(PGCTRL, pn->proc_dir);
  3400. remove:
  3401. remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
  3402. return ret;
  3403. }
  3404. static void __net_exit pg_net_exit(struct net *net)
  3405. {
  3406. struct pktgen_net *pn = net_generic(net, pg_net_id);
  3407. struct pktgen_thread *t;
  3408. struct list_head *q, *n;
  3409. LIST_HEAD(list);
  3410. /* Stop all interfaces & threads */
  3411. pn->pktgen_exiting = true;
  3412. mutex_lock(&pktgen_thread_lock);
  3413. list_splice_init(&pn->pktgen_threads, &list);
  3414. mutex_unlock(&pktgen_thread_lock);
  3415. list_for_each_safe(q, n, &list) {
  3416. t = list_entry(q, struct pktgen_thread, th_list);
  3417. list_del(&t->th_list);
  3418. kthread_stop_put(t->tsk);
  3419. kfree(t);
  3420. }
  3421. remove_proc_entry(PGCTRL, pn->proc_dir);
  3422. remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
  3423. }
  3424. static struct pernet_operations pg_net_ops = {
  3425. .init = pg_net_init,
  3426. .exit = pg_net_exit,
  3427. .id = &pg_net_id,
  3428. .size = sizeof(struct pktgen_net),
  3429. };
  3430. static int __init pg_init(void)
  3431. {
  3432. int ret = 0;
  3433. pr_info("%s", version);
  3434. ret = register_pernet_subsys(&pg_net_ops);
  3435. if (ret)
  3436. return ret;
  3437. ret = register_netdevice_notifier(&pktgen_notifier_block);
  3438. if (ret)
  3439. unregister_pernet_subsys(&pg_net_ops);
  3440. return ret;
  3441. }
  3442. static void __exit pg_cleanup(void)
  3443. {
  3444. unregister_netdevice_notifier(&pktgen_notifier_block);
  3445. unregister_pernet_subsys(&pg_net_ops);
  3446. /* Don't need rcu_barrier() due to use of kfree_rcu() */
  3447. }
  3448. module_init(pg_init);
  3449. module_exit(pg_cleanup);
  3450. MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
  3451. MODULE_DESCRIPTION("Packet Generator tool");
  3452. MODULE_LICENSE("GPL");
  3453. MODULE_VERSION(VERSION);
  3454. module_param(pg_count_d, int, 0);
  3455. MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
  3456. module_param(pg_delay_d, int, 0);
  3457. MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
  3458. module_param(pg_clone_skb_d, int, 0);
  3459. MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
  3460. module_param(debug, int, 0);
  3461. MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");