virtio_net.c 73 KB

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