net.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /* Copyright (C) 2009 Red Hat, Inc.
  2. * Author: Michael S. Tsirkin <mst@redhat.com>
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2.
  5. *
  6. * virtio-net server in host kernel.
  7. */
  8. #include <linux/compat.h>
  9. #include <linux/eventfd.h>
  10. #include <linux/vhost.h>
  11. #include <linux/virtio_net.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/mutex.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/file.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched/clock.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/net.h>
  23. #include <linux/if_packet.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/if_tun.h>
  26. #include <linux/if_macvlan.h>
  27. #include <linux/if_tap.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/skb_array.h>
  30. #include <linux/skbuff.h>
  31. #include <net/sock.h>
  32. #include "vhost.h"
  33. static int experimental_zcopytx = 1;
  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 TX used buffers for outstanding zerocopy */
  41. #define VHOST_MAX_PEND 128
  42. #define VHOST_GOODCOPY_LEN 256
  43. /*
  44. * For transmit, used buffer len is unused; we override it to track buffer
  45. * status internally; used for zerocopy tx only.
  46. */
  47. /* Lower device DMA failed */
  48. #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
  49. /* Lower device DMA done */
  50. #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
  51. /* Lower device DMA in progress */
  52. #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
  53. /* Buffer unused */
  54. #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
  55. #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
  56. enum {
  57. VHOST_NET_FEATURES = VHOST_FEATURES |
  58. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  59. (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  60. (1ULL << VIRTIO_F_IOMMU_PLATFORM)
  61. };
  62. enum {
  63. VHOST_NET_VQ_RX = 0,
  64. VHOST_NET_VQ_TX = 1,
  65. VHOST_NET_VQ_MAX = 2,
  66. };
  67. struct vhost_net_ubuf_ref {
  68. /* refcount follows semantics similar to kref:
  69. * 0: object is released
  70. * 1: no outstanding ubufs
  71. * >1: outstanding ubufs
  72. */
  73. atomic_t refcount;
  74. wait_queue_head_t wait;
  75. struct vhost_virtqueue *vq;
  76. };
  77. #define VHOST_RX_BATCH 64
  78. struct vhost_net_buf {
  79. void **queue;
  80. int tail;
  81. int head;
  82. };
  83. struct vhost_net_virtqueue {
  84. struct vhost_virtqueue vq;
  85. size_t vhost_hlen;
  86. size_t sock_hlen;
  87. /* vhost zerocopy support fields below: */
  88. /* last used idx for outstanding DMA zerocopy buffers */
  89. int upend_idx;
  90. /* first used idx for DMA done zerocopy buffers */
  91. int done_idx;
  92. /* an array of userspace buffers info */
  93. struct ubuf_info *ubuf_info;
  94. /* Reference counting for outstanding ubufs.
  95. * Protected by vq mutex. Writers must also take device mutex. */
  96. struct vhost_net_ubuf_ref *ubufs;
  97. struct ptr_ring *rx_ring;
  98. struct vhost_net_buf rxq;
  99. };
  100. struct vhost_net {
  101. struct vhost_dev dev;
  102. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  103. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  104. /* Number of TX recently submitted.
  105. * Protected by tx vq lock. */
  106. unsigned tx_packets;
  107. /* Number of times zerocopy TX recently failed.
  108. * Protected by tx vq lock. */
  109. unsigned tx_zcopy_err;
  110. /* Flush in progress. Protected by tx vq lock. */
  111. bool tx_flush;
  112. };
  113. static unsigned vhost_net_zcopy_mask __read_mostly;
  114. static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
  115. {
  116. if (rxq->tail != rxq->head)
  117. return rxq->queue[rxq->head];
  118. else
  119. return NULL;
  120. }
  121. static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
  122. {
  123. return rxq->tail - rxq->head;
  124. }
  125. static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
  126. {
  127. return rxq->tail == rxq->head;
  128. }
  129. static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
  130. {
  131. void *ret = vhost_net_buf_get_ptr(rxq);
  132. ++rxq->head;
  133. return ret;
  134. }
  135. static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
  136. {
  137. struct vhost_net_buf *rxq = &nvq->rxq;
  138. rxq->head = 0;
  139. rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
  140. VHOST_RX_BATCH);
  141. return rxq->tail;
  142. }
  143. static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
  144. {
  145. struct vhost_net_buf *rxq = &nvq->rxq;
  146. if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
  147. ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
  148. vhost_net_buf_get_size(rxq),
  149. __skb_array_destroy_skb);
  150. rxq->head = rxq->tail = 0;
  151. }
  152. }
  153. static int vhost_net_buf_peek_len(void *ptr)
  154. {
  155. if (tun_is_xdp_buff(ptr)) {
  156. struct xdp_buff *xdp = tun_ptr_to_xdp(ptr);
  157. return xdp->data_end - xdp->data;
  158. }
  159. return __skb_array_len_with_tag(ptr);
  160. }
  161. static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
  162. {
  163. struct vhost_net_buf *rxq = &nvq->rxq;
  164. if (!vhost_net_buf_is_empty(rxq))
  165. goto out;
  166. if (!vhost_net_buf_produce(nvq))
  167. return 0;
  168. out:
  169. return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq));
  170. }
  171. static void vhost_net_buf_init(struct vhost_net_buf *rxq)
  172. {
  173. rxq->head = rxq->tail = 0;
  174. }
  175. static void vhost_net_enable_zcopy(int vq)
  176. {
  177. vhost_net_zcopy_mask |= 0x1 << vq;
  178. }
  179. static struct vhost_net_ubuf_ref *
  180. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  181. {
  182. struct vhost_net_ubuf_ref *ubufs;
  183. /* No zero copy backend? Nothing to count. */
  184. if (!zcopy)
  185. return NULL;
  186. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  187. if (!ubufs)
  188. return ERR_PTR(-ENOMEM);
  189. atomic_set(&ubufs->refcount, 1);
  190. init_waitqueue_head(&ubufs->wait);
  191. ubufs->vq = vq;
  192. return ubufs;
  193. }
  194. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  195. {
  196. int r = atomic_sub_return(1, &ubufs->refcount);
  197. if (unlikely(!r))
  198. wake_up(&ubufs->wait);
  199. return r;
  200. }
  201. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  202. {
  203. vhost_net_ubuf_put(ubufs);
  204. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  205. }
  206. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  207. {
  208. vhost_net_ubuf_put_and_wait(ubufs);
  209. kfree(ubufs);
  210. }
  211. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  212. {
  213. int i;
  214. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  215. kfree(n->vqs[i].ubuf_info);
  216. n->vqs[i].ubuf_info = NULL;
  217. }
  218. }
  219. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  220. {
  221. bool zcopy;
  222. int i;
  223. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  224. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  225. if (!zcopy)
  226. continue;
  227. n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
  228. UIO_MAXIOV, GFP_KERNEL);
  229. if (!n->vqs[i].ubuf_info)
  230. goto err;
  231. }
  232. return 0;
  233. err:
  234. vhost_net_clear_ubuf_info(n);
  235. return -ENOMEM;
  236. }
  237. static void vhost_net_vq_reset(struct vhost_net *n)
  238. {
  239. int i;
  240. vhost_net_clear_ubuf_info(n);
  241. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  242. n->vqs[i].done_idx = 0;
  243. n->vqs[i].upend_idx = 0;
  244. n->vqs[i].ubufs = NULL;
  245. n->vqs[i].vhost_hlen = 0;
  246. n->vqs[i].sock_hlen = 0;
  247. vhost_net_buf_init(&n->vqs[i].rxq);
  248. }
  249. }
  250. static void vhost_net_tx_packet(struct vhost_net *net)
  251. {
  252. ++net->tx_packets;
  253. if (net->tx_packets < 1024)
  254. return;
  255. net->tx_packets = 0;
  256. net->tx_zcopy_err = 0;
  257. }
  258. static void vhost_net_tx_err(struct vhost_net *net)
  259. {
  260. ++net->tx_zcopy_err;
  261. }
  262. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  263. {
  264. /* TX flush waits for outstanding DMAs to be done.
  265. * Don't start new DMAs.
  266. */
  267. return !net->tx_flush &&
  268. net->tx_packets / 64 >= net->tx_zcopy_err;
  269. }
  270. static bool vhost_sock_zcopy(struct socket *sock)
  271. {
  272. return unlikely(experimental_zcopytx) &&
  273. sock_flag(sock->sk, SOCK_ZEROCOPY);
  274. }
  275. /* In case of DMA done not in order in lower device driver for some reason.
  276. * upend_idx is used to track end of used idx, done_idx is used to track head
  277. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  278. * guest used idx.
  279. */
  280. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  281. struct vhost_virtqueue *vq)
  282. {
  283. struct vhost_net_virtqueue *nvq =
  284. container_of(vq, struct vhost_net_virtqueue, vq);
  285. int i, add;
  286. int j = 0;
  287. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  288. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  289. vhost_net_tx_err(net);
  290. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  291. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  292. ++j;
  293. } else
  294. break;
  295. }
  296. while (j) {
  297. add = min(UIO_MAXIOV - nvq->done_idx, j);
  298. vhost_add_used_and_signal_n(vq->dev, vq,
  299. &vq->heads[nvq->done_idx], add);
  300. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  301. j -= add;
  302. }
  303. }
  304. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  305. {
  306. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  307. struct vhost_virtqueue *vq = ubufs->vq;
  308. int cnt;
  309. rcu_read_lock_bh();
  310. /* set len to mark this desc buffers done DMA */
  311. vq->heads[ubuf->desc].len = success ?
  312. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  313. cnt = vhost_net_ubuf_put(ubufs);
  314. /*
  315. * Trigger polling thread if guest stopped submitting new buffers:
  316. * in this case, the refcount after decrement will eventually reach 1.
  317. * We also trigger polling periodically after each 16 packets
  318. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  319. * less than 10% of times).
  320. */
  321. if (cnt <= 1 || !(cnt % 16))
  322. vhost_poll_queue(&vq->poll);
  323. rcu_read_unlock_bh();
  324. }
  325. static inline unsigned long busy_clock(void)
  326. {
  327. return local_clock() >> 10;
  328. }
  329. static bool vhost_can_busy_poll(struct vhost_dev *dev,
  330. unsigned long endtime)
  331. {
  332. return likely(!need_resched()) &&
  333. likely(!time_after(busy_clock(), endtime)) &&
  334. likely(!signal_pending(current)) &&
  335. !vhost_has_work(dev);
  336. }
  337. static void vhost_net_disable_vq(struct vhost_net *n,
  338. struct vhost_virtqueue *vq)
  339. {
  340. struct vhost_net_virtqueue *nvq =
  341. container_of(vq, struct vhost_net_virtqueue, vq);
  342. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  343. if (!vq->private_data)
  344. return;
  345. vhost_poll_stop(poll);
  346. }
  347. static int vhost_net_enable_vq(struct vhost_net *n,
  348. struct vhost_virtqueue *vq)
  349. {
  350. struct vhost_net_virtqueue *nvq =
  351. container_of(vq, struct vhost_net_virtqueue, vq);
  352. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  353. struct socket *sock;
  354. sock = vq->private_data;
  355. if (!sock)
  356. return 0;
  357. return vhost_poll_start(poll, sock->file);
  358. }
  359. static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
  360. struct vhost_virtqueue *vq,
  361. struct iovec iov[], unsigned int iov_size,
  362. unsigned int *out_num, unsigned int *in_num)
  363. {
  364. unsigned long uninitialized_var(endtime);
  365. int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  366. out_num, in_num, NULL, NULL);
  367. if (r == vq->num && vq->busyloop_timeout) {
  368. preempt_disable();
  369. endtime = busy_clock() + vq->busyloop_timeout;
  370. while (vhost_can_busy_poll(vq->dev, endtime) &&
  371. vhost_vq_avail_empty(vq->dev, vq))
  372. cpu_relax();
  373. preempt_enable();
  374. r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  375. out_num, in_num, NULL, NULL);
  376. }
  377. return r;
  378. }
  379. static bool vhost_exceeds_maxpend(struct vhost_net *net)
  380. {
  381. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  382. struct vhost_virtqueue *vq = &nvq->vq;
  383. return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
  384. min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
  385. }
  386. /* Expects to be always run from workqueue - which acts as
  387. * read-size critical section for our kind of RCU. */
  388. static void handle_tx(struct vhost_net *net)
  389. {
  390. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  391. struct vhost_virtqueue *vq = &nvq->vq;
  392. unsigned out, in;
  393. int head;
  394. struct msghdr msg = {
  395. .msg_name = NULL,
  396. .msg_namelen = 0,
  397. .msg_control = NULL,
  398. .msg_controllen = 0,
  399. .msg_flags = MSG_DONTWAIT,
  400. };
  401. size_t len, total_len = 0;
  402. int err;
  403. size_t hdr_size;
  404. struct socket *sock;
  405. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  406. bool zcopy, zcopy_used;
  407. mutex_lock(&vq->mutex);
  408. sock = vq->private_data;
  409. if (!sock)
  410. goto out;
  411. if (!vq_iotlb_prefetch(vq))
  412. goto out;
  413. vhost_disable_notify(&net->dev, vq);
  414. vhost_net_disable_vq(net, vq);
  415. hdr_size = nvq->vhost_hlen;
  416. zcopy = nvq->ubufs;
  417. for (;;) {
  418. /* Release DMAs done buffers first */
  419. if (zcopy)
  420. vhost_zerocopy_signal_used(net, vq);
  421. head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
  422. ARRAY_SIZE(vq->iov),
  423. &out, &in);
  424. /* On error, stop handling until the next kick. */
  425. if (unlikely(head < 0))
  426. break;
  427. /* Nothing new? Wait for eventfd to tell us they refilled. */
  428. if (head == vq->num) {
  429. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  430. vhost_disable_notify(&net->dev, vq);
  431. continue;
  432. }
  433. break;
  434. }
  435. if (in) {
  436. vq_err(vq, "Unexpected descriptor format for TX: "
  437. "out %d, int %d\n", out, in);
  438. break;
  439. }
  440. /* Skip header. TODO: support TSO. */
  441. len = iov_length(vq->iov, out);
  442. iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
  443. iov_iter_advance(&msg.msg_iter, hdr_size);
  444. /* Sanity check */
  445. if (!msg_data_left(&msg)) {
  446. vq_err(vq, "Unexpected header len for TX: "
  447. "%zd expected %zd\n",
  448. len, hdr_size);
  449. break;
  450. }
  451. len = msg_data_left(&msg);
  452. zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
  453. && !vhost_exceeds_maxpend(net)
  454. && vhost_net_tx_select_zcopy(net);
  455. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  456. if (zcopy_used) {
  457. struct ubuf_info *ubuf;
  458. ubuf = nvq->ubuf_info + nvq->upend_idx;
  459. vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
  460. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  461. ubuf->callback = vhost_zerocopy_callback;
  462. ubuf->ctx = nvq->ubufs;
  463. ubuf->desc = nvq->upend_idx;
  464. refcount_set(&ubuf->refcnt, 1);
  465. msg.msg_control = ubuf;
  466. msg.msg_controllen = sizeof(ubuf);
  467. ubufs = nvq->ubufs;
  468. atomic_inc(&ubufs->refcount);
  469. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  470. } else {
  471. msg.msg_control = NULL;
  472. ubufs = NULL;
  473. }
  474. total_len += len;
  475. if (total_len < VHOST_NET_WEIGHT &&
  476. !vhost_vq_avail_empty(&net->dev, vq) &&
  477. likely(!vhost_exceeds_maxpend(net))) {
  478. msg.msg_flags |= MSG_MORE;
  479. } else {
  480. msg.msg_flags &= ~MSG_MORE;
  481. }
  482. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  483. err = sock->ops->sendmsg(sock, &msg, len);
  484. if (unlikely(err < 0)) {
  485. if (zcopy_used) {
  486. vhost_net_ubuf_put(ubufs);
  487. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  488. % UIO_MAXIOV;
  489. }
  490. vhost_discard_vq_desc(vq, 1);
  491. vhost_net_enable_vq(net, vq);
  492. break;
  493. }
  494. if (err != len)
  495. pr_debug("Truncated TX packet: "
  496. " len %d != %zd\n", err, len);
  497. if (!zcopy_used)
  498. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  499. else
  500. vhost_zerocopy_signal_used(net, vq);
  501. vhost_net_tx_packet(net);
  502. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  503. vhost_poll_queue(&vq->poll);
  504. break;
  505. }
  506. }
  507. out:
  508. mutex_unlock(&vq->mutex);
  509. }
  510. static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
  511. {
  512. struct sk_buff *head;
  513. int len = 0;
  514. unsigned long flags;
  515. if (rvq->rx_ring)
  516. return vhost_net_buf_peek(rvq);
  517. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  518. head = skb_peek(&sk->sk_receive_queue);
  519. if (likely(head)) {
  520. len = head->len;
  521. if (skb_vlan_tag_present(head))
  522. len += VLAN_HLEN;
  523. }
  524. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  525. return len;
  526. }
  527. static int sk_has_rx_data(struct sock *sk)
  528. {
  529. struct socket *sock = sk->sk_socket;
  530. if (sock->ops->peek_len)
  531. return sock->ops->peek_len(sock);
  532. return skb_queue_empty(&sk->sk_receive_queue);
  533. }
  534. static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk)
  535. {
  536. struct vhost_net_virtqueue *rvq = &net->vqs[VHOST_NET_VQ_RX];
  537. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  538. struct vhost_virtqueue *vq = &nvq->vq;
  539. unsigned long uninitialized_var(endtime);
  540. int len = peek_head_len(rvq, sk);
  541. if (!len && vq->busyloop_timeout) {
  542. /* Both tx vq and rx socket were polled here */
  543. mutex_lock(&vq->mutex);
  544. vhost_disable_notify(&net->dev, vq);
  545. preempt_disable();
  546. endtime = busy_clock() + vq->busyloop_timeout;
  547. while (vhost_can_busy_poll(&net->dev, endtime) &&
  548. !sk_has_rx_data(sk) &&
  549. vhost_vq_avail_empty(&net->dev, vq))
  550. cpu_relax();
  551. preempt_enable();
  552. if (!vhost_vq_avail_empty(&net->dev, vq))
  553. vhost_poll_queue(&vq->poll);
  554. else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  555. vhost_disable_notify(&net->dev, vq);
  556. vhost_poll_queue(&vq->poll);
  557. }
  558. mutex_unlock(&vq->mutex);
  559. len = peek_head_len(rvq, sk);
  560. }
  561. return len;
  562. }
  563. /* This is a multi-buffer version of vhost_get_desc, that works if
  564. * vq has read descriptors only.
  565. * @vq - the relevant virtqueue
  566. * @datalen - data length we'll be reading
  567. * @iovcount - returned count of io vectors we fill
  568. * @log - vhost log
  569. * @log_num - log offset
  570. * @quota - headcount quota, 1 for big buffer
  571. * returns number of buffer heads allocated, negative on error
  572. */
  573. static int get_rx_bufs(struct vhost_virtqueue *vq,
  574. struct vring_used_elem *heads,
  575. int datalen,
  576. unsigned *iovcount,
  577. struct vhost_log *log,
  578. unsigned *log_num,
  579. unsigned int quota)
  580. {
  581. unsigned int out, in;
  582. int seg = 0;
  583. int headcount = 0;
  584. unsigned d;
  585. int r, nlogs = 0;
  586. /* len is always initialized before use since we are always called with
  587. * datalen > 0.
  588. */
  589. u32 uninitialized_var(len);
  590. while (datalen > 0 && headcount < quota) {
  591. if (unlikely(seg >= UIO_MAXIOV)) {
  592. r = -ENOBUFS;
  593. goto err;
  594. }
  595. r = vhost_get_vq_desc(vq, vq->iov + seg,
  596. ARRAY_SIZE(vq->iov) - seg, &out,
  597. &in, log, log_num);
  598. if (unlikely(r < 0))
  599. goto err;
  600. d = r;
  601. if (d == vq->num) {
  602. r = 0;
  603. goto err;
  604. }
  605. if (unlikely(out || in <= 0)) {
  606. vq_err(vq, "unexpected descriptor format for RX: "
  607. "out %d, in %d\n", out, in);
  608. r = -EINVAL;
  609. goto err;
  610. }
  611. if (unlikely(log)) {
  612. nlogs += *log_num;
  613. log += *log_num;
  614. }
  615. heads[headcount].id = cpu_to_vhost32(vq, d);
  616. len = iov_length(vq->iov + seg, in);
  617. heads[headcount].len = cpu_to_vhost32(vq, len);
  618. datalen -= len;
  619. ++headcount;
  620. seg += in;
  621. }
  622. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  623. *iovcount = seg;
  624. if (unlikely(log))
  625. *log_num = nlogs;
  626. /* Detect overrun */
  627. if (unlikely(datalen > 0)) {
  628. r = UIO_MAXIOV + 1;
  629. goto err;
  630. }
  631. return headcount;
  632. err:
  633. vhost_discard_vq_desc(vq, headcount);
  634. return r;
  635. }
  636. /* Expects to be always run from workqueue - which acts as
  637. * read-size critical section for our kind of RCU. */
  638. static void handle_rx(struct vhost_net *net)
  639. {
  640. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  641. struct vhost_virtqueue *vq = &nvq->vq;
  642. unsigned uninitialized_var(in), log;
  643. struct vhost_log *vq_log;
  644. struct msghdr msg = {
  645. .msg_name = NULL,
  646. .msg_namelen = 0,
  647. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  648. .msg_controllen = 0,
  649. .msg_flags = MSG_DONTWAIT,
  650. };
  651. struct virtio_net_hdr hdr = {
  652. .flags = 0,
  653. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  654. };
  655. size_t total_len = 0;
  656. int err, mergeable;
  657. s16 headcount, nheads = 0;
  658. size_t vhost_hlen, sock_hlen;
  659. size_t vhost_len, sock_len;
  660. struct socket *sock;
  661. struct iov_iter fixup;
  662. __virtio16 num_buffers;
  663. mutex_lock(&vq->mutex);
  664. sock = vq->private_data;
  665. if (!sock)
  666. goto out;
  667. if (!vq_iotlb_prefetch(vq))
  668. goto out;
  669. vhost_disable_notify(&net->dev, vq);
  670. vhost_net_disable_vq(net, vq);
  671. vhost_hlen = nvq->vhost_hlen;
  672. sock_hlen = nvq->sock_hlen;
  673. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  674. vq->log : NULL;
  675. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  676. while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
  677. sock_len += sock_hlen;
  678. vhost_len = sock_len + vhost_hlen;
  679. headcount = get_rx_bufs(vq, vq->heads + nheads, vhost_len,
  680. &in, vq_log, &log,
  681. likely(mergeable) ? UIO_MAXIOV : 1);
  682. /* On error, stop handling until the next kick. */
  683. if (unlikely(headcount < 0))
  684. goto out;
  685. /* OK, now we need to know about added descriptors. */
  686. if (!headcount) {
  687. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  688. /* They have slipped one in as we were
  689. * doing that: check again. */
  690. vhost_disable_notify(&net->dev, vq);
  691. continue;
  692. }
  693. /* Nothing new? Wait for eventfd to tell us
  694. * they refilled. */
  695. goto out;
  696. }
  697. if (nvq->rx_ring)
  698. msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
  699. /* On overrun, truncate and discard */
  700. if (unlikely(headcount > UIO_MAXIOV)) {
  701. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  702. err = sock->ops->recvmsg(sock, &msg,
  703. 1, MSG_DONTWAIT | MSG_TRUNC);
  704. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  705. continue;
  706. }
  707. /* We don't need to be notified again. */
  708. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  709. fixup = msg.msg_iter;
  710. if (unlikely((vhost_hlen))) {
  711. /* We will supply the header ourselves
  712. * TODO: support TSO.
  713. */
  714. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  715. }
  716. err = sock->ops->recvmsg(sock, &msg,
  717. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  718. /* Userspace might have consumed the packet meanwhile:
  719. * it's not supposed to do this usually, but might be hard
  720. * to prevent. Discard data we got (if any) and keep going. */
  721. if (unlikely(err != sock_len)) {
  722. pr_debug("Discarded rx packet: "
  723. " len %d, expected %zd\n", err, sock_len);
  724. vhost_discard_vq_desc(vq, headcount);
  725. continue;
  726. }
  727. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  728. if (unlikely(vhost_hlen)) {
  729. if (copy_to_iter(&hdr, sizeof(hdr),
  730. &fixup) != sizeof(hdr)) {
  731. vq_err(vq, "Unable to write vnet_hdr "
  732. "at addr %p\n", vq->iov->iov_base);
  733. goto out;
  734. }
  735. } else {
  736. /* Header came from socket; we'll need to patch
  737. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  738. */
  739. iov_iter_advance(&fixup, sizeof(hdr));
  740. }
  741. /* TODO: Should check and handle checksum. */
  742. num_buffers = cpu_to_vhost16(vq, headcount);
  743. if (likely(mergeable) &&
  744. copy_to_iter(&num_buffers, sizeof num_buffers,
  745. &fixup) != sizeof num_buffers) {
  746. vq_err(vq, "Failed num_buffers write");
  747. vhost_discard_vq_desc(vq, headcount);
  748. goto out;
  749. }
  750. nheads += headcount;
  751. if (nheads > VHOST_RX_BATCH) {
  752. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  753. nheads);
  754. nheads = 0;
  755. }
  756. if (unlikely(vq_log))
  757. vhost_log_write(vq, vq_log, log, vhost_len);
  758. total_len += vhost_len;
  759. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  760. vhost_poll_queue(&vq->poll);
  761. goto out;
  762. }
  763. }
  764. vhost_net_enable_vq(net, vq);
  765. out:
  766. if (nheads)
  767. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  768. nheads);
  769. mutex_unlock(&vq->mutex);
  770. }
  771. static void handle_tx_kick(struct vhost_work *work)
  772. {
  773. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  774. poll.work);
  775. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  776. handle_tx(net);
  777. }
  778. static void handle_rx_kick(struct vhost_work *work)
  779. {
  780. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  781. poll.work);
  782. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  783. handle_rx(net);
  784. }
  785. static void handle_tx_net(struct vhost_work *work)
  786. {
  787. struct vhost_net *net = container_of(work, struct vhost_net,
  788. poll[VHOST_NET_VQ_TX].work);
  789. handle_tx(net);
  790. }
  791. static void handle_rx_net(struct vhost_work *work)
  792. {
  793. struct vhost_net *net = container_of(work, struct vhost_net,
  794. poll[VHOST_NET_VQ_RX].work);
  795. handle_rx(net);
  796. }
  797. static int vhost_net_open(struct inode *inode, struct file *f)
  798. {
  799. struct vhost_net *n;
  800. struct vhost_dev *dev;
  801. struct vhost_virtqueue **vqs;
  802. void **queue;
  803. int i;
  804. n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  805. if (!n)
  806. return -ENOMEM;
  807. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  808. if (!vqs) {
  809. kvfree(n);
  810. return -ENOMEM;
  811. }
  812. queue = kmalloc_array(VHOST_RX_BATCH, sizeof(void *),
  813. GFP_KERNEL);
  814. if (!queue) {
  815. kfree(vqs);
  816. kvfree(n);
  817. return -ENOMEM;
  818. }
  819. n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
  820. dev = &n->dev;
  821. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  822. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  823. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  824. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  825. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  826. n->vqs[i].ubufs = NULL;
  827. n->vqs[i].ubuf_info = NULL;
  828. n->vqs[i].upend_idx = 0;
  829. n->vqs[i].done_idx = 0;
  830. n->vqs[i].vhost_hlen = 0;
  831. n->vqs[i].sock_hlen = 0;
  832. vhost_net_buf_init(&n->vqs[i].rxq);
  833. }
  834. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  835. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  836. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  837. f->private_data = n;
  838. return 0;
  839. }
  840. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  841. struct vhost_virtqueue *vq)
  842. {
  843. struct socket *sock;
  844. struct vhost_net_virtqueue *nvq =
  845. container_of(vq, struct vhost_net_virtqueue, vq);
  846. mutex_lock(&vq->mutex);
  847. sock = vq->private_data;
  848. vhost_net_disable_vq(n, vq);
  849. vq->private_data = NULL;
  850. vhost_net_buf_unproduce(nvq);
  851. mutex_unlock(&vq->mutex);
  852. return sock;
  853. }
  854. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  855. struct socket **rx_sock)
  856. {
  857. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  858. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  859. }
  860. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  861. {
  862. vhost_poll_flush(n->poll + index);
  863. vhost_poll_flush(&n->vqs[index].vq.poll);
  864. }
  865. static void vhost_net_flush(struct vhost_net *n)
  866. {
  867. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  868. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  869. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  870. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  871. n->tx_flush = true;
  872. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  873. /* Wait for all lower device DMAs done. */
  874. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  875. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  876. n->tx_flush = false;
  877. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  878. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  879. }
  880. }
  881. static int vhost_net_release(struct inode *inode, struct file *f)
  882. {
  883. struct vhost_net *n = f->private_data;
  884. struct socket *tx_sock;
  885. struct socket *rx_sock;
  886. vhost_net_stop(n, &tx_sock, &rx_sock);
  887. vhost_net_flush(n);
  888. vhost_dev_stop(&n->dev);
  889. vhost_dev_cleanup(&n->dev);
  890. vhost_net_vq_reset(n);
  891. if (tx_sock)
  892. sockfd_put(tx_sock);
  893. if (rx_sock)
  894. sockfd_put(rx_sock);
  895. /* Make sure no callbacks are outstanding */
  896. synchronize_rcu_bh();
  897. /* We do an extra flush before freeing memory,
  898. * since jobs can re-queue themselves. */
  899. vhost_net_flush(n);
  900. kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
  901. kfree(n->dev.vqs);
  902. kvfree(n);
  903. return 0;
  904. }
  905. static struct socket *get_raw_socket(int fd)
  906. {
  907. struct {
  908. struct sockaddr_ll sa;
  909. char buf[MAX_ADDR_LEN];
  910. } uaddr;
  911. int uaddr_len = sizeof uaddr, r;
  912. struct socket *sock = sockfd_lookup(fd, &r);
  913. if (!sock)
  914. return ERR_PTR(-ENOTSOCK);
  915. /* Parameter checking */
  916. if (sock->sk->sk_type != SOCK_RAW) {
  917. r = -ESOCKTNOSUPPORT;
  918. goto err;
  919. }
  920. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  921. &uaddr_len, 0);
  922. if (r)
  923. goto err;
  924. if (uaddr.sa.sll_family != AF_PACKET) {
  925. r = -EPFNOSUPPORT;
  926. goto err;
  927. }
  928. return sock;
  929. err:
  930. sockfd_put(sock);
  931. return ERR_PTR(r);
  932. }
  933. static struct ptr_ring *get_tap_ptr_ring(int fd)
  934. {
  935. struct ptr_ring *ring;
  936. struct file *file = fget(fd);
  937. if (!file)
  938. return NULL;
  939. ring = tun_get_tx_ring(file);
  940. if (!IS_ERR(ring))
  941. goto out;
  942. ring = tap_get_ptr_ring(file);
  943. if (!IS_ERR(ring))
  944. goto out;
  945. ring = NULL;
  946. out:
  947. fput(file);
  948. return ring;
  949. }
  950. static struct socket *get_tap_socket(int fd)
  951. {
  952. struct file *file = fget(fd);
  953. struct socket *sock;
  954. if (!file)
  955. return ERR_PTR(-EBADF);
  956. sock = tun_get_socket(file);
  957. if (!IS_ERR(sock))
  958. return sock;
  959. sock = tap_get_socket(file);
  960. if (IS_ERR(sock))
  961. fput(file);
  962. return sock;
  963. }
  964. static struct socket *get_socket(int fd)
  965. {
  966. struct socket *sock;
  967. /* special case to disable backend */
  968. if (fd == -1)
  969. return NULL;
  970. sock = get_raw_socket(fd);
  971. if (!IS_ERR(sock))
  972. return sock;
  973. sock = get_tap_socket(fd);
  974. if (!IS_ERR(sock))
  975. return sock;
  976. return ERR_PTR(-ENOTSOCK);
  977. }
  978. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  979. {
  980. struct socket *sock, *oldsock;
  981. struct vhost_virtqueue *vq;
  982. struct vhost_net_virtqueue *nvq;
  983. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  984. int r;
  985. mutex_lock(&n->dev.mutex);
  986. r = vhost_dev_check_owner(&n->dev);
  987. if (r)
  988. goto err;
  989. if (index >= VHOST_NET_VQ_MAX) {
  990. r = -ENOBUFS;
  991. goto err;
  992. }
  993. vq = &n->vqs[index].vq;
  994. nvq = &n->vqs[index];
  995. mutex_lock(&vq->mutex);
  996. /* Verify that ring has been setup correctly. */
  997. if (!vhost_vq_access_ok(vq)) {
  998. r = -EFAULT;
  999. goto err_vq;
  1000. }
  1001. sock = get_socket(fd);
  1002. if (IS_ERR(sock)) {
  1003. r = PTR_ERR(sock);
  1004. goto err_vq;
  1005. }
  1006. /* start polling new socket */
  1007. oldsock = vq->private_data;
  1008. if (sock != oldsock) {
  1009. ubufs = vhost_net_ubuf_alloc(vq,
  1010. sock && vhost_sock_zcopy(sock));
  1011. if (IS_ERR(ubufs)) {
  1012. r = PTR_ERR(ubufs);
  1013. goto err_ubufs;
  1014. }
  1015. vhost_net_disable_vq(n, vq);
  1016. vq->private_data = sock;
  1017. vhost_net_buf_unproduce(nvq);
  1018. if (index == VHOST_NET_VQ_RX)
  1019. nvq->rx_ring = get_tap_ptr_ring(fd);
  1020. r = vhost_vq_init_access(vq);
  1021. if (r)
  1022. goto err_used;
  1023. r = vhost_net_enable_vq(n, vq);
  1024. if (r)
  1025. goto err_used;
  1026. oldubufs = nvq->ubufs;
  1027. nvq->ubufs = ubufs;
  1028. n->tx_packets = 0;
  1029. n->tx_zcopy_err = 0;
  1030. n->tx_flush = false;
  1031. }
  1032. mutex_unlock(&vq->mutex);
  1033. if (oldubufs) {
  1034. vhost_net_ubuf_put_wait_and_free(oldubufs);
  1035. mutex_lock(&vq->mutex);
  1036. vhost_zerocopy_signal_used(n, vq);
  1037. mutex_unlock(&vq->mutex);
  1038. }
  1039. if (oldsock) {
  1040. vhost_net_flush_vq(n, index);
  1041. sockfd_put(oldsock);
  1042. }
  1043. mutex_unlock(&n->dev.mutex);
  1044. return 0;
  1045. err_used:
  1046. vq->private_data = oldsock;
  1047. vhost_net_enable_vq(n, vq);
  1048. if (ubufs)
  1049. vhost_net_ubuf_put_wait_and_free(ubufs);
  1050. err_ubufs:
  1051. sockfd_put(sock);
  1052. err_vq:
  1053. mutex_unlock(&vq->mutex);
  1054. err:
  1055. mutex_unlock(&n->dev.mutex);
  1056. return r;
  1057. }
  1058. static long vhost_net_reset_owner(struct vhost_net *n)
  1059. {
  1060. struct socket *tx_sock = NULL;
  1061. struct socket *rx_sock = NULL;
  1062. long err;
  1063. struct vhost_umem *umem;
  1064. mutex_lock(&n->dev.mutex);
  1065. err = vhost_dev_check_owner(&n->dev);
  1066. if (err)
  1067. goto done;
  1068. umem = vhost_dev_reset_owner_prepare();
  1069. if (!umem) {
  1070. err = -ENOMEM;
  1071. goto done;
  1072. }
  1073. vhost_net_stop(n, &tx_sock, &rx_sock);
  1074. vhost_net_flush(n);
  1075. vhost_dev_stop(&n->dev);
  1076. vhost_dev_reset_owner(&n->dev, umem);
  1077. vhost_net_vq_reset(n);
  1078. done:
  1079. mutex_unlock(&n->dev.mutex);
  1080. if (tx_sock)
  1081. sockfd_put(tx_sock);
  1082. if (rx_sock)
  1083. sockfd_put(rx_sock);
  1084. return err;
  1085. }
  1086. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  1087. {
  1088. size_t vhost_hlen, sock_hlen, hdr_len;
  1089. int i;
  1090. hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  1091. (1ULL << VIRTIO_F_VERSION_1))) ?
  1092. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  1093. sizeof(struct virtio_net_hdr);
  1094. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  1095. /* vhost provides vnet_hdr */
  1096. vhost_hlen = hdr_len;
  1097. sock_hlen = 0;
  1098. } else {
  1099. /* socket provides vnet_hdr */
  1100. vhost_hlen = 0;
  1101. sock_hlen = hdr_len;
  1102. }
  1103. mutex_lock(&n->dev.mutex);
  1104. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1105. !vhost_log_access_ok(&n->dev))
  1106. goto out_unlock;
  1107. if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
  1108. if (vhost_init_device_iotlb(&n->dev, true))
  1109. goto out_unlock;
  1110. }
  1111. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  1112. mutex_lock(&n->vqs[i].vq.mutex);
  1113. n->vqs[i].vq.acked_features = features;
  1114. n->vqs[i].vhost_hlen = vhost_hlen;
  1115. n->vqs[i].sock_hlen = sock_hlen;
  1116. mutex_unlock(&n->vqs[i].vq.mutex);
  1117. }
  1118. mutex_unlock(&n->dev.mutex);
  1119. return 0;
  1120. out_unlock:
  1121. mutex_unlock(&n->dev.mutex);
  1122. return -EFAULT;
  1123. }
  1124. static long vhost_net_set_owner(struct vhost_net *n)
  1125. {
  1126. int r;
  1127. mutex_lock(&n->dev.mutex);
  1128. if (vhost_dev_has_owner(&n->dev)) {
  1129. r = -EBUSY;
  1130. goto out;
  1131. }
  1132. r = vhost_net_set_ubuf_info(n);
  1133. if (r)
  1134. goto out;
  1135. r = vhost_dev_set_owner(&n->dev);
  1136. if (r)
  1137. vhost_net_clear_ubuf_info(n);
  1138. vhost_net_flush(n);
  1139. out:
  1140. mutex_unlock(&n->dev.mutex);
  1141. return r;
  1142. }
  1143. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  1144. unsigned long arg)
  1145. {
  1146. struct vhost_net *n = f->private_data;
  1147. void __user *argp = (void __user *)arg;
  1148. u64 __user *featurep = argp;
  1149. struct vhost_vring_file backend;
  1150. u64 features;
  1151. int r;
  1152. switch (ioctl) {
  1153. case VHOST_NET_SET_BACKEND:
  1154. if (copy_from_user(&backend, argp, sizeof backend))
  1155. return -EFAULT;
  1156. return vhost_net_set_backend(n, backend.index, backend.fd);
  1157. case VHOST_GET_FEATURES:
  1158. features = VHOST_NET_FEATURES;
  1159. if (copy_to_user(featurep, &features, sizeof features))
  1160. return -EFAULT;
  1161. return 0;
  1162. case VHOST_SET_FEATURES:
  1163. if (copy_from_user(&features, featurep, sizeof features))
  1164. return -EFAULT;
  1165. if (features & ~VHOST_NET_FEATURES)
  1166. return -EOPNOTSUPP;
  1167. return vhost_net_set_features(n, features);
  1168. case VHOST_RESET_OWNER:
  1169. return vhost_net_reset_owner(n);
  1170. case VHOST_SET_OWNER:
  1171. return vhost_net_set_owner(n);
  1172. default:
  1173. mutex_lock(&n->dev.mutex);
  1174. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  1175. if (r == -ENOIOCTLCMD)
  1176. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  1177. else
  1178. vhost_net_flush(n);
  1179. mutex_unlock(&n->dev.mutex);
  1180. return r;
  1181. }
  1182. }
  1183. #ifdef CONFIG_COMPAT
  1184. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  1185. unsigned long arg)
  1186. {
  1187. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1188. }
  1189. #endif
  1190. static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1191. {
  1192. struct file *file = iocb->ki_filp;
  1193. struct vhost_net *n = file->private_data;
  1194. struct vhost_dev *dev = &n->dev;
  1195. int noblock = file->f_flags & O_NONBLOCK;
  1196. return vhost_chr_read_iter(dev, to, noblock);
  1197. }
  1198. static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
  1199. struct iov_iter *from)
  1200. {
  1201. struct file *file = iocb->ki_filp;
  1202. struct vhost_net *n = file->private_data;
  1203. struct vhost_dev *dev = &n->dev;
  1204. return vhost_chr_write_iter(dev, from);
  1205. }
  1206. static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
  1207. {
  1208. struct vhost_net *n = file->private_data;
  1209. struct vhost_dev *dev = &n->dev;
  1210. return vhost_chr_poll(file, dev, wait);
  1211. }
  1212. static const struct file_operations vhost_net_fops = {
  1213. .owner = THIS_MODULE,
  1214. .release = vhost_net_release,
  1215. .read_iter = vhost_net_chr_read_iter,
  1216. .write_iter = vhost_net_chr_write_iter,
  1217. .poll = vhost_net_chr_poll,
  1218. .unlocked_ioctl = vhost_net_ioctl,
  1219. #ifdef CONFIG_COMPAT
  1220. .compat_ioctl = vhost_net_compat_ioctl,
  1221. #endif
  1222. .open = vhost_net_open,
  1223. .llseek = noop_llseek,
  1224. };
  1225. static struct miscdevice vhost_net_misc = {
  1226. .minor = VHOST_NET_MINOR,
  1227. .name = "vhost-net",
  1228. .fops = &vhost_net_fops,
  1229. };
  1230. static int vhost_net_init(void)
  1231. {
  1232. if (experimental_zcopytx)
  1233. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1234. return misc_register(&vhost_net_misc);
  1235. }
  1236. module_init(vhost_net_init);
  1237. static void vhost_net_exit(void)
  1238. {
  1239. misc_deregister(&vhost_net_misc);
  1240. }
  1241. module_exit(vhost_net_exit);
  1242. MODULE_VERSION("0.0.1");
  1243. MODULE_LICENSE("GPL v2");
  1244. MODULE_AUTHOR("Michael S. Tsirkin");
  1245. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1246. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1247. MODULE_ALIAS("devname:vhost-net");