virtio_net.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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. skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
  438. err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
  439. if (err < 0)
  440. dev_kfree_skb(skb);
  441. return err;
  442. }
  443. static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  444. gfp_t gfp)
  445. {
  446. struct page *first, *list = NULL;
  447. char *p;
  448. int i, err, offset;
  449. sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
  450. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  451. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  452. first = get_a_page(rq, gfp);
  453. if (!first) {
  454. if (list)
  455. give_pages(rq, list);
  456. return -ENOMEM;
  457. }
  458. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  459. /* chain new page in list head to match sg */
  460. first->private = (unsigned long)list;
  461. list = first;
  462. }
  463. first = get_a_page(rq, gfp);
  464. if (!first) {
  465. give_pages(rq, list);
  466. return -ENOMEM;
  467. }
  468. p = page_address(first);
  469. /* rq->sg[0], rq->sg[1] share the same page */
  470. /* a separated rq->sg[0] for header - required in case !any_header_sg */
  471. sg_set_buf(&rq->sg[0], p, vi->hdr_len);
  472. /* rq->sg[1] for data packet, from offset */
  473. offset = sizeof(struct padded_vnet_hdr);
  474. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  475. /* chain first in list head */
  476. first->private = (unsigned long)list;
  477. err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
  478. first, gfp);
  479. if (err < 0)
  480. give_pages(rq, first);
  481. return err;
  482. }
  483. static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
  484. {
  485. const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  486. unsigned int len;
  487. len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
  488. GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
  489. return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
  490. }
  491. static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
  492. {
  493. struct page_frag *alloc_frag = &rq->alloc_frag;
  494. char *buf;
  495. unsigned long ctx;
  496. int err;
  497. unsigned int len, hole;
  498. len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
  499. if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
  500. return -ENOMEM;
  501. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  502. ctx = mergeable_buf_to_ctx(buf, len);
  503. get_page(alloc_frag->page);
  504. alloc_frag->offset += len;
  505. hole = alloc_frag->size - alloc_frag->offset;
  506. if (hole < len) {
  507. /* To avoid internal fragmentation, if there is very likely not
  508. * enough space for another buffer, add the remaining space to
  509. * the current buffer. This extra space is not included in
  510. * the truesize stored in ctx.
  511. */
  512. len += hole;
  513. alloc_frag->offset += hole;
  514. }
  515. sg_init_one(rq->sg, buf, len);
  516. err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
  517. if (err < 0)
  518. put_page(virt_to_head_page(buf));
  519. return err;
  520. }
  521. /*
  522. * Returns false if we couldn't fill entirely (OOM).
  523. *
  524. * Normally run in the receive path, but can also be run from ndo_open
  525. * before we're receiving packets, or from refill_work which is
  526. * careful to disable receiving (using napi_disable).
  527. */
  528. static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
  529. gfp_t gfp)
  530. {
  531. int err;
  532. bool oom;
  533. gfp |= __GFP_COLD;
  534. do {
  535. if (vi->mergeable_rx_bufs)
  536. err = add_recvbuf_mergeable(rq, gfp);
  537. else if (vi->big_packets)
  538. err = add_recvbuf_big(vi, rq, gfp);
  539. else
  540. err = add_recvbuf_small(vi, rq, gfp);
  541. oom = err == -ENOMEM;
  542. if (err)
  543. break;
  544. } while (rq->vq->num_free);
  545. virtqueue_kick(rq->vq);
  546. return !oom;
  547. }
  548. static void skb_recv_done(struct virtqueue *rvq)
  549. {
  550. struct virtnet_info *vi = rvq->vdev->priv;
  551. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  552. /* Schedule NAPI, Suppress further interrupts if successful. */
  553. if (napi_schedule_prep(&rq->napi)) {
  554. virtqueue_disable_cb(rvq);
  555. __napi_schedule(&rq->napi);
  556. }
  557. }
  558. static void virtnet_napi_enable(struct receive_queue *rq)
  559. {
  560. napi_enable(&rq->napi);
  561. /* If all buffers were filled by other side before we napi_enabled, we
  562. * won't get another interrupt, so process any outstanding packets
  563. * now. virtnet_poll wants re-enable the queue, so we disable here.
  564. * We synchronize against interrupts via NAPI_STATE_SCHED */
  565. if (napi_schedule_prep(&rq->napi)) {
  566. virtqueue_disable_cb(rq->vq);
  567. local_bh_disable();
  568. __napi_schedule(&rq->napi);
  569. local_bh_enable();
  570. }
  571. }
  572. static void refill_work(struct work_struct *work)
  573. {
  574. struct virtnet_info *vi =
  575. container_of(work, struct virtnet_info, refill.work);
  576. bool still_empty;
  577. int i;
  578. for (i = 0; i < vi->curr_queue_pairs; i++) {
  579. struct receive_queue *rq = &vi->rq[i];
  580. napi_disable(&rq->napi);
  581. still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
  582. virtnet_napi_enable(rq);
  583. /* In theory, this can happen: if we don't get any buffers in
  584. * we will *never* try to fill again.
  585. */
  586. if (still_empty)
  587. schedule_delayed_work(&vi->refill, HZ/2);
  588. }
  589. }
  590. static int virtnet_receive(struct receive_queue *rq, int budget)
  591. {
  592. struct virtnet_info *vi = rq->vq->vdev->priv;
  593. unsigned int len, received = 0;
  594. void *buf;
  595. while (received < budget &&
  596. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  597. receive_buf(vi, rq, buf, len);
  598. received++;
  599. }
  600. if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
  601. if (!try_fill_recv(vi, rq, GFP_ATOMIC))
  602. schedule_delayed_work(&vi->refill, 0);
  603. }
  604. return received;
  605. }
  606. static int virtnet_poll(struct napi_struct *napi, int budget)
  607. {
  608. struct receive_queue *rq =
  609. container_of(napi, struct receive_queue, napi);
  610. unsigned int r, received;
  611. received = virtnet_receive(rq, budget);
  612. /* Out of packets? */
  613. if (received < budget) {
  614. r = virtqueue_enable_cb_prepare(rq->vq);
  615. napi_complete_done(napi, received);
  616. if (unlikely(virtqueue_poll(rq->vq, r)) &&
  617. napi_schedule_prep(napi)) {
  618. virtqueue_disable_cb(rq->vq);
  619. __napi_schedule(napi);
  620. }
  621. }
  622. return received;
  623. }
  624. #ifdef CONFIG_NET_RX_BUSY_POLL
  625. /* must be called with local_bh_disable()d */
  626. static int virtnet_busy_poll(struct napi_struct *napi)
  627. {
  628. struct receive_queue *rq =
  629. container_of(napi, struct receive_queue, napi);
  630. struct virtnet_info *vi = rq->vq->vdev->priv;
  631. int r, received = 0, budget = 4;
  632. if (!(vi->status & VIRTIO_NET_S_LINK_UP))
  633. return LL_FLUSH_FAILED;
  634. if (!napi_schedule_prep(napi))
  635. return LL_FLUSH_BUSY;
  636. virtqueue_disable_cb(rq->vq);
  637. again:
  638. received += virtnet_receive(rq, budget);
  639. r = virtqueue_enable_cb_prepare(rq->vq);
  640. clear_bit(NAPI_STATE_SCHED, &napi->state);
  641. if (unlikely(virtqueue_poll(rq->vq, r)) &&
  642. napi_schedule_prep(napi)) {
  643. virtqueue_disable_cb(rq->vq);
  644. if (received < budget) {
  645. budget -= received;
  646. goto again;
  647. } else {
  648. __napi_schedule(napi);
  649. }
  650. }
  651. return received;
  652. }
  653. #endif /* CONFIG_NET_RX_BUSY_POLL */
  654. static int virtnet_open(struct net_device *dev)
  655. {
  656. struct virtnet_info *vi = netdev_priv(dev);
  657. int i;
  658. for (i = 0; i < vi->max_queue_pairs; i++) {
  659. if (i < vi->curr_queue_pairs)
  660. /* Make sure we have some buffers: if oom use wq. */
  661. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  662. schedule_delayed_work(&vi->refill, 0);
  663. virtnet_napi_enable(&vi->rq[i]);
  664. }
  665. return 0;
  666. }
  667. static void free_old_xmit_skbs(struct send_queue *sq)
  668. {
  669. struct sk_buff *skb;
  670. unsigned int len;
  671. struct virtnet_info *vi = sq->vq->vdev->priv;
  672. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  673. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  674. pr_debug("Sent skb %p\n", skb);
  675. u64_stats_update_begin(&stats->tx_syncp);
  676. stats->tx_bytes += skb->len;
  677. stats->tx_packets++;
  678. u64_stats_update_end(&stats->tx_syncp);
  679. dev_kfree_skb_any(skb);
  680. }
  681. }
  682. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  683. {
  684. struct virtio_net_hdr_mrg_rxbuf *hdr;
  685. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  686. struct virtnet_info *vi = sq->vq->vdev->priv;
  687. unsigned num_sg;
  688. unsigned hdr_len = vi->hdr_len;
  689. bool can_push;
  690. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  691. can_push = vi->any_header_sg &&
  692. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  693. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  694. /* Even if we can, don't push here yet as this would skew
  695. * csum_start offset below. */
  696. if (can_push)
  697. hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
  698. else
  699. hdr = skb_vnet_hdr(skb);
  700. if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
  701. virtio_is_little_endian(vi->vdev), false))
  702. BUG();
  703. if (vi->mergeable_rx_bufs)
  704. hdr->num_buffers = 0;
  705. sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
  706. if (can_push) {
  707. __skb_push(skb, hdr_len);
  708. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  709. /* Pull header back to avoid skew in tx bytes calculations. */
  710. __skb_pull(skb, hdr_len);
  711. } else {
  712. sg_set_buf(sq->sg, hdr, hdr_len);
  713. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
  714. }
  715. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  716. }
  717. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  718. {
  719. struct virtnet_info *vi = netdev_priv(dev);
  720. int qnum = skb_get_queue_mapping(skb);
  721. struct send_queue *sq = &vi->sq[qnum];
  722. int err;
  723. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  724. bool kick = !skb->xmit_more;
  725. /* Free up any pending old buffers before queueing new ones. */
  726. free_old_xmit_skbs(sq);
  727. /* timestamp packet in software */
  728. skb_tx_timestamp(skb);
  729. /* Try to transmit */
  730. err = xmit_skb(sq, skb);
  731. /* This should not happen! */
  732. if (unlikely(err)) {
  733. dev->stats.tx_fifo_errors++;
  734. if (net_ratelimit())
  735. dev_warn(&dev->dev,
  736. "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
  737. dev->stats.tx_dropped++;
  738. dev_kfree_skb_any(skb);
  739. return NETDEV_TX_OK;
  740. }
  741. /* Don't wait up for transmitted skbs to be freed. */
  742. skb_orphan(skb);
  743. nf_reset(skb);
  744. /* If running out of space, stop queue to avoid getting packets that we
  745. * are then unable to transmit.
  746. * An alternative would be to force queuing layer to requeue the skb by
  747. * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
  748. * returned in a normal path of operation: it means that driver is not
  749. * maintaining the TX queue stop/start state properly, and causes
  750. * the stack to do a non-trivial amount of useless work.
  751. * Since most packets only take 1 or 2 ring slots, stopping the queue
  752. * early means 16 slots are typically wasted.
  753. */
  754. if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
  755. netif_stop_subqueue(dev, qnum);
  756. if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  757. /* More just got used, free them then recheck. */
  758. free_old_xmit_skbs(sq);
  759. if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
  760. netif_start_subqueue(dev, qnum);
  761. virtqueue_disable_cb(sq->vq);
  762. }
  763. }
  764. }
  765. if (kick || netif_xmit_stopped(txq))
  766. virtqueue_kick(sq->vq);
  767. return NETDEV_TX_OK;
  768. }
  769. /*
  770. * Send command via the control virtqueue and check status. Commands
  771. * supported by the hypervisor, as indicated by feature bits, should
  772. * never fail unless improperly formatted.
  773. */
  774. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  775. struct scatterlist *out)
  776. {
  777. struct scatterlist *sgs[4], hdr, stat;
  778. unsigned out_num = 0, tmp;
  779. /* Caller should know better */
  780. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  781. vi->ctrl_status = ~0;
  782. vi->ctrl_hdr.class = class;
  783. vi->ctrl_hdr.cmd = cmd;
  784. /* Add header */
  785. sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
  786. sgs[out_num++] = &hdr;
  787. if (out)
  788. sgs[out_num++] = out;
  789. /* Add return status. */
  790. sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
  791. sgs[out_num] = &stat;
  792. BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
  793. virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
  794. if (unlikely(!virtqueue_kick(vi->cvq)))
  795. return vi->ctrl_status == VIRTIO_NET_OK;
  796. /* Spin for a response, the kick causes an ioport write, trapping
  797. * into the hypervisor, so the request should be handled immediately.
  798. */
  799. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  800. !virtqueue_is_broken(vi->cvq))
  801. cpu_relax();
  802. return vi->ctrl_status == VIRTIO_NET_OK;
  803. }
  804. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  805. {
  806. struct virtnet_info *vi = netdev_priv(dev);
  807. struct virtio_device *vdev = vi->vdev;
  808. int ret;
  809. struct sockaddr *addr;
  810. struct scatterlist sg;
  811. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  812. if (!addr)
  813. return -ENOMEM;
  814. memcpy(addr, p, sizeof(*addr));
  815. ret = eth_prepare_mac_addr_change(dev, addr);
  816. if (ret)
  817. goto out;
  818. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  819. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  820. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  821. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  822. dev_warn(&vdev->dev,
  823. "Failed to set mac address by vq command.\n");
  824. ret = -EINVAL;
  825. goto out;
  826. }
  827. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  828. !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  829. unsigned int i;
  830. /* Naturally, this has an atomicity problem. */
  831. for (i = 0; i < dev->addr_len; i++)
  832. virtio_cwrite8(vdev,
  833. offsetof(struct virtio_net_config, mac) +
  834. i, addr->sa_data[i]);
  835. }
  836. eth_commit_mac_addr_change(dev, p);
  837. ret = 0;
  838. out:
  839. kfree(addr);
  840. return ret;
  841. }
  842. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  843. struct rtnl_link_stats64 *tot)
  844. {
  845. struct virtnet_info *vi = netdev_priv(dev);
  846. int cpu;
  847. unsigned int start;
  848. for_each_possible_cpu(cpu) {
  849. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  850. u64 tpackets, tbytes, rpackets, rbytes;
  851. do {
  852. start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
  853. tpackets = stats->tx_packets;
  854. tbytes = stats->tx_bytes;
  855. } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
  856. do {
  857. start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
  858. rpackets = stats->rx_packets;
  859. rbytes = stats->rx_bytes;
  860. } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
  861. tot->rx_packets += rpackets;
  862. tot->tx_packets += tpackets;
  863. tot->rx_bytes += rbytes;
  864. tot->tx_bytes += tbytes;
  865. }
  866. tot->tx_dropped = dev->stats.tx_dropped;
  867. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  868. tot->rx_dropped = dev->stats.rx_dropped;
  869. tot->rx_length_errors = dev->stats.rx_length_errors;
  870. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  871. return tot;
  872. }
  873. #ifdef CONFIG_NET_POLL_CONTROLLER
  874. static void virtnet_netpoll(struct net_device *dev)
  875. {
  876. struct virtnet_info *vi = netdev_priv(dev);
  877. int i;
  878. for (i = 0; i < vi->curr_queue_pairs; i++)
  879. napi_schedule(&vi->rq[i].napi);
  880. }
  881. #endif
  882. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  883. {
  884. rtnl_lock();
  885. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  886. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  887. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  888. rtnl_unlock();
  889. }
  890. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  891. {
  892. struct scatterlist sg;
  893. struct net_device *dev = vi->dev;
  894. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  895. return 0;
  896. vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
  897. sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
  898. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  899. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  900. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  901. queue_pairs);
  902. return -EINVAL;
  903. } else {
  904. vi->curr_queue_pairs = queue_pairs;
  905. /* virtnet_open() will refill when device is going to up. */
  906. if (dev->flags & IFF_UP)
  907. schedule_delayed_work(&vi->refill, 0);
  908. }
  909. return 0;
  910. }
  911. static int virtnet_close(struct net_device *dev)
  912. {
  913. struct virtnet_info *vi = netdev_priv(dev);
  914. int i;
  915. /* Make sure refill_work doesn't re-enable napi! */
  916. cancel_delayed_work_sync(&vi->refill);
  917. for (i = 0; i < vi->max_queue_pairs; i++)
  918. napi_disable(&vi->rq[i].napi);
  919. return 0;
  920. }
  921. static void virtnet_set_rx_mode(struct net_device *dev)
  922. {
  923. struct virtnet_info *vi = netdev_priv(dev);
  924. struct scatterlist sg[2];
  925. struct virtio_net_ctrl_mac *mac_data;
  926. struct netdev_hw_addr *ha;
  927. int uc_count;
  928. int mc_count;
  929. void *buf;
  930. int i;
  931. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  932. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  933. return;
  934. vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
  935. vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  936. sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
  937. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  938. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  939. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  940. vi->ctrl_promisc ? "en" : "dis");
  941. sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
  942. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  943. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  944. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  945. vi->ctrl_allmulti ? "en" : "dis");
  946. uc_count = netdev_uc_count(dev);
  947. mc_count = netdev_mc_count(dev);
  948. /* MAC filter - use one buffer for both lists */
  949. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  950. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  951. mac_data = buf;
  952. if (!buf)
  953. return;
  954. sg_init_table(sg, 2);
  955. /* Store the unicast list and count in the front of the buffer */
  956. mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
  957. i = 0;
  958. netdev_for_each_uc_addr(ha, dev)
  959. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  960. sg_set_buf(&sg[0], mac_data,
  961. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  962. /* multicast list and count fill the end */
  963. mac_data = (void *)&mac_data->macs[uc_count][0];
  964. mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
  965. i = 0;
  966. netdev_for_each_mc_addr(ha, dev)
  967. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  968. sg_set_buf(&sg[1], mac_data,
  969. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  970. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  971. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  972. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  973. kfree(buf);
  974. }
  975. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  976. __be16 proto, u16 vid)
  977. {
  978. struct virtnet_info *vi = netdev_priv(dev);
  979. struct scatterlist sg;
  980. vi->ctrl_vid = vid;
  981. sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
  982. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  983. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  984. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  985. return 0;
  986. }
  987. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  988. __be16 proto, u16 vid)
  989. {
  990. struct virtnet_info *vi = netdev_priv(dev);
  991. struct scatterlist sg;
  992. vi->ctrl_vid = vid;
  993. sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
  994. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  995. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  996. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  997. return 0;
  998. }
  999. static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
  1000. {
  1001. int i;
  1002. if (vi->affinity_hint_set) {
  1003. for (i = 0; i < vi->max_queue_pairs; i++) {
  1004. virtqueue_set_affinity(vi->rq[i].vq, -1);
  1005. virtqueue_set_affinity(vi->sq[i].vq, -1);
  1006. }
  1007. vi->affinity_hint_set = false;
  1008. }
  1009. }
  1010. static void virtnet_set_affinity(struct virtnet_info *vi)
  1011. {
  1012. int i;
  1013. int cpu;
  1014. /* In multiqueue mode, when the number of cpu is equal to the number of
  1015. * queue pairs, we let the queue pairs to be private to one cpu by
  1016. * setting the affinity hint to eliminate the contention.
  1017. */
  1018. if (vi->curr_queue_pairs == 1 ||
  1019. vi->max_queue_pairs != num_online_cpus()) {
  1020. virtnet_clean_affinity(vi, -1);
  1021. return;
  1022. }
  1023. i = 0;
  1024. for_each_online_cpu(cpu) {
  1025. virtqueue_set_affinity(vi->rq[i].vq, cpu);
  1026. virtqueue_set_affinity(vi->sq[i].vq, cpu);
  1027. netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
  1028. i++;
  1029. }
  1030. vi->affinity_hint_set = true;
  1031. }
  1032. static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
  1033. {
  1034. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1035. node);
  1036. virtnet_set_affinity(vi);
  1037. return 0;
  1038. }
  1039. static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
  1040. {
  1041. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1042. node_dead);
  1043. virtnet_set_affinity(vi);
  1044. return 0;
  1045. }
  1046. static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
  1047. {
  1048. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1049. node);
  1050. virtnet_clean_affinity(vi, cpu);
  1051. return 0;
  1052. }
  1053. static enum cpuhp_state virtionet_online;
  1054. static int virtnet_cpu_notif_add(struct virtnet_info *vi)
  1055. {
  1056. int ret;
  1057. ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
  1058. if (ret)
  1059. return ret;
  1060. ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1061. &vi->node_dead);
  1062. if (!ret)
  1063. return ret;
  1064. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1065. return ret;
  1066. }
  1067. static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
  1068. {
  1069. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1070. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1071. &vi->node_dead);
  1072. }
  1073. static void virtnet_get_ringparam(struct net_device *dev,
  1074. struct ethtool_ringparam *ring)
  1075. {
  1076. struct virtnet_info *vi = netdev_priv(dev);
  1077. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  1078. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  1079. ring->rx_pending = ring->rx_max_pending;
  1080. ring->tx_pending = ring->tx_max_pending;
  1081. }
  1082. static void virtnet_get_drvinfo(struct net_device *dev,
  1083. struct ethtool_drvinfo *info)
  1084. {
  1085. struct virtnet_info *vi = netdev_priv(dev);
  1086. struct virtio_device *vdev = vi->vdev;
  1087. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1088. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  1089. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  1090. }
  1091. /* TODO: Eliminate OOO packets during switching */
  1092. static int virtnet_set_channels(struct net_device *dev,
  1093. struct ethtool_channels *channels)
  1094. {
  1095. struct virtnet_info *vi = netdev_priv(dev);
  1096. u16 queue_pairs = channels->combined_count;
  1097. int err;
  1098. /* We don't support separate rx/tx channels.
  1099. * We don't allow setting 'other' channels.
  1100. */
  1101. if (channels->rx_count || channels->tx_count || channels->other_count)
  1102. return -EINVAL;
  1103. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  1104. return -EINVAL;
  1105. get_online_cpus();
  1106. err = virtnet_set_queues(vi, queue_pairs);
  1107. if (!err) {
  1108. netif_set_real_num_tx_queues(dev, queue_pairs);
  1109. netif_set_real_num_rx_queues(dev, queue_pairs);
  1110. virtnet_set_affinity(vi);
  1111. }
  1112. put_online_cpus();
  1113. return err;
  1114. }
  1115. static void virtnet_get_channels(struct net_device *dev,
  1116. struct ethtool_channels *channels)
  1117. {
  1118. struct virtnet_info *vi = netdev_priv(dev);
  1119. channels->combined_count = vi->curr_queue_pairs;
  1120. channels->max_combined = vi->max_queue_pairs;
  1121. channels->max_other = 0;
  1122. channels->rx_count = 0;
  1123. channels->tx_count = 0;
  1124. channels->other_count = 0;
  1125. }
  1126. /* Check if the user is trying to change anything besides speed/duplex */
  1127. static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
  1128. {
  1129. struct ethtool_cmd diff1 = *cmd;
  1130. struct ethtool_cmd diff2 = {};
  1131. /* cmd is always set so we need to clear it, validate the port type
  1132. * and also without autonegotiation we can ignore advertising
  1133. */
  1134. ethtool_cmd_speed_set(&diff1, 0);
  1135. diff2.port = PORT_OTHER;
  1136. diff1.advertising = 0;
  1137. diff1.duplex = 0;
  1138. diff1.cmd = 0;
  1139. return !memcmp(&diff1, &diff2, sizeof(diff1));
  1140. }
  1141. static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1142. {
  1143. struct virtnet_info *vi = netdev_priv(dev);
  1144. u32 speed;
  1145. speed = ethtool_cmd_speed(cmd);
  1146. /* don't allow custom speed and duplex */
  1147. if (!ethtool_validate_speed(speed) ||
  1148. !ethtool_validate_duplex(cmd->duplex) ||
  1149. !virtnet_validate_ethtool_cmd(cmd))
  1150. return -EINVAL;
  1151. vi->speed = speed;
  1152. vi->duplex = cmd->duplex;
  1153. return 0;
  1154. }
  1155. static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1156. {
  1157. struct virtnet_info *vi = netdev_priv(dev);
  1158. ethtool_cmd_speed_set(cmd, vi->speed);
  1159. cmd->duplex = vi->duplex;
  1160. cmd->port = PORT_OTHER;
  1161. return 0;
  1162. }
  1163. static void virtnet_init_settings(struct net_device *dev)
  1164. {
  1165. struct virtnet_info *vi = netdev_priv(dev);
  1166. vi->speed = SPEED_UNKNOWN;
  1167. vi->duplex = DUPLEX_UNKNOWN;
  1168. }
  1169. static const struct ethtool_ops virtnet_ethtool_ops = {
  1170. .get_drvinfo = virtnet_get_drvinfo,
  1171. .get_link = ethtool_op_get_link,
  1172. .get_ringparam = virtnet_get_ringparam,
  1173. .set_channels = virtnet_set_channels,
  1174. .get_channels = virtnet_get_channels,
  1175. .get_ts_info = ethtool_op_get_ts_info,
  1176. .get_settings = virtnet_get_settings,
  1177. .set_settings = virtnet_set_settings,
  1178. };
  1179. #define MIN_MTU 68
  1180. #define MAX_MTU 65535
  1181. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  1182. {
  1183. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  1184. return -EINVAL;
  1185. dev->mtu = new_mtu;
  1186. return 0;
  1187. }
  1188. static const struct net_device_ops virtnet_netdev = {
  1189. .ndo_open = virtnet_open,
  1190. .ndo_stop = virtnet_close,
  1191. .ndo_start_xmit = start_xmit,
  1192. .ndo_validate_addr = eth_validate_addr,
  1193. .ndo_set_mac_address = virtnet_set_mac_address,
  1194. .ndo_set_rx_mode = virtnet_set_rx_mode,
  1195. .ndo_change_mtu = virtnet_change_mtu,
  1196. .ndo_get_stats64 = virtnet_stats,
  1197. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  1198. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  1199. #ifdef CONFIG_NET_POLL_CONTROLLER
  1200. .ndo_poll_controller = virtnet_netpoll,
  1201. #endif
  1202. #ifdef CONFIG_NET_RX_BUSY_POLL
  1203. .ndo_busy_poll = virtnet_busy_poll,
  1204. #endif
  1205. .ndo_features_check = passthru_features_check,
  1206. };
  1207. static void virtnet_config_changed_work(struct work_struct *work)
  1208. {
  1209. struct virtnet_info *vi =
  1210. container_of(work, struct virtnet_info, config_work);
  1211. u16 v;
  1212. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  1213. struct virtio_net_config, status, &v) < 0)
  1214. return;
  1215. if (v & VIRTIO_NET_S_ANNOUNCE) {
  1216. netdev_notify_peers(vi->dev);
  1217. virtnet_ack_link_announce(vi);
  1218. }
  1219. /* Ignore unknown (future) status bits */
  1220. v &= VIRTIO_NET_S_LINK_UP;
  1221. if (vi->status == v)
  1222. return;
  1223. vi->status = v;
  1224. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  1225. netif_carrier_on(vi->dev);
  1226. netif_tx_wake_all_queues(vi->dev);
  1227. } else {
  1228. netif_carrier_off(vi->dev);
  1229. netif_tx_stop_all_queues(vi->dev);
  1230. }
  1231. }
  1232. static void virtnet_config_changed(struct virtio_device *vdev)
  1233. {
  1234. struct virtnet_info *vi = vdev->priv;
  1235. schedule_work(&vi->config_work);
  1236. }
  1237. static void virtnet_free_queues(struct virtnet_info *vi)
  1238. {
  1239. int i;
  1240. for (i = 0; i < vi->max_queue_pairs; i++) {
  1241. napi_hash_del(&vi->rq[i].napi);
  1242. netif_napi_del(&vi->rq[i].napi);
  1243. }
  1244. /* We called napi_hash_del() before netif_napi_del(),
  1245. * we need to respect an RCU grace period before freeing vi->rq
  1246. */
  1247. synchronize_net();
  1248. kfree(vi->rq);
  1249. kfree(vi->sq);
  1250. }
  1251. static void free_receive_bufs(struct virtnet_info *vi)
  1252. {
  1253. int i;
  1254. for (i = 0; i < vi->max_queue_pairs; i++) {
  1255. while (vi->rq[i].pages)
  1256. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  1257. }
  1258. }
  1259. static void free_receive_page_frags(struct virtnet_info *vi)
  1260. {
  1261. int i;
  1262. for (i = 0; i < vi->max_queue_pairs; i++)
  1263. if (vi->rq[i].alloc_frag.page)
  1264. put_page(vi->rq[i].alloc_frag.page);
  1265. }
  1266. static void free_unused_bufs(struct virtnet_info *vi)
  1267. {
  1268. void *buf;
  1269. int i;
  1270. for (i = 0; i < vi->max_queue_pairs; i++) {
  1271. struct virtqueue *vq = vi->sq[i].vq;
  1272. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
  1273. dev_kfree_skb(buf);
  1274. }
  1275. for (i = 0; i < vi->max_queue_pairs; i++) {
  1276. struct virtqueue *vq = vi->rq[i].vq;
  1277. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1278. if (vi->mergeable_rx_bufs) {
  1279. unsigned long ctx = (unsigned long)buf;
  1280. void *base = mergeable_ctx_to_buf_address(ctx);
  1281. put_page(virt_to_head_page(base));
  1282. } else if (vi->big_packets) {
  1283. give_pages(&vi->rq[i], buf);
  1284. } else {
  1285. dev_kfree_skb(buf);
  1286. }
  1287. }
  1288. }
  1289. }
  1290. static void virtnet_del_vqs(struct virtnet_info *vi)
  1291. {
  1292. struct virtio_device *vdev = vi->vdev;
  1293. virtnet_clean_affinity(vi, -1);
  1294. vdev->config->del_vqs(vdev);
  1295. virtnet_free_queues(vi);
  1296. }
  1297. static int virtnet_find_vqs(struct virtnet_info *vi)
  1298. {
  1299. vq_callback_t **callbacks;
  1300. struct virtqueue **vqs;
  1301. int ret = -ENOMEM;
  1302. int i, total_vqs;
  1303. const char **names;
  1304. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  1305. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  1306. * possible control vq.
  1307. */
  1308. total_vqs = vi->max_queue_pairs * 2 +
  1309. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  1310. /* Allocate space for find_vqs parameters */
  1311. vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
  1312. if (!vqs)
  1313. goto err_vq;
  1314. callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
  1315. if (!callbacks)
  1316. goto err_callback;
  1317. names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
  1318. if (!names)
  1319. goto err_names;
  1320. /* Parameters for control virtqueue, if any */
  1321. if (vi->has_cvq) {
  1322. callbacks[total_vqs - 1] = NULL;
  1323. names[total_vqs - 1] = "control";
  1324. }
  1325. /* Allocate/initialize parameters for send/receive virtqueues */
  1326. for (i = 0; i < vi->max_queue_pairs; i++) {
  1327. callbacks[rxq2vq(i)] = skb_recv_done;
  1328. callbacks[txq2vq(i)] = skb_xmit_done;
  1329. sprintf(vi->rq[i].name, "input.%d", i);
  1330. sprintf(vi->sq[i].name, "output.%d", i);
  1331. names[rxq2vq(i)] = vi->rq[i].name;
  1332. names[txq2vq(i)] = vi->sq[i].name;
  1333. }
  1334. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  1335. names);
  1336. if (ret)
  1337. goto err_find;
  1338. if (vi->has_cvq) {
  1339. vi->cvq = vqs[total_vqs - 1];
  1340. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  1341. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1342. }
  1343. for (i = 0; i < vi->max_queue_pairs; i++) {
  1344. vi->rq[i].vq = vqs[rxq2vq(i)];
  1345. vi->sq[i].vq = vqs[txq2vq(i)];
  1346. }
  1347. kfree(names);
  1348. kfree(callbacks);
  1349. kfree(vqs);
  1350. return 0;
  1351. err_find:
  1352. kfree(names);
  1353. err_names:
  1354. kfree(callbacks);
  1355. err_callback:
  1356. kfree(vqs);
  1357. err_vq:
  1358. return ret;
  1359. }
  1360. static int virtnet_alloc_queues(struct virtnet_info *vi)
  1361. {
  1362. int i;
  1363. vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
  1364. if (!vi->sq)
  1365. goto err_sq;
  1366. vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
  1367. if (!vi->rq)
  1368. goto err_rq;
  1369. INIT_DELAYED_WORK(&vi->refill, refill_work);
  1370. for (i = 0; i < vi->max_queue_pairs; i++) {
  1371. vi->rq[i].pages = NULL;
  1372. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  1373. napi_weight);
  1374. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  1375. ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
  1376. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  1377. }
  1378. return 0;
  1379. err_rq:
  1380. kfree(vi->sq);
  1381. err_sq:
  1382. return -ENOMEM;
  1383. }
  1384. static int init_vqs(struct virtnet_info *vi)
  1385. {
  1386. int ret;
  1387. /* Allocate send & receive queues */
  1388. ret = virtnet_alloc_queues(vi);
  1389. if (ret)
  1390. goto err;
  1391. ret = virtnet_find_vqs(vi);
  1392. if (ret)
  1393. goto err_free;
  1394. get_online_cpus();
  1395. virtnet_set_affinity(vi);
  1396. put_online_cpus();
  1397. return 0;
  1398. err_free:
  1399. virtnet_free_queues(vi);
  1400. err:
  1401. return ret;
  1402. }
  1403. #ifdef CONFIG_SYSFS
  1404. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  1405. struct rx_queue_attribute *attribute, char *buf)
  1406. {
  1407. struct virtnet_info *vi = netdev_priv(queue->dev);
  1408. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  1409. struct ewma_pkt_len *avg;
  1410. BUG_ON(queue_index >= vi->max_queue_pairs);
  1411. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  1412. return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
  1413. }
  1414. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  1415. __ATTR_RO(mergeable_rx_buffer_size);
  1416. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  1417. &mergeable_rx_buffer_size_attribute.attr,
  1418. NULL
  1419. };
  1420. static const struct attribute_group virtio_net_mrg_rx_group = {
  1421. .name = "virtio_net",
  1422. .attrs = virtio_net_mrg_rx_attrs
  1423. };
  1424. #endif
  1425. static bool virtnet_fail_on_feature(struct virtio_device *vdev,
  1426. unsigned int fbit,
  1427. const char *fname, const char *dname)
  1428. {
  1429. if (!virtio_has_feature(vdev, fbit))
  1430. return false;
  1431. dev_err(&vdev->dev, "device advertises feature %s but not %s",
  1432. fname, dname);
  1433. return true;
  1434. }
  1435. #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
  1436. virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
  1437. static bool virtnet_validate_features(struct virtio_device *vdev)
  1438. {
  1439. if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
  1440. (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
  1441. "VIRTIO_NET_F_CTRL_VQ") ||
  1442. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
  1443. "VIRTIO_NET_F_CTRL_VQ") ||
  1444. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
  1445. "VIRTIO_NET_F_CTRL_VQ") ||
  1446. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
  1447. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
  1448. "VIRTIO_NET_F_CTRL_VQ"))) {
  1449. return false;
  1450. }
  1451. return true;
  1452. }
  1453. static int virtnet_probe(struct virtio_device *vdev)
  1454. {
  1455. int i, err;
  1456. struct net_device *dev;
  1457. struct virtnet_info *vi;
  1458. u16 max_queue_pairs;
  1459. int mtu;
  1460. if (!vdev->config->get) {
  1461. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  1462. __func__);
  1463. return -EINVAL;
  1464. }
  1465. if (!virtnet_validate_features(vdev))
  1466. return -EINVAL;
  1467. /* Find if host supports multiqueue virtio_net device */
  1468. err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
  1469. struct virtio_net_config,
  1470. max_virtqueue_pairs, &max_queue_pairs);
  1471. /* We need at least 2 queue's */
  1472. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  1473. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  1474. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1475. max_queue_pairs = 1;
  1476. /* Allocate ourselves a network device with room for our info */
  1477. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  1478. if (!dev)
  1479. return -ENOMEM;
  1480. /* Set up network device as normal. */
  1481. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1482. dev->netdev_ops = &virtnet_netdev;
  1483. dev->features = NETIF_F_HIGHDMA;
  1484. dev->ethtool_ops = &virtnet_ethtool_ops;
  1485. SET_NETDEV_DEV(dev, &vdev->dev);
  1486. /* Do we support "hardware" checksums? */
  1487. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  1488. /* This opens up the world of extra features. */
  1489. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  1490. if (csum)
  1491. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  1492. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  1493. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  1494. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  1495. }
  1496. /* Individual feature bits: what can host handle? */
  1497. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  1498. dev->hw_features |= NETIF_F_TSO;
  1499. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  1500. dev->hw_features |= NETIF_F_TSO6;
  1501. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  1502. dev->hw_features |= NETIF_F_TSO_ECN;
  1503. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  1504. dev->hw_features |= NETIF_F_UFO;
  1505. dev->features |= NETIF_F_GSO_ROBUST;
  1506. if (gso)
  1507. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  1508. /* (!csum && gso) case will be fixed by register_netdev() */
  1509. }
  1510. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  1511. dev->features |= NETIF_F_RXCSUM;
  1512. dev->vlan_features = dev->features;
  1513. /* Configuration may specify what MAC to use. Otherwise random. */
  1514. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  1515. virtio_cread_bytes(vdev,
  1516. offsetof(struct virtio_net_config, mac),
  1517. dev->dev_addr, dev->addr_len);
  1518. else
  1519. eth_hw_addr_random(dev);
  1520. /* Set up our device-specific information */
  1521. vi = netdev_priv(dev);
  1522. vi->dev = dev;
  1523. vi->vdev = vdev;
  1524. vdev->priv = vi;
  1525. vi->stats = alloc_percpu(struct virtnet_stats);
  1526. err = -ENOMEM;
  1527. if (vi->stats == NULL)
  1528. goto free;
  1529. for_each_possible_cpu(i) {
  1530. struct virtnet_stats *virtnet_stats;
  1531. virtnet_stats = per_cpu_ptr(vi->stats, i);
  1532. u64_stats_init(&virtnet_stats->tx_syncp);
  1533. u64_stats_init(&virtnet_stats->rx_syncp);
  1534. }
  1535. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  1536. /* If we can receive ANY GSO packets, we must allocate large ones. */
  1537. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  1538. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  1539. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
  1540. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
  1541. vi->big_packets = true;
  1542. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  1543. vi->mergeable_rx_bufs = true;
  1544. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
  1545. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  1546. vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  1547. else
  1548. vi->hdr_len = sizeof(struct virtio_net_hdr);
  1549. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
  1550. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  1551. vi->any_header_sg = true;
  1552. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  1553. vi->has_cvq = true;
  1554. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  1555. mtu = virtio_cread16(vdev,
  1556. offsetof(struct virtio_net_config,
  1557. mtu));
  1558. if (virtnet_change_mtu(dev, mtu))
  1559. __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
  1560. }
  1561. if (vi->any_header_sg)
  1562. dev->needed_headroom = vi->hdr_len;
  1563. /* Use single tx/rx queue pair as default */
  1564. vi->curr_queue_pairs = 1;
  1565. vi->max_queue_pairs = max_queue_pairs;
  1566. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  1567. err = init_vqs(vi);
  1568. if (err)
  1569. goto free_stats;
  1570. #ifdef CONFIG_SYSFS
  1571. if (vi->mergeable_rx_bufs)
  1572. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  1573. #endif
  1574. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  1575. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  1576. virtnet_init_settings(dev);
  1577. err = register_netdev(dev);
  1578. if (err) {
  1579. pr_debug("virtio_net: registering device failed\n");
  1580. goto free_vqs;
  1581. }
  1582. virtio_device_ready(vdev);
  1583. err = virtnet_cpu_notif_add(vi);
  1584. if (err) {
  1585. pr_debug("virtio_net: registering cpu notifier failed\n");
  1586. goto free_unregister_netdev;
  1587. }
  1588. /* Assume link up if device can't report link status,
  1589. otherwise get link status from config. */
  1590. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  1591. netif_carrier_off(dev);
  1592. schedule_work(&vi->config_work);
  1593. } else {
  1594. vi->status = VIRTIO_NET_S_LINK_UP;
  1595. netif_carrier_on(dev);
  1596. }
  1597. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  1598. dev->name, max_queue_pairs);
  1599. return 0;
  1600. free_unregister_netdev:
  1601. vi->vdev->config->reset(vdev);
  1602. unregister_netdev(dev);
  1603. free_vqs:
  1604. cancel_delayed_work_sync(&vi->refill);
  1605. free_receive_page_frags(vi);
  1606. virtnet_del_vqs(vi);
  1607. free_stats:
  1608. free_percpu(vi->stats);
  1609. free:
  1610. free_netdev(dev);
  1611. return err;
  1612. }
  1613. static void remove_vq_common(struct virtnet_info *vi)
  1614. {
  1615. vi->vdev->config->reset(vi->vdev);
  1616. /* Free unused buffers in both send and recv, if any. */
  1617. free_unused_bufs(vi);
  1618. free_receive_bufs(vi);
  1619. free_receive_page_frags(vi);
  1620. virtnet_del_vqs(vi);
  1621. }
  1622. static void virtnet_remove(struct virtio_device *vdev)
  1623. {
  1624. struct virtnet_info *vi = vdev->priv;
  1625. virtnet_cpu_notif_remove(vi);
  1626. /* Make sure no work handler is accessing the device. */
  1627. flush_work(&vi->config_work);
  1628. unregister_netdev(vi->dev);
  1629. remove_vq_common(vi);
  1630. free_percpu(vi->stats);
  1631. free_netdev(vi->dev);
  1632. }
  1633. #ifdef CONFIG_PM_SLEEP
  1634. static int virtnet_freeze(struct virtio_device *vdev)
  1635. {
  1636. struct virtnet_info *vi = vdev->priv;
  1637. int i;
  1638. virtnet_cpu_notif_remove(vi);
  1639. /* Make sure no work handler is accessing the device */
  1640. flush_work(&vi->config_work);
  1641. netif_device_detach(vi->dev);
  1642. cancel_delayed_work_sync(&vi->refill);
  1643. if (netif_running(vi->dev)) {
  1644. for (i = 0; i < vi->max_queue_pairs; i++)
  1645. napi_disable(&vi->rq[i].napi);
  1646. }
  1647. remove_vq_common(vi);
  1648. return 0;
  1649. }
  1650. static int virtnet_restore(struct virtio_device *vdev)
  1651. {
  1652. struct virtnet_info *vi = vdev->priv;
  1653. int err, i;
  1654. err = init_vqs(vi);
  1655. if (err)
  1656. return err;
  1657. virtio_device_ready(vdev);
  1658. if (netif_running(vi->dev)) {
  1659. for (i = 0; i < vi->curr_queue_pairs; i++)
  1660. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  1661. schedule_delayed_work(&vi->refill, 0);
  1662. for (i = 0; i < vi->max_queue_pairs; i++)
  1663. virtnet_napi_enable(&vi->rq[i]);
  1664. }
  1665. netif_device_attach(vi->dev);
  1666. rtnl_lock();
  1667. virtnet_set_queues(vi, vi->curr_queue_pairs);
  1668. rtnl_unlock();
  1669. err = virtnet_cpu_notif_add(vi);
  1670. if (err)
  1671. return err;
  1672. return 0;
  1673. }
  1674. #endif
  1675. static struct virtio_device_id id_table[] = {
  1676. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1677. { 0 },
  1678. };
  1679. #define VIRTNET_FEATURES \
  1680. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
  1681. VIRTIO_NET_F_MAC, \
  1682. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
  1683. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
  1684. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
  1685. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
  1686. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
  1687. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
  1688. VIRTIO_NET_F_CTRL_MAC_ADDR, \
  1689. VIRTIO_NET_F_MTU
  1690. static unsigned int features[] = {
  1691. VIRTNET_FEATURES,
  1692. };
  1693. static unsigned int features_legacy[] = {
  1694. VIRTNET_FEATURES,
  1695. VIRTIO_NET_F_GSO,
  1696. VIRTIO_F_ANY_LAYOUT,
  1697. };
  1698. static struct virtio_driver virtio_net_driver = {
  1699. .feature_table = features,
  1700. .feature_table_size = ARRAY_SIZE(features),
  1701. .feature_table_legacy = features_legacy,
  1702. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  1703. .driver.name = KBUILD_MODNAME,
  1704. .driver.owner = THIS_MODULE,
  1705. .id_table = id_table,
  1706. .probe = virtnet_probe,
  1707. .remove = virtnet_remove,
  1708. .config_changed = virtnet_config_changed,
  1709. #ifdef CONFIG_PM_SLEEP
  1710. .freeze = virtnet_freeze,
  1711. .restore = virtnet_restore,
  1712. #endif
  1713. };
  1714. static __init int virtio_net_driver_init(void)
  1715. {
  1716. int ret;
  1717. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "AP_VIRT_NET_ONLINE",
  1718. virtnet_cpu_online,
  1719. virtnet_cpu_down_prep);
  1720. if (ret < 0)
  1721. goto out;
  1722. virtionet_online = ret;
  1723. ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "VIRT_NET_DEAD",
  1724. NULL, virtnet_cpu_dead);
  1725. if (ret)
  1726. goto err_dead;
  1727. ret = register_virtio_driver(&virtio_net_driver);
  1728. if (ret)
  1729. goto err_virtio;
  1730. return 0;
  1731. err_virtio:
  1732. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  1733. err_dead:
  1734. cpuhp_remove_multi_state(virtionet_online);
  1735. out:
  1736. return ret;
  1737. }
  1738. module_init(virtio_net_driver_init);
  1739. static __exit void virtio_net_driver_exit(void)
  1740. {
  1741. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  1742. cpuhp_remove_multi_state(virtionet_online);
  1743. unregister_virtio_driver(&virtio_net_driver);
  1744. }
  1745. module_exit(virtio_net_driver_exit);
  1746. MODULE_DEVICE_TABLE(virtio, id_table);
  1747. MODULE_DESCRIPTION("Virtio network driver");
  1748. MODULE_LICENSE("GPL");