net.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2009 Red Hat, Inc.
  3. * Author: Michael S. Tsirkin <[email protected]>
  4. *
  5. * virtio-net server in host kernel.
  6. */
  7. #include <linux/compat.h>
  8. #include <linux/eventfd.h>
  9. #include <linux/vhost.h>
  10. #include <linux/virtio_net.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/mutex.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/file.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched/clock.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/net.h>
  22. #include <linux/if_packet.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/if_tun.h>
  25. #include <linux/if_macvlan.h>
  26. #include <linux/if_tap.h>
  27. #include <linux/if_vlan.h>
  28. #include <linux/skb_array.h>
  29. #include <linux/skbuff.h>
  30. #include <net/sock.h>
  31. #include <net/xdp.h>
  32. #include "vhost.h"
  33. static int experimental_zcopytx = 0;
  34. module_param(experimental_zcopytx, int, 0444);
  35. MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
  36. " 1 -Enable; 0 - Disable");
  37. /* Max number of bytes transferred before requeueing the job.
  38. * Using this limit prevents one virtqueue from starving others. */
  39. #define VHOST_NET_WEIGHT 0x80000
  40. /* Max number of packets transferred before requeueing the job.
  41. * Using this limit prevents one virtqueue from starving others with small
  42. * pkts.
  43. */
  44. #define VHOST_NET_PKT_WEIGHT 256
  45. /* MAX number of TX used buffers for outstanding zerocopy */
  46. #define VHOST_MAX_PEND 128
  47. #define VHOST_GOODCOPY_LEN 256
  48. /*
  49. * For transmit, used buffer len is unused; we override it to track buffer
  50. * status internally; used for zerocopy tx only.
  51. */
  52. /* Lower device DMA failed */
  53. #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
  54. /* Lower device DMA done */
  55. #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
  56. /* Lower device DMA in progress */
  57. #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
  58. /* Buffer unused */
  59. #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
  60. #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
  61. enum {
  62. VHOST_NET_FEATURES = VHOST_FEATURES |
  63. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  64. (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  65. (1ULL << VIRTIO_F_ACCESS_PLATFORM)
  66. };
  67. enum {
  68. VHOST_NET_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
  69. };
  70. enum {
  71. VHOST_NET_VQ_RX = 0,
  72. VHOST_NET_VQ_TX = 1,
  73. VHOST_NET_VQ_MAX = 2,
  74. };
  75. struct vhost_net_ubuf_ref {
  76. /* refcount follows semantics similar to kref:
  77. * 0: object is released
  78. * 1: no outstanding ubufs
  79. * >1: outstanding ubufs
  80. */
  81. atomic_t refcount;
  82. wait_queue_head_t wait;
  83. struct vhost_virtqueue *vq;
  84. };
  85. #define VHOST_NET_BATCH 64
  86. struct vhost_net_buf {
  87. void **queue;
  88. int tail;
  89. int head;
  90. };
  91. struct vhost_net_virtqueue {
  92. struct vhost_virtqueue vq;
  93. size_t vhost_hlen;
  94. size_t sock_hlen;
  95. /* vhost zerocopy support fields below: */
  96. /* last used idx for outstanding DMA zerocopy buffers */
  97. int upend_idx;
  98. /* For TX, first used idx for DMA done zerocopy buffers
  99. * For RX, number of batched heads
  100. */
  101. int done_idx;
  102. /* Number of XDP frames batched */
  103. int batched_xdp;
  104. /* an array of userspace buffers info */
  105. struct ubuf_info_msgzc *ubuf_info;
  106. /* Reference counting for outstanding ubufs.
  107. * Protected by vq mutex. Writers must also take device mutex. */
  108. struct vhost_net_ubuf_ref *ubufs;
  109. struct ptr_ring *rx_ring;
  110. struct vhost_net_buf rxq;
  111. /* Batched XDP buffs */
  112. struct xdp_buff *xdp;
  113. };
  114. struct vhost_net {
  115. struct vhost_dev dev;
  116. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  117. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  118. /* Number of TX recently submitted.
  119. * Protected by tx vq lock. */
  120. unsigned tx_packets;
  121. /* Number of times zerocopy TX recently failed.
  122. * Protected by tx vq lock. */
  123. unsigned tx_zcopy_err;
  124. /* Flush in progress. Protected by tx vq lock. */
  125. bool tx_flush;
  126. /* Private page frag */
  127. struct page_frag page_frag;
  128. /* Refcount bias of page frag */
  129. int refcnt_bias;
  130. };
  131. static unsigned vhost_net_zcopy_mask __read_mostly;
  132. static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
  133. {
  134. if (rxq->tail != rxq->head)
  135. return rxq->queue[rxq->head];
  136. else
  137. return NULL;
  138. }
  139. static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
  140. {
  141. return rxq->tail - rxq->head;
  142. }
  143. static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
  144. {
  145. return rxq->tail == rxq->head;
  146. }
  147. static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
  148. {
  149. void *ret = vhost_net_buf_get_ptr(rxq);
  150. ++rxq->head;
  151. return ret;
  152. }
  153. static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
  154. {
  155. struct vhost_net_buf *rxq = &nvq->rxq;
  156. rxq->head = 0;
  157. rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
  158. VHOST_NET_BATCH);
  159. return rxq->tail;
  160. }
  161. static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
  162. {
  163. struct vhost_net_buf *rxq = &nvq->rxq;
  164. if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
  165. ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
  166. vhost_net_buf_get_size(rxq),
  167. tun_ptr_free);
  168. rxq->head = rxq->tail = 0;
  169. }
  170. }
  171. static int vhost_net_buf_peek_len(void *ptr)
  172. {
  173. if (tun_is_xdp_frame(ptr)) {
  174. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  175. return xdpf->len;
  176. }
  177. return __skb_array_len_with_tag(ptr);
  178. }
  179. static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
  180. {
  181. struct vhost_net_buf *rxq = &nvq->rxq;
  182. if (!vhost_net_buf_is_empty(rxq))
  183. goto out;
  184. if (!vhost_net_buf_produce(nvq))
  185. return 0;
  186. out:
  187. return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq));
  188. }
  189. static void vhost_net_buf_init(struct vhost_net_buf *rxq)
  190. {
  191. rxq->head = rxq->tail = 0;
  192. }
  193. static void vhost_net_enable_zcopy(int vq)
  194. {
  195. vhost_net_zcopy_mask |= 0x1 << vq;
  196. }
  197. static struct vhost_net_ubuf_ref *
  198. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  199. {
  200. struct vhost_net_ubuf_ref *ubufs;
  201. /* No zero copy backend? Nothing to count. */
  202. if (!zcopy)
  203. return NULL;
  204. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  205. if (!ubufs)
  206. return ERR_PTR(-ENOMEM);
  207. atomic_set(&ubufs->refcount, 1);
  208. init_waitqueue_head(&ubufs->wait);
  209. ubufs->vq = vq;
  210. return ubufs;
  211. }
  212. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  213. {
  214. int r = atomic_sub_return(1, &ubufs->refcount);
  215. if (unlikely(!r))
  216. wake_up(&ubufs->wait);
  217. return r;
  218. }
  219. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  220. {
  221. vhost_net_ubuf_put(ubufs);
  222. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  223. }
  224. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  225. {
  226. vhost_net_ubuf_put_and_wait(ubufs);
  227. kfree(ubufs);
  228. }
  229. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  230. {
  231. int i;
  232. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  233. kfree(n->vqs[i].ubuf_info);
  234. n->vqs[i].ubuf_info = NULL;
  235. }
  236. }
  237. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  238. {
  239. bool zcopy;
  240. int i;
  241. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  242. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  243. if (!zcopy)
  244. continue;
  245. n->vqs[i].ubuf_info =
  246. kmalloc_array(UIO_MAXIOV,
  247. sizeof(*n->vqs[i].ubuf_info),
  248. GFP_KERNEL);
  249. if (!n->vqs[i].ubuf_info)
  250. goto err;
  251. }
  252. return 0;
  253. err:
  254. vhost_net_clear_ubuf_info(n);
  255. return -ENOMEM;
  256. }
  257. static void vhost_net_vq_reset(struct vhost_net *n)
  258. {
  259. int i;
  260. vhost_net_clear_ubuf_info(n);
  261. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  262. n->vqs[i].done_idx = 0;
  263. n->vqs[i].upend_idx = 0;
  264. n->vqs[i].ubufs = NULL;
  265. n->vqs[i].vhost_hlen = 0;
  266. n->vqs[i].sock_hlen = 0;
  267. vhost_net_buf_init(&n->vqs[i].rxq);
  268. }
  269. }
  270. static void vhost_net_tx_packet(struct vhost_net *net)
  271. {
  272. ++net->tx_packets;
  273. if (net->tx_packets < 1024)
  274. return;
  275. net->tx_packets = 0;
  276. net->tx_zcopy_err = 0;
  277. }
  278. static void vhost_net_tx_err(struct vhost_net *net)
  279. {
  280. ++net->tx_zcopy_err;
  281. }
  282. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  283. {
  284. /* TX flush waits for outstanding DMAs to be done.
  285. * Don't start new DMAs.
  286. */
  287. return !net->tx_flush &&
  288. net->tx_packets / 64 >= net->tx_zcopy_err;
  289. }
  290. static bool vhost_sock_zcopy(struct socket *sock)
  291. {
  292. return unlikely(experimental_zcopytx) &&
  293. sock_flag(sock->sk, SOCK_ZEROCOPY);
  294. }
  295. static bool vhost_sock_xdp(struct socket *sock)
  296. {
  297. return sock_flag(sock->sk, SOCK_XDP);
  298. }
  299. /* In case of DMA done not in order in lower device driver for some reason.
  300. * upend_idx is used to track end of used idx, done_idx is used to track head
  301. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  302. * guest used idx.
  303. */
  304. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  305. struct vhost_virtqueue *vq)
  306. {
  307. struct vhost_net_virtqueue *nvq =
  308. container_of(vq, struct vhost_net_virtqueue, vq);
  309. int i, add;
  310. int j = 0;
  311. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  312. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  313. vhost_net_tx_err(net);
  314. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  315. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  316. ++j;
  317. } else
  318. break;
  319. }
  320. while (j) {
  321. add = min(UIO_MAXIOV - nvq->done_idx, j);
  322. vhost_add_used_and_signal_n(vq->dev, vq,
  323. &vq->heads[nvq->done_idx], add);
  324. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  325. j -= add;
  326. }
  327. }
  328. static void vhost_zerocopy_callback(struct sk_buff *skb,
  329. struct ubuf_info *ubuf_base, bool success)
  330. {
  331. struct ubuf_info_msgzc *ubuf = uarg_to_msgzc(ubuf_base);
  332. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  333. struct vhost_virtqueue *vq = ubufs->vq;
  334. int cnt;
  335. rcu_read_lock_bh();
  336. /* set len to mark this desc buffers done DMA */
  337. vq->heads[ubuf->desc].len = success ?
  338. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  339. cnt = vhost_net_ubuf_put(ubufs);
  340. /*
  341. * Trigger polling thread if guest stopped submitting new buffers:
  342. * in this case, the refcount after decrement will eventually reach 1.
  343. * We also trigger polling periodically after each 16 packets
  344. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  345. * less than 10% of times).
  346. */
  347. if (cnt <= 1 || !(cnt % 16))
  348. vhost_poll_queue(&vq->poll);
  349. rcu_read_unlock_bh();
  350. }
  351. static inline unsigned long busy_clock(void)
  352. {
  353. return local_clock() >> 10;
  354. }
  355. static bool vhost_can_busy_poll(unsigned long endtime)
  356. {
  357. return likely(!need_resched() && !time_after(busy_clock(), endtime) &&
  358. !signal_pending(current));
  359. }
  360. static void vhost_net_disable_vq(struct vhost_net *n,
  361. struct vhost_virtqueue *vq)
  362. {
  363. struct vhost_net_virtqueue *nvq =
  364. container_of(vq, struct vhost_net_virtqueue, vq);
  365. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  366. if (!vhost_vq_get_backend(vq))
  367. return;
  368. vhost_poll_stop(poll);
  369. }
  370. static int vhost_net_enable_vq(struct vhost_net *n,
  371. struct vhost_virtqueue *vq)
  372. {
  373. struct vhost_net_virtqueue *nvq =
  374. container_of(vq, struct vhost_net_virtqueue, vq);
  375. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  376. struct socket *sock;
  377. sock = vhost_vq_get_backend(vq);
  378. if (!sock)
  379. return 0;
  380. return vhost_poll_start(poll, sock->file);
  381. }
  382. static void vhost_net_signal_used(struct vhost_net_virtqueue *nvq)
  383. {
  384. struct vhost_virtqueue *vq = &nvq->vq;
  385. struct vhost_dev *dev = vq->dev;
  386. if (!nvq->done_idx)
  387. return;
  388. vhost_add_used_and_signal_n(dev, vq, vq->heads, nvq->done_idx);
  389. nvq->done_idx = 0;
  390. }
  391. static void vhost_tx_batch(struct vhost_net *net,
  392. struct vhost_net_virtqueue *nvq,
  393. struct socket *sock,
  394. struct msghdr *msghdr)
  395. {
  396. struct tun_msg_ctl ctl = {
  397. .type = TUN_MSG_PTR,
  398. .num = nvq->batched_xdp,
  399. .ptr = nvq->xdp,
  400. };
  401. int i, err;
  402. if (nvq->batched_xdp == 0)
  403. goto signal_used;
  404. msghdr->msg_control = &ctl;
  405. msghdr->msg_controllen = sizeof(ctl);
  406. err = sock->ops->sendmsg(sock, msghdr, 0);
  407. if (unlikely(err < 0)) {
  408. vq_err(&nvq->vq, "Fail to batch sending packets\n");
  409. /* free pages owned by XDP; since this is an unlikely error path,
  410. * keep it simple and avoid more complex bulk update for the
  411. * used pages
  412. */
  413. for (i = 0; i < nvq->batched_xdp; ++i)
  414. put_page(virt_to_head_page(nvq->xdp[i].data));
  415. nvq->batched_xdp = 0;
  416. nvq->done_idx = 0;
  417. return;
  418. }
  419. signal_used:
  420. vhost_net_signal_used(nvq);
  421. nvq->batched_xdp = 0;
  422. }
  423. static int sock_has_rx_data(struct socket *sock)
  424. {
  425. if (unlikely(!sock))
  426. return 0;
  427. if (sock->ops->peek_len)
  428. return sock->ops->peek_len(sock);
  429. return skb_queue_empty(&sock->sk->sk_receive_queue);
  430. }
  431. static void vhost_net_busy_poll_try_queue(struct vhost_net *net,
  432. struct vhost_virtqueue *vq)
  433. {
  434. if (!vhost_vq_avail_empty(&net->dev, vq)) {
  435. vhost_poll_queue(&vq->poll);
  436. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  437. vhost_disable_notify(&net->dev, vq);
  438. vhost_poll_queue(&vq->poll);
  439. }
  440. }
  441. static void vhost_net_busy_poll(struct vhost_net *net,
  442. struct vhost_virtqueue *rvq,
  443. struct vhost_virtqueue *tvq,
  444. bool *busyloop_intr,
  445. bool poll_rx)
  446. {
  447. unsigned long busyloop_timeout;
  448. unsigned long endtime;
  449. struct socket *sock;
  450. struct vhost_virtqueue *vq = poll_rx ? tvq : rvq;
  451. /* Try to hold the vq mutex of the paired virtqueue. We can't
  452. * use mutex_lock() here since we could not guarantee a
  453. * consistenet lock ordering.
  454. */
  455. if (!mutex_trylock(&vq->mutex))
  456. return;
  457. vhost_disable_notify(&net->dev, vq);
  458. sock = vhost_vq_get_backend(rvq);
  459. busyloop_timeout = poll_rx ? rvq->busyloop_timeout:
  460. tvq->busyloop_timeout;
  461. preempt_disable();
  462. endtime = busy_clock() + busyloop_timeout;
  463. while (vhost_can_busy_poll(endtime)) {
  464. if (vhost_has_work(&net->dev)) {
  465. *busyloop_intr = true;
  466. break;
  467. }
  468. if ((sock_has_rx_data(sock) &&
  469. !vhost_vq_avail_empty(&net->dev, rvq)) ||
  470. !vhost_vq_avail_empty(&net->dev, tvq))
  471. break;
  472. cpu_relax();
  473. }
  474. preempt_enable();
  475. if (poll_rx || sock_has_rx_data(sock))
  476. vhost_net_busy_poll_try_queue(net, vq);
  477. else if (!poll_rx) /* On tx here, sock has no rx data. */
  478. vhost_enable_notify(&net->dev, rvq);
  479. mutex_unlock(&vq->mutex);
  480. }
  481. static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
  482. struct vhost_net_virtqueue *tnvq,
  483. unsigned int *out_num, unsigned int *in_num,
  484. struct msghdr *msghdr, bool *busyloop_intr)
  485. {
  486. struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
  487. struct vhost_virtqueue *rvq = &rnvq->vq;
  488. struct vhost_virtqueue *tvq = &tnvq->vq;
  489. int r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
  490. out_num, in_num, NULL, NULL);
  491. if (r == tvq->num && tvq->busyloop_timeout) {
  492. /* Flush batched packets first */
  493. if (!vhost_sock_zcopy(vhost_vq_get_backend(tvq)))
  494. vhost_tx_batch(net, tnvq,
  495. vhost_vq_get_backend(tvq),
  496. msghdr);
  497. vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, false);
  498. r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
  499. out_num, in_num, NULL, NULL);
  500. }
  501. return r;
  502. }
  503. static bool vhost_exceeds_maxpend(struct vhost_net *net)
  504. {
  505. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  506. struct vhost_virtqueue *vq = &nvq->vq;
  507. return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
  508. min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
  509. }
  510. static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter,
  511. size_t hdr_size, int out)
  512. {
  513. /* Skip header. TODO: support TSO. */
  514. size_t len = iov_length(vq->iov, out);
  515. iov_iter_init(iter, ITER_SOURCE, vq->iov, out, len);
  516. iov_iter_advance(iter, hdr_size);
  517. return iov_iter_count(iter);
  518. }
  519. static int get_tx_bufs(struct vhost_net *net,
  520. struct vhost_net_virtqueue *nvq,
  521. struct msghdr *msg,
  522. unsigned int *out, unsigned int *in,
  523. size_t *len, bool *busyloop_intr)
  524. {
  525. struct vhost_virtqueue *vq = &nvq->vq;
  526. int ret;
  527. ret = vhost_net_tx_get_vq_desc(net, nvq, out, in, msg, busyloop_intr);
  528. if (ret < 0 || ret == vq->num)
  529. return ret;
  530. if (*in) {
  531. vq_err(vq, "Unexpected descriptor format for TX: out %d, int %d\n",
  532. *out, *in);
  533. return -EFAULT;
  534. }
  535. /* Sanity check */
  536. *len = init_iov_iter(vq, &msg->msg_iter, nvq->vhost_hlen, *out);
  537. if (*len == 0) {
  538. vq_err(vq, "Unexpected header len for TX: %zd expected %zd\n",
  539. *len, nvq->vhost_hlen);
  540. return -EFAULT;
  541. }
  542. return ret;
  543. }
  544. static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len)
  545. {
  546. return total_len < VHOST_NET_WEIGHT &&
  547. !vhost_vq_avail_empty(vq->dev, vq);
  548. }
  549. static bool vhost_net_page_frag_refill(struct vhost_net *net, unsigned int sz,
  550. struct page_frag *pfrag, gfp_t gfp)
  551. {
  552. if (pfrag->page) {
  553. if (pfrag->offset + sz <= pfrag->size)
  554. return true;
  555. __page_frag_cache_drain(pfrag->page, net->refcnt_bias);
  556. }
  557. pfrag->offset = 0;
  558. net->refcnt_bias = 0;
  559. if (SKB_FRAG_PAGE_ORDER) {
  560. /* Avoid direct reclaim but allow kswapd to wake */
  561. pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
  562. __GFP_COMP | __GFP_NOWARN |
  563. __GFP_NORETRY,
  564. SKB_FRAG_PAGE_ORDER);
  565. if (likely(pfrag->page)) {
  566. pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
  567. goto done;
  568. }
  569. }
  570. pfrag->page = alloc_page(gfp);
  571. if (likely(pfrag->page)) {
  572. pfrag->size = PAGE_SIZE;
  573. goto done;
  574. }
  575. return false;
  576. done:
  577. net->refcnt_bias = USHRT_MAX;
  578. page_ref_add(pfrag->page, USHRT_MAX - 1);
  579. return true;
  580. }
  581. #define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  582. static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
  583. struct iov_iter *from)
  584. {
  585. struct vhost_virtqueue *vq = &nvq->vq;
  586. struct vhost_net *net = container_of(vq->dev, struct vhost_net,
  587. dev);
  588. struct socket *sock = vhost_vq_get_backend(vq);
  589. struct page_frag *alloc_frag = &net->page_frag;
  590. struct virtio_net_hdr *gso;
  591. struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
  592. struct tun_xdp_hdr *hdr;
  593. size_t len = iov_iter_count(from);
  594. int headroom = vhost_sock_xdp(sock) ? XDP_PACKET_HEADROOM : 0;
  595. int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  596. int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen);
  597. int sock_hlen = nvq->sock_hlen;
  598. void *buf;
  599. int copied;
  600. if (unlikely(len < nvq->sock_hlen))
  601. return -EFAULT;
  602. if (SKB_DATA_ALIGN(len + pad) +
  603. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
  604. return -ENOSPC;
  605. buflen += SKB_DATA_ALIGN(len + pad);
  606. alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
  607. if (unlikely(!vhost_net_page_frag_refill(net, buflen,
  608. alloc_frag, GFP_KERNEL)))
  609. return -ENOMEM;
  610. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  611. copied = copy_page_from_iter(alloc_frag->page,
  612. alloc_frag->offset +
  613. offsetof(struct tun_xdp_hdr, gso),
  614. sock_hlen, from);
  615. if (copied != sock_hlen)
  616. return -EFAULT;
  617. hdr = buf;
  618. gso = &hdr->gso;
  619. if ((gso->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  620. vhost16_to_cpu(vq, gso->csum_start) +
  621. vhost16_to_cpu(vq, gso->csum_offset) + 2 >
  622. vhost16_to_cpu(vq, gso->hdr_len)) {
  623. gso->hdr_len = cpu_to_vhost16(vq,
  624. vhost16_to_cpu(vq, gso->csum_start) +
  625. vhost16_to_cpu(vq, gso->csum_offset) + 2);
  626. if (vhost16_to_cpu(vq, gso->hdr_len) > len)
  627. return -EINVAL;
  628. }
  629. len -= sock_hlen;
  630. copied = copy_page_from_iter(alloc_frag->page,
  631. alloc_frag->offset + pad,
  632. len, from);
  633. if (copied != len)
  634. return -EFAULT;
  635. xdp_init_buff(xdp, buflen, NULL);
  636. xdp_prepare_buff(xdp, buf, pad, len, true);
  637. hdr->buflen = buflen;
  638. --net->refcnt_bias;
  639. alloc_frag->offset += buflen;
  640. ++nvq->batched_xdp;
  641. return 0;
  642. }
  643. static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
  644. {
  645. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  646. struct vhost_virtqueue *vq = &nvq->vq;
  647. unsigned out, in;
  648. int head;
  649. struct msghdr msg = {
  650. .msg_name = NULL,
  651. .msg_namelen = 0,
  652. .msg_control = NULL,
  653. .msg_controllen = 0,
  654. .msg_flags = MSG_DONTWAIT,
  655. };
  656. size_t len, total_len = 0;
  657. int err;
  658. int sent_pkts = 0;
  659. bool sock_can_batch = (sock->sk->sk_sndbuf == INT_MAX);
  660. do {
  661. bool busyloop_intr = false;
  662. if (nvq->done_idx == VHOST_NET_BATCH)
  663. vhost_tx_batch(net, nvq, sock, &msg);
  664. head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
  665. &busyloop_intr);
  666. /* On error, stop handling until the next kick. */
  667. if (unlikely(head < 0))
  668. break;
  669. /* Nothing new? Wait for eventfd to tell us they refilled. */
  670. if (head == vq->num) {
  671. if (unlikely(busyloop_intr)) {
  672. vhost_poll_queue(&vq->poll);
  673. } else if (unlikely(vhost_enable_notify(&net->dev,
  674. vq))) {
  675. vhost_disable_notify(&net->dev, vq);
  676. continue;
  677. }
  678. break;
  679. }
  680. total_len += len;
  681. /* For simplicity, TX batching is only enabled if
  682. * sndbuf is unlimited.
  683. */
  684. if (sock_can_batch) {
  685. err = vhost_net_build_xdp(nvq, &msg.msg_iter);
  686. if (!err) {
  687. goto done;
  688. } else if (unlikely(err != -ENOSPC)) {
  689. vhost_tx_batch(net, nvq, sock, &msg);
  690. vhost_discard_vq_desc(vq, 1);
  691. vhost_net_enable_vq(net, vq);
  692. break;
  693. }
  694. /* We can't build XDP buff, go for single
  695. * packet path but let's flush batched
  696. * packets.
  697. */
  698. vhost_tx_batch(net, nvq, sock, &msg);
  699. msg.msg_control = NULL;
  700. } else {
  701. if (tx_can_batch(vq, total_len))
  702. msg.msg_flags |= MSG_MORE;
  703. else
  704. msg.msg_flags &= ~MSG_MORE;
  705. }
  706. err = sock->ops->sendmsg(sock, &msg, len);
  707. if (unlikely(err < 0)) {
  708. if (err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS) {
  709. vhost_discard_vq_desc(vq, 1);
  710. vhost_net_enable_vq(net, vq);
  711. break;
  712. }
  713. pr_debug("Fail to send packet: err %d", err);
  714. } else if (unlikely(err != len))
  715. pr_debug("Truncated TX packet: len %d != %zd\n",
  716. err, len);
  717. done:
  718. vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head);
  719. vq->heads[nvq->done_idx].len = 0;
  720. ++nvq->done_idx;
  721. } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
  722. vhost_tx_batch(net, nvq, sock, &msg);
  723. }
  724. static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
  725. {
  726. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  727. struct vhost_virtqueue *vq = &nvq->vq;
  728. unsigned out, in;
  729. int head;
  730. struct msghdr msg = {
  731. .msg_name = NULL,
  732. .msg_namelen = 0,
  733. .msg_control = NULL,
  734. .msg_controllen = 0,
  735. .msg_flags = MSG_DONTWAIT,
  736. };
  737. struct tun_msg_ctl ctl;
  738. size_t len, total_len = 0;
  739. int err;
  740. struct vhost_net_ubuf_ref *ubufs;
  741. struct ubuf_info_msgzc *ubuf;
  742. bool zcopy_used;
  743. int sent_pkts = 0;
  744. do {
  745. bool busyloop_intr;
  746. /* Release DMAs done buffers first */
  747. vhost_zerocopy_signal_used(net, vq);
  748. busyloop_intr = false;
  749. head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
  750. &busyloop_intr);
  751. /* On error, stop handling until the next kick. */
  752. if (unlikely(head < 0))
  753. break;
  754. /* Nothing new? Wait for eventfd to tell us they refilled. */
  755. if (head == vq->num) {
  756. if (unlikely(busyloop_intr)) {
  757. vhost_poll_queue(&vq->poll);
  758. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  759. vhost_disable_notify(&net->dev, vq);
  760. continue;
  761. }
  762. break;
  763. }
  764. zcopy_used = len >= VHOST_GOODCOPY_LEN
  765. && !vhost_exceeds_maxpend(net)
  766. && vhost_net_tx_select_zcopy(net);
  767. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  768. if (zcopy_used) {
  769. ubuf = nvq->ubuf_info + nvq->upend_idx;
  770. vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
  771. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  772. ubuf->ctx = nvq->ubufs;
  773. ubuf->desc = nvq->upend_idx;
  774. ubuf->ubuf.callback = vhost_zerocopy_callback;
  775. ubuf->ubuf.flags = SKBFL_ZEROCOPY_FRAG;
  776. refcount_set(&ubuf->ubuf.refcnt, 1);
  777. msg.msg_control = &ctl;
  778. ctl.type = TUN_MSG_UBUF;
  779. ctl.ptr = &ubuf->ubuf;
  780. msg.msg_controllen = sizeof(ctl);
  781. ubufs = nvq->ubufs;
  782. atomic_inc(&ubufs->refcount);
  783. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  784. } else {
  785. msg.msg_control = NULL;
  786. ubufs = NULL;
  787. }
  788. total_len += len;
  789. if (tx_can_batch(vq, total_len) &&
  790. likely(!vhost_exceeds_maxpend(net))) {
  791. msg.msg_flags |= MSG_MORE;
  792. } else {
  793. msg.msg_flags &= ~MSG_MORE;
  794. }
  795. err = sock->ops->sendmsg(sock, &msg, len);
  796. if (unlikely(err < 0)) {
  797. bool retry = err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS;
  798. if (zcopy_used) {
  799. if (vq->heads[ubuf->desc].len == VHOST_DMA_IN_PROGRESS)
  800. vhost_net_ubuf_put(ubufs);
  801. if (retry)
  802. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  803. % UIO_MAXIOV;
  804. else
  805. vq->heads[ubuf->desc].len = VHOST_DMA_DONE_LEN;
  806. }
  807. if (retry) {
  808. vhost_discard_vq_desc(vq, 1);
  809. vhost_net_enable_vq(net, vq);
  810. break;
  811. }
  812. pr_debug("Fail to send packet: err %d", err);
  813. } else if (unlikely(err != len))
  814. pr_debug("Truncated TX packet: "
  815. " len %d != %zd\n", err, len);
  816. if (!zcopy_used)
  817. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  818. else
  819. vhost_zerocopy_signal_used(net, vq);
  820. vhost_net_tx_packet(net);
  821. } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
  822. }
  823. /* Expects to be always run from workqueue - which acts as
  824. * read-size critical section for our kind of RCU. */
  825. static void handle_tx(struct vhost_net *net)
  826. {
  827. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  828. struct vhost_virtqueue *vq = &nvq->vq;
  829. struct socket *sock;
  830. mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_TX);
  831. sock = vhost_vq_get_backend(vq);
  832. if (!sock)
  833. goto out;
  834. if (!vq_meta_prefetch(vq))
  835. goto out;
  836. vhost_disable_notify(&net->dev, vq);
  837. vhost_net_disable_vq(net, vq);
  838. if (vhost_sock_zcopy(sock))
  839. handle_tx_zerocopy(net, sock);
  840. else
  841. handle_tx_copy(net, sock);
  842. out:
  843. mutex_unlock(&vq->mutex);
  844. }
  845. static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
  846. {
  847. struct sk_buff *head;
  848. int len = 0;
  849. unsigned long flags;
  850. if (rvq->rx_ring)
  851. return vhost_net_buf_peek(rvq);
  852. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  853. head = skb_peek(&sk->sk_receive_queue);
  854. if (likely(head)) {
  855. len = head->len;
  856. if (skb_vlan_tag_present(head))
  857. len += VLAN_HLEN;
  858. }
  859. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  860. return len;
  861. }
  862. static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk,
  863. bool *busyloop_intr)
  864. {
  865. struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
  866. struct vhost_net_virtqueue *tnvq = &net->vqs[VHOST_NET_VQ_TX];
  867. struct vhost_virtqueue *rvq = &rnvq->vq;
  868. struct vhost_virtqueue *tvq = &tnvq->vq;
  869. int len = peek_head_len(rnvq, sk);
  870. if (!len && rvq->busyloop_timeout) {
  871. /* Flush batched heads first */
  872. vhost_net_signal_used(rnvq);
  873. /* Both tx vq and rx socket were polled here */
  874. vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, true);
  875. len = peek_head_len(rnvq, sk);
  876. }
  877. return len;
  878. }
  879. /* This is a multi-buffer version of vhost_get_desc, that works if
  880. * vq has read descriptors only.
  881. * @vq - the relevant virtqueue
  882. * @datalen - data length we'll be reading
  883. * @iovcount - returned count of io vectors we fill
  884. * @log - vhost log
  885. * @log_num - log offset
  886. * @quota - headcount quota, 1 for big buffer
  887. * returns number of buffer heads allocated, negative on error
  888. */
  889. static int get_rx_bufs(struct vhost_virtqueue *vq,
  890. struct vring_used_elem *heads,
  891. int datalen,
  892. unsigned *iovcount,
  893. struct vhost_log *log,
  894. unsigned *log_num,
  895. unsigned int quota)
  896. {
  897. unsigned int out, in;
  898. int seg = 0;
  899. int headcount = 0;
  900. unsigned d;
  901. int r, nlogs = 0;
  902. /* len is always initialized before use since we are always called with
  903. * datalen > 0.
  904. */
  905. u32 len;
  906. while (datalen > 0 && headcount < quota) {
  907. if (unlikely(seg >= UIO_MAXIOV)) {
  908. r = -ENOBUFS;
  909. goto err;
  910. }
  911. r = vhost_get_vq_desc(vq, vq->iov + seg,
  912. ARRAY_SIZE(vq->iov) - seg, &out,
  913. &in, log, log_num);
  914. if (unlikely(r < 0))
  915. goto err;
  916. d = r;
  917. if (d == vq->num) {
  918. r = 0;
  919. goto err;
  920. }
  921. if (unlikely(out || in <= 0)) {
  922. vq_err(vq, "unexpected descriptor format for RX: "
  923. "out %d, in %d\n", out, in);
  924. r = -EINVAL;
  925. goto err;
  926. }
  927. if (unlikely(log)) {
  928. nlogs += *log_num;
  929. log += *log_num;
  930. }
  931. heads[headcount].id = cpu_to_vhost32(vq, d);
  932. len = iov_length(vq->iov + seg, in);
  933. heads[headcount].len = cpu_to_vhost32(vq, len);
  934. datalen -= len;
  935. ++headcount;
  936. seg += in;
  937. }
  938. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  939. *iovcount = seg;
  940. if (unlikely(log))
  941. *log_num = nlogs;
  942. /* Detect overrun */
  943. if (unlikely(datalen > 0)) {
  944. r = UIO_MAXIOV + 1;
  945. goto err;
  946. }
  947. return headcount;
  948. err:
  949. vhost_discard_vq_desc(vq, headcount);
  950. return r;
  951. }
  952. /* Expects to be always run from workqueue - which acts as
  953. * read-size critical section for our kind of RCU. */
  954. static void handle_rx(struct vhost_net *net)
  955. {
  956. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  957. struct vhost_virtqueue *vq = &nvq->vq;
  958. unsigned in, log;
  959. struct vhost_log *vq_log;
  960. struct msghdr msg = {
  961. .msg_name = NULL,
  962. .msg_namelen = 0,
  963. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  964. .msg_controllen = 0,
  965. .msg_flags = MSG_DONTWAIT,
  966. };
  967. struct virtio_net_hdr hdr = {
  968. .flags = 0,
  969. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  970. };
  971. size_t total_len = 0;
  972. int err, mergeable;
  973. s16 headcount;
  974. size_t vhost_hlen, sock_hlen;
  975. size_t vhost_len, sock_len;
  976. bool busyloop_intr = false;
  977. struct socket *sock;
  978. struct iov_iter fixup;
  979. __virtio16 num_buffers;
  980. int recv_pkts = 0;
  981. mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_RX);
  982. sock = vhost_vq_get_backend(vq);
  983. if (!sock)
  984. goto out;
  985. if (!vq_meta_prefetch(vq))
  986. goto out;
  987. vhost_disable_notify(&net->dev, vq);
  988. vhost_net_disable_vq(net, vq);
  989. vhost_hlen = nvq->vhost_hlen;
  990. sock_hlen = nvq->sock_hlen;
  991. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  992. vq->log : NULL;
  993. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  994. do {
  995. sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
  996. &busyloop_intr);
  997. if (!sock_len)
  998. break;
  999. sock_len += sock_hlen;
  1000. vhost_len = sock_len + vhost_hlen;
  1001. headcount = get_rx_bufs(vq, vq->heads + nvq->done_idx,
  1002. vhost_len, &in, vq_log, &log,
  1003. likely(mergeable) ? UIO_MAXIOV : 1);
  1004. /* On error, stop handling until the next kick. */
  1005. if (unlikely(headcount < 0))
  1006. goto out;
  1007. /* OK, now we need to know about added descriptors. */
  1008. if (!headcount) {
  1009. if (unlikely(busyloop_intr)) {
  1010. vhost_poll_queue(&vq->poll);
  1011. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  1012. /* They have slipped one in as we were
  1013. * doing that: check again. */
  1014. vhost_disable_notify(&net->dev, vq);
  1015. continue;
  1016. }
  1017. /* Nothing new? Wait for eventfd to tell us
  1018. * they refilled. */
  1019. goto out;
  1020. }
  1021. busyloop_intr = false;
  1022. if (nvq->rx_ring)
  1023. msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
  1024. /* On overrun, truncate and discard */
  1025. if (unlikely(headcount > UIO_MAXIOV)) {
  1026. iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, 1, 1);
  1027. err = sock->ops->recvmsg(sock, &msg,
  1028. 1, MSG_DONTWAIT | MSG_TRUNC);
  1029. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  1030. continue;
  1031. }
  1032. /* We don't need to be notified again. */
  1033. iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, in, vhost_len);
  1034. fixup = msg.msg_iter;
  1035. if (unlikely((vhost_hlen))) {
  1036. /* We will supply the header ourselves
  1037. * TODO: support TSO.
  1038. */
  1039. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  1040. }
  1041. err = sock->ops->recvmsg(sock, &msg,
  1042. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  1043. /* Userspace might have consumed the packet meanwhile:
  1044. * it's not supposed to do this usually, but might be hard
  1045. * to prevent. Discard data we got (if any) and keep going. */
  1046. if (unlikely(err != sock_len)) {
  1047. pr_debug("Discarded rx packet: "
  1048. " len %d, expected %zd\n", err, sock_len);
  1049. vhost_discard_vq_desc(vq, headcount);
  1050. continue;
  1051. }
  1052. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  1053. if (unlikely(vhost_hlen)) {
  1054. if (copy_to_iter(&hdr, sizeof(hdr),
  1055. &fixup) != sizeof(hdr)) {
  1056. vq_err(vq, "Unable to write vnet_hdr "
  1057. "at addr %p\n", vq->iov->iov_base);
  1058. goto out;
  1059. }
  1060. } else {
  1061. /* Header came from socket; we'll need to patch
  1062. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  1063. */
  1064. iov_iter_advance(&fixup, sizeof(hdr));
  1065. }
  1066. /* TODO: Should check and handle checksum. */
  1067. num_buffers = cpu_to_vhost16(vq, headcount);
  1068. if (likely(mergeable) &&
  1069. copy_to_iter(&num_buffers, sizeof num_buffers,
  1070. &fixup) != sizeof num_buffers) {
  1071. vq_err(vq, "Failed num_buffers write");
  1072. vhost_discard_vq_desc(vq, headcount);
  1073. goto out;
  1074. }
  1075. nvq->done_idx += headcount;
  1076. if (nvq->done_idx > VHOST_NET_BATCH)
  1077. vhost_net_signal_used(nvq);
  1078. if (unlikely(vq_log))
  1079. vhost_log_write(vq, vq_log, log, vhost_len,
  1080. vq->iov, in);
  1081. total_len += vhost_len;
  1082. } while (likely(!vhost_exceeds_weight(vq, ++recv_pkts, total_len)));
  1083. if (unlikely(busyloop_intr))
  1084. vhost_poll_queue(&vq->poll);
  1085. else if (!sock_len)
  1086. vhost_net_enable_vq(net, vq);
  1087. out:
  1088. vhost_net_signal_used(nvq);
  1089. mutex_unlock(&vq->mutex);
  1090. }
  1091. static void handle_tx_kick(struct vhost_work *work)
  1092. {
  1093. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1094. poll.work);
  1095. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  1096. handle_tx(net);
  1097. }
  1098. static void handle_rx_kick(struct vhost_work *work)
  1099. {
  1100. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1101. poll.work);
  1102. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  1103. handle_rx(net);
  1104. }
  1105. static void handle_tx_net(struct vhost_work *work)
  1106. {
  1107. struct vhost_net *net = container_of(work, struct vhost_net,
  1108. poll[VHOST_NET_VQ_TX].work);
  1109. handle_tx(net);
  1110. }
  1111. static void handle_rx_net(struct vhost_work *work)
  1112. {
  1113. struct vhost_net *net = container_of(work, struct vhost_net,
  1114. poll[VHOST_NET_VQ_RX].work);
  1115. handle_rx(net);
  1116. }
  1117. static int vhost_net_open(struct inode *inode, struct file *f)
  1118. {
  1119. struct vhost_net *n;
  1120. struct vhost_dev *dev;
  1121. struct vhost_virtqueue **vqs;
  1122. void **queue;
  1123. struct xdp_buff *xdp;
  1124. int i;
  1125. n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  1126. if (!n)
  1127. return -ENOMEM;
  1128. vqs = kmalloc_array(VHOST_NET_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
  1129. if (!vqs) {
  1130. kvfree(n);
  1131. return -ENOMEM;
  1132. }
  1133. queue = kmalloc_array(VHOST_NET_BATCH, sizeof(void *),
  1134. GFP_KERNEL);
  1135. if (!queue) {
  1136. kfree(vqs);
  1137. kvfree(n);
  1138. return -ENOMEM;
  1139. }
  1140. n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
  1141. xdp = kmalloc_array(VHOST_NET_BATCH, sizeof(*xdp), GFP_KERNEL);
  1142. if (!xdp) {
  1143. kfree(vqs);
  1144. kvfree(n);
  1145. kfree(queue);
  1146. return -ENOMEM;
  1147. }
  1148. n->vqs[VHOST_NET_VQ_TX].xdp = xdp;
  1149. dev = &n->dev;
  1150. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  1151. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  1152. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  1153. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  1154. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  1155. n->vqs[i].ubufs = NULL;
  1156. n->vqs[i].ubuf_info = NULL;
  1157. n->vqs[i].upend_idx = 0;
  1158. n->vqs[i].done_idx = 0;
  1159. n->vqs[i].batched_xdp = 0;
  1160. n->vqs[i].vhost_hlen = 0;
  1161. n->vqs[i].sock_hlen = 0;
  1162. n->vqs[i].rx_ring = NULL;
  1163. vhost_net_buf_init(&n->vqs[i].rxq);
  1164. }
  1165. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
  1166. UIO_MAXIOV + VHOST_NET_BATCH,
  1167. VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT, true,
  1168. NULL);
  1169. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
  1170. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);
  1171. f->private_data = n;
  1172. n->page_frag.page = NULL;
  1173. n->refcnt_bias = 0;
  1174. return 0;
  1175. }
  1176. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  1177. struct vhost_virtqueue *vq)
  1178. {
  1179. struct socket *sock;
  1180. struct vhost_net_virtqueue *nvq =
  1181. container_of(vq, struct vhost_net_virtqueue, vq);
  1182. mutex_lock(&vq->mutex);
  1183. sock = vhost_vq_get_backend(vq);
  1184. vhost_net_disable_vq(n, vq);
  1185. vhost_vq_set_backend(vq, NULL);
  1186. vhost_net_buf_unproduce(nvq);
  1187. nvq->rx_ring = NULL;
  1188. mutex_unlock(&vq->mutex);
  1189. return sock;
  1190. }
  1191. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  1192. struct socket **rx_sock)
  1193. {
  1194. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  1195. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  1196. }
  1197. static void vhost_net_flush(struct vhost_net *n)
  1198. {
  1199. vhost_dev_flush(&n->dev);
  1200. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  1201. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1202. n->tx_flush = true;
  1203. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1204. /* Wait for all lower device DMAs done. */
  1205. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  1206. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1207. n->tx_flush = false;
  1208. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  1209. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1210. }
  1211. }
  1212. static int vhost_net_release(struct inode *inode, struct file *f)
  1213. {
  1214. struct vhost_net *n = f->private_data;
  1215. struct socket *tx_sock;
  1216. struct socket *rx_sock;
  1217. vhost_net_stop(n, &tx_sock, &rx_sock);
  1218. vhost_net_flush(n);
  1219. vhost_dev_stop(&n->dev);
  1220. vhost_dev_cleanup(&n->dev);
  1221. vhost_net_vq_reset(n);
  1222. if (tx_sock)
  1223. sockfd_put(tx_sock);
  1224. if (rx_sock)
  1225. sockfd_put(rx_sock);
  1226. /* Make sure no callbacks are outstanding */
  1227. synchronize_rcu();
  1228. /* We do an extra flush before freeing memory,
  1229. * since jobs can re-queue themselves. */
  1230. vhost_net_flush(n);
  1231. kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
  1232. kfree(n->vqs[VHOST_NET_VQ_TX].xdp);
  1233. kfree(n->dev.vqs);
  1234. if (n->page_frag.page)
  1235. __page_frag_cache_drain(n->page_frag.page, n->refcnt_bias);
  1236. kvfree(n);
  1237. return 0;
  1238. }
  1239. static struct socket *get_raw_socket(int fd)
  1240. {
  1241. int r;
  1242. struct socket *sock = sockfd_lookup(fd, &r);
  1243. if (!sock)
  1244. return ERR_PTR(-ENOTSOCK);
  1245. /* Parameter checking */
  1246. if (sock->sk->sk_type != SOCK_RAW) {
  1247. r = -ESOCKTNOSUPPORT;
  1248. goto err;
  1249. }
  1250. if (sock->sk->sk_family != AF_PACKET) {
  1251. r = -EPFNOSUPPORT;
  1252. goto err;
  1253. }
  1254. return sock;
  1255. err:
  1256. sockfd_put(sock);
  1257. return ERR_PTR(r);
  1258. }
  1259. static struct ptr_ring *get_tap_ptr_ring(struct file *file)
  1260. {
  1261. struct ptr_ring *ring;
  1262. ring = tun_get_tx_ring(file);
  1263. if (!IS_ERR(ring))
  1264. goto out;
  1265. ring = tap_get_ptr_ring(file);
  1266. if (!IS_ERR(ring))
  1267. goto out;
  1268. ring = NULL;
  1269. out:
  1270. return ring;
  1271. }
  1272. static struct socket *get_tap_socket(int fd)
  1273. {
  1274. struct file *file = fget(fd);
  1275. struct socket *sock;
  1276. if (!file)
  1277. return ERR_PTR(-EBADF);
  1278. sock = tun_get_socket(file);
  1279. if (!IS_ERR(sock))
  1280. return sock;
  1281. sock = tap_get_socket(file);
  1282. if (IS_ERR(sock))
  1283. fput(file);
  1284. return sock;
  1285. }
  1286. static struct socket *get_socket(int fd)
  1287. {
  1288. struct socket *sock;
  1289. /* special case to disable backend */
  1290. if (fd == -1)
  1291. return NULL;
  1292. sock = get_raw_socket(fd);
  1293. if (!IS_ERR(sock))
  1294. return sock;
  1295. sock = get_tap_socket(fd);
  1296. if (!IS_ERR(sock))
  1297. return sock;
  1298. return ERR_PTR(-ENOTSOCK);
  1299. }
  1300. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  1301. {
  1302. struct socket *sock, *oldsock;
  1303. struct vhost_virtqueue *vq;
  1304. struct vhost_net_virtqueue *nvq;
  1305. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  1306. int r;
  1307. mutex_lock(&n->dev.mutex);
  1308. r = vhost_dev_check_owner(&n->dev);
  1309. if (r)
  1310. goto err;
  1311. if (index >= VHOST_NET_VQ_MAX) {
  1312. r = -ENOBUFS;
  1313. goto err;
  1314. }
  1315. vq = &n->vqs[index].vq;
  1316. nvq = &n->vqs[index];
  1317. mutex_lock(&vq->mutex);
  1318. if (fd == -1)
  1319. vhost_clear_msg(&n->dev);
  1320. /* Verify that ring has been setup correctly. */
  1321. if (!vhost_vq_access_ok(vq)) {
  1322. r = -EFAULT;
  1323. goto err_vq;
  1324. }
  1325. sock = get_socket(fd);
  1326. if (IS_ERR(sock)) {
  1327. r = PTR_ERR(sock);
  1328. goto err_vq;
  1329. }
  1330. /* start polling new socket */
  1331. oldsock = vhost_vq_get_backend(vq);
  1332. if (sock != oldsock) {
  1333. ubufs = vhost_net_ubuf_alloc(vq,
  1334. sock && vhost_sock_zcopy(sock));
  1335. if (IS_ERR(ubufs)) {
  1336. r = PTR_ERR(ubufs);
  1337. goto err_ubufs;
  1338. }
  1339. vhost_net_disable_vq(n, vq);
  1340. vhost_vq_set_backend(vq, sock);
  1341. vhost_net_buf_unproduce(nvq);
  1342. r = vhost_vq_init_access(vq);
  1343. if (r)
  1344. goto err_used;
  1345. r = vhost_net_enable_vq(n, vq);
  1346. if (r)
  1347. goto err_used;
  1348. if (index == VHOST_NET_VQ_RX) {
  1349. if (sock)
  1350. nvq->rx_ring = get_tap_ptr_ring(sock->file);
  1351. else
  1352. nvq->rx_ring = NULL;
  1353. }
  1354. oldubufs = nvq->ubufs;
  1355. nvq->ubufs = ubufs;
  1356. n->tx_packets = 0;
  1357. n->tx_zcopy_err = 0;
  1358. n->tx_flush = false;
  1359. }
  1360. mutex_unlock(&vq->mutex);
  1361. if (oldubufs) {
  1362. vhost_net_ubuf_put_wait_and_free(oldubufs);
  1363. mutex_lock(&vq->mutex);
  1364. vhost_zerocopy_signal_used(n, vq);
  1365. mutex_unlock(&vq->mutex);
  1366. }
  1367. if (oldsock) {
  1368. vhost_dev_flush(&n->dev);
  1369. sockfd_put(oldsock);
  1370. }
  1371. mutex_unlock(&n->dev.mutex);
  1372. return 0;
  1373. err_used:
  1374. vhost_vq_set_backend(vq, oldsock);
  1375. vhost_net_enable_vq(n, vq);
  1376. if (ubufs)
  1377. vhost_net_ubuf_put_wait_and_free(ubufs);
  1378. err_ubufs:
  1379. if (sock)
  1380. sockfd_put(sock);
  1381. err_vq:
  1382. mutex_unlock(&vq->mutex);
  1383. err:
  1384. mutex_unlock(&n->dev.mutex);
  1385. return r;
  1386. }
  1387. static long vhost_net_reset_owner(struct vhost_net *n)
  1388. {
  1389. struct socket *tx_sock = NULL;
  1390. struct socket *rx_sock = NULL;
  1391. long err;
  1392. struct vhost_iotlb *umem;
  1393. mutex_lock(&n->dev.mutex);
  1394. err = vhost_dev_check_owner(&n->dev);
  1395. if (err)
  1396. goto done;
  1397. umem = vhost_dev_reset_owner_prepare();
  1398. if (!umem) {
  1399. err = -ENOMEM;
  1400. goto done;
  1401. }
  1402. vhost_net_stop(n, &tx_sock, &rx_sock);
  1403. vhost_net_flush(n);
  1404. vhost_dev_stop(&n->dev);
  1405. vhost_dev_reset_owner(&n->dev, umem);
  1406. vhost_net_vq_reset(n);
  1407. done:
  1408. mutex_unlock(&n->dev.mutex);
  1409. if (tx_sock)
  1410. sockfd_put(tx_sock);
  1411. if (rx_sock)
  1412. sockfd_put(rx_sock);
  1413. return err;
  1414. }
  1415. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  1416. {
  1417. size_t vhost_hlen, sock_hlen, hdr_len;
  1418. int i;
  1419. hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  1420. (1ULL << VIRTIO_F_VERSION_1))) ?
  1421. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  1422. sizeof(struct virtio_net_hdr);
  1423. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  1424. /* vhost provides vnet_hdr */
  1425. vhost_hlen = hdr_len;
  1426. sock_hlen = 0;
  1427. } else {
  1428. /* socket provides vnet_hdr */
  1429. vhost_hlen = 0;
  1430. sock_hlen = hdr_len;
  1431. }
  1432. mutex_lock(&n->dev.mutex);
  1433. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1434. !vhost_log_access_ok(&n->dev))
  1435. goto out_unlock;
  1436. if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
  1437. if (vhost_init_device_iotlb(&n->dev, true))
  1438. goto out_unlock;
  1439. }
  1440. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  1441. mutex_lock(&n->vqs[i].vq.mutex);
  1442. n->vqs[i].vq.acked_features = features;
  1443. n->vqs[i].vhost_hlen = vhost_hlen;
  1444. n->vqs[i].sock_hlen = sock_hlen;
  1445. mutex_unlock(&n->vqs[i].vq.mutex);
  1446. }
  1447. mutex_unlock(&n->dev.mutex);
  1448. return 0;
  1449. out_unlock:
  1450. mutex_unlock(&n->dev.mutex);
  1451. return -EFAULT;
  1452. }
  1453. static long vhost_net_set_owner(struct vhost_net *n)
  1454. {
  1455. int r;
  1456. mutex_lock(&n->dev.mutex);
  1457. if (vhost_dev_has_owner(&n->dev)) {
  1458. r = -EBUSY;
  1459. goto out;
  1460. }
  1461. r = vhost_net_set_ubuf_info(n);
  1462. if (r)
  1463. goto out;
  1464. r = vhost_dev_set_owner(&n->dev);
  1465. if (r)
  1466. vhost_net_clear_ubuf_info(n);
  1467. vhost_net_flush(n);
  1468. out:
  1469. mutex_unlock(&n->dev.mutex);
  1470. return r;
  1471. }
  1472. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  1473. unsigned long arg)
  1474. {
  1475. struct vhost_net *n = f->private_data;
  1476. void __user *argp = (void __user *)arg;
  1477. u64 __user *featurep = argp;
  1478. struct vhost_vring_file backend;
  1479. u64 features;
  1480. int r;
  1481. switch (ioctl) {
  1482. case VHOST_NET_SET_BACKEND:
  1483. if (copy_from_user(&backend, argp, sizeof backend))
  1484. return -EFAULT;
  1485. return vhost_net_set_backend(n, backend.index, backend.fd);
  1486. case VHOST_GET_FEATURES:
  1487. features = VHOST_NET_FEATURES;
  1488. if (copy_to_user(featurep, &features, sizeof features))
  1489. return -EFAULT;
  1490. return 0;
  1491. case VHOST_SET_FEATURES:
  1492. if (copy_from_user(&features, featurep, sizeof features))
  1493. return -EFAULT;
  1494. if (features & ~VHOST_NET_FEATURES)
  1495. return -EOPNOTSUPP;
  1496. return vhost_net_set_features(n, features);
  1497. case VHOST_GET_BACKEND_FEATURES:
  1498. features = VHOST_NET_BACKEND_FEATURES;
  1499. if (copy_to_user(featurep, &features, sizeof(features)))
  1500. return -EFAULT;
  1501. return 0;
  1502. case VHOST_SET_BACKEND_FEATURES:
  1503. if (copy_from_user(&features, featurep, sizeof(features)))
  1504. return -EFAULT;
  1505. if (features & ~VHOST_NET_BACKEND_FEATURES)
  1506. return -EOPNOTSUPP;
  1507. vhost_set_backend_features(&n->dev, features);
  1508. return 0;
  1509. case VHOST_RESET_OWNER:
  1510. return vhost_net_reset_owner(n);
  1511. case VHOST_SET_OWNER:
  1512. return vhost_net_set_owner(n);
  1513. default:
  1514. mutex_lock(&n->dev.mutex);
  1515. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  1516. if (r == -ENOIOCTLCMD)
  1517. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  1518. else
  1519. vhost_net_flush(n);
  1520. mutex_unlock(&n->dev.mutex);
  1521. return r;
  1522. }
  1523. }
  1524. static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1525. {
  1526. struct file *file = iocb->ki_filp;
  1527. struct vhost_net *n = file->private_data;
  1528. struct vhost_dev *dev = &n->dev;
  1529. int noblock = file->f_flags & O_NONBLOCK;
  1530. return vhost_chr_read_iter(dev, to, noblock);
  1531. }
  1532. static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
  1533. struct iov_iter *from)
  1534. {
  1535. struct file *file = iocb->ki_filp;
  1536. struct vhost_net *n = file->private_data;
  1537. struct vhost_dev *dev = &n->dev;
  1538. return vhost_chr_write_iter(dev, from);
  1539. }
  1540. static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
  1541. {
  1542. struct vhost_net *n = file->private_data;
  1543. struct vhost_dev *dev = &n->dev;
  1544. return vhost_chr_poll(file, dev, wait);
  1545. }
  1546. static const struct file_operations vhost_net_fops = {
  1547. .owner = THIS_MODULE,
  1548. .release = vhost_net_release,
  1549. .read_iter = vhost_net_chr_read_iter,
  1550. .write_iter = vhost_net_chr_write_iter,
  1551. .poll = vhost_net_chr_poll,
  1552. .unlocked_ioctl = vhost_net_ioctl,
  1553. .compat_ioctl = compat_ptr_ioctl,
  1554. .open = vhost_net_open,
  1555. .llseek = noop_llseek,
  1556. };
  1557. static struct miscdevice vhost_net_misc = {
  1558. .minor = VHOST_NET_MINOR,
  1559. .name = "vhost-net",
  1560. .fops = &vhost_net_fops,
  1561. };
  1562. static int __init vhost_net_init(void)
  1563. {
  1564. if (experimental_zcopytx)
  1565. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1566. return misc_register(&vhost_net_misc);
  1567. }
  1568. module_init(vhost_net_init);
  1569. static void __exit vhost_net_exit(void)
  1570. {
  1571. misc_deregister(&vhost_net_misc);
  1572. }
  1573. module_exit(vhost_net_exit);
  1574. MODULE_VERSION("0.0.1");
  1575. MODULE_LICENSE("GPL v2");
  1576. MODULE_AUTHOR("Michael S. Tsirkin");
  1577. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1578. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1579. MODULE_ALIAS("devname:vhost-net");