[lnst] Introducing TCPConnection test module
by Jiří Pírko
commit 8455e3fdd307f29b9029dee66bb927913373bbfb
Author: Jan Tluka <jtluka(a)redhat.com>
Date: Tue Dec 10 17:33:51 2013 +0100
Introducing TCPConnection test module
This is a python wrapper for tcp_conn test tool.
Example of use:
<run module="TCPConnection" host="2" bg_id="server">
<options>
<option name="mode" value="server"/>
<option name="address" value="{ip(2,testiface)}"/>
<option name="portrange" value="10000-10050"/>
<option name="continuous" value="yes"/>
</options>
</run>
<ctl_wait seconds="3"/>
<run module="TCPConnection" host="1" bg_id="client">
<options>
<option name="mode" value="client"/>
<option name="address" value="{ip(2,testiface)}"/>
<option name="portrange" value="10000-10050"/>
<option name="continuous" value="yes"/>
</options>
</run>
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
Signed-off-by: Jiri Pirko <jiri(a)resnulli.us>
test_modules/TCPConnection.py | 60 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 60 insertions(+), 0 deletions(-)
---
diff --git a/test_modules/TCPConnection.py b/test_modules/TCPConnection.py
new file mode 100644
index 0000000..43b88a9
--- /dev/null
+++ b/test_modules/TCPConnection.py
@@ -0,0 +1,60 @@
+"""
+This module defines TCPConnection test,
+a python wrapper for LNST's tcp_conn test tool
+"""
+
+__author__ = """
+jtluka(a)redhat.com (Jan Tluka)
+"""
+
+import re
+import errno
+import logging
+from lnst.Common.TestsCommon import TestGeneric
+from lnst.Common.Utils import bool_it
+
+class TCPConnection(TestGeneric):
+ def run(self):
+ mode = self.get_mopt("mode")
+ address = self.get_mopt("address")
+ portrange = self.get_mopt("portrange")
+
+ continuous = self.get_opt("continuous")
+ debug = self.get_opt("debug")
+ ipv6 = self.get_opt("ipv6")
+
+ cmd = ""
+ if (mode == "server"):
+ logging.debug("TCPConnection: running as server")
+ cmd += "./tcp_listen"
+ elif (mode == "client"):
+ logging.debug("TCPConnection: running as client")
+ cmd += "./tcp_connect"
+ else:
+ raise Exception("Invalid mode value for TCPConnection test module!")
+
+ cmd += " -a %s -p %s" % (address, portrange)
+
+ if continuous and bool_it(continuous):
+ cmd += " -c"
+
+ if debug and bool_it(debug):
+ cmd += " -d"
+
+ if ipv6 and bool_it(ipv6):
+ cmd += " -6"
+
+ output = self.exec_from("tcp_conn", cmd, die_on_err=False, log_outputs=True)[0]
+
+ logging.debug("TCPConnection done, inspecting logs ...")
+
+ m = None
+ if (mode == "client"):
+ m = re.search("made [0-9]* connections", output)
+ elif (mode == "server"):
+ m = re.search("handled [0-9]* connections", output)
+
+ if m is None:
+ return self.set_fail({'msg': "Unexpected error"})
+ else:
+ return self.set_pass({'msg': m.group(0)})
9 years, 5 months
[lnst] tcp_con: add support for ipv6
by Jiří Pírko
commit de04aa6bfa9f2d49b7540b6c3558ebba4c863bbd
Author: Jan Tluka <jtluka(a)redhat.com>
Date: Tue Dec 10 17:30:06 2013 +0100
tcp_con: add support for ipv6
The tcp_conn test tools are now capable of ipv6 connections. Simply pass -6
parameter to the tool to enable it, e.g.
./tcp_listen -a fe01::1 -p 10000-10050 -6
Same goes for client program, tcp_listen,
./tcp_connect -a fe01::1 -p 10000-10050 -6
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
Signed-off-by: Jiri Pirko <jiri(a)resnulli.us>
test_tools/tcp_conn/tcp_connect.c | 54 +++++++++++++++++-----
test_tools/tcp_conn/tcp_listen.c | 91 ++++++++++++++++++++++++++++++-------
2 files changed, 118 insertions(+), 27 deletions(-)
---
diff --git a/test_tools/tcp_conn/tcp_connect.c b/test_tools/tcp_conn/tcp_connect.c
index fe351a6..3bb4597 100644
--- a/test_tools/tcp_conn/tcp_connect.c
+++ b/test_tools/tcp_conn/tcp_connect.c
@@ -31,7 +31,7 @@ sem_t *mutex;
int usage()
{
- printf("./tcp_connect -p [port_range] -a [ipaddr]\n");
+ printf("./tcp_connect -p [port_range] -a [ipaddr] [-d] [-c] [-6]\n");
return 0;
}
@@ -52,34 +52,61 @@ void terminate_connections(int p)
*term_flag = 1;
}
-int handle_connections(char* host, int port)
+int handle_connections(char* host, int port, int ipv6)
{
int conn_sock;
struct sockaddr_in my_addr;
+ struct sockaddr_in6 my_addr6;
char data[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char buf[21*10*strlen(data)+1];
+ int family;
snprintf(msg, MSG_MAX, "Starting connection on %s port %i", host, port);
debug(msg);
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(port);
- if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ if (ipv6)
{
- printf("failed on inet_aton()\n");
- return 1;
+ family = my_addr6.sin6_family = AF_INET6;
+ my_addr6.sin6_port = htons(port);
+ if (inet_pton(AF_INET6, host, &my_addr6.sin6_addr) != 1) {
+ perror("fail on inet_pton");
+ return 1;
+ }
+ }
+ else
+ {
+ family = my_addr.sin_family = AF_INET;
+ my_addr.sin_port = htons(port);
+ if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ {
+ printf("failed on inet_aton()\n");
+ return 1;
+ }
}
do
{
- conn_sock = socket(AF_INET, SOCK_STREAM, 0);
+ conn_sock = socket(family, SOCK_STREAM, 0);
if (conn_sock == -1)
{
perror("fail on socket()");
return 1;
}
- if (connect(conn_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)) == -1)
+ struct sockaddr* sa;
+ socklen_t sa_len;
+ if (ipv6)
+ {
+ sa = (struct sockaddr*) &my_addr6;
+ sa_len = sizeof(struct sockaddr_in6);
+ }
+ else
+ {
+ sa = (struct sockaddr*) &my_addr;
+ sa_len = sizeof(struct sockaddr_in);
+ }
+
+ if (connect(conn_sock, sa, sa_len) == -1)
{
perror("fail on connect");
return 1;
@@ -137,6 +164,7 @@ int main(int argc, char **argv)
char *delimiter;
struct sigaction sa;
int shm;
+ int ipv6 = 0;
term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -179,7 +207,7 @@ int main(int argc, char **argv)
handlers_count = 0;
/* collect program args */
- while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
+ while ((opt = getopt(argc, argv, "p:a:dc6")) != -1) {
switch (opt) {
case 'p':
strncpy(port_str, optarg, 256);
@@ -188,6 +216,7 @@ int main(int argc, char **argv)
if (delimiter == NULL)
{
usage();
+ return 1;
}
strncpy(str_port_start, port_str, delimiter - port_str);
str_port_start[delimiter - port_str] = '\0';
@@ -205,6 +234,9 @@ int main(int argc, char **argv)
case 'c':
cont = 1;
break;
+ case '6':
+ ipv6 = 1;
+ break;
}
}
@@ -227,7 +259,7 @@ int main(int argc, char **argv)
sa.sa_handler = SIG_DFL;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
- handle_connections(host_str, p);
+ handle_connections(host_str, p, ipv6);
return 0;
}
}
diff --git a/test_tools/tcp_conn/tcp_listen.c b/test_tools/tcp_conn/tcp_listen.c
index 2fc77f6..7a292fe 100644
--- a/test_tools/tcp_conn/tcp_listen.c
+++ b/test_tools/tcp_conn/tcp_listen.c
@@ -31,7 +31,7 @@ sem_t *mutex;
int usage()
{
- printf("./tcp_listen -p [port_range] -a [ipaddr]\n");
+ printf("./tcp_listen -p [port_range] -a [ipaddr] [-d] [-c] [-6]\n");
return 0;
}
@@ -52,42 +52,77 @@ void terminate_connections(int p)
*term_flag = 1;
}
-int handle_connections(char* host, int port)
+int handle_connections(char* host, int port, int ipv6)
{
int listen_sock;
struct sockaddr_in my_addr;
+ struct sockaddr_in6 my_addr6;
socklen_t my_addr_size = sizeof(my_addr);
+ socklen_t my_addr6_size = sizeof(my_addr6);
int remote_sock;
struct sockaddr_in remote;
+ struct sockaddr_in6 remote6;
socklen_t remote_size = sizeof(remote);
+ socklen_t remote6_size = sizeof(remote6);
char data[256];
+ int family;
snprintf(msg, MSG_MAX, "Starting listener on %s port %i", host, port);
debug(msg);
- bzero(&my_addr, my_addr_size);
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(port);
- if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ if (ipv6)
{
- printf("failed on inet_aton()\n");
- return 1;
+ family = AF_INET6;
+ bzero(&my_addr6, my_addr6_size);
+ my_addr6.sin6_family = family;
+ my_addr6.sin6_port = htons(port);
+ if (inet_pton(AF_INET6, host, &(my_addr6.sin6_addr)) != 1)
+ {
+ printf("failed on inet_pton()\n");
+ return 1;
+ }
+ }
+ else
+ {
+ family = AF_INET;
+ bzero(&my_addr, my_addr_size);
+ my_addr.sin_family = family;
+ my_addr.sin_port = htons(port);
+ if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ {
+ printf("failed on inet_aton()\n");
+ return 1;
+ }
}
- if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ if ((listen_sock = socket(family, SOCK_STREAM, 0)) == -1)
{
perror("fail on socket creation");
return 1;
}
int on = 1;
- if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
+ if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on,
+ sizeof(on)) == -1)
{
perror("fail on setsockopt");
return 1;
}
- if (bind(listen_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)))
+ struct sockaddr* sa;
+ socklen_t sa_len;
+ if (ipv6)
+ {
+ sa = (struct sockaddr*) &my_addr6;
+ sa_len = my_addr6_size;
+ }
+ else
+ {
+ sa = (struct sockaddr*) &my_addr;
+ sa_len = my_addr_size;
+ }
+
+ if (bind(listen_sock, sa, sa_len))
{
perror("fail on bind");
return 1;
@@ -102,12 +137,27 @@ int handle_connections(char* host, int port)
do
{
struct in_addr ra = remote.sin_addr;
+ struct in6_addr ra6 = remote6.sin6_addr;
char host_address_str[256];
ssize_t read_rc;
+ struct sockaddr* r_sockaddr;
+ socklen_t* r_sockaddr_size;
+
+ if (ipv6)
+ {
+ bzero(&remote6, remote6_size);
+ r_sockaddr = (struct sockaddr*) &remote6;
+ r_sockaddr_size = &remote6_size;
+ }
+ else
+ {
+ bzero(&remote, remote_size);
+ r_sockaddr = (struct sockaddr*) &remote;
+ r_sockaddr_size = &remote_size;
+ }
/* handle single connection */
- bzero(&remote, remote_size);
- remote_sock = accept(listen_sock, (struct sockaddr*) &remote, &remote_size);
+ remote_sock = accept(listen_sock, r_sockaddr, r_sockaddr_size);
if (remote_sock == -1)
{
perror("failure on accept");
@@ -119,8 +169,11 @@ int handle_connections(char* host, int port)
*connection_count += 1;
sem_post(mutex);
+ if (ipv6)
+ inet_ntop(AF_INET6, &ra6, host_address_str, 255);
+ else
+ inet_ntop(AF_INET, &ra, host_address_str, 255);
- inet_ntop(AF_INET, &ra, host_address_str, 255);
snprintf(msg, MSG_MAX, "accepted connection from host %s port %i", host_address_str, port);
debug(msg);
@@ -155,6 +208,7 @@ int main(int argc, char **argv)
struct sigaction sa;
struct sigaction sa2;
int shm;
+ int ipv6 = 0;
term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -196,7 +250,7 @@ int main(int argc, char **argv)
sigaction(SIGINT, &sa, NULL);
/* collect program args */
- while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
+ while ((opt = getopt(argc, argv, "p:a:dc6")) != -1) {
switch (opt) {
case 'p':
strncpy(port_str, optarg, 256);
@@ -205,6 +259,7 @@ int main(int argc, char **argv)
if (delimiter == NULL)
{
usage();
+ return 1;
}
strncpy(str_port_start, port_str, delimiter - port_str);
str_port_start[delimiter - port_str] = '\0';
@@ -221,6 +276,10 @@ int main(int argc, char **argv)
break;
case 'c':
cont = 1;
+ break;
+ case '6':
+ ipv6 = 1;
+ break;
}
}
@@ -246,7 +305,7 @@ int main(int argc, char **argv)
sigaction(SIGINT, &sa, NULL);
/* run the main processing loop */
- handle_connections(host_str, p);
+ handle_connections(host_str, p, ipv6);
return 0;
}
}
9 years, 5 months
[PATCH] tcp_con: add support for ipv6
by Jan Tluka
The tcp_conn test tools are now capable of ipv6 connections. Simply pass -6
parameter to the tool to enable it, e.g.
./tcp_listen -a fe01::1 -p 10000-10050 -6
Same goes for client program, tcp_listen,
./tcp_connect -a fe01::1 -p 10000-10050 -6
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
test_tools/tcp_conn/tcp_connect.c | 54 ++++++++++++++++++-----
test_tools/tcp_conn/tcp_listen.c | 91 ++++++++++++++++++++++++++++++++-------
2 files changed, 118 insertions(+), 27 deletions(-)
diff --git a/test_tools/tcp_conn/tcp_connect.c b/test_tools/tcp_conn/tcp_connect.c
index fe351a6..3bb4597 100644
--- a/test_tools/tcp_conn/tcp_connect.c
+++ b/test_tools/tcp_conn/tcp_connect.c
@@ -31,7 +31,7 @@ sem_t *mutex;
int usage()
{
- printf("./tcp_connect -p [port_range] -a [ipaddr]\n");
+ printf("./tcp_connect -p [port_range] -a [ipaddr] [-d] [-c] [-6]\n");
return 0;
}
@@ -52,34 +52,61 @@ void terminate_connections(int p)
*term_flag = 1;
}
-int handle_connections(char* host, int port)
+int handle_connections(char* host, int port, int ipv6)
{
int conn_sock;
struct sockaddr_in my_addr;
+ struct sockaddr_in6 my_addr6;
char data[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char buf[21*10*strlen(data)+1];
+ int family;
snprintf(msg, MSG_MAX, "Starting connection on %s port %i", host, port);
debug(msg);
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(port);
- if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ if (ipv6)
{
- printf("failed on inet_aton()\n");
- return 1;
+ family = my_addr6.sin6_family = AF_INET6;
+ my_addr6.sin6_port = htons(port);
+ if (inet_pton(AF_INET6, host, &my_addr6.sin6_addr) != 1) {
+ perror("fail on inet_pton");
+ return 1;
+ }
+ }
+ else
+ {
+ family = my_addr.sin_family = AF_INET;
+ my_addr.sin_port = htons(port);
+ if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ {
+ printf("failed on inet_aton()\n");
+ return 1;
+ }
}
do
{
- conn_sock = socket(AF_INET, SOCK_STREAM, 0);
+ conn_sock = socket(family, SOCK_STREAM, 0);
if (conn_sock == -1)
{
perror("fail on socket()");
return 1;
}
- if (connect(conn_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)) == -1)
+ struct sockaddr* sa;
+ socklen_t sa_len;
+ if (ipv6)
+ {
+ sa = (struct sockaddr*) &my_addr6;
+ sa_len = sizeof(struct sockaddr_in6);
+ }
+ else
+ {
+ sa = (struct sockaddr*) &my_addr;
+ sa_len = sizeof(struct sockaddr_in);
+ }
+
+ if (connect(conn_sock, sa, sa_len) == -1)
{
perror("fail on connect");
return 1;
@@ -137,6 +164,7 @@ int main(int argc, char **argv)
char *delimiter;
struct sigaction sa;
int shm;
+ int ipv6 = 0;
term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -179,7 +207,7 @@ int main(int argc, char **argv)
handlers_count = 0;
/* collect program args */
- while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
+ while ((opt = getopt(argc, argv, "p:a:dc6")) != -1) {
switch (opt) {
case 'p':
strncpy(port_str, optarg, 256);
@@ -188,6 +216,7 @@ int main(int argc, char **argv)
if (delimiter == NULL)
{
usage();
+ return 1;
}
strncpy(str_port_start, port_str, delimiter - port_str);
str_port_start[delimiter - port_str] = '\0';
@@ -205,6 +234,9 @@ int main(int argc, char **argv)
case 'c':
cont = 1;
break;
+ case '6':
+ ipv6 = 1;
+ break;
}
}
@@ -227,7 +259,7 @@ int main(int argc, char **argv)
sa.sa_handler = SIG_DFL;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
- handle_connections(host_str, p);
+ handle_connections(host_str, p, ipv6);
return 0;
}
}
diff --git a/test_tools/tcp_conn/tcp_listen.c b/test_tools/tcp_conn/tcp_listen.c
index 2fc77f6..7a292fe 100644
--- a/test_tools/tcp_conn/tcp_listen.c
+++ b/test_tools/tcp_conn/tcp_listen.c
@@ -31,7 +31,7 @@ sem_t *mutex;
int usage()
{
- printf("./tcp_listen -p [port_range] -a [ipaddr]\n");
+ printf("./tcp_listen -p [port_range] -a [ipaddr] [-d] [-c] [-6]\n");
return 0;
}
@@ -52,42 +52,77 @@ void terminate_connections(int p)
*term_flag = 1;
}
-int handle_connections(char* host, int port)
+int handle_connections(char* host, int port, int ipv6)
{
int listen_sock;
struct sockaddr_in my_addr;
+ struct sockaddr_in6 my_addr6;
socklen_t my_addr_size = sizeof(my_addr);
+ socklen_t my_addr6_size = sizeof(my_addr6);
int remote_sock;
struct sockaddr_in remote;
+ struct sockaddr_in6 remote6;
socklen_t remote_size = sizeof(remote);
+ socklen_t remote6_size = sizeof(remote6);
char data[256];
+ int family;
snprintf(msg, MSG_MAX, "Starting listener on %s port %i", host, port);
debug(msg);
- bzero(&my_addr, my_addr_size);
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(port);
- if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ if (ipv6)
{
- printf("failed on inet_aton()\n");
- return 1;
+ family = AF_INET6;
+ bzero(&my_addr6, my_addr6_size);
+ my_addr6.sin6_family = family;
+ my_addr6.sin6_port = htons(port);
+ if (inet_pton(AF_INET6, host, &(my_addr6.sin6_addr)) != 1)
+ {
+ printf("failed on inet_pton()\n");
+ return 1;
+ }
+ }
+ else
+ {
+ family = AF_INET;
+ bzero(&my_addr, my_addr_size);
+ my_addr.sin_family = family;
+ my_addr.sin_port = htons(port);
+ if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ {
+ printf("failed on inet_aton()\n");
+ return 1;
+ }
}
- if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ if ((listen_sock = socket(family, SOCK_STREAM, 0)) == -1)
{
perror("fail on socket creation");
return 1;
}
int on = 1;
- if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
+ if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on,
+ sizeof(on)) == -1)
{
perror("fail on setsockopt");
return 1;
}
- if (bind(listen_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)))
+ struct sockaddr* sa;
+ socklen_t sa_len;
+ if (ipv6)
+ {
+ sa = (struct sockaddr*) &my_addr6;
+ sa_len = my_addr6_size;
+ }
+ else
+ {
+ sa = (struct sockaddr*) &my_addr;
+ sa_len = my_addr_size;
+ }
+
+ if (bind(listen_sock, sa, sa_len))
{
perror("fail on bind");
return 1;
@@ -102,12 +137,27 @@ int handle_connections(char* host, int port)
do
{
struct in_addr ra = remote.sin_addr;
+ struct in6_addr ra6 = remote6.sin6_addr;
char host_address_str[256];
ssize_t read_rc;
+ struct sockaddr* r_sockaddr;
+ socklen_t* r_sockaddr_size;
+
+ if (ipv6)
+ {
+ bzero(&remote6, remote6_size);
+ r_sockaddr = (struct sockaddr*) &remote6;
+ r_sockaddr_size = &remote6_size;
+ }
+ else
+ {
+ bzero(&remote, remote_size);
+ r_sockaddr = (struct sockaddr*) &remote;
+ r_sockaddr_size = &remote_size;
+ }
/* handle single connection */
- bzero(&remote, remote_size);
- remote_sock = accept(listen_sock, (struct sockaddr*) &remote, &remote_size);
+ remote_sock = accept(listen_sock, r_sockaddr, r_sockaddr_size);
if (remote_sock == -1)
{
perror("failure on accept");
@@ -119,8 +169,11 @@ int handle_connections(char* host, int port)
*connection_count += 1;
sem_post(mutex);
+ if (ipv6)
+ inet_ntop(AF_INET6, &ra6, host_address_str, 255);
+ else
+ inet_ntop(AF_INET, &ra, host_address_str, 255);
- inet_ntop(AF_INET, &ra, host_address_str, 255);
snprintf(msg, MSG_MAX, "accepted connection from host %s port %i", host_address_str, port);
debug(msg);
@@ -155,6 +208,7 @@ int main(int argc, char **argv)
struct sigaction sa;
struct sigaction sa2;
int shm;
+ int ipv6 = 0;
term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -196,7 +250,7 @@ int main(int argc, char **argv)
sigaction(SIGINT, &sa, NULL);
/* collect program args */
- while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
+ while ((opt = getopt(argc, argv, "p:a:dc6")) != -1) {
switch (opt) {
case 'p':
strncpy(port_str, optarg, 256);
@@ -205,6 +259,7 @@ int main(int argc, char **argv)
if (delimiter == NULL)
{
usage();
+ return 1;
}
strncpy(str_port_start, port_str, delimiter - port_str);
str_port_start[delimiter - port_str] = '\0';
@@ -221,6 +276,10 @@ int main(int argc, char **argv)
break;
case 'c':
cont = 1;
+ break;
+ case '6':
+ ipv6 = 1;
+ break;
}
}
@@ -246,7 +305,7 @@ int main(int argc, char **argv)
sigaction(SIGINT, &sa, NULL);
/* run the main processing loop */
- handle_connections(host_str, p);
+ handle_connections(host_str, p, ipv6);
return 0;
}
}
--
1.8.1.4
9 years, 6 months
[PATCH] Introducing TCPConnection test module
by Jan Tluka
This is a python wrapper for tcp_conn test tool.
Example of use:
<run module="TCPConnection" host="2" bg_id="server">
<options>
<option name="mode" value="server"/>
<option name="address" value="{ip(2,testiface)}"/>
<option name="portrange" value="10000-10050"/>
<option name="continuous" value="yes"/>
</options>
</run>
<ctl_wait seconds="3"/>
<run module="TCPConnection" host="1" bg_id="client">
<options>
<option name="mode" value="client"/>
<option name="address" value="{ip(2,testiface)}"/>
<option name="portrange" value="10000-10050"/>
<option name="continuous" value="yes"/>
</options>
</run>
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
test_modules/TCPConnection.py | 60 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
create mode 100644 test_modules/TCPConnection.py
diff --git a/test_modules/TCPConnection.py b/test_modules/TCPConnection.py
new file mode 100644
index 0000000..43b88a9
--- /dev/null
+++ b/test_modules/TCPConnection.py
@@ -0,0 +1,60 @@
+"""
+This module defines TCPConnection test,
+a python wrapper for LNST's tcp_conn test tool
+"""
+
+__author__ = """
+jtluka(a)redhat.com (Jan Tluka)
+"""
+
+import re
+import errno
+import logging
+from lnst.Common.TestsCommon import TestGeneric
+from lnst.Common.Utils import bool_it
+
+class TCPConnection(TestGeneric):
+ def run(self):
+ mode = self.get_mopt("mode")
+ address = self.get_mopt("address")
+ portrange = self.get_mopt("portrange")
+
+ continuous = self.get_opt("continuous")
+ debug = self.get_opt("debug")
+ ipv6 = self.get_opt("ipv6")
+
+ cmd = ""
+ if (mode == "server"):
+ logging.debug("TCPConnection: running as server")
+ cmd += "./tcp_listen"
+ elif (mode == "client"):
+ logging.debug("TCPConnection: running as client")
+ cmd += "./tcp_connect"
+ else:
+ raise Exception("Invalid mode value for TCPConnection test module!")
+
+ cmd += " -a %s -p %s" % (address, portrange)
+
+ if continuous and bool_it(continuous):
+ cmd += " -c"
+
+ if debug and bool_it(debug):
+ cmd += " -d"
+
+ if ipv6 and bool_it(ipv6):
+ cmd += " -6"
+
+ output = self.exec_from("tcp_conn", cmd, die_on_err=False, log_outputs=True)[0]
+
+ logging.debug("TCPConnection done, inspecting logs ...")
+
+ m = None
+ if (mode == "client"):
+ m = re.search("made [0-9]* connections", output)
+ elif (mode == "server"):
+ m = re.search("handled [0-9]* connections", output)
+
+ if m is None:
+ return self.set_fail({'msg': "Unexpected error"})
+ else:
+ return self.set_pass({'msg': m.group(0)})
--
1.8.1.4
9 years, 6 months
[PATCH] tcp_con: add support for ipv6
by Jan Tluka
The tcp_conn test tools are now capable of ipv6 connections. Simply pass -6
parameter to the tool to enable it, e.g.
./tcp_listen -a fe01::1 -p 10000-10050 -6
Same goes for client program, tcp_listen,
./tcp_connect -a fe01::1 -p 10000-10050 -6
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
test_tools/tcp_conn/tcp_connect.c | 54 ++++++++++++++++++-----
test_tools/tcp_conn/tcp_listen.c | 91 ++++++++++++++++++++++++++++++++-------
2 files changed, 118 insertions(+), 27 deletions(-)
diff --git a/test_tools/tcp_conn/tcp_connect.c b/test_tools/tcp_conn/tcp_connect.c
index fe351a6..3bb4597 100644
--- a/test_tools/tcp_conn/tcp_connect.c
+++ b/test_tools/tcp_conn/tcp_connect.c
@@ -31,7 +31,7 @@ sem_t *mutex;
int usage()
{
- printf("./tcp_connect -p [port_range] -a [ipaddr]\n");
+ printf("./tcp_connect -p [port_range] -a [ipaddr] [-d] [-c] [-6]\n");
return 0;
}
@@ -52,34 +52,61 @@ void terminate_connections(int p)
*term_flag = 1;
}
-int handle_connections(char* host, int port)
+int handle_connections(char* host, int port, int ipv6)
{
int conn_sock;
struct sockaddr_in my_addr;
+ struct sockaddr_in6 my_addr6;
char data[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char buf[21*10*strlen(data)+1];
+ int family;
snprintf(msg, MSG_MAX, "Starting connection on %s port %i", host, port);
debug(msg);
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(port);
- if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ if (ipv6)
{
- printf("failed on inet_aton()\n");
- return 1;
+ family = my_addr6.sin6_family = AF_INET6;
+ my_addr6.sin6_port = htons(port);
+ if (inet_pton(AF_INET6, host, &my_addr6.sin6_addr) != 1) {
+ perror("fail on inet_pton");
+ return 1;
+ }
+ }
+ else
+ {
+ family = my_addr.sin_family = AF_INET;
+ my_addr.sin_port = htons(port);
+ if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ {
+ printf("failed on inet_aton()\n");
+ return 1;
+ }
}
do
{
- conn_sock = socket(AF_INET, SOCK_STREAM, 0);
+ conn_sock = socket(family, SOCK_STREAM, 0);
if (conn_sock == -1)
{
perror("fail on socket()");
return 1;
}
- if (connect(conn_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)) == -1)
+ struct sockaddr* sa;
+ socklen_t sa_len;
+ if (ipv6)
+ {
+ sa = (struct sockaddr*) &my_addr6;
+ sa_len = sizeof(struct sockaddr_in6);
+ }
+ else
+ {
+ sa = (struct sockaddr*) &my_addr;
+ sa_len = sizeof(struct sockaddr_in);
+ }
+
+ if (connect(conn_sock, sa, sa_len) == -1)
{
perror("fail on connect");
return 1;
@@ -137,6 +164,7 @@ int main(int argc, char **argv)
char *delimiter;
struct sigaction sa;
int shm;
+ int ipv6 = 0;
term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -179,7 +207,7 @@ int main(int argc, char **argv)
handlers_count = 0;
/* collect program args */
- while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
+ while ((opt = getopt(argc, argv, "p:a:dc6")) != -1) {
switch (opt) {
case 'p':
strncpy(port_str, optarg, 256);
@@ -188,6 +216,7 @@ int main(int argc, char **argv)
if (delimiter == NULL)
{
usage();
+ return 1;
}
strncpy(str_port_start, port_str, delimiter - port_str);
str_port_start[delimiter - port_str] = '\0';
@@ -205,6 +234,9 @@ int main(int argc, char **argv)
case 'c':
cont = 1;
break;
+ case '6':
+ ipv6 = 1;
+ break;
}
}
@@ -227,7 +259,7 @@ int main(int argc, char **argv)
sa.sa_handler = SIG_DFL;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
- handle_connections(host_str, p);
+ handle_connections(host_str, p, ipv6);
return 0;
}
}
diff --git a/test_tools/tcp_conn/tcp_listen.c b/test_tools/tcp_conn/tcp_listen.c
index 2fc77f6..7a292fe 100644
--- a/test_tools/tcp_conn/tcp_listen.c
+++ b/test_tools/tcp_conn/tcp_listen.c
@@ -31,7 +31,7 @@ sem_t *mutex;
int usage()
{
- printf("./tcp_listen -p [port_range] -a [ipaddr]\n");
+ printf("./tcp_listen -p [port_range] -a [ipaddr] [-d] [-c] [-6]\n");
return 0;
}
@@ -52,42 +52,77 @@ void terminate_connections(int p)
*term_flag = 1;
}
-int handle_connections(char* host, int port)
+int handle_connections(char* host, int port, int ipv6)
{
int listen_sock;
struct sockaddr_in my_addr;
+ struct sockaddr_in6 my_addr6;
socklen_t my_addr_size = sizeof(my_addr);
+ socklen_t my_addr6_size = sizeof(my_addr6);
int remote_sock;
struct sockaddr_in remote;
+ struct sockaddr_in6 remote6;
socklen_t remote_size = sizeof(remote);
+ socklen_t remote6_size = sizeof(remote6);
char data[256];
+ int family;
snprintf(msg, MSG_MAX, "Starting listener on %s port %i", host, port);
debug(msg);
- bzero(&my_addr, my_addr_size);
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(port);
- if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ if (ipv6)
{
- printf("failed on inet_aton()\n");
- return 1;
+ family = AF_INET6;
+ bzero(&my_addr6, my_addr6_size);
+ my_addr6.sin6_family = family;
+ my_addr6.sin6_port = htons(port);
+ if (inet_pton(AF_INET6, host, &(my_addr6.sin6_addr)) != 1)
+ {
+ printf("failed on inet_pton()\n");
+ return 1;
+ }
+ }
+ else
+ {
+ family = AF_INET;
+ bzero(&my_addr, my_addr_size);
+ my_addr.sin_family = family;
+ my_addr.sin_port = htons(port);
+ if (inet_aton(host, &(my_addr.sin_addr)) == 0)
+ {
+ printf("failed on inet_aton()\n");
+ return 1;
+ }
}
- if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ if ((listen_sock = socket(family, SOCK_STREAM, 0)) == -1)
{
perror("fail on socket creation");
return 1;
}
int on = 1;
- if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
+ if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on,
+ sizeof(on)) == -1)
{
perror("fail on setsockopt");
return 1;
}
- if (bind(listen_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)))
+ struct sockaddr* sa;
+ socklen_t sa_len;
+ if (ipv6)
+ {
+ sa = (struct sockaddr*) &my_addr6;
+ sa_len = my_addr6_size;
+ }
+ else
+ {
+ sa = (struct sockaddr*) &my_addr;
+ sa_len = my_addr_size;
+ }
+
+ if (bind(listen_sock, sa, sa_len))
{
perror("fail on bind");
return 1;
@@ -102,12 +137,27 @@ int handle_connections(char* host, int port)
do
{
struct in_addr ra = remote.sin_addr;
+ struct in6_addr ra6 = remote6.sin6_addr;
char host_address_str[256];
ssize_t read_rc;
+ struct sockaddr* r_sockaddr;
+ socklen_t* r_sockaddr_size;
+
+ if (ipv6)
+ {
+ bzero(&remote6, remote6_size);
+ r_sockaddr = (struct sockaddr*) &remote6;
+ r_sockaddr_size = &remote6_size;
+ }
+ else
+ {
+ bzero(&remote, remote_size);
+ r_sockaddr = (struct sockaddr*) &remote;
+ r_sockaddr_size = &remote_size;
+ }
/* handle single connection */
- bzero(&remote, remote_size);
- remote_sock = accept(listen_sock, (struct sockaddr*) &remote, &remote_size);
+ remote_sock = accept(listen_sock, r_sockaddr, r_sockaddr_size);
if (remote_sock == -1)
{
perror("failure on accept");
@@ -119,8 +169,11 @@ int handle_connections(char* host, int port)
*connection_count += 1;
sem_post(mutex);
+ if (ipv6)
+ inet_ntop(AF_INET6, &ra6, host_address_str, 255);
+ else
+ inet_ntop(AF_INET, &ra, host_address_str, 255);
- inet_ntop(AF_INET, &ra, host_address_str, 255);
snprintf(msg, MSG_MAX, "accepted connection from host %s port %i", host_address_str, port);
debug(msg);
@@ -155,6 +208,7 @@ int main(int argc, char **argv)
struct sigaction sa;
struct sigaction sa2;
int shm;
+ int ipv6 = 0;
term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -196,7 +250,7 @@ int main(int argc, char **argv)
sigaction(SIGINT, &sa, NULL);
/* collect program args */
- while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
+ while ((opt = getopt(argc, argv, "p:a:dc6")) != -1) {
switch (opt) {
case 'p':
strncpy(port_str, optarg, 256);
@@ -205,6 +259,7 @@ int main(int argc, char **argv)
if (delimiter == NULL)
{
usage();
+ return 1;
}
strncpy(str_port_start, port_str, delimiter - port_str);
str_port_start[delimiter - port_str] = '\0';
@@ -221,6 +276,10 @@ int main(int argc, char **argv)
break;
case 'c':
cont = 1;
+ break;
+ case '6':
+ ipv6 = 1;
+ break;
}
}
@@ -246,7 +305,7 @@ int main(int argc, char **argv)
sigaction(SIGINT, &sa, NULL);
/* run the main processing loop */
- handle_connections(host_str, p);
+ handle_connections(host_str, p, ipv6);
return 0;
}
}
--
1.8.1.4
9 years, 6 months
[PATCH] test_tools: make tcp_conn tools more robust and reliable
by Jan Tluka
Major and minor fixes for tcp_conn suite,
* removed unused variables and code cleanup
* server/client processes are terminated in graceful way using shared
flag instead of sending SIGKILL that left connections in unfinished state
* connection counters are now placed in shared memory instead of
a variable available to controller only and incremented upon the
signal delivery
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
test_tools/tcp_conn/Makefile | 4 +-
test_tools/tcp_conn/tcp_connect.c | 73 ++++++++++++++++++++++++++-------
test_tools/tcp_conn/tcp_listen.c | 85 ++++++++++++++++++++++++++++-----------
3 files changed, 123 insertions(+), 39 deletions(-)
diff --git a/test_tools/tcp_conn/Makefile b/test_tools/tcp_conn/Makefile
index 2e8f5e6..a570641 100644
--- a/test_tools/tcp_conn/Makefile
+++ b/test_tools/tcp_conn/Makefile
@@ -3,10 +3,10 @@ CC=gcc
all: tcp_listen tcp_connect
tcp_listen: tcp_listen.c
- $(CC) -o tcp_listen tcp_listen.c
+ $(CC) -o tcp_listen tcp_listen.c -pthread -lrt
tcp_connect: tcp_connect.c
- $(CC) -o tcp_connect tcp_connect.c
+ $(CC) -o tcp_connect tcp_connect.c -pthread -lrt
clean:
rm -f tcp_connect tcp_listen
diff --git a/test_tools/tcp_conn/tcp_connect.c b/test_tools/tcp_conn/tcp_connect.c
index cb6b8c4..36a017f 100644
--- a/test_tools/tcp_conn/tcp_connect.c
+++ b/test_tools/tcp_conn/tcp_connect.c
@@ -1,21 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <stdlib.h>
+#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
+#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
+#include <semaphore.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#define IPCKEY 5678
int connection_handlers[1024];
int handlers_count;
+int *connection_count;
int debug_on = 0;
int cont = 0;
+int *term_flag;
+
+sem_t *mutex;
int usage()
{
@@ -26,7 +38,7 @@ int usage()
#define MSG_MAX 256
char msg[MSG_MAX];
-int debug(char* msg)
+void debug(char* msg)
{
if (debug_on)
{
@@ -36,24 +48,14 @@ int debug(char* msg)
void terminate_connections(int p)
{
- int cc;
-
debug("signalled");
- for (cc=0; cc < handlers_count; cc++)
- {
- snprintf(msg, MSG_MAX, "killing process %i", connection_handlers[cc]);
- debug(msg);
- kill(connection_handlers[cc], SIGKILL);
- }
+ *term_flag = 1;
}
int handle_connections(char* host, int port)
{
int conn_sock;
- int remote_sock;
struct sockaddr_in my_addr;
- struct sockaddr remote;
- int end_loop = 0;
char data[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char buf[21*10*strlen(data)+1];
@@ -83,6 +85,10 @@ int handle_connections(char* host, int port)
return 1;
}
+ sem_wait(mutex);
+ *connection_count += 1;
+ sem_post(mutex);
+
int bursts = 5*(random() % 10) + 1;
int b;
int sum = 0;
@@ -98,7 +104,11 @@ int handle_connections(char* host, int port)
strncpy(buf + (j*strlen(data)), data, strlen(data));
}
- int wr_rc = write(conn_sock, buf, parts * strlen(data));
+ if (write(conn_sock, buf, parts * strlen(data)) == -1)
+ {
+ perror("failed to send data");
+ return 1;
+ }
usleep(100*(random()%100));
}
@@ -108,7 +118,7 @@ int handle_connections(char* host, int port)
debug(msg);
close(conn_sock);
- } while (cont);
+ } while (cont && (*term_flag) == 0);
return 0;
}
@@ -126,7 +136,40 @@ int main(int argc, char **argv)
int opt;
char *delimiter;
struct sigaction sa;
+ int shm;
+
+ term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *term_flag = 0;
+
+ connection_count = mmap(NULL, sizeof *connection_count, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *connection_count = 0;
+ /* counter synchronization stuff - semaphore and shared memory */
+ if ((shm = shm_open("myshm", O_RDWR | O_CREAT, S_IRWXU)) < 0) {
+ perror("shm_open");
+ exit(1);
+ }
+
+ if ( ftruncate(shm, sizeof(sem_t)) < 0 ) {
+ perror("ftruncate");
+ exit(1);
+ }
+
+ /* place shared mutex into shared memory */
+ if ((mutex = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0)) == MAP_FAILED) {
+ perror("mmap");
+ exit(1);
+ }
+
+ if( sem_init(mutex,1,1) < 0)
+ {
+ perror("semaphore initilization");
+ exit(0);
+ }
+
+ /* signal handling */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &terminate_connections;
@@ -200,5 +243,7 @@ int main(int argc, char **argv)
debug("tcp_connect finished");
+ printf("made %i connections\n", *connection_count);
+
return 0;
}
diff --git a/test_tools/tcp_conn/tcp_listen.c b/test_tools/tcp_conn/tcp_listen.c
index ef5eeb2..7911cfe 100644
--- a/test_tools/tcp_conn/tcp_listen.c
+++ b/test_tools/tcp_conn/tcp_listen.c
@@ -1,21 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <stdlib.h>
+#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
+#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
+#include <semaphore.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define IPCKEY 5678
int listener_count;
int listeners[1024];
+int *connection_count;
-int connection_count;
int debug_on = 0;
int cont = 0;
+int *term_flag;
+
+sem_t *mutex;
int usage()
{
@@ -23,7 +35,7 @@ int usage()
return 0;
}
-int debug(char* msg)
+void debug(char* msg)
{
if (debug_on)
{
@@ -36,21 +48,8 @@ char msg[MSG_MAX];
void terminate_connections(int p)
{
- int l;
-
debug("signal received, killing servers");
- for (l=0; l < listener_count; l++)
- {
- snprintf(msg, MSG_MAX, "killing process %i", listeners[l]);
- debug(msg);
- kill(listeners[l], SIGKILL);
- }
-}
-
-void connection_counter(int p)
-{
- debug("increase counter");
- connection_count += 1;
+ *term_flag = 1;
}
int handle_connections(char* host, int port)
@@ -81,6 +80,13 @@ int handle_connections(char* host, int port)
return 1;
}
+ int on = 1;
+ if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
+ {
+ perror("fail on setsockopt");
+ return 1;
+ }
+
if (bind(listen_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)))
{
perror("fail on bind");
@@ -109,6 +115,11 @@ int handle_connections(char* host, int port)
return 1;
}
+ sem_wait(mutex);
+ *connection_count += 1;
+ sem_post(mutex);
+
+
inet_ntop(AF_INET, &ra, host_address_str, 255);
snprintf(msg, MSG_MAX, "accepted connection from host %s port %i", host_address_str, port);
debug(msg);
@@ -122,8 +133,7 @@ int handle_connections(char* host, int port)
snprintf(msg, MSG_MAX, "connection closed, read %d bytes (%i)", sum, port);
debug(msg);
close(remote_sock);
- kill(getppid(), SIGUSR1);
- } while (cont);
+ } while (cont && (*term_flag) == 0);
close(listen_sock);
@@ -144,7 +154,40 @@ int main(int argc, char **argv)
char *delimiter;
struct sigaction sa;
struct sigaction sa2;
+ int shm;
+
+ term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *term_flag = 0;
+
+ connection_count = mmap(NULL, sizeof *connection_count, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *connection_count = 0;
+
+ /* counter synchronization stuff - semaphore and shared memory */
+ if ((shm = shm_open("myshm", O_RDWR | O_CREAT, S_IRWXU)) < 0) {
+ perror("shm_open");
+ exit(1);
+ }
+
+ if ( ftruncate(shm, sizeof(sem_t)) < 0 ) {
+ perror("ftruncate");
+ exit(1);
+ }
+ /* place shared mutex into shared memory */
+ if ((mutex = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0)) == MAP_FAILED) {
+ perror("mmap");
+ exit(1);
+ }
+
+ if( sem_init(mutex,1,1) < 0)
+ {
+ perror("semaphore initilization");
+ exit(0);
+ }
+
+ /* signal handling */
memset(&sa, 0, sizeof(sa));
memset(&sa2, 0, sizeof(sa2));
@@ -152,9 +195,6 @@ int main(int argc, char **argv)
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
- sa2.sa_handler = &connection_counter;
- sigaction(SIGUSR1, &sa2, NULL);
-
/* collect program args */
while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
switch (opt) {
@@ -216,7 +256,6 @@ int main(int argc, char **argv)
int i;
for (i=0; i < listener_count; i++)
{
- int rc = -1;
while (wait(&child_status) == -1 && errno == EINTR)
{
;
@@ -226,7 +265,7 @@ int main(int argc, char **argv)
debug("tcp_listener finished");
- printf("handled %i connections\n", connection_count);
+ printf("handled %i connections\n", *connection_count);
return 0;
}
--
1.8.1.4
9 years, 6 months
[lnst] test_tools: make tcp_conn tools more robust and reliable
by Jiří Pírko
commit 0c0486b1bdf45b1ce3e7f8e728f0777c379ee2ff
Author: Jan Tluka <jtluka(a)redhat.com>
Date: Fri Dec 6 15:47:33 2013 +0100
test_tools: make tcp_conn tools more robust and reliable
Major and minor fixes for tcp_conn suite,
* removed unused variables and code cleanup
* server/client processes are terminated in graceful way using shared
flag instead of sending SIGKILL that left connections in unfinished state
* connection counters are now placed in shared memory instead of
a variable available to controller only and incremented upon the
signal delivery
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
Signed-off-by: Jiri Pirko <jiri(a)resnulli.us>
test_tools/tcp_conn/Makefile | 4 +-
test_tools/tcp_conn/tcp_connect.c | 73 +++++++++++++++++++++++++------
test_tools/tcp_conn/tcp_listen.c | 85 +++++++++++++++++++++++++++----------
3 files changed, 123 insertions(+), 39 deletions(-)
---
diff --git a/test_tools/tcp_conn/Makefile b/test_tools/tcp_conn/Makefile
index 2e8f5e6..a570641 100644
--- a/test_tools/tcp_conn/Makefile
+++ b/test_tools/tcp_conn/Makefile
@@ -3,10 +3,10 @@ CC=gcc
all: tcp_listen tcp_connect
tcp_listen: tcp_listen.c
- $(CC) -o tcp_listen tcp_listen.c
+ $(CC) -o tcp_listen tcp_listen.c -pthread -lrt
tcp_connect: tcp_connect.c
- $(CC) -o tcp_connect tcp_connect.c
+ $(CC) -o tcp_connect tcp_connect.c -pthread -lrt
clean:
rm -f tcp_connect tcp_listen
diff --git a/test_tools/tcp_conn/tcp_connect.c b/test_tools/tcp_conn/tcp_connect.c
index cb6b8c4..fe351a6 100644
--- a/test_tools/tcp_conn/tcp_connect.c
+++ b/test_tools/tcp_conn/tcp_connect.c
@@ -1,21 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <stdlib.h>
+#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
+#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
+#include <semaphore.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#define IPCKEY 5678
int connection_handlers[1024];
int handlers_count;
+int *connection_count;
int debug_on = 0;
int cont = 0;
+int *term_flag;
+
+sem_t *mutex;
int usage()
{
@@ -26,7 +38,7 @@ int usage()
#define MSG_MAX 256
char msg[MSG_MAX];
-int debug(char* msg)
+void debug(char* msg)
{
if (debug_on)
{
@@ -36,24 +48,14 @@ int debug(char* msg)
void terminate_connections(int p)
{
- int cc;
-
debug("signalled");
- for (cc=0; cc < handlers_count; cc++)
- {
- snprintf(msg, MSG_MAX, "killing process %i", connection_handlers[cc]);
- debug(msg);
- kill(connection_handlers[cc], SIGKILL);
- }
+ *term_flag = 1;
}
int handle_connections(char* host, int port)
{
int conn_sock;
- int remote_sock;
struct sockaddr_in my_addr;
- struct sockaddr remote;
- int end_loop = 0;
char data[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char buf[21*10*strlen(data)+1];
@@ -83,6 +85,10 @@ int handle_connections(char* host, int port)
return 1;
}
+ sem_wait(mutex);
+ *connection_count += 1;
+ sem_post(mutex);
+
int bursts = 5*(random() % 10) + 1;
int b;
int sum = 0;
@@ -98,7 +104,11 @@ int handle_connections(char* host, int port)
strncpy(buf + (j*strlen(data)), data, strlen(data));
}
- int wr_rc = write(conn_sock, buf, parts * strlen(data));
+ if (write(conn_sock, buf, parts * strlen(data)) == -1)
+ {
+ perror("failed to send data");
+ return 1;
+ }
usleep(100*(random()%100));
}
@@ -108,7 +118,7 @@ int handle_connections(char* host, int port)
debug(msg);
close(conn_sock);
- } while (cont);
+ } while (cont && (*term_flag) == 0);
return 0;
}
@@ -126,7 +136,40 @@ int main(int argc, char **argv)
int opt;
char *delimiter;
struct sigaction sa;
+ int shm;
+
+ term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *term_flag = 0;
+
+ connection_count = mmap(NULL, sizeof *connection_count, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *connection_count = 0;
+ /* counter synchronization stuff - semaphore and shared memory */
+ if ((shm = shm_open("myshm", O_RDWR | O_CREAT, S_IRWXU)) < 0) {
+ perror("shm_open");
+ exit(1);
+ }
+
+ if ( ftruncate(shm, sizeof(sem_t)) < 0 ) {
+ perror("ftruncate");
+ exit(1);
+ }
+
+ /* place shared mutex into shared memory */
+ if ((mutex = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0)) == MAP_FAILED) {
+ perror("mmap");
+ exit(1);
+ }
+
+ if( sem_init(mutex,1,1) < 0)
+ {
+ perror("semaphore initilization");
+ exit(0);
+ }
+
+ /* signal handling */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &terminate_connections;
@@ -200,5 +243,7 @@ int main(int argc, char **argv)
debug("tcp_connect finished");
+ printf("made %i connections\n", *connection_count);
+
return 0;
}
diff --git a/test_tools/tcp_conn/tcp_listen.c b/test_tools/tcp_conn/tcp_listen.c
index ef5eeb2..2fc77f6 100644
--- a/test_tools/tcp_conn/tcp_listen.c
+++ b/test_tools/tcp_conn/tcp_listen.c
@@ -1,21 +1,33 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <stdlib.h>
+#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
+#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
+#include <semaphore.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define IPCKEY 5678
int listener_count;
int listeners[1024];
+int *connection_count;
-int connection_count;
int debug_on = 0;
int cont = 0;
+int *term_flag;
+
+sem_t *mutex;
int usage()
{
@@ -23,7 +35,7 @@ int usage()
return 0;
}
-int debug(char* msg)
+void debug(char* msg)
{
if (debug_on)
{
@@ -36,21 +48,8 @@ char msg[MSG_MAX];
void terminate_connections(int p)
{
- int l;
-
debug("signal received, killing servers");
- for (l=0; l < listener_count; l++)
- {
- snprintf(msg, MSG_MAX, "killing process %i", listeners[l]);
- debug(msg);
- kill(listeners[l], SIGKILL);
- }
-}
-
-void connection_counter(int p)
-{
- debug("increase counter");
- connection_count += 1;
+ *term_flag = 1;
}
int handle_connections(char* host, int port)
@@ -81,6 +80,13 @@ int handle_connections(char* host, int port)
return 1;
}
+ int on = 1;
+ if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
+ {
+ perror("fail on setsockopt");
+ return 1;
+ }
+
if (bind(listen_sock, (struct sockaddr*) &my_addr, sizeof(struct sockaddr_in)))
{
perror("fail on bind");
@@ -109,6 +115,11 @@ int handle_connections(char* host, int port)
return 1;
}
+ sem_wait(mutex);
+ *connection_count += 1;
+ sem_post(mutex);
+
+
inet_ntop(AF_INET, &ra, host_address_str, 255);
snprintf(msg, MSG_MAX, "accepted connection from host %s port %i", host_address_str, port);
debug(msg);
@@ -122,8 +133,7 @@ int handle_connections(char* host, int port)
snprintf(msg, MSG_MAX, "connection closed, read %d bytes (%i)", sum, port);
debug(msg);
close(remote_sock);
- kill(getppid(), SIGUSR1);
- } while (cont);
+ } while (cont && (*term_flag) == 0);
close(listen_sock);
@@ -144,7 +154,40 @@ int main(int argc, char **argv)
char *delimiter;
struct sigaction sa;
struct sigaction sa2;
+ int shm;
+
+ term_flag = mmap(NULL, sizeof *term_flag, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *term_flag = 0;
+
+ connection_count = mmap(NULL, sizeof *connection_count, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *connection_count = 0;
+
+ /* counter synchronization stuff - semaphore and shared memory */
+ if ((shm = shm_open("myshm", O_RDWR | O_CREAT, S_IRWXU)) < 0) {
+ perror("shm_open");
+ exit(1);
+ }
+
+ if ( ftruncate(shm, sizeof(sem_t)) < 0 ) {
+ perror("ftruncate");
+ exit(1);
+ }
+ /* place shared mutex into shared memory */
+ if ((mutex = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0)) == MAP_FAILED) {
+ perror("mmap");
+ exit(1);
+ }
+
+ if( sem_init(mutex,1,1) < 0)
+ {
+ perror("semaphore initilization");
+ exit(0);
+ }
+
+ /* signal handling */
memset(&sa, 0, sizeof(sa));
memset(&sa2, 0, sizeof(sa2));
@@ -152,9 +195,6 @@ int main(int argc, char **argv)
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
- sa2.sa_handler = &connection_counter;
- sigaction(SIGUSR1, &sa2, NULL);
-
/* collect program args */
while ((opt = getopt(argc, argv, "p:a:dc")) != -1) {
switch (opt) {
@@ -216,7 +256,6 @@ int main(int argc, char **argv)
int i;
for (i=0; i < listener_count; i++)
{
- int rc = -1;
while (wait(&child_status) == -1 && errno == EINTR)
{
;
@@ -226,7 +265,7 @@ int main(int argc, char **argv)
debug("tcp_listener finished");
- printf("handled %i connections\n", connection_count);
+ printf("handled %i connections\n", *connection_count);
return 0;
}
9 years, 6 months
[lnst] VirtUtils: be compatible with older libvirt python bindings
by Jiří Pírko
commit 7a886974d172caa4561d62f232d417697a45836b
Author: Jan Tluka <jtluka(a)redhat.com>
Date: Mon Dec 2 19:37:49 2013 +0100
VirtUtils: be compatible with older libvirt python bindings
Libvirt python API changed between 0.10.2 (F18) and 1.0.5 (F19) and default
parameter value in open() method has been added. This patch makes LNST able
to run without error with older libvirt python bindings.
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
Signed-off-by: Jiri Pirko <jiri(a)resnulli.us>
lnst/Common/VirtUtils.py | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
---
diff --git a/lnst/Common/VirtUtils.py b/lnst/Common/VirtUtils.py
index 9390714..f602c99 100644
--- a/lnst/Common/VirtUtils.py
+++ b/lnst/Common/VirtUtils.py
@@ -20,7 +20,7 @@ from lnst.Common.NetUtils import normalize_hwaddr, scan_netdevs
#this is a global object because opening the connection to libvirt in every
#object instance that uses it sometimes fails - the libvirt server probably
#can't handle that many connections at a time
-_libvirt_conn = libvirt.open()
+_libvirt_conn = libvirt.open(None)
class VirtUtilsError(Exception):
pass
9 years, 6 months
[PATCH] VirtUtils: be compatible with older libvirt python bindings
by Jan Tluka
Libvirt python API changed between 0.10.2 (F18) and 1.0.5 (F19) and default
parameter value in open() method has been added. This patch makes LNST able
to run without error with older libvirt python bindings.
Signed-off-by: Jan Tluka <jtluka(a)redhat.com>
---
lnst/Common/VirtUtils.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lnst/Common/VirtUtils.py b/lnst/Common/VirtUtils.py
index 9390714..f602c99 100644
--- a/lnst/Common/VirtUtils.py
+++ b/lnst/Common/VirtUtils.py
@@ -20,7 +20,7 @@ from lnst.Common.NetUtils import normalize_hwaddr, scan_netdevs
#this is a global object because opening the connection to libvirt in every
#object instance that uses it sometimes fails - the libvirt server probably
#can't handle that many connections at a time
-_libvirt_conn = libvirt.open()
+_libvirt_conn = libvirt.open(None)
class VirtUtilsError(Exception):
pass
--
1.8.1.4
9 years, 6 months