net.c 22 KB

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