virtio_net.c 81 KB

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