net.c 35 KB

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