virtio_net.c 44 KB

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