From 11a5652da72c7f683d6e1bd4df9025e8114b690b Mon Sep 17 00:00:00 2001 From: Yongrong Wang Date: Tue, 9 Jul 2024 14:20:47 +0800 Subject: [PATCH 1/3] openamp: replace all strncpy to strlcpy strlcpy is more convenient and safer than strncpy Signed-off-by: Yongrong Wang --- apps/system/linux/machine/generic/platform_info.c | 4 ++-- lib/remoteproc/remoteproc.c | 2 +- lib/rpmsg/rpmsg.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/system/linux/machine/generic/platform_info.c b/apps/system/linux/machine/generic/platform_info.c index 5f7c8d1d..f129d297 100644 --- a/apps/system/linux/machine/generic/platform_info.c +++ b/apps/system/linux/machine/generic/platform_info.c @@ -159,7 +159,7 @@ static int sk_unix_client(const char *descr) memset(&addr, 0, sizeof addr); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, descr + strlen(UNIX_PREFIX), + strlcpy(addr.sun_path, descr + strlen(UNIX_PREFIX), sizeof addr.sun_path); if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) >= 0) { printf("connected to %s\r\n", descr + strlen(UNIX_PREFIX)); @@ -178,7 +178,7 @@ static int sk_unix_server(const char *descr) fd = socket(AF_UNIX, SOCK_STREAM, 0); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, descr + strlen(UNIXS_PREFIX), + strlcpy(addr.sun_path, descr + strlen(UNIXS_PREFIX), sizeof addr.sun_path); unlink(addr.sun_path); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { diff --git a/lib/remoteproc/remoteproc.c b/lib/remoteproc/remoteproc.c index 3ac4a1bb..fc1862cb 100644 --- a/lib/remoteproc/remoteproc.c +++ b/lib/remoteproc/remoteproc.c @@ -306,7 +306,7 @@ void remoteproc_init_mem(struct remoteproc_mem *mem, const char *name, if (!mem || !io || size == 0) return; if (name) - strncpy(mem->name, name, sizeof(mem->name)); + strlcpy(mem->name, name, sizeof(mem->name)); else mem->name[0] = 0; mem->pa = pa; diff --git a/lib/rpmsg/rpmsg.c b/lib/rpmsg/rpmsg.c index 39774bc1..86e026f9 100644 --- a/lib/rpmsg/rpmsg.c +++ b/lib/rpmsg/rpmsg.c @@ -141,7 +141,7 @@ int rpmsg_send_ns_message(struct rpmsg_endpoint *ept, unsigned long flags) ns_msg.flags = flags; ns_msg.addr = ept->addr; - strncpy(ns_msg.name, ept->name, sizeof(ns_msg.name)); + strlcpy(ns_msg.name, ept->name, sizeof(ns_msg.name)); ret = rpmsg_send_offchannel_raw(ept, ept->addr, RPMSG_NS_EPT_ADDR, &ns_msg, sizeof(ns_msg), true); @@ -305,7 +305,7 @@ void rpmsg_register_endpoint(struct rpmsg_device *rdev, rpmsg_ept_cb cb, rpmsg_ns_unbind_cb ns_unbind_cb, void *priv) { - strncpy(ept->name, name ? name : "", sizeof(ept->name)); + strlcpy(ept->name, name ? name : "", sizeof(ept->name)); ept->refcnt = 1; ept->addr = src; ept->dest_addr = dest; From 7e46eb47445a64eb42e500b088f3d7ef9ff748ac Mon Sep 17 00:00:00 2001 From: wangyongrong Date: Tue, 23 Jan 2024 10:45:25 +0800 Subject: [PATCH 2/3] openamp: move notify_wait() in rpmsg virtio to rpmsg. This ops can do in rpmsg without coupling to virtio. Signed-off-by: wangyongrong --- lib/include/openamp/rpmsg.h | 5 +++++ lib/rpmsg/rpmsg.c | 8 ++++++++ lib/rpmsg/rpmsg_internal.h | 2 ++ lib/rpmsg/rpmsg_virtio.c | 8 +++----- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/include/openamp/rpmsg.h b/lib/include/openamp/rpmsg.h index f994e1d5..00ba7b4a 100644 --- a/lib/include/openamp/rpmsg.h +++ b/lib/include/openamp/rpmsg.h @@ -55,6 +55,7 @@ typedef void (*rpmsg_ept_release_cb)(struct rpmsg_endpoint *ept); typedef void (*rpmsg_ns_unbind_cb)(struct rpmsg_endpoint *ept); typedef void (*rpmsg_ns_bind_cb)(struct rpmsg_device *rdev, const char *name, uint32_t dest); +typedef int (*rpmsg_notify_wait_cb)(struct rpmsg_device *rdev, uint32_t id); /** * @brief Structure that binds a local RPMsg address to its user @@ -150,7 +151,11 @@ struct rpmsg_device { /** Callback handler for name service announcement, called when remote ept is destroyed */ rpmsg_ns_bind_cb ns_unbind_cb; + /** callback handler for rpmsg service, called when service can't get tx buffer */ + rpmsg_notify_wait_cb notify_wait_cb; + /** RPMsg device operations */ + struct rpmsg_device_ops ops; /** Create/destroy namespace message */ diff --git a/lib/rpmsg/rpmsg.c b/lib/rpmsg/rpmsg.c index 86e026f9..55c3c53a 100644 --- a/lib/rpmsg/rpmsg.c +++ b/lib/rpmsg/rpmsg.c @@ -385,3 +385,11 @@ void rpmsg_destroy_ept(struct rpmsg_endpoint *ept) (void)rpmsg_send_ns_message(ept, RPMSG_NS_DESTROY); rpmsg_unregister_endpoint(ept); } + +int rpmsg_notify_wait(struct rpmsg_device *rdev, uint32_t id) +{ + if (rdev->notify_wait_cb) + return rdev->notify_wait_cb(rdev, id); + + return RPMSG_ERR_NXIO; +} diff --git a/lib/rpmsg/rpmsg_internal.h b/lib/rpmsg/rpmsg_internal.h index 27b0f0d1..ecae5c0c 100644 --- a/lib/rpmsg/rpmsg_internal.h +++ b/lib/rpmsg/rpmsg_internal.h @@ -108,6 +108,8 @@ rpmsg_get_ept_from_addr(struct rpmsg_device *rdev, uint32_t addr) return rpmsg_get_endpoint(rdev, NULL, addr, RPMSG_ADDR_ANY); } +int rpmsg_notify_wait(struct rpmsg_device *rdev, uint32_t id); + /** * @internal * diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index 7baaedd5..c9e7f0a0 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -342,16 +342,14 @@ static void rpmsg_virtio_release_rx_buffer(struct rpmsg_device *rdev, metal_mutex_release(&rdev->lock); } -static int rpmsg_virtio_notify_wait(struct rpmsg_virtio_device *rvdev, struct virtqueue *vq) +static int rpmsg_virtio_notify_wait(struct rpmsg_virtio_device *rvdev, + struct virtqueue *vq) { struct virtio_vring_info *vring_info; vring_info = &rvdev->vdev->vrings_info[vq->vq_queue_index]; - if (!rvdev->notify_wait_cb) - return RPMSG_EOPNOTSUPP; - - return rvdev->notify_wait_cb(&rvdev->rdev, vring_info->notifyid); + return rpmsg_notify_wait(&rvdev->rdev, vring_info->notifyid); } static void *rpmsg_virtio_get_tx_payload_buffer(struct rpmsg_device *rdev, From 854051d2ceb52bb6a09197f4e9a6cddac2792aff Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 7 Jan 2019 02:15:42 +0800 Subject: [PATCH 3/3] ns: acknowledge the received creation message the two phase handsake make the client could initiate the transfer immediately without the server side send any dummy message first. Signed-off-by: Xiang Xiao Signed-off-by: Yongrong Wang --- lib/include/openamp/rpmsg.h | 3 +++ lib/include/openamp/rpmsg_virtio.h | 1 + lib/rpmsg/rpmsg.c | 5 ++++- lib/rpmsg/rpmsg_internal.h | 2 ++ lib/rpmsg/rpmsg_virtio.c | 14 +++++++++++--- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/include/openamp/rpmsg.h b/lib/include/openamp/rpmsg.h index 00ba7b4a..c3d39bb0 100644 --- a/lib/include/openamp/rpmsg.h +++ b/lib/include/openamp/rpmsg.h @@ -160,6 +160,9 @@ struct rpmsg_device { /** Create/destroy namespace message */ bool support_ns; + + /** Ack namespace message */ + bool support_ack; }; /** diff --git a/lib/include/openamp/rpmsg_virtio.h b/lib/include/openamp/rpmsg_virtio.h index 44ac4c09..6d6925f2 100644 --- a/lib/include/openamp/rpmsg_virtio.h +++ b/lib/include/openamp/rpmsg_virtio.h @@ -29,6 +29,7 @@ extern "C" { /* The feature bitmap for virtio rpmsg */ #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */ +#define VIRTIO_RPMSG_F_ACK 1 /* RP supports name service acknowledge */ #ifdef VIRTIO_CACHED_BUFFERS #warning "VIRTIO_CACHED_BUFFERS is deprecated, please use VIRTIO_USE_DCACHE" diff --git a/lib/rpmsg/rpmsg.c b/lib/rpmsg/rpmsg.c index 55c3c53a..da075892 100644 --- a/lib/rpmsg/rpmsg.c +++ b/lib/rpmsg/rpmsg.c @@ -357,10 +357,13 @@ int rpmsg_create_ept(struct rpmsg_endpoint *ept, struct rpmsg_device *rdev, rpmsg_register_endpoint(rdev, ept, name, addr, dest, cb, unbind_cb, ept->priv); metal_mutex_release(&rdev->lock); - /* Send NS announcement to remote processor */ + /* Send NS announcement/acknowledge to remote processor */ if (ept->name[0] && rdev->support_ns && ept->dest_addr == RPMSG_ADDR_ANY) status = rpmsg_send_ns_message(ept, RPMSG_NS_CREATE); + else if (ept->name[0] && rdev->support_ack && + ept->dest_addr != RPMSG_ADDR_ANY) + status = rpmsg_send_ns_message(ept, RPMSG_NS_CREATE_ACK); if (status) rpmsg_unregister_endpoint(ept); diff --git a/lib/rpmsg/rpmsg_internal.h b/lib/rpmsg/rpmsg_internal.h index ecae5c0c..bd57eda0 100644 --- a/lib/rpmsg/rpmsg_internal.h +++ b/lib/rpmsg/rpmsg_internal.h @@ -44,6 +44,8 @@ enum rpmsg_ns_flags { RPMSG_NS_CREATE = 0, /** A known remote service was just destroyed */ RPMSG_NS_DESTROY = 1, + /** Aknowledge the previous creation message*/ + RPMSG_NS_CREATE_ACK = 2, }; /** diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index c9e7f0a0..84cf610a 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -654,7 +654,7 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, */ ept_to_release = _ept && _ept->release_cb; - if (ns_msg->flags & RPMSG_NS_DESTROY) { + if (ns_msg->flags == RPMSG_NS_DESTROY) { if (_ept) _ept->dest_addr = RPMSG_ADDR_ANY; if (ept_to_release) @@ -669,7 +669,7 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, rpmsg_ept_decref(_ept); metal_mutex_release(&rdev->lock); } - } else { + } else if (ns_msg->flags == RPMSG_NS_CREATE) { if (!_ept) { /* * send callback to application, that can @@ -683,7 +683,14 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, } else { _ept->dest_addr = dest; metal_mutex_release(&rdev->lock); + if (_ept->name[0] && rdev->support_ack) + rpmsg_send_ns_message(_ept, RPMSG_NS_CREATE_ACK); } + } else { /* RPMSG_NS_CREATE_ACK */ + /* save the received destination address */ + if (_ept) + _ept->dest_addr = dest; + metal_mutex_release(&rdev->lock); } return RPMSG_SUCCESS; @@ -826,6 +833,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, if (status) return status; rdev->support_ns = !!(features & (1 << VIRTIO_RPMSG_F_NS)); + rdev->support_ack = !!(features & (1 << VIRTIO_RPMSG_F_ACK)); if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) { /* @@ -924,7 +932,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, * Create name service announcement endpoint if device supports name * service announcement feature. */ - if (rdev->support_ns) { + if (rdev->support_ns || rdev->support_ack) { rpmsg_register_endpoint(rdev, &rdev->ns_ept, "NS", RPMSG_NS_EPT_ADDR, RPMSG_NS_EPT_ADDR, rpmsg_virtio_ns_callback, NULL, rvdev);