virtio_net.c 41 KB

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