virtio_net.c 82 KB

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