virtio_net.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  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/bpf.h>
  26. #include <linux/bpf_trace.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/slab.h>
  30. #include <linux/cpu.h>
  31. #include <linux/average.h>
  32. #include <net/route.h>
  33. static int napi_weight = NAPI_POLL_WEIGHT;
  34. module_param(napi_weight, int, 0444);
  35. static bool csum = true, gso = true, napi_tx;
  36. module_param(csum, bool, 0444);
  37. module_param(gso, bool, 0444);
  38. module_param(napi_tx, bool, 0644);
  39. /* FIXME: MTU in config. */
  40. #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  41. #define GOOD_COPY_LEN 128
  42. #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  43. /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
  44. #define VIRTIO_XDP_HEADROOM 256
  45. /* RX packet size EWMA. The average packet size is used to determine the packet
  46. * buffer size when refilling RX rings. As the entire RX ring may be refilled
  47. * at once, the weight is chosen so that the EWMA will be insensitive to short-
  48. * term, transient changes in packet size.
  49. */
  50. DECLARE_EWMA(pkt_len, 0, 64)
  51. #define VIRTNET_DRIVER_VERSION "1.0.0"
  52. static const unsigned long guest_offloads[] = {
  53. VIRTIO_NET_F_GUEST_TSO4,
  54. VIRTIO_NET_F_GUEST_TSO6,
  55. VIRTIO_NET_F_GUEST_ECN,
  56. VIRTIO_NET_F_GUEST_UFO
  57. };
  58. struct virtnet_stats {
  59. struct u64_stats_sync tx_syncp;
  60. struct u64_stats_sync rx_syncp;
  61. u64 tx_bytes;
  62. u64 tx_packets;
  63. u64 rx_bytes;
  64. u64 rx_packets;
  65. };
  66. /* Internal representation of a send virtqueue */
  67. struct send_queue {
  68. /* Virtqueue associated with this send _queue */
  69. struct virtqueue *vq;
  70. /* TX: fragments + linear part + virtio header */
  71. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  72. /* Name of the send queue: output.$index */
  73. char name[40];
  74. struct napi_struct napi;
  75. };
  76. /* Internal representation of a receive virtqueue */
  77. struct receive_queue {
  78. /* Virtqueue associated with this receive_queue */
  79. struct virtqueue *vq;
  80. struct napi_struct napi;
  81. struct bpf_prog __rcu *xdp_prog;
  82. /* Chain pages by the private ptr. */
  83. struct page *pages;
  84. /* Average packet length for mergeable receive buffers. */
  85. struct ewma_pkt_len mrg_avg_pkt_len;
  86. /* Page frag for packet buffer allocation. */
  87. struct page_frag alloc_frag;
  88. /* RX: fragments + linear part + virtio header */
  89. struct scatterlist sg[MAX_SKB_FRAGS + 2];
  90. /* Min single buffer size for mergeable buffers case. */
  91. unsigned int min_buf_len;
  92. /* Name of this receive queue: input.$index */
  93. char name[40];
  94. };
  95. struct virtnet_info {
  96. struct virtio_device *vdev;
  97. struct virtqueue *cvq;
  98. struct net_device *dev;
  99. struct send_queue *sq;
  100. struct receive_queue *rq;
  101. unsigned int status;
  102. /* Max # of queue pairs supported by the device */
  103. u16 max_queue_pairs;
  104. /* # of queue pairs currently used by the driver */
  105. u16 curr_queue_pairs;
  106. /* # of XDP queue pairs currently used by the driver */
  107. u16 xdp_queue_pairs;
  108. /* I like... big packets and I cannot lie! */
  109. bool big_packets;
  110. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  111. bool mergeable_rx_bufs;
  112. /* Has control virtqueue */
  113. bool has_cvq;
  114. /* Host can handle any s/g split between our header and packet data */
  115. bool any_header_sg;
  116. /* Packet virtio header size */
  117. u8 hdr_len;
  118. /* Active statistics */
  119. struct virtnet_stats __percpu *stats;
  120. /* Work struct for refilling if we run low on memory. */
  121. struct delayed_work refill;
  122. /* Work struct for config space updates */
  123. struct work_struct config_work;
  124. /* Does the affinity hint is set for virtqueues? */
  125. bool affinity_hint_set;
  126. /* CPU hotplug instances for online & dead */
  127. struct hlist_node node;
  128. struct hlist_node node_dead;
  129. /* Control VQ buffers: protected by the rtnl lock */
  130. struct virtio_net_ctrl_hdr ctrl_hdr;
  131. virtio_net_ctrl_ack ctrl_status;
  132. struct virtio_net_ctrl_mq ctrl_mq;
  133. u8 ctrl_promisc;
  134. u8 ctrl_allmulti;
  135. u16 ctrl_vid;
  136. u64 ctrl_offloads;
  137. /* Ethtool settings */
  138. u8 duplex;
  139. u32 speed;
  140. unsigned long guest_offloads;
  141. };
  142. struct padded_vnet_hdr {
  143. struct virtio_net_hdr_mrg_rxbuf hdr;
  144. /*
  145. * hdr is in a separate sg buffer, and data sg buffer shares same page
  146. * with this header sg. This padding makes next sg 16 byte aligned
  147. * after the header.
  148. */
  149. char padding[4];
  150. };
  151. /* Converting between virtqueue no. and kernel tx/rx queue no.
  152. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  153. */
  154. static int vq2txq(struct virtqueue *vq)
  155. {
  156. return (vq->index - 1) / 2;
  157. }
  158. static int txq2vq(int txq)
  159. {
  160. return txq * 2 + 1;
  161. }
  162. static int vq2rxq(struct virtqueue *vq)
  163. {
  164. return vq->index / 2;
  165. }
  166. static int rxq2vq(int rxq)
  167. {
  168. return rxq * 2;
  169. }
  170. static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
  171. {
  172. return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
  173. }
  174. /*
  175. * private is used to chain pages for big packets, put the whole
  176. * most recent used list in the beginning for reuse
  177. */
  178. static void give_pages(struct receive_queue *rq, struct page *page)
  179. {
  180. struct page *end;
  181. /* Find end of list, sew whole thing into vi->rq.pages. */
  182. for (end = page; end->private; end = (struct page *)end->private);
  183. end->private = (unsigned long)rq->pages;
  184. rq->pages = page;
  185. }
  186. static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  187. {
  188. struct page *p = rq->pages;
  189. if (p) {
  190. rq->pages = (struct page *)p->private;
  191. /* clear private here, it is used to chain pages */
  192. p->private = 0;
  193. } else
  194. p = alloc_page(gfp_mask);
  195. return p;
  196. }
  197. static void virtqueue_napi_schedule(struct napi_struct *napi,
  198. struct virtqueue *vq)
  199. {
  200. if (napi_schedule_prep(napi)) {
  201. virtqueue_disable_cb(vq);
  202. __napi_schedule(napi);
  203. }
  204. }
  205. static void virtqueue_napi_complete(struct napi_struct *napi,
  206. struct virtqueue *vq, int processed)
  207. {
  208. int opaque;
  209. opaque = virtqueue_enable_cb_prepare(vq);
  210. if (napi_complete_done(napi, processed) &&
  211. unlikely(virtqueue_poll(vq, opaque)))
  212. virtqueue_napi_schedule(napi, vq);
  213. }
  214. static void skb_xmit_done(struct virtqueue *vq)
  215. {
  216. struct virtnet_info *vi = vq->vdev->priv;
  217. struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
  218. /* Suppress further interrupts. */
  219. virtqueue_disable_cb(vq);
  220. if (napi->weight)
  221. virtqueue_napi_schedule(napi, vq);
  222. else
  223. /* We were probably waiting for more output buffers. */
  224. netif_wake_subqueue(vi->dev, vq2txq(vq));
  225. }
  226. #define MRG_CTX_HEADER_SHIFT 22
  227. static void *mergeable_len_to_ctx(unsigned int truesize,
  228. unsigned int headroom)
  229. {
  230. return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
  231. }
  232. static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
  233. {
  234. return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
  235. }
  236. static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
  237. {
  238. return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
  239. }
  240. /* Called from bottom half context */
  241. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  242. struct receive_queue *rq,
  243. struct page *page, unsigned int offset,
  244. unsigned int len, unsigned int truesize)
  245. {
  246. struct sk_buff *skb;
  247. struct virtio_net_hdr_mrg_rxbuf *hdr;
  248. unsigned int copy, hdr_len, hdr_padded_len;
  249. char *p;
  250. p = page_address(page) + offset;
  251. /* copy small packet so we can reuse these pages for small data */
  252. skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
  253. if (unlikely(!skb))
  254. return NULL;
  255. hdr = skb_vnet_hdr(skb);
  256. hdr_len = vi->hdr_len;
  257. if (vi->mergeable_rx_bufs)
  258. hdr_padded_len = sizeof(*hdr);
  259. else
  260. hdr_padded_len = sizeof(struct padded_vnet_hdr);
  261. memcpy(hdr, p, hdr_len);
  262. len -= hdr_len;
  263. offset += hdr_padded_len;
  264. p += hdr_padded_len;
  265. copy = len;
  266. if (copy > skb_tailroom(skb))
  267. copy = skb_tailroom(skb);
  268. skb_put_data(skb, p, copy);
  269. len -= copy;
  270. offset += copy;
  271. if (vi->mergeable_rx_bufs) {
  272. if (len)
  273. skb_add_rx_frag(skb, 0, page, offset, len, truesize);
  274. else
  275. put_page(page);
  276. return skb;
  277. }
  278. /*
  279. * Verify that we can indeed put this data into a skb.
  280. * This is here to handle cases when the device erroneously
  281. * tries to receive more than is possible. This is usually
  282. * the case of a broken device.
  283. */
  284. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  285. net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
  286. dev_kfree_skb(skb);
  287. return NULL;
  288. }
  289. BUG_ON(offset >= PAGE_SIZE);
  290. while (len) {
  291. unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
  292. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
  293. frag_size, truesize);
  294. len -= frag_size;
  295. page = (struct page *)page->private;
  296. offset = 0;
  297. }
  298. if (page)
  299. give_pages(rq, page);
  300. return skb;
  301. }
  302. static bool virtnet_xdp_xmit(struct virtnet_info *vi,
  303. struct receive_queue *rq,
  304. struct xdp_buff *xdp)
  305. {
  306. struct virtio_net_hdr_mrg_rxbuf *hdr;
  307. unsigned int len;
  308. struct send_queue *sq;
  309. unsigned int qp;
  310. void *xdp_sent;
  311. int err;
  312. qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
  313. sq = &vi->sq[qp];
  314. /* Free up any pending old buffers before queueing new ones. */
  315. while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  316. struct page *sent_page = virt_to_head_page(xdp_sent);
  317. put_page(sent_page);
  318. }
  319. xdp->data -= vi->hdr_len;
  320. /* Zero header and leave csum up to XDP layers */
  321. hdr = xdp->data;
  322. memset(hdr, 0, vi->hdr_len);
  323. sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
  324. err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
  325. if (unlikely(err)) {
  326. struct page *page = virt_to_head_page(xdp->data);
  327. put_page(page);
  328. return false;
  329. }
  330. virtqueue_kick(sq->vq);
  331. return true;
  332. }
  333. static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
  334. {
  335. return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
  336. }
  337. /* We copy the packet for XDP in the following cases:
  338. *
  339. * 1) Packet is scattered across multiple rx buffers.
  340. * 2) Headroom space is insufficient.
  341. *
  342. * This is inefficient but it's a temporary condition that
  343. * we hit right after XDP is enabled and until queue is refilled
  344. * with large buffers with sufficient headroom - so it should affect
  345. * at most queue size packets.
  346. * Afterwards, the conditions to enable
  347. * XDP should preclude the underlying device from sending packets
  348. * across multiple buffers (num_buf > 1), and we make sure buffers
  349. * have enough headroom.
  350. */
  351. static struct page *xdp_linearize_page(struct receive_queue *rq,
  352. u16 *num_buf,
  353. struct page *p,
  354. int offset,
  355. int page_off,
  356. unsigned int *len)
  357. {
  358. struct page *page = alloc_page(GFP_ATOMIC);
  359. if (!page)
  360. return NULL;
  361. memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
  362. page_off += *len;
  363. while (--*num_buf) {
  364. unsigned int buflen;
  365. void *buf;
  366. int off;
  367. buf = virtqueue_get_buf(rq->vq, &buflen);
  368. if (unlikely(!buf))
  369. goto err_buf;
  370. p = virt_to_head_page(buf);
  371. off = buf - page_address(p);
  372. /* guard against a misconfigured or uncooperative backend that
  373. * is sending packet larger than the MTU.
  374. */
  375. if ((page_off + buflen) > PAGE_SIZE) {
  376. put_page(p);
  377. goto err_buf;
  378. }
  379. memcpy(page_address(page) + page_off,
  380. page_address(p) + off, buflen);
  381. page_off += buflen;
  382. put_page(p);
  383. }
  384. /* Headroom does not contribute to packet length */
  385. *len = page_off - VIRTIO_XDP_HEADROOM;
  386. return page;
  387. err_buf:
  388. __free_pages(page, 0);
  389. return NULL;
  390. }
  391. static struct sk_buff *receive_small(struct net_device *dev,
  392. struct virtnet_info *vi,
  393. struct receive_queue *rq,
  394. void *buf, void *ctx,
  395. unsigned int len)
  396. {
  397. struct sk_buff *skb;
  398. struct bpf_prog *xdp_prog;
  399. unsigned int xdp_headroom = (unsigned long)ctx;
  400. unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
  401. unsigned int headroom = vi->hdr_len + header_offset;
  402. unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  403. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  404. struct page *page = virt_to_head_page(buf);
  405. unsigned int delta = 0;
  406. struct page *xdp_page;
  407. len -= vi->hdr_len;
  408. rcu_read_lock();
  409. xdp_prog = rcu_dereference(rq->xdp_prog);
  410. if (xdp_prog) {
  411. struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
  412. struct xdp_buff xdp;
  413. void *orig_data;
  414. u32 act;
  415. if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
  416. goto err_xdp;
  417. if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
  418. int offset = buf - page_address(page) + header_offset;
  419. unsigned int tlen = len + vi->hdr_len;
  420. u16 num_buf = 1;
  421. xdp_headroom = virtnet_get_headroom(vi);
  422. header_offset = VIRTNET_RX_PAD + xdp_headroom;
  423. headroom = vi->hdr_len + header_offset;
  424. buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
  425. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  426. xdp_page = xdp_linearize_page(rq, &num_buf, page,
  427. offset, header_offset,
  428. &tlen);
  429. if (!xdp_page)
  430. goto err_xdp;
  431. buf = page_address(xdp_page);
  432. put_page(page);
  433. page = xdp_page;
  434. }
  435. xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
  436. xdp.data = xdp.data_hard_start + xdp_headroom;
  437. xdp.data_end = xdp.data + len;
  438. orig_data = xdp.data;
  439. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  440. switch (act) {
  441. case XDP_PASS:
  442. /* Recalculate length in case bpf program changed it */
  443. delta = orig_data - xdp.data;
  444. break;
  445. case XDP_TX:
  446. if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
  447. trace_xdp_exception(vi->dev, xdp_prog, act);
  448. rcu_read_unlock();
  449. goto xdp_xmit;
  450. default:
  451. bpf_warn_invalid_xdp_action(act);
  452. case XDP_ABORTED:
  453. trace_xdp_exception(vi->dev, xdp_prog, act);
  454. case XDP_DROP:
  455. goto err_xdp;
  456. }
  457. }
  458. rcu_read_unlock();
  459. skb = build_skb(buf, buflen);
  460. if (!skb) {
  461. put_page(page);
  462. goto err;
  463. }
  464. skb_reserve(skb, headroom - delta);
  465. skb_put(skb, len + delta);
  466. if (!delta) {
  467. buf += header_offset;
  468. memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
  469. } /* keep zeroed vnet hdr since packet was changed by bpf */
  470. err:
  471. return skb;
  472. err_xdp:
  473. rcu_read_unlock();
  474. dev->stats.rx_dropped++;
  475. put_page(page);
  476. xdp_xmit:
  477. return NULL;
  478. }
  479. static struct sk_buff *receive_big(struct net_device *dev,
  480. struct virtnet_info *vi,
  481. struct receive_queue *rq,
  482. void *buf,
  483. unsigned int len)
  484. {
  485. struct page *page = buf;
  486. struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
  487. if (unlikely(!skb))
  488. goto err;
  489. return skb;
  490. err:
  491. dev->stats.rx_dropped++;
  492. give_pages(rq, page);
  493. return NULL;
  494. }
  495. static struct sk_buff *receive_mergeable(struct net_device *dev,
  496. struct virtnet_info *vi,
  497. struct receive_queue *rq,
  498. void *buf,
  499. void *ctx,
  500. unsigned int len)
  501. {
  502. struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
  503. u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
  504. struct page *page = virt_to_head_page(buf);
  505. int offset = buf - page_address(page);
  506. struct sk_buff *head_skb, *curr_skb;
  507. struct bpf_prog *xdp_prog;
  508. unsigned int truesize;
  509. unsigned int headroom = mergeable_ctx_to_headroom(ctx);
  510. head_skb = NULL;
  511. rcu_read_lock();
  512. xdp_prog = rcu_dereference(rq->xdp_prog);
  513. if (xdp_prog) {
  514. struct page *xdp_page;
  515. struct xdp_buff xdp;
  516. void *data;
  517. u32 act;
  518. /* This happens when rx buffer size is underestimated */
  519. if (unlikely(num_buf > 1 ||
  520. headroom < virtnet_get_headroom(vi))) {
  521. /* linearize data for XDP */
  522. xdp_page = xdp_linearize_page(rq, &num_buf,
  523. page, offset,
  524. VIRTIO_XDP_HEADROOM,
  525. &len);
  526. if (!xdp_page)
  527. goto err_xdp;
  528. offset = VIRTIO_XDP_HEADROOM;
  529. } else {
  530. xdp_page = page;
  531. }
  532. /* Transient failure which in theory could occur if
  533. * in-flight packets from before XDP was enabled reach
  534. * the receive path after XDP is loaded. In practice I
  535. * was not able to create this condition.
  536. */
  537. if (unlikely(hdr->hdr.gso_type))
  538. goto err_xdp;
  539. /* Allow consuming headroom but reserve enough space to push
  540. * the descriptor on if we get an XDP_TX return code.
  541. */
  542. data = page_address(xdp_page) + offset;
  543. xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
  544. xdp.data = data + vi->hdr_len;
  545. xdp.data_end = xdp.data + (len - vi->hdr_len);
  546. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  547. switch (act) {
  548. case XDP_PASS:
  549. /* recalculate offset to account for any header
  550. * adjustments. Note other cases do not build an
  551. * skb and avoid using offset
  552. */
  553. offset = xdp.data -
  554. page_address(xdp_page) - vi->hdr_len;
  555. /* We can only create skb based on xdp_page. */
  556. if (unlikely(xdp_page != page)) {
  557. rcu_read_unlock();
  558. put_page(page);
  559. head_skb = page_to_skb(vi, rq, xdp_page,
  560. offset, len, PAGE_SIZE);
  561. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
  562. return head_skb;
  563. }
  564. break;
  565. case XDP_TX:
  566. if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
  567. trace_xdp_exception(vi->dev, xdp_prog, act);
  568. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
  569. if (unlikely(xdp_page != page))
  570. goto err_xdp;
  571. rcu_read_unlock();
  572. goto xdp_xmit;
  573. default:
  574. bpf_warn_invalid_xdp_action(act);
  575. case XDP_ABORTED:
  576. trace_xdp_exception(vi->dev, xdp_prog, act);
  577. case XDP_DROP:
  578. if (unlikely(xdp_page != page))
  579. __free_pages(xdp_page, 0);
  580. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
  581. goto err_xdp;
  582. }
  583. }
  584. rcu_read_unlock();
  585. truesize = mergeable_ctx_to_truesize(ctx);
  586. if (unlikely(len > truesize)) {
  587. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  588. dev->name, len, (unsigned long)ctx);
  589. dev->stats.rx_length_errors++;
  590. goto err_skb;
  591. }
  592. head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
  593. curr_skb = head_skb;
  594. if (unlikely(!curr_skb))
  595. goto err_skb;
  596. while (--num_buf) {
  597. int num_skb_frags;
  598. buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
  599. if (unlikely(!ctx)) {
  600. pr_debug("%s: rx error: %d buffers out of %d missing\n",
  601. dev->name, num_buf,
  602. virtio16_to_cpu(vi->vdev,
  603. hdr->num_buffers));
  604. dev->stats.rx_length_errors++;
  605. goto err_buf;
  606. }
  607. page = virt_to_head_page(buf);
  608. truesize = mergeable_ctx_to_truesize(ctx);
  609. if (unlikely(len > truesize)) {
  610. pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
  611. dev->name, len, (unsigned long)ctx);
  612. dev->stats.rx_length_errors++;
  613. goto err_skb;
  614. }
  615. num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
  616. if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
  617. struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
  618. if (unlikely(!nskb))
  619. goto err_skb;
  620. if (curr_skb == head_skb)
  621. skb_shinfo(curr_skb)->frag_list = nskb;
  622. else
  623. curr_skb->next = nskb;
  624. curr_skb = nskb;
  625. head_skb->truesize += nskb->truesize;
  626. num_skb_frags = 0;
  627. }
  628. if (curr_skb != head_skb) {
  629. head_skb->data_len += len;
  630. head_skb->len += len;
  631. head_skb->truesize += truesize;
  632. }
  633. offset = buf - page_address(page);
  634. if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
  635. put_page(page);
  636. skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  637. len, truesize);
  638. } else {
  639. skb_add_rx_frag(curr_skb, num_skb_frags, page,
  640. offset, len, truesize);
  641. }
  642. }
  643. ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
  644. return head_skb;
  645. err_xdp:
  646. rcu_read_unlock();
  647. err_skb:
  648. put_page(page);
  649. while (--num_buf) {
  650. buf = virtqueue_get_buf(rq->vq, &len);
  651. if (unlikely(!buf)) {
  652. pr_debug("%s: rx error: %d buffers missing\n",
  653. dev->name, num_buf);
  654. dev->stats.rx_length_errors++;
  655. break;
  656. }
  657. page = virt_to_head_page(buf);
  658. put_page(page);
  659. }
  660. err_buf:
  661. dev->stats.rx_dropped++;
  662. dev_kfree_skb(head_skb);
  663. xdp_xmit:
  664. return NULL;
  665. }
  666. static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  667. void *buf, unsigned int len, void **ctx)
  668. {
  669. struct net_device *dev = vi->dev;
  670. struct sk_buff *skb;
  671. struct virtio_net_hdr_mrg_rxbuf *hdr;
  672. int ret;
  673. if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
  674. pr_debug("%s: short packet %i\n", dev->name, len);
  675. dev->stats.rx_length_errors++;
  676. if (vi->mergeable_rx_bufs) {
  677. put_page(virt_to_head_page(buf));
  678. } else if (vi->big_packets) {
  679. give_pages(rq, buf);
  680. } else {
  681. put_page(virt_to_head_page(buf));
  682. }
  683. return 0;
  684. }
  685. if (vi->mergeable_rx_bufs)
  686. skb = receive_mergeable(dev, vi, rq, buf, ctx, len);
  687. else if (vi->big_packets)
  688. skb = receive_big(dev, vi, rq, buf, len);
  689. else
  690. skb = receive_small(dev, vi, rq, buf, ctx, len);
  691. if (unlikely(!skb))
  692. return 0;
  693. hdr = skb_vnet_hdr(skb);
  694. ret = skb->len;
  695. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
  696. skb->ip_summed = CHECKSUM_UNNECESSARY;
  697. if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
  698. virtio_is_little_endian(vi->vdev))) {
  699. net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
  700. dev->name, hdr->hdr.gso_type,
  701. hdr->hdr.gso_size);
  702. goto frame_err;
  703. }
  704. skb->protocol = eth_type_trans(skb, dev);
  705. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  706. ntohs(skb->protocol), skb->len, skb->pkt_type);
  707. napi_gro_receive(&rq->napi, skb);
  708. return ret;
  709. frame_err:
  710. dev->stats.rx_frame_errors++;
  711. dev_kfree_skb(skb);
  712. return 0;
  713. }
  714. /* Unlike mergeable buffers, all buffers are allocated to the
  715. * same size, except for the headroom. For this reason we do
  716. * not need to use mergeable_len_to_ctx here - it is enough
  717. * to store the headroom as the context ignoring the truesize.
  718. */
  719. static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
  720. gfp_t gfp)
  721. {
  722. struct page_frag *alloc_frag = &rq->alloc_frag;
  723. char *buf;
  724. unsigned int xdp_headroom = virtnet_get_headroom(vi);
  725. void *ctx = (void *)(unsigned long)xdp_headroom;
  726. int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
  727. int err;
  728. len = SKB_DATA_ALIGN(len) +
  729. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  730. if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
  731. return -ENOMEM;
  732. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  733. get_page(alloc_frag->page);
  734. alloc_frag->offset += len;
  735. sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
  736. vi->hdr_len + GOOD_PACKET_LEN);
  737. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  738. if (err < 0)
  739. put_page(virt_to_head_page(buf));
  740. return err;
  741. }
  742. static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  743. gfp_t gfp)
  744. {
  745. struct page *first, *list = NULL;
  746. char *p;
  747. int i, err, offset;
  748. sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
  749. /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
  750. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  751. first = get_a_page(rq, gfp);
  752. if (!first) {
  753. if (list)
  754. give_pages(rq, list);
  755. return -ENOMEM;
  756. }
  757. sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
  758. /* chain new page in list head to match sg */
  759. first->private = (unsigned long)list;
  760. list = first;
  761. }
  762. first = get_a_page(rq, gfp);
  763. if (!first) {
  764. give_pages(rq, list);
  765. return -ENOMEM;
  766. }
  767. p = page_address(first);
  768. /* rq->sg[0], rq->sg[1] share the same page */
  769. /* a separated rq->sg[0] for header - required in case !any_header_sg */
  770. sg_set_buf(&rq->sg[0], p, vi->hdr_len);
  771. /* rq->sg[1] for data packet, from offset */
  772. offset = sizeof(struct padded_vnet_hdr);
  773. sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
  774. /* chain first in list head */
  775. first->private = (unsigned long)list;
  776. err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
  777. first, gfp);
  778. if (err < 0)
  779. give_pages(rq, first);
  780. return err;
  781. }
  782. static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
  783. struct ewma_pkt_len *avg_pkt_len)
  784. {
  785. const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  786. unsigned int len;
  787. len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
  788. rq->min_buf_len, PAGE_SIZE - hdr_len);
  789. return ALIGN(len, L1_CACHE_BYTES);
  790. }
  791. static int add_recvbuf_mergeable(struct virtnet_info *vi,
  792. struct receive_queue *rq, gfp_t gfp)
  793. {
  794. struct page_frag *alloc_frag = &rq->alloc_frag;
  795. unsigned int headroom = virtnet_get_headroom(vi);
  796. char *buf;
  797. void *ctx;
  798. int err;
  799. unsigned int len, hole;
  800. len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len);
  801. if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
  802. return -ENOMEM;
  803. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  804. buf += headroom; /* advance address leaving hole at front of pkt */
  805. get_page(alloc_frag->page);
  806. alloc_frag->offset += len + headroom;
  807. hole = alloc_frag->size - alloc_frag->offset;
  808. if (hole < len + headroom) {
  809. /* To avoid internal fragmentation, if there is very likely not
  810. * enough space for another buffer, add the remaining space to
  811. * the current buffer.
  812. */
  813. len += hole;
  814. alloc_frag->offset += hole;
  815. }
  816. sg_init_one(rq->sg, buf, len);
  817. ctx = mergeable_len_to_ctx(len, headroom);
  818. err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
  819. if (err < 0)
  820. put_page(virt_to_head_page(buf));
  821. return err;
  822. }
  823. /*
  824. * Returns false if we couldn't fill entirely (OOM).
  825. *
  826. * Normally run in the receive path, but can also be run from ndo_open
  827. * before we're receiving packets, or from refill_work which is
  828. * careful to disable receiving (using napi_disable).
  829. */
  830. static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
  831. gfp_t gfp)
  832. {
  833. int err;
  834. bool oom;
  835. gfp |= __GFP_COLD;
  836. do {
  837. if (vi->mergeable_rx_bufs)
  838. err = add_recvbuf_mergeable(vi, rq, gfp);
  839. else if (vi->big_packets)
  840. err = add_recvbuf_big(vi, rq, gfp);
  841. else
  842. err = add_recvbuf_small(vi, rq, gfp);
  843. oom = err == -ENOMEM;
  844. if (err)
  845. break;
  846. } while (rq->vq->num_free);
  847. virtqueue_kick(rq->vq);
  848. return !oom;
  849. }
  850. static void skb_recv_done(struct virtqueue *rvq)
  851. {
  852. struct virtnet_info *vi = rvq->vdev->priv;
  853. struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
  854. virtqueue_napi_schedule(&rq->napi, rvq);
  855. }
  856. static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
  857. {
  858. napi_enable(napi);
  859. /* If all buffers were filled by other side before we napi_enabled, we
  860. * won't get another interrupt, so process any outstanding packets now.
  861. * Call local_bh_enable after to trigger softIRQ processing.
  862. */
  863. local_bh_disable();
  864. virtqueue_napi_schedule(napi, vq);
  865. local_bh_enable();
  866. }
  867. static void virtnet_napi_tx_enable(struct virtnet_info *vi,
  868. struct virtqueue *vq,
  869. struct napi_struct *napi)
  870. {
  871. if (!napi->weight)
  872. return;
  873. /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
  874. * enable the feature if this is likely affine with the transmit path.
  875. */
  876. if (!vi->affinity_hint_set) {
  877. napi->weight = 0;
  878. return;
  879. }
  880. return virtnet_napi_enable(vq, napi);
  881. }
  882. static void virtnet_napi_tx_disable(struct napi_struct *napi)
  883. {
  884. if (napi->weight)
  885. napi_disable(napi);
  886. }
  887. static void refill_work(struct work_struct *work)
  888. {
  889. struct virtnet_info *vi =
  890. container_of(work, struct virtnet_info, refill.work);
  891. bool still_empty;
  892. int i;
  893. for (i = 0; i < vi->curr_queue_pairs; i++) {
  894. struct receive_queue *rq = &vi->rq[i];
  895. napi_disable(&rq->napi);
  896. still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
  897. virtnet_napi_enable(rq->vq, &rq->napi);
  898. /* In theory, this can happen: if we don't get any buffers in
  899. * we will *never* try to fill again.
  900. */
  901. if (still_empty)
  902. schedule_delayed_work(&vi->refill, HZ/2);
  903. }
  904. }
  905. static int virtnet_receive(struct receive_queue *rq, int budget)
  906. {
  907. struct virtnet_info *vi = rq->vq->vdev->priv;
  908. unsigned int len, received = 0, bytes = 0;
  909. void *buf;
  910. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  911. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  912. void *ctx;
  913. while (received < budget &&
  914. (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
  915. bytes += receive_buf(vi, rq, buf, len, ctx);
  916. received++;
  917. }
  918. } else {
  919. while (received < budget &&
  920. (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
  921. bytes += receive_buf(vi, rq, buf, len, NULL);
  922. received++;
  923. }
  924. }
  925. if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
  926. if (!try_fill_recv(vi, rq, GFP_ATOMIC))
  927. schedule_delayed_work(&vi->refill, 0);
  928. }
  929. u64_stats_update_begin(&stats->rx_syncp);
  930. stats->rx_bytes += bytes;
  931. stats->rx_packets += received;
  932. u64_stats_update_end(&stats->rx_syncp);
  933. return received;
  934. }
  935. static void free_old_xmit_skbs(struct send_queue *sq)
  936. {
  937. struct sk_buff *skb;
  938. unsigned int len;
  939. struct virtnet_info *vi = sq->vq->vdev->priv;
  940. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  941. unsigned int packets = 0;
  942. unsigned int bytes = 0;
  943. while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
  944. pr_debug("Sent skb %p\n", skb);
  945. bytes += skb->len;
  946. packets++;
  947. dev_consume_skb_any(skb);
  948. }
  949. /* Avoid overhead when no packets have been processed
  950. * happens when called speculatively from start_xmit.
  951. */
  952. if (!packets)
  953. return;
  954. u64_stats_update_begin(&stats->tx_syncp);
  955. stats->tx_bytes += bytes;
  956. stats->tx_packets += packets;
  957. u64_stats_update_end(&stats->tx_syncp);
  958. }
  959. static void virtnet_poll_cleantx(struct receive_queue *rq)
  960. {
  961. struct virtnet_info *vi = rq->vq->vdev->priv;
  962. unsigned int index = vq2rxq(rq->vq);
  963. struct send_queue *sq = &vi->sq[index];
  964. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
  965. if (!sq->napi.weight)
  966. return;
  967. if (__netif_tx_trylock(txq)) {
  968. free_old_xmit_skbs(sq);
  969. __netif_tx_unlock(txq);
  970. }
  971. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  972. netif_tx_wake_queue(txq);
  973. }
  974. static int virtnet_poll(struct napi_struct *napi, int budget)
  975. {
  976. struct receive_queue *rq =
  977. container_of(napi, struct receive_queue, napi);
  978. unsigned int received;
  979. virtnet_poll_cleantx(rq);
  980. received = virtnet_receive(rq, budget);
  981. /* Out of packets? */
  982. if (received < budget)
  983. virtqueue_napi_complete(napi, rq->vq, received);
  984. return received;
  985. }
  986. static int virtnet_open(struct net_device *dev)
  987. {
  988. struct virtnet_info *vi = netdev_priv(dev);
  989. int i;
  990. for (i = 0; i < vi->max_queue_pairs; i++) {
  991. if (i < vi->curr_queue_pairs)
  992. /* Make sure we have some buffers: if oom use wq. */
  993. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  994. schedule_delayed_work(&vi->refill, 0);
  995. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  996. virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
  997. }
  998. return 0;
  999. }
  1000. static int virtnet_poll_tx(struct napi_struct *napi, int budget)
  1001. {
  1002. struct send_queue *sq = container_of(napi, struct send_queue, napi);
  1003. struct virtnet_info *vi = sq->vq->vdev->priv;
  1004. struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
  1005. __netif_tx_lock(txq, raw_smp_processor_id());
  1006. free_old_xmit_skbs(sq);
  1007. __netif_tx_unlock(txq);
  1008. virtqueue_napi_complete(napi, sq->vq, 0);
  1009. if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
  1010. netif_tx_wake_queue(txq);
  1011. return 0;
  1012. }
  1013. static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
  1014. {
  1015. struct virtio_net_hdr_mrg_rxbuf *hdr;
  1016. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  1017. struct virtnet_info *vi = sq->vq->vdev->priv;
  1018. int num_sg;
  1019. unsigned hdr_len = vi->hdr_len;
  1020. bool can_push;
  1021. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  1022. can_push = vi->any_header_sg &&
  1023. !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
  1024. !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
  1025. /* Even if we can, don't push here yet as this would skew
  1026. * csum_start offset below. */
  1027. if (can_push)
  1028. hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
  1029. else
  1030. hdr = skb_vnet_hdr(skb);
  1031. if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
  1032. virtio_is_little_endian(vi->vdev), false))
  1033. BUG();
  1034. if (vi->mergeable_rx_bufs)
  1035. hdr->num_buffers = 0;
  1036. sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
  1037. if (can_push) {
  1038. __skb_push(skb, hdr_len);
  1039. num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
  1040. if (unlikely(num_sg < 0))
  1041. return num_sg;
  1042. /* Pull header back to avoid skew in tx bytes calculations. */
  1043. __skb_pull(skb, hdr_len);
  1044. } else {
  1045. sg_set_buf(sq->sg, hdr, hdr_len);
  1046. num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
  1047. if (unlikely(num_sg < 0))
  1048. return num_sg;
  1049. num_sg++;
  1050. }
  1051. return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
  1052. }
  1053. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  1054. {
  1055. struct virtnet_info *vi = netdev_priv(dev);
  1056. int qnum = skb_get_queue_mapping(skb);
  1057. struct send_queue *sq = &vi->sq[qnum];
  1058. int err;
  1059. struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
  1060. bool kick = !skb->xmit_more;
  1061. bool use_napi = sq->napi.weight;
  1062. /* Free up any pending old buffers before queueing new ones. */
  1063. free_old_xmit_skbs(sq);
  1064. if (use_napi && kick)
  1065. virtqueue_enable_cb_delayed(sq->vq);
  1066. /* timestamp packet in software */
  1067. skb_tx_timestamp(skb);
  1068. /* Try to transmit */
  1069. err = xmit_skb(sq, skb);
  1070. /* This should not happen! */
  1071. if (unlikely(err)) {
  1072. dev->stats.tx_fifo_errors++;
  1073. if (net_ratelimit())
  1074. dev_warn(&dev->dev,
  1075. "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
  1076. dev->stats.tx_dropped++;
  1077. dev_kfree_skb_any(skb);
  1078. return NETDEV_TX_OK;
  1079. }
  1080. /* Don't wait up for transmitted skbs to be freed. */
  1081. if (!use_napi) {
  1082. skb_orphan(skb);
  1083. nf_reset(skb);
  1084. }
  1085. /* If running out of space, stop queue to avoid getting packets that we
  1086. * are then unable to transmit.
  1087. * An alternative would be to force queuing layer to requeue the skb by
  1088. * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
  1089. * returned in a normal path of operation: it means that driver is not
  1090. * maintaining the TX queue stop/start state properly, and causes
  1091. * the stack to do a non-trivial amount of useless work.
  1092. * Since most packets only take 1 or 2 ring slots, stopping the queue
  1093. * early means 16 slots are typically wasted.
  1094. */
  1095. if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
  1096. netif_stop_subqueue(dev, qnum);
  1097. if (!use_napi &&
  1098. unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
  1099. /* More just got used, free them then recheck. */
  1100. free_old_xmit_skbs(sq);
  1101. if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
  1102. netif_start_subqueue(dev, qnum);
  1103. virtqueue_disable_cb(sq->vq);
  1104. }
  1105. }
  1106. }
  1107. if (kick || netif_xmit_stopped(txq))
  1108. virtqueue_kick(sq->vq);
  1109. return NETDEV_TX_OK;
  1110. }
  1111. /*
  1112. * Send command via the control virtqueue and check status. Commands
  1113. * supported by the hypervisor, as indicated by feature bits, should
  1114. * never fail unless improperly formatted.
  1115. */
  1116. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  1117. struct scatterlist *out)
  1118. {
  1119. struct scatterlist *sgs[4], hdr, stat;
  1120. unsigned out_num = 0, tmp;
  1121. /* Caller should know better */
  1122. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
  1123. vi->ctrl_status = ~0;
  1124. vi->ctrl_hdr.class = class;
  1125. vi->ctrl_hdr.cmd = cmd;
  1126. /* Add header */
  1127. sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
  1128. sgs[out_num++] = &hdr;
  1129. if (out)
  1130. sgs[out_num++] = out;
  1131. /* Add return status. */
  1132. sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
  1133. sgs[out_num] = &stat;
  1134. BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
  1135. virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
  1136. if (unlikely(!virtqueue_kick(vi->cvq)))
  1137. return vi->ctrl_status == VIRTIO_NET_OK;
  1138. /* Spin for a response, the kick causes an ioport write, trapping
  1139. * into the hypervisor, so the request should be handled immediately.
  1140. */
  1141. while (!virtqueue_get_buf(vi->cvq, &tmp) &&
  1142. !virtqueue_is_broken(vi->cvq))
  1143. cpu_relax();
  1144. return vi->ctrl_status == VIRTIO_NET_OK;
  1145. }
  1146. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  1147. {
  1148. struct virtnet_info *vi = netdev_priv(dev);
  1149. struct virtio_device *vdev = vi->vdev;
  1150. int ret;
  1151. struct sockaddr *addr;
  1152. struct scatterlist sg;
  1153. addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
  1154. if (!addr)
  1155. return -ENOMEM;
  1156. ret = eth_prepare_mac_addr_change(dev, addr);
  1157. if (ret)
  1158. goto out;
  1159. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
  1160. sg_init_one(&sg, addr->sa_data, dev->addr_len);
  1161. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1162. VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
  1163. dev_warn(&vdev->dev,
  1164. "Failed to set mac address by vq command.\n");
  1165. ret = -EINVAL;
  1166. goto out;
  1167. }
  1168. } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
  1169. !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1170. unsigned int i;
  1171. /* Naturally, this has an atomicity problem. */
  1172. for (i = 0; i < dev->addr_len; i++)
  1173. virtio_cwrite8(vdev,
  1174. offsetof(struct virtio_net_config, mac) +
  1175. i, addr->sa_data[i]);
  1176. }
  1177. eth_commit_mac_addr_change(dev, p);
  1178. ret = 0;
  1179. out:
  1180. kfree(addr);
  1181. return ret;
  1182. }
  1183. static void virtnet_stats(struct net_device *dev,
  1184. struct rtnl_link_stats64 *tot)
  1185. {
  1186. struct virtnet_info *vi = netdev_priv(dev);
  1187. int cpu;
  1188. unsigned int start;
  1189. for_each_possible_cpu(cpu) {
  1190. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  1191. u64 tpackets, tbytes, rpackets, rbytes;
  1192. do {
  1193. start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
  1194. tpackets = stats->tx_packets;
  1195. tbytes = stats->tx_bytes;
  1196. } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
  1197. do {
  1198. start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
  1199. rpackets = stats->rx_packets;
  1200. rbytes = stats->rx_bytes;
  1201. } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
  1202. tot->rx_packets += rpackets;
  1203. tot->tx_packets += tpackets;
  1204. tot->rx_bytes += rbytes;
  1205. tot->tx_bytes += tbytes;
  1206. }
  1207. tot->tx_dropped = dev->stats.tx_dropped;
  1208. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  1209. tot->rx_dropped = dev->stats.rx_dropped;
  1210. tot->rx_length_errors = dev->stats.rx_length_errors;
  1211. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  1212. }
  1213. #ifdef CONFIG_NET_POLL_CONTROLLER
  1214. static void virtnet_netpoll(struct net_device *dev)
  1215. {
  1216. struct virtnet_info *vi = netdev_priv(dev);
  1217. int i;
  1218. for (i = 0; i < vi->curr_queue_pairs; i++)
  1219. napi_schedule(&vi->rq[i].napi);
  1220. }
  1221. #endif
  1222. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  1223. {
  1224. rtnl_lock();
  1225. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  1226. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
  1227. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  1228. rtnl_unlock();
  1229. }
  1230. static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1231. {
  1232. struct scatterlist sg;
  1233. struct net_device *dev = vi->dev;
  1234. if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
  1235. return 0;
  1236. vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
  1237. sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
  1238. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
  1239. VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
  1240. dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
  1241. queue_pairs);
  1242. return -EINVAL;
  1243. } else {
  1244. vi->curr_queue_pairs = queue_pairs;
  1245. /* virtnet_open() will refill when device is going to up. */
  1246. if (dev->flags & IFF_UP)
  1247. schedule_delayed_work(&vi->refill, 0);
  1248. }
  1249. return 0;
  1250. }
  1251. static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
  1252. {
  1253. int err;
  1254. rtnl_lock();
  1255. err = _virtnet_set_queues(vi, queue_pairs);
  1256. rtnl_unlock();
  1257. return err;
  1258. }
  1259. static int virtnet_close(struct net_device *dev)
  1260. {
  1261. struct virtnet_info *vi = netdev_priv(dev);
  1262. int i;
  1263. /* Make sure refill_work doesn't re-enable napi! */
  1264. cancel_delayed_work_sync(&vi->refill);
  1265. for (i = 0; i < vi->max_queue_pairs; i++) {
  1266. napi_disable(&vi->rq[i].napi);
  1267. virtnet_napi_tx_disable(&vi->sq[i].napi);
  1268. }
  1269. return 0;
  1270. }
  1271. static void virtnet_set_rx_mode(struct net_device *dev)
  1272. {
  1273. struct virtnet_info *vi = netdev_priv(dev);
  1274. struct scatterlist sg[2];
  1275. struct virtio_net_ctrl_mac *mac_data;
  1276. struct netdev_hw_addr *ha;
  1277. int uc_count;
  1278. int mc_count;
  1279. void *buf;
  1280. int i;
  1281. /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
  1282. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  1283. return;
  1284. vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
  1285. vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  1286. sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
  1287. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1288. VIRTIO_NET_CTRL_RX_PROMISC, sg))
  1289. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  1290. vi->ctrl_promisc ? "en" : "dis");
  1291. sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
  1292. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  1293. VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
  1294. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  1295. vi->ctrl_allmulti ? "en" : "dis");
  1296. uc_count = netdev_uc_count(dev);
  1297. mc_count = netdev_mc_count(dev);
  1298. /* MAC filter - use one buffer for both lists */
  1299. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  1300. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  1301. mac_data = buf;
  1302. if (!buf)
  1303. return;
  1304. sg_init_table(sg, 2);
  1305. /* Store the unicast list and count in the front of the buffer */
  1306. mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
  1307. i = 0;
  1308. netdev_for_each_uc_addr(ha, dev)
  1309. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1310. sg_set_buf(&sg[0], mac_data,
  1311. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  1312. /* multicast list and count fill the end */
  1313. mac_data = (void *)&mac_data->macs[uc_count][0];
  1314. mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
  1315. i = 0;
  1316. netdev_for_each_mc_addr(ha, dev)
  1317. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  1318. sg_set_buf(&sg[1], mac_data,
  1319. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  1320. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  1321. VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  1322. dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
  1323. kfree(buf);
  1324. }
  1325. static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  1326. __be16 proto, u16 vid)
  1327. {
  1328. struct virtnet_info *vi = netdev_priv(dev);
  1329. struct scatterlist sg;
  1330. vi->ctrl_vid = vid;
  1331. sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
  1332. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1333. VIRTIO_NET_CTRL_VLAN_ADD, &sg))
  1334. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  1335. return 0;
  1336. }
  1337. static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
  1338. __be16 proto, u16 vid)
  1339. {
  1340. struct virtnet_info *vi = netdev_priv(dev);
  1341. struct scatterlist sg;
  1342. vi->ctrl_vid = vid;
  1343. sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
  1344. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  1345. VIRTIO_NET_CTRL_VLAN_DEL, &sg))
  1346. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  1347. return 0;
  1348. }
  1349. static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
  1350. {
  1351. int i;
  1352. if (vi->affinity_hint_set) {
  1353. for (i = 0; i < vi->max_queue_pairs; i++) {
  1354. virtqueue_set_affinity(vi->rq[i].vq, -1);
  1355. virtqueue_set_affinity(vi->sq[i].vq, -1);
  1356. }
  1357. vi->affinity_hint_set = false;
  1358. }
  1359. }
  1360. static void virtnet_set_affinity(struct virtnet_info *vi)
  1361. {
  1362. int i;
  1363. int cpu;
  1364. /* In multiqueue mode, when the number of cpu is equal to the number of
  1365. * queue pairs, we let the queue pairs to be private to one cpu by
  1366. * setting the affinity hint to eliminate the contention.
  1367. */
  1368. if (vi->curr_queue_pairs == 1 ||
  1369. vi->max_queue_pairs != num_online_cpus()) {
  1370. virtnet_clean_affinity(vi, -1);
  1371. return;
  1372. }
  1373. i = 0;
  1374. for_each_online_cpu(cpu) {
  1375. virtqueue_set_affinity(vi->rq[i].vq, cpu);
  1376. virtqueue_set_affinity(vi->sq[i].vq, cpu);
  1377. netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
  1378. i++;
  1379. }
  1380. vi->affinity_hint_set = true;
  1381. }
  1382. static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
  1383. {
  1384. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1385. node);
  1386. virtnet_set_affinity(vi);
  1387. return 0;
  1388. }
  1389. static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
  1390. {
  1391. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1392. node_dead);
  1393. virtnet_set_affinity(vi);
  1394. return 0;
  1395. }
  1396. static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
  1397. {
  1398. struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
  1399. node);
  1400. virtnet_clean_affinity(vi, cpu);
  1401. return 0;
  1402. }
  1403. static enum cpuhp_state virtionet_online;
  1404. static int virtnet_cpu_notif_add(struct virtnet_info *vi)
  1405. {
  1406. int ret;
  1407. ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
  1408. if (ret)
  1409. return ret;
  1410. ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1411. &vi->node_dead);
  1412. if (!ret)
  1413. return ret;
  1414. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1415. return ret;
  1416. }
  1417. static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
  1418. {
  1419. cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
  1420. cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
  1421. &vi->node_dead);
  1422. }
  1423. static void virtnet_get_ringparam(struct net_device *dev,
  1424. struct ethtool_ringparam *ring)
  1425. {
  1426. struct virtnet_info *vi = netdev_priv(dev);
  1427. ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
  1428. ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
  1429. ring->rx_pending = ring->rx_max_pending;
  1430. ring->tx_pending = ring->tx_max_pending;
  1431. }
  1432. static void virtnet_get_drvinfo(struct net_device *dev,
  1433. struct ethtool_drvinfo *info)
  1434. {
  1435. struct virtnet_info *vi = netdev_priv(dev);
  1436. struct virtio_device *vdev = vi->vdev;
  1437. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1438. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  1439. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  1440. }
  1441. /* TODO: Eliminate OOO packets during switching */
  1442. static int virtnet_set_channels(struct net_device *dev,
  1443. struct ethtool_channels *channels)
  1444. {
  1445. struct virtnet_info *vi = netdev_priv(dev);
  1446. u16 queue_pairs = channels->combined_count;
  1447. int err;
  1448. /* We don't support separate rx/tx channels.
  1449. * We don't allow setting 'other' channels.
  1450. */
  1451. if (channels->rx_count || channels->tx_count || channels->other_count)
  1452. return -EINVAL;
  1453. if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
  1454. return -EINVAL;
  1455. /* For now we don't support modifying channels while XDP is loaded
  1456. * also when XDP is loaded all RX queues have XDP programs so we only
  1457. * need to check a single RX queue.
  1458. */
  1459. if (vi->rq[0].xdp_prog)
  1460. return -EINVAL;
  1461. get_online_cpus();
  1462. err = _virtnet_set_queues(vi, queue_pairs);
  1463. if (!err) {
  1464. netif_set_real_num_tx_queues(dev, queue_pairs);
  1465. netif_set_real_num_rx_queues(dev, queue_pairs);
  1466. virtnet_set_affinity(vi);
  1467. }
  1468. put_online_cpus();
  1469. return err;
  1470. }
  1471. static void virtnet_get_channels(struct net_device *dev,
  1472. struct ethtool_channels *channels)
  1473. {
  1474. struct virtnet_info *vi = netdev_priv(dev);
  1475. channels->combined_count = vi->curr_queue_pairs;
  1476. channels->max_combined = vi->max_queue_pairs;
  1477. channels->max_other = 0;
  1478. channels->rx_count = 0;
  1479. channels->tx_count = 0;
  1480. channels->other_count = 0;
  1481. }
  1482. /* Check if the user is trying to change anything besides speed/duplex */
  1483. static bool
  1484. virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
  1485. {
  1486. struct ethtool_link_ksettings diff1 = *cmd;
  1487. struct ethtool_link_ksettings diff2 = {};
  1488. /* cmd is always set so we need to clear it, validate the port type
  1489. * and also without autonegotiation we can ignore advertising
  1490. */
  1491. diff1.base.speed = 0;
  1492. diff2.base.port = PORT_OTHER;
  1493. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  1494. diff1.base.duplex = 0;
  1495. diff1.base.cmd = 0;
  1496. diff1.base.link_mode_masks_nwords = 0;
  1497. return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
  1498. bitmap_empty(diff1.link_modes.supported,
  1499. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  1500. bitmap_empty(diff1.link_modes.advertising,
  1501. __ETHTOOL_LINK_MODE_MASK_NBITS) &&
  1502. bitmap_empty(diff1.link_modes.lp_advertising,
  1503. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1504. }
  1505. static int virtnet_set_link_ksettings(struct net_device *dev,
  1506. const struct ethtool_link_ksettings *cmd)
  1507. {
  1508. struct virtnet_info *vi = netdev_priv(dev);
  1509. u32 speed;
  1510. speed = cmd->base.speed;
  1511. /* don't allow custom speed and duplex */
  1512. if (!ethtool_validate_speed(speed) ||
  1513. !ethtool_validate_duplex(cmd->base.duplex) ||
  1514. !virtnet_validate_ethtool_cmd(cmd))
  1515. return -EINVAL;
  1516. vi->speed = speed;
  1517. vi->duplex = cmd->base.duplex;
  1518. return 0;
  1519. }
  1520. static int virtnet_get_link_ksettings(struct net_device *dev,
  1521. struct ethtool_link_ksettings *cmd)
  1522. {
  1523. struct virtnet_info *vi = netdev_priv(dev);
  1524. cmd->base.speed = vi->speed;
  1525. cmd->base.duplex = vi->duplex;
  1526. cmd->base.port = PORT_OTHER;
  1527. return 0;
  1528. }
  1529. static void virtnet_init_settings(struct net_device *dev)
  1530. {
  1531. struct virtnet_info *vi = netdev_priv(dev);
  1532. vi->speed = SPEED_UNKNOWN;
  1533. vi->duplex = DUPLEX_UNKNOWN;
  1534. }
  1535. static const struct ethtool_ops virtnet_ethtool_ops = {
  1536. .get_drvinfo = virtnet_get_drvinfo,
  1537. .get_link = ethtool_op_get_link,
  1538. .get_ringparam = virtnet_get_ringparam,
  1539. .set_channels = virtnet_set_channels,
  1540. .get_channels = virtnet_get_channels,
  1541. .get_ts_info = ethtool_op_get_ts_info,
  1542. .get_link_ksettings = virtnet_get_link_ksettings,
  1543. .set_link_ksettings = virtnet_set_link_ksettings,
  1544. };
  1545. static void virtnet_freeze_down(struct virtio_device *vdev)
  1546. {
  1547. struct virtnet_info *vi = vdev->priv;
  1548. int i;
  1549. /* Make sure no work handler is accessing the device */
  1550. flush_work(&vi->config_work);
  1551. netif_device_detach(vi->dev);
  1552. netif_tx_disable(vi->dev);
  1553. cancel_delayed_work_sync(&vi->refill);
  1554. if (netif_running(vi->dev)) {
  1555. for (i = 0; i < vi->max_queue_pairs; i++) {
  1556. napi_disable(&vi->rq[i].napi);
  1557. virtnet_napi_tx_disable(&vi->sq[i].napi);
  1558. }
  1559. }
  1560. }
  1561. static int init_vqs(struct virtnet_info *vi);
  1562. static int virtnet_restore_up(struct virtio_device *vdev)
  1563. {
  1564. struct virtnet_info *vi = vdev->priv;
  1565. int err, i;
  1566. err = init_vqs(vi);
  1567. if (err)
  1568. return err;
  1569. virtio_device_ready(vdev);
  1570. if (netif_running(vi->dev)) {
  1571. for (i = 0; i < vi->curr_queue_pairs; i++)
  1572. if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
  1573. schedule_delayed_work(&vi->refill, 0);
  1574. for (i = 0; i < vi->max_queue_pairs; i++) {
  1575. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1576. virtnet_napi_tx_enable(vi, vi->sq[i].vq,
  1577. &vi->sq[i].napi);
  1578. }
  1579. }
  1580. netif_device_attach(vi->dev);
  1581. return err;
  1582. }
  1583. static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
  1584. {
  1585. struct scatterlist sg;
  1586. vi->ctrl_offloads = cpu_to_virtio64(vi->vdev, offloads);
  1587. sg_init_one(&sg, &vi->ctrl_offloads, sizeof(vi->ctrl_offloads));
  1588. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
  1589. VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
  1590. dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
  1591. return -EINVAL;
  1592. }
  1593. return 0;
  1594. }
  1595. static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
  1596. {
  1597. u64 offloads = 0;
  1598. if (!vi->guest_offloads)
  1599. return 0;
  1600. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
  1601. offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM;
  1602. return virtnet_set_guest_offloads(vi, offloads);
  1603. }
  1604. static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
  1605. {
  1606. u64 offloads = vi->guest_offloads;
  1607. if (!vi->guest_offloads)
  1608. return 0;
  1609. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
  1610. offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM;
  1611. return virtnet_set_guest_offloads(vi, offloads);
  1612. }
  1613. static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  1614. struct netlink_ext_ack *extack)
  1615. {
  1616. unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
  1617. struct virtnet_info *vi = netdev_priv(dev);
  1618. struct bpf_prog *old_prog;
  1619. u16 xdp_qp = 0, curr_qp;
  1620. int i, err;
  1621. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
  1622. && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  1623. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  1624. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
  1625. virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) {
  1626. NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first");
  1627. return -EOPNOTSUPP;
  1628. }
  1629. if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
  1630. NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
  1631. return -EINVAL;
  1632. }
  1633. if (dev->mtu > max_sz) {
  1634. NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
  1635. netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
  1636. return -EINVAL;
  1637. }
  1638. curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
  1639. if (prog)
  1640. xdp_qp = nr_cpu_ids;
  1641. /* XDP requires extra queues for XDP_TX */
  1642. if (curr_qp + xdp_qp > vi->max_queue_pairs) {
  1643. NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
  1644. netdev_warn(dev, "request %i queues but max is %i\n",
  1645. curr_qp + xdp_qp, vi->max_queue_pairs);
  1646. return -ENOMEM;
  1647. }
  1648. if (prog) {
  1649. prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
  1650. if (IS_ERR(prog))
  1651. return PTR_ERR(prog);
  1652. }
  1653. /* Make sure NAPI is not using any XDP TX queues for RX. */
  1654. for (i = 0; i < vi->max_queue_pairs; i++)
  1655. napi_disable(&vi->rq[i].napi);
  1656. netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
  1657. err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
  1658. if (err)
  1659. goto err;
  1660. vi->xdp_queue_pairs = xdp_qp;
  1661. for (i = 0; i < vi->max_queue_pairs; i++) {
  1662. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  1663. rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
  1664. if (i == 0) {
  1665. if (!old_prog)
  1666. virtnet_clear_guest_offloads(vi);
  1667. if (!prog)
  1668. virtnet_restore_guest_offloads(vi);
  1669. }
  1670. if (old_prog)
  1671. bpf_prog_put(old_prog);
  1672. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1673. }
  1674. return 0;
  1675. err:
  1676. for (i = 0; i < vi->max_queue_pairs; i++)
  1677. virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
  1678. if (prog)
  1679. bpf_prog_sub(prog, vi->max_queue_pairs - 1);
  1680. return err;
  1681. }
  1682. static u32 virtnet_xdp_query(struct net_device *dev)
  1683. {
  1684. struct virtnet_info *vi = netdev_priv(dev);
  1685. const struct bpf_prog *xdp_prog;
  1686. int i;
  1687. for (i = 0; i < vi->max_queue_pairs; i++) {
  1688. xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  1689. if (xdp_prog)
  1690. return xdp_prog->aux->id;
  1691. }
  1692. return 0;
  1693. }
  1694. static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
  1695. {
  1696. switch (xdp->command) {
  1697. case XDP_SETUP_PROG:
  1698. return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
  1699. case XDP_QUERY_PROG:
  1700. xdp->prog_id = virtnet_xdp_query(dev);
  1701. xdp->prog_attached = !!xdp->prog_id;
  1702. return 0;
  1703. default:
  1704. return -EINVAL;
  1705. }
  1706. }
  1707. static const struct net_device_ops virtnet_netdev = {
  1708. .ndo_open = virtnet_open,
  1709. .ndo_stop = virtnet_close,
  1710. .ndo_start_xmit = start_xmit,
  1711. .ndo_validate_addr = eth_validate_addr,
  1712. .ndo_set_mac_address = virtnet_set_mac_address,
  1713. .ndo_set_rx_mode = virtnet_set_rx_mode,
  1714. .ndo_get_stats64 = virtnet_stats,
  1715. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  1716. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  1717. #ifdef CONFIG_NET_POLL_CONTROLLER
  1718. .ndo_poll_controller = virtnet_netpoll,
  1719. #endif
  1720. .ndo_xdp = virtnet_xdp,
  1721. .ndo_features_check = passthru_features_check,
  1722. };
  1723. static void virtnet_config_changed_work(struct work_struct *work)
  1724. {
  1725. struct virtnet_info *vi =
  1726. container_of(work, struct virtnet_info, config_work);
  1727. u16 v;
  1728. if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
  1729. struct virtio_net_config, status, &v) < 0)
  1730. return;
  1731. if (v & VIRTIO_NET_S_ANNOUNCE) {
  1732. netdev_notify_peers(vi->dev);
  1733. virtnet_ack_link_announce(vi);
  1734. }
  1735. /* Ignore unknown (future) status bits */
  1736. v &= VIRTIO_NET_S_LINK_UP;
  1737. if (vi->status == v)
  1738. return;
  1739. vi->status = v;
  1740. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  1741. netif_carrier_on(vi->dev);
  1742. netif_tx_wake_all_queues(vi->dev);
  1743. } else {
  1744. netif_carrier_off(vi->dev);
  1745. netif_tx_stop_all_queues(vi->dev);
  1746. }
  1747. }
  1748. static void virtnet_config_changed(struct virtio_device *vdev)
  1749. {
  1750. struct virtnet_info *vi = vdev->priv;
  1751. schedule_work(&vi->config_work);
  1752. }
  1753. static void virtnet_free_queues(struct virtnet_info *vi)
  1754. {
  1755. int i;
  1756. for (i = 0; i < vi->max_queue_pairs; i++) {
  1757. napi_hash_del(&vi->rq[i].napi);
  1758. netif_napi_del(&vi->rq[i].napi);
  1759. netif_napi_del(&vi->sq[i].napi);
  1760. }
  1761. /* We called napi_hash_del() before netif_napi_del(),
  1762. * we need to respect an RCU grace period before freeing vi->rq
  1763. */
  1764. synchronize_net();
  1765. kfree(vi->rq);
  1766. kfree(vi->sq);
  1767. }
  1768. static void _free_receive_bufs(struct virtnet_info *vi)
  1769. {
  1770. struct bpf_prog *old_prog;
  1771. int i;
  1772. for (i = 0; i < vi->max_queue_pairs; i++) {
  1773. while (vi->rq[i].pages)
  1774. __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
  1775. old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
  1776. RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
  1777. if (old_prog)
  1778. bpf_prog_put(old_prog);
  1779. }
  1780. }
  1781. static void free_receive_bufs(struct virtnet_info *vi)
  1782. {
  1783. rtnl_lock();
  1784. _free_receive_bufs(vi);
  1785. rtnl_unlock();
  1786. }
  1787. static void free_receive_page_frags(struct virtnet_info *vi)
  1788. {
  1789. int i;
  1790. for (i = 0; i < vi->max_queue_pairs; i++)
  1791. if (vi->rq[i].alloc_frag.page)
  1792. put_page(vi->rq[i].alloc_frag.page);
  1793. }
  1794. static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
  1795. {
  1796. if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
  1797. return false;
  1798. else if (q < vi->curr_queue_pairs)
  1799. return true;
  1800. else
  1801. return false;
  1802. }
  1803. static void free_unused_bufs(struct virtnet_info *vi)
  1804. {
  1805. void *buf;
  1806. int i;
  1807. for (i = 0; i < vi->max_queue_pairs; i++) {
  1808. struct virtqueue *vq = vi->sq[i].vq;
  1809. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1810. if (!is_xdp_raw_buffer_queue(vi, i))
  1811. dev_kfree_skb(buf);
  1812. else
  1813. put_page(virt_to_head_page(buf));
  1814. }
  1815. }
  1816. for (i = 0; i < vi->max_queue_pairs; i++) {
  1817. struct virtqueue *vq = vi->rq[i].vq;
  1818. while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
  1819. if (vi->mergeable_rx_bufs) {
  1820. put_page(virt_to_head_page(buf));
  1821. } else if (vi->big_packets) {
  1822. give_pages(&vi->rq[i], buf);
  1823. } else {
  1824. put_page(virt_to_head_page(buf));
  1825. }
  1826. }
  1827. }
  1828. }
  1829. static void virtnet_del_vqs(struct virtnet_info *vi)
  1830. {
  1831. struct virtio_device *vdev = vi->vdev;
  1832. virtnet_clean_affinity(vi, -1);
  1833. vdev->config->del_vqs(vdev);
  1834. virtnet_free_queues(vi);
  1835. }
  1836. /* How large should a single buffer be so a queue full of these can fit at
  1837. * least one full packet?
  1838. * Logic below assumes the mergeable buffer header is used.
  1839. */
  1840. static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
  1841. {
  1842. const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  1843. unsigned int rq_size = virtqueue_get_vring_size(vq);
  1844. unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
  1845. unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
  1846. unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
  1847. return max(max(min_buf_len, hdr_len) - hdr_len,
  1848. (unsigned int)GOOD_PACKET_LEN);
  1849. }
  1850. static int virtnet_find_vqs(struct virtnet_info *vi)
  1851. {
  1852. vq_callback_t **callbacks;
  1853. struct virtqueue **vqs;
  1854. int ret = -ENOMEM;
  1855. int i, total_vqs;
  1856. const char **names;
  1857. bool *ctx;
  1858. /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
  1859. * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
  1860. * possible control vq.
  1861. */
  1862. total_vqs = vi->max_queue_pairs * 2 +
  1863. virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
  1864. /* Allocate space for find_vqs parameters */
  1865. vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
  1866. if (!vqs)
  1867. goto err_vq;
  1868. callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
  1869. if (!callbacks)
  1870. goto err_callback;
  1871. names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
  1872. if (!names)
  1873. goto err_names;
  1874. if (!vi->big_packets || vi->mergeable_rx_bufs) {
  1875. ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
  1876. if (!ctx)
  1877. goto err_ctx;
  1878. } else {
  1879. ctx = NULL;
  1880. }
  1881. /* Parameters for control virtqueue, if any */
  1882. if (vi->has_cvq) {
  1883. callbacks[total_vqs - 1] = NULL;
  1884. names[total_vqs - 1] = "control";
  1885. }
  1886. /* Allocate/initialize parameters for send/receive virtqueues */
  1887. for (i = 0; i < vi->max_queue_pairs; i++) {
  1888. callbacks[rxq2vq(i)] = skb_recv_done;
  1889. callbacks[txq2vq(i)] = skb_xmit_done;
  1890. sprintf(vi->rq[i].name, "input.%d", i);
  1891. sprintf(vi->sq[i].name, "output.%d", i);
  1892. names[rxq2vq(i)] = vi->rq[i].name;
  1893. names[txq2vq(i)] = vi->sq[i].name;
  1894. if (ctx)
  1895. ctx[rxq2vq(i)] = true;
  1896. }
  1897. ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
  1898. names, ctx, NULL);
  1899. if (ret)
  1900. goto err_find;
  1901. if (vi->has_cvq) {
  1902. vi->cvq = vqs[total_vqs - 1];
  1903. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  1904. vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1905. }
  1906. for (i = 0; i < vi->max_queue_pairs; i++) {
  1907. vi->rq[i].vq = vqs[rxq2vq(i)];
  1908. vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
  1909. vi->sq[i].vq = vqs[txq2vq(i)];
  1910. }
  1911. kfree(names);
  1912. kfree(callbacks);
  1913. kfree(vqs);
  1914. kfree(ctx);
  1915. return 0;
  1916. err_find:
  1917. kfree(ctx);
  1918. err_ctx:
  1919. kfree(names);
  1920. err_names:
  1921. kfree(callbacks);
  1922. err_callback:
  1923. kfree(vqs);
  1924. err_vq:
  1925. return ret;
  1926. }
  1927. static int virtnet_alloc_queues(struct virtnet_info *vi)
  1928. {
  1929. int i;
  1930. vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
  1931. if (!vi->sq)
  1932. goto err_sq;
  1933. vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
  1934. if (!vi->rq)
  1935. goto err_rq;
  1936. INIT_DELAYED_WORK(&vi->refill, refill_work);
  1937. for (i = 0; i < vi->max_queue_pairs; i++) {
  1938. vi->rq[i].pages = NULL;
  1939. netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
  1940. napi_weight);
  1941. netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
  1942. napi_tx ? napi_weight : 0);
  1943. sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
  1944. ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
  1945. sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
  1946. }
  1947. return 0;
  1948. err_rq:
  1949. kfree(vi->sq);
  1950. err_sq:
  1951. return -ENOMEM;
  1952. }
  1953. static int init_vqs(struct virtnet_info *vi)
  1954. {
  1955. int ret;
  1956. /* Allocate send & receive queues */
  1957. ret = virtnet_alloc_queues(vi);
  1958. if (ret)
  1959. goto err;
  1960. ret = virtnet_find_vqs(vi);
  1961. if (ret)
  1962. goto err_free;
  1963. get_online_cpus();
  1964. virtnet_set_affinity(vi);
  1965. put_online_cpus();
  1966. return 0;
  1967. err_free:
  1968. virtnet_free_queues(vi);
  1969. err:
  1970. return ret;
  1971. }
  1972. #ifdef CONFIG_SYSFS
  1973. static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
  1974. char *buf)
  1975. {
  1976. struct virtnet_info *vi = netdev_priv(queue->dev);
  1977. unsigned int queue_index = get_netdev_rx_queue_index(queue);
  1978. struct ewma_pkt_len *avg;
  1979. BUG_ON(queue_index >= vi->max_queue_pairs);
  1980. avg = &vi->rq[queue_index].mrg_avg_pkt_len;
  1981. return sprintf(buf, "%u\n",
  1982. get_mergeable_buf_len(&vi->rq[queue_index], avg));
  1983. }
  1984. static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
  1985. __ATTR_RO(mergeable_rx_buffer_size);
  1986. static struct attribute *virtio_net_mrg_rx_attrs[] = {
  1987. &mergeable_rx_buffer_size_attribute.attr,
  1988. NULL
  1989. };
  1990. static const struct attribute_group virtio_net_mrg_rx_group = {
  1991. .name = "virtio_net",
  1992. .attrs = virtio_net_mrg_rx_attrs
  1993. };
  1994. #endif
  1995. static bool virtnet_fail_on_feature(struct virtio_device *vdev,
  1996. unsigned int fbit,
  1997. const char *fname, const char *dname)
  1998. {
  1999. if (!virtio_has_feature(vdev, fbit))
  2000. return false;
  2001. dev_err(&vdev->dev, "device advertises feature %s but not %s",
  2002. fname, dname);
  2003. return true;
  2004. }
  2005. #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
  2006. virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
  2007. static bool virtnet_validate_features(struct virtio_device *vdev)
  2008. {
  2009. if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
  2010. (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
  2011. "VIRTIO_NET_F_CTRL_VQ") ||
  2012. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
  2013. "VIRTIO_NET_F_CTRL_VQ") ||
  2014. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
  2015. "VIRTIO_NET_F_CTRL_VQ") ||
  2016. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
  2017. VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
  2018. "VIRTIO_NET_F_CTRL_VQ"))) {
  2019. return false;
  2020. }
  2021. return true;
  2022. }
  2023. #define MIN_MTU ETH_MIN_MTU
  2024. #define MAX_MTU ETH_MAX_MTU
  2025. static int virtnet_validate(struct virtio_device *vdev)
  2026. {
  2027. if (!vdev->config->get) {
  2028. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  2029. __func__);
  2030. return -EINVAL;
  2031. }
  2032. if (!virtnet_validate_features(vdev))
  2033. return -EINVAL;
  2034. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  2035. int mtu = virtio_cread16(vdev,
  2036. offsetof(struct virtio_net_config,
  2037. mtu));
  2038. if (mtu < MIN_MTU)
  2039. __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
  2040. }
  2041. return 0;
  2042. }
  2043. static int virtnet_probe(struct virtio_device *vdev)
  2044. {
  2045. int i, err;
  2046. struct net_device *dev;
  2047. struct virtnet_info *vi;
  2048. u16 max_queue_pairs;
  2049. int mtu;
  2050. /* Find if host supports multiqueue virtio_net device */
  2051. err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
  2052. struct virtio_net_config,
  2053. max_virtqueue_pairs, &max_queue_pairs);
  2054. /* We need at least 2 queue's */
  2055. if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  2056. max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  2057. !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  2058. max_queue_pairs = 1;
  2059. /* Allocate ourselves a network device with room for our info */
  2060. dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
  2061. if (!dev)
  2062. return -ENOMEM;
  2063. /* Set up network device as normal. */
  2064. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  2065. dev->netdev_ops = &virtnet_netdev;
  2066. dev->features = NETIF_F_HIGHDMA;
  2067. dev->ethtool_ops = &virtnet_ethtool_ops;
  2068. SET_NETDEV_DEV(dev, &vdev->dev);
  2069. /* Do we support "hardware" checksums? */
  2070. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  2071. /* This opens up the world of extra features. */
  2072. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  2073. if (csum)
  2074. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  2075. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  2076. dev->hw_features |= NETIF_F_TSO
  2077. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  2078. }
  2079. /* Individual feature bits: what can host handle? */
  2080. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  2081. dev->hw_features |= NETIF_F_TSO;
  2082. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  2083. dev->hw_features |= NETIF_F_TSO6;
  2084. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  2085. dev->hw_features |= NETIF_F_TSO_ECN;
  2086. dev->features |= NETIF_F_GSO_ROBUST;
  2087. if (gso)
  2088. dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
  2089. /* (!csum && gso) case will be fixed by register_netdev() */
  2090. }
  2091. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
  2092. dev->features |= NETIF_F_RXCSUM;
  2093. dev->vlan_features = dev->features;
  2094. /* MTU range: 68 - 65535 */
  2095. dev->min_mtu = MIN_MTU;
  2096. dev->max_mtu = MAX_MTU;
  2097. /* Configuration may specify what MAC to use. Otherwise random. */
  2098. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  2099. virtio_cread_bytes(vdev,
  2100. offsetof(struct virtio_net_config, mac),
  2101. dev->dev_addr, dev->addr_len);
  2102. else
  2103. eth_hw_addr_random(dev);
  2104. /* Set up our device-specific information */
  2105. vi = netdev_priv(dev);
  2106. vi->dev = dev;
  2107. vi->vdev = vdev;
  2108. vdev->priv = vi;
  2109. vi->stats = alloc_percpu(struct virtnet_stats);
  2110. err = -ENOMEM;
  2111. if (vi->stats == NULL)
  2112. goto free;
  2113. for_each_possible_cpu(i) {
  2114. struct virtnet_stats *virtnet_stats;
  2115. virtnet_stats = per_cpu_ptr(vi->stats, i);
  2116. u64_stats_init(&virtnet_stats->tx_syncp);
  2117. u64_stats_init(&virtnet_stats->rx_syncp);
  2118. }
  2119. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  2120. /* If we can receive ANY GSO packets, we must allocate large ones. */
  2121. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  2122. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  2123. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
  2124. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
  2125. vi->big_packets = true;
  2126. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  2127. vi->mergeable_rx_bufs = true;
  2128. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
  2129. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  2130. vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  2131. else
  2132. vi->hdr_len = sizeof(struct virtio_net_hdr);
  2133. if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
  2134. virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  2135. vi->any_header_sg = true;
  2136. if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
  2137. vi->has_cvq = true;
  2138. if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
  2139. mtu = virtio_cread16(vdev,
  2140. offsetof(struct virtio_net_config,
  2141. mtu));
  2142. if (mtu < dev->min_mtu) {
  2143. /* Should never trigger: MTU was previously validated
  2144. * in virtnet_validate.
  2145. */
  2146. dev_err(&vdev->dev, "device MTU appears to have changed "
  2147. "it is now %d < %d", mtu, dev->min_mtu);
  2148. goto free_stats;
  2149. }
  2150. dev->mtu = mtu;
  2151. dev->max_mtu = mtu;
  2152. /* TODO: size buffers correctly in this case. */
  2153. if (dev->mtu > ETH_DATA_LEN)
  2154. vi->big_packets = true;
  2155. }
  2156. if (vi->any_header_sg)
  2157. dev->needed_headroom = vi->hdr_len;
  2158. /* Enable multiqueue by default */
  2159. if (num_online_cpus() >= max_queue_pairs)
  2160. vi->curr_queue_pairs = max_queue_pairs;
  2161. else
  2162. vi->curr_queue_pairs = num_online_cpus();
  2163. vi->max_queue_pairs = max_queue_pairs;
  2164. /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
  2165. err = init_vqs(vi);
  2166. if (err)
  2167. goto free_stats;
  2168. #ifdef CONFIG_SYSFS
  2169. if (vi->mergeable_rx_bufs)
  2170. dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
  2171. #endif
  2172. netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
  2173. netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
  2174. virtnet_init_settings(dev);
  2175. err = register_netdev(dev);
  2176. if (err) {
  2177. pr_debug("virtio_net: registering device failed\n");
  2178. goto free_vqs;
  2179. }
  2180. virtio_device_ready(vdev);
  2181. err = virtnet_cpu_notif_add(vi);
  2182. if (err) {
  2183. pr_debug("virtio_net: registering cpu notifier failed\n");
  2184. goto free_unregister_netdev;
  2185. }
  2186. virtnet_set_queues(vi, vi->curr_queue_pairs);
  2187. /* Assume link up if device can't report link status,
  2188. otherwise get link status from config. */
  2189. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  2190. netif_carrier_off(dev);
  2191. schedule_work(&vi->config_work);
  2192. } else {
  2193. vi->status = VIRTIO_NET_S_LINK_UP;
  2194. netif_carrier_on(dev);
  2195. }
  2196. for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
  2197. if (virtio_has_feature(vi->vdev, guest_offloads[i]))
  2198. set_bit(guest_offloads[i], &vi->guest_offloads);
  2199. pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
  2200. dev->name, max_queue_pairs);
  2201. return 0;
  2202. free_unregister_netdev:
  2203. vi->vdev->config->reset(vdev);
  2204. unregister_netdev(dev);
  2205. free_vqs:
  2206. cancel_delayed_work_sync(&vi->refill);
  2207. free_receive_page_frags(vi);
  2208. virtnet_del_vqs(vi);
  2209. free_stats:
  2210. free_percpu(vi->stats);
  2211. free:
  2212. free_netdev(dev);
  2213. return err;
  2214. }
  2215. static void remove_vq_common(struct virtnet_info *vi)
  2216. {
  2217. vi->vdev->config->reset(vi->vdev);
  2218. /* Free unused buffers in both send and recv, if any. */
  2219. free_unused_bufs(vi);
  2220. free_receive_bufs(vi);
  2221. free_receive_page_frags(vi);
  2222. virtnet_del_vqs(vi);
  2223. }
  2224. static void virtnet_remove(struct virtio_device *vdev)
  2225. {
  2226. struct virtnet_info *vi = vdev->priv;
  2227. virtnet_cpu_notif_remove(vi);
  2228. /* Make sure no work handler is accessing the device. */
  2229. flush_work(&vi->config_work);
  2230. unregister_netdev(vi->dev);
  2231. remove_vq_common(vi);
  2232. free_percpu(vi->stats);
  2233. free_netdev(vi->dev);
  2234. }
  2235. static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
  2236. {
  2237. struct virtnet_info *vi = vdev->priv;
  2238. virtnet_cpu_notif_remove(vi);
  2239. virtnet_freeze_down(vdev);
  2240. remove_vq_common(vi);
  2241. return 0;
  2242. }
  2243. static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
  2244. {
  2245. struct virtnet_info *vi = vdev->priv;
  2246. int err;
  2247. err = virtnet_restore_up(vdev);
  2248. if (err)
  2249. return err;
  2250. virtnet_set_queues(vi, vi->curr_queue_pairs);
  2251. err = virtnet_cpu_notif_add(vi);
  2252. if (err)
  2253. return err;
  2254. return 0;
  2255. }
  2256. static struct virtio_device_id id_table[] = {
  2257. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  2258. { 0 },
  2259. };
  2260. #define VIRTNET_FEATURES \
  2261. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
  2262. VIRTIO_NET_F_MAC, \
  2263. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
  2264. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
  2265. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
  2266. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
  2267. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
  2268. VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
  2269. VIRTIO_NET_F_CTRL_MAC_ADDR, \
  2270. VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
  2271. static unsigned int features[] = {
  2272. VIRTNET_FEATURES,
  2273. };
  2274. static unsigned int features_legacy[] = {
  2275. VIRTNET_FEATURES,
  2276. VIRTIO_NET_F_GSO,
  2277. VIRTIO_F_ANY_LAYOUT,
  2278. };
  2279. static struct virtio_driver virtio_net_driver = {
  2280. .feature_table = features,
  2281. .feature_table_size = ARRAY_SIZE(features),
  2282. .feature_table_legacy = features_legacy,
  2283. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  2284. .driver.name = KBUILD_MODNAME,
  2285. .driver.owner = THIS_MODULE,
  2286. .id_table = id_table,
  2287. .validate = virtnet_validate,
  2288. .probe = virtnet_probe,
  2289. .remove = virtnet_remove,
  2290. .config_changed = virtnet_config_changed,
  2291. #ifdef CONFIG_PM_SLEEP
  2292. .freeze = virtnet_freeze,
  2293. .restore = virtnet_restore,
  2294. #endif
  2295. };
  2296. static __init int virtio_net_driver_init(void)
  2297. {
  2298. int ret;
  2299. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
  2300. virtnet_cpu_online,
  2301. virtnet_cpu_down_prep);
  2302. if (ret < 0)
  2303. goto out;
  2304. virtionet_online = ret;
  2305. ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
  2306. NULL, virtnet_cpu_dead);
  2307. if (ret)
  2308. goto err_dead;
  2309. ret = register_virtio_driver(&virtio_net_driver);
  2310. if (ret)
  2311. goto err_virtio;
  2312. return 0;
  2313. err_virtio:
  2314. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  2315. err_dead:
  2316. cpuhp_remove_multi_state(virtionet_online);
  2317. out:
  2318. return ret;
  2319. }
  2320. module_init(virtio_net_driver_init);
  2321. static __exit void virtio_net_driver_exit(void)
  2322. {
  2323. unregister_virtio_driver(&virtio_net_driver);
  2324. cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
  2325. cpuhp_remove_multi_state(virtionet_online);
  2326. }
  2327. module_exit(virtio_net_driver_exit);
  2328. MODULE_DEVICE_TABLE(virtio, id_table);
  2329. MODULE_DESCRIPTION("Virtio network driver");
  2330. MODULE_LICENSE("GPL");