net.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/mutex.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/file.h>
  18. #include <linux/slab.h>
  19. #include <linux/vmalloc.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 <linux/if_vlan.h>
  26. #include <net/sock.h>
  27. #include "vhost.h"
  28. static int experimental_zcopytx = 1;
  29. module_param(experimental_zcopytx, int, 0444);
  30. MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
  31. " 1 -Enable; 0 - Disable");
  32. /* Max number of bytes transferred before requeueing the job.
  33. * Using this limit prevents one virtqueue from starving others. */
  34. #define VHOST_NET_WEIGHT 0x80000
  35. /* MAX number of TX used buffers for outstanding zerocopy */
  36. #define VHOST_MAX_PEND 128
  37. #define VHOST_GOODCOPY_LEN 256
  38. /*
  39. * For transmit, used buffer len is unused; we override it to track buffer
  40. * status internally; used for zerocopy tx only.
  41. */
  42. /* Lower device DMA failed */
  43. #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
  44. /* Lower device DMA done */
  45. #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
  46. /* Lower device DMA in progress */
  47. #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
  48. /* Buffer unused */
  49. #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
  50. #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
  51. enum {
  52. VHOST_NET_FEATURES = VHOST_FEATURES |
  53. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  54. (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  55. (1ULL << VIRTIO_F_IOMMU_PLATFORM)
  56. };
  57. enum {
  58. VHOST_NET_VQ_RX = 0,
  59. VHOST_NET_VQ_TX = 1,
  60. VHOST_NET_VQ_MAX = 2,
  61. };
  62. struct vhost_net_ubuf_ref {
  63. /* refcount follows semantics similar to kref:
  64. * 0: object is released
  65. * 1: no outstanding ubufs
  66. * >1: outstanding ubufs
  67. */
  68. atomic_t refcount;
  69. wait_queue_head_t wait;
  70. struct vhost_virtqueue *vq;
  71. };
  72. struct vhost_net_virtqueue {
  73. struct vhost_virtqueue vq;
  74. size_t vhost_hlen;
  75. size_t sock_hlen;
  76. /* vhost zerocopy support fields below: */
  77. /* last used idx for outstanding DMA zerocopy buffers */
  78. int upend_idx;
  79. /* first used idx for DMA done zerocopy buffers */
  80. int done_idx;
  81. /* an array of userspace buffers info */
  82. struct ubuf_info *ubuf_info;
  83. /* Reference counting for outstanding ubufs.
  84. * Protected by vq mutex. Writers must also take device mutex. */
  85. struct vhost_net_ubuf_ref *ubufs;
  86. };
  87. struct vhost_net {
  88. struct vhost_dev dev;
  89. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  90. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  91. /* Number of TX recently submitted.
  92. * Protected by tx vq lock. */
  93. unsigned tx_packets;
  94. /* Number of times zerocopy TX recently failed.
  95. * Protected by tx vq lock. */
  96. unsigned tx_zcopy_err;
  97. /* Flush in progress. Protected by tx vq lock. */
  98. bool tx_flush;
  99. };
  100. static unsigned vhost_net_zcopy_mask __read_mostly;
  101. static void vhost_net_enable_zcopy(int vq)
  102. {
  103. vhost_net_zcopy_mask |= 0x1 << vq;
  104. }
  105. static struct vhost_net_ubuf_ref *
  106. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  107. {
  108. struct vhost_net_ubuf_ref *ubufs;
  109. /* No zero copy backend? Nothing to count. */
  110. if (!zcopy)
  111. return NULL;
  112. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  113. if (!ubufs)
  114. return ERR_PTR(-ENOMEM);
  115. atomic_set(&ubufs->refcount, 1);
  116. init_waitqueue_head(&ubufs->wait);
  117. ubufs->vq = vq;
  118. return ubufs;
  119. }
  120. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  121. {
  122. int r = atomic_sub_return(1, &ubufs->refcount);
  123. if (unlikely(!r))
  124. wake_up(&ubufs->wait);
  125. return r;
  126. }
  127. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  128. {
  129. vhost_net_ubuf_put(ubufs);
  130. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  131. }
  132. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  133. {
  134. vhost_net_ubuf_put_and_wait(ubufs);
  135. kfree(ubufs);
  136. }
  137. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  138. {
  139. int i;
  140. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  141. kfree(n->vqs[i].ubuf_info);
  142. n->vqs[i].ubuf_info = NULL;
  143. }
  144. }
  145. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  146. {
  147. bool zcopy;
  148. int i;
  149. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  150. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  151. if (!zcopy)
  152. continue;
  153. n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
  154. UIO_MAXIOV, GFP_KERNEL);
  155. if (!n->vqs[i].ubuf_info)
  156. goto err;
  157. }
  158. return 0;
  159. err:
  160. vhost_net_clear_ubuf_info(n);
  161. return -ENOMEM;
  162. }
  163. static void vhost_net_vq_reset(struct vhost_net *n)
  164. {
  165. int i;
  166. vhost_net_clear_ubuf_info(n);
  167. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  168. n->vqs[i].done_idx = 0;
  169. n->vqs[i].upend_idx = 0;
  170. n->vqs[i].ubufs = NULL;
  171. n->vqs[i].vhost_hlen = 0;
  172. n->vqs[i].sock_hlen = 0;
  173. }
  174. }
  175. static void vhost_net_tx_packet(struct vhost_net *net)
  176. {
  177. ++net->tx_packets;
  178. if (net->tx_packets < 1024)
  179. return;
  180. net->tx_packets = 0;
  181. net->tx_zcopy_err = 0;
  182. }
  183. static void vhost_net_tx_err(struct vhost_net *net)
  184. {
  185. ++net->tx_zcopy_err;
  186. }
  187. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  188. {
  189. /* TX flush waits for outstanding DMAs to be done.
  190. * Don't start new DMAs.
  191. */
  192. return !net->tx_flush &&
  193. net->tx_packets / 64 >= net->tx_zcopy_err;
  194. }
  195. static bool vhost_sock_zcopy(struct socket *sock)
  196. {
  197. return unlikely(experimental_zcopytx) &&
  198. sock_flag(sock->sk, SOCK_ZEROCOPY);
  199. }
  200. /* In case of DMA done not in order in lower device driver for some reason.
  201. * upend_idx is used to track end of used idx, done_idx is used to track head
  202. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  203. * guest used idx.
  204. */
  205. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  206. struct vhost_virtqueue *vq)
  207. {
  208. struct vhost_net_virtqueue *nvq =
  209. container_of(vq, struct vhost_net_virtqueue, vq);
  210. int i, add;
  211. int j = 0;
  212. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  213. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  214. vhost_net_tx_err(net);
  215. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  216. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  217. ++j;
  218. } else
  219. break;
  220. }
  221. while (j) {
  222. add = min(UIO_MAXIOV - nvq->done_idx, j);
  223. vhost_add_used_and_signal_n(vq->dev, vq,
  224. &vq->heads[nvq->done_idx], add);
  225. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  226. j -= add;
  227. }
  228. }
  229. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  230. {
  231. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  232. struct vhost_virtqueue *vq = ubufs->vq;
  233. int cnt;
  234. rcu_read_lock_bh();
  235. /* set len to mark this desc buffers done DMA */
  236. vq->heads[ubuf->desc].len = success ?
  237. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  238. cnt = vhost_net_ubuf_put(ubufs);
  239. /*
  240. * Trigger polling thread if guest stopped submitting new buffers:
  241. * in this case, the refcount after decrement will eventually reach 1.
  242. * We also trigger polling periodically after each 16 packets
  243. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  244. * less than 10% of times).
  245. */
  246. if (cnt <= 1 || !(cnt % 16))
  247. vhost_poll_queue(&vq->poll);
  248. rcu_read_unlock_bh();
  249. }
  250. static inline unsigned long busy_clock(void)
  251. {
  252. return local_clock() >> 10;
  253. }
  254. static bool vhost_can_busy_poll(struct vhost_dev *dev,
  255. unsigned long endtime)
  256. {
  257. return likely(!need_resched()) &&
  258. likely(!time_after(busy_clock(), endtime)) &&
  259. likely(!signal_pending(current)) &&
  260. !vhost_has_work(dev);
  261. }
  262. static void vhost_net_disable_vq(struct vhost_net *n,
  263. struct vhost_virtqueue *vq)
  264. {
  265. struct vhost_net_virtqueue *nvq =
  266. container_of(vq, struct vhost_net_virtqueue, vq);
  267. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  268. if (!vq->private_data)
  269. return;
  270. vhost_poll_stop(poll);
  271. }
  272. static int vhost_net_enable_vq(struct vhost_net *n,
  273. struct vhost_virtqueue *vq)
  274. {
  275. struct vhost_net_virtqueue *nvq =
  276. container_of(vq, struct vhost_net_virtqueue, vq);
  277. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  278. struct socket *sock;
  279. sock = vq->private_data;
  280. if (!sock)
  281. return 0;
  282. return vhost_poll_start(poll, sock->file);
  283. }
  284. static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
  285. struct vhost_virtqueue *vq,
  286. struct iovec iov[], unsigned int iov_size,
  287. unsigned int *out_num, unsigned int *in_num)
  288. {
  289. unsigned long uninitialized_var(endtime);
  290. int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  291. out_num, in_num, NULL, NULL);
  292. if (r == vq->num && vq->busyloop_timeout) {
  293. preempt_disable();
  294. endtime = busy_clock() + vq->busyloop_timeout;
  295. while (vhost_can_busy_poll(vq->dev, endtime) &&
  296. vhost_vq_avail_empty(vq->dev, vq))
  297. cpu_relax();
  298. preempt_enable();
  299. r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  300. out_num, in_num, NULL, NULL);
  301. }
  302. return r;
  303. }
  304. /* Expects to be always run from workqueue - which acts as
  305. * read-size critical section for our kind of RCU. */
  306. static void handle_tx(struct vhost_net *net)
  307. {
  308. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  309. struct vhost_virtqueue *vq = &nvq->vq;
  310. unsigned out, in;
  311. int head;
  312. struct msghdr msg = {
  313. .msg_name = NULL,
  314. .msg_namelen = 0,
  315. .msg_control = NULL,
  316. .msg_controllen = 0,
  317. .msg_flags = MSG_DONTWAIT,
  318. };
  319. size_t len, total_len = 0;
  320. int err;
  321. size_t hdr_size;
  322. struct socket *sock;
  323. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  324. bool zcopy, zcopy_used;
  325. mutex_lock(&vq->mutex);
  326. sock = vq->private_data;
  327. if (!sock)
  328. goto out;
  329. if (!vq_iotlb_prefetch(vq))
  330. goto out;
  331. vhost_disable_notify(&net->dev, vq);
  332. hdr_size = nvq->vhost_hlen;
  333. zcopy = nvq->ubufs;
  334. for (;;) {
  335. /* Release DMAs done buffers first */
  336. if (zcopy)
  337. vhost_zerocopy_signal_used(net, vq);
  338. /* If more outstanding DMAs, queue the work.
  339. * Handle upend_idx wrap around
  340. */
  341. if (unlikely((nvq->upend_idx + vq->num - VHOST_MAX_PEND)
  342. % UIO_MAXIOV == nvq->done_idx))
  343. break;
  344. head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
  345. ARRAY_SIZE(vq->iov),
  346. &out, &in);
  347. /* On error, stop handling until the next kick. */
  348. if (unlikely(head < 0))
  349. break;
  350. /* Nothing new? Wait for eventfd to tell us they refilled. */
  351. if (head == vq->num) {
  352. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  353. vhost_disable_notify(&net->dev, vq);
  354. continue;
  355. }
  356. break;
  357. }
  358. if (in) {
  359. vq_err(vq, "Unexpected descriptor format for TX: "
  360. "out %d, int %d\n", out, in);
  361. break;
  362. }
  363. /* Skip header. TODO: support TSO. */
  364. len = iov_length(vq->iov, out);
  365. iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
  366. iov_iter_advance(&msg.msg_iter, hdr_size);
  367. /* Sanity check */
  368. if (!msg_data_left(&msg)) {
  369. vq_err(vq, "Unexpected header len for TX: "
  370. "%zd expected %zd\n",
  371. len, hdr_size);
  372. break;
  373. }
  374. len = msg_data_left(&msg);
  375. zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
  376. && (nvq->upend_idx + 1) % UIO_MAXIOV !=
  377. nvq->done_idx
  378. && vhost_net_tx_select_zcopy(net);
  379. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  380. if (zcopy_used) {
  381. struct ubuf_info *ubuf;
  382. ubuf = nvq->ubuf_info + nvq->upend_idx;
  383. vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
  384. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  385. ubuf->callback = vhost_zerocopy_callback;
  386. ubuf->ctx = nvq->ubufs;
  387. ubuf->desc = nvq->upend_idx;
  388. msg.msg_control = ubuf;
  389. msg.msg_controllen = sizeof(ubuf);
  390. ubufs = nvq->ubufs;
  391. atomic_inc(&ubufs->refcount);
  392. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  393. } else {
  394. msg.msg_control = NULL;
  395. ubufs = NULL;
  396. }
  397. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  398. err = sock->ops->sendmsg(sock, &msg, len);
  399. if (unlikely(err < 0)) {
  400. if (zcopy_used) {
  401. vhost_net_ubuf_put(ubufs);
  402. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  403. % UIO_MAXIOV;
  404. }
  405. vhost_discard_vq_desc(vq, 1);
  406. break;
  407. }
  408. if (err != len)
  409. pr_debug("Truncated TX packet: "
  410. " len %d != %zd\n", err, len);
  411. if (!zcopy_used)
  412. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  413. else
  414. vhost_zerocopy_signal_used(net, vq);
  415. total_len += len;
  416. vhost_net_tx_packet(net);
  417. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  418. vhost_poll_queue(&vq->poll);
  419. break;
  420. }
  421. }
  422. out:
  423. mutex_unlock(&vq->mutex);
  424. }
  425. static int peek_head_len(struct sock *sk)
  426. {
  427. struct socket *sock = sk->sk_socket;
  428. struct sk_buff *head;
  429. int len = 0;
  430. unsigned long flags;
  431. if (sock->ops->peek_len)
  432. return sock->ops->peek_len(sock);
  433. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  434. head = skb_peek(&sk->sk_receive_queue);
  435. if (likely(head)) {
  436. len = head->len;
  437. if (skb_vlan_tag_present(head))
  438. len += VLAN_HLEN;
  439. }
  440. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  441. return len;
  442. }
  443. static int sk_has_rx_data(struct sock *sk)
  444. {
  445. struct socket *sock = sk->sk_socket;
  446. if (sock->ops->peek_len)
  447. return sock->ops->peek_len(sock);
  448. return skb_queue_empty(&sk->sk_receive_queue);
  449. }
  450. static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk)
  451. {
  452. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  453. struct vhost_virtqueue *vq = &nvq->vq;
  454. unsigned long uninitialized_var(endtime);
  455. int len = peek_head_len(sk);
  456. if (!len && vq->busyloop_timeout) {
  457. /* Both tx vq and rx socket were polled here */
  458. mutex_lock(&vq->mutex);
  459. vhost_disable_notify(&net->dev, vq);
  460. preempt_disable();
  461. endtime = busy_clock() + vq->busyloop_timeout;
  462. while (vhost_can_busy_poll(&net->dev, endtime) &&
  463. !sk_has_rx_data(sk) &&
  464. vhost_vq_avail_empty(&net->dev, vq))
  465. cpu_relax();
  466. preempt_enable();
  467. if (vhost_enable_notify(&net->dev, vq))
  468. vhost_poll_queue(&vq->poll);
  469. mutex_unlock(&vq->mutex);
  470. len = peek_head_len(sk);
  471. }
  472. return len;
  473. }
  474. /* This is a multi-buffer version of vhost_get_desc, that works if
  475. * vq has read descriptors only.
  476. * @vq - the relevant virtqueue
  477. * @datalen - data length we'll be reading
  478. * @iovcount - returned count of io vectors we fill
  479. * @log - vhost log
  480. * @log_num - log offset
  481. * @quota - headcount quota, 1 for big buffer
  482. * returns number of buffer heads allocated, negative on error
  483. */
  484. static int get_rx_bufs(struct vhost_virtqueue *vq,
  485. struct vring_used_elem *heads,
  486. int datalen,
  487. unsigned *iovcount,
  488. struct vhost_log *log,
  489. unsigned *log_num,
  490. unsigned int quota)
  491. {
  492. unsigned int out, in;
  493. int seg = 0;
  494. int headcount = 0;
  495. unsigned d;
  496. int r, nlogs = 0;
  497. /* len is always initialized before use since we are always called with
  498. * datalen > 0.
  499. */
  500. u32 uninitialized_var(len);
  501. while (datalen > 0 && headcount < quota) {
  502. if (unlikely(seg >= UIO_MAXIOV)) {
  503. r = -ENOBUFS;
  504. goto err;
  505. }
  506. r = vhost_get_vq_desc(vq, vq->iov + seg,
  507. ARRAY_SIZE(vq->iov) - seg, &out,
  508. &in, log, log_num);
  509. if (unlikely(r < 0))
  510. goto err;
  511. d = r;
  512. if (d == vq->num) {
  513. r = 0;
  514. goto err;
  515. }
  516. if (unlikely(out || in <= 0)) {
  517. vq_err(vq, "unexpected descriptor format for RX: "
  518. "out %d, in %d\n", out, in);
  519. r = -EINVAL;
  520. goto err;
  521. }
  522. if (unlikely(log)) {
  523. nlogs += *log_num;
  524. log += *log_num;
  525. }
  526. heads[headcount].id = cpu_to_vhost32(vq, d);
  527. len = iov_length(vq->iov + seg, in);
  528. heads[headcount].len = cpu_to_vhost32(vq, len);
  529. datalen -= len;
  530. ++headcount;
  531. seg += in;
  532. }
  533. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  534. *iovcount = seg;
  535. if (unlikely(log))
  536. *log_num = nlogs;
  537. /* Detect overrun */
  538. if (unlikely(datalen > 0)) {
  539. r = UIO_MAXIOV + 1;
  540. goto err;
  541. }
  542. return headcount;
  543. err:
  544. vhost_discard_vq_desc(vq, headcount);
  545. return r;
  546. }
  547. /* Expects to be always run from workqueue - which acts as
  548. * read-size critical section for our kind of RCU. */
  549. static void handle_rx(struct vhost_net *net)
  550. {
  551. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  552. struct vhost_virtqueue *vq = &nvq->vq;
  553. unsigned uninitialized_var(in), log;
  554. struct vhost_log *vq_log;
  555. struct msghdr msg = {
  556. .msg_name = NULL,
  557. .msg_namelen = 0,
  558. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  559. .msg_controllen = 0,
  560. .msg_flags = MSG_DONTWAIT,
  561. };
  562. struct virtio_net_hdr hdr = {
  563. .flags = 0,
  564. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  565. };
  566. size_t total_len = 0;
  567. int err, mergeable;
  568. s16 headcount;
  569. size_t vhost_hlen, sock_hlen;
  570. size_t vhost_len, sock_len;
  571. struct socket *sock;
  572. struct iov_iter fixup;
  573. __virtio16 num_buffers;
  574. mutex_lock(&vq->mutex);
  575. sock = vq->private_data;
  576. if (!sock)
  577. goto out;
  578. if (!vq_iotlb_prefetch(vq))
  579. goto out;
  580. vhost_disable_notify(&net->dev, vq);
  581. vhost_net_disable_vq(net, vq);
  582. vhost_hlen = nvq->vhost_hlen;
  583. sock_hlen = nvq->sock_hlen;
  584. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  585. vq->log : NULL;
  586. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  587. while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
  588. sock_len += sock_hlen;
  589. vhost_len = sock_len + vhost_hlen;
  590. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  591. &in, vq_log, &log,
  592. likely(mergeable) ? UIO_MAXIOV : 1);
  593. /* On error, stop handling until the next kick. */
  594. if (unlikely(headcount < 0))
  595. goto out;
  596. /* On overrun, truncate and discard */
  597. if (unlikely(headcount > UIO_MAXIOV)) {
  598. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  599. err = sock->ops->recvmsg(sock, &msg,
  600. 1, MSG_DONTWAIT | MSG_TRUNC);
  601. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  602. continue;
  603. }
  604. /* OK, now we need to know about added descriptors. */
  605. if (!headcount) {
  606. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  607. /* They have slipped one in as we were
  608. * doing that: check again. */
  609. vhost_disable_notify(&net->dev, vq);
  610. continue;
  611. }
  612. /* Nothing new? Wait for eventfd to tell us
  613. * they refilled. */
  614. goto out;
  615. }
  616. /* We don't need to be notified again. */
  617. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  618. fixup = msg.msg_iter;
  619. if (unlikely((vhost_hlen))) {
  620. /* We will supply the header ourselves
  621. * TODO: support TSO.
  622. */
  623. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  624. }
  625. err = sock->ops->recvmsg(sock, &msg,
  626. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  627. /* Userspace might have consumed the packet meanwhile:
  628. * it's not supposed to do this usually, but might be hard
  629. * to prevent. Discard data we got (if any) and keep going. */
  630. if (unlikely(err != sock_len)) {
  631. pr_debug("Discarded rx packet: "
  632. " len %d, expected %zd\n", err, sock_len);
  633. vhost_discard_vq_desc(vq, headcount);
  634. continue;
  635. }
  636. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  637. if (unlikely(vhost_hlen)) {
  638. if (copy_to_iter(&hdr, sizeof(hdr),
  639. &fixup) != sizeof(hdr)) {
  640. vq_err(vq, "Unable to write vnet_hdr "
  641. "at addr %p\n", vq->iov->iov_base);
  642. goto out;
  643. }
  644. } else {
  645. /* Header came from socket; we'll need to patch
  646. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  647. */
  648. iov_iter_advance(&fixup, sizeof(hdr));
  649. }
  650. /* TODO: Should check and handle checksum. */
  651. num_buffers = cpu_to_vhost16(vq, headcount);
  652. if (likely(mergeable) &&
  653. copy_to_iter(&num_buffers, sizeof num_buffers,
  654. &fixup) != sizeof num_buffers) {
  655. vq_err(vq, "Failed num_buffers write");
  656. vhost_discard_vq_desc(vq, headcount);
  657. goto out;
  658. }
  659. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  660. headcount);
  661. if (unlikely(vq_log))
  662. vhost_log_write(vq, vq_log, log, vhost_len);
  663. total_len += vhost_len;
  664. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  665. vhost_poll_queue(&vq->poll);
  666. goto out;
  667. }
  668. }
  669. vhost_net_enable_vq(net, vq);
  670. out:
  671. mutex_unlock(&vq->mutex);
  672. }
  673. static void handle_tx_kick(struct vhost_work *work)
  674. {
  675. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  676. poll.work);
  677. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  678. handle_tx(net);
  679. }
  680. static void handle_rx_kick(struct vhost_work *work)
  681. {
  682. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  683. poll.work);
  684. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  685. handle_rx(net);
  686. }
  687. static void handle_tx_net(struct vhost_work *work)
  688. {
  689. struct vhost_net *net = container_of(work, struct vhost_net,
  690. poll[VHOST_NET_VQ_TX].work);
  691. handle_tx(net);
  692. }
  693. static void handle_rx_net(struct vhost_work *work)
  694. {
  695. struct vhost_net *net = container_of(work, struct vhost_net,
  696. poll[VHOST_NET_VQ_RX].work);
  697. handle_rx(net);
  698. }
  699. static int vhost_net_open(struct inode *inode, struct file *f)
  700. {
  701. struct vhost_net *n;
  702. struct vhost_dev *dev;
  703. struct vhost_virtqueue **vqs;
  704. int i;
  705. n = kmalloc(sizeof *n, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  706. if (!n) {
  707. n = vmalloc(sizeof *n);
  708. if (!n)
  709. return -ENOMEM;
  710. }
  711. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  712. if (!vqs) {
  713. kvfree(n);
  714. return -ENOMEM;
  715. }
  716. dev = &n->dev;
  717. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  718. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  719. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  720. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  721. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  722. n->vqs[i].ubufs = NULL;
  723. n->vqs[i].ubuf_info = NULL;
  724. n->vqs[i].upend_idx = 0;
  725. n->vqs[i].done_idx = 0;
  726. n->vqs[i].vhost_hlen = 0;
  727. n->vqs[i].sock_hlen = 0;
  728. }
  729. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  730. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  731. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  732. f->private_data = n;
  733. return 0;
  734. }
  735. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  736. struct vhost_virtqueue *vq)
  737. {
  738. struct socket *sock;
  739. mutex_lock(&vq->mutex);
  740. sock = vq->private_data;
  741. vhost_net_disable_vq(n, vq);
  742. vq->private_data = NULL;
  743. mutex_unlock(&vq->mutex);
  744. return sock;
  745. }
  746. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  747. struct socket **rx_sock)
  748. {
  749. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  750. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  751. }
  752. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  753. {
  754. vhost_poll_flush(n->poll + index);
  755. vhost_poll_flush(&n->vqs[index].vq.poll);
  756. }
  757. static void vhost_net_flush(struct vhost_net *n)
  758. {
  759. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  760. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  761. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  762. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  763. n->tx_flush = true;
  764. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  765. /* Wait for all lower device DMAs done. */
  766. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  767. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  768. n->tx_flush = false;
  769. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  770. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  771. }
  772. }
  773. static int vhost_net_release(struct inode *inode, struct file *f)
  774. {
  775. struct vhost_net *n = f->private_data;
  776. struct socket *tx_sock;
  777. struct socket *rx_sock;
  778. vhost_net_stop(n, &tx_sock, &rx_sock);
  779. vhost_net_flush(n);
  780. vhost_dev_stop(&n->dev);
  781. vhost_dev_cleanup(&n->dev, false);
  782. vhost_net_vq_reset(n);
  783. if (tx_sock)
  784. sockfd_put(tx_sock);
  785. if (rx_sock)
  786. sockfd_put(rx_sock);
  787. /* Make sure no callbacks are outstanding */
  788. synchronize_rcu_bh();
  789. /* We do an extra flush before freeing memory,
  790. * since jobs can re-queue themselves. */
  791. vhost_net_flush(n);
  792. kfree(n->dev.vqs);
  793. kvfree(n);
  794. return 0;
  795. }
  796. static struct socket *get_raw_socket(int fd)
  797. {
  798. struct {
  799. struct sockaddr_ll sa;
  800. char buf[MAX_ADDR_LEN];
  801. } uaddr;
  802. int uaddr_len = sizeof uaddr, r;
  803. struct socket *sock = sockfd_lookup(fd, &r);
  804. if (!sock)
  805. return ERR_PTR(-ENOTSOCK);
  806. /* Parameter checking */
  807. if (sock->sk->sk_type != SOCK_RAW) {
  808. r = -ESOCKTNOSUPPORT;
  809. goto err;
  810. }
  811. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  812. &uaddr_len, 0);
  813. if (r)
  814. goto err;
  815. if (uaddr.sa.sll_family != AF_PACKET) {
  816. r = -EPFNOSUPPORT;
  817. goto err;
  818. }
  819. return sock;
  820. err:
  821. sockfd_put(sock);
  822. return ERR_PTR(r);
  823. }
  824. static struct socket *get_tap_socket(int fd)
  825. {
  826. struct file *file = fget(fd);
  827. struct socket *sock;
  828. if (!file)
  829. return ERR_PTR(-EBADF);
  830. sock = tun_get_socket(file);
  831. if (!IS_ERR(sock))
  832. return sock;
  833. sock = macvtap_get_socket(file);
  834. if (IS_ERR(sock))
  835. fput(file);
  836. return sock;
  837. }
  838. static struct socket *get_socket(int fd)
  839. {
  840. struct socket *sock;
  841. /* special case to disable backend */
  842. if (fd == -1)
  843. return NULL;
  844. sock = get_raw_socket(fd);
  845. if (!IS_ERR(sock))
  846. return sock;
  847. sock = get_tap_socket(fd);
  848. if (!IS_ERR(sock))
  849. return sock;
  850. return ERR_PTR(-ENOTSOCK);
  851. }
  852. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  853. {
  854. struct socket *sock, *oldsock;
  855. struct vhost_virtqueue *vq;
  856. struct vhost_net_virtqueue *nvq;
  857. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  858. int r;
  859. mutex_lock(&n->dev.mutex);
  860. r = vhost_dev_check_owner(&n->dev);
  861. if (r)
  862. goto err;
  863. if (index >= VHOST_NET_VQ_MAX) {
  864. r = -ENOBUFS;
  865. goto err;
  866. }
  867. vq = &n->vqs[index].vq;
  868. nvq = &n->vqs[index];
  869. mutex_lock(&vq->mutex);
  870. /* Verify that ring has been setup correctly. */
  871. if (!vhost_vq_access_ok(vq)) {
  872. r = -EFAULT;
  873. goto err_vq;
  874. }
  875. sock = get_socket(fd);
  876. if (IS_ERR(sock)) {
  877. r = PTR_ERR(sock);
  878. goto err_vq;
  879. }
  880. /* start polling new socket */
  881. oldsock = vq->private_data;
  882. if (sock != oldsock) {
  883. ubufs = vhost_net_ubuf_alloc(vq,
  884. sock && vhost_sock_zcopy(sock));
  885. if (IS_ERR(ubufs)) {
  886. r = PTR_ERR(ubufs);
  887. goto err_ubufs;
  888. }
  889. vhost_net_disable_vq(n, vq);
  890. vq->private_data = sock;
  891. r = vhost_vq_init_access(vq);
  892. if (r)
  893. goto err_used;
  894. r = vhost_net_enable_vq(n, vq);
  895. if (r)
  896. goto err_used;
  897. oldubufs = nvq->ubufs;
  898. nvq->ubufs = ubufs;
  899. n->tx_packets = 0;
  900. n->tx_zcopy_err = 0;
  901. n->tx_flush = false;
  902. }
  903. mutex_unlock(&vq->mutex);
  904. if (oldubufs) {
  905. vhost_net_ubuf_put_wait_and_free(oldubufs);
  906. mutex_lock(&vq->mutex);
  907. vhost_zerocopy_signal_used(n, vq);
  908. mutex_unlock(&vq->mutex);
  909. }
  910. if (oldsock) {
  911. vhost_net_flush_vq(n, index);
  912. sockfd_put(oldsock);
  913. }
  914. mutex_unlock(&n->dev.mutex);
  915. return 0;
  916. err_used:
  917. vq->private_data = oldsock;
  918. vhost_net_enable_vq(n, vq);
  919. if (ubufs)
  920. vhost_net_ubuf_put_wait_and_free(ubufs);
  921. err_ubufs:
  922. sockfd_put(sock);
  923. err_vq:
  924. mutex_unlock(&vq->mutex);
  925. err:
  926. mutex_unlock(&n->dev.mutex);
  927. return r;
  928. }
  929. static long vhost_net_reset_owner(struct vhost_net *n)
  930. {
  931. struct socket *tx_sock = NULL;
  932. struct socket *rx_sock = NULL;
  933. long err;
  934. struct vhost_umem *umem;
  935. mutex_lock(&n->dev.mutex);
  936. err = vhost_dev_check_owner(&n->dev);
  937. if (err)
  938. goto done;
  939. umem = vhost_dev_reset_owner_prepare();
  940. if (!umem) {
  941. err = -ENOMEM;
  942. goto done;
  943. }
  944. vhost_net_stop(n, &tx_sock, &rx_sock);
  945. vhost_net_flush(n);
  946. vhost_dev_reset_owner(&n->dev, umem);
  947. vhost_net_vq_reset(n);
  948. done:
  949. mutex_unlock(&n->dev.mutex);
  950. if (tx_sock)
  951. sockfd_put(tx_sock);
  952. if (rx_sock)
  953. sockfd_put(rx_sock);
  954. return err;
  955. }
  956. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  957. {
  958. size_t vhost_hlen, sock_hlen, hdr_len;
  959. int i;
  960. hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  961. (1ULL << VIRTIO_F_VERSION_1))) ?
  962. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  963. sizeof(struct virtio_net_hdr);
  964. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  965. /* vhost provides vnet_hdr */
  966. vhost_hlen = hdr_len;
  967. sock_hlen = 0;
  968. } else {
  969. /* socket provides vnet_hdr */
  970. vhost_hlen = 0;
  971. sock_hlen = hdr_len;
  972. }
  973. mutex_lock(&n->dev.mutex);
  974. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  975. !vhost_log_access_ok(&n->dev))
  976. goto out_unlock;
  977. if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
  978. if (vhost_init_device_iotlb(&n->dev, true))
  979. goto out_unlock;
  980. }
  981. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  982. mutex_lock(&n->vqs[i].vq.mutex);
  983. n->vqs[i].vq.acked_features = features;
  984. n->vqs[i].vhost_hlen = vhost_hlen;
  985. n->vqs[i].sock_hlen = sock_hlen;
  986. mutex_unlock(&n->vqs[i].vq.mutex);
  987. }
  988. mutex_unlock(&n->dev.mutex);
  989. return 0;
  990. out_unlock:
  991. mutex_unlock(&n->dev.mutex);
  992. return -EFAULT;
  993. }
  994. static long vhost_net_set_owner(struct vhost_net *n)
  995. {
  996. int r;
  997. mutex_lock(&n->dev.mutex);
  998. if (vhost_dev_has_owner(&n->dev)) {
  999. r = -EBUSY;
  1000. goto out;
  1001. }
  1002. r = vhost_net_set_ubuf_info(n);
  1003. if (r)
  1004. goto out;
  1005. r = vhost_dev_set_owner(&n->dev);
  1006. if (r)
  1007. vhost_net_clear_ubuf_info(n);
  1008. vhost_net_flush(n);
  1009. out:
  1010. mutex_unlock(&n->dev.mutex);
  1011. return r;
  1012. }
  1013. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  1014. unsigned long arg)
  1015. {
  1016. struct vhost_net *n = f->private_data;
  1017. void __user *argp = (void __user *)arg;
  1018. u64 __user *featurep = argp;
  1019. struct vhost_vring_file backend;
  1020. u64 features;
  1021. int r;
  1022. switch (ioctl) {
  1023. case VHOST_NET_SET_BACKEND:
  1024. if (copy_from_user(&backend, argp, sizeof backend))
  1025. return -EFAULT;
  1026. return vhost_net_set_backend(n, backend.index, backend.fd);
  1027. case VHOST_GET_FEATURES:
  1028. features = VHOST_NET_FEATURES;
  1029. if (copy_to_user(featurep, &features, sizeof features))
  1030. return -EFAULT;
  1031. return 0;
  1032. case VHOST_SET_FEATURES:
  1033. if (copy_from_user(&features, featurep, sizeof features))
  1034. return -EFAULT;
  1035. if (features & ~VHOST_NET_FEATURES)
  1036. return -EOPNOTSUPP;
  1037. return vhost_net_set_features(n, features);
  1038. case VHOST_RESET_OWNER:
  1039. return vhost_net_reset_owner(n);
  1040. case VHOST_SET_OWNER:
  1041. return vhost_net_set_owner(n);
  1042. default:
  1043. mutex_lock(&n->dev.mutex);
  1044. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  1045. if (r == -ENOIOCTLCMD)
  1046. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  1047. else
  1048. vhost_net_flush(n);
  1049. mutex_unlock(&n->dev.mutex);
  1050. return r;
  1051. }
  1052. }
  1053. #ifdef CONFIG_COMPAT
  1054. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  1055. unsigned long arg)
  1056. {
  1057. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1058. }
  1059. #endif
  1060. static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1061. {
  1062. struct file *file = iocb->ki_filp;
  1063. struct vhost_net *n = file->private_data;
  1064. struct vhost_dev *dev = &n->dev;
  1065. int noblock = file->f_flags & O_NONBLOCK;
  1066. return vhost_chr_read_iter(dev, to, noblock);
  1067. }
  1068. static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
  1069. struct iov_iter *from)
  1070. {
  1071. struct file *file = iocb->ki_filp;
  1072. struct vhost_net *n = file->private_data;
  1073. struct vhost_dev *dev = &n->dev;
  1074. return vhost_chr_write_iter(dev, from);
  1075. }
  1076. static unsigned int vhost_net_chr_poll(struct file *file, poll_table *wait)
  1077. {
  1078. struct vhost_net *n = file->private_data;
  1079. struct vhost_dev *dev = &n->dev;
  1080. return vhost_chr_poll(file, dev, wait);
  1081. }
  1082. static const struct file_operations vhost_net_fops = {
  1083. .owner = THIS_MODULE,
  1084. .release = vhost_net_release,
  1085. .read_iter = vhost_net_chr_read_iter,
  1086. .write_iter = vhost_net_chr_write_iter,
  1087. .poll = vhost_net_chr_poll,
  1088. .unlocked_ioctl = vhost_net_ioctl,
  1089. #ifdef CONFIG_COMPAT
  1090. .compat_ioctl = vhost_net_compat_ioctl,
  1091. #endif
  1092. .open = vhost_net_open,
  1093. .llseek = noop_llseek,
  1094. };
  1095. static struct miscdevice vhost_net_misc = {
  1096. .minor = VHOST_NET_MINOR,
  1097. .name = "vhost-net",
  1098. .fops = &vhost_net_fops,
  1099. };
  1100. static int vhost_net_init(void)
  1101. {
  1102. if (experimental_zcopytx)
  1103. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1104. return misc_register(&vhost_net_misc);
  1105. }
  1106. module_init(vhost_net_init);
  1107. static void vhost_net_exit(void)
  1108. {
  1109. misc_deregister(&vhost_net_misc);
  1110. }
  1111. module_exit(vhost_net_exit);
  1112. MODULE_VERSION("0.0.1");
  1113. MODULE_LICENSE("GPL v2");
  1114. MODULE_AUTHOR("Michael S. Tsirkin");
  1115. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1116. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1117. MODULE_ALIAS("devname:vhost-net");