virtio_net.c 79 KB

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