"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-icmp-flood.c" (15 Mar 2019, 4282 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-icmp-flood.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(HAVE_NETINET_IP_H) &&   \
   28     defined(HAVE_NETINET_IP_ICMP_H) &&  \
   29     defined(HAVE_ICMPHDR)
   30 
   31 #define MAX_PAYLOAD_SIZE    (1000)
   32 
   33 /*
   34  *  stress_icmp_flood_supported()
   35  *      check if we can run this as root
   36  */
   37 static int stress_icmp_flood_supported(void)
   38 {
   39     if (geteuid() != 0) {
   40         pr_inf("icmp flood stressor will be skipped, "
   41             "need to be running as root for this stressor\n");
   42         return -1;
   43     }
   44     return 0;
   45 }
   46 
   47 static inline uint32_t checksum(uint16_t *ptr, size_t n)
   48 {
   49     uint32_t sum = 0;
   50 
   51     while (n > 1) {
   52         sum += *ptr++;
   53         n -= 2;
   54     }
   55 
   56     if (n)
   57         sum += *(uint8_t*)ptr;
   58 
   59     sum = (sum >> 16) + (sum & 0xffff);
   60     sum += (sum >> 16);
   61 
   62     return ~sum;
   63 }
   64 
   65 /*
   66  *  stress_icmp_flood
   67  *  stress local host with ICMP flood
   68  */
   69 static int stress_icmp_flood(const args_t *args)
   70 {
   71     int fd, rc = EXIT_FAILURE;
   72     const int set_on = 1;
   73     const unsigned long addr = inet_addr("127.0.0.1");
   74     struct sockaddr_in servaddr;
   75     uint64_t sendto_fails = 0;
   76 
   77     fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
   78     if (fd < 0) {
   79         pr_fail_err("socket");
   80         goto err;
   81     }
   82     if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL,
   83         (const char *)&set_on, sizeof(set_on)) < 0) {
   84         pr_fail_err("setsockopt IP_HDRINCL");
   85         goto err_socket;
   86     }
   87     if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
   88         (const char *)&set_on, sizeof(set_on)) < 0) {
   89         pr_fail_err("setsockopt SO_BROADCAST");
   90         goto err_socket;
   91     }
   92 
   93     servaddr.sin_family = AF_INET;
   94     servaddr.sin_addr.s_addr = addr;
   95     (void)memset(&servaddr.sin_zero, 0, sizeof(servaddr.sin_zero));
   96 
   97     do {
   98         const size_t payload_len = (mwc32() % MAX_PAYLOAD_SIZE) + 1;
   99         const size_t pkt_len =
  100             sizeof(struct iphdr) + sizeof(struct icmphdr) + payload_len;
  101         char pkt[pkt_len];
  102         struct iphdr *ip_hdr = (struct iphdr *)pkt;
  103         struct icmphdr *icmp_hdr = (struct icmphdr *)(pkt + sizeof(struct iphdr));
  104 
  105         (void)memset(pkt, 0, sizeof(pkt));
  106 
  107         ip_hdr->version = 4;
  108         ip_hdr->ihl = 5;
  109         ip_hdr->tos = 0;
  110         ip_hdr->tot_len = htons(pkt_len);
  111         ip_hdr->id = mwc32();
  112         ip_hdr->frag_off = 0;
  113         ip_hdr->ttl = 64;
  114         ip_hdr->protocol = IPPROTO_ICMP;
  115         ip_hdr->saddr = addr;
  116         ip_hdr->daddr = addr;
  117 
  118         icmp_hdr->type = ICMP_ECHO;
  119         icmp_hdr->code = 0;
  120         icmp_hdr->un.echo.sequence = mwc32();
  121         icmp_hdr->un.echo.id = mwc32();
  122 
  123         /*
  124          * Generating random data is expensive so do it every 64 packets
  125          */
  126         if ((get_counter(args) & 0x3f) == 0)
  127             stress_strnrnd(pkt + sizeof(struct iphdr) +
  128                 sizeof(struct icmphdr), payload_len);
  129         icmp_hdr->checksum = checksum((uint16_t *)icmp_hdr,
  130             sizeof(struct icmphdr) + payload_len);
  131 
  132         if ((sendto(fd, pkt, pkt_len, 0,
  133                (struct sockaddr*)&servaddr, sizeof(servaddr))) < 1) {
  134             sendto_fails++;
  135         }
  136         inc_counter(args);
  137     } while (keep_stressing());
  138 
  139     pr_dbg("%s: %.2f%% of %" PRIu64 " sendto messages succeeded.\n",
  140         args->name,
  141         100.0 * (float)(get_counter(args) - sendto_fails) / get_counter(args),
  142         get_counter(args));
  143 
  144     rc = EXIT_SUCCESS;
  145 
  146 err_socket:
  147     (void)close(fd);
  148 err:
  149     return rc;
  150 }
  151 
  152 stressor_info_t stress_icmp_flood_info = {
  153     .stressor = stress_icmp_flood,
  154     .supported = stress_icmp_flood_supported,
  155     .class = CLASS_OS | CLASS_NETWORK
  156 };
  157 #else
  158 stressor_info_t stress_icmp_flood_info = {
  159     .stressor = stress_not_implemented,
  160     .class = CLASS_OS | CLASS_NETWORK
  161 };
  162 #endif