virtio_net.c 20 KB

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