1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2018 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define NET_LOG_LEVEL CONFIG_NET_L2_ETHERNET_LOG_LEVEL
10
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
13
14 #include <zephyr/types.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <zephyr/sys/printk.h>
20 #include <zephyr/linker/sections.h>
21 #include <zephyr/random/random.h>
22
23 #include <zephyr/ztest.h>
24
25 #include <zephyr/net/ethernet.h>
26 #include <zephyr/net/dummy.h>
27 #include <zephyr/net/buf.h>
28 #include <zephyr/net/net_ip.h>
29 #include <zephyr/net/ethernet_vlan.h>
30 #include <zephyr/net/net_l2.h>
31
32 #include "ipv6.h"
33
34 #define NET_LOG_ENABLED 1
35 #include "net_private.h"
36
37 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
38 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
39 #else
40 #define DBG(fmt, ...)
41 #endif
42
43 #define TEST_PORT 9999
44
45 #define VLAN_TAG_1 100
46 #define VLAN_TAG_2 200
47 #define VLAN_TAG_3 300
48 #define VLAN_TAG_4 400
49 #define VLAN_TAG_5 500
50
51 static char *test_data = "Test data to be sent";
52
53 /* Interface 1 addresses */
54 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
56
57 /* Interface 2 addresses */
58 static struct in6_addr my_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
60
61 /* Interface 3 addresses */
62 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 2, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
64
65 /* Destination address for test packets */
66 static struct in6_addr dst_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 9, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
68
69 /* Extra address is assigned to ll_addr */
70 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
71 0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
72 0x04 } } };
73
74 /* Keep track of all ethernet interfaces */
75 static struct net_if *eth_interfaces[NET_VLAN_MAX_COUNT + 1];
76 static struct net_if *dummy_interfaces[2];
77 static struct net_if *extra_eth;
78
79 static struct net_context *udp_v6_ctx;
80
81 static bool test_failed;
82 static bool test_started;
83
84 static K_SEM_DEFINE(wait_data, 0, UINT_MAX);
85
86 #define WAIT_TIME K_SECONDS(1)
87
88 struct eth_context {
89 struct net_if *iface;
90 uint8_t mac_addr[6];
91
92 uint16_t expecting_tag;
93 };
94
95 static struct eth_context eth_vlan_context;
96
eth_vlan_iface_init(struct net_if * iface)97 static void eth_vlan_iface_init(struct net_if *iface)
98 {
99 const struct device *dev = net_if_get_device(iface);
100 struct eth_context *context = dev->data;
101
102 net_if_set_link_addr(iface, context->mac_addr,
103 sizeof(context->mac_addr),
104 NET_LINK_ETHERNET);
105
106 ethernet_init(iface);
107 }
108
eth_tx(const struct device * dev,struct net_pkt * pkt)109 static int eth_tx(const struct device *dev, struct net_pkt *pkt)
110 {
111 struct eth_context *context = dev->data;
112
113 zassert_equal_ptr(ð_vlan_context, context,
114 "Context pointers do not match (%p vs %p)",
115 eth_vlan_context, context);
116
117 if (!pkt->buffer) {
118 DBG("No data to send!\n");
119 return -ENODATA;
120 }
121
122 if (test_started) {
123 struct net_eth_vlan_hdr *hdr =
124 (struct net_eth_vlan_hdr *)NET_ETH_HDR(pkt);
125
126 zassert_equal(context->expecting_tag,
127 net_pkt_vlan_tag(pkt),
128 "Invalid VLAN tag (%d vs %d) in TX pkt\n",
129 net_pkt_vlan_tag(pkt),
130 context->expecting_tag);
131
132 zassert_equal(context->expecting_tag,
133 net_eth_vlan_get_vid(ntohs(hdr->vlan.tci)),
134 "Invalid VLAN tag in ethernet header");
135
136 k_sem_give(&wait_data);
137 }
138
139 return 0;
140 }
141
eth_capabilities(const struct device * dev)142 static enum ethernet_hw_caps eth_capabilities(const struct device *dev)
143 {
144 return ETHERNET_HW_VLAN;
145 }
146
147 static struct ethernet_api api_funcs = {
148 .iface_api.init = eth_vlan_iface_init,
149
150 .get_capabilities = eth_capabilities,
151 .send = eth_tx,
152 };
153
generate_mac(uint8_t * mac_addr)154 static void generate_mac(uint8_t *mac_addr)
155 {
156 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
157 mac_addr[0] = 0x00;
158 mac_addr[1] = 0x00;
159 mac_addr[2] = 0x5E;
160 mac_addr[3] = 0x00;
161 mac_addr[4] = 0x53;
162 mac_addr[5] = sys_rand32_get();
163 }
164
eth_vlan_init(const struct device * dev)165 static int eth_vlan_init(const struct device *dev)
166 {
167 struct eth_context *context = dev->data;
168
169 generate_mac(context->mac_addr);
170
171 return 0;
172 }
173
174 ETH_NET_DEVICE_INIT(eth_vlan_test, "eth_vlan_test",
175 eth_vlan_init, NULL,
176 ð_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY,
177 &api_funcs, NET_ETH_MTU);
178
eth_init(const struct device * dev)179 static int eth_init(const struct device *dev)
180 {
181 struct eth_context *context = dev->data;
182
183 generate_mac(context->mac_addr);
184
185 return 0;
186 }
187
188 /* Create one ethernet interface that does not have VLAN support. This
189 * is quite unlikely that this would be done in real life but for testing
190 * purposes create it here.
191 */
192 NET_DEVICE_INIT(eth_test, "eth_test", eth_init, NULL,
193 ð_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY,
194 &api_funcs, ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2),
195 NET_ETH_MTU);
196
197 struct net_if_test {
198 uint8_t idx; /* not used for anything, just a dummy value */
199 uint8_t mac_addr[sizeof(struct net_eth_addr)];
200 struct net_linkaddr ll_addr;
201 };
202
net_iface_get_mac(const struct device * dev)203 static uint8_t *net_iface_get_mac(const struct device *dev)
204 {
205 struct net_if_test *data = dev->data;
206
207 if (data->mac_addr[2] == 0x00) {
208 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
209 data->mac_addr[0] = 0x00;
210 data->mac_addr[1] = 0x00;
211 data->mac_addr[2] = 0x5E;
212 data->mac_addr[3] = 0x00;
213 data->mac_addr[4] = 0x53;
214 data->mac_addr[5] = sys_rand32_get();
215 }
216
217 data->ll_addr.addr = data->mac_addr;
218 data->ll_addr.len = 6U;
219
220 return data->mac_addr;
221 }
222
net_iface_init(struct net_if * iface)223 static void net_iface_init(struct net_if *iface)
224 {
225 uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));
226
227 net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
228 NET_LINK_ETHERNET);
229 }
230
sender_iface(const struct device * dev,struct net_pkt * pkt)231 static int sender_iface(const struct device *dev, struct net_pkt *pkt)
232 {
233 return 0;
234 }
235
236 struct net_if_test net_iface1_data;
237 struct net_if_test net_iface2_data;
238
239 static struct dummy_api net_iface_api = {
240 .iface_api.init = net_iface_init,
241 .send = sender_iface,
242 };
243
244 /* For testing purposes, create two dummy network interfaces so we can check
245 * that no VLANs are created for it.
246 */
247 NET_DEVICE_INIT_INSTANCE(net_iface1_test,
248 "iface1",
249 iface1,
250 NULL,
251 NULL,
252 &net_iface1_data,
253 NULL,
254 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
255 &net_iface_api,
256 DUMMY_L2,
257 NET_L2_GET_CTX_TYPE(DUMMY_L2),
258 127);
259
260 NET_DEVICE_INIT_INSTANCE(net_iface2_test,
261 "iface2",
262 iface2,
263 NULL,
264 NULL,
265 &net_iface2_data,
266 NULL,
267 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
268 &net_iface_api,
269 DUMMY_L2,
270 NET_L2_GET_CTX_TYPE(DUMMY_L2),
271 127);
272
273 struct user_data {
274 int eth_if_count;
275 int dummy_if_count;
276 int total_if_count;
277 };
278
279 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
iface2str(struct net_if * iface)280 static const char *iface2str(struct net_if *iface)
281 {
282 #ifdef CONFIG_NET_L2_ETHERNET
283 if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
284 return "Ethernet";
285 }
286 #endif
287
288 #ifdef CONFIG_NET_L2_DUMMY
289 if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
290 return "Dummy";
291 }
292 #endif
293
294 return "<unknown type>";
295 }
296 #endif
297
iface_cb(struct net_if * iface,void * user_data)298 static void iface_cb(struct net_if *iface, void *user_data)
299 {
300 struct user_data *ud = user_data;
301
302 DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface),
303 net_if_get_by_iface(iface));
304
305 if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
306 if (PART_OF_ARRAY(NET_IF_GET_NAME(eth_test, 0), iface)) {
307 if (!extra_eth) {
308 /* Just use the first interface */
309 extra_eth = iface;
310 }
311 } else {
312 eth_interfaces[ud->eth_if_count++] = iface;
313 }
314 }
315
316 if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
317 dummy_interfaces[ud->dummy_if_count++] = iface;
318
319 zassert_true(ud->dummy_if_count <= 2,
320 "Too many dummy interfaces");
321 }
322
323 /* By default all interfaces are down initially */
324 net_if_down(iface);
325
326 ud->total_if_count++;
327 }
328
test_vlan_setup(void)329 static void test_vlan_setup(void)
330 {
331 struct user_data ud = { 0 };
332
333 /* Make sure we have enough virtual interfaces */
334 net_if_foreach(iface_cb, &ud);
335
336 /* One extra eth interface without vlan support */
337 zassert_equal(ud.eth_if_count, NET_VLAN_MAX_COUNT,
338 "Invalid number of VLANs %d vs %d\n",
339 ud.eth_if_count, NET_VLAN_MAX_COUNT);
340
341 zassert_equal(ud.total_if_count, NET_VLAN_MAX_COUNT + 1 + 2,
342 "Invalid number of interfaces");
343
344 /* Put the extra non-vlan ethernet interface to last */
345 eth_interfaces[4] = extra_eth;
346 zassert_not_null(extra_eth, "Extra interface missing");
347 zassert_equal_ptr(net_if_l2(extra_eth), &NET_L2_GET_NAME(ETHERNET),
348 "Invalid L2 type %p for iface %p (should be %p)\n",
349 net_if_l2(extra_eth), extra_eth,
350 &NET_L2_GET_NAME(ETHERNET));
351 }
352
test_address_setup(void)353 static void test_address_setup(void)
354 {
355 struct net_if_addr *ifaddr;
356 struct net_if *iface1, *iface2, *iface3;
357
358 iface1 = eth_interfaces[1]; /* This has VLAN enabled */
359 iface2 = eth_interfaces[0]; /* and this one not */
360 iface3 = eth_interfaces[3]; /* and this one has VLAN enabled */
361
362 zassert_not_null(iface1, "Interface 1");
363 zassert_not_null(iface2, "Interface 2");
364 zassert_not_null(iface3, "Interface 3");
365
366 ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
367 NET_ADDR_MANUAL, 0);
368 if (!ifaddr) {
369 DBG("Cannot add IPv6 address %s\n",
370 net_sprint_ipv6_addr(&my_addr1));
371 zassert_not_null(ifaddr, "addr1");
372 }
373
374 /* For testing purposes we need to set the addresses preferred */
375 ifaddr->addr_state = NET_ADDR_PREFERRED;
376
377 ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr,
378 NET_ADDR_MANUAL, 0);
379 if (!ifaddr) {
380 DBG("Cannot add IPv6 address %s\n",
381 net_sprint_ipv6_addr(&ll_addr));
382 zassert_not_null(ifaddr, "ll_addr");
383 }
384
385 ifaddr->addr_state = NET_ADDR_PREFERRED;
386
387 ifaddr = net_if_ipv6_addr_add(iface2, &my_addr2,
388 NET_ADDR_MANUAL, 0);
389 if (!ifaddr) {
390 DBG("Cannot add IPv6 address %s\n",
391 net_sprint_ipv6_addr(&my_addr2));
392 zassert_not_null(ifaddr, "addr2");
393 }
394
395 ifaddr->addr_state = NET_ADDR_PREFERRED;
396
397 ifaddr = net_if_ipv6_addr_add(iface3, &my_addr3,
398 NET_ADDR_MANUAL, 0);
399 if (!ifaddr) {
400 DBG("Cannot add IPv6 address %s\n",
401 net_sprint_ipv6_addr(&my_addr3));
402 zassert_not_null(ifaddr, "addr3");
403 }
404
405 net_if_up(iface1);
406 net_if_up(iface2);
407 net_if_up(iface3);
408
409 /* The interface might receive data which might fail the checks
410 * in the iface sending function, so we need to reset the failure
411 * flag.
412 */
413 test_failed = false;
414 }
415
ZTEST(net_vlan,test_vlan_tci)416 ZTEST(net_vlan, test_vlan_tci)
417 {
418 struct net_pkt *pkt;
419 uint16_t tci;
420 uint16_t tag;
421 uint8_t priority;
422 bool dei;
423
424 pkt = net_pkt_alloc(K_FOREVER);
425
426 tag = NET_VLAN_TAG_UNSPEC;
427 net_pkt_set_vlan_tag(pkt, tag);
428
429 priority = 0U;
430 net_pkt_set_vlan_priority(pkt, priority);
431
432 zassert_equal(net_pkt_vlan_tag(pkt), NET_VLAN_TAG_UNSPEC,
433 "invalid VLAN tag unspec");
434 zassert_equal(net_pkt_vlan_priority(pkt), priority,
435 "invalid VLAN priority");
436
437 net_pkt_set_vlan_tag(pkt, 0);
438 zassert_equal(net_pkt_vlan_tag(pkt), 0, "invalid VLAN tag");
439
440 /* TCI should be zero now */
441 zassert_equal(net_pkt_vlan_tci(pkt), 0, "invalid VLAN TCI");
442
443 priority = 1U;
444 net_pkt_set_vlan_priority(pkt, priority);
445
446 zassert_equal(net_pkt_vlan_priority(pkt), priority,
447 "invalid VLAN priority");
448
449 net_pkt_set_vlan_tag(pkt, tag);
450
451 zassert_equal(net_pkt_vlan_tag(pkt), NET_VLAN_TAG_UNSPEC,
452 "invalid VLAN tag unspec");
453
454 zassert_equal(net_pkt_vlan_priority(pkt), priority,
455 "invalid VLAN priority");
456
457 net_pkt_set_vlan_tag(pkt, 0);
458 zassert_equal(net_pkt_vlan_priority(pkt), priority,
459 "invalid VLAN priority");
460
461 dei = true;
462 net_pkt_set_vlan_dei(pkt, dei);
463
464 zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
465 zassert_equal(net_pkt_vlan_priority(pkt), priority,
466 "invalid VLAN priority");
467 zassert_equal(net_pkt_vlan_tag(pkt), 0, "invalid VLAN tag");
468
469 net_pkt_set_vlan_tag(pkt, tag);
470 zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
471 zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
472 zassert_equal(net_pkt_vlan_priority(pkt), priority,
473 "invalid VLAN priority");
474
475 dei = false;
476 net_pkt_set_vlan_dei(pkt, dei);
477 zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
478 zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
479 zassert_equal(net_pkt_vlan_priority(pkt), priority,
480 "invalid VLAN priority");
481
482 tag = 0U;
483 net_pkt_set_vlan_tag(pkt, tag);
484 zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
485 zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
486 zassert_equal(net_pkt_vlan_priority(pkt), priority,
487 "invalid VLAN priority");
488
489 priority = 0U;
490 net_pkt_set_vlan_priority(pkt, priority);
491 zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
492 zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
493 zassert_equal(net_pkt_vlan_priority(pkt), priority,
494 "invalid VLAN priority");
495
496 zassert_equal(net_pkt_vlan_tci(pkt), 0, "invalid VLAN TCI");
497
498 tci = 0U;
499 tag = 100U;
500 priority = 3U;
501
502 tci = net_eth_vlan_set_vid(tci, tag);
503 tci = net_eth_vlan_set_pcp(tci, priority);
504
505 zassert_equal(tag, net_eth_vlan_get_vid(tci), "Invalid VLAN tag");
506 zassert_equal(priority, net_eth_vlan_get_pcp(tci),
507 "Invalid VLAN priority");
508
509 net_pkt_unref(pkt);
510 }
511
512 /* Enable two VLAN tags and verity that proper interfaces are enabled.
513 */
test_vlan_enable(void)514 static void test_vlan_enable(void)
515 {
516 struct ethernet_context *eth_ctx;
517 struct net_if *iface;
518 int ret;
519
520 ret = net_eth_vlan_enable(eth_interfaces[1], VLAN_TAG_1);
521 zassert_equal(ret, 0, "Cannot enable %d (%d)\n", VLAN_TAG_1, ret);
522 ret = net_eth_vlan_enable(eth_interfaces[3], VLAN_TAG_2);
523 zassert_equal(ret, 0, "Cannot enable %d (%d)\n", VLAN_TAG_2, ret);
524
525 eth_ctx = net_if_l2_data(eth_interfaces[0]);
526
527 iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_1);
528 zassert_equal_ptr(iface, eth_interfaces[1],
529 "Invalid interface for tag %d (%p vs %p)\n",
530 VLAN_TAG_1, iface, eth_interfaces[1]);
531
532 iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_2);
533 zassert_equal_ptr(iface, eth_interfaces[3],
534 "Invalid interface for tag %d (%p vs %p)\n",
535 VLAN_TAG_2, iface, eth_interfaces[3]);
536
537 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
538 zassert_equal(ret, false, "VLAN enabled for interface 0");
539
540 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[1]);
541 zassert_equal(ret, true, "VLAN disabled for interface 1");
542
543 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[2]);
544 zassert_equal(ret, false, "VLAN enabled for interface 2");
545
546 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[3]);
547 zassert_equal(ret, true, "VLAN disabled for interface 3");
548
549 iface = eth_interfaces[0];
550 ret = net_eth_vlan_enable(iface, NET_VLAN_TAG_UNSPEC);
551 zassert_equal(ret, -EBADF, "Invalid VLAN tag value %d\n", ret);
552
553 iface = eth_interfaces[1];
554 ret = net_eth_vlan_enable(iface, VLAN_TAG_1);
555 zassert_equal(ret, -EALREADY, "VLAN tag %d enabled for iface 1\n",
556 VLAN_TAG_1);
557 }
558
test_vlan_disable(void)559 static void test_vlan_disable(void)
560 {
561 struct ethernet_context *eth_ctx;
562 struct net_if *iface;
563 int ret;
564
565 ret = net_eth_vlan_disable(eth_interfaces[1], VLAN_TAG_1);
566 zassert_equal(ret, 0, "Cannot disable %d (%d)\n", VLAN_TAG_1, ret);
567 ret = net_eth_vlan_disable(eth_interfaces[3], VLAN_TAG_2);
568 zassert_equal(ret, 0, "Cannot disable %d (%d)\n", VLAN_TAG_2, ret);
569
570 eth_ctx = net_if_l2_data(eth_interfaces[0]);
571
572 iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_1);
573 zassert_equal_ptr(iface, eth_interfaces[0],
574 "Invalid interface for tag %d (%p vs %p)\n",
575 VLAN_TAG_1, iface, eth_interfaces[0]);
576
577 iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_2);
578 zassert_equal_ptr(iface, eth_interfaces[0],
579 "Invalid interface for tag %d (%p vs %p)\n",
580 VLAN_TAG_2, iface, eth_interfaces[0]);
581
582 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
583 zassert_equal(ret, false, "VLAN enabled for interface 0");
584
585 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[1]);
586 zassert_equal(ret, false, "VLAN enabled for interface 1");
587
588 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[2]);
589 zassert_equal(ret, false, "VLAN enabled for interface 2");
590
591 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[3]);
592 zassert_equal(ret, false, "VLAN enabled for interface 3");
593
594 iface = eth_interfaces[0];
595 ret = net_eth_vlan_disable(iface, NET_VLAN_TAG_UNSPEC);
596 zassert_equal(ret, -EBADF, "Invalid VLAN tag value %d\n", ret);
597
598 iface = eth_interfaces[1];
599 ret = net_eth_vlan_disable(iface, VLAN_TAG_1);
600 zassert_equal(ret, -ESRCH, "VLAN tag %d disabled for iface 1\n",
601 VLAN_TAG_1);
602 }
603
test_vlan_enable_all(void)604 static void test_vlan_enable_all(void)
605 {
606 struct ethernet_context *eth_ctx;
607 struct net_if *iface;
608 int ret;
609
610 ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_1);
611 zassert_equal(ret, 0, "Cannot enable %d\n", VLAN_TAG_1);
612 ret = net_eth_vlan_enable(eth_interfaces[1], VLAN_TAG_2);
613 zassert_equal(ret, 0, "Cannot enable %d\n", VLAN_TAG_2);
614 ret = net_eth_vlan_enable(eth_interfaces[2], VLAN_TAG_3);
615 zassert_equal(ret, 0, "Cannot enable %d\n", VLAN_TAG_3);
616 ret = net_eth_vlan_enable(eth_interfaces[3], VLAN_TAG_4);
617 zassert_equal(ret, 0, "Cannot enable %d\n", VLAN_TAG_4);
618
619 eth_ctx = net_if_l2_data(eth_interfaces[0]);
620
621 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
622 zassert_equal(ret, true, "VLAN disabled for interface 0");
623
624 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[1]);
625 zassert_equal(ret, true, "VLAN disabled for interface 1");
626
627 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[2]);
628 zassert_equal(ret, true, "VLAN disabled for interface 2");
629
630 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[3]);
631 zassert_equal(ret, true, "VLAN disabled for interface 3");
632
633 iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
634 zassert_not_null(iface, "No dummy iface found");
635
636 zassert_equal(net_if_l2(iface), &NET_L2_GET_NAME(DUMMY),
637 "Not a dummy interface");
638
639 ret = net_eth_vlan_enable(iface, VLAN_TAG_5);
640 zassert_equal(ret, -EINVAL, "Wrong iface type (%d)\n", ret);
641 }
642
test_vlan_disable_all(void)643 static void test_vlan_disable_all(void)
644 {
645 struct ethernet_context *eth_ctx;
646 struct net_if *iface;
647 int ret;
648
649 ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_1);
650 zassert_equal(ret, 0, "Cannot disable %d\n", VLAN_TAG_1);
651 ret = net_eth_vlan_disable(eth_interfaces[1], VLAN_TAG_2);
652 zassert_equal(ret, 0, "Cannot disable %d\n", VLAN_TAG_2);
653 ret = net_eth_vlan_disable(eth_interfaces[2], VLAN_TAG_3);
654 zassert_equal(ret, 0, "Cannot disable %d\n", VLAN_TAG_3);
655 ret = net_eth_vlan_disable(eth_interfaces[3], VLAN_TAG_4);
656 zassert_equal(ret, 0, "Cannot disable %d\n", VLAN_TAG_4);
657
658 eth_ctx = net_if_l2_data(eth_interfaces[0]);
659
660 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
661 zassert_equal(ret, false, "VLAN enabled for interface 0");
662
663 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[1]);
664 zassert_equal(ret, false, "VLAN enabled for interface 1");
665
666 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[2]);
667 zassert_equal(ret, false, "VLAN enabled for interface 2");
668
669 ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[3]);
670 zassert_equal(ret, false, "VLAN enabled for interface 3");
671
672 iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
673 zassert_not_null(iface, "No dummy iface found");
674
675 zassert_equal(net_if_l2(iface), &NET_L2_GET_NAME(DUMMY),
676 "Not a dummy interface");
677
678 ret = net_eth_vlan_disable(iface, VLAN_TAG_5);
679 zassert_equal(ret, -EINVAL, "Wrong iface type (%d)\n", ret);
680 }
681
add_neighbor(struct net_if * iface,struct in6_addr * addr)682 static bool add_neighbor(struct net_if *iface, struct in6_addr *addr)
683 {
684 struct net_linkaddr_storage llstorage;
685 struct net_linkaddr lladdr;
686 struct net_nbr *nbr;
687
688 llstorage.addr[0] = 0x01;
689 llstorage.addr[1] = 0x02;
690 llstorage.addr[2] = 0x33;
691 llstorage.addr[3] = 0x44;
692 llstorage.addr[4] = 0x05;
693 llstorage.addr[5] = 0x06;
694
695 lladdr.len = 6U;
696 lladdr.addr = llstorage.addr;
697 lladdr.type = NET_LINK_ETHERNET;
698
699 nbr = net_ipv6_nbr_add(iface, addr, &lladdr, false,
700 NET_IPV6_NBR_STATE_REACHABLE);
701 if (!nbr) {
702 DBG("Cannot add dst %s to neighbor cache\n",
703 net_sprint_ipv6_addr(addr));
704 return false;
705 }
706
707 return true;
708 }
709
ZTEST(net_vlan,test_vlan_send_data)710 ZTEST(net_vlan, test_vlan_send_data)
711 {
712 struct ethernet_context *eth_ctx; /* This is L2 context */
713 struct eth_context *ctx; /* This is interface context */
714 struct net_if *iface;
715 int ret;
716 struct sockaddr_in6 dst_addr6 = {
717 .sin6_family = AF_INET6,
718 .sin6_port = htons(TEST_PORT),
719 };
720 struct sockaddr_in6 src_addr6 = {
721 .sin6_family = AF_INET6,
722 .sin6_port = 0,
723 };
724
725 /* Setup the interfaces */
726 test_vlan_enable();
727
728 ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP,
729 &udp_v6_ctx);
730 zassert_equal(ret, 0, "Create IPv6 UDP context failed");
731
732 memcpy(&src_addr6.sin6_addr, &my_addr1, sizeof(struct in6_addr));
733 memcpy(&dst_addr6.sin6_addr, &dst_addr, sizeof(struct in6_addr));
734
735 ret = net_context_bind(udp_v6_ctx, (struct sockaddr *)&src_addr6,
736 sizeof(struct sockaddr_in6));
737 zassert_equal(ret, 0, "Context bind failure test failed");
738
739 iface = eth_interfaces[1]; /* This is the VLAN interface */
740 ctx = net_if_get_device(iface)->data;
741 eth_ctx = net_if_l2_data(iface);
742 ret = net_eth_is_vlan_enabled(eth_ctx, iface);
743 zassert_equal(ret, true, "VLAN disabled for interface 1");
744
745 ctx->expecting_tag = VLAN_TAG_1;
746
747 iface = eth_interfaces[3]; /* This is also VLAN interface */
748 ctx = net_if_get_device(iface)->data;
749 eth_ctx = net_if_l2_data(iface);
750 ret = net_eth_is_vlan_enabled(eth_ctx, iface);
751 zassert_equal(ret, true, "VLAN disabled for interface 1");
752
753 test_started = true;
754
755 ret = add_neighbor(iface, &dst_addr);
756 zassert_true(ret, "Cannot add neighbor");
757
758 ret = net_context_sendto(udp_v6_ctx, test_data, strlen(test_data),
759 (struct sockaddr *)&dst_addr6,
760 sizeof(struct sockaddr_in6),
761 NULL, K_NO_WAIT, NULL);
762 zassert_true(ret > 0, "Send UDP pkt failed");
763
764 if (k_sem_take(&wait_data, WAIT_TIME)) {
765 DBG("Timeout while waiting interface data\n");
766 zassert_false(true, "Timeout");
767 }
768
769 net_context_unref(udp_v6_ctx);
770 }
771
setup(void)772 static void *setup(void)
773 {
774 test_vlan_setup();
775 test_address_setup();
776 return NULL;
777 }
778
ZTEST(net_vlan,test_vlan_enable_disable)779 ZTEST(net_vlan, test_vlan_enable_disable)
780 {
781 test_vlan_enable();
782 test_vlan_disable();
783 }
784
ZTEST(net_vlan,test_vlan_enable_disable_all)785 ZTEST(net_vlan, test_vlan_enable_disable_all)
786 {
787 test_vlan_enable_all();
788 test_vlan_disable_all();
789 }
790
791 ZTEST_SUITE(net_vlan, NULL, setup, NULL, NULL, NULL);
792