virtio_net.c 51 KB

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