virtio_net.c 33 KB

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