diff mbox series

[RFC,v4,10/11] qemu-sockets: introduce socket_uri()

Message ID 20220623155317.675932-11-lvivier@redhat.com (mailing list archive)
State New, archived
Headers show
Series qapi: net: add unix socket type support to netdev backend | expand

Commit Message

Laurent Vivier June 23, 2022, 3:53 p.m. UTC
Format a string URI from a SocketAddress.

Original code from hmp-cmds.c:SocketAddress_to_str()

Replace 'tcp:' by 'inet:' (because 'inet' can be also 'udp').
Replace 'tcp:' by 'vsock:' with vsock socket type.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 include/qemu/sockets.h |  2 +-
 monitor/hmp-cmds.c     | 23 +----------------------
 util/qemu-sockets.c    | 20 ++++++++++++++++++++
 3 files changed, 22 insertions(+), 23 deletions(-)

Comments

Markus Armbruster June 29, 2022, 11:26 a.m. UTC | #1
Laurent Vivier <lvivier@redhat.com> writes:

> Format a string URI from a SocketAddress.
>
> Original code from hmp-cmds.c:SocketAddress_to_str()
>
> Replace 'tcp:' by 'inet:' (because 'inet' can be also 'udp').

This one's merely misleading.

> Replace 'tcp:' by 'vsock:' with vsock socket type.

This one's positively wrong: it makes a vsock address look like an inet
address with CID misinterpreted as host.  Goes back to commit 9aca82ba31
"migration: Create socket-address parameter"

> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>  include/qemu/sockets.h |  2 +-
>  monitor/hmp-cmds.c     | 23 +----------------------
>  util/qemu-sockets.c    | 20 ++++++++++++++++++++
>  3 files changed, 22 insertions(+), 23 deletions(-)
>
> diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
> index 47194b9732f8..3e2ae7e21705 100644
> --- a/include/qemu/sockets.h
> +++ b/include/qemu/sockets.h
> @@ -41,6 +41,7 @@ int unix_listen(const char *path, Error **errp);
>  int unix_connect(const char *path, Error **errp);
>  
>  SocketAddress *socket_parse(const char *str, Error **errp);
> +char *socket_uri(SocketAddress *addr);
>  int socket_connect(SocketAddress *addr, Error **errp);
>  int socket_listen(SocketAddress *addr, int num, Error **errp);
>  void socket_listen_cleanup(int fd, Error **errp);
> @@ -123,5 +124,4 @@ SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
>   * Return 0 on success.
>   */
>  int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
> -
>  #endif /* QEMU_SOCKETS_H */
> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> index 47a27326eef7..eb0fe0a293b8 100644
> --- a/monitor/hmp-cmds.c
> +++ b/monitor/hmp-cmds.c
> @@ -197,27 +197,6 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
>      qapi_free_MouseInfoList(mice_list);
>  }
>  
> -static char *SocketAddress_to_str(SocketAddress *addr)
> -{
> -    switch (addr->type) {
> -    case SOCKET_ADDRESS_TYPE_INET:
> -        return g_strdup_printf("tcp:%s:%s",
> -                               addr->u.inet.host,
> -                               addr->u.inet.port);
> -    case SOCKET_ADDRESS_TYPE_UNIX:
> -        return g_strdup_printf("unix:%s",
> -                               addr->u.q_unix.path);
> -    case SOCKET_ADDRESS_TYPE_FD:
> -        return g_strdup_printf("fd:%s", addr->u.fd.str);
> -    case SOCKET_ADDRESS_TYPE_VSOCK:
> -        return g_strdup_printf("tcp:%s:%s",
> -                               addr->u.vsock.cid,
> -                               addr->u.vsock.port);
> -    default:
> -        return g_strdup("unknown address type");
> -    }
> -}
> -
>  void hmp_info_migrate(Monitor *mon, const QDict *qdict)
>  {
>      MigrationInfo *info;
> @@ -375,7 +354,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict)
>          monitor_printf(mon, "socket address: [\n");
>  
>          for (addr = info->socket_address; addr; addr = addr->next) {
> -            char *s = SocketAddress_to_str(addr->value);
> +            char *s = socket_uri(addr->value);
>              monitor_printf(mon, "\t%s\n", s);
>              g_free(s);
>          }
> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
> index 13b5b197f9ea..4efc2ce8b074 100644
> --- a/util/qemu-sockets.c
> +++ b/util/qemu-sockets.c
> @@ -1098,6 +1098,26 @@ int unix_connect(const char *path, Error **errp)
>      return sock;
>  }
>  
> +char *socket_uri(SocketAddress *addr)
> +{
> +    switch (addr->type) {
> +    case SOCKET_ADDRESS_TYPE_INET:
> +        return g_strdup_printf("inet:%s:%s",
> +                               addr->u.inet.host,
> +                               addr->u.inet.port);
> +    case SOCKET_ADDRESS_TYPE_UNIX:
> +        return g_strdup_printf("unix:%s",
> +                               addr->u.q_unix.path);
> +    case SOCKET_ADDRESS_TYPE_FD:
> +        return g_strdup_printf("fd:%s", addr->u.fd.str);
> +    case SOCKET_ADDRESS_TYPE_VSOCK:
> +        return g_strdup_printf("vsock:%s:%s",
> +                               addr->u.vsock.cid,
> +                               addr->u.vsock.port);
> +    default:
> +        return g_strdup("unknown address type");
> +    }
> +}
>  
>  SocketAddress *socket_parse(const char *str, Error **errp)
>  {

Why do you move and rename?  I'm not objecting, I just want to know the
reason :)
Laurent Vivier July 1, 2022, 9:36 a.m. UTC | #2
On 29/06/2022 13:26, Markus Armbruster wrote:
> Laurent Vivier <lvivier@redhat.com> writes:
> 
>> Format a string URI from a SocketAddress.
>>
>> Original code from hmp-cmds.c:SocketAddress_to_str()
>>
>> Replace 'tcp:' by 'inet:' (because 'inet' can be also 'udp').
> 
> This one's merely misleading.
> 
>> Replace 'tcp:' by 'vsock:' with vsock socket type.
> 
> This one's positively wrong: it makes a vsock address look like an inet
> address with CID misinterpreted as host.  Goes back to commit 9aca82ba31
> "migration: Create socket-address parameter"
> 
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
>>   include/qemu/sockets.h |  2 +-
>>   monitor/hmp-cmds.c     | 23 +----------------------
>>   util/qemu-sockets.c    | 20 ++++++++++++++++++++
>>   3 files changed, 22 insertions(+), 23 deletions(-)
>>
>> diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
>> index 47194b9732f8..3e2ae7e21705 100644
>> --- a/include/qemu/sockets.h
>> +++ b/include/qemu/sockets.h
>> @@ -41,6 +41,7 @@ int unix_listen(const char *path, Error **errp);
>>   int unix_connect(const char *path, Error **errp);
>>   
>>   SocketAddress *socket_parse(const char *str, Error **errp);
>> +char *socket_uri(SocketAddress *addr);
>>   int socket_connect(SocketAddress *addr, Error **errp);
>>   int socket_listen(SocketAddress *addr, int num, Error **errp);
>>   void socket_listen_cleanup(int fd, Error **errp);
>> @@ -123,5 +124,4 @@ SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
>>    * Return 0 on success.
>>    */
>>   int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
>> -
>>   #endif /* QEMU_SOCKETS_H */
>> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
>> index 47a27326eef7..eb0fe0a293b8 100644
>> --- a/monitor/hmp-cmds.c
>> +++ b/monitor/hmp-cmds.c
>> @@ -197,27 +197,6 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
>>       qapi_free_MouseInfoList(mice_list);
>>   }
>>   
>> -static char *SocketAddress_to_str(SocketAddress *addr)
>> -{
>> -    switch (addr->type) {
>> -    case SOCKET_ADDRESS_TYPE_INET:
>> -        return g_strdup_printf("tcp:%s:%s",
>> -                               addr->u.inet.host,
>> -                               addr->u.inet.port);
>> -    case SOCKET_ADDRESS_TYPE_UNIX:
>> -        return g_strdup_printf("unix:%s",
>> -                               addr->u.q_unix.path);
>> -    case SOCKET_ADDRESS_TYPE_FD:
>> -        return g_strdup_printf("fd:%s", addr->u.fd.str);
>> -    case SOCKET_ADDRESS_TYPE_VSOCK:
>> -        return g_strdup_printf("tcp:%s:%s",
>> -                               addr->u.vsock.cid,
>> -                               addr->u.vsock.port);
>> -    default:
>> -        return g_strdup("unknown address type");
>> -    }
>> -}
>> -
>>   void hmp_info_migrate(Monitor *mon, const QDict *qdict)
>>   {
>>       MigrationInfo *info;
>> @@ -375,7 +354,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict)
>>           monitor_printf(mon, "socket address: [\n");
>>   
>>           for (addr = info->socket_address; addr; addr = addr->next) {
>> -            char *s = SocketAddress_to_str(addr->value);
>> +            char *s = socket_uri(addr->value);
>>               monitor_printf(mon, "\t%s\n", s);
>>               g_free(s);
>>           }
>> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
>> index 13b5b197f9ea..4efc2ce8b074 100644
>> --- a/util/qemu-sockets.c
>> +++ b/util/qemu-sockets.c
>> @@ -1098,6 +1098,26 @@ int unix_connect(const char *path, Error **errp)
>>       return sock;
>>   }
>>   
>> +char *socket_uri(SocketAddress *addr)
>> +{
>> +    switch (addr->type) {
>> +    case SOCKET_ADDRESS_TYPE_INET:
>> +        return g_strdup_printf("inet:%s:%s",
>> +                               addr->u.inet.host,
>> +                               addr->u.inet.port);
>> +    case SOCKET_ADDRESS_TYPE_UNIX:
>> +        return g_strdup_printf("unix:%s",
>> +                               addr->u.q_unix.path);
>> +    case SOCKET_ADDRESS_TYPE_FD:
>> +        return g_strdup_printf("fd:%s", addr->u.fd.str);
>> +    case SOCKET_ADDRESS_TYPE_VSOCK:
>> +        return g_strdup_printf("vsock:%s:%s",
>> +                               addr->u.vsock.cid,
>> +                               addr->u.vsock.port);
>> +    default:
>> +        return g_strdup("unknown address type");
>> +    }
>> +}
>>   
>>   SocketAddress *socket_parse(const char *str, Error **errp)
>>   {
> 
> Why do you move and rename?  I'm not objecting, I just want to know the
> reason :)

