Skip to content

Commit 7e8ded9

Browse files
rveerama1jukkar
authored andcommitted
samples: sockets: Add packet socket sample application
Sample application which opens a packet socket and receives every packet on the wire and send some dummy packet over socket. Simple demo of how to use packet sockets. Signed-off-by: Ravi kumar Veeramally <[email protected]>
1 parent a9a2b3e commit 7e8ded9

File tree

5 files changed

+280
-0
lines changed

5 files changed

+280
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.13.1)
2+
3+
macro(set_conf_file)
4+
if(EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
5+
set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
6+
else()
7+
set(CONF_FILE "prj.conf")
8+
endif()
9+
endmacro()
10+
11+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
12+
project(sockets_echo_server)
13+
14+
target_sources( app PRIVATE src/packet.c)
15+
16+
include($ENV{ZEPHYR_BASE}/samples/net/common/common.cmake)

samples/net/sockets/packet/README.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. _packet-socket-sample:
2+
3+
Packet socket sample
4+
####################
5+
6+
Overview
7+
********
8+
9+
This sample is a simple packet socket application showing usage
10+
of packet sockets over Ethernet. The sample prints every packet
11+
received, and sends a dummy packet every 5 seconds.
12+
The Zephyr network subsystem does not touch any of the headers
13+
(L2, L3, etc.).
14+
15+
Building and Running
16+
********************
17+
18+
When the application is run, it opens a packet socket and prints
19+
the length of the packet it receives. After that it sends a dummy
20+
packet every 5 seconds. You can use Wireshark to observe these
21+
sent and received packets.
22+
23+
See the `net-tools`_ project for more details.
24+
25+
This sample can be built and executed on QEMU as described
26+
in :ref:`networking_with_qemu`.
27+
28+
.. _`net-tools`: https://github.com/zephyrproject-rtos/net-tools

samples/net/sockets/packet/prj.conf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Generic networking options
2+
CONFIG_NETWORKING=y
3+
CONFIG_NET_UDP=n
4+
CONFIG_NET_TCP=n
5+
CONFIG_NET_IPV6=n
6+
CONFIG_NET_IPV4=y
7+
CONFIG_NET_MAX_CONTEXTS=10
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_SOCKETS_POSIX_NAMES=y
10+
CONFIG_POSIX_MAX_FDS=6
11+
# Packet socket configuration
12+
CONFIG_NET_SOCKETS_PACKET=y
13+
14+
# Kernel options
15+
CONFIG_MAIN_STACK_SIZE=2048
16+
CONFIG_ENTROPY_GENERATOR=y
17+
CONFIG_INIT_STACKS=y
18+
19+
# Network buffers
20+
CONFIG_NET_PKT_RX_COUNT=16
21+
CONFIG_NET_PKT_TX_COUNT=16
22+
CONFIG_NET_BUF_RX_COUNT=64
23+
CONFIG_NET_BUF_TX_COUNT=64
24+
CONFIG_NET_CONTEXT_NET_PKT_POOL=y
25+
26+
# Network shell
27+
CONFIG_NET_SHELL=y
28+
29+
# Logging
30+
CONFIG_LOG=y
31+
CONFIG_NET_LOG=y
32+
CONFIG_NET_STATISTICS=y
33+
CONFIG_PRINTK=y
34+
CONFIG_LOG_BACKEND_NATIVE_POSIX=y
35+
36+
CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=n
37+
CONFIG_NET_CONTEXT_LOG_LEVEL_DBG=n
38+
CONFIG_NET_CORE_LOG_LEVEL_DBG=n
39+
CONFIG_NET_CONN_LOG_LEVEL_DBG=n
40+
CONFIG_NET_IF_LOG_LEVEL_DBG=n
41+
CONFIG_NET_L2_ETHERNET_LOG_LEVEL_DBG=n
42+
CONFIG_ETHERNET_LOG_LEVEL_DBG=n
43+
CONFIG_NET_PKT_LOG_LEVEL_ERR=n
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sample:
2+
description: Packet socket sample
3+
name: packet-socket
4+
tests:
5+
test:
6+
harness: net
7+
platform_whitelist: native_posix
8+
tags: net sockets packet-socket
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright (c) 2019 Intel Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <logging/log.h>
8+
LOG_MODULE_REGISTER(net_pkt_sock_sample, LOG_LEVEL_DBG);
9+
10+
#include <zephyr.h>
11+
#include <errno.h>
12+
#include <stdio.h>
13+
14+
#include <net/socket.h>
15+
#include <net/ethernet.h>
16+
17+
#define STACK_SIZE 1024
18+
#define THREAD_PRIORITY K_PRIO_COOP(8)
19+
#define RECV_BUFFER_SIZE 1280
20+
#define RAW_WAIT K_SECONDS(5)
21+
22+
static struct packet_data packet;
23+
24+
static struct k_sem quit_lock;
25+
26+
struct packet_data {
27+
int sock;
28+
char recv_buffer[RECV_BUFFER_SIZE];
29+
struct k_delayed_work send;
30+
};
31+
32+
static void process_packet(void);
33+
static void send_packet(void);
34+
35+
K_THREAD_DEFINE(packet_thread_id, STACK_SIZE,
36+
process_packet, NULL, NULL, NULL,
37+
THREAD_PRIORITY, 0, K_FOREVER);
38+
39+
/* Generated by http://www.lipsum.com/
40+
* 2 paragraphs, 179 words, 1160 bytes of Lorem Ipsum
41+
*/
42+
const char lorem_ipsum[] =
43+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque "
44+
"sodales lorem lorem, sed congue enim vehicula a. Sed finibus diam sed "
45+
"odio ultrices pharetra. Nullam dictum arcu ultricies turpis congue, "
46+
"vel venenatis turpis venenatis. Nam tempus arcu eros, ac congue libero "
47+
"tristique congue. Proin velit lectus, euismod sit amet quam in, "
48+
"maximus condimentum urna. Cras vel erat luctus, mattis orci ut, varius "
49+
"urna. Nam eu lobortis velit."
50+
"\n"
51+
"Nullam sit amet diam vel odio sodales cursus vehicula eu arcu. Proin "
52+
"fringilla, enim nec consectetur mollis, lorem orci interdum nisi, "
53+
"vitae suscipit nisi mauris eu mi. Proin diam enim, mollis ac rhoncus "
54+
"vitae, placerat et eros. Suspendisse convallis, ipsum nec rhoncus "
55+
"aliquam, ex augue ultrices nisl, id aliquet mi diam quis ante. "
56+
"Pellentesque venenatis ornare ultrices. Quisque et porttitor lectus. "
57+
"Ut venenatis nunc et urna imperdiet porttitor non laoreet massa. Donec "
58+
"eleifend eros in mi sagittis egestas. Sed et mi nunc. Nunc vulputate, "
59+
"mauris non ullamcorper viverra, lorem nulla vulputate diam, et congue "
60+
"dui velit non erat. Duis interdum leo et ipsum tempor consequat. In "
61+
"faucibus enim quis purus vulputate nullam."
62+
"\n";
63+
64+
static void quit(void)
65+
{
66+
k_sem_give(&quit_lock);
67+
}
68+
69+
static int start_packet_socket(void)
70+
{
71+
struct sockaddr_ll dst;
72+
int ret;
73+
74+
packet.sock = socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
75+
if (packet.sock < 0) {
76+
LOG_ERR("Failed to create RAW socket : %d", errno);
77+
return -errno;
78+
}
79+
80+
dst.sll_ifindex = 0;
81+
dst.sll_family = AF_PACKET;
82+
83+
ret = bind(packet.sock, (const struct sockaddr *)&dst,
84+
sizeof(struct sockaddr_ll));
85+
if (ret < 0) {
86+
LOG_ERR("Failed to bind packet socket : %d", errno);
87+
return -errno;
88+
}
89+
90+
return 0;
91+
}
92+
93+
static void wait_send(struct k_work *work)
94+
{
95+
/* Send a new packet at this point */
96+
send_packet();
97+
}
98+
99+
static void send_packet(void)
100+
{
101+
struct sockaddr_ll dst;
102+
u8_t send = 100;
103+
int ret;
104+
105+
dst.sll_ifindex = 0;
106+
107+
/* Sending dummy data */
108+
ret = sendto(packet.sock, lorem_ipsum, send, 0,
109+
(const struct sockaddr *)&dst,
110+
sizeof(struct sockaddr_ll));
111+
if (ret < 0) {
112+
LOG_ERR("Failed to send, errno %d", errno);
113+
} else {
114+
LOG_DBG("Sent %d bytes", send);
115+
}
116+
117+
k_delayed_work_submit(&packet.send, RAW_WAIT);
118+
}
119+
120+
static int process_packet_socket(void)
121+
{
122+
int ret = 0;
123+
int received;
124+
125+
LOG_INF("Waiting for packets ...");
126+
127+
send_packet();
128+
129+
do {
130+
received = recv(packet.sock, packet.recv_buffer,
131+
sizeof(packet.recv_buffer), 0);
132+
133+
if (received < 0) {
134+
LOG_ERR("RAW : recv error %d", errno);
135+
ret = -errno;
136+
break;
137+
}
138+
139+
LOG_DBG("Received %d bytes", received);
140+
141+
} while (true);
142+
143+
return ret;
144+
}
145+
146+
static void process_packet(void)
147+
{
148+
int ret;
149+
150+
ret = start_packet_socket();
151+
if (ret < 0) {
152+
quit();
153+
return;
154+
}
155+
156+
while (ret == 0) {
157+
ret = process_packet_socket();
158+
if (ret < 0) {
159+
quit();
160+
return;
161+
}
162+
}
163+
}
164+
165+
void main(void)
166+
{
167+
k_sem_init(&quit_lock, 0, UINT_MAX);
168+
k_delayed_work_init(&packet.send, wait_send);
169+
170+
LOG_INF("Packet socket sample is running");
171+
172+
k_thread_start(packet_thread_id);
173+
174+
k_sem_take(&quit_lock, K_FOREVER);
175+
176+
LOG_INF("Stopping...");
177+
178+
k_thread_abort(packet_thread_id);
179+
180+
k_delayed_work_cancel(&packet.send);
181+
182+
if (packet.sock > 0) {
183+
(void)close(packet.sock);
184+
}
185+
}

0 commit comments

Comments
 (0)