virtio_net.c 82 KB

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