net.c 16 KB

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