I missed your comment before sending the v5.

I move and rename because this function is the counterpart for socket_parse(), so make the 
name similar and put the function in the same place.

Perhaps it should also be improved to display all the options ("keep-alive", "mptcp", ...) 
that socket_parse() is able to decode?

Thanks,
Laurent
Markus Armbruster July 4, 2022, 5:08 a.m. UTC | #3
Laurent Vivier <lvivier@redhat.com> writes:

> On 29/06/2022 13:26, Markus Armbruster wrote:
>> Laurent Vivier <lvivier@redhat.com> writes:
>> 
>>> Format a string URI from a SocketAddress.
>>>
>>> Original code from hmp-cmds.c:SocketAddress_to_str()
>>>
>>> Replace 'tcp:' by 'inet:' (because 'inet' can be also 'udp').
>> 
>> This one's merely misleading.
>> 
>>> Replace 'tcp:' by 'vsock:' with vsock socket type.
>> 
>> This one's positively wrong: it makes a vsock address look like an inet
>> address with CID misinterpreted as host.  Goes back to commit 9aca82ba31
>> "migration: Create socket-address parameter"
>> 
>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>> ---
>>>   include/qemu/sockets.h |  2 +-
>>>   monitor/hmp-cmds.c     | 23 +----------------------
>>>   util/qemu-sockets.c    | 20 ++++++++++++++++++++
>>>   3 files changed, 22 insertions(+), 23 deletions(-)
>>>
>>> diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
>>> index 47194b9732f8..3e2ae7e21705 100644
>>> --- a/include/qemu/sockets.h
>>> +++ b/include/qemu/sockets.h
>>> @@ -41,6 +41,7 @@ int unix_listen(const char *path, Error **errp);
>>>   int unix_connect(const char *path, Error **errp);
>>>   
>>>   SocketAddress *socket_parse(const char *str, Error **errp);
>>> +char *socket_uri(SocketAddress *addr);
>>>   int socket_connect(SocketAddress *addr, Error **errp);
>>>   int socket_listen(SocketAddress *addr, int num, Error **errp);
>>>   void socket_listen_cleanup(int fd, Error **errp);
>>> @@ -123,5 +124,4 @@ SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
>>>    * Return 0 on success.
>>>    */
>>>   int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
>>> -
>>>   #endif /* QEMU_SOCKETS_H */
>>> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
>>> index 47a27326eef7..eb0fe0a293b8 100644
>>> --- a/monitor/hmp-cmds.c
>>> +++ b/monitor/hmp-cmds.c
>>> @@ -197,27 +197,6 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
>>>       qapi_free_MouseInfoList(mice_list);
>>>   }
>>>   
>>> -static char *SocketAddress_to_str(SocketAddress *addr)
>>> -{
>>> -    switch (addr->type) {
>>> -    case SOCKET_ADDRESS_TYPE_INET:
>>> -        return g_strdup_printf("tcp:%s:%s",
>>> -                               addr->u.inet.host,
>>> -                               addr->u.inet.port);
>>> -    case SOCKET_ADDRESS_TYPE_UNIX:
>>> -        return g_strdup_printf("unix:%s",
>>> -                               addr->u.q_unix.path);
>>> -    case SOCKET_ADDRESS_TYPE_FD:
>>> -        return g_strdup_printf("fd:%s", addr->u.fd.str);
>>> -    case SOCKET_ADDRESS_TYPE_VSOCK:
>>> -        return g_strdup_printf("tcp:%s:%s",
>>> -                               addr->u.vsock.cid,
>>> -                               addr->u.vsock.port);
>>> -    default:
>>> -        return g_strdup("unknown address type");
>>> -    }
>>> -}
>>> -
>>>   void hmp_info_migrate(Monitor *mon, const QDict *qdict)
>>>   {
>>>       MigrationInfo *info;
>>> @@ -375,7 +354,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict)
>>>           monitor_printf(mon, "socket address: [\n");
>>>   
>>>           for (addr = info->socket_address; addr; addr = addr->next) {
>>> -            char *s = SocketAddress_to_str(addr->value);
>>> +            char *s = socket_uri(addr->value);
>>>               monitor_printf(mon, "\t%s\n", s);
>>>               g_free(s);
>>>           }
>>> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
>>> index 13b5b197f9ea..4efc2ce8b074 100644
>>> --- a/util/qemu-sockets.c
>>> +++ b/util/qemu-sockets.c
>>> @@ -1098,6 +1098,26 @@ int unix_connect(const char *path, Error **errp)
>>>       return sock;
>>>   }
>>>   
>>> +char *socket_uri(SocketAddress *addr)
>>> +{
>>> +    switch (addr->type) {
>>> +    case SOCKET_ADDRESS_TYPE_INET:
>>> +        return g_strdup_printf("inet:%s:%s",
>>> +                               addr->u.inet.host,
>>> +                               addr->u.inet.port);
>>> +    case SOCKET_ADDRESS_TYPE_UNIX:
>>> +        return g_strdup_printf("unix:%s",
>>> +                               addr->u.q_unix.path);
>>> +    case SOCKET_ADDRESS_TYPE_FD:
>>> +        return g_strdup_printf("fd:%s", addr->u.fd.str);
>>> +    case SOCKET_ADDRESS_TYPE_VSOCK:
>>> +        return g_strdup_printf("vsock:%s:%s",
>>> +                               addr->u.vsock.cid,
>>> +                               addr->u.vsock.port);
>>> +    default:
>>> +        return g_strdup("unknown address type");
>>> +    }
>>> +}
>>>   
>>>   SocketAddress *socket_parse(const char *str, Error **errp)
>>>   {
>> 
>> Why do you move and rename?  I'm not objecting, I just want to know the
>> reason :)
>
> I missed your comment before sending the v5.
>
> I move and rename because this function is the counterpart for socket_parse(), so make the 
> name similar and put the function in the same place.

