net.c 35 KB

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