Skip to content

Commit 73b43e0

Browse files
jukkarAnas Nashif
authored andcommitted
drivers: eth: native_posix: Add VLAN support
Support also virtual LAN (VLAN) with native_posix ethernet driver. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 02ee365 commit 73b43e0

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

drivers/ethernet/eth_native_posix.c

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@
3131
#include "eth_native_posix_priv.h"
3232

3333
#if defined(CONFIG_NET_L2_ETHERNET)
34-
#define _ETH_L2_LAYER ETHERNET_L2
35-
#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(ETHERNET_L2)
3634
#define _ETH_MTU 1500
3735
#endif
3836

3937
#define NET_BUF_TIMEOUT MSEC(10)
4038

39+
#if defined(CONFIG_NET_VLAN)
40+
#define ETH_HDR_LEN sizeof(struct net_eth_vlan_hdr)
41+
#else
42+
#define ETH_HDR_LEN sizeof(struct net_eth_hdr)
43+
#endif
44+
4145
struct eth_context {
42-
u8_t recv[_ETH_MTU + sizeof(struct net_eth_hdr)];
43-
u8_t send[_ETH_MTU + sizeof(struct net_eth_hdr)];
46+
u8_t recv[_ETH_MTU + ETH_HDR_LEN];
47+
u8_t send[_ETH_MTU + ETH_HDR_LEN];
4448
u8_t mac_addr[6];
4549
struct net_linkaddr ll_addr;
4650
struct net_if *iface;
@@ -105,8 +109,28 @@ static struct net_linkaddr *eth_get_mac(struct eth_context *ctx)
105109
return &ctx->ll_addr;
106110
}
107111

112+
static inline struct net_if *get_iface(struct eth_context *ctx,
113+
u16_t vlan_tag)
114+
{
115+
#if defined(CONFIG_NET_VLAN)
116+
struct net_if *iface;
117+
118+
iface = net_eth_get_vlan_iface(ctx->iface, vlan_tag);
119+
if (!iface) {
120+
return ctx->iface;
121+
}
122+
123+
return iface;
124+
#else
125+
ARG_UNUSED(vlan_tag);
126+
127+
return ctx->iface;
128+
#endif
129+
}
130+
108131
static int read_data(struct eth_context *ctx, int fd)
109132
{
133+
u16_t vlan_tag = NET_VLAN_TAG_UNSPEC;
110134
struct net_pkt *pkt;
111135
struct net_buf *frag;
112136
int ret;
@@ -138,9 +162,23 @@ static int read_data(struct eth_context *ctx, int fd)
138162
count += frag->len;
139163
} while (ret > 0);
140164

165+
#if defined(CONFIG_NET_VLAN)
166+
{
167+
struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
168+
169+
if (ntohs(hdr->type) == NET_ETH_PTYPE_VLAN) {
170+
struct net_eth_vlan_hdr *hdr_vlan =
171+
(struct net_eth_vlan_hdr *)NET_ETH_HDR(pkt);
172+
173+
net_pkt_set_vlan_tci(pkt, ntohs(hdr_vlan->vlan.tci));
174+
vlan_tag = net_pkt_vlan_tag(pkt);
175+
}
176+
}
177+
#endif
178+
141179
SYS_LOG_DBG("Recv pkt %p len %d", pkt, net_pkt_get_len(pkt));
142180

143-
if (net_recv_data(ctx->iface, pkt) < 0) {
181+
if (net_recv_data(get_iface(ctx, vlan_tag), pkt) < 0) {
144182
net_pkt_unref(pkt);
145183
}
146184

@@ -179,12 +217,15 @@ static void eth_iface_init(struct net_if *iface)
179217
struct eth_context *ctx = net_if_get_device(iface)->driver_data;
180218
struct net_linkaddr *ll_addr = eth_get_mac(ctx);
181219

220+
ctx->iface = iface;
221+
222+
ethernet_init(iface);
223+
182224
if (ctx->init_done) {
183225
return;
184226
}
185227

186228
ctx->init_done = true;
187-
ctx->iface = iface;
188229

189230
#if defined(CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC)
190231
/* 00-00-5E-00-53-xx Documentation RFC 7042 */
@@ -228,12 +269,25 @@ static void eth_iface_init(struct net_if *iface)
228269
}
229270
}
230271

272+
#if defined(CONFIG_NET_VLAN)
273+
static enum eth_hw_caps eth_capabilities(struct device *dev)
274+
{
275+
ARG_UNUSED(dev);
276+
277+
return ETH_HW_VLAN;
278+
}
279+
#endif
280+
231281
static const struct ethernet_api eth_if_api = {
232282
.iface_api.init = eth_iface_init,
233283
.iface_api.send = eth_send,
284+
285+
#if defined(CONFIG_NET_VLAN)
286+
.get_capabilities = eth_capabilities,
287+
#endif
234288
};
235289

236-
NET_DEVICE_INIT(eth_native_posix, CONFIG_ETH_NATIVE_POSIX_DRV_NAME,
237-
eth_init, &eth_context_data, NULL,
238-
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &eth_if_api,
239-
_ETH_L2_LAYER, _ETH_L2_CTX_TYPE, _ETH_MTU);
290+
ETH_NET_DEVICE_INIT(eth_native_posix, CONFIG_ETH_NATIVE_POSIX_DRV_NAME,
291+
eth_init, &eth_context_data, NULL,
292+
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &eth_if_api,
293+
_ETH_MTU);

samples/net/vlan/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ CONFIG_NET_DEBUG_IF=n
5858
# and that is enforced by Kconfig file.
5959
CONFIG_NET_VLAN=y
6060
CONFIG_NET_VLAN_COUNT=2
61+
62+
# Settings for native_posix ethernet driver (if compiled for that board)
63+
CONFIG_ETH_NATIVE_POSIX=y

0 commit comments

Comments
 (0)