Fair enough.  Recommend to work this into the commit message.  Could be
easier to write up if you make it a separate commit.

While there, you could also work in my review comments on why you're
replacing the 'tcp:' prefix.  Possible additional reason: consistency
with socket_parse().

Hmm, it's not actually consistent: socket_parse() doesn't recognize an
"inet:" prefix.  Omit it in socket_uri()?

> Perhaps it should also be improved to display all the options ("keep-alive", "mptcp", ...) 
> that socket_parse() is able to decode?

Maybe.  Requires a review of how it is used.
diff mbox series

Patch

diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index 47194b9732f8..3e2ae7e21705 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -41,6 +41,7 @@  int unix_listen(const char *path, Error **errp);
 int unix_connect(const char *path, Error **errp);
 
 SocketAddress *socket_parse(const char *str, Error **errp);
+char *socket_uri(SocketAddress *addr);
 int socket_connect(SocketAddress *addr, Error **errp);
 int socket_listen(SocketAddress *addr, int num, Error **errp);
 void socket_listen_cleanup(int fd, Error **errp);
@@ -123,5 +124,4 @@  SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
  * Return 0 on success.
  */
 int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
-
 #endif /* QEMU_SOCKETS_H */
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 47a27326eef7..eb0fe0a293b8 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -197,27 +197,6 @@  void hmp_info_mice(Monitor *mon, const QDict *qdict)
     qapi_free_MouseInfoList(mice_list);
 }
 
