virtio_net.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  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/scatterlist.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/slab.h>
  28. #include <linux/cpu.h>
  29. #include <linux/average.h>
  30. #include <net/busy_poll.h>
  31. static int napi_weight = NAPI_POLL_WEIGHT;
  32. module_param(napi_weight, int, 0444);
  33. static bool csum = true, gso = true;
  34. module_param(csum, bool, 0444);
  35. module_param(gso, bool, 0444);
  36. /* FIXME: MTU in config. */
  37. #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  38. #define GOOD_COPY_LEN 128
  39. /* Weight used for the RX packet size EWMA. The average packet size is used to
  40. * determine the packet buffer size when refilling RX rings. As the entire RX
  41. * ring may be refilled at once, the weight is chosen so that the EWMA will be
  42. * insensitive to short-term, transient changes in packet size.
  43. */
  44. #define RECEIVE_AVG_WEIGHT 64
  45. /* Minimum alignment for mergeable packet buffers. */
  46. #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, 256)
  47. #define VIRTNET_DRIVER_VERSION "1.0.0"
  48. struct virtnet_stats {
  49. struct u64_stats_sync tx_syncp;
  50. struct u64_stats_sync rx_syncp;
  51. u64 tx_bytes;
  52. u64 tx_packets;
  53. u64 rx_bytes;
  54. u64 rx_packets;
  55. };
  56. /* Internal representation of a send virtqueue */
  57. struct send_queue {
  58. /* Virtqueue associated with this send _queue */
  59. struct virtqueue *vq;
  60. /* TX: fragments + linear part + virtio header */
  61. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  62. /* Name of the send queue: output.$index */
  63. char name[40];
  64. };
  65. /* Internal representation of a receive virtqueue */
  66. struct receive_queue {
  67. /* Virtqueue associated with this receive_queue */
  68. struct virtqueue *vq;
  69. struct napi_struct napi;
  70. /* Chain pages by the private ptr. */
  71. struct page *pages;
  72. /* Average packet length for mergeable receive buffers. */
  73. struct ewma mrg_avg_pkt_len;
  74. /* Page frag for packet buffer allocation. */
  75. struct page_frag alloc_frag;
  76. /* RX: fragments + linear part + virtio header */
  77. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  78. /* Name of this receive queue: input.$index */
  79. char name[40];
  80. };
  81. struct virtnet_info {
  82. struct virtio_device *vdev;
  83. struct virtqueue *cvq;
  84. struct net_device *dev;
  85. struct send_queue *sq;
  86. struct receive_queue *rq;
  87. unsigned int status;
  88. /* Max # of queue pairs supported by the device */
  89. u16 max_queue_pairs;
  90. /* # of queue pairs currently used by the driver */
  91. u16 curr_queue_pairs;
  92. /* I like... big packets and I cannot lie! */
  93. bool big_packets;
  94. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  95. bool mergeable_rx_bufs;
  96. /* Has control virtqueue */
  97. bool has_cvq;
  98. /* Host can handle any s/g split between our header and packet data */
  99. bool any_header_sg;
  100. /* Active statistics */
  101. struct virtnet_stats __percpu *stats;
  102. /* Work struct for refilling if we run low on memory. */
  103. struct delayed_work refill;
  104. /* Work struct for config space updates */
  105. struct work_struct config_work;
  106. /* Does the affinity hint is set for virtqueues? */
  107. bool affinity_hint_set;
  108. /* CPU hot plug notifier */
  109. struct notifier_block nb;
  110. };
  111. struct skb_vnet_hdr {
  112. union {
  113. struct virtio_net_hdr hdr;
  114. struct virtio_net_hdr_mrg_rxbuf mhdr;
  115. };
  116. };
  117. struct padded_vnet_hdr {
  118. struct virtio_net_hdr hdr;
  119. /*
  120. * virtio_net_hdr should be in a separated sg buffer because of a
  121. * QEMU bug, and data sg buffer shares same page with this header sg.
  122. * This padding makes next sg 16 byte aligned after virtio_net_hdr.
  123. */
  124. char padding[6];
  125. };
  126. /* Converting between virtqueue no. and kernel tx/rx queue no.
  127. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  128. */
  129. static int vq2txq(struct virtqueue *vq)
  130. {
  131. return (vq->index - 1) / 2;
  132. }
  133. static int txq2vq(int txq)
  134. {
  135. return txq * 2 + 1;
  136. }
  137. static int vq2rxq(struct virtqueue *vq)
  138. {
  139. return vq->index / 2;
  140. }
  141. static int rxq2vq(int rxq)
  142. {
  143. return rxq * 2;
  144. }
  145. static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
  146. {
  147. return (struct skb_vnet_hdr *)skb->cb;
  148. }
  149. /*
  150. * private is used to chain pages for big packets, put the whole
  151. * most recent used list in the beginning for reuse
  152. */
  153. static void give_pages(struct receive_queue *rq, struct page *page)
  154. {
  155. struct page *end;
  156. /* Find end of list, sew whole thing into vi->rq.pages. */
  157. for (end = page; end->private; end = (struct page *)end->private);
  158. end->private = (unsigned long)rq->pages;
  159. rq->pages = page;
  160. }
  161. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  162. {
  163. struct page *p = rq->pages;
  164. if (p) {
  165. rq->pages = (struct page *)p->private;
  166. /* clear private here, it is used to chain pages */
  167. p->private = 0;
  168. } else
  169. p = alloc_page(gfp_mask);
  170. return p;
  171. }
  172. static void skb_xmit_done(struct virtqueue *vq)
  173. {
  174. struct virtnet_info *vi = vq->vdev->priv;
  175. /* Suppress further interrupts. */
  176. virtqueue_disable_cb(vq);
  177. /* We were probably waiting for more output buffers. */
  178. netif_wake_subqueue(vi->dev, vq2txq(vq));
  179. }
  180. static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
  181. {
  182. unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
  183. return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
  184. }
  185. static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
  186. {
  187. return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
  188. }
  189. static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
  190. {
  191. unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
  192. return (unsigned long)buf | (size - 1);
  193. }
  194. /* Called from bottom half context */
  195. static struct sk_buff *page_to_skb(struct receive_queue *rq,
  196. struct page *page, unsigned int offset,
  197. unsigned int len, unsigned int truesize)
  198. {
  199. struct virtnet_info *vi = rq->vq->vdev->priv;
  200. struct sk_buff *skb;
  201. struct skb_vnet_hdr *hdr;
  202. unsigned int copy, hdr_len, hdr_padded_len;
  203. char *p;
  204. p = page_address(page) + offset;
  205. /* copy small packet so we can reuse these pages for small data */
  206. skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
  207. if (unlikely(!skb))
  208. return NULL;
  209. hdr = skb_vnet_hdr(skb);
  210. if (vi->mergeable_rx_bufs) {
  211. hdr_len = sizeof hdr->mhdr;
  212. hdr_padded_len = sizeof hdr->mhdr;
  213. } else {
  214. hdr_len = sizeof hdr->hdr;
  215. hdr_padded_len = sizeof(struct padded_vnet_hdr);
  216. }
  217. memcpy(hdr, p, hdr_len);
  218. len -= hdr_len;
  219. offset += hdr_padded_len;
  220. p += hdr_padded_len;
  221. copy = len;
  222. if (copy > skb_tailroom(skb))
  223. copy = skb_tailroom(skb);
  224. memcpy(skb_put(skb, copy), p, copy);
  225. len -= copy;
  226. offset += copy;
  227. if (vi->mergeable_rx_bufs) {
  228. if (len)
  229. skb_add_rx_frag(skb, 0, page, offset, len, truesize);
  230. else
  231. put_page(page);
  232. return skb;
  233. }
  234. /*
  235. * Verify that we can indeed put this data into a skb.
  236. * This is here to handle cases when the device erroneously
  237. * tries to receive more than is possible. This is usually
  238. * the case of a broken device.
  239. */
  240. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  241. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  242. dev_kfree_skb(skb);
  243. return NULL;
  244. }
  245. BUG_ON(offset >= PAGE_SIZE);
  246. while (len) {
  247. unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
  248. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
  249. frag_size, truesize);
  250. len -= frag_size;
  251. page = (struct page *)page->private;
  252. offset = 0;
  253. }
  254. if (page)
  255. give_pages(rq, page);
  256. return skb;
  257. }
  258. static struct sk_buff *receive_small(void *buf, unsigned int len)
  259. {
  260. struct sk_buff * skb = buf;
  261. len -= sizeof(struct virtio_net_hdr);
  262. skb_trim(skb, len);
  263. return skb;
  264. }
  265. static struct sk_buff *receive_big(struct net_device *dev,
  266. struct receive_queue *rq,
  267. void *buf,
  268. unsigned int len)
  269. {
  270. struct page *page = buf;
  271. struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
  272. if (unlikely(!skb))
  273. goto err;
  274. return skb;
  275. err:
  276. dev->stats.rx_dropped++;
  277. give_pages(rq, page);
  278. return NULL;
  279. }
  280. static struct sk_buff *receive_mergeable(struct net_device *dev,
  281. struct receive_queue *rq,
  282. unsigned long ctx,
  283. unsigned int len)
  284. {
  285. void *buf = mergeable_ctx_to_buf_address(ctx);
  286. struct skb_vnet_hdr *hdr = buf;
  287. int num_buf = hdr->mhdr.num_buffers;
  288. struct page *page = virt_to_head_page(buf);
  289. int offset = buf - page_address(page);
  290. unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
  291. struct sk_buff *head_skb = page_to_skb(rq, page, offset, len, truesize);
  292. struct sk_buff *curr_skb = head_skb;
  293. if (unlikely(!curr_skb))
  294. goto err_skb;
  295. while (--num_buf) {
  296. int num_skb_frags;
  297. ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
  298. if (unlikely(!ctx)) {
  299. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  300. dev->name, num_buf, hdr->mhdr.num_buffers);
  301. dev->stats.rx_length_errors++;
  302. goto err_buf;
  303. }
  304. buf = mergeable_ctx_to_buf_address(ctx);
  305. page = virt_to_head_page(buf);
  306. num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
  307. if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
  308. struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
  309. if (unlikely(!nskb))
  310. goto err_skb;
  311. if (curr_skb == head_skb)
  312. skb_shinfo(curr_skb)->frag_list = nskb;
  313. else
  314. curr_skb->next = nskb;
  315. curr_skb = nskb;
  316. head_skb->truesize += nskb->truesize;
  317. num_skb_frags = 0;
  318. }
  319. truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
  320. if (curr_skb != head_skb) {
  321. head_skb->data_len += len;
  322. head_skb->len += len;
  323. head_skb->truesize += truesize;
  324. }
  325. offset = buf - page_address(page);
  326. if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
  327. put_page(page);
  328. skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  329. len, truesize);
  330. } else {
  331. skb_add_rx_frag(curr_skb, num_skb_frags, page,
  332. offset, len, truesize);
  333. }
  334. }
  335. ewma_add(&rq->mrg_avg_pkt_len, head_skb->len);
  336. return head_skb;
  337. err_skb:
  338. put_page(page);
  339. while (--num_buf) {
  340. ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
  341. if (unlikely(!ctx)) {
  342. pr_debug("%s: rx error: %d buffers missing\n",
  343. dev->name, num_buf);
  344. dev->stats.rx_length_errors++;
  345. break;
  346. }
  347. page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
  348. put_page(page);
  349. }
  350. err_buf:
  351. dev->stats.rx_dropped++;
  352. dev_kfree_skb(head_skb);
  353. return NULL;
  354. }
  355. static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
  356. {
  357. struct virtnet_info *vi = rq->vq->vdev->priv;
  358. struct net_device *dev = vi->dev;
  359. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  360. struct sk_buff *skb;
  361. struct skb_vnet_hdr *hdr;
  362. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  363. pr_debug("%s: short packet %i\n", dev->name, len);
  364. dev->stats.rx_length_errors++;
  365. if (vi->mergeable_rx_bufs) {
  366. unsigned long ctx = (unsigned long)buf;
  367. void *base = mergeable_ctx_to_buf_address(ctx);
  368. put_page(virt_to_head_page(base));
  369. } else if (vi->big_packets) {
  370. give_pages(rq, buf);
  371. } else {
  372. dev_kfree_skb(buf);
  373. }
  374. return;
  375. }
  376. if (vi->mergeable_rx_bufs)
  377. skb = receive_mergeable(dev, rq, (unsigned long)buf, len);
  378. else if (vi->big_packets)
  379. skb = receive_big(dev, rq, buf, len);
  380. else
  381. skb = receive_small(buf, len);
  382. if (unlikely(!skb))
  383. return;
  384. hdr = skb_vnet_hdr(skb);
  385. u64_stats_update_begin(&stats->rx_syncp);
  386. stats->rx_bytes += skb->len;
  387. stats->rx_packets++;
  388. u64_stats_update_end(&stats->rx_syncp);
  389. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  390. pr_debug("Needs csum!\n");
  391. if (!skb_partial_csum_set(skb,
  392. hdr->hdr.csum_start,
  393. hdr->hdr.csum_offset))
  394. goto frame_err;
  395. } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
  396. skb->ip_summed = CHECKSUM_UNNECESSARY;
  397. }
  398. skb->protocol = eth_type_trans(skb, dev);
  399. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  400. ntohs(skb->protocol), skb->len, skb->pkt_type);
  401. if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  402. pr_debug("GSO!\n");
  403. switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  404. case VIRTIO_NET_HDR_GSO_TCPV4:
  405. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  406. break;
  407. case VIRTIO_NET_HDR_GSO_UDP:
  408. {
  409. static bool warned;
  410. if (!warned) {
  411. warned = true;
  412. netdev_warn(dev,
  413. "host using disabled UFO feature; please fix it\n");
  414. }
  415. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  416. break;
  417. }
  418. case VIRTIO_NET_HDR_GSO_TCPV6:
  419. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  420. break;
  421. default:
  422. net_warn_ratelimited("%s: bad gso type %u.\n",
  423. dev->name, hdr->hdr.gso_type);
  424. goto frame_err;
  425. }
  426. if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  427. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  428. skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
  429. if (skb_shinfo(skb)->gso_size == 0) {
  430. net_warn_ratelimited("%s: zero gso size.\n", dev->name);
  431. goto frame_err;
  432. }
  433. /* Header must be checked, and gso_segs computed. */
  434. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  435. skb_shinfo(skb)->gso_segs = 0;
  436. }
  437. skb_mark_napi_id(skb, &rq->napi);
  438. netif_receive_skb(skb);
  439. return;
  440. frame_err:
  441. dev->stats.rx_frame_errors++;
  442. dev_kfree_skb(skb);
  443. }
  444. static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
  445. {
  446. struct virtnet_info *vi = rq->vq->vdev->priv;
  447. struct sk_buff *skb;
  448. struct skb_vnet_hdr *hdr;
  449. int err;
  450. skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
  451. if (unlikely(!skb))
  452. return -ENOMEM;
  453. skb_put(skb, GOOD_PACKET_LEN);
  454. hdr = skb_vnet_hdr(skb);
  455. sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
  456. sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
  457. skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
  458. err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
  459. if (err < 0)
  460. dev_kfree_skb(skb);
  461. return err;
  462. }
  463. static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
  464. {
  465. struct page *first, *list = NULL;
  466. char *p;
  467. int i, err, offset;
  468. sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
  469. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  470. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  471. first = get_a_page(rq, gfp);
  472. if (!first) {
  473. if (list)
  474. give_pages(rq, list);
  475. return -ENOMEM;
  476. }
  477. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  478. /* chain new page in list head to match sg */
  479. first->private = (unsigned long)list;
  480. list = first;
  481. }
  482. first = get_a_page(rq, gfp);
  483. if (!first) {
  484. give_pages(rq, list);
  485. return -ENOMEM;
  486. }
  487. p = page_address(first);
  488. /* rq->sg[0], rq->sg[1] share the same page */
  489. /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
  490. sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
  491. /* rq->sg[1] for data packet, from offset */
  492. offset = sizeof(struct padded_vnet_hdr);
  493. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  494. /* chain first in list head */
  495. first->private = (unsigned long)list;
  496. err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
  497. first, gfp);
  498. if (err < 0)
  499. give_pages(rq, first);
  500. return err;
  501. }
  502. static unsigned int get_mergeable_buf_len(struct ewma *avg_pkt_len)
  503. {
  504. const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  505. unsigned int len;
  506. len = hdr_len + clamp_t(unsigned int, ewma_read(avg_pkt_len),
  507. GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
  508. return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
  509. }
  510. static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
  511. {
  512. struct page_frag *alloc_frag = &rq->alloc_frag;
  513. char *buf;
  514. unsigned long ctx;
  515. int err;
  516. unsigned int len, hole;
  517. len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
  518. if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
  519. return -ENOMEM;
  520. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  521. ctx = mergeable_buf_to_ctx(buf, len);
  522. get_page(alloc_frag->page);
  523. alloc_frag->offset += len;
  524. hole = alloc_frag->size - alloc_frag->offset;
  525. if (hole < len) {
  526. /* To avoid internal fragmentation, if there is very likely not
  527. * enough space for another buffer, add the remaining space to
  528. * the current buffer. This extra space is not included in
  529. * the truesize stored in ctx.
  530. */
  531. len += hole;
  532. alloc_frag->offset += hole;
  533. }
  534. sg_init_one(rq->sg, buf, len);
  535. err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
  536. if (err < 0)
  537. put_page(virt_to_head_page(buf));
  538. return err;
  539. }
  540. /*
  541. * Returns false if we couldn't fill entirely (OOM).
  542. *
  543. * Normally run in the receive path, but can also be run from ndo_open
  544. * before we're receiving packets, or from refill_work which is
  545. * careful to disable receiving (using napi_disable).
  546. */
  547. static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
  548. {
  549. struct virtnet_info *vi = rq->vq->vdev->priv;
  550. int err;
  551. bool oom;
  552. gfp |= __GFP_COLD;
  553. do {
  554. if (vi->mergeable_rx_bufs)
  555. err = add_recvbuf_mergeable(rq, gfp);
  556. else if (vi->big_packets)
  557. err = add_recvbuf_big(rq, gfp);
  558. else
  559. err = add_recvbuf_small(rq, gfp);
  560. oom = err == -ENOMEM;
  561. if (err)
  562. break;
  563. } while (rq->vq->num_free);
  564. virtqueue_kick(rq->vq);
  565. return !oom;
  566. }
  567. static void skb_recv_done(struct virtqueue *rvq)
  568. {
  569. struct virtnet_info *vi = rvq->vdev->priv;
  570. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  571. /* Schedule NAPI, Suppress further interrupts if successful. */
  572. if (napi_schedule_prep(&rq->napi)) {
  573. virtqueue_disable_cb(rvq);
  574. __napi_schedule(&rq->napi);
  575. }
  576. }
  577. static void virtnet_napi_enable(struct receive_queue *rq)
  578. {
  579. napi_enable(&rq->napi);
  580. /* If all buffers were filled by other side before we napi_enabled, we
  581. * won't get another interrupt, so process any outstanding packets
  582. * now. virtnet_poll wants re-enable the queue, so we disable here.
  583. * We synchronize against interrupts via NAPI_STATE_SCHED */
  584. if (napi_schedule_prep(&rq->napi)) {
  585. virtqueue_disable_cb(rq->vq);
  586. local_bh_disable();
  587. __napi_schedule(&rq->napi);
  588. local_bh_enable();
  589. }
  590. }
  591. static void refill_work(struct work_struct *work)
  592. {
  593. struct virtnet_info *vi =
  594. container_of(work, struct virtnet_info, refill.work);
  595. bool still_empty;
  596. int i;
  597. for (i = 0; i < vi->curr_queue_pairs; i++) {
  598. struct receive_queue *rq = &vi->rq[i];
  599. napi_disable(&rq->napi);
  600. still_empty = !try_fill_recv(rq, GFP_KERNEL);
  601. virtnet_napi_enable(rq);
  602. /* In theory, this can happen: if we don't get any buffers in
  603. * we will *never* try to fill again.
  604. */
  605. if (still_empty)
  606. schedule_delayed_work(&vi->refill, HZ/2);
  607. }
  608. }
  609. static int virtnet_receive(struct receive_queue *rq, int budget)
  610. {
  611. struct virtnet_info *vi = rq->vq->vdev->priv;
  612. unsigned int len, received = 0;
  613. void *buf;
  614. while (received < budget &&
  615. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  616. receive_buf(rq, buf, len);
  617. received++;
  618. }
  619. if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
  620. if (!try_fill_recv(rq, GFP_ATOMIC))
  621. schedule_delayed_work(&vi->refill, 0);
  622. }
  623. return received;
  624. }
  625. static int virtnet_poll(struct napi_struct *napi, int budget)
  626. {
  627. struct receive_queue *rq =
  628. container_of(napi, struct receive_queue, napi);
  629. unsigned int r, received = 0;
  630. again:
  631. received += virtnet_receive(rq, budget - received);
  632. /* Out of packets? */
  633. if (received < budget) {
  634. r = virtqueue_enable_cb_prepare(rq->vq);
  635. napi_complete(napi);
  636. if (unlikely(virtqueue_poll(rq->vq, r)) &&
  637. napi_schedule_prep(napi)) {
  638. virtqueue_disable_cb(rq->vq);
  639. __napi_schedule(napi);
  640. goto again;
  641. }
  642. }
  643. return received;
  644. }
  645. #ifdef CONFIG_NET_RX_BUSY_POLL
  646. /* must be called with local_bh_disable()d */
  647. static int virtnet_busy_poll(struct napi_struct *napi)
  648. {
  649. struct receive_queue *rq =
  650. container_of(napi, struct receive_queue, napi);
  651. struct virtnet_info *vi = rq->vq->vdev->priv;
  652. int r, received = 0, budget = 4;
  653. if (!(vi->status & VIRTIO_NET_S_LINK_UP))
  654. return LL_FLUSH_FAILED;
  655. if (!napi_schedule_prep(napi))
  656. return LL_FLUSH_BUSY;
  657. virtqueue_disable_cb(rq->vq);
  658. again:
  659. received += virtnet_receive(rq, budget);
  660. r = virtqueue_enable_cb_prepare(rq->vq);
  661. clear_bit(NAPI_STATE_SCHED, &napi->state);
  662. if (unlikely(virtqueue_poll(rq->vq, r)) &&
  663. napi_schedule_prep(napi)) {
  664. virtqueue_disable_cb(rq->vq);
  665. if (received < budget) {
  666. budget -= received;
  667. goto again;
  668. } else {
  669. __napi_schedule(napi);
  670. }
  671. }
  672. return received;
  673. }
  674. #endif /* CONFIG_NET_RX_BUSY_POLL */
  675. static int virtnet_open(struct net_device *dev)
  676. {
  677. struct virtnet_info *vi = netdev_priv(dev);
  678. int i;
  679. for (i = 0; i < vi->max_queue_pairs; i++) {
  680. if (i < vi->curr_queue_pairs)
  681. /* Make sure we have some buffers: if oom use wq. */
  682. if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
  683. schedule_delayed_work(&vi->refill, 0);
  684. virtnet_napi_enable(&vi->rq[i]);
  685. }
  686. return 0;
  687. }
  688. static void free_old_xmit_skbs(struct send_queue *sq)
  689. {
  690. struct sk_buff *skb;
  691. unsigned int len;
  692. struct virtnet_info *vi = sq->vq->vdev->priv;
  693. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  694. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  695. pr_debug("Sent skb %p\n", skb);
  696. u64_stats_update_begin(&stats->tx_syncp);
  697. stats->tx_bytes += skb->len;
  698. stats->tx_packets++;
  699. u64_stats_update_end(&stats->tx_syncp);
  700. dev_kfree_skb_any(skb);
  701. }
  702. }
  703. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  704. {
  705. struct skb_vnet_hdr *hdr;
  706. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  707. struct virtnet_info *vi = sq->vq->vdev->priv;
  708. unsigned num_sg;
  709. unsigned hdr_len;
  710. bool can_push;
  711. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  712. if (vi->mergeable_rx_bufs)
  713. hdr_len = sizeof hdr->mhdr;
  714. else
  715. hdr_len = sizeof hdr->hdr;
  716. can_push = vi->any_header_sg &&
  717. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  718. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  719. /* Even if we can, don't push here yet as this would skew
  720. * csum_start offset below. */
  721. if (can_push)
  722. hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
  723. else
  724. hdr = skb_vnet_hdr(skb);
  725. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  726. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  727. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  728. hdr->hdr.csum_offset = skb->csum_offset;
  729. } else {
  730. hdr->hdr.flags = 0;
  731. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  732. }
  733. if (skb_is_gso(skb)) {
  734. hdr->hdr.hdr_len = skb_headlen(skb);
  735. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  736. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  737. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  738. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  739. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  740. else
  741. BUG();
  742. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  743. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  744. } else {
  745. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  746. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  747. }
  748. if (vi->mergeable_rx_bufs)
  749. hdr->mhdr.num_buffers = 0;
  750. sg_init_table(sq->sg, MAX_SKB_FRAGS + 2);
  751. if (can_push) {
  752. __skb_push(skb, hdr_len);
  753. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  754. /* Pull header back to avoid skew in tx bytes calculations. */
  755. __skb_pull(skb, hdr_len);
  756. } else {
  757. sg_set_buf(sq->sg, hdr, hdr_len);
  758. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
  759. }
  760. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  761. }
  762. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  763. {
  764. struct virtnet_info *vi = netdev_priv(dev);
  765. int qnum = skb_get_queue_mapping(skb);
  766. struct send_queue *sq = &vi->sq[qnum];
  767. int err;
  768. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  769. bool kick = !skb->xmit_more;
  770. /* Free up any pending old buffers before queueing new ones. */
  771. free_old_xmit_skbs(sq);
  772. /* Try to transmit */
  773. err = xmit_skb(sq, skb);
  774. /* This should not happen! */
  775. if (unlikely(err)) {
  776. dev->stats.tx_fifo_errors++;
  777. if (net_ratelimit())
  778. dev_warn(&dev->dev,
  779. "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
  780. dev->stats.tx_dropped++;
  781. dev_kfree_skb_any(skb);
  782. return NETDEV_TX_OK;
  783. }
  784. /* Don't wait up for transmitted skbs to be freed. */
  785. skb_orphan(skb);
  786. nf_reset(skb);
  787. /* Apparently nice girls don't return TX_BUSY; stop the queue
  788. * before it gets out of hand. Naturally, this wastes entries. */
  789. if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
  790. netif_stop_subqueue(dev, qnum);
  791. if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  792. /* More just got used, free them then recheck. */
  793. free_old_xmit_skbs(sq);
  794. if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
  795. netif_start_subqueue(dev, qnum);
  796. virtqueue_disable_cb(sq->vq);
  797. }
  798. }
  799. }
  800. if (kick || netif_xmit_stopped(txq))
  801. virtqueue_kick(sq->vq);
  802. return NETDEV_TX_OK;
  803. }
  804. /*
  805. * Send command via the control virtqueue and check status. Commands
  806. * supported by the hypervisor, as indicated by feature bits, should
  807. * never fail unless improperly formatted.
  808. */
  809. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  810. struct scatterlist *out)
  811. {
  812. struct scatterlist *sgs[4], hdr, stat;
  813. struct virtio_net_ctrl_hdr ctrl;
  814. virtio_net_ctrl_ack status = ~0;
  815. unsigned out_num = 0, tmp;
  816. /* Caller should know better */
  817. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  818. ctrl.class = class;
  819. ctrl.cmd = cmd;
  820. /* Add header */
  821. sg_init_one(&hdr, &ctrl, sizeof(ctrl));
  822. sgs[out_num++] = &hdr;
  823. if (out)
  824. sgs[out_num++] = out;
  825. /* Add return status. */
  826. sg_init_one(&stat, &status, sizeof(status));
  827. sgs[out_num] = &stat;
  828. BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
  829. virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
  830. if (unlikely(!virtqueue_kick(vi->cvq)))
  831. return status == VIRTIO_NET_OK;
  832. /* Spin for a response, the kick causes an ioport write, trapping
  833. * into the hypervisor, so the request should be handled immediately.
  834. */
  835. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  836. !virtqueue_is_broken(vi->cvq))
  837. cpu_relax();
  838. return status == VIRTIO_NET_OK;
  839. }
  840. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  841. {
  842. struct virtnet_info *vi = netdev_priv(dev);
  843. struct virtio_device *vdev = vi->vdev;
  844. int ret;
  845. struct sockaddr *addr = p;
  846. struct scatterlist sg;
  847. ret = eth_prepare_mac_addr_change(dev, p);
  848. if (ret)
  849. return ret;
  850. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  851. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  852. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  853. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  854. dev_warn(&vdev->dev,
  855. "Failed to set mac address by vq command.\n");
  856. return -EINVAL;
  857. }
  858. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  859. unsigned int i;
  860. /* Naturally, this has an atomicity problem. */
  861. for (i = 0; i < dev->addr_len; i++)
  862. virtio_cwrite8(vdev,
  863. offsetof(struct virtio_net_config, mac) +
  864. i, addr->sa_data[i]);
  865. }
  866. eth_commit_mac_addr_change(dev, p);
  867. return 0;
  868. }
  869. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  870. struct rtnl_link_stats64 *tot)
  871. {
  872. struct virtnet_info *vi = netdev_priv(dev);
  873. int cpu;
  874. unsigned int start;
  875. for_each_possible_cpu(cpu) {
  876. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  877. u64 tpackets, tbytes, rpackets, rbytes;
  878. do {
  879. start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
  880. tpackets = stats->tx_packets;
  881. tbytes = stats->tx_bytes;
  882. } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
  883. do {
  884. start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
  885. rpackets = stats->rx_packets;
  886. rbytes = stats->rx_bytes;
  887. } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
  888. tot->rx_packets += rpackets;
  889. tot->tx_packets += tpackets;
  890. tot->rx_bytes += rbytes;
  891. tot->tx_bytes += tbytes;
  892. }
  893. tot->tx_dropped = dev->stats.tx_dropped;
  894. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  895. tot->rx_dropped = dev->stats.rx_dropped;
  896. tot->rx_length_errors = dev->stats.rx_length_errors;
  897. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  898. return tot;
  899. }
  900. #ifdef CONFIG_NET_POLL_CONTROLLER
  901. static void virtnet_netpoll(struct net_device *dev)
  902. {
  903. struct virtnet_info *vi = netdev_priv(dev);
  904. int i;
  905. for (i = 0; i < vi->curr_queue_pairs; i++)
  906. napi_schedule(&vi->rq[i].napi);
  907. }
  908. #endif
  909. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  910. {
  911. rtnl_lock();
  912. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  913. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  914. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  915. rtnl_unlock();
  916. }
  917. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  918. {
  919. struct scatterlist sg;
  920. struct virtio_net_ctrl_mq s;
  921. struct net_device *dev = vi->dev;
  922. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  923. return 0;
  924. s.virtqueue_pairs = queue_pairs;
  925. sg_init_one(&sg, &s, sizeof(s));
  926. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  927. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  928. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  929. queue_pairs);
  930. return -EINVAL;
  931. } else {
  932. vi->curr_queue_pairs = queue_pairs;
  933. /* virtnet_open() will refill when device is going to up. */
  934. if (dev->flags & IFF_UP)
  935. schedule_delayed_work(&vi->refill, 0);
  936. }
  937. return 0;
  938. }
  939. static int virtnet_close(struct net_device *dev)
  940. {
  941. struct virtnet_info *vi = netdev_priv(dev);
  942. int i;
  943. /* Make sure refill_work doesn't re-enable napi! */
  944. cancel_delayed_work_sync(&vi->refill);
  945. for (i = 0; i < vi->max_queue_pairs; i++)
  946. napi_disable(&vi->rq[i].napi);
  947. return 0;
  948. }
  949. static void virtnet_set_rx_mode(struct net_device *dev)
  950. {
  951. struct virtnet_info *vi = netdev_priv(dev);
  952. struct scatterlist sg[2];
  953. u8 promisc, allmulti;
  954. struct virtio_net_ctrl_mac *mac_data;
  955. struct netdev_hw_addr *ha;
  956. int uc_count;
  957. int mc_count;
  958. void *buf;
  959. int i;
  960. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  961. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  962. return;
  963. promisc = ((dev->flags & IFF_PROMISC) != 0);
  964. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  965. sg_init_one(sg, &promisc, sizeof(promisc));
  966. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  967. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  968. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  969. promisc ? "en" : "dis");
  970. sg_init_one(sg, &allmulti, sizeof(allmulti));
  971. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  972. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  973. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  974. allmulti ? "en" : "dis");
  975. uc_count = netdev_uc_count(dev);
  976. mc_count = netdev_mc_count(dev);
  977. /* MAC filter - use one buffer for both lists */
  978. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  979. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  980. mac_data = buf;
  981. if (!buf)
  982. return;
  983. sg_init_table(sg, 2);
  984. /* Store the unicast list and count in the front of the buffer */
  985. mac_data->entries = uc_count;
  986. i = 0;
  987. netdev_for_each_uc_addr(ha, dev)
  988. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  989. sg_set_buf(&sg[0], mac_data,
  990. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  991. /* multicast list and count fill the end */
  992. mac_data = (void *)&mac_data->macs[uc_count][0];
  993. mac_data->entries = mc_count;
  994. i = 0;
  995. netdev_for_each_mc_addr(ha, dev)
  996. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  997. sg_set_buf(&sg[1], mac_data,
  998. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  999. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1000. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  1001. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  1002. kfree(buf);
  1003. }
  1004. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  1005. __be16 proto, u16 vid)
  1006. {
  1007. struct virtnet_info *vi = netdev_priv(dev);
  1008. struct scatterlist sg;
  1009. sg_init_one(&sg, &vid, sizeof(vid));
  1010. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1011. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  1012. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  1013. return 0;
  1014. }
  1015. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  1016. __be16 proto, u16 vid)
  1017. {
  1018. struct virtnet_info *vi = netdev_priv(dev);
  1019. struct scatterlist sg;
  1020. sg_init_one(&sg, &vid, sizeof(vid));
  1021. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1022. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  1023. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  1024. return 0;
  1025. }
  1026. static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
  1027. {
  1028. int i;
  1029. if (vi->affinity_hint_set) {
  1030. for (i = 0; i < vi->max_queue_pairs; i++) {
  1031. virtqueue_set_affinity(vi->rq[i].vq, -1);
  1032. virtqueue_set_affinity(vi->sq[i].vq, -1);
  1033. }
  1034. vi->affinity_hint_set = false;
  1035. }
  1036. }
  1037. static void virtnet_set_affinity(struct virtnet_info *vi)
  1038. {
  1039. int i;
  1040. int cpu;
  1041. /* In multiqueue mode, when the number of cpu is equal to the number of
  1042. * queue pairs, we let the queue pairs to be private to one cpu by
  1043. * setting the affinity hint to eliminate the contention.
  1044. */
  1045. if (vi->curr_queue_pairs == 1 ||
  1046. vi->max_queue_pairs != num_online_cpus()) {
  1047. virtnet_clean_affinity(vi, -1);
  1048. return;
  1049. }
  1050. i = 0;
  1051. for_each_online_cpu(cpu) {
  1052. virtqueue_set_affinity(vi->rq[i].vq, cpu);
  1053. virtqueue_set_affinity(vi->sq[i].vq, cpu);
  1054. netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
  1055. i++;
  1056. }
  1057. vi->affinity_hint_set = true;
  1058. }
  1059. static int virtnet_cpu_callback(struct notifier_block *nfb,
  1060. unsigned long action, void *hcpu)
  1061. {
  1062. struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
  1063. switch(action & ~CPU_TASKS_FROZEN) {
  1064. case CPU_ONLINE:
  1065. case CPU_DOWN_FAILED:
  1066. case CPU_DEAD:
  1067. virtnet_set_affinity(vi);
  1068. break;
  1069. case CPU_DOWN_PREPARE:
  1070. virtnet_clean_affinity(vi, (long)hcpu);
  1071. break;
  1072. default:
  1073. break;
  1074. }
  1075. return NOTIFY_OK;
  1076. }
  1077. static void virtnet_get_ringparam(struct net_device *dev,
  1078. struct ethtool_ringparam *ring)
  1079. {
  1080. struct virtnet_info *vi = netdev_priv(dev);
  1081. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  1082. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  1083. ring->rx_pending = ring->rx_max_pending;
  1084. ring->tx_pending = ring->tx_max_pending;
  1085. }
  1086. static void virtnet_get_drvinfo(struct net_device *dev,
  1087. struct ethtool_drvinfo *info)
  1088. {
  1089. struct virtnet_info *vi = netdev_priv(dev);
  1090. struct virtio_device *vdev = vi->vdev;
  1091. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1092. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  1093. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  1094. }
  1095. /* TODO: Eliminate OOO packets during switching */
  1096. static int virtnet_set_channels(struct net_device *dev,
  1097. struct ethtool_channels *channels)
  1098. {
  1099. struct virtnet_info *vi = netdev_priv(dev);
  1100. u16 queue_pairs = channels->combined_count;
  1101. int err;
  1102. /* We don't support separate rx/tx channels.
  1103. * We don't allow setting 'other' channels.
  1104. */
  1105. if (channels->rx_count || channels->tx_count || channels->other_count)
  1106. return -EINVAL;
  1107. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  1108. return -EINVAL;
  1109. get_online_cpus();
  1110. err = virtnet_set_queues(vi, queue_pairs);
  1111. if (!err) {
  1112. netif_set_real_num_tx_queues(dev, queue_pairs);
  1113. netif_set_real_num_rx_queues(dev, queue_pairs);
  1114. virtnet_set_affinity(vi);
  1115. }
  1116. put_online_cpus();
  1117. return err;
  1118. }
  1119. static void virtnet_get_channels(struct net_device *dev,
  1120. struct ethtool_channels *channels)
  1121. {
  1122. struct virtnet_info *vi = netdev_priv(dev);
  1123. channels->combined_count = vi->curr_queue_pairs;
  1124. channels->max_combined = vi->max_queue_pairs;
  1125. channels->max_other = 0;
  1126. channels->rx_count = 0;
  1127. channels->tx_count = 0;
  1128. channels->other_count = 0;
  1129. }
  1130. static const struct ethtool_ops virtnet_ethtool_ops = {
  1131. .get_drvinfo = virtnet_get_drvinfo,
  1132. .get_link = ethtool_op_get_link,
  1133. .get_ringparam = virtnet_get_ringparam,
  1134. .set_channels = virtnet_set_channels,
  1135. .get_channels = virtnet_get_channels,
  1136. };
  1137. #define MIN_MTU 68
  1138. #define MAX_MTU 65535
  1139. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  1140. {
  1141. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  1142. return -EINVAL;
  1143. dev->mtu = new_mtu;
  1144. return 0;
  1145. }
  1146. static const struct net_device_ops virtnet_netdev = {
  1147. .ndo_open = virtnet_open,
  1148. .ndo_stop = virtnet_close,
  1149. .ndo_start_xmit = start_xmit,
  1150. .ndo_validate_addr = eth_validate_addr,
  1151. .ndo_set_mac_address = virtnet_set_mac_address,
  1152. .ndo_set_rx_mode = virtnet_set_rx_mode,
  1153. .ndo_change_mtu = virtnet_change_mtu,
  1154. .ndo_get_stats64 = virtnet_stats,
  1155. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  1156. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  1157. #ifdef CONFIG_NET_POLL_CONTROLLER
  1158. .ndo_poll_controller = virtnet_netpoll,
  1159. #endif
  1160. #ifdef CONFIG_NET_RX_BUSY_POLL
  1161. .ndo_busy_poll = virtnet_busy_poll,
  1162. #endif
  1163. };
  1164. static void virtnet_config_changed_work(struct work_struct *work)
  1165. {
  1166. struct virtnet_info *vi =
  1167. container_of(work, struct virtnet_info, config_work);
  1168. u16 v;
  1169. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  1170. struct virtio_net_config, status, &v) < 0)
  1171. return;
  1172. if (v & VIRTIO_NET_S_ANNOUNCE) {
  1173. netdev_notify_peers(vi->dev);
  1174. virtnet_ack_link_announce(vi);
  1175. }
  1176. /* Ignore unknown (future) status bits */
  1177. v &= VIRTIO_NET_S_LINK_UP;
  1178. if (vi->status == v)
  1179. return;
  1180. vi->status = v;
  1181. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  1182. netif_carrier_on(vi->dev);
  1183. netif_tx_wake_all_queues(vi->dev);
  1184. } else {
  1185. netif_carrier_off(vi->dev);
  1186. netif_tx_stop_all_queues(vi->dev);
  1187. }
  1188. }
  1189. static void virtnet_config_changed(struct virtio_device *vdev)
  1190. {
  1191. struct virtnet_info *vi = vdev->priv;
  1192. schedule_work(&vi->config_work);
  1193. }
  1194. static void virtnet_free_queues(struct virtnet_info *vi)
  1195. {
  1196. int i;
  1197. for (i = 0; i < vi->max_queue_pairs; i++)
  1198. netif_napi_del(&vi->rq[i].napi);
  1199. kfree(vi->rq);
  1200. kfree(vi->sq);
  1201. }
  1202. static void free_receive_bufs(struct virtnet_info *vi)
  1203. {
  1204. int i;
  1205. for (i = 0; i < vi->max_queue_pairs; i++) {
  1206. while (vi->rq[i].pages)
  1207. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  1208. }
  1209. }
  1210. static void free_receive_page_frags(struct virtnet_info *vi)
  1211. {
  1212. int i;
  1213. for (i = 0; i < vi->max_queue_pairs; i++)
  1214. if (vi->rq[i].alloc_frag.page)
  1215. put_page(vi->rq[i].alloc_frag.page);
  1216. }
  1217. static void free_unused_bufs(struct virtnet_info *vi)
  1218. {
  1219. void *buf;
  1220. int i;
  1221. for (i = 0; i < vi->max_queue_pairs; i++) {
  1222. struct virtqueue *vq = vi->sq[i].vq;
  1223. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  1224. dev_kfree_skb(buf);
  1225. }
  1226. for (i = 0; i < vi->max_queue_pairs; i++) {
  1227. struct virtqueue *vq = vi->rq[i].vq;
  1228. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1229. if (vi->mergeable_rx_bufs) {
  1230. unsigned long ctx = (unsigned long)buf;
  1231. void *base = mergeable_ctx_to_buf_address(ctx);
  1232. put_page(virt_to_head_page(base));
  1233. } else if (vi->big_packets) {
  1234. give_pages(&vi->rq[i], buf);
  1235. } else {
  1236. dev_kfree_skb(buf);
  1237. }
  1238. }
  1239. }
  1240. }
  1241. static void virtnet_del_vqs(struct virtnet_info *vi)
  1242. {
  1243. struct virtio_device *vdev = vi->vdev;
  1244. virtnet_clean_affinity(vi, -1);
  1245. vdev->config->del_vqs(vdev);
  1246. virtnet_free_queues(vi);
  1247. }
  1248. static int virtnet_find_vqs(struct virtnet_info *vi)
  1249. {
  1250. vq_callback_t **callbacks;
  1251. struct virtqueue **vqs;
  1252. int ret = -ENOMEM;
  1253. int i, total_vqs;
  1254. const char **names;
  1255. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  1256. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  1257. * possible control vq.
  1258. */
  1259. total_vqs = vi->max_queue_pairs * 2 +
  1260. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  1261. /* Allocate space for find_vqs parameters */
  1262. vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
  1263. if (!vqs)
  1264. goto err_vq;
  1265. callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
  1266. if (!callbacks)
  1267. goto err_callback;
  1268. names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
  1269. if (!names)
  1270. goto err_names;
  1271. /* Parameters for control virtqueue, if any */
  1272. if (vi->has_cvq) {
  1273. callbacks[total_vqs - 1] = NULL;
  1274. names[total_vqs - 1] = "control";
  1275. }
  1276. /* Allocate/initialize parameters for send/receive virtqueues */
  1277. for (i = 0; i < vi->max_queue_pairs; i++) {
  1278. callbacks[rxq2vq(i)] = skb_recv_done;
  1279. callbacks[txq2vq(i)] = skb_xmit_done;
  1280. sprintf(vi->rq[i].name, "input.%d", i);
  1281. sprintf(vi->sq[i].name, "output.%d", i);
  1282. names[rxq2vq(i)] = vi->rq[i].name;
  1283. names[txq2vq(i)] = vi->sq[i].name;
  1284. }
  1285. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  1286. names);
  1287. if (ret)
  1288. goto err_find;
  1289. if (vi->has_cvq) {
  1290. vi->cvq = vqs[total_vqs - 1];
  1291. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  1292. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1293. }
  1294. for (i = 0; i < vi->max_queue_pairs; i++) {
  1295. vi->rq[i].vq = vqs[rxq2vq(i)];
  1296. vi->sq[i].vq = vqs[txq2vq(i)];
  1297. }
  1298. kfree(names);
  1299. kfree(callbacks);
  1300. kfree(vqs);
  1301. return 0;
  1302. err_find:
  1303. kfree(names);
  1304. err_names:
  1305. kfree(callbacks);
  1306. err_callback:
  1307. kfree(vqs);
  1308. err_vq:
  1309. return ret;
  1310. }
  1311. static int virtnet_alloc_queues(struct virtnet_info *vi)
  1312. {
  1313. int i;
  1314. vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
  1315. if (!vi->sq)
  1316. goto err_sq;
  1317. vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
  1318. if (!vi->rq)
  1319. goto err_rq;
  1320. INIT_DELAYED_WORK(&vi->refill, refill_work);
  1321. for (i = 0; i < vi->max_queue_pairs; i++) {
  1322. vi->rq[i].pages = NULL;
  1323. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  1324. napi_weight);
  1325. napi_hash_add(&vi->rq[i].napi);
  1326. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  1327. ewma_init(&vi->rq[i].mrg_avg_pkt_len, 1, RECEIVE_AVG_WEIGHT);
  1328. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  1329. }
  1330. return 0;
  1331. err_rq:
  1332. kfree(vi->sq);
  1333. err_sq:
  1334. return -ENOMEM;
  1335. }
  1336. static int init_vqs(struct virtnet_info *vi)
  1337. {
  1338. int ret;
  1339. /* Allocate send & receive queues */
  1340. ret = virtnet_alloc_queues(vi);
  1341. if (ret)
  1342. goto err;
  1343. ret = virtnet_find_vqs(vi);
  1344. if (ret)
  1345. goto err_free;
  1346. get_online_cpus();
  1347. virtnet_set_affinity(vi);
  1348. put_online_cpus();
  1349. return 0;
  1350. err_free:
  1351. virtnet_free_queues(vi);
  1352. err:
  1353. return ret;
  1354. }
  1355. #ifdef CONFIG_SYSFS
  1356. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  1357. struct rx_queue_attribute *attribute, char *buf)
  1358. {
  1359. struct virtnet_info *vi = netdev_priv(queue->dev);
  1360. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  1361. struct ewma *avg;
  1362. BUG_ON(queue_index >= vi->max_queue_pairs);
  1363. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  1364. return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
  1365. }
  1366. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  1367. __ATTR_RO(mergeable_rx_buffer_size);
  1368. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  1369. &mergeable_rx_buffer_size_attribute.attr,
  1370. NULL
  1371. };
  1372. static const struct attribute_group virtio_net_mrg_rx_group = {
  1373. .name = "virtio_net",
  1374. .attrs = virtio_net_mrg_rx_attrs
  1375. };
  1376. #endif
  1377. static int virtnet_probe(struct virtio_device *vdev)
  1378. {
  1379. int i, err;
  1380. struct net_device *dev;
  1381. struct virtnet_info *vi;
  1382. u16 max_queue_pairs;
  1383. /* Find if host supports multiqueue virtio_net device */
  1384. err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
  1385. struct virtio_net_config,
  1386. max_virtqueue_pairs, &max_queue_pairs);
  1387. /* We need at least 2 queue's */
  1388. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  1389. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  1390. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1391. max_queue_pairs = 1;
  1392. /* Allocate ourselves a network device with room for our info */
  1393. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  1394. if (!dev)
  1395. return -ENOMEM;
  1396. /* Set up network device as normal. */
  1397. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1398. dev->netdev_ops = &virtnet_netdev;
  1399. dev->features = NETIF_F_HIGHDMA;
  1400. dev->ethtool_ops = &virtnet_ethtool_ops;
  1401. SET_NETDEV_DEV(dev, &vdev->dev);
  1402. /* Do we support "hardware" checksums? */
  1403. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  1404. /* This opens up the world of extra features. */
  1405. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  1406. if (csum)
  1407. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  1408. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  1409. dev->hw_features |= NETIF_F_TSO
  1410. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  1411. }
  1412. /* Individual feature bits: what can host handle? */
  1413. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  1414. dev->hw_features |= NETIF_F_TSO;
  1415. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  1416. dev->hw_features |= NETIF_F_TSO6;
  1417. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  1418. dev->hw_features |= NETIF_F_TSO_ECN;
  1419. if (gso)
  1420. dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
  1421. /* (!csum && gso) case will be fixed by register_netdev() */
  1422. }
  1423. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  1424. dev->features |= NETIF_F_RXCSUM;
  1425. dev->vlan_features = dev->features;
  1426. /* Configuration may specify what MAC to use. Otherwise random. */
  1427. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  1428. virtio_cread_bytes(vdev,
  1429. offsetof(struct virtio_net_config, mac),
  1430. dev->dev_addr, dev->addr_len);
  1431. else
  1432. eth_hw_addr_random(dev);
  1433. /* Set up our device-specific information */
  1434. vi = netdev_priv(dev);
  1435. vi->dev = dev;
  1436. vi->vdev = vdev;
  1437. vdev->priv = vi;
  1438. vi->stats = alloc_percpu(struct virtnet_stats);
  1439. err = -ENOMEM;
  1440. if (vi->stats == NULL)
  1441. goto free;
  1442. for_each_possible_cpu(i) {
  1443. struct virtnet_stats *virtnet_stats;
  1444. virtnet_stats = per_cpu_ptr(vi->stats, i);
  1445. u64_stats_init(&virtnet_stats->tx_syncp);
  1446. u64_stats_init(&virtnet_stats->rx_syncp);
  1447. }
  1448. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  1449. /* If we can receive ANY GSO packets, we must allocate large ones. */
  1450. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  1451. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  1452. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  1453. vi->big_packets = true;
  1454. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  1455. vi->mergeable_rx_bufs = true;
  1456. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
  1457. vi->any_header_sg = true;
  1458. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1459. vi->has_cvq = true;
  1460. if (vi->any_header_sg) {
  1461. if (vi->mergeable_rx_bufs)
  1462. dev->needed_headroom = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  1463. else
  1464. dev->needed_headroom = sizeof(struct virtio_net_hdr);
  1465. }
  1466. /* Use single tx/rx queue pair as default */
  1467. vi->curr_queue_pairs = 1;
  1468. vi->max_queue_pairs = max_queue_pairs;
  1469. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  1470. err = init_vqs(vi);
  1471. if (err)
  1472. goto free_stats;
  1473. #ifdef CONFIG_SYSFS
  1474. if (vi->mergeable_rx_bufs)
  1475. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  1476. #endif
  1477. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  1478. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  1479. err = register_netdev(dev);
  1480. if (err) {
  1481. pr_debug("virtio_net: registering device failed\n");
  1482. goto free_vqs;
  1483. }
  1484. virtio_device_ready(vdev);
  1485. /* Last of all, set up some receive buffers. */
  1486. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1487. try_fill_recv(&vi->rq[i], GFP_KERNEL);
  1488. /* If we didn't even get one input buffer, we're useless. */
  1489. if (vi->rq[i].vq->num_free ==
  1490. virtqueue_get_vring_size(vi->rq[i].vq)) {
  1491. free_unused_bufs(vi);
  1492. err = -ENOMEM;
  1493. goto free_recv_bufs;
  1494. }
  1495. }
  1496. vi->nb.notifier_call = &virtnet_cpu_callback;
  1497. err = register_hotcpu_notifier(&vi->nb);
  1498. if (err) {
  1499. pr_debug("virtio_net: registering cpu notifier failed\n");
  1500. goto free_recv_bufs;
  1501. }
  1502. /* Assume link up if device can't report link status,
  1503. otherwise get link status from config. */
  1504. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  1505. netif_carrier_off(dev);
  1506. schedule_work(&vi->config_work);
  1507. } else {
  1508. vi->status = VIRTIO_NET_S_LINK_UP;
  1509. netif_carrier_on(dev);
  1510. }
  1511. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  1512. dev->name, max_queue_pairs);
  1513. return 0;
  1514. free_recv_bufs:
  1515. vi->vdev->config->reset(vdev);
  1516. free_receive_bufs(vi);
  1517. unregister_netdev(dev);
  1518. free_vqs:
  1519. cancel_delayed_work_sync(&vi->refill);
  1520. free_receive_page_frags(vi);
  1521. virtnet_del_vqs(vi);
  1522. free_stats:
  1523. free_percpu(vi->stats);
  1524. free:
  1525. free_netdev(dev);
  1526. return err;
  1527. }
  1528. static void remove_vq_common(struct virtnet_info *vi)
  1529. {
  1530. vi->vdev->config->reset(vi->vdev);
  1531. /* Free unused buffers in both send and recv, if any. */
  1532. free_unused_bufs(vi);
  1533. free_receive_bufs(vi);
  1534. free_receive_page_frags(vi);
  1535. virtnet_del_vqs(vi);
  1536. }
  1537. static void virtnet_remove(struct virtio_device *vdev)
  1538. {
  1539. struct virtnet_info *vi = vdev->priv;
  1540. unregister_hotcpu_notifier(&vi->nb);
  1541. /* Make sure no work handler is accessing the device. */
  1542. flush_work(&vi->config_work);
  1543. unregister_netdev(vi->dev);
  1544. remove_vq_common(vi);
  1545. free_percpu(vi->stats);
  1546. free_netdev(vi->dev);
  1547. }
  1548. #ifdef CONFIG_PM_SLEEP
  1549. static int virtnet_freeze(struct virtio_device *vdev)
  1550. {
  1551. struct virtnet_info *vi = vdev->priv;
  1552. int i;
  1553. unregister_hotcpu_notifier(&vi->nb);
  1554. /* Make sure no work handler is accessing the device */
  1555. flush_work(&vi->config_work);
  1556. netif_device_detach(vi->dev);
  1557. cancel_delayed_work_sync(&vi->refill);
  1558. if (netif_running(vi->dev)) {
  1559. for (i = 0; i < vi->max_queue_pairs; i++) {
  1560. napi_disable(&vi->rq[i].napi);
  1561. napi_hash_del(&vi->rq[i].napi);
  1562. netif_napi_del(&vi->rq[i].napi);
  1563. }
  1564. }
  1565. remove_vq_common(vi);
  1566. return 0;
  1567. }
  1568. static int virtnet_restore(struct virtio_device *vdev)
  1569. {
  1570. struct virtnet_info *vi = vdev->priv;
  1571. int err, i;
  1572. err = init_vqs(vi);
  1573. if (err)
  1574. return err;
  1575. virtio_device_ready(vdev);
  1576. if (netif_running(vi->dev)) {
  1577. for (i = 0; i < vi->curr_queue_pairs; i++)
  1578. if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
  1579. schedule_delayed_work(&vi->refill, 0);
  1580. for (i = 0; i < vi->max_queue_pairs; i++)
  1581. virtnet_napi_enable(&vi->rq[i]);
  1582. }
  1583. netif_device_attach(vi->dev);
  1584. rtnl_lock();
  1585. virtnet_set_queues(vi, vi->curr_queue_pairs);
  1586. rtnl_unlock();
  1587. err = register_hotcpu_notifier(&vi->nb);
  1588. if (err)
  1589. return err;
  1590. return 0;
  1591. }
  1592. #endif
  1593. static struct virtio_device_id id_table[] = {
  1594. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1595. { 0 },
  1596. };
  1597. static unsigned int features[] = {
  1598. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  1599. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  1600. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_TSO6,
  1601. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  1602. VIRTIO_NET_F_GUEST_ECN,
  1603. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  1604. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  1605. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
  1606. VIRTIO_NET_F_CTRL_MAC_ADDR,
  1607. VIRTIO_F_ANY_LAYOUT,
  1608. };
  1609. static struct virtio_driver virtio_net_driver = {
  1610. .feature_table = features,
  1611. .feature_table_size = ARRAY_SIZE(features),
  1612. .driver.name = KBUILD_MODNAME,
  1613. .driver.owner = THIS_MODULE,
  1614. .id_table = id_table,
  1615. .probe = virtnet_probe,
  1616. .remove = virtnet_remove,
  1617. .config_changed = virtnet_config_changed,
  1618. #ifdef CONFIG_PM_SLEEP
  1619. .freeze = virtnet_freeze,
  1620. .restore = virtnet_restore,
  1621. #endif
  1622. };
  1623. module_virtio_driver(virtio_net_driver);
  1624. MODULE_DEVICE_TABLE(virtio, id_table);
  1625. MODULE_DESCRIPTION("Virtio network driver");
  1626. MODULE_LICENSE("GPL");