virtio_net.c 50 KB

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