virtio_net.c 64 KB

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