virtio_net.c 66 KB

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