net.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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_lowlatency();
  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_nested(&vq->mutex, 1);
  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_lowlatency();
  466. preempt_enable();
  467. if (!vhost_vq_avail_empty(&net->dev, vq))
  468. vhost_poll_queue(&vq->poll);
  469. else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  470. vhost_disable_notify(&net->dev, vq);
  471. vhost_poll_queue(&vq->poll);
  472. }
  473. mutex_unlock(&vq->mutex);
  474. len = peek_head_len(sk);
  475. }
  476. return len;
  477. }
  478. /* This is a multi-buffer version of vhost_get_desc, that works if
  479. * vq has read descriptors only.
  480. * @vq - the relevant virtqueue
  481. * @datalen - data length we'll be reading
  482. * @iovcount - returned count of io vectors we fill
  483. * @log - vhost log
  484. * @log_num - log offset
  485. * @quota - headcount quota, 1 for big buffer
  486. * returns number of buffer heads allocated, negative on error
  487. */
  488. static int get_rx_bufs(struct vhost_virtqueue *vq,
  489. struct vring_used_elem *heads,
  490. int datalen,
  491. unsigned *iovcount,
  492. struct vhost_log *log,
  493. unsigned *log_num,
  494. unsigned int quota)
  495. {
  496. unsigned int out, in;
  497. int seg = 0;
  498. int headcount = 0;
  499. unsigned d;
  500. int r, nlogs = 0;
  501. /* len is always initialized before use since we are always called with
  502. * datalen > 0.
  503. */
  504. u32 uninitialized_var(len);
  505. while (datalen > 0 && headcount < quota) {
  506. if (unlikely(seg >= UIO_MAXIOV)) {
  507. r = -ENOBUFS;
  508. goto err;
  509. }
  510. r = vhost_get_vq_desc(vq, vq->iov + seg,
  511. ARRAY_SIZE(vq->iov) - seg, &out,
  512. &in, log, log_num);
  513. if (unlikely(r < 0))
  514. goto err;
  515. d = r;
  516. if (d == vq->num) {
  517. r = 0;
  518. goto err;
  519. }
  520. if (unlikely(out || in <= 0)) {
  521. vq_err(vq, "unexpected descriptor format for RX: "
  522. "out %d, in %d\n", out, in);
  523. r = -EINVAL;
  524. goto err;
  525. }
  526. if (unlikely(log)) {
  527. nlogs += *log_num;
  528. log += *log_num;
  529. }
  530. heads[headcount].id = cpu_to_vhost32(vq, d);
  531. len = iov_length(vq->iov + seg, in);
  532. heads[headcount].len = cpu_to_vhost32(vq, len);
  533. datalen -= len;
  534. ++headcount;
  535. seg += in;
  536. }
  537. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  538. *iovcount = seg;
  539. if (unlikely(log))
  540. *log_num = nlogs;
  541. /* Detect overrun */
  542. if (unlikely(datalen > 0)) {
  543. r = UIO_MAXIOV + 1;
  544. goto err;
  545. }
  546. return headcount;
  547. err:
  548. vhost_discard_vq_desc(vq, headcount);
  549. return r;
  550. }
  551. /* Expects to be always run from workqueue - which acts as
  552. * read-size critical section for our kind of RCU. */
  553. static void handle_rx(struct vhost_net *net)
  554. {
  555. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  556. struct vhost_virtqueue *vq = &nvq->vq;
  557. unsigned uninitialized_var(in), log;
  558. struct vhost_log *vq_log;
  559. struct msghdr msg = {
  560. .msg_name = NULL,
  561. .msg_namelen = 0,
  562. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  563. .msg_controllen = 0,
  564. .msg_flags = MSG_DONTWAIT,
  565. };
  566. struct virtio_net_hdr hdr = {
  567. .flags = 0,
  568. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  569. };
  570. size_t total_len = 0;
  571. int err, mergeable;
  572. s16 headcount;
  573. size_t vhost_hlen, sock_hlen;
  574. size_t vhost_len, sock_len;
  575. struct socket *sock;
  576. struct iov_iter fixup;
  577. __virtio16 num_buffers;
  578. mutex_lock_nested(&vq->mutex, 0);
  579. sock = vq->private_data;
  580. if (!sock)
  581. goto out;
  582. if (!vq_iotlb_prefetch(vq))
  583. goto out;
  584. vhost_disable_notify(&net->dev, vq);
  585. vhost_net_disable_vq(net, vq);
  586. vhost_hlen = nvq->vhost_hlen;
  587. sock_hlen = nvq->sock_hlen;
  588. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  589. vq->log : NULL;
  590. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  591. while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
  592. sock_len += sock_hlen;
  593. vhost_len = sock_len + vhost_hlen;
  594. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  595. &in, vq_log, &log,
  596. likely(mergeable) ? UIO_MAXIOV : 1);
  597. /* On error, stop handling until the next kick. */
  598. if (unlikely(headcount < 0))
  599. goto out;
  600. /* On overrun, truncate and discard */
  601. if (unlikely(headcount > UIO_MAXIOV)) {
  602. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  603. err = sock->ops->recvmsg(sock, &msg,
  604. 1, MSG_DONTWAIT | MSG_TRUNC);
  605. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  606. continue;
  607. }
  608. /* OK, now we need to know about added descriptors. */
  609. if (!headcount) {
  610. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  611. /* They have slipped one in as we were
  612. * doing that: check again. */
  613. vhost_disable_notify(&net->dev, vq);
  614. continue;
  615. }
  616. /* Nothing new? Wait for eventfd to tell us
  617. * they refilled. */
  618. goto out;
  619. }
  620. /* We don't need to be notified again. */
  621. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  622. fixup = msg.msg_iter;
  623. if (unlikely((vhost_hlen))) {
  624. /* We will supply the header ourselves
  625. * TODO: support TSO.
  626. */
  627. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  628. }
  629. err = sock->ops->recvmsg(sock, &msg,
  630. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  631. /* Userspace might have consumed the packet meanwhile:
  632. * it's not supposed to do this usually, but might be hard
  633. * to prevent. Discard data we got (if any) and keep going. */
  634. if (unlikely(err != sock_len)) {
  635. pr_debug("Discarded rx packet: "
  636. " len %d, expected %zd\n", err, sock_len);
  637. vhost_discard_vq_desc(vq, headcount);
  638. continue;
  639. }
  640. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  641. if (unlikely(vhost_hlen)) {
  642. if (copy_to_iter(&hdr, sizeof(hdr),
  643. &fixup) != sizeof(hdr)) {
  644. vq_err(vq, "Unable to write vnet_hdr "
  645. "at addr %p\n", vq->iov->iov_base);
  646. goto out;
  647. }
  648. } else {
  649. /* Header came from socket; we'll need to patch
  650. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  651. */
  652. iov_iter_advance(&fixup, sizeof(hdr));
  653. }
  654. /* TODO: Should check and handle checksum. */
  655. num_buffers = cpu_to_vhost16(vq, headcount);
  656. if (likely(mergeable) &&
  657. copy_to_iter(&num_buffers, sizeof num_buffers,
  658. &fixup) != sizeof num_buffers) {
  659. vq_err(vq, "Failed num_buffers write");
  660. vhost_discard_vq_desc(vq, headcount);
  661. goto out;
  662. }
  663. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  664. headcount);
  665. if (unlikely(vq_log))
  666. vhost_log_write(vq, vq_log, log, vhost_len);
  667. total_len += vhost_len;
  668. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  669. vhost_poll_queue(&vq->poll);
  670. goto out;
  671. }
  672. }
  673. vhost_net_enable_vq(net, vq);
  674. out:
  675. mutex_unlock(&vq->mutex);
  676. }
  677. static void handle_tx_kick(struct vhost_work *work)
  678. {
  679. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  680. poll.work);
  681. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  682. handle_tx(net);
  683. }
  684. static void handle_rx_kick(struct vhost_work *work)
  685. {
  686. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  687. poll.work);
  688. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  689. handle_rx(net);
  690. }
  691. static void handle_tx_net(struct vhost_work *work)
  692. {
  693. struct vhost_net *net = container_of(work, struct vhost_net,
  694. poll[VHOST_NET_VQ_TX].work);
  695. handle_tx(net);
  696. }
  697. static void handle_rx_net(struct vhost_work *work)
  698. {
  699. struct vhost_net *net = container_of(work, struct vhost_net,
  700. poll[VHOST_NET_VQ_RX].work);
  701. handle_rx(net);
  702. }
  703. static int vhost_net_open(struct inode *inode, struct file *f)
  704. {
  705. struct vhost_net *n;
  706. struct vhost_dev *dev;
  707. struct vhost_virtqueue **vqs;
  708. int i;
  709. n = kmalloc(sizeof *n, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  710. if (!n) {
  711. n = vmalloc(sizeof *n);
  712. if (!n)
  713. return -ENOMEM;
  714. }
  715. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  716. if (!vqs) {
  717. kvfree(n);
  718. return -ENOMEM;
  719. }
  720. dev = &n->dev;
  721. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  722. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  723. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  724. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  725. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  726. n->vqs[i].ubufs = NULL;
  727. n->vqs[i].ubuf_info = NULL;
  728. n->vqs[i].upend_idx = 0;
  729. n->vqs[i].done_idx = 0;
  730. n->vqs[i].vhost_hlen = 0;
  731. n->vqs[i].sock_hlen = 0;
  732. }
  733. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  734. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  735. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  736. f->private_data = n;
  737. return 0;
  738. }
  739. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  740. struct vhost_virtqueue *vq)
  741. {
  742. struct socket *sock;
  743. mutex_lock(&vq->mutex);
  744. sock = vq->private_data;
  745. vhost_net_disable_vq(n, vq);
  746. vq->private_data = NULL;
  747. mutex_unlock(&vq->mutex);
  748. return sock;
  749. }
  750. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  751. struct socket **rx_sock)
  752. {
  753. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  754. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  755. }
  756. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  757. {
  758. vhost_poll_flush(n->poll + index);
  759. vhost_poll_flush(&n->vqs[index].vq.poll);
  760. }
  761. static void vhost_net_flush(struct vhost_net *n)
  762. {
  763. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  764. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  765. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  766. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  767. n->tx_flush = true;
  768. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  769. /* Wait for all lower device DMAs done. */
  770. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  771. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  772. n->tx_flush = false;
  773. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  774. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  775. }
  776. }
  777. static int vhost_net_release(struct inode *inode, struct file *f)
  778. {
  779. struct vhost_net *n = f->private_data;
  780. struct socket *tx_sock;
  781. struct socket *rx_sock;
  782. vhost_net_stop(n, &tx_sock, &rx_sock);
  783. vhost_net_flush(n);
  784. vhost_dev_stop(&n->dev);
  785. vhost_dev_cleanup(&n->dev, false);
  786. vhost_net_vq_reset(n);
  787. if (tx_sock)
  788. sockfd_put(tx_sock);
  789. if (rx_sock)
  790. sockfd_put(rx_sock);
  791. /* Make sure no callbacks are outstanding */
  792. synchronize_rcu_bh();
  793. /* We do an extra flush before freeing memory,
  794. * since jobs can re-queue themselves. */
  795. vhost_net_flush(n);
  796. kfree(n->dev.vqs);
  797. kvfree(n);
  798. return 0;
  799. }
  800. static struct socket *get_raw_socket(int fd)
  801. {
  802. struct {
  803. struct sockaddr_ll sa;
  804. char buf[MAX_ADDR_LEN];
  805. } uaddr;
  806. int uaddr_len = sizeof uaddr, r;
  807. struct socket *sock = sockfd_lookup(fd, &r);
  808. if (!sock)
  809. return ERR_PTR(-ENOTSOCK);
  810. /* Parameter checking */
  811. if (sock->sk->sk_type != SOCK_RAW) {
  812. r = -ESOCKTNOSUPPORT;
  813. goto err;
  814. }
  815. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  816. &uaddr_len, 0);
  817. if (r)
  818. goto err;
  819. if (uaddr.sa.sll_family != AF_PACKET) {
  820. r = -EPFNOSUPPORT;
  821. goto err;
  822. }
  823. return sock;
  824. err:
  825. sockfd_put(sock);
  826. return ERR_PTR(r);
  827. }
  828. static struct socket *get_tap_socket(int fd)
  829. {
  830. struct file *file = fget(fd);
  831. struct socket *sock;
  832. if (!file)
  833. return ERR_PTR(-EBADF);
  834. sock = tun_get_socket(file);
  835. if (!IS_ERR(sock))
  836. return sock;
  837. sock = macvtap_get_socket(file);
  838. if (IS_ERR(sock))
  839. fput(file);
  840. return sock;
  841. }
  842. static struct socket *get_socket(int fd)
  843. {
  844. struct socket *sock;
  845. /* special case to disable backend */
  846. if (fd == -1)
  847. return NULL;
  848. sock = get_raw_socket(fd);
  849. if (!IS_ERR(sock))
  850. return sock;
  851. sock = get_tap_socket(fd);
  852. if (!IS_ERR(sock))
  853. return sock;
  854. return ERR_PTR(-ENOTSOCK);
  855. }
  856. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  857. {
  858. struct socket *sock, *oldsock;
  859. struct vhost_virtqueue *vq;
  860. struct vhost_net_virtqueue *nvq;
  861. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  862. int r;
  863. mutex_lock(&n->dev.mutex);
  864. r = vhost_dev_check_owner(&n->dev);
  865. if (r)
  866. goto err;
  867. if (index >= VHOST_NET_VQ_MAX) {
  868. r = -ENOBUFS;
  869. goto err;
  870. }
  871. vq = &n->vqs[index].vq;
  872. nvq = &n->vqs[index];
  873. mutex_lock(&vq->mutex);
  874. /* Verify that ring has been setup correctly. */
  875. if (!vhost_vq_access_ok(vq)) {
  876. r = -EFAULT;
  877. goto err_vq;
  878. }
  879. sock = get_socket(fd);
  880. if (IS_ERR(sock)) {
  881. r = PTR_ERR(sock);
  882. goto err_vq;
  883. }
  884. /* start polling new socket */
  885. oldsock = vq->private_data;
  886. if (sock != oldsock) {
  887. ubufs = vhost_net_ubuf_alloc(vq,
  888. sock && vhost_sock_zcopy(sock));
  889. if (IS_ERR(ubufs)) {
  890. r = PTR_ERR(ubufs);
  891. goto err_ubufs;
  892. }
  893. vhost_net_disable_vq(n, vq);
  894. vq->private_data = sock;
  895. r = vhost_vq_init_access(vq);
  896. if (r)
  897. goto err_used;
  898. r = vhost_net_enable_vq(n, vq);
  899. if (r)
  900. goto err_used;
  901. oldubufs = nvq->ubufs;
  902. nvq->ubufs = ubufs;
  903. n->tx_packets = 0;
  904. n->tx_zcopy_err = 0;
  905. n->tx_flush = false;
  906. }
  907. mutex_unlock(&vq->mutex);
  908. if (oldubufs) {
  909. vhost_net_ubuf_put_wait_and_free(oldubufs);
  910. mutex_lock(&vq->mutex);
  911. vhost_zerocopy_signal_used(n, vq);
  912. mutex_unlock(&vq->mutex);
  913. }
  914. if (oldsock) {
  915. vhost_net_flush_vq(n, index);
  916. sockfd_put(oldsock);
  917. }
  918. mutex_unlock(&n->dev.mutex);
  919. return 0;
  920. err_used:
  921. vq->private_data = oldsock;
  922. vhost_net_enable_vq(n, vq);
  923. if (ubufs)
  924. vhost_net_ubuf_put_wait_and_free(ubufs);
  925. err_ubufs:
  926. if (sock)
  927. sockfd_put(sock);
  928. err_vq:
  929. mutex_unlock(&vq->mutex);
  930. err:
  931. mutex_unlock(&n->dev.mutex);
  932. return r;
  933. }
  934. static long vhost_net_reset_owner(struct vhost_net *n)
  935. {
  936. struct socket *tx_sock = NULL;
  937. struct socket *rx_sock = NULL;
  938. long err;
  939. struct vhost_umem *umem;
  940. mutex_lock(&n->dev.mutex);
  941. err = vhost_dev_check_owner(&n->dev);
  942. if (err)
  943. goto done;
  944. umem = vhost_dev_reset_owner_prepare();
  945. if (!umem) {
  946. err = -ENOMEM;
  947. goto done;
  948. }
  949. vhost_net_stop(n, &tx_sock, &rx_sock);
  950. vhost_net_flush(n);
  951. vhost_dev_stop(&n->dev);
  952. vhost_dev_reset_owner(&n->dev, umem);
  953. vhost_net_vq_reset(n);
  954. done:
  955. mutex_unlock(&n->dev.mutex);
  956. if (tx_sock)
  957. sockfd_put(tx_sock);
  958. if (rx_sock)
  959. sockfd_put(rx_sock);
  960. return err;
  961. }
  962. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  963. {
  964. size_t vhost_hlen, sock_hlen, hdr_len;
  965. int i;
  966. hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  967. (1ULL << VIRTIO_F_VERSION_1))) ?
  968. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  969. sizeof(struct virtio_net_hdr);
  970. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  971. /* vhost provides vnet_hdr */
  972. vhost_hlen = hdr_len;
  973. sock_hlen = 0;
  974. } else {
  975. /* socket provides vnet_hdr */
  976. vhost_hlen = 0;
  977. sock_hlen = hdr_len;
  978. }
  979. mutex_lock(&n->dev.mutex);
  980. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  981. !vhost_log_access_ok(&n->dev))
  982. goto out_unlock;
  983. if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
  984. if (vhost_init_device_iotlb(&n->dev, true))
  985. goto out_unlock;
  986. }
  987. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  988. mutex_lock(&n->vqs[i].vq.mutex);
  989. n->vqs[i].vq.acked_features = features;
  990. n->vqs[i].vhost_hlen = vhost_hlen;
  991. n->vqs[i].sock_hlen = sock_hlen;
  992. mutex_unlock(&n->vqs[i].vq.mutex);
  993. }
  994. mutex_unlock(&n->dev.mutex);
  995. return 0;
  996. out_unlock:
  997. mutex_unlock(&n->dev.mutex);
  998. return -EFAULT;
  999. }
  1000. static long vhost_net_set_owner(struct vhost_net *n)
  1001. {
  1002. int r;
  1003. mutex_lock(&n->dev.mutex);
  1004. if (vhost_dev_has_owner(&n->dev)) {
  1005. r = -EBUSY;
  1006. goto out;
  1007. }
  1008. r = vhost_net_set_ubuf_info(n);
  1009. if (r)
  1010. goto out;
  1011. r = vhost_dev_set_owner(&n->dev);
  1012. if (r)
  1013. vhost_net_clear_ubuf_info(n);
  1014. vhost_net_flush(n);
  1015. out:
  1016. mutex_unlock(&n->dev.mutex);
  1017. return r;
  1018. }
  1019. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  1020. unsigned long arg)
  1021. {
  1022. struct vhost_net *n = f->private_data;
  1023. void __user *argp = (void __user *)arg;
  1024. u64 __user *featurep = argp;
  1025. struct vhost_vring_file backend;
  1026. u64 features;
  1027. int r;
  1028. switch (ioctl) {
  1029. case VHOST_NET_SET_BACKEND:
  1030. if (copy_from_user(&backend, argp, sizeof backend))
  1031. return -EFAULT;
  1032. return vhost_net_set_backend(n, backend.index, backend.fd);
  1033. case VHOST_GET_FEATURES:
  1034. features = VHOST_NET_FEATURES;
  1035. if (copy_to_user(featurep, &features, sizeof features))
  1036. return -EFAULT;
  1037. return 0;
  1038. case VHOST_SET_FEATURES:
  1039. if (copy_from_user(&features, featurep, sizeof features))
  1040. return -EFAULT;
  1041. if (features & ~VHOST_NET_FEATURES)
  1042. return -EOPNOTSUPP;
  1043. return vhost_net_set_features(n, features);
  1044. case VHOST_RESET_OWNER:
  1045. return vhost_net_reset_owner(n);
  1046. case VHOST_SET_OWNER:
  1047. return vhost_net_set_owner(n);
  1048. default:
  1049. mutex_lock(&n->dev.mutex);
  1050. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  1051. if (r == -ENOIOCTLCMD)
  1052. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  1053. else
  1054. vhost_net_flush(n);
  1055. mutex_unlock(&n->dev.mutex);
  1056. return r;
  1057. }
  1058. }
  1059. #ifdef CONFIG_COMPAT
  1060. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  1061. unsigned long arg)
  1062. {
  1063. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1064. }
  1065. #endif
  1066. static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1067. {
  1068. struct file *file = iocb->ki_filp;
  1069. struct vhost_net *n = file->private_data;
  1070. struct vhost_dev *dev = &n->dev;
  1071. int noblock = file->f_flags & O_NONBLOCK;
  1072. return vhost_chr_read_iter(dev, to, noblock);
  1073. }
  1074. static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
  1075. struct iov_iter *from)
  1076. {
  1077. struct file *file = iocb->ki_filp;
  1078. struct vhost_net *n = file->private_data;
  1079. struct vhost_dev *dev = &n->dev;
  1080. return vhost_chr_write_iter(dev, from);
  1081. }
  1082. static unsigned int vhost_net_chr_poll(struct file *file, poll_table *wait)
  1083. {
  1084. struct vhost_net *n = file->private_data;
  1085. struct vhost_dev *dev = &n->dev;
  1086. return vhost_chr_poll(file, dev, wait);
  1087. }
  1088. static const struct file_operations vhost_net_fops = {
  1089. .owner = THIS_MODULE,
  1090. .release = vhost_net_release,
  1091. .read_iter = vhost_net_chr_read_iter,
  1092. .write_iter = vhost_net_chr_write_iter,
  1093. .poll = vhost_net_chr_poll,
  1094. .unlocked_ioctl = vhost_net_ioctl,
  1095. #ifdef CONFIG_COMPAT
  1096. .compat_ioctl = vhost_net_compat_ioctl,
  1097. #endif
  1098. .open = vhost_net_open,
  1099. .llseek = noop_llseek,
  1100. };
  1101. static struct miscdevice vhost_net_misc = {
  1102. .minor = VHOST_NET_MINOR,
  1103. .name = "vhost-net",
  1104. .fops = &vhost_net_fops,
  1105. };
  1106. static int vhost_net_init(void)
  1107. {
  1108. if (experimental_zcopytx)
  1109. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1110. return misc_register(&vhost_net_misc);
  1111. }
  1112. module_init(vhost_net_init);
  1113. static void vhost_net_exit(void)
  1114. {
  1115. misc_deregister(&vhost_net_misc);
  1116. }
  1117. module_exit(vhost_net_exit);
  1118. MODULE_VERSION("0.0.1");
  1119. MODULE_LICENSE("GPL v2");
  1120. MODULE_AUTHOR("Michael S. Tsirkin");
  1121. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1122. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1123. MODULE_ALIAS("devname:vhost-net");