virtio_net.c 52 KB

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