virtio_net.c 73 KB

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