virtio_net.c 70 KB

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