virtio_net.c 50 KB

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