net.c 22 KB

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