net.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. /* TODO: check that we are running from vhost_worker? */
  119. sock = rcu_dereference_check(vq->private_data, 1);
  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. /* TODO: check that we are running from vhost_worker? */
  284. struct socket *sock = rcu_dereference_check(vq->private_data, 1);
  285. if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
  286. return;
  287. mutex_lock(&vq->mutex);
  288. vhost_disable_notify(vq);
  289. hdr_size = vq->vhost_hlen;
  290. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  291. vq->log : NULL;
  292. for (;;) {
  293. head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
  294. ARRAY_SIZE(vq->iov),
  295. &out, &in,
  296. vq_log, &log);
  297. /* On error, stop handling until the next kick. */
  298. if (unlikely(head < 0))
  299. break;
  300. /* OK, now we need to know about added descriptors. */
  301. if (head == vq->num) {
  302. if (unlikely(vhost_enable_notify(vq))) {
  303. /* They have slipped one in as we were
  304. * doing that: check again. */
  305. vhost_disable_notify(vq);
  306. continue;
  307. }
  308. /* Nothing new? Wait for eventfd to tell us
  309. * they refilled. */
  310. break;
  311. }
  312. /* We don't need to be notified again. */
  313. if (out) {
  314. vq_err(vq, "Unexpected descriptor format for RX: "
  315. "out %d, int %d\n",
  316. out, in);
  317. break;
  318. }
  319. /* Skip header. TODO: support TSO/mergeable rx buffers. */
  320. s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
  321. msg.msg_iovlen = in;
  322. len = iov_length(vq->iov, in);
  323. /* Sanity check */
  324. if (!len) {
  325. vq_err(vq, "Unexpected header len for RX: "
  326. "%zd expected %zd\n",
  327. iov_length(vq->hdr, s), hdr_size);
  328. break;
  329. }
  330. err = sock->ops->recvmsg(NULL, sock, &msg,
  331. len, MSG_DONTWAIT | MSG_TRUNC);
  332. /* TODO: Check specific error and bomb out unless EAGAIN? */
  333. if (err < 0) {
  334. vhost_discard_vq_desc(vq, 1);
  335. break;
  336. }
  337. /* TODO: Should check and handle checksum. */
  338. if (err > len) {
  339. pr_debug("Discarded truncated rx packet: "
  340. " len %d > %zd\n", err, len);
  341. vhost_discard_vq_desc(vq, 1);
  342. continue;
  343. }
  344. len = err;
  345. err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
  346. if (err) {
  347. vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
  348. vq->iov->iov_base, err);
  349. break;
  350. }
  351. len += hdr_size;
  352. vhost_add_used_and_signal(&net->dev, vq, head, len);
  353. if (unlikely(vq_log))
  354. vhost_log_write(vq, vq_log, log, len);
  355. total_len += len;
  356. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  357. vhost_poll_queue(&vq->poll);
  358. break;
  359. }
  360. }
  361. mutex_unlock(&vq->mutex);
  362. }
  363. /* Expects to be always run from workqueue - which acts as
  364. * read-size critical section for our kind of RCU. */
  365. static void handle_rx_mergeable(struct vhost_net *net)
  366. {
  367. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
  368. unsigned uninitialized_var(in), log;
  369. struct vhost_log *vq_log;
  370. struct msghdr msg = {
  371. .msg_name = NULL,
  372. .msg_namelen = 0,
  373. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  374. .msg_controllen = 0,
  375. .msg_iov = vq->iov,
  376. .msg_flags = MSG_DONTWAIT,
  377. };
  378. struct virtio_net_hdr_mrg_rxbuf hdr = {
  379. .hdr.flags = 0,
  380. .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
  381. };
  382. size_t total_len = 0;
  383. int err, headcount;
  384. size_t vhost_hlen, sock_hlen;
  385. size_t vhost_len, sock_len;
  386. /* TODO: check that we are running from vhost_worker? */
  387. struct socket *sock = rcu_dereference_check(vq->private_data, 1);
  388. if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
  389. return;
  390. mutex_lock(&vq->mutex);
  391. vhost_disable_notify(vq);
  392. vhost_hlen = vq->vhost_hlen;
  393. sock_hlen = vq->sock_hlen;
  394. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  395. vq->log : NULL;
  396. while ((sock_len = peek_head_len(sock->sk))) {
  397. sock_len += sock_hlen;
  398. vhost_len = sock_len + vhost_hlen;
  399. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  400. &in, vq_log, &log);
  401. /* On error, stop handling until the next kick. */
  402. if (unlikely(headcount < 0))
  403. break;
  404. /* OK, now we need to know about added descriptors. */
  405. if (!headcount) {
  406. if (unlikely(vhost_enable_notify(vq))) {
  407. /* They have slipped one in as we were
  408. * doing that: check again. */
  409. vhost_disable_notify(vq);
  410. continue;
  411. }
  412. /* Nothing new? Wait for eventfd to tell us
  413. * they refilled. */
  414. break;
  415. }
  416. /* We don't need to be notified again. */
  417. if (unlikely((vhost_hlen)))
  418. /* Skip header. TODO: support TSO. */
  419. move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
  420. else
  421. /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
  422. * needed because recvmsg can modify msg_iov. */
  423. copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
  424. msg.msg_iovlen = in;
  425. err = sock->ops->recvmsg(NULL, sock, &msg,
  426. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  427. /* Userspace might have consumed the packet meanwhile:
  428. * it's not supposed to do this usually, but might be hard
  429. * to prevent. Discard data we got (if any) and keep going. */
  430. if (unlikely(err != sock_len)) {
  431. pr_debug("Discarded rx packet: "
  432. " len %d, expected %zd\n", err, sock_len);
  433. vhost_discard_vq_desc(vq, headcount);
  434. continue;
  435. }
  436. if (unlikely(vhost_hlen) &&
  437. memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
  438. vhost_hlen)) {
  439. vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
  440. vq->iov->iov_base);
  441. break;
  442. }
  443. /* TODO: Should check and handle checksum. */
  444. if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF) &&
  445. memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
  446. offsetof(typeof(hdr), num_buffers),
  447. sizeof hdr.num_buffers)) {
  448. vq_err(vq, "Failed num_buffers write");
  449. vhost_discard_vq_desc(vq, headcount);
  450. break;
  451. }
  452. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  453. headcount);
  454. if (unlikely(vq_log))
  455. vhost_log_write(vq, vq_log, log, vhost_len);
  456. total_len += vhost_len;
  457. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  458. vhost_poll_queue(&vq->poll);
  459. break;
  460. }
  461. }
  462. mutex_unlock(&vq->mutex);
  463. }
  464. static void handle_rx(struct vhost_net *net)
  465. {
  466. if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF))
  467. handle_rx_mergeable(net);
  468. else
  469. handle_rx_big(net);
  470. }
  471. static void handle_tx_kick(struct vhost_work *work)
  472. {
  473. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  474. poll.work);
  475. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  476. handle_tx(net);
  477. }
  478. static void handle_rx_kick(struct vhost_work *work)
  479. {
  480. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  481. poll.work);
  482. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  483. handle_rx(net);
  484. }
  485. static void handle_tx_net(struct vhost_work *work)
  486. {
  487. struct vhost_net *net = container_of(work, struct vhost_net,
  488. poll[VHOST_NET_VQ_TX].work);
  489. handle_tx(net);
  490. }
  491. static void handle_rx_net(struct vhost_work *work)
  492. {
  493. struct vhost_net *net = container_of(work, struct vhost_net,
  494. poll[VHOST_NET_VQ_RX].work);
  495. handle_rx(net);
  496. }
  497. static int vhost_net_open(struct inode *inode, struct file *f)
  498. {
  499. struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
  500. struct vhost_dev *dev;
  501. int r;
  502. if (!n)
  503. return -ENOMEM;
  504. dev = &n->dev;
  505. n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
  506. n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
  507. r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
  508. if (r < 0) {
  509. kfree(n);
  510. return r;
  511. }
  512. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  513. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  514. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  515. f->private_data = n;
  516. return 0;
  517. }
  518. static void vhost_net_disable_vq(struct vhost_net *n,
  519. struct vhost_virtqueue *vq)
  520. {
  521. if (!vq->private_data)
  522. return;
  523. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  524. tx_poll_stop(n);
  525. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  526. } else
  527. vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
  528. }
  529. static void vhost_net_enable_vq(struct vhost_net *n,
  530. struct vhost_virtqueue *vq)
  531. {
  532. struct socket *sock;
  533. sock = rcu_dereference_protected(vq->private_data,
  534. lockdep_is_held(&vq->mutex));
  535. if (!sock)
  536. return;
  537. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  538. n->tx_poll_state = VHOST_NET_POLL_STOPPED;
  539. tx_poll_start(n, sock);
  540. } else
  541. vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
  542. }
  543. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  544. struct vhost_virtqueue *vq)
  545. {
  546. struct socket *sock;
  547. mutex_lock(&vq->mutex);
  548. sock = rcu_dereference_protected(vq->private_data,
  549. lockdep_is_held(&vq->mutex));
  550. vhost_net_disable_vq(n, vq);
  551. rcu_assign_pointer(vq->private_data, NULL);
  552. mutex_unlock(&vq->mutex);
  553. return sock;
  554. }
  555. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  556. struct socket **rx_sock)
  557. {
  558. *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
  559. *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
  560. }
  561. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  562. {
  563. vhost_poll_flush(n->poll + index);
  564. vhost_poll_flush(&n->dev.vqs[index].poll);
  565. }
  566. static void vhost_net_flush(struct vhost_net *n)
  567. {
  568. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  569. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  570. }
  571. static int vhost_net_release(struct inode *inode, struct file *f)
  572. {
  573. struct vhost_net *n = f->private_data;
  574. struct socket *tx_sock;
  575. struct socket *rx_sock;
  576. vhost_net_stop(n, &tx_sock, &rx_sock);
  577. vhost_net_flush(n);
  578. vhost_dev_cleanup(&n->dev);
  579. if (tx_sock)
  580. fput(tx_sock->file);
  581. if (rx_sock)
  582. fput(rx_sock->file);
  583. /* We do an extra flush before freeing memory,
  584. * since jobs can re-queue themselves. */
  585. vhost_net_flush(n);
  586. kfree(n);
  587. return 0;
  588. }
  589. static struct socket *get_raw_socket(int fd)
  590. {
  591. struct {
  592. struct sockaddr_ll sa;
  593. char buf[MAX_ADDR_LEN];
  594. } uaddr;
  595. int uaddr_len = sizeof uaddr, r;
  596. struct socket *sock = sockfd_lookup(fd, &r);
  597. if (!sock)
  598. return ERR_PTR(-ENOTSOCK);
  599. /* Parameter checking */
  600. if (sock->sk->sk_type != SOCK_RAW) {
  601. r = -ESOCKTNOSUPPORT;
  602. goto err;
  603. }
  604. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  605. &uaddr_len, 0);
  606. if (r)
  607. goto err;
  608. if (uaddr.sa.sll_family != AF_PACKET) {
  609. r = -EPFNOSUPPORT;
  610. goto err;
  611. }
  612. return sock;
  613. err:
  614. fput(sock->file);
  615. return ERR_PTR(r);
  616. }
  617. static struct socket *get_tap_socket(int fd)
  618. {
  619. struct file *file = fget(fd);
  620. struct socket *sock;
  621. if (!file)
  622. return ERR_PTR(-EBADF);
  623. sock = tun_get_socket(file);
  624. if (!IS_ERR(sock))
  625. return sock;
  626. sock = macvtap_get_socket(file);
  627. if (IS_ERR(sock))
  628. fput(file);
  629. return sock;
  630. }
  631. static struct socket *get_socket(int fd)
  632. {
  633. struct socket *sock;
  634. /* special case to disable backend */
  635. if (fd == -1)
  636. return NULL;
  637. sock = get_raw_socket(fd);
  638. if (!IS_ERR(sock))
  639. return sock;
  640. sock = get_tap_socket(fd);
  641. if (!IS_ERR(sock))
  642. return sock;
  643. return ERR_PTR(-ENOTSOCK);
  644. }
  645. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  646. {
  647. struct socket *sock, *oldsock;
  648. struct vhost_virtqueue *vq;
  649. int r;
  650. mutex_lock(&n->dev.mutex);
  651. r = vhost_dev_check_owner(&n->dev);
  652. if (r)
  653. goto err;
  654. if (index >= VHOST_NET_VQ_MAX) {
  655. r = -ENOBUFS;
  656. goto err;
  657. }
  658. vq = n->vqs + index;
  659. mutex_lock(&vq->mutex);
  660. /* Verify that ring has been setup correctly. */
  661. if (!vhost_vq_access_ok(vq)) {
  662. r = -EFAULT;
  663. goto err_vq;
  664. }
  665. sock = get_socket(fd);
  666. if (IS_ERR(sock)) {
  667. r = PTR_ERR(sock);
  668. goto err_vq;
  669. }
  670. /* start polling new socket */
  671. oldsock = rcu_dereference_protected(vq->private_data,
  672. lockdep_is_held(&vq->mutex));
  673. if (sock != oldsock) {
  674. vhost_net_disable_vq(n, vq);
  675. rcu_assign_pointer(vq->private_data, sock);
  676. vhost_net_enable_vq(n, vq);
  677. }
  678. mutex_unlock(&vq->mutex);
  679. if (oldsock) {
  680. vhost_net_flush_vq(n, index);
  681. fput(oldsock->file);
  682. }
  683. mutex_unlock(&n->dev.mutex);
  684. return 0;
  685. err_vq:
  686. mutex_unlock(&vq->mutex);
  687. err:
  688. mutex_unlock(&n->dev.mutex);
  689. return r;
  690. }
  691. static long vhost_net_reset_owner(struct vhost_net *n)
  692. {
  693. struct socket *tx_sock = NULL;
  694. struct socket *rx_sock = NULL;
  695. long err;
  696. mutex_lock(&n->dev.mutex);
  697. err = vhost_dev_check_owner(&n->dev);
  698. if (err)
  699. goto done;
  700. vhost_net_stop(n, &tx_sock, &rx_sock);
  701. vhost_net_flush(n);
  702. err = vhost_dev_reset_owner(&n->dev);
  703. done:
  704. mutex_unlock(&n->dev.mutex);
  705. if (tx_sock)
  706. fput(tx_sock->file);
  707. if (rx_sock)
  708. fput(rx_sock->file);
  709. return err;
  710. }
  711. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  712. {
  713. size_t vhost_hlen, sock_hlen, hdr_len;
  714. int i;
  715. hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
  716. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  717. sizeof(struct virtio_net_hdr);
  718. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  719. /* vhost provides vnet_hdr */
  720. vhost_hlen = hdr_len;
  721. sock_hlen = 0;
  722. } else {
  723. /* socket provides vnet_hdr */
  724. vhost_hlen = 0;
  725. sock_hlen = hdr_len;
  726. }
  727. mutex_lock(&n->dev.mutex);
  728. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  729. !vhost_log_access_ok(&n->dev)) {
  730. mutex_unlock(&n->dev.mutex);
  731. return -EFAULT;
  732. }
  733. n->dev.acked_features = features;
  734. smp_wmb();
  735. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  736. mutex_lock(&n->vqs[i].mutex);
  737. n->vqs[i].vhost_hlen = vhost_hlen;
  738. n->vqs[i].sock_hlen = sock_hlen;
  739. mutex_unlock(&n->vqs[i].mutex);
  740. }
  741. vhost_net_flush(n);
  742. mutex_unlock(&n->dev.mutex);
  743. return 0;
  744. }
  745. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  746. unsigned long arg)
  747. {
  748. struct vhost_net *n = f->private_data;
  749. void __user *argp = (void __user *)arg;
  750. u64 __user *featurep = argp;
  751. struct vhost_vring_file backend;
  752. u64 features;
  753. int r;
  754. switch (ioctl) {
  755. case VHOST_NET_SET_BACKEND:
  756. if (copy_from_user(&backend, argp, sizeof backend))
  757. return -EFAULT;
  758. return vhost_net_set_backend(n, backend.index, backend.fd);
  759. case VHOST_GET_FEATURES:
  760. features = VHOST_FEATURES;
  761. if (copy_to_user(featurep, &features, sizeof features))
  762. return -EFAULT;
  763. return 0;
  764. case VHOST_SET_FEATURES:
  765. if (copy_from_user(&features, featurep, sizeof features))
  766. return -EFAULT;
  767. if (features & ~VHOST_FEATURES)
  768. return -EOPNOTSUPP;
  769. return vhost_net_set_features(n, features);
  770. case VHOST_RESET_OWNER:
  771. return vhost_net_reset_owner(n);
  772. default:
  773. mutex_lock(&n->dev.mutex);
  774. r = vhost_dev_ioctl(&n->dev, ioctl, arg);
  775. vhost_net_flush(n);
  776. mutex_unlock(&n->dev.mutex);
  777. return r;
  778. }
  779. }
  780. #ifdef CONFIG_COMPAT
  781. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  782. unsigned long arg)
  783. {
  784. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  785. }
  786. #endif
  787. static const struct file_operations vhost_net_fops = {
  788. .owner = THIS_MODULE,
  789. .release = vhost_net_release,
  790. .unlocked_ioctl = vhost_net_ioctl,
  791. #ifdef CONFIG_COMPAT
  792. .compat_ioctl = vhost_net_compat_ioctl,
  793. #endif
  794. .open = vhost_net_open,
  795. .llseek = noop_llseek,
  796. };
  797. static struct miscdevice vhost_net_misc = {
  798. MISC_DYNAMIC_MINOR,
  799. "vhost-net",
  800. &vhost_net_fops,
  801. };
  802. static int vhost_net_init(void)
  803. {
  804. return misc_register(&vhost_net_misc);
  805. }
  806. module_init(vhost_net_init);
  807. static void vhost_net_exit(void)
  808. {
  809. misc_deregister(&vhost_net_misc);
  810. }
  811. module_exit(vhost_net_exit);
  812. MODULE_VERSION("0.0.1");
  813. MODULE_LICENSE("GPL v2");
  814. MODULE_AUTHOR("Michael S. Tsirkin");
  815. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");