net.c 31 KB

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