virtio_net.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 2
  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 + NET_IP_ALIGN);
  234. if (unlikely(!skb))
  235. break;
  236. skb_reserve(skb, NET_IP_ALIGN);
  237. skb_put(skb, MAX_PACKET_LEN);
  238. hdr = skb_vnet_hdr(skb);
  239. sg_set_buf(sg, hdr, sizeof(*hdr));
  240. if (vi->big_packets) {
  241. for (i = 0; i < MAX_SKB_FRAGS; i++) {
  242. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  243. f->page = get_a_page(vi, GFP_ATOMIC);
  244. if (!f->page)
  245. break;
  246. f->page_offset = 0;
  247. f->size = PAGE_SIZE;
  248. skb->data_len += PAGE_SIZE;
  249. skb->len += PAGE_SIZE;
  250. skb_shinfo(skb)->nr_frags++;
  251. }
  252. }
  253. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  254. skb_queue_head(&vi->recv, skb);
  255. err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
  256. if (err) {
  257. skb_unlink(skb, &vi->recv);
  258. trim_pages(vi, skb);
  259. kfree_skb(skb);
  260. break;
  261. }
  262. vi->num++;
  263. }
  264. if (unlikely(vi->num > vi->max))
  265. vi->max = vi->num;
  266. vi->rvq->vq_ops->kick(vi->rvq);
  267. }
  268. static void try_fill_recv(struct virtnet_info *vi)
  269. {
  270. struct sk_buff *skb;
  271. struct scatterlist sg[1];
  272. int err;
  273. if (!vi->mergeable_rx_bufs) {
  274. try_fill_recv_maxbufs(vi);
  275. return;
  276. }
  277. for (;;) {
  278. skb_frag_t *f;
  279. skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
  280. if (unlikely(!skb))
  281. break;
  282. skb_reserve(skb, NET_IP_ALIGN);
  283. f = &skb_shinfo(skb)->frags[0];
  284. f->page = get_a_page(vi, GFP_ATOMIC);
  285. if (!f->page) {
  286. kfree_skb(skb);
  287. break;
  288. }
  289. f->page_offset = 0;
  290. f->size = PAGE_SIZE;
  291. skb_shinfo(skb)->nr_frags++;
  292. sg_init_one(sg, page_address(f->page), PAGE_SIZE);
  293. skb_queue_head(&vi->recv, skb);
  294. err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
  295. if (err) {
  296. skb_unlink(skb, &vi->recv);
  297. kfree_skb(skb);
  298. break;
  299. }
  300. vi->num++;
  301. }
  302. if (unlikely(vi->num > vi->max))
  303. vi->max = vi->num;
  304. vi->rvq->vq_ops->kick(vi->rvq);
  305. }
  306. static void skb_recv_done(struct virtqueue *rvq)
  307. {
  308. struct virtnet_info *vi = rvq->vdev->priv;
  309. /* Schedule NAPI, Suppress further interrupts if successful. */
  310. if (napi_schedule_prep(&vi->napi)) {
  311. rvq->vq_ops->disable_cb(rvq);
  312. __napi_schedule(&vi->napi);
  313. }
  314. }
  315. static int virtnet_poll(struct napi_struct *napi, int budget)
  316. {
  317. struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
  318. struct sk_buff *skb = NULL;
  319. unsigned int len, received = 0;
  320. again:
  321. while (received < budget &&
  322. (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
  323. __skb_unlink(skb, &vi->recv);
  324. receive_skb(vi->dev, skb, len);
  325. vi->num--;
  326. received++;
  327. }
  328. /* FIXME: If we oom and completely run out of inbufs, we need
  329. * to start a timer trying to fill more. */
  330. if (vi->num < vi->max / 2)
  331. try_fill_recv(vi);
  332. /* Out of packets? */
  333. if (received < budget) {
  334. napi_complete(napi);
  335. if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
  336. && napi_schedule_prep(napi)) {
  337. vi->rvq->vq_ops->disable_cb(vi->rvq);
  338. __napi_schedule(napi);
  339. goto again;
  340. }
  341. }
  342. return received;
  343. }
  344. static void free_old_xmit_skbs(struct virtnet_info *vi)
  345. {
  346. struct sk_buff *skb;
  347. unsigned int len;
  348. while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
  349. pr_debug("Sent skb %p\n", skb);
  350. __skb_unlink(skb, &vi->send);
  351. vi->dev->stats.tx_bytes += skb->len;
  352. vi->dev->stats.tx_packets++;
  353. kfree_skb(skb);
  354. }
  355. }
  356. /* If the virtio transport doesn't always notify us when all in-flight packets
  357. * are consumed, we fall back to using this function on a timer to free them. */
  358. static void xmit_free(unsigned long data)
  359. {
  360. struct virtnet_info *vi = (void *)data;
  361. netif_tx_lock(vi->dev);
  362. free_old_xmit_skbs(vi);
  363. if (!skb_queue_empty(&vi->send))
  364. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  365. netif_tx_unlock(vi->dev);
  366. }
  367. static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
  368. {
  369. int num, err;
  370. struct scatterlist sg[2+MAX_SKB_FRAGS];
  371. struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
  372. struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
  373. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  374. sg_init_table(sg, 2+MAX_SKB_FRAGS);
  375. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  376. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  377. hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  378. hdr->csum_start = skb->csum_start - skb_headroom(skb);
  379. hdr->csum_offset = skb->csum_offset;
  380. } else {
  381. hdr->flags = 0;
  382. hdr->csum_offset = hdr->csum_start = 0;
  383. }
  384. if (skb_is_gso(skb)) {
  385. hdr->hdr_len = skb_headlen(skb);
  386. hdr->gso_size = skb_shinfo(skb)->gso_size;
  387. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  388. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  389. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  390. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  391. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  392. hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  393. else
  394. BUG();
  395. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  396. hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  397. } else {
  398. hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  399. hdr->gso_size = hdr->hdr_len = 0;
  400. }
  401. mhdr->num_buffers = 0;
  402. /* Encode metadata header at front. */
  403. if (vi->mergeable_rx_bufs)
  404. sg_set_buf(sg, mhdr, sizeof(*mhdr));
  405. else
  406. sg_set_buf(sg, hdr, sizeof(*hdr));
  407. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  408. err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
  409. if (!err && !vi->free_in_tasklet)
  410. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  411. return err;
  412. }
  413. static void xmit_tasklet(unsigned long data)
  414. {
  415. struct virtnet_info *vi = (void *)data;
  416. netif_tx_lock_bh(vi->dev);
  417. if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
  418. vi->svq->vq_ops->kick(vi->svq);
  419. vi->last_xmit_skb = NULL;
  420. }
  421. if (vi->free_in_tasklet)
  422. free_old_xmit_skbs(vi);
  423. netif_tx_unlock_bh(vi->dev);
  424. }
  425. static int start_xmit(struct sk_buff *skb, struct net_device *dev)
  426. {
  427. struct virtnet_info *vi = netdev_priv(dev);
  428. again:
  429. /* Free up any pending old buffers before queueing new ones. */
  430. free_old_xmit_skbs(vi);
  431. /* If we has a buffer left over from last time, send it now. */
  432. if (unlikely(vi->last_xmit_skb) &&
  433. xmit_skb(vi, vi->last_xmit_skb) != 0)
  434. goto stop_queue;
  435. vi->last_xmit_skb = NULL;
  436. /* Put new one in send queue and do transmit */
  437. if (likely(skb)) {
  438. __skb_queue_head(&vi->send, skb);
  439. if (xmit_skb(vi, skb) != 0) {
  440. vi->last_xmit_skb = skb;
  441. skb = NULL;
  442. goto stop_queue;
  443. }
  444. }
  445. done:
  446. vi->svq->vq_ops->kick(vi->svq);
  447. return NETDEV_TX_OK;
  448. stop_queue:
  449. pr_debug("%s: virtio not prepared to send\n", dev->name);
  450. netif_stop_queue(dev);
  451. /* Activate callback for using skbs: if this returns false it
  452. * means some were used in the meantime. */
  453. if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
  454. vi->svq->vq_ops->disable_cb(vi->svq);
  455. netif_start_queue(dev);
  456. goto again;
  457. }
  458. if (skb) {
  459. /* Drop this skb: we only queue one. */
  460. vi->dev->stats.tx_dropped++;
  461. kfree_skb(skb);
  462. }
  463. goto done;
  464. }
  465. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  466. {
  467. struct virtnet_info *vi = netdev_priv(dev);
  468. struct virtio_device *vdev = vi->vdev;
  469. int ret;
  470. ret = eth_mac_addr(dev, p);
  471. if (ret)
  472. return ret;
  473. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  474. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  475. dev->dev_addr, dev->addr_len);
  476. return 0;
  477. }
  478. #ifdef CONFIG_NET_POLL_CONTROLLER
  479. static void virtnet_netpoll(struct net_device *dev)
  480. {
  481. struct virtnet_info *vi = netdev_priv(dev);
  482. napi_schedule(&vi->napi);
  483. }
  484. #endif
  485. static int virtnet_open(struct net_device *dev)
  486. {
  487. struct virtnet_info *vi = netdev_priv(dev);
  488. napi_enable(&vi->napi);
  489. /* If all buffers were filled by other side before we napi_enabled, we
  490. * won't get another interrupt, so process any outstanding packets
  491. * now. virtnet_poll wants re-enable the queue, so we disable here.
  492. * We synchronize against interrupts via NAPI_STATE_SCHED */
  493. if (napi_schedule_prep(&vi->napi)) {
  494. vi->rvq->vq_ops->disable_cb(vi->rvq);
  495. __napi_schedule(&vi->napi);
  496. }
  497. return 0;
  498. }
  499. /*
  500. * Send command via the control virtqueue and check status. Commands
  501. * supported by the hypervisor, as indicated by feature bits, should
  502. * never fail unless improperly formated.
  503. */
  504. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  505. struct scatterlist *data, int out, int in)
  506. {
  507. struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  508. struct virtio_net_ctrl_hdr ctrl;
  509. virtio_net_ctrl_ack status = ~0;
  510. unsigned int tmp;
  511. int i;
  512. /* Caller should know better */
  513. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
  514. (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
  515. out++; /* Add header */
  516. in++; /* Add return status */
  517. ctrl.class = class;
  518. ctrl.cmd = cmd;
  519. sg_init_table(sg, out + in);
  520. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  521. for_each_sg(data, s, out + in - 2, i)
  522. sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
  523. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  524. BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi));
  525. vi->cvq->vq_ops->kick(vi->cvq);
  526. /*
  527. * Spin for a response, the kick causes an ioport write, trapping
  528. * into the hypervisor, so the request should be handled immediately.
  529. */
  530. while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
  531. cpu_relax();
  532. return status == VIRTIO_NET_OK;
  533. }
  534. static int virtnet_close(struct net_device *dev)
  535. {
  536. struct virtnet_info *vi = netdev_priv(dev);
  537. napi_disable(&vi->napi);
  538. return 0;
  539. }
  540. static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
  541. {
  542. struct virtnet_info *vi = netdev_priv(dev);
  543. struct virtio_device *vdev = vi->vdev;
  544. if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
  545. return -ENOSYS;
  546. return ethtool_op_set_tx_hw_csum(dev, data);
  547. }
  548. static void virtnet_set_rx_mode(struct net_device *dev)
  549. {
  550. struct virtnet_info *vi = netdev_priv(dev);
  551. struct scatterlist sg[2];
  552. u8 promisc, allmulti;
  553. struct virtio_net_ctrl_mac *mac_data;
  554. struct dev_addr_list *addr;
  555. struct netdev_hw_addr *ha;
  556. void *buf;
  557. int i;
  558. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  559. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  560. return;
  561. promisc = ((dev->flags & IFF_PROMISC) != 0);
  562. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  563. sg_init_one(sg, &promisc, sizeof(promisc));
  564. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  565. VIRTIO_NET_CTRL_RX_PROMISC,
  566. sg, 1, 0))
  567. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  568. promisc ? "en" : "dis");
  569. sg_init_one(sg, &allmulti, sizeof(allmulti));
  570. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  571. VIRTIO_NET_CTRL_RX_ALLMULTI,
  572. sg, 1, 0))
  573. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  574. allmulti ? "en" : "dis");
  575. /* MAC filter - use one buffer for both lists */
  576. mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
  577. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  578. if (!buf) {
  579. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  580. return;
  581. }
  582. sg_init_table(sg, 2);
  583. /* Store the unicast list and count in the front of the buffer */
  584. mac_data->entries = dev->uc.count;
  585. i = 0;
  586. list_for_each_entry(ha, &dev->uc.list, list)
  587. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  588. sg_set_buf(&sg[0], mac_data,
  589. sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
  590. /* multicast list and count fill the end */
  591. mac_data = (void *)&mac_data->macs[dev->uc.count][0];
  592. mac_data->entries = dev->mc_count;
  593. addr = dev->mc_list;
  594. for (i = 0; i < dev->mc_count; i++, addr = addr->next)
  595. memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
  596. sg_set_buf(&sg[1], mac_data,
  597. sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
  598. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  599. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  600. sg, 2, 0))
  601. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  602. kfree(buf);
  603. }
  604. static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  605. {
  606. struct virtnet_info *vi = netdev_priv(dev);
  607. struct scatterlist sg;
  608. sg_init_one(&sg, &vid, sizeof(vid));
  609. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  610. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  611. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  612. }
  613. static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  614. {
  615. struct virtnet_info *vi = netdev_priv(dev);
  616. struct scatterlist sg;
  617. sg_init_one(&sg, &vid, sizeof(vid));
  618. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  619. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  620. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  621. }
  622. static struct ethtool_ops virtnet_ethtool_ops = {
  623. .set_tx_csum = virtnet_set_tx_csum,
  624. .set_sg = ethtool_op_set_sg,
  625. .set_tso = ethtool_op_set_tso,
  626. .get_link = ethtool_op_get_link,
  627. };
  628. #define MIN_MTU 68
  629. #define MAX_MTU 65535
  630. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  631. {
  632. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  633. return -EINVAL;
  634. dev->mtu = new_mtu;
  635. return 0;
  636. }
  637. static const struct net_device_ops virtnet_netdev = {
  638. .ndo_open = virtnet_open,
  639. .ndo_stop = virtnet_close,
  640. .ndo_start_xmit = start_xmit,
  641. .ndo_validate_addr = eth_validate_addr,
  642. .ndo_set_mac_address = virtnet_set_mac_address,
  643. .ndo_set_rx_mode = virtnet_set_rx_mode,
  644. .ndo_change_mtu = virtnet_change_mtu,
  645. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  646. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  647. #ifdef CONFIG_NET_POLL_CONTROLLER
  648. .ndo_poll_controller = virtnet_netpoll,
  649. #endif
  650. };
  651. static void virtnet_update_status(struct virtnet_info *vi)
  652. {
  653. u16 v;
  654. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
  655. return;
  656. vi->vdev->config->get(vi->vdev,
  657. offsetof(struct virtio_net_config, status),
  658. &v, sizeof(v));
  659. /* Ignore unknown (future) status bits */
  660. v &= VIRTIO_NET_S_LINK_UP;
  661. if (vi->status == v)
  662. return;
  663. vi->status = v;
  664. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  665. netif_carrier_on(vi->dev);
  666. netif_wake_queue(vi->dev);
  667. } else {
  668. netif_carrier_off(vi->dev);
  669. netif_stop_queue(vi->dev);
  670. }
  671. }
  672. static void virtnet_config_changed(struct virtio_device *vdev)
  673. {
  674. struct virtnet_info *vi = vdev->priv;
  675. virtnet_update_status(vi);
  676. }
  677. static int virtnet_probe(struct virtio_device *vdev)
  678. {
  679. int err;
  680. struct net_device *dev;
  681. struct virtnet_info *vi;
  682. struct virtqueue *vqs[3];
  683. vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
  684. const char *names[] = { "input", "output", "control" };
  685. int nvqs;
  686. /* Allocate ourselves a network device with room for our info */
  687. dev = alloc_etherdev(sizeof(struct virtnet_info));
  688. if (!dev)
  689. return -ENOMEM;
  690. /* Set up network device as normal. */
  691. dev->netdev_ops = &virtnet_netdev;
  692. dev->features = NETIF_F_HIGHDMA;
  693. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  694. SET_NETDEV_DEV(dev, &vdev->dev);
  695. /* Do we support "hardware" checksums? */
  696. if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  697. /* This opens up the world of extra features. */
  698. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  699. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  700. dev->features |= NETIF_F_TSO | NETIF_F_UFO
  701. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  702. }
  703. /* Individual feature bits: what can host handle? */
  704. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  705. dev->features |= NETIF_F_TSO;
  706. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  707. dev->features |= NETIF_F_TSO6;
  708. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  709. dev->features |= NETIF_F_TSO_ECN;
  710. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  711. dev->features |= NETIF_F_UFO;
  712. }
  713. /* Configuration may specify what MAC to use. Otherwise random. */
  714. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  715. vdev->config->get(vdev,
  716. offsetof(struct virtio_net_config, mac),
  717. dev->dev_addr, dev->addr_len);
  718. } else
  719. random_ether_addr(dev->dev_addr);
  720. /* Set up our device-specific information */
  721. vi = netdev_priv(dev);
  722. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  723. vi->dev = dev;
  724. vi->vdev = vdev;
  725. vdev->priv = vi;
  726. vi->pages = NULL;
  727. /* If they give us a callback when all buffers are done, we don't need
  728. * the timer. */
  729. vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
  730. /* If we can receive ANY GSO packets, we must allocate large ones. */
  731. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
  732. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
  733. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  734. vi->big_packets = true;
  735. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  736. vi->mergeable_rx_bufs = true;
  737. /* We expect two virtqueues, receive then send,
  738. * and optionally control. */
  739. nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
  740. err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
  741. if (err)
  742. goto free;
  743. vi->rvq = vqs[0];
  744. vi->svq = vqs[1];
  745. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  746. vi->cvq = vqs[2];
  747. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  748. dev->features |= NETIF_F_HW_VLAN_FILTER;
  749. }
  750. /* Initialize our empty receive and send queues. */
  751. skb_queue_head_init(&vi->recv);
  752. skb_queue_head_init(&vi->send);
  753. tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
  754. if (!vi->free_in_tasklet)
  755. setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
  756. err = register_netdev(dev);
  757. if (err) {
  758. pr_debug("virtio_net: registering device failed\n");
  759. goto free_vqs;
  760. }
  761. /* Last of all, set up some receive buffers. */
  762. try_fill_recv(vi);
  763. /* If we didn't even get one input buffer, we're useless. */
  764. if (vi->num == 0) {
  765. err = -ENOMEM;
  766. goto unregister;
  767. }
  768. vi->status = VIRTIO_NET_S_LINK_UP;
  769. virtnet_update_status(vi);
  770. netif_carrier_on(dev);
  771. pr_debug("virtnet: registered device %s\n", dev->name);
  772. return 0;
  773. unregister:
  774. unregister_netdev(dev);
  775. free_vqs:
  776. vdev->config->del_vqs(vdev);
  777. free:
  778. free_netdev(dev);
  779. return err;
  780. }
  781. static void virtnet_remove(struct virtio_device *vdev)
  782. {
  783. struct virtnet_info *vi = vdev->priv;
  784. struct sk_buff *skb;
  785. /* Stop all the virtqueues. */
  786. vdev->config->reset(vdev);
  787. if (!vi->free_in_tasklet)
  788. del_timer_sync(&vi->xmit_free_timer);
  789. /* Free our skbs in send and recv queues, if any. */
  790. while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
  791. kfree_skb(skb);
  792. vi->num--;
  793. }
  794. __skb_queue_purge(&vi->send);
  795. BUG_ON(vi->num != 0);
  796. unregister_netdev(vi->dev);
  797. vdev->config->del_vqs(vi->vdev);
  798. while (vi->pages)
  799. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  800. free_netdev(vi->dev);
  801. }
  802. static struct virtio_device_id id_table[] = {
  803. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  804. { 0 },
  805. };
  806. static unsigned int features[] = {
  807. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  808. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  809. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  810. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  811. VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
  812. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  813. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  814. VIRTIO_F_NOTIFY_ON_EMPTY,
  815. };
  816. static struct virtio_driver virtio_net = {
  817. .feature_table = features,
  818. .feature_table_size = ARRAY_SIZE(features),
  819. .driver.name = KBUILD_MODNAME,
  820. .driver.owner = THIS_MODULE,
  821. .id_table = id_table,
  822. .probe = virtnet_probe,
  823. .remove = __devexit_p(virtnet_remove),
  824. .config_changed = virtnet_config_changed,
  825. };
  826. static int __init init(void)
  827. {
  828. return register_virtio_driver(&virtio_net);
  829. }
  830. static void __exit fini(void)
  831. {
  832. unregister_virtio_driver(&virtio_net);
  833. }
  834. module_init(init);
  835. module_exit(fini);
  836. MODULE_DEVICE_TABLE(virtio, id_table);
  837. MODULE_DESCRIPTION("Virtio network driver");
  838. MODULE_LICENSE("GPL");