virtio_net.c 63 KB

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