samples: net: socket: packet: Refactor to allow flooding

Create two threads, one for receiving packet socket data and
the other for sending raw Ethernet frames.
Add flood option where it is possible to stress test the IP
stack. Flooding is disabled by default.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
diff --git a/samples/net/sockets/packet/Kconfig b/samples/net/sockets/packet/Kconfig
new file mode 100644
index 0000000..df90133
--- /dev/null
+++ b/samples/net/sockets/packet/Kconfig
@@ -0,0 +1,16 @@
+# Private config options for packet socket sample app
+
+# Copyright (c) 2020 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+mainmenu "Networking packet socket sample application"
+
+config NET_SAMPLE_SEND_WAIT_TIME
+	int "Wait time between sent packets (in ms)"
+	default 5000
+	help
+	  By default the application will send a packet every 5 seconds.
+	  If set to 0, then the packets are sent as fast as possible, which
+	  will stress test the network stack.
+
+source "Kconfig.zephyr"
diff --git a/samples/net/sockets/packet/src/packet.c b/samples/net/sockets/packet/src/packet.c
index 7ce4022..bcce4b4 100644
--- a/samples/net/sockets/packet/src/packet.c
+++ b/samples/net/sockets/packet/src/packet.c
@@ -17,23 +17,28 @@
 #define STACK_SIZE 1024
 #define THREAD_PRIORITY K_PRIO_COOP(8)
 #define RECV_BUFFER_SIZE 1280
-#define RAW_WAIT K_SECONDS(5)
+#define WAIT_TIME CONFIG_NET_SAMPLE_SEND_WAIT_TIME
+
+#define FLOOD (CONFIG_NET_SAMPLE_SEND_WAIT_TIME ? 0 : 1)
 
 static struct k_sem quit_lock;
 
 struct packet_data {
-	int sock;
+	int send_sock;
+	int recv_sock;
 	char recv_buffer[RECV_BUFFER_SIZE];
-	struct k_delayed_work send;
 };
 
 static struct packet_data packet;
 
-static void process_packet(void);
+static void recv_packet(void);
 static void send_packet(void);
 
-K_THREAD_DEFINE(packet_thread_id, STACK_SIZE,
-		process_packet, NULL, NULL, NULL,
+K_THREAD_DEFINE(receiver_thread_id, STACK_SIZE,
+		recv_packet, NULL, NULL, NULL,
+		THREAD_PRIORITY, 0, -1);
+K_THREAD_DEFINE(sender_thread_id, STACK_SIZE,
+		send_packet, NULL, NULL, NULL,
 		THREAD_PRIORITY, 0, -1);
 
 /* Generated by http://www.lipsum.com/
@@ -66,13 +71,13 @@
 	k_sem_give(&quit_lock);
 }
 
-static int start_packet_socket(void)
+static int start_socket(int *sock)
 {
 	struct sockaddr_ll dst;
 	int ret;
 
-	packet.sock = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
-	if (packet.sock < 0) {
+	*sock = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
+	if (*sock < 0) {
 		LOG_ERR("Failed to create RAW socket : %d", errno);
 		return -errno;
 	}
@@ -80,7 +85,7 @@
 	dst.sll_ifindex = net_if_get_by_iface(net_if_get_default());
 	dst.sll_family = AF_PACKET;
 
-	ret = bind(packet.sock, (const struct sockaddr *)&dst,
+	ret = bind(*sock, (const struct sockaddr *)&dst,
 		   sizeof(struct sockaddr_ll));
 	if (ret < 0) {
 		LOG_ERR("Failed to bind packet socket : %d", errno);
@@ -90,45 +95,16 @@
 	return 0;
 }
 
-static void wait_send(struct k_work *work)
-{
-	/* Send a new packet at this point */
-	send_packet();
-}
-
-static void send_packet(void)
-{
-	struct sockaddr_ll dst;
-	uint8_t send = 100U;
-	int ret;
-
-	dst.sll_ifindex = net_if_get_by_iface(net_if_get_default());
-
-	/* Sending dummy data */
-	ret = sendto(packet.sock, lorem_ipsum, send, 0,
-		     (const struct sockaddr *)&dst,
-		     sizeof(struct sockaddr_ll));
-	if (ret < 0) {
-		LOG_ERR("Failed to send, errno %d", errno);
-	} else {
-		LOG_DBG("Sent %d bytes", send);
-	}
-
-	k_delayed_work_submit(&packet.send, RAW_WAIT);
-}
-
-static int process_packet_socket(void)
+static int recv_packet_socket(struct packet_data *packet)
 {
 	int ret = 0;
 	int received;
 
 	LOG_INF("Waiting for packets ...");
 
-	send_packet();
-
 	do {
-		received = recv(packet.sock, packet.recv_buffer,
-				    sizeof(packet.recv_buffer), 0);
+		received = recv(packet->recv_sock, packet->recv_buffer,
+				sizeof(packet->recv_buffer), 0);
 
 		if (received < 0) {
 			LOG_ERR("RAW : recv error %d", errno);
@@ -143,18 +119,67 @@
 	return ret;
 }
 
-static void process_packet(void)
+static void recv_packet(void)
 {
 	int ret;
 
-	ret = start_packet_socket();
+	ret = start_socket(&packet.recv_sock);
 	if (ret < 0) {
 		quit();
 		return;
 	}
 
 	while (ret == 0) {
-		ret = process_packet_socket();
+		ret = recv_packet_socket(&packet);
+		if (ret < 0) {
+			quit();
+			return;
+		}
+	}
+}
+
+static int send_packet_socket(struct packet_data *packet)
+{
+	struct sockaddr_ll dst;
+	size_t send = 100U;
+	int ret;
+
+	dst.sll_ifindex = net_if_get_by_iface(net_if_get_default());
+
+	do {
+		/* Sending dummy data */
+		ret = sendto(packet->send_sock, lorem_ipsum, send, 0,
+			     (const struct sockaddr *)&dst,
+			     sizeof(struct sockaddr_ll));
+		if (ret < 0) {
+			LOG_ERR("Failed to send, errno %d", errno);
+			break;
+		} else {
+			if (!FLOOD) {
+				LOG_DBG("Sent %zd bytes", send);
+			}
+		}
+
+		if (!FLOOD) {
+			k_msleep(WAIT_TIME);
+		}
+	} while (true);
+
+	return ret;
+}
+
+static void send_packet(void)
+{
+	int ret;
+
+	ret = start_socket(&packet.send_sock);
+	if (ret < 0) {
+		quit();
+		return;
+	}
+
+	while (ret == 0) {
+		ret = send_packet_socket(&packet);
 		if (ret < 0) {
 			quit();
 			return;
@@ -165,21 +190,24 @@
 void main(void)
 {
 	k_sem_init(&quit_lock, 0, UINT_MAX);
-	k_delayed_work_init(&packet.send, wait_send);
 
 	LOG_INF("Packet socket sample is running");
 
-	k_thread_start(packet_thread_id);
+	k_thread_start(receiver_thread_id);
+	k_thread_start(sender_thread_id);
 
 	k_sem_take(&quit_lock, K_FOREVER);
 
 	LOG_INF("Stopping...");
 
-	k_thread_abort(packet_thread_id);
+	k_thread_abort(receiver_thread_id);
+	k_thread_abort(sender_thread_id);
 
-	k_delayed_work_cancel(&packet.send);
+	if (packet.recv_sock > 0) {
+		(void)close(packet.recv_sock);
+	}
 
-	if (packet.sock > 0) {
-		(void)close(packet.sock);
+	if (packet.send_sock > 0) {
+		(void)close(packet.send_sock);
 	}
 }