net.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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/rcupdate.h>
  18. #include <linux/file.h>
  19. #include <linux/slab.h>
  20. #include <linux/net.h>
  21. #include <linux/if_packet.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/if_tun.h>
  24. #include <linux/if_macvlan.h>
  25. #include <net/sock.h>
  26. #include "vhost.h"
  27. static int experimental_zcopytx;
  28. module_param(experimental_zcopytx, int, 0444);
  29. MODULE_PARM_DESC(experimental_zcopytx, "Enable Experimental Zero Copy TX");
  30. /* Max number of bytes transferred before requeueing the job.
  31. * Using this limit prevents one virtqueue from starving others. */
  32. #define VHOST_NET_WEIGHT 0x80000
  33. /* MAX number of TX used buffers for outstanding zerocopy */
  34. #define VHOST_MAX_PEND 128
  35. #define VHOST_GOODCOPY_LEN 256
  36. enum {
  37. VHOST_NET_VQ_RX = 0,
  38. VHOST_NET_VQ_TX = 1,
  39. VHOST_NET_VQ_MAX = 2,
  40. };
  41. enum vhost_net_poll_state {
  42. VHOST_NET_POLL_DISABLED = 0,
  43. VHOST_NET_POLL_STARTED = 1,
  44. VHOST_NET_POLL_STOPPED = 2,
  45. };
  46. struct vhost_net {
  47. struct vhost_dev dev;
  48. struct vhost_virtqueue vqs[VHOST_NET_VQ_MAX];
  49. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  50. /* Tells us whether we are polling a socket for TX.
  51. * We only do this when socket buffer fills up.
  52. * Protected by tx vq lock. */
  53. enum vhost_net_poll_state tx_poll_state;
  54. };
  55. static bool vhost_sock_zcopy(struct socket *sock)
  56. {
  57. return unlikely(experimental_zcopytx) &&
  58. sock_flag(sock->sk, SOCK_ZEROCOPY);
  59. }
  60. /* Pop first len bytes from iovec. Return number of segments used. */
  61. static int move_iovec_hdr(struct iovec *from, struct iovec *to,
  62. size_t len, int iov_count)
  63. {
  64. int seg = 0;
  65. size_t size;
  66. while (len && seg < iov_count) {
  67. size = min(from->iov_len, len);
  68. to->iov_base = from->iov_base;
  69. to->iov_len = size;
  70. from->iov_len -= size;
  71. from->iov_base += size;
  72. len -= size;
  73. ++from;
  74. ++to;
  75. ++seg;
  76. }
  77. return seg;
  78. }
  79. /* Copy iovec entries for len bytes from iovec. */
  80. static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
  81. size_t len, int iovcount)
  82. {
  83. int seg = 0;
  84. size_t size;
  85. while (len && seg < iovcount) {
  86. size = min(from->iov_len, len);
  87. to->iov_base = from->iov_base;
  88. to->iov_len = size;
  89. len -= size;
  90. ++from;
  91. ++to;
  92. ++seg;
  93. }
  94. }
  95. /* Caller must have TX VQ lock */
  96. static void tx_poll_stop(struct vhost_net *net)
  97. {
  98. if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED))
  99. return;
  100. vhost_poll_stop(net->poll + VHOST_NET_VQ_TX);
  101. net->tx_poll_state = VHOST_NET_POLL_STOPPED;
  102. }
  103. /* Caller must have TX VQ lock */
  104. static void tx_poll_start(struct vhost_net *net, struct socket *sock)
  105. {
  106. if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED))
  107. return;
  108. vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file);
  109. net->tx_poll_state = VHOST_NET_POLL_STARTED;
  110. }
  111. /* Expects to be always run from workqueue - which acts as
  112. * read-size critical section for our kind of RCU. */
  113. static void handle_tx(struct vhost_net *net)
  114. {
  115. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
  116. unsigned out, in, s;
  117. int head;
  118. struct msghdr msg = {
  119. .msg_name = NULL,
  120. .msg_namelen = 0,
  121. .msg_control = NULL,
  122. .msg_controllen = 0,
  123. .msg_iov = vq->iov,
  124. .msg_flags = MSG_DONTWAIT,
  125. };
  126. size_t len, total_len = 0;
  127. int err, wmem;
  128. size_t hdr_size;
  129. struct socket *sock;
  130. struct vhost_ubuf_ref *uninitialized_var(ubufs);
  131. bool zcopy;
  132. /* TODO: check that we are running from vhost_worker? */
  133. sock = rcu_dereference_check(vq->private_data, 1);
  134. if (!sock)
  135. return;
  136. wmem = atomic_read(&sock->sk->sk_wmem_alloc);
  137. if (wmem >= sock->sk->sk_sndbuf) {
  138. mutex_lock(&vq->mutex);
  139. tx_poll_start(net, sock);
  140. mutex_unlock(&vq->mutex);
  141. return;
  142. }
  143. mutex_lock(&vq->mutex);
  144. vhost_disable_notify(&net->dev, vq);
  145. if (wmem < sock->sk->sk_sndbuf / 2)
  146. tx_poll_stop(net);
  147. hdr_size = vq->vhost_hlen;
  148. zcopy = vhost_sock_zcopy(sock);
  149. for (;;) {
  150. /* Release DMAs done buffers first */
  151. if (zcopy)
  152. vhost_zerocopy_signal_used(vq);
  153. head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
  154. ARRAY_SIZE(vq->iov),
  155. &out, &in,
  156. NULL, NULL);
  157. /* On error, stop handling until the next kick. */
  158. if (unlikely(head < 0))
  159. break;
  160. /* Nothing new? Wait for eventfd to tell us they refilled. */
  161. if (head == vq->num) {
  162. wmem = atomic_read(&sock->sk->sk_wmem_alloc);
  163. if (wmem >= sock->sk->sk_sndbuf * 3 / 4) {
  164. tx_poll_start(net, sock);
  165. set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
  166. break;
  167. }
  168. /* If more outstanding DMAs, queue the work */
  169. if (unlikely(vq->upend_idx - vq->done_idx >
  170. VHOST_MAX_PEND)) {
  171. tx_poll_start(net, sock);
  172. set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
  173. break;
  174. }
  175. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  176. vhost_disable_notify(&net->dev, vq);
  177. continue;
  178. }
  179. break;
  180. }
  181. if (in) {
  182. vq_err(vq, "Unexpected descriptor format for TX: "
  183. "out %d, int %d\n", out, in);
  184. break;
  185. }
  186. /* Skip header. TODO: support TSO. */
  187. s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
  188. msg.msg_iovlen = out;
  189. len = iov_length(vq->iov, out);
  190. /* Sanity check */
  191. if (!len) {
  192. vq_err(vq, "Unexpected header len for TX: "
  193. "%zd expected %zd\n",
  194. iov_length(vq->hdr, s), hdr_size);
  195. break;
  196. }
  197. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  198. if (zcopy) {
  199. vq->heads[vq->upend_idx].id = head;
  200. if (len < VHOST_GOODCOPY_LEN) {
  201. /* copy don't need to wait for DMA done */
  202. vq->heads[vq->upend_idx].len =
  203. VHOST_DMA_DONE_LEN;
  204. msg.msg_control = NULL;
  205. msg.msg_controllen = 0;
  206. ubufs = NULL;
  207. } else {
  208. struct ubuf_info *ubuf = &vq->ubuf_info[head];
  209. vq->heads[vq->upend_idx].len = len;
  210. ubuf->callback = vhost_zerocopy_callback;
  211. ubuf->arg = vq->ubufs;
  212. ubuf->desc = vq->upend_idx;
  213. msg.msg_control = ubuf;
  214. msg.msg_controllen = sizeof(ubuf);
  215. ubufs = vq->ubufs;
  216. kref_get(&ubufs->kref);
  217. }
  218. vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
  219. }
  220. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  221. err = sock->ops->sendmsg(NULL, sock, &msg, len);
  222. if (unlikely(err < 0)) {
  223. if (zcopy) {
  224. if (ubufs)
  225. vhost_ubuf_put(ubufs);
  226. vq->upend_idx = ((unsigned)vq->upend_idx - 1) %
  227. UIO_MAXIOV;
  228. }
  229. vhost_discard_vq_desc(vq, 1);
  230. tx_poll_start(net, sock);
  231. break;
  232. }
  233. if (err != len)
  234. pr_debug("Truncated TX packet: "
  235. " len %d != %zd\n", err, len);
  236. if (!zcopy)
  237. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  238. total_len += len;
  239. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  240. vhost_poll_queue(&vq->poll);
  241. break;
  242. }
  243. }
  244. mutex_unlock(&vq->mutex);
  245. }
  246. static int peek_head_len(struct sock *sk)
  247. {
  248. struct sk_buff *head;
  249. int len = 0;
  250. unsigned long flags;
  251. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  252. head = skb_peek(&sk->sk_receive_queue);
  253. if (likely(head))
  254. len = head->len;
  255. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  256. return len;
  257. }
  258. /* This is a multi-buffer version of vhost_get_desc, that works if
  259. * vq has read descriptors only.
  260. * @vq - the relevant virtqueue
  261. * @datalen - data length we'll be reading
  262. * @iovcount - returned count of io vectors we fill
  263. * @log - vhost log
  264. * @log_num - log offset
  265. * @quota - headcount quota, 1 for big buffer
  266. * returns number of buffer heads allocated, negative on error
  267. */
  268. static int get_rx_bufs(struct vhost_virtqueue *vq,
  269. struct vring_used_elem *heads,
  270. int datalen,
  271. unsigned *iovcount,
  272. struct vhost_log *log,
  273. unsigned *log_num,
  274. unsigned int quota)
  275. {
  276. unsigned int out, in;
  277. int seg = 0;
  278. int headcount = 0;
  279. unsigned d;
  280. int r, nlogs = 0;
  281. while (datalen > 0 && headcount < quota) {
  282. if (unlikely(seg >= UIO_MAXIOV)) {
  283. r = -ENOBUFS;
  284. goto err;
  285. }
  286. d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
  287. ARRAY_SIZE(vq->iov) - seg, &out,
  288. &in, log, log_num);
  289. if (d == vq->num) {
  290. r = 0;
  291. goto err;
  292. }
  293. if (unlikely(out || in <= 0)) {
  294. vq_err(vq, "unexpected descriptor format for RX: "
  295. "out %d, in %d\n", out, in);
  296. r = -EINVAL;
  297. goto err;
  298. }
  299. if (unlikely(log)) {
  300. nlogs += *log_num;
  301. log += *log_num;
  302. }
  303. heads[headcount].id = d;
  304. heads[headcount].len = iov_length(vq->iov + seg, in);
  305. datalen -= heads[headcount].len;
  306. ++headcount;
  307. seg += in;
  308. }
  309. heads[headcount - 1].len += datalen;
  310. *iovcount = seg;
  311. if (unlikely(log))
  312. *log_num = nlogs;
  313. return headcount;
  314. err:
  315. vhost_discard_vq_desc(vq, headcount);
  316. return r;
  317. }
  318. /* Expects to be always run from workqueue - which acts as
  319. * read-size critical section for our kind of RCU. */
  320. static void handle_rx(struct vhost_net *net)
  321. {
  322. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
  323. unsigned uninitialized_var(in), log;
  324. struct vhost_log *vq_log;
  325. struct msghdr msg = {
  326. .msg_name = NULL,
  327. .msg_namelen = 0,
  328. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  329. .msg_controllen = 0,
  330. .msg_iov = vq->iov,
  331. .msg_flags = MSG_DONTWAIT,
  332. };
  333. struct virtio_net_hdr_mrg_rxbuf hdr = {
  334. .hdr.flags = 0,
  335. .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
  336. };
  337. size_t total_len = 0;
  338. int err, headcount, mergeable;
  339. size_t vhost_hlen, sock_hlen;
  340. size_t vhost_len, sock_len;
  341. /* TODO: check that we are running from vhost_worker? */
  342. struct socket *sock = rcu_dereference_check(vq->private_data, 1);
  343. if (!sock)
  344. return;
  345. mutex_lock(&vq->mutex);
  346. vhost_disable_notify(&net->dev, vq);
  347. vhost_hlen = vq->vhost_hlen;
  348. sock_hlen = vq->sock_hlen;
  349. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  350. vq->log : NULL;
  351. mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
  352. while ((sock_len = peek_head_len(sock->sk))) {
  353. sock_len += sock_hlen;
  354. vhost_len = sock_len + vhost_hlen;
  355. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  356. &in, vq_log, &log,
  357. likely(mergeable) ? UIO_MAXIOV : 1);
  358. /* On error, stop handling until the next kick. */
  359. if (unlikely(headcount < 0))
  360. break;
  361. /* OK, now we need to know about added descriptors. */
  362. if (!headcount) {
  363. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  364. /* They have slipped one in as we were
  365. * doing that: check again. */
  366. vhost_disable_notify(&net->dev, vq);
  367. continue;
  368. }
  369. /* Nothing new? Wait for eventfd to tell us
  370. * they refilled. */
  371. break;
  372. }
  373. /* We don't need to be notified again. */
  374. if (unlikely((vhost_hlen)))
  375. /* Skip header. TODO: support TSO. */
  376. move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
  377. else
  378. /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
  379. * needed because recvmsg can modify msg_iov. */
  380. copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
  381. msg.msg_iovlen = in;
  382. err = sock->ops->recvmsg(NULL, sock, &msg,
  383. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  384. /* Userspace might have consumed the packet meanwhile:
  385. * it's not supposed to do this usually, but might be hard
  386. * to prevent. Discard data we got (if any) and keep going. */
  387. if (unlikely(err != sock_len)) {
  388. pr_debug("Discarded rx packet: "
  389. " len %d, expected %zd\n", err, sock_len);
  390. vhost_discard_vq_desc(vq, headcount);
  391. continue;
  392. }
  393. if (unlikely(vhost_hlen) &&
  394. memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
  395. vhost_hlen)) {
  396. vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
  397. vq->iov->iov_base);
  398. break;
  399. }
  400. /* TODO: Should check and handle checksum. */
  401. if (likely(mergeable) &&
  402. memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
  403. offsetof(typeof(hdr), num_buffers),
  404. sizeof hdr.num_buffers)) {
  405. vq_err(vq, "Failed num_buffers write");
  406. vhost_discard_vq_desc(vq, headcount);
  407. break;
  408. }
  409. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  410. headcount);
  411. if (unlikely(vq_log))
  412. vhost_log_write(vq, vq_log, log, vhost_len);
  413. total_len += vhost_len;
  414. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  415. vhost_poll_queue(&vq->poll);
  416. break;
  417. }
  418. }
  419. mutex_unlock(&vq->mutex);
  420. }
  421. static void handle_tx_kick(struct vhost_work *work)
  422. {
  423. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  424. poll.work);
  425. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  426. handle_tx(net);
  427. }
  428. static void handle_rx_kick(struct vhost_work *work)
  429. {
  430. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  431. poll.work);
  432. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  433. handle_rx(net);
  434. }
  435. static void handle_tx_net(struct vhost_work *work)
  436. {
  437. struct vhost_net *net = container_of(work, struct vhost_net,
  438. poll[VHOST_NET_VQ_TX].work);
  439. handle_tx(net);
  440. }
  441. static void handle_rx_net(struct vhost_work *work)
  442. {
  443. struct vhost_net *net = container_of(work, struct vhost_net,
  444. poll[VHOST_NET_VQ_RX].work);
  445. handle_rx(net);
  446. }
  447. static int vhost_net_open(struct inode *inode, struct file *f)
  448. {
  449. struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
  450. struct vhost_dev *dev;
  451. int r;
  452. if (!n)
  453. return -ENOMEM;
  454. dev = &n->dev;
  455. n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
  456. n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
  457. r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
  458. if (r < 0) {
  459. kfree(n);
  460. return r;
  461. }
  462. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  463. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  464. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  465. f->private_data = n;
  466. return 0;
  467. }
  468. static void vhost_net_disable_vq(struct vhost_net *n,
  469. struct vhost_virtqueue *vq)
  470. {
  471. if (!vq->private_data)
  472. return;
  473. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  474. tx_poll_stop(n);
  475. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  476. } else
  477. vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
  478. }
  479. static void vhost_net_enable_vq(struct vhost_net *n,
  480. struct vhost_virtqueue *vq)
  481. {
  482. struct socket *sock;
  483. sock = rcu_dereference_protected(vq->private_data,
  484. lockdep_is_held(&vq->mutex));
  485. if (!sock)
  486. return;
  487. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  488. n->tx_poll_state = VHOST_NET_POLL_STOPPED;
  489. tx_poll_start(n, sock);
  490. } else
  491. vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
  492. }
  493. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  494. struct vhost_virtqueue *vq)
  495. {
  496. struct socket *sock;
  497. mutex_lock(&vq->mutex);
  498. sock = rcu_dereference_protected(vq->private_data,
  499. lockdep_is_held(&vq->mutex));
  500. vhost_net_disable_vq(n, vq);
  501. rcu_assign_pointer(vq->private_data, NULL);
  502. mutex_unlock(&vq->mutex);
  503. return sock;
  504. }
  505. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  506. struct socket **rx_sock)
  507. {
  508. *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
  509. *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
  510. }
  511. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  512. {
  513. vhost_poll_flush(n->poll + index);
  514. vhost_poll_flush(&n->dev.vqs[index].poll);
  515. }
  516. static void vhost_net_flush(struct vhost_net *n)
  517. {
  518. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  519. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  520. }
  521. static int vhost_net_release(struct inode *inode, struct file *f)
  522. {
  523. struct vhost_net *n = f->private_data;
  524. struct socket *tx_sock;
  525. struct socket *rx_sock;
  526. vhost_net_stop(n, &tx_sock, &rx_sock);
  527. vhost_net_flush(n);
  528. vhost_dev_cleanup(&n->dev);
  529. if (tx_sock)
  530. fput(tx_sock->file);
  531. if (rx_sock)
  532. fput(rx_sock->file);
  533. /* We do an extra flush before freeing memory,
  534. * since jobs can re-queue themselves. */
  535. vhost_net_flush(n);
  536. kfree(n);
  537. return 0;
  538. }
  539. static struct socket *get_raw_socket(int fd)
  540. {
  541. struct {
  542. struct sockaddr_ll sa;
  543. char buf[MAX_ADDR_LEN];
  544. } uaddr;
  545. int uaddr_len = sizeof uaddr, r;
  546. struct socket *sock = sockfd_lookup(fd, &r);
  547. if (!sock)
  548. return ERR_PTR(-ENOTSOCK);
  549. /* Parameter checking */
  550. if (sock->sk->sk_type != SOCK_RAW) {
  551. r = -ESOCKTNOSUPPORT;
  552. goto err;
  553. }
  554. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  555. &uaddr_len, 0);
  556. if (r)
  557. goto err;
  558. if (uaddr.sa.sll_family != AF_PACKET) {
  559. r = -EPFNOSUPPORT;
  560. goto err;
  561. }
  562. return sock;
  563. err:
  564. fput(sock->file);
  565. return ERR_PTR(r);
  566. }
  567. static struct socket *get_tap_socket(int fd)
  568. {
  569. struct file *file = fget(fd);
  570. struct socket *sock;
  571. if (!file)
  572. return ERR_PTR(-EBADF);
  573. sock = tun_get_socket(file);
  574. if (!IS_ERR(sock))
  575. return sock;
  576. sock = macvtap_get_socket(file);
  577. if (IS_ERR(sock))
  578. fput(file);
  579. return sock;
  580. }
  581. static struct socket *get_socket(int fd)
  582. {
  583. struct socket *sock;
  584. /* special case to disable backend */
  585. if (fd == -1)
  586. return NULL;
  587. sock = get_raw_socket(fd);
  588. if (!IS_ERR(sock))
  589. return sock;
  590. sock = get_tap_socket(fd);
  591. if (!IS_ERR(sock))
  592. return sock;
  593. return ERR_PTR(-ENOTSOCK);
  594. }
  595. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  596. {
  597. struct socket *sock, *oldsock;
  598. struct vhost_virtqueue *vq;
  599. struct vhost_ubuf_ref *ubufs, *oldubufs = NULL;
  600. int r;
  601. mutex_lock(&n->dev.mutex);
  602. r = vhost_dev_check_owner(&n->dev);
  603. if (r)
  604. goto err;
  605. if (index >= VHOST_NET_VQ_MAX) {
  606. r = -ENOBUFS;
  607. goto err;
  608. }
  609. vq = n->vqs + index;
  610. mutex_lock(&vq->mutex);
  611. /* Verify that ring has been setup correctly. */
  612. if (!vhost_vq_access_ok(vq)) {
  613. r = -EFAULT;
  614. goto err_vq;
  615. }
  616. sock = get_socket(fd);
  617. if (IS_ERR(sock)) {
  618. r = PTR_ERR(sock);
  619. goto err_vq;
  620. }
  621. /* start polling new socket */
  622. oldsock = rcu_dereference_protected(vq->private_data,
  623. lockdep_is_held(&vq->mutex));
  624. if (sock != oldsock) {
  625. ubufs = vhost_ubuf_alloc(vq, sock && vhost_sock_zcopy(sock));
  626. if (IS_ERR(ubufs)) {
  627. r = PTR_ERR(ubufs);
  628. goto err_ubufs;
  629. }
  630. oldubufs = vq->ubufs;
  631. vq->ubufs = ubufs;
  632. vhost_net_disable_vq(n, vq);
  633. rcu_assign_pointer(vq->private_data, sock);
  634. vhost_net_enable_vq(n, vq);
  635. r = vhost_init_used(vq);
  636. if (r)
  637. goto err_vq;
  638. }
  639. mutex_unlock(&vq->mutex);
  640. if (oldubufs) {
  641. vhost_ubuf_put_and_wait(oldubufs);
  642. mutex_lock(&vq->mutex);
  643. vhost_zerocopy_signal_used(vq);
  644. mutex_unlock(&vq->mutex);
  645. }
  646. if (oldsock) {
  647. vhost_net_flush_vq(n, index);
  648. fput(oldsock->file);
  649. }
  650. mutex_unlock(&n->dev.mutex);
  651. return 0;
  652. err_ubufs:
  653. fput(sock->file);
  654. err_vq:
  655. mutex_unlock(&vq->mutex);
  656. err:
  657. mutex_unlock(&n->dev.mutex);
  658. return r;
  659. }
  660. static long vhost_net_reset_owner(struct vhost_net *n)
  661. {
  662. struct socket *tx_sock = NULL;
  663. struct socket *rx_sock = NULL;
  664. long err;
  665. mutex_lock(&n->dev.mutex);
  666. err = vhost_dev_check_owner(&n->dev);
  667. if (err)
  668. goto done;
  669. vhost_net_stop(n, &tx_sock, &rx_sock);
  670. vhost_net_flush(n);
  671. err = vhost_dev_reset_owner(&n->dev);
  672. done:
  673. mutex_unlock(&n->dev.mutex);
  674. if (tx_sock)
  675. fput(tx_sock->file);
  676. if (rx_sock)
  677. fput(rx_sock->file);
  678. return err;
  679. }
  680. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  681. {
  682. size_t vhost_hlen, sock_hlen, hdr_len;
  683. int i;
  684. hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
  685. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  686. sizeof(struct virtio_net_hdr);
  687. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  688. /* vhost provides vnet_hdr */
  689. vhost_hlen = hdr_len;
  690. sock_hlen = 0;
  691. } else {
  692. /* socket provides vnet_hdr */
  693. vhost_hlen = 0;
  694. sock_hlen = hdr_len;
  695. }
  696. mutex_lock(&n->dev.mutex);
  697. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  698. !vhost_log_access_ok(&n->dev)) {
  699. mutex_unlock(&n->dev.mutex);
  700. return -EFAULT;
  701. }
  702. n->dev.acked_features = features;
  703. smp_wmb();
  704. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  705. mutex_lock(&n->vqs[i].mutex);
  706. n->vqs[i].vhost_hlen = vhost_hlen;
  707. n->vqs[i].sock_hlen = sock_hlen;
  708. mutex_unlock(&n->vqs[i].mutex);
  709. }
  710. vhost_net_flush(n);
  711. mutex_unlock(&n->dev.mutex);
  712. return 0;
  713. }
  714. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  715. unsigned long arg)
  716. {
  717. struct vhost_net *n = f->private_data;
  718. void __user *argp = (void __user *)arg;
  719. u64 __user *featurep = argp;
  720. struct vhost_vring_file backend;
  721. u64 features;
  722. int r;
  723. switch (ioctl) {
  724. case VHOST_NET_SET_BACKEND:
  725. if (copy_from_user(&backend, argp, sizeof backend))
  726. return -EFAULT;
  727. return vhost_net_set_backend(n, backend.index, backend.fd);
  728. case VHOST_GET_FEATURES:
  729. features = VHOST_FEATURES;
  730. if (copy_to_user(featurep, &features, sizeof features))
  731. return -EFAULT;
  732. return 0;
  733. case VHOST_SET_FEATURES:
  734. if (copy_from_user(&features, featurep, sizeof features))
  735. return -EFAULT;
  736. if (features & ~VHOST_FEATURES)
  737. return -EOPNOTSUPP;
  738. return vhost_net_set_features(n, features);
  739. case VHOST_RESET_OWNER:
  740. return vhost_net_reset_owner(n);
  741. default:
  742. mutex_lock(&n->dev.mutex);
  743. r = vhost_dev_ioctl(&n->dev, ioctl, arg);
  744. vhost_net_flush(n);
  745. mutex_unlock(&n->dev.mutex);
  746. return r;
  747. }
  748. }
  749. #ifdef CONFIG_COMPAT
  750. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  751. unsigned long arg)
  752. {
  753. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  754. }
  755. #endif
  756. static const struct file_operations vhost_net_fops = {
  757. .owner = THIS_MODULE,
  758. .release = vhost_net_release,
  759. .unlocked_ioctl = vhost_net_ioctl,
  760. #ifdef CONFIG_COMPAT
  761. .compat_ioctl = vhost_net_compat_ioctl,
  762. #endif
  763. .open = vhost_net_open,
  764. .llseek = noop_llseek,
  765. };
  766. static struct miscdevice vhost_net_misc = {
  767. MISC_DYNAMIC_MINOR,
  768. "vhost-net",
  769. &vhost_net_fops,
  770. };
  771. static int vhost_net_init(void)
  772. {
  773. if (experimental_zcopytx)
  774. vhost_enable_zcopy(VHOST_NET_VQ_TX);
  775. return misc_register(&vhost_net_misc);
  776. }
  777. module_init(vhost_net_init);
  778. static void vhost_net_exit(void)
  779. {
  780. misc_deregister(&vhost_net_misc);
  781. }
  782. module_exit(vhost_net_exit);
  783. MODULE_VERSION("0.0.1");
  784. MODULE_LICENSE("GPL v2");
  785. MODULE_AUTHOR("Michael S. Tsirkin");
  786. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");