|
31 | 31 | #include "eth_native_posix_priv.h"
|
32 | 32 |
|
33 | 33 | #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) |
36 | 34 | #define _ETH_MTU 1500
|
37 | 35 | #endif
|
38 | 36 |
|
39 | 37 | #define NET_BUF_TIMEOUT MSEC(10)
|
40 | 38 |
|
| 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 | + |
41 | 45 | 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]; |
44 | 48 | u8_t mac_addr[6];
|
45 | 49 | struct net_linkaddr ll_addr;
|
46 | 50 | struct net_if *iface;
|
@@ -105,8 +109,28 @@ static struct net_linkaddr *eth_get_mac(struct eth_context *ctx)
|
105 | 109 | return &ctx->ll_addr;
|
106 | 110 | }
|
107 | 111 |
|
| 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 | + |
108 | 131 | static int read_data(struct eth_context *ctx, int fd)
|
109 | 132 | {
|
| 133 | + u16_t vlan_tag = NET_VLAN_TAG_UNSPEC; |
110 | 134 | struct net_pkt *pkt;
|
111 | 135 | struct net_buf *frag;
|
112 | 136 | int ret;
|
@@ -138,9 +162,23 @@ static int read_data(struct eth_context *ctx, int fd)
|
138 | 162 | count += frag->len;
|
139 | 163 | } while (ret > 0);
|
140 | 164 |
|
| 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 | + |
141 | 179 | SYS_LOG_DBG("Recv pkt %p len %d", pkt, net_pkt_get_len(pkt));
|
142 | 180 |
|
143 |
| - if (net_recv_data(ctx->iface, pkt) < 0) { |
| 181 | + if (net_recv_data(get_iface(ctx, vlan_tag), pkt) < 0) { |
144 | 182 | net_pkt_unref(pkt);
|
145 | 183 | }
|
146 | 184 |
|
@@ -179,12 +217,15 @@ static void eth_iface_init(struct net_if *iface)
|
179 | 217 | struct eth_context *ctx = net_if_get_device(iface)->driver_data;
|
180 | 218 | struct net_linkaddr *ll_addr = eth_get_mac(ctx);
|
181 | 219 |
|
| 220 | + ctx->iface = iface; |
| 221 | + |
| 222 | + ethernet_init(iface); |
| 223 | + |
182 | 224 | if (ctx->init_done) {
|
183 | 225 | return;
|
184 | 226 | }
|
185 | 227 |
|
186 | 228 | ctx->init_done = true;
|
187 |
| - ctx->iface = iface; |
188 | 229 |
|
189 | 230 | #if defined(CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC)
|
190 | 231 | /* 00-00-5E-00-53-xx Documentation RFC 7042 */
|
@@ -228,12 +269,25 @@ static void eth_iface_init(struct net_if *iface)
|
228 | 269 | }
|
229 | 270 | }
|
230 | 271 |
|
| 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 | + |
231 | 281 | static const struct ethernet_api eth_if_api = {
|
232 | 282 | .iface_api.init = eth_iface_init,
|
233 | 283 | .iface_api.send = eth_send,
|
| 284 | + |
| 285 | +#if defined(CONFIG_NET_VLAN) |
| 286 | + .get_capabilities = eth_capabilities, |
| 287 | +#endif |
234 | 288 | };
|
235 | 289 |
|
236 |
| -NET_DEVICE_INIT(eth_native_posix, CONFIG_ETH_NATIVE_POSIX_DRV_NAME, |
237 |
| - eth_init, ð_context_data, NULL, |
238 |
| - CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, ð_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, ð_context_data, NULL, |
| 292 | + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, ð_if_api, |
| 293 | + _ETH_MTU); |
0 commit comments