net.c 35 KB

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