virtio_net.c 54 KB

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