net.c 28 KB

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