[PATCH v2 2/2] tools/xenstore: simplify xenstored main loop

Juergen Gross posted 2 patches 4 years, 9 months ago
There is a newer version of this series
[PATCH v2 2/2] tools/xenstore: simplify xenstored main loop
Posted by Juergen Gross 4 years, 9 months ago
The main loop of xenstored is rather complicated due to different
handling of socket and ring-page interfaces. Unify that handling by
introducing interface type specific functions can_read() and
can_write().

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- split off function vector introduction (Julien Grall)
---
 tools/xenstore/xenstored_core.c   | 77 +++++++++++++++----------------
 tools/xenstore/xenstored_core.h   |  2 +
 tools/xenstore/xenstored_domain.c |  2 +
 3 files changed, 41 insertions(+), 40 deletions(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 856f518075..883a1a582a 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -1659,9 +1659,34 @@ static int readfd(struct connection *conn, void *data, unsigned int len)
 	return rc;
 }
 
+static bool socket_can_process(struct connection *conn, int mask)
+{
+	if (conn->pollfd_idx == -1)
+		return false;
+
+	if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
+		talloc_free(conn);
+		return false;
+	}
+
+	return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
+}
+
+static bool socket_can_write(struct connection *conn)
+{
+	return socket_can_process(conn, POLLOUT);
+}
+
+static bool socket_can_read(struct connection *conn)
+{
+	return socket_can_process(conn, POLLIN);
+}
+
 const struct interface_funcs socket_funcs = {
 	.write = writefd,
 	.read = readfd,
+	.can_write = socket_can_write,
+	.can_read = socket_can_read,
 };
 
 static void accept_connection(int sock)
@@ -2296,47 +2321,19 @@ int main(int argc, char *argv[])
 			if (&next->list != &connections)
 				talloc_increase_ref_count(next);
 
