virtio_net.c 54 KB

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