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