net.c 22 KB

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