virtio_net.c 30 KB

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