virtio_net.c 76 KB

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