"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-net.c" (15 Mar 2019, 4570 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 "core-net.c" see the Fossies "Dox" file reference documentation.

    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 typedef struct {
   28     const char *name;
   29     const int  domain;
   30     const int  domain_flags;
   31 } domain_t;
   32 
   33 static const domain_t domains[] = {
   34     { "ipv4",   AF_INET,    DOMAIN_INET },
   35     { "ipv6",   AF_INET6,   DOMAIN_INET6 },
   36     { "unix",   AF_UNIX,    DOMAIN_UNIX },
   37 };
   38 
   39 /*
   40  *  stress_set_net_port()
   41  *  set up port number from opt
   42  */
   43 void stress_set_net_port(
   44     const char *optname,
   45     const char *opt,
   46     const int min_port,
   47     const int max_port,
   48     int *port)
   49 {
   50     *port = get_uint64(opt);
   51     check_range(optname, *port,
   52         min_port, max_port - STRESS_PROCS_MAX);
   53 }
   54 
   55 /*
   56  *  stress_set_net_domain()
   57  *  set the domain option
   58  */
   59 int stress_set_net_domain(
   60     const int domain_mask,
   61     const char *name,
   62     const char *domain_name,
   63     int *domain)
   64 {
   65     size_t i;
   66 
   67     for (i = 0; i < SIZEOF_ARRAY(domains); i++) {
   68         if ((domain_mask & domains[i].domain_flags) &&
   69             !strcmp(domain_name, domains[i].name)) {
   70             *domain = domains[i].domain;
   71             return 0;
   72         }
   73     }
   74     (void)fprintf(stderr, "%s: domain must be one of:", name);
   75     for (i = 0; i < SIZEOF_ARRAY(domains); i++)
   76         if (domain_mask & domains[i].domain_flags)
   77             (void)fprintf(stderr, " %s", domains[i].name);
   78     (void)fprintf(stderr, "\n");
   79     *domain = 0;
   80     return -1;
   81 }
   82 
   83 /*
   84  *  setup socket address
   85  */
   86 void stress_set_sockaddr(
   87     const char *name,
   88     const uint32_t instance,
   89     const pid_t ppid,
   90     const int domain,
   91     const int port,
   92     struct sockaddr **sockaddr,
   93     socklen_t *len,
   94     const int net_addr)
   95 {
   96     switch (domain) {
   97 #if defined(AF_INET)
   98     case AF_INET: {
   99         static struct sockaddr_in addr;
  100 
  101         (void)memset(&addr, 0, sizeof(addr));
  102         addr.sin_family = domain;
  103         switch (net_addr) {
  104         case NET_ADDR_LOOPBACK:
  105             addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  106             break;
  107         case NET_ADDR_ANY:
  108         default:
  109             addr.sin_addr.s_addr = htonl(INADDR_ANY);
  110             break;
  111         }
  112         addr.sin_port = htons(port + instance);
  113         *sockaddr = (struct sockaddr *)&addr;
  114         *len = sizeof(addr);
  115         break;
  116     }
  117 #endif
  118 #if defined(AF_INET6)
  119     case AF_INET6: {
  120         static struct sockaddr_in6 addr;
  121 #if defined(__minix__)
  122         static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  123         static const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  124 #endif
  125         (void)memset(&addr, 0, sizeof(addr));
  126         addr.sin6_family = domain;
  127         switch (net_addr) {
  128         case NET_ADDR_LOOPBACK:
  129             addr.sin6_addr = in6addr_loopback;
  130             break;
  131         case NET_ADDR_ANY:
  132         default:
  133             addr.sin6_addr = in6addr_any;
  134             break;
  135         }
  136         addr.sin6_port = htons(port + instance);
  137         *sockaddr = (struct sockaddr *)&addr;
  138         *len = sizeof(addr);
  139         break;
  140     }
  141 #endif
  142 #if defined(AF_UNIX)
  143     case AF_UNIX: {
  144         static struct sockaddr_un addr;
  145 
  146         (void)memset(&addr, 0, sizeof(addr));
  147         addr.sun_family = AF_UNIX;
  148         (void)snprintf(addr.sun_path, sizeof(addr.sun_path),
  149             "/tmp/stress-ng-%d-%" PRIu32,
  150             (int)ppid, instance);
  151         *sockaddr = (struct sockaddr *)&addr;
  152         *len = sizeof(addr);
  153         break;
  154     }
  155 #endif
  156     default:
  157         pr_fail("%s: unknown domain %d\n", name, domain);
  158         (void)kill(getppid(), SIGALRM);
  159         _exit(EXIT_FAILURE);
  160     }
  161 }
  162 
  163 /*
  164  *  setup just the socket address port
  165  */
  166 void HOT stress_set_sockaddr_port(
  167     const int domain,
  168     const int port,
  169     struct sockaddr *sockaddr)
  170 {
  171     switch (domain) {
  172 #if defined(AF_INET)
  173     case AF_INET: {
  174         struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
  175 
  176         addr->sin_port = htons(port);
  177         break;
  178     }
  179 #endif
  180 #if defined(AF_INET6)
  181     case AF_INET6: {
  182         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)sockaddr;
  183         addr->sin6_port = htons(port);
  184         break;
  185     }
  186 #endif
  187 #if defined(AF_UNIX)
  188     case AF_UNIX:
  189         break;
  190 #endif
  191     default:
  192         break;
  193     }
  194 }