virtio_net.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. #include <linux/slab.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_stats {
  39. struct u64_stats_sync syncp;
  40. u64 tx_bytes;
  41. u64 tx_packets;
  42. u64 rx_bytes;
  43. u64 rx_packets;
  44. };
  45. struct virtnet_info {
  46. struct virtio_device *vdev;
  47. struct virtqueue *rvq, *svq, *cvq;
  48. struct net_device *dev;
  49. struct napi_struct napi;
  50. unsigned int status;
  51. /* Number of input buffers, and max we've ever had. */
  52. unsigned int num, max;
  53. /* I like... big packets and I cannot lie! */
  54. bool big_packets;
  55. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  56. bool mergeable_rx_bufs;
  57. /* Active statistics */
  58. struct virtnet_stats __percpu *stats;
  59. /* Work struct for refilling if we run low on memory. */
  60. struct delayed_work refill;
  61. /* Chain pages by the private ptr. */
  62. struct page *pages;
  63. /* fragments + linear part + virtio header */
  64. struct scatterlist rx_sg[MAX_SKB_FRAGS + 2];
  65. struct scatterlist tx_sg[MAX_SKB_FRAGS + 2];
  66. };
  67. struct skb_vnet_hdr {
  68. union {
  69. struct virtio_net_hdr hdr;
  70. struct virtio_net_hdr_mrg_rxbuf mhdr;
  71. };
  72. unsigned int num_sg;
  73. };
  74. struct padded_vnet_hdr {
  75. struct virtio_net_hdr hdr;
  76. /*
  77. * virtio_net_hdr should be in a separated sg buffer because of a
  78. * QEMU bug, and data sg buffer shares same page with this header sg.
  79. * This padding makes next sg 16 byte aligned after virtio_net_hdr.
  80. */
  81. char padding[6];
  82. };
  83. static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
  84. {
  85. return (struct skb_vnet_hdr *)skb->cb;
  86. }
  87. /*
  88. * private is used to chain pages for big packets, put the whole
  89. * most recent used list in the beginning for reuse
  90. */
  91. static void give_pages(struct virtnet_info *vi, struct page *page)
  92. {
  93. struct page *end;
  94. /* Find end of list, sew whole thing into vi->pages. */
  95. for (end = page; end->private; end = (struct page *)end->private);
  96. end->private = (unsigned long)vi->pages;
  97. vi->pages = page;
  98. }
  99. static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
  100. {
  101. struct page *p = vi->pages;
  102. if (p) {
  103. vi->pages = (struct page *)p->private;
  104. /* clear private here, it is used to chain pages */
  105. p->private = 0;
  106. } else
  107. p = alloc_page(gfp_mask);
  108. return p;
  109. }
  110. static void skb_xmit_done(struct virtqueue *svq)
  111. {
  112. struct virtnet_info *vi = svq->vdev->priv;
  113. /* Suppress further interrupts. */
  114. virtqueue_disable_cb(svq);
  115. /* We were probably waiting for more output buffers. */
  116. netif_wake_queue(vi->dev);
  117. }
  118. static void set_skb_frag(struct sk_buff *skb, struct page *page,
  119. unsigned int offset, unsigned int *len)
  120. {
  121. int size = min((unsigned)PAGE_SIZE - offset, *len);
  122. int i = skb_shinfo(skb)->nr_frags;
  123. __skb_fill_page_desc(skb, i, page, offset, size);
  124. skb->data_len += size;
  125. skb->len += size;
  126. skb->truesize += PAGE_SIZE;
  127. skb_shinfo(skb)->nr_frags++;
  128. *len -= size;
  129. }
  130. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  131. struct page *page, unsigned int len)
  132. {
  133. struct sk_buff *skb;
  134. struct skb_vnet_hdr *hdr;
  135. unsigned int copy, hdr_len, offset;
  136. char *p;
  137. p = page_address(page);
  138. /* copy small packet so we can reuse these pages for small data */
  139. skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
  140. if (unlikely(!skb))
  141. return NULL;
  142. hdr = skb_vnet_hdr(skb);
  143. if (vi->mergeable_rx_bufs) {
  144. hdr_len = sizeof hdr->mhdr;
  145. offset = hdr_len;
  146. } else {
  147. hdr_len = sizeof hdr->hdr;
  148. offset = sizeof(struct padded_vnet_hdr);
  149. }
  150. memcpy(hdr, p, hdr_len);
  151. len -= hdr_len;
  152. p += offset;
  153. copy = len;
  154. if (copy > skb_tailroom(skb))
  155. copy = skb_tailroom(skb);
  156. memcpy(skb_put(skb, copy), p, copy);
  157. len -= copy;
  158. offset += copy;
  159. /*
  160. * Verify that we can indeed put this data into a skb.
  161. * This is here to handle cases when the device erroneously
  162. * tries to receive more than is possible. This is usually
  163. * the case of a broken device.
  164. */
  165. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  166. if (net_ratelimit())
  167. pr_debug("%s: too much data\n", skb->dev->name);
  168. dev_kfree_skb(skb);
  169. return NULL;
  170. }
  171. while (len) {
  172. set_skb_frag(skb, page, offset, &len);
  173. page = (struct page *)page->private;
  174. offset = 0;
  175. }
  176. if (page)
  177. give_pages(vi, page);
  178. return skb;
  179. }
  180. static int receive_mergeable(struct virtnet_info *vi, struct sk_buff *skb)
  181. {
  182. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  183. struct page *page;
  184. int num_buf, i, len;
  185. num_buf = hdr->mhdr.num_buffers;
  186. while (--num_buf) {
  187. i = skb_shinfo(skb)->nr_frags;
  188. if (i >= MAX_SKB_FRAGS) {
  189. pr_debug("%s: packet too long\n", skb->dev->name);
  190. skb->dev->stats.rx_length_errors++;
  191. return -EINVAL;
  192. }
  193. page = virtqueue_get_buf(vi->rvq, &len);
  194. if (!page) {
  195. pr_debug("%s: rx error: %d buffers missing\n",
  196. skb->dev->name, hdr->mhdr.num_buffers);
  197. skb->dev->stats.rx_length_errors++;
  198. return -EINVAL;
  199. }
  200. if (len > PAGE_SIZE)
  201. len = PAGE_SIZE;
  202. set_skb_frag(skb, page, 0, &len);
  203. --vi->num;
  204. }
  205. return 0;
  206. }
  207. static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
  208. {
  209. struct virtnet_info *vi = netdev_priv(dev);
  210. struct virtnet_stats __percpu *stats = this_cpu_ptr(vi->stats);
  211. struct sk_buff *skb;
  212. struct page *page;
  213. struct skb_vnet_hdr *hdr;
  214. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  215. pr_debug("%s: short packet %i\n", dev->name, len);
  216. dev->stats.rx_length_errors++;
  217. if (vi->mergeable_rx_bufs || vi->big_packets)
  218. give_pages(vi, buf);
  219. else
  220. dev_kfree_skb(buf);
  221. return;
  222. }
  223. if (!vi->mergeable_rx_bufs && !vi->big_packets) {
  224. skb = buf;
  225. len -= sizeof(struct virtio_net_hdr);
  226. skb_trim(skb, len);
  227. } else {
  228. page = buf;
  229. skb = page_to_skb(vi, page, len);
  230. if (unlikely(!skb)) {
  231. dev->stats.rx_dropped++;
  232. give_pages(vi, page);
  233. return;
  234. }
  235. if (vi->mergeable_rx_bufs)
  236. if (receive_mergeable(vi, skb)) {
  237. dev_kfree_skb(skb);
  238. return;
  239. }
  240. }
  241. hdr = skb_vnet_hdr(skb);
  242. u64_stats_update_begin(&stats->syncp);
  243. stats->rx_bytes += skb->len;
  244. stats->rx_packets++;
  245. u64_stats_update_end(&stats->syncp);
  246. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  247. pr_debug("Needs csum!\n");
  248. if (!skb_partial_csum_set(skb,
  249. hdr->hdr.csum_start,
  250. hdr->hdr.csum_offset))
  251. goto frame_err;
  252. } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
  253. skb->ip_summed = CHECKSUM_UNNECESSARY;
  254. }
  255. skb->protocol = eth_type_trans(skb, dev);
  256. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  257. ntohs(skb->protocol), skb->len, skb->pkt_type);
  258. if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  259. pr_debug("GSO!\n");
  260. switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  261. case VIRTIO_NET_HDR_GSO_TCPV4:
  262. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  263. break;
  264. case VIRTIO_NET_HDR_GSO_UDP:
  265. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  266. break;
  267. case VIRTIO_NET_HDR_GSO_TCPV6:
  268. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  269. break;
  270. default:
  271. if (net_ratelimit())
  272. printk(KERN_WARNING "%s: bad gso type %u.\n",
  273. dev->name, hdr->hdr.gso_type);
  274. goto frame_err;
  275. }
  276. if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  277. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  278. skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
  279. if (skb_shinfo(skb)->gso_size == 0) {
  280. if (net_ratelimit())
  281. printk(KERN_WARNING "%s: zero gso size.\n",
  282. dev->name);
  283. goto frame_err;
  284. }
  285. /* Header must be checked, and gso_segs computed. */
  286. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  287. skb_shinfo(skb)->gso_segs = 0;
  288. }
  289. netif_receive_skb(skb);
  290. return;
  291. frame_err:
  292. dev->stats.rx_frame_errors++;
  293. dev_kfree_skb(skb);
  294. }
  295. static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
  296. {
  297. struct sk_buff *skb;
  298. struct skb_vnet_hdr *hdr;
  299. int err;
  300. skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
  301. if (unlikely(!skb))
  302. return -ENOMEM;
  303. skb_put(skb, MAX_PACKET_LEN);
  304. hdr = skb_vnet_hdr(skb);
  305. sg_set_buf(vi->rx_sg, &hdr->hdr, sizeof hdr->hdr);
  306. skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
  307. err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
  308. if (err < 0)
  309. dev_kfree_skb(skb);
  310. return err;
  311. }
  312. static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
  313. {
  314. struct page *first, *list = NULL;
  315. char *p;
  316. int i, err, offset;
  317. /* page in vi->rx_sg[MAX_SKB_FRAGS + 1] is list tail */
  318. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  319. first = get_a_page(vi, gfp);
  320. if (!first) {
  321. if (list)
  322. give_pages(vi, list);
  323. return -ENOMEM;
  324. }
  325. sg_set_buf(&vi->rx_sg[i], page_address(first), PAGE_SIZE);
  326. /* chain new page in list head to match sg */
  327. first->private = (unsigned long)list;
  328. list = first;
  329. }
  330. first = get_a_page(vi, gfp);
  331. if (!first) {
  332. give_pages(vi, list);
  333. return -ENOMEM;
  334. }
  335. p = page_address(first);
  336. /* vi->rx_sg[0], vi->rx_sg[1] share the same page */
  337. /* a separated vi->rx_sg[0] for virtio_net_hdr only due to QEMU bug */
  338. sg_set_buf(&vi->rx_sg[0], p, sizeof(struct virtio_net_hdr));
  339. /* vi->rx_sg[1] for data packet, from offset */
  340. offset = sizeof(struct padded_vnet_hdr);
  341. sg_set_buf(&vi->rx_sg[1], p + offset, PAGE_SIZE - offset);
  342. /* chain first in list head */
  343. first->private = (unsigned long)list;
  344. err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
  345. first, gfp);
  346. if (err < 0)
  347. give_pages(vi, first);
  348. return err;
  349. }
  350. static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
  351. {
  352. struct page *page;
  353. int err;
  354. page = get_a_page(vi, gfp);
  355. if (!page)
  356. return -ENOMEM;
  357. sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
  358. err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
  359. if (err < 0)
  360. give_pages(vi, page);
  361. return err;
  362. }
  363. /* Returns false if we couldn't fill entirely (OOM). */
  364. static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
  365. {
  366. int err;
  367. bool oom;
  368. do {
  369. if (vi->mergeable_rx_bufs)
  370. err = add_recvbuf_mergeable(vi, gfp);
  371. else if (vi->big_packets)
  372. err = add_recvbuf_big(vi, gfp);
  373. else
  374. err = add_recvbuf_small(vi, gfp);
  375. oom = err == -ENOMEM;
  376. if (err < 0)
  377. break;
  378. ++vi->num;
  379. } while (err > 0);
  380. if (unlikely(vi->num > vi->max))
  381. vi->max = vi->num;
  382. virtqueue_kick(vi->rvq);
  383. return !oom;
  384. }
  385. static void skb_recv_done(struct virtqueue *rvq)
  386. {
  387. struct virtnet_info *vi = rvq->vdev->priv;
  388. /* Schedule NAPI, Suppress further interrupts if successful. */
  389. if (napi_schedule_prep(&vi->napi)) {
  390. virtqueue_disable_cb(rvq);
  391. __napi_schedule(&vi->napi);
  392. }
  393. }
  394. static void virtnet_napi_enable(struct virtnet_info *vi)
  395. {
  396. napi_enable(&vi->napi);
  397. /* If all buffers were filled by other side before we napi_enabled, we
  398. * won't get another interrupt, so process any outstanding packets
  399. * now. virtnet_poll wants re-enable the queue, so we disable here.
  400. * We synchronize against interrupts via NAPI_STATE_SCHED */
  401. if (napi_schedule_prep(&vi->napi)) {
  402. virtqueue_disable_cb(vi->rvq);
  403. __napi_schedule(&vi->napi);
  404. }
  405. }
  406. static void refill_work(struct work_struct *work)
  407. {
  408. struct virtnet_info *vi;
  409. bool still_empty;
  410. vi = container_of(work, struct virtnet_info, refill.work);
  411. napi_disable(&vi->napi);
  412. still_empty = !try_fill_recv(vi, GFP_KERNEL);
  413. virtnet_napi_enable(vi);
  414. /* In theory, this can happen: if we don't get any buffers in
  415. * we will *never* try to fill again. */
  416. if (still_empty)
  417. schedule_delayed_work(&vi->refill, HZ/2);
  418. }
  419. static int virtnet_poll(struct napi_struct *napi, int budget)
  420. {
  421. struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
  422. void *buf;
  423. unsigned int len, received = 0;
  424. again:
  425. while (received < budget &&
  426. (buf = virtqueue_get_buf(vi->rvq, &len)) != NULL) {
  427. receive_buf(vi->dev, buf, len);
  428. --vi->num;
  429. received++;
  430. }
  431. if (vi->num < vi->max / 2) {
  432. if (!try_fill_recv(vi, GFP_ATOMIC))
  433. schedule_delayed_work(&vi->refill, 0);
  434. }
  435. /* Out of packets? */
  436. if (received < budget) {
  437. napi_complete(napi);
  438. if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
  439. napi_schedule_prep(napi)) {
  440. virtqueue_disable_cb(vi->rvq);
  441. __napi_schedule(napi);
  442. goto again;
  443. }
  444. }
  445. return received;
  446. }
  447. static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
  448. {
  449. struct sk_buff *skb;
  450. unsigned int len, tot_sgs = 0;
  451. struct virtnet_stats __percpu *stats = this_cpu_ptr(vi->stats);
  452. while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
  453. pr_debug("Sent skb %p\n", skb);
  454. u64_stats_update_begin(&stats->syncp);
  455. stats->tx_bytes += skb->len;
  456. stats->tx_packets++;
  457. u64_stats_update_end(&stats->syncp);
  458. tot_sgs += skb_vnet_hdr(skb)->num_sg;
  459. dev_kfree_skb_any(skb);
  460. }
  461. return tot_sgs;
  462. }
  463. static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
  464. {
  465. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  466. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  467. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  468. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  469. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  470. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  471. hdr->hdr.csum_offset = skb->csum_offset;
  472. } else {
  473. hdr->hdr.flags = 0;
  474. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  475. }
  476. if (skb_is_gso(skb)) {
  477. hdr->hdr.hdr_len = skb_headlen(skb);
  478. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  479. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  480. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  481. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  482. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  483. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  484. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  485. else
  486. BUG();
  487. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  488. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  489. } else {
  490. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  491. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  492. }
  493. hdr->mhdr.num_buffers = 0;
  494. /* Encode metadata header at front. */
  495. if (vi->mergeable_rx_bufs)
  496. sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
  497. else
  498. sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
  499. hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
  500. return virtqueue_add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
  501. 0, skb);
  502. }
  503. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  504. {
  505. struct virtnet_info *vi = netdev_priv(dev);
  506. int capacity;
  507. /* Free up any pending old buffers before queueing new ones. */
  508. free_old_xmit_skbs(vi);
  509. /* Try to transmit */
  510. capacity = xmit_skb(vi, skb);
  511. /* This can happen with OOM and indirect buffers. */
  512. if (unlikely(capacity < 0)) {
  513. if (net_ratelimit()) {
  514. if (likely(capacity == -ENOMEM)) {
  515. dev_warn(&dev->dev,
  516. "TX queue failure: out of memory\n");
  517. } else {
  518. dev->stats.tx_fifo_errors++;
  519. dev_warn(&dev->dev,
  520. "Unexpected TX queue failure: %d\n",
  521. capacity);
  522. }
  523. }
  524. dev->stats.tx_dropped++;
  525. kfree_skb(skb);
  526. return NETDEV_TX_OK;
  527. }
  528. virtqueue_kick(vi->svq);
  529. /* Don't wait up for transmitted skbs to be freed. */
  530. skb_orphan(skb);
  531. nf_reset(skb);
  532. /* Apparently nice girls don't return TX_BUSY; stop the queue
  533. * before it gets out of hand. Naturally, this wastes entries. */
  534. if (capacity < 2+MAX_SKB_FRAGS) {
  535. netif_stop_queue(dev);
  536. if (unlikely(!virtqueue_enable_cb_delayed(vi->svq))) {
  537. /* More just got used, free them then recheck. */
  538. capacity += free_old_xmit_skbs(vi);
  539. if (capacity >= 2+MAX_SKB_FRAGS) {
  540. netif_start_queue(dev);
  541. virtqueue_disable_cb(vi->svq);
  542. }
  543. }
  544. }
  545. return NETDEV_TX_OK;
  546. }
  547. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  548. {
  549. struct virtnet_info *vi = netdev_priv(dev);
  550. struct virtio_device *vdev = vi->vdev;
  551. int ret;
  552. ret = eth_mac_addr(dev, p);
  553. if (ret)
  554. return ret;
  555. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  556. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  557. dev->dev_addr, dev->addr_len);
  558. return 0;
  559. }
  560. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  561. struct rtnl_link_stats64 *tot)
  562. {
  563. struct virtnet_info *vi = netdev_priv(dev);
  564. int cpu;
  565. unsigned int start;
  566. for_each_possible_cpu(cpu) {
  567. struct virtnet_stats __percpu *stats
  568. = per_cpu_ptr(vi->stats, cpu);
  569. u64 tpackets, tbytes, rpackets, rbytes;
  570. do {
  571. start = u64_stats_fetch_begin(&stats->syncp);
  572. tpackets = stats->tx_packets;
  573. tbytes = stats->tx_bytes;
  574. rpackets = stats->rx_packets;
  575. rbytes = stats->rx_bytes;
  576. } while (u64_stats_fetch_retry(&stats->syncp, start));
  577. tot->rx_packets += rpackets;
  578. tot->tx_packets += tpackets;
  579. tot->rx_bytes += rbytes;
  580. tot->tx_bytes += tbytes;
  581. }
  582. tot->tx_dropped = dev->stats.tx_dropped;
  583. tot->rx_dropped = dev->stats.rx_dropped;
  584. tot->rx_length_errors = dev->stats.rx_length_errors;
  585. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  586. return tot;
  587. }
  588. #ifdef CONFIG_NET_POLL_CONTROLLER
  589. static void virtnet_netpoll(struct net_device *dev)
  590. {
  591. struct virtnet_info *vi = netdev_priv(dev);
  592. napi_schedule(&vi->napi);
  593. }
  594. #endif
  595. static int virtnet_open(struct net_device *dev)
  596. {
  597. struct virtnet_info *vi = netdev_priv(dev);
  598. virtnet_napi_enable(vi);
  599. return 0;
  600. }
  601. /*
  602. * Send command via the control virtqueue and check status. Commands
  603. * supported by the hypervisor, as indicated by feature bits, should
  604. * never fail unless improperly formated.
  605. */
  606. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  607. struct scatterlist *data, int out, int in)
  608. {
  609. struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  610. struct virtio_net_ctrl_hdr ctrl;
  611. virtio_net_ctrl_ack status = ~0;
  612. unsigned int tmp;
  613. int i;
  614. /* Caller should know better */
  615. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
  616. (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
  617. out++; /* Add header */
  618. in++; /* Add return status */
  619. ctrl.class = class;
  620. ctrl.cmd = cmd;
  621. sg_init_table(sg, out + in);
  622. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  623. for_each_sg(data, s, out + in - 2, i)
  624. sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
  625. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  626. BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi) < 0);
  627. virtqueue_kick(vi->cvq);
  628. /*
  629. * Spin for a response, the kick causes an ioport write, trapping
  630. * into the hypervisor, so the request should be handled immediately.
  631. */
  632. while (!virtqueue_get_buf(vi->cvq, &tmp))
  633. cpu_relax();
  634. return status == VIRTIO_NET_OK;
  635. }
  636. static int virtnet_close(struct net_device *dev)
  637. {
  638. struct virtnet_info *vi = netdev_priv(dev);
  639. napi_disable(&vi->napi);
  640. return 0;
  641. }
  642. static void virtnet_set_rx_mode(struct net_device *dev)
  643. {
  644. struct virtnet_info *vi = netdev_priv(dev);
  645. struct scatterlist sg[2];
  646. u8 promisc, allmulti;
  647. struct virtio_net_ctrl_mac *mac_data;
  648. struct netdev_hw_addr *ha;
  649. int uc_count;
  650. int mc_count;
  651. void *buf;
  652. int i;
  653. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  654. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  655. return;
  656. promisc = ((dev->flags & IFF_PROMISC) != 0);
  657. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  658. sg_init_one(sg, &promisc, sizeof(promisc));
  659. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  660. VIRTIO_NET_CTRL_RX_PROMISC,
  661. sg, 1, 0))
  662. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  663. promisc ? "en" : "dis");
  664. sg_init_one(sg, &allmulti, sizeof(allmulti));
  665. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  666. VIRTIO_NET_CTRL_RX_ALLMULTI,
  667. sg, 1, 0))
  668. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  669. allmulti ? "en" : "dis");
  670. uc_count = netdev_uc_count(dev);
  671. mc_count = netdev_mc_count(dev);
  672. /* MAC filter - use one buffer for both lists */
  673. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  674. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  675. mac_data = buf;
  676. if (!buf) {
  677. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  678. return;
  679. }
  680. sg_init_table(sg, 2);
  681. /* Store the unicast list and count in the front of the buffer */
  682. mac_data->entries = uc_count;
  683. i = 0;
  684. netdev_for_each_uc_addr(ha, dev)
  685. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  686. sg_set_buf(&sg[0], mac_data,
  687. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  688. /* multicast list and count fill the end */
  689. mac_data = (void *)&mac_data->macs[uc_count][0];
  690. mac_data->entries = mc_count;
  691. i = 0;
  692. netdev_for_each_mc_addr(ha, dev)
  693. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  694. sg_set_buf(&sg[1], mac_data,
  695. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  696. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  697. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  698. sg, 2, 0))
  699. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  700. kfree(buf);
  701. }
  702. static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  703. {
  704. struct virtnet_info *vi = netdev_priv(dev);
  705. struct scatterlist sg;
  706. sg_init_one(&sg, &vid, sizeof(vid));
  707. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  708. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  709. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  710. }
  711. static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  712. {
  713. struct virtnet_info *vi = netdev_priv(dev);
  714. struct scatterlist sg;
  715. sg_init_one(&sg, &vid, sizeof(vid));
  716. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  717. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  718. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  719. }
  720. static void virtnet_get_ringparam(struct net_device *dev,
  721. struct ethtool_ringparam *ring)
  722. {
  723. struct virtnet_info *vi = netdev_priv(dev);
  724. ring->rx_max_pending = virtqueue_get_vring_size(vi->rvq);
  725. ring->tx_max_pending = virtqueue_get_vring_size(vi->svq);
  726. ring->rx_pending = ring->rx_max_pending;
  727. ring->tx_pending = ring->tx_max_pending;
  728. }
  729. static const struct ethtool_ops virtnet_ethtool_ops = {
  730. .get_link = ethtool_op_get_link,
  731. .get_ringparam = virtnet_get_ringparam,
  732. };
  733. #define MIN_MTU 68
  734. #define MAX_MTU 65535
  735. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  736. {
  737. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  738. return -EINVAL;
  739. dev->mtu = new_mtu;
  740. return 0;
  741. }
  742. static const struct net_device_ops virtnet_netdev = {
  743. .ndo_open = virtnet_open,
  744. .ndo_stop = virtnet_close,
  745. .ndo_start_xmit = start_xmit,
  746. .ndo_validate_addr = eth_validate_addr,
  747. .ndo_set_mac_address = virtnet_set_mac_address,
  748. .ndo_set_rx_mode = virtnet_set_rx_mode,
  749. .ndo_change_mtu = virtnet_change_mtu,
  750. .ndo_get_stats64 = virtnet_stats,
  751. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  752. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  753. #ifdef CONFIG_NET_POLL_CONTROLLER
  754. .ndo_poll_controller = virtnet_netpoll,
  755. #endif
  756. };
  757. static void virtnet_update_status(struct virtnet_info *vi)
  758. {
  759. u16 v;
  760. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
  761. return;
  762. vi->vdev->config->get(vi->vdev,
  763. offsetof(struct virtio_net_config, status),
  764. &v, sizeof(v));
  765. /* Ignore unknown (future) status bits */
  766. v &= VIRTIO_NET_S_LINK_UP;
  767. if (vi->status == v)
  768. return;
  769. vi->status = v;
  770. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  771. netif_carrier_on(vi->dev);
  772. netif_wake_queue(vi->dev);
  773. } else {
  774. netif_carrier_off(vi->dev);
  775. netif_stop_queue(vi->dev);
  776. }
  777. }
  778. static void virtnet_config_changed(struct virtio_device *vdev)
  779. {
  780. struct virtnet_info *vi = vdev->priv;
  781. virtnet_update_status(vi);
  782. }
  783. static int virtnet_probe(struct virtio_device *vdev)
  784. {
  785. int err;
  786. struct net_device *dev;
  787. struct virtnet_info *vi;
  788. struct virtqueue *vqs[3];
  789. vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
  790. const char *names[] = { "input", "output", "control" };
  791. int nvqs;
  792. /* Allocate ourselves a network device with room for our info */
  793. dev = alloc_etherdev(sizeof(struct virtnet_info));
  794. if (!dev)
  795. return -ENOMEM;
  796. /* Set up network device as normal. */
  797. dev->priv_flags |= IFF_UNICAST_FLT;
  798. dev->netdev_ops = &virtnet_netdev;
  799. dev->features = NETIF_F_HIGHDMA;
  800. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  801. SET_NETDEV_DEV(dev, &vdev->dev);
  802. /* Do we support "hardware" checksums? */
  803. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  804. /* This opens up the world of extra features. */
  805. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  806. if (csum)
  807. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  808. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  809. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  810. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  811. }
  812. /* Individual feature bits: what can host handle? */
  813. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  814. dev->hw_features |= NETIF_F_TSO;
  815. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  816. dev->hw_features |= NETIF_F_TSO6;
  817. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  818. dev->hw_features |= NETIF_F_TSO_ECN;
  819. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  820. dev->hw_features |= NETIF_F_UFO;
  821. if (gso)
  822. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  823. /* (!csum && gso) case will be fixed by register_netdev() */
  824. }
  825. /* Configuration may specify what MAC to use. Otherwise random. */
  826. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  827. vdev->config->get(vdev,
  828. offsetof(struct virtio_net_config, mac),
  829. dev->dev_addr, dev->addr_len);
  830. } else
  831. random_ether_addr(dev->dev_addr);
  832. /* Set up our device-specific information */
  833. vi = netdev_priv(dev);
  834. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  835. vi->dev = dev;
  836. vi->vdev = vdev;
  837. vdev->priv = vi;
  838. vi->pages = NULL;
  839. vi->stats = alloc_percpu(struct virtnet_stats);
  840. err = -ENOMEM;
  841. if (vi->stats == NULL)
  842. goto free;
  843. INIT_DELAYED_WORK(&vi->refill, refill_work);
  844. sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
  845. sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
  846. /* If we can receive ANY GSO packets, we must allocate large ones. */
  847. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  848. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  849. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  850. vi->big_packets = true;
  851. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  852. vi->mergeable_rx_bufs = true;
  853. /* We expect two virtqueues, receive then send,
  854. * and optionally control. */
  855. nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
  856. err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
  857. if (err)
  858. goto free_stats;
  859. vi->rvq = vqs[0];
  860. vi->svq = vqs[1];
  861. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  862. vi->cvq = vqs[2];
  863. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  864. dev->features |= NETIF_F_HW_VLAN_FILTER;
  865. }
  866. err = register_netdev(dev);
  867. if (err) {
  868. pr_debug("virtio_net: registering device failed\n");
  869. goto free_vqs;
  870. }
  871. /* Last of all, set up some receive buffers. */
  872. try_fill_recv(vi, GFP_KERNEL);
  873. /* If we didn't even get one input buffer, we're useless. */
  874. if (vi->num == 0) {
  875. err = -ENOMEM;
  876. goto unregister;
  877. }
  878. /* Assume link up if device can't report link status,
  879. otherwise get link status from config. */
  880. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  881. netif_carrier_off(dev);
  882. virtnet_update_status(vi);
  883. } else {
  884. vi->status = VIRTIO_NET_S_LINK_UP;
  885. netif_carrier_on(dev);
  886. }
  887. pr_debug("virtnet: registered device %s\n", dev->name);
  888. return 0;
  889. unregister:
  890. unregister_netdev(dev);
  891. cancel_delayed_work_sync(&vi->refill);
  892. free_vqs:
  893. vdev->config->del_vqs(vdev);
  894. free_stats:
  895. free_percpu(vi->stats);
  896. free:
  897. free_netdev(dev);
  898. return err;
  899. }
  900. static void free_unused_bufs(struct virtnet_info *vi)
  901. {
  902. void *buf;
  903. while (1) {
  904. buf = virtqueue_detach_unused_buf(vi->svq);
  905. if (!buf)
  906. break;
  907. dev_kfree_skb(buf);
  908. }
  909. while (1) {
  910. buf = virtqueue_detach_unused_buf(vi->rvq);
  911. if (!buf)
  912. break;
  913. if (vi->mergeable_rx_bufs || vi->big_packets)
  914. give_pages(vi, buf);
  915. else
  916. dev_kfree_skb(buf);
  917. --vi->num;
  918. }
  919. BUG_ON(vi->num != 0);
  920. }
  921. static void __devexit virtnet_remove(struct virtio_device *vdev)
  922. {
  923. struct virtnet_info *vi = vdev->priv;
  924. /* Stop all the virtqueues. */
  925. vdev->config->reset(vdev);
  926. unregister_netdev(vi->dev);
  927. cancel_delayed_work_sync(&vi->refill);
  928. /* Free unused buffers in both send and recv, if any. */
  929. free_unused_bufs(vi);
  930. vdev->config->del_vqs(vi->vdev);
  931. while (vi->pages)
  932. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  933. free_percpu(vi->stats);
  934. free_netdev(vi->dev);
  935. }
  936. static struct virtio_device_id id_table[] = {
  937. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  938. { 0 },
  939. };
  940. static unsigned int features[] = {
  941. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  942. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  943. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  944. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  945. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
  946. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  947. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  948. };
  949. static struct virtio_driver virtio_net_driver = {
  950. .feature_table = features,
  951. .feature_table_size = ARRAY_SIZE(features),
  952. .driver.name = KBUILD_MODNAME,
  953. .driver.owner = THIS_MODULE,
  954. .id_table = id_table,
  955. .probe = virtnet_probe,
  956. .remove = __devexit_p(virtnet_remove),
  957. .config_changed = virtnet_config_changed,
  958. };
  959. static int __init init(void)
  960. {
  961. return register_virtio_driver(&virtio_net_driver);
  962. }
  963. static void __exit fini(void)
  964. {
  965. unregister_virtio_driver(&virtio_net_driver);
  966. }
  967. module_init(init);
  968. module_exit(fini);
  969. MODULE_DEVICE_TABLE(virtio, id_table);
  970. MODULE_DESCRIPTION("Virtio network driver");
  971. MODULE_LICENSE("GPL");