virtio_net.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* A network driver using virtio.
  3. *
  4. * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
  5. */
  6. //#define DEBUG
  7. #include <linux/netdevice.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/module.h>
  11. #include <linux/virtio.h>
  12. #include <linux/virtio_net.h>
  13. #include <linux/bpf.h>
  14. #include <linux/bpf_trace.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/slab.h>
  18. #include <linux/cpu.h>
  19. #include <linux/average.h>
  20. #include <linux/filter.h>
  21. #include <linux/kernel.h>
  22. #include <net/route.h>
  23. #include <net/xdp.h>
  24. #include <net/net_failover.h>
  25. static int napi_weight = NAPI_POLL_WEIGHT;
  26. module_param(napi_weight, int, 0444);
  27. static bool csum = true, gso = true, napi_tx = true;
  28. module_param(csum, bool, 0444);
  29. module_param(gso, bool, 0444);
  30. module_param(napi_tx, bool, 0644);
  31. /* FIXME: MTU in config. */
  32. #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  33. #define GOOD_COPY_LEN 128
  34. #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  35. /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
  36. #define VIRTIO_XDP_HEADROOM 256
  37. /* Separating two types of XDP xmit */
  38. #define VIRTIO_XDP_TX BIT(0)
  39. #define VIRTIO_XDP_REDIR BIT(1)
  40. #define VIRTIO_XDP_FLAG BIT(0)
  41. /* RX packet size EWMA. The average packet size is used to determine the packet
  42. * buffer size when refilling RX rings. As the entire RX ring may be refilled
  43. * at once, the weight is chosen so that the EWMA will be insensitive to short-
  44. * term, transient changes in packet size.
  45. */
  46. DECLARE_EWMA(pkt_len, 0, 64)
  47. #define VIRTNET_DRIVER_VERSION "1.0.0"
  48. static const unsigned long guest_offloads[] = {
  49. VIRTIO_NET_F_GUEST_TSO4,
  50. VIRTIO_NET_F_GUEST_TSO6,
  51. VIRTIO_NET_F_GUEST_ECN,
  52. VIRTIO_NET_F_GUEST_UFO,
  53. VIRTIO_NET_F_GUEST_CSUM
  54. };
  55. #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
  56. (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
  57. (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
  58. (1ULL << VIRTIO_NET_F_GUEST_UFO))
  59. struct virtnet_stat_desc {
  60. char desc[ETH_GSTRING_LEN];
  61. size_t offset;
  62. };
  63. struct virtnet_sq_stats {
  64. struct u64_stats_sync syncp;
  65. u64 packets;
  66. u64 bytes;
  67. u64 xdp_tx;
  68. u64 xdp_tx_drops;
  69. u64 kicks;
  70. };
  71. struct virtnet_rq_stats {
  72. struct u64_stats_sync syncp;
  73. u64 packets;
  74. u64 bytes;
  75. u64 drops;
  76. u64 xdp_packets;
  77. u64 xdp_tx;
  78. u64 xdp_redirects;
  79. u64 xdp_drops;
  80. u64 kicks;
  81. };
  82. #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
  83. #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
  84. static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
  85. { "packets", VIRTNET_SQ_STAT(packets) },
  86. { "bytes", VIRTNET_SQ_STAT(bytes) },
  87. { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
  88. { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
  89. { "kicks", VIRTNET_SQ_STAT(kicks) },
  90. };
  91. static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
  92. { "packets", VIRTNET_RQ_STAT(packets) },
  93. { "bytes", VIRTNET_RQ_STAT(bytes) },
  94. { "drops", VIRTNET_RQ_STAT(drops) },
  95. { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
  96. { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
  97. { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
  98. { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
  99. { "kicks", VIRTNET_RQ_STAT(kicks) },
  100. };
  101. #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
  102. #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
  103. /* Internal representation of a send virtqueue */
  104. struct send_queue {
  105. /* Virtqueue associated with this send _queue */
  106. struct virtqueue *vq;
  107. /* TX: fragments + linear part + virtio header */
  108. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  109. /* Name of the send queue: output.$index */
  110. char name[40];
  111. struct virtnet_sq_stats stats;
  112. struct napi_struct napi;
  113. };
  114. /* Internal representation of a receive virtqueue */
  115. struct receive_queue {
  116. /* Virtqueue associated with this receive_queue */
  117. struct virtqueue *vq;
  118. struct napi_struct napi;
  119. struct bpf_prog __rcu *xdp_prog;
  120. struct virtnet_rq_stats stats;
  121. /* Chain pages by the private ptr. */
  122. struct page *pages;
  123. /* Average packet length for mergeable receive buffers. */
  124. struct ewma_pkt_len mrg_avg_pkt_len;
  125. /* Page frag for packet buffer allocation. */
  126. struct page_frag alloc_frag;
  127. /* RX: fragments + linear part + virtio header */
  128. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  129. /* Min single buffer size for mergeable buffers case. */
  130. unsigned int min_buf_len;
  131. /* Name of this receive queue: input.$index */
  132. char name[40];
  133. struct xdp_rxq_info xdp_rxq;
  134. };
  135. /* Control VQ buffers: protected by the rtnl lock */
  136. struct control_buf {
  137. struct virtio_net_ctrl_hdr hdr;
  138. virtio_net_ctrl_ack status;
  139. struct virtio_net_ctrl_mq mq;
  140. u8 promisc;
  141. u8 allmulti;
  142. __virtio16 vid;
  143. __virtio64 offloads;
  144. };
  145. struct virtnet_info {
  146. struct virtio_device *vdev;
  147. struct virtqueue *cvq;
  148. struct net_device *dev;
  149. struct send_queue *sq;
  150. struct receive_queue *rq;
  151. unsigned int status;
  152. /* Max # of queue pairs supported by the device */
  153. u16 max_queue_pairs;
  154. /* # of queue pairs currently used by the driver */
  155. u16 curr_queue_pairs;
  156. /* # of XDP queue pairs currently used by the driver */
  157. u16 xdp_queue_pairs;
  158. /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
  159. bool xdp_enabled;
  160. /* I like... big packets and I cannot lie! */
  161. bool big_packets;
  162. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  163. bool mergeable_rx_bufs;
  164. /* Has control virtqueue */
  165. bool has_cvq;
  166. /* Host can handle any s/g split between our header and packet data */
  167. bool any_header_sg;
  168. /* Packet virtio header size */
  169. u8 hdr_len;
  170. /* Work struct for refilling if we run low on memory. */
  171. struct delayed_work refill;
  172. /* Work struct for config space updates */
  173. struct work_struct config_work;
  174. /* Does the affinity hint is set for virtqueues? */
  175. bool affinity_hint_set;
  176. /* CPU hotplug instances for online & dead */
  177. struct hlist_node node;
  178. struct hlist_node node_dead;
  179. struct control_buf *ctrl;
  180. /* Ethtool settings */
  181. u8 duplex;
  182. u32 speed;
  183. unsigned long guest_offloads;
  184. unsigned long guest_offloads_capable;
  185. /* failover when STANDBY feature enabled */
  186. struct failover *failover;
  187. };
  188. struct padded_vnet_hdr {
  189. struct virtio_net_hdr_mrg_rxbuf hdr;
  190. /*
  191. * hdr is in a separate sg buffer, and data sg buffer shares same page
  192. * with this header sg. This padding makes next sg 16 byte aligned
  193. * after the header.
  194. */
  195. char padding[4];
  196. };
  197. static bool is_xdp_frame(void *ptr)
  198. {
  199. return (unsigned long)ptr & VIRTIO_XDP_FLAG;
  200. }
  201. static void *xdp_to_ptr(struct xdp_frame *ptr)
  202. {
  203. return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
  204. }
  205. static struct xdp_frame *ptr_to_xdp(void *ptr)
  206. {
  207. return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
  208. }
  209. /* Converting between virtqueue no. and kernel tx/rx queue no.
  210. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  211. */
  212. static int vq2txq(struct virtqueue *vq)
  213. {
  214. return (vq->index - 1) / 2;
  215. }
  216. static int txq2vq(int txq)
  217. {
  218. return txq * 2 + 1;
  219. }
  220. static int vq2rxq(struct virtqueue *vq)
  221. {
  222. return vq->index / 2;
  223. }
  224. static int rxq2vq(int rxq)
  225. {
  226. return rxq * 2;
  227. }
  228. static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
  229. {
  230. return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
  231. }
  232. /*
  233. * private is used to chain pages for big packets, put the whole
  234. * most recent used list in the beginning for reuse
  235. */
  236. static void give_pages(struct receive_queue *rq, struct page *page)
  237. {
  238. struct page *end;
  239. /* Find end of list, sew whole thing into vi->rq.pages. */
  240. for (end = page; end->private; end = (struct page *)end->private);
  241. end->private = (unsigned long)rq->pages;
  242. rq->pages = page;
  243. }
  244. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  245. {
  246. struct page *p = rq->pages;
  247. if (p) {
  248. rq->pages = (struct page *)p->private;
  249. /* clear private here, it is used to chain pages */
  250. p->private = 0;
  251. } else
  252. p = alloc_page(gfp_mask);
  253. return p;
  254. }
  255. static void virtqueue_napi_schedule(struct napi_struct *napi,
  256. struct virtqueue *vq)
  257. {
  258. if (napi_schedule_prep(napi)) {
  259. virtqueue_disable_cb(vq);
  260. __napi_schedule(napi);
  261. }
  262. }
  263. static void virtqueue_napi_complete(struct napi_struct *napi,
  264. struct virtqueue *vq, int processed)
  265. {
  266. int opaque;
  267. opaque = virtqueue_enable_cb_prepare(vq);
  268. if (napi_complete_done(napi, processed)) {
  269. if (unlikely(virtqueue_poll(vq, opaque)))
  270. virtqueue_napi_schedule(napi, vq);
  271. } else {
  272. virtqueue_disable_cb(vq);
  273. }
  274. }
  275. static void skb_xmit_done(struct virtqueue *vq)
  276. {
  277. struct virtnet_info *vi = vq->vdev->priv;
  278. struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
  279. /* Suppress further interrupts. */
  280. virtqueue_disable_cb(vq);
  281. if (napi->weight)
  282. virtqueue_napi_schedule(napi, vq);
  283. else
  284. /* We were probably waiting for more output buffers. */
  285. netif_wake_subqueue(vi->dev, vq2txq(vq));
  286. }
  287. #define MRG_CTX_HEADER_SHIFT 22
  288. static void *mergeable_len_to_ctx(unsigned int truesize,
  289. unsigned int headroom)
  290. {
  291. return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
  292. }
  293. static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
  294. {
  295. return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
  296. }
  297. static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
  298. {
  299. return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
  300. }
  301. /* Called from bottom half context */
  302. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  303. struct receive_queue *rq,
  304. struct page *page, unsigned int offset,
  305. unsigned int len, unsigned int truesize,
  306. bool hdr_valid, unsigned int metasize)
  307. {
  308. struct sk_buff *skb;
  309. struct virtio_net_hdr_mrg_rxbuf *hdr;
  310. unsigned int copy, hdr_len, hdr_padded_len;
  311. char *p;
  312. p = page_address(page) + offset;
  313. /* copy small packet so we can reuse these pages for small data */
  314. skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
  315. if (unlikely(!skb))
  316. return NULL;
  317. hdr = skb_vnet_hdr(skb);
  318. hdr_len = vi->hdr_len;
  319. if (vi->mergeable_rx_bufs)
  320. hdr_padded_len = sizeof(*hdr);
  321. else
  322. hdr_padded_len = sizeof(struct padded_vnet_hdr);
  323. /* hdr_valid means no XDP, so we can copy the vnet header */
  324. if (hdr_valid)
  325. memcpy(hdr, p, hdr_len);
  326. len -= hdr_len;
  327. offset += hdr_padded_len;
  328. p += hdr_padded_len;
  329. /* Copy all frame if it fits skb->head, otherwise
  330. * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
  331. */
  332. if (len <= skb_tailroom(skb))
  333. copy = len;
  334. else
  335. copy = ETH_HLEN + metasize;
  336. skb_put_data(skb, p, copy);
  337. if (metasize) {
  338. __skb_pull(skb, metasize);
  339. skb_metadata_set(skb, metasize);
  340. }
  341. len -= copy;
  342. offset += copy;
  343. if (vi->mergeable_rx_bufs) {
  344. if (len)
  345. skb_add_rx_frag(skb, 0, page, offset, len, truesize);
  346. else
  347. put_page(page);
  348. return skb;
  349. }
  350. /*
  351. * Verify that we can indeed put this data into a skb.
  352. * This is here to handle cases when the device erroneously
  353. * tries to receive more than is possible. This is usually
  354. * the case of a broken device.
  355. */
  356. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  357. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  358. dev_kfree_skb(skb);
  359. return NULL;
  360. }
  361. BUG_ON(offset >= PAGE_SIZE);
  362. while (len) {
  363. unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
  364. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
  365. frag_size, truesize);
  366. len -= frag_size;
  367. page = (struct page *)page->private;
  368. offset = 0;
  369. }
  370. if (page)
  371. give_pages(rq, page);
  372. return skb;
  373. }
  374. static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
  375. struct send_queue *sq,
  376. struct xdp_frame *xdpf)
  377. {
  378. struct virtio_net_hdr_mrg_rxbuf *hdr;
  379. int err;
  380. if (unlikely(xdpf->headroom < vi->hdr_len))
  381. return -EOVERFLOW;
  382. /* Make room for virtqueue hdr (also change xdpf->headroom?) */
  383. xdpf->data -= vi->hdr_len;
  384. /* Zero header and leave csum up to XDP layers */
  385. hdr = xdpf->data;
  386. memset(hdr, 0, vi->hdr_len);
  387. xdpf->len += vi->hdr_len;
  388. sg_init_one(sq->sg, xdpf->data, xdpf->len);
  389. err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
  390. GFP_ATOMIC);
  391. if (unlikely(err))
  392. return -ENOSPC; /* Caller handle free/refcnt */
  393. return 0;
  394. }
  395. /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
  396. * the current cpu, so it does not need to be locked.
  397. *
  398. * Here we use marco instead of inline functions because we have to deal with
  399. * three issues at the same time: 1. the choice of sq. 2. judge and execute the
  400. * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
  401. * functions to perfectly solve these three problems at the same time.
  402. */
  403. #define virtnet_xdp_get_sq(vi) ({ \
  404. struct netdev_queue *txq; \
  405. typeof(vi) v = (vi); \
  406. unsigned int qp; \
  407. \
  408. if (v->curr_queue_pairs > nr_cpu_ids) { \
  409. qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
  410. qp += smp_processor_id(); \
  411. txq = netdev_get_tx_queue(v->dev, qp); \
  412. __netif_tx_acquire(txq); \
  413. } else { \
  414. qp = smp_processor_id() % v->curr_queue_pairs; \
  415. txq = netdev_get_tx_queue(v->dev, qp); \
  416. __netif_tx_lock(txq, raw_smp_processor_id()); \
  417. } \
  418. v->sq + qp; \
  419. })
  420. #define virtnet_xdp_put_sq(vi, q) { \
  421. struct netdev_queue *txq; \
  422. typeof(vi) v = (vi); \
  423. \
  424. txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
  425. if (v->curr_queue_pairs > nr_cpu_ids) \
  426. __netif_tx_release(txq); \
  427. else \
  428. __netif_tx_unlock(txq); \
  429. }
  430. static int virtnet_xdp_xmit(struct net_device *dev,
  431. int n, struct xdp_frame **frames, u32 flags)
  432. {
  433. struct virtnet_info *vi = netdev_priv(dev);
  434. struct receive_queue *rq = vi->rq;
  435. struct bpf_prog *xdp_prog;
  436. struct send_queue *sq;
  437. unsigned int len;
  438. int packets = 0;
  439. int bytes = 0;
  440. int drops = 0;
  441. int kicks = 0;
  442. int ret, err;
  443. void *ptr;
  444. int i;
  445. /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
  446. * indicate XDP resources have been successfully allocated.
  447. */
  448. xdp_prog = rcu_dereference(rq->xdp_prog);
  449. if (!xdp_prog)
  450. return -ENXIO;
  451. sq = virtnet_xdp_get_sq(vi);
  452. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
  453. ret = -EINVAL;
  454. drops = n;
  455. goto out;
  456. }
  457. /* Free up any pending old buffers before queueing new ones. */
  458. while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  459. if (likely(is_xdp_frame(ptr))) {
  460. struct xdp_frame *frame = ptr_to_xdp(ptr);
  461. bytes += frame->len;
  462. xdp_return_frame(frame);
  463. } else {
  464. struct sk_buff *skb = ptr;
  465. bytes += skb->len;
  466. napi_consume_skb(skb, false);
  467. }
  468. packets++;
  469. }
  470. for (i = 0; i < n; i++) {
  471. struct xdp_frame *xdpf = frames[i];
  472. err = __virtnet_xdp_xmit_one(vi, sq, xdpf);
  473. if (err) {
  474. xdp_return_frame_rx_napi(xdpf);
  475. drops++;
  476. }
  477. }
  478. ret = n - drops;
  479. if (flags & XDP_XMIT_FLUSH) {
  480. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
  481. kicks = 1;
  482. }
  483. out:
  484. u64_stats_update_begin(&sq->stats.syncp);
  485. sq->stats.bytes += bytes;
  486. sq->stats.packets += packets;
  487. sq->stats.xdp_tx += n;
  488. sq->stats.xdp_tx_drops += drops;
  489. sq->stats.kicks += kicks;
  490. u64_stats_update_end(&sq->stats.syncp);
  491. virtnet_xdp_put_sq(vi, sq);
  492. return ret;
  493. }
  494. static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
  495. {
  496. return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
  497. }
  498. /* We copy the packet for XDP in the following cases:
  499. *
  500. * 1) Packet is scattered across multiple rx buffers.
  501. * 2) Headroom space is insufficient.
  502. *
  503. * This is inefficient but it's a temporary condition that
  504. * we hit right after XDP is enabled and until queue is refilled
  505. * with large buffers with sufficient headroom - so it should affect
  506. * at most queue size packets.
  507. * Afterwards, the conditions to enable
  508. * XDP should preclude the underlying device from sending packets
  509. * across multiple buffers (num_buf > 1), and we make sure buffers
  510. * have enough headroom.
  511. */
  512. static struct page *xdp_linearize_page(struct receive_queue *rq,
  513. u16 *num_buf,
  514. struct page *p,
  515. int offset,
  516. int page_off,
  517. unsigned int *len)
  518. {
  519. struct page *page = alloc_page(GFP_ATOMIC);
  520. if (!page)
  521. return NULL;
  522. memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
  523. page_off += *len;
  524. while (--*num_buf) {
  525. int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  526. unsigned int buflen;
  527. void *buf;
  528. int off;
  529. buf = virtqueue_get_buf(rq->vq, &buflen);
  530. if (unlikely(!buf))
  531. goto err_buf;
  532. p = virt_to_head_page(buf);
  533. off = buf - page_address(p);
  534. /* guard against a misconfigured or uncooperative backend that
  535. * is sending packet larger than the MTU.
  536. */
  537. if ((page_off + buflen + tailroom) > PAGE_SIZE) {
  538. put_page(p);
  539. goto err_buf;
  540. }
  541. memcpy(page_address(page) + page_off,
  542. page_address(p) + off, buflen);
  543. page_off += buflen;
  544. put_page(p);
  545. }
  546. /* Headroom does not contribute to packet length */
  547. *len = page_off - VIRTIO_XDP_HEADROOM;
  548. return page;
  549. err_buf:
  550. __free_pages(page, 0);
  551. return NULL;
  552. }
  553. static struct sk_buff *receive_small(struct net_device *dev,
  554. struct virtnet_info *vi,
  555. struct receive_queue *rq,
  556. void *buf, void *ctx,
  557. unsigned int len,
  558. unsigned int *xdp_xmit,
  559. struct virtnet_rq_stats *stats)
  560. {
  561. struct sk_buff *skb;
  562. struct bpf_prog *xdp_prog;
  563. unsigned int xdp_headroom = (unsigned long)ctx;
  564. unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
  565. unsigned int headroom = vi->hdr_len + header_offset;
  566. unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  567. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  568. struct page *page = virt_to_head_page(buf);
  569. unsigned int delta = 0;
  570. struct page *xdp_page;
  571. int err;
  572. unsigned int metasize = 0;
  573. len -= vi->hdr_len;
  574. stats->bytes += len;
  575. rcu_read_lock();
  576. xdp_prog = rcu_dereference(rq->xdp_prog);
  577. if (xdp_prog) {
  578. struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
  579. struct xdp_frame *xdpf;
  580. struct xdp_buff xdp;
  581. void *orig_data;
  582. u32 act;
  583. if (unlikely(hdr->hdr.gso_type))
  584. goto err_xdp;
  585. if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
  586. int offset = buf - page_address(page) + header_offset;
  587. unsigned int tlen = len + vi->hdr_len;
  588. u16 num_buf = 1;
  589. xdp_headroom = virtnet_get_headroom(vi);
  590. header_offset = VIRTNET_RX_PAD + xdp_headroom;
  591. headroom = vi->hdr_len + header_offset;
  592. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  593. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  594. xdp_page = xdp_linearize_page(rq, &num_buf, page,
  595. offset, header_offset,
  596. &tlen);
  597. if (!xdp_page)
  598. goto err_xdp;
  599. buf = page_address(xdp_page);
  600. put_page(page);
  601. page = xdp_page;
  602. }
  603. xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
  604. xdp.data = xdp.data_hard_start + xdp_headroom;
  605. xdp.data_end = xdp.data + len;
  606. xdp.data_meta = xdp.data;
  607. xdp.rxq = &rq->xdp_rxq;
  608. orig_data = xdp.data;
  609. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  610. stats->xdp_packets++;
  611. switch (act) {
  612. case XDP_PASS:
  613. /* Recalculate length in case bpf program changed it */
  614. delta = orig_data - xdp.data;
  615. len = xdp.data_end - xdp.data;
  616. metasize = xdp.data - xdp.data_meta;
  617. break;
  618. case XDP_TX:
  619. stats->xdp_tx++;
  620. xdpf = convert_to_xdp_frame(&xdp);
  621. if (unlikely(!xdpf))
  622. goto err_xdp;
  623. err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
  624. if (unlikely(err < 0)) {
  625. trace_xdp_exception(vi->dev, xdp_prog, act);
  626. goto err_xdp;
  627. }
  628. *xdp_xmit |= VIRTIO_XDP_TX;
  629. rcu_read_unlock();
  630. goto xdp_xmit;
  631. case XDP_REDIRECT:
  632. stats->xdp_redirects++;
  633. err = xdp_do_redirect(dev, &xdp, xdp_prog);
  634. if (err)
  635. goto err_xdp;
  636. *xdp_xmit |= VIRTIO_XDP_REDIR;
  637. rcu_read_unlock();
  638. goto xdp_xmit;
  639. default:
  640. bpf_warn_invalid_xdp_action(act);
  641. /* fall through */
  642. case XDP_ABORTED:
  643. trace_xdp_exception(vi->dev, xdp_prog, act);
  644. case XDP_DROP:
  645. goto err_xdp;
  646. }
  647. }
  648. rcu_read_unlock();
  649. skb = build_skb(buf, buflen);
  650. if (!skb) {
  651. put_page(page);
  652. goto err;
  653. }
  654. skb_reserve(skb, headroom - delta);
  655. skb_put(skb, len);
  656. if (!delta) {
  657. buf += header_offset;
  658. memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
  659. } /* keep zeroed vnet hdr since packet was changed by bpf */
  660. if (metasize)
  661. skb_metadata_set(skb, metasize);
  662. err:
  663. return skb;
  664. err_xdp:
  665. rcu_read_unlock();
  666. stats->xdp_drops++;
  667. stats->drops++;
  668. put_page(page);
  669. xdp_xmit:
  670. return NULL;
  671. }
  672. static struct sk_buff *receive_big(struct net_device *dev,
  673. struct virtnet_info *vi,
  674. struct receive_queue *rq,
  675. void *buf,
  676. unsigned int len,
  677. struct virtnet_rq_stats *stats)
  678. {
  679. struct page *page = buf;
  680. struct sk_buff *skb =
  681. page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0);
  682. stats->bytes += len - vi->hdr_len;
  683. if (unlikely(!skb))
  684. goto err;
  685. return skb;
  686. err:
  687. stats->drops++;
  688. give_pages(rq, page);
  689. return NULL;
  690. }
  691. static struct sk_buff *receive_mergeable(struct net_device *dev,
  692. struct virtnet_info *vi,
  693. struct receive_queue *rq,
  694. void *buf,
  695. void *ctx,
  696. unsigned int len,
  697. unsigned int *xdp_xmit,
  698. struct virtnet_rq_stats *stats)
  699. {
  700. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  701. u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  702. struct page *page = virt_to_head_page(buf);
  703. int offset = buf - page_address(page);
  704. struct sk_buff *head_skb, *curr_skb;
  705. struct bpf_prog *xdp_prog;
  706. unsigned int truesize;
  707. unsigned int headroom = mergeable_ctx_to_headroom(ctx);
  708. int err;
  709. unsigned int metasize = 0;
  710. head_skb = NULL;
  711. stats->bytes += len - vi->hdr_len;
  712. rcu_read_lock();
  713. xdp_prog = rcu_dereference(rq->xdp_prog);
  714. if (xdp_prog) {
  715. struct xdp_frame *xdpf;
  716. struct page *xdp_page;
  717. struct xdp_buff xdp;
  718. void *data;
  719. u32 act;
  720. /* Transient failure which in theory could occur if
  721. * in-flight packets from before XDP was enabled reach
  722. * the receive path after XDP is loaded.
  723. */
  724. if (unlikely(hdr->hdr.gso_type))
  725. goto err_xdp;
  726. /* This happens when rx buffer size is underestimated
  727. * or headroom is not enough because of the buffer
  728. * was refilled before XDP is set. This should only
  729. * happen for the first several packets, so we don't
  730. * care much about its performance.
  731. */
  732. if (unlikely(num_buf > 1 ||
  733. headroom < virtnet_get_headroom(vi))) {
  734. /* linearize data for XDP */
  735. xdp_page = xdp_linearize_page(rq, &num_buf,
  736. page, offset,
  737. VIRTIO_XDP_HEADROOM,
  738. &len);
  739. if (!xdp_page)
  740. goto err_xdp;
  741. offset = VIRTIO_XDP_HEADROOM;
  742. } else {
  743. xdp_page = page;
  744. }
  745. /* Allow consuming headroom but reserve enough space to push
  746. * the descriptor on if we get an XDP_TX return code.
  747. */
  748. data = page_address(xdp_page) + offset;
  749. xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
  750. xdp.data = data + vi->hdr_len;
  751. xdp.data_end = xdp.data + (len - vi->hdr_len);
  752. xdp.data_meta = xdp.data;
  753. xdp.rxq = &rq->xdp_rxq;
  754. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  755. stats->xdp_packets++;
  756. switch (act) {
  757. case XDP_PASS:
  758. metasize = xdp.data - xdp.data_meta;
  759. /* recalculate offset to account for any header
  760. * adjustments and minus the metasize to copy the
  761. * metadata in page_to_skb(). Note other cases do not
  762. * build an skb and avoid using offset
  763. */
  764. offset = xdp.data - page_address(xdp_page) -
  765. vi->hdr_len - metasize;
  766. /* recalculate len if xdp.data, xdp.data_end or
  767. * xdp.data_meta were adjusted
  768. */
  769. len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
  770. /* We can only create skb based on xdp_page. */
  771. if (unlikely(xdp_page != page)) {
  772. rcu_read_unlock();
  773. put_page(page);
  774. head_skb = page_to_skb(vi, rq, xdp_page, offset,
  775. len, PAGE_SIZE, false,
  776. metasize);
  777. return head_skb;
  778. }
  779. break;
  780. case XDP_TX:
  781. stats->xdp_tx++;
  782. xdpf = convert_to_xdp_frame(&xdp);
  783. if (unlikely(!xdpf))
  784. goto err_xdp;
  785. err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
  786. if (unlikely(err < 0)) {
  787. trace_xdp_exception(vi->dev, xdp_prog, act);
  788. if (unlikely(xdp_page != page))
  789. put_page(xdp_page);
  790. goto err_xdp;
  791. }
  792. *xdp_xmit |= VIRTIO_XDP_TX;
  793. if (unlikely(xdp_page != page))
  794. put_page(page);
  795. rcu_read_unlock();
  796. goto xdp_xmit;
  797. case XDP_REDIRECT:
  798. stats->xdp_redirects++;
  799. err = xdp_do_redirect(dev, &xdp, xdp_prog);
  800. if (err) {
  801. if (unlikely(xdp_page != page))
  802. put_page(xdp_page);
  803. goto err_xdp;
  804. }
  805. *xdp_xmit |= VIRTIO_XDP_REDIR;
  806. if (unlikely(xdp_page != page))
  807. put_page(page);
  808. rcu_read_unlock();
  809. goto xdp_xmit;
  810. default:
  811. bpf_warn_invalid_xdp_action(act);
  812. /* fall through */
  813. case XDP_ABORTED:
  814. trace_xdp_exception(vi->dev, xdp_prog, act);
  815. /* fall through */
  816. case XDP_DROP:
  817. if (unlikely(xdp_page != page))
  818. __free_pages(xdp_page, 0);
  819. goto err_xdp;
  820. }
  821. }
  822. rcu_read_unlock();
  823. truesize = mergeable_ctx_to_truesize(ctx);
  824. if (unlikely(len > truesize)) {
  825. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  826. dev->name, len, (unsigned long)ctx);
  827. dev->stats.rx_length_errors++;
  828. goto err_skb;
  829. }
  830. head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
  831. metasize);
  832. curr_skb = head_skb;
  833. if (unlikely(!curr_skb))
  834. goto err_skb;
  835. while (--num_buf) {
  836. int num_skb_frags;
  837. buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
  838. if (unlikely(!buf)) {
  839. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  840. dev->name, num_buf,
  841. virtio16_to_cpu(vi->vdev,
  842. hdr->num_buffers));
  843. dev->stats.rx_length_errors++;
  844. goto err_buf;
  845. }
  846. stats->bytes += len;
  847. page = virt_to_head_page(buf);
  848. truesize = mergeable_ctx_to_truesize(ctx);
  849. if (unlikely(len > truesize)) {
  850. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  851. dev->name, len, (unsigned long)ctx);
  852. dev->stats.rx_length_errors++;
  853. goto err_skb;
  854. }
  855. num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
  856. if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
  857. struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
  858. if (unlikely(!nskb))
  859. goto err_skb;
  860. if (curr_skb == head_skb)
  861. skb_shinfo(curr_skb)->frag_list = nskb;
  862. else
  863. curr_skb->next = nskb;
  864. curr_skb = nskb;
  865. head_skb->truesize += nskb->truesize;
  866. num_skb_frags = 0;
  867. }
  868. if (curr_skb != head_skb) {
  869. head_skb->data_len += len;
  870. head_skb->len += len;
  871. head_skb->truesize += truesize;
  872. }
  873. offset = buf - page_address(page);
  874. if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
  875. put_page(page);
  876. skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  877. len, truesize);
  878. } else {
  879. skb_add_rx_frag(curr_skb, num_skb_frags, page,
  880. offset, len, truesize);
  881. }
  882. }
  883. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
  884. return head_skb;
  885. err_xdp:
  886. rcu_read_unlock();
  887. stats->xdp_drops++;
  888. err_skb:
  889. put_page(page);
  890. while (num_buf-- > 1) {
  891. buf = virtqueue_get_buf(rq->vq, &len);
  892. if (unlikely(!buf)) {
  893. pr_debug("%s: rx error: %d buffers missing\n",
  894. dev->name, num_buf);
  895. dev->stats.rx_length_errors++;
  896. break;
  897. }
  898. stats->bytes += len;
  899. page = virt_to_head_page(buf);
  900. put_page(page);
  901. }
  902. err_buf:
  903. stats->drops++;
  904. dev_kfree_skb(head_skb);
  905. xdp_xmit:
  906. return NULL;
  907. }
  908. static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  909. void *buf, unsigned int len, void **ctx,
  910. unsigned int *xdp_xmit,
  911. struct virtnet_rq_stats *stats)
  912. {
  913. struct net_device *dev = vi->dev;
  914. struct sk_buff *skb;
  915. struct virtio_net_hdr_mrg_rxbuf *hdr;
  916. if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
  917. pr_debug("%s: short packet %i\n", dev->name, len);
  918. dev->stats.rx_length_errors++;
  919. if (vi->mergeable_rx_bufs) {
  920. put_page(virt_to_head_page(buf));
  921. } else if (vi->big_packets) {
  922. give_pages(rq, buf);
  923. } else {
  924. put_page(virt_to_head_page(buf));
  925. }
  926. return;
  927. }
  928. if (vi->mergeable_rx_bufs)
  929. skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
  930. stats);
  931. else if (vi->big_packets)
  932. skb = receive_big(dev, vi, rq, buf, len, stats);
  933. else
  934. skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
  935. if (unlikely(!skb))
  936. return;
  937. hdr = skb_vnet_hdr(skb);
  938. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
  939. skb->ip_summed = CHECKSUM_UNNECESSARY;
  940. if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
  941. virtio_is_little_endian(vi->vdev))) {
  942. net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
  943. dev->name, hdr->hdr.gso_type,
  944. hdr->hdr.gso_size);
  945. goto frame_err;
  946. }
  947. skb_record_rx_queue(skb, vq2rxq(rq->vq));
  948. skb->protocol = eth_type_trans(skb, dev);
  949. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  950. ntohs(skb->protocol), skb->len, skb->pkt_type);
  951. napi_gro_receive(&rq->napi, skb);
  952. return;
  953. frame_err:
  954. dev->stats.rx_frame_errors++;
  955. dev_kfree_skb(skb);
  956. }
  957. /* Unlike mergeable buffers, all buffers are allocated to the
  958. * same size, except for the headroom. For this reason we do
  959. * not need to use mergeable_len_to_ctx here - it is enough
  960. * to store the headroom as the context ignoring the truesize.
  961. */
  962. static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
  963. gfp_t gfp)
  964. {
  965. struct page_frag *alloc_frag = &rq->alloc_frag;
  966. char *buf;
  967. unsigned int xdp_headroom = virtnet_get_headroom(vi);
  968. void *ctx = (void *)(unsigned long)xdp_headroom;
  969. int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
  970. int err;
  971. len = SKB_DATA_ALIGN(len) +
  972. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  973. if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
  974. return -ENOMEM;
  975. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  976. get_page(alloc_frag->page);
  977. alloc_frag->offset += len;
  978. sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
  979. vi->hdr_len + GOOD_PACKET_LEN);
  980. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  981. if (err < 0)
  982. put_page(virt_to_head_page(buf));
  983. return err;
  984. }
  985. static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  986. gfp_t gfp)
  987. {
  988. struct page *first, *list = NULL;
  989. char *p;
  990. int i, err, offset;
  991. sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
  992. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  993. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  994. first = get_a_page(rq, gfp);
  995. if (!first) {
  996. if (list)
  997. give_pages(rq, list);
  998. return -ENOMEM;
  999. }
  1000. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  1001. /* chain new page in list head to match sg */
  1002. first->private = (unsigned long)list;
  1003. list = first;
  1004. }
  1005. first = get_a_page(rq, gfp);
  1006. if (!first) {
  1007. give_pages(rq, list);
  1008. return -ENOMEM;
  1009. }
  1010. p = page_address(first);
  1011. /* rq->sg[0], rq->sg[1] share the same page */
  1012. /* a separated rq->sg[0] for header - required in case !any_header_sg */
  1013. sg_set_buf(&rq->sg[0], p, vi->hdr_len);
  1014. /* rq->sg[1] for data packet, from offset */
  1015. offset = sizeof(struct padded_vnet_hdr);
  1016. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  1017. /* chain first in list head */
  1018. first->private = (unsigned long)list;
  1019. err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
  1020. first, gfp);
  1021. if (err < 0)
  1022. give_pages(rq, first);
  1023. return err;
  1024. }
  1025. static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
  1026. struct ewma_pkt_len *avg_pkt_len,
  1027. unsigned int room)
  1028. {
  1029. const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  1030. unsigned int len;
  1031. if (room)
  1032. return PAGE_SIZE - room;
  1033. len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
  1034. rq->min_buf_len, PAGE_SIZE - hdr_len);
  1035. return ALIGN(len, L1_CACHE_BYTES);
  1036. }
  1037. static int add_recvbuf_mergeable(struct virtnet_info *vi,
  1038. struct receive_queue *rq, gfp_t gfp)
  1039. {
  1040. struct page_frag *alloc_frag = &rq->alloc_frag;
  1041. unsigned int headroom = virtnet_get_headroom(vi);
  1042. unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  1043. unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
  1044. char *buf;
  1045. void *ctx;
  1046. int err;
  1047. unsigned int len, hole;
  1048. /* Extra tailroom is needed to satisfy XDP's assumption. This
  1049. * means rx frags coalescing won't work, but consider we've
  1050. * disabled GSO for XDP, it won't be a big issue.
  1051. */
  1052. len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
  1053. if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
  1054. return -ENOMEM;
  1055. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  1056. buf += headroom; /* advance address leaving hole at front of pkt */
  1057. get_page(alloc_frag->page);
  1058. alloc_frag->offset += len + room;
  1059. hole = alloc_frag->size - alloc_frag->offset;
  1060. if (hole < len + room) {
  1061. /* To avoid internal fragmentation, if there is very likely not
  1062. * enough space for another buffer, add the remaining space to
  1063. * the current buffer.
  1064. */
  1065. len += hole;
  1066. alloc_frag->offset += hole;
  1067. }
  1068. sg_init_one(rq->sg, buf, len);
  1069. ctx = mergeable_len_to_ctx(len, headroom);
  1070. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  1071. if (err < 0)
  1072. put_page(virt_to_head_page(buf));
  1073. return err;
  1074. }
  1075. /*
  1076. * Returns false if we couldn't fill entirely (OOM).
  1077. *
  1078. * Normally run in the receive path, but can also be run from ndo_open
  1079. * before we're receiving packets, or from refill_work which is
  1080. * careful to disable receiving (using napi_disable).
  1081. */
  1082. static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
  1083. gfp_t gfp)
  1084. {
  1085. int err;
  1086. bool oom;
  1087. do {
  1088. if (vi->mergeable_rx_bufs)
  1089. err = add_recvbuf_mergeable(vi, rq, gfp);
  1090. else if (vi->big_packets)
  1091. err = add_recvbuf_big(vi, rq, gfp);
  1092. else
  1093. err = add_recvbuf_small(vi, rq, gfp);
  1094. oom = err == -ENOMEM;
  1095. if (err)
  1096. break;
  1097. } while (rq->vq->num_free);
  1098. if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
  1099. unsigned long flags;
  1100. flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
  1101. rq->stats.kicks++;
  1102. u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
  1103. }
  1104. return !oom;
  1105. }
  1106. static void skb_recv_done(struct virtqueue *rvq)
  1107. {
  1108. struct virtnet_info *vi = rvq->vdev->priv;
  1109. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  1110. virtqueue_napi_schedule(&rq->napi, rvq);
  1111. }
  1112. static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
  1113. {
  1114. napi_enable(napi);
  1115. /* If all buffers were filled by other side before we napi_enabled, we
  1116. * won't get another interrupt, so process any outstanding packets now.
  1117. * Call local_bh_enable after to trigger softIRQ processing.
  1118. */
  1119. local_bh_disable();
  1120. virtqueue_napi_schedule(napi, vq);
  1121. local_bh_enable();
  1122. }
  1123. static void virtnet_napi_tx_enable(struct virtnet_info *vi,
  1124. struct virtqueue *vq,
  1125. struct napi_struct *napi)
  1126. {
  1127. if (!napi->weight)
  1128. return;
  1129. /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
  1130. * enable the feature if this is likely affine with the transmit path.
  1131. */
  1132. if (!vi->affinity_hint_set) {
  1133. napi->weight = 0;
  1134. return;
  1135. }
  1136. return virtnet_napi_enable(vq, napi);
  1137. }
  1138. static void virtnet_napi_tx_disable(struct napi_struct *napi)
  1139. {
  1140. if (napi->weight)
  1141. napi_disable(napi);
  1142. }
  1143. static void refill_work(struct work_struct *work)
  1144. {
  1145. struct virtnet_info *vi =
  1146. container_of(work, struct virtnet_info, refill.work);
  1147. bool still_empty;
  1148. int i;
  1149. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1150. struct receive_queue *rq = &vi->rq[i];
  1151. napi_disable(&rq->napi);
  1152. still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
  1153. virtnet_napi_enable(rq->vq, &rq->napi);
  1154. /* In theory, this can happen: if we don't get any buffers in
  1155. * we will *never* try to fill again.
  1156. */
  1157. if (still_empty)
  1158. schedule_delayed_work(&vi->refill, HZ/2);
  1159. }
  1160. }
  1161. static int virtnet_receive(struct receive_queue *rq, int budget,
  1162. unsigned int *xdp_xmit)
  1163. {
  1164. struct virtnet_info *vi = rq->vq->vdev->priv;
  1165. struct virtnet_rq_stats stats = {};
  1166. unsigned int len;
  1167. void *buf;
  1168. int i;
  1169. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  1170. void *ctx;
  1171. while (stats.packets < budget &&
  1172. (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
  1173. receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
  1174. stats.packets++;
  1175. }
  1176. } else {
  1177. while (stats.packets < budget &&
  1178. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  1179. receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
  1180. stats.packets++;
  1181. }
  1182. }
  1183. if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
  1184. if (!try_fill_recv(vi, rq, GFP_ATOMIC))
  1185. schedule_delayed_work(&vi->refill, 0);
  1186. }
  1187. u64_stats_update_begin(&rq->stats.syncp);
  1188. for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
  1189. size_t offset = virtnet_rq_stats_desc[i].offset;
  1190. u64 *item;
  1191. item = (u64 *)((u8 *)&rq->stats + offset);
  1192. *item += *(u64 *)((u8 *)&stats + offset);
  1193. }
  1194. u64_stats_update_end(&rq->stats.syncp);
  1195. return stats.packets;
  1196. }
  1197. static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
  1198. {
  1199. unsigned int len;
  1200. unsigned int packets = 0;
  1201. unsigned int bytes = 0;
  1202. void *ptr;
  1203. while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  1204. if (likely(!is_xdp_frame(ptr))) {
  1205. struct sk_buff *skb = ptr;
  1206. pr_debug("Sent skb %p\n", skb);
  1207. bytes += skb->len;
  1208. napi_consume_skb(skb, in_napi);
  1209. } else {
  1210. struct xdp_frame *frame = ptr_to_xdp(ptr);
  1211. bytes += frame->len;
  1212. xdp_return_frame(frame);
  1213. }
  1214. packets++;
  1215. }
  1216. /* Avoid overhead when no packets have been processed
  1217. * happens when called speculatively from start_xmit.
  1218. */
  1219. if (!packets)
  1220. return;
  1221. u64_stats_update_begin(&sq->stats.syncp);
  1222. sq->stats.bytes += bytes;
  1223. sq->stats.packets += packets;
  1224. u64_stats_update_end(&sq->stats.syncp);
  1225. }
  1226. static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
  1227. {
  1228. if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
  1229. return false;
  1230. else if (q < vi->curr_queue_pairs)
  1231. return true;
  1232. else
  1233. return false;
  1234. }
  1235. static void virtnet_poll_cleantx(struct receive_queue *rq)
  1236. {
  1237. struct virtnet_info *vi = rq->vq->vdev->priv;
  1238. unsigned int index = vq2rxq(rq->vq);
  1239. struct send_queue *sq = &vi->sq[index];
  1240. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
  1241. if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
  1242. return;
  1243. if (__netif_tx_trylock(txq)) {
  1244. free_old_xmit_skbs(sq, true);
  1245. __netif_tx_unlock(txq);
  1246. }
  1247. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1248. netif_tx_wake_queue(txq);
  1249. }
  1250. static int virtnet_poll(struct napi_struct *napi, int budget)
  1251. {
  1252. struct receive_queue *rq =
  1253. container_of(napi, struct receive_queue, napi);
  1254. struct virtnet_info *vi = rq->vq->vdev->priv;
  1255. struct send_queue *sq;
  1256. unsigned int received;
  1257. unsigned int xdp_xmit = 0;
  1258. virtnet_poll_cleantx(rq);
  1259. received = virtnet_receive(rq, budget, &xdp_xmit);
  1260. /* Out of packets? */
  1261. if (received < budget)
  1262. virtqueue_napi_complete(napi, rq->vq, received);
  1263. if (xdp_xmit & VIRTIO_XDP_REDIR)
  1264. xdp_do_flush_map();
  1265. if (xdp_xmit & VIRTIO_XDP_TX) {
  1266. sq = virtnet_xdp_get_sq(vi);
  1267. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
  1268. u64_stats_update_begin(&sq->stats.syncp);
  1269. sq->stats.kicks++;
  1270. u64_stats_update_end(&sq->stats.syncp);
  1271. }
  1272. virtnet_xdp_put_sq(vi, sq);
  1273. }
  1274. return received;
  1275. }
  1276. static int virtnet_open(struct net_device *dev)
  1277. {
  1278. struct virtnet_info *vi = netdev_priv(dev);
  1279. int i, err;
  1280. for (i = 0; i < vi->max_queue_pairs; i++) {
  1281. if (i < vi->curr_queue_pairs)
  1282. /* Make sure we have some buffers: if oom use wq. */
  1283. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  1284. schedule_delayed_work(&vi->refill, 0);
  1285. err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i);
  1286. if (err < 0)
  1287. return err;
  1288. err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
  1289. MEM_TYPE_PAGE_SHARED, NULL);
  1290. if (err < 0) {
  1291. xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
  1292. return err;
  1293. }
  1294. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1295. virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
  1296. }
  1297. return 0;
  1298. }
  1299. static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  1300. {
  1301. struct send_queue *sq = container_of(napi, struct send_queue, napi);
  1302. struct virtnet_info *vi = sq->vq->vdev->priv;
  1303. unsigned int index = vq2txq(sq->vq);
  1304. struct netdev_queue *txq;
  1305. int opaque;
  1306. bool done;
  1307. if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
  1308. /* We don't need to enable cb for XDP */
  1309. napi_complete_done(napi, 0);
  1310. return 0;
  1311. }
  1312. txq = netdev_get_tx_queue(vi->dev, index);
  1313. __netif_tx_lock(txq, raw_smp_processor_id());
  1314. virtqueue_disable_cb(sq->vq);
  1315. free_old_xmit_skbs(sq, true);
  1316. opaque = virtqueue_enable_cb_prepare(sq->vq);
  1317. done = napi_complete_done(napi, 0);
  1318. if (!done)
  1319. virtqueue_disable_cb(sq->vq);
  1320. __netif_tx_unlock(txq);
  1321. if (done) {
  1322. if (unlikely(virtqueue_poll(sq->vq, opaque))) {
  1323. if (napi_schedule_prep(napi)) {
  1324. __netif_tx_lock(txq, raw_smp_processor_id());
  1325. virtqueue_disable_cb(sq->vq);
  1326. __netif_tx_unlock(txq);
  1327. __napi_schedule(napi);
  1328. }
  1329. }
  1330. }
  1331. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1332. netif_tx_wake_queue(txq);
  1333. return 0;
  1334. }
  1335. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  1336. {
  1337. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1338. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  1339. struct virtnet_info *vi = sq->vq->vdev->priv;
  1340. int num_sg;
  1341. unsigned hdr_len = vi->hdr_len;
  1342. bool can_push;
  1343. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  1344. can_push = vi->any_header_sg &&
  1345. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  1346. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  1347. /* Even if we can, don't push here yet as this would skew
  1348. * csum_start offset below. */
  1349. if (can_push)
  1350. hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
  1351. else
  1352. hdr = skb_vnet_hdr(skb);
  1353. if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
  1354. virtio_is_little_endian(vi->vdev), false,
  1355. 0))
  1356. return -EPROTO;
  1357. if (vi->mergeable_rx_bufs)
  1358. hdr->num_buffers = 0;
  1359. sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
  1360. if (can_push) {
  1361. __skb_push(skb, hdr_len);
  1362. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  1363. if (unlikely(num_sg < 0))
  1364. return num_sg;
  1365. /* Pull header back to avoid skew in tx bytes calculations. */
  1366. __skb_pull(skb, hdr_len);
  1367. } else {
  1368. sg_set_buf(sq->sg, hdr, hdr_len);
  1369. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
  1370. if (unlikely(num_sg < 0))
  1371. return num_sg;
  1372. num_sg++;
  1373. }
  1374. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  1375. }
  1376. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  1377. {
  1378. struct virtnet_info *vi = netdev_priv(dev);
  1379. int qnum = skb_get_queue_mapping(skb);
  1380. struct send_queue *sq = &vi->sq[qnum];
  1381. int err;
  1382. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  1383. bool kick = !netdev_xmit_more();
  1384. bool use_napi = sq->napi.weight;
  1385. /* Free up any pending old buffers before queueing new ones. */
  1386. free_old_xmit_skbs(sq, false);
  1387. if (use_napi && kick)
  1388. virtqueue_enable_cb_delayed(sq->vq);
  1389. /* timestamp packet in software */
  1390. skb_tx_timestamp(skb);
  1391. /* Try to transmit */
  1392. err = xmit_skb(sq, skb);
  1393. /* This should not happen! */
  1394. if (unlikely(err)) {
  1395. dev->stats.tx_fifo_errors++;
  1396. if (net_ratelimit())
  1397. dev_warn(&dev->dev,
  1398. "Unexpected TXQ (%d) queue failure: %d\n",
  1399. qnum, err);
  1400. dev->stats.tx_dropped++;
  1401. dev_kfree_skb_any(skb);
  1402. return NETDEV_TX_OK;
  1403. }
  1404. /* Don't wait up for transmitted skbs to be freed. */
  1405. if (!use_napi) {
  1406. skb_orphan(skb);
  1407. nf_reset_ct(skb);
  1408. }
  1409. /* If running out of space, stop queue to avoid getting packets that we
  1410. * are then unable to transmit.
  1411. * An alternative would be to force queuing layer to requeue the skb by
  1412. * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
  1413. * returned in a normal path of operation: it means that driver is not
  1414. * maintaining the TX queue stop/start state properly, and causes
  1415. * the stack to do a non-trivial amount of useless work.
  1416. * Since most packets only take 1 or 2 ring slots, stopping the queue
  1417. * early means 16 slots are typically wasted.
  1418. */
  1419. if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
  1420. netif_stop_subqueue(dev, qnum);
  1421. if (!use_napi &&
  1422. unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  1423. /* More just got used, free them then recheck. */
  1424. free_old_xmit_skbs(sq, false);
  1425. if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
  1426. netif_start_subqueue(dev, qnum);
  1427. virtqueue_disable_cb(sq->vq);
  1428. }
  1429. }
  1430. }
  1431. if (kick || netif_xmit_stopped(txq)) {
  1432. if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
  1433. u64_stats_update_begin(&sq->stats.syncp);
  1434. sq->stats.kicks++;
  1435. u64_stats_update_end(&sq->stats.syncp);
  1436. }
  1437. }
  1438. return NETDEV_TX_OK;
  1439. }
  1440. /*
  1441. * Send command via the control virtqueue and check status. Commands
  1442. * supported by the hypervisor, as indicated by feature bits, should
  1443. * never fail unless improperly formatted.
  1444. */
  1445. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  1446. struct scatterlist *out)
  1447. {
  1448. struct scatterlist *sgs[4], hdr, stat;
  1449. unsigned out_num = 0, tmp;
  1450. /* Caller should know better */
  1451. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  1452. vi->ctrl->status = ~0;
  1453. vi->ctrl->hdr.class = class;
  1454. vi->ctrl->hdr.cmd = cmd;
  1455. /* Add header */
  1456. sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
  1457. sgs[out_num++] = &hdr;
  1458. if (out)
  1459. sgs[out_num++] = out;
  1460. /* Add return status. */
  1461. sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
  1462. sgs[out_num] = &stat;
  1463. BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
  1464. virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
  1465. if (unlikely(!virtqueue_kick(vi->cvq)))
  1466. return vi->ctrl->status == VIRTIO_NET_OK;
  1467. /* Spin for a response, the kick causes an ioport write, trapping
  1468. * into the hypervisor, so the request should be handled immediately.
  1469. */
  1470. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  1471. !virtqueue_is_broken(vi->cvq))
  1472. cpu_relax();
  1473. return vi->ctrl->status == VIRTIO_NET_OK;
  1474. }
  1475. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  1476. {
  1477. struct virtnet_info *vi = netdev_priv(dev);
  1478. struct virtio_device *vdev = vi->vdev;
  1479. int ret;
  1480. struct sockaddr *addr;
  1481. struct scatterlist sg;
  1482. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
  1483. return -EOPNOTSUPP;
  1484. addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
  1485. if (!addr)
  1486. return -ENOMEM;
  1487. ret = eth_prepare_mac_addr_change(dev, addr);
  1488. if (ret)
  1489. goto out;
  1490. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  1491. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  1492. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1493. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  1494. dev_warn(&vdev->dev,
  1495. "Failed to set mac address by vq command.\n");
  1496. ret = -EINVAL;
  1497. goto out;
  1498. }
  1499. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  1500. !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1501. unsigned int i;
  1502. /* Naturally, this has an atomicity problem. */
  1503. for (i = 0; i < dev->addr_len; i++)
  1504. virtio_cwrite8(vdev,
  1505. offsetof(struct virtio_net_config, mac) +
  1506. i, addr->sa_data[i]);
  1507. }
  1508. eth_commit_mac_addr_change(dev, p);
  1509. ret = 0;
  1510. out:
  1511. kfree(addr);
  1512. return ret;
  1513. }
  1514. static void virtnet_stats(struct net_device *dev,
  1515. struct rtnl_link_stats64 *tot)
  1516. {
  1517. struct virtnet_info *vi = netdev_priv(dev);
  1518. unsigned int start;
  1519. int i;
  1520. for (i = 0; i < vi->max_queue_pairs; i++) {
  1521. u64 tpackets, tbytes, rpackets, rbytes, rdrops;
  1522. struct receive_queue *rq = &vi->rq[i];
  1523. struct send_queue *sq = &vi->sq[i];
  1524. do {
  1525. start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
  1526. tpackets = sq->stats.packets;
  1527. tbytes = sq->stats.bytes;
  1528. } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
  1529. do {
  1530. start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
  1531. rpackets = rq->stats.packets;
  1532. rbytes = rq->stats.bytes;
  1533. rdrops = rq->stats.drops;
  1534. } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
  1535. tot->rx_packets += rpackets;
  1536. tot->tx_packets += tpackets;
  1537. tot->rx_bytes += rbytes;
  1538. tot->tx_bytes += tbytes;
  1539. tot->rx_dropped += rdrops;
  1540. }
  1541. tot->tx_dropped = dev->stats.tx_dropped;
  1542. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  1543. tot->rx_length_errors = dev->stats.rx_length_errors;
  1544. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  1545. }
  1546. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  1547. {
  1548. rtnl_lock();
  1549. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  1550. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  1551. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  1552. rtnl_unlock();
  1553. }
  1554. static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1555. {
  1556. struct scatterlist sg;
  1557. struct net_device *dev = vi->dev;
  1558. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  1559. return 0;
  1560. vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
  1561. sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
  1562. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  1563. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  1564. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  1565. queue_pairs);
  1566. return -EINVAL;
  1567. } else {
  1568. vi->curr_queue_pairs = queue_pairs;
  1569. /* virtnet_open() will refill when device is going to up. */
  1570. if (dev->flags & IFF_UP)
  1571. schedule_delayed_work(&vi->refill, 0);
  1572. }
  1573. return 0;
  1574. }
  1575. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1576. {
  1577. int err;
  1578. rtnl_lock();
  1579. err = _virtnet_set_queues(vi, queue_pairs);
  1580. rtnl_unlock();
  1581. return err;
  1582. }
  1583. static int virtnet_close(struct net_device *dev)
  1584. {
  1585. struct virtnet_info *vi = netdev_priv(dev);
  1586. int i;
  1587. /* Make sure refill_work doesn't re-enable napi! */
  1588. cancel_delayed_work_sync(&vi->refill);
  1589. for (i = 0; i < vi->max_queue_pairs; i++) {
  1590. xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
  1591. napi_disable(&vi->rq[i].napi);
  1592. virtnet_napi_tx_disable(&vi->sq[i].napi);
  1593. }
  1594. return 0;
  1595. }
  1596. static void virtnet_set_rx_mode(struct net_device *dev)
  1597. {
  1598. struct virtnet_info *vi = netdev_priv(dev);
  1599. struct scatterlist sg[2];
  1600. struct virtio_net_ctrl_mac *mac_data;
  1601. struct netdev_hw_addr *ha;
  1602. int uc_count;
  1603. int mc_count;
  1604. void *buf;
  1605. int i;
  1606. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  1607. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  1608. return;
  1609. vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
  1610. vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  1611. sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
  1612. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1613. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  1614. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  1615. vi->ctrl->promisc ? "en" : "dis");
  1616. sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
  1617. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1618. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  1619. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  1620. vi->ctrl->allmulti ? "en" : "dis");
  1621. uc_count = netdev_uc_count(dev);
  1622. mc_count = netdev_mc_count(dev);
  1623. /* MAC filter - use one buffer for both lists */
  1624. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  1625. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  1626. mac_data = buf;
  1627. if (!buf)
  1628. return;
  1629. sg_init_table(sg, 2);
  1630. /* Store the unicast list and count in the front of the buffer */
  1631. mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
  1632. i = 0;
  1633. netdev_for_each_uc_addr(ha, dev)
  1634. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1635. sg_set_buf(&sg[0], mac_data,
  1636. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  1637. /* multicast list and count fill the end */
  1638. mac_data = (void *)&mac_data->macs[uc_count][0];
  1639. mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
  1640. i = 0;
  1641. netdev_for_each_mc_addr(ha, dev)
  1642. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1643. sg_set_buf(&sg[1], mac_data,
  1644. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  1645. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1646. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  1647. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  1648. kfree(buf);
  1649. }
  1650. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  1651. __be16 proto, u16 vid)
  1652. {
  1653. struct virtnet_info *vi = netdev_priv(dev);
  1654. struct scatterlist sg;
  1655. vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
  1656. sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
  1657. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1658. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  1659. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  1660. return 0;
  1661. }
  1662. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  1663. __be16 proto, u16 vid)
  1664. {
  1665. struct virtnet_info *vi = netdev_priv(dev);
  1666. struct scatterlist sg;
  1667. vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
  1668. sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
  1669. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1670. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  1671. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  1672. return 0;
  1673. }
  1674. static void virtnet_clean_affinity(struct virtnet_info *vi)
  1675. {
  1676. int i;
  1677. if (vi->affinity_hint_set) {
  1678. for (i = 0; i < vi->max_queue_pairs; i++) {
  1679. virtqueue_set_affinity(vi->rq[i].vq, NULL);
  1680. virtqueue_set_affinity(vi->sq[i].vq, NULL);
  1681. }
  1682. vi->affinity_hint_set = false;
  1683. }
  1684. }
  1685. static void virtnet_set_affinity(struct virtnet_info *vi)
  1686. {
  1687. cpumask_var_t mask;
  1688. int stragglers;
  1689. int group_size;
  1690. int i, j, cpu;
  1691. int num_cpu;
  1692. int stride;
  1693. if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
  1694. virtnet_clean_affinity(vi);
  1695. return;
  1696. }
  1697. num_cpu = num_online_cpus();
  1698. stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
  1699. stragglers = num_cpu >= vi->curr_queue_pairs ?
  1700. num_cpu % vi->curr_queue_pairs :
  1701. 0;
  1702. cpu = cpumask_next(-1, cpu_online_mask);
  1703. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1704. group_size = stride + (i < stragglers ? 1 : 0);
  1705. for (j = 0; j < group_size; j++) {
  1706. cpumask_set_cpu(cpu, mask);
  1707. cpu = cpumask_next_wrap(cpu, cpu_online_mask,
  1708. nr_cpu_ids, false);
  1709. }
  1710. virtqueue_set_affinity(vi->rq[i].vq, mask);
  1711. virtqueue_set_affinity(vi->sq[i].vq, mask);
  1712. __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, false);
  1713. cpumask_clear(mask);
  1714. }
  1715. vi->affinity_hint_set = true;
  1716. free_cpumask_var(mask);
  1717. }
  1718. static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
  1719. {
  1720. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1721. node);
  1722. virtnet_set_affinity(vi);
  1723. return 0;
  1724. }
  1725. static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
  1726. {
  1727. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1728. node_dead);
  1729. virtnet_set_affinity(vi);
  1730. return 0;
  1731. }
  1732. static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
  1733. {
  1734. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1735. node);
  1736. virtnet_clean_affinity(vi);
  1737. return 0;
  1738. }
  1739. static enum cpuhp_state virtionet_online;
  1740. static int virtnet_cpu_notif_add(struct virtnet_info *vi)
  1741. {
  1742. int ret;
  1743. ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
  1744. if (ret)
  1745. return ret;
  1746. ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1747. &vi->node_dead);
  1748. if (!ret)
  1749. return ret;
  1750. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1751. return ret;
  1752. }
  1753. static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
  1754. {
  1755. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1756. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1757. &vi->node_dead);
  1758. }
  1759. static void virtnet_get_ringparam(struct net_device *dev,
  1760. struct ethtool_ringparam *ring)
  1761. {
  1762. struct virtnet_info *vi = netdev_priv(dev);
  1763. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  1764. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  1765. ring->rx_pending = ring->rx_max_pending;
  1766. ring->tx_pending = ring->tx_max_pending;
  1767. }
  1768. static void virtnet_get_drvinfo(struct net_device *dev,
  1769. struct ethtool_drvinfo *info)
  1770. {
  1771. struct virtnet_info *vi = netdev_priv(dev);
  1772. struct virtio_device *vdev = vi->vdev;
  1773. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1774. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  1775. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  1776. }
  1777. /* TODO: Eliminate OOO packets during switching */
  1778. static int virtnet_set_channels(struct net_device *dev,
  1779. struct ethtool_channels *channels)
  1780. {
  1781. struct virtnet_info *vi = netdev_priv(dev);
  1782. u16 queue_pairs = channels->combined_count;
  1783. int err;
  1784. /* We don't support separate rx/tx channels.
  1785. * We don't allow setting 'other' channels.
  1786. */
  1787. if (channels->rx_count || channels->tx_count || channels->other_count)
  1788. return -EINVAL;
  1789. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  1790. return -EINVAL;
  1791. /* For now we don't support modifying channels while XDP is loaded
  1792. * also when XDP is loaded all RX queues have XDP programs so we only
  1793. * need to check a single RX queue.
  1794. */
  1795. if (vi->rq[0].xdp_prog)
  1796. return -EINVAL;
  1797. get_online_cpus();
  1798. err = _virtnet_set_queues(vi, queue_pairs);
  1799. if (err) {
  1800. put_online_cpus();
  1801. goto err;
  1802. }
  1803. virtnet_set_affinity(vi);
  1804. put_online_cpus();
  1805. netif_set_real_num_tx_queues(dev, queue_pairs);
  1806. netif_set_real_num_rx_queues(dev, queue_pairs);
  1807. err:
  1808. return err;
  1809. }
  1810. static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1811. {
  1812. struct virtnet_info *vi = netdev_priv(dev);
  1813. char *p = (char *)data;
  1814. unsigned int i, j;
  1815. switch (stringset) {
  1816. case ETH_SS_STATS:
  1817. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1818. for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
  1819. snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_%s",
  1820. i, virtnet_rq_stats_desc[j].desc);
  1821. p += ETH_GSTRING_LEN;
  1822. }
  1823. }
  1824. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1825. for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
  1826. snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_%s",
  1827. i, virtnet_sq_stats_desc[j].desc);
  1828. p += ETH_GSTRING_LEN;
  1829. }
  1830. }
  1831. break;
  1832. }
  1833. }
  1834. static int virtnet_get_sset_count(struct net_device *dev, int sset)
  1835. {
  1836. struct virtnet_info *vi = netdev_priv(dev);
  1837. switch (sset) {
  1838. case ETH_SS_STATS:
  1839. return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
  1840. VIRTNET_SQ_STATS_LEN);
  1841. default:
  1842. return -EOPNOTSUPP;
  1843. }
  1844. }
  1845. static void virtnet_get_ethtool_stats(struct net_device *dev,
  1846. struct ethtool_stats *stats, u64 *data)
  1847. {
  1848. struct virtnet_info *vi = netdev_priv(dev);
  1849. unsigned int idx = 0, start, i, j;
  1850. const u8 *stats_base;
  1851. size_t offset;
  1852. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1853. struct receive_queue *rq = &vi->rq[i];
  1854. stats_base = (u8 *)&rq->stats;
  1855. do {
  1856. start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
  1857. for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
  1858. offset = virtnet_rq_stats_desc[j].offset;
  1859. data[idx + j] = *(u64 *)(stats_base + offset);
  1860. }
  1861. } while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
  1862. idx += VIRTNET_RQ_STATS_LEN;
  1863. }
  1864. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1865. struct send_queue *sq = &vi->sq[i];
  1866. stats_base = (u8 *)&sq->stats;
  1867. do {
  1868. start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
  1869. for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
  1870. offset = virtnet_sq_stats_desc[j].offset;
  1871. data[idx + j] = *(u64 *)(stats_base + offset);
  1872. }
  1873. } while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
  1874. idx += VIRTNET_SQ_STATS_LEN;
  1875. }
  1876. }
  1877. static void virtnet_get_channels(struct net_device *dev,
  1878. struct ethtool_channels *channels)
  1879. {
  1880. struct virtnet_info *vi = netdev_priv(dev);
  1881. channels->combined_count = vi->curr_queue_pairs;
  1882. channels->max_combined = vi->max_queue_pairs;
  1883. channels->max_other = 0;
  1884. channels->rx_count = 0;
  1885. channels->tx_count = 0;
  1886. channels->other_count = 0;
  1887. }
  1888. /* Check if the user is trying to change anything besides speed/duplex */
  1889. static bool
  1890. virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
  1891. {
  1892. struct ethtool_link_ksettings diff1 = *cmd;
  1893. struct ethtool_link_ksettings diff2 = {};
  1894. /* cmd is always set so we need to clear it, validate the port type
  1895. * and also without autonegotiation we can ignore advertising
  1896. */
  1897. diff1.base.speed = 0;
  1898. diff2.base.port = PORT_OTHER;
  1899. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  1900. diff1.base.duplex = 0;
  1901. diff1.base.cmd = 0;
  1902. diff1.base.link_mode_masks_nwords = 0;
  1903. return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
  1904. bitmap_empty(diff1.link_modes.supported,
  1905. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  1906. bitmap_empty(diff1.link_modes.advertising,
  1907. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  1908. bitmap_empty(diff1.link_modes.lp_advertising,
  1909. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1910. }
  1911. static int virtnet_set_link_ksettings(struct net_device *dev,
  1912. const struct ethtool_link_ksettings *cmd)
  1913. {
  1914. struct virtnet_info *vi = netdev_priv(dev);
  1915. u32 speed;
  1916. speed = cmd->base.speed;
  1917. /* don't allow custom speed and duplex */
  1918. if (!ethtool_validate_speed(speed) ||
  1919. !ethtool_validate_duplex(cmd->base.duplex) ||
  1920. !virtnet_validate_ethtool_cmd(cmd))
  1921. return -EINVAL;
  1922. vi->speed = speed;
  1923. vi->duplex = cmd->base.duplex;
  1924. return 0;
  1925. }
  1926. static int virtnet_get_link_ksettings(struct net_device *dev,
  1927. struct ethtool_link_ksettings *cmd)
  1928. {
  1929. struct virtnet_info *vi = netdev_priv(dev);
  1930. cmd->base.speed = vi->speed;
  1931. cmd->base.duplex = vi->duplex;
  1932. cmd->base.port = PORT_OTHER;
  1933. return 0;
  1934. }
  1935. static int virtnet_set_coalesce(struct net_device *dev,
  1936. struct ethtool_coalesce *ec)
  1937. {
  1938. struct ethtool_coalesce ec_default = {
  1939. .cmd = ETHTOOL_SCOALESCE,
  1940. .rx_max_coalesced_frames = 1,
  1941. };
  1942. struct virtnet_info *vi = netdev_priv(dev);
  1943. int i, napi_weight;
  1944. if (ec->tx_max_coalesced_frames > 1)
  1945. return -EINVAL;
  1946. ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  1947. napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
  1948. /* disallow changes to fields not explicitly tested above */
  1949. if (memcmp(ec, &ec_default, sizeof(ec_default)))
  1950. return -EINVAL;
  1951. if (napi_weight ^ vi->sq[0].napi.weight) {
  1952. if (dev->flags & IFF_UP)
  1953. return -EBUSY;
  1954. for (i = 0; i < vi->max_queue_pairs; i++)
  1955. vi->sq[i].napi.weight = napi_weight;
  1956. }
  1957. return 0;
  1958. }
  1959. static int virtnet_get_coalesce(struct net_device *dev,
  1960. struct ethtool_coalesce *ec)
  1961. {
  1962. struct ethtool_coalesce ec_default = {
  1963. .cmd = ETHTOOL_GCOALESCE,
  1964. .rx_max_coalesced_frames = 1,
  1965. };
  1966. struct virtnet_info *vi = netdev_priv(dev);
  1967. memcpy(ec, &ec_default, sizeof(ec_default));
  1968. if (vi->sq[0].napi.weight)
  1969. ec->tx_max_coalesced_frames = 1;
  1970. return 0;
  1971. }
  1972. static void virtnet_init_settings(struct net_device *dev)
  1973. {
  1974. struct virtnet_info *vi = netdev_priv(dev);
  1975. vi->speed = SPEED_UNKNOWN;
  1976. vi->duplex = DUPLEX_UNKNOWN;
  1977. }
  1978. static void virtnet_update_settings(struct virtnet_info *vi)
  1979. {
  1980. u32 speed;
  1981. u8 duplex;
  1982. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
  1983. return;
  1984. speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
  1985. speed));
  1986. if (ethtool_validate_speed(speed))
  1987. vi->speed = speed;
  1988. duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
  1989. duplex));
  1990. if (ethtool_validate_duplex(duplex))
  1991. vi->duplex = duplex;
  1992. }
  1993. static const struct ethtool_ops virtnet_ethtool_ops = {
  1994. .get_drvinfo = virtnet_get_drvinfo,
  1995. .get_link = ethtool_op_get_link,
  1996. .get_ringparam = virtnet_get_ringparam,
  1997. .get_strings = virtnet_get_strings,
  1998. .get_sset_count = virtnet_get_sset_count,
  1999. .get_ethtool_stats = virtnet_get_ethtool_stats,
  2000. .set_channels = virtnet_set_channels,
  2001. .get_channels = virtnet_get_channels,
  2002. .get_ts_info = ethtool_op_get_ts_info,
  2003. .get_link_ksettings = virtnet_get_link_ksettings,
  2004. .set_link_ksettings = virtnet_set_link_ksettings,
  2005. .set_coalesce = virtnet_set_coalesce,
  2006. .get_coalesce = virtnet_get_coalesce,
  2007. };
  2008. static void virtnet_freeze_down(struct virtio_device *vdev)
  2009. {
  2010. struct virtnet_info *vi = vdev->priv;
  2011. int i;
  2012. /* Make sure no work handler is accessing the device */
  2013. flush_work(&vi->config_work);
  2014. netif_tx_lock_bh(vi->dev);
  2015. netif_device_detach(vi->dev);
  2016. netif_tx_unlock_bh(vi->dev);
  2017. cancel_delayed_work_sync(&vi->refill);
  2018. if (netif_running(vi->dev)) {
  2019. for (i = 0; i < vi->max_queue_pairs; i++) {
  2020. napi_disable(&vi->rq[i].napi);
  2021. virtnet_napi_tx_disable(&vi->sq[i].napi);
  2022. }
  2023. }
  2024. }
  2025. static int init_vqs(struct virtnet_info *vi);
  2026. static int virtnet_restore_up(struct virtio_device *vdev)
  2027. {
  2028. struct virtnet_info *vi = vdev->priv;
  2029. int err, i;
  2030. err = init_vqs(vi);
  2031. if (err)
  2032. return err;
  2033. virtio_device_ready(vdev);
  2034. if (netif_running(vi->dev)) {
  2035. for (i = 0; i < vi->curr_queue_pairs; i++)
  2036. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  2037. schedule_delayed_work(&vi->refill, 0);
  2038. for (i = 0; i < vi->max_queue_pairs; i++) {
  2039. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  2040. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  2041. &vi->sq[i].napi);
  2042. }
  2043. }
  2044. netif_tx_lock_bh(vi->dev);
  2045. netif_device_attach(vi->dev);
  2046. netif_tx_unlock_bh(vi->dev);
  2047. return err;
  2048. }
  2049. static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
  2050. {
  2051. struct scatterlist sg;
  2052. vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
  2053. sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
  2054. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
  2055. VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
  2056. dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
  2057. return -EINVAL;
  2058. }
  2059. return 0;
  2060. }
  2061. static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
  2062. {
  2063. u64 offloads = 0;
  2064. if (!vi->guest_offloads)
  2065. return 0;
  2066. return virtnet_set_guest_offloads(vi, offloads);
  2067. }
  2068. static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
  2069. {
  2070. u64 offloads = vi->guest_offloads;
  2071. if (!vi->guest_offloads)
  2072. return 0;
  2073. return virtnet_set_guest_offloads(vi, offloads);
  2074. }
  2075. static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  2076. struct netlink_ext_ack *extack)
  2077. {
  2078. unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
  2079. struct virtnet_info *vi = netdev_priv(dev);
  2080. struct bpf_prog *old_prog;
  2081. u16 xdp_qp = 0, curr_qp;
  2082. int i, err;
  2083. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
  2084. && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  2085. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  2086. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  2087. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
  2088. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
  2089. NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
  2090. return -EOPNOTSUPP;
  2091. }
  2092. if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
  2093. NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
  2094. return -EINVAL;
  2095. }
  2096. if (dev->mtu > max_sz) {
  2097. NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
  2098. netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
  2099. return -EINVAL;
  2100. }
  2101. curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
  2102. if (prog)
  2103. xdp_qp = nr_cpu_ids;
  2104. /* XDP requires extra queues for XDP_TX */
  2105. if (curr_qp + xdp_qp > vi->max_queue_pairs) {
  2106. netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
  2107. curr_qp + xdp_qp, vi->max_queue_pairs);
  2108. xdp_qp = 0;
  2109. }
  2110. old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
  2111. if (!prog && !old_prog)
  2112. return 0;
  2113. if (prog) {
  2114. prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
  2115. if (IS_ERR(prog))
  2116. return PTR_ERR(prog);
  2117. }
  2118. /* Make sure NAPI is not using any XDP TX queues for RX. */
  2119. if (netif_running(dev)) {
  2120. for (i = 0; i < vi->max_queue_pairs; i++) {
  2121. napi_disable(&vi->rq[i].napi);
  2122. virtnet_napi_tx_disable(&vi->sq[i].napi);
  2123. }
  2124. }
  2125. if (!prog) {
  2126. for (i = 0; i < vi->max_queue_pairs; i++) {
  2127. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  2128. if (i == 0)
  2129. virtnet_restore_guest_offloads(vi);
  2130. }
  2131. synchronize_net();
  2132. }
  2133. err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
  2134. if (err)
  2135. goto err;
  2136. netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
  2137. vi->xdp_queue_pairs = xdp_qp;
  2138. if (prog) {
  2139. vi->xdp_enabled = true;
  2140. for (i = 0; i < vi->max_queue_pairs; i++) {
  2141. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  2142. if (i == 0 && !old_prog)
  2143. virtnet_clear_guest_offloads(vi);
  2144. }
  2145. } else {
  2146. vi->xdp_enabled = false;
  2147. }
  2148. for (i = 0; i < vi->max_queue_pairs; i++) {
  2149. if (old_prog)
  2150. bpf_prog_put(old_prog);
  2151. if (netif_running(dev)) {
  2152. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  2153. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  2154. &vi->sq[i].napi);
  2155. }
  2156. }
  2157. return 0;
  2158. err:
  2159. if (!prog) {
  2160. virtnet_clear_guest_offloads(vi);
  2161. for (i = 0; i < vi->max_queue_pairs; i++)
  2162. rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
  2163. }
  2164. if (netif_running(dev)) {
  2165. for (i = 0; i < vi->max_queue_pairs; i++) {
  2166. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  2167. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  2168. &vi->sq[i].napi);
  2169. }
  2170. }
  2171. if (prog)
  2172. bpf_prog_sub(prog, vi->max_queue_pairs - 1);
  2173. return err;
  2174. }
  2175. static u32 virtnet_xdp_query(struct net_device *dev)
  2176. {
  2177. struct virtnet_info *vi = netdev_priv(dev);
  2178. const struct bpf_prog *xdp_prog;
  2179. int i;
  2180. for (i = 0; i < vi->max_queue_pairs; i++) {
  2181. xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  2182. if (xdp_prog)
  2183. return xdp_prog->aux->id;
  2184. }
  2185. return 0;
  2186. }
  2187. static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  2188. {
  2189. switch (xdp->command) {
  2190. case XDP_SETUP_PROG:
  2191. return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
  2192. case XDP_QUERY_PROG:
  2193. xdp->prog_id = virtnet_xdp_query(dev);
  2194. return 0;
  2195. default:
  2196. return -EINVAL;
  2197. }
  2198. }
  2199. static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
  2200. size_t len)
  2201. {
  2202. struct virtnet_info *vi = netdev_priv(dev);
  2203. int ret;
  2204. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
  2205. return -EOPNOTSUPP;
  2206. ret = snprintf(buf, len, "sby");
  2207. if (ret >= len)
  2208. return -EOPNOTSUPP;
  2209. return 0;
  2210. }
  2211. static int virtnet_set_features(struct net_device *dev,
  2212. netdev_features_t features)
  2213. {
  2214. struct virtnet_info *vi = netdev_priv(dev);
  2215. u64 offloads;
  2216. int err;
  2217. if ((dev->features ^ features) & NETIF_F_GRO_HW) {
  2218. if (vi->xdp_enabled)
  2219. return -EBUSY;
  2220. if (features & NETIF_F_GRO_HW)
  2221. offloads = vi->guest_offloads_capable;
  2222. else
  2223. offloads = vi->guest_offloads_capable &
  2224. ~GUEST_OFFLOAD_GRO_HW_MASK;
  2225. err = virtnet_set_guest_offloads(vi, offloads);
  2226. if (err)
  2227. return err;
  2228. vi->guest_offloads = offloads;
  2229. }
  2230. return 0;
  2231. }
  2232. static const struct net_device_ops virtnet_netdev = {
  2233. .ndo_open = virtnet_open,
  2234. .ndo_stop = virtnet_close,
  2235. .ndo_start_xmit = start_xmit,
  2236. .ndo_validate_addr = eth_validate_addr,
  2237. .ndo_set_mac_address = virtnet_set_mac_address,
  2238. .ndo_set_rx_mode = virtnet_set_rx_mode,
  2239. .ndo_get_stats64 = virtnet_stats,
  2240. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  2241. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  2242. .ndo_bpf = virtnet_xdp,
  2243. .ndo_xdp_xmit = virtnet_xdp_xmit,
  2244. .ndo_features_check = passthru_features_check,
  2245. .ndo_get_phys_port_name = virtnet_get_phys_port_name,
  2246. .ndo_set_features = virtnet_set_features,
  2247. };
  2248. static void virtnet_config_changed_work(struct work_struct *work)
  2249. {
  2250. struct virtnet_info *vi =
  2251. container_of(work, struct virtnet_info, config_work);
  2252. u16 v;
  2253. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  2254. struct virtio_net_config, status, &v) < 0)
  2255. return;
  2256. if (v & VIRTIO_NET_S_ANNOUNCE) {
  2257. netdev_notify_peers(vi->dev);
  2258. virtnet_ack_link_announce(vi);
  2259. }
  2260. /* Ignore unknown (future) status bits */
  2261. v &= VIRTIO_NET_S_LINK_UP;
  2262. if (vi->status == v)
  2263. return;
  2264. vi->status = v;
  2265. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  2266. virtnet_update_settings(vi);
  2267. netif_carrier_on(vi->dev);
  2268. netif_tx_wake_all_queues(vi->dev);
  2269. } else {
  2270. netif_carrier_off(vi->dev);
  2271. netif_tx_stop_all_queues(vi->dev);
  2272. }
  2273. }
  2274. static void virtnet_config_changed(struct virtio_device *vdev)
  2275. {
  2276. struct virtnet_info *vi = vdev->priv;
  2277. schedule_work(&vi->config_work);
  2278. }
  2279. static void virtnet_free_queues(struct virtnet_info *vi)
  2280. {
  2281. int i;
  2282. for (i = 0; i < vi->max_queue_pairs; i++) {
  2283. napi_hash_del(&vi->rq[i].napi);
  2284. netif_napi_del(&vi->rq[i].napi);
  2285. netif_napi_del(&vi->sq[i].napi);
  2286. }
  2287. /* We called napi_hash_del() before netif_napi_del(),
  2288. * we need to respect an RCU grace period before freeing vi->rq
  2289. */
  2290. synchronize_net();
  2291. kfree(vi->rq);
  2292. kfree(vi->sq);
  2293. kfree(vi->ctrl);
  2294. }
  2295. static void _free_receive_bufs(struct virtnet_info *vi)
  2296. {
  2297. struct bpf_prog *old_prog;
  2298. int i;
  2299. for (i = 0; i < vi->max_queue_pairs; i++) {
  2300. while (vi->rq[i].pages)
  2301. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  2302. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  2303. RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
  2304. if (old_prog)
  2305. bpf_prog_put(old_prog);
  2306. }
  2307. }
  2308. static void free_receive_bufs(struct virtnet_info *vi)
  2309. {
  2310. rtnl_lock();
  2311. _free_receive_bufs(vi);
  2312. rtnl_unlock();
  2313. }
  2314. static void free_receive_page_frags(struct virtnet_info *vi)
  2315. {
  2316. int i;
  2317. for (i = 0; i < vi->max_queue_pairs; i++)
  2318. if (vi->rq[i].alloc_frag.page)
  2319. put_page(vi->rq[i].alloc_frag.page);
  2320. }
  2321. static void free_unused_bufs(struct virtnet_info *vi)
  2322. {
  2323. void *buf;
  2324. int i;
  2325. for (i = 0; i < vi->max_queue_pairs; i++) {
  2326. struct virtqueue *vq = vi->sq[i].vq;
  2327. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  2328. if (!is_xdp_frame(buf))
  2329. dev_kfree_skb(buf);
  2330. else
  2331. xdp_return_frame(ptr_to_xdp(buf));
  2332. }
  2333. }
  2334. for (i = 0; i < vi->max_queue_pairs; i++) {
  2335. struct virtqueue *vq = vi->rq[i].vq;
  2336. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  2337. if (vi->mergeable_rx_bufs) {
  2338. put_page(virt_to_head_page(buf));
  2339. } else if (vi->big_packets) {
  2340. give_pages(&vi->rq[i], buf);
  2341. } else {
  2342. put_page(virt_to_head_page(buf));
  2343. }
  2344. }
  2345. }
  2346. }
  2347. static void virtnet_del_vqs(struct virtnet_info *vi)
  2348. {
  2349. struct virtio_device *vdev = vi->vdev;
  2350. virtnet_clean_affinity(vi);
  2351. vdev->config->del_vqs(vdev);
  2352. virtnet_free_queues(vi);
  2353. }
  2354. /* How large should a single buffer be so a queue full of these can fit at
  2355. * least one full packet?
  2356. * Logic below assumes the mergeable buffer header is used.
  2357. */
  2358. static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
  2359. {
  2360. const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  2361. unsigned int rq_size = virtqueue_get_vring_size(vq);
  2362. unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
  2363. unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
  2364. unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
  2365. return max(max(min_buf_len, hdr_len) - hdr_len,
  2366. (unsigned int)GOOD_PACKET_LEN);
  2367. }
  2368. static int virtnet_find_vqs(struct virtnet_info *vi)
  2369. {
  2370. vq_callback_t **callbacks;
  2371. struct virtqueue **vqs;
  2372. int ret = -ENOMEM;
  2373. int i, total_vqs;
  2374. const char **names;
  2375. bool *ctx;
  2376. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  2377. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  2378. * possible control vq.
  2379. */
  2380. total_vqs = vi->max_queue_pairs * 2 +
  2381. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  2382. /* Allocate space for find_vqs parameters */
  2383. vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
  2384. if (!vqs)
  2385. goto err_vq;
  2386. callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
  2387. if (!callbacks)
  2388. goto err_callback;
  2389. names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
  2390. if (!names)
  2391. goto err_names;
  2392. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  2393. ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
  2394. if (!ctx)
  2395. goto err_ctx;
  2396. } else {
  2397. ctx = NULL;
  2398. }
  2399. /* Parameters for control virtqueue, if any */
  2400. if (vi->has_cvq) {
  2401. callbacks[total_vqs - 1] = NULL;
  2402. names[total_vqs - 1] = "control";
  2403. }
  2404. /* Allocate/initialize parameters for send/receive virtqueues */
  2405. for (i = 0; i < vi->max_queue_pairs; i++) {
  2406. callbacks[rxq2vq(i)] = skb_recv_done;
  2407. callbacks[txq2vq(i)] = skb_xmit_done;
  2408. sprintf(vi->rq[i].name, "input.%d", i);
  2409. sprintf(vi->sq[i].name, "output.%d", i);
  2410. names[rxq2vq(i)] = vi->rq[i].name;
  2411. names[txq2vq(i)] = vi->sq[i].name;
  2412. if (ctx)
  2413. ctx[rxq2vq(i)] = true;
  2414. }
  2415. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  2416. names, ctx, NULL);
  2417. if (ret)
  2418. goto err_find;
  2419. if (vi->has_cvq) {
  2420. vi->cvq = vqs[total_vqs - 1];
  2421. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  2422. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2423. }
  2424. for (i = 0; i < vi->max_queue_pairs; i++) {
  2425. vi->rq[i].vq = vqs[rxq2vq(i)];
  2426. vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
  2427. vi->sq[i].vq = vqs[txq2vq(i)];
  2428. }
  2429. /* run here: ret == 0. */
  2430. err_find:
  2431. kfree(ctx);
  2432. err_ctx:
  2433. kfree(names);
  2434. err_names:
  2435. kfree(callbacks);
  2436. err_callback:
  2437. kfree(vqs);
  2438. err_vq:
  2439. return ret;
  2440. }
  2441. static int virtnet_alloc_queues(struct virtnet_info *vi)
  2442. {
  2443. int i;
  2444. vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
  2445. if (!vi->ctrl)
  2446. goto err_ctrl;
  2447. vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
  2448. if (!vi->sq)
  2449. goto err_sq;
  2450. vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
  2451. if (!vi->rq)
  2452. goto err_rq;
  2453. INIT_DELAYED_WORK(&vi->refill, refill_work);
  2454. for (i = 0; i < vi->max_queue_pairs; i++) {
  2455. vi->rq[i].pages = NULL;
  2456. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  2457. napi_weight);
  2458. netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
  2459. napi_tx ? napi_weight : 0);
  2460. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  2461. ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
  2462. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  2463. u64_stats_init(&vi->rq[i].stats.syncp);
  2464. u64_stats_init(&vi->sq[i].stats.syncp);
  2465. }
  2466. return 0;
  2467. err_rq:
  2468. kfree(vi->sq);
  2469. err_sq:
  2470. kfree(vi->ctrl);
  2471. err_ctrl:
  2472. return -ENOMEM;
  2473. }
  2474. static int init_vqs(struct virtnet_info *vi)
  2475. {
  2476. int ret;
  2477. /* Allocate send & receive queues */
  2478. ret = virtnet_alloc_queues(vi);
  2479. if (ret)
  2480. goto err;
  2481. ret = virtnet_find_vqs(vi);
  2482. if (ret)
  2483. goto err_free;
  2484. get_online_cpus();
  2485. virtnet_set_affinity(vi);
  2486. put_online_cpus();
  2487. return 0;
  2488. err_free:
  2489. virtnet_free_queues(vi);
  2490. err:
  2491. return ret;
  2492. }
  2493. #ifdef CONFIG_SYSFS
  2494. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  2495. char *buf)
  2496. {
  2497. struct virtnet_info *vi = netdev_priv(queue->dev);
  2498. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  2499. unsigned int headroom = virtnet_get_headroom(vi);
  2500. unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
  2501. struct ewma_pkt_len *avg;
  2502. BUG_ON(queue_index >= vi->max_queue_pairs);
  2503. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  2504. return sprintf(buf, "%u\n",
  2505. get_mergeable_buf_len(&vi->rq[queue_index], avg,
  2506. SKB_DATA_ALIGN(headroom + tailroom)));
  2507. }
  2508. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  2509. __ATTR_RO(mergeable_rx_buffer_size);
  2510. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  2511. &mergeable_rx_buffer_size_attribute.attr,
  2512. NULL
  2513. };
  2514. static const struct attribute_group virtio_net_mrg_rx_group = {
  2515. .name = "virtio_net",
  2516. .attrs = virtio_net_mrg_rx_attrs
  2517. };
  2518. #endif
  2519. static bool virtnet_fail_on_feature(struct virtio_device *vdev,
  2520. unsigned int fbit,
  2521. const char *fname, const char *dname)
  2522. {
  2523. if (!virtio_has_feature(vdev, fbit))
  2524. return false;
  2525. dev_err(&vdev->dev, "device advertises feature %s but not %s",
  2526. fname, dname);
  2527. return true;
  2528. }
  2529. #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
  2530. virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
  2531. static bool virtnet_validate_features(struct virtio_device *vdev)
  2532. {
  2533. if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
  2534. (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
  2535. "VIRTIO_NET_F_CTRL_VQ") ||
  2536. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
  2537. "VIRTIO_NET_F_CTRL_VQ") ||
  2538. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
  2539. "VIRTIO_NET_F_CTRL_VQ") ||
  2540. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
  2541. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
  2542. "VIRTIO_NET_F_CTRL_VQ"))) {
  2543. return false;
  2544. }
  2545. return true;
  2546. }
  2547. #define MIN_MTU ETH_MIN_MTU
  2548. #define MAX_MTU ETH_MAX_MTU
  2549. static int virtnet_validate(struct virtio_device *vdev)
  2550. {
  2551. if (!vdev->config->get) {
  2552. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  2553. __func__);
  2554. return -EINVAL;
  2555. }
  2556. if (!virtnet_validate_features(vdev))
  2557. return -EINVAL;
  2558. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  2559. int mtu = virtio_cread16(vdev,
  2560. offsetof(struct virtio_net_config,
  2561. mtu));
  2562. if (mtu < MIN_MTU)
  2563. __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
  2564. }
  2565. return 0;
  2566. }
  2567. static int virtnet_probe(struct virtio_device *vdev)
  2568. {
  2569. int i, err = -ENOMEM;
  2570. struct net_device *dev;
  2571. struct virtnet_info *vi;
  2572. u16 max_queue_pairs;
  2573. int mtu;
  2574. /* Find if host supports multiqueue virtio_net device */
  2575. err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
  2576. struct virtio_net_config,
  2577. max_virtqueue_pairs, &max_queue_pairs);
  2578. /* We need at least 2 queue's */
  2579. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  2580. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  2581. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  2582. max_queue_pairs = 1;
  2583. /* Allocate ourselves a network device with room for our info */
  2584. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  2585. if (!dev)
  2586. return -ENOMEM;
  2587. /* Set up network device as normal. */
  2588. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  2589. dev->netdev_ops = &virtnet_netdev;
  2590. dev->features = NETIF_F_HIGHDMA;
  2591. dev->ethtool_ops = &virtnet_ethtool_ops;
  2592. SET_NETDEV_DEV(dev, &vdev->dev);
  2593. /* Do we support "hardware" checksums? */
  2594. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  2595. /* This opens up the world of extra features. */
  2596. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  2597. if (csum)
  2598. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  2599. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  2600. dev->hw_features |= NETIF_F_TSO
  2601. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  2602. }
  2603. /* Individual feature bits: what can host handle? */
  2604. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  2605. dev->hw_features |= NETIF_F_TSO;
  2606. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  2607. dev->hw_features |= NETIF_F_TSO6;
  2608. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  2609. dev->hw_features |= NETIF_F_TSO_ECN;
  2610. dev->features |= NETIF_F_GSO_ROBUST;
  2611. if (gso)
  2612. dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
  2613. /* (!csum && gso) case will be fixed by register_netdev() */
  2614. }
  2615. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  2616. dev->features |= NETIF_F_RXCSUM;
  2617. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  2618. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
  2619. dev->features |= NETIF_F_GRO_HW;
  2620. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
  2621. dev->hw_features |= NETIF_F_GRO_HW;
  2622. dev->vlan_features = dev->features;
  2623. /* MTU range: 68 - 65535 */
  2624. dev->min_mtu = MIN_MTU;
  2625. dev->max_mtu = MAX_MTU;
  2626. /* Configuration may specify what MAC to use. Otherwise random. */
  2627. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  2628. virtio_cread_bytes(vdev,
  2629. offsetof(struct virtio_net_config, mac),
  2630. dev->dev_addr, dev->addr_len);
  2631. else
  2632. eth_hw_addr_random(dev);
  2633. /* Set up our device-specific information */
  2634. vi = netdev_priv(dev);
  2635. vi->dev = dev;
  2636. vi->vdev = vdev;
  2637. vdev->priv = vi;
  2638. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  2639. /* If we can receive ANY GSO packets, we must allocate large ones. */
  2640. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  2641. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  2642. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
  2643. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
  2644. vi->big_packets = true;
  2645. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  2646. vi->mergeable_rx_bufs = true;
  2647. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
  2648. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  2649. vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  2650. else
  2651. vi->hdr_len = sizeof(struct virtio_net_hdr);
  2652. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
  2653. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  2654. vi->any_header_sg = true;
  2655. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  2656. vi->has_cvq = true;
  2657. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  2658. mtu = virtio_cread16(vdev,
  2659. offsetof(struct virtio_net_config,
  2660. mtu));
  2661. if (mtu < dev->min_mtu) {
  2662. /* Should never trigger: MTU was previously validated
  2663. * in virtnet_validate.
  2664. */
  2665. dev_err(&vdev->dev,
  2666. "device MTU appears to have changed it is now %d < %d",
  2667. mtu, dev->min_mtu);
  2668. err = -EINVAL;
  2669. goto free;
  2670. }
  2671. dev->mtu = mtu;
  2672. dev->max_mtu = mtu;
  2673. /* TODO: size buffers correctly in this case. */
  2674. if (dev->mtu > ETH_DATA_LEN)
  2675. vi->big_packets = true;
  2676. }
  2677. if (vi->any_header_sg)
  2678. dev->needed_headroom = vi->hdr_len;
  2679. /* Enable multiqueue by default */
  2680. if (num_online_cpus() >= max_queue_pairs)
  2681. vi->curr_queue_pairs = max_queue_pairs;
  2682. else
  2683. vi->curr_queue_pairs = num_online_cpus();
  2684. vi->max_queue_pairs = max_queue_pairs;
  2685. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  2686. err = init_vqs(vi);
  2687. if (err)
  2688. goto free;
  2689. #ifdef CONFIG_SYSFS
  2690. if (vi->mergeable_rx_bufs)
  2691. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  2692. #endif
  2693. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  2694. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  2695. virtnet_init_settings(dev);
  2696. if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
  2697. vi->failover = net_failover_create(vi->dev);
  2698. if (IS_ERR(vi->failover)) {
  2699. err = PTR_ERR(vi->failover);
  2700. goto free_vqs;
  2701. }
  2702. }
  2703. err = register_netdev(dev);
  2704. if (err) {
  2705. pr_debug("virtio_net: registering device failed\n");
  2706. goto free_failover;
  2707. }
  2708. virtio_device_ready(vdev);
  2709. err = virtnet_cpu_notif_add(vi);
  2710. if (err) {
  2711. pr_debug("virtio_net: registering cpu notifier failed\n");
  2712. goto free_unregister_netdev;
  2713. }
  2714. virtnet_set_queues(vi, vi->curr_queue_pairs);
  2715. /* Assume link up if device can't report link status,
  2716. otherwise get link status from config. */
  2717. netif_carrier_off(dev);
  2718. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  2719. schedule_work(&vi->config_work);
  2720. } else {
  2721. vi->status = VIRTIO_NET_S_LINK_UP;
  2722. virtnet_update_settings(vi);
  2723. netif_carrier_on(dev);
  2724. }
  2725. for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
  2726. if (virtio_has_feature(vi->vdev, guest_offloads[i]))
  2727. set_bit(guest_offloads[i], &vi->guest_offloads);
  2728. vi->guest_offloads_capable = vi->guest_offloads;
  2729. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  2730. dev->name, max_queue_pairs);
  2731. return 0;
  2732. free_unregister_netdev:
  2733. vi->vdev->config->reset(vdev);
  2734. unregister_netdev(dev);
  2735. free_failover:
  2736. net_failover_destroy(vi->failover);
  2737. free_vqs:
  2738. cancel_delayed_work_sync(&vi->refill);
  2739. free_receive_page_frags(vi);
  2740. virtnet_del_vqs(vi);
  2741. free:
  2742. free_netdev(dev);
  2743. return err;
  2744. }
  2745. static void remove_vq_common(struct virtnet_info *vi)
  2746. {
  2747. vi->vdev->config->reset(vi->vdev);
  2748. /* Free unused buffers in both send and recv, if any. */
  2749. free_unused_bufs(vi);
  2750. free_receive_bufs(vi);
  2751. free_receive_page_frags(vi);
  2752. virtnet_del_vqs(vi);
  2753. }
  2754. static void virtnet_remove(struct virtio_device *vdev)
  2755. {
  2756. struct virtnet_info *vi = vdev->priv;
  2757. virtnet_cpu_notif_remove(vi);
  2758. /* Make sure no work handler is accessing the device. */
  2759. flush_work(&vi->config_work);
  2760. unregister_netdev(vi->dev);
  2761. net_failover_destroy(vi->failover);
  2762. remove_vq_common(vi);
  2763. free_netdev(vi->dev);
  2764. }
  2765. static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
  2766. {
  2767. struct virtnet_info *vi = vdev->priv;
  2768. virtnet_cpu_notif_remove(vi);
  2769. virtnet_freeze_down(vdev);
  2770. remove_vq_common(vi);
  2771. return 0;
  2772. }
  2773. static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
  2774. {
  2775. struct virtnet_info *vi = vdev->priv;
  2776. int err;
  2777. err = virtnet_restore_up(vdev);
  2778. if (err)
  2779. return err;
  2780. virtnet_set_queues(vi, vi->curr_queue_pairs);
  2781. err = virtnet_cpu_notif_add(vi);
  2782. if (err) {
  2783. virtnet_freeze_down(vdev);
  2784. remove_vq_common(vi);
  2785. return err;
  2786. }
  2787. return 0;
  2788. }
  2789. static struct virtio_device_id id_table[] = {
  2790. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  2791. { 0 },
  2792. };
  2793. #define VIRTNET_FEATURES \
  2794. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
  2795. VIRTIO_NET_F_MAC, \
  2796. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
  2797. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
  2798. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
  2799. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
  2800. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
  2801. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
  2802. VIRTIO_NET_F_CTRL_MAC_ADDR, \
  2803. VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
  2804. VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
  2805. static unsigned int features[] = {
  2806. VIRTNET_FEATURES,
  2807. };
  2808. static unsigned int features_legacy[] = {
  2809. VIRTNET_FEATURES,
  2810. VIRTIO_NET_F_GSO,
  2811. VIRTIO_F_ANY_LAYOUT,
  2812. };
  2813. static struct virtio_driver virtio_net_driver = {
  2814. .feature_table = features,
  2815. .feature_table_size = ARRAY_SIZE(features),
  2816. .feature_table_legacy = features_legacy,
  2817. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  2818. .driver.name = KBUILD_MODNAME,
  2819. .driver.owner = THIS_MODULE,
  2820. .id_table = id_table,
  2821. .validate = virtnet_validate,
  2822. .probe = virtnet_probe,
  2823. .remove = virtnet_remove,
  2824. .config_changed = virtnet_config_changed,
  2825. #ifdef CONFIG_PM_SLEEP
  2826. .freeze = virtnet_freeze,
  2827. .restore = virtnet_restore,
  2828. #endif
  2829. };
  2830. static __init int virtio_net_driver_init(void)
  2831. {
  2832. int ret;
  2833. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
  2834. virtnet_cpu_online,
  2835. virtnet_cpu_down_prep);
  2836. if (ret < 0)
  2837. goto out;
  2838. virtionet_online = ret;
  2839. ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
  2840. NULL, virtnet_cpu_dead);
  2841. if (ret)
  2842. goto err_dead;
  2843. ret = register_virtio_driver(&virtio_net_driver);
  2844. if (ret)
  2845. goto err_virtio;
  2846. return 0;
  2847. err_virtio:
  2848. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  2849. err_dead:
  2850. cpuhp_remove_multi_state(virtionet_online);
  2851. out:
  2852. return ret;
  2853. }
  2854. module_init(virtio_net_driver_init);
  2855. static __exit void virtio_net_driver_exit(void)
  2856. {
  2857. unregister_virtio_driver(&virtio_net_driver);
  2858. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  2859. cpuhp_remove_multi_state(virtionet_online);
  2860. }
  2861. module_exit(virtio_net_driver_exit);
  2862. MODULE_DEVICE_TABLE(virtio, id_table);
  2863. MODULE_DESCRIPTION("Virtio network driver");
  2864. MODULE_LICENSE("GPL");