virtio_net.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /* A simple 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. static int napi_weight = 128;
  29. module_param(napi_weight, int, 0444);
  30. static int csum = 1, gso = 1;
  31. module_param(csum, bool, 0444);
  32. module_param(gso, bool, 0444);
  33. /* FIXME: MTU in config. */
  34. #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  35. #define GOOD_COPY_LEN 128
  36. struct virtnet_info
  37. {
  38. struct virtio_device *vdev;
  39. struct virtqueue *rvq, *svq;
  40. struct net_device *dev;
  41. struct napi_struct napi;
  42. unsigned int status;
  43. /* The skb we couldn't send because buffers were full. */
  44. struct sk_buff *last_xmit_skb;
  45. /* If we need to free in a timer, this is it. */
  46. struct timer_list xmit_free_timer;
  47. /* Number of input buffers, and max we've ever had. */
  48. unsigned int num, max;
  49. /* For cleaning up after transmission. */
  50. struct tasklet_struct tasklet;
  51. bool free_in_tasklet;
  52. /* I like... big packets and I cannot lie! */
  53. bool big_packets;
  54. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  55. bool mergeable_rx_bufs;
  56. /* Receive & send queues. */
  57. struct sk_buff_head recv;
  58. struct sk_buff_head send;
  59. /* Chain pages by the private ptr. */
  60. struct page *pages;
  61. };
  62. static inline void *skb_vnet_hdr(struct sk_buff *skb)
  63. {
  64. return (struct virtio_net_hdr *)skb->cb;
  65. }
  66. static void give_a_page(struct virtnet_info *vi, struct page *page)
  67. {
  68. page->private = (unsigned long)vi->pages;
  69. vi->pages = page;
  70. }
  71. static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
  72. {
  73. unsigned int i;
  74. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  75. give_a_page(vi, skb_shinfo(skb)->frags[i].page);
  76. skb_shinfo(skb)->nr_frags = 0;
  77. skb->data_len = 0;
  78. }
  79. static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
  80. {
  81. struct page *p = vi->pages;
  82. if (p)
  83. vi->pages = (struct page *)p->private;
  84. else
  85. p = alloc_page(gfp_mask);
  86. return p;
  87. }
  88. static void skb_xmit_done(struct virtqueue *svq)
  89. {
  90. struct virtnet_info *vi = svq->vdev->priv;
  91. /* Suppress further interrupts. */
  92. svq->vq_ops->disable_cb(svq);
  93. /* We were probably waiting for more output buffers. */
  94. netif_wake_queue(vi->dev);
  95. /* Make sure we re-xmit last_xmit_skb: if there are no more packets
  96. * queued, start_xmit won't be called. */
  97. tasklet_schedule(&vi->tasklet);
  98. }
  99. static void receive_skb(struct net_device *dev, struct sk_buff *skb,
  100. unsigned len)
  101. {
  102. struct virtnet_info *vi = netdev_priv(dev);
  103. struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
  104. int err;
  105. int i;
  106. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  107. pr_debug("%s: short packet %i\n", dev->name, len);
  108. dev->stats.rx_length_errors++;
  109. goto drop;
  110. }
  111. if (vi->mergeable_rx_bufs) {
  112. struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
  113. unsigned int copy;
  114. char *p = page_address(skb_shinfo(skb)->frags[0].page);
  115. if (len > PAGE_SIZE)
  116. len = PAGE_SIZE;
  117. len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
  118. memcpy(hdr, p, sizeof(*mhdr));
  119. p += sizeof(*mhdr);
  120. copy = len;
  121. if (copy > skb_tailroom(skb))
  122. copy = skb_tailroom(skb);
  123. memcpy(skb_put(skb, copy), p, copy);
  124. len -= copy;
  125. if (!len) {
  126. give_a_page(vi, skb_shinfo(skb)->frags[0].page);
  127. skb_shinfo(skb)->nr_frags--;
  128. } else {
  129. skb_shinfo(skb)->frags[0].page_offset +=
  130. sizeof(*mhdr) + copy;
  131. skb_shinfo(skb)->frags[0].size = len;
  132. skb->data_len += len;
  133. skb->len += len;
  134. }
  135. while (--mhdr->num_buffers) {
  136. struct sk_buff *nskb;
  137. i = skb_shinfo(skb)->nr_frags;
  138. if (i >= MAX_SKB_FRAGS) {
  139. pr_debug("%s: packet too long %d\n", dev->name,
  140. len);
  141. dev->stats.rx_length_errors++;
  142. goto drop;
  143. }
  144. nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
  145. if (!nskb) {
  146. pr_debug("%s: rx error: %d buffers missing\n",
  147. dev->name, mhdr->num_buffers);
  148. dev->stats.rx_length_errors++;
  149. goto drop;
  150. }
  151. __skb_unlink(nskb, &vi->recv);
  152. vi->num--;
  153. skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
  154. skb_shinfo(nskb)->nr_frags = 0;
  155. kfree_skb(nskb);
  156. if (len > PAGE_SIZE)
  157. len = PAGE_SIZE;
  158. skb_shinfo(skb)->frags[i].size = len;
  159. skb_shinfo(skb)->nr_frags++;
  160. skb->data_len += len;
  161. skb->len += len;
  162. }
  163. } else {
  164. len -= sizeof(struct virtio_net_hdr);
  165. if (len <= MAX_PACKET_LEN)
  166. trim_pages(vi, skb);
  167. err = pskb_trim(skb, len);
  168. if (err) {
  169. pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
  170. len, err);
  171. dev->stats.rx_dropped++;
  172. goto drop;
  173. }
  174. }
  175. skb->truesize += skb->data_len;
  176. dev->stats.rx_bytes += skb->len;
  177. dev->stats.rx_packets++;
  178. if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  179. pr_debug("Needs csum!\n");
  180. if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
  181. goto frame_err;
  182. }
  183. skb->protocol = eth_type_trans(skb, dev);
  184. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  185. ntohs(skb->protocol), skb->len, skb->pkt_type);
  186. if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  187. pr_debug("GSO!\n");
  188. switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  189. case VIRTIO_NET_HDR_GSO_TCPV4:
  190. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  191. break;
  192. case VIRTIO_NET_HDR_GSO_UDP:
  193. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  194. break;
  195. case VIRTIO_NET_HDR_GSO_TCPV6:
  196. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  197. break;
  198. default:
  199. if (net_ratelimit())
  200. printk(KERN_WARNING "%s: bad gso type %u.\n",
  201. dev->name, hdr->gso_type);
  202. goto frame_err;
  203. }
  204. if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  205. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  206. skb_shinfo(skb)->gso_size = hdr->gso_size;
  207. if (skb_shinfo(skb)->gso_size == 0) {
  208. if (net_ratelimit())
  209. printk(KERN_WARNING "%s: zero gso size.\n",
  210. dev->name);
  211. goto frame_err;
  212. }
  213. /* Header must be checked, and gso_segs computed. */
  214. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  215. skb_shinfo(skb)->gso_segs = 0;
  216. }
  217. netif_receive_skb(skb);
  218. return;
  219. frame_err:
  220. dev->stats.rx_frame_errors++;
  221. drop:
  222. dev_kfree_skb(skb);
  223. }
  224. static void try_fill_recv_maxbufs(struct virtnet_info *vi)
  225. {
  226. struct sk_buff *skb;
  227. struct scatterlist sg[2+MAX_SKB_FRAGS];
  228. int num, err, i;
  229. sg_init_table(sg, 2+MAX_SKB_FRAGS);
  230. for (;;) {
  231. struct virtio_net_hdr *hdr;
  232. skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
  233. if (unlikely(!skb))
  234. break;
  235. skb_put(skb, MAX_PACKET_LEN);
  236. hdr = skb_vnet_hdr(skb);
  237. sg_set_buf(sg, hdr, sizeof(*hdr));
  238. if (vi->big_packets) {
  239. for (i = 0; i < MAX_SKB_FRAGS; i++) {
  240. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  241. f->page = get_a_page(vi, GFP_ATOMIC);
  242. if (!f->page)
  243. break;
  244. f->page_offset = 0;
  245. f->size = PAGE_SIZE;
  246. skb->data_len += PAGE_SIZE;
  247. skb->len += PAGE_SIZE;
  248. skb_shinfo(skb)->nr_frags++;
  249. }
  250. }
  251. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  252. skb_queue_head(&vi->recv, skb);
  253. err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
  254. if (err) {
  255. skb_unlink(skb, &vi->recv);
  256. trim_pages(vi, skb);
  257. kfree_skb(skb);
  258. break;
  259. }
  260. vi->num++;
  261. }
  262. if (unlikely(vi->num > vi->max))
  263. vi->max = vi->num;
  264. vi->rvq->vq_ops->kick(vi->rvq);
  265. }
  266. static void try_fill_recv(struct virtnet_info *vi)
  267. {
  268. struct sk_buff *skb;
  269. struct scatterlist sg[1];
  270. int err;
  271. if (!vi->mergeable_rx_bufs) {
  272. try_fill_recv_maxbufs(vi);
  273. return;
  274. }
  275. for (;;) {
  276. skb_frag_t *f;
  277. skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
  278. if (unlikely(!skb))
  279. break;
  280. skb_reserve(skb, NET_IP_ALIGN);
  281. f = &skb_shinfo(skb)->frags[0];
  282. f->page = get_a_page(vi, GFP_ATOMIC);
  283. if (!f->page) {
  284. kfree_skb(skb);
  285. break;
  286. }
  287. f->page_offset = 0;
  288. f->size = PAGE_SIZE;
  289. skb_shinfo(skb)->nr_frags++;
  290. sg_init_one(sg, page_address(f->page), PAGE_SIZE);
  291. skb_queue_head(&vi->recv, skb);
  292. err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
  293. if (err) {
  294. skb_unlink(skb, &vi->recv);
  295. kfree_skb(skb);
  296. break;
  297. }
  298. vi->num++;
  299. }
  300. if (unlikely(vi->num > vi->max))
  301. vi->max = vi->num;
  302. vi->rvq->vq_ops->kick(vi->rvq);
  303. }
  304. static void skb_recv_done(struct virtqueue *rvq)
  305. {
  306. struct virtnet_info *vi = rvq->vdev->priv;
  307. /* Schedule NAPI, Suppress further interrupts if successful. */
  308. if (napi_schedule_prep(&vi->napi)) {
  309. rvq->vq_ops->disable_cb(rvq);
  310. __napi_schedule(&vi->napi);
  311. }
  312. }
  313. static int virtnet_poll(struct napi_struct *napi, int budget)
  314. {
  315. struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
  316. struct sk_buff *skb = NULL;
  317. unsigned int len, received = 0;
  318. again:
  319. while (received < budget &&
  320. (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
  321. __skb_unlink(skb, &vi->recv);
  322. receive_skb(vi->dev, skb, len);
  323. vi->num--;
  324. received++;
  325. }
  326. /* FIXME: If we oom and completely run out of inbufs, we need
  327. * to start a timer trying to fill more. */
  328. if (vi->num < vi->max / 2)
  329. try_fill_recv(vi);
  330. /* Out of packets? */
  331. if (received < budget) {
  332. napi_complete(napi);
  333. if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
  334. && napi_schedule_prep(napi)) {
  335. vi->rvq->vq_ops->disable_cb(vi->rvq);
  336. __napi_schedule(napi);
  337. goto again;
  338. }
  339. }
  340. return received;
  341. }
  342. static void free_old_xmit_skbs(struct virtnet_info *vi)
  343. {
  344. struct sk_buff *skb;
  345. unsigned int len;
  346. while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
  347. pr_debug("Sent skb %p\n", skb);
  348. __skb_unlink(skb, &vi->send);
  349. vi->dev->stats.tx_bytes += skb->len;
  350. vi->dev->stats.tx_packets++;
  351. kfree_skb(skb);
  352. }
  353. }
  354. /* If the virtio transport doesn't always notify us when all in-flight packets
  355. * are consumed, we fall back to using this function on a timer to free them. */
  356. static void xmit_free(unsigned long data)
  357. {
  358. struct virtnet_info *vi = (void *)data;
  359. netif_tx_lock(vi->dev);
  360. free_old_xmit_skbs(vi);
  361. if (!skb_queue_empty(&vi->send))
  362. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  363. netif_tx_unlock(vi->dev);
  364. }
  365. static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
  366. {
  367. int num, err;
  368. struct scatterlist sg[2+MAX_SKB_FRAGS];
  369. struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
  370. struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
  371. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  372. sg_init_table(sg, 2+MAX_SKB_FRAGS);
  373. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  374. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  375. hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  376. hdr->csum_start = skb->csum_start - skb_headroom(skb);
  377. hdr->csum_offset = skb->csum_offset;
  378. } else {
  379. hdr->flags = 0;
  380. hdr->csum_offset = hdr->csum_start = 0;
  381. }
  382. if (skb_is_gso(skb)) {
  383. hdr->hdr_len = skb_transport_header(skb) - skb->data;
  384. hdr->gso_size = skb_shinfo(skb)->gso_size;
  385. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  386. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  387. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  388. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  389. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  390. hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  391. else
  392. BUG();
  393. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  394. hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  395. } else {
  396. hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  397. hdr->gso_size = hdr->hdr_len = 0;
  398. }
  399. mhdr->num_buffers = 0;
  400. /* Encode metadata header at front. */
  401. if (vi->mergeable_rx_bufs)
  402. sg_set_buf(sg, mhdr, sizeof(*mhdr));
  403. else
  404. sg_set_buf(sg, hdr, sizeof(*hdr));
  405. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  406. err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
  407. if (!err && !vi->free_in_tasklet)
  408. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  409. return err;
  410. }
  411. static void xmit_tasklet(unsigned long data)
  412. {
  413. struct virtnet_info *vi = (void *)data;
  414. netif_tx_lock_bh(vi->dev);
  415. if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
  416. vi->svq->vq_ops->kick(vi->svq);
  417. vi->last_xmit_skb = NULL;
  418. }
  419. if (vi->free_in_tasklet)
  420. free_old_xmit_skbs(vi);
  421. netif_tx_unlock_bh(vi->dev);
  422. }
  423. static int start_xmit(struct sk_buff *skb, struct net_device *dev)
  424. {
  425. struct virtnet_info *vi = netdev_priv(dev);
  426. again:
  427. /* Free up any pending old buffers before queueing new ones. */
  428. free_old_xmit_skbs(vi);
  429. /* If we has a buffer left over from last time, send it now. */
  430. if (unlikely(vi->last_xmit_skb) &&
  431. xmit_skb(vi, vi->last_xmit_skb) != 0)
  432. goto stop_queue;
  433. vi->last_xmit_skb = NULL;
  434. /* Put new one in send queue and do transmit */
  435. if (likely(skb)) {
  436. __skb_queue_head(&vi->send, skb);
  437. if (xmit_skb(vi, skb) != 0) {
  438. vi->last_xmit_skb = skb;
  439. skb = NULL;
  440. goto stop_queue;
  441. }
  442. }
  443. done:
  444. vi->svq->vq_ops->kick(vi->svq);
  445. return NETDEV_TX_OK;
  446. stop_queue:
  447. pr_debug("%s: virtio not prepared to send\n", dev->name);
  448. netif_stop_queue(dev);
  449. /* Activate callback for using skbs: if this returns false it
  450. * means some were used in the meantime. */
  451. if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
  452. vi->svq->vq_ops->disable_cb(vi->svq);
  453. netif_start_queue(dev);
  454. goto again;
  455. }
  456. if (skb) {
  457. /* Drop this skb: we only queue one. */
  458. vi->dev->stats.tx_dropped++;
  459. kfree_skb(skb);
  460. }
  461. goto done;
  462. }
  463. #ifdef CONFIG_NET_POLL_CONTROLLER
  464. static void virtnet_netpoll(struct net_device *dev)
  465. {
  466. struct virtnet_info *vi = netdev_priv(dev);
  467. napi_schedule(&vi->napi);
  468. }
  469. #endif
  470. static int virtnet_open(struct net_device *dev)
  471. {
  472. struct virtnet_info *vi = netdev_priv(dev);
  473. napi_enable(&vi->napi);
  474. /* If all buffers were filled by other side before we napi_enabled, we
  475. * won't get another interrupt, so process any outstanding packets
  476. * now. virtnet_poll wants re-enable the queue, so we disable here.
  477. * We synchronize against interrupts via NAPI_STATE_SCHED */
  478. if (napi_schedule_prep(&vi->napi)) {
  479. vi->rvq->vq_ops->disable_cb(vi->rvq);
  480. __napi_schedule(&vi->napi);
  481. }
  482. return 0;
  483. }
  484. static int virtnet_close(struct net_device *dev)
  485. {
  486. struct virtnet_info *vi = netdev_priv(dev);
  487. napi_disable(&vi->napi);
  488. return 0;
  489. }
  490. static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
  491. {
  492. struct virtnet_info *vi = netdev_priv(dev);
  493. struct virtio_device *vdev = vi->vdev;
  494. if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
  495. return -ENOSYS;
  496. return ethtool_op_set_tx_hw_csum(dev, data);
  497. }
  498. static struct ethtool_ops virtnet_ethtool_ops = {
  499. .set_tx_csum = virtnet_set_tx_csum,
  500. .set_sg = ethtool_op_set_sg,
  501. .set_tso = ethtool_op_set_tso,
  502. .get_link = ethtool_op_get_link,
  503. };
  504. #define MIN_MTU 68
  505. #define MAX_MTU 65535
  506. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  507. {
  508. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  509. return -EINVAL;
  510. dev->mtu = new_mtu;
  511. return 0;
  512. }
  513. static const struct net_device_ops virtnet_netdev = {
  514. .ndo_open = virtnet_open,
  515. .ndo_stop = virtnet_close,
  516. .ndo_start_xmit = start_xmit,
  517. .ndo_validate_addr = eth_validate_addr,
  518. .ndo_set_mac_address = eth_mac_addr,
  519. .ndo_change_mtu = virtnet_change_mtu,
  520. #ifdef CONFIG_NET_POLL_CONTROLLER
  521. .ndo_poll_controller = virtnet_netpoll,
  522. #endif
  523. };
  524. static void virtnet_update_status(struct virtnet_info *vi)
  525. {
  526. u16 v;
  527. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
  528. return;
  529. vi->vdev->config->get(vi->vdev,
  530. offsetof(struct virtio_net_config, status),
  531. &v, sizeof(v));
  532. /* Ignore unknown (future) status bits */
  533. v &= VIRTIO_NET_S_LINK_UP;
  534. if (vi->status == v)
  535. return;
  536. vi->status = v;
  537. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  538. netif_carrier_on(vi->dev);
  539. netif_wake_queue(vi->dev);
  540. } else {
  541. netif_carrier_off(vi->dev);
  542. netif_stop_queue(vi->dev);
  543. }
  544. }
  545. static void virtnet_config_changed(struct virtio_device *vdev)
  546. {
  547. struct virtnet_info *vi = vdev->priv;
  548. virtnet_update_status(vi);
  549. }
  550. static int virtnet_probe(struct virtio_device *vdev)
  551. {
  552. int err;
  553. struct net_device *dev;
  554. struct virtnet_info *vi;
  555. /* Allocate ourselves a network device with room for our info */
  556. dev = alloc_etherdev(sizeof(struct virtnet_info));
  557. if (!dev)
  558. return -ENOMEM;
  559. /* Set up network device as normal. */
  560. dev->netdev_ops = &virtnet_netdev;
  561. dev->features = NETIF_F_HIGHDMA;
  562. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  563. SET_NETDEV_DEV(dev, &vdev->dev);
  564. /* Do we support "hardware" checksums? */
  565. if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  566. /* This opens up the world of extra features. */
  567. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  568. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  569. dev->features |= NETIF_F_TSO | NETIF_F_UFO
  570. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  571. }
  572. /* Individual feature bits: what can host handle? */
  573. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  574. dev->features |= NETIF_F_TSO;
  575. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  576. dev->features |= NETIF_F_TSO6;
  577. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  578. dev->features |= NETIF_F_TSO_ECN;
  579. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  580. dev->features |= NETIF_F_UFO;
  581. }
  582. /* Configuration may specify what MAC to use. Otherwise random. */
  583. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  584. vdev->config->get(vdev,
  585. offsetof(struct virtio_net_config, mac),
  586. dev->dev_addr, dev->addr_len);
  587. } else
  588. random_ether_addr(dev->dev_addr);
  589. /* Set up our device-specific information */
  590. vi = netdev_priv(dev);
  591. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  592. vi->dev = dev;
  593. vi->vdev = vdev;
  594. vdev->priv = vi;
  595. vi->pages = NULL;
  596. /* If they give us a callback when all buffers are done, we don't need
  597. * the timer. */
  598. vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
  599. /* If we can receive ANY GSO packets, we must allocate large ones. */
  600. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
  601. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
  602. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  603. vi->big_packets = true;
  604. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  605. vi->mergeable_rx_bufs = true;
  606. /* We expect two virtqueues, receive then send. */
  607. vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
  608. if (IS_ERR(vi->rvq)) {
  609. err = PTR_ERR(vi->rvq);
  610. goto free;
  611. }
  612. vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
  613. if (IS_ERR(vi->svq)) {
  614. err = PTR_ERR(vi->svq);
  615. goto free_recv;
  616. }
  617. /* Initialize our empty receive and send queues. */
  618. skb_queue_head_init(&vi->recv);
  619. skb_queue_head_init(&vi->send);
  620. tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
  621. if (!vi->free_in_tasklet)
  622. setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
  623. err = register_netdev(dev);
  624. if (err) {
  625. pr_debug("virtio_net: registering device failed\n");
  626. goto free_send;
  627. }
  628. /* Last of all, set up some receive buffers. */
  629. try_fill_recv(vi);
  630. /* If we didn't even get one input buffer, we're useless. */
  631. if (vi->num == 0) {
  632. err = -ENOMEM;
  633. goto unregister;
  634. }
  635. vi->status = VIRTIO_NET_S_LINK_UP;
  636. virtnet_update_status(vi);
  637. pr_debug("virtnet: registered device %s\n", dev->name);
  638. return 0;
  639. unregister:
  640. unregister_netdev(dev);
  641. free_send:
  642. vdev->config->del_vq(vi->svq);
  643. free_recv:
  644. vdev->config->del_vq(vi->rvq);
  645. free:
  646. free_netdev(dev);
  647. return err;
  648. }
  649. static void virtnet_remove(struct virtio_device *vdev)
  650. {
  651. struct virtnet_info *vi = vdev->priv;
  652. struct sk_buff *skb;
  653. /* Stop all the virtqueues. */
  654. vdev->config->reset(vdev);
  655. if (!vi->free_in_tasklet)
  656. del_timer_sync(&vi->xmit_free_timer);
  657. /* Free our skbs in send and recv queues, if any. */
  658. while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
  659. kfree_skb(skb);
  660. vi->num--;
  661. }
  662. __skb_queue_purge(&vi->send);
  663. BUG_ON(vi->num != 0);
  664. vdev->config->del_vq(vi->svq);
  665. vdev->config->del_vq(vi->rvq);
  666. unregister_netdev(vi->dev);
  667. while (vi->pages)
  668. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  669. free_netdev(vi->dev);
  670. }
  671. static struct virtio_device_id id_table[] = {
  672. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  673. { 0 },
  674. };
  675. static unsigned int features[] = {
  676. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  677. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  678. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  679. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  680. VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
  681. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS,
  682. VIRTIO_F_NOTIFY_ON_EMPTY,
  683. };
  684. static struct virtio_driver virtio_net = {
  685. .feature_table = features,
  686. .feature_table_size = ARRAY_SIZE(features),
  687. .driver.name = KBUILD_MODNAME,
  688. .driver.owner = THIS_MODULE,
  689. .id_table = id_table,
  690. .probe = virtnet_probe,
  691. .remove = __devexit_p(virtnet_remove),
  692. .config_changed = virtnet_config_changed,
  693. };
  694. static int __init init(void)
  695. {
  696. return register_virtio_driver(&virtio_net);
  697. }
  698. static void __exit fini(void)
  699. {
  700. unregister_virtio_driver(&virtio_net);
  701. }
  702. module_init(init);
  703. module_exit(fini);
  704. MODULE_DEVICE_TABLE(virtio, id_table);
  705. MODULE_DESCRIPTION("Virtio network driver");
  706. MODULE_LICENSE("GPL");