virtio_net.c 29 KB

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