virtio_net.c 41 KB

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