virtio_net.c 83 KB

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