virtio_net.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. };
  54. /* Internal representation of a receive virtqueue */
  55. struct receive_queue {
  56. /* Virtqueue associated with this receive_queue */
  57. struct virtqueue *vq;
  58. struct napi_struct napi;
  59. /* Number of input buffers, and max we've ever had. */
  60. unsigned int num, max;
  61. /* Chain pages by the private ptr. */
  62. struct page *pages;
  63. /* RX: fragments + linear part + virtio header */
  64. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  65. };
  66. struct virtnet_info {
  67. struct virtio_device *vdev;
  68. struct virtqueue *cvq;
  69. struct net_device *dev;
  70. struct send_queue sq;
  71. struct receive_queue rq;
  72. unsigned int status;
  73. /* I like... big packets and I cannot lie! */
  74. bool big_packets;
  75. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  76. bool mergeable_rx_bufs;
  77. /* enable config space updates */
  78. bool config_enable;
  79. /* Active statistics */
  80. struct virtnet_stats __percpu *stats;
  81. /* Work struct for refilling if we run low on memory. */
  82. struct delayed_work refill;
  83. /* Work struct for config space updates */
  84. struct work_struct config_work;
  85. /* Lock for config space updates */
  86. struct mutex config_lock;
  87. };
  88. struct skb_vnet_hdr {
  89. union {
  90. struct virtio_net_hdr hdr;
  91. struct virtio_net_hdr_mrg_rxbuf mhdr;
  92. };
  93. unsigned int num_sg;
  94. };
  95. struct padded_vnet_hdr {
  96. struct virtio_net_hdr hdr;
  97. /*
  98. * virtio_net_hdr should be in a separated sg buffer because of a
  99. * QEMU bug, and data sg buffer shares same page with this header sg.
  100. * This padding makes next sg 16 byte aligned after virtio_net_hdr.
  101. */
  102. char padding[6];
  103. };
  104. static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
  105. {
  106. return (struct skb_vnet_hdr *)skb->cb;
  107. }
  108. /*
  109. * private is used to chain pages for big packets, put the whole
  110. * most recent used list in the beginning for reuse
  111. */
  112. static void give_pages(struct receive_queue *rq, struct page *page)
  113. {
  114. struct page *end;
  115. /* Find end of list, sew whole thing into vi->rq.pages. */
  116. for (end = page; end->private; end = (struct page *)end->private);
  117. end->private = (unsigned long)rq->pages;
  118. rq->pages = page;
  119. }
  120. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  121. {
  122. struct page *p = rq->pages;
  123. if (p) {
  124. rq->pages = (struct page *)p->private;
  125. /* clear private here, it is used to chain pages */
  126. p->private = 0;
  127. } else
  128. p = alloc_page(gfp_mask);
  129. return p;
  130. }
  131. static void skb_xmit_done(struct virtqueue *vq)
  132. {
  133. struct virtnet_info *vi = vq->vdev->priv;
  134. /* Suppress further interrupts. */
  135. virtqueue_disable_cb(vq);
  136. /* We were probably waiting for more output buffers. */
  137. netif_wake_queue(vi->dev);
  138. }
  139. static void set_skb_frag(struct sk_buff *skb, struct page *page,
  140. unsigned int offset, unsigned int *len)
  141. {
  142. int size = min((unsigned)PAGE_SIZE - offset, *len);
  143. int i = skb_shinfo(skb)->nr_frags;
  144. __skb_fill_page_desc(skb, i, page, offset, size);
  145. skb->data_len += size;
  146. skb->len += size;
  147. skb->truesize += PAGE_SIZE;
  148. skb_shinfo(skb)->nr_frags++;
  149. *len -= size;
  150. }
  151. /* Called from bottom half context */
  152. static struct sk_buff *page_to_skb(struct receive_queue *rq,
  153. struct page *page, unsigned int len)
  154. {
  155. struct virtnet_info *vi = rq->vq->vdev->priv;
  156. struct sk_buff *skb;
  157. struct skb_vnet_hdr *hdr;
  158. unsigned int copy, hdr_len, offset;
  159. char *p;
  160. p = page_address(page);
  161. /* copy small packet so we can reuse these pages for small data */
  162. skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
  163. if (unlikely(!skb))
  164. return NULL;
  165. hdr = skb_vnet_hdr(skb);
  166. if (vi->mergeable_rx_bufs) {
  167. hdr_len = sizeof hdr->mhdr;
  168. offset = hdr_len;
  169. } else {
  170. hdr_len = sizeof hdr->hdr;
  171. offset = sizeof(struct padded_vnet_hdr);
  172. }
  173. memcpy(hdr, p, hdr_len);
  174. len -= hdr_len;
  175. p += offset;
  176. copy = len;
  177. if (copy > skb_tailroom(skb))
  178. copy = skb_tailroom(skb);
  179. memcpy(skb_put(skb, copy), p, copy);
  180. len -= copy;
  181. offset += copy;
  182. /*
  183. * Verify that we can indeed put this data into a skb.
  184. * This is here to handle cases when the device erroneously
  185. * tries to receive more than is possible. This is usually
  186. * the case of a broken device.
  187. */
  188. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  189. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  190. dev_kfree_skb(skb);
  191. return NULL;
  192. }
  193. while (len) {
  194. set_skb_frag(skb, page, offset, &len);
  195. page = (struct page *)page->private;
  196. offset = 0;
  197. }
  198. if (page)
  199. give_pages(rq, page);
  200. return skb;
  201. }
  202. static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb)
  203. {
  204. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  205. struct page *page;
  206. int num_buf, i, len;
  207. num_buf = hdr->mhdr.num_buffers;
  208. while (--num_buf) {
  209. i = skb_shinfo(skb)->nr_frags;
  210. if (i >= MAX_SKB_FRAGS) {
  211. pr_debug("%s: packet too long\n", skb->dev->name);
  212. skb->dev->stats.rx_length_errors++;
  213. return -EINVAL;
  214. }
  215. page = virtqueue_get_buf(rq->vq, &len);
  216. if (!page) {
  217. pr_debug("%s: rx error: %d buffers missing\n",
  218. skb->dev->name, hdr->mhdr.num_buffers);
  219. skb->dev->stats.rx_length_errors++;
  220. return -EINVAL;
  221. }
  222. if (len > PAGE_SIZE)
  223. len = PAGE_SIZE;
  224. set_skb_frag(skb, page, 0, &len);
  225. --rq->num;
  226. }
  227. return 0;
  228. }
  229. static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
  230. {
  231. struct virtnet_info *vi = rq->vq->vdev->priv;
  232. struct net_device *dev = vi->dev;
  233. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  234. struct sk_buff *skb;
  235. struct page *page;
  236. struct skb_vnet_hdr *hdr;
  237. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  238. pr_debug("%s: short packet %i\n", dev->name, len);
  239. dev->stats.rx_length_errors++;
  240. if (vi->mergeable_rx_bufs || vi->big_packets)
  241. give_pages(rq, buf);
  242. else
  243. dev_kfree_skb(buf);
  244. return;
  245. }
  246. if (!vi->mergeable_rx_bufs && !vi->big_packets) {
  247. skb = buf;
  248. len -= sizeof(struct virtio_net_hdr);
  249. skb_trim(skb, len);
  250. } else {
  251. page = buf;
  252. skb = page_to_skb(rq, page, len);
  253. if (unlikely(!skb)) {
  254. dev->stats.rx_dropped++;
  255. give_pages(rq, page);
  256. return;
  257. }
  258. if (vi->mergeable_rx_bufs)
  259. if (receive_mergeable(rq, skb)) {
  260. dev_kfree_skb(skb);
  261. return;
  262. }
  263. }
  264. hdr = skb_vnet_hdr(skb);
  265. u64_stats_update_begin(&stats->rx_syncp);
  266. stats->rx_bytes += skb->len;
  267. stats->rx_packets++;
  268. u64_stats_update_end(&stats->rx_syncp);
  269. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  270. pr_debug("Needs csum!\n");
  271. if (!skb_partial_csum_set(skb,
  272. hdr->hdr.csum_start,
  273. hdr->hdr.csum_offset))
  274. goto frame_err;
  275. } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
  276. skb->ip_summed = CHECKSUM_UNNECESSARY;
  277. }
  278. skb->protocol = eth_type_trans(skb, dev);
  279. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  280. ntohs(skb->protocol), skb->len, skb->pkt_type);
  281. if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  282. pr_debug("GSO!\n");
  283. switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  284. case VIRTIO_NET_HDR_GSO_TCPV4:
  285. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  286. break;
  287. case VIRTIO_NET_HDR_GSO_UDP:
  288. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  289. break;
  290. case VIRTIO_NET_HDR_GSO_TCPV6:
  291. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  292. break;
  293. default:
  294. net_warn_ratelimited("%s: bad gso type %u.\n",
  295. dev->name, hdr->hdr.gso_type);
  296. goto frame_err;
  297. }
  298. if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  299. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  300. skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
  301. if (skb_shinfo(skb)->gso_size == 0) {
  302. net_warn_ratelimited("%s: zero gso size.\n", dev->name);
  303. goto frame_err;
  304. }
  305. /* Header must be checked, and gso_segs computed. */
  306. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  307. skb_shinfo(skb)->gso_segs = 0;
  308. }
  309. netif_receive_skb(skb);
  310. return;
  311. frame_err:
  312. dev->stats.rx_frame_errors++;
  313. dev_kfree_skb(skb);
  314. }
  315. static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
  316. {
  317. struct virtnet_info *vi = rq->vq->vdev->priv;
  318. struct sk_buff *skb;
  319. struct skb_vnet_hdr *hdr;
  320. int err;
  321. skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
  322. if (unlikely(!skb))
  323. return -ENOMEM;
  324. skb_put(skb, MAX_PACKET_LEN);
  325. hdr = skb_vnet_hdr(skb);
  326. sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
  327. skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
  328. err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb, gfp);
  329. if (err < 0)
  330. dev_kfree_skb(skb);
  331. return err;
  332. }
  333. static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
  334. {
  335. struct page *first, *list = NULL;
  336. char *p;
  337. int i, err, offset;
  338. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  339. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  340. first = get_a_page(rq, gfp);
  341. if (!first) {
  342. if (list)
  343. give_pages(rq, list);
  344. return -ENOMEM;
  345. }
  346. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  347. /* chain new page in list head to match sg */
  348. first->private = (unsigned long)list;
  349. list = first;
  350. }
  351. first = get_a_page(rq, gfp);
  352. if (!first) {
  353. give_pages(rq, list);
  354. return -ENOMEM;
  355. }
  356. p = page_address(first);
  357. /* rq->sg[0], rq->sg[1] share the same page */
  358. /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
  359. sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
  360. /* rq->sg[1] for data packet, from offset */
  361. offset = sizeof(struct padded_vnet_hdr);
  362. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  363. /* chain first in list head */
  364. first->private = (unsigned long)list;
  365. err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2,
  366. first, gfp);
  367. if (err < 0)
  368. give_pages(rq, first);
  369. return err;
  370. }
  371. static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
  372. {
  373. struct page *page;
  374. int err;
  375. page = get_a_page(rq, gfp);
  376. if (!page)
  377. return -ENOMEM;
  378. sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
  379. err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page, gfp);
  380. if (err < 0)
  381. give_pages(rq, page);
  382. return err;
  383. }
  384. /*
  385. * Returns false if we couldn't fill entirely (OOM).
  386. *
  387. * Normally run in the receive path, but can also be run from ndo_open
  388. * before we're receiving packets, or from refill_work which is
  389. * careful to disable receiving (using napi_disable).
  390. */
  391. static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
  392. {
  393. struct virtnet_info *vi = rq->vq->vdev->priv;
  394. int err;
  395. bool oom;
  396. do {
  397. if (vi->mergeable_rx_bufs)
  398. err = add_recvbuf_mergeable(rq, gfp);
  399. else if (vi->big_packets)
  400. err = add_recvbuf_big(rq, gfp);
  401. else
  402. err = add_recvbuf_small(rq, gfp);
  403. oom = err == -ENOMEM;
  404. if (err < 0)
  405. break;
  406. ++rq->num;
  407. } while (err > 0);
  408. if (unlikely(rq->num > rq->max))
  409. rq->max = rq->num;
  410. virtqueue_kick(rq->vq);
  411. return !oom;
  412. }
  413. static void skb_recv_done(struct virtqueue *rvq)
  414. {
  415. struct virtnet_info *vi = rvq->vdev->priv;
  416. struct receive_queue *rq = &vi->rq;
  417. /* Schedule NAPI, Suppress further interrupts if successful. */
  418. if (napi_schedule_prep(&rq->napi)) {
  419. virtqueue_disable_cb(rvq);
  420. __napi_schedule(&rq->napi);
  421. }
  422. }
  423. static void virtnet_napi_enable(struct receive_queue *rq)
  424. {
  425. napi_enable(&rq->napi);
  426. /* If all buffers were filled by other side before we napi_enabled, we
  427. * won't get another interrupt, so process any outstanding packets
  428. * now. virtnet_poll wants re-enable the queue, so we disable here.
  429. * We synchronize against interrupts via NAPI_STATE_SCHED */
  430. if (napi_schedule_prep(&rq->napi)) {
  431. virtqueue_disable_cb(rq->vq);
  432. local_bh_disable();
  433. __napi_schedule(&rq->napi);
  434. local_bh_enable();
  435. }
  436. }
  437. static void refill_work(struct work_struct *work)
  438. {
  439. struct virtnet_info *vi =
  440. container_of(work, struct virtnet_info, refill.work);
  441. bool still_empty;
  442. napi_disable(&vi->rq.napi);
  443. still_empty = !try_fill_recv(&vi->rq, GFP_KERNEL);
  444. virtnet_napi_enable(&vi->rq);
  445. /* In theory, this can happen: if we don't get any buffers in
  446. * we will *never* try to fill again. */
  447. if (still_empty)
  448. schedule_delayed_work(&vi->refill, HZ/2);
  449. }
  450. static int virtnet_poll(struct napi_struct *napi, int budget)
  451. {
  452. struct receive_queue *rq =
  453. container_of(napi, struct receive_queue, napi);
  454. struct virtnet_info *vi = rq->vq->vdev->priv;
  455. void *buf;
  456. unsigned int len, received = 0;
  457. again:
  458. while (received < budget &&
  459. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  460. receive_buf(rq, buf, len);
  461. --rq->num;
  462. received++;
  463. }
  464. if (rq->num < rq->max / 2) {
  465. if (!try_fill_recv(rq, GFP_ATOMIC))
  466. schedule_delayed_work(&vi->refill, 0);
  467. }
  468. /* Out of packets? */
  469. if (received < budget) {
  470. napi_complete(napi);
  471. if (unlikely(!virtqueue_enable_cb(rq->vq)) &&
  472. napi_schedule_prep(napi)) {
  473. virtqueue_disable_cb(rq->vq);
  474. __napi_schedule(napi);
  475. goto again;
  476. }
  477. }
  478. return received;
  479. }
  480. static unsigned int free_old_xmit_skbs(struct send_queue *sq)
  481. {
  482. struct sk_buff *skb;
  483. unsigned int len, tot_sgs = 0;
  484. struct virtnet_info *vi = sq->vq->vdev->priv;
  485. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  486. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  487. pr_debug("Sent skb %p\n", skb);
  488. u64_stats_update_begin(&stats->tx_syncp);
  489. stats->tx_bytes += skb->len;
  490. stats->tx_packets++;
  491. u64_stats_update_end(&stats->tx_syncp);
  492. tot_sgs += skb_vnet_hdr(skb)->num_sg;
  493. dev_kfree_skb_any(skb);
  494. }
  495. return tot_sgs;
  496. }
  497. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  498. {
  499. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  500. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  501. struct virtnet_info *vi = sq->vq->vdev->priv;
  502. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  503. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  504. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  505. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  506. hdr->hdr.csum_offset = skb->csum_offset;
  507. } else {
  508. hdr->hdr.flags = 0;
  509. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  510. }
  511. if (skb_is_gso(skb)) {
  512. hdr->hdr.hdr_len = skb_headlen(skb);
  513. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  514. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  515. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  516. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  517. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  518. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  519. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  520. else
  521. BUG();
  522. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  523. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  524. } else {
  525. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  526. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  527. }
  528. hdr->mhdr.num_buffers = 0;
  529. /* Encode metadata header at front. */
  530. if (vi->mergeable_rx_bufs)
  531. sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr);
  532. else
  533. sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
  534. hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
  535. return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
  536. 0, skb, GFP_ATOMIC);
  537. }
  538. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  539. {
  540. struct virtnet_info *vi = netdev_priv(dev);
  541. struct send_queue *sq = &vi->sq;
  542. int capacity;
  543. /* Free up any pending old buffers before queueing new ones. */
  544. free_old_xmit_skbs(sq);
  545. /* Try to transmit */
  546. capacity = xmit_skb(sq, skb);
  547. /* This can happen with OOM and indirect buffers. */
  548. if (unlikely(capacity < 0)) {
  549. if (likely(capacity == -ENOMEM)) {
  550. if (net_ratelimit())
  551. dev_warn(&dev->dev,
  552. "TX queue failure: out of memory\n");
  553. } else {
  554. dev->stats.tx_fifo_errors++;
  555. if (net_ratelimit())
  556. dev_warn(&dev->dev,
  557. "Unexpected TX queue failure: %d\n",
  558. capacity);
  559. }
  560. dev->stats.tx_dropped++;
  561. kfree_skb(skb);
  562. return NETDEV_TX_OK;
  563. }
  564. virtqueue_kick(sq->vq);
  565. /* Don't wait up for transmitted skbs to be freed. */
  566. skb_orphan(skb);
  567. nf_reset(skb);
  568. /* Apparently nice girls don't return TX_BUSY; stop the queue
  569. * before it gets out of hand. Naturally, this wastes entries. */
  570. if (capacity < 2+MAX_SKB_FRAGS) {
  571. netif_stop_queue(dev);
  572. if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  573. /* More just got used, free them then recheck. */
  574. capacity += free_old_xmit_skbs(sq);
  575. if (capacity >= 2+MAX_SKB_FRAGS) {
  576. netif_start_queue(dev);
  577. virtqueue_disable_cb(sq->vq);
  578. }
  579. }
  580. }
  581. return NETDEV_TX_OK;
  582. }
  583. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  584. {
  585. struct virtnet_info *vi = netdev_priv(dev);
  586. struct virtio_device *vdev = vi->vdev;
  587. int ret;
  588. ret = eth_mac_addr(dev, p);
  589. if (ret)
  590. return ret;
  591. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  592. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  593. dev->dev_addr, dev->addr_len);
  594. return 0;
  595. }
  596. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  597. struct rtnl_link_stats64 *tot)
  598. {
  599. struct virtnet_info *vi = netdev_priv(dev);
  600. int cpu;
  601. unsigned int start;
  602. for_each_possible_cpu(cpu) {
  603. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  604. u64 tpackets, tbytes, rpackets, rbytes;
  605. do {
  606. start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
  607. tpackets = stats->tx_packets;
  608. tbytes = stats->tx_bytes;
  609. } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
  610. do {
  611. start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
  612. rpackets = stats->rx_packets;
  613. rbytes = stats->rx_bytes;
  614. } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
  615. tot->rx_packets += rpackets;
  616. tot->tx_packets += tpackets;
  617. tot->rx_bytes += rbytes;
  618. tot->tx_bytes += tbytes;
  619. }
  620. tot->tx_dropped = dev->stats.tx_dropped;
  621. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  622. tot->rx_dropped = dev->stats.rx_dropped;
  623. tot->rx_length_errors = dev->stats.rx_length_errors;
  624. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  625. return tot;
  626. }
  627. #ifdef CONFIG_NET_POLL_CONTROLLER
  628. static void virtnet_netpoll(struct net_device *dev)
  629. {
  630. struct virtnet_info *vi = netdev_priv(dev);
  631. napi_schedule(&vi->rq.napi);
  632. }
  633. #endif
  634. static int virtnet_open(struct net_device *dev)
  635. {
  636. struct virtnet_info *vi = netdev_priv(dev);
  637. /* Make sure we have some buffers: if oom use wq. */
  638. if (!try_fill_recv(&vi->rq, GFP_KERNEL))
  639. schedule_delayed_work(&vi->refill, 0);
  640. virtnet_napi_enable(&vi->rq);
  641. return 0;
  642. }
  643. /*
  644. * Send command via the control virtqueue and check status. Commands
  645. * supported by the hypervisor, as indicated by feature bits, should
  646. * never fail unless improperly formated.
  647. */
  648. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  649. struct scatterlist *data, int out, int in)
  650. {
  651. struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  652. struct virtio_net_ctrl_hdr ctrl;
  653. virtio_net_ctrl_ack status = ~0;
  654. unsigned int tmp;
  655. int i;
  656. /* Caller should know better */
  657. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
  658. (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
  659. out++; /* Add header */
  660. in++; /* Add return status */
  661. ctrl.class = class;
  662. ctrl.cmd = cmd;
  663. sg_init_table(sg, out + in);
  664. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  665. for_each_sg(data, s, out + in - 2, i)
  666. sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
  667. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  668. BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
  669. virtqueue_kick(vi->cvq);
  670. /*
  671. * Spin for a response, the kick causes an ioport write, trapping
  672. * into the hypervisor, so the request should be handled immediately.
  673. */
  674. while (!virtqueue_get_buf(vi->cvq, &tmp))
  675. cpu_relax();
  676. return status == VIRTIO_NET_OK;
  677. }
  678. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  679. {
  680. rtnl_lock();
  681. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  682. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
  683. 0, 0))
  684. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  685. rtnl_unlock();
  686. }
  687. static int virtnet_close(struct net_device *dev)
  688. {
  689. struct virtnet_info *vi = netdev_priv(dev);
  690. /* Make sure refill_work doesn't re-enable napi! */
  691. cancel_delayed_work_sync(&vi->refill);
  692. napi_disable(&vi->rq.napi);
  693. return 0;
  694. }
  695. static void virtnet_set_rx_mode(struct net_device *dev)
  696. {
  697. struct virtnet_info *vi = netdev_priv(dev);
  698. struct scatterlist sg[2];
  699. u8 promisc, allmulti;
  700. struct virtio_net_ctrl_mac *mac_data;
  701. struct netdev_hw_addr *ha;
  702. int uc_count;
  703. int mc_count;
  704. void *buf;
  705. int i;
  706. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  707. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  708. return;
  709. promisc = ((dev->flags & IFF_PROMISC) != 0);
  710. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  711. sg_init_one(sg, &promisc, sizeof(promisc));
  712. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  713. VIRTIO_NET_CTRL_RX_PROMISC,
  714. sg, 1, 0))
  715. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  716. promisc ? "en" : "dis");
  717. sg_init_one(sg, &allmulti, sizeof(allmulti));
  718. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  719. VIRTIO_NET_CTRL_RX_ALLMULTI,
  720. sg, 1, 0))
  721. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  722. allmulti ? "en" : "dis");
  723. uc_count = netdev_uc_count(dev);
  724. mc_count = netdev_mc_count(dev);
  725. /* MAC filter - use one buffer for both lists */
  726. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  727. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  728. mac_data = buf;
  729. if (!buf) {
  730. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  731. return;
  732. }
  733. sg_init_table(sg, 2);
  734. /* Store the unicast list and count in the front of the buffer */
  735. mac_data->entries = uc_count;
  736. i = 0;
  737. netdev_for_each_uc_addr(ha, dev)
  738. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  739. sg_set_buf(&sg[0], mac_data,
  740. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  741. /* multicast list and count fill the end */
  742. mac_data = (void *)&mac_data->macs[uc_count][0];
  743. mac_data->entries = mc_count;
  744. i = 0;
  745. netdev_for_each_mc_addr(ha, dev)
  746. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  747. sg_set_buf(&sg[1], mac_data,
  748. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  749. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  750. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  751. sg, 2, 0))
  752. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  753. kfree(buf);
  754. }
  755. static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  756. {
  757. struct virtnet_info *vi = netdev_priv(dev);
  758. struct scatterlist sg;
  759. sg_init_one(&sg, &vid, sizeof(vid));
  760. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  761. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  762. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  763. return 0;
  764. }
  765. static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  766. {
  767. struct virtnet_info *vi = netdev_priv(dev);
  768. struct scatterlist sg;
  769. sg_init_one(&sg, &vid, sizeof(vid));
  770. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  771. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  772. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  773. return 0;
  774. }
  775. static void virtnet_get_ringparam(struct net_device *dev,
  776. struct ethtool_ringparam *ring)
  777. {
  778. struct virtnet_info *vi = netdev_priv(dev);
  779. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq.vq);
  780. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq.vq);
  781. ring->rx_pending = ring->rx_max_pending;
  782. ring->tx_pending = ring->tx_max_pending;
  783. }
  784. static void virtnet_get_drvinfo(struct net_device *dev,
  785. struct ethtool_drvinfo *info)
  786. {
  787. struct virtnet_info *vi = netdev_priv(dev);
  788. struct virtio_device *vdev = vi->vdev;
  789. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  790. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  791. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  792. }
  793. static const struct ethtool_ops virtnet_ethtool_ops = {
  794. .get_drvinfo = virtnet_get_drvinfo,
  795. .get_link = ethtool_op_get_link,
  796. .get_ringparam = virtnet_get_ringparam,
  797. };
  798. #define MIN_MTU 68
  799. #define MAX_MTU 65535
  800. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  801. {
  802. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  803. return -EINVAL;
  804. dev->mtu = new_mtu;
  805. return 0;
  806. }
  807. static const struct net_device_ops virtnet_netdev = {
  808. .ndo_open = virtnet_open,
  809. .ndo_stop = virtnet_close,
  810. .ndo_start_xmit = start_xmit,
  811. .ndo_validate_addr = eth_validate_addr,
  812. .ndo_set_mac_address = virtnet_set_mac_address,
  813. .ndo_set_rx_mode = virtnet_set_rx_mode,
  814. .ndo_change_mtu = virtnet_change_mtu,
  815. .ndo_get_stats64 = virtnet_stats,
  816. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  817. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  818. #ifdef CONFIG_NET_POLL_CONTROLLER
  819. .ndo_poll_controller = virtnet_netpoll,
  820. #endif
  821. };
  822. static void virtnet_config_changed_work(struct work_struct *work)
  823. {
  824. struct virtnet_info *vi =
  825. container_of(work, struct virtnet_info, config_work);
  826. u16 v;
  827. mutex_lock(&vi->config_lock);
  828. if (!vi->config_enable)
  829. goto done;
  830. if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
  831. offsetof(struct virtio_net_config, status),
  832. &v) < 0)
  833. goto done;
  834. if (v & VIRTIO_NET_S_ANNOUNCE) {
  835. netdev_notify_peers(vi->dev);
  836. virtnet_ack_link_announce(vi);
  837. }
  838. /* Ignore unknown (future) status bits */
  839. v &= VIRTIO_NET_S_LINK_UP;
  840. if (vi->status == v)
  841. goto done;
  842. vi->status = v;
  843. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  844. netif_carrier_on(vi->dev);
  845. netif_wake_queue(vi->dev);
  846. } else {
  847. netif_carrier_off(vi->dev);
  848. netif_stop_queue(vi->dev);
  849. }
  850. done:
  851. mutex_unlock(&vi->config_lock);
  852. }
  853. static void virtnet_config_changed(struct virtio_device *vdev)
  854. {
  855. struct virtnet_info *vi = vdev->priv;
  856. schedule_work(&vi->config_work);
  857. }
  858. static void virtnet_del_vqs(struct virtnet_info *vi)
  859. {
  860. struct virtio_device *vdev = vi->vdev;
  861. vdev->config->del_vqs(vdev);
  862. }
  863. static int init_vqs(struct virtnet_info *vi)
  864. {
  865. struct virtqueue *vqs[3];
  866. vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
  867. const char *names[] = { "input", "output", "control" };
  868. int nvqs, err;
  869. /* We expect two virtqueues, receive then send,
  870. * and optionally control. */
  871. nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
  872. err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
  873. if (err)
  874. return err;
  875. vi->rq.vq = vqs[0];
  876. vi->sq.vq = vqs[1];
  877. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  878. vi->cvq = vqs[2];
  879. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  880. vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
  881. }
  882. return 0;
  883. }
  884. static int virtnet_probe(struct virtio_device *vdev)
  885. {
  886. int err;
  887. struct net_device *dev;
  888. struct virtnet_info *vi;
  889. /* Allocate ourselves a network device with room for our info */
  890. dev = alloc_etherdev(sizeof(struct virtnet_info));
  891. if (!dev)
  892. return -ENOMEM;
  893. /* Set up network device as normal. */
  894. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  895. dev->netdev_ops = &virtnet_netdev;
  896. dev->features = NETIF_F_HIGHDMA;
  897. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  898. SET_NETDEV_DEV(dev, &vdev->dev);
  899. /* Do we support "hardware" checksums? */
  900. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  901. /* This opens up the world of extra features. */
  902. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  903. if (csum)
  904. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  905. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  906. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  907. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  908. }
  909. /* Individual feature bits: what can host handle? */
  910. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  911. dev->hw_features |= NETIF_F_TSO;
  912. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  913. dev->hw_features |= NETIF_F_TSO6;
  914. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  915. dev->hw_features |= NETIF_F_TSO_ECN;
  916. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  917. dev->hw_features |= NETIF_F_UFO;
  918. if (gso)
  919. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  920. /* (!csum && gso) case will be fixed by register_netdev() */
  921. }
  922. /* Configuration may specify what MAC to use. Otherwise random. */
  923. if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
  924. offsetof(struct virtio_net_config, mac),
  925. dev->dev_addr, dev->addr_len) < 0)
  926. eth_hw_addr_random(dev);
  927. /* Set up our device-specific information */
  928. vi = netdev_priv(dev);
  929. netif_napi_add(dev, &vi->rq.napi, virtnet_poll, napi_weight);
  930. vi->dev = dev;
  931. vi->vdev = vdev;
  932. vdev->priv = vi;
  933. vi->rq.pages = NULL;
  934. vi->stats = alloc_percpu(struct virtnet_stats);
  935. err = -ENOMEM;
  936. if (vi->stats == NULL)
  937. goto free;
  938. INIT_DELAYED_WORK(&vi->refill, refill_work);
  939. mutex_init(&vi->config_lock);
  940. vi->config_enable = true;
  941. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  942. sg_init_table(vi->rq.sg, ARRAY_SIZE(vi->rq.sg));
  943. sg_init_table(vi->sq.sg, ARRAY_SIZE(vi->sq.sg));
  944. /* If we can receive ANY GSO packets, we must allocate large ones. */
  945. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  946. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  947. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  948. vi->big_packets = true;
  949. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  950. vi->mergeable_rx_bufs = true;
  951. err = init_vqs(vi);
  952. if (err)
  953. goto free_stats;
  954. err = register_netdev(dev);
  955. if (err) {
  956. pr_debug("virtio_net: registering device failed\n");
  957. goto free_vqs;
  958. }
  959. /* Last of all, set up some receive buffers. */
  960. try_fill_recv(&vi->rq, GFP_KERNEL);
  961. /* If we didn't even get one input buffer, we're useless. */
  962. if (vi->rq.num == 0) {
  963. err = -ENOMEM;
  964. goto unregister;
  965. }
  966. /* Assume link up if device can't report link status,
  967. otherwise get link status from config. */
  968. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  969. netif_carrier_off(dev);
  970. schedule_work(&vi->config_work);
  971. } else {
  972. vi->status = VIRTIO_NET_S_LINK_UP;
  973. netif_carrier_on(dev);
  974. }
  975. pr_debug("virtnet: registered device %s\n", dev->name);
  976. return 0;
  977. unregister:
  978. unregister_netdev(dev);
  979. free_vqs:
  980. virtnet_del_vqs(vi);
  981. free_stats:
  982. free_percpu(vi->stats);
  983. free:
  984. free_netdev(dev);
  985. return err;
  986. }
  987. static void free_unused_bufs(struct virtnet_info *vi)
  988. {
  989. void *buf;
  990. while (1) {
  991. buf = virtqueue_detach_unused_buf(vi->sq.vq);
  992. if (!buf)
  993. break;
  994. dev_kfree_skb(buf);
  995. }
  996. while (1) {
  997. buf = virtqueue_detach_unused_buf(vi->rq.vq);
  998. if (!buf)
  999. break;
  1000. if (vi->mergeable_rx_bufs || vi->big_packets)
  1001. give_pages(&vi->rq, buf);
  1002. else
  1003. dev_kfree_skb(buf);
  1004. --vi->rq.num;
  1005. }
  1006. BUG_ON(vi->rq.num != 0);
  1007. }
  1008. static void remove_vq_common(struct virtnet_info *vi)
  1009. {
  1010. vi->vdev->config->reset(vi->vdev);
  1011. /* Free unused buffers in both send and recv, if any. */
  1012. free_unused_bufs(vi);
  1013. virtnet_del_vqs(vi);
  1014. while (vi->rq.pages)
  1015. __free_pages(get_a_page(&vi->rq, GFP_KERNEL), 0);
  1016. }
  1017. static void virtnet_remove(struct virtio_device *vdev)
  1018. {
  1019. struct virtnet_info *vi = vdev->priv;
  1020. /* Prevent config work handler from accessing the device. */
  1021. mutex_lock(&vi->config_lock);
  1022. vi->config_enable = false;
  1023. mutex_unlock(&vi->config_lock);
  1024. unregister_netdev(vi->dev);
  1025. remove_vq_common(vi);
  1026. flush_work(&vi->config_work);
  1027. free_percpu(vi->stats);
  1028. free_netdev(vi->dev);
  1029. }
  1030. #ifdef CONFIG_PM
  1031. static int virtnet_freeze(struct virtio_device *vdev)
  1032. {
  1033. struct virtnet_info *vi = vdev->priv;
  1034. /* Prevent config work handler from accessing the device */
  1035. mutex_lock(&vi->config_lock);
  1036. vi->config_enable = false;
  1037. mutex_unlock(&vi->config_lock);
  1038. netif_device_detach(vi->dev);
  1039. cancel_delayed_work_sync(&vi->refill);
  1040. if (netif_running(vi->dev))
  1041. napi_disable(&vi->rq.napi);
  1042. remove_vq_common(vi);
  1043. flush_work(&vi->config_work);
  1044. return 0;
  1045. }
  1046. static int virtnet_restore(struct virtio_device *vdev)
  1047. {
  1048. struct virtnet_info *vi = vdev->priv;
  1049. int err;
  1050. err = init_vqs(vi);
  1051. if (err)
  1052. return err;
  1053. if (netif_running(vi->dev))
  1054. virtnet_napi_enable(&vi->rq);
  1055. netif_device_attach(vi->dev);
  1056. if (!try_fill_recv(&vi->rq, GFP_KERNEL))
  1057. schedule_delayed_work(&vi->refill, 0);
  1058. mutex_lock(&vi->config_lock);
  1059. vi->config_enable = true;
  1060. mutex_unlock(&vi->config_lock);
  1061. return 0;
  1062. }
  1063. #endif
  1064. static struct virtio_device_id id_table[] = {
  1065. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1066. { 0 },
  1067. };
  1068. static unsigned int features[] = {
  1069. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  1070. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  1071. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  1072. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  1073. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
  1074. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  1075. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  1076. VIRTIO_NET_F_GUEST_ANNOUNCE,
  1077. };
  1078. static struct virtio_driver virtio_net_driver = {
  1079. .feature_table = features,
  1080. .feature_table_size = ARRAY_SIZE(features),
  1081. .driver.name = KBUILD_MODNAME,
  1082. .driver.owner = THIS_MODULE,
  1083. .id_table = id_table,
  1084. .probe = virtnet_probe,
  1085. .remove = virtnet_remove,
  1086. .config_changed = virtnet_config_changed,
  1087. #ifdef CONFIG_PM
  1088. .freeze = virtnet_freeze,
  1089. .restore = virtnet_restore,
  1090. #endif
  1091. };
  1092. static int __init init(void)
  1093. {
  1094. return register_virtio_driver(&virtio_net_driver);
  1095. }
  1096. static void __exit fini(void)
  1097. {
  1098. unregister_virtio_driver(&virtio_net_driver);
  1099. }
  1100. module_init(init);
  1101. module_exit(fini);
  1102. MODULE_DEVICE_TABLE(virtio, id_table);
  1103. MODULE_DESCRIPTION("Virtio network driver");
  1104. MODULE_LICENSE("GPL");