virtio_net.c 63 KB

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