net.c 22 KB

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