virtio_net.c 46 KB

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