virtio_net.c 43 KB

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