virtio_net.c 52 KB

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