virtio_net.c 46 KB

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