virtio_net.c 46 KB

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