virtio_net.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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 bool csum = true, gso = true;
  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. #define VIRTNET_DRIVER_VERSION "1.0.0"
  39. struct virtnet_stats {
  40. struct u64_stats_sync tx_syncp;
  41. struct u64_stats_sync rx_syncp;
  42. u64 tx_bytes;
  43. u64 tx_packets;
  44. u64 rx_bytes;
  45. u64 rx_packets;
  46. };
  47. /* Internal representation of a send virtqueue */
  48. struct send_queue {
  49. /* Virtqueue associated with this send _queue */
  50. struct virtqueue *vq;
  51. /* TX: fragments + linear part + virtio header */
  52. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  53. /* Name of the send queue: output.$index */
  54. char name[40];
  55. };
  56. /* Internal representation of a receive virtqueue */
  57. struct receive_queue {
  58. /* Virtqueue associated with this receive_queue */
  59. struct virtqueue *vq;
  60. struct napi_struct napi;
  61. /* Number of input buffers, and max we've ever had. */
  62. unsigned int num, max;
  63. /* Chain pages by the private ptr. */
  64. struct page *pages;
  65. /* RX: fragments + linear part + virtio header */
  66. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  67. /* Name of this receive queue: input.$index */
  68. char name[40];
  69. };
  70. struct virtnet_info {
  71. struct virtio_device *vdev;
  72. struct virtqueue *cvq;
  73. struct net_device *dev;
  74. struct send_queue *sq;
  75. struct receive_queue *rq;
  76. unsigned int status;
  77. /* Max # of queue pairs supported by the device */
  78. u16 max_queue_pairs;
  79. /* # of queue pairs currently used by the driver */
  80. u16 curr_queue_pairs;
  81. /* I like... big packets and I cannot lie! */
  82. bool big_packets;
  83. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  84. bool mergeable_rx_bufs;
  85. /* Has control virtqueue */
  86. bool has_cvq;
  87. /* enable config space updates */
  88. bool config_enable;
  89. /* Active statistics */
  90. struct virtnet_stats __percpu *stats;
  91. /* Work struct for refilling if we run low on memory. */
  92. struct delayed_work refill;
  93. /* Work struct for config space updates */
  94. struct work_struct config_work;
  95. /* Lock for config space updates */
  96. struct mutex config_lock;
  97. /* Does the affinity hint is set for virtqueues? */
  98. bool affinity_hint_set;
  99. };
  100. struct skb_vnet_hdr {
  101. union {
  102. struct virtio_net_hdr hdr;
  103. struct virtio_net_hdr_mrg_rxbuf mhdr;
  104. };
  105. unsigned int num_sg;
  106. };
  107. struct padded_vnet_hdr {
  108. struct virtio_net_hdr hdr;
  109. /*
  110. * virtio_net_hdr should be in a separated sg buffer because of a
  111. * QEMU bug, and data sg buffer shares same page with this header sg.
  112. * This padding makes next sg 16 byte aligned after virtio_net_hdr.
  113. */
  114. char padding[6];
  115. };
  116. /* Converting between virtqueue no. and kernel tx/rx queue no.
  117. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  118. */
  119. static int vq2txq(struct virtqueue *vq)
  120. {
  121. return (virtqueue_get_queue_index(vq) - 1) / 2;
  122. }
  123. static int txq2vq(int txq)
  124. {
  125. return txq * 2 + 1;
  126. }
  127. static int vq2rxq(struct virtqueue *vq)
  128. {
  129. return virtqueue_get_queue_index(vq) / 2;
  130. }
  131. static int rxq2vq(int rxq)
  132. {
  133. return rxq * 2;
  134. }
  135. static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
  136. {
  137. return (struct skb_vnet_hdr *)skb->cb;
  138. }
  139. /*
  140. * private is used to chain pages for big packets, put the whole
  141. * most recent used list in the beginning for reuse
  142. */
  143. static void give_pages(struct receive_queue *rq, struct page *page)
  144. {
  145. struct page *end;
  146. /* Find end of list, sew whole thing into vi->rq.pages. */
  147. for (end = page; end->private; end = (struct page *)end->private);
  148. end->private = (unsigned long)rq->pages;
  149. rq->pages = page;
  150. }
  151. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  152. {
  153. struct page *p = rq->pages;
  154. if (p) {
  155. rq->pages = (struct page *)p->private;
  156. /* clear private here, it is used to chain pages */
  157. p->private = 0;
  158. } else
  159. p = alloc_page(gfp_mask);
  160. return p;
  161. }
  162. static void skb_xmit_done(struct virtqueue *vq)
  163. {
  164. struct virtnet_info *vi = vq->vdev->priv;
  165. /* Suppress further interrupts. */
  166. virtqueue_disable_cb(vq);
  167. /* We were probably waiting for more output buffers. */
  168. netif_wake_subqueue(vi->dev, vq2txq(vq));
  169. }
  170. static void set_skb_frag(struct sk_buff *skb, struct page *page,
  171. unsigned int offset, unsigned int *len)
  172. {
  173. int size = min((unsigned)PAGE_SIZE - offset, *len);
  174. int i = skb_shinfo(skb)->nr_frags;
  175. __skb_fill_page_desc(skb, i, page, offset, size);
  176. skb->data_len += size;
  177. skb->len += size;
  178. skb->truesize += PAGE_SIZE;
  179. skb_shinfo(skb)->nr_frags++;
  180. *len -= size;
  181. }
  182. /* Called from bottom half context */
  183. static struct sk_buff *page_to_skb(struct receive_queue *rq,
  184. struct page *page, unsigned int len)
  185. {
  186. struct virtnet_info *vi = rq->vq->vdev->priv;
  187. struct sk_buff *skb;
  188. struct skb_vnet_hdr *hdr;
  189. unsigned int copy, hdr_len, offset;
  190. char *p;
  191. p = page_address(page);
  192. /* copy small packet so we can reuse these pages for small data */
  193. skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
  194. if (unlikely(!skb))
  195. return NULL;
  196. hdr = skb_vnet_hdr(skb);
  197. if (vi->mergeable_rx_bufs) {
  198. hdr_len = sizeof hdr->mhdr;
  199. offset = hdr_len;
  200. } else {
  201. hdr_len = sizeof hdr->hdr;
  202. offset = sizeof(struct padded_vnet_hdr);
  203. }
  204. memcpy(hdr, p, hdr_len);
  205. len -= hdr_len;
  206. p += offset;
  207. copy = len;
  208. if (copy > skb_tailroom(skb))
  209. copy = skb_tailroom(skb);
  210. memcpy(skb_put(skb, copy), p, copy);
  211. len -= copy;
  212. offset += copy;
  213. /*
  214. * Verify that we can indeed put this data into a skb.
  215. * This is here to handle cases when the device erroneously
  216. * tries to receive more than is possible. This is usually
  217. * the case of a broken device.
  218. */
  219. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  220. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  221. dev_kfree_skb(skb);
  222. return NULL;
  223. }
  224. while (len) {
  225. set_skb_frag(skb, page, offset, &len);
  226. page = (struct page *)page->private;
  227. offset = 0;
  228. }
  229. if (page)
  230. give_pages(rq, page);
  231. return skb;
  232. }
  233. static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb)
  234. {
  235. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  236. struct page *page;
  237. int num_buf, i, len;
  238. num_buf = hdr->mhdr.num_buffers;
  239. while (--num_buf) {
  240. i = skb_shinfo(skb)->nr_frags;
  241. if (i >= MAX_SKB_FRAGS) {
  242. pr_debug("%s: packet too long\n", skb->dev->name);
  243. skb->dev->stats.rx_length_errors++;
  244. return -EINVAL;
  245. }
  246. page = virtqueue_get_buf(rq->vq, &len);
  247. if (!page) {
  248. pr_debug("%s: rx error: %d buffers missing\n",
  249. skb->dev->name, hdr->mhdr.num_buffers);
  250. skb->dev->stats.rx_length_errors++;
  251. return -EINVAL;
  252. }
  253. if (len > PAGE_SIZE)
  254. len = PAGE_SIZE;
  255. set_skb_frag(skb, page, 0, &len);
  256. --rq->num;
  257. }
  258. return 0;
  259. }
  260. static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
  261. {
  262. struct virtnet_info *vi = rq->vq->vdev->priv;
  263. struct net_device *dev = vi->dev;
  264. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  265. struct sk_buff *skb;
  266. struct page *page;
  267. struct skb_vnet_hdr *hdr;
  268. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  269. pr_debug("%s: short packet %i\n", dev->name, len);
  270. dev->stats.rx_length_errors++;
  271. if (vi->mergeable_rx_bufs || vi->big_packets)
  272. give_pages(rq, buf);
  273. else
  274. dev_kfree_skb(buf);
  275. return;
  276. }
  277. if (!vi->mergeable_rx_bufs && !vi->big_packets) {
  278. skb = buf;
  279. len -= sizeof(struct virtio_net_hdr);
  280. skb_trim(skb, len);
  281. } else {
  282. page = buf;
  283. skb = page_to_skb(rq, page, len);
  284. if (unlikely(!skb)) {
  285. dev->stats.rx_dropped++;
  286. give_pages(rq, page);
  287. return;
  288. }
  289. if (vi->mergeable_rx_bufs)
  290. if (receive_mergeable(rq, skb)) {
  291. dev_kfree_skb(skb);
  292. return;
  293. }
  294. }
  295. hdr = skb_vnet_hdr(skb);
  296. u64_stats_update_begin(&stats->rx_syncp);
  297. stats->rx_bytes += skb->len;
  298. stats->rx_packets++;
  299. u64_stats_update_end(&stats->rx_syncp);
  300. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  301. pr_debug("Needs csum!\n");
  302. if (!skb_partial_csum_set(skb,
  303. hdr->hdr.csum_start,
  304. hdr->hdr.csum_offset))
  305. goto frame_err;
  306. } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
  307. skb->ip_summed = CHECKSUM_UNNECESSARY;
  308. }
  309. skb->protocol = eth_type_trans(skb, dev);
  310. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  311. ntohs(skb->protocol), skb->len, skb->pkt_type);
  312. if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  313. pr_debug("GSO!\n");
  314. switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  315. case VIRTIO_NET_HDR_GSO_TCPV4:
  316. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  317. break;
  318. case VIRTIO_NET_HDR_GSO_UDP:
  319. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  320. break;
  321. case VIRTIO_NET_HDR_GSO_TCPV6:
  322. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  323. break;
  324. default:
  325. net_warn_ratelimited("%s: bad gso type %u.\n",
  326. dev->name, hdr->hdr.gso_type);
  327. goto frame_err;
  328. }
  329. if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  330. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  331. skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
  332. if (skb_shinfo(skb)->gso_size == 0) {
  333. net_warn_ratelimited("%s: zero gso size.\n", dev->name);
  334. goto frame_err;
  335. }
  336. /* Header must be checked, and gso_segs computed. */
  337. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  338. skb_shinfo(skb)->gso_segs = 0;
  339. }
  340. netif_receive_skb(skb);
  341. return;
  342. frame_err:
  343. dev->stats.rx_frame_errors++;
  344. dev_kfree_skb(skb);
  345. }
  346. static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
  347. {
  348. struct virtnet_info *vi = rq->vq->vdev->priv;
  349. struct sk_buff *skb;
  350. struct skb_vnet_hdr *hdr;
  351. int err;
  352. skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
  353. if (unlikely(!skb))
  354. return -ENOMEM;
  355. skb_put(skb, MAX_PACKET_LEN);
  356. hdr = skb_vnet_hdr(skb);
  357. sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
  358. skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
  359. err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb, gfp);
  360. if (err < 0)
  361. dev_kfree_skb(skb);
  362. return err;
  363. }
  364. static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
  365. {
  366. struct page *first, *list = NULL;
  367. char *p;
  368. int i, err, offset;
  369. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  370. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  371. first = get_a_page(rq, gfp);
  372. if (!first) {
  373. if (list)
  374. give_pages(rq, list);
  375. return -ENOMEM;
  376. }
  377. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  378. /* chain new page in list head to match sg */
  379. first->private = (unsigned long)list;
  380. list = first;
  381. }
  382. first = get_a_page(rq, gfp);
  383. if (!first) {
  384. give_pages(rq, list);
  385. return -ENOMEM;
  386. }
  387. p = page_address(first);
  388. /* rq->sg[0], rq->sg[1] share the same page */
  389. /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
  390. sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
  391. /* rq->sg[1] for data packet, from offset */
  392. offset = sizeof(struct padded_vnet_hdr);
  393. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  394. /* chain first in list head */
  395. first->private = (unsigned long)list;
  396. err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2,
  397. first, gfp);
  398. if (err < 0)
  399. give_pages(rq, first);
  400. return err;
  401. }
  402. static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
  403. {
  404. struct page *page;
  405. int err;
  406. page = get_a_page(rq, gfp);
  407. if (!page)
  408. return -ENOMEM;
  409. sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
  410. err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page, gfp);
  411. if (err < 0)
  412. give_pages(rq, page);
  413. return err;
  414. }
  415. /*
  416. * Returns false if we couldn't fill entirely (OOM).
  417. *
  418. * Normally run in the receive path, but can also be run from ndo_open
  419. * before we're receiving packets, or from refill_work which is
  420. * careful to disable receiving (using napi_disable).
  421. */
  422. static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
  423. {
  424. struct virtnet_info *vi = rq->vq->vdev->priv;
  425. int err;
  426. bool oom;
  427. do {
  428. if (vi->mergeable_rx_bufs)
  429. err = add_recvbuf_mergeable(rq, gfp);
  430. else if (vi->big_packets)
  431. err = add_recvbuf_big(rq, gfp);
  432. else
  433. err = add_recvbuf_small(rq, gfp);
  434. oom = err == -ENOMEM;
  435. if (err < 0)
  436. break;
  437. ++rq->num;
  438. } while (err > 0);
  439. if (unlikely(rq->num > rq->max))
  440. rq->max = rq->num;
  441. virtqueue_kick(rq->vq);
  442. return !oom;
  443. }
  444. static void skb_recv_done(struct virtqueue *rvq)
  445. {
  446. struct virtnet_info *vi = rvq->vdev->priv;
  447. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  448. /* Schedule NAPI, Suppress further interrupts if successful. */
  449. if (napi_schedule_prep(&rq->napi)) {
  450. virtqueue_disable_cb(rvq);
  451. __napi_schedule(&rq->napi);
  452. }
  453. }
  454. static void virtnet_napi_enable(struct receive_queue *rq)
  455. {
  456. napi_enable(&rq->napi);
  457. /* If all buffers were filled by other side before we napi_enabled, we
  458. * won't get another interrupt, so process any outstanding packets
  459. * now. virtnet_poll wants re-enable the queue, so we disable here.
  460. * We synchronize against interrupts via NAPI_STATE_SCHED */
  461. if (napi_schedule_prep(&rq->napi)) {
  462. virtqueue_disable_cb(rq->vq);
  463. local_bh_disable();
  464. __napi_schedule(&rq->napi);
  465. local_bh_enable();
  466. }
  467. }
  468. static void refill_work(struct work_struct *work)
  469. {
  470. struct virtnet_info *vi =
  471. container_of(work, struct virtnet_info, refill.work);
  472. bool still_empty;
  473. int i;
  474. for (i = 0; i < vi->max_queue_pairs; i++) {
  475. struct receive_queue *rq = &vi->rq[i];
  476. napi_disable(&rq->napi);
  477. still_empty = !try_fill_recv(rq, GFP_KERNEL);
  478. virtnet_napi_enable(rq);
  479. /* In theory, this can happen: if we don't get any buffers in
  480. * we will *never* try to fill again.
  481. */
  482. if (still_empty)
  483. schedule_delayed_work(&vi->refill, HZ/2);
  484. }
  485. }
  486. static int virtnet_poll(struct napi_struct *napi, int budget)
  487. {
  488. struct receive_queue *rq =
  489. container_of(napi, struct receive_queue, napi);
  490. struct virtnet_info *vi = rq->vq->vdev->priv;
  491. void *buf;
  492. unsigned int len, received = 0;
  493. again:
  494. while (received < budget &&
  495. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  496. receive_buf(rq, buf, len);
  497. --rq->num;
  498. received++;
  499. }
  500. if (rq->num < rq->max / 2) {
  501. if (!try_fill_recv(rq, GFP_ATOMIC))
  502. schedule_delayed_work(&vi->refill, 0);
  503. }
  504. /* Out of packets? */
  505. if (received < budget) {
  506. napi_complete(napi);
  507. if (unlikely(!virtqueue_enable_cb(rq->vq)) &&
  508. napi_schedule_prep(napi)) {
  509. virtqueue_disable_cb(rq->vq);
  510. __napi_schedule(napi);
  511. goto again;
  512. }
  513. }
  514. return received;
  515. }
  516. static int virtnet_open(struct net_device *dev)
  517. {
  518. struct virtnet_info *vi = netdev_priv(dev);
  519. int i;
  520. for (i = 0; i < vi->max_queue_pairs; i++) {
  521. /* Make sure we have some buffers: if oom use wq. */
  522. if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
  523. schedule_delayed_work(&vi->refill, 0);
  524. virtnet_napi_enable(&vi->rq[i]);
  525. }
  526. return 0;
  527. }
  528. static unsigned int free_old_xmit_skbs(struct send_queue *sq)
  529. {
  530. struct sk_buff *skb;
  531. unsigned int len, tot_sgs = 0;
  532. struct virtnet_info *vi = sq->vq->vdev->priv;
  533. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  534. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  535. pr_debug("Sent skb %p\n", skb);
  536. u64_stats_update_begin(&stats->tx_syncp);
  537. stats->tx_bytes += skb->len;
  538. stats->tx_packets++;
  539. u64_stats_update_end(&stats->tx_syncp);
  540. tot_sgs += skb_vnet_hdr(skb)->num_sg;
  541. dev_kfree_skb_any(skb);
  542. }
  543. return tot_sgs;
  544. }
  545. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  546. {
  547. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  548. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  549. struct virtnet_info *vi = sq->vq->vdev->priv;
  550. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  551. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  552. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  553. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  554. hdr->hdr.csum_offset = skb->csum_offset;
  555. } else {
  556. hdr->hdr.flags = 0;
  557. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  558. }
  559. if (skb_is_gso(skb)) {
  560. hdr->hdr.hdr_len = skb_headlen(skb);
  561. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  562. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  563. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  564. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  565. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  566. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  567. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  568. else
  569. BUG();
  570. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  571. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  572. } else {
  573. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  574. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  575. }
  576. hdr->mhdr.num_buffers = 0;
  577. /* Encode metadata header at front. */
  578. if (vi->mergeable_rx_bufs)
  579. sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr);
  580. else
  581. sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
  582. hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
  583. return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
  584. 0, skb, GFP_ATOMIC);
  585. }
  586. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  587. {
  588. struct virtnet_info *vi = netdev_priv(dev);
  589. int qnum = skb_get_queue_mapping(skb);
  590. struct send_queue *sq = &vi->sq[qnum];
  591. int capacity;
  592. /* Free up any pending old buffers before queueing new ones. */
  593. free_old_xmit_skbs(sq);
  594. /* Try to transmit */
  595. capacity = xmit_skb(sq, skb);
  596. /* This can happen with OOM and indirect buffers. */
  597. if (unlikely(capacity < 0)) {
  598. if (likely(capacity == -ENOMEM)) {
  599. if (net_ratelimit())
  600. dev_warn(&dev->dev,
  601. "TXQ (%d) failure: out of memory\n",
  602. qnum);
  603. } else {
  604. dev->stats.tx_fifo_errors++;
  605. if (net_ratelimit())
  606. dev_warn(&dev->dev,
  607. "Unexpected TXQ (%d) failure: %d\n",
  608. qnum, capacity);
  609. }
  610. dev->stats.tx_dropped++;
  611. kfree_skb(skb);
  612. return NETDEV_TX_OK;
  613. }
  614. virtqueue_kick(sq->vq);
  615. /* Don't wait up for transmitted skbs to be freed. */
  616. skb_orphan(skb);
  617. nf_reset(skb);
  618. /* Apparently nice girls don't return TX_BUSY; stop the queue
  619. * before it gets out of hand. Naturally, this wastes entries. */
  620. if (capacity < 2+MAX_SKB_FRAGS) {
  621. netif_stop_subqueue(dev, qnum);
  622. if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  623. /* More just got used, free them then recheck. */
  624. capacity += free_old_xmit_skbs(sq);
  625. if (capacity >= 2+MAX_SKB_FRAGS) {
  626. netif_start_subqueue(dev, qnum);
  627. virtqueue_disable_cb(sq->vq);
  628. }
  629. }
  630. }
  631. return NETDEV_TX_OK;
  632. }
  633. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  634. {
  635. struct virtnet_info *vi = netdev_priv(dev);
  636. struct virtio_device *vdev = vi->vdev;
  637. int ret;
  638. ret = eth_mac_addr(dev, p);
  639. if (ret)
  640. return ret;
  641. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  642. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  643. dev->dev_addr, dev->addr_len);
  644. return 0;
  645. }
  646. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  647. struct rtnl_link_stats64 *tot)
  648. {
  649. struct virtnet_info *vi = netdev_priv(dev);
  650. int cpu;
  651. unsigned int start;
  652. for_each_possible_cpu(cpu) {
  653. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  654. u64 tpackets, tbytes, rpackets, rbytes;
  655. do {
  656. start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
  657. tpackets = stats->tx_packets;
  658. tbytes = stats->tx_bytes;
  659. } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
  660. do {
  661. start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
  662. rpackets = stats->rx_packets;
  663. rbytes = stats->rx_bytes;
  664. } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
  665. tot->rx_packets += rpackets;
  666. tot->tx_packets += tpackets;
  667. tot->rx_bytes += rbytes;
  668. tot->tx_bytes += tbytes;
  669. }
  670. tot->tx_dropped = dev->stats.tx_dropped;
  671. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  672. tot->rx_dropped = dev->stats.rx_dropped;
  673. tot->rx_length_errors = dev->stats.rx_length_errors;
  674. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  675. return tot;
  676. }
  677. #ifdef CONFIG_NET_POLL_CONTROLLER
  678. static void virtnet_netpoll(struct net_device *dev)
  679. {
  680. struct virtnet_info *vi = netdev_priv(dev);
  681. int i;
  682. for (i = 0; i < vi->curr_queue_pairs; i++)
  683. napi_schedule(&vi->rq[i].napi);
  684. }
  685. #endif
  686. /*
  687. * Send command via the control virtqueue and check status. Commands
  688. * supported by the hypervisor, as indicated by feature bits, should
  689. * never fail unless improperly formated.
  690. */
  691. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  692. struct scatterlist *data, int out, int in)
  693. {
  694. struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  695. struct virtio_net_ctrl_hdr ctrl;
  696. virtio_net_ctrl_ack status = ~0;
  697. unsigned int tmp;
  698. int i;
  699. /* Caller should know better */
  700. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
  701. (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
  702. out++; /* Add header */
  703. in++; /* Add return status */
  704. ctrl.class = class;
  705. ctrl.cmd = cmd;
  706. sg_init_table(sg, out + in);
  707. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  708. for_each_sg(data, s, out + in - 2, i)
  709. sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
  710. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  711. BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
  712. virtqueue_kick(vi->cvq);
  713. /*
  714. * Spin for a response, the kick causes an ioport write, trapping
  715. * into the hypervisor, so the request should be handled immediately.
  716. */
  717. while (!virtqueue_get_buf(vi->cvq, &tmp))
  718. cpu_relax();
  719. return status == VIRTIO_NET_OK;
  720. }
  721. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  722. {
  723. rtnl_lock();
  724. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  725. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
  726. 0, 0))
  727. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  728. rtnl_unlock();
  729. }
  730. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  731. {
  732. struct scatterlist sg;
  733. struct virtio_net_ctrl_mq s;
  734. struct net_device *dev = vi->dev;
  735. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  736. return 0;
  737. s.virtqueue_pairs = queue_pairs;
  738. sg_init_one(&sg, &s, sizeof(s));
  739. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  740. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, 1, 0)){
  741. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  742. queue_pairs);
  743. return -EINVAL;
  744. } else
  745. vi->curr_queue_pairs = queue_pairs;
  746. return 0;
  747. }
  748. static int virtnet_close(struct net_device *dev)
  749. {
  750. struct virtnet_info *vi = netdev_priv(dev);
  751. int i;
  752. /* Make sure refill_work doesn't re-enable napi! */
  753. cancel_delayed_work_sync(&vi->refill);
  754. for (i = 0; i < vi->max_queue_pairs; i++)
  755. napi_disable(&vi->rq[i].napi);
  756. return 0;
  757. }
  758. static void virtnet_set_rx_mode(struct net_device *dev)
  759. {
  760. struct virtnet_info *vi = netdev_priv(dev);
  761. struct scatterlist sg[2];
  762. u8 promisc, allmulti;
  763. struct virtio_net_ctrl_mac *mac_data;
  764. struct netdev_hw_addr *ha;
  765. int uc_count;
  766. int mc_count;
  767. void *buf;
  768. int i;
  769. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  770. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  771. return;
  772. promisc = ((dev->flags & IFF_PROMISC) != 0);
  773. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  774. sg_init_one(sg, &promisc, sizeof(promisc));
  775. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  776. VIRTIO_NET_CTRL_RX_PROMISC,
  777. sg, 1, 0))
  778. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  779. promisc ? "en" : "dis");
  780. sg_init_one(sg, &allmulti, sizeof(allmulti));
  781. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  782. VIRTIO_NET_CTRL_RX_ALLMULTI,
  783. sg, 1, 0))
  784. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  785. allmulti ? "en" : "dis");
  786. uc_count = netdev_uc_count(dev);
  787. mc_count = netdev_mc_count(dev);
  788. /* MAC filter - use one buffer for both lists */
  789. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  790. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  791. mac_data = buf;
  792. if (!buf) {
  793. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  794. return;
  795. }
  796. sg_init_table(sg, 2);
  797. /* Store the unicast list and count in the front of the buffer */
  798. mac_data->entries = uc_count;
  799. i = 0;
  800. netdev_for_each_uc_addr(ha, dev)
  801. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  802. sg_set_buf(&sg[0], mac_data,
  803. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  804. /* multicast list and count fill the end */
  805. mac_data = (void *)&mac_data->macs[uc_count][0];
  806. mac_data->entries = mc_count;
  807. i = 0;
  808. netdev_for_each_mc_addr(ha, dev)
  809. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  810. sg_set_buf(&sg[1], mac_data,
  811. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  812. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  813. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  814. sg, 2, 0))
  815. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  816. kfree(buf);
  817. }
  818. static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  819. {
  820. struct virtnet_info *vi = netdev_priv(dev);
  821. struct scatterlist sg;
  822. sg_init_one(&sg, &vid, sizeof(vid));
  823. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  824. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  825. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  826. return 0;
  827. }
  828. static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  829. {
  830. struct virtnet_info *vi = netdev_priv(dev);
  831. struct scatterlist sg;
  832. sg_init_one(&sg, &vid, sizeof(vid));
  833. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  834. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  835. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  836. return 0;
  837. }
  838. static void virtnet_set_affinity(struct virtnet_info *vi, bool set)
  839. {
  840. int i;
  841. /* In multiqueue mode, when the number of cpu is equal to the number of
  842. * queue pairs, we let the queue pairs to be private to one cpu by
  843. * setting the affinity hint to eliminate the contention.
  844. */
  845. if ((vi->curr_queue_pairs == 1 ||
  846. vi->max_queue_pairs != num_online_cpus()) && set) {
  847. if (vi->affinity_hint_set)
  848. set = false;
  849. else
  850. return;
  851. }
  852. for (i = 0; i < vi->max_queue_pairs; i++) {
  853. int cpu = set ? i : -1;
  854. virtqueue_set_affinity(vi->rq[i].vq, cpu);
  855. virtqueue_set_affinity(vi->sq[i].vq, cpu);
  856. }
  857. if (set)
  858. vi->affinity_hint_set = true;
  859. else
  860. vi->affinity_hint_set = false;
  861. }
  862. static void virtnet_get_ringparam(struct net_device *dev,
  863. struct ethtool_ringparam *ring)
  864. {
  865. struct virtnet_info *vi = netdev_priv(dev);
  866. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  867. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  868. ring->rx_pending = ring->rx_max_pending;
  869. ring->tx_pending = ring->tx_max_pending;
  870. }
  871. static void virtnet_get_drvinfo(struct net_device *dev,
  872. struct ethtool_drvinfo *info)
  873. {
  874. struct virtnet_info *vi = netdev_priv(dev);
  875. struct virtio_device *vdev = vi->vdev;
  876. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  877. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  878. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  879. }
  880. /* TODO: Eliminate OOO packets during switching */
  881. static int virtnet_set_channels(struct net_device *dev,
  882. struct ethtool_channels *channels)
  883. {
  884. struct virtnet_info *vi = netdev_priv(dev);
  885. u16 queue_pairs = channels->combined_count;
  886. int err;
  887. /* We don't support separate rx/tx channels.
  888. * We don't allow setting 'other' channels.
  889. */
  890. if (channels->rx_count || channels->tx_count || channels->other_count)
  891. return -EINVAL;
  892. if (queue_pairs > vi->max_queue_pairs)
  893. return -EINVAL;
  894. err = virtnet_set_queues(vi, queue_pairs);
  895. if (!err) {
  896. netif_set_real_num_tx_queues(dev, queue_pairs);
  897. netif_set_real_num_rx_queues(dev, queue_pairs);
  898. virtnet_set_affinity(vi, true);
  899. }
  900. return err;
  901. }
  902. static void virtnet_get_channels(struct net_device *dev,
  903. struct ethtool_channels *channels)
  904. {
  905. struct virtnet_info *vi = netdev_priv(dev);
  906. channels->combined_count = vi->curr_queue_pairs;
  907. channels->max_combined = vi->max_queue_pairs;
  908. channels->max_other = 0;
  909. channels->rx_count = 0;
  910. channels->tx_count = 0;
  911. channels->other_count = 0;
  912. }
  913. static const struct ethtool_ops virtnet_ethtool_ops = {
  914. .get_drvinfo = virtnet_get_drvinfo,
  915. .get_link = ethtool_op_get_link,
  916. .get_ringparam = virtnet_get_ringparam,
  917. .set_channels = virtnet_set_channels,
  918. .get_channels = virtnet_get_channels,
  919. };
  920. #define MIN_MTU 68
  921. #define MAX_MTU 65535
  922. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  923. {
  924. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  925. return -EINVAL;
  926. dev->mtu = new_mtu;
  927. return 0;
  928. }
  929. /* To avoid contending a lock hold by a vcpu who would exit to host, select the
  930. * txq based on the processor id.
  931. * TODO: handle cpu hotplug.
  932. */
  933. static u16 virtnet_select_queue(struct net_device *dev, struct sk_buff *skb)
  934. {
  935. int txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) :
  936. smp_processor_id();
  937. while (unlikely(txq >= dev->real_num_tx_queues))
  938. txq -= dev->real_num_tx_queues;
  939. return txq;
  940. }
  941. static const struct net_device_ops virtnet_netdev = {
  942. .ndo_open = virtnet_open,
  943. .ndo_stop = virtnet_close,
  944. .ndo_start_xmit = start_xmit,
  945. .ndo_validate_addr = eth_validate_addr,
  946. .ndo_set_mac_address = virtnet_set_mac_address,
  947. .ndo_set_rx_mode = virtnet_set_rx_mode,
  948. .ndo_change_mtu = virtnet_change_mtu,
  949. .ndo_get_stats64 = virtnet_stats,
  950. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  951. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  952. .ndo_select_queue = virtnet_select_queue,
  953. #ifdef CONFIG_NET_POLL_CONTROLLER
  954. .ndo_poll_controller = virtnet_netpoll,
  955. #endif
  956. };
  957. static void virtnet_config_changed_work(struct work_struct *work)
  958. {
  959. struct virtnet_info *vi =
  960. container_of(work, struct virtnet_info, config_work);
  961. u16 v;
  962. mutex_lock(&vi->config_lock);
  963. if (!vi->config_enable)
  964. goto done;
  965. if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
  966. offsetof(struct virtio_net_config, status),
  967. &v) < 0)
  968. goto done;
  969. if (v & VIRTIO_NET_S_ANNOUNCE) {
  970. netdev_notify_peers(vi->dev);
  971. virtnet_ack_link_announce(vi);
  972. }
  973. /* Ignore unknown (future) status bits */
  974. v &= VIRTIO_NET_S_LINK_UP;
  975. if (vi->status == v)
  976. goto done;
  977. vi->status = v;
  978. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  979. netif_carrier_on(vi->dev);
  980. netif_tx_wake_all_queues(vi->dev);
  981. } else {
  982. netif_carrier_off(vi->dev);
  983. netif_tx_stop_all_queues(vi->dev);
  984. }
  985. done:
  986. mutex_unlock(&vi->config_lock);
  987. }
  988. static void virtnet_config_changed(struct virtio_device *vdev)
  989. {
  990. struct virtnet_info *vi = vdev->priv;
  991. schedule_work(&vi->config_work);
  992. }
  993. static void virtnet_free_queues(struct virtnet_info *vi)
  994. {
  995. kfree(vi->rq);
  996. kfree(vi->sq);
  997. }
  998. static void free_receive_bufs(struct virtnet_info *vi)
  999. {
  1000. int i;
  1001. for (i = 0; i < vi->max_queue_pairs; i++) {
  1002. while (vi->rq[i].pages)
  1003. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  1004. }
  1005. }
  1006. static void free_unused_bufs(struct virtnet_info *vi)
  1007. {
  1008. void *buf;
  1009. int i;
  1010. for (i = 0; i < vi->max_queue_pairs; i++) {
  1011. struct virtqueue *vq = vi->sq[i].vq;
  1012. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  1013. dev_kfree_skb(buf);
  1014. }
  1015. for (i = 0; i < vi->max_queue_pairs; i++) {
  1016. struct virtqueue *vq = vi->rq[i].vq;
  1017. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1018. if (vi->mergeable_rx_bufs || vi->big_packets)
  1019. give_pages(&vi->rq[i], buf);
  1020. else
  1021. dev_kfree_skb(buf);
  1022. --vi->rq[i].num;
  1023. }
  1024. BUG_ON(vi->rq[i].num != 0);
  1025. }
  1026. }
  1027. static void virtnet_del_vqs(struct virtnet_info *vi)
  1028. {
  1029. struct virtio_device *vdev = vi->vdev;
  1030. virtnet_set_affinity(vi, false);
  1031. vdev->config->del_vqs(vdev);
  1032. virtnet_free_queues(vi);
  1033. }
  1034. static int virtnet_find_vqs(struct virtnet_info *vi)
  1035. {
  1036. vq_callback_t **callbacks;
  1037. struct virtqueue **vqs;
  1038. int ret = -ENOMEM;
  1039. int i, total_vqs;
  1040. const char **names;
  1041. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  1042. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  1043. * possible control vq.
  1044. */
  1045. total_vqs = vi->max_queue_pairs * 2 +
  1046. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  1047. /* Allocate space for find_vqs parameters */
  1048. vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
  1049. if (!vqs)
  1050. goto err_vq;
  1051. callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
  1052. if (!callbacks)
  1053. goto err_callback;
  1054. names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
  1055. if (!names)
  1056. goto err_names;
  1057. /* Parameters for control virtqueue, if any */
  1058. if (vi->has_cvq) {
  1059. callbacks[total_vqs - 1] = NULL;
  1060. names[total_vqs - 1] = "control";
  1061. }
  1062. /* Allocate/initialize parameters for send/receive virtqueues */
  1063. for (i = 0; i < vi->max_queue_pairs; i++) {
  1064. callbacks[rxq2vq(i)] = skb_recv_done;
  1065. callbacks[txq2vq(i)] = skb_xmit_done;
  1066. sprintf(vi->rq[i].name, "input.%d", i);
  1067. sprintf(vi->sq[i].name, "output.%d", i);
  1068. names[rxq2vq(i)] = vi->rq[i].name;
  1069. names[txq2vq(i)] = vi->sq[i].name;
  1070. }
  1071. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  1072. names);
  1073. if (ret)
  1074. goto err_find;
  1075. if (vi->has_cvq) {
  1076. vi->cvq = vqs[total_vqs - 1];
  1077. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  1078. vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
  1079. }
  1080. for (i = 0; i < vi->max_queue_pairs; i++) {
  1081. vi->rq[i].vq = vqs[rxq2vq(i)];
  1082. vi->sq[i].vq = vqs[txq2vq(i)];
  1083. }
  1084. kfree(names);
  1085. kfree(callbacks);
  1086. kfree(vqs);
  1087. return 0;
  1088. err_find:
  1089. kfree(names);
  1090. err_names:
  1091. kfree(callbacks);
  1092. err_callback:
  1093. kfree(vqs);
  1094. err_vq:
  1095. return ret;
  1096. }
  1097. static int virtnet_alloc_queues(struct virtnet_info *vi)
  1098. {
  1099. int i;
  1100. vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
  1101. if (!vi->sq)
  1102. goto err_sq;
  1103. vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
  1104. if (!vi->rq)
  1105. goto err_rq;
  1106. INIT_DELAYED_WORK(&vi->refill, refill_work);
  1107. for (i = 0; i < vi->max_queue_pairs; i++) {
  1108. vi->rq[i].pages = NULL;
  1109. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  1110. napi_weight);
  1111. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  1112. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  1113. }
  1114. return 0;
  1115. err_rq:
  1116. kfree(vi->sq);
  1117. err_sq:
  1118. return -ENOMEM;
  1119. }
  1120. static int init_vqs(struct virtnet_info *vi)
  1121. {
  1122. int ret;
  1123. /* Allocate send & receive queues */
  1124. ret = virtnet_alloc_queues(vi);
  1125. if (ret)
  1126. goto err;
  1127. ret = virtnet_find_vqs(vi);
  1128. if (ret)
  1129. goto err_free;
  1130. virtnet_set_affinity(vi, true);
  1131. return 0;
  1132. err_free:
  1133. virtnet_free_queues(vi);
  1134. err:
  1135. return ret;
  1136. }
  1137. static int virtnet_probe(struct virtio_device *vdev)
  1138. {
  1139. int i, err;
  1140. struct net_device *dev;
  1141. struct virtnet_info *vi;
  1142. u16 max_queue_pairs;
  1143. /* Find if host supports multiqueue virtio_net device */
  1144. err = virtio_config_val(vdev, VIRTIO_NET_F_MQ,
  1145. offsetof(struct virtio_net_config,
  1146. max_virtqueue_pairs), &max_queue_pairs);
  1147. /* We need at least 2 queue's */
  1148. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  1149. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  1150. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1151. max_queue_pairs = 1;
  1152. /* Allocate ourselves a network device with room for our info */
  1153. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  1154. if (!dev)
  1155. return -ENOMEM;
  1156. /* Set up network device as normal. */
  1157. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1158. dev->netdev_ops = &virtnet_netdev;
  1159. dev->features = NETIF_F_HIGHDMA;
  1160. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  1161. SET_NETDEV_DEV(dev, &vdev->dev);
  1162. /* Do we support "hardware" checksums? */
  1163. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  1164. /* This opens up the world of extra features. */
  1165. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  1166. if (csum)
  1167. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  1168. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  1169. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  1170. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  1171. }
  1172. /* Individual feature bits: what can host handle? */
  1173. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  1174. dev->hw_features |= NETIF_F_TSO;
  1175. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  1176. dev->hw_features |= NETIF_F_TSO6;
  1177. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  1178. dev->hw_features |= NETIF_F_TSO_ECN;
  1179. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  1180. dev->hw_features |= NETIF_F_UFO;
  1181. if (gso)
  1182. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  1183. /* (!csum && gso) case will be fixed by register_netdev() */
  1184. }
  1185. /* Configuration may specify what MAC to use. Otherwise random. */
  1186. if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
  1187. offsetof(struct virtio_net_config, mac),
  1188. dev->dev_addr, dev->addr_len) < 0)
  1189. eth_hw_addr_random(dev);
  1190. /* Set up our device-specific information */
  1191. vi = netdev_priv(dev);
  1192. vi->dev = dev;
  1193. vi->vdev = vdev;
  1194. vdev->priv = vi;
  1195. vi->stats = alloc_percpu(struct virtnet_stats);
  1196. err = -ENOMEM;
  1197. if (vi->stats == NULL)
  1198. goto free;
  1199. mutex_init(&vi->config_lock);
  1200. vi->config_enable = true;
  1201. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  1202. /* If we can receive ANY GSO packets, we must allocate large ones. */
  1203. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  1204. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  1205. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  1206. vi->big_packets = true;
  1207. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  1208. vi->mergeable_rx_bufs = true;
  1209. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1210. vi->has_cvq = true;
  1211. /* Use single tx/rx queue pair as default */
  1212. vi->curr_queue_pairs = 1;
  1213. vi->max_queue_pairs = max_queue_pairs;
  1214. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  1215. err = init_vqs(vi);
  1216. if (err)
  1217. goto free_stats;
  1218. netif_set_real_num_tx_queues(dev, 1);
  1219. netif_set_real_num_rx_queues(dev, 1);
  1220. err = register_netdev(dev);
  1221. if (err) {
  1222. pr_debug("virtio_net: registering device failed\n");
  1223. goto free_vqs;
  1224. }
  1225. /* Last of all, set up some receive buffers. */
  1226. for (i = 0; i < vi->max_queue_pairs; i++) {
  1227. try_fill_recv(&vi->rq[i], GFP_KERNEL);
  1228. /* If we didn't even get one input buffer, we're useless. */
  1229. if (vi->rq[i].num == 0) {
  1230. free_unused_bufs(vi);
  1231. err = -ENOMEM;
  1232. goto free_recv_bufs;
  1233. }
  1234. }
  1235. /* Assume link up if device can't report link status,
  1236. otherwise get link status from config. */
  1237. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  1238. netif_carrier_off(dev);
  1239. schedule_work(&vi->config_work);
  1240. } else {
  1241. vi->status = VIRTIO_NET_S_LINK_UP;
  1242. netif_carrier_on(dev);
  1243. }
  1244. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  1245. dev->name, max_queue_pairs);
  1246. return 0;
  1247. free_recv_bufs:
  1248. free_receive_bufs(vi);
  1249. unregister_netdev(dev);
  1250. free_vqs:
  1251. cancel_delayed_work_sync(&vi->refill);
  1252. virtnet_del_vqs(vi);
  1253. free_stats:
  1254. free_percpu(vi->stats);
  1255. free:
  1256. free_netdev(dev);
  1257. return err;
  1258. }
  1259. static void remove_vq_common(struct virtnet_info *vi)
  1260. {
  1261. vi->vdev->config->reset(vi->vdev);
  1262. /* Free unused buffers in both send and recv, if any. */
  1263. free_unused_bufs(vi);
  1264. free_receive_bufs(vi);
  1265. virtnet_del_vqs(vi);
  1266. }
  1267. static void virtnet_remove(struct virtio_device *vdev)
  1268. {
  1269. struct virtnet_info *vi = vdev->priv;
  1270. /* Prevent config work handler from accessing the device. */
  1271. mutex_lock(&vi->config_lock);
  1272. vi->config_enable = false;
  1273. mutex_unlock(&vi->config_lock);
  1274. unregister_netdev(vi->dev);
  1275. remove_vq_common(vi);
  1276. flush_work(&vi->config_work);
  1277. free_percpu(vi->stats);
  1278. free_netdev(vi->dev);
  1279. }
  1280. #ifdef CONFIG_PM
  1281. static int virtnet_freeze(struct virtio_device *vdev)
  1282. {
  1283. struct virtnet_info *vi = vdev->priv;
  1284. int i;
  1285. /* Prevent config work handler from accessing the device */
  1286. mutex_lock(&vi->config_lock);
  1287. vi->config_enable = false;
  1288. mutex_unlock(&vi->config_lock);
  1289. netif_device_detach(vi->dev);
  1290. cancel_delayed_work_sync(&vi->refill);
  1291. if (netif_running(vi->dev))
  1292. for (i = 0; i < vi->max_queue_pairs; i++) {
  1293. napi_disable(&vi->rq[i].napi);
  1294. netif_napi_del(&vi->rq[i].napi);
  1295. }
  1296. remove_vq_common(vi);
  1297. flush_work(&vi->config_work);
  1298. return 0;
  1299. }
  1300. static int virtnet_restore(struct virtio_device *vdev)
  1301. {
  1302. struct virtnet_info *vi = vdev->priv;
  1303. int err, i;
  1304. err = init_vqs(vi);
  1305. if (err)
  1306. return err;
  1307. if (netif_running(vi->dev))
  1308. for (i = 0; i < vi->max_queue_pairs; i++)
  1309. virtnet_napi_enable(&vi->rq[i]);
  1310. netif_device_attach(vi->dev);
  1311. for (i = 0; i < vi->max_queue_pairs; i++)
  1312. if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
  1313. schedule_delayed_work(&vi->refill, 0);
  1314. mutex_lock(&vi->config_lock);
  1315. vi->config_enable = true;
  1316. mutex_unlock(&vi->config_lock);
  1317. virtnet_set_queues(vi, vi->curr_queue_pairs);
  1318. return 0;
  1319. }
  1320. #endif
  1321. static struct virtio_device_id id_table[] = {
  1322. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1323. { 0 },
  1324. };
  1325. static unsigned int features[] = {
  1326. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  1327. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  1328. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  1329. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  1330. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
  1331. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  1332. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  1333. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
  1334. };
  1335. static struct virtio_driver virtio_net_driver = {
  1336. .feature_table = features,
  1337. .feature_table_size = ARRAY_SIZE(features),
  1338. .driver.name = KBUILD_MODNAME,
  1339. .driver.owner = THIS_MODULE,
  1340. .id_table = id_table,
  1341. .probe = virtnet_probe,
  1342. .remove = virtnet_remove,
  1343. .config_changed = virtnet_config_changed,
  1344. #ifdef CONFIG_PM
  1345. .freeze = virtnet_freeze,
  1346. .restore = virtnet_restore,
  1347. #endif
  1348. };
  1349. static int __init init(void)
  1350. {
  1351. return register_virtio_driver(&virtio_net_driver);
  1352. }
  1353. static void __exit fini(void)
  1354. {
  1355. unregister_virtio_driver(&virtio_net_driver);
  1356. }
  1357. module_init(init);
  1358. module_exit(fini);
  1359. MODULE_DEVICE_TABLE(virtio, id_table);
  1360. MODULE_DESCRIPTION("Virtio network driver");
  1361. MODULE_LICENSE("GPL");