virtio_net.c 29 KB

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