virtio_net.c 70 KB

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