virtio_net.c 52 KB

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