net.c 22 KB

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