virtio_net.c 33 KB

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