virtio_net.c 63 KB

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