virtio_net.c 73 KB

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