net.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. /* Caller must have TX VQ lock */
  69. static void tx_poll_stop(struct vhost_net *net)
  70. {
  71. if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED))
  72. return;
  73. vhost_poll_stop(net->poll + VHOST_NET_VQ_TX);
  74. net->tx_poll_state = VHOST_NET_POLL_STOPPED;
  75. }
  76. /* Caller must have TX VQ lock */
  77. static void tx_poll_start(struct vhost_net *net, struct socket *sock)
  78. {
  79. if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED))
  80. return;
  81. vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file);
  82. net->tx_poll_state = VHOST_NET_POLL_STARTED;
  83. }
  84. /* Expects to be always run from workqueue - which acts as
  85. * read-size critical section for our kind of RCU. */
  86. static void handle_tx(struct vhost_net *net)
  87. {
  88. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
  89. unsigned out, in, s;
  90. int head;
  91. struct msghdr msg = {
  92. .msg_name = NULL,
  93. .msg_namelen = 0,
  94. .msg_control = NULL,
  95. .msg_controllen = 0,
  96. .msg_iov = vq->iov,
  97. .msg_flags = MSG_DONTWAIT,
  98. };
  99. size_t len, total_len = 0;
  100. int err, wmem;
  101. size_t hdr_size;
  102. struct socket *sock = rcu_dereference(vq->private_data);
  103. if (!sock)
  104. return;
  105. wmem = atomic_read(&sock->sk->sk_wmem_alloc);
  106. if (wmem >= sock->sk->sk_sndbuf) {
  107. mutex_lock(&vq->mutex);
  108. tx_poll_start(net, sock);
  109. mutex_unlock(&vq->mutex);
  110. return;
  111. }
  112. use_mm(net->dev.mm);
  113. mutex_lock(&vq->mutex);
  114. vhost_disable_notify(vq);
  115. if (wmem < sock->sk->sk_sndbuf / 2)
  116. tx_poll_stop(net);
  117. hdr_size = vq->hdr_size;
  118. for (;;) {
  119. head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
  120. ARRAY_SIZE(vq->iov),
  121. &out, &in,
  122. NULL, NULL);
  123. /* On error, stop handling until the next kick. */
  124. if (unlikely(head < 0))
  125. break;
  126. /* Nothing new? Wait for eventfd to tell us they refilled. */
  127. if (head == vq->num) {
  128. wmem = atomic_read(&sock->sk->sk_wmem_alloc);
  129. if (wmem >= sock->sk->sk_sndbuf * 3 / 4) {
  130. tx_poll_start(net, sock);
  131. set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
  132. break;
  133. }
  134. if (unlikely(vhost_enable_notify(vq))) {
  135. vhost_disable_notify(vq);
  136. continue;
  137. }
  138. break;
  139. }
  140. if (in) {
  141. vq_err(vq, "Unexpected descriptor format for TX: "
  142. "out %d, int %d\n", out, in);
  143. break;
  144. }
  145. /* Skip header. TODO: support TSO. */
  146. s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
  147. msg.msg_iovlen = out;
  148. len = iov_length(vq->iov, out);
  149. /* Sanity check */
  150. if (!len) {
  151. vq_err(vq, "Unexpected header len for TX: "
  152. "%zd expected %zd\n",
  153. iov_length(vq->hdr, s), hdr_size);
  154. break;
  155. }
  156. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  157. err = sock->ops->sendmsg(NULL, sock, &msg, len);
  158. if (unlikely(err < 0)) {
  159. vhost_discard_vq_desc(vq);
  160. tx_poll_start(net, sock);
  161. break;
  162. }
  163. if (err != len)
  164. pr_err("Truncated TX packet: "
  165. " len %d != %zd\n", err, len);
  166. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  167. total_len += len;
  168. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  169. vhost_poll_queue(&vq->poll);
  170. break;
  171. }
  172. }
  173. mutex_unlock(&vq->mutex);
  174. unuse_mm(net->dev.mm);
  175. }
  176. /* Expects to be always run from workqueue - which acts as
  177. * read-size critical section for our kind of RCU. */
  178. static void handle_rx(struct vhost_net *net)
  179. {
  180. struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
  181. unsigned out, in, log, s;
  182. int head;
  183. struct vhost_log *vq_log;
  184. struct msghdr msg = {
  185. .msg_name = NULL,
  186. .msg_namelen = 0,
  187. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  188. .msg_controllen = 0,
  189. .msg_iov = vq->iov,
  190. .msg_flags = MSG_DONTWAIT,
  191. };
  192. struct virtio_net_hdr hdr = {
  193. .flags = 0,
  194. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  195. };
  196. size_t len, total_len = 0;
  197. int err;
  198. size_t hdr_size;
  199. struct socket *sock = rcu_dereference(vq->private_data);
  200. if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
  201. return;
  202. use_mm(net->dev.mm);
  203. mutex_lock(&vq->mutex);
  204. vhost_disable_notify(vq);
  205. hdr_size = vq->hdr_size;
  206. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  207. vq->log : NULL;
  208. for (;;) {
  209. head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
  210. ARRAY_SIZE(vq->iov),
  211. &out, &in,
  212. vq_log, &log);
  213. /* On error, stop handling until the next kick. */
  214. if (unlikely(head < 0))
  215. break;
  216. /* OK, now we need to know about added descriptors. */
  217. if (head == vq->num) {
  218. if (unlikely(vhost_enable_notify(vq))) {
  219. /* They have slipped one in as we were
  220. * doing that: check again. */
  221. vhost_disable_notify(vq);
  222. continue;
  223. }
  224. /* Nothing new? Wait for eventfd to tell us
  225. * they refilled. */
  226. break;
  227. }
  228. /* We don't need to be notified again. */
  229. if (out) {
  230. vq_err(vq, "Unexpected descriptor format for RX: "
  231. "out %d, int %d\n",
  232. out, in);
  233. break;
  234. }
  235. /* Skip header. TODO: support TSO/mergeable rx buffers. */
  236. s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
  237. msg.msg_iovlen = in;
  238. len = iov_length(vq->iov, in);
  239. /* Sanity check */
  240. if (!len) {
  241. vq_err(vq, "Unexpected header len for RX: "
  242. "%zd expected %zd\n",
  243. iov_length(vq->hdr, s), hdr_size);
  244. break;
  245. }
  246. err = sock->ops->recvmsg(NULL, sock, &msg,
  247. len, MSG_DONTWAIT | MSG_TRUNC);
  248. /* TODO: Check specific error and bomb out unless EAGAIN? */
  249. if (err < 0) {
  250. vhost_discard_vq_desc(vq);
  251. break;
  252. }
  253. /* TODO: Should check and handle checksum. */
  254. if (err > len) {
  255. pr_err("Discarded truncated rx packet: "
  256. " len %d > %zd\n", err, len);
  257. vhost_discard_vq_desc(vq);
  258. continue;
  259. }
  260. len = err;
  261. err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
  262. if (err) {
  263. vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
  264. vq->iov->iov_base, err);
  265. break;
  266. }
  267. len += hdr_size;
  268. vhost_add_used_and_signal(&net->dev, vq, head, len);
  269. if (unlikely(vq_log))
  270. vhost_log_write(vq, vq_log, log, len);
  271. total_len += len;
  272. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  273. vhost_poll_queue(&vq->poll);
  274. break;
  275. }
  276. }
  277. mutex_unlock(&vq->mutex);
  278. unuse_mm(net->dev.mm);
  279. }
  280. static void handle_tx_kick(struct work_struct *work)
  281. {
  282. struct vhost_virtqueue *vq;
  283. struct vhost_net *net;
  284. vq = container_of(work, struct vhost_virtqueue, poll.work);
  285. net = container_of(vq->dev, struct vhost_net, dev);
  286. handle_tx(net);
  287. }
  288. static void handle_rx_kick(struct work_struct *work)
  289. {
  290. struct vhost_virtqueue *vq;
  291. struct vhost_net *net;
  292. vq = container_of(work, struct vhost_virtqueue, poll.work);
  293. net = container_of(vq->dev, struct vhost_net, dev);
  294. handle_rx(net);
  295. }
  296. static void handle_tx_net(struct work_struct *work)
  297. {
  298. struct vhost_net *net;
  299. net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_TX].work);
  300. handle_tx(net);
  301. }
  302. static void handle_rx_net(struct work_struct *work)
  303. {
  304. struct vhost_net *net;
  305. net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_RX].work);
  306. handle_rx(net);
  307. }
  308. static int vhost_net_open(struct inode *inode, struct file *f)
  309. {
  310. struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
  311. int r;
  312. if (!n)
  313. return -ENOMEM;
  314. n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
  315. n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
  316. r = vhost_dev_init(&n->dev, n->vqs, VHOST_NET_VQ_MAX);
  317. if (r < 0) {
  318. kfree(n);
  319. return r;
  320. }
  321. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT);
  322. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN);
  323. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  324. f->private_data = n;
  325. return 0;
  326. }
  327. static void vhost_net_disable_vq(struct vhost_net *n,
  328. struct vhost_virtqueue *vq)
  329. {
  330. if (!vq->private_data)
  331. return;
  332. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  333. tx_poll_stop(n);
  334. n->tx_poll_state = VHOST_NET_POLL_DISABLED;
  335. } else
  336. vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
  337. }
  338. static void vhost_net_enable_vq(struct vhost_net *n,
  339. struct vhost_virtqueue *vq)
  340. {
  341. struct socket *sock = vq->private_data;
  342. if (!sock)
  343. return;
  344. if (vq == n->vqs + VHOST_NET_VQ_TX) {
  345. n->tx_poll_state = VHOST_NET_POLL_STOPPED;
  346. tx_poll_start(n, sock);
  347. } else
  348. vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
  349. }
  350. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  351. struct vhost_virtqueue *vq)
  352. {
  353. struct socket *sock;
  354. mutex_lock(&vq->mutex);
  355. sock = vq->private_data;
  356. vhost_net_disable_vq(n, vq);
  357. rcu_assign_pointer(vq->private_data, NULL);
  358. mutex_unlock(&vq->mutex);
  359. return sock;
  360. }
  361. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  362. struct socket **rx_sock)
  363. {
  364. *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
  365. *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
  366. }
  367. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  368. {
  369. vhost_poll_flush(n->poll + index);
  370. vhost_poll_flush(&n->dev.vqs[index].poll);
  371. }
  372. static void vhost_net_flush(struct vhost_net *n)
  373. {
  374. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  375. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  376. }
  377. static int vhost_net_release(struct inode *inode, struct file *f)
  378. {
  379. struct vhost_net *n = f->private_data;
  380. struct socket *tx_sock;
  381. struct socket *rx_sock;
  382. vhost_net_stop(n, &tx_sock, &rx_sock);
  383. vhost_net_flush(n);
  384. vhost_dev_cleanup(&n->dev);
  385. if (tx_sock)
  386. fput(tx_sock->file);
  387. if (rx_sock)
  388. fput(rx_sock->file);
  389. /* We do an extra flush before freeing memory,
  390. * since jobs can re-queue themselves. */
  391. vhost_net_flush(n);
  392. kfree(n);
  393. return 0;
  394. }
  395. static struct socket *get_raw_socket(int fd)
  396. {
  397. struct {
  398. struct sockaddr_ll sa;
  399. char buf[MAX_ADDR_LEN];
  400. } uaddr;
  401. int uaddr_len = sizeof uaddr, r;
  402. struct socket *sock = sockfd_lookup(fd, &r);
  403. if (!sock)
  404. return ERR_PTR(-ENOTSOCK);
  405. /* Parameter checking */
  406. if (sock->sk->sk_type != SOCK_RAW) {
  407. r = -ESOCKTNOSUPPORT;
  408. goto err;
  409. }
  410. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  411. &uaddr_len, 0);
  412. if (r)
  413. goto err;
  414. if (uaddr.sa.sll_family != AF_PACKET) {
  415. r = -EPFNOSUPPORT;
  416. goto err;
  417. }
  418. return sock;
  419. err:
  420. fput(sock->file);
  421. return ERR_PTR(r);
  422. }
  423. static struct socket *get_tap_socket(int fd)
  424. {
  425. struct file *file = fget(fd);
  426. struct socket *sock;
  427. if (!file)
  428. return ERR_PTR(-EBADF);
  429. sock = tun_get_socket(file);
  430. if (!IS_ERR(sock))
  431. return sock;
  432. sock = macvtap_get_socket(file);
  433. if (IS_ERR(sock))
  434. fput(file);
  435. return sock;
  436. }
  437. static struct socket *get_socket(int fd)
  438. {
  439. struct socket *sock;
  440. /* special case to disable backend */
  441. if (fd == -1)
  442. return NULL;
  443. sock = get_raw_socket(fd);
  444. if (!IS_ERR(sock))
  445. return sock;
  446. sock = get_tap_socket(fd);
  447. if (!IS_ERR(sock))
  448. return sock;
  449. return ERR_PTR(-ENOTSOCK);
  450. }
  451. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  452. {
  453. struct socket *sock, *oldsock;
  454. struct vhost_virtqueue *vq;
  455. int r;
  456. mutex_lock(&n->dev.mutex);
  457. r = vhost_dev_check_owner(&n->dev);
  458. if (r)
  459. goto err;
  460. if (index >= VHOST_NET_VQ_MAX) {
  461. r = -ENOBUFS;
  462. goto err;
  463. }
  464. vq = n->vqs + index;
  465. mutex_lock(&vq->mutex);
  466. /* Verify that ring has been setup correctly. */
  467. if (!vhost_vq_access_ok(vq)) {
  468. r = -EFAULT;
  469. goto err_vq;
  470. }
  471. sock = get_socket(fd);
  472. if (IS_ERR(sock)) {
  473. r = PTR_ERR(sock);
  474. goto err_vq;
  475. }
  476. /* start polling new socket */
  477. oldsock = vq->private_data;
  478. if (sock == oldsock)
  479. goto done;
  480. vhost_net_disable_vq(n, vq);
  481. rcu_assign_pointer(vq->private_data, sock);
  482. vhost_net_enable_vq(n, vq);
  483. done:
  484. mutex_unlock(&vq->mutex);
  485. if (oldsock) {
  486. vhost_net_flush_vq(n, index);
  487. fput(oldsock->file);
  488. }
  489. mutex_unlock(&n->dev.mutex);
  490. return 0;
  491. err_vq:
  492. mutex_unlock(&vq->mutex);
  493. err:
  494. mutex_unlock(&n->dev.mutex);
  495. return r;
  496. }
  497. static long vhost_net_reset_owner(struct vhost_net *n)
  498. {
  499. struct socket *tx_sock = NULL;
  500. struct socket *rx_sock = NULL;
  501. long err;
  502. mutex_lock(&n->dev.mutex);
  503. err = vhost_dev_check_owner(&n->dev);
  504. if (err)
  505. goto done;
  506. vhost_net_stop(n, &tx_sock, &rx_sock);
  507. vhost_net_flush(n);
  508. err = vhost_dev_reset_owner(&n->dev);
  509. done:
  510. mutex_unlock(&n->dev.mutex);
  511. if (tx_sock)
  512. fput(tx_sock->file);
  513. if (rx_sock)
  514. fput(rx_sock->file);
  515. return err;
  516. }
  517. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  518. {
  519. size_t hdr_size = features & (1 << VHOST_NET_F_VIRTIO_NET_HDR) ?
  520. sizeof(struct virtio_net_hdr) : 0;
  521. int i;
  522. mutex_lock(&n->dev.mutex);
  523. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  524. !vhost_log_access_ok(&n->dev)) {
  525. mutex_unlock(&n->dev.mutex);
  526. return -EFAULT;
  527. }
  528. n->dev.acked_features = features;
  529. smp_wmb();
  530. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  531. mutex_lock(&n->vqs[i].mutex);
  532. n->vqs[i].hdr_size = hdr_size;
  533. mutex_unlock(&n->vqs[i].mutex);
  534. }
  535. vhost_net_flush(n);
  536. mutex_unlock(&n->dev.mutex);
  537. return 0;
  538. }
  539. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  540. unsigned long arg)
  541. {
  542. struct vhost_net *n = f->private_data;
  543. void __user *argp = (void __user *)arg;
  544. u64 __user *featurep = argp;
  545. struct vhost_vring_file backend;
  546. u64 features;
  547. int r;
  548. switch (ioctl) {
  549. case VHOST_NET_SET_BACKEND:
  550. if (copy_from_user(&backend, argp, sizeof backend))
  551. return -EFAULT;
  552. return vhost_net_set_backend(n, backend.index, backend.fd);
  553. case VHOST_GET_FEATURES:
  554. features = VHOST_FEATURES;
  555. if (copy_to_user(featurep, &features, sizeof features))
  556. return -EFAULT;
  557. return 0;
  558. case VHOST_SET_FEATURES:
  559. if (copy_from_user(&features, featurep, sizeof features))
  560. return -EFAULT;
  561. if (features & ~VHOST_FEATURES)
  562. return -EOPNOTSUPP;
  563. return vhost_net_set_features(n, features);
  564. case VHOST_RESET_OWNER:
  565. return vhost_net_reset_owner(n);
  566. default:
  567. mutex_lock(&n->dev.mutex);
  568. r = vhost_dev_ioctl(&n->dev, ioctl, arg);
  569. vhost_net_flush(n);
  570. mutex_unlock(&n->dev.mutex);
  571. return r;
  572. }
  573. }
  574. #ifdef CONFIG_COMPAT
  575. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  576. unsigned long arg)
  577. {
  578. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  579. }
  580. #endif
  581. const static struct file_operations vhost_net_fops = {
  582. .owner = THIS_MODULE,
  583. .release = vhost_net_release,
  584. .unlocked_ioctl = vhost_net_ioctl,
  585. #ifdef CONFIG_COMPAT
  586. .compat_ioctl = vhost_net_compat_ioctl,
  587. #endif
  588. .open = vhost_net_open,
  589. };
  590. static struct miscdevice vhost_net_misc = {
  591. VHOST_NET_MINOR,
  592. "vhost-net",
  593. &vhost_net_fops,
  594. };
  595. static int vhost_net_init(void)
  596. {
  597. int r = vhost_init();
  598. if (r)
  599. goto err_init;
  600. r = misc_register(&vhost_net_misc);
  601. if (r)
  602. goto err_reg;
  603. return 0;
  604. err_reg:
  605. vhost_cleanup();
  606. err_init:
  607. return r;
  608. }
  609. module_init(vhost_net_init);
  610. static void vhost_net_exit(void)
  611. {
  612. misc_deregister(&vhost_net_misc);
  613. vhost_cleanup();
  614. }
  615. module_exit(vhost_net_exit);
  616. MODULE_VERSION("0.0.1");
  617. MODULE_LICENSE("GPL v2");
  618. MODULE_AUTHOR("Michael S. Tsirkin");
  619. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");