virtio_net.c 33 KB

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