virtio_net.c 62 KB

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