-static char *SocketAddress_to_str(SocketAddress *addr)
-{
-    switch (addr->type) {
-    case SOCKET_ADDRESS_TYPE_INET:
-        return g_strdup_printf("tcp:%s:%s",
-                               addr->u.inet.host,
-                               addr->u.inet.port);
-    case SOCKET_ADDRESS_TYPE_UNIX:
-        return g_strdup_printf("unix:%s",
-                               addr->u.q_unix.path);
-    case SOCKET_ADDRESS_TYPE_FD:
-        return g_strdup_printf("fd:%s", addr->u.fd.str);
-    case SOCKET_ADDRESS_TYPE_VSOCK:
-        return g_strdup_printf("tcp:%s:%s",
-                               addr->u.vsock.cid,
-                               addr->u.vsock.port);
-    default:
-        return g_strdup("unknown address type");
-    }
-}
-
 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
 {
     MigrationInfo *info;
@@ -375,7 +354,7 @@  void hmp_info_migrate(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "socket address: [\n");
 
         for (addr = info->socket_address; addr; addr = addr->next) {
-            char *s = SocketAddress_to_str(addr->value);
+            char *s = socket_uri(addr->value);
             monitor_printf(mon, "\t%s\n", s);
             g_free(s);
         }
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 13b5b197f9ea..4efc2ce8b074 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -1098,6 +1098,26 @@  int unix_connect(const char *path, Error **errp)
     return sock;
 }
 
+char *socket_uri(SocketAddress *addr)
+{
+    switch (addr->type) {
+    case SOCKET_ADDRESS_TYPE_INET:
+        return g_strdup_printf("inet:%s:%s",
+                               addr->u.inet.host,
+                               addr->u.inet.port);
+    case SOCKET_ADDRESS_TYPE_UNIX:
+        return g_strdup_printf("unix:%s",
+                               addr->u.q_unix.path);
+    case SOCKET_ADDRESS_TYPE_FD:
+        return g_strdup_printf("fd:%s", addr->u.fd.str);
+    case SOCKET_ADDRESS_TYPE_VSOCK:
+        return g_strdup_printf("vsock:%s:%s",
+                               addr->u.vsock.cid,
+                               addr->u.vsock.port);
+    default:
+        return g_strdup("unknown address type");
+    }
+}
 
 SocketAddress *socket_parse(const char *str, Error **errp)
 {