virtio_net.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. __napi_schedule(&vi->napi);
  418. }
  419. }
  420. static void refill_work(struct work_struct *work)
  421. {
  422. struct virtnet_info *vi;
  423. bool still_empty;
  424. vi = container_of(work, struct virtnet_info, refill.work);
  425. napi_disable(&vi->napi);
  426. still_empty = !try_fill_recv(vi, GFP_KERNEL);
  427. virtnet_napi_enable(vi);
  428. /* In theory, this can happen: if we don't get any buffers in
  429. * we will *never* try to fill again. */
  430. if (still_empty)
  431. queue_delayed_work(system_nrt_wq, &vi->refill, HZ/2);
  432. }
  433. static int virtnet_poll(struct napi_struct *napi, int budget)
  434. {
  435. struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
  436. void *buf;
  437. unsigned int len, received = 0;
  438. again:
  439. while (received < budget &&
  440. (buf = virtqueue_get_buf(vi->rvq, &len)) != NULL) {
  441. receive_buf(vi->dev, buf, len);
  442. --vi->num;
  443. received++;
  444. }
  445. if (vi->num < vi->max / 2) {
  446. if (!try_fill_recv(vi, GFP_ATOMIC))
  447. queue_delayed_work(system_nrt_wq, &vi->refill, 0);
  448. }
  449. /* Out of packets? */
  450. if (received < budget) {
  451. napi_complete(napi);
  452. if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
  453. napi_schedule_prep(napi)) {
  454. virtqueue_disable_cb(vi->rvq);
  455. __napi_schedule(napi);
  456. goto again;
  457. }
  458. }
  459. return received;
  460. }
  461. static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
  462. {
  463. struct sk_buff *skb;
  464. unsigned int len, tot_sgs = 0;
  465. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  466. while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
  467. pr_debug("Sent skb %p\n", skb);
  468. u64_stats_update_begin(&stats->syncp);
  469. stats->tx_bytes += skb->len;
  470. stats->tx_packets++;
  471. u64_stats_update_end(&stats->syncp);
  472. tot_sgs += skb_vnet_hdr(skb)->num_sg;
  473. dev_kfree_skb_any(skb);
  474. }
  475. return tot_sgs;
  476. }
  477. static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
  478. {
  479. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  480. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  481. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  482. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  483. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  484. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  485. hdr->hdr.csum_offset = skb->csum_offset;
  486. } else {
  487. hdr->hdr.flags = 0;
  488. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  489. }
  490. if (skb_is_gso(skb)) {
  491. hdr->hdr.hdr_len = skb_headlen(skb);
  492. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  493. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  494. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  495. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  496. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  497. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  498. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  499. else
  500. BUG();
  501. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  502. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  503. } else {
  504. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  505. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  506. }
  507. hdr->mhdr.num_buffers = 0;
  508. /* Encode metadata header at front. */
  509. if (vi->mergeable_rx_bufs)
  510. sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
  511. else
  512. sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
  513. hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
  514. return virtqueue_add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
  515. 0, skb, GFP_ATOMIC);
  516. }
  517. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  518. {
  519. struct virtnet_info *vi = netdev_priv(dev);
  520. int capacity;
  521. /* Free up any pending old buffers before queueing new ones. */
  522. free_old_xmit_skbs(vi);
  523. /* Try to transmit */
  524. capacity = xmit_skb(vi, skb);
  525. /* This can happen with OOM and indirect buffers. */
  526. if (unlikely(capacity < 0)) {
  527. if (likely(capacity == -ENOMEM)) {
  528. if (net_ratelimit())
  529. dev_warn(&dev->dev,
  530. "TX queue failure: out of memory\n");
  531. } else {
  532. dev->stats.tx_fifo_errors++;
  533. if (net_ratelimit())
  534. dev_warn(&dev->dev,
  535. "Unexpected TX queue failure: %d\n",
  536. capacity);
  537. }
  538. dev->stats.tx_dropped++;
  539. kfree_skb(skb);
  540. return NETDEV_TX_OK;
  541. }
  542. virtqueue_kick(vi->svq);
  543. /* Don't wait up for transmitted skbs to be freed. */
  544. skb_orphan(skb);
  545. nf_reset(skb);
  546. /* Apparently nice girls don't return TX_BUSY; stop the queue
  547. * before it gets out of hand. Naturally, this wastes entries. */
  548. if (capacity < 2+MAX_SKB_FRAGS) {
  549. netif_stop_queue(dev);
  550. if (unlikely(!virtqueue_enable_cb_delayed(vi->svq))) {
  551. /* More just got used, free them then recheck. */
  552. capacity += free_old_xmit_skbs(vi);
  553. if (capacity >= 2+MAX_SKB_FRAGS) {
  554. netif_start_queue(dev);
  555. virtqueue_disable_cb(vi->svq);
  556. }
  557. }
  558. }
  559. return NETDEV_TX_OK;
  560. }
  561. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  562. {
  563. struct virtnet_info *vi = netdev_priv(dev);
  564. struct virtio_device *vdev = vi->vdev;
  565. int ret;
  566. ret = eth_mac_addr(dev, p);
  567. if (ret)
  568. return ret;
  569. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  570. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  571. dev->dev_addr, dev->addr_len);
  572. return 0;
  573. }
  574. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  575. struct rtnl_link_stats64 *tot)
  576. {
  577. struct virtnet_info *vi = netdev_priv(dev);
  578. int cpu;
  579. unsigned int start;
  580. for_each_possible_cpu(cpu) {
  581. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  582. u64 tpackets, tbytes, rpackets, rbytes;
  583. do {
  584. start = u64_stats_fetch_begin(&stats->syncp);
  585. tpackets = stats->tx_packets;
  586. tbytes = stats->tx_bytes;
  587. rpackets = stats->rx_packets;
  588. rbytes = stats->rx_bytes;
  589. } while (u64_stats_fetch_retry(&stats->syncp, start));
  590. tot->rx_packets += rpackets;
  591. tot->tx_packets += tpackets;
  592. tot->rx_bytes += rbytes;
  593. tot->tx_bytes += tbytes;
  594. }
  595. tot->tx_dropped = dev->stats.tx_dropped;
  596. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  597. tot->rx_dropped = dev->stats.rx_dropped;
  598. tot->rx_length_errors = dev->stats.rx_length_errors;
  599. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  600. return tot;
  601. }
  602. #ifdef CONFIG_NET_POLL_CONTROLLER
  603. static void virtnet_netpoll(struct net_device *dev)
  604. {
  605. struct virtnet_info *vi = netdev_priv(dev);
  606. napi_schedule(&vi->napi);
  607. }
  608. #endif
  609. static int virtnet_open(struct net_device *dev)
  610. {
  611. struct virtnet_info *vi = netdev_priv(dev);
  612. /* Make sure we have some buffers: if oom use wq. */
  613. if (!try_fill_recv(vi, GFP_KERNEL))
  614. queue_delayed_work(system_nrt_wq, &vi->refill, 0);
  615. virtnet_napi_enable(vi);
  616. return 0;
  617. }
  618. /*
  619. * Send command via the control virtqueue and check status. Commands
  620. * supported by the hypervisor, as indicated by feature bits, should
  621. * never fail unless improperly formated.
  622. */
  623. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  624. struct scatterlist *data, int out, int in)
  625. {
  626. struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  627. struct virtio_net_ctrl_hdr ctrl;
  628. virtio_net_ctrl_ack status = ~0;
  629. unsigned int tmp;
  630. int i;
  631. /* Caller should know better */
  632. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
  633. (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
  634. out++; /* Add header */
  635. in++; /* Add return status */
  636. ctrl.class = class;
  637. ctrl.cmd = cmd;
  638. sg_init_table(sg, out + in);
  639. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  640. for_each_sg(data, s, out + in - 2, i)
  641. sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
  642. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  643. BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
  644. virtqueue_kick(vi->cvq);
  645. /*
  646. * Spin for a response, the kick causes an ioport write, trapping
  647. * into the hypervisor, so the request should be handled immediately.
  648. */
  649. while (!virtqueue_get_buf(vi->cvq, &tmp))
  650. cpu_relax();
  651. return status == VIRTIO_NET_OK;
  652. }
  653. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  654. {
  655. rtnl_lock();
  656. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  657. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
  658. 0, 0))
  659. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  660. rtnl_unlock();
  661. }
  662. static int virtnet_close(struct net_device *dev)
  663. {
  664. struct virtnet_info *vi = netdev_priv(dev);
  665. /* Make sure refill_work doesn't re-enable napi! */
  666. cancel_delayed_work_sync(&vi->refill);
  667. napi_disable(&vi->napi);
  668. return 0;
  669. }
  670. static void virtnet_set_rx_mode(struct net_device *dev)
  671. {
  672. struct virtnet_info *vi = netdev_priv(dev);
  673. struct scatterlist sg[2];
  674. u8 promisc, allmulti;
  675. struct virtio_net_ctrl_mac *mac_data;
  676. struct netdev_hw_addr *ha;
  677. int uc_count;
  678. int mc_count;
  679. void *buf;
  680. int i;
  681. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  682. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  683. return;
  684. promisc = ((dev->flags & IFF_PROMISC) != 0);
  685. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  686. sg_init_one(sg, &promisc, sizeof(promisc));
  687. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  688. VIRTIO_NET_CTRL_RX_PROMISC,
  689. sg, 1, 0))
  690. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  691. promisc ? "en" : "dis");
  692. sg_init_one(sg, &allmulti, sizeof(allmulti));
  693. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  694. VIRTIO_NET_CTRL_RX_ALLMULTI,
  695. sg, 1, 0))
  696. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  697. allmulti ? "en" : "dis");
  698. uc_count = netdev_uc_count(dev);
  699. mc_count = netdev_mc_count(dev);
  700. /* MAC filter - use one buffer for both lists */
  701. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  702. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  703. mac_data = buf;
  704. if (!buf) {
  705. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  706. return;
  707. }
  708. sg_init_table(sg, 2);
  709. /* Store the unicast list and count in the front of the buffer */
  710. mac_data->entries = uc_count;
  711. i = 0;
  712. netdev_for_each_uc_addr(ha, dev)
  713. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  714. sg_set_buf(&sg[0], mac_data,
  715. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  716. /* multicast list and count fill the end */
  717. mac_data = (void *)&mac_data->macs[uc_count][0];
  718. mac_data->entries = mc_count;
  719. i = 0;
  720. netdev_for_each_mc_addr(ha, dev)
  721. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  722. sg_set_buf(&sg[1], mac_data,
  723. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  724. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  725. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  726. sg, 2, 0))
  727. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  728. kfree(buf);
  729. }
  730. static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  731. {
  732. struct virtnet_info *vi = netdev_priv(dev);
  733. struct scatterlist sg;
  734. sg_init_one(&sg, &vid, sizeof(vid));
  735. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  736. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  737. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  738. return 0;
  739. }
  740. static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  741. {
  742. struct virtnet_info *vi = netdev_priv(dev);
  743. struct scatterlist sg;
  744. sg_init_one(&sg, &vid, sizeof(vid));
  745. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  746. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  747. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  748. return 0;
  749. }
  750. static void virtnet_get_ringparam(struct net_device *dev,
  751. struct ethtool_ringparam *ring)
  752. {
  753. struct virtnet_info *vi = netdev_priv(dev);
  754. ring->rx_max_pending = virtqueue_get_vring_size(vi->rvq);
  755. ring->tx_max_pending = virtqueue_get_vring_size(vi->svq);
  756. ring->rx_pending = ring->rx_max_pending;
  757. ring->tx_pending = ring->tx_max_pending;
  758. }
  759. static void virtnet_get_drvinfo(struct net_device *dev,
  760. struct ethtool_drvinfo *info)
  761. {
  762. struct virtnet_info *vi = netdev_priv(dev);
  763. struct virtio_device *vdev = vi->vdev;
  764. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  765. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  766. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  767. }
  768. static const struct ethtool_ops virtnet_ethtool_ops = {
  769. .get_drvinfo = virtnet_get_drvinfo,
  770. .get_link = ethtool_op_get_link,
  771. .get_ringparam = virtnet_get_ringparam,
  772. };
  773. #define MIN_MTU 68
  774. #define MAX_MTU 65535
  775. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  776. {
  777. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  778. return -EINVAL;
  779. dev->mtu = new_mtu;
  780. return 0;
  781. }
  782. static const struct net_device_ops virtnet_netdev = {
  783. .ndo_open = virtnet_open,
  784. .ndo_stop = virtnet_close,
  785. .ndo_start_xmit = start_xmit,
  786. .ndo_validate_addr = eth_validate_addr,
  787. .ndo_set_mac_address = virtnet_set_mac_address,
  788. .ndo_set_rx_mode = virtnet_set_rx_mode,
  789. .ndo_change_mtu = virtnet_change_mtu,
  790. .ndo_get_stats64 = virtnet_stats,
  791. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  792. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  793. #ifdef CONFIG_NET_POLL_CONTROLLER
  794. .ndo_poll_controller = virtnet_netpoll,
  795. #endif
  796. };
  797. static void virtnet_config_changed_work(struct work_struct *work)
  798. {
  799. struct virtnet_info *vi =
  800. container_of(work, struct virtnet_info, config_work);
  801. u16 v;
  802. mutex_lock(&vi->config_lock);
  803. if (!vi->config_enable)
  804. goto done;
  805. if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
  806. offsetof(struct virtio_net_config, status),
  807. &v) < 0)
  808. goto done;
  809. if (v & VIRTIO_NET_S_ANNOUNCE) {
  810. netif_notify_peers(vi->dev);
  811. virtnet_ack_link_announce(vi);
  812. }
  813. /* Ignore unknown (future) status bits */
  814. v &= VIRTIO_NET_S_LINK_UP;
  815. if (vi->status == v)
  816. goto done;
  817. vi->status = v;
  818. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  819. netif_carrier_on(vi->dev);
  820. netif_wake_queue(vi->dev);
  821. } else {
  822. netif_carrier_off(vi->dev);
  823. netif_stop_queue(vi->dev);
  824. }
  825. done:
  826. mutex_unlock(&vi->config_lock);
  827. }
  828. static void virtnet_config_changed(struct virtio_device *vdev)
  829. {
  830. struct virtnet_info *vi = vdev->priv;
  831. queue_work(system_nrt_wq, &vi->config_work);
  832. }
  833. static int init_vqs(struct virtnet_info *vi)
  834. {
  835. struct virtqueue *vqs[3];
  836. vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
  837. const char *names[] = { "input", "output", "control" };
  838. int nvqs, err;
  839. /* We expect two virtqueues, receive then send,
  840. * and optionally control. */
  841. nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
  842. err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
  843. if (err)
  844. return err;
  845. vi->rvq = vqs[0];
  846. vi->svq = vqs[1];
  847. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  848. vi->cvq = vqs[2];
  849. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  850. vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
  851. }
  852. return 0;
  853. }
  854. static int virtnet_probe(struct virtio_device *vdev)
  855. {
  856. int err;
  857. struct net_device *dev;
  858. struct virtnet_info *vi;
  859. /* Allocate ourselves a network device with room for our info */
  860. dev = alloc_etherdev(sizeof(struct virtnet_info));
  861. if (!dev)
  862. return -ENOMEM;
  863. /* Set up network device as normal. */
  864. dev->priv_flags |= IFF_UNICAST_FLT;
  865. dev->netdev_ops = &virtnet_netdev;
  866. dev->features = NETIF_F_HIGHDMA;
  867. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  868. SET_NETDEV_DEV(dev, &vdev->dev);
  869. /* Do we support "hardware" checksums? */
  870. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  871. /* This opens up the world of extra features. */
  872. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  873. if (csum)
  874. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  875. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  876. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  877. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  878. }
  879. /* Individual feature bits: what can host handle? */
  880. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  881. dev->hw_features |= NETIF_F_TSO;
  882. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  883. dev->hw_features |= NETIF_F_TSO6;
  884. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  885. dev->hw_features |= NETIF_F_TSO_ECN;
  886. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  887. dev->hw_features |= NETIF_F_UFO;
  888. if (gso)
  889. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  890. /* (!csum && gso) case will be fixed by register_netdev() */
  891. }
  892. /* Configuration may specify what MAC to use. Otherwise random. */
  893. if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
  894. offsetof(struct virtio_net_config, mac),
  895. dev->dev_addr, dev->addr_len) < 0)
  896. eth_hw_addr_random(dev);
  897. /* Set up our device-specific information */
  898. vi = netdev_priv(dev);
  899. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  900. vi->dev = dev;
  901. vi->vdev = vdev;
  902. vdev->priv = vi;
  903. vi->pages = NULL;
  904. vi->stats = alloc_percpu(struct virtnet_stats);
  905. err = -ENOMEM;
  906. if (vi->stats == NULL)
  907. goto free;
  908. INIT_DELAYED_WORK(&vi->refill, refill_work);
  909. mutex_init(&vi->config_lock);
  910. vi->config_enable = true;
  911. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  912. sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
  913. sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
  914. /* If we can receive ANY GSO packets, we must allocate large ones. */
  915. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  916. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  917. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  918. vi->big_packets = true;
  919. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  920. vi->mergeable_rx_bufs = true;
  921. err = init_vqs(vi);
  922. if (err)
  923. goto free_stats;
  924. err = register_netdev(dev);
  925. if (err) {
  926. pr_debug("virtio_net: registering device failed\n");
  927. goto free_vqs;
  928. }
  929. /* Last of all, set up some receive buffers. */
  930. try_fill_recv(vi, GFP_KERNEL);
  931. /* If we didn't even get one input buffer, we're useless. */
  932. if (vi->num == 0) {
  933. err = -ENOMEM;
  934. goto unregister;
  935. }
  936. /* Assume link up if device can't report link status,
  937. otherwise get link status from config. */
  938. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  939. netif_carrier_off(dev);
  940. queue_work(system_nrt_wq, &vi->config_work);
  941. } else {
  942. vi->status = VIRTIO_NET_S_LINK_UP;
  943. netif_carrier_on(dev);
  944. }
  945. pr_debug("virtnet: registered device %s\n", dev->name);
  946. return 0;
  947. unregister:
  948. unregister_netdev(dev);
  949. free_vqs:
  950. vdev->config->del_vqs(vdev);
  951. free_stats:
  952. free_percpu(vi->stats);
  953. free:
  954. free_netdev(dev);
  955. return err;
  956. }
  957. static void free_unused_bufs(struct virtnet_info *vi)
  958. {
  959. void *buf;
  960. while (1) {
  961. buf = virtqueue_detach_unused_buf(vi->svq);
  962. if (!buf)
  963. break;
  964. dev_kfree_skb(buf);
  965. }
  966. while (1) {
  967. buf = virtqueue_detach_unused_buf(vi->rvq);
  968. if (!buf)
  969. break;
  970. if (vi->mergeable_rx_bufs || vi->big_packets)
  971. give_pages(vi, buf);
  972. else
  973. dev_kfree_skb(buf);
  974. --vi->num;
  975. }
  976. BUG_ON(vi->num != 0);
  977. }
  978. static void remove_vq_common(struct virtnet_info *vi)
  979. {
  980. vi->vdev->config->reset(vi->vdev);
  981. /* Free unused buffers in both send and recv, if any. */
  982. free_unused_bufs(vi);
  983. vi->vdev->config->del_vqs(vi->vdev);
  984. while (vi->pages)
  985. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  986. }
  987. static void __devexit virtnet_remove(struct virtio_device *vdev)
  988. {
  989. struct virtnet_info *vi = vdev->priv;
  990. /* Prevent config work handler from accessing the device. */
  991. mutex_lock(&vi->config_lock);
  992. vi->config_enable = false;
  993. mutex_unlock(&vi->config_lock);
  994. unregister_netdev(vi->dev);
  995. remove_vq_common(vi);
  996. flush_work(&vi->config_work);
  997. free_percpu(vi->stats);
  998. free_netdev(vi->dev);
  999. }
  1000. #ifdef CONFIG_PM
  1001. static int virtnet_freeze(struct virtio_device *vdev)
  1002. {
  1003. struct virtnet_info *vi = vdev->priv;
  1004. /* Prevent config work handler from accessing the device */
  1005. mutex_lock(&vi->config_lock);
  1006. vi->config_enable = false;
  1007. mutex_unlock(&vi->config_lock);
  1008. virtqueue_disable_cb(vi->rvq);
  1009. virtqueue_disable_cb(vi->svq);
  1010. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
  1011. virtqueue_disable_cb(vi->cvq);
  1012. netif_device_detach(vi->dev);
  1013. cancel_delayed_work_sync(&vi->refill);
  1014. if (netif_running(vi->dev))
  1015. napi_disable(&vi->napi);
  1016. remove_vq_common(vi);
  1017. flush_work(&vi->config_work);
  1018. return 0;
  1019. }
  1020. static int virtnet_restore(struct virtio_device *vdev)
  1021. {
  1022. struct virtnet_info *vi = vdev->priv;
  1023. int err;
  1024. err = init_vqs(vi);
  1025. if (err)
  1026. return err;
  1027. if (netif_running(vi->dev))
  1028. virtnet_napi_enable(vi);
  1029. netif_device_attach(vi->dev);
  1030. if (!try_fill_recv(vi, GFP_KERNEL))
  1031. queue_delayed_work(system_nrt_wq, &vi->refill, 0);
  1032. mutex_lock(&vi->config_lock);
  1033. vi->config_enable = true;
  1034. mutex_unlock(&vi->config_lock);
  1035. return 0;
  1036. }
  1037. #endif
  1038. static struct virtio_device_id id_table[] = {
  1039. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1040. { 0 },
  1041. };
  1042. static unsigned int features[] = {
  1043. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  1044. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  1045. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  1046. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  1047. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
  1048. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  1049. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  1050. VIRTIO_NET_F_GUEST_ANNOUNCE,
  1051. };
  1052. static struct virtio_driver virtio_net_driver = {
  1053. .feature_table = features,
  1054. .feature_table_size = ARRAY_SIZE(features),
  1055. .driver.name = KBUILD_MODNAME,
  1056. .driver.owner = THIS_MODULE,
  1057. .id_table = id_table,
  1058. .probe = virtnet_probe,
  1059. .remove = __devexit_p(virtnet_remove),
  1060. .config_changed = virtnet_config_changed,
  1061. #ifdef CONFIG_PM
  1062. .freeze = virtnet_freeze,
  1063. .restore = virtnet_restore,
  1064. #endif
  1065. };
  1066. static int __init init(void)
  1067. {
  1068. return register_virtio_driver(&virtio_net_driver);
  1069. }
  1070. static void __exit fini(void)
  1071. {
  1072. unregister_virtio_driver(&virtio_net_driver);
  1073. }
  1074. module_init(init);
  1075. module_exit(fini);
  1076. MODULE_DEVICE_TABLE(virtio, id_table);
  1077. MODULE_DESCRIPTION("Virtio network driver");
  1078. MODULE_LICENSE("GPL");