virtio_net.c 51 KB

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