Skip to content

Commit

Permalink
samples: net: zperf: Support existing IP
Browse files Browse the repository at this point in the history
Considering that IP can be set dynamically by DHCP
or statically before test previously, we could have
alternative way by supporting both setting IP and
existing IP.

Signed-off-by: Bub Wei <[email protected]>
  • Loading branch information
Bub Wei authored and jukkar committed Dec 13, 2018
1 parent aa19fd9 commit c356b57
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 28 deletions.
3 changes: 3 additions & 0 deletions samples/net/zperf/src/zperf_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@ extern void zperf_tcp_upload(const struct shell *shell,

extern void connect_ap(char *ssid);

const struct in_addr *zperf_get_default_if_in4_addr(void);
const struct in6_addr *zperf_get_default_if_in6_addr(void);

#endif /* __ZPERF_INTERNAL_H */
20 changes: 20 additions & 0 deletions samples/net/zperf/src/zperf_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ int zperf_get_ipv4_addr(const struct shell *shell, char *host,
return 0;
}

const struct in_addr *zperf_get_default_if_in4_addr(void)
{
#if CONFIG_NET_IPV4
return net_if_ipv4_select_src_addr(NULL,
net_ipv4_unspecified_address());
#else
return NULL;
#endif
}

const struct in6_addr *zperf_get_default_if_in6_addr(void)
{
#if CONFIG_NET_IPV6
return net_if_ipv6_select_src_addr(NULL,
net_ipv6_unspecified_address());
#else
return NULL;
#endif
}

static int cmd_setip(const struct shell *shell, size_t argc, char *argv[])
{
int start = 0;
Expand Down
57 changes: 43 additions & 14 deletions samples/net/zperf/src/zperf_tcp_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
{
struct net_context *context4 = NULL;
struct net_context *context6 = NULL;
const struct in_addr *in4_addr = NULL;
const struct in6_addr *in6_addr = NULL;
int ret;

if (IS_ENABLED(CONFIG_NET_IPV6)) {
Expand All @@ -134,7 +136,7 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
in4_addr_my = zperf_get_sin();
}

if (IS_ENABLED(CONFIG_NET_IPV4) && MY_IP4ADDR) {
if (IS_ENABLED(CONFIG_NET_IPV4)) {
ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP,
&context4);
if (ret < 0) {
Expand All @@ -143,12 +145,25 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
return;
}

ret = zperf_get_ipv4_addr(shell, MY_IP4ADDR,
&in4_addr_my->sin_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv4\n");
return;
if (MY_IP4ADDR && strlen(MY_IP4ADDR)) {
/* Use Setting IP */
ret = zperf_get_ipv4_addr(shell, MY_IP4ADDR,
&in4_addr_my->sin_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv4\n");
return;
}
} else {
/* Use existing IP */
in4_addr = zperf_get_default_if_in4_addr();
if (!in4_addr) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to get IPv4 by default\n");
return;
}
memcpy(&in4_addr_my->sin_addr, in4_addr,
sizeof(struct in_addr));
}

shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n",
Expand All @@ -157,7 +172,7 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
in4_addr_my->sin_port = htons(port);
}

if (IS_ENABLED(CONFIG_NET_IPV6) && MY_IP6ADDR) {
if (IS_ENABLED(CONFIG_NET_IPV6)) {
ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP,
&context6);
if (ret < 0) {
Expand All @@ -166,12 +181,26 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
return;
}

ret = zperf_get_ipv6_addr(shell, MY_IP6ADDR, MY_PREFIX_LEN_STR,
&in6_addr_my->sin6_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv6\n");
return;
if (MY_IP6ADDR && strlen(MY_IP6ADDR)) {
/* Use Setting IP */
ret = zperf_get_ipv6_addr(shell, MY_IP6ADDR,
MY_PREFIX_LEN_STR,
&in6_addr_my->sin6_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv6\n");
return;
}
} else {
/* Use existing IP */
in6_addr = zperf_get_default_if_in6_addr();
if (!in6_addr) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to get IPv4 by default\n");
return;
}
memcpy(&in6_addr_my->sin6_addr, in6_addr,
sizeof(struct in6_addr));
}

shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n",
Expand Down
58 changes: 44 additions & 14 deletions samples/net/zperf/src/zperf_udp_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
{
struct net_context *context4 = NULL;
struct net_context *context6 = NULL;
const struct in_addr *in4_addr = NULL;
const struct in6_addr *in6_addr = NULL;
int ret;

if (IS_ENABLED(CONFIG_NET_IPV6)) {
Expand All @@ -314,7 +316,8 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
in4_addr_my = zperf_get_sin();
}

if (IS_ENABLED(CONFIG_NET_IPV4) && MY_IP4ADDR) {

if (IS_ENABLED(CONFIG_NET_IPV4)) {
ret = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
&context4);
if (ret < 0) {
Expand All @@ -323,12 +326,25 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
return;
}

ret = zperf_get_ipv4_addr(shell, MY_IP4ADDR,
&in4_addr_my->sin_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv4\n");
return;
if (MY_IP4ADDR && strlen(MY_IP4ADDR)) {
/* Use setting IP */
ret = zperf_get_ipv4_addr(shell, MY_IP4ADDR,
&in4_addr_my->sin_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv4\n");
return;
}
} else {
/* Use existing IP */
in4_addr = zperf_get_default_if_in4_addr();
if (!in4_addr) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to get IPv4 by default\n");
return;
}
memcpy(&in4_addr_my->sin_addr, in4_addr,
sizeof(struct in_addr));
}

shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n",
Expand All @@ -350,7 +366,7 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
}
}

if (IS_ENABLED(CONFIG_NET_IPV6) && MY_IP6ADDR) {
if (IS_ENABLED(CONFIG_NET_IPV6)) {
ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP,
&context6);
if (ret < 0) {
Expand All @@ -359,12 +375,26 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
return;
}

ret = zperf_get_ipv6_addr(shell, MY_IP6ADDR, MY_PREFIX_LEN_STR,
&in6_addr_my->sin6_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv6\n");
return;
if (MY_IP6ADDR && strlen(MY_IP6ADDR)) {
/* Use setting IP */
ret = zperf_get_ipv6_addr(shell, MY_IP6ADDR,
MY_PREFIX_LEN_STR,
&in6_addr_my->sin6_addr);
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv6\n");
return;
}
} else {
/* Use existing IP */
in6_addr = zperf_get_default_if_in6_addr();
if (!in6_addr) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to get IPv4 by default\n");
return;
}
memcpy(&in6_addr_my->sin6_addr, in6_addr,
sizeof(struct in6_addr));
}

shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n",
Expand Down

0 comments on commit c356b57

Please sign in to comment.