virtio_net.c 53 KB

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