virtio_net.c 49 KB

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