virtio_net.c 70 KB

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