virtio_net.c 20 KB

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