virtio_net.c 27 KB

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