virtio_net.c 45 KB

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