From 713f5536e99bdc86ed59dec477a2b0b83cad723a Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 11 Jun 2020 18:13:40 +0530 Subject: [PATCH] Add retry mechanism for netlink socket creation Change-Id: Ic0512f1bc680a68a9209c097b7ef4a2fb1a8548c Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/helper/helper-nl.c | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 79eb620..a418017 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.99 +Version: 0.1.0 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-nl.c b/src/helper/helper-nl.c index ff9d1af..6d54e8f 100755 --- a/src/helper/helper-nl.c +++ b/src/helper/helper-nl.c @@ -20,35 +20,54 @@ #include #include #include +#include -/** - * create_netlink(): Create netlink socket and returns it. - * Returns: Created socket on success and -1 on failure. - */ -API int create_netlink(int protocol, uint32_t groups) +#define NETLINK_SOCK_RETRY_COUNT 3 + +int __create_netlink(int protocol, uint32_t groups, int retry) { /** * TODO it's one socket, in future make set of sockets * unique for protocol and groups */ int sock; - sock = socket(PF_NETLINK, SOCK_RAW, protocol); - if (sock < 0) + + if (retry <= 0) return -EINVAL; //LCOV_EXCL_LINE + errno = 0; + sock = socket(PF_NETLINK, SOCK_RAW, protocol); + if (sock < 0) { + STC_LOGE("failed to open socket errno [%d], retry [%d]", + errno, NETLINK_SOCK_RETRY_COUNT - retry); //LCOV_EXCL_LINE + return __create_netlink(protocol, groups, --retry); //LCOV_EXCL_LINE + } + struct sockaddr_nl src_addr = { 0, }; src_addr.nl_family = AF_NETLINK; src_addr.nl_groups = groups; + errno = 0; if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) { + STC_LOGE("failed to bind socket errno [%d], retry [%d]", + errno, NETLINK_SOCK_RETRY_COUNT - retry); //LCOV_EXCL_LINE close(sock); //LCOV_EXCL_LINE - return -1; //LCOV_EXCL_LINE + return __create_netlink(protocol, groups, --retry); //LCOV_EXCL_LINE } return sock; } +/** + * create_netlink(): Create netlink socket and returns it. + * Returns: Created socket on success and -1 on failure. + */ +API int create_netlink(int protocol, uint32_t groups) +{ + return __create_netlink(protocol, groups, NETLINK_SOCK_RETRY_COUNT); +} + void fill_attribute_list(struct rtattr **atb, const int max_len, struct rtattr *rt_na, int rt_len) { -- 2.7.4