net.c 16 KB

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