virtio_net.c 53 KB

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