virtio_net.c 42 KB

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