virtio_net.c 45 KB

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