virtio_net.c 41 KB

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