net.c 34 KB

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