From patchwork Fri Jun 17 15:55:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Pinto X-Patchwork-Id: 9184483 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 38D95608A2 for ; Fri, 17 Jun 2016 15:56:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10EE727C2C for ; Fri, 17 Jun 2016 15:56:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 055A82835E; Fri, 17 Jun 2016 15:56:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id DEA8927C2C for ; Fri, 17 Jun 2016 15:56:50 +0000 (UTC) Received: from localhost ([::1]:58592 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDw8X-0007JQ-Rv for patchwork-qemu-devel@patchwork.kernel.org; Fri, 17 Jun 2016 11:56:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53300) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDw7P-0006gI-LH for qemu-devel@nongnu.org; Fri, 17 Jun 2016 11:55:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDw7L-0001Ay-Ob for qemu-devel@nongnu.org; Fri, 17 Jun 2016 11:55:38 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:35207) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDw7L-00019z-C5 for qemu-devel@nongnu.org; Fri, 17 Jun 2016 11:55:35 -0400 Received: by mail-wm0-x243.google.com with SMTP id k184so731791wme.2 for ; Fri, 17 Jun 2016 08:55:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtualopensystems-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DTq4zyNMTqAcrKU2L9Vvah6KHct1sd2nd7qmy0LGEbA=; b=PNGpIA1Po6w/6IBxhYFRfwk01BHoWZW3sQ8bswMRWeX+ctrg1Oxoi3Gkcb9KWo3jbJ 1tzn84k/hVn1KP3bF4KH5plVwtWbfMRTRcy5l2e2SQAr+S0lv0dI4uf4UMSpYXX5br1P cCkagNgArujqxNCr9472jWmJxKxe0oaJh0IZL3p7+y/Ou2H6LJBmFOfGIVIquG4M9Pwz xNljq/ZB4wrhrfDAe4zbRePSc2rBW7hldlqygYzqXgdgC1LMRbf0zm7vhET3rkHC2sUq eg4hIb54hY19swIX0VYTbhxGVTzuk4oTIA9rqNOx0oESVu2x5GNNwGTxO58Ehbez7IOc ebdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DTq4zyNMTqAcrKU2L9Vvah6KHct1sd2nd7qmy0LGEbA=; b=MWRzLxOpsBH5KYdRxadFC4L8RVk24DtB85fTQokynQDOi4am2tuz8f1D1/T5frQ336 yxPvyizx0IqmZs9HrhcB+wHEyWaSzkYJjBr69Yna2dblkBoJGibEW3lOzVM9kxM8Gyzm AvOCy6+3DThQEXIj/1Cixv+cKBs1CK2YmtHa5RN8hk50G8QOJ1+T7E3cjzWZDfkEoNco 8xxJbefrbTp5tofIn8Hqn4Ah31Gt8cFCXwPaBmNCEwYFMDlemNsGthxio0OjRLR08qXE 2eg8MTkcr8WuwGKF4yVZ/GRxF8gkVJg5JoZeYBeHSUk/YVTyEyGZjR4SuMjNxpA+2NiK Ofow== X-Gm-Message-State: ALyK8tLAtQl40nuoc+3x2pTifh/Imn4VshiwZWOVeoLr2zgnBIaxJeJewVZQbPUnBivPQA== X-Received: by 10.194.104.196 with SMTP id gg4mr3466729wjb.112.1466178934575; Fri, 17 Jun 2016 08:55:34 -0700 (PDT) Received: from bumma.localdomain ([151.67.13.17]) by smtp.googlemail.com with ESMTPSA id q189sm254937wmd.19.2016.06.17.08.55.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 17 Jun 2016 08:55:33 -0700 (PDT) From: Christian Pinto To: qemu-devel@nongnu.org Date: Fri, 17 Jun 2016 17:55:03 +0200 Message-Id: <1466178903-2184-7-git-send-email-c.pinto@virtualopensystems.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1466178903-2184-1-git-send-email-c.pinto@virtualopensystems.com> References: <1466178903-2184-1-git-send-email-c.pinto@virtualopensystems.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [RFC v3 6/6] hw/misc: sdm communication socket X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Claudio.Fontana@huawei.com, Jani.Kokkonen@huawei.com, tech@virtualopensystems.com, b.reynal@virtualopensystems.com, Christian Pinto Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Baptiste Reynal This is the socket implementation of an SDM communication channel. A master device can communicate with a slave over a socket. Parameters: socket=[socket_id] - specifies the multi-client socket This patch depends on "[RFC v2 1/1] backend: multi-client-socket" Signed-off-by: Baptiste Reynal Signed-off-by: Christian Pinto --- v2 -> v3: - added sdm_socket_update_num_slaves to socket based communication channel - realloc slaves array in case of max slaves num change --- hw/misc/Makefile.objs | 1 + hw/misc/sdm-communication-socket.c | 160 +++++++++++++++++++++++++++++ include/hw/misc/sdm-communication-socket.h | 42 ++++++++ 3 files changed, 203 insertions(+) create mode 100644 hw/misc/sdm-communication-socket.c create mode 100644 include/hw/misc/sdm-communication-socket.h diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs index cad0b4c..8f38d35 100644 --- a/hw/misc/Makefile.objs +++ b/hw/misc/Makefile.objs @@ -26,6 +26,7 @@ obj-$(CONFIG_SDM) += sdm-communication.o obj-$(CONFIG_SDM) += sdm-signal.o obj-$(CONFIG_SDM) += sdm-platform.o obj-$(CONFIG_SDM) += sdm-communication-local.o +obj-$(CONFIG_SDM) += sdm-communication-socket.o obj-$(CONFIG_REALVIEW) += arm_sysctl.o obj-$(CONFIG_NSERIES) += cbus.o diff --git a/hw/misc/sdm-communication-socket.c b/hw/misc/sdm-communication-socket.c new file mode 100644 index 0000000..15ebed7 --- /dev/null +++ b/hw/misc/sdm-communication-socket.c @@ -0,0 +1,160 @@ +/* + * SDM Communication Socket + * + * Copyright (C) 2016 - Virtual Open Systems + * + * Author: Baptiste Reynal + * Christian Pinto + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ +#include "hw/misc/sdm-communication-socket.h" +#include "hw/qdev-properties.h" +#include "qemu/error-report.h" + +static int sdm_socket_signal(SDMCommunication *sdmc, SDMDevice *sdm, + SDMSignalData *signal) +{ + SDMCommunicationSocket *sdmcs = SDM_COMMUNICATION_SOCKET(sdmc); + int len; + char *message; + SDMSignalData *payload; + + if (!sdmcs->slaves[signal->slave]) { + error_report("SDMCommunicationSocket: invalid slave ID %d", + signal->slave); + return -1; + } + + len = strlen(SDM_SIGNAL_CMD) + sizeof(SDMSignalData) + 1; + message = malloc(len); + + strcpy((char *) message, SDM_SIGNAL_CMD); + payload = (SDMSignalData *) (message + strlen(SDM_SIGNAL_CMD) + 1); + memcpy(payload, signal, sizeof(SDMSignalData)); + + multi_socket_write_to(sdmcs->slaves[signal->slave], + message, + len); + + return 0; +} + +static void sdm_register_ms_handler(MSClient *c, const char *message, + void *opaque) +{ + SDMCommunication *sdmc = opaque; + SDMCommunicationSocket *sdmcs = SDM_COMMUNICATION_SOCKET(sdmc); + int slave_id; + + slave_id = sdm_device_accept(sdmcs->master); + if (slave_id >= 0) { + sdmcs->slaves[slave_id] = c; + } else { + error_report("SDMCommunicationSocket: Slave rejected, no ID available"); + } +} + +static void sdm_signal_ms_handler(MSClient *c, const char *message, + void *opaque) +{ + SDMCommunication *sdmc = opaque; + SDMCommunicationSocket *sdmcs = SDM_COMMUNICATION_SOCKET(sdmc); + int slave; + SDMSignalData *signal; + + signal = (SDMSignalData *) message; + + /* Find slave id */ + for (slave=0; slave<=sdmcs->num_slaves; slave++) { + if (sdmcs->slaves[slave] == c) + break; + } + + signal->slave = slave; + + while (sdm_device_notify(sdmcs->master, signal) < 0) { + sleep(100); + } +} + +static int sdm_socket_connect(SDMCommunication *sdmc, SDMDevice *sdm) +{ + SDMCommunicationSocket *sdmcs = SDM_COMMUNICATION_SOCKET(sdmc); + + sdmcs->master = sdm; + + sdmcs->num_slaves = sdm_device_get_num_slaves(sdm); + sdmcs->slaves = calloc(sdmcs->num_slaves + 1, + sizeof(MSClient *)); + + sdmcs->slaves[0] = &sdmcs->socket->listener; + multi_socket_add_handler(sdmcs->socket, SDM_SIGNAL_CMD, + sdm_signal_ms_handler, sdmc); + + if (sdm_device_is_master(sdm)) { + multi_socket_add_handler(sdmcs->socket, SDM_REGISTER_CMD, + sdm_register_ms_handler, sdmc); + } else { + multi_socket_write_to(sdmcs->slaves[0], + SDM_REGISTER_CMD, + strlen(SDM_REGISTER_CMD) + 1); + } + + return 0; +} + +static int sdm_socket_update_num_slaves(SDMCommunication *sdmc, SDMDevice *sdm, + uint16_t num_slaves) +{ + SDMCommunicationSocket *sdmcs = SDM_COMMUNICATION_SOCKET(sdmc); + MSClient **tmp_slaves; + + tmp_slaves = realloc(sdmcs->slaves, (num_slaves + 1) * + sizeof(MSClient *)); + + if(tmp_slaves == NULL) + return -1; + + sdmcs->slaves = tmp_slaves; + sdmcs->num_slaves = num_slaves; + sdm_device_set_num_slaves(sdm, num_slaves); + + return 0; +} + +static void sdm_communication_socket_class_init(ObjectClass *oc, void *data) +{ + SDMCommunicationClass *sdmck = SDM_COMMUNICATION_CLASS(oc); + + sdmck->signal = sdm_socket_signal; + sdmck->connect = sdm_socket_connect; + sdmck->update_num_slaves = sdm_socket_update_num_slaves; +} + +static void sdm_communication_socket_init(Object *obj) +{ + SDMCommunicationSocket *s = SDM_COMMUNICATION_SOCKET(obj); + + object_property_add_link(obj, "socket", TYPE_MULTI_SOCKET_BACKEND, + (Object **)&s->socket, + object_property_allow_set_link, + OBJ_PROP_LINK_UNREF_ON_RELEASE, + &error_abort); +} + +static const TypeInfo sdm_communication_socket_info = { + .name = TYPE_SDM_COMMUNICATION_SOCKET, + .parent = TYPE_SDM_COMMUNICATION, + .class_init = sdm_communication_socket_class_init, + .instance_size = sizeof(SDMCommunicationSocket), + .instance_init = sdm_communication_socket_init, +}; + +static void register_types(void) +{ + type_register_static(&sdm_communication_socket_info); +} + +type_init(register_types); diff --git a/include/hw/misc/sdm-communication-socket.h b/include/hw/misc/sdm-communication-socket.h new file mode 100644 index 0000000..aa24454 --- /dev/null +++ b/include/hw/misc/sdm-communication-socket.h @@ -0,0 +1,42 @@ +/* + * SDM Communication Socket + * + * Copyright (C) 2016 - Virtual Open Systems + * + * Author: Baptiste Reynal + * + * This works is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ +#ifndef HW_SDM_COMM_SOCK_H +#define HW_SDM_COMM_SOCK_H + +#include "hw/misc/sdm-communication.h" +#include "qemu/multi-socket.h" + +#define SDM_REGISTER_CMD "sdm_register" +#define SDM_SIGNAL_CMD "sdm_signal" + +#define TYPE_SDM_COMMUNICATION_SOCKET "sdm-communication-socket" +#define SDM_COMMUNICATION_SOCKET(obj) \ + OBJECT_CHECK(SDMCommunicationSocket, (obj), \ + TYPE_SDM_COMMUNICATION_SOCKET) + +typedef struct SDMCommunicationSocket SDMCommunicationSocket; + +/** + * @SDMCommunicationSocket + * + * @parent: opaque parent object container + */ +struct SDMCommunicationSocket { + /* private */ + SDMCommunication parent; + + SDMDevice *master; + MSBackend *socket; + + int num_slaves; + MSClient **slaves; +}; +#endif