virtio_net.c 27 KB

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