virtio_net.c 26 KB

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