virtio_net.c 80 KB

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