net.c 35 KB

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