-			if (conn->domain) {
-				if (domain_can_read(conn))
-					handle_input(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-				if (domain_can_write(conn) &&
-				    !list_empty(&conn->out_list))
-					handle_output(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-			} else {
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLIN) &&
-						 !conn->is_ignored)
-						handle_input(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLOUT) &&
-						 !conn->is_ignored)
-						handle_output(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
+			if (conn->funcs->can_read(conn))
+				handle_input(conn);
+			if (talloc_free(conn) == 0)
+				continue;
 
-				conn->pollfd_idx = -1;
-			}
+			talloc_increase_ref_count(conn);
+
+			if (conn->funcs->can_write(conn))
+				handle_output(conn);
+			if (talloc_free(conn) == 0)
+				continue;
+
+			conn->pollfd_idx = -1;
 		}
 
 		if (delayed_requests) {
diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h
index 021e41076d..c6e04c0708 100644
--- a/tools/xenstore/xenstored_core.h
+++ b/tools/xenstore/xenstored_core.h
@@ -90,6 +90,8 @@ struct connection;
 struct interface_funcs {
 	int (*write)(struct connection *, const void *, unsigned int);
 	int (*read)(struct connection *, void *, unsigned int);
+	bool (*can_write)(struct connection *);
+	bool (*can_read)(struct connection *);
 };
 
 struct connection
diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_domain.c
index f3cd56050e..708bf68af0 100644
--- a/tools/xenstore/xenstored_domain.c
+++ b/tools/xenstore/xenstored_domain.c
@@ -175,6 +175,8 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
 static const struct interface_funcs domain_funcs = {
 	.write = writechn,
 	.read = readchn,
+	.can_write = domain_can_write,
+	.can_read = domain_can_read,
 };
 
 static void *map_interface(domid_t domid)
-- 
2.26.2


Re: [PATCH v2 2/2] tools/xenstore: simplify xenstored main loop
Posted by Julien Grall 4 years, 9 months ago
Hi Juergen,

On 14/05/2021 12:56, Juergen Gross wrote:
> The main loop of xenstored is rather complicated due to different
> handling of socket and ring-page interfaces. Unify that handling by
> introducing interface type specific functions can_read() and
> can_write().
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
> V2:
> - split off function vector introduction (Julien Grall)
> ---
>   tools/xenstore/xenstored_core.c   | 77 +++++++++++++++----------------
>   tools/xenstore/xenstored_core.h   |  2 +
>   tools/xenstore/xenstored_domain.c |  2 +
>   3 files changed, 41 insertions(+), 40 deletions(-)
> 
> diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
> index 856f518075..883a1a582a 100644
> --- a/tools/xenstore/xenstored_core.c
> +++ b/tools/xenstore/xenstored_core.c
> @@ -1659,9 +1659,34 @@ static int readfd(struct connection *conn, void *data, unsigned int len)
>   	return rc;
>   }
>   
> +static bool socket_can_process(struct connection *conn, int mask)
> +{
> +	if (conn->pollfd_idx == -1)
> +		return false;
> +
> +	if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
> +		talloc_free(conn);
> +		return false;
> +	}
> +
> +	return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
> +}
> +
> +static bool socket_can_write(struct connection *conn)
> +{
> +	return socket_can_process(conn, POLLOUT);
> +}
> +
> +static bool socket_can_read(struct connection *conn)
> +{
> +	return socket_can_process(conn, POLLIN);
> +}
> +
>   const struct interface_funcs socket_funcs = {
>   	.write = writefd,
>   	.read = readfd,
> +	.can_write = socket_can_write,
> +	.can_read = socket_can_read,
>   };
>   
>   static void accept_connection(int sock)
> @@ -2296,47 +2321,19 @@ int main(int argc, char *argv[])
>   			if (&next->list != &connections)
>   				talloc_increase_ref_count(next);
>   
> -			if (conn->domain) {
> -				if (domain_can_read(conn))
> -					handle_input(conn);
> -				if (talloc_free(conn) == 0)
> -					continue;
> -
> -				talloc_increase_ref_count(conn);
> -				if (domain_can_write(conn) &&
> -				    !list_empty(&conn->out_list))

AFAICT, the check "!list_empty(&conn->out_list)" can be safely removed 
because write_messages() will check if the list is empty (list_top() 
returns NULL in this case). Is that correct?

Cheers,

-- 
Julien Grall

Re: [PATCH v2 2/2] tools/xenstore: simplify xenstored main loop
Posted by Juergen Gross 4 years, 8 months ago
On 14.05.21 19:05, Julien Grall wrote:
> Hi Juergen,
> 
> On 14/05/2021 12:56, Juergen Gross wrote:
>> The main loop of xenstored is rather complicated due to different
>> handling of socket and ring-page interfaces. Unify that handling by
>> introducing interface type specific functions can_read() and
>> can_write().
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>> V2:
>> - split off function vector introduction (Julien Grall)
>> ---
>>   tools/xenstore/xenstored_core.c   | 77 +++++++++++++++----------------
>>   tools/xenstore/xenstored_core.h   |  2 +
>>   tools/xenstore/xenstored_domain.c |  2 +
>>   3 files changed, 41 insertions(+), 40 deletions(-)
>>
>> diff --git a/tools/xenstore/xenstored_core.c 
>> b/tools/xenstore/xenstored_core.c
>> index 856f518075..883a1a582a 100644
>> --- a/tools/xenstore/xenstored_core.c
>> +++ b/tools/xenstore/xenstored_core.c
>> @@ -1659,9 +1659,34 @@ static int readfd(struct connection *conn, void 
>> *data, unsigned int len)
>>       return rc;
>>   }
>> +static bool socket_can_process(struct connection *conn, int mask)
>> +{
>> +    if (conn->pollfd_idx == -1)
>> +        return false;
>> +
>> +    if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
>> +        talloc_free(conn);
>> +        return false;
>> +    }
>> +
>> +    return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
>> +}
>> +
>> +static bool socket_can_write(struct connection *conn)
>> +{
>> +    return socket_can_process(conn, POLLOUT);
>> +}
>> +
>> +static bool socket_can_read(struct connection *conn)
>> +{
>> +    return socket_can_process(conn, POLLIN);
>> +}
>> +
>>   const struct interface_funcs socket_funcs = {
>>       .write = writefd,
>>       .read = readfd,
>> +    .can_write = socket_can_write,
>> +    .can_read = socket_can_read,
>>   };
>>   static void accept_connection(int sock)
>> @@ -2296,47 +2321,19 @@ int main(int argc, char *argv[])
>>               if (&next->list != &connections)
>>                   talloc_increase_ref_count(next);
>> -            if (conn->domain) {
>> -                if (domain_can_read(conn))
>> -                    handle_input(conn);
>> -                if (talloc_free(conn) == 0)
>> -                    continue;
>> -
>> -                talloc_increase_ref_count(conn);
>> -                if (domain_can_write(conn) &&
>> -                    !list_empty(&conn->out_list))
> 
> AFAICT, the check "!list_empty(&conn->out_list)" can be safely removed 
> because write_messages() will check if the list is empty (list_top() 
> returns NULL in this case). Is that correct?

Yes.


Juergen

Re: [PATCH v2 2/2] tools/xenstore: simplify xenstored main loop
Posted by Julien Grall 4 years, 8 months ago
Hi Juergen,

On 17/05/2021 07:10, Juergen Gross wrote:
> On 14.05.21 19:05, Julien Grall wrote:
>> Hi Juergen,
>>
>> On 14/05/2021 12:56, Juergen Gross wrote:
>>> The main loop of xenstored is rather complicated due to different
>>> handling of socket and ring-page interfaces. Unify that handling by
>>> introducing interface type specific functions can_read() and
>>> can_write().
>>>
>>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>> ---
>>> V2:
>>> - split off function vector introduction (Julien Grall)
>>> ---
>>>   tools/xenstore/xenstored_core.c   | 77 +++++++++++++++----------------
>>>   tools/xenstore/xenstored_core.h   |  2 +
>>>   tools/xenstore/xenstored_domain.c |  2 +
>>>   3 files changed, 41 insertions(+), 40 deletions(-)
>>>
>>> diff --git a/tools/xenstore/xenstored_core.c 
>>> b/tools/xenstore/xenstored_core.c
>>> index 856f518075..883a1a582a 100644
>>> --- a/tools/xenstore/xenstored_core.c
>>> +++ b/tools/xenstore/xenstored_core.c
>>> @@ -1659,9 +1659,34 @@ static int readfd(struct connection *conn, 
>>> void *data, unsigned int len)
>>>       return rc;
>>>   }
>>> +static bool socket_can_process(struct connection *conn, int mask)
>>> +{
>>> +    if (conn->pollfd_idx == -1)
>>> +        return false;
>>> +
>>> +    if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
>>> +        talloc_free(conn);
>>> +        return false;
>>> +    }
>>> +
>>> +    return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
>>> +}
>>> +
>>> +static bool socket_can_write(struct connection *conn)
>>> +{
>>> +    return socket_can_process(conn, POLLOUT);
>>> +}
>>> +
>>> +static bool socket_can_read(struct connection *conn)
>>> +{
>>> +    return socket_can_process(conn, POLLIN);
>>> +}
>>> +
>>>   const struct interface_funcs socket_funcs = {
>>>       .write = writefd,
>>>       .read = readfd,
>>> +    .can_write = socket_can_write,
>>> +    .can_read = socket_can_read,
>>>   };
>>>   static void accept_connection(int sock)
>>> @@ -2296,47 +2321,19 @@ int main(int argc, char *argv[])
>>>               if (&next->list != &connections)
>>>                   talloc_increase_ref_count(next);
>>> -            if (conn->domain) {
>>> -                if (domain_can_read(conn))
>>> -                    handle_input(conn);
>>> -                if (talloc_free(conn) == 0)
>>> -                    continue;
>>> -
>>> -                talloc_increase_ref_count(conn);
>>> -                if (domain_can_write(conn) &&
>>> -                    !list_empty(&conn->out_list))
>>
>> AFAICT, the check "!list_empty(&conn->out_list)" can be safely removed 
>> because write_messages() will check if the list is empty (list_top() 
>> returns NULL in this case). Is that correct?
> 
> Yes.

Thanks, how about adding in the commit message:

"Take the opportunity to remove the empty list check before calling 
write_messages() because the function is already able to cope with an 
empty list."

I can update the commit message while committing it.

Cheers,

-- 
Julien Grall