virtio_net.c 72 KB

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