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