virtio_net.c 54 KB

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