virtio_net.c 66 KB

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