"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-socket-diag.c" (15 Mar 2019, 5244 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-socket-diag.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.49_vs_0.09.50.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #if defined(__linux__) &&       \
   28     defined(HAVE_LINUX_SOCK_DIAG_H) &&  \
   29     defined(HAVE_LINUX_NETLINK_H) &&    \
   30     defined(HAVE_LINUX_RTNETLINK_H) &&  \
   31     defined(HAVE_LINUX_UNIX_DIAG_H)
   32 
   33 typedef struct {
   34     struct nlmsghdr nlh;
   35     struct unix_diag_req udr;
   36 } sockdiag_request_t;
   37 
   38 static int sockdiag_send(const args_t *args, const int fd)
   39 {
   40     static struct sockaddr_nl nladdr = {
   41         .nl_family = AF_NETLINK
   42     };
   43 
   44     static sockdiag_request_t request = {
   45         .nlh = {
   46             .nlmsg_len = sizeof(sockdiag_request_t),
   47             .nlmsg_type = SOCK_DIAG_BY_FAMILY,
   48             .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
   49         },
   50         .udr = {
   51             .sdiag_family = AF_UNIX,
   52             .sdiag_protocol = 0,
   53             .udiag_states = -1,
   54             .udiag_ino = 0,
   55             .udiag_cookie = { 0 },
   56             .udiag_show = UDIAG_SHOW_NAME |
   57                       UDIAG_SHOW_VFS |
   58                       UDIAG_SHOW_PEER |
   59                       UDIAG_SHOW_ICONS |
   60                       UDIAG_SHOW_RQLEN |
   61                       UDIAG_SHOW_MEMINFO,
   62         }
   63     };
   64 
   65     static struct iovec iov = {
   66         .iov_base = &request,
   67         .iov_len = sizeof(request)
   68     };
   69 
   70     static struct msghdr msg = {
   71         .msg_name = (void *)&nladdr,
   72         .msg_namelen = sizeof(nladdr),
   73         .msg_iov = &iov,
   74         .msg_iovlen = 1
   75     };
   76 
   77     while (keep_stressing()) {
   78         ssize_t ret;
   79 
   80         ret = sendmsg(fd, &msg, 0);
   81         if (ret > 0)
   82             return 1;
   83         if (errno != EINTR)
   84             return -1;
   85     }
   86     return 0;
   87 }
   88 
   89 static int stress_sockdiag_parse(
   90     const args_t *args,
   91     struct unix_diag_msg *diag,
   92     unsigned int len)
   93 {
   94     struct rtattr *attr;
   95     unsigned int rta_len;
   96 
   97     if (len < NLMSG_LENGTH(sizeof(*diag))) {
   98         /* short response, ignore for now */
   99         return -1;
  100     }
  101 
  102     if (diag->udiag_family != AF_UNIX) {
  103         /* bad family, ignore */
  104         return -1;
  105     }
  106 
  107     rta_len = len - NLMSG_LENGTH(sizeof(*diag));
  108     for (attr = (struct rtattr *) (diag + 1);
  109          RTA_OK(attr, rta_len) && keep_stressing();
  110          attr = RTA_NEXT(attr, rta_len)) {
  111         switch (attr->rta_type) {
  112         case UNIX_DIAG_NAME:
  113             inc_counter(args);
  114             break;
  115         case UNIX_DIAG_PEER:
  116             inc_counter(args);
  117             break;
  118         default:
  119             break;
  120         }
  121     }
  122 
  123     return 0;
  124 }
  125 
  126 static int sockdiag_recv(const args_t *args, const int fd)
  127 {
  128     static uint32_t buf[4096];
  129     int flags = 0;
  130 
  131     static struct sockaddr_nl nladdr = {
  132         .nl_family = AF_NETLINK
  133     };
  134 
  135     static struct iovec iov = {
  136         .iov_base = buf,
  137         .iov_len = sizeof(buf)
  138     };
  139 
  140     for (;;) {
  141         ssize_t ret;
  142         static struct msghdr msg = {
  143             .msg_name = (void *)&nladdr,
  144             .msg_namelen = sizeof(nladdr),
  145             .msg_iov = &iov,
  146             .msg_iovlen = 1
  147         };
  148         struct nlmsghdr *h = (struct nlmsghdr *)buf;
  149 
  150         ret = recvmsg(fd, &msg, flags);
  151         if (ret == 0)
  152             return 0;
  153         if (ret < 0) {
  154             if (errno == EINTR)
  155                 continue;
  156 
  157             return -1;
  158         }
  159         if (!NLMSG_OK(h, ret))
  160             return -1;
  161 
  162         for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
  163             if (h->nlmsg_type == NLMSG_DONE)
  164                 return 0;
  165 
  166             if (h->nlmsg_type == NLMSG_ERROR)
  167                 return -1;
  168 
  169             if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
  170                 return -1;
  171 
  172             if (stress_sockdiag_parse(args, NLMSG_DATA(h), h->nlmsg_len))
  173                 return -1;
  174         }
  175     }
  176     return 0;
  177 }
  178 
  179 /*
  180  *  stress_sockdiag
  181  *  stress by heavy socket I/O
  182  */
  183 static int stress_sockdiag(const args_t *args)
  184 {
  185     int ret = EXIT_SUCCESS;
  186 
  187     do {
  188         int fd, rc;
  189 
  190         fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
  191         if (fd < 0) {
  192             if (errno == EPROTONOSUPPORT) {
  193                 pr_inf("%s: NETLINK_SOCK_DIAG not supported, skipping stressor\n",
  194                     args->name);
  195                 ret = EXIT_NOT_IMPLEMENTED;
  196                 break;
  197             }
  198             pr_err("%s: NETLINK_SOCK_DIAG open failed: errno=%d (%s)\n",
  199                 args->name, errno, strerror(errno));
  200             ret = EXIT_FAILURE;
  201             break;
  202         }
  203         rc = sockdiag_send(args, fd);
  204         if (rc < 0) {
  205             pr_err("%s: NETLINK_SOCK_DIAG send query failed: errno=%d (%s)\n",
  206                 args->name, errno, strerror(errno));
  207             ret = EXIT_FAILURE;
  208             (void)close(fd);
  209             break;
  210         } else if (rc == 0) {
  211             /* Nothing sent or timed out? */
  212             (void)close(fd);
  213             break;
  214         }
  215         rc = sockdiag_recv(args, fd);
  216         if (rc < 0) {
  217             (void)close(fd);
  218             break;
  219         }
  220         (void)close(fd);
  221     } while (keep_stressing());
  222 
  223     return ret;
  224 }
  225 
  226 stressor_info_t stress_sockdiag_info = {
  227     .stressor = stress_sockdiag,
  228     .class = CLASS_NETWORK | CLASS_OS
  229 };
  230 #else
  231 stressor_info_t stress_sockdiag_info = {
  232     .stressor = stress_not_implemented,
  233     .class = CLASS_NETWORK | CLASS_OS
  234 };
  235 #endif