virtio_net.c 24 KB

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