virtio_net.c 46 KB

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