virtio_net.c 32 KB

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