From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Eugene Crosser <crosser@average.org>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH nft 2/2] Handle retriable errors from mnl functions
Date: Wed, 8 Dec 2021 19:11:08 +0100 [thread overview]
Message-ID: <YbD1PNLwpV/uGjn4@salvia> (raw)
In-Reply-To: <20211208134914.16365-3-crosser@average.org>
On Wed, Dec 08, 2021 at 02:49:14PM +0100, Eugene Crosser wrote:
> diff --git a/src/iface.c b/src/iface.c
> index d0e1834c..029f6476 100644
> --- a/src/iface.c
> +++ b/src/iface.c
> @@ -66,39 +66,54 @@ void iface_cache_update(void)
> struct nlmsghdr *nlh;
> struct rtgenmsg *rt;
> uint32_t seq, portid;
> + bool need_restart;
> + int retry_count = 5;
> int ret;
>
> - nlh = mnl_nlmsg_put_header(buf);
> - nlh->nlmsg_type = RTM_GETLINK;
> - nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
> - nlh->nlmsg_seq = seq = time(NULL);
> - rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
> - rt->rtgen_family = AF_PACKET;
> -
> - nl = mnl_socket_open(NETLINK_ROUTE);
> - if (nl == NULL)
> - netlink_init_error();
> -
> - if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
> - netlink_init_error();
> -
> - portid = mnl_socket_get_portid(nl);
> -
> - if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0)
> - netlink_init_error();
> -
> - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
> - while (ret > 0) {
> - ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
> - if (ret <= MNL_CB_STOP)
> - break;
> - ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
> - }
> - if (ret == -1)
> + do {
> + nlh = mnl_nlmsg_put_header(buf);
> + nlh->nlmsg_type = RTM_GETLINK;
> + nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
> + nlh->nlmsg_seq = seq = time(NULL);
> + rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
> + rt->rtgen_family = AF_PACKET;
> +
> + nl = mnl_socket_open(NETLINK_ROUTE);
> + if (nl == NULL)
> + netlink_init_error();
> +
> + if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
> + netlink_init_error();
> +
> + portid = mnl_socket_get_portid(nl);
> +
> + if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0)
> + netlink_init_error();
> +
> + need_restart = false;
> + while (true) {
> + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
> + if (ret == -1) {
> + if (errno == EINTR)
> + continue;
> + else
> + netlink_init_error();
> + }
> + ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
> + if (ret == MNL_CB_STOP)
> + break;
> + if (ret == -1) {
> + if (errno == EINTR)
> + need_restart = true;
> + else
> + netlink_init_error();
> + }
> + }
> + mnl_socket_close(nl);
BTW, could you just rename iface_cache_update() to
__iface_cache_update() then add the loop to retry on EINTR? That would
skip this extra large indent in this patch.
Thanks.
> + } while (need_restart && retry_count--);
> + if (need_restart)
> netlink_init_error();
>
> - mnl_socket_close(nl);
> -
> iface_cache_init = true;
> }
>
> --
> 2.32.0
>
prev parent reply other threads:[~2021-12-08 18:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-08 13:49 [PATCH nft] Improve handling of errors from mnl* functions" Eugene Crosser
2021-12-08 13:49 ` [PATCH nft 1/2] Use abort() in case of netlink_abi_error Eugene Crosser
2021-12-08 13:49 ` [PATCH nft 2/2] Handle retriable errors from mnl functions Eugene Crosser
2021-12-08 18:06 ` Pablo Neira Ayuso
2021-12-16 20:33 ` Eugene Crosser
2021-12-08 18:11 ` Pablo Neira Ayuso [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YbD1PNLwpV/uGjn4@salvia \
--to=pablo@netfilter.org \
--cc=crosser@average.org \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).