virtio_net.c 83 KB

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