net.c 47 KB

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