Commit 0bac1366 authored by Jukka Rissanen's avatar Jukka Rissanen Committed by Jukka Rissanen
Browse files

samples: net: socket: packet: Add SOCK_DGRAM support



If user has enabled SOCK_DGRAM support for AF_PACKET type, then
use that in the packet sample application instead of SOCK_RAW.
This simplifies the application as we do not need to handle
the Ethernet frame when sending or receiving the packets.

Signed-off-by: default avatarJukka Rissanen <jukka.rissanen@linux.intel.com>
parent e6672f84
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -13,4 +13,19 @@ config NET_SAMPLE_SEND_WAIT_TIME
	  If set to 0, then the packets are sent as fast as possible, which
	  will stress test the network stack.

config NET_SAMPLE_ENABLE_PACKET_DGRAM
	bool "Use AF_PACKET with SOCK_DGRAM"
	depends on NET_SOCKETS_PACKET_DGRAM
	default y
	help
	  This will strip Ethernet header from received packets
	  and insert Ethernet header to sent packets.

config NET_SAMPLE_DESTINATION_ADDR
	string "Destination Ethernet MAC address"
	depends on NET_SOCKETS_PACKET_DGRAM
	default "00:11:22:33:44:55"
	help
	  Where to send the Ethernet frames.

source "Kconfig.zephyr"
+26 −4
Original line number Diff line number Diff line
@@ -73,12 +73,18 @@ static void quit(void)

static int start_socket(int *sock)
{
	struct sockaddr_ll dst;
	struct sockaddr_ll dst = { 0 };
	int ret;

	*sock = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
	*sock = socket(AF_PACKET,
		       IS_ENABLED(CONFIG_NET_SAMPLE_ENABLE_PACKET_DGRAM) ?
							SOCK_DGRAM : SOCK_RAW,
		       ETH_P_ALL);
	if (*sock < 0) {
		LOG_ERR("Failed to create RAW socket : %d", errno);
		LOG_ERR("Failed to create %s socket : %d",
			IS_ENABLED(CONFIG_NET_SAMPLE_ENABLE_PACKET_DGRAM) ?
							"DGRAM" : "RAW",
			errno);
		return -errno;
	}

@@ -140,12 +146,28 @@ static void recv_packet(void)

static int send_packet_socket(struct packet_data *packet)
{
	struct sockaddr_ll dst;
	struct sockaddr_ll dst = { 0 };
	size_t send = 100U;
	int ret;

	dst.sll_ifindex = net_if_get_by_iface(net_if_get_default());

	if (IS_ENABLED(CONFIG_NET_SAMPLE_ENABLE_PACKET_DGRAM)) {
		dst.sll_halen = sizeof(struct net_eth_addr);

		/* FIXME: assume IP data atm */
		dst.sll_protocol = htons(ETH_P_IP);

		ret = net_bytes_from_str(
			dst.sll_addr,
			dst.sll_halen,
			CONFIG_NET_SAMPLE_DESTINATION_ADDR);
		if (ret < 0) {
			LOG_ERR("Invalid MAC address '%s'",
				CONFIG_NET_SAMPLE_DESTINATION_ADDR);
		}
	}

	do {
		/* Sending dummy data */
		ret = sendto(packet->send_sock, lorem_ipsum, send, 0,