"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-udp-flood.c" (15 Mar 2019, 3071 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-udp-flood.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.09.55_vs_0.09.56.

    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 /*
   28  *  stress_set_udp_domain()
   29  *      set the udp domain option
   30  */
   31 int stress_set_udp_flood_domain(const char *name)
   32 {
   33     int ret, udp_flood_domain;
   34 
   35     ret = stress_set_net_domain(DOMAIN_INET_ALL, "udp-flood-domain",
   36         name, &udp_flood_domain);
   37     set_setting("udp-flood-domain", TYPE_ID_INT, &udp_flood_domain);
   38 
   39     return ret;
   40 }
   41 
   42 #if defined(AF_PACKET)
   43 
   44 /*
   45  *  stress_udp_flood
   46  *  UDP flood
   47  */
   48 static int stress_udp_flood(const args_t *args)
   49 {
   50     int fd, rc = EXIT_SUCCESS, j = 0;
   51     int udp_flood_domain = AF_INET;
   52     int port = 1024;
   53     struct sockaddr *addr;
   54     socklen_t addr_len;
   55     const size_t sz_max = 23 + args->instance;
   56     size_t sz = 1;
   57 
   58     static const char data[64] =
   59         "0123456789ABCDEFGHIJKLMNOPQRSTUV"
   60         "WXYZabcdefghijklmnopqrstuvwxyz@!";
   61 
   62     (void)get_setting("udp-flood-domain", &udp_flood_domain);
   63 
   64     if ((fd = socket(udp_flood_domain, SOCK_DGRAM, AF_PACKET)) < 0) {
   65         if (errno == EPROTONOSUPPORT) {
   66             pr_inf("%s: skipping stressor, protocol not supported\n",
   67                 args->name);
   68             return EXIT_NOT_IMPLEMENTED;
   69         }
   70         pr_fail_dbg("socket");
   71         return EXIT_FAILURE;
   72     }
   73     stress_set_sockaddr(args->name, args->instance, args->pid,
   74         udp_flood_domain, port,
   75         &addr, &addr_len, NET_ADDR_ANY);
   76 
   77     do {
   78         char buf[sz];
   79         int rand_port;
   80 
   81         (void)memset(buf, data[j++ & 63], sz);
   82 
   83         stress_set_sockaddr_port(udp_flood_domain, port, addr);
   84         if (sendto(fd, buf, sz, 0, addr, addr_len) > 0)
   85             inc_counter(args);
   86         if (++port > 65535)
   87             port = 1024;
   88 
   89         if (!keep_stressing())
   90             break;
   91 
   92         rand_port = 1024 + (mwc16() % (65535 - 1024));
   93         stress_set_sockaddr_port(udp_flood_domain, rand_port, addr);
   94         if (sendto(fd, buf, sz, 0, addr, addr_len) > 0)
   95             inc_counter(args);
   96 
   97         if (++sz > sz_max)
   98             sz = 1;
   99     } while (keep_stressing());
  100 
  101     (void)close(fd);
  102 
  103     return rc;
  104 }
  105 
  106 stressor_info_t stress_udp_flood_info = {
  107     .stressor = stress_udp_flood,
  108     .class = CLASS_NETWORK | CLASS_OS
  109 };
  110 #else
  111 stressor_info_t stress_udp_flood_info = {
  112     .stressor = stress_not_implemented,
  113     .class = CLASS_NETWORK | CLASS_OS
  114 };
  115 #endif