virtio_net.c 26 KB

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