virtio_net.c 82 KB

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