virtio_net.c 27 KB

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