virtio_net.c 46 KB

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