virtio_net.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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. virtqueue_kick(rq->vq);
  503. return !oom;
  504. }
  505. static void skb_recv_done(struct virtqueue *rvq)
  506. {
  507. struct virtnet_info *vi = rvq->vdev->priv;
  508. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  509. /* Schedule NAPI, Suppress further interrupts if successful. */
  510. if (napi_schedule_prep(&rq->napi)) {
  511. virtqueue_disable_cb(rvq);
  512. __napi_schedule(&rq->napi);
  513. }
  514. }
  515. static void virtnet_napi_enable(struct receive_queue *rq)
  516. {
  517. napi_enable(&rq->napi);
  518. /* If all buffers were filled by other side before we napi_enabled, we
  519. * won't get another interrupt, so process any outstanding packets
  520. * now. virtnet_poll wants re-enable the queue, so we disable here.
  521. * We synchronize against interrupts via NAPI_STATE_SCHED */
  522. if (napi_schedule_prep(&rq->napi)) {
  523. virtqueue_disable_cb(rq->vq);
  524. local_bh_disable();
  525. __napi_schedule(&rq->napi);
  526. local_bh_enable();
  527. }
  528. }
  529. static void refill_work(struct work_struct *work)
  530. {
  531. struct virtnet_info *vi =
  532. container_of(work, struct virtnet_info, refill.work);
  533. bool still_empty;
  534. int i;
  535. for (i = 0; i < vi->curr_queue_pairs; i++) {
  536. struct receive_queue *rq = &vi->rq[i];
  537. napi_disable(&rq->napi);
  538. still_empty = !try_fill_recv(rq, GFP_KERNEL);
  539. virtnet_napi_enable(rq);
  540. /* In theory, this can happen: if we don't get any buffers in
  541. * we will *never* try to fill again.
  542. */
  543. if (still_empty)
  544. schedule_delayed_work(&vi->refill, HZ/2);
  545. }
  546. }
  547. static int virtnet_poll(struct napi_struct *napi, int budget)
  548. {
  549. struct receive_queue *rq =
  550. container_of(napi, struct receive_queue, napi);
  551. struct virtnet_info *vi = rq->vq->vdev->priv;
  552. void *buf;
  553. unsigned int r, len, received = 0;
  554. again:
  555. while (received < budget &&
  556. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  557. receive_buf(rq, buf, len);
  558. --rq->num;
  559. received++;
  560. }
  561. if (rq->num < rq->max / 2) {
  562. if (!try_fill_recv(rq, GFP_ATOMIC))
  563. schedule_delayed_work(&vi->refill, 0);
  564. }
  565. /* Out of packets? */
  566. if (received < budget) {
  567. r = virtqueue_enable_cb_prepare(rq->vq);
  568. napi_complete(napi);
  569. if (unlikely(virtqueue_poll(rq->vq, r)) &&
  570. napi_schedule_prep(napi)) {
  571. virtqueue_disable_cb(rq->vq);
  572. __napi_schedule(napi);
  573. goto again;
  574. }
  575. }
  576. return received;
  577. }
  578. static int virtnet_open(struct net_device *dev)
  579. {
  580. struct virtnet_info *vi = netdev_priv(dev);
  581. int i;
  582. for (i = 0; i < vi->max_queue_pairs; i++) {
  583. if (i < vi->curr_queue_pairs)
  584. /* Make sure we have some buffers: if oom use wq. */
  585. if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
  586. schedule_delayed_work(&vi->refill, 0);
  587. virtnet_napi_enable(&vi->rq[i]);
  588. }
  589. return 0;
  590. }
  591. static void free_old_xmit_skbs(struct send_queue *sq)
  592. {
  593. struct sk_buff *skb;
  594. unsigned int len;
  595. struct virtnet_info *vi = sq->vq->vdev->priv;
  596. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  597. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  598. pr_debug("Sent skb %p\n", skb);
  599. u64_stats_update_begin(&stats->tx_syncp);
  600. stats->tx_bytes += skb->len;
  601. stats->tx_packets++;
  602. u64_stats_update_end(&stats->tx_syncp);
  603. dev_kfree_skb_any(skb);
  604. }
  605. }
  606. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  607. {
  608. struct skb_vnet_hdr *hdr;
  609. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  610. struct virtnet_info *vi = sq->vq->vdev->priv;
  611. unsigned num_sg;
  612. unsigned hdr_len;
  613. bool can_push;
  614. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  615. if (vi->mergeable_rx_bufs)
  616. hdr_len = sizeof hdr->mhdr;
  617. else
  618. hdr_len = sizeof hdr->hdr;
  619. can_push = vi->any_header_sg &&
  620. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  621. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  622. /* Even if we can, don't push here yet as this would skew
  623. * csum_start offset below. */
  624. if (can_push)
  625. hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
  626. else
  627. hdr = skb_vnet_hdr(skb);
  628. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  629. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  630. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  631. hdr->hdr.csum_offset = skb->csum_offset;
  632. } else {
  633. hdr->hdr.flags = 0;
  634. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  635. }
  636. if (skb_is_gso(skb)) {
  637. hdr->hdr.hdr_len = skb_headlen(skb);
  638. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  639. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  640. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  641. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  642. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  643. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  644. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  645. else
  646. BUG();
  647. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  648. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  649. } else {
  650. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  651. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  652. }
  653. if (vi->mergeable_rx_bufs)
  654. hdr->mhdr.num_buffers = 0;
  655. if (can_push) {
  656. __skb_push(skb, hdr_len);
  657. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  658. /* Pull header back to avoid skew in tx bytes calculations. */
  659. __skb_pull(skb, hdr_len);
  660. } else {
  661. sg_set_buf(sq->sg, hdr, hdr_len);
  662. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
  663. }
  664. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  665. }
  666. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  667. {
  668. struct virtnet_info *vi = netdev_priv(dev);
  669. int qnum = skb_get_queue_mapping(skb);
  670. struct send_queue *sq = &vi->sq[qnum];
  671. int err;
  672. /* Free up any pending old buffers before queueing new ones. */
  673. free_old_xmit_skbs(sq);
  674. /* Try to transmit */
  675. err = xmit_skb(sq, skb);
  676. /* This should not happen! */
  677. if (unlikely(err)) {
  678. dev->stats.tx_fifo_errors++;
  679. if (net_ratelimit())
  680. dev_warn(&dev->dev,
  681. "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
  682. dev->stats.tx_dropped++;
  683. kfree_skb(skb);
  684. return NETDEV_TX_OK;
  685. }
  686. virtqueue_kick(sq->vq);
  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. virtqueue_kick(vi->cvq);
  736. /* Spin for a response, the kick causes an ioport write, trapping
  737. * into the hypervisor, so the request should be handled immediately.
  738. */
  739. while (!virtqueue_get_buf(vi->cvq, &tmp))
  740. cpu_relax();
  741. return status == VIRTIO_NET_OK;
  742. }
  743. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  744. {
  745. struct virtnet_info *vi = netdev_priv(dev);
  746. struct virtio_device *vdev = vi->vdev;
  747. int ret;
  748. struct sockaddr *addr = p;
  749. struct scatterlist sg;
  750. ret = eth_prepare_mac_addr_change(dev, p);
  751. if (ret)
  752. return ret;
  753. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  754. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  755. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  756. VIRTIO_NET_CTRL_MAC_ADDR_SET,
  757. &sg, NULL)) {
  758. dev_warn(&vdev->dev,
  759. "Failed to set mac address by vq command.\n");
  760. return -EINVAL;
  761. }
  762. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  763. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  764. addr->sa_data, dev->addr_len);
  765. }
  766. eth_commit_mac_addr_change(dev, p);
  767. return 0;
  768. }
  769. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  770. struct rtnl_link_stats64 *tot)
  771. {
  772. struct virtnet_info *vi = netdev_priv(dev);
  773. int cpu;
  774. unsigned int start;
  775. for_each_possible_cpu(cpu) {
  776. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  777. u64 tpackets, tbytes, rpackets, rbytes;
  778. do {
  779. start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
  780. tpackets = stats->tx_packets;
  781. tbytes = stats->tx_bytes;
  782. } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
  783. do {
  784. start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
  785. rpackets = stats->rx_packets;
  786. rbytes = stats->rx_bytes;
  787. } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
  788. tot->rx_packets += rpackets;
  789. tot->tx_packets += tpackets;
  790. tot->rx_bytes += rbytes;
  791. tot->tx_bytes += tbytes;
  792. }
  793. tot->tx_dropped = dev->stats.tx_dropped;
  794. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  795. tot->rx_dropped = dev->stats.rx_dropped;
  796. tot->rx_length_errors = dev->stats.rx_length_errors;
  797. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  798. return tot;
  799. }
  800. #ifdef CONFIG_NET_POLL_CONTROLLER
  801. static void virtnet_netpoll(struct net_device *dev)
  802. {
  803. struct virtnet_info *vi = netdev_priv(dev);
  804. int i;
  805. for (i = 0; i < vi->curr_queue_pairs; i++)
  806. napi_schedule(&vi->rq[i].napi);
  807. }
  808. #endif
  809. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  810. {
  811. rtnl_lock();
  812. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  813. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
  814. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  815. rtnl_unlock();
  816. }
  817. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  818. {
  819. struct scatterlist sg;
  820. struct virtio_net_ctrl_mq s;
  821. struct net_device *dev = vi->dev;
  822. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  823. return 0;
  824. s.virtqueue_pairs = queue_pairs;
  825. sg_init_one(&sg, &s, sizeof(s));
  826. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  827. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
  828. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  829. queue_pairs);
  830. return -EINVAL;
  831. } else {
  832. vi->curr_queue_pairs = queue_pairs;
  833. /* virtnet_open() will refill when device is going to up. */
  834. if (dev->flags & IFF_UP)
  835. schedule_delayed_work(&vi->refill, 0);
  836. }
  837. return 0;
  838. }
  839. static int virtnet_close(struct net_device *dev)
  840. {
  841. struct virtnet_info *vi = netdev_priv(dev);
  842. int i;
  843. /* Make sure refill_work doesn't re-enable napi! */
  844. cancel_delayed_work_sync(&vi->refill);
  845. for (i = 0; i < vi->max_queue_pairs; i++)
  846. napi_disable(&vi->rq[i].napi);
  847. return 0;
  848. }
  849. static void virtnet_set_rx_mode(struct net_device *dev)
  850. {
  851. struct virtnet_info *vi = netdev_priv(dev);
  852. struct scatterlist sg[2];
  853. u8 promisc, allmulti;
  854. struct virtio_net_ctrl_mac *mac_data;
  855. struct netdev_hw_addr *ha;
  856. int uc_count;
  857. int mc_count;
  858. void *buf;
  859. int i;
  860. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  861. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  862. return;
  863. promisc = ((dev->flags & IFF_PROMISC) != 0);
  864. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  865. sg_init_one(sg, &promisc, sizeof(promisc));
  866. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  867. VIRTIO_NET_CTRL_RX_PROMISC,
  868. sg, NULL))
  869. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  870. promisc ? "en" : "dis");
  871. sg_init_one(sg, &allmulti, sizeof(allmulti));
  872. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  873. VIRTIO_NET_CTRL_RX_ALLMULTI,
  874. sg, NULL))
  875. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  876. allmulti ? "en" : "dis");
  877. uc_count = netdev_uc_count(dev);
  878. mc_count = netdev_mc_count(dev);
  879. /* MAC filter - use one buffer for both lists */
  880. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  881. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  882. mac_data = buf;
  883. if (!buf)
  884. return;
  885. sg_init_table(sg, 2);
  886. /* Store the unicast list and count in the front of the buffer */
  887. mac_data->entries = uc_count;
  888. i = 0;
  889. netdev_for_each_uc_addr(ha, dev)
  890. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  891. sg_set_buf(&sg[0], mac_data,
  892. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  893. /* multicast list and count fill the end */
  894. mac_data = (void *)&mac_data->macs[uc_count][0];
  895. mac_data->entries = mc_count;
  896. i = 0;
  897. netdev_for_each_mc_addr(ha, dev)
  898. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  899. sg_set_buf(&sg[1], mac_data,
  900. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  901. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  902. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  903. sg, NULL))
  904. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  905. kfree(buf);
  906. }
  907. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  908. __be16 proto, u16 vid)
  909. {
  910. struct virtnet_info *vi = netdev_priv(dev);
  911. struct scatterlist sg;
  912. sg_init_one(&sg, &vid, sizeof(vid));
  913. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  914. VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
  915. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  916. return 0;
  917. }
  918. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  919. __be16 proto, u16 vid)
  920. {
  921. struct virtnet_info *vi = netdev_priv(dev);
  922. struct scatterlist sg;
  923. sg_init_one(&sg, &vid, sizeof(vid));
  924. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  925. VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
  926. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  927. return 0;
  928. }
  929. static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
  930. {
  931. int i;
  932. if (vi->affinity_hint_set) {
  933. for (i = 0; i < vi->max_queue_pairs; i++) {
  934. virtqueue_set_affinity(vi->rq[i].vq, -1);
  935. virtqueue_set_affinity(vi->sq[i].vq, -1);
  936. }
  937. vi->affinity_hint_set = false;
  938. }
  939. }
  940. static void virtnet_set_affinity(struct virtnet_info *vi)
  941. {
  942. int i;
  943. int cpu;
  944. /* In multiqueue mode, when the number of cpu is equal to the number of
  945. * queue pairs, we let the queue pairs to be private to one cpu by
  946. * setting the affinity hint to eliminate the contention.
  947. */
  948. if (vi->curr_queue_pairs == 1 ||
  949. vi->max_queue_pairs != num_online_cpus()) {
  950. virtnet_clean_affinity(vi, -1);
  951. return;
  952. }
  953. i = 0;
  954. for_each_online_cpu(cpu) {
  955. virtqueue_set_affinity(vi->rq[i].vq, cpu);
  956. virtqueue_set_affinity(vi->sq[i].vq, cpu);
  957. netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
  958. i++;
  959. }
  960. vi->affinity_hint_set = true;
  961. }
  962. static int virtnet_cpu_callback(struct notifier_block *nfb,
  963. unsigned long action, void *hcpu)
  964. {
  965. struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
  966. switch(action & ~CPU_TASKS_FROZEN) {
  967. case CPU_ONLINE:
  968. case CPU_DOWN_FAILED:
  969. case CPU_DEAD:
  970. virtnet_set_affinity(vi);
  971. break;
  972. case CPU_DOWN_PREPARE:
  973. virtnet_clean_affinity(vi, (long)hcpu);
  974. break;
  975. default:
  976. break;
  977. }
  978. return NOTIFY_OK;
  979. }
  980. static void virtnet_get_ringparam(struct net_device *dev,
  981. struct ethtool_ringparam *ring)
  982. {
  983. struct virtnet_info *vi = netdev_priv(dev);
  984. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  985. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  986. ring->rx_pending = ring->rx_max_pending;
  987. ring->tx_pending = ring->tx_max_pending;
  988. }
  989. static void virtnet_get_drvinfo(struct net_device *dev,
  990. struct ethtool_drvinfo *info)
  991. {
  992. struct virtnet_info *vi = netdev_priv(dev);
  993. struct virtio_device *vdev = vi->vdev;
  994. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  995. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  996. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  997. }
  998. /* TODO: Eliminate OOO packets during switching */
  999. static int virtnet_set_channels(struct net_device *dev,
  1000. struct ethtool_channels *channels)
  1001. {
  1002. struct virtnet_info *vi = netdev_priv(dev);
  1003. u16 queue_pairs = channels->combined_count;
  1004. int err;
  1005. /* We don't support separate rx/tx channels.
  1006. * We don't allow setting 'other' channels.
  1007. */
  1008. if (channels->rx_count || channels->tx_count || channels->other_count)
  1009. return -EINVAL;
  1010. if (queue_pairs > vi->max_queue_pairs)
  1011. return -EINVAL;
  1012. get_online_cpus();
  1013. err = virtnet_set_queues(vi, queue_pairs);
  1014. if (!err) {
  1015. netif_set_real_num_tx_queues(dev, queue_pairs);
  1016. netif_set_real_num_rx_queues(dev, queue_pairs);
  1017. virtnet_set_affinity(vi);
  1018. }
  1019. put_online_cpus();
  1020. return err;
  1021. }
  1022. static void virtnet_get_channels(struct net_device *dev,
  1023. struct ethtool_channels *channels)
  1024. {
  1025. struct virtnet_info *vi = netdev_priv(dev);
  1026. channels->combined_count = vi->curr_queue_pairs;
  1027. channels->max_combined = vi->max_queue_pairs;
  1028. channels->max_other = 0;
  1029. channels->rx_count = 0;
  1030. channels->tx_count = 0;
  1031. channels->other_count = 0;
  1032. }
  1033. static const struct ethtool_ops virtnet_ethtool_ops = {
  1034. .get_drvinfo = virtnet_get_drvinfo,
  1035. .get_link = ethtool_op_get_link,
  1036. .get_ringparam = virtnet_get_ringparam,
  1037. .set_channels = virtnet_set_channels,
  1038. .get_channels = virtnet_get_channels,
  1039. };
  1040. #define MIN_MTU 68
  1041. #define MAX_MTU 65535
  1042. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  1043. {
  1044. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  1045. return -EINVAL;
  1046. dev->mtu = new_mtu;
  1047. return 0;
  1048. }
  1049. static const struct net_device_ops virtnet_netdev = {
  1050. .ndo_open = virtnet_open,
  1051. .ndo_stop = virtnet_close,
  1052. .ndo_start_xmit = start_xmit,
  1053. .ndo_validate_addr = eth_validate_addr,
  1054. .ndo_set_mac_address = virtnet_set_mac_address,
  1055. .ndo_set_rx_mode = virtnet_set_rx_mode,
  1056. .ndo_change_mtu = virtnet_change_mtu,
  1057. .ndo_get_stats64 = virtnet_stats,
  1058. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  1059. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  1060. #ifdef CONFIG_NET_POLL_CONTROLLER
  1061. .ndo_poll_controller = virtnet_netpoll,
  1062. #endif
  1063. };
  1064. static void virtnet_config_changed_work(struct work_struct *work)
  1065. {
  1066. struct virtnet_info *vi =
  1067. container_of(work, struct virtnet_info, config_work);
  1068. u16 v;
  1069. mutex_lock(&vi->config_lock);
  1070. if (!vi->config_enable)
  1071. goto done;
  1072. if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
  1073. offsetof(struct virtio_net_config, status),
  1074. &v) < 0)
  1075. goto done;
  1076. if (v & VIRTIO_NET_S_ANNOUNCE) {
  1077. netdev_notify_peers(vi->dev);
  1078. virtnet_ack_link_announce(vi);
  1079. }
  1080. /* Ignore unknown (future) status bits */
  1081. v &= VIRTIO_NET_S_LINK_UP;
  1082. if (vi->status == v)
  1083. goto done;
  1084. vi->status = v;
  1085. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  1086. netif_carrier_on(vi->dev);
  1087. netif_tx_wake_all_queues(vi->dev);
  1088. } else {
  1089. netif_carrier_off(vi->dev);
  1090. netif_tx_stop_all_queues(vi->dev);
  1091. }
  1092. done:
  1093. mutex_unlock(&vi->config_lock);
  1094. }
  1095. static void virtnet_config_changed(struct virtio_device *vdev)
  1096. {
  1097. struct virtnet_info *vi = vdev->priv;
  1098. schedule_work(&vi->config_work);
  1099. }
  1100. static void virtnet_free_queues(struct virtnet_info *vi)
  1101. {
  1102. kfree(vi->rq);
  1103. kfree(vi->sq);
  1104. }
  1105. static void free_receive_bufs(struct virtnet_info *vi)
  1106. {
  1107. int i;
  1108. for (i = 0; i < vi->max_queue_pairs; i++) {
  1109. while (vi->rq[i].pages)
  1110. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  1111. }
  1112. }
  1113. static void free_unused_bufs(struct virtnet_info *vi)
  1114. {
  1115. void *buf;
  1116. int i;
  1117. for (i = 0; i < vi->max_queue_pairs; i++) {
  1118. struct virtqueue *vq = vi->sq[i].vq;
  1119. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  1120. dev_kfree_skb(buf);
  1121. }
  1122. for (i = 0; i < vi->max_queue_pairs; i++) {
  1123. struct virtqueue *vq = vi->rq[i].vq;
  1124. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1125. if (vi->big_packets)
  1126. give_pages(&vi->rq[i], buf);
  1127. else if (vi->mergeable_rx_bufs)
  1128. put_page(virt_to_head_page(buf));
  1129. else
  1130. dev_kfree_skb(buf);
  1131. --vi->rq[i].num;
  1132. }
  1133. BUG_ON(vi->rq[i].num != 0);
  1134. }
  1135. }
  1136. static void virtnet_del_vqs(struct virtnet_info *vi)
  1137. {
  1138. struct virtio_device *vdev = vi->vdev;
  1139. virtnet_clean_affinity(vi, -1);
  1140. vdev->config->del_vqs(vdev);
  1141. virtnet_free_queues(vi);
  1142. }
  1143. static int virtnet_find_vqs(struct virtnet_info *vi)
  1144. {
  1145. vq_callback_t **callbacks;
  1146. struct virtqueue **vqs;
  1147. int ret = -ENOMEM;
  1148. int i, total_vqs;
  1149. const char **names;
  1150. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  1151. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  1152. * possible control vq.
  1153. */
  1154. total_vqs = vi->max_queue_pairs * 2 +
  1155. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  1156. /* Allocate space for find_vqs parameters */
  1157. vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
  1158. if (!vqs)
  1159. goto err_vq;
  1160. callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
  1161. if (!callbacks)
  1162. goto err_callback;
  1163. names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
  1164. if (!names)
  1165. goto err_names;
  1166. /* Parameters for control virtqueue, if any */
  1167. if (vi->has_cvq) {
  1168. callbacks[total_vqs - 1] = NULL;
  1169. names[total_vqs - 1] = "control";
  1170. }
  1171. /* Allocate/initialize parameters for send/receive virtqueues */
  1172. for (i = 0; i < vi->max_queue_pairs; i++) {
  1173. callbacks[rxq2vq(i)] = skb_recv_done;
  1174. callbacks[txq2vq(i)] = skb_xmit_done;
  1175. sprintf(vi->rq[i].name, "input.%d", i);
  1176. sprintf(vi->sq[i].name, "output.%d", i);
  1177. names[rxq2vq(i)] = vi->rq[i].name;
  1178. names[txq2vq(i)] = vi->sq[i].name;
  1179. }
  1180. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  1181. names);
  1182. if (ret)
  1183. goto err_find;
  1184. if (vi->has_cvq) {
  1185. vi->cvq = vqs[total_vqs - 1];
  1186. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  1187. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1188. }
  1189. for (i = 0; i < vi->max_queue_pairs; i++) {
  1190. vi->rq[i].vq = vqs[rxq2vq(i)];
  1191. vi->sq[i].vq = vqs[txq2vq(i)];
  1192. }
  1193. kfree(names);
  1194. kfree(callbacks);
  1195. kfree(vqs);
  1196. return 0;
  1197. err_find:
  1198. kfree(names);
  1199. err_names:
  1200. kfree(callbacks);
  1201. err_callback:
  1202. kfree(vqs);
  1203. err_vq:
  1204. return ret;
  1205. }
  1206. static int virtnet_alloc_queues(struct virtnet_info *vi)
  1207. {
  1208. int i;
  1209. vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
  1210. if (!vi->sq)
  1211. goto err_sq;
  1212. vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
  1213. if (!vi->rq)
  1214. goto err_rq;
  1215. INIT_DELAYED_WORK(&vi->refill, refill_work);
  1216. for (i = 0; i < vi->max_queue_pairs; i++) {
  1217. vi->rq[i].pages = NULL;
  1218. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  1219. napi_weight);
  1220. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  1221. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  1222. }
  1223. return 0;
  1224. err_rq:
  1225. kfree(vi->sq);
  1226. err_sq:
  1227. return -ENOMEM;
  1228. }
  1229. static int init_vqs(struct virtnet_info *vi)
  1230. {
  1231. int ret;
  1232. /* Allocate send & receive queues */
  1233. ret = virtnet_alloc_queues(vi);
  1234. if (ret)
  1235. goto err;
  1236. ret = virtnet_find_vqs(vi);
  1237. if (ret)
  1238. goto err_free;
  1239. get_online_cpus();
  1240. virtnet_set_affinity(vi);
  1241. put_online_cpus();
  1242. return 0;
  1243. err_free:
  1244. virtnet_free_queues(vi);
  1245. err:
  1246. return ret;
  1247. }
  1248. static int virtnet_probe(struct virtio_device *vdev)
  1249. {
  1250. int i, err;
  1251. struct net_device *dev;
  1252. struct virtnet_info *vi;
  1253. u16 max_queue_pairs;
  1254. /* Find if host supports multiqueue virtio_net device */
  1255. err = virtio_config_val(vdev, VIRTIO_NET_F_MQ,
  1256. offsetof(struct virtio_net_config,
  1257. max_virtqueue_pairs), &max_queue_pairs);
  1258. /* We need at least 2 queue's */
  1259. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  1260. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  1261. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1262. max_queue_pairs = 1;
  1263. /* Allocate ourselves a network device with room for our info */
  1264. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  1265. if (!dev)
  1266. return -ENOMEM;
  1267. /* Set up network device as normal. */
  1268. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1269. dev->netdev_ops = &virtnet_netdev;
  1270. dev->features = NETIF_F_HIGHDMA;
  1271. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  1272. SET_NETDEV_DEV(dev, &vdev->dev);
  1273. /* Do we support "hardware" checksums? */
  1274. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  1275. /* This opens up the world of extra features. */
  1276. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  1277. if (csum)
  1278. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  1279. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  1280. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  1281. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  1282. }
  1283. /* Individual feature bits: what can host handle? */
  1284. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  1285. dev->hw_features |= NETIF_F_TSO;
  1286. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  1287. dev->hw_features |= NETIF_F_TSO6;
  1288. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  1289. dev->hw_features |= NETIF_F_TSO_ECN;
  1290. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  1291. dev->hw_features |= NETIF_F_UFO;
  1292. if (gso)
  1293. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  1294. /* (!csum && gso) case will be fixed by register_netdev() */
  1295. }
  1296. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  1297. dev->features |= NETIF_F_RXCSUM;
  1298. dev->vlan_features = dev->features;
  1299. /* Configuration may specify what MAC to use. Otherwise random. */
  1300. if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
  1301. offsetof(struct virtio_net_config, mac),
  1302. dev->dev_addr, dev->addr_len) < 0)
  1303. eth_hw_addr_random(dev);
  1304. /* Set up our device-specific information */
  1305. vi = netdev_priv(dev);
  1306. vi->dev = dev;
  1307. vi->vdev = vdev;
  1308. vdev->priv = vi;
  1309. vi->stats = alloc_percpu(struct virtnet_stats);
  1310. err = -ENOMEM;
  1311. if (vi->stats == NULL)
  1312. goto free;
  1313. mutex_init(&vi->config_lock);
  1314. vi->config_enable = true;
  1315. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  1316. /* If we can receive ANY GSO packets, we must allocate large ones. */
  1317. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  1318. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  1319. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  1320. vi->big_packets = true;
  1321. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  1322. vi->mergeable_rx_bufs = true;
  1323. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
  1324. vi->any_header_sg = true;
  1325. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1326. vi->has_cvq = true;
  1327. /* Use single tx/rx queue pair as default */
  1328. vi->curr_queue_pairs = 1;
  1329. vi->max_queue_pairs = max_queue_pairs;
  1330. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  1331. err = init_vqs(vi);
  1332. if (err)
  1333. goto free_stats;
  1334. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  1335. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  1336. err = register_netdev(dev);
  1337. if (err) {
  1338. pr_debug("virtio_net: registering device failed\n");
  1339. goto free_vqs;
  1340. }
  1341. /* Last of all, set up some receive buffers. */
  1342. for (i = 0; i < vi->curr_queue_pairs; i++) {
  1343. try_fill_recv(&vi->rq[i], GFP_KERNEL);
  1344. /* If we didn't even get one input buffer, we're useless. */
  1345. if (vi->rq[i].num == 0) {
  1346. free_unused_bufs(vi);
  1347. err = -ENOMEM;
  1348. goto free_recv_bufs;
  1349. }
  1350. }
  1351. vi->nb.notifier_call = &virtnet_cpu_callback;
  1352. err = register_hotcpu_notifier(&vi->nb);
  1353. if (err) {
  1354. pr_debug("virtio_net: registering cpu notifier failed\n");
  1355. goto free_recv_bufs;
  1356. }
  1357. /* Assume link up if device can't report link status,
  1358. otherwise get link status from config. */
  1359. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  1360. netif_carrier_off(dev);
  1361. schedule_work(&vi->config_work);
  1362. } else {
  1363. vi->status = VIRTIO_NET_S_LINK_UP;
  1364. netif_carrier_on(dev);
  1365. }
  1366. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  1367. dev->name, max_queue_pairs);
  1368. return 0;
  1369. free_recv_bufs:
  1370. free_receive_bufs(vi);
  1371. unregister_netdev(dev);
  1372. free_vqs:
  1373. cancel_delayed_work_sync(&vi->refill);
  1374. virtnet_del_vqs(vi);
  1375. if (vi->alloc_frag.page)
  1376. put_page(vi->alloc_frag.page);
  1377. free_stats:
  1378. free_percpu(vi->stats);
  1379. free:
  1380. free_netdev(dev);
  1381. return err;
  1382. }
  1383. static void remove_vq_common(struct virtnet_info *vi)
  1384. {
  1385. vi->vdev->config->reset(vi->vdev);
  1386. /* Free unused buffers in both send and recv, if any. */
  1387. free_unused_bufs(vi);
  1388. free_receive_bufs(vi);
  1389. virtnet_del_vqs(vi);
  1390. }
  1391. static void virtnet_remove(struct virtio_device *vdev)
  1392. {
  1393. struct virtnet_info *vi = vdev->priv;
  1394. unregister_hotcpu_notifier(&vi->nb);
  1395. /* Prevent config work handler from accessing the device. */
  1396. mutex_lock(&vi->config_lock);
  1397. vi->config_enable = false;
  1398. mutex_unlock(&vi->config_lock);
  1399. unregister_netdev(vi->dev);
  1400. remove_vq_common(vi);
  1401. if (vi->alloc_frag.page)
  1402. put_page(vi->alloc_frag.page);
  1403. flush_work(&vi->config_work);
  1404. free_percpu(vi->stats);
  1405. free_netdev(vi->dev);
  1406. }
  1407. #ifdef CONFIG_PM
  1408. static int virtnet_freeze(struct virtio_device *vdev)
  1409. {
  1410. struct virtnet_info *vi = vdev->priv;
  1411. int i;
  1412. unregister_hotcpu_notifier(&vi->nb);
  1413. /* Prevent config work handler from accessing the device */
  1414. mutex_lock(&vi->config_lock);
  1415. vi->config_enable = false;
  1416. mutex_unlock(&vi->config_lock);
  1417. netif_device_detach(vi->dev);
  1418. cancel_delayed_work_sync(&vi->refill);
  1419. if (netif_running(vi->dev))
  1420. for (i = 0; i < vi->max_queue_pairs; i++) {
  1421. napi_disable(&vi->rq[i].napi);
  1422. netif_napi_del(&vi->rq[i].napi);
  1423. }
  1424. remove_vq_common(vi);
  1425. flush_work(&vi->config_work);
  1426. return 0;
  1427. }
  1428. static int virtnet_restore(struct virtio_device *vdev)
  1429. {
  1430. struct virtnet_info *vi = vdev->priv;
  1431. int err, i;
  1432. err = init_vqs(vi);
  1433. if (err)
  1434. return err;
  1435. if (netif_running(vi->dev))
  1436. for (i = 0; i < vi->max_queue_pairs; i++)
  1437. virtnet_napi_enable(&vi->rq[i]);
  1438. netif_device_attach(vi->dev);
  1439. for (i = 0; i < vi->curr_queue_pairs; i++)
  1440. if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
  1441. schedule_delayed_work(&vi->refill, 0);
  1442. mutex_lock(&vi->config_lock);
  1443. vi->config_enable = true;
  1444. mutex_unlock(&vi->config_lock);
  1445. rtnl_lock();
  1446. virtnet_set_queues(vi, vi->curr_queue_pairs);
  1447. rtnl_unlock();
  1448. err = register_hotcpu_notifier(&vi->nb);
  1449. if (err)
  1450. return err;
  1451. return 0;
  1452. }
  1453. #endif
  1454. static struct virtio_device_id id_table[] = {
  1455. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1456. { 0 },
  1457. };
  1458. static unsigned int features[] = {
  1459. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  1460. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  1461. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  1462. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  1463. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
  1464. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  1465. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  1466. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
  1467. VIRTIO_NET_F_CTRL_MAC_ADDR,
  1468. VIRTIO_F_ANY_LAYOUT,
  1469. };
  1470. static struct virtio_driver virtio_net_driver = {
  1471. .feature_table = features,
  1472. .feature_table_size = ARRAY_SIZE(features),
  1473. .driver.name = KBUILD_MODNAME,
  1474. .driver.owner = THIS_MODULE,
  1475. .id_table = id_table,
  1476. .probe = virtnet_probe,
  1477. .remove = virtnet_remove,
  1478. .config_changed = virtnet_config_changed,
  1479. #ifdef CONFIG_PM
  1480. .freeze = virtnet_freeze,
  1481. .restore = virtnet_restore,
  1482. #endif
  1483. };
  1484. module_virtio_driver(virtio_net_driver);
  1485. MODULE_DEVICE_TABLE(virtio, id_table);
  1486. MODULE_DESCRIPTION("Virtio network driver");
  1487. MODULE_LICENSE("GPL");