net.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 = rcu_dereference(vq->private_data);
  119. if (!sock)
  120. return;
  121. wmem = atomic_read(&sock->sk->sk_wmem_alloc);
  122. if (wmem >= sock->sk->sk_sndbuf) {
  123. mutex_lock(&vq->mutex);
  124. tx_poll_start(net, sock);
  125. mutex_unlock(&vq->mutex);
  126. return;
  127. }
  128. use_mm(net->dev.mm);
  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. unuse_mm(net->dev.mm);
  191. }
  192. static int peek_head_len(struct sock *sk)
  193. {
  194. struct sk_buff *head;
  195. int len = 0;
  196. lock_sock(sk);
  197. head = skb_peek(&sk->sk_receive_queue);
  198. if (head)
  199. len = head->len;
  200. release_sock(sk);
  201. return len;
  202. }
  203. /* This is a multi-buffer version of vhost_get_desc, that works if
  204. * vq has read descriptors only.
  205. * @vq - the relevant virtqueue
  206. * @datalen - data length we'll be reading
  207. * @iovcount - returned count of io vectors we fill
  208. * @log - vhost log
  209. * @log_num - log offset
  210. * returns number of buffer heads allocated, negative on error
  211. */
  212. static int get_rx_bufs(struct vhost_virtqueue *vq,
  213. struct vring_used_elem *heads,
  214. int datalen,
  215. unsigned *iovcount,
  216. struct vhost_log *log,
  217. unsigned *log_num)
  218. {
  219. unsigned int out, in;
  220. int seg = 0;
  221. int headcount = 0;
  222. unsigned d;
  223. int r, nlogs = 0;
  224. while (datalen > 0) {
  225. if (unlikely(seg >= VHOST_NET_MAX_SG)) {
  226. r = -ENOBUFS;
  227. goto err;
  228. }
  229. d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
  230. ARRAY_SIZE(vq->iov) - seg, &out,
  231. &in, log, log_num);
  232. if (d == vq->num) {
  233. r = 0;
  234. goto err;
  235. }
  236. if (unlikely(out || in <= 0)) {
  237. vq_err(vq, "unexpected descriptor format for RX: "
  238. "out %d, in %d\n", out, in);
  239. r = -EINVAL;
  240. goto err;
  241. }
  242. if (unlikely(log)) {
  243. nlogs += *log_num;
  244. log += *log_num;
  245. }
  246. heads[headcount].id = d;
  247. heads[headcount].len = iov_length(vq->iov + seg, in);
  248. datalen -= heads[headcount].len;
  249. ++headcount;
  250. seg += in;
  251. }
  252. heads[headcount - 1].len += datalen;
  253. *iovcount = seg;
  254. if (unlikely(log))
  255. *log_num = nlogs;
  256. return headcount;
  257. err:
  258. vhost_discard_vq_desc(vq, headcount);
  259. return r;
  260. }
  261. /* Expects to be always run from workqueue - which acts as
  262. * read-size critical section for our kind of RCU. */
  263. static void handle_rx_big(struct vhost_net *net)
  264. {
  265. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
  266. unsigned out, in, log, s;
  267. int head;
  268. struct vhost_log *vq_log;
  269. struct msghdr msg = {
  270. .msg_name = NULL,
  271. .msg_namelen = 0,
  272. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  273. .msg_controllen = 0,
  274. .msg_iov = vq->iov,
  275. .msg_flags = MSG_DONTWAIT,
  276. };
  277. struct virtio_net_hdr hdr = {
  278. .flags = 0,
  279. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  280. };
  281. size_t len, total_len = 0;
  282. int err;
  283. size_t hdr_size;
  284. struct socket *sock = rcu_dereference(vq->private_data);
  285. if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
  286. return;
  287. use_mm(net->dev.mm);
  288. mutex_lock(&vq->mutex);
  289. vhost_disable_notify(vq);
  290. hdr_size = vq->vhost_hlen;
  291. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  292. vq->log : NULL;
  293. for (;;) {
  294. head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
  295. ARRAY_SIZE(vq->iov),
  296. &out, &in,
  297. vq_log, &log);
  298. /* On error, stop handling until the next kick. */
  299. if (unlikely(head < 0))
  300. break;
  301. /* OK, now we need to know about added descriptors. */
  302. if (head == vq->num) {
  303. if (unlikely(vhost_enable_notify(vq))) {
  304. /* They have slipped one in as we were
  305. * doing that: check again. */
  306. vhost_disable_notify(vq);
  307. continue;
  308. }
  309. /* Nothing new? Wait for eventfd to tell us
  310. * they refilled. */
  311. break;
  312. }
  313. /* We don't need to be notified again. */
  314. if (out) {
  315. vq_err(vq, "Unexpected descriptor format for RX: "
  316. "out %d, int %d\n",
  317. out, in);
  318. break;
  319. }
  320. /* Skip header. TODO: support TSO/mergeable rx buffers. */
  321. s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
  322. msg.msg_iovlen = in;
  323. len = iov_length(vq->iov, in);
  324. /* Sanity check */
  325. if (!len) {
  326. vq_err(vq, "Unexpected header len for RX: "
  327. "%zd expected %zd\n",
  328. iov_length(vq->hdr, s), hdr_size);
  329. break;
  330. }
  331. err = sock->ops->recvmsg(NULL, sock, &msg,
  332. len, MSG_DONTWAIT | MSG_TRUNC);
  333. /* TODO: Check specific error and bomb out unless EAGAIN? */
  334. if (err < 0) {
  335. vhost_discard_vq_desc(vq, 1);
  336. break;
  337. }
  338. /* TODO: Should check and handle checksum. */
  339. if (err > len) {
  340. pr_debug("Discarded truncated rx packet: "
  341. " len %d > %zd\n", err, len);
  342. vhost_discard_vq_desc(vq, 1);
  343. continue;
  344. }
  345. len = err;
  346. err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
  347. if (err) {
  348. vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
  349. vq->iov->iov_base, err);
  350. break;
  351. }
  352. len += hdr_size;
  353. vhost_add_used_and_signal(&net->dev, vq, head, len);
  354. if (unlikely(vq_log))
  355. vhost_log_write(vq, vq_log, log, len);
  356. total_len += len;
  357. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  358. vhost_poll_queue(&vq->poll);
  359. break;
  360. }
  361. }
  362. mutex_unlock(&vq->mutex);
  363. unuse_mm(net->dev.mm);
  364. }
  365. /* Expects to be always run from workqueue - which acts as
  366. * read-size critical section for our kind of RCU. */
  367. static void handle_rx_mergeable(struct vhost_net *net)
  368. {
  369. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
  370. unsigned uninitialized_var(in), log;
  371. struct vhost_log *vq_log;
  372. struct msghdr msg = {
  373. .msg_name = NULL,
  374. .msg_namelen = 0,
  375. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  376. .msg_controllen = 0,
  377. .msg_iov = vq->iov,
  378. .msg_flags = MSG_DONTWAIT,
  379. };
  380. struct virtio_net_hdr_mrg_rxbuf hdr = {
  381. .hdr.flags = 0,
  382. .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
  383. };
  384. size_t total_len = 0;
  385. int err, headcount;
  386. size_t vhost_hlen, sock_hlen;
  387. size_t vhost_len, sock_len;
  388. struct socket *sock = rcu_dereference(vq->private_data);
  389. if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
  390. return;
  391. use_mm(net->dev.mm);
  392. mutex_lock(&vq->mutex);
  393. vhost_disable_notify(vq);
  394. vhost_hlen = vq->vhost_hlen;
  395. sock_hlen = vq->sock_hlen;
  396. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  397. vq->log : NULL;
  398. while ((sock_len = peek_head_len(sock->sk))) {
  399. sock_len += sock_hlen;
  400. vhost_len = sock_len + vhost_hlen;
  401. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  402. &in, vq_log, &log);
  403. /* On error, stop handling until the next kick. */
  404. if (unlikely(headcount < 0))
  405. break;
  406. /* OK, now we need to know about added descriptors. */
  407. if (!headcount) {
  408. if (unlikely(vhost_enable_notify(vq))) {
  409. /* They have slipped one in as we were
  410. * doing that: check again. */
  411. vhost_disable_notify(vq);
  412. continue;
  413. }
  414. /* Nothing new? Wait for eventfd to tell us
  415. * they refilled. */
  416. break;
  417. }
  418. /* We don't need to be notified again. */
  419. if (unlikely((vhost_hlen)))
  420. /* Skip header. TODO: support TSO. */
  421. move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
  422. else
  423. /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
  424. * needed because sendmsg can modify msg_iov. */
  425. copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
  426. msg.msg_iovlen = in;
  427. err = sock->ops->recvmsg(NULL, sock, &msg,
  428. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  429. /* Userspace might have consumed the packet meanwhile:
  430. * it's not supposed to do this usually, but might be hard
  431. * to prevent. Discard data we got (if any) and keep going. */
  432. if (unlikely(err != sock_len)) {
  433. pr_debug("Discarded rx packet: "
  434. " len %d, expected %zd\n", err, sock_len);
  435. vhost_discard_vq_desc(vq, headcount);
  436. continue;
  437. }
  438. if (unlikely(vhost_hlen) &&
  439. memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
  440. vhost_hlen)) {
  441. vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
  442. vq->iov->iov_base);
  443. break;
  444. }
  445. /* TODO: Should check and handle checksum. */
  446. if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF) &&
  447. memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
  448. offsetof(typeof(hdr), num_buffers),
  449. sizeof hdr.num_buffers)) {
  450. vq_err(vq, "Failed num_buffers write");
  451. vhost_discard_vq_desc(vq, headcount);
  452. break;
  453. }
  454. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  455. headcount);
  456. if (unlikely(vq_log))
  457. vhost_log_write(vq, vq_log, log, vhost_len);
  458. total_len += vhost_len;
  459. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  460. vhost_poll_queue(&vq->poll);
  461. break;
  462. }
  463. }
  464. mutex_unlock(&vq->mutex);
  465. unuse_mm(net->dev.mm);
  466. }
  467. static void handle_rx(struct vhost_net *net)
  468. {
  469. if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF))
  470. handle_rx_mergeable(net);
  471. else
  472. handle_rx_big(net);
  473. }
  474. static void handle_tx_kick(struct vhost_work *work)
  475. {
  476. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  477. poll.work);
  478. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  479. handle_tx(net);
  480. }
  481. static void handle_rx_kick(struct vhost_work *work)
  482. {
  483. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  484. poll.work);
  485. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  486. handle_rx(net);
  487. }
  488. static void handle_tx_net(struct vhost_work *work)
  489. {
  490. struct vhost_net *net = container_of(work, struct vhost_net,
  491. poll[VHOST_NET_VQ_TX].work);
  492. handle_tx(net);
  493. }
  494. static void handle_rx_net(struct vhost_work *work)
  495. {
  496. struct vhost_net *net = container_of(work, struct vhost_net,
  497. poll[VHOST_NET_VQ_RX].work);
  498. handle_rx(net);
  499. }
  500. static int vhost_net_open(struct inode *inode, struct file *f)
  501. {
  502. struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
  503. struct vhost_dev *dev;
  504. int r;
  505. if (!n)
  506. return -ENOMEM;
  507. dev = &n->dev;
  508. n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
  509. n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
  510. r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
  511. if (r < 0) {
  512. kfree(n);
  513. return r;
  514. }
  515. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  516. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  517. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  518. f->private_data = n;
  519. return 0;
  520. }
  521. static void vhost_net_disable_vq(struct vhost_net *n,
  522. struct vhost_virtqueue *vq)
  523. {
  524. if (!vq->private_data)
  525. return;
  526. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  527. tx_poll_stop(n);
  528. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  529. } else
  530. vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
  531. }
  532. static void vhost_net_enable_vq(struct vhost_net *n,
  533. struct vhost_virtqueue *vq)
  534. {
  535. struct socket *sock = vq->private_data;
  536. if (!sock)
  537. return;
  538. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  539. n->tx_poll_state = VHOST_NET_POLL_STOPPED;
  540. tx_poll_start(n, sock);
  541. } else
  542. vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
  543. }
  544. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  545. struct vhost_virtqueue *vq)
  546. {
  547. struct socket *sock;
  548. mutex_lock(&vq->mutex);
  549. sock = vq->private_data;
  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 = vq->private_data;
  672. if (sock != oldsock) {
  673. vhost_net_disable_vq(n, vq);
  674. rcu_assign_pointer(vq->private_data, sock);
  675. vhost_net_enable_vq(n, vq);
  676. }
  677. mutex_unlock(&vq->mutex);
  678. if (oldsock) {
  679. vhost_net_flush_vq(n, index);
  680. fput(oldsock->file);
  681. }
  682. mutex_unlock(&n->dev.mutex);
  683. return 0;
  684. err_vq:
  685. mutex_unlock(&vq->mutex);
  686. err:
  687. mutex_unlock(&n->dev.mutex);
  688. return r;
  689. }
  690. static long vhost_net_reset_owner(struct vhost_net *n)
  691. {
  692. struct socket *tx_sock = NULL;
  693. struct socket *rx_sock = NULL;
  694. long err;
  695. mutex_lock(&n->dev.mutex);
  696. err = vhost_dev_check_owner(&n->dev);
  697. if (err)
  698. goto done;
  699. vhost_net_stop(n, &tx_sock, &rx_sock);
  700. vhost_net_flush(n);
  701. err = vhost_dev_reset_owner(&n->dev);
  702. done:
  703. mutex_unlock(&n->dev.mutex);
  704. if (tx_sock)
  705. fput(tx_sock->file);
  706. if (rx_sock)
  707. fput(rx_sock->file);
  708. return err;
  709. }
  710. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  711. {
  712. size_t vhost_hlen, sock_hlen, hdr_len;
  713. int i;
  714. hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
  715. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  716. sizeof(struct virtio_net_hdr);
  717. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  718. /* vhost provides vnet_hdr */
  719. vhost_hlen = hdr_len;
  720. sock_hlen = 0;
  721. } else {
  722. /* socket provides vnet_hdr */
  723. vhost_hlen = 0;
  724. sock_hlen = hdr_len;
  725. }
  726. mutex_lock(&n->dev.mutex);
  727. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  728. !vhost_log_access_ok(&n->dev)) {
  729. mutex_unlock(&n->dev.mutex);
  730. return -EFAULT;
  731. }
  732. n->dev.acked_features = features;
  733. smp_wmb();
  734. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  735. mutex_lock(&n->vqs[i].mutex);
  736. n->vqs[i].vhost_hlen = vhost_hlen;
  737. n->vqs[i].sock_hlen = sock_hlen;
  738. mutex_unlock(&n->vqs[i].mutex);
  739. }
  740. vhost_net_flush(n);
  741. mutex_unlock(&n->dev.mutex);
  742. return 0;
  743. }
  744. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  745. unsigned long arg)
  746. {
  747. struct vhost_net *n = f->private_data;
  748. void __user *argp = (void __user *)arg;
  749. u64 __user *featurep = argp;
  750. struct vhost_vring_file backend;
  751. u64 features;
  752. int r;
  753. switch (ioctl) {
  754. case VHOST_NET_SET_BACKEND:
  755. if (copy_from_user(&backend, argp, sizeof backend))
  756. return -EFAULT;
  757. return vhost_net_set_backend(n, backend.index, backend.fd);
  758. case VHOST_GET_FEATURES:
  759. features = VHOST_FEATURES;
  760. if (copy_to_user(featurep, &features, sizeof features))
  761. return -EFAULT;
  762. return 0;
  763. case VHOST_SET_FEATURES:
  764. if (copy_from_user(&features, featurep, sizeof features))
  765. return -EFAULT;
  766. if (features & ~VHOST_FEATURES)
  767. return -EOPNOTSUPP;
  768. return vhost_net_set_features(n, features);
  769. case VHOST_RESET_OWNER:
  770. return vhost_net_reset_owner(n);
  771. default:
  772. mutex_lock(&n->dev.mutex);
  773. r = vhost_dev_ioctl(&n->dev, ioctl, arg);
  774. vhost_net_flush(n);
  775. mutex_unlock(&n->dev.mutex);
  776. return r;
  777. }
  778. }
  779. #ifdef CONFIG_COMPAT
  780. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  781. unsigned long arg)
  782. {
  783. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  784. }
  785. #endif
  786. static const struct file_operations vhost_net_fops = {
  787. .owner = THIS_MODULE,
  788. .release = vhost_net_release,
  789. .unlocked_ioctl = vhost_net_ioctl,
  790. #ifdef CONFIG_COMPAT
  791. .compat_ioctl = vhost_net_compat_ioctl,
  792. #endif
  793. .open = vhost_net_open,
  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");