virtio_net.c 43 KB

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