virtio_net.c 52 KB

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