virtio_net.c 25 KB

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