virtio_net.c 43 KB

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