2 * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <linux/socket.h>
21 #include <xen/events.h>
22 #include <xen/grant_table.h>
24 #include <xen/xenbus.h>
25 #include <xen/interface/io/pvcalls.h>
27 #include "pvcalls-front.h"
29 #define PVCALLS_INVALID_ID UINT_MAX
30 #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
31 #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
33 struct pvcalls_bedata {
34 struct xen_pvcalls_front_ring ring;
38 struct list_head socket_mappings;
39 spinlock_t socket_lock;
41 wait_queue_head_t inflight_req;
42 struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
44 /* Only one front/back connection supported. */
45 static struct xenbus_device *pvcalls_front_dev;
46 static atomic_t pvcalls_refcount;
48 /* first increment refcount, then proceed */
49 #define pvcalls_enter() { \
50 atomic_inc(&pvcalls_refcount); \
53 /* first complete other operations, then decrement refcount */
54 #define pvcalls_exit() { \
55 atomic_dec(&pvcalls_refcount); \
60 struct list_head list;
66 struct pvcalls_data_intf *ring;
67 struct pvcalls_data data;
68 struct mutex in_mutex;
69 struct mutex out_mutex;
71 wait_queue_head_t inflight_conn_req;
75 #define PVCALLS_STATUS_UNINITALIZED 0
76 #define PVCALLS_STATUS_BIND 1
77 #define PVCALLS_STATUS_LISTEN 2
80 * Internal state-machine flags.
81 * Only one accept operation can be inflight for a socket.
82 * Only one poll operation can be inflight for a given socket.
84 #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
86 uint32_t inflight_req_id;
87 struct sock_mapping *accept_map;
88 wait_queue_head_t inflight_accept_req;
93 static inline int get_request(struct pvcalls_bedata *bedata, int *req_id)
95 *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
96 if (RING_FULL(&bedata->ring) ||
97 bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID)
102 static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
104 struct xenbus_device *dev = dev_id;
105 struct pvcalls_bedata *bedata;
106 struct xen_pvcalls_response *rsp;
108 int req_id = 0, more = 0, done = 0;
114 bedata = dev_get_drvdata(&dev->dev);
115 if (bedata == NULL) {
121 while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) {
122 rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons);
124 req_id = rsp->req_id;
125 dst = (uint8_t *)&bedata->rsp[req_id] + sizeof(rsp->req_id);
126 src = (uint8_t *)rsp + sizeof(rsp->req_id);
127 memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id));
129 * First copy the rest of the data, then req_id. It is
130 * paired with the barrier when accessing bedata->rsp.
133 bedata->rsp[req_id].req_id = rsp->req_id;
136 bedata->ring.rsp_cons++;
139 RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more);
143 wake_up(&bedata->inflight_req);
148 static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
149 struct sock_mapping *map)
153 static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
155 struct sock_mapping *map = sock_map;
160 wake_up_interruptible(&map->active.inflight_conn_req);
165 int pvcalls_front_socket(struct socket *sock)
167 struct pvcalls_bedata *bedata;
168 struct sock_mapping *map = NULL;
169 struct xen_pvcalls_request *req;
170 int notify, req_id, ret;
173 * PVCalls only supports domain AF_INET,
174 * type SOCK_STREAM and protocol 0 sockets for now.
176 * Check socket type here, AF_INET and protocol checks are done
179 if (sock->type != SOCK_STREAM)
183 if (!pvcalls_front_dev) {
187 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
189 map = kzalloc(sizeof(*map), GFP_KERNEL);
195 spin_lock(&bedata->socket_lock);
197 ret = get_request(bedata, &req_id);
200 spin_unlock(&bedata->socket_lock);
206 * sock->sk->sk_send_head is not used for ip sockets: reuse the
207 * field to store a pointer to the struct sock_mapping
208 * corresponding to the socket. This way, we can easily get the
209 * struct sock_mapping from the struct socket.
211 sock->sk->sk_send_head = (void *)map;
212 list_add_tail(&map->list, &bedata->socket_mappings);
214 req = RING_GET_REQUEST(&bedata->ring, req_id);
215 req->req_id = req_id;
216 req->cmd = PVCALLS_SOCKET;
217 req->u.socket.id = (uintptr_t) map;
218 req->u.socket.domain = AF_INET;
219 req->u.socket.type = SOCK_STREAM;
220 req->u.socket.protocol = IPPROTO_IP;
222 bedata->ring.req_prod_pvt++;
223 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
224 spin_unlock(&bedata->socket_lock);
226 notify_remote_via_irq(bedata->irq);
228 wait_event(bedata->inflight_req,
229 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
231 /* read req_id, then the content */
233 ret = bedata->rsp[req_id].ret;
234 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
240 static int create_active(struct sock_mapping *map, int *evtchn)
243 int ret = -ENOMEM, irq = -1, i;
246 init_waitqueue_head(&map->active.inflight_conn_req);
248 map->active.ring = (struct pvcalls_data_intf *)
249 __get_free_page(GFP_KERNEL | __GFP_ZERO);
250 if (map->active.ring == NULL)
252 map->active.ring->ring_order = PVCALLS_RING_ORDER;
253 bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
257 for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
258 map->active.ring->ref[i] = gnttab_grant_foreign_access(
259 pvcalls_front_dev->otherend_id,
260 pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
262 map->active.ref = gnttab_grant_foreign_access(
263 pvcalls_front_dev->otherend_id,
264 pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
266 map->active.data.in = bytes;
267 map->active.data.out = bytes +
268 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
270 ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
273 irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
274 0, "pvcalls-frontend", map);
280 map->active.irq = irq;
281 map->active_socket = true;
282 mutex_init(&map->active.in_mutex);
283 mutex_init(&map->active.out_mutex);
289 unbind_from_irqhandler(irq, map);
290 else if (*evtchn >= 0)
291 xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
292 kfree(map->active.data.in);
293 kfree(map->active.ring);
297 int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
298 int addr_len, int flags)
300 struct pvcalls_bedata *bedata;
301 struct sock_mapping *map = NULL;
302 struct xen_pvcalls_request *req;
303 int notify, req_id, ret, evtchn;
305 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
309 if (!pvcalls_front_dev) {
314 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
316 map = (struct sock_mapping *)sock->sk->sk_send_head;
322 spin_lock(&bedata->socket_lock);
323 ret = get_request(bedata, &req_id);
325 spin_unlock(&bedata->socket_lock);
329 ret = create_active(map, &evtchn);
331 spin_unlock(&bedata->socket_lock);
336 req = RING_GET_REQUEST(&bedata->ring, req_id);
337 req->req_id = req_id;
338 req->cmd = PVCALLS_CONNECT;
339 req->u.connect.id = (uintptr_t)map;
340 req->u.connect.len = addr_len;
341 req->u.connect.flags = flags;
342 req->u.connect.ref = map->active.ref;
343 req->u.connect.evtchn = evtchn;
344 memcpy(req->u.connect.addr, addr, sizeof(*addr));
348 bedata->ring.req_prod_pvt++;
349 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
350 spin_unlock(&bedata->socket_lock);
353 notify_remote_via_irq(bedata->irq);
355 wait_event(bedata->inflight_req,
356 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
358 /* read req_id, then the content */
360 ret = bedata->rsp[req_id].ret;
361 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
366 int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
368 struct pvcalls_bedata *bedata;
369 struct sock_mapping *map = NULL;
370 struct xen_pvcalls_request *req;
371 int notify, req_id, ret;
373 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
377 if (!pvcalls_front_dev) {
381 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
383 map = (struct sock_mapping *) sock->sk->sk_send_head;
389 spin_lock(&bedata->socket_lock);
390 ret = get_request(bedata, &req_id);
392 spin_unlock(&bedata->socket_lock);
396 req = RING_GET_REQUEST(&bedata->ring, req_id);
397 req->req_id = req_id;
399 req->cmd = PVCALLS_BIND;
400 req->u.bind.id = (uintptr_t)map;
401 memcpy(req->u.bind.addr, addr, sizeof(*addr));
402 req->u.bind.len = addr_len;
404 init_waitqueue_head(&map->passive.inflight_accept_req);
406 map->active_socket = false;
408 bedata->ring.req_prod_pvt++;
409 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
410 spin_unlock(&bedata->socket_lock);
412 notify_remote_via_irq(bedata->irq);
414 wait_event(bedata->inflight_req,
415 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
417 /* read req_id, then the content */
419 ret = bedata->rsp[req_id].ret;
420 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
422 map->passive.status = PVCALLS_STATUS_BIND;
427 int pvcalls_front_listen(struct socket *sock, int backlog)
429 struct pvcalls_bedata *bedata;
430 struct sock_mapping *map;
431 struct xen_pvcalls_request *req;
432 int notify, req_id, ret;
435 if (!pvcalls_front_dev) {
439 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
441 map = (struct sock_mapping *) sock->sk->sk_send_head;
447 if (map->passive.status != PVCALLS_STATUS_BIND) {
452 spin_lock(&bedata->socket_lock);
453 ret = get_request(bedata, &req_id);
455 spin_unlock(&bedata->socket_lock);
459 req = RING_GET_REQUEST(&bedata->ring, req_id);
460 req->req_id = req_id;
461 req->cmd = PVCALLS_LISTEN;
462 req->u.listen.id = (uintptr_t) map;
463 req->u.listen.backlog = backlog;
465 bedata->ring.req_prod_pvt++;
466 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
467 spin_unlock(&bedata->socket_lock);
469 notify_remote_via_irq(bedata->irq);
471 wait_event(bedata->inflight_req,
472 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
474 /* read req_id, then the content */
476 ret = bedata->rsp[req_id].ret;
477 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
479 map->passive.status = PVCALLS_STATUS_LISTEN;
484 int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
486 struct pvcalls_bedata *bedata;
487 struct sock_mapping *map;
488 struct sock_mapping *map2 = NULL;
489 struct xen_pvcalls_request *req;
490 int notify, req_id, ret, evtchn, nonblock;
493 if (!pvcalls_front_dev) {
497 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
499 map = (struct sock_mapping *) sock->sk->sk_send_head;
505 if (map->passive.status != PVCALLS_STATUS_LISTEN) {
510 nonblock = flags & SOCK_NONBLOCK;
512 * Backend only supports 1 inflight accept request, will return
513 * errors for the others
515 if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
516 (void *)&map->passive.flags)) {
517 req_id = READ_ONCE(map->passive.inflight_req_id);
518 if (req_id != PVCALLS_INVALID_ID &&
519 READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
520 map2 = map->passive.accept_map;
527 if (wait_event_interruptible(map->passive.inflight_accept_req,
528 !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
529 (void *)&map->passive.flags))) {
535 spin_lock(&bedata->socket_lock);
536 ret = get_request(bedata, &req_id);
538 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
539 (void *)&map->passive.flags);
540 spin_unlock(&bedata->socket_lock);
544 map2 = kzalloc(sizeof(*map2), GFP_KERNEL);
546 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
547 (void *)&map->passive.flags);
548 spin_unlock(&bedata->socket_lock);
552 ret = create_active(map2, &evtchn);
555 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
556 (void *)&map->passive.flags);
557 spin_unlock(&bedata->socket_lock);
561 list_add_tail(&map2->list, &bedata->socket_mappings);
563 req = RING_GET_REQUEST(&bedata->ring, req_id);
564 req->req_id = req_id;
565 req->cmd = PVCALLS_ACCEPT;
566 req->u.accept.id = (uintptr_t) map;
567 req->u.accept.ref = map2->active.ref;
568 req->u.accept.id_new = (uintptr_t) map2;
569 req->u.accept.evtchn = evtchn;
570 map->passive.accept_map = map2;
572 bedata->ring.req_prod_pvt++;
573 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
574 spin_unlock(&bedata->socket_lock);
576 notify_remote_via_irq(bedata->irq);
577 /* We could check if we have received a response before returning. */
579 WRITE_ONCE(map->passive.inflight_req_id, req_id);
584 if (wait_event_interruptible(bedata->inflight_req,
585 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
589 /* read req_id, then the content */
593 map2->sock = newsock;
594 newsock->sk = kzalloc(sizeof(*newsock->sk), GFP_KERNEL);
596 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
597 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
598 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
599 (void *)&map->passive.flags);
600 pvcalls_front_free_map(bedata, map2);
604 newsock->sk->sk_send_head = (void *)map2;
606 ret = bedata->rsp[req_id].ret;
607 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
608 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
610 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
611 wake_up(&map->passive.inflight_accept_req);
617 static const struct xenbus_device_id pvcalls_front_ids[] = {
622 static int pvcalls_front_remove(struct xenbus_device *dev)
624 struct pvcalls_bedata *bedata;
625 struct sock_mapping *map = NULL, *n;
627 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
628 dev_set_drvdata(&dev->dev, NULL);
629 pvcalls_front_dev = NULL;
630 if (bedata->irq >= 0)
631 unbind_from_irqhandler(bedata->irq, dev);
633 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
634 map->sock->sk->sk_send_head = NULL;
635 if (map->active_socket) {
636 map->active.ring->in_error = -EBADF;
637 wake_up_interruptible(&map->active.inflight_conn_req);
642 while (atomic_read(&pvcalls_refcount) > 0)
644 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
645 if (map->active_socket) {
646 /* No need to lock, refcount is 0 */
647 pvcalls_front_free_map(bedata, map);
649 list_del(&map->list);
653 if (bedata->ref >= 0)
654 gnttab_end_foreign_access(bedata->ref, 0, 0);
655 kfree(bedata->ring.sring);
657 xenbus_switch_state(dev, XenbusStateClosed);
661 static int pvcalls_front_probe(struct xenbus_device *dev,
662 const struct xenbus_device_id *id)
664 int ret = -ENOMEM, evtchn, i;
665 unsigned int max_page_order, function_calls, len;
667 grant_ref_t gref_head = 0;
668 struct xenbus_transaction xbt;
669 struct pvcalls_bedata *bedata = NULL;
670 struct xen_pvcalls_sring *sring;
672 if (pvcalls_front_dev != NULL) {
673 dev_err(&dev->dev, "only one PV Calls connection supported\n");
677 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
680 if (strcmp(versions, "1")) {
685 max_page_order = xenbus_read_unsigned(dev->otherend,
686 "max-page-order", 0);
687 if (max_page_order < PVCALLS_RING_ORDER)
689 function_calls = xenbus_read_unsigned(dev->otherend,
690 "function-calls", 0);
691 /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
692 if (function_calls != 1)
694 pr_info("%s max-page-order is %u\n", __func__, max_page_order);
696 bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
700 dev_set_drvdata(&dev->dev, bedata);
701 pvcalls_front_dev = dev;
702 init_waitqueue_head(&bedata->inflight_req);
703 INIT_LIST_HEAD(&bedata->socket_mappings);
704 spin_lock_init(&bedata->socket_lock);
708 for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
709 bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
711 sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
715 SHARED_RING_INIT(sring);
716 FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
718 ret = xenbus_alloc_evtchn(dev, &evtchn);
722 bedata->irq = bind_evtchn_to_irqhandler(evtchn,
723 pvcalls_front_event_handler,
724 0, "pvcalls-frontend", dev);
725 if (bedata->irq < 0) {
730 ret = gnttab_alloc_grant_references(1, &gref_head);
733 bedata->ref = gnttab_claim_grant_reference(&gref_head);
734 if (bedata->ref < 0) {
738 gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
739 virt_to_gfn((void *)sring), 0);
742 ret = xenbus_transaction_start(&xbt);
744 xenbus_dev_fatal(dev, ret, "starting transaction");
747 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
750 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
753 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
757 ret = xenbus_transaction_end(xbt, 0);
761 xenbus_dev_fatal(dev, ret, "completing transaction");
764 xenbus_switch_state(dev, XenbusStateInitialised);
769 xenbus_transaction_end(xbt, 1);
770 xenbus_dev_fatal(dev, ret, "writing xenstore");
772 pvcalls_front_remove(dev);
776 static void pvcalls_front_changed(struct xenbus_device *dev,
777 enum xenbus_state backend_state)
779 switch (backend_state) {
780 case XenbusStateReconfiguring:
781 case XenbusStateReconfigured:
782 case XenbusStateInitialising:
783 case XenbusStateInitialised:
784 case XenbusStateUnknown:
787 case XenbusStateInitWait:
790 case XenbusStateConnected:
791 xenbus_switch_state(dev, XenbusStateConnected);
794 case XenbusStateClosed:
795 if (dev->state == XenbusStateClosed)
797 /* Missed the backend's CLOSING state -- fallthrough */
798 case XenbusStateClosing:
799 xenbus_frontend_closed(dev);
804 static struct xenbus_driver pvcalls_front_driver = {
805 .ids = pvcalls_front_ids,
806 .probe = pvcalls_front_probe,
807 .remove = pvcalls_front_remove,
808 .otherend_changed = pvcalls_front_changed,
811 static int __init pvcalls_frontend_init(void)
816 pr_info("Initialising Xen pvcalls frontend driver\n");
818 return xenbus_register_frontend(&pvcalls_front_driver);
821 module_init(pvcalls_frontend_init);