virtio_net.c 46 KB

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