virtio_net.c 26 KB

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