virtio_net.c 26 